code wiki / _hdl_build / nx_dxbc_real_gate.nx
nx_dxbc_real_gate.nx source
↩ module page · 63 lines · 4346 B
1// nx_dxbc_real_gate.nx -- HEAVY VET (MSU discipline): grade our DXBC decoder against a REAL fxc-compiled shader, not
2// our own fixture. Ground truth = Microsoft fxc /Fc disassembly of ps_5_0 (knowledge/real_shader.cso, 760B, 5 chunks):
3// 7 declarations (dcl_globalFlags/sampler/resource_texture2d/input_ps x2/output/temps) + sample + mad + mov + ret,
4// ZERO dp3 (the optimizer folded dot(n,(0,0,1)) -> .zzzz). program type = 0 (pixel).
5// If our opcode numbers / container walk are wrong, THIS is what catches it (a self-gate cannot). license_tier: ORIGINAL expect_exit: 0
6import "nx_syscalls.nx"
7import "nx_dxbc.nx"
8
9func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func 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 }
11
12func main() -> i64 {
13 hw("=== nx_dxbc_real_gate -- VET our decoder vs a REAL fxc-compiled DXBC (external ground truth) ===\n" as *u8)
14 var fails: i64 = 0
15 let szp: *i64 = sys_mmap(16) as *i64
16 let buf: *u8 = sys_read_file("knowledge/real_shader.cso" as *u8, szp)
17 let n: i64 = szp[0]
18 if (buf as i64) == 0 { hw("FAIL cannot read knowledge/real_shader.cso (run fxc first)\n" as *u8); sys_exit(1); return 1 }
19 hw(" read real_shader.cso "); pn(n); hw(" bytes\n" as *u8)
20
21 // T1 magic + total_size matches fxc (760)
22 var t1: i64 = 0
23 if dxbc_is(buf, n) == 1 { if dxbc_total_size(buf) == 760 { t1 = 1 } }
24 if t1 == 1 { hw("T1 PASS real DXBC magic + total_size=760 (matches fxc)\n" as *u8) } else { fails=fails+1; hw("T1 FAIL magic/size ts="); pn(dxbc_total_size(buf)); hw("\n" as *u8) }
25
26 // T2 chunk_count == 5 (fxc: RDEF/ISGN/OSGN/SHEX/STAT) -- our walk must handle the REAL multi-chunk container
27 let cc: i64 = dxbc_chunk_count(buf)
28 var t2: i64 = 0
29 if cc == 5 { t2 = 1 }
30 if t2 == 1 { hw("T2 PASS chunk_count=5 (real container, not our 1-chunk fixture)\n" as *u8) } else { fails=fails+1; hw("T2 FAIL cc="); pn(cc); hw("\n" as *u8) }
31
32 // T3 find SHEX among the 5 chunks + program type 0 (pixel, ps_5_0)
33 let payoff: i64 = dxbc_find(buf, n, dx_fourcc(83, 72, 69, 88))
34 let pt: i64 = dxbc_program_type(dx_u32(buf, payoff))
35 hw(" SHEX @"); pn(payoff); hw(" program_type="); pn(pt); hw(" (fxc: 0=pixel)\n" as *u8)
36 var t3: i64 = 0
37 if payoff > 0 { if pt == 0 { t3 = 1 } }
38 if t3 == 1 { hw("T3 PASS found SHEX in the real container; program type = PIXEL\n" as *u8) } else { fails=fails+1; hw("T3 FAIL payoff="); pn(payoff); hw(" pt="); pn(pt); hw("\n" as *u8) }
39
40 // T4 decode + grade classification vs fxc ground truth
41 let cnt: *i64 = sys_mmap(64) as *i64
42 dxbc_decode_shader(buf, payoff, n, cnt)
43 hw(" OURS: total="); pn(cnt[0]); hw(" dcl="); pn(cnt[1]); hw(" mov="); pn(cnt[2]); hw(" arith="); pn(cnt[3]); hw(" dot="); pn(cnt[4]); hw(" sample="); pn(cnt[5]); hw(" ret="); pn(cnt[6]); hw("\n" as *u8)
44 hw(" FXC : total=11 dcl=7 mov=1 arith(mad)=1 dot=0 sample=1 ret=1\n" as *u8)
45 var t4: i64 = 0
46 if cnt[5] == 1 { if cnt[3] == 1 { if cnt[2] == 1 { if cnt[6] == 1 { if cnt[4] == 0 { t4 = 1 } } } } }
47 if t4 == 1 { hw("T4 PASS executable ops match fxc: sample=1 mad=1 mov=1 ret=1 dp3=0\n" as *u8) } else { fails=fails+1; hw("T4 FAIL executable-op classification disagrees with fxc\n" as *u8) }
48
49 // T5 declaration count matches fxc (7 dcl_*)
50 var t5: i64 = 0
51 if cnt[1] == 7 { t5 = 1 }
52 if t5 == 1 { hw("T5 PASS declaration count=7 matches fxc\n" as *u8) } else { fails=fails+1; hw("T5 FAIL dcl="); pn(cnt[1]); hw(" expected 7\n" as *u8) }
53
54 // T6 total instruction count matches fxc (11)
55 var t6: i64 = 0
56 if cnt[0] == 11 { t6 = 1 }
57 if t6 == 1 { hw("T6 PASS total instruction count=11 matches fxc\n" as *u8) } else { fails=fails+1; hw("T6 FAIL total="); pn(cnt[0]); hw(" expected 11\n" as *u8) }
58
59 if fails == 0 { hw("NX-DXBC-REAL GREEN -- our decoder EXTERNALLY VALIDATED against Microsoft fxc on a real ps_5_0 shader\n" as *u8); sys_exit(0); return 0 }
60 hw("NX-DXBC-REAL RED fails="); pn(fails); hw(" -- external tool caught a decoder bug (the MSU value); fix + re-run\n" as *u8)
61 sys_exit(1)
62 return 1
63}