code wiki / _hdl_build / nx_spec_gate.nx
nx_spec_gate.nx source
↩ module page · 62 lines · 5123 B
1// nx_spec_gate.nx -- GATE for CAP-PBR rung-2 (Blinn-Phong specular, mr_face_specular + mr_pow_q14 over render_core):
2// the glossy highlight from the COMPUTED (deformable) face normal + the half-vector. Proves with HARD numbers:
3// (T1) perfect mirror config -> full highlight == Q, (T2) grazing -> 0, (T3 MEASURED) higher shininess NARROWS the
4// highlight (dimmer at a fixed off-angle), (T4 MEASURED) tilting the view off the mirror angle dims it MONOTONICALLY,
5// (T5 teeth) back-facing + degenerate -> 0 (no div0/no hallucinated light), (T6) integer pow correct. Q14 no-float.
6// expect_exit: 0
7import "nx_syscalls.nx"
8import "nx_render_core.nx"
9import "nx_meshrender.nx"
10
11func g_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12func g_pn(v: i64) -> i64 { let b: *u8=sys_mmap(28); var x: i64=v; if x<0{b[0]=45 as u8;sys_write(1,b,1);x=0-x} if x==0{b[0]=48 as u8;sys_write(1,b,1);return 0} var d: i64=0; var y: i64=x; while y>0{d=d+1;y=y/10} var i: i64=d-1; y=x; while i>=0{b[i]=(48+(y%10)) as u8;y=y/10;i=i-1} sys_write(1,b,d); return 0 }
13func ck(name: *u8, c: i64) -> i64 { if c==1 { g_puts(" PASS " as *u8) } else { g_puts(" FAIL " as *u8) } g_puts(name); g_puts("\n" as *u8); return c }
14
15func main() -> i64 {
16 g_puts("nx_spec_gate (CAP-PBR rung-2: Blinn-Phong specular highlight from the computed half-vector)\n" as *u8)
17 let Q: i64 = 16384
18 // face in z=0 plane (p0,p1,p2) -> computed normal +z. View/light dirs are unit Q14.
19 // T1: perfect mirror -- L and V both +z -> H=+z -> dot(unitN,H)=Q -> spec=Q
20 let s1: i64 = mr_face_specular(0,0,0, Q,0,0, 0,Q,0, 0,0,Q, 0,0,Q, 8)
21 // T2: grazing -- L,V along +x -> H=+x perpendicular to the +z normal -> 0
22 let s2: i64 = mr_face_specular(0,0,0, Q,0,0, 0,Q,0, Q,0,0, Q,0,0, 8)
23 // T3: off-angle (V 30deg off z), shininess 2 vs 16 -> the sharper (16) highlight is dimmer at the same angle
24 let vx30: i64 = rc_sin_q14(30); let vz30: i64 = rc_cos_q14(30)
25 let s3a: i64 = mr_face_specular(0,0,0, Q,0,0, 0,Q,0, 0,0,Q, vx30,0,vz30, 2)
26 let s3b: i64 = mr_face_specular(0,0,0, Q,0,0, 0,Q,0, 0,0,Q, vx30,0,vz30, 16)
27 // T4: monotonic dim as V tilts 0 -> 30 -> 60 off the mirror angle (shininess 8)
28 let vx60: i64 = rc_sin_q14(60); let vz60: i64 = rc_cos_q14(60)
29 let s4_0: i64 = mr_face_specular(0,0,0, Q,0,0, 0,Q,0, 0,0,Q, 0,0,Q, 8)
30 let s4_30: i64 = mr_face_specular(0,0,0, Q,0,0, 0,Q,0, 0,0,Q, vx30,0,vz30, 8)
31 let s4_60: i64 = mr_face_specular(0,0,0, Q,0,0, 0,Q,0, 0,0,Q, vx60,0,vz60, 8)
32 // T5 teeth: back-facing (L,V from behind) -> 0 ; degenerate zero-area face -> 0
33 let s5a: i64 = mr_face_specular(0,0,0, Q,0,0, 0,Q,0, 0,0,0-Q, 0,0,0-Q, 8)
34 let s5b: i64 = mr_face_specular(0,0,0, 0,0,0, 0,0,0, 0,0,Q, 0,0,Q, 8)
35 // T6: integer pow correctness
36 let p1: i64 = mr_pow_q14(Q, 5); let p2: i64 = mr_pow_q14(Q/2, 2); let p3: i64 = mr_pow_q14(123, 0)
37
38 g_puts(" mirror="); g_pn(s1); g_puts(" grazing="); g_pn(s2); g_puts(" off30^2="); g_pn(s3a); g_puts(" off30^16="); g_pn(s3b); g_puts(" tilt[0,30,60]="); g_pn(s4_0); g_puts(","); g_pn(s4_30); g_puts(","); g_pn(s4_60); g_puts(" back="); g_pn(s5a); g_puts(" degen="); g_pn(s5b); g_puts(" pow[Q^5,(Q/2)^2,x^0]="); g_pn(p1); g_puts(","); g_pn(p2); g_puts(","); g_pn(p3); g_puts("\n" as *u8)
39
40 var pass: i64=0; var t: i64=0
41 var c1: i64=0; if s1==Q { c1=1 }
42 pass=pass+ck("T1: perfect mirror (L,V,N aligned) -> full highlight == Q" as *u8, c1); t=t+1
43 var c2: i64=0; if s2==0 { c2=1 }
44 pass=pass+ck("T2: grazing (half-vector perpendicular to the normal) -> 0" as *u8, c2); t=t+1
45 var c3: i64=0; if s3a>s3b { if s3b>0 { if s3a<Q { c3=1 } } }
46 pass=pass+ck("T3 (MEASURED): higher shininess NARROWS the highlight (off-angle ^16 dimmer than ^2, both < Q)" as *u8, c3); t=t+1
47 var c4: i64=0; if s4_0==Q { if s4_0>s4_30 { if s4_30>s4_60 { if s4_60>0 { c4=1 } } } }
48 pass=pass+ck("T4 (MEASURED): tilting the view off the mirror angle dims the highlight MONOTONICALLY (Q>30deg>60deg>0)" as *u8, c4); t=t+1
49 var c5: i64=0; if s5a==0 { if s5b==0 { c5=1 } }
50 pass=pass+ck("T5 (teeth): back-facing + degenerate face -> 0 (no div0, no hallucinated highlight)" as *u8, c5); t=t+1
51 var c6: i64=0; if p1==Q { if p2==(Q/4) { if p3==Q { c6=1 } } }
52 pass=pass+ck("T6: integer pow correct (Q^5==Q, (Q/2)^2==Q/4, x^0==Q)" as *u8, c6); t=t+1
53
54 var okall: i64=0; if pass==t { okall=1 }
55 g_puts("---- nx_spec_gate: passed "); g_pn(pass); g_puts(" / "); g_pn(t); g_puts(" ----\n" as *u8)
56 if okall==1 {
57 let logf: i64=sys_openat_append("knowledge/status/spec_gate.log" as *u8, 420)
58 if logf>=0 { let msg: *u8="SPEC GREEN: CAP-PBR rung-2 Blinn-Phong specular -- mirror==Q + shininess-narrows + monotonic-tilt + back/degenerate teeth + pow, no-float\n" as *u8; var ml: i64=0; while msg[ml]!=(0 as u8){ml=ml+1} let z: i64=sys_write(logf,msg,ml); sys_close(logf) }
59 g_puts("verdict=GREEN (a glossy specular highlight from the computed half-vector -- the matte->shiny PBR rung)\n" as *u8); sys_exit(0); return 0
60 }
61 g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1
62}