Tiny3D
Loading...
Searching...
No Matches
t3d.h
Go to the documentation of this file.
1
6#ifndef TINY3D_T3D_H
7#define TINY3D_T3D_H
8
9#include <t3d/t3dmath.h>
10#include <libdragon.h>
11
12#ifdef __cplusplus
13extern "C"
14{
15#endif
16
17#define T3D_VERTEX_CACHE_SIZE 70
18
19extern uint32_t T3D_RSP_ID;
20
21// RSP commands, must match with the commands defined in `rsp/rsp_tiny3d.rspl`
22enum T3DCmd {
23 T3D_CMD_TRI_DRAW = 0x0,
24 T3D_CMD_SCREEN_SIZE = 0x1,
25 T3D_CMD_MATRIX_STACK = 0x2,
26 T3D_CMD_SET_WORD = 0x3,
27 T3D_CMD_VERT_LOAD = 0x4,
28 T3D_CMD_LIGHT_SET = 0x5,
29 T3D_CMD_DRAWFLAGS = 0x6,
30 T3D_CMD_PROJ_SET = 0x7,
31 T3D_CMD_FOG_RANGE = 0x8,
32 T3D_CMD_FOG_STATE = 0x9,
33 T3D_CMD_TRI_SYNC = 0xA,
34 T3D_CMD_TRI_STRIP = 0xB,
35 T3D_CMD_TRI_SEQ = 0xC,
36 // = 0xD,
37 // = 0xE,
38 // = 0xF,
39};
40
41// Internal vertex format, interleaves two vertices
42typedef struct {
43 /* 0x00 */ int16_t posA[3]; // s16 (used in the ucode as the int. part of a s16.16)
44 /* 0x06 */ uint16_t normA; // 5,6,5 packed normal
45 /* 0x08 */ int16_t posB[3]; // s16 (used in the ucode as the int. part of a s16.16)
46 /* 0x0E */ uint16_t normB; // 5,6,5 packed normal
47 /* 0x10 */ uint32_t rgbaA; // RGBA8 color
48 /* 0x14 */ uint32_t rgbaB; // RGBA8 color
49 /* 0x18 */ int16_t stA[2]; // UV fixed point 10.5 (pixel coords)
50 /* 0x1C */ int16_t stB[2]; // UV fixed point 10.5 (pixel coords)
51} __attribute__((aligned(8))) T3DVertPacked;
52
53_Static_assert(sizeof(T3DVertPacked) == 0x20, "T3DVertPacked has wrong size");
54
55enum T3DDrawFlags {
56 T3D_FLAG_DEPTH = 1 << 0,
57 T3D_FLAG_TEXTURED = 1 << 1,
58 T3D_FLAG_SHADED = 1 << 2,
59 T3D_FLAG_CULL_FRONT = 1 << 3,
60 T3D_FLAG_CULL_BACK = 1 << 4,
61 T3D_FLAG_NO_LIGHT = 1 << 16,
62};
63
64// Segment addresses, some are used internally but can be set by the user too
65enum T3DSegment {
66 T3D_SEGMENT_1 = 1,
67 T3D_SEGMENT_2 = 2,
68 T3D_SEGMENT_3 = 3,
69 T3D_SEGMENT_4 = 4,
70 T3D_SEGMENT_5 = 5,
71 T3D_SEGMENT_6 = 6,
72 T3D_SEGMENT_SKELETON = 7,
73};
74
75// Vertex effect functions
76enum T3DVertexFX {
77 T3D_VERTEX_FX_NONE = 0,
78 T3D_VERTEX_FX_SPHERICAL_UV = 1,
79 T3D_VERTEX_FX_CELSHADE_COLOR = 2,
80 T3D_VERTEX_FX_CELSHADE_ALPHA = 3,
81 T3D_VERTEX_FX_OUTLINE = 4,
82 T3D_VERTEX_FX_UV_OFFSET = 5,
83};
84
91typedef struct {
92 T3DMat4FP _matCameraFP [[deprecated("Use t3d_viewport_set_view_matrix()")]];
93 T3DMat4FP _matProjFP [[deprecated("Use t3d_viewport_set_projection_matrix()")]];
94
95 T3DMat4 matCamera; // view matrix, can be set via `t3d_viewport_look_at`
96 T3DMat4 matProj; // projection matrix, can be set via `t3d_viewport_set_projection`
97
98 T3DMat4 matCamProj; // combined view-projection matrix, calculated automatically on demand
99 T3DFrustum viewFrustum; // frustum, calculated from the combined matrix
100 bool _isCamProjDirty; // flag to indicate if the combined matrix needs to be recalculated
101
102 int32_t offset[2]; // screen offset in pixel, [0,0] by default
103 int32_t size[2]; // screen size in pixel, same as the allocated framebuffer size by default
104 int guardBandScale; // guard band for clipping, 2 by default
105 int useRejection; // use rejection instead of clipping, false by default
106
107 float _normScaleW; // factor to normalize W in the ucode, set automatically
108
109 uint16_t _bufferCount; // amount of matrices / framebuffers
110 uint16_t _bufferIdx; // current buffer index
111 T3DMat4FP *_matFP; // stores both camera and projection matrices
113
117typedef struct {
118 // Internal matrix stack size, must be at least 2.
119 // If set two zero, 8 will be used by default.
120 int matrixStackSize;
122
127void t3d_init(T3DInitParams params);
128
132void t3d_destroy(void);
133
137void t3d_frame_start(void);
138
139
141void t3d_screen_clear_color(color_t color);
142
145
157 return (T3DViewport){
158 ._isCamProjDirty = true,
159 .offset = {0, 0},
160 .size = {(int32_t)display_get_width(), (int32_t)display_get_height()},
161 .guardBandScale = 2,
162 .useRejection = false,
163 ._bufferCount = 0,
164 ._matFP = NULL,
165 };
166}
167
178inline static T3DViewport t3d_viewport_create_buffered(uint16_t count) {
180 vp._bufferCount = count;
181 vp._bufferIdx = 0;
182 vp._matFP = (T3DMat4FP*)malloc_uncached(sizeof(T3DMat4FP) * 2 * count);
183 return vp;
184}
185
194inline static void t3d_viewport_destroy(T3DViewport *viewport) {
195 if(viewport->_matFP) {
196 free_uncached(viewport->_matFP);
197 viewport->_matFP = NULL;
198 viewport->_bufferCount = 0;
199 viewport->_bufferIdx = 0;
200 }
201}
202
212void t3d_viewport_attach(T3DViewport *viewport);
213
219
228inline static void t3d_viewport_set_area(T3DViewport *viewport, int32_t x, int32_t y, int32_t width, int32_t height) {
229 viewport->offset[0] = x;
230 viewport->offset[1] = y;
231 viewport->size[0] = width;
232 viewport->size[1] = height;
233}
234
245void t3d_viewport_set_perspective(T3DViewport *viewport, float fov, float aspectRatio, float near, float far);
246
258void t3d_viewport_set_projection(T3DViewport *viewport, float fov, float near, float far);
259
271void t3d_viewport_set_ortho(T3DViewport *viewport, float left, float right, float bottom, float top, float near, float far);
272
282inline static void t3d_viewport_set_w_normalize(T3DViewport *viewport, float near, float far) {
283 viewport->_normScaleW = 2.0f / (far + near);
284}
285
294void t3d_viewport_look_at(T3DViewport *viewport, const T3DVec3 *eye, const T3DVec3 *target, const T3DVec3 *up);
295
303void t3d_viewport_set_view_matrix(T3DViewport *viewport, const T3DMat4 *mat);
304
313void t3d_viewport_set_projection_matrix(T3DViewport *viewport, const T3DMat4 *mat);
314
323void t3d_viewport_calc_viewspace_pos(T3DViewport *viewport, T3DVec3 *out, const T3DVec3 *pos);
324
331void t3d_tri_draw(uint32_t v0, uint32_t v1, uint32_t v2);
332
344void t3d_tri_draw_unindexed(uint32_t baseIndex, uint32_t triCount);
345
357void t3d_quad_draw_unindexed(uint32_t baseIndex, uint32_t quadCount);
358
359
378void t3d_tri_draw_strip(int16_t* indexBuff, int count);
379
387void t3d_tri_draw_strip_and_sync(int16_t* indexBuff, int count);
388
394inline static void t3d_tri_sync() {
395 rspq_write(T3D_RSP_ID, T3D_CMD_TRI_SYNC, 0);
396}
397
407void t3d_matrix_set(const T3DMat4FP *mat, bool doMultiply);
408
413void t3d_matrix_push(const T3DMat4FP *mat);
414
419void t3d_matrix_pop(int count);
420
433void t3d_matrix_push_pos(int count);
434
439void t3d_matrix_set_proj(const T3DMat4FP *mat);
440
448void t3d_vert_load(const T3DVertPacked *vertices, uint32_t offset, uint32_t count);
449
456void t3d_light_set_ambient(const uint8_t *color);
457
467void t3d_light_set_directional(int index, const uint8_t *color, const T3DVec3 *dir);
468
486void t3d_light_set_point(int index, const uint8_t *color, const T3DVec3 *pos, float size, bool ignoreNormals);
487
493void t3d_light_set_count(int count);
494
505void t3d_light_set_exposure(float exposure);
506
515void t3d_fog_set_range(float near, float far);
516
521static inline void t3d_fog_set_enabled(bool isEnabled) {
522 // 0xB/0xC are the offsets of attributes (color/UV) in a vertex on the RSP side
523 // this allows the code to do a branch-less save.
524 // 0xB points to alpha of the current vertex, 0xC to the UV which get overwritten later
525 rspq_write(T3D_RSP_ID, T3D_CMD_FOG_STATE, isEnabled ? 0x0B : 0x0C);
526}
527
533uint16_t t3d_vert_pack_normal(const T3DVec3 *normal);
534
539void t3d_state_set_drawflags(enum T3DDrawFlags drawFlags);
540
548void t3d_state_set_depth_offset(int16_t offset);
549
567void t3d_state_set_alpha_to_tile(bool enable);
568
586void t3d_state_set_vertex_fx(enum T3DVertexFX func, int16_t arg0, int16_t arg1);
587
596void t3d_state_set_vertex_fx_scale(uint16_t scale);
597
606void t3d_segment_set(uint8_t segmentId, void* address);
607
615static inline void* t3d_segment_placeholder(uint8_t segmentId) {
616 return (void*)(uint32_t)(segmentId << (8*3 + 2));
617}
618
628static inline void* t3d_segment_address(uint8_t segmentId, void* ptr) {
629 return (void*)(PhysicalAddr(ptr) | (segmentId << (8*3 + 2)));
630}
631
632// Index-buffer helpers:
633
656void t3d_indexbuffer_convert(int16_t indices[], int count);
657
658// Vertex-buffer helpers:
659
665static inline int16_t* t3d_vertbuffer_get_pos(T3DVertPacked vert[], int idx) {
666 return (idx & 1) ? vert[idx/2].posB : vert[idx/2].posA;
667}
668
674static inline int16_t* t3d_vertbuffer_get_uv(T3DVertPacked vert[], int idx) {
675 return (idx & 1) ? vert[idx/2].stB : vert[idx/2].stA;
676}
677
683static inline uint32_t* t3d_vertbuffer_get_color(T3DVertPacked vert[], int idx) {
684 return (idx & 1) ? &vert[idx/2].rgbaB : &vert[idx/2].rgbaA;
685}
686
692static inline uint8_t* t3d_vertbuffer_get_rgba(T3DVertPacked vert[], int idx) {
693 return (idx & 1) ? (uint8_t*)&vert[idx/2].rgbaB : (uint8_t*)&vert[idx/2].rgbaA;
694}
695
701static inline uint16_t* t3d_vertbuffer_get_norm(T3DVertPacked vert[], int idx) {
702 return (idx & 1) ? &vert[idx/2].normB : &vert[idx/2].normA;
703}
704
705#ifdef __cplusplus
706}
707#endif
708
709// C++ wrappers that allow (const-)references to be passed instead of pointers
710#ifdef __cplusplus
711
712inline void t3d_viewport_attach(T3DViewport &viewport) { t3d_viewport_attach(&viewport); }
713inline void t3d_viewport_set_area(T3DViewport &viewport, int32_t x, int32_t y, int32_t width, int32_t height) { t3d_viewport_set_area(&viewport, x, y, width, height); }
714inline void t3d_viewport_set_projection(T3DViewport &viewport, float fov, float near, float far) { t3d_viewport_set_projection(&viewport, fov, near, far); }
715inline void t3d_viewport_set_ortho(T3DViewport &viewport, float left, float right, float bottom, float top, float near, float far) { t3d_viewport_set_ortho(&viewport, left, right, bottom, top, near, far); }
716inline void t3d_viewport_set_w_normalize(T3DViewport &viewport, float near, float far) { t3d_viewport_set_w_normalize(&viewport, near, far); }
717inline void t3d_viewport_look_at(T3DViewport &viewport, const T3DVec3 &eye, const T3DVec3 &target, const T3DVec3 &up = T3DVec3{{0,1,0}}) { t3d_viewport_look_at(&viewport, &eye, &target, &up); }
718inline void t3d_viewport_calc_viewspace_pos(T3DViewport &viewport, T3DVec3 &out, const T3DVec3 &pos) { t3d_viewport_calc_viewspace_pos(&viewport, &out, &pos); }
719
720inline void t3d_light_set_directional(int index, const uint8_t *color, const T3DVec3 &dir) { t3d_light_set_directional(index, color, &dir); }
721inline void t3d_light_set_point(int index, const uint8_t *color, const T3DVec3 &pos, float size, bool ignoreNormals = false) { t3d_light_set_point(index, color, &pos, size, ignoreNormals); }
722
723inline void t3d_light_set_ambient(const color_t &color) { t3d_light_set_ambient((const uint8_t*)&color); }
724inline void t3d_light_set_directional(int index, const color_t &color, const T3DVec3 &dir) { t3d_light_set_directional(index, (const uint8_t*)&color, &dir); }
725inline void t3d_light_set_point(int index, const color_t &color, const T3DVec3 &pos, float size, bool ignoreNormals = false) { t3d_light_set_point(index, (const uint8_t*)&color, &pos, size, ignoreNormals); }
726
727#endif
728
729#endif //TINY3D_T3D_H
Definition t3dmath.h:38
Definition t3d.h:117
Definition t3d.h:91
static void t3d_viewport_destroy(T3DViewport *viewport)
Definition t3d.h:194
void t3d_state_set_vertex_fx(enum T3DVertexFX func, int16_t arg0, int16_t arg1)
Definition t3d.c:294
void t3d_destroy(void)
Destroys the tiny3d library.
Definition t3d.c:65
void t3d_vert_load(const T3DVertPacked *vertices, uint32_t offset, uint32_t count)
Definition t3d.c:125
void t3d_viewport_attach(T3DViewport *viewport)
Definition t3d.c:440
static uint32_t * t3d_vertbuffer_get_color(T3DVertPacked vert[], int idx)
Definition t3d.h:683
void t3d_indexbuffer_convert(int16_t indices[], int count)
Definition t3d.c:594
void t3d_light_set_count(int count)
Definition t3d.c:162
static uint16_t * t3d_vertbuffer_get_norm(T3DVertPacked vert[], int idx)
Definition t3d.h:701
void t3d_viewport_calc_viewspace_pos(T3DViewport *viewport, T3DVec3 *out, const T3DVec3 *pos)
Definition t3d.c:564
void t3d_tri_draw_strip_and_sync(int16_t *indexBuff, int count)
Definition t3d.c:408
void t3d_state_set_alpha_to_tile(bool enable)
Definition t3d.c:287
void t3d_light_set_ambient(const uint8_t *color)
Definition t3d.c:173
static int16_t * t3d_vertbuffer_get_uv(T3DVertPacked vert[], int idx)
Definition t3d.h:674
static void * t3d_segment_address(uint8_t segmentId, void *ptr)
Definition t3d.h:628
void t3d_viewport_set_view_matrix(T3DViewport *viewport, const T3DMat4 *mat)
Definition t3d.c:548
static T3DViewport t3d_viewport_create_buffered(uint16_t count)
Definition t3d.h:178
static uint8_t * t3d_vertbuffer_get_rgba(T3DVertPacked vert[], int idx)
Definition t3d.h:692
uint16_t t3d_vert_pack_normal(const T3DVec3 *normal)
Definition t3d.c:242
void t3d_matrix_pop(int count)
Definition t3d.c:111
void t3d_viewport_set_perspective(T3DViewport *viewport, float fov, float aspectRatio, float near, float far)
Definition t3d.c:523
void t3d_viewport_set_ortho(T3DViewport *viewport, float left, float right, float bottom, float top, float near, float far)
Definition t3d.c:534
void t3d_segment_set(uint8_t segmentId, void *address)
Definition t3d.c:341
void t3d_tri_draw(uint32_t v0, uint32_t v1, uint32_t v2)
Draws a single triangle, referencing loaded vertices.
Definition t3d.c:349
void t3d_tri_draw_strip(int16_t *indexBuff, int count)
Definition t3d.c:403
void t3d_init(T3DInitParams params)
Initializes the tiny3d library.
Definition t3d.c:36
void t3d_matrix_push(const T3DMat4FP *mat)
Definition t3d.c:107
static void t3d_viewport_set_w_normalize(T3DViewport *viewport, float near, float far)
Definition t3d.h:282
void t3d_screen_clear_color(color_t color)
Clears the entire screen with a given color.
Definition t3d.c:79
void t3d_state_set_depth_offset(int16_t offset)
Definition t3d.c:283
void t3d_state_set_drawflags(enum T3DDrawFlags drawFlags)
Definition t3d.c:259
void t3d_matrix_push_pos(int count)
Definition t3d.c:116
static void t3d_tri_sync()
Definition t3d.h:394
void t3d_frame_start(void)
Starts a new frame, this will setup some default states.
Definition t3d.c:146
void t3d_screen_clear_depth()
Clears the entire depth buffer with a fixed value (0xFFFC)
Definition t3d.c:83
void t3d_fog_set_range(float near, float far)
Definition t3d.c:413
void t3d_light_set_exposure(float exposure)
Definition t3d.c:167
void t3d_viewport_look_at(T3DViewport *viewport, const T3DVec3 *eye, const T3DVec3 *target, const T3DVec3 *up)
Definition t3d.c:540
void t3d_state_set_vertex_fx_scale(uint16_t scale)
Definition t3d.c:336
void t3d_viewport_set_projection(T3DViewport *viewport, float fov, float near, float far)
Definition t3d.c:529
static void t3d_viewport_set_area(T3DViewport *viewport, int32_t x, int32_t y, int32_t width, int32_t height)
Definition t3d.h:228
void t3d_quad_draw_unindexed(uint32_t baseIndex, uint32_t quadCount)
Definition t3d.c:385
void t3d_matrix_set_proj(const T3DMat4FP *mat)
Definition t3d.c:121
static void * t3d_segment_placeholder(uint8_t segmentId)
Definition t3d.h:615
void t3d_light_set_directional(int index, const uint8_t *color, const T3DVec3 *dir)
Definition t3d.c:182
void t3d_matrix_set(const T3DMat4FP *mat, bool doMultiply)
Definition t3d.c:103
static T3DViewport t3d_viewport_create()
Definition t3d.h:156
static int16_t * t3d_vertbuffer_get_pos(T3DVertPacked vert[], int idx)
Definition t3d.h:665
static void t3d_fog_set_enabled(bool isEnabled)
Definition t3d.h:521
void t3d_light_set_point(int index, const uint8_t *color, const T3DVec3 *pos, float size, bool ignoreNormals)
Definition t3d.c:206
void t3d_viewport_set_projection_matrix(T3DViewport *viewport, const T3DMat4 *mat)
Definition t3d.c:556
T3DViewport * t3d_viewport_get()
Definition t3d.c:519
void t3d_tri_draw_unindexed(uint32_t baseIndex, uint32_t triCount)
Definition t3d.c:381
__attribute__((deprecated)) void t3d_debug_print_init()
Initializes the debug print system, make sure to have 'font.ia4.png' in your FS.