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
19#define T3D_LIB_VER_MAJOR 1
20#define T3D_LIB_VER_MINOR 0
21#define T3D_LIB_VER_PATCH 0
22
23extern uint32_t T3D_RSP_ID;
24
25// RSP commands, must match with the commands defined in `rsp/rsp_tiny3d.rspl`
26enum T3DCmd {
27 T3D_CMD_TRI_DRAW = 0x0,
28 T3D_CMD_SCREEN_SIZE = 0x1,
29 T3D_CMD_MATRIX_STACK = 0x2,
30 T3D_CMD_SET_WORD = 0x3,
31 T3D_CMD_VERT_LOAD = 0x4,
32 T3D_CMD_LIGHT_SET = 0x5,
33 T3D_CMD_DRAWFLAGS = 0x6,
34 T3D_CMD_PROJ_SET = 0x7,
35 T3D_CMD_PATCH = 0x8,
36 T3D_CMD_FOG_STATE = 0x9,
37 T3D_CMD_TRI_SYNC = 0xA,
38 T3D_CMD_TRI_STRIP = 0xB,
39 T3D_CMD_TRI_SEQ = 0xC,
40 // = 0xD,
41 // = 0xE,
42 // = 0xF,
43};
44
45// Internal vertex format, interleaves two vertices
46typedef struct {
47 /* 0x00 */ int16_t posA[3]; // s16 (used in the ucode as the int. part of a s16.16)
48 /* 0x06 */ uint16_t normA; // 5,6,5 packed normal
49 /* 0x08 */ int16_t posB[3]; // s16 (used in the ucode as the int. part of a s16.16)
50 /* 0x0E */ uint16_t normB; // 5,6,5 packed normal
51 /* 0x10 */ uint32_t rgbaA; // RGBA8 color
52 /* 0x14 */ uint32_t rgbaB; // RGBA8 color
53 /* 0x18 */ int16_t stA[2]; // UV fixed point 10.5 (pixel coords)
54 /* 0x1C */ int16_t stB[2]; // UV fixed point 10.5 (pixel coords)
55} __attribute__((aligned(8))) T3DVertPacked;
56
57static_assert(sizeof(T3DVertPacked) == 0x20, "T3DVertPacked has wrong size");
58
59enum T3DDrawFlags {
60 T3D_FLAG_DEPTH = 1 << 0,
61 T3D_FLAG_TEXTURED = 1 << 1,
62 T3D_FLAG_SHADED = 1 << 2,
63 T3D_FLAG_CULL_FRONT = 1 << 3,
64 T3D_FLAG_CULL_BACK = 1 << 4,
65 T3D_FLAG_NO_LIGHT = 1 << 16,
66};
67
68// Segment addresses, some are used internally but can be set by the user too
69enum T3DSegment {
70 T3D_SEGMENT_1 = 1,
71 T3D_SEGMENT_2 = 2,
72 T3D_SEGMENT_3 = 3,
73 T3D_SEGMENT_4 = 4,
74 T3D_SEGMENT_5 = 5,
75 T3D_SEGMENT_6 = 6,
76 T3D_SEGMENT_SKELETON = 7,
77};
78
79// Vertex effect functions
80enum T3DVertexFX {
81 T3D_VERTEX_FX_NONE = 0,
82 T3D_VERTEX_FX_SPHERICAL_UV = 1,
83 T3D_VERTEX_FX_CELSHADE_COLOR = 2,
84 T3D_VERTEX_FX_CELSHADE_ALPHA = 3,
85 T3D_VERTEX_FX_OUTLINE = 4,
86 T3D_VERTEX_FX_UV_OFFSET = 5,
87};
88
89// computation to use to combine lighting and vertex color
90enum T3DLightingMode
91{
92 T3D_LIGHTING_MODE_MUL = 0,
93 T3D_LIGHTING_MODE_ADD = 1,
94 T3D_LIGHTING_MODE_REPLACE = 2,
95};
96
103typedef struct {
104 T3DMat4FP _matCameraFP [[deprecated("Use t3d_viewport_set_view_matrix()")]];
105 T3DMat4FP _matProjFP [[deprecated("Use t3d_viewport_set_projection_matrix()")]];
106
107 T3DMat4 matCamera; // view matrix, can be set via `t3d_viewport_look_at`
108 T3DMat4 matProj; // projection matrix, can be set via `t3d_viewport_set_projection`
109
110 T3DMat4 matCamProj; // combined view-projection matrix, calculated automatically on demand
111 T3DFrustum viewFrustum; // frustum, calculated from the combined matrix
112 bool _isCamProjDirty; // flag to indicate if the combined matrix needs to be recalculated
113
114 int32_t offset[2]; // screen offset in pixel, [0,0] by default
115 int32_t size[2]; // screen size in pixel, same as the allocated framebuffer size by default
116 int guardBandScale; // guard band for clipping, 2 by default
117 int useRejection; // use rejection instead of clipping, false by default
118
119 float _normScaleW; // factor to normalize W in the ucode, set automatically
120
121 uint16_t _bufferCount; // amount of matrices / framebuffers
122 uint16_t _bufferIdx; // current buffer index
123 T3DMat4FP *_matFP; // stores both camera and projection matrices
125
129typedef struct {
130 // Internal matrix stack size, must be at least 2.
131 // If set two zero, 8 will be used by default.
132 int matrixStackSize;
134
139typedef struct {
140 uint32_t trisPreCull;
141 uint32_t trisPostCull;
142} T3DMetrics;
143
148void t3d_init(T3DInitParams params);
149
153void t3d_destroy(void);
154
158void t3d_frame_start(void);
159
160
162void t3d_screen_clear_color(color_t color);
163
166
174void t3d_metrics_fetch(T3DMetrics* data);
175
187 return (T3DViewport){
188 ._isCamProjDirty = true,
189 .offset = {0, 0},
190 .size = {(int32_t)display_get_width(), (int32_t)display_get_height()},
191 .guardBandScale = 2,
192 .useRejection = false,
193 ._bufferCount = 0,
194 ._matFP = NULL,
195 };
196}
197
208inline static T3DViewport t3d_viewport_create_buffered(uint16_t count) {
210 vp._bufferCount = count;
211 vp._bufferIdx = 0;
212 vp._matFP = (T3DMat4FP*)malloc_uncached(sizeof(T3DMat4FP) * 2 * count);
213 return vp;
214}
215
224inline static void t3d_viewport_destroy(T3DViewport *viewport) {
225 if(viewport->_matFP) {
226 free_uncached(viewport->_matFP);
227 viewport->_matFP = NULL;
228 viewport->_bufferCount = 0;
229 viewport->_bufferIdx = 0;
230 }
231}
232
242void t3d_viewport_attach(T3DViewport *viewport);
243
249
258inline static void t3d_viewport_set_area(T3DViewport *viewport, int32_t x, int32_t y, int32_t width, int32_t height) {
259 viewport->offset[0] = x;
260 viewport->offset[1] = y;
261 viewport->size[0] = width;
262 viewport->size[1] = height;
263}
264
275void t3d_viewport_set_perspective(T3DViewport *viewport, float fov, float aspectRatio, float near, float far);
276
288void t3d_viewport_set_projection(T3DViewport *viewport, float fov, float near, float far);
289
301void t3d_viewport_set_ortho(T3DViewport *viewport, float left, float right, float bottom, float top, float near, float far);
302
312inline static void t3d_viewport_set_w_normalize(T3DViewport *viewport, float near, float far) {
313 viewport->_normScaleW = 2.0f / (far + near);
314}
315
324void t3d_viewport_look_at(T3DViewport *viewport, const T3DVec3 *eye, const T3DVec3 *target, const T3DVec3 *up);
325
333void t3d_viewport_set_view_matrix(T3DViewport *viewport, const T3DMat4 *mat);
334
343void t3d_viewport_set_projection_matrix(T3DViewport *viewport, const T3DMat4 *mat);
344
353void t3d_viewport_calc_viewspace_pos(T3DViewport *viewport, T3DVec3 *out, const T3DVec3 *pos);
354
361void t3d_tri_draw(uint32_t v0, uint32_t v1, uint32_t v2);
362
374void t3d_tri_draw_unindexed(uint32_t baseIndex, uint32_t triCount);
375
387void t3d_quad_draw_unindexed(uint32_t baseIndex, uint32_t quadCount);
388
389
408void t3d_tri_draw_strip(int16_t* indexBuff, int count);
409
417void t3d_tri_draw_strip_and_sync(int16_t* indexBuff, int count);
418
424inline static void t3d_tri_sync() {
425 rspq_write(T3D_RSP_ID, T3D_CMD_TRI_SYNC, 0);
426}
427
437void t3d_matrix_set(const T3DMat4FP *mat, bool doMultiply);
438
443void t3d_matrix_push(const T3DMat4FP *mat);
444
449void t3d_matrix_pop(int count);
450
463void t3d_matrix_push_pos(int count);
464
469void t3d_matrix_set_proj(const T3DMat4FP *mat);
470
478void t3d_vert_load(const T3DVertPacked *vertices, uint32_t offset, uint32_t count);
479
486void t3d_light_set_ambient(const uint8_t *color);
487
497void t3d_light_set_directional(int index, const uint8_t *color, const T3DVec3 *dir);
498
516void t3d_light_set_point(int index, const uint8_t *color, const T3DVec3 *pos, float size, bool ignoreNormals);
517
523void t3d_light_set_count(int count);
524
535void t3d_light_set_exposure(float exposure);
536
546void t3d_fog_set_range(float near, float far);
547
552static inline void t3d_fog_set_enabled(bool isEnabled) {
553 // 0xB/0xC are the offsets of attributes (color/UV) in a vertex on the RSP side
554 // this allows the code to do a branch-less save.
555 // 0xB points to alpha of the current vertex, 0xC to the UV which get overwritten later
556 rspq_write(T3D_RSP_ID, T3D_CMD_FOG_STATE, isEnabled ? 0x0B : 0x0C);
557}
558
564uint16_t t3d_vert_pack_normal(const T3DVec3 *normal);
565
570void t3d_state_set_drawflags(enum T3DDrawFlags drawFlags);
571
579void t3d_state_set_depth_offset(int16_t offset);
580
598void t3d_state_set_alpha_to_tile(bool enable);
599
617void t3d_state_set_vertex_fx(enum T3DVertexFX func, int16_t arg0, int16_t arg1);
618
627void t3d_state_set_vertex_fx_scale(uint16_t scale);
628
650void t3d_state_set_lighting_mode(enum T3DLightingMode mode);
651
668void t3d_state_set_cull_invert(bool invert);
669
678void t3d_segment_set(uint8_t segmentId, void* address);
679
687static inline void* t3d_segment_placeholder(uint8_t segmentId) {
688 return (void*)(uint32_t)(segmentId << (8*3 + 2));
689}
690
700static inline void* t3d_segment_address(uint8_t segmentId, void* ptr) {
701 return (void*)(PhysicalAddr(ptr) | (segmentId << (8*3 + 2)));
702}
703
704// Index-buffer helpers:
705
728void t3d_indexbuffer_convert(int16_t indices[], int count);
729
730// Vertex-buffer helpers:
731
737static inline int16_t* t3d_vertbuffer_get_pos(T3DVertPacked vert[], int idx) {
738 return (idx & 1) ? vert[idx/2].posB : vert[idx/2].posA;
739}
740
746static inline int16_t* t3d_vertbuffer_get_uv(T3DVertPacked vert[], int idx) {
747 return (idx & 1) ? vert[idx/2].stB : vert[idx/2].stA;
748}
749
755static inline uint32_t* t3d_vertbuffer_get_color(T3DVertPacked vert[], int idx) {
756 return (idx & 1) ? &vert[idx/2].rgbaB : &vert[idx/2].rgbaA;
757}
758
764static inline uint8_t* t3d_vertbuffer_get_rgba(T3DVertPacked vert[], int idx) {
765 return (idx & 1) ? (uint8_t*)&vert[idx/2].rgbaB : (uint8_t*)&vert[idx/2].rgbaA;
766}
767
773static inline uint16_t* t3d_vertbuffer_get_norm(T3DVertPacked vert[], int idx) {
774 return (idx & 1) ? &vert[idx/2].normB : &vert[idx/2].normA;
775}
776
777#ifdef __cplusplus
778}
779#endif
780
781// C++ wrappers that allow (const-)references to be passed instead of pointers
782#ifdef __cplusplus
783
784inline void t3d_viewport_attach(T3DViewport &viewport) { t3d_viewport_attach(&viewport); }
785inline 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); }
786inline void t3d_viewport_set_projection(T3DViewport &viewport, float fov, float near, float far) { t3d_viewport_set_projection(&viewport, fov, near, far); }
787inline 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); }
788inline void t3d_viewport_set_w_normalize(T3DViewport &viewport, float near, float far) { t3d_viewport_set_w_normalize(&viewport, near, far); }
789inline 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); }
790inline void t3d_viewport_calc_viewspace_pos(T3DViewport &viewport, T3DVec3 &out, const T3DVec3 &pos) { t3d_viewport_calc_viewspace_pos(&viewport, &out, &pos); }
791
792inline void t3d_light_set_directional(int index, const uint8_t *color, const T3DVec3 &dir) { t3d_light_set_directional(index, color, &dir); }
793inline 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); }
794
795inline void t3d_light_set_ambient(const color_t &color) { t3d_light_set_ambient((const uint8_t*)&color); }
796inline void t3d_light_set_directional(int index, const color_t &color, const T3DVec3 &dir) { t3d_light_set_directional(index, (const uint8_t*)&color, &dir); }
797inline 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); }
798
799#endif
800
801#endif //TINY3D_T3D_H
Definition t3dmath.h:38
Definition t3d.h:129
Definition t3d.h:139
Definition t3d.h:103
static void t3d_viewport_destroy(T3DViewport *viewport)
Definition t3d.h:224
void t3d_state_set_vertex_fx(enum T3DVertexFX func, int16_t arg0, int16_t arg1)
Definition t3d.c:378
void t3d_destroy(void)
Destroys the tiny3d library.
Definition t3d.c:93
void t3d_vert_load(const T3DVertPacked *vertices, uint32_t offset, uint32_t count)
Definition t3d.c:184
void t3d_viewport_attach(T3DViewport *viewport)
Definition t3d.c:593
static uint32_t * t3d_vertbuffer_get_color(T3DVertPacked vert[], int idx)
Definition t3d.h:755
void t3d_indexbuffer_convert(int16_t indices[], int count)
Definition t3d.c:773
void t3d_light_set_count(int count)
Definition t3d.c:227
static uint16_t * t3d_vertbuffer_get_norm(T3DVertPacked vert[], int idx)
Definition t3d.h:773
void t3d_viewport_calc_viewspace_pos(T3DViewport *viewport, T3DVec3 *out, const T3DVec3 *pos)
Definition t3d.c:743
void t3d_tri_draw_strip_and_sync(int16_t *indexBuff, int count)
Definition t3d.c:547
void t3d_state_set_alpha_to_tile(bool enable)
Definition t3d.c:371
void t3d_state_set_lighting_mode(enum T3DLightingMode mode)
Definition t3d.c:425
void t3d_light_set_ambient(const uint8_t *color)
Definition t3d.c:241
void t3d_state_set_cull_invert(bool invert)
Definition t3d.c:451
static int16_t * t3d_vertbuffer_get_uv(T3DVertPacked vert[], int idx)
Definition t3d.h:746
static void * t3d_segment_address(uint8_t segmentId, void *ptr)
Definition t3d.h:700
void t3d_viewport_set_view_matrix(T3DViewport *viewport, const T3DMat4 *mat)
Definition t3d.c:727
static T3DViewport t3d_viewport_create_buffered(uint16_t count)
Definition t3d.h:208
static uint8_t * t3d_vertbuffer_get_rgba(T3DVertPacked vert[], int idx)
Definition t3d.h:764
uint16_t t3d_vert_pack_normal(const T3DVec3 *normal)
Definition t3d.c:318
void t3d_matrix_pop(int count)
Definition t3d.c:170
void t3d_viewport_set_perspective(T3DViewport *viewport, float fov, float aspectRatio, float near, float far)
Definition t3d.c:702
void t3d_viewport_set_ortho(T3DViewport *viewport, float left, float right, float bottom, float top, float near, float far)
Definition t3d.c:713
void t3d_segment_set(uint8_t segmentId, void *address)
Definition t3d.c:473
void t3d_tri_draw(uint32_t v0, uint32_t v1, uint32_t v2)
Draws a single triangle, referencing loaded vertices.
Definition t3d.c:481
void t3d_tri_draw_strip(int16_t *indexBuff, int count)
Definition t3d.c:542
void t3d_init(T3DInitParams params)
Initializes the tiny3d library.
Definition t3d.c:59
void t3d_matrix_push(const T3DMat4FP *mat)
Definition t3d.c:166
static void t3d_viewport_set_w_normalize(T3DViewport *viewport, float near, float far)
Definition t3d.h:312
void t3d_screen_clear_color(color_t color)
Clears the entire screen with a given color.
Definition t3d.c:107
void t3d_state_set_depth_offset(int16_t offset)
Definition t3d.c:367
void t3d_state_set_drawflags(enum T3DDrawFlags drawFlags)
Definition t3d.c:335
void t3d_matrix_push_pos(int count)
Definition t3d.c:175
static void t3d_tri_sync()
Definition t3d.h:424
void t3d_frame_start(void)
Starts a new frame, this will setup some default states.
Definition t3d.c:211
void t3d_screen_clear_depth()
Clears the entire depth buffer with a fixed value (0xFFFC)
Definition t3d.c:111
void t3d_fog_set_range(float near, float far)
Definition t3d.c:552
void t3d_light_set_exposure(float exposure)
Definition t3d.c:235
void t3d_viewport_look_at(T3DViewport *viewport, const T3DVec3 *eye, const T3DVec3 *target, const T3DVec3 *up)
Definition t3d.c:719
void t3d_state_set_vertex_fx_scale(uint16_t scale)
Definition t3d.c:420
void t3d_viewport_set_projection(T3DViewport *viewport, float fov, float near, float far)
Definition t3d.c:708
static void t3d_viewport_set_area(T3DViewport *viewport, int32_t x, int32_t y, int32_t width, int32_t height)
Definition t3d.h:258
void t3d_quad_draw_unindexed(uint32_t baseIndex, uint32_t quadCount)
Definition t3d.c:522
void t3d_matrix_set_proj(const T3DMat4FP *mat)
Definition t3d.c:180
static void * t3d_segment_placeholder(uint8_t segmentId)
Definition t3d.h:687
void t3d_light_set_directional(int index, const uint8_t *color, const T3DVec3 *dir)
Definition t3d.c:250
void t3d_matrix_set(const T3DMat4FP *mat, bool doMultiply)
Definition t3d.c:162
static T3DViewport t3d_viewport_create()
Definition t3d.h:186
static int16_t * t3d_vertbuffer_get_pos(T3DVertPacked vert[], int idx)
Definition t3d.h:737
static void t3d_fog_set_enabled(bool isEnabled)
Definition t3d.h:552
void t3d_light_set_point(int index, const uint8_t *color, const T3DVec3 *pos, float size, bool ignoreNormals)
Definition t3d.c:278
void t3d_viewport_set_projection_matrix(T3DViewport *viewport, const T3DMat4 *mat)
Definition t3d.c:735
void t3d_metrics_fetch(T3DMetrics *data)
Definition t3d.c:154
T3DViewport * t3d_viewport_get()
Definition t3d.c:698
void t3d_tri_draw_unindexed(uint32_t baseIndex, uint32_t triCount)
Definition t3d.c:518
__attribute__((deprecated)) void t3d_debug_print_init()
Initializes the debug print system, make sure to have 'font.ia4.png' in your FS.