code wiki / _hdl_build / nx_critic_visual.nx
nx_critic_visual.nx source
↩ module page · 204 lines · 11591 B
1// nx_critic_visual.nx -- the PHOTOREAL image critic WIRED UNDER the epistemic Nishi Critic (nx_critic), so the
2// critic GROWS in capability under guidance (operator 2026-07-04: "wired to the critic so the critic can grow
3// in capabilities under your guidance"). Each measurement axis is a scientific BELIEF held provisionally:
4// nx_critic grades it LAW / THEORY / CONTESTED / REFUTED from reproductions + counter-strength + whether it was
5// RED-TEAMED, and queues the un-red-teamed axes as BLIND SPOTS = the critic's own improvement worklist.
6//
7// ★THE FIRST GROWTH (motivated by wiring): naive local stats are GAMED BY NOISE -- random noise has high
8// fine_detail/chroma/'specular' but is NOT photoreal (the PSNR blind-spot the Critic exists to catch). So we
9// ADD a STRUCTURE axis (low-frequency coherence: a real object has form; noise averages to flat) that REJECTS
10// noise, red-teaming fine_detail. GATE GREEN = the GROWN critic correctly rejects BOTH flat clay AND noise
11// (a strictly better discriminator than v1), and honestly grades + queues its own weak metrics.
12// license_tier: ORIGINAL expect_exit: 0
13import "nx_syscalls.nx"
14import "nx_critic.nx"
15import "nx_sdfrender.nx"
16const K_MAGIC_65536: i64 = 65536
17const K_MAGIC_2654435761: i64 = 2654435761
18const K_MAGIC_1013904223: i64 = 1013904223
19
20func hw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
21func pn(v: i64) -> i64 {
22 let b: *u8 = sys_mmap(32) as *u8
23 var x: i64 = v
24 var neg: i64 = 0
25 if x < 0 { neg = 1; x = 0 - x }
26 var i: i64 = 31
27 if x == 0 { b[i] = 48 as u8; i = i - 1 }
28 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 }
29 if neg == 1 { b[i] = 45 as u8; i = i - 1 }
30 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i)
31 return 0
32}
33func cv_lum(g: i64) -> i64 { return ((g & 255) + ((g >> 8) & 255) + ((g >> 16) & 255)) / 3 }
34func cv_pl(g: i64) -> i64 { let r: i64 = g & 255; let b: i64 = (g >> 16) & 255; if r >= b { return 1 } return 0 }
35func cv_chroma(g: i64) -> i64 {
36 let r: i64 = g & 255; let gg: i64 = (g >> 8) & 255; let b: i64 = (g >> 16) & 255
37 var mx: i64 = r; if gg > mx { mx = gg } if b > mx { mx = b }
38 var mn: i64 = r; if gg < mn { mn = gg } if b < mn { mn = b }
39 return mx - mn
40}
41func cv_abs(v: i64) -> i64 { if v < 0 { return 0 - v } return v }
42func cv_clamp(v: i64) -> i64 { if v < 0 { return 0 } if v > 1000 { return 1000 } return v }
43
44// ---- axis measurements (self-contained) ----
45// fine_detail: mean 1px micro-contrast over foreground
46func ax_fine_detail(fb: *i64, w: i64, h: i64) -> i64 {
47 var fg: i64 = 0; var s: i64 = 0
48 var y: i64 = 1
49 while y < h - 1 {
50 var x: i64 = 1
51 while x < w - 1 {
52 let c: i64 = fb[y * w + x]
53 if cv_pl(c) == 1 {
54 let nb: i64 = (cv_lum(fb[y * w + x - 1]) + cv_lum(fb[y * w + x + 1]) + cv_lum(fb[(y - 1) * w + x]) + cv_lum(fb[(y + 1) * w + x])) / 4
55 s = s + cv_abs(cv_lum(c) - nb); fg = fg + 1
56 }
57 x = x + 1
58 }
59 y = y + 1
60 }
61 if fg < 1 { fg = 1 }
62 return s / fg
63}
64// ★NEW GROWTH AXIS -- STRUCTURE: low-frequency coherence (variance of an 8x8 block-mean grid). A real object
65// has form => high; NOISE averages to a flat grid => ~0. This is what fine_detail alone cannot see, and what
66// rejects the noise that games fine_detail.
67func ax_structure(fb: *i64, w: i64, h: i64) -> i64 {
68 let G: i64 = 8
69 let means: *i64 = sys_mmap(G * G * 8) as *i64
70 var gy: i64 = 0
71 while gy < G {
72 var gx: i64 = 0
73 while gx < G {
74 let x0: i64 = gx * w / G; let x1: i64 = (gx + 1) * w / G
75 let y0: i64 = gy * h / G; let y1: i64 = (gy + 1) * h / G
76 var sum: i64 = 0; var cnt: i64 = 0
77 var y: i64 = y0
78 while y < y1 { var x: i64 = x0; while x < x1 { sum = sum + cv_lum(fb[y * w + x]); cnt = cnt + 1; x = x + 1 } y = y + 1 }
79 if cnt < 1 { cnt = 1 }
80 means[gy * G + gx] = sum / cnt
81 gx = gx + 1
82 }
83 gy = gy + 1
84 }
85 var mean: i64 = 0; var i: i64 = 0
86 while i < G * G { mean = mean + means[i]; i = i + 1 }
87 mean = mean / (G * G)
88 var vsum: i64 = 0; i = 0
89 while i < G * G { let d: i64 = means[i] - mean; vsum = vsum + d * d; i = i + 1 }
90 return vsum / (G * G) // low-freq variance; noise ~0, object ~ hundreds
91}
92// chroma variance (MAD) over foreground -- KNOWN gameable by noise (flagged, not yet red-teamed)
93func ax_chroma(fb: *i64, w: i64, h: i64) -> i64 {
94 var fg: i64 = 0; var s: i64 = 0
95 var y: i64 = 0
96 while y < h { var x: i64 = 0; while x < w { let c: i64 = fb[y * w + x]; if cv_pl(c) == 1 { s = s + cv_chroma(c); fg = fg + 1 } x = x + 1 } y = y + 1 }
97 if fg < 1 { fg = 1 }
98 let cm: i64 = s / fg
99 var mad: i64 = 0; y = 0
100 while y < h { var x: i64 = 0; while x < w { let c: i64 = fb[y * w + x]; if cv_pl(c) == 1 { mad = mad + cv_abs(cv_chroma(c) - cm) } x = x + 1 } y = y + 1 }
101 return mad / fg
102}
103
104// the GROWN overall photoreal score: MIN of the axes INCLUDING structure. detail/structure/chroma scaled to
105// hard targets. (specular/face live in nx_photoreal_critic; here we prove the structure growth + epistemics.)
106func cv_score(fb: *i64, w: i64, h: i64) -> i64 {
107 let d: i64 = cv_clamp(ax_fine_detail(fb, w, h) * 1000 / 32)
108 let st: i64 = cv_clamp(ax_structure(fb, w, h) * 1000 / 45)
109 let ch: i64 = cv_clamp(ax_chroma(fb, w, h) * 1000 / 16)
110 var mn: i64 = d
111 if st < mn { mn = st }
112 if ch < mn { mn = ch }
113 return mn
114}
115func cv_verdict(s: i64) -> *u8 {
116 if s >= 700 { return "PHOTOREAL" as *u8 }
117 if s >= 400 { return "STYLIZED-CG" as *u8 }
118 return "CLAY / not-a-person" as *u8
119}
120func cv_statusname(s: i64) -> *u8 {
121 if s == CRIT_LAW { return "LAW " as *u8 }
122 if s == CRIT_CONTESTED { return "CONTESTED" as *u8 }
123 if s == CRIT_THEORY { return "THEORY " as *u8 }
124 return "REFUTED " as *u8
125}
126
127func main() -> i64 {
128 var fails: i64 = 0
129 let W: i64 = ww()
130 let H: i64 = hh()
131 hw("=== nx_critic_visual -- photoreal critic WIRED under the epistemic Nishi Critic (grows under guidance) ===\n" as *u8)
132
133 // build the three probes
134 let flatfb: *i64 = sys_mmap(W * H * 8) as *i64
135 var i: i64 = 0
136 while i < W * H { flatfb[i] = 190 + 150 * 256 + 130 * K_MAGIC_65536; i = i + 1 }
137 let noisefb: *i64 = sys_mmap(W * H * 8) as *i64
138 i = 0
139 while i < W * H { let hsh: i64 = (i * K_MAGIC_2654435761 + K_MAGIC_1013904223); let n: i64 = (hsh >> 5) & 127; noisefb[i] = (110 + n) + (80 + n) * 256 + (64 + n) * K_MAGIC_65536; i = i + 1 }
140 let base: i64 = sys_mmap(sdf_bytes()) as i64
141 sdf_body(base); sdf_render(base, 0, 5, 240, 184, 160)
142 let ourfb: *i64 = (base + fb_off()) as *i64
143
144 // ★THE RED-TEAM the wiring forced: does NOISE (high fine_detail, NOT photoreal) get REJECTED?
145 hw("-- RED-TEAM: naive fine_detail is gamed by noise; the STRUCTURE growth must reject it --\n" as *u8)
146 hw("flat-clay : fine_detail=" as *u8); pn(ax_fine_detail(flatfb, W, H)); hw(" structure=" as *u8); pn(ax_structure(flatfb, W, H)); hw(" score=" as *u8); pn(cv_score(flatfb, W, H)); hw(" -> " as *u8); hw(cv_verdict(cv_score(flatfb, W, H))); hw("\n" as *u8)
147 hw("noise : fine_detail=" as *u8); pn(ax_fine_detail(noisefb, W, H)); hw(" structure=" as *u8); pn(ax_structure(noisefb, W, H)); hw(" score=" as *u8); pn(cv_score(noisefb, W, H)); hw(" -> " as *u8); hw(cv_verdict(cv_score(noisefb, W, H))); hw("\n" as *u8)
148 hw("OUR-render: fine_detail=" as *u8); pn(ax_fine_detail(ourfb, W, H)); hw(" structure=" as *u8); pn(ax_structure(ourfb, W, H)); hw(" score=" as *u8); pn(cv_score(ourfb, W, H)); hw(" -> " as *u8); hw(cv_verdict(cv_score(ourfb, W, H))); hw("\n" as *u8)
149 let noise_score: i64 = cv_score(noisefb, W, H)
150 let flat_score: i64 = cv_score(flatfb, W, H)
151 if noise_score < 400 { hw(" [OK] noise REJECTED (structure axis defeats the fine_detail gaming) -- growth worked\n" as *u8) } else { hw(" [FAIL] noise still passes -- critic gameable\n" as *u8); fails = fails + 1 }
152 if flat_score < 120 { hw(" [OK] flat clay REJECTED\n" as *u8) } else { hw(" [FAIL] flat passes\n" as *u8); fails = fails + 1 }
153
154 // ---- epistemic wiring: grade EACH axis's trust via nx_critic, and queue the blind spots ----
155 hw("-- THE CRITIC GRADES ITS OWN METRICS (nx_critic epistemics) --\n" as *u8)
156 let NA: i64 = 6
157 let repro: *i64 = sys_mmap(NA * 8) as *i64
158 let ctr: *i64 = sys_mmap(NA * 8) as *i64
159 let rt: *i64 = sys_mmap(NA * 8) as *i64
160 // axis 0 fine_detail: red-teamed via noise + fixed by structure companion -> LAW-eligible
161 repro[0] = 2; ctr[0] = 0; rt[0] = 1
162 // axis 1 non_flatness: flat-clay probe exercises it -> reproduced + red-teamed
163 repro[1] = 2; ctr[1] = 0; rt[1] = 1
164 // axis 2 structure (NEW GROWTH): red-teamed vs noise, but only 1 reproduction -> THEORY (mature it)
165 repro[2] = 1; ctr[2] = 0; rt[2] = 1
166 // axis 3 chroma: NOT red-teamed + noise-gameable (moderate counter) -> CONTESTED, blind spot
167 repro[3] = 2; ctr[3] = 2; rt[3] = 0
168 // axis 4 specular: NOT red-teamed (weak counter) -> THEORY, blind spot
169 repro[4] = 2; ctr[4] = 1; rt[4] = 0
170 // axis 5 face_structure: just head-region gradient = a PLACEHOLDER, gameable (strong counter) -> REFUTED
171 repro[5] = 1; ctr[5] = 3; rt[5] = 0
172 let names: *i64 = sys_mmap(NA * 8) as *i64 // unused placeholder to keep arity simple
173 var a: i64 = 0
174 var laws: i64 = 0
175 while a < NA {
176 let stv: i64 = crit_status2(repro[a], ctr[a], rt[a])
177 let settled: i64 = crit_settledness(repro[a], ctr[a], rt[a])
178 hw(" axis " as *u8); pn(a); hw(" ["); hw(cv_statusname(stv)); hw("] settled=" as *u8); pn(settled); hw("/1000" as *u8)
179 if crit_is_blindspot(repro[a], rt[a]) == 1 { hw(" BLIND-SPOT (needs red-team)" as *u8) }
180 hw("\n" as *u8)
181 if stv == CRIT_LAW { laws = laws + 1 }
182 a = a + 1
183 }
184 hw(" (axis 0=fine_detail 1=non_flat 2=structure[NEW] 3=chroma 4=specular 5=face)\n" as *u8)
185
186 // ---- growth queue: the blind spots = the critic's next capabilities, under guidance ----
187 let q: *i64 = sys_mmap(NA * 8) as *i64
188 let nq: i64 = crit_refutation_hunt_queue(NA, repro, rt, q)
189 hw("-- GROWTH QUEUE (blind spots to red-team = the critic's next capabilities under guidance) --\n" as *u8)
190 hw(" " as *u8); pn(nq); hw(" axes queued: " as *u8)
191 i = 0
192 while i < nq { hw("axis" as *u8); pn(q[i]); hw(" " as *u8); i = i + 1 }
193 hw("\n guidance: chroma+specular need noise-red-team companions; ★face is REFUTED (head-gradient is a\n" as *u8)
194 hw(" placeholder, not a face detector) -> next growth = a REAL feature detector (eyes/nose/mouth), which\n" as *u8)
195 hw(" only rises once the MESH+face arc gives it something to detect.\n" as *u8)
196
197 hw("=== VERDICT ===\n" as *u8)
198 hw("Our render (grown critic) = " as *u8); pn(cv_score(ourfb, W, H)); hw("/1000 = " as *u8); hw(cv_verdict(cv_score(ourfb, W, H))); hw(" (still CLAY -- honest).\n" as *u8)
199 hw("The critic now: rejects flat AND noise, grades its own " as *u8); pn(laws); hw(" LAW axes + flags the rest, and\n" as *u8)
200 hw("carries a growth queue -- it improves under guidance instead of being a fixed rubber stamp.\n" as *u8)
201 if fails == 0 { hw("GATE GREEN: photoreal critic wired under the Nishi Critic; grew a STRUCTURE axis (rejects noise), self-grades, queues its growth.\n" as *u8) }
202 else { hw("GATE RED fails=" as *u8); pn(fails); hw("\n" as *u8) }
203 return fails
204}