nx_pe_dll_d3d11draw.nx source
↩ module page · 208 lines · 10960 B
1// nx_pe_dll_d3d11draw.nx -- REAL GEOMETRY DRAW through the D3D11 API. Superset of d3d11ctx: device(43)+context(108)
2// vtables, 12 compiled methods. A foreign process via <d3d11.h>: create device -> GetImmediateContext -> CreateBuffer
3// (verts) [dev 3] -> CreateRenderTargetView [dev 9] -> OMSetRenderTargets [ctx 33] -> IASetVertexBuffers [ctx 18] ->
4// Draw(3,0) [ctx 13] -> the bound triangle is rasterized into the bound RT with barycentric per-vertex color interp
5// (the WARP-exact affine interp). Output graded vs knowledge/warp_gradient.ppm. Real geometry pipeline (verts from a real
6// bound VB) w/ fixed-function coloring (programmable VS/PS = the nx_dxbc_exec-caller-scratch rung). Method bodies =
7// sovereign nx_cc NishiLang; vtables+glue = sovereign emitter. Graded by Microsoft <d3d11.h> slots + WARP pixels.
8// Input /tmp/nishi_export_lib.s Output knowledge/nishi_d3d11draw.dll license_tier: ORIGINAL expect_exit: 0
9import "nx_syscalls.nx"
10import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
11import "nxasm_x86.nx"
12import "nx_pe_writer.nx"
13
14const N_DEV: i64 = 43
15const N_CTX: i64 = 108
16const N_METH: i64 = 12
17
18func d11_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
19// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer
20// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the
21// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls).
22// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign.
23func d11_putn(v: i64) -> i64 { nxi_out(v); return 0 }
24func d11_wstr(buf: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){ buf[off+i]=s[i]; i=i+1 } buf[off+i]=0 as u8; return i+1 }
25
26func d11_assemble(src: *u8, n: i64, out: *u8, out_cap: i64, names: *i64, offs: *i64, cnt: i64) -> i64 {
27 let lab_off: *i64 = sys_mmap(ASM_MAX_LABELS * 8) as *i64
28 let lab_len: *i64 = sys_mmap(ASM_MAX_LABELS * 8) as *i64
29 let lab_addr: *i64 = sys_mmap(ASM_MAX_LABELS * 8) as *i64
30 let lab_sec: *i64 = sys_mmap(ASM_MAX_LABELS * 8) as *i64
31 let op0: *i64 = sys_mmap(72) as *i64 // 9 slots for SIB (matches nxasm_x86)
32 let op1: *i64 = sys_mmap(72) as *i64
33 let op2: *i64 = sys_mmap(72) as *i64 // API DRIFT FIX: axc_pass gained op2
34 let scratch: *u8 = sys_mmap(64)
35 let posbox: *i64 = sys_mmap(16) as *i64
36 let n_lab_box: *i64 = sys_mmap(16) as *i64
37 n_lab_box[0] = 0
38 let lh: *i64 = sys_mmap(ASM_LH_SIZE * 8) as *i64
39 let text_size: i64 = axc_pass(src, n, out, 0, 0, lab_off, lab_len, lab_addr, lab_sec, n_lab_box, lh, op0, op1, op2, scratch, posbox)
40 if text_size < 0 { return text_size }
41 let n_lab: i64 = n_lab_box[0]
42 var k: i64 = 0
43 while k < n_lab {
44 if lab_sec[k] == 1 { lab_addr[k] = lab_addr[k] + text_size }
45 var m: i64 = 0
46 while m < cnt { if axc_tok_is(src, lab_off[k], lab_len[k], names[m] as *u8) == 1 { offs[m] = lab_addr[k] } m = m + 1 }
47 k = k + 1
48 }
49 axc_lh_build(src, lab_off, lab_len, n_lab, lh)
50 let total: i64 = axc_pass(src, n, out, text_size, 1, lab_off, lab_len, lab_addr, lab_sec, n_lab_box, lh, op0, op1, op2, scratch, posbox)
51 if total < 0 { return total }
52 if total > out_cap { return 0 - 200 }
53 return total
54}
55
56func d11_thunk(buf: *u8, tco: i64, moff: i64) -> i64 {
57 let t: i64 = 0x200 + tco
58 _w8(buf,t+0,0x57); _w8(buf,t+1,0x56)
59 _w8(buf,t+2,0x48); _w8(buf,t+3,0x89); _w8(buf,t+4,0xCF)
60 _w8(buf,t+5,0x48); _w8(buf,t+6,0x89); _w8(buf,t+7,0xD6)
61 _w8(buf,t+8,0x4C); _w8(buf,t+9,0x89); _w8(buf,t+10,0xC2)
62 _w8(buf,t+11,0x4C); _w8(buf,t+12,0x89); _w8(buf,t+13,0xC9)
63 _w8(buf,t+14,0x48); _w8(buf,t+15,0x83); _w8(buf,t+16,0xEC); _w8(buf,t+17,0x08)
64 _w8(buf,t+18,0xE8); _w32(buf, t+19, moff - (tco + 23))
65 _w8(buf,t+23,0x48); _w8(buf,t+24,0x83); _w8(buf,t+25,0xC4); _w8(buf,t+26,0x08)
66 _w8(buf,t+27,0x5E); _w8(buf,t+28,0x5F); _w8(buf,t+29,0xC3)
67 return 0
68}
69
70// methods: 0=qi 1=addref 2=release 3=getfl 4=notimpl 5=getctx 6=create_rtv 7=clearrtv 8=create_buffer 9=om_set_rt 10=ia_set_vb 11=draw
71func d11_dev_slot(s: i64) -> i64 {
72 if s == 0 { return 0 }
73 if s == 1 { return 1 }
74 if s == 2 { return 2 }
75 if s == 3 { return 8 } // CreateBuffer
76 if s == 9 { return 6 } // CreateRenderTargetView
77 if s == 37 { return 3 } // GetFeatureLevel
78 if s == 40 { return 5 } // GetImmediateContext
79 return 4
80}
81func d11_ctx_slot(s: i64) -> i64 {
82 if s == 0 { return 0 }
83 if s == 1 { return 1 }
84 if s == 2 { return 2 }
85 if s == 13 { return 11 } // Draw
86 if s == 18 { return 10 } // IASetVertexBuffers
87 if s == 33 { return 9 } // OMSetRenderTargets
88 if s == 50 { return 7 } // ClearRenderTargetView
89 return 4
90}
91
92func d11_emit(buf: *u8, code: *u8, code_len: i64, offs: *i64) -> i64 {
93 if (buf as i64) == 0 { return 0 - NX_PE_BAD_INPUT }
94 let FF: i64 = code_len + N_METH * 30
95 let DV: i64 = ((FF + 41 + 7) / 8) * 8
96 let CV: i64 = DV + N_DEV * 8
97 let text_used: i64 = CV + N_CTX * 8
98 let text_pages: i64 = (text_used + 4095) / 4096
99 let text_raw: i64 = text_pages * 4096
100 let edata_rva: i64 = 0x1000 + text_raw
101 let edata_file: i64 = 0x200 + text_raw
102 let size_of_image: i64 = edata_rva + 0x1000
103 let file_size: i64 = edata_file + 0x200
104
105 _w16(buf, 0, 0x5A4D); _w32(buf, 0x3C, 0x80); _w32(buf, 0x80, 0x00004550)
106 _w16(buf, 0x84 + 0, PE_MACHINE_AMD64); _w16(buf, 0x84 + 2, 2); _w16(buf, 0x84 + 16, 0xF0); _w16(buf, 0x84 + 18, 0x2022)
107 let O: i64 = 0x98
108 _w16(buf, O + 0, PE_OH_MAGIC_PEPLUS); _w8(buf, O + 2, 1)
109 _w32(buf, O + 4, text_raw); _w32(buf, O + 8, 0x200)
110 _w32(buf, O + 16, 0); _w32(buf, O + 20, 0x1000)
111 _w64(buf, O + 24, 0x80000000, 0x1)
112 _w32(buf, O + 32, 0x1000); _w32(buf, O + 36, 0x200)
113 _w16(buf, O + 40, 6); _w16(buf, O + 48, 6)
114 _w32(buf, O + 56, size_of_image); _w32(buf, O + 60, 0x200)
115 _w16(buf, O + 68, PE_SUBSYSTEM_CONSOLE)
116 _w64(buf, O + 72, 0x100000, 0); _w64(buf, O + 80, 0x1000, 0); _w64(buf, O + 88, 0x100000, 0); _w64(buf, O + 96, 0x1000, 0)
117 _w32(buf, O + 108, 16)
118 _w32(buf, O + 112 + 0, edata_rva); _w32(buf, O + 112 + 4, 0x56)
119
120 _emit_section_header(buf, FOFF_SECT_TBL, 46, 116, 101, 120, 116, 0, 0, 0, text_used, 0x1000, text_raw, 0x200, PE_SECT_CODE_X_R)
121 _emit_section_header(buf, FOFF_SECT_TBL + 40, 46, 101, 100, 97, 116, 97, 0, 0, 0x56, edata_rva, 0x200, edata_file, PE_SECT_DATA_R)
122
123 var i: i64 = 0
124 while i < code_len { buf[0x200 + i] = code[i]; i = i + 1 }
125
126 var j: i64 = 0
127 while j < N_METH { d11_thunk(buf, code_len + j*30, offs[j]); j = j + 1 }
128
129 let f: i64 = 0x200 + FF
130 _w8(buf,f+0,0x48); _w8(buf,f+1,0x89); _w8(buf,f+2,0xC8)
131 _w8(buf,f+3,0x48); _w8(buf,f+4,0x8D); _w8(buf,f+5,0x15); _w32(buf, f+6, DV - FF - 10)
132 _w8(buf,f+10,0x48); _w8(buf,f+11,0x89); _w8(buf,f+12,0x11)
133 _w8(buf,f+13,0x48); _w8(buf,f+14,0xC7); _w8(buf,f+15,0x41); _w8(buf,f+16,0x08); _w32(buf, f+17, 1)
134 _w8(buf,f+21,0x48); _w8(buf,f+22,0x8D); _w8(buf,f+23,0x15); _w32(buf, f+24, CV - FF - 28)
135 _w8(buf,f+28,0x48); _w8(buf,f+29,0x89); _w8(buf,f+30,0x51); _w8(buf,f+31,0x10)
136 _w8(buf,f+32,0x48); _w8(buf,f+33,0xC7); _w8(buf,f+34,0x41); _w8(buf,f+35,0x18); _w32(buf, f+36, 1)
137 _w8(buf,f+40,0xC3)
138
139 var s: i64 = 0
140 while s < N_DEV { _w64(buf, 0x200 + DV + s*8, 0x80000000 + 0x1000 + code_len + d11_dev_slot(s)*30, 1); s = s + 1 }
141 var c: i64 = 0
142 while c < N_CTX { _w64(buf, 0x200 + CV + c*8, 0x80000000 + 0x1000 + code_len + d11_ctx_slot(c)*30, 1); c = c + 1 }
143
144 let E: i64 = edata_file
145 _w32(buf, E + 12, edata_rva + 0x4A)
146 _w32(buf, E + 16, 1); _w32(buf, E + 20, 1); _w32(buf, E + 24, 1)
147 _w32(buf, E + 28, edata_rva + 0x28); _w32(buf, E + 32, edata_rva + 0x2C); _w32(buf, E + 36, edata_rva + 0x30)
148 _w32(buf, E + 0x28, 0x1000 + FF)
149 _w32(buf, E + 0x2C, edata_rva + 0x32)
150 _w16(buf, E + 0x30, 0)
151 d11_wstr(buf, E + 0x32, "nishi_d3d_create_device\x00" as *u8)
152 d11_wstr(buf, E + 0x4A, "nishicc.dll\x00" as *u8)
153 return file_size
154}
155
156func main() -> i64 {
157 d11_puts("=== nx_pe_dll_d3d11draw -- device+context DLL with a real geometry Draw ===\n" as *u8)
158 let lenbox: *i64 = sys_mmap(16) as *i64
159 let src: *u8 = sys_read_file("/tmp/nishi_export_lib.s" as *u8, lenbox)
160 if (src as i64) == 0 { d11_puts("FAIL no /tmp/nishi_export_lib.s\n" as *u8); sys_exit(1); return 1 }
161 let n: i64 = lenbox[0]
162 if n <= 0 { d11_puts("FAIL empty .s\n" as *u8); sys_exit(2); return 2 }
163
164 let names: *i64 = sys_mmap(8*N_METH) as *i64
165 names[0] = "nx_dev_qi" as *u8 as i64
166 names[1] = "nx_dev_addref" as *u8 as i64
167 names[2] = "nx_dev_release" as *u8 as i64
168 names[3] = "nx_dev_getfl" as *u8 as i64
169 names[4] = "nx_dev_notimpl" as *u8 as i64
170 names[5] = "nx_dev_getctx" as *u8 as i64
171 names[6] = "nx_dev_create_rtv" as *u8 as i64
172 names[7] = "nx_ctx_clearrtv" as *u8 as i64
173 names[8] = "nx_dev_create_buffer" as *u8 as i64
174 names[9] = "nx_ctx_om_set_rt" as *u8 as i64
175 names[10] = "nx_ctx_ia_set_vb" as *u8 as i64
176 names[11] = "nx_ctx_draw" as *u8 as i64
177 let offs: *i64 = sys_mmap(8*N_METH) as *i64
178 var z: i64 = 0
179 while z < N_METH { offs[z] = 0 - 1; z = z + 1 }
180
181 let code: *u8 = sys_mmap(2097152)
182 let code_len: i64 = d11_assemble(src, n, code, 2097152, names, offs, N_METH)
183 if code_len < 0 { d11_puts("FAIL assemble rc="); d11_putn(code_len); d11_puts("\n" as *u8); sys_exit(3); return 3 }
184 var miss: i64 = 0
185 var q: i64 = 0
186 while q < N_METH { if offs[q] < 0 { d11_puts(" MISSING method "); d11_putn(q); d11_puts("\n" as *u8); miss = miss + 1 } q = q + 1 }
187 if miss > 0 { d11_puts("FAIL method(s) not found\n" as *u8); sys_exit(4); return 4 }
188 d11_puts(" code_len="); d11_putn(code_len); d11_puts(" factory@"); d11_putn(code_len + N_METH*30); d11_puts("\n" as *u8)
189
190 let buf: *u8 = sys_mmap(0x40000)
191 let fsz: i64 = d11_emit(buf, code, code_len, offs)
192 if fsz < 0 { d11_puts("FAIL emit rc="); d11_putn(fsz); d11_puts("\n" as *u8); sys_exit(5); return 5 }
193
194 let fd: i64 = sys_openat_wr("knowledge/nishi_d3d11draw.dll\x00" as *u8, 0x1a4)
195 if fd < 0 { d11_puts("FAIL open output\n" as *u8); sys_exit(6); return 6 }
196 let wrote: i64 = sys_write(fd, buf, fsz)
197 sys_close(fd)
198 d11_puts(" wrote "); d11_putn(wrote); d11_puts(" bytes -> knowledge/nishi_d3d11draw.dll (device 43 + context 108 vtables, 12 methods)\n" as *u8)
199
200 var fails: i64 = 0
201 if (buf[0] as i64) != 0x4D { fails=fails+1 }
202 if (buf[0x84+18] as i64) != 0x22 { fails=fails+1 }
203 if wrote != fsz { fails=fails+1 }
204 if fails == 0 { d11_puts("NX-PE-D3D11DRAW GREEN -- geometry-draw DLL; harness must Draw a triangle via real <d3d11.h>\n" as *u8); sys_exit(0); return 0 }
205 d11_puts("NX-PE-D3D11DRAW RED fails="); d11_putn(fails); d11_puts("\n" as *u8)
206 sys_exit(1)
207 return 1
208}