code wiki / _hdl_build / nx_vcodec_luma32_vm_gate.nx
nx_vcodec_luma32_vm_gate.nx source
↩ module page · 60 lines · 3511 B
1// nx_vcodec_luma32_vm_gate.nx -- LOCALIZER: does the shipped luma nx_vcodec.wasm run byte-exact in the VM
2// at 32x32 (4 macroblocks, BW=2)? The existing inter VM gate only ever tested 16x16 (1 macroblock). The
3// color VM gate traps during the 32x32 encode; this isolates whether the MULTI-MACROBLOCK wasm path is the
4// culprit (luma 32x32 also traps => engine/lowering bug, pre-existing, affects H9 at real resolutions) or
5// whether it's specific to my color wrapper (luma 32x32 GREEN => look at the color wrapper). expect_exit: 0
6// license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_gate_emit_lib.nx"
9import "nx_wasm_vm.nx"
10import "nx_vcodec.nx"
11
12const VCODEC_WASM: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/web_assets/_video_build/nx_vcodec.wasm"
13const O_CUR: i64 = 0
14const O_PREV: i64 = 16384
15const O_DEC: i64 = 49152
16
17func main() -> i64 {
18 g_puts("nx_vcodec LUMA 32x32 (4-macroblock) wasm-VM localizer\n" as *u8)
19 var pass: i64 = 0; var total: i64 = 0
20 let box: *i64 = sys_mmap(16) as *i64
21 let wasm: *u8 = sys_read_file(VCODEC_WASM, box)
22 if (wasm as i64) == 0 { g_puts(" FAIL cannot read nx_vcodec.wasm\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 }
23 let mod: *WasmMod = wm_new(wasm, box[0])
24 if wm_parse(mod) != 0 { g_puts(" FAIL parse\n" as *u8); g_puts("verdict=RED\n" as *u8); sys_exit(1); return 1 }
25 mod.mem = sys_mmap(mod.mem_bytes) as *u8
26
27 let W: i64 = 32; let H: i64 = 32; let N: i64 = W*H; let QP: i64 = 20; let SAD: i64 = 64
28 let cur: *u8 = sys_mmap(N) as *u8
29 let prev: *u8 = sys_mmap(N) as *u8
30 let eR: *u8 = sys_mmap(N) as *u8
31 let dR: *u8 = sys_mmap(N) as *u8
32 let stream: *u8 = sys_mmap(8192) as *u8
33 let blk: *i64 = sys_mmap(16*8) as *i64
34 let mv: *i64 = sys_mmap(2*8) as *i64
35
36 var y: i64 = 0
37 while y < H { var x: i64 = 0
38 while x < W { cur[y*W+x] = (40 + ((x*11 + y*7) % 120)) as u8; x = x + 1 } y = y + 1 }
39
40 g_puts(" [running native keyframe 32x32...]\n" as *u8)
41 let nbits: i64 = vc_enc_frame_packed(cur, prev, eR, W, H, QP, 1, SAD, stream, blk, mv)
42 vc_dec_frame_packed(prev, dR, W, H, QP, stream, blk, mv)
43 g_puts(" [native done nbits=" as *u8); g_pn(nbits); g_puts("; running VM 32x32 (the suspect path)...]\n" as *u8)
44
45 var i: i64 = 0
46 while i < N { mod.mem[O_CUR + i] = cur[i]; i = i + 1 }
47 let vbits: i64 = wm_run(mod, "vc_wasm_encode" as *u8, W, H, QP, 1, SAD, 5)
48 g_puts(" [VM encode survived; vbits=" as *u8); g_pn(vbits); g_puts("; running VM decode...]\n" as *u8)
49 wm_run(mod, "vc_wasm_decode" as *u8, W, H, QP, 0, 0, 3)
50 var mm: i64 = 0
51 i = 0; while i < N { if (mod.mem[O_DEC + i] as i64) != (dR[i] as i64) { mm = mm + 1 } i = i + 1 }
52
53 g_puts(" luma32 bits VM=" as *u8); g_pn(vbits); g_puts(" native=" as *u8); g_pn(nbits); g_puts(" recon-mismatch-px=" as *u8); g_pn(mm); g_puts("\n" as *u8)
54 pass = pass + g_check("luma 32x32 (4 MB) recon VM==native byte-exact" as *u8, mm == 0); total = total + 1
55 pass = pass + g_check("luma 32x32 bits VM==native" as *u8, (vbits == nbits) as i64); total = total + 1
56
57 g_puts("---- luma32 localizer: passed " as *u8); g_pn(pass); g_puts(" / " as *u8); g_pn(total); g_puts(" ----\n" as *u8)
58 if pass == total { g_puts("verdict=GREEN -- engine multi-macroblock IS wasm-faithful; trap is color-wrapper-specific\n" as *u8); sys_exit(0); return 0 }
59 g_puts("verdict=RED -- multi-macroblock wasm path is the issue (pre-existing, not color-specific)\n" as *u8); sys_exit(1); return 1
60}