code wiki / _hdl_build / nx_cull_lod_gate.nx

nx_cull_lod_gate.nx source

↩ module page · 63 lines · 3654 B

1// nx_cull_lod_gate.nx -- proves P3 culling+LOD with a MEASURED work reduction + neg-control. Scene of 20 2// triangles: 4 near-visible, 4 mid-visible, 4 far(distance-culled), 4 back-facing(culled), 4 out-of-frustum 3// (culled). Asserts survivor count, LOD weighting (near=4 > mid=2), and culled-work << brute. NEG-CONTROL: 4// an all-visible-near scene yields NO reduction (work==brute) -> proves the measure is honest, not always 5// claiming a win. license_tier: ORIGINAL expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_cull_lod.nx" 8 9func uw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func un(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 } 11 12func main() -> i64 { 13 let W: i64 = 100 14 let FAR: i64 = 100 15 let MID: i64 = 50 16 let T: i64 = 20 17 let nz: *i64 = sys_mmap(8 * 24) as *i64 18 let dist: *i64 = sys_mmap(8 * 24) as *i64 19 let sx: *i64 = sys_mmap(8 * 24) as *i64 20 var i: i64 = 0 21 while i < T { 22 if i < 4 { nz[i]=1; dist[i]=10; sx[i]=50 } // near visible 23 else { if i < 8 { nz[i]=1; dist[i]=60; sx[i]=50 } // mid visible 24 else { if i < 12{ nz[i]=1; dist[i]=200; sx[i]=50 } // FAR -> distance-culled 25 else { if i < 16{ nz[i]=0 - 1; dist[i]=10; sx[i]=50 } // back-facing -> culled 26 else { nz[i]=1; dist[i]=10; sx[i]=0 - 5 } } } } // out of frustum -> culled 27 i = i + 1 28 } 29 30 var pass: i64 = 0 31 var total: i64 = 0 32 33 let surv: i64 = cl_survivors(nz, dist, sx, T, FAR, W) 34 total=total+1; if surv == 8 { pass=pass+1 } else { uw("U1 FAIL survivors="); un(surv); uw(" want 8\n") } 35 36 // LOD weighting: near (dist 10) = 4, mid (dist 60) = 2, far (dist 200) = 1 37 total=total+1; if cl_lod_weight(10, MID, FAR) == 4 { pass=pass+1 } else { uw("U2 FAIL near LOD\n") } 38 total=total+1; if cl_lod_weight(60, MID, FAR) == 2 { pass=pass+1 } else { uw("U3 FAIL mid LOD\n") } 39 total=total+1; if cl_lod_weight(200, MID, FAR) == 1 { pass=pass+1 } else { uw("U4 FAIL far LOD\n") } 40 41 let work: i64 = cl_work(nz, dist, sx, T, FAR, W, MID) // 4*4 (near) + 4*2 (mid) = 24 42 let brute: i64 = cl_brute(T) // 20*4 = 80 43 total=total+1; if work == 24 { pass=pass+1 } else { uw("U5 FAIL work="); un(work); uw(" want 24\n") } 44 total=total+1; if work < brute { pass=pass+1 } else { uw("U6 FAIL no reduction\n") } 45 46 // NEG-CONTROL: all-visible-near scene -> NO reduction (work == brute) -> honest, not a fake win 47 let nz2: *i64 = sys_mmap(8 * 16) as *i64 48 let d2: *i64 = sys_mmap(8 * 16) as *i64 49 let sx2: *i64 = sys_mmap(8 * 16) as *i64 50 var j: i64 = 0 51 while j < 8 { nz2[j]=1; d2[j]=10; sx2[j]=50; j=j+1 } 52 let work2: i64 = cl_work(nz2, d2, sx2, 8, FAR, W, MID) // 8*4 = 32 53 let brute2: i64 = cl_brute(8) // 32 54 total=total+1; if work2 == brute2 { pass=pass+1 } else { uw("U7 FAIL neg-control reduced when nothing to cull\n") } 55 56 uw("=== CULL+LOD work (20-tri scene) ===\n") 57 uw(" survivors="); un(surv); uw("/"); un(T); uw(" culled+LOD work="); un(work); uw(" brute="); un(brute) 58 uw(" reduction="); un((brute - work) * 100 / brute); uw("%\n") 59 uw("CULL-LOD "); un(pass); uw("/"); un(total); uw("\n") 60 if pass == total { uw("CULL-LOD ALL-PASS (culling + LOD weighting + measured reduction + neg-control)\n"); sys_exit(0) } 61 sys_exit(1) 62 return 1 63}