Sorting Primitives into the OT

Started to add some ergonomic definitions into duffle.
This commit is contained in:
2025-09-15 01:36:25 -04:00
parent e04bcb91b4
commit f35e60877c
10 changed files with 383 additions and 49 deletions
+116 -24
View File
@@ -1,40 +1,108 @@
// #include <stdlib.h>
#include "stdio.h"
#include <stdlib.h>
#include "assert.h"
#include "libgpu.h"
#include "libetc.h"
#include "duffle/dsl.h"
#include "duffle/memory.h"
#include "duffle/math.h"
#include "duffle/gp.h"
#include "hello_gpu.h"
#include "libgpu.h"
#include "libetc.h"
typedef def_farray(Vec_2S16, 3);
typedef def_struct(TriFlat) {
U32 tag;
RGB8 color;
BYTE code;
union {
struct {
Vec_2S16 p0;
Vec_2S16 p1;
Vec_2S16 p2;
};
A3_Vec_2S16 points;
};
};
typedef def_farray(Vec_2S16, 4);
typedef def_struct(QuadFlat) {
U32 tag;
RGB8 color;
BYTE code;
union {
struct {
Vec_2S16 p0;
Vec_2S16 p1;
Vec_2S16 p2;
Vec_2S16 p3;
};
A4_Vec_2S16 points;
};
};
typedef def_struct(Tile) {
U32 tag;
RGB8 color;
BYTE code;
Rect_S16 rect;
};
DoubleBuffer screen_buffer;
S16 active_screen_buffer;
#define PrimitiveBuff_Len 2048
#define OrderingTbl_Len 16
void gp_screen_init_c11(void)
typedef U32 OrderingTable_Buffer[OrderingTbl_Len];
typedef def_farray(OrderingTable_Buffer, 2);
typedef BYTE PrimitiveBuffer[PrimitiveBuff_Len];
typedef def_farray(PrimitiveBuffer, 2);
typedef def_struct(PrimitiveArena) {
A2_PrimitiveBuffer buf;
SSIZE used;
};
typedef def_struct(SMemory) {
DoubleBuffer screen_buf;
A2_OrderingTable_Buffer ordering_tbl;
PrimitiveArena primitives;
S16 active_screen_buf;
};
global SMemory static_mem;
extern SMemory static_mem;
BYTE* prim__alloc(SSIZE type_width, ct_lit Str8 type_name) {
gknown PrimitiveArena* pa = & static_mem.primitives;
gknown BYTE* buf = (BYTE*) static_mem.primitives.buf[static_mem.active_screen_buf];
assert(pa->used + type_width < PrimitiveBuff_Len);
BYTE* 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_screen_buf)
{
ResetGraph(0);
SetDispMask(1); // gp_DisplayEnabled
// Just setting env data, not interacting with console hw.
// First buffer area
SetDefDispEnv((DISPENV*)& screen_buffer.display[0], 0, 0, ScreenRes_X, ScreenRes_Y);
SetDefDrawEnv((DRAWENV*)& screen_buffer.draw [0], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y);
SetDefDispEnv((DISPENV*)& screen_buf->display[0], 0, 0, ScreenRes_X, ScreenRes_Y);
SetDefDrawEnv((DRAWENV*)& screen_buf->draw [0], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y);
// Second buffer area
SetDefDispEnv((DISPENV*)& screen_buffer.display[1], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y);
SetDefDrawEnv((DRAWENV*)& screen_buffer.draw [1], 0, 0, ScreenRes_X, ScreenRes_Y);
SetDefDispEnv((DISPENV*)& screen_buf->display[1], 0, ScreenRes_Y, ScreenRes_X, ScreenRes_Y);
SetDefDrawEnv((DRAWENV*)& screen_buf->draw [1], 0, 0, ScreenRes_X, ScreenRes_Y);
// Set the back/drawing buffer
screen_buffer.draw[0].enable_auto_clear = true;
screen_buffer.draw[1].enable_auto_clear = true;
screen_buf->draw[0].enable_auto_clear = true;
screen_buf->draw[1].enable_auto_clear = true;
// Set the background clear color
screen_buffer.draw[0].initial_bg_color = (RGB8){ .r = 63, .g = 0, .b = 127 };
screen_buffer.draw[1].initial_bg_color = (RGB8){ .r = 127, .g = 63, .b = 0 };
screen_buf->draw[0].initial_bg_color = (RGB8){ .r = 63, .g = 0, .b = 127 };
screen_buf->draw[1].initial_bg_color = (RGB8){ .r = 127, .g = 63, .b = 0 };
// Set the current initial buffer
active_screen_buffer = 0;
* active_screen_buf = 0;
PutDispEnv((DISPENV*)& screen_buffer.display[active_screen_buffer]);
PutDispEnv((DISPENV*)& screen_buf->display[* active_screen_buf]);
DRAWENV* wtf = (DRAWENV*)& screen_buffer.draw [active_screen_buffer];
PutDrawEnv(wtf);
DRAWENV* env = (DRAWENV*)& screen_buf->draw[* active_screen_buf];
PutDrawEnv(env);
// Initialize and setup the GTE geometry offsets
InitGeom();
@@ -43,27 +111,51 @@ void gp_screen_init_c11(void)
SetGeomScreen(ScreenRes_CenterX);
}
void gp_display_frame(void) {
void gp_display_frame(DoubleBuffer* screen_buf, S16* active_screen_buf, U32* ordering_buf) {
DrawSync(0);
VSync(0);
PutDispEnv((DISPENV*)& screen_buffer.display[active_screen_buffer]);
PutDrawEnv((DRAWENV*)& screen_buffer.draw [active_screen_buffer]);
PutDispEnv((DISPENV*)& screen_buf->display[* active_screen_buf]);
PutDrawEnv((DRAWENV*)& screen_buf->draw [* active_screen_buf]);
{
// TODO: Sort objects in ordering table
DrawOTag((u_long*) (ordering_buf + OrderingTbl_Len - 1));
}
active_screen_buffer = !active_screen_buffer; // Swap current buffer
* active_screen_buf = ! (* active_screen_buf); // Swap current buffer
gknown static_mem.primitives.used = 0;
}
void render(void) {
}
void update(PrimitiveArena* pa, S16 active_screen_buff, U32* ordering_buf) {
ClearOTagR((u_long*) ordering_buf, OrderingTbl_Len);
Tile* tile = prim_alloc(Tile); setTile((TILE*) tile);
tile->rect = (Rect_S16){ 82, 32, 64, 64 };
tile->color = (RGB8){ 0, 255, 0};
addPrim(ordering_buf, tile);
TriFlat* tri = prim_alloc(TriFlat); setPolyF3(tri);
tri->p0 = (Vec_2S16){ 64, 100};
tri->p1 = (Vec_2S16){200, 150};
tri->p2 = (Vec_2S16){ 50, 220};
tri->color = (RGB8){ 255, 0, 255 };
addPrim(ordering_buf, tri);
}
int main(void)
{
static_mem.primitives.used = 0;
gp_screen_init();
// gp_screen_init_c11(& static_mem.screen_buf, & static_mem.active_screen_buf);
while (1)
{
gknown S16* active_screen = & static_mem.active_screen_buf;
gknown U32* ordering_buf = static_mem.ordering_tbl[* active_screen];
update(& static_mem.primitives, * active_screen, ordering_buf);
render();
gp_display_frame();
gp_display_frame(& static_mem.screen_buf, active_screen, ordering_buf);
};
return 0;
}
-3
View File
@@ -36,6 +36,3 @@ typedef def_struct(DoubleBuffer) {
#define ScreenRes_Y 240
#define ScreenRes_CenterX (ScreenRes_X >> 1)
#define ScreenRes_CenterY (ScreenRes_Y >> 1)
extern DoubleBuffer screen_buffer;
extern S16 active_screen_buffer;
+16 -13
View File
@@ -42,6 +42,9 @@
.equ ScreenRes_CenterX, (ScreenRes_X >> 1)
.equ ScreenRes_CenterY, (ScreenRes_Y >> 1)
.equ SMemory_screen_buf, DoubleBuffer * 0
.equ SMemory_active_screen_buf, S16 * 0 + DoubleBuffer
.equ CF_Shadow, 16
.extern ResetGraph
@@ -86,7 +89,7 @@ gp_screen_init_asm:
#define gp0 gpio_port0(rio_offset)
#define gp1 gpio_port1(rio_offset)
def_cf_sp_size 0x80;
def_cf_sp_size 0x18; // Should be enough for all calls within this proc, for some reason SetDefDispEnv needs the offset to be CF_Shadow..
stack_alloc cf_ssize
store_word rret_addr, 0($sp)
@@ -97,26 +100,26 @@ gp_screen_init_asm:
load_imm SetDispMask_mask, 1; jump_nlink SetDispMask
// First buffer area
load_addr rtmp_0, screen_buffer; add_ui SetDefDispEnv_env, rtmp_0, DoubleBuffer_display_0
load_addr rtmp_0, static_mem; add_ui SetDefDispEnv_env, rtmp_0, SMemory_screen_buf + DoubleBuffer_display_0
move SetDefDispEnv_x, $zero
move SetDefDispEnv_y, $zero
load_imm SetDefDispEnv_w, ScreenRes_X
load_imm rtmp_0, ScreenRes_Y; store_word rtmp_0, SetDefDispEnv_h($sp)
jump_nlink SetDefDispEnv
load_addr rtmp_0, screen_buffer; add_ui SetDefDrawEnv_env, rtmp_0, DoubleBuffer_draw_0
load_addr rtmp_0, static_mem; add_ui SetDefDrawEnv_env, rtmp_0, SMemory_screen_buf + DoubleBuffer_draw_0
move SetDefDrawEnv_x, $zero
load_imm SetDefDrawEnv_y, ScreenRes_Y
load_imm SetDefDrawEnv_w, ScreenRes_X
load_imm rtmp_0, ScreenRes_Y; store_word rtmp_0, SetDefDrawEnv_h($sp)
jump_nlink SetDefDrawEnv
// Second buffer area
load_addr rtmp_0, screen_buffer; add_ui SetDefDispEnv_env, rtmp_0, DoubleBuffer_display_1
load_addr rtmp_0, static_mem; add_ui SetDefDispEnv_env, rtmp_0, SMemory_screen_buf + DoubleBuffer_display_1
move SetDefDispEnv_x, $zero
load_imm SetDefDispEnv_y, ScreenRes_Y
load_imm SetDefDispEnv_w, ScreenRes_X
load_imm rtmp_0, ScreenRes_Y; store_word rtmp_0, SetDefDispEnv_h($sp)
jump_nlink SetDefDispEnv
load_addr rtmp_0, screen_buffer; add_ui SetDefDrawEnv_env, rtmp_0, DoubleBuffer_draw_1
load_addr rtmp_0, static_mem; add_ui SetDefDrawEnv_env, rtmp_0, SMemory_screen_buf + DoubleBuffer_draw_1
move SetDefDrawEnv_x, $zero
move SetDefDrawEnv_y, $zero
load_imm SetDefDrawEnv_w, ScreenRes_X
@@ -125,7 +128,7 @@ gp_screen_init_asm:
// Set the back/drawing buffer
load_imm rtmp_1, true
load_addr rtmp_0, screen_buffer;
load_addr rtmp_0, static_mem; // At SMemory_screen_buf
store_word rtmp_1, DoubleBuffer_draw_0 + DrawEnv_enable_auto_clear(rtmp_0)
store_word rtmp_1, DoubleBuffer_draw_1 + DrawEnv_enable_auto_clear(rtmp_0)
@@ -139,17 +142,17 @@ gp_screen_init_asm:
store_byte rtmp_3, DoubleBuffer_draw_1 + DrawEnv_initial_bg_color + RGB8_r(rtmp_0)
store_byte rtmp_2, DoubleBuffer_draw_1 + DrawEnv_initial_bg_color + RGB8_g(rtmp_0)
store_byte rtmp_1, DoubleBuffer_draw_1 + DrawEnv_initial_bg_color + RGB8_b(rtmp_0)
load_addr rtmp_0, active_screen_buffer; store_word rtmp_1, 0(rtmp_0)
load_addr rtmp_0, static_mem; store_word rtmp_1, SMemory_active_screen_buf(rtmp_0)
load_addr rtmp_1, active_screen_buffer; load_half rtmp_1, 0(rtmp_1); // rtmp_1 = active_screen_buffer
load_imm rtmp_2, DisplayEnv; mult_u rtmp_1, rtmp_2; mov_from_low rtmp_2 // rtmp_2 = DisplayEnv.type_size * active_screen_Buffer (rtmp_1)
add_ui rtmp_2, rtmp_2, DoubleBuffer_display // rtmp_2 += DoubleBuffer.display
load_addr rtmp_0, screen_buffer; add_u PutDispEnv_env, rtmp_0, rtmp_2 // rarg_0 = rtmp_0 (screen_buffer) + rtmp_2 (.display[active_screen-buffer])
load_addr rtmp_1, static_mem; load_half rtmp_1, SMemory_active_screen_buf(rtmp_1); // rtmp_1 = active_screen_buffer
load_imm rtmp_2, DisplayEnv; mult_u rtmp_1, rtmp_2; mov_from_low rtmp_2 // rtmp_2 = DisplayEnv.type_size * active_screen_Buffer (rtmp_1)
add_ui rtmp_2, rtmp_2, DoubleBuffer_display // rtmp_2 += DoubleBuffer.display
load_addr rtmp_0, static_mem; add_u PutDispEnv_env, rtmp_0, rtmp_2 // rarg_0 = rtmp_0 (screen_buffer) + rtmp_2 (.display[active_screen-buffer])
jump_nlink PutDispEnv
load_addr rtmp_1, active_screen_buffer; load_half rtmp_1, 0(rtmp_1);
load_addr rtmp_1, static_mem; load_half rtmp_1, SMemory_active_screen_buf(rtmp_1);
load_imm rtmp_2, DrawEnv; mult_u rtmp_1, rtmp_2; mov_from_low rtmp_2;
add_ui rtmp_2, rtmp_2, DoubleBuffer_draw
load_addr rtmp_0, screen_buffer; add_u PutDrawEnv_env, rtmp_0, rtmp_2
load_addr rtmp_0, static_mem; add_u PutDrawEnv_env, rtmp_0, rtmp_2
jump_nlink PutDrawEnv
// Initialize and setup the GTE geometry offsets