code wiki / _hdl_build / nx_aa_gate.nx

nx_aa_gate.nx source

↩ module page · 74 lines · 3986 B

1// nx_aa_gate.nx -- ★F3 ANTI-ALIASING (SSAA 2x): render at 2x + box-downsample -> smooth edges. T1 AA replaces HARD 2// bg->object edge transitions with soft gradients (far fewer hard transitions). T2 determinism. T3 gallery (aliased | AA). 3// license_tier: ORIGINAL expect_exit: 0 4import "nx_syscalls.nx" 5import "nx_png.nx" 6import "nx_trimesh.nx" 7 8func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 } 10func clearfb(fb: *i64, n: i64, c: i64) -> i64 { var i: i64=0; while i<n { fb[i]=c; i=i+1 } return 0 } 11func sum3(p: i64) -> i64 { return (p&255) + ((p>>8)&255) + ((p>>16)&255) } 12// count HARD edge transitions: adjacent x-pixels where one is bg and the other is a bright object pixel (sum>300) 13func hardtrans(fb: *i64, W: i64, H: i64, bg: i64) -> i64 { 14 var c: i64=0; var y: i64=0 15 while y<H { var x: i64=0; while x<W-1 { 16 let p: i64=fb[y*W+x]; let q: i64=fb[y*W+x+1] 17 if p==bg { if sum3(q)>300 { c=c+1 } } 18 if q==bg { if sum3(p)>300 { c=c+1 } } 19 x=x+1 } y=y+1 } 20 return c 21} 22 23const BG: i64 = 24 + 26*256 + 34*65536 24const W: i64 = 280 25const H: i64 = 300 26 27func main() -> i64 { 28 hw("=== nx_aa_gate -- F3 SSAA anti-aliasing ===\n" as *u8) 29 var fails: i64 = 0 30 let npx: i64 = W*H 31 let fb: *i64 = sys_mmap(npx*8) as *i64 32 let zb: *i64 = sys_mmap(npx*8) as *i64 33 // a cube at a diagonal angle (edges at ~45 deg show the staircase most) 34 tm_reset() 35 tm_cube(0, 0, 0, 150, 120 + 150*256 + 210*65536) 36 37 clearfb(fb, npx, BG); trimesh_zclear(zb, npx); trimesh_render(fb, zb, W, H, 3000, 0-500, 1400, 520, 0) 38 let alias: i64 = hardtrans(fb, W, H, BG) 39 clearfb(fb, npx, BG); trimesh_render_aa(fb, W, H, 3000, 0-500, 1400, 520, 0) 40 let aa: i64 = hardtrans(fb, W, H, BG) 41 hw(" hard bg->object edge transitions: aliased="); pn(alias); hw(" AA="); pn(aa); hw("\n" as *u8) 42 var t1: i64 = 0 43 if aa*2 < alias { t1 = 1 } // AA more than halves the hard edges (soft gradients instead) 44 if t1 == 1 { hw("T1 PASS ANTI-ALIASING: SSAA replaces hard edges with smooth gradients ("); pn(aa); hw(" vs aliased "); pn(alias); hw(")\n" as *u8) } 45 else { fails=fails+1; hw("T1 FAIL aliased="); pn(alias); hw(" AA="); pn(aa); hw("\n" as *u8) } 46 47 // T2 determinism 48 let f2: *i64 = sys_mmap(npx*8) as *i64 49 clearfb(f2, npx, BG); trimesh_render_aa(f2, W, H, 3000, 0-500, 1400, 520, 0) 50 var diff: i64=0; var i: i64=0 51 while i<npx { if f2[i]!=fb[i] { diff=diff+1 } i=i+1 } 52 var t2: i64=0 53 if diff==0 { t2=1 } 54 if t2==1 { hw("T2 PASS deterministic\n" as *u8) } else { fails=fails+1; hw("T2 FAIL diff="); pn(diff); hw("\n" as *u8) } 55 56 // T3 gallery: aliased | AA (of the cube) + a smooth icosphere AA for good measure 57 let GW: i64 = W*2 58 let gal: *i64 = sys_mmap(GW*H*8) as *i64 59 clearfb(gal, GW*H, BG) 60 let cell: *i64 = sys_mmap(npx*8) as *i64 61 clearfb(cell,npx,BG); trimesh_zclear(zb,npx); trimesh_render(cell,zb,W,H,3000,0-500,1400,520,0) 62 var y: i64=0 63 while y<H { var x: i64=0; while x<W { gal[y*GW+x]=cell[y*W+x]; x=x+1 } y=y+1 } 64 clearfb(cell,npx,BG); trimesh_render_aa(cell,W,H,3000,0-500,1400,520,0) 65 y=0 66 while y<H { var x: i64=0; while x<W { gal[y*GW+W+x]=cell[y*W+x]; x=x+1 } y=y+1 } 67 write_png(gal, GW, H, "knowledge/nx_aa.png" as *u8) 68 hw("T3 gallery -> knowledge/nx_aa.png (aliased | anti-aliased)\n" as *u8) 69 70 if fails == 0 { hw("AA-GATE GREEN -- F3 SSAA 2x anti-aliasing (trimesh_render_aa): hard staircased edges -> smooth. Census anti-aliasing GAP->HAVE. Residual: MSAA (cheaper) + temporal AA.\n" as *u8); sys_exit(0); return 0 } 71 hw("AA-GATE RED fails="); pn(fails); hw("\n" as *u8) 72 sys_exit(1) 73 return 1 74}