code wiki / _hdl_build / nx_raster3d.nx
nx_raster3d.nx
buildroot/runtime/_hdl_build/nx_raster3d.nx
about
nx_raster3d.nx -- GATE: the SOVEREIGN INTEGER-DETERMINISTIC 3D PIPELINE (closes the GRAPHICS-API "3D pipeline"
gap -- the LAST software OS gap). Extends nx_raster (2D edge-function fill) to a real 3D renderer, 100% integer/
fixed-point => BIT-EXACT on ANY CPU (x86/RISC-V/no-FPU), NO GPU/driver. Stages:
(1) TRANSFORM -- fixed-point (Q8) rotate-around-Y + translate (model->view).
(2) PROJECT -- perspective projection: screen = center + focal*coord / view_z (the divide-by-z that makes 3D).
(3) Z-BUFFER -- per-pixel depth test (barycentric-interpolated view-z); nearer occludes farther = hidden-surface
removal, the defining 3D feature.
(4) SHADE -- integer flat Lambert: intensity = dot(faceNormal, light) normalized by integer isqrt.
T1 projection KAT (a view point projects to the exact screen pixel).
T2 Z-BUFFER occlusion is ORDER-INDEPENDENT (near occludes far whether drawn near-first or far-first).
T3 full pipeline renders a tetrahedron (transform->project->z-buffer); a real image, front faces occlude back.
T4 (EXCEED) determinism -- render twice -> BIT-IDENTICAL framebuffer + z-buffer checksum.
T5 flat-shading KAT (normal-toward-light=full, perpendicular=dark) + bounds (no OOB writes).
expect_exit: 0 Sovereign: nx_syscalls. NEVER-BRICK: software render, writes 0 GPU firmware.
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 16 | const K_MAGIC_5381: i64 = 5381 |
| 22 | const W: i64 = 64 |
| 23 | const H: i64 = 64 |
| 24 | const SCALE: i64 = 256 // Q8 fixed-point |
| 25 | const FOCAL: i64 = 350 |
| 26 | const CAMZ: i64 = 420 // push the model in front of the camera |
| 27 | const CX: i64 = 32 |
| 28 | const CY: i64 = 32 |
| 29 | const ZINF: i64 = 1000000000 |
| 31 | const SIN30: i64 = 128 |
| 32 | const COS30: i64 = 222 |
functions
| 18 | func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } |
| 19 | func g_pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var x: i64=v; if x<0{b[0]=45;sys_write(1,b,1);x=0-x} if x==0{b[0]=48;sys_write(1,b,1);return 0} var d: i64=0; var y: i64=x; while y>0{d=d+1;y=y/10} var i: i64=d-1; y=x; while i>=0{b[i]=(48+(y%10)) as u8;y=y/10;i=i-1} sys_write(1,b,d); return 0 } |
| 20 | func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c } |
| 34 | func isqrt(n: i64) -> i64 { if n<=0 { return 0 } var x: i64=n; var y: i64=(x+1)/2; while y<x { x=y; y=(x+n/x)/2 } return x } called by 1: shade |
| 35 | func fb_clear(fb: *i64, zb: *i64) -> i64 { var i: i64=0; while i<W*H { fb[i]=0; zb[i]=ZINF; i=i+1 } return 0 } |
| 36 | func fb_get(fb: *i64, x: i64, y: i64) -> i64 { return fb[y*W+x] } called by 1: main |
| 37 | func zb_get(zb: *i64, x: i64, y: i64) -> i64 { return zb[y*W+x] } |
| 38 | func fb_filled(fb: *i64) -> i64 { var c: i64=0; var i: i64=0; while i<W*H { if fb[i]!=0 { c=c+1 } i=i+1 } return c } called by 1: render_tetra |
| 39 | func cksum(fb: *i64) -> i64 { var h: i64=K_MAGIC_5381; var i: i64=0; while i<W*H { h=((h<<5)+h)+fb[i]; i=i+1 } return h } called by 1: main |
| 42 | func transform(wx: i64, wy: i64, wz: i64, vout: *i64) -> i64 called by 1: render_tetra |
| 49 | func project(vx: i64, vy: i64, vz: i64, sout: *i64) -> i64 |
| 56 | func edge(ax: i64, ay: i64, bx: i64, by: i64, px: i64, py: i64) -> i64 { return (bx-ax)*(py-ay) - (by-ay)*(px-ax) } called by 1: draw_tri3d |
| 59 | func draw_tri3d(fb: *i64, zb: *i64, x0: i64, y0: i64, z0: i64, x1: i64, y1: i64, z1: i64, x2: i64, y2: i64, z2: i64, color: i64) -> i64 |
| 90 | func shade(cr: i64, ax: i64, ay: i64, az: i64, bx: i64, by: i64, bz: i64, cx: i64, cy: i64, cz: i64, lx: i64, ly: i64, lz: i64) -> i64 |
| 106 | func render_tetra(fb: *i64, zb: *i64) -> i64 |
| 140 | func main() -> i64 |