nx_raster.nx source
↩ module page · 74 lines · 5294 B
1// nx_raster.nx -- SOVEREIGN INTEGER-DETERMINISTIC RASTERIZER (the render-pipeline seed; the better-than-DirectX core).
2// An edge-function triangle rasterizer over an INTEGER packed-RGB framebuffer. 100% integer -> BIT-EXACT, the SAME
3// framebuffer on ANY CPU (native x86 on this laptop, RISC-V, no-FPU MCU) with NO GPU/driver/API. DirectX/Vulkan/
4// Metal are float + GPU-driver-bound -> frames differ across hardware (non-deterministic); we are reproducible.
5// T1 raster a triangle -> filled pixels > 0. T2 an inside pixel is filled, an outside corner is not.
6// T3 (EXCEED) determinism -- two rasterizations give a BIT-IDENTICAL framebuffer checksum. T4 bounds (no OOB).
7// expect_exit: 0 Sovereign: nx_syscalls. NEVER-BRICK: software raster, writes 0 GPU firmware.
8import "nx_syscalls.nx"
9const K_MAGIC_5381: i64 = 5381
10
11func 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 }
12func 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 }
13func 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 }
14
15const W: i64 = 32
16const H: i64 = 32
17func fb_clear(fb: *i64) -> i64 { var i: i64=0; while i<W*H { fb[i]=0; i=i+1 } return 0 }
18func edge(ax: i64, ay: i64, bx: i64, by: i64, px: i64, py: i64) -> i64 { return (bx-ax)*(py-ay) - (by-ay)*(px-ax) }
19func draw_tri(fb: *i64, x0: i64, y0: i64, x1: i64, y1: i64, x2: i64, y2: i64, color: i64) -> i64 {
20 var py: i64=0
21 while py<H { var px: i64=0
22 while px<W {
23 let e0: i64=edge(x0,y0,x1,y1,px,py); let e1: i64=edge(x1,y1,x2,y2,px,py); let e2: i64=edge(x2,y2,x0,y0,px,py)
24 var pp: i64=0; if e0>=0 { if e1>=0 { if e2>=0 { pp=1 } } }
25 var np: i64=0; if e0<=0 { if e1<=0 { if e2<=0 { np=1 } } }
26 if pp==1 { fb[py*W+px]=color } else { if np==1 { fb[py*W+px]=color } }
27 px=px+1
28 }
29 py=py+1
30 }
31 return 0
32}
33func fb_get(fb: *i64, x: i64, y: i64) -> i64 { return fb[y*W+x] }
34func 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 }
35func fb_checksum(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 }
36
37func main() -> i64 {
38 g_puts("nx_raster (SOVEREIGN integer-deterministic rasterizer -- bit-exact frames on any CPU, no GPU/driver)\n" as *u8)
39 var pass: i64=0; var total: i64=0
40 let fb: *i64 = sys_mmap(W*H*8) as *i64
41
42 fb_clear(fb)
43 draw_tri(fb, 4,4, 28,8, 10,28, 0xFF0000) // a red triangle
44 let filled: i64 = fb_filled(fb)
45 g_puts(" rasterized triangle (4,4)-(28,8)-(10,28): filled "); g_pn(filled); g_puts(" / "); g_pn(W*H); g_puts(" pixels\n" as *u8)
46 var t1: i64=0; if filled>50 { t1=1 }
47 pass=pass+ck("T1: a triangle rasterizes -- a substantial pixel area is filled" as *u8, t1); total=total+1
48
49 var t2: i64=0; if fb_get(fb,14,13)==0xFF0000 { if fb_get(fb,0,0)==0 { if fb_get(fb,31,31)==0 { t2=1 } } }
50 g_puts(" inside(14,13)="); g_pn(fb_get(fb,14,13)); g_puts(" corner(0,0)="); g_pn(fb_get(fb,0,0)); g_puts(" corner(31,31)="); g_pn(fb_get(fb,31,31)); g_puts("\n" as *u8)
51 pass=pass+ck("T2: an inside pixel is filled (0xFF0000); outside corners are NOT (coverage correct)" as *u8, t2); total=total+1
52
53 let cs1: i64 = fb_checksum(fb)
54 let fb2: *i64 = sys_mmap(W*H*8) as *i64; fb_clear(fb2); draw_tri(fb2, 4,4, 28,8, 10,28, 0xFF0000)
55 let cs2: i64 = fb_checksum(fb2)
56 g_puts(" framebuffer checksum run1="); g_pn(cs1); g_puts(" run2="); g_pn(cs2); g_puts(" (BIT-EXACT -> identical on any CPU: x86/RISC-V/no-FPU)\n" as *u8)
57 var t3: i64=0; if cs1==cs2 { t3=1 }
58 pass=pass+ck("T3 (EXCEED): DETERMINISTIC -- two rasterizations give a BIT-IDENTICAL framebuffer (DirectX/GPU cannot guarantee this)" as *u8, t3); total=total+1
59
60 // T4 bounds: a tiny off-screen triangle fills nothing OOB (clear stays clean outside)
61 let fb3: *i64 = sys_mmap(W*H*8) as *i64; fb_clear(fb3); draw_tri(fb3, 1,1, 3,1, 1,3, 0x00FF00)
62 var oob: i64=0; if fb_get(fb3,20,20)!=0 { oob=1 } // far pixel must stay 0
63 var t4: i64=0; if oob==0 { if fb_filled(fb3)>0 { t4=1 } }
64 pass=pass+ck("T4: bounds -- a small triangle fills only its area; far pixels stay clear (no OOB writes)" as *u8, t4); total=total+1
65
66 var okall: i64=0; if pass==total { okall=1 }
67 g_puts("---- nx_raster: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n" as *u8)
68 if okall==1 {
69 let logf: i64=sys_openat_append("knowledge/status/raster.log" as *u8, 420)
70 if logf>=0 { let z: i64=sys_write(logf,"NXRASTER GREEN: integer edge-function triangle rasterizer; BIT-EXACT deterministic framebuffer (any CPU, no GPU)\n" as *u8,108); sys_close(logf) }
71 g_puts("verdict=GREEN (sovereign integer-deterministic rasterizer: bit-exact frames on any hardware, no GPU/driver; the better-than-DirectX render seed)\n" as *u8); sys_exit(0); return 0
72 }
73 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
74}