code wiki / (root) / nx_gsplat_tile_gate.nx

nx_gsplat_tile_gate.nx source

↩ module page · 325 lines · 17686 B

1// nx_gsplat_tile_gate.nx -- GATE for the tiled rasterization policy. 2// 3// ★★THE ACCEPT RULE WAS DECLARED IN THE ORGAN'S SOURCE BEFORE THIS GATE WAS RUN, and this file only 4// enforces it: 5// CORRECTNESS -- the tiled frame is BYTE-IDENTICAL to the full-screen frame. An optimisation is allowed 6// to change cost and NOTHING else. "Close enough" would mean the two renderers had 7// quietly become two rulers. 8// COST -- strictly fewer conic evaluations on a scene with real occlusion. 9// T4 is the honest counterweight: on a scene with NO occlusion the policy must save NOTHING and still be 10// byte-identical. A win that appears on every scene regardless of occlusion would mean the saving came 11// from somewhere other than the mechanism claimed, and that is the failure this tooth exists to catch. 12// 13// T3 replaces the question "what tile size?" with a MEASUREMENT. The organ names no favourite edge; the 14// gate sweeps a doubling series and publishes evaluations and list occupancy at each, and requires 15// byte-identity at EVERY size. A constant nobody measured would have been the alternative. 16// license_tier: ORIGINAL expect_exit: 0 17import "nx_syscalls.nx" 18import "nx_gsplat.nx" 19import "nx_gsplat_tile_lib.nx" 20import "nx_png.nx" 21import "nx_gate_verdict.nx" 22 23const TG_CAP: i64 = 2048 24const TG_LAYERS: i64 = 24 25const TG_PERLAYER: i64 = 60 26const TG_GRIDX: i64 = 10 27const TG_BUDGET: i64 = 1000000 28const TG_CAMZ: i64 = 14 29const TG_SWEEP0: i64 = 8 30// worker budget for the parallel arm. A FIXTURE choice, declared: the gate says how many it asked for and 31// then proves each one actually did work, so an idle worker cannot hide inside the total. 32const TG_WORKERS: i64 = 4 33 34// A DENSE, OCCLUDING scene: many opaque layers stacked in depth, the front one covering the view. The 35// layers behind it cannot change a pixel, which is exactly the work the tiled policy should decline. 36func tg_dense(gauss: *i64) -> i64 { 37 var n: i64 = 0 38 var l: i64 = 0 39 while l < TG_LAYERS { 40 var i: i64 = 0 41 while i < TG_PERLAYER { 42 let gx: i64 = 0 - 9000 + (i % TG_GRIDX)*2000 43 let gy: i64 = 0 - 4000 + (i / TG_GRIDX)*1600 44 let gz: i64 = 0 - l*900 45 gs_set_aniso(gauss, n, gx, gy, gz, 0, 0, 0-256, 900, 200 - l*4, 120 + i, 180, 256) 46 n = n + 1 47 i = i + 1 48 } 49 l = l + 1 50 } 51 return n 52} 53 54// A SPARSE scene: a handful of small, faint splats that never saturate anything. The control. 55func tg_sparse(gauss: *i64) -> i64 { 56 var i: i64 = 0 57 while i < 12 { 58 gs_set_aniso(gauss, i, 0 - 9000 + i*1600, 2000, 0 - i*300, 0, 0, 0-256, 260, 200, 140, 90, 40) 59 i = i + 1 60 } 61 return 12 62} 63 64// the conic evaluations the FULL-SCREEN policy performs: the clipped AABB area of every visible splat. 65// This is exactly the rectangle gs_render_aniso walks, computed from the same projection, so the two 66// renderers are compared in the same currency without a stopwatch. 67func tg_fullscreen_evals(vis: i64, sxb: *i64, syb: *i64, pa: *i64, pc: *i64, order: *i64) -> i64 { 68 let W: i64 = gs_w() 69 let H: i64 = gs_h() 70 var e: i64 = 0 71 var oi: i64 = 0 72 while oi < vis { 73 let g: i64 = order[oi] 74 let rx: i64 = gs_splat_rx(pa[g]) 75 let ry: i64 = gs_splat_ry(pc[g]) 76 var x0: i64 = sxb[g] - rx 77 if x0 < 0 { x0 = 0 } 78 var x1: i64 = sxb[g] + rx 79 if x1 >= W { x1 = W - 1 } 80 var y0: i64 = syb[g] - ry 81 if y0 < 0 { y0 = 0 } 82 var y1: i64 = syb[g] + ry 83 if y1 >= H { y1 = H - 1 } 84 if x0 <= x1 { if y0 <= y1 { e = e + (x1-x0+1)*(y1-y0+1) } } 85 oi = oi + 1 86 } 87 return e 88} 89 90func tg_same(a: *i64, b: *i64, n: i64) -> i64 { 91 var i: i64 = 0 92 while i < n { if a[i] != b[i] { return 0 } i = i + 1 } 93 return 1 94} 95 96func main() -> i64 { 97 let ctr: *i64 = gv_ctr() 98 gv_head("nx_gsplat_tile_gate -- tiled rasterization: byte-identical output, strictly less work" as *u8) 99 100 let W: i64 = gs_w() 101 let H: i64 = gs_h() 102 let npx: i64 = W*H 103 let st: i64 = gs_stride_aniso() 104 let gauss: *i64 = sys_mmap(TG_CAP*st*8) as *i64 105 let fbA: *i64 = sys_mmap(npx*8) as *i64 106 let fbB: *i64 = sys_mmap(npx*8) as *i64 107 let acc: *i64 = sys_mmap(npx*3*8) as *i64 108 let trn: *i64 = sys_mmap(npx*8) as *i64 109 let dep: *i64 = sys_mmap(TG_CAP*8) as *i64 110 let sxb: *i64 = sys_mmap(TG_CAP*8) as *i64 111 let syb: *i64 = sys_mmap(TG_CAP*8) as *i64 112 let pa: *i64 = sys_mmap(TG_CAP*8) as *i64 113 let pb: *i64 = sys_mmap(TG_CAP*8) as *i64 114 let pc: *i64 = sys_mmap(TG_CAP*8) as *i64 115 let pdt: *i64 = sys_mmap(TG_CAP*8) as *i64 116 let ord: *i64 = sys_mmap(TG_CAP*8) as *i64 117 let cnt: *i64 = sys_mmap((gs_nb()+2)*8) as *i64 118 let elut: *i64 = sys_mmap(gs_expn()*8) as *i64 119 gs_build_explut(elut) 120 121 let ng: i64 = tg_dense(gauss) 122 gv_puts(" dense scene: " as *u8); gv_num(ng) 123 gv_puts(" opaque splats in " as *u8); gv_num(TG_LAYERS) 124 gv_puts(" depth layers, framebuffer " as *u8); gv_num(W); gv_puts("x" as *u8); gv_num(H); gv_puts("\n" as *u8) 125 126 // the incumbent full-screen frame, and its cost in the shared currency 127 let vis: i64 = gs_render_aniso(gauss, ng, 0, TG_CAMZ, fbA, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44) 128 let fsevals: i64 = tg_fullscreen_evals(vis, sxb, syb, pa, pc, ord) 129 gv_puts(" full-screen: visible=" as *u8); gv_num(vis) 130 gv_puts(" conic_evaluations=" as *u8); gv_num(fsevals); gv_puts("\n" as *u8) 131 132 // T1 the binding tooth: BYTE-IDENTICAL frames. 133 let r1: i64 = gt_init(32, TG_BUDGET, TG_WORKERS) 134 gt_render(gauss, ng, 0, TG_CAMZ, fbB, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44, 1) 135 let identical: i64 = tg_same(fbA, fbB, npx) 136 gv_puts(" tiled(edge 32): conic_evaluations=" as *u8); gv_num(gt_evals()) 137 gv_puts(" entries=" as *u8); gv_num(gt_entries()) 138 gv_puts(" splat-tile pairs skipped=" as *u8); gv_num(gt_skipped()) 139 gv_puts(" arena_bytes=" as *u8); gv_num(gt_bytes()); gv_puts("\n" as *u8) 140 var t1: i64 = 0 141 if r1 == 0 { if identical == 1 { t1 = 1 } } 142 gv_check("T1 CORRECTNESS: the tiled frame is BYTE-IDENTICAL to the full-screen frame" as *u8, t1, ctr) 143 144 // T2 the cost half of the accept rule. 145 var t2: i64 = 0 146 if gt_evals() < fsevals { if gt_skipped() > 0 { t2 = 1 } } 147 gv_puts(" cost: full-screen " as *u8); gv_num(fsevals) 148 gv_puts(" -> tiled " as *u8); gv_num(gt_evals()) 149 gv_puts(" evaluations, saved " as *u8); gv_num(fsevals - gt_evals()) 150 if fsevals > 0 { gv_puts(" (" as *u8); gv_num((fsevals - gt_evals())*1000/fsevals); gv_puts(" permil)" as *u8) } 151 gv_puts("\n" as *u8) 152 gv_check("T2 COST: strictly fewer conic evaluations than the full-screen policy on an occluding scene" as *u8, t2, ctr) 153 154 // T3 the tile edge is a MEASUREMENT, not a constant: sweep a doubling series, publish the curve, and 155 // require byte-identity at every size. This is what the organ declining to name a favourite buys. 156 var sweep_ok: i64 = 1 157 var sizes: i64 = 0 158 var best_edge: i64 = 0 159 var best_evals: i64 = 0 160 var edge: i64 = TG_SWEEP0 161 while edge <= H { 162 let ri: i64 = gt_init(edge, TG_BUDGET, TG_WORKERS) 163 if ri != 0 { sweep_ok = 0 } else { 164 gt_render(gauss, ng, 0, TG_CAMZ, fbB, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44, 1) 165 let same: i64 = tg_same(fbA, fbB, npx) 166 if same == 0 { sweep_ok = 0 } 167 gv_puts(" edge=" as *u8); gv_num(edge) 168 gv_puts(" tiles=" as *u8); gv_num(gt_tiles()) 169 gv_puts(" evals=" as *u8); gv_num(gt_evals()) 170 gv_puts(" entries=" as *u8); gv_num(gt_entries()) 171 gv_puts(" identical=" as *u8); gv_num(same); gv_puts("\n" as *u8) 172 if best_edge == 0 { best_edge = edge; best_evals = gt_evals() } 173 if gt_evals() < best_evals { best_edge = edge; best_evals = gt_evals() } 174 sizes = sizes + 1 175 } 176 edge = edge*2 177 } 178 gv_puts(" sweep: " as *u8); gv_num(sizes) 179 gv_puts(" tile sizes measured, cheapest edge=" as *u8); gv_num(best_edge) 180 gv_puts(" at " as *u8); gv_num(best_evals); gv_puts(" evaluations\n" as *u8) 181 var t3: i64 = 0 182 if sweep_ok == 1 { if sizes > 1 { t3 = 1 } } 183 gv_check("T3 tile size is a MEASUREMENT: every size in the sweep renders byte-identically, curve published" as *u8, t3, ctr) 184 185 // T4 ★THE HONEST COUNTERWEIGHT: with NO occlusion the policy must save NOTHING and still be identical. 186 // A saving that appeared here too would mean the win did not come from the mechanism claimed. 187 let ngs: i64 = tg_sparse(gauss) 188 let vis2: i64 = gs_render_aniso(gauss, ngs, 0, TG_CAMZ, fbA, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44) 189 let fsev2: i64 = tg_fullscreen_evals(vis2, sxb, syb, pa, pc, ord) 190 gt_init(32, TG_BUDGET, TG_WORKERS) 191 gt_render(gauss, ngs, 0, TG_CAMZ, fbB, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44, 1) 192 let same2: i64 = tg_same(fbA, fbB, npx) 193 gv_puts(" NO-OCCLUSION control: full-screen " as *u8); gv_num(fsev2) 194 gv_puts(" vs tiled " as *u8); gv_num(gt_evals()) 195 gv_puts(" evaluations, skipped=" as *u8); gv_num(gt_skipped()) 196 gv_puts(" identical=" as *u8); gv_num(same2); gv_puts("\n" as *u8) 197 var t4: i64 = 0 198 if same2 == 1 { if gt_skipped() == 0 { if gt_evals() == fsev2 { t4 = 1 } } } 199 gv_check("T4 neg-control-no-occlusion: with nothing to occlude the policy saves NOTHING and stays identical" as *u8, t4, ctr) 200 201 // T5 the deny-guards, both directions. 202 let g1: i64 = gt_init(gt_edge_min() - 1, TG_BUDGET, TG_WORKERS) 203 let g2: i64 = gt_init(W + 1, TG_BUDGET, TG_WORKERS) 204 let g3: i64 = gt_init(32, 0, TG_WORKERS) 205 let g4: i64 = gt_init(32, TG_BUDGET, TG_WORKERS) 206 gv_puts(" guards: edge_below_floor=" as *u8); gv_num(g1) 207 gv_puts(" edge_past_framebuffer=" as *u8); gv_num(g2) 208 gv_puts(" empty_budget=" as *u8); gv_num(g3) 209 gv_puts(" POSITIVE CONTROL valid=" as *u8); gv_num(g4); gv_puts("\n" as *u8) 210 var t5: i64 = 0 211 if g1 == 0 - 3 { if g2 == 0 - 3 { if g3 == 0 - 5 { if g4 == 0 { t5 = 1 } } } } 212 gv_check("T5 guards NAME their rule and a valid configuration is ACCEPTED (not a guard that refuses everything)" as *u8, t5, ctr) 213 214 // T6 an entry budget too small for the scene REFUSES rather than rendering a truncated frame, and the 215 // organ is still usable afterwards -- the refusal is total, not a half-applied state. 216 let ngd: i64 = tg_dense(gauss) 217 gt_init(TG_SWEEP0, 1, TG_WORKERS) 218 let rtrunc: i64 = gt_render(gauss, ngd, 0, TG_CAMZ, fbB, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44, 1) 219 // ⚠THE REFERENCE MUST BE RE-RENDERED FOR *THIS* SCENE. An earlier revision of this tooth compared the 220 // recovered dense frame against fbA while fbA still held the SPARSE control frame from T4 -- two 221 // unrelated images, which differ for reasons that have nothing to do with the refusal. It passed, and 222 // it proved nothing. ASSERT THE FIXTURE REACHED THE CONDITION BEFORE ASSERTING THE OUTCOME. 223 gs_render_aniso(gauss, ngd, 0, TG_CAMZ, fbA, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44) 224 gt_init(32, TG_BUDGET, TG_WORKERS) 225 gt_render(gauss, ngd, 0, TG_CAMZ, fbB, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44, 1) 226 let recovered: i64 = tg_same(fbA, fbB, npx) 227 gv_puts(" starved budget: render returned " as *u8); gv_num(rtrunc) 228 gv_puts(", and after re-init the SAME scene renders correctly again=" as *u8); gv_num(recovered); gv_puts("\n" as *u8) 229 var t6: i64 = 0 230 if rtrunc == 0 - 4 { if recovered == 1 { t6 = 1 } } 231 gv_check("T6 an insufficient entry budget REFUSES rather than silently truncating the tile lists" as *u8, t6, ctr) 232 233 // T7 determinism 234 gt_render(gauss, ngd, 0, TG_CAMZ, fbA, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44, 1) 235 let ev1: i64 = gt_evals() 236 gt_render(gauss, ngd, 0, TG_CAMZ, fbB, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44, 1) 237 var t7: i64 = 0 238 if tg_same(fbA, fbB, npx) == 1 { if ev1 == gt_evals() { t7 = 1 } } 239 write_png(fbB, W, H, "knowledge/nx_gsplat_tiled.png" as *u8) 240 gv_puts(" determinism: two renders byte-equal, evaluations " as *u8); gv_num(ev1) 241 gv_puts(" both times; artifact knowledge/nx_gsplat_tiled.png\n" as *u8) 242 gv_check("T7 deterministic: identical frame AND identical work on a repeat render" as *u8, t7, ctr) 243 244 // T8 the diagnostic path. A refusal whose message length computes to zero prints NOTHING, and every 245 // guard tooth above still passes because they only read return codes -- so the organ would refuse 246 // silently and the operator would see a failure with no reason. nx_gate_bite SURVIVED a mutant here on 247 // the first run, the same hole the 4D gate had: each organ rolls its own string length because the 248 // estate has no shared one (measured: zero matches for sys_strlen across 23,047 sources; debt filed). 249 var t8: i64 = 0 250 if gt_slen("" as *u8) == 0 { if gt_slen("ab" as *u8) == 2 { t8 = 1 } } 251 gv_puts(" diagnostic path: slen(empty)=" as *u8); gv_num(gt_slen("" as *u8)) 252 gv_puts(" slen(ab)=" as *u8); gv_num(gt_slen("ab" as *u8)); gv_puts("\n" as *u8) 253 gv_check("T8 a refusal that cannot measure its own message prints nothing: the length function is correct" as *u8, t8, ctr) 254 255 // ---------------- THE PARALLEL RUNG ---------------- 256 // acc/trans/fb for this arm are MAP_SHARED. A PRIVATE mapping would give every forked worker its own 257 // copy-on-write pages: each child would render its tiles into an image the parent never sees, the 258 // parent would resolve an EMPTY frame, and every worker would still report real evaluations. That 259 // failure reads as a correctness bug in the blend rather than a mapping mistake, which is exactly why 260 // the contract is stated in the lib and asserted here instead of assumed. 261 let accS: *i64 = sys_mmap_shared(npx*3*8) as *i64 262 let trnS: *i64 = sys_mmap_shared(npx*8) as *i64 263 let fbS: *i64 = sys_mmap_shared(npx*8) as *i64 264 let ngp: i64 = tg_dense(gauss) 265 gt_init(32, TG_BUDGET, TG_WORKERS) 266 let t0: i64 = sys_clock_now_us() 267 gt_render(gauss, ngp, 0, TG_CAMZ, fbA, acc, trn, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44, 1) 268 let t1: i64 = sys_clock_now_us() 269 let ser_evals: i64 = gt_evals() 270 gt_init(32, TG_BUDGET, TG_WORKERS) 271 let t2: i64 = sys_clock_now_us() 272 gt_render(gauss, ngp, 0, TG_CAMZ, fbS, accS, trnS, dep, sxb, syb, pa, pb, pc, pdt, ord, cnt, elut, 26, 28, 44, TG_WORKERS) 273 let t3: i64 = sys_clock_now_us() 274 let par_evals: i64 = gt_evals() 275 // ★ELAPSED IS DATA, NOT A TOOTH. Wall-clock depends on how many cores this host has free right now, so 276 // a threshold on it would be a verdict about the machine rather than about the code. It is published 277 // next to the eval counts so a reader can see whether the 4-way SPLIT (which IS asserted) actually 278 // converted into time on THIS box. 279 let ser_us: i64 = t1 - t0 280 let par_us: i64 = t3 - t2 281 var speedup_permil: i64 = 0 282 if par_us > 0 { speedup_permil = ser_us*1000/par_us } 283 gv_puts(" ---- WALL CLOCK: serial_us=" as *u8); gv_num(ser_us) 284 gv_puts(" parallel_us=" as *u8); gv_num(par_us) 285 gv_puts(" speedup_permil=" as *u8); gv_num(speedup_permil) 286 gv_puts(" (1000 = no gain; machine-dependent, reported not asserted)\n" as *u8) 287 let same_par: i64 = tg_same(fbA, fbS, npx) 288 gv_puts(" PARALLEL: workers=" as *u8); gv_num(TG_WORKERS) 289 gv_puts(" serial_evals=" as *u8); gv_num(ser_evals) 290 gv_puts(" parallel_evals=" as *u8); gv_num(par_evals) 291 gv_puts(" byte_identical=" as *u8); gv_num(same_par); gv_puts("\n" as *u8) 292 gv_check("T9 the PARALLEL frame is BYTE-IDENTICAL to the serial one -- forking changed cost, not pixels" as *u8, same_par, ctr) 293 294 var busy: i64 = 0 295 var wsum: i64 = 0 296 var w: i64 = 0 297 while w < TG_WORKERS { 298 let we: i64 = gt_worker_evals(w) 299 wsum = wsum + we 300 if we > 0 { busy = busy + 1 } 301 gv_puts(" worker " as *u8); gv_num(w); gv_puts(" evals=" as *u8); gv_num(we); gv_puts("\n" as *u8) 302 w = w + 1 303 } 304 var spread: i64 = 0 305 if busy == TG_WORKERS { spread = 1 } 306 gv_check("T10 the work is SPREAD: every declared worker performed evaluations, none idled" as *u8, spread, ctr) 307 308 var reconciled: i64 = 0 309 if wsum == par_evals { if par_evals == ser_evals { reconciled = 1 } } 310 gv_puts(" reconcile: sum(workers)=" as *u8); gv_num(wsum) 311 gv_puts(" parallel_total=" as *u8); gv_num(par_evals) 312 gv_puts(" serial_total=" as *u8); gv_num(ser_evals); gv_puts("\n" as *u8) 313 gv_check("T11 the totals RECONCILE: per-worker sum == parallel total == serial total (no work lost or double-counted)" as *u8, reconciled, ctr) 314 315 let wbad: i64 = gt_init(32, TG_BUDGET, 0) 316 let wgood: i64 = gt_init(32, TG_BUDGET, TG_WORKERS) 317 gv_puts(" worker guard: zero=" as *u8); gv_num(wbad); gv_puts(" valid=" as *u8); gv_num(wgood); gv_puts("\n" as *u8) 318 var wg: i64 = 0 319 if wbad == 0 - 6 { if wgood == 0 { wg = 1 } } 320 gv_check("T12 worker guard NAMES its rule (-6) and has a POSITIVE CONTROL: a valid budget is accepted" as *u8, wg, ctr) 321 322 let rc: i64 = gv_verdict("GSPLAT-TILE-GATE" as *u8, ctr, "tiled rasterization is byte-identical to the full-screen policy and strictly cheaper where occlusion exists, with the tile size published as a curve rather than chosen as a constant" as *u8) 323 sys_exit(rc) 324 return rc 325}