code wiki / _hdl_build / nx_tex_sample_gate.nx

nx_tex_sample_gate.nx source

↩ module page · 99 lines · 5486 B

1// nx_tex_sample_gate.nx -- proves general UV-sampled TEXTURE MAPPING: nx_tex_sample bilinear 4-tap + wrap modes 2// (clamp/repeat/mirror), the GLSL texture2D / HLSL Sample primitive, pure integer. Hand-computed KATs + a 3// neg-control + an artifact. Refutes the graphics-census "texture mapping PARTIAL: no general UV-sampled textures". 4// license_tier: ORIGINAL expect_exit: 0 5import "nx_syscalls.nx" 6import "nx_tex_sample.nx" 7 8func hw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 9func 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 } 10func ocat(o: *u8, at: i64, s: *u8) -> i64 { var i: i64=0; var a: i64=at; while s[i]!=(0 as u8){o[a]=s[i]; a=a+1; i=i+1} return a } 11func ocatn(o: *u8, at: i64, v: i64) -> i64 { 12 let b: *u8 = sys_mmap(32) as *u8 13 var x: i64 = v; var ng: i64 = 0 14 if x < 0 { ng = 1; x = 0 - x } 15 var i: i64 = 31 16 if x == 0 { b[i]=48 as u8; i=i-1 } 17 while x > 0 { b[i]=(48+x%10) as u8; x=x/10; i=i-1 } 18 if ng == 1 { b[i]=45 as u8; i=i-1 } 19 var a: i64 = at; var j: i64 = i+1 20 while j < 32 { o[a]=b[j]; a=a+1; j=j+1 } 21 return a 22} 23 24func main() -> i64 { 25 hw("=== nx_tex_sample_gate -- general UV-sampled texture mapping (bilinear + wrap) ===\n" as *u8) 26 var fails: i64 = 0 27 let Q: i64 = 1024 28 29 // 2x2 grayscale texture: (0,0)=0 (1,0)=100 (0,1)=200 (1,1)=40 (row-major px[y*2+x]) 30 let tex: *Image = nx_image_alloc(2, 2, 1) 31 let px: *u8 = tex.pixels 32 px[0] = 0 as u8 33 px[1] = 100 as u8 34 px[2] = 200 as u8 35 px[3] = 40 as u8 36 37 // T1 NEAREST fetches the right texels at UV corners 38 var t1: i64 = 1 39 if nx_txs_sample_nearest(tex, 256, 256, NX_TXS_WRAP_CLAMP) != 0 { t1 = 0 } 40 if nx_txs_sample_nearest(tex, 768, 256, NX_TXS_WRAP_CLAMP) != 100 { t1 = 0 } 41 if nx_txs_sample_nearest(tex, 256, 768, NX_TXS_WRAP_CLAMP) != 200 { t1 = 0 } 42 if t1 == 1 { hw("T1 PASS nearest fetches the right texels (0/100/200)\n" as *u8) } else { fails=fails+1; hw("T1 FAIL nearest\n" as *u8) } 43 44 // T2 BILINEAR at the exact center = the 4-texel average (0+100+200+40)/4 = 85 45 let cen: i64 = nx_txs_sample_bilinear(tex, 512, 512, NX_TXS_WRAP_CLAMP) 46 var t2: i64 = 0 47 if cen == 85 { t2 = 1 } 48 if t2 == 1 { hw("T2 PASS bilinear center = 4-texel average (85)\n" as *u8) } else { fails=fails+1; hw("T2 FAIL bilinear center="); pn(cen); hw(" expected 85\n" as *u8) } 49 50 // T3 WRAP modes distinct via public API: at u=2.0, clamp->col1(100), repeat->col0(0) 51 let wc: i64 = nx_txs_sample_nearest(tex, 2048, 256, NX_TXS_WRAP_CLAMP) 52 let wr: i64 = nx_txs_sample_nearest(tex, 2048, 256, NX_TXS_WRAP_REPEAT) 53 var t3: i64 = 0 54 if wc == 100 { if wr == 0 { t3 = 1 } } 55 if t3 == 1 { hw("T3 PASS clamp vs repeat distinct at u=2.0 (100 vs 0)\n" as *u8) } else { fails=fails+1; hw("T3 FAIL wrap clamp="); pn(wc); hw(" repeat="); pn(wr); hw("\n" as *u8) } 56 57 // T4 MIRROR distinct from repeat: at u=3.0, mirror->col1(100), repeat->col0(0) 58 let wm: i64 = nx_txs_sample_nearest(tex, 3072, 256, NX_TXS_WRAP_MIRROR) 59 let wr2: i64 = nx_txs_sample_nearest(tex, 3072, 256, NX_TXS_WRAP_REPEAT) 60 var t4: i64 = 0 61 if wm == 100 { if wr2 == 0 { t4 = 1 } } 62 if t4 == 1 { hw("T4 PASS mirror vs repeat distinct at u=3.0 (100 vs 0)\n" as *u8) } else { fails=fails+1; hw("T4 FAIL mirror="); pn(wm); hw(" repeat="); pn(wr2); hw("\n" as *u8) } 63 64 // T5 NEG CONTROL: bilinear at center is a real BLEND (not equal to any single texel -> proves interpolation) 65 var t5: i64 = 1 66 if cen == 0 { t5 = 0 } 67 if cen == 100 { t5 = 0 } 68 if cen == 200 { t5 = 0 } 69 if cen == 40 { t5 = 0 } 70 if t5 == 1 { hw("T5 PASS neg-control: bilinear blends (85 is no single texel)\n" as *u8) } else { fails=fails+1; hw("T5 FAIL bilinear collapsed to a texel\n" as *u8) } 71 72 // T6 ARTIFACT: bilinear-upsample the 2x2 texture to 16x16 and dump as a KAT report 73 let rep: *u8 = sys_mmap(8192) 74 var o: i64 = 0 75 o = ocat(rep, o, "NISHI TEXTURE MAPPING -- general UV-sampled textures (nx_tex_sample): bilinear 4-tap + clamp/repeat/mirror wrap, Q10 UV = the GLSL texture2D / HLSL Sample primitive, pure integer.\n" as *u8) 76 o = ocat(rep, o, "2x2 tex {0,100 / 200,40}. KATs: nearest corners OK; bilinear center=" as *u8); o = ocatn(rep, o, cen); o = ocat(rep, o, " (=4-texel avg 85); clamp/repeat/mirror distinct.\n16x16 bilinear upsample (clamp), row-major 0-255:\n" as *u8) 77 var yy: i64 = 0 78 while yy < 16 { 79 var xx: i64 = 0 80 while xx < 16 { 81 let u: i64 = xx * Q / 16 82 let v: i64 = yy * Q / 16 83 let s: i64 = nx_txs_sample_bilinear(tex, u, v, NX_TXS_WRAP_CLAMP) 84 o = ocatn(rep, o, s); o = ocat(rep, o, " " as *u8) 85 xx = xx + 1 86 } 87 o = ocat(rep, o, "\n" as *u8) 88 yy = yy + 1 89 } 90 rep[o] = 0 as u8 91 let fd: i64 = sys_openat_wr("knowledge/nx_tex_sample.txt\x00" as *u8, 0x1a4) 92 sys_write(fd, rep, o); sys_close(fd) 93 hw("T6 artifact -> knowledge/nx_tex_sample.txt ("); pn(o); hw(" bytes)\n" as *u8) 94 95 if fails == 0 { hw("NX-TEX-SAMPLE GREEN -- general UV texture sampling: bilinear + 3 wrap modes gate-proven\n" as *u8); sys_exit(0); return 0 } 96 hw("NX-TEX-SAMPLE RED fails="); pn(fails); hw("\n" as *u8) 97 sys_exit(1) 98 return 1 99}