code wiki / _hdl_build / nx_photoreal_critic.nx
nx_photoreal_critic.nx source
↩ module page · 212 lines · 10975 B
1// nx_photoreal_critic.nx -- the HARD PHOTOREAL CRITIC (operator 2026-07-04: "for graphics to be photorealistic
2// you need a HARD CRITIC ... have it PARTNER with you on your upward climb"). Measures a rendered framebuffer
3// on concrete, grounded axes that separate PHOTOREAL from CG/CLAY (natural-scene-statistics family, grounded in
4// knowledge/fetched/prc_*.raw: NR-IQA measures exactly local micro-contrast + statistical regularity that clay
5// violates):
6// 1 fine_detail -- fine micro-contrast (pores/texture). CLAY ~ 0 (too smooth). #1 discriminator.
7// 2 non_flatness -- inverse of large same-color regions. CLAY = flat.
8// 3 chroma_var -- skin-tone variation (veins/blush/subsurface). CLAY = uniform.
9// 4 specular -- small sharp localized highlights. CLAY = none/broad.
10// 5 face_structure -- local detail in the head region. A blob has NO face -> reads as not-a-person.
11// CALIBRATED + LIAR-KILLED: it is PROVEN to score a flat clay image ~0 (CLAY) and to respond to real detail
12// (noise raises fine_detail) BEFORE its verdict on our render is trusted -- exactly the font-arc discipline
13// (a critic that doesn't discriminate is a rubber stamp). Partner: it names the WEAKEST axis = the next fix,
14// and states the honest CEILING of the current approach. GREEN = the critic is a VALID discriminator (NOT that
15// our render is photoreal). license_tier: ORIGINAL expect_exit: 0
16import "nx_syscalls.nx"
17import "nx_sdfrender.nx"
18const K_MAGIC_65536: i64 = 65536
19const K_MAGIC_2654435761: i64 = 2654435761
20const K_MAGIC_1013904223: i64 = 1013904223
21
22func hw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
23func pn(v: i64) -> i64 {
24 let b: *u8 = sys_mmap(32) as *u8
25 var x: i64 = v
26 var neg: i64 = 0
27 if x < 0 { neg = 1; x = 0 - x }
28 var i: i64 = 31
29 if x == 0 { b[i] = 48 as u8; i = i - 1 }
30 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 }
31 if neg == 1 { b[i] = 45 as u8; i = i - 1 }
32 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i)
33 return 0
34}
35func pl(g: i64) -> i64 { let r: i64 = g & 255; let b: i64 = (g >> 16) & 255; if r >= b { return 1 } return 0 } // foreground=skin (r>=b)
36func lum(g: i64) -> i64 { return ((g & 255) + ((g >> 8) & 255) + ((g >> 16) & 255)) / 3 }
37func chroma(g: i64) -> i64 {
38 let r: i64 = g & 255; let gg: i64 = (g >> 8) & 255; let b: i64 = (g >> 16) & 255
39 var mx: i64 = r; if gg > mx { mx = gg } if b > mx { mx = b }
40 var mn: i64 = r; if gg < mn { mn = gg } if b < mn { mn = b }
41 return mx - mn
42}
43func iabs(v: i64) -> i64 { if v < 0 { return 0 - v } return v }
44
45// measure the 5 raw signals into m[]: m[0]=fine_detail avg, m[1]=flat_pct(permil), m[2]=chroma_mad,
46// m[3]=specular_pct(permil of fg near-brightest), m[4]=head_region fine_detail. m[5]=fg count.
47func critic_measure(fb: *i64, w: i64, h: i64, m: *i64) -> i64 {
48 var fg: i64 = 0
49 var hf_sum: i64 = 0
50 var flat: i64 = 0
51 var chr_sum: i64 = 0
52 var maxl: i64 = 0
53 // pass 1: brightest fg luminance + chroma mean + fine detail
54 var y: i64 = 1
55 while y < h - 1 {
56 var x: i64 = 1
57 while x < w - 1 {
58 let c: i64 = fb[y * w + x]
59 if pl(c) == 1 {
60 fg = fg + 1
61 let l0: i64 = lum(c)
62 if l0 > maxl { maxl = l0 }
63 let nb: i64 = (lum(fb[y * w + x - 1]) + lum(fb[y * w + x + 1]) + lum(fb[(y - 1) * w + x]) + lum(fb[(y + 1) * w + x])) / 4
64 let hf: i64 = iabs(l0 - nb)
65 hf_sum = hf_sum + hf
66 if hf < 2 { flat = flat + 1 }
67 chr_sum = chr_sum + chroma(c)
68 }
69 x = x + 1
70 }
71 y = y + 1
72 }
73 if fg < 1 { fg = 1 }
74 let chr_mean: i64 = chr_sum / fg
75 m[0] = hf_sum / fg
76 m[1] = flat * 1000 / fg
77 // pass 2: chroma MAD + specular (near-brightest) + head-region detail (top 22% of fg bbox)
78 // find fg row bounds
79 var top: i64 = h
80 var bot: i64 = 0
81 y = 1
82 while y < h - 1 {
83 var x2: i64 = 1
84 while x2 < w - 1 { if pl(fb[y * w + x2]) == 1 { if y < top { top = y } if y > bot { bot = y } } x2 = x2 + 1 }
85 y = y + 1
86 }
87 let headcut: i64 = top + (bot - top) * 22 / 100
88 var chr_mad: i64 = 0
89 var spec: i64 = 0
90 var head_hf: i64 = 0
91 var head_n: i64 = 0
92 y = 1
93 while y < h - 1 {
94 var x3: i64 = 1
95 while x3 < w - 1 {
96 let c: i64 = fb[y * w + x3]
97 if pl(c) == 1 {
98 chr_mad = chr_mad + iabs(chroma(c) - chr_mean)
99 if lum(c) >= maxl - 22 { spec = spec + 1 }
100 if y <= headcut {
101 let nb: i64 = (lum(fb[y * w + x3 - 1]) + lum(fb[y * w + x3 + 1]) + lum(fb[(y - 1) * w + x3]) + lum(fb[(y + 1) * w + x3])) / 4
102 head_hf = head_hf + iabs(lum(c) - nb)
103 head_n = head_n + 1
104 }
105 }
106 x3 = x3 + 1
107 }
108 y = y + 1
109 }
110 if head_n < 1 { head_n = 1 }
111 m[2] = chr_mad / fg
112 m[3] = spec * 1000 / fg
113 m[4] = head_hf / head_n
114 m[5] = fg
115 return 0
116}
117
118// convert raw signals -> axis scores 0..1000 (HARD targets from NR-IQA/skin literature intuition).
119func clamp1000(v: i64) -> i64 { if v < 0 { return 0 } if v > 1000 { return 1000 } return v }
120func critic_score(m: *i64, a: *i64) -> i64 {
121 a[0] = clamp1000(m[0] * 1000 / 32) // fine_detail: target ~32 micro-contrast for skin pores
122 a[1] = clamp1000(1000 - m[1]) // non-flatness: penalize flat %
123 a[2] = clamp1000(m[2] * 1000 / 16) // chroma variation: target ~16 MAD
124 var sp: i64 = m[3] // specular: want SOME but SPARSE localized; 5..60permil good
125 if sp > 60 { sp = 60 + (sp - 60) / 4 } // broad speculars (huge %) are CG-plastic, not photoreal
126 a[3] = clamp1000(sp * 1000 / 45)
127 a[4] = clamp1000(m[4] * 1000 / 34) // face: head region needs eye/nose/mouth detail
128 // overall = MIN axis (a chain is as strong as its weakest link -- HARD)
129 var mn: i64 = a[0]
130 var i: i64 = 1
131 while i < 5 { if a[i] < mn { mn = a[i] } i = i + 1 }
132 return mn
133}
134func worst_axis(a: *i64) -> *u8 {
135 var mi: i64 = 0
136 var i: i64 = 1
137 while i < 5 { if a[i] < a[mi] { mi = i } i = i + 1 }
138 if mi == 0 { return "fine_detail (too smooth -- no pore/micro-texture)" as *u8 }
139 if mi == 1 { return "non_flatness (large flat same-color regions)" as *u8 }
140 if mi == 2 { return "chroma_var (skin tone too uniform -- no veins/blush/subsurface variation)" as *u8 }
141 if mi == 3 { return "specular (no small sharp highlights)" as *u8 }
142 return "face_structure (no facial features -- reads as a blob, not a person)" as *u8
143}
144func verdict(score: i64) -> *u8 {
145 if score >= 700 { return "PHOTOREAL" as *u8 }
146 if score >= 400 { return "STYLIZED-CG" as *u8 }
147 return "CLAY / not-a-person" as *u8
148}
149
150func report(label: *u8, fb: *i64, w: i64, h: i64) -> i64 {
151 let m: *i64 = sys_mmap(64) as *i64
152 let a: *i64 = sys_mmap(64) as *i64
153 critic_measure(fb, w, h, m)
154 let score: i64 = critic_score(m, a)
155 hw(label); hw(" raw{detail=" as *u8); pn(m[0]); hw(" flat%o=" as *u8); pn(m[1]); hw(" chroma=" as *u8); pn(m[2]); hw(" spec%o=" as *u8); pn(m[3]); hw(" head=" as *u8); pn(m[4]); hw("}\n" as *u8)
156 hw(" axes/1000{detail=" as *u8); pn(a[0]); hw(" flatness=" as *u8); pn(a[1]); hw(" chroma=" as *u8); pn(a[2]); hw(" spec=" as *u8); pn(a[3]); hw(" face=" as *u8); pn(a[4]); hw("}\n" as *u8)
157 hw(" SCORE " as *u8); pn(score); hw("/1000 VERDICT " as *u8); hw(verdict(score)); hw(" weakest: " as *u8); hw(worst_axis(a)); hw("\n" as *u8)
158 return score
159}
160
161func main() -> i64 {
162 var fails: i64 = 0
163 let W: i64 = ww()
164 let H: i64 = hh()
165 hw("=== nx_photoreal_critic -- HARD, calibrated, liar-killed. GREEN = valid discriminator (NOT that we're photoreal) ===\n" as *u8)
166
167 // CALIBRATION A: a flat CLAY image MUST score CLAY (detail ~0)
168 let flatfb: *i64 = sys_mmap(W * H * 8) as *i64
169 var i: i64 = 0
170 while i < W * H { flatfb[i] = 190 + 150 * 256 + 130 * K_MAGIC_65536; i = i + 1 } // uniform skin-ish tone
171 hw("-- CALIBRATION --\n" as *u8)
172 let sflat: i64 = report("flat-clay " as *u8, flatfb, W, H)
173 if sflat < 120 { hw(" [OK] flat image scores CLAY (critic is not a rubber stamp)\n" as *u8) } else { hw(" [FAIL] flat image did not score CLAY -- critic BROKEN\n" as *u8); fails = fails + 1 }
174
175 // CALIBRATION B: a high-freq detail image MUST raise fine_detail well above flat (metric is alive)
176 let noisefb: *i64 = sys_mmap(W * H * 8) as *i64
177 i = 0
178 while i < W * H {
179 let hsh: i64 = (i * K_MAGIC_2654435761 + K_MAGIC_1013904223)
180 let n: i64 = (hsh >> 5) & 127 // strong per-pixel high-frequency variation
181 let r: i64 = 110 + n; let g: i64 = 80 + n; let b: i64 = 64 + n
182 noisefb[i] = r + g * 256 + b * K_MAGIC_65536
183 i = i + 1
184 }
185 let mnoise: *i64 = sys_mmap(64) as *i64
186 let mflat2: *i64 = sys_mmap(64) as *i64
187 critic_measure(noisefb, W, H, mnoise)
188 critic_measure(flatfb, W, H, mflat2)
189 hw("detail-probe: noise fine_detail=" as *u8); pn(mnoise[0]); hw(" vs flat=" as *u8); pn(mflat2[0]); hw("\n" as *u8)
190 if mnoise[0] > mflat2[0] + 10 { hw(" [OK] the fine_detail metric RESPONDS to real detail (alive)\n" as *u8) } else { hw(" [FAIL] fine_detail metric is dead\n" as *u8); fails = fails + 1 }
191
192 // THE SUBJECT: our actual render, measured honestly
193 hw("-- SUBJECT: our sovereign SDF render (nx_sdfrender), measured --\n" as *u8)
194 let base: i64 = sys_mmap(sdf_bytes()) as i64
195 sdf_body(base)
196 sdf_render(base, 0, 5, 240, 184, 160)
197 let ours: i64 = report("OUR-render " as *u8, (base + fb_off()) as *i64, W, H)
198
199 hw("=== CRITIC VERDICT (partner) ===\n" as *u8)
200 hw("Our render scores " as *u8); pn(ours); hw("/1000 = " as *u8); hw(verdict(ours)); hw(".\n" as *u8)
201 hw("NEXT FIX (weakest axis) drives the climb -- see 'weakest' above.\n" as *u8)
202 hw("★HONEST CEILING (the font-arc lesson): the ellipsoid-SDF approach CANNOT produce pore-scale geometry,\n" as *u8)
203 hw(" real albedo/vein variation, eyes, or a face. Like primitive-stroke glyphs top out at 'clean' not\n" as *u8)
204 hw(" 'gorgeous', this tops out at 'clean stylized figure' -- NOT photoreal. More shader tweaks CANNOT\n" as *u8)
205 hw(" cross it; the face_structure + fine_detail axes will stay low until the MESH+SCAN arc (generate ->\n" as *u8)
206 hw(" 3D -> retopo -> UV -> scanned skin maps) + real eyes/hair. I will not claim photoreal progress the\n" as *u8)
207 hw(" critic does not confirm.\n" as *u8)
208
209 if fails == 0 { hw("GATE GREEN: the hard critic is a VALID, calibrated, liar-killed discriminator -- now the partner for every visual rung.\n" as *u8) }
210 else { hw("GATE RED: critic calibration failed fails=" as *u8); pn(fails); hw("\n" as *u8) }
211 return fails
212}