code wiki / _hdl_build / nx_dxbc_exec_gate.nx

nx_dxbc_exec_gate.nx source

↩ module page · 62 lines · 4611 B

1// nx_dxbc_exec_gate.nx -- D2 proof, EXTERNALLY GRADED: execute the REAL fxc-compiled arithmetic shader 2// (knowledge/arith_shader.cso = "return c*2.0 + 0.5", a mad) on known inputs and grade the float32 output 3// bit-exactly against the shader's known semantics. If ANY operand-decode or execution bit is wrong, the output 4// won't match -> this validates the whole D2 path (operand decode + register I/O + mad) end-to-end. license_tier: ORIGINAL expect_exit: 0 5import "nx_syscalls.nx" 6import "nx_dxbc.nx" 7import "nx_dxbc_exec.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// print a float32 value x10 (readable one-decimal): 1.5 -> "15" 12func pf(bits: i64) -> i64 { pn(f32_int(f32_mul(bits, f32_of(10)))); return 0 } 13 14func main() -> i64 { 15 hw("=== nx_dxbc_exec_gate -- D2: EXECUTE a real fxc shader (c*2+0.5), grade output vs ground truth ===\n" as *u8) 16 var fails: i64 = 0 17 let szp: *i64 = sys_mmap(16) as *i64 18 let buf: *u8 = sys_read_file("knowledge/arith_shader.cso" as *u8, szp) 19 let n: i64 = szp[0] 20 if (buf as i64) == 0 { hw("FAIL cannot read arith_shader.cso (run fxc first)\n" as *u8); sys_exit(1); return 1 } 21 let payoff: i64 = dxbc_find(buf, n, dx_fourcc(83, 72, 69, 88)) 22 let pt: i64 = dxbc_program_type(dx_u32(buf, payoff)) 23 var t1: i64 = 0 24 if payoff > 0 { if pt == 0 { t1 = 1 } } 25 if t1 == 1 { hw("T1 PASS real shader loaded (ps_5_0, SHEX found)\n" as *u8) } else { fails=fails+1; hw("T1 FAIL\n" as *u8) } 26 27 let regs: *i64 = sys_mmap(256*8) as *i64 28 29 // ---- Test A: input v0 = (0.25, 0.5, 0.75, 1.0) -> expect o0 = (1.0, 1.5, 2.0, 2.5) ---- 30 regs[0] = f32_div(f32_of(1), f32_of(4)) 31 regs[1] = f32_div(f32_of(1), f32_of(2)) 32 regs[2] = f32_div(f32_of(3), f32_of(4)) 33 regs[3] = f32_of(1) 34 sh_execute(buf, payoff, n, regs) 35 hw(" A: in(0.25,0.5,0.75,1.0) -> out x10 = "); pf(regs[64]); hw(" "); pf(regs[65]); hw(" "); pf(regs[66]); hw(" "); pf(regs[67]); hw(" (expect 10 15 20 25)\n" as *u8) 36 var t2: i64 = 0 37 if regs[64] == 1065353216 { if regs[65] == 1069547520 { if regs[66] == 1073741824 { if regs[67] == 1075838976 { t2 = 1 } } } } 38 if t2 == 1 { hw("T2 PASS o0 == (1.0,1.5,2.0,2.5) bit-exact -- operand decode + mad execute CORRECT\n" as *u8) } else { fails=fails+1; hw("T2 FAIL o0 bits="); pn(regs[64]); hw(","); pn(regs[65]); hw(","); pn(regs[66]); hw(","); pn(regs[67]); hw("\n" as *u8) } 39 40 // ---- Test B: input v0 = (1,2,3,4) -> expect o0 = (2.5, 4.5, 6.5, 8.5) (different input = real computation) ---- 41 regs[0] = f32_of(1); regs[1] = f32_of(2); regs[2] = f32_of(3); regs[3] = f32_of(4) 42 sh_execute(buf, payoff, n, regs) 43 hw(" B: in(1,2,3,4) -> out x10 = "); pf(regs[64]); hw(" "); pf(regs[65]); hw(" "); pf(regs[66]); hw(" "); pf(regs[67]); hw(" (expect 25 45 65 85)\n" as *u8) 44 var t3: i64 = 0 45 if regs[64] == 1075838976 { if regs[65] == 1083179008 { if regs[66] == 1087373312 { if regs[67] == 1091043328 { t3 = 1 } } } } 46 if t3 == 1 { hw("T3 PASS o0 == (2.5,4.5,6.5,8.5) on a DIFFERENT input -- it truly computes, not constants\n" as *u8) } else { fails=fails+1; hw("T3 FAIL o0 bits="); pn(regs[64]); hw(","); pn(regs[65]); hw(","); pn(regs[66]); hw(","); pn(regs[67]); hw("\n" as *u8) } 47 48 let rep: *u8 = sys_mmap(1024) 49 var q: i64 = 0 50 var s: *u8 = "NISHI DXBC EXECUTE -- D2. A REAL fxc-compiled ps_5_0 shader (c*2+0.5) is decoded (operands: type/swizzle/mask/immediate from the real token layout) and EXECUTED on a 4-comp float32 register machine (hardware mulss/addss). Output graded bit-exact vs the shader's known semantics on two inputs. This validates operand-decode + execution end-to-end vs Microsoft's compiler. NEXT D2b: dp3/dot + texture SAMPLE + WARP pixel-diff.\n" as *u8 51 var k: i64 = 0 52 while s[k] != (0 as u8) { rep[q] = s[k]; q = q + 1; k = k + 1 } 53 rep[q] = 0 as u8 54 let fd: i64 = sys_openat_wr("knowledge/nx_dxbc_exec.txt\x00" as *u8, 0x1a4) 55 sys_write(fd, rep, q); sys_close(fd) 56 hw("T4 artifact -> knowledge/nx_dxbc_exec.txt ("); pn(q); hw(" bytes)\n" as *u8) 57 58 if fails == 0 { hw("NX-DXBC-EXEC GREEN -- D2: a real fxc shader DECODES + EXECUTES correctly, bit-exact vs ground truth\n" as *u8); sys_exit(0); return 0 } 59 hw("NX-DXBC-EXEC RED fails="); pn(fails); hw("\n" as *u8) 60 sys_exit(1) 61 return 1 62}