7/7 Fixed-Point math

This commit is contained in:
2026-05-31 20:23:14 -04:00
parent e409fe0b2d
commit 66981351ee
7 changed files with 269 additions and 177 deletions
+41 -45
View File
@@ -37,49 +37,43 @@ enum {
gpio_port_0 = 0x1810,
gpio_port_1 = 0x1814,
gmcd_offset = 24,
gcmd_offset = 24,
gp_Reset = (gcmd_Reset << gmcd_offset),
gp_Reset = (gcmd_Reset << gcmd_offset),
gp_DisplayEnabled = (gcmd_DisplayEnable << gcmd_offset | 0x0),
gp_DisplayDisabled = (gcmd_DisplayEnable << gcmd_offset | 0x1),
gp_DMA_FIFO = 1,
gp_DMA_CPU_to_GPU = 2,
gp_DMA_GPU_to_CPU = 3,
gp_DMA_Request = (gcmd_DMA_Request << gcmd_offset),
gp_HorizontalDisplayRange_3168_608 = (gcmd_HorizontalDisplayRange << gcmd_offset | 0xC60 << 12 | 0x260),
gp_VerticalDiplayRange = (gcmd_VerticalDisplayRange << gcmd_offset),
gp_VerticalDisplayRange_264_24 = (gp_VerticalDiplayRange | 264 << 10 | 24),
gp_VerticalDisplayRange_504_24 = (gp_VerticalDiplayRange | 504 << 10 | 24),
gp_DisplayMode = (gcmd_DisplayMode << gcmd_offset),
gp_Disp_HRes_256 = (0x0),
gp_Disp_HRes_320 = (0x1),
gp_Disp_HRes_512 = (0x2),
gp_Disp_HRes_640 = (0x3),
gp_Disp_VRes_240 = (0x0 << 2),
gp_Disp_VRes_480 = (0x1 << 2),
gp_Disp_Color15 = (0x0 << 4),
gp_Disp_Color24 = (0x1 << 4),
gp_Disp_VInterlace = (0x1 << 5),
gp_DisplayMode_320x240_15bit_NTSC = (gp_DisplayMode | gp_Disp_HRes_320 | gp_Disp_VRes_240 | gp_Disp_Color15),
gp_DisplayMOde_640x480_24bbp_NTSC = (gp_DisplayMode | gp_Disp_HRes_640 | gp_Disp_VRes_480 | gp_Disp_Color24 | gp_Disp_VInterlace),
gp_DrawMode_DrawAllowed = 10,
gp_SetDrawMode_DrawAllowed = (gcmd_SetDrawMode << gcmd_offset | 0x1 << gp_DrawMode_DrawAllowed),
gp_SetArea_TopLeft = (gcmd_SetDrawArea_TopLeft << gcmd_offset),
gp_SetArea_BottomRight = (gcmd_SetDrawArea_BotRight << gcmd_offset),
};
// #define gpio_port0 0x1810
// #define gpio_port1 0x1814
// #define gcmd_offset 24
// #define gp_Reset (gcmd_Reset << gcmd_offset)
#define gp_DisplayEnabled (gcmd_DisplayEnable << gcmd_offset | 0x0)
#define gp_DisplayDisabled (gcmd_DisplayEnable << gcmd_offset | 0x1)
#define gp_DMA_FIFO 1
#define gp_DMA_CPU_to_GPU 2
#define gp_DMA_GPU_to_CPU 3
#define gp_DMA_Request (gcmd_DMA_Request << gcmd_offset)
#define gp_HorizontalDisplayRange_3168_608 (gcmd_HorizontalDisplayRange << gcmd_offset | 0xC60 << 12 | 0x260)
#define gp_VerticalDiplayRange (gcmd_VerticalDisplayRange << gcmd_offset)
#define gp_VerticalDisplayRange_264_24 (gp_VerticalDiplayRange | 264 << 10 | 24)
#define gp_VerticalDisplayRange_504_24 (gp_VerticalDiplayRange | 504 << 10 | 24)
#define gp_DisplayMode (gcmd_DisplayMode << gcmd_offset)
#define gp_Disp_HRes_256 (0x0)
#define gp_Disp_HRes_320 (0x1)
#define gp_Disp_HRes_512 (0x2)
#define gp_Disp_HRes_640 (0x3)
#define gp_Disp_VRes_240 (0x0 << 2)
#define gp_Disp_VRes_480 (0x1 << 2)
#define gp_Disp_Color15 (0x0 << 4)
#define gp_Disp_Color24 (0x1 << 4)
#define gp_Disp_VInterlace (0x1 << 5)
#define gp_DisplayMode_320x240_15bit_NTSC (gp_DisplayMode | gp_Disp_HRes_320 | gp_Disp_VRes_240 | gp_Disp_Color15)
#define gp_DisplayMOde_640x480_24bbp_NTSC (gp_DisplayMode | gp_Disp_HRes_640 | gp_Disp_VRes_480 | gp_Disp_Color24 | gp_Disp_VInterlace)
#define gp_DrawMode_DrawAllowed 10
#define gp_SetDrawMode_DrawAllowed (gcmd_SetDrawMode << gcmd_offset | 0x1 << gp_DrawMode_DrawAllowed)
#define gp_SetArea_TopLeft (gcmd_SetDrawArea_TopLeft << gcmd_offset)
#define gp_SetArea_BottomRight (gcmd_SetDrawArea_BotRight << gcmd_offset)
typedef def_struct(RGB8) { B1 r; B1 g; B1 b; };
#define rgb8(r, g, b) (RGB8){ r, g, b }
@@ -87,10 +81,12 @@ typedef def_struct(RGB8) { B1 r; B1 g; B1 b; };
typedef B1 gp_Pixel16[1];
typedef B1 gp_Pixel24[3];
#define gp_b10_X 0
#define gp_b10_Y 10
#define gp_b16_X 0
#define gp_b16_Y 16
enum {
gp_b10_X = 0,
gp_b10_Y = 10,
gp_b16_X = 0,
gp_b16_Y = 16,
};
typedef def_struct(gp_Vec2) { U2 y; U2 x; };
+13
View File
@@ -33,6 +33,9 @@ typedef def_struct(Rect_S4) { S4 x; S4 y; S4 width; S4 height; };
typedef def_struct(M3_S2) { A3A3_S2 m; A3_S4 t; };
typedef def_farray(V2_S2, 3);
typedef def_farray(V2_S2, 4);
#define v2s2(x,y) (V2_S2){x,y}
#define v3s2(x,y,z) (V3_S2){x,y,z,0}
#define v3s4(x,y,z) (V3_S4){x,y,z,0}
@@ -45,6 +48,16 @@ FI_ void add_a3s4(A3_S4_R out_a, A3_S4 b) {
(out_a[0])[2] += b[2];
}
FI_ void add_a3s4_fp(A3_S4_R out_a, A3_S4 b) {
(out_a[0])[0] += b[0] >> 1;
(out_a[0])[1] += b[1] >> 1;
(out_a[0])[2] += b[2] >> 1;
}
FI_ void add_v3s4(V3_S4_R out_a, V3_S4 b) {
add_a3s4(pcast(A3_S4_R, out_a), pcast(A3_S4, b));
}
FI_ void add_v3s4_fp(V3_S4_R out_a, V3_S4 b) {
add_a3s4_fp(pcast(A3_S4_R, out_a), pcast(A3_S4, b));
}
+112 -98
View File
@@ -11,55 +11,6 @@
#include "duffle/gp.h"
#include "hello_gpu.h"
typedef def_farray(V2_S2, 3);
typedef def_struct(Poly_F3) {
U4 tag;
RGB8 color;
B1 code;
union {
struct {
V2_S2 p0;
V2_S2 p1;
V2_S2 p2;
};
A3_V2_S2 points;
};
};
typedef def_struct(Poly_G3) {
U4 tag; RGB8 c0; B1 code;
V2_S2 p0; RGB8 c1; B1 pad1;
V2_S2 p1; RGB8 c2; B1 pad2;
V2_S2 p2;
};
typedef def_farray(V2_S2, 4);
typedef def_struct(Poly_F4) {
U4 tag;
RGB8 color;
B1 code;
union {
struct {
V2_S2 p0;
V2_S2 p1;
V2_S2 p2;
V2_S2 p3;
};
A4_V2_S2 points;
};
};
typedef def_struct(Poly_G4) {
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) {
U4 tag;
RGB8 color;
B1 code;
Rect_S2 rect;
};
enum {
PrimitiveBuff_Len = 4096,
OrderingTbl_Len = 2048
@@ -79,8 +30,7 @@ typedef def_struct(PrimitiveArena) {
typedef def_farray(V3_S2, Cube_num_verts);
#define Cube_num_faces 6
typedef def_farray(V4_S2, Cube_num_faces);
typedef A6_V4_S2 ACubeFaces;
void cube128_init(A8_V3_S2* verts, ACubeFaces* faces) {
void ent_cube128_init(A8_V3_S2* verts, A6_V4_S2* faces) {
memory_copy(verts, & (A8_V3_S2) {
{ -128, -128, -128 },
{ 128, -128, -128 },
@@ -90,9 +40,7 @@ void cube128_init(A8_V3_S2* verts, ACubeFaces* faces) {
{ 128, 128, -128 },
{ 128, 128, 128 },
{ -128, 128, 128 }
},
size_of(A8_V3_S2)
);
}, size_of(A8_V3_S2) );
memory_copy(faces, & (A6_V4_S2) {
{ 3, 2, 0, 1 },
{ 0, 1, 4, 5 },
@@ -100,11 +48,43 @@ void cube128_init(A8_V3_S2* verts, ACubeFaces* faces) {
{ 1, 2, 5, 6 },
{ 2, 3, 6, 7 },
{ 3, 0, 7, 4 },
},
sizeof(A6_V4_S2)
);
}, size_of(A6_V4_S2) );
return;
}
typedef def_struct(Ent_Cube) {
V3_S4 accel;
V3_S4 vel;
V3_S4 pos;
V3_S4 scale;
V3_S2 rot;
A8_V3_S2 verts;
A6_V4_S2 faces;
};
#define Floor_num_verts 4
typedef def_farray(V3_S2, Floor_num_verts);
#define Floor_num_faces 2
typedef def_farray(V3_S2, Floor_num_faces);
void ent_floor_init(A4_V3_S2* verts, A2_V3_S2* faces) {
memory_copy(verts, &(A4_V3_S2) {
{ -900, 0, -900 },
{ -900, 0, 900 },
{ 900, 0, -900 },
{ 900, 0, 900 },
}, size_of(A8_V3_S2));
memory_copy(faces, & (A2_V3_S2) {
{ 0, 1, 2 },
{ 1, 3, 2 },
}, size_of(A2_V3_S2));
};
typedef def_struct(Ent_Floor) {
V3_S4 accel;
V3_S4 pos;
V3_S4 scale;
V3_S2 rot;
A4_V3_S2 verts;
A2_V3_S2 faces;
};
typedef def_struct(SMemory) {
DoubleBuffer screen_buf;
@@ -112,18 +92,10 @@ typedef def_struct(SMemory) {
PrimitiveArena primitives;
S2 active_buf_id;
V3_S2 rotation;
V3_S4 translation;
V3_S4 scale;
M3_S2 tform_world;
A8_V3_S2 cube_verts;
ACubeFaces cube_faces;
V3_S4 vel;
V3_S4 acc;
V3_S4 pos;
Ent_Cube cube;
Ent_Floor floor;
};
global SMemory static_mem;
extern SMemory static_mem;
@@ -156,8 +128,9 @@ void gp_screen_init_c11(DoubleBuffer* screen_buf, S2* active_buf_id)
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 = rgb8( .r = 13, .g = 0, .b = 47 );
screen_buf->draw[1].initial_bg_color = rgb8( .r = 47, .g = 13, .b = 0 );
screen_buf->draw[0].initial_bg_color = rgb8( .r = 7, .g = 7, .b = 7 );
screen_buf->draw[1].initial_bg_color = rgb8( .r = 7, .g = 7, .b = 7 );
// screen_buf->draw[1].initial_bg_color = rgb8( .r = 47, .g = 13, .b = 0 );
displayenv_put(& r_(screen_buf->display)[ active_buf_id[0] ]);
drawenv_put (& r_(screen_buf->draw )[ active_buf_id[0] ]);
@@ -189,11 +162,11 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
orderingtbl_clear_reverse(ordering_buf, OrderingTbl_Len);
// Update the position based on acceleration and velocity
gknown V3_S4_R pos = & static_mem.pos;
gknown V3_S4_R vel = & static_mem.vel;
gknown V3_S4_R acc = & static_mem.acc;
gknown V3_S4_R pos = & static_mem.cube.pos;
gknown V3_S4_R vel = & static_mem.cube.vel;
gknown V3_S4_R acc = & static_mem.cube.accel;
add_v3s4(vel, acc[0]);
add_v3s4(pos, vel[0]);
add_v3s4_fp(pos, vel[0]);
// vel->x += acc->x;
// vel->y += acc->y;
// vel->z += acc->z;
@@ -201,20 +174,21 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
// pos->y += vel->y;
// pos->z += vel->z;
if (pos->y > 400) vel->y *= -1;
m3s2_rotation (& static_mem.rotation, & static_mem.tform_world);
m3s2_translation(& static_mem.tform_world, & static_mem.pos);
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);
if (pos->y + 150 > static_mem.floor.pos.y) vel->y *= -1;
// Prep
S4 nclip = 0;
S4 orderingtbl_z = 0;
A2_S2 p; //???
S4 flag; //????
// Draw Cube
{
m3s2_rotation (& static_mem.cube.rot, & static_mem.tform_world);
m3s2_translation(& static_mem.tform_world, & static_mem.cube.pos);
m3s2_scale (& static_mem.tform_world, & static_mem.cube.scale);
gte_matrix_set_rotation (& static_mem.tform_world);
gte_matrix_set_translation(& static_mem.tform_world);
for (U4 face_id = 0; face_id < Cube_num_faces; face_id += 1)
{
Poly_G4* quad = prim_alloc(Poly_G4); set_poly_g4(quad);
@@ -223,11 +197,11 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
quad->c2 = rgb8( 0, 255, 255);
quad->c3 = rgb8( 0, 255, 0);
V4_S2* face = & static_mem.cube_faces[face_id];
V3_S2* p0 = & static_mem.cube_verts[face->x];
V3_S2* p1 = & static_mem.cube_verts[face->y];
V3_S2* p2 = & static_mem.cube_verts[face->z];
V3_S2* p3 = & static_mem.cube_verts[face->w];
V4_S2* face = & static_mem.cube.faces[face_id];
V3_S2* p0 = & static_mem.cube.verts[face->x];
V3_S2* p1 = & static_mem.cube.verts[face->y];
V3_S2* p2 = & static_mem.cube.verts[face->z];
V3_S2* p3 = & static_mem.cube.verts[face->w];
nclip = rtp_avg_nclip_a4_v3s2(
p0, p1, p2, p3,
@@ -242,24 +216,64 @@ void update(PrimitiveArena* pa, U4* ordering_buf)
orderingtbl_add_primitive(ordering_buf[orderingtbl_z], quad);
}
}
// static_mem.rotation.x += 6;
// static_mem.rotation.y += 8;
// static_mem.rotation.z += 12;
static_mem.rotation.x += 20;
// static_mem.cube.rot.x += 6;
// static_mem.cube.rot.y += 8;
// static_mem.cube.rot.z += 12;
static_mem.cube.rot.y += 20;
}
// Draw Floor
{
m3s2_rotation (& static_mem.floor.rot, & static_mem.tform_world);
m3s2_translation(& static_mem.tform_world, & static_mem.floor.pos);
m3s2_scale (& static_mem.tform_world, & static_mem.floor.scale);
gte_matrix_set_rotation (& static_mem.tform_world);
gte_matrix_set_translation(& static_mem.tform_world);
for (U4 face_id = 0; face_id < Floor_num_faces; face_id += 1)
{
Poly_F3* tri = prim_alloc(Poly_F3); set_poly_f3(tri);
tri->color = rgb8(255, 255, 255);
V3_S2* face = & static_mem.floor.faces[face_id];
V3_S2* p0 = & static_mem.floor.verts[face->x];
V3_S2* p1 = & static_mem.floor.verts[face->y];
V3_S2* p2 = & static_mem.floor.verts[face->z];
nclip = rtp_avg_nclip_a3_v3s2(p0, p1, p2
, & tri->p0, & tri->p1, & tri->p2
, & p, & orderingtbl_z, & flag
);
if (nclip <= 0) {
continue;
}
if ((orderingtbl_z > 0) && (orderingtbl_z < OrderingTbl_Len)) {
orderingtbl_add_primitive(ordering_buf[orderingtbl_z], tri);
}
}
static_mem.floor.rot.y += 5;
}
}
int main(void)
{
static_mem = (SMemory){0};
static_mem.primitives.used = 0;
cube128_init(& static_mem.cube_verts, & static_mem.cube_faces);
static_mem.rotation = v3s2(0, 0, 0);
static_mem.translation = v3s4(0, 0, 900);
static_mem.scale = v3s4(fp_one, fp_one, fp_one);
static_mem.acc = v3s4(0, 1, 0);
static_mem.pos = v3s4(0, -400, 1800);
gknown gp_screen_init();
// gp_screen_init_c11(& static_mem.screen_buf, & static_mem.active_screen_buf);
ent_cube128_init(& static_mem.cube.verts, & static_mem.cube.faces); {
Ent_Cube* cube = & static_mem.cube;
cube->rot = v3s2(0, 0, 0);
// cube->pos = v3s4(0, 0, 900);
cube->scale = v3s4_fp_one();
cube->accel = v3s4(0, 1, 0);
cube->pos = v3s4(0, -400, 1800);
}
ent_floor_init(& static_mem.floor.verts, & static_mem.floor.faces); {
Ent_Floor* floor = & static_mem.floor;
floor->rot = v3s2(0, 0, 0);
floor->pos = v3s4(0, 450, 1800);
floor->scale = v3s4_fp_one();
}
// gknown gp_screen_init();
gp_screen_init_c11(& static_mem.screen_buf, & static_mem.active_buf_id);
while (1)
{
gknown S2* active_buf_id = & static_mem.active_buf_id;
+55 -1
View File
@@ -34,7 +34,7 @@ typedef def_struct(DoubleBuffer) {
#define ScreenRes_X 320
#define ScreenRes_Y 240
#define ScreenZ 400
#define ScreenZ 320
#define ScreenRes_CenterX (ScreenRes_X >> 1)
#define ScreenRes_CenterY (ScreenRes_Y >> 1)
@@ -106,6 +106,58 @@ typedef def_struct(PolyTag) {
// #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
typedef def_struct(Poly_F3) {
U4 tag;
RGB8 color;
B1 code;
union {
struct {
V2_S2 p0;
V2_S2 p1;
V2_S2 p2;
};
A3_V2_S2 points;
};
};
typedef def_struct(Poly_G3) {
U4 tag; RGB8 c0; B1 code;
V2_S2 p0; RGB8 c1; B1 pad1;
V2_S2 p1; RGB8 c2; B1 pad2;
V2_S2 p2;
};
typedef def_struct(Poly_F4) {
U4 tag;
RGB8 color;
B1 code;
union {
struct {
V2_S2 p0;
V2_S2 p1;
V2_S2 p2;
V2_S2 p3;
};
A4_V2_S2 points;
};
};
typedef def_struct(Poly_G4) {
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) {
U4 tag;
RGB8 color;
B1 code;
Rect_S2 rect;
};
/*
Linear Algebra
*/
@@ -151,3 +203,5 @@ void gte_matrix_set_translation(M3_S2* mat) __asm__("SetTransMatrix");
enum {
fp_one = (1 << 12),
};
#define v3s4_fp_one() v3s4(fp_one, fp_one, fp_one)
+2 -2
View File
@@ -293,8 +293,8 @@ function build-graphis_hello {
$compile_args += $f_debug
# $compile_args += $f_optimize_none
# $compile_args += $f_optimize_intrinsics
# $compile_args += $f_optimize_size
$compile_args += $f_optimize_debug
$compile_args += $f_optimize_size
# $compile_args += $f_optimize_debug
$compile_args += ($f_include + $path_code)
compile-unit $src_c $module_c $includes $compile_args
+8
View File
@@ -0,0 +1,8 @@
{
"configureme": false,
"enabled": true,
"hardPix": 1.5,
"hardScan": 4.5,
"scanlines": true,
"useSrgb": false
}
+7
View File
@@ -0,0 +1,7 @@
{
"configureme": true,
"grey": false,
"mask": 0.5,
"masktype": 2,
"warp": 0
}