Update convention yet again

Doing stuff inspired by Timothy Lottes's fixing c vod
This commit is contained in:
2025-10-11 00:18:44 -04:00
parent 2f797bf9e2
commit 274e0f31f5
10 changed files with 327 additions and 276 deletions
+111 -49
View File
@@ -10,104 +10,150 @@
#include "duffle/gp.h"
#include "hello_gpu.h"
typedef def_farray(Vec_2S16, 3);
typedef def_farray(V2_S2, 3);
typedef def_struct(TriFlat) {
U32 tag;
U4 tag;
RGB8 color;
BYTE code;
B1 code;
union {
struct {
Vec_2S16 p0;
Vec_2S16 p1;
Vec_2S16 p2;
V2_S2 p0;
V2_S2 p1;
V2_S2 p2;
};
A3_Vec_2S16 points;
A3_V2_S2 points;
};
};
typedef def_farray(Vec_2S16, 4);
typedef def_farray(V2_S2, 4);
typedef def_struct(QuadFlat) {
U32 tag;
U4 tag;
RGB8 color;
BYTE code;
B1 code;
union {
struct {
Vec_2S16 p0;
Vec_2S16 p1;
Vec_2S16 p2;
Vec_2S16 p3;
V2_S2 p0;
V2_S2 p1;
V2_S2 p2;
V2_S2 p3;
};
A4_Vec_2S16 points;
A4_V2_S2 points;
};
};
typedef def_struct(QuadGouraud) {
U32 tag; RGB8 c0; BYTE code;
Vec_2S16 p0; RGB8 c1; BYTE pad1;
Vec_2S16 p1; RGB8 c2; BYTE pad2;
Vec_2S16 p2; RGB8 c3; BYTE pad3;
Vec_2S16 p3;
U4 tag; RGB8 c0; B1 code;
V2_S2 p0; RGB8 c1; B1 pad1;
V2_S2 p1; RGB8 c2; B1 pad2;
V2_S2 p2; RGB8 c3; B1 pad3;
V2_S2 p3;
};
typedef def_struct(Tile) {
U32 tag;
RGB8 color;
BYTE code;
Rect_S16 rect;
U4 tag;
RGB8 color;
B1 code;
Rect_S2 rect;
};
#define PrimitiveBuff_Len 2048
#define OrderingTbl_Len 32
#define OrderingTbl_Len 256
typedef U32 OrderingTable_Buffer[OrderingTbl_Len];
typedef U4 OrderingTable_Buffer[OrderingTbl_Len];
typedef def_farray(OrderingTable_Buffer, 2);
typedef BYTE PrimitiveBuffer[PrimitiveBuff_Len];
typedef B1 PrimitiveBuffer[PrimitiveBuff_Len];
typedef def_farray(PrimitiveBuffer, 2);
typedef def_struct(PrimitiveArena) {
A2_PrimitiveBuffer buf;
SSIZE used;
U4 used;
};
#define Cube_num_verts 8
#define Cube_num_faces 12
typedef def_farray(V3_S2, Cube_num_verts);
typedef def_farray(V3_S2, Cube_num_faces);
void cube128_init(A8_V3_S2* verts, A12_V3_S2* faces) {
memory_copy(verts, & (A8_V3_S2) {
{ -128, -128, -128 },
{ 128, -128, -128 },
{ 128, -128, 128 },
{ -128, -128, 128 },
{ -128, 128, -128 },
{ 128, 128, -128 },
{ 128, 128, 128 },
{ -128, 128, 128 }
},
size_of(A8_V3_S2)
);
memory_copy(faces, & (A12_V3_S2) {
{ 0, 3, 2 }, // top
{ 0, 2, 1 }, // top
{ 4, 0, 1 }, // front
{ 4, 1, 5 }, // front
{ 7, 4, 5 }, // bottom
{ 7, 5, 6 }, // bottom
{ 5, 1, 2 }, // right
{ 5, 2, 6 }, // right
{ 2, 3, 7 }, // back
{ 2, 7, 6 }, // back
{ 0, 4, 7 }, // left
{ 0, 7, 3 } // left
},
size_of(A12_V3_S2)
);
return;
}
typedef def_struct(SMemory) {
DoubleBuffer screen_buf;
A2_OrderingTable_Buffer ordering_tbl;
PrimitiveArena primitives;
S16 active_buf_id;
S2 active_buf_id;
V3_S2 rotation;
V3_S4 translation;
V3_S4 scale;
M3_S2 tform_world;
A8_V3_S2 cube_verts;
A12_V3_S2 cube_faces;
};
global SMemory static_mem;
extern SMemory static_mem;
BYTE* prim__alloc(SSIZE type_width, dbg_args(Str8 type_name)) {
gknown PrimitiveArena* pa = & static_mem.primitives;
gknown BYTE* buf = (BYTE*) static_mem.primitives.buf[static_mem.active_buf_id];
B1* prim__alloc(U4 type_width, Str8 type_name) {
gknown PrimitiveArena* pa = & static_mem.primitives;
gknown B1* buf = (B1*) r_(static_mem.primitives.buf)[static_mem.active_buf_id];
assert(pa->used + type_width < PrimitiveBuff_Len);
BYTE* next = buf + pa->used;
pa->used += type_width;
B1* next = buf + pa->used;
pa->used += type_width;
return next;
}
#define prim_alloc(type) (type*)prim__alloc(size_of(type), txt( stringify(type)))
void gp_screen_init_c11(DoubleBuffer* screen_buf, S16* active_buf_id)
void gp_screen_init_c11(DoubleBuffer* screen_buf, S2* active_buf_id)
{
reset_graph(0);
set_display_enabled(1); // gp_DisplayEnabled
// Set the current initial buffer
* active_buf_id = 0;
active_buf_id[0] = 0;
// Just setting env data, not interacting with console hw.
// First buffer area
displayenv_init(& screen_buf->display[0], 0, 0, ScreenRes_X, ScreenRes_Y);
drawenv_init (& screen_buf->draw [0], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y);
displayenv_init(& r_(screen_buf->display)[0], 0, 0, ScreenRes_X, ScreenRes_Y);
drawenv_init (& r_(screen_buf->draw )[0], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y);
// Second buffer area
displayenv_init(& screen_buf->display[1], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y);
drawenv_init (& screen_buf->draw [1], 0, 0, ScreenRes_X, ScreenRes_Y);
displayenv_init(& r_(screen_buf->display)[1], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y);
drawenv_init (& r_(screen_buf->draw )[1], 0, 0, ScreenRes_X, ScreenRes_Y);
// Set the back/drawing buffer
screen_buf->draw[0].enable_auto_clear = true;
screen_buf->draw[1].enable_auto_clear = true;
// Set the background clear color
screen_buf->draw[0].initial_bg_color = rgba8( .r = 63, .g = 0, .b = 127 );
screen_buf->draw[1].initial_bg_color = rgba8( .r = 127, .g = 63, .b = 0 );
displayenv_put(& screen_buf->display[* active_buf_id]);
drawenv_put (& screen_buf->draw [* active_buf_id]);
displayenv_put(& r_(screen_buf->display)[ active_buf_id[0] ]);
drawenv_put (& r_(screen_buf->draw )[ active_buf_id[0] ]);
// Initialize and setup the GTE geometry offsets
geom_init();
@@ -115,11 +161,11 @@ void gp_screen_init_c11(DoubleBuffer* screen_buf, S16* active_buf_id)
geom_set_screen(ScreenZ);
}
void gp_display_frame(DoubleBuffer* screen_buf, S16* active_buf_id, U32* ordering_buf, PrimitiveArena* pa) {
void gp_display_frame(DoubleBuffer* screen_buf, S2* active_buf_id, U4* ordering_buf, PrimitiveArena* pa) {
draw_sync(0);
vsync(0);
displayenv_put(& screen_buf->display[* active_buf_id]);
drawenv_put (& screen_buf->draw [* active_buf_id]);
displayenv_put(& r_(screen_buf->display)[active_buf_id[0] ]);
drawenv_put (& r_(screen_buf->draw) [active_buf_id[0] ]);
{
draw_orderingtbl(ordering_buf + OrderingTbl_Len - 1);
pa->used = 0;
@@ -130,10 +176,24 @@ void gp_display_frame(DoubleBuffer* screen_buf, S16* active_buf_id, U32* orderin
void render(void) {
}
void update(PrimitiveArena* pa, U32* ordering_buf) {
void update(PrimitiveArena* pa, U4* ordering_buf)
{
orderingtbl_clear_reverse(ordering_buf, OrderingTbl_Len);
m3s2_rotation (& static_mem.rotation, & static_mem.tform_world);
m3s2_translation(& static_mem.tform_world, & static_mem.translation);
m3s2_scale (& static_mem.tform_world, & static_mem.scale);
gte_matrix_set_rotation (& static_mem.tform_world);
gte_matrix_set_translation(& static_mem.tform_world);
TriFlat* tri = prim_alloc(TriFlat); set_tri_flat(tri);
tri->color = rgba8(255, 0, 0);
S4 otz = 0;
// otz += vec_3s16_rtp(& )
#if 0
Tile* tile = prim_alloc(Tile); set_tile(tile);
tile->rect = (Rect_S16){ 82, 32, 64, 64 };
tile->color = (RGB8){ 0, 255, 0};
@@ -163,17 +223,19 @@ void update(PrimitiveArena* pa, U32* ordering_buf) {
quadf->p3 = vec_2s16(220 + 15, 80 + 9);
quadf->color = rgba8(22, 22, 22);
orderingtbl_add_primitive(ordering_buf, quadf);
#endif
}
int main(void)
{
static_mem.primitives.used = 0;
cube128_init(& static_mem.cube_verts, & static_mem.cube_faces);
gknown gp_screen_init();
// gp_screen_init_c11(& static_mem.screen_buf, & static_mem.active_screen_buf);
while (1)
{
gknown S16* active_buf_id = & static_mem.active_buf_id;
gknown U32* ordering_buf = static_mem.ordering_tbl[* active_buf_id];
gknown S2* active_buf_id = & static_mem.active_buf_id;
gknown U4* ordering_buf = r_(static_mem.ordering_tbl)[active_buf_id[0]];
gknown PrimitiveArena* pa = & static_mem.primitives;
update(pa, ordering_buf);
render();
+50 -39
View File
@@ -5,25 +5,25 @@
# include "duffle/gp.h"
#endif
typedef def_struct(DrawEnv_Packed) { U32 tag; U32 code[15]; };
typedef def_struct(DrawEnv_Packed) { U4 tag; U4 code[15]; };
typedef def_struct(DrawEnv) {
Rect_S16 clip_area;
A2_S16 drawing_offset;
Rect_S16 texture_window;
S16 texture_page;
BYTE flag_dither;
BYTE flag_draw_on_display;
BYTE enable_auto_clear;
RGB8 initial_bg_color;
Rect_S2 clip_area;
A2_S2 drawing_offset;
Rect_S2 texture_window;
S2 texture_page;
B1 flag_dither;
B1 flag_draw_on_display;
B1 enable_auto_clear;
RGB8 initial_bg_color;
DrawEnv_Packed dr_env; // reserved
};
typedef def_struct(DisplayEnv) {
Rect_S16 display_area;
Rect_S16 screen;
BYTE vinterlace;
BYTE color24;
BYTE pad0;
BYTE pad1;
Rect_S2 display_area;
Rect_S2 screen;
B1 vinterlace;
B1 color24;
B1 pad0;
B1 pad1;
};
typedef def_farray(DrawEnv, 2);
typedef def_farray(DisplayEnv, 2);
@@ -38,51 +38,49 @@ typedef def_struct(DoubleBuffer) {
#define ScreenRes_CenterX (ScreenRes_X >> 1)
#define ScreenRes_CenterY (ScreenRes_Y >> 1)
DisplayEnv* displayenv_init(DisplayEnv* env, S32 x, S32 y, S32 w, S32 h) __asm__("SetDefDispEnv");
DrawEnv* drawenv_init (DrawEnv* env, S32 x, S32 y, S32 w, S32 h) __asm__("SetDefDrawEnv");
DisplayEnv* displayenv_init(DisplayEnv* env, S4 x, S4 y, S4 w, S4 h) __asm__("SetDefDispEnv");
DrawEnv* drawenv_init (DrawEnv* env, S4 x, S4 y, S4 w, S4 h) __asm__("SetDefDrawEnv");
DisplayEnv* displayenv_put(DisplayEnv* env) __asm__("PutDispEnv");
DrawEnv* drawenv_put (DrawEnv* env) __asm__("PutDrawEnv");
U32 geom_init(void) __asm__("InitGeom");
void geom_set_offset(U32 x, U32 y) __asm__("SetGeomOffset");
void geom_set_screen(U32 h) __asm__("SetGeomScreen");
U4 geom_init(void) __asm__("InitGeom");
void geom_set_offset(U4 x, U4 y) __asm__("SetGeomOffset");
void geom_set_screen(U4 h) __asm__("SetGeomScreen");
U32* orderingtbl_clear_reverse(U32* ot, SSIZE len) __asm__("ClearOTagR");
U4* orderingtbl_clear_reverse(U4* ot, U4 len) __asm__("ClearOTagR");
U32 reset_graph(U32 mode) __asm__("ResetGraph");
void set_display_enabled(U32 mask) __asm__("SetDispMask");
U4 reset_graph(U4 mode) __asm__("ResetGraph");
void set_display_enabled(U4 mask) __asm__("SetDispMask");
U32 draw_sync(U32 mode) __asm__("DrawSync");
U32 vsync(U32 mode) __asm("VSync");
U4 draw_sync(U4 mode) __asm__("DrawSync");
U4 vsync(U4 mode) __asm__("VSync");
void draw_orderingtbl(U32* buf) __asm__("DrawOTag");
void draw_orderingtbl(U4* buf) __asm__("DrawOTag");
typedef def_struct(PolyTag) {
U32 addr: 24;
U32 len: 8;
U4 addr: 24;
U4 len: 8;
RGB8 color;
BYTE code;
B1 code;
};
/*
* Primitive Handling Macros
*/
#define set_len( p, _len) (((PolyTag*)(p))->len = (BYTE)(_len))
#define set_addr(p, _addr) (((PolyTag*)(p))->addr = (U32 )(_addr))
#define set_code(p, _code) (((PolyTag*)(p))->code = (BYTE)(_code))
#define get_len(p) (BYTE)(((PolyTag*)(p))->len)
#define get_code(p) (BYTE)(((PolyTag*)(p))->code)
#define get_addr(p) (U32 )(((PolyTag*)(p))->addr)
#define set_len( p, _len) (((PolyTag*)(p))->len = (B1)(_len))
#define set_addr(p, _addr) (((PolyTag*)(p))->addr = (U4)(_addr))
#define set_code(p, _code) (((PolyTag*)(p))->code = (B1)(_code))
#define get_len(p) (B1)(((PolyTag*)(p))->len)
#define get_code(p) (B1)(((PolyTag*)(p))->code)
#define get_addr(p) (U4)(((PolyTag*)(p))->addr)
#define orderingtbl_add_primitive(ot, p) set_addr(p, get_addr(ot)), set_addr(ot, p)
#define orderingtbl_add_primitives(ot, p0, p1) set_addr(p1, get_addr(ot)), set_addr(ot, p0)
/* Primitive Lentgh Code */
/*-------------------------------------------------------------------- */
/* */
/* Primitive Length Code */
#define set_tri_flat(p) set_len(p, 4), set_code(p, 0x20)
// #define setPolyFT3(p) set_len(p, 7), set_code(p, 0x24)
// #define setPolyG3(p) set_len(p, 6), set_code(p, 0x30)
@@ -106,3 +104,16 @@ typedef def_struct(PolyTag) {
// #define setLineG3(p) set_len(p, 7), set_code(p, 0x58),(p)->pad = 0x55555555, (p)->p2 = 0
// #define setLineF4(p) set_len(p, 6), set_code(p, 0x4c),(p)->pad = 0x55555555
// #define setLineG4(p) set_len(p, 9), set_code(p, 0x5c),(p)->pad = 0x55555555, (p)->p2 = 0, (p)->p3 = 0
/*
Linear Algebra
*/
M3_S2* m3s2_rotation (V3_S2* vec, M3_S2* mat) __asm__("RotMatrix");
M3_S2* m3s2_translation(M3_S2* mat, V3_S4* vec) __asm__("TransMatrix");
M3_S2* m3s2_scale (M3_S2* mat, V3_S4* vec) __asm__("ScaleMatrix");
S4 v3s2_rtp(V3_S2* vec, A2_S2 xy, A2_S2* pp, S4* flag) __asm__("RotTransPers"); // Rotation, Translate, Perspective
void gte_matrix_set_rotation (M3_S2* mat) __asm__("SetRotMatrix");
void gte_matrix_set_translation(M3_S2* mat) __asm__("SetTransMatrix");