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
53static_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
127typedef struct {
128 uint32_t trisPreCull;
129 uint32_t trisPostCull;
130} T3DMetrics;
131
136void t3d_init(T3DInitParams params);
137
141void t3d_destroy(void);
142
146void t3d_frame_start(void);
147
148
150void t3d_screen_clear_color(color_t color);
151
154
162void t3d_metrics_fetch(T3DMetrics* data);
163
175 return (T3DViewport){
176 ._isCamProjDirty = true,
177 .offset = {0, 0},
178 .size = {(int32_t)display_get_width(), (int32_t)display_get_height()},
179 .guardBandScale = 2,
180 .useRejection = false,
181 ._bufferCount = 0,
182 ._matFP = NULL,
183 };
184}
185
196inline static T3DViewport t3d_viewport_create_buffered(uint16_t count) {
198 vp._bufferCount = count;
199 vp._bufferIdx = 0;
200 vp._matFP = (T3DMat4FP*)malloc_uncached(sizeof(T3DMat4FP) * 2 * count);
201 return vp;
202}
203
212inline static void t3d_viewport_destroy(T3DViewport *viewport) {
213 if(viewport->_matFP) {
214 free_uncached(viewport->_matFP);
215 viewport->_matFP = NULL;
216 viewport->_bufferCount = 0;
217 viewport->_bufferIdx = 0;
218 }
219}
220
230void t3d_viewport_attach(T3DViewport *viewport);
231
237
246inline static void t3d_viewport_set_area(T3DViewport *viewport, int32_t x, int32_t y, int32_t width, int32_t height) {
247 viewport->offset[0] = x;
248 viewport->offset[1] = y;
249 viewport->size[0] = width;
250 viewport->size[1] = height;
251}
252
263void t3d_viewport_set_perspective(T3DViewport *viewport, float fov, float aspectRatio, float near, float far);
264
276void t3d_viewport_set_projection(T3DViewport *viewport, float fov, float near, float far);
277
289void t3d_viewport_set_ortho(T3DViewport *viewport, float left, float right, float bottom, float top, float near, float far);
290
300inline static void t3d_viewport_set_w_normalize(T3DViewport *viewport, float near, float far) {
301 viewport->_normScaleW = 2.0f / (far + near);
302}
303
312void t3d_viewport_look_at(T3DViewport *viewport, const T3DVec3 *eye, const T3DVec3 *target, const T3DVec3 *up);
313
321void t3d_viewport_set_view_matrix(T3DViewport *viewport, const T3DMat4 *mat);
322
331void t3d_viewport_set_projection_matrix(T3DViewport *viewport, const T3DMat4 *mat);
332
341void t3d_viewport_calc_viewspace_pos(T3DViewport *viewport, T3DVec3 *out, const T3DVec3 *pos);
342
349void t3d_tri_draw(uint32_t v0, uint32_t v1, uint32_t v2);
350
362void t3d_tri_draw_unindexed(uint32_t baseIndex, uint32_t triCount);
363
375void t3d_quad_draw_unindexed(uint32_t baseIndex, uint32_t quadCount);
376
377
396void t3d_tri_draw_strip(int16_t* indexBuff, int count);
397
405void t3d_tri_draw_strip_and_sync(int16_t* indexBuff, int count);
406
412inline static void t3d_tri_sync() {
413 rspq_write(T3D_RSP_ID, T3D_CMD_TRI_SYNC, 0);
414}
415
425void t3d_matrix_set(const T3DMat4FP *mat, bool doMultiply);
426
431void t3d_matrix_push(const T3DMat4FP *mat);
432
437void t3d_matrix_pop(int count);
438
451void t3d_matrix_push_pos(int count);
452
457void t3d_matrix_set_proj(const T3DMat4FP *mat);
458
466void t3d_vert_load(const T3DVertPacked *vertices, uint32_t offset, uint32_t count);
467
474void t3d_light_set_ambient(const uint8_t *color);
475
485void t3d_light_set_directional(int index, const uint8_t *color, const T3DVec3 *dir);
486
504void t3d_light_set_point(int index, const uint8_t *color, const T3DVec3 *pos, float size, bool ignoreNormals);
505
511void t3d_light_set_count(int count);
512
523void t3d_light_set_exposure(float exposure);
524
533void t3d_fog_set_range(float near, float far);
534
539static inline void t3d_fog_set_enabled(bool isEnabled) {
540 // 0xB/0xC are the offsets of attributes (color/UV) in a vertex on the RSP side
541 // this allows the code to do a branch-less save.
542 // 0xB points to alpha of the current vertex, 0xC to the UV which get overwritten later
543 rspq_write(T3D_RSP_ID, T3D_CMD_FOG_STATE, isEnabled ? 0x0B : 0x0C);
544}
545
551uint16_t t3d_vert_pack_normal(const T3DVec3 *normal);
552
557void t3d_state_set_drawflags(enum T3DDrawFlags drawFlags);
558
566void t3d_state_set_depth_offset(int16_t offset);
567
585void t3d_state_set_alpha_to_tile(bool enable);
586
604void t3d_state_set_vertex_fx(enum T3DVertexFX func, int16_t arg0, int16_t arg1);
605
614void t3d_state_set_vertex_fx_scale(uint16_t scale);
615
624void t3d_segment_set(uint8_t segmentId, void* address);
625
633static inline void* t3d_segment_placeholder(uint8_t segmentId) {
634 return (void*)(uint32_t)(segmentId << (8*3 + 2));
635}
636
646static inline void* t3d_segment_address(uint8_t segmentId, void* ptr) {
647 return (void*)(PhysicalAddr(ptr) | (segmentId << (8*3 + 2)));
648}
649
650// Index-buffer helpers:
651
674void t3d_indexbuffer_convert(int16_t indices[], int count);
675
676// Vertex-buffer helpers:
677
683static inline int16_t* t3d_vertbuffer_get_pos(T3DVertPacked vert[], int idx) {
684 return (idx & 1) ? vert[idx/2].posB : vert[idx/2].posA;
685}
686
692static inline int16_t* t3d_vertbuffer_get_uv(T3DVertPacked vert[], int idx) {
693 return (idx & 1) ? vert[idx/2].stB : vert[idx/2].stA;
694}
695
701static inline uint32_t* t3d_vertbuffer_get_color(T3DVertPacked vert[], int idx) {
702 return (idx & 1) ? &vert[idx/2].rgbaB : &vert[idx/2].rgbaA;
703}
704
710static inline uint8_t* t3d_vertbuffer_get_rgba(T3DVertPacked vert[], int idx) {
711 return (idx & 1) ? (uint8_t*)&vert[idx/2].rgbaB : (uint8_t*)&vert[idx/2].rgbaA;
712}
713
719static inline uint16_t* t3d_vertbuffer_get_norm(T3DVertPacked vert[], int idx) {
720 return (idx & 1) ? &vert[idx/2].normB : &vert[idx/2].normA;
721}
722
723#ifdef __cplusplus
724}
725#endif
726
727// C++ wrappers that allow (const-)references to be passed instead of pointers
728#ifdef __cplusplus
729
730inline void t3d_viewport_attach(T3DViewport &viewport) { t3d_viewport_attach(&viewport); }
731inline 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); }
732inline void t3d_viewport_set_projection(T3DViewport &viewport, float fov, float near, float far) { t3d_viewport_set_projection(&viewport, fov, near, far); }
733inline 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); }
734inline void t3d_viewport_set_w_normalize(T3DViewport &viewport, float near, float far) { t3d_viewport_set_w_normalize(&viewport, near, far); }
735inline 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); }
736inline void t3d_viewport_calc_viewspace_pos(T3DViewport &viewport, T3DVec3 &out, const T3DVec3 &pos) { t3d_viewport_calc_viewspace_pos(&viewport, &out, &pos); }
737
738inline void t3d_light_set_directional(int index, const uint8_t *color, const T3DVec3 &dir) { t3d_light_set_directional(index, color, &dir); }
739inline 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); }
740
741inline void t3d_light_set_ambient(const color_t &color) { t3d_light_set_ambient((const uint8_t*)&color); }
742inline void t3d_light_set_directional(int index, const color_t &color, const T3DVec3 &dir) { t3d_light_set_directional(index, (const uint8_t*)&color, &dir); }
743inline 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); }
744
745#endif
746
747#endif //TINY3D_T3D_H
Definition t3dmath.h:38
Definition t3d.h:117
Definition t3d.h:127
Definition t3d.h:91
static void t3d_viewport_destroy(T3DViewport *viewport)
Definition t3d.h:212
void t3d_state_set_vertex_fx(enum T3DVertexFX func, int16_t arg0, int16_t arg1)
Definition t3d.c:314
void t3d_destroy(void)
Destroys the tiny3d library.
Definition t3d.c:66
void t3d_vert_load(const T3DVertPacked *vertices, uint32_t offset, uint32_t count)
Definition t3d.c:134
void t3d_viewport_attach(T3DViewport *viewport)
Definition t3d.c:460
static uint32_t * t3d_vertbuffer_get_color(T3DVertPacked vert[], int idx)
Definition t3d.h:701
void t3d_indexbuffer_convert(int16_t indices[], int count)
Definition t3d.c:623
void t3d_light_set_count(int count)
Definition t3d.c:171
static uint16_t * t3d_vertbuffer_get_norm(T3DVertPacked vert[], int idx)
Definition t3d.h:719
void t3d_viewport_calc_viewspace_pos(T3DViewport *viewport, T3DVec3 *out, const T3DVec3 *pos)
Definition t3d.c:593
void t3d_tri_draw_strip_and_sync(int16_t *indexBuff, int count)
Definition t3d.c:428
void t3d_state_set_alpha_to_tile(bool enable)
Definition t3d.c:307
void t3d_light_set_ambient(const uint8_t *color)
Definition t3d.c:185
static int16_t * t3d_vertbuffer_get_uv(T3DVertPacked vert[], int idx)
Definition t3d.h:692
static void * t3d_segment_address(uint8_t segmentId, void *ptr)
Definition t3d.h:646
void t3d_viewport_set_view_matrix(T3DViewport *viewport, const T3DMat4 *mat)
Definition t3d.c:577
static T3DViewport t3d_viewport_create_buffered(uint16_t count)
Definition t3d.h:196
static uint8_t * t3d_vertbuffer_get_rgba(T3DVertPacked vert[], int idx)
Definition t3d.h:710
uint16_t t3d_vert_pack_normal(const T3DVec3 *normal)
Definition t3d.c:262
void t3d_matrix_pop(int count)
Definition t3d.c:120
void t3d_viewport_set_perspective(T3DViewport *viewport, float fov, float aspectRatio, float near, float far)
Definition t3d.c:552
void t3d_viewport_set_ortho(T3DViewport *viewport, float left, float right, float bottom, float top, float near, float far)
Definition t3d.c:563
void t3d_segment_set(uint8_t segmentId, void *address)
Definition t3d.c:361
void t3d_tri_draw(uint32_t v0, uint32_t v1, uint32_t v2)
Draws a single triangle, referencing loaded vertices.
Definition t3d.c:369
void t3d_tri_draw_strip(int16_t *indexBuff, int count)
Definition t3d.c:423
void t3d_init(T3DInitParams params)
Initializes the tiny3d library.
Definition t3d.c:37
void t3d_matrix_push(const T3DMat4FP *mat)
Definition t3d.c:116
static void t3d_viewport_set_w_normalize(T3DViewport *viewport, float near, float far)
Definition t3d.h:300
void t3d_screen_clear_color(color_t color)
Clears the entire screen with a given color.
Definition t3d.c:80
void t3d_state_set_depth_offset(int16_t offset)
Definition t3d.c:303
void t3d_state_set_drawflags(enum T3DDrawFlags drawFlags)
Definition t3d.c:279
void t3d_matrix_push_pos(int count)
Definition t3d.c:125
static void t3d_tri_sync()
Definition t3d.h:412
void t3d_frame_start(void)
Starts a new frame, this will setup some default states.
Definition t3d.c:155
void t3d_screen_clear_depth()
Clears the entire depth buffer with a fixed value (0xFFFC)
Definition t3d.c:84
void t3d_fog_set_range(float near, float far)
Definition t3d.c:433
void t3d_light_set_exposure(float exposure)
Definition t3d.c:179
void t3d_viewport_look_at(T3DViewport *viewport, const T3DVec3 *eye, const T3DVec3 *target, const T3DVec3 *up)
Definition t3d.c:569
void t3d_state_set_vertex_fx_scale(uint16_t scale)
Definition t3d.c:356
void t3d_viewport_set_projection(T3DViewport *viewport, float fov, float near, float far)
Definition t3d.c:558
static void t3d_viewport_set_area(T3DViewport *viewport, int32_t x, int32_t y, int32_t width, int32_t height)
Definition t3d.h:246
void t3d_quad_draw_unindexed(uint32_t baseIndex, uint32_t quadCount)
Definition t3d.c:405
void t3d_matrix_set_proj(const T3DMat4FP *mat)
Definition t3d.c:130
static void * t3d_segment_placeholder(uint8_t segmentId)
Definition t3d.h:633
void t3d_light_set_directional(int index, const uint8_t *color, const T3DVec3 *dir)
Definition t3d.c:194
void t3d_matrix_set(const T3DMat4FP *mat, bool doMultiply)
Definition t3d.c:112
static T3DViewport t3d_viewport_create()
Definition t3d.h:174
static int16_t * t3d_vertbuffer_get_pos(T3DVertPacked vert[], int idx)
Definition t3d.h:683
static void t3d_fog_set_enabled(bool isEnabled)
Definition t3d.h:539
void t3d_light_set_point(int index, const uint8_t *color, const T3DVec3 *pos, float size, bool ignoreNormals)
Definition t3d.c:222
void t3d_viewport_set_projection_matrix(T3DViewport *viewport, const T3DMat4 *mat)
Definition t3d.c:585
void t3d_metrics_fetch(T3DMetrics *data)
Definition t3d.c:104
T3DViewport * t3d_viewport_get()
Definition t3d.c:548
void t3d_tri_draw_unindexed(uint32_t baseIndex, uint32_t triCount)
Definition t3d.c:401
__attribute__((deprecated)) void t3d_debug_print_init()
Initializes the debug print system, make sure to have 'font.ia4.png' in your FS.