code wiki / _hdl_build / nx_gx_firstbyte_gate.nx
nx_gx_firstbyte_gate.nx source
↩ module page · 260 lines · 13763 B
1// nx_gx_firstbyte_gate.nx -- Gx re-grounding: the sovereign graphics stack proven FROM THE FIRST BYTE UP.
2// One canvas, seven panels, each a rung of the chain -- byte0 marker / scanline gradient / framebuffer checksum /
3// triangle raster (count vs analytic area) / z-buffer (near wins though far drawn LAST) / per-pixel lit sphere /
4// SSS wrap vs clay -- then the PNG artifact's OWN first byte (0x89 magic) is read back and verified. Every rung
5// is measured in-gate, not asserted; the composite PNG is the eyeball artifact. HONEST: the deeper rungs
6// (GGX BRDF, browser compositing, PBR) stay in their own gates -- this is the trunk they stand on.
7// license_tier: ORIGINAL expect_exit: 0
8import "nx_png_write.nx"
9
10func fb_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
11func fb_pn(v: i64) -> i64 { let b: *u8=sys_mmap(32) as *u8; var x: i64=v; var ng: i64=0; if x<0{ng=1;x=0-x} var i: i64=31; if x==0{b[i]=48 as u8;i=i-1} while x>0{b[i]=(48+x%10) as u8;x=x/10;i=i-1} if ng==1{b[i]=45 as u8;i=i-1} sys_write(1,(b as i64+i+1) as *u8,31-i); return 0 }
12func fb_isqrt(n: i64) -> i64 { if n<=0 { return 0 } var x: i64=n; var y: i64=(x+n/x)/2; var it: i64=0; while it<64 { if y>=x { it=64 } else { x=y; y=(x+n/x)/2; it=it+1 } } return x }
13func fb_edge(ax: i64, ay: i64, bx: i64, by: i64, px: i64, py: i64) -> i64 { return (bx-ax)*(py-ay)-(by-ay)*(px-ax) }
14func fb_wrap(ndl: i64, w: i64, Q: i64) -> i64 { var v: i64=((ndl+w)*Q)/(Q+w); if v<0{v=0} if v>Q{v=Q} return v }
15
16func main() -> i64 {
17 fb_puts("=== nx_gx_firstbyte_gate -- the graphics stack from the first byte up ===\n" as *u8)
18 var fails: i64=0
19 let Q: i64=4096
20 let W: i64=770; let H: i64=240
21 let rgb: *u8=sys_mmap(W*H*3+16)
22 var i: i64=0
23 while i<W*H { let o: i64=i*3; rgb[o]=14 as u8; rgb[o+1]=15 as u8; rgb[o+2]=20 as u8; i=i+1 }
24 // panel separators at 110,220,330,440,550,660 for the eyeball
25 var sy: i64=0
26 while sy<H {
27 var sp: i64=1
28 while sp<7 { let o: i64=(sy*W+sp*110)*3; rgb[o]=60 as u8; rgb[o+1]=60 as u8; rgb[o+2]=70 as u8; sp=sp+1 }
29 sy=sy+1
30 }
31
32 // ---- P0 (0..109): the first byte, magnified. 32x32 white block at (8,8) = the zoomed first pixel.
33 var by: i64=8
34 while by<40 {
35 var bx: i64=8
36 while bx<40 { let o: i64=(by*W+bx)*3; rgb[o]=255 as u8; rgb[o+1]=254 as u8; rgb[o+2]=253 as u8; bx=bx+1 }
37 by=by+1
38 }
39
40 // ---- P1 (110..219): scanline gradient, red channel 0..255 left to right, every row.
41 var gy: i64=0
42 while gy<H {
43 var gx: i64=110
44 while gx<220 { let v: i64=((gx-110)*255)/109; let o: i64=(gy*W+gx)*3; rgb[o]=v as u8; rgb[o+1]=(v/3) as u8; rgb[o+2]=((255-v)/3) as u8; gx=gx+1 }
45 gy=gy+1
46 }
47
48 // ---- P2 (220..329): framebuffer checksum -- checker pattern, expected sum accumulated at write time.
49 var cksum_expect: i64=0
50 var cy2: i64=0
51 while cy2<H {
52 var cx2: i64=221
53 while cx2<330 {
54 let cell: i64=((cx2-220)/18 + cy2/18)%2
55 var cv: i64=40; if cell==1 { cv=210 }
56 let o: i64=(cy2*W+cx2)*3
57 rgb[o]=cv as u8; rgb[o+1]=cv as u8; rgb[o+2]=(cv/2) as u8
58 cksum_expect=cksum_expect+cv+cv+cv/2
59 cx2=cx2+1
60 }
61 cy2=cy2+1
62 }
63
64 // ---- P3 (330..439): triangle raster, pixel count vs analytic area.
65 let ax: i64=360; let ay: i64=30
66 let bx2: i64=430; let by2: i64=120
67 let cx3: i64=340; let cy3: i64=200
68 let crossA: i64=fb_edge(ax,ay,bx2,by2,cx3,cy3)
69 var sA: i64=1; if crossA<0 { sA=0-1 }
70 var tricount: i64=0
71 var ty: i64=20
72 while ty<220 {
73 var tx: i64=331
74 while tx<440 {
75 let e0: i64=fb_edge(ax,ay,bx2,by2,tx,ty)*sA
76 let e1: i64=fb_edge(bx2,by2,cx3,cy3,tx,ty)*sA
77 let e2: i64=fb_edge(cx3,cy3,ax,ay,tx,ty)*sA
78 if e0>=0 { if e1>=0 { if e2>=0 {
79 let o: i64=(ty*W+tx)*3; rgb[o]=250 as u8; rgb[o+1]=160 as u8; rgb[o+2]=40 as u8; tricount=tricount+1
80 } } }
81 tx=tx+1
82 }
83 ty=ty+1
84 }
85 var areaA: i64=crossA; if areaA<0 { areaA=0-areaA }
86 let areahalf: i64=areaA/2
87
88 // ---- P4 (440..549): z-buffer. NEAR (cyan, z=100) drawn FIRST, FAR (red, z=200) drawn LAST.
89 // If the overlap stays cyan, only the z-test explains it (painter's order would paint it red).
90 let zbuf: *i64=sys_mmap(W*H*8+16) as *i64
91 var zi: i64=0
92 while zi<W*H { zbuf[zi]=1073741824; zi=zi+1 }
93 var zrejected: i64=0; var ox: i64=0-1; var oy: i64=0-1
94 var pass2: i64=0
95 while pass2<2 {
96 var nax: i64=460; var nay: i64=40; var nbx: i64=530; var nby: i64=110; var ncx: i64=460; var ncy: i64=180
97 var zval: i64=100; var zr: i64=40; var zg: i64=230; var zb: i64=230
98 if pass2==1 { nax=470; nay=30; nbx=540; nby=140; ncx=480; ncy=210; zval=200; zr=230; zg=60; zb=60 }
99 let crossZ: i64=fb_edge(nax,nay,nbx,nby,ncx,ncy)
100 var sZ: i64=1; if crossZ<0 { sZ=0-1 }
101 var zy: i64=20
102 while zy<220 {
103 var zx: i64=441
104 while zx<550 {
105 let f0: i64=fb_edge(nax,nay,nbx,nby,zx,zy)*sZ
106 let f1: i64=fb_edge(nbx,nby,ncx,ncy,zx,zy)*sZ
107 let f2: i64=fb_edge(ncx,ncy,nax,nay,zx,zy)*sZ
108 if f0>=0 { if f1>=0 { if f2>=0 {
109 let zo: i64=zy*W+zx
110 if zval<zbuf[zo] {
111 zbuf[zo]=zval
112 let o: i64=zo*3; rgb[o]=zr as u8; rgb[o+1]=zg as u8; rgb[o+2]=zb as u8
113 } else {
114 zrejected=zrejected+1
115 if ox<0 { ox=zx; oy=zy }
116 }
117 } } }
118 zx=zx+1
119 }
120 zy=zy+1
121 }
122 pass2=pass2+1
123 }
124
125 // ---- P5 (550..659): per-pixel lit sphere (Lambert + specular hotspot), Q12.
126 let L2: i64=2*2+3*3+6*6
127 let llen: i64=fb_isqrt(L2*Q*Q)
128 let lx: i64=(2*Q*Q)/llen; let ly: i64=(3*Q*Q)/llen; let lz: i64=(6*Q*Q)/llen
129 let hxr: i64=lx; let hyr: i64=ly; let hzr: i64=lz+Q
130 let hlen: i64=fb_isqrt(hxr*hxr+hyr*hyr+hzr*hzr)
131 let hx: i64=(hxr*Q)/hlen; let hy: i64=(hyr*Q)/hlen; let hz: i64=(hzr*Q)/hlen
132 let scx: i64=605; let scy: i64=120; let R: i64=80
133 var maxc: i64=0; var mindisc: i64=255
134 var py5: i64=scy-R
135 while py5<=scy+R {
136 var px5: i64=scx-R
137 while px5<=scx+R {
138 let dx: i64=px5-scx; let dy: i64=py5-scy
139 let r2: i64=dx*dx+dy*dy
140 if r2<=R*R {
141 let dz: i64=fb_isqrt(R*R-r2)
142 let nx: i64=(dx*Q)/R; let ny: i64=(0-dy*Q)/R; let nz: i64=(dz*Q)/R
143 var ndl: i64=(nx*lx+ny*ly+nz*lz)/Q; if ndl<0 { ndl=0 }
144 var ndh: i64=(nx*hx+ny*hy+nz*hz)/Q; if ndh<0 { ndh=0 }
145 let n2: i64=(ndh*ndh)/Q; let n4: i64=(n2*n2)/Q; let n8: i64=(n4*n4)/Q
146 let spec: i64=(n8*220)/Q
147 var rr: i64=(90*ndl)/Q+8+spec; var gg: i64=(140*ndl)/Q+9+spec; var bb: i64=(235*ndl)/Q+12+spec
148 if rr>255{rr=255} if gg>255{gg=255} if bb>255{bb=255}
149 let o: i64=(py5*W+px5)*3
150 rgb[o]=rr as u8; rgb[o+1]=gg as u8; rgb[o+2]=bb as u8
151 if bb>maxc { maxc=bb }
152 var mx: i64=rr; if gg>mx { mx=gg } if bb>mx { mx=bb }
153 if mx<mindisc { mindisc=mx }
154 }
155 px5=px5+1
156 }
157 py5=py5+1
158 }
159
160 // ---- P6 (660..769): SSS wrap vs clay, two small spheres, same albedo+light (re-proving the ladder rung).
161 let ar6: i64=232; let ag6: i64=176; let ab6: i64=150
162 let R6: i64=42
163 let WR: i64=2048; let WG: i64=900; let WB: i64=256
164 var claylit: i64=0; var ssslit: i64=0
165 var mode: i64=0
166 while mode<2 {
167 let ccx: i64=715
168 let ccy: i64=65+mode*110
169 var py6: i64=ccy-R6
170 while py6<=ccy+R6 {
171 var px6: i64=ccx-R6
172 while px6<=ccx+R6 {
173 let dx: i64=px6-ccx; let dy: i64=py6-ccy
174 let r2: i64=dx*dx+dy*dy
175 if r2<=R6*R6 {
176 let dz: i64=fb_isqrt(R6*R6-r2)
177 let nx: i64=(dx*Q)/R6; let ny: i64=(0-dy*Q)/R6; let nz: i64=(dz*Q)/R6
178 let ndl: i64=(nx*lx+ny*ly+nz*lz)/Q
179 var rr: i64=0; var gg: i64=0; var bb: i64=0
180 if mode==0 {
181 var d: i64=ndl; if d<0 { d=0 }
182 rr=(ar6*d)/Q+(ar6*18)/255; gg=(ag6*d)/Q+(ag6*18)/255; bb=(ab6*d)/Q+(ab6*18)/255
183 } else {
184 let wr: i64=fb_wrap(ndl,WR,Q); let wg: i64=fb_wrap(ndl,WG,Q); let wb: i64=fb_wrap(ndl,WB,Q)
185 rr=(ar6*wr)/Q+(ar6*18)/255; gg=(ag6*wg)/Q+(ag6*18)/255; bb=(ab6*wb)/Q+(ab6*18)/255
186 }
187 if rr>255{rr=255} if gg>255{gg=255} if bb>255{bb=255}
188 let o: i64=(py6*W+px6)*3
189 rgb[o]=rr as u8; rgb[o+1]=gg as u8; rgb[o+2]=bb as u8
190 if rr>50 { if mode==0 { claylit=claylit+1 } else { ssslit=ssslit+1 } }
191 }
192 px6=px6+1
193 }
194 py6=py6+1
195 }
196 mode=mode+1
197 }
198
199 // the FIRST BYTE: pixel (0,0) written LAST so nothing overwrites offset 0.
200 rgb[0]=255 as u8; rgb[1]=254 as u8; rgb[2]=253 as u8
201
202 nx_png_write_rgb("knowledge/nx_gx_firstbyte.png\x00" as *u8, rgb, W, H)
203 fb_puts(" wrote knowledge/nx_gx_firstbyte.png\n" as *u8)
204
205 // ---- the ladder of proofs, bottom up ----
206 // T1 the first byte of the framebuffer holds its value
207 if rgb[0]==(255 as u8) { fb_puts("T1 PASS byte 0 of the framebuffer = 255 (written, held)\n" as *u8) } else { fails=fails+1; fb_puts("T1 FAIL byte 0\n" as *u8) }
208 // T2 the first PIXEL: three distinct bytes addressable
209 var t2: i64=0
210 if rgb[0]==(255 as u8) { if rgb[1]==(254 as u8) { if rgb[2]==(253 as u8) { t2=1 } } }
211 if t2==1 { fb_puts("T2 PASS pixel 0 = (255,254,253) -- per-byte RGB addressing\n" as *u8) } else { fails=fails+1; fb_puts("T2 FAIL pixel 0\n" as *u8) }
212 // T3 scanline gradient: endpoints exact + midpoint linear
213 let g0: i64=rgb[(120*W+110)*3] as i64
214 let g1: i64=rgb[(120*W+219)*3] as i64
215 let gm: i64=rgb[(120*W+165)*3] as i64
216 var t3: i64=0
217 if g0==0 { if g1==255 { if gm>=120 { if gm<=137 { t3=1 } } } }
218 if t3==1 { fb_puts("T3 PASS scanline gradient 0->255 linear (mid="); fb_pn(gm); fb_puts(")\n" as *u8) } else { fails=fails+1; fb_puts("T3 FAIL gradient "); fb_pn(g0); fb_puts(" "); fb_pn(gm); fb_puts(" "); fb_pn(g1); fb_puts("\n" as *u8) }
219 // T4 framebuffer checksum: re-scan the checker region, must equal the write-time sum
220 var cksum_scan: i64=0
221 var vy: i64=0
222 while vy<H {
223 var vx: i64=221
224 while vx<330 { let o: i64=(vy*W+vx)*3; cksum_scan=cksum_scan+(rgb[o] as i64)+(rgb[o+1] as i64)+(rgb[o+2] as i64); vx=vx+1 }
225 vy=vy+1
226 }
227 if cksum_scan==cksum_expect { fb_puts("T4 PASS framebuffer holds "); fb_pn(cksum_scan); fb_puts(" checksum bytes exactly\n" as *u8) } else { fails=fails+1; fb_puts("T4 FAIL checksum "); fb_pn(cksum_scan); fb_puts(" != "); fb_pn(cksum_expect); fb_puts("\n" as *u8) }
228 // T5 triangle raster count vs analytic area within 15 percent
229 var lo: i64=(areahalf*85)/100; var hi: i64=(areahalf*115)/100
230 var t5: i64=0
231 if tricount>=lo { if tricount<=hi { t5=1 } }
232 if t5==1 { fb_puts("T5 PASS triangle raster "); fb_pn(tricount); fb_puts(" px vs analytic area "); fb_pn(areahalf); fb_puts("\n" as *u8) } else { fails=fails+1; fb_puts("T5 FAIL raster "); fb_pn(tricount); fb_puts(" vs "); fb_pn(areahalf); fb_puts("\n" as *u8) }
233 // T6 z-buffer: far fragments rejected + overlap pixel stayed NEAR-cyan though far drawn last
234 var t6: i64=0
235 if zrejected>50 { if ox>=0 {
236 let oo: i64=(oy*W+ox)*3
237 let orr: i64=rgb[oo] as i64; let ogg: i64=rgb[oo+1] as i64
238 if ogg>200 { if orr<100 { t6=1 } }
239 } }
240 if t6==1 { fb_puts("T6 PASS z-buffer: "); fb_pn(zrejected); fb_puts(" far fragments rejected, overlap stayed near-cyan\n" as *u8) } else { fails=fails+1; fb_puts("T6 FAIL z-buffer rejected="); fb_pn(zrejected); fb_puts("\n" as *u8) }
241 // T7 lit sphere: specular hotspot near white + a real dark terminator in one disc
242 var t7: i64=0
243 if maxc>=250 { if mindisc<=45 { t7=1 } }
244 if t7==1 { fb_puts("T7 PASS lit sphere hotspot="); fb_pn(maxc); fb_puts(" terminator-min="); fb_pn(mindisc); fb_puts("\n" as *u8) } else { fails=fails+1; fb_puts("T7 FAIL sphere "); fb_pn(maxc); fb_puts(" "); fb_pn(mindisc); fb_puts("\n" as *u8) }
245 // T8 SSS wraps past the terminator vs clay (the ladder rung re-proven)
246 var t8: i64=0
247 if ssslit>claylit+150 { t8=1 }
248 if t8==1 { fb_puts("T8 PASS SSS wrap lit="); fb_pn(ssslit); fb_puts(" > clay lit="); fb_pn(claylit); fb_puts("\n" as *u8) } else { fails=fails+1; fb_puts("T8 FAIL sss "); fb_pn(ssslit); fb_puts(" clay "); fb_pn(claylit); fb_puts("\n" as *u8) }
249 // T9 the artifact's own first byte: PNG magic 137 80 78 71
250 let szp: *i64=sys_mmap(16) as *i64
251 let rb: *u8=sys_read_file("knowledge/nx_gx_firstbyte.png\x00" as *u8, szp)
252 var t9: i64=0
253 if (rb as i64)!=0 { if szp[0]>1000 { if rb[0]==(137 as u8) { if rb[1]==(80 as u8) { if rb[2]==(78 as u8) { if rb[3]==(71 as u8) { t9=1 } } } } } }
254 if t9==1 { fb_puts("T9 PASS artifact first byte = 137 (PNG magic), "); fb_pn(szp[0]); fb_puts(" bytes on disk\n" as *u8) } else { fails=fails+1; fb_puts("T9 FAIL png readback\n" as *u8) }
255
256 if fails==0 { fb_puts("GX-FIRSTBYTE GREEN -- the sovereign chain holds from byte 0 to the shipped artifact\n" as *u8); sys_exit(0); return 0 }
257 fb_puts("GX-FIRSTBYTE RED fails="); fb_pn(fails); fb_puts("\n" as *u8)
258 sys_exit(1)
259 return 1
260}