code wiki / _hdl_build / nx_cull_lod.nx

nx_cull_lod.nx source

↩ module page · 41 lines · 1725 B

1// nx_cull_lod.nx -- P3 RESPONSIVENESS: sovereign CULLING + LOD, the "do less work" lever that the 2// research named as the top win you fully own on weak hardware. Back-face + distance + frustum culling 3// drop triangles BEFORE the pixel stage; LOD lowers detail with distance. Pure integer; the work it 4// avoids is MEASURED (survivor count + LOD-weighted work vs brute-force). 100% sovereign. license_tier: ORIGINAL 5 6// visible? back-face (normal_z<=0 faces away), distance (dist>far), frustum (sx outside [0,w)) all cull. 7func cl_visible(nz: i64, dist: i64, sx: i64, far: i64, w: i64) -> i64 { 8 if nz <= 0 { return 0 } 9 if dist > far { return 0 } 10 if sx < 0 { return 0 } 11 if sx >= w { return 0 } 12 return 1 13} 14 15// LOD weight: near=4 (full detail), mid=2, far=1 (coarse) -- fewer triangles per object with distance. 16func cl_lod_weight(dist: i64, mid: i64, far: i64) -> i64 { 17 if dist <= mid { return 4 } 18 if dist <= far { return 2 } 19 return 1 20} 21 22// total render work = sum of LOD weights over VISIBLE triangles (what the engine actually pays). 23func cl_work(nz: *i64, dist: *i64, sx: *i64, n: i64, far: i64, w: i64, mid: i64) -> i64 { 24 var sum: i64 = 0 25 var i: i64 = 0 26 while i < n { 27 if cl_visible(nz[i], dist[i], sx[i], far, w) == 1 { sum = sum + cl_lod_weight(dist[i], mid, far) } 28 i = i + 1 29 } 30 return sum 31} 32 33// brute force: every triangle at full detail (the no-culling, no-LOD baseline). 34func cl_brute(n: i64) -> i64 { return n * 4 } 35 36func cl_survivors(nz: *i64, dist: *i64, sx: *i64, n: i64, far: i64, w: i64) -> i64 { 37 var s: i64 = 0 38 var i: i64 = 0 39 while i < n { if cl_visible(nz[i], dist[i], sx[i], far, w) == 1 { s = s + 1 } i = i + 1 } 40 return s 41}