code wiki / _hdl_build / nx_ng_diffrender.nx
nx_ng_diffrender.nx
buildroot/runtime/_hdl_build/nx_ng_diffrender.nx
about
nx_ng_diffrender.nx -- CAP-DIFF-RENDER, the next missing generation past the sovereign-raster substrate
(nx_ng_gap: prereqs CAP-RASTER + CAP-NF-AUTOGRAD BUILT -> this is the adjacent-possible build). The FIRST
rung of differentiable rendering: gradients of rendered PIXELS w.r.t. a SCENE PARAMETER, the substrate
NeRF / 3D-Gaussian-Splatting optimization is built on.
Minimal honest proof (operator: measured, no wave; HARD evidence per the genealogy ladder): a 1D soft-edge
rasterizer renders an image from one scene param p (the edge position) in Q16 fixed-point. We then OPTIMIZE
p by gradient descent to match a target image (rendered at p*). Correctness is proven the rigorous way:
T1 DESCENT converges -- GD on p drives loss -> ~0 and p -> p* (the gradient points the right way),
T2 NEG-CONTROL diverges -- ASCENDING the same gradient INCREASES loss (the gradient has teeth/direction),
T3 finite-difference SIGN check -- sign(analytic dLoss/dp) == sign((L(p+e)-L(p-e))) (the gradient is real),
T4 zero-at-optimum -- loss(p*) == 0 (the render + loss are consistent).
Q16 fixed-point only (no float), sovereign syscalls only. license_tier: ORIGINAL expect_exit: 0
dependencies 1 imports · 0 importers
imports: nx_syscalls.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 16 | const Q: i64 = 65536 // 1.0 in Q16 |
| 17 | const HALF: i64 = 32768 // 0.5 |
| 18 | const NPX: i64 = 16 // canvas pixels |
| 19 | const INVS: i64 = 16384 // 1/s with softness s=4 px (Q16/4) |
| 20 | const PSTAR: i64 = 524288 // target edge position = 8.0 px |
| 21 | const P0: i64 = 196608 // initial guess = 3.0 px |
| 22 | const LR: i64 = 4096 // learning rate = 0.0625 (Q16) |
| 23 | const NSTEP: i64 = 120 |
| 24 | const EPS: i64 = 16384 // finite-diff step = 0.25 px |
| 25 | const PCHK: i64 = 393216 // sign-check point = 6.0 px |
| 26 | const TOL: i64 = 32768 // convergence tolerance = 0.5 px |
functions
| 28 | func dp(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } |
| 29 | func dpn(v: i64) -> i64 |
| 38 | func dr_qm(a: i64, b: i64) -> i64 { return (a * b) >> 16 } |
| 39 | func dr_clamp(u: i64) -> i64 { if u < 0 { return 0 } if u > Q { return Q } return u } called by 1: dr_cov |
| 41 | func dr_cov(i: i64, p: i64) -> i64 { let di: i64 = i * Q - p; let u: i64 = dr_qm(di, INVS) + HALF; return dr_clamp(u) } |
| 43 | func dr_active(i: i64, p: i64) -> i64 { let di: i64 = i * Q - p; let u: i64 = dr_qm(di, INVS) + HALF; if u > 0 { if u < Q { return 1 } } return 0 } |
| 44 | func dr_render(p: i64, out: *i64) -> i64 { var i: i64 = 0; while i < NPX { out[i] = dr_cov(i, p); i = i + 1 } return 0 } |
| 45 | func dr_loss(p: i64, tgt: *i64) -> i64 { var s: i64 = 0; var i: i64 = 0; while i < NPX { let d: i64 = dr_cov(i, p) - tgt[i]; s = s + dr_qm(d, d); i = i + 1 } return s } |
| 47 | func dr_grad(p: i64, tgt: *i64) -> i64 |
| 60 | func main() -> i64 |