code wiki / _hdl_build / nx_motion_photo_gate.nx
nx_motion_photo_gate.nx source
↩ module page · 163 lines · 7535 B
1// nx_motion_photo_gate.nx -- SKIN THE MANNEQUIN, proven: the SAME move (wave) now renders as the CONTINUOUS
2// SDF body (smin-fused, SSS/pores/spec/AO shading = the photoreal-path renderer) driven by the FK rig, and the
3// CRITIC scores it against the pioneer body at the SAME resolution (the no-self-flattery bar).
4// T1 valid APNG (acTL num_frames == N)
5// T2 MOTION present (frame checksums differ)
6// T3 body ON SCREEN (foreground coverage >= 4%)
7// T4 critic: posed-body frame scores in the SAME CLASS as the static pioneer at this res (>= 60% of it)
8// Writes knowledge/nx_motion_wave_photo.png (APNG) + nx_motion_photo_frame0.png + nx_motion_photo_pioneer.png.
9// license_tier: ORIGINAL expect_exit: 0
10import "nx_syscalls.nx"
11import "nx_sdfrender.nx"
12import "nx_skeleton.nx"
13import "nx_figure_render.nx"
14import "nx_movelib.nx"
15import "nx_bodypose.nx"
16import "nx_apng.nx"
17import "nx_png.nx"
18import "nx_critvis_lib.nx"
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; var neg: i64 = 0
24 if x < 0 { neg = 1; x = 0 - x }
25 var i: i64 = 31
26 if x == 0 { b[i] = 48 as u8; i = i - 1 }
27 while x > 0 { b[i] = (48 + x % 10) as u8; x = x / 10; i = i - 1 }
28 if neg == 1 { b[i] = 45 as u8; i = i - 1 }
29 sys_write(1, (b as i64 + i + 1) as *u8, 31 - i)
30 return 0
31}
32func cksum(fb: *i64, n: i64) -> i64 {
33 var s: i64 = 1469598103
34 var i: i64 = 0
35 while i < n { s = (s * 31 + (fb[i] & 0xffffff)) & 0x0fffffffffffffff; i = i + 1 }
36 return s
37}
38func find4(buf: *u8, n: i64, a: i64, b: i64, c: i64, d: i64) -> i64 {
39 var i: i64 = 0
40 while i + 4 <= n {
41 if (buf[i]&0xff)==a { if (buf[i+1]&0xff)==b { if (buf[i+2]&0xff)==c { if (buf[i+3]&0xff)==d { return i } } } }
42 i = i + 1
43 }
44 return 0 - 1
45}
46
47const RW: i64 = 128
48const RH: i64 = 96
49const RFOCAL: i64 = 146 // 586 * 128 / 512 -> same FOV as the 512-wide production render
50
51// low-res single-ray render through the PRODUCTION shader (sdf_shade_ray: march+normal+bump+shadow+AO+SSS+spec)
52func pg_render(base: i64, fb: *i64, yaw: i64, camz_units: i64) -> i64 {
53 let R: i64 = camz_units * 1024
54 let cy4: i64 = it_cos4096(yaw)
55 let sy4: i64 = it_sin4096(yaw)
56 let rox: i64 = 0 - sy4 * R / 4096
57 let roz: i64 = 0 - cy4 * R / 4096
58 var y: i64 = 0
59 while y < RH {
60 let mc1: i64 = 26 + y * 34 / RH
61 let mc2: i64 = 28 + y * 32 / RH
62 let mc3: i64 = 44 + y * 28 / RH
63 let misscol: i64 = mc1 + mc2 * 256 + mc3 * 65536
64 var x: i64 = 0
65 while x < RW {
66 let ndcx: i64 = (2 * x + 1 - RW) * 1024 / (2 * RFOCAL)
67 let ndcy: i64 = (RH - (2 * y + 1)) * 1024 / (2 * RFOCAL)
68 let rl: i64 = sdf_isqrt(ndcx * ndcx + ndcy * ndcy + 1024 * 1024)
69 let vdx: i64 = ndcx * 1024 / rl
70 let rdy: i64 = ndcy * 1024 / rl
71 let vdz: i64 = 1024 * 1024 / rl
72 let rdx: i64 = (cy4 * vdx + sy4 * vdz) / 4096
73 let rdz: i64 = (0 - sy4 * vdx + cy4 * vdz) / 4096
74 let col: i64 = sdf_shade_ray(base, rox, 0, roz, rdx, rdy, rdz, 240, 184, 160, misscol)
75 fb[y * RW + x] = col
76 x = x + 1
77 }
78 y = y + 1
79 }
80 return 0
81}
82func fgcount(fb: *i64, n: i64) -> i64 {
83 var c: i64 = 0
84 var i: i64 = 0
85 while i < n { let px: i64 = fb[i]; if cv_pl(px) == 1 { c = c + 1 } i = i + 1 }
86 return c
87}
88
89func main() -> i64 {
90 hw("=== nx_motion_photo_gate -- the MOVE on the CONTINUOUS SDF body (photoreal-path shader), critic-scored ===\n" as *u8)
91 let base: i64 = sys_mmap(sdf_bytes()) as i64
92 let fb: *i64 = sys_mmap(RW * RH * 8) as *i64
93 let sk: i64 = sys_mmap(sk_bytes()) as i64
94 rig_build(sk)
95 let move: i64 = sys_mmap(move_bytes()) as i64
96 move_gen(move, 1) // WAVE
97 let period: i64 = (mv_hdr(move))[1]
98
99 // pioneer reference at the SAME resolution (fair same-res critic comparison)
100 sdf_body(base)
101 pg_render(base, fb, 0, 5)
102 let pioneerScore: i64 = cv_score(fb, RW, RH)
103 write_png(fb, RW, RH, "knowledge/nx_motion_photo_pioneer.png" as *u8)
104 hw(" pioneer (static sdf_body) @128x96: critic=" as *u8); pn(pioneerScore); hw("/1000 " as *u8); hw(cv_verdict(pioneerScore)); hw("\n" as *u8)
105
106 let apbuf: *u8 = sys_mmap(16 * 1024 * 1024)
107 let seq: *i64 = sys_mmap(8) as *i64; seq[0] = 0
108 let nframes: i64 = 8
109 var delay_cs: i64 = period / nframes / 10
110 if delay_cs < 4 { delay_cs = 4 }
111 var o: i64 = apng_open(apbuf, RW, RH, nframes)
112 var sum0: i64 = 0
113 var summid: i64 = 0
114 var posedScore: i64 = 0
115 var fg0: i64 = 0
116 var f: i64 = 0
117 while f < nframes {
118 let t: i64 = f * period / nframes
119 move_pose_at(move, sk, t)
120 sk_update(sk)
121 let np: i64 = bodypose_fill(base, sk)
122 pg_render(base, fb, 0, 5)
123 if f == 0 {
124 sum0 = cksum(fb, RW * RH)
125 posedScore = cv_score(fb, RW, RH)
126 fg0 = fgcount(fb, RW * RH)
127 write_png(fb, RW, RH, "knowledge/nx_motion_photo_frame0.png" as *u8)
128 hw(" posed body: parts=" as *u8); pn(np); hw(" critic=" as *u8); pn(posedScore); hw("/1000 " as *u8); hw(cv_verdict(posedScore)); hw(" fg-px=" as *u8); pn(fg0); hw("\n" as *u8)
129 }
130 if f == nframes / 2 { summid = cksum(fb, RW * RH) }
131 var first: i64 = 0
132 if f == 0 { first = 1 }
133 o = apng_frame(apbuf, o, seq, fb, RW, RH, delay_cs, first)
134 f = f + 1
135 }
136 o = apng_close(apbuf, o)
137 let fd: i64 = sys_openat_wr("knowledge/nx_motion_wave_photo.png" as *u8, 0x1a4)
138 if fd >= 0 { sys_write(fd, apbuf, o); sys_close(fd) }
139 hw(" wrote knowledge/nx_motion_wave_photo.png bytes=" as *u8); pn(o); hw(" frames=" as *u8); pn(nframes); hw("\n" as *u8)
140
141 var fails: i64 = 0
142 var t1: i64 = 1
143 if (apbuf[0]&0xff) != 137 { t1 = 0 }
144 let actl: i64 = find4(apbuf, o, 97, 99, 84, 76)
145 var nf_field: i64 = 0
146 if actl >= 0 { nf_field = ((apbuf[actl+4]&0xff)<<24)|((apbuf[actl+5]&0xff)<<16)|((apbuf[actl+6]&0xff)<<8)|(apbuf[actl+7]&0xff) }
147 if nf_field != nframes { t1 = 0 }
148 if find4(apbuf, o, 102, 100, 65, 84) < 0 { t1 = 0 }
149 if t1 == 1 { hw("T1 PASS valid APNG (acTL num_frames=" as *u8); pn(nf_field); hw(")\n" as *u8) }
150 else { fails = fails + 1; hw("T1 FAIL APNG invalid\n" as *u8) }
151 if sum0 != summid { hw("T2 PASS motion present on the SDF body (frame checksums differ)\n" as *u8) }
152 else { fails = fails + 1; hw("T2 FAIL no motion\n" as *u8) }
153 let fgmin: i64 = RW * RH * 4 / 100
154 if fg0 >= fgmin { hw("T3 PASS body on screen (fg coverage " as *u8); pn(fg0 * 100 / (RW * RH)); hw(" percent)\n" as *u8) }
155 else { fails = fails + 1; hw("T3 FAIL body not visible (fg=" as *u8); pn(fg0); hw(")\n" as *u8) }
156 let floor60: i64 = pioneerScore * 6 / 10
157 if posedScore >= floor60 { hw("T4 PASS posed-body critic score " as *u8); pn(posedScore); hw(" within class of pioneer " as *u8); pn(pioneerScore); hw(" at same res (no quality collapse from posing)\n" as *u8) }
158 else { fails = fails + 1; hw("T4 FAIL posed " as *u8); pn(posedScore); hw(" vs pioneer " as *u8); pn(pioneerScore); hw("\n" as *u8) }
159
160 if fails == 0 { hw("MOTION-PHOTO-GATE 4/4 GREEN -- the move runs on the CONTINUOUS shaded body; critic-compared to the pioneer honestly\n" as *u8); sys_exit(0); return 0 }
161 hw("MOTION-PHOTO-GATE RED fails=" as *u8); pn(fails); hw("\n" as *u8)
162 sys_exit(1); return 1
163}