code wiki / _hdl_build / nx_gx_eye_gate.nx

nx_gx_eye_gate.nx source

↩ module page · 248 lines · 13644 B

1// nx_gx_eye_gate.nx -- Gx/FIDELITY rung 2: the EYE shader (cornea/iris/sclera) -- the #2 clay->flesh lever after SSS. 2// Two eyeballs, same light, side by side. LEFT = doll eye (white ball + flat brown disc + broad dull specular). 3// RIGHT = eye shader: corneal REFRACTION (the iris is seen THROUGH the cornea and appears magnified), radial iris 4// FIBERS (stroma spokes), LIMBAL RING (dark iris-sclera boundary), sharp corneal CATCHLIGHT (tiny, near-white -- 5// the "alive" signal), and a WARM non-paper-white sclera. Every cue is measured against the doll eye in-gate. 6// Integer Q12, sovereign, zero libs. HONEST: shader-model rung ONLY -- no eyelids/lashes/tear-film meniscus/vessels, 7// bare eyeball geometry; a detail critic must still rank this below a photo. Those are the next rungs. 8// license_tier: ORIGINAL expect_exit: 0 9import "nx_png_write.nx" 10 11func eg_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 eg_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 } 13func eg_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 } 14// diamond angle 0..4Q from a direction (piecewise-linear atan2 stand-in, integer-pure) 15func eg_dang(dx: i64, dy: i64, Q: i64) -> i64 { 16 var adx: i64=dx; if adx<0 { adx=0-adx } 17 var ady: i64=dy; if ady<0 { ady=0-ady } 18 var den: i64=adx+ady; if den<1 { den=1 } 19 let base: i64=(ady*Q)/den 20 if dx>=0 { if dy>=0 { return base } } 21 if dx<0 { if dy>=0 { return 2*Q-base } } 22 if dx<0 { return 2*Q+base } 23 return 4*Q-base 24} 25// max channel of a rendered pixel 26func eg_lum(rgb: *u8, W: i64, x: i64, y: i64) -> i64 { 27 let o: i64=(y*W+x)*3 28 var m: i64=rgb[o] as i64 29 if (rgb[o+1] as i64)>m { m=rgb[o+1] as i64 } 30 if (rgb[o+2] as i64)>m { m=rgb[o+2] as i64 } 31 return m 32} 33 34func main() -> i64 { 35 eg_puts("=== nx_gx_eye_gate -- eye shader (cornea/iris/sclera) vs doll eye ===\n" as *u8) 36 var fails: i64=0 37 let Q: i64=4096 38 let W: i64=520; let H: i64=260 39 let rgb: *u8=sys_mmap(W*H*3+16) 40 var i: i64=0 41 while i<W*H { let o: i64=i*3; rgb[o]=16 as u8; rgb[o+1]=17 as u8; rgb[o+2]=22 as u8; i=i+1 } 42 // shared light + half vector (Q12) 43 let L2: i64=2*2+3*3+6*6 44 let llen: i64=eg_isqrt(L2*Q*Q) 45 let lx: i64=(2*Q*Q)/llen; let ly: i64=(3*Q*Q)/llen; let lz: i64=(6*Q*Q)/llen 46 let hxr: i64=lx; let hyr: i64=ly; let hzr: i64=lz+Q 47 let hlen: i64=eg_isqrt(hxr*hxr+hyr*hyr+hzr*hzr) 48 let hx: i64=(hxr*Q)/hlen; let hy: i64=(hyr*Q)/hlen; let hz: i64=(hzr*Q)/hlen 49 let R: i64=90 50 let PR: i64=14; let IR: i64=38 // geometric pupil + iris radii, SAME for both eyes 51 let CR: i64=52 // corneal zone (right eye only) 52 let MAG: i64=3200 // refraction: sample radius rs = r*3200/4096 -> iris appears 1.28x larger 53 let SPOKES: i64=26 54 var flatIris: i64=0; var eyeIris: i64=0 55 var mode: i64=0 56 while mode<2 { 57 let cx: i64=130+mode*260 58 let cy: i64=130 59 var py: i64=cy-R 60 while py<=cy+R { 61 var px: i64=cx-R 62 while px<=cx+R { 63 let dx: i64=px-cx; let dy: i64=py-cy 64 let r2: i64=dx*dx+dy*dy 65 if r2<=R*R { 66 let dz: i64=eg_isqrt(R*R-r2) 67 let nx: i64=(dx*Q)/R; let ny: i64=(0-dy*Q)/R; let nz: i64=(dz*Q)/R 68 var ndl: i64=(nx*lx+ny*ly+nz*lz)/Q; if ndl<0 { ndl=0 } 69 var ndh: i64=(nx*hx+ny*hy+nz*hz)/Q; if ndh<0 { ndh=0 } 70 let n2: i64=(ndh*ndh)/Q; let n4: i64=(n2*n2)/Q; let n8: i64=(n4*n4)/Q 71 let r: i64=eg_isqrt(r2) 72 var rr: i64=0; var gg: i64=0; var bb: i64=0 73 if mode==0 { 74 // ---- DOLL EYE: flat disc iris, uniform brown, paper-white ball, broad dull spec 75 if r<PR { 76 rr=12; gg=10; bb=10 77 } else { if r<IR { 78 rr=120; gg=78; bb=32 79 } else { 80 let lit: i64=(600*Q+400*ndl)/1000 81 rr=(240*lit)/Q; gg=(240*lit)/Q; bb=(240*lit)/Q 82 } } 83 let spec: i64=(n4*200)/Q 84 rr=rr+spec; gg=gg+spec; bb=bb+spec 85 } else { 86 // ---- EYE SHADER 87 var rs: i64=r 88 if r<CR { rs=(r*MAG)/Q } // corneal refraction: magnified iris sample 89 if rs<PR { 90 rr=12; gg=10; bb=10 91 } else { if rs<IR { 92 // iris: radial color gradient inner amber -> outer dark + stroma spokes 93 let t: i64=((rs-PR)*Q)/(IR-PR) 94 var ir: i64=150-((80*t)/Q) 95 var ig: i64=100-((55*t)/Q) 96 var ib: i64=40-((20*t)/Q) 97 if rs>PR+2 { if rs<IR-5 { 98 let a: i64=eg_dang(dx,dy,Q) 99 let w: i64=(a*SPOKES)%(2*Q) 100 var tri: i64=w-Q; if tri<0 { tri=0-tri } 101 let fib: i64=((tri*32)/Q)-16 102 ir=ir+fib; ig=ig+fib 103 } } 104 // limbal ring: dark band at the iris boundary 105 if rs>=IR-4 { ir=(ir*35)/100; ig=(ig*35)/100; ib=(ib*35)/100 } 106 let lit: i64=(700*Q+300*ndl)/1000 107 rr=(ir*lit)/Q; gg=(ig*lit)/Q; bb=(ib*lit)/Q 108 } else { 109 // limbal ring outer half then warm sclera with mild ambient occlusion 110 let lit: i64=(550*Q+450*ndl)/1000 111 let ao: i64=Q-(r2*Q)/(2*R*R) 112 var sr: i64=(236*lit)/Q; var sg: i64=(228*lit)/Q; var sb: i64=(212*lit)/Q 113 sr=(sr*ao)/Q; sg=(sg*ao)/Q; sb=(sb*ao)/Q 114 if rs<IR+2 { sr=(sr*45)/100; sg=(sg*45)/100; sb=(sb*45)/100 } 115 rr=sr; gg=sg; bb=sb 116 } } 117 // corneal catchlight: a thresholded mirror spot -- GEOMETRICALLY tiny (ndh within 1.1pct of 118 // perfect reflection), zero everywhere else so it cannot wash out the iris fiber signal 119 var catch: i64=0 120 if ndh>4050 { catch=((ndh-4050)*255)/46 } 121 if catch>255 { catch=255 } 122 let wet: i64=(n8*40)/Q 123 rr=rr+catch+wet; gg=gg+catch+wet; bb=bb+catch+wet 124 } 125 if rr>255{rr=255} if gg>255{gg=255} if bb>255{bb=255} 126 if rr<0{rr=0} if gg<0{gg=0} if bb<0{bb=0} 127 let o: i64=(py*W+px)*3 128 rgb[o]=rr as u8; rgb[o+1]=gg as u8; rgb[o+2]=bb as u8 129 // iris-colored census (the refraction magnification metric) 130 if rr>55 { if rr>gg+20 { if gg>bb+5 { 131 if mode==0 { flatIris=flatIris+1 } else { eyeIris=eyeIris+1 } 132 } } } 133 } 134 px=px+1 135 } 136 py=py+1 137 } 138 mode=mode+1 139 } 140 nx_png_write_rgb("knowledge/nx_gx_eye.png\x00" as *u8, rgb, W, H) 141 eg_puts(" wrote knowledge/nx_gx_eye.png\n" as *u8) 142 143 let fcx: i64=130; let ecx: i64=390; let cy: i64=130 144 // T1 both eyeballs rendered (disc pixel sanity via center + rim luminance) 145 var t1: i64=0 146 if eg_lum(rgb,W,fcx,cy)>5 { if eg_lum(rgb,W,ecx,cy)>5 { if eg_lum(rgb,W,ecx+R-6,cy)>30 { t1=1 } } } 147 if t1==1 { eg_puts("T1 PASS both eyeballs rendered\n" as *u8) } else { fails=fails+1; eg_puts("T1 FAIL render\n" as *u8) } 148 // T2 catchlight SHARPNESS: px within 4 of each half's max luminance -- tiny for the eye, broad for the doll 149 var flatMax: i64=0; var eyeMax: i64=0 150 var sy: i64=cy-R 151 while sy<=cy+R { 152 var sx: i64=0 153 while sx<2 { 154 var xx: i64=fcx-R; if sx==1 { xx=ecx-R } 155 let cxs: i64=fcx+sx*260 156 var px2: i64=cxs-R 157 while px2<=cxs+R { 158 let ddx: i64=px2-cxs; let ddy: i64=sy-cy 159 if ddx*ddx+ddy*ddy<=R*R { 160 let l: i64=eg_lum(rgb,W,px2,sy) 161 if sx==0 { if l>flatMax { flatMax=l } } else { if l>eyeMax { eyeMax=l } } 162 } 163 px2=px2+1 164 } 165 sx=sx+1 166 } 167 sy=sy+1 168 } 169 var flatTight: i64=0; var eyeTight: i64=0 170 sy=cy-R 171 while sy<=cy+R { 172 var sx2: i64=0 173 while sx2<2 { 174 let cxs: i64=fcx+sx2*260 175 var px3: i64=cxs-R 176 while px3<=cxs+R { 177 let ddx: i64=px3-cxs; let ddy: i64=sy-cy 178 if ddx*ddx+ddy*ddy<=R*R { 179 let l: i64=eg_lum(rgb,W,px3,sy) 180 if sx2==0 { if l>=flatMax-4 { flatTight=flatTight+1 } } else { if l>=eyeMax-4 { eyeTight=eyeTight+1 } } 181 } 182 px3=px3+1 183 } 184 sx2=sx2+1 185 } 186 sy=sy+1 187 } 188 var t2: i64=0 189 if eyeTight>=2 { if eyeTight*3<flatTight { t2=1 } } 190 if t2==1 { eg_puts("T2 PASS catchlight sharp: eye top-band="); eg_pn(eyeTight); eg_puts(" px vs doll broad="); eg_pn(flatTight); eg_puts(" px\n" as *u8) } else { fails=fails+1; eg_puts("T2 FAIL catchlight eye="); eg_pn(eyeTight); eg_puts(" doll="); eg_pn(flatTight); eg_puts("\n" as *u8) } 191 // T3 limbal ring: on the eye, the iris-sclera boundary is DARKER than both neighbours; absent on the doll 192 let ringR: i64=47; let irisR: i64=33; let sclR: i64=60 193 let eRing: i64=eg_lum(rgb,W,ecx+ringR,cy) 194 let eIris: i64=eg_lum(rgb,W,ecx+irisR,cy) 195 let eScl: i64=eg_lum(rgb,W,ecx+sclR,cy) 196 let fRing: i64=eg_lum(rgb,W,fcx+ringR,cy) 197 let fScl: i64=eg_lum(rgb,W,fcx+sclR,cy) 198 var t3: i64=0 199 if eRing<eIris-10 { if eRing<eScl-30 { if fRing>fScl-15 { t3=1 } } } 200 if t3==1 { eg_puts("T3 PASS limbal ring: eye boundary lum "); eg_pn(eRing); eg_puts(" < iris "); eg_pn(eIris); eg_puts(" and sclera "); eg_pn(eScl); eg_puts("; doll has none\n" as *u8) } else { fails=fails+1; eg_puts("T3 FAIL limbal "); eg_pn(eRing); eg_puts(" "); eg_pn(eIris); eg_puts(" "); eg_pn(eScl); eg_puts(" doll "); eg_pn(fRing); eg_puts(" "); eg_pn(fScl); eg_puts("\n" as *u8) } 201 // T4 iris stroma fibers: same radius, ~half-spoke apart -> big lum delta on the eye, smooth on the doll 202 let rf: i64=33 203 let e1: i64=eg_lum(rgb,W,ecx+rf,cy) 204 let e2: i64=eg_lum(rgb,W,ecx+rf,cy-4) 205 let e3: i64=eg_lum(rgb,W,ecx,cy-rf) 206 let e4: i64=eg_lum(rgb,W,ecx+4,cy-rf) 207 var d1: i64=e1-e2; if d1<0 { d1=0-d1 } 208 var d2: i64=e3-e4; if d2<0 { d2=0-d2 } 209 var ed: i64=d1; if d2>ed { ed=d2 } 210 let f1: i64=eg_lum(rgb,W,fcx+22,cy) 211 let f2: i64=eg_lum(rgb,W,fcx+22,cy-4) 212 var fd: i64=f1-f2; if fd<0 { fd=0-fd } 213 var t4: i64=0 214 if ed>=12 { if fd<8 { t4=1 } } 215 if t4==1 { eg_puts("T4 PASS iris fibers: eye angular delta "); eg_pn(ed); eg_puts(" vs doll "); eg_pn(fd); eg_puts("\n" as *u8) } else { fails=fails+1; eg_puts("T4 FAIL fibers eye="); eg_pn(ed); eg_puts(" doll="); eg_pn(fd); eg_puts("\n" as *u8) } 216 // T5 corneal refraction: the eye's iris appears magnified vs the doll's identical geometric iris 217 var t5: i64=0 218 if eyeIris*100>flatIris*115 { t5=1 } 219 if t5==1 { eg_puts("T5 PASS corneal refraction magnifies iris: "); eg_pn(eyeIris); eg_puts(" px vs doll "); eg_pn(flatIris); eg_puts(" px (same geometric radius)\n" as *u8) } else { fails=fails+1; eg_puts("T5 FAIL refraction eye="); eg_pn(eyeIris); eg_puts(" doll="); eg_pn(flatIris); eg_puts("\n" as *u8) } 220 // T6 warm sclera: mean R-B over a sclera patch -- warm on the eye, zero on the paper-white doll 221 var eWarm: i64=0; var fWarm: i64=0 222 var wy: i64=cy-6 223 while wy<=cy+6 { 224 var wx: i64=56 225 while wx<=70 { 226 let eo: i64=(wy*W+ecx+wx)*3 227 let fo: i64=(wy*W+fcx+wx)*3 228 eWarm=eWarm+(rgb[eo] as i64)-(rgb[eo+2] as i64) 229 fWarm=fWarm+(rgb[fo] as i64)-(rgb[fo+2] as i64) 230 wx=wx+1 231 } 232 wy=wy+1 233 } 234 var t6: i64=0 235 if eWarm>fWarm+400 { t6=1 } 236 if t6==1 { eg_puts("T6 PASS warm sclera: eye sumR-B "); eg_pn(eWarm); eg_puts(" vs doll "); eg_pn(fWarm); eg_puts("\n" as *u8) } else { fails=fails+1; eg_puts("T6 FAIL sclera warmth "); eg_pn(eWarm); eg_puts(" "); eg_pn(fWarm); eg_puts("\n" as *u8) } 237 // T7 artifact on disk with the PNG first byte 238 let szp: *i64=sys_mmap(16) as *i64 239 let rb: *u8=sys_read_file("knowledge/nx_gx_eye.png\x00" as *u8, szp) 240 var t7: i64=0 241 if (rb as i64)!=0 { if szp[0]>1000 { if rb[0]==(137 as u8) { t7=1 } } } 242 if t7==1 { eg_puts("T7 PASS PNG ("); eg_pn(szp[0]); eg_puts(" bytes)\n" as *u8) } else { fails=fails+1; eg_puts("T7 FAIL png\n" as *u8) } 243 244 if fails==0 { eg_puts("GX-EYE GREEN -- cornea/iris/sclera shader measurably beats the doll eye on 5 cues; honest residual: lids/lashes/tear-film/vessels\n" as *u8); sys_exit(0); return 0 } 245 eg_puts("GX-EYE RED fails="); eg_pn(fails); eg_puts("\n" as *u8) 246 sys_exit(1) 247 return 1 248}