upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/device_proxy_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/device_proxy_test.cpp')
-rw-r--r--runtime/test/device_proxy_test.cpp407
1 files changed, 396 insertions, 11 deletions
diff --git a/runtime/test/device_proxy_test.cpp b/runtime/test/device_proxy_test.cpp
index 08b34d8c9..5fae4621c 100644
--- a/runtime/test/device_proxy_test.cpp
+++ b/runtime/test/device_proxy_test.cpp
@@ -11,6 +11,7 @@
11// slot 8/36 CreateCommandQueue/CreateFence (return queue and fence proxies that run GPU work) 11// slot 8/36 CreateCommandQueue/CreateFence (return queue and fence proxies that run GPU work)
12// Slot 9 on the device is CreateCommandAllocator on the PC vtable and must still forward, which proves the handle 12// Slot 9 on the device is CreateCommandAllocator on the PC vtable and must still forward, which proves the handle
13// getters were not wedged into the device slots. Console output also shows the proxy's first-call slot log. 13// getters were not wedged into the device slots. Console output also shows the proxy's first-call slot log.
14#include <vector>
14#include <windows.h> 15#include <windows.h>
15#include <d3d12.h> 16#include <d3d12.h>
16#include <d3dcompiler.h> 17#include <d3dcompiler.h>
@@ -26,8 +27,13 @@
26#include "d3d12_trace.h" 27#include "d3d12_trace.h"
27#include "d3d12_xs_desc_clean.h" 28#include "d3d12_xs_desc_clean.h"
28#include "d3d12_xs_device.h" 29#include "d3d12_xs_device.h"
30#include "host_wine.h"
29#include "shims.h" 31#include "shims.h"
30#include "../src/frame_pacer.h" 32#include "../src/frame_pacer.h"
33#include "../src/shader_library.h"
34
35#include <filesystem>
36#include <fstream>
31 37
32namespace { 38namespace {
33 39
@@ -110,6 +116,190 @@ bool load_compute_shader(ComPtr<ID3DBlob>& bytecode) {
110 return SUCCEEDED(hr) && bytecode; 116 return SUCCEEDED(hr) && bytecode;
111} 117}
112 118
119// ---- Slot 55 (CreateDerivedPipelineState) ----
120
121constexpr std::size_t kRecordBytes = 0x14c;
122
123// One record: u32 tag, then the payload at +4 (d3d12_xs_pso_stream.h).
124void put_record(unsigned char* record, std::uint32_t tag, const void* payload, std::size_t size) {
125 std::memset(record, 0, kRecordBytes);
126 std::memcpy(record, &tag, sizeof(tag));
127 if (size) std::memcpy(record + 4, payload, size);
128}
129
130std::size_t align_up(std::size_t value, std::size_t align) { return (value + align - 1) / align * align; }
131
132// A stream written in the console's layout: u32 type, payload at its alignment, next entry 8-aligned.
133struct StreamBytes {
134 std::vector<unsigned char> bytes;
135 void add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type, const void* payload, std::size_t size, std::size_t align) {
136 const std::size_t at = bytes.size();
137 bytes.resize(at + 4);
138 const std::uint32_t t = type;
139 std::memcpy(bytes.data() + at, &t, sizeof(t));
140 bytes.resize(align_up(at + 4, align), 0);
141 const auto* p = static_cast<const unsigned char*>(payload);
142 bytes.insert(bytes.end(), p, p + size);
143 bytes.resize(align_up(bytes.size(), 8), 0);
144 }
145};
146
147bool compile_hlsl(const char* source, const char* target, ComPtr<ID3DBlob>& bytecode) {
148 HMODULE compiler = LoadLibraryExA("d3dcompiler_47.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
149 auto compile = compiler ? reinterpret_cast<pD3DCompile>(GetProcAddress(compiler, "D3DCompile")) : nullptr;
150 if (!compile) return false;
151 ComPtr<ID3DBlob> errors;
152 const HRESULT hr = compile(source, std::strlen(source), "device_proxy_slot55.hlsl", nullptr, nullptr, "main",
153 target, D3DCOMPILE_ENABLE_STRICTNESS, 0, bytecode.GetAddressOf(),
154 errors.GetAddressOf());
155 if (FAILED(hr) && errors) std::printf("shader compile error: %s\n", static_cast<const char*>(errors->GetBufferPointer()));
156 return SUCCEEDED(hr) && bytecode;
157}
158
159// Graphics stream with every state a derived record can patch: DEPTH_STENCIL1, RASTERIZER, BLEND, SAMPLE_MASK.
160std::vector<unsigned char> graphics_stream(ID3D12RootSignature* root, ID3DBlob* vs, ID3DBlob* ps) {
161 StreamBytes s;
162 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE, &root, sizeof(root), alignof(void*));
163 const D3D12_SHADER_BYTECODE vs_code{vs->GetBufferPointer(), vs->GetBufferSize()};
164 const D3D12_SHADER_BYTECODE ps_code{ps->GetBufferPointer(), ps->GetBufferSize()};
165 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_VS, &vs_code, sizeof(vs_code), alignof(void*));
166 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PS, &ps_code, sizeof(ps_code), alignof(void*));
167 D3D12_RT_FORMAT_ARRAY rt{};
168 rt.RTFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
169 rt.NumRenderTargets = 1;
170 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RENDER_TARGET_FORMATS, &rt, sizeof(rt), 4);
171 const DXGI_FORMAT dsv = DXGI_FORMAT_UNKNOWN;
172 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT, &dsv, sizeof(dsv), 4);
173 const DXGI_SAMPLE_DESC sample{1, 0};
174 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_DESC, &sample, sizeof(sample), 4);
175 const D3D12_PRIMITIVE_TOPOLOGY_TYPE topology = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
176 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_PRIMITIVE_TOPOLOGY, &topology, sizeof(topology), 4);
177
178 D3D12_BLEND_DESC blend{};
179 for (auto& rtb : blend.RenderTarget) {
180 rtb.SrcBlend = rtb.SrcBlendAlpha = D3D12_BLEND_ONE;
181 rtb.DestBlend = rtb.DestBlendAlpha = D3D12_BLEND_ZERO;
182 rtb.BlendOp = rtb.BlendOpAlpha = D3D12_BLEND_OP_ADD;
183 rtb.LogicOp = D3D12_LOGIC_OP_NOOP;
184 rtb.RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
185 }
186 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_BLEND, &blend, sizeof(blend), 4);
187 const UINT mask = 0xffffffffu;
188 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_SAMPLE_MASK, &mask, sizeof(mask), 4);
189 D3D12_RASTERIZER_DESC raster{};
190 raster.FillMode = D3D12_FILL_MODE_SOLID;
191 raster.CullMode = D3D12_CULL_MODE_BACK;
192 raster.DepthClipEnable = TRUE;
193 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_RASTERIZER, &raster, sizeof(raster), 4);
194 D3D12_DEPTH_STENCIL_DESC1 ds{};
195 ds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
196 ds.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
197 ds.StencilReadMask = ds.StencilWriteMask = 0xff;
198 for (D3D12_DEPTH_STENCILOP_DESC* op : {&ds.FrontFace, &ds.BackFace}) {
199 op->StencilFailOp = op->StencilDepthFailOp = op->StencilPassOp = D3D12_STENCIL_OP_KEEP;
200 op->StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
201 }
202 s.add(D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1, &ds, sizeof(ds), 4);
203 return s.bytes;
204}
205
206void check_derived_states(ID3D12Device* device, void** proxy_vtbl, ID3D12RootSignature* root,
207 ID3D12PipelineState* compute_base, ID3D12PipelineState* native_base) {
208 using DeriveFn = HRESULT(WINAPI*)(void*, void*, std::uint64_t, const void*, const IID*, void**);
209 auto derive = reinterpret_cast<DeriveFn>(proxy_vtbl[55]);
210 const IID iid = __uuidof(ID3D12PipelineState);
211 unsigned char rec_tag5[kRecordBytes], rec_raster[kRecordBytes];
212 unsigned char five[5 * kRecordBytes] = {};
213 put_record(rec_tag5, 5, nullptr, 0);
214 D3D12_RASTERIZER_DESC raster{};
215 raster.FillMode = D3D12_FILL_MODE_WIREFRAME;
216 raster.CullMode = D3D12_CULL_MODE_BACK;
217 raster.DepthClipEnable = TRUE;
218 put_record(rec_raster, 1, &raster, sizeof(raster));
219 ComPtr<ID3D12PipelineState> out;
220
221 // Refusals: a compute base has no graphics subobjects, a native compute PSO was never kept, and the arguments.
222 out.Reset();
223 check(derive(device, compute_base, 1, rec_tag5, &iid, reinterpret_cast<void**>(out.GetAddressOf())) == E_INVALIDARG
224 && !out, "slot 55 refuses tag 5 (E_INVALIDARG, null output)");
225 out.Reset();
226 check(derive(device, compute_base, 1, rec_raster, &iid, reinterpret_cast<void**>(out.GetAddressOf())) == E_INVALIDARG
227 && !out, "slot 55 refuses a rasterizer record on a base without RASTERIZER (E_INVALIDARG)");
228 out.Reset();
229 check(derive(device, native_base, 1, rec_raster, &iid, reinterpret_cast<void**>(out.GetAddressOf())) == E_FAIL
230 && !out, "slot 55 refuses a base PSO that was never kept (null output, no derivation)");
231 out.Reset();
232 check(derive(device, compute_base, 1, nullptr, &iid, reinterpret_cast<void**>(out.GetAddressOf())) == E_POINTER,
233 "slot 55 with null records is E_POINTER");
234 out.Reset();
235 check(derive(device, compute_base, 0, rec_raster, &iid, reinterpret_cast<void**>(out.GetAddressOf())) == E_INVALIDARG,
236 "slot 55 with n = 0 is E_INVALIDARG");
237 check(derive(device, compute_base, 5, five, &iid, reinterpret_cast<void**>(out.GetAddressOf())) == E_INVALIDARG,
238 "slot 55 with n = 5 is E_INVALIDARG");
239 check(derive(device, compute_base, 1, rec_raster, nullptr, reinterpret_cast<void**>(out.GetAddressOf())) == E_POINTER,
240 "slot 55 with null riid is E_POINTER");
241
242 // Positive: a graphics base through slot 86, then tags 0, 1 and 3 in one call, tag 2 alone, and tag 2 again on
243 // the derived PSO (a derived PSO is a base too).
244 ComPtr<ID3DBlob> vs, ps;
245 if (!compile_hlsl("float4 main() : SV_Position { return float4(0, 0, 0, 1); }", "vs_5_0", vs) ||
246 !compile_hlsl("float4 main() : SV_Target { return float4(0, 0, 0, 1); }", "ps_5_0", ps)) {
247 check(false, "graphics shaders compile for the slot 55 positive test");
248 return;
249 }
250 const std::vector<unsigned char> stream = graphics_stream(root, vs.Get(), ps.Get());
251 D3D12_PIPELINE_STATE_STREAM_DESC stream_desc{stream.size(), const_cast<unsigned char*>(stream.data())};
252 using CreateStreamFn = HRESULT(WINAPI*)(void*, const D3D12_PIPELINE_STATE_STREAM_DESC*, const IID*, void**);
253 ComPtr<ID3D12PipelineState> base;
254 HRESULT hr = reinterpret_cast<CreateStreamFn>(proxy_vtbl[86])(device, &stream_desc, &iid,
255 reinterpret_cast<void**>(base.GetAddressOf()));
256 check(SUCCEEDED(hr) && base, "slot 86 creates the graphics base PSO (WARP accepts the stream)");
257 if (!base) return;
258
259 D3D12_DEPTH_STENCIL_DESC ds{};
260 ds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
261 ds.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
262 ds.StencilReadMask = ds.StencilWriteMask = 0xff;
263 for (D3D12_DEPTH_STENCILOP_DESC* op : {&ds.FrontFace, &ds.BackFace}) {
264 op->StencilFailOp = op->StencilDepthFailOp = op->StencilPassOp = D3D12_STENCIL_OP_KEEP;
265 op->StencilFunc = D3D12_COMPARISON_FUNC_ALWAYS;
266 }
267 unsigned char recs[3 * kRecordBytes];
268 put_record(recs, 0, &ds, sizeof(ds));
269 put_record(recs + kRecordBytes, 1, &raster, sizeof(raster));
270 const UINT sample_mask = 0x1;
271 put_record(recs + 2 * kRecordBytes, 3, &sample_mask, sizeof(sample_mask));
272 ComPtr<ID3D12PipelineState> three;
273 hr = derive(device, base.Get(), 3, recs, &iid, reinterpret_cast<void**>(three.GetAddressOf()));
274 check(SUCCEEDED(hr) && three, "slot 55 derives a PSO from tags 0, 1 and 3 (n = 3) on the graphics base");
275
276 D3D12_BLEND_DESC blend{};
277 for (auto& rtb : blend.RenderTarget) {
278 rtb.SrcBlend = rtb.SrcBlendAlpha = D3D12_BLEND_ONE;
279 rtb.DestBlend = rtb.DestBlendAlpha = D3D12_BLEND_ZERO;
280 rtb.BlendOp = rtb.BlendOpAlpha = D3D12_BLEND_OP_ADD;
281 rtb.LogicOp = D3D12_LOGIC_OP_NOOP;
282 rtb.RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
283 }
284 // Four records (raster, depth-stencil, sample mask, blend): the largest count the console builds.
285 unsigned char four_recs[4 * kRecordBytes];
286 std::memcpy(four_recs, recs, 3 * kRecordBytes);
287 put_record(four_recs + 3 * kRecordBytes, 2, &blend, sizeof(blend));
288 ComPtr<ID3D12PipelineState> quad;
289 hr = derive(device, base.Get(), 4, four_recs, &iid, reinterpret_cast<void**>(quad.GetAddressOf()));
290 check(SUCCEEDED(hr) && quad, "slot 55 derives a PSO from four records (n = 4)");
291
292 unsigned char rec_blend[kRecordBytes];
293 put_record(rec_blend, 2, &blend, sizeof(blend));
294 ComPtr<ID3D12PipelineState> blended;
295 hr = derive(device, base.Get(), 1, rec_blend, &iid, reinterpret_cast<void**>(blended.GetAddressOf()));
296 check(SUCCEEDED(hr) && blended, "slot 55 derives a PSO from tag 2 (blend), n = 1");
297
298 ComPtr<ID3D12PipelineState> chained;
299 hr = derive(device, three.Get(), 1, rec_blend, &iid, reinterpret_cast<void**>(chained.GetAddressOf()));
300 check(SUCCEEDED(hr) && chained, "slot 55 derives from a derived PSO (its stream is kept too)");
301}
302
113} // namespace 303} // namespace
114 304
115int main(int argc, char** argv) { 305int main(int argc, char** argv) {
@@ -206,12 +396,25 @@ int main(int argc, char** argv) {
206 const LUID real_luid = real ? real->GetAdapterLuid() : LUID{}; 396 const LUID real_luid = real ? real->GetAdapterLuid() : LUID{};
207 check(proxy_luid.LowPart == real_luid.LowPart && proxy_luid.HighPart == real_luid.HighPart, 397 check(proxy_luid.LowPart == real_luid.LowPart && proxy_luid.HighPart == real_luid.HighPart,
208 "slot 43 forwards GetAdapterLuid (struct return)"); 398 "slot 43 forwards GetAdapterLuid (struct return)");
209 // Slot 25 returns a struct wider than 8 bytes. The console title calls it with the C ABI: the hidden 399 // Slots 25 and 26 return a struct wider than 8 bytes. The console title calls them with the C ABI: the hidden
210 // return pointer first (RCX), then the device (RDX), then the arguments, and the buffer comes back in RAX 400 // return pointer first (RCX), then the device (RDX), then the arguments, and the buffer comes back in RAX
211 // (measured for slot 26 on the retail executable). Call slot 25 that way through a raw function pointer and compare 401 // (measured for slot 26 on the retail executable). Call them that way through raw function pointers and compare
212 // with the PC device's own answer. 402 // with the PC device's own answers.
213 check(rt::d3d12_xs::device_slot_overridden(25), "device slot 25 is the C-ABI GetResourceAllocationInfo override"); 403 check(rt::d3d12_xs::device_slot_overridden(25), "device slot 25 is the C-ABI GetResourceAllocationInfo override");
404 check(rt::d3d12_xs::device_slot_overridden(26), "device slot 26 is the C-ABI GetCustomHeapProperties override");
214 if (real) { 405 if (real) {
406 using HeapPropsFn = D3D12_HEAP_PROPERTIES*(WINAPI*)(D3D12_HEAP_PROPERTIES*, void*, UINT, D3D12_HEAP_TYPE);
407 auto get_heap_props = reinterpret_cast<HeapPropsFn>(proxy_vtbl[26]);
408 for (D3D12_HEAP_TYPE type : {D3D12_HEAP_TYPE_DEFAULT, D3D12_HEAP_TYPE_UPLOAD, D3D12_HEAP_TYPE_READBACK}) {
409 D3D12_HEAP_PROPERTIES got;
410 std::memset(&got, 0xcd, sizeof(got));
411 D3D12_HEAP_PROPERTIES* returned = get_heap_props(&got, device.Get(), 1, type);
412 const D3D12_HEAP_PROPERTIES want = real->GetCustomHeapProperties(1, type);
413 check(returned == &got, "slot 26 returns the caller's buffer in RAX");
414 check(std::memcmp(&got, &want, sizeof(got)) == 0,
415 "slot 26 (C ABI) writes the PC device's GetCustomHeapProperties into the caller's buffer");
416 }
417
215 using AllocInfoFn = D3D12_RESOURCE_ALLOCATION_INFO*(WINAPI*)(D3D12_RESOURCE_ALLOCATION_INFO*, void*, UINT, UINT, 418 using AllocInfoFn = D3D12_RESOURCE_ALLOCATION_INFO*(WINAPI*)(D3D12_RESOURCE_ALLOCATION_INFO*, void*, UINT, UINT,
216 const D3D12_RESOURCE_DESC*); 419 const D3D12_RESOURCE_DESC*);
217 auto get_alloc_info = reinterpret_cast<AllocInfoFn>(proxy_vtbl[25]); 420 auto get_alloc_info = reinterpret_cast<AllocInfoFn>(proxy_vtbl[25]);
@@ -227,8 +430,11 @@ int main(int argc, char** argv) {
227 check(got.SizeInBytes == want.SizeInBytes && got.Alignment == want.Alignment, 430 check(got.SizeInBytes == want.SizeInBytes && got.Alignment == want.Alignment,
228 "slot 25 (C ABI) writes the PC device's GetResourceAllocationInfo into the caller's buffer"); 431 "slot 25 (C ABI) writes the PC device's GetResourceAllocationInfo into the caller's buffer");
229 432
230 // The runtime's own code calls the proxy through d3d12.h (PC form: this, then the return buffer). Slot 25 433 // The runtime's own code calls the proxy through d3d12.h (PC form: this, then the return buffer). Both
231 // still answers that form. 434 // slots still answer that form.
435 const D3D12_HEAP_PROPERTIES pc_props = device->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD);
436 const D3D12_HEAP_PROPERTIES pc_want = real->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD);
437 check(std::memcmp(&pc_props, &pc_want, sizeof(pc_props)) == 0, "slot 26 still answers the PC (C++) form");
232 const D3D12_RESOURCE_ALLOCATION_INFO pc_info = device->GetResourceAllocationInfo(0, 1, &desc); 438 const D3D12_RESOURCE_ALLOCATION_INFO pc_info = device->GetResourceAllocationInfo(0, 1, &desc);
233 check(pc_info.SizeInBytes == want.SizeInBytes && pc_info.Alignment == want.Alignment, 439 check(pc_info.SizeInBytes == want.SizeInBytes && pc_info.Alignment == want.Alignment,
234 "slot 25 still answers the PC (C++) form"); 440 "slot 25 still answers the PC (C++) form");
@@ -248,8 +454,11 @@ int main(int argc, char** argv) {
248 ds_uav.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; 454 ds_uav.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
249 D3D12_RESOURCE_DESC pc_pair = ds_uav; 455 D3D12_RESOURCE_DESC pc_pair = ds_uav;
250 pc_pair.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; 456 pc_pair.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
251 check(real->GetResourceAllocationInfo(0, 1, &pc_pair).SizeInBytes == UINT64_MAX, 457 // Microsoft's runtime validates the flag pair; Wine's vkd3d sizes the texture without looking at it, so the
252 "the PC device refuses a depth-stencil texture with unordered access (negative control)"); 458 // control only says something about the first.
459 if (!hostwine::host_is_wine())
460 check(real->GetResourceAllocationInfo(0, 1, &pc_pair).SizeInBytes == UINT64_MAX,
461 "the PC device refuses a depth-stencil texture with unordered access (negative control)");
253 D3D12_RESOURCE_DESC ds_only = pc_pair; 462 D3D12_RESOURCE_DESC ds_only = pc_pair;
254 ds_only.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; 463 ds_only.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
255 const D3D12_RESOURCE_ALLOCATION_INFO ds_want = real->GetResourceAllocationInfo(0, 1, &ds_only); 464 const D3D12_RESOURCE_ALLOCATION_INFO ds_want = real->GetResourceAllocationInfo(0, 1, &ds_only);
@@ -267,11 +476,37 @@ int main(int argc, char** argv) {
267 "pc_resource_desc keeps unordered access on buffers and color textures"); 476 "pc_resource_desc keeps unordered access on buffers and color textures");
268 D3D12_RESOURCE_ALLOCATION_INFO ds_got{~0ull, ~0ull}; 477 D3D12_RESOURCE_ALLOCATION_INFO ds_got{~0ull, ~0ull};
269 get_alloc_info(&ds_got, device.Get(), 0, 1, &ds_uav); 478 get_alloc_info(&ds_got, device.Get(), 0, 1, &ds_uav);
270 check(ds_got.SizeInBytes != UINT64_MAX && ds_got.SizeInBytes >= ds_want.SizeInBytes && 479 // Sized as the console lays it out (12eb84f75: a depth plane and a stencil plane in 64 KiB tiles), which can
271 (ds_got.SizeInBytes >> 32) == 0, 480 // be less than the PC answer for the depth-stencil texture alone, so only "sized, and in 32 bits" is checked.
481 check(ds_got.SizeInBytes != UINT64_MAX && ds_got.SizeInBytes != 0 && (ds_got.SizeInBytes >> 32) == 0,
272 "slot 25 (C ABI) sizes a console depth-stencil texture with unordered access"); 482 "slot 25 (C ABI) sizes a console depth-stencil texture with unordered access");
273 } 483 }
274 484
485 constexpr auto kUav = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
486 constexpr auto kDepthStencil = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
487 constexpr auto kRenderTarget = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
488 const struct {
489 DXGI_FORMAT format;
490 D3D12_RESOURCE_FLAGS input;
491 D3D12_RESOURCE_FLAGS expected;
492 const char* message;
493 } flag_cases[] = {
494 {DXGI_FORMAT_B5G5R5A1_UNORM, kRenderTarget | kUav, kRenderTarget, "B5G5R5A1 keeps only RT"},
495 {DXGI_FORMAT_R32G8X24_TYPELESS, kDepthStencil | kUav, kDepthStencil, "R32G8X24 depth keeps only DS"},
496 {DXGI_FORMAT_R8G8B8A8_UNORM, kRenderTarget | kUav, kRenderTarget | kUav, "RGBA8 keeps RT and UAV"},
497 {DXGI_FORMAT_R32_FLOAT, kUav, kUav, "R32_FLOAT keeps UAV"},
498 {DXGI_FORMAT_R32_TYPELESS, kUav, kUav, "R32_TYPELESS without DS keeps UAV"},
499 {DXGI_FORMAT_D16_UNORM, kDepthStencil | kUav, kDepthStencil, "D16 depth keeps only DS"},
500 };
501 for (const auto& test : flag_cases) {
502 D3D12_RESOURCE_DESC desc{};
503 desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
504 desc.Format = test.format;
505 desc.Flags = test.input;
506 const auto clean = rt::d3d12_xs::pc_resource_desc(desc);
507 check(clean.Flags == test.expected && clean.Format == test.format && desc.Flags == test.input, test.message);
508 }
509
275 D3D12_RESOURCE_DESC reserved_desc = buffer_desc(); 510 D3D12_RESOURCE_DESC reserved_desc = buffer_desc();
276 reserved_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; 511 reserved_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
277 ComPtr<ID3D12Resource> reserved; 512 ComPtr<ID3D12Resource> reserved;
@@ -287,6 +522,7 @@ int main(int argc, char** argv) {
287 D3D12_HEAP_PROPERTIES*(WINAPI*)(D3D12_HEAP_PROPERTIES*, void*, UINT, D3D12_HEAP_TYPE); 522 D3D12_HEAP_PROPERTIES*(WINAPI*)(D3D12_HEAP_PROPERTIES*, void*, UINT, D3D12_HEAP_TYPE);
288 auto get_custom_heap = reinterpret_cast<XboxGetCustomHeapPropertiesFn>(proxy_vtbl[26]); 523 auto get_custom_heap = reinterpret_cast<XboxGetCustomHeapPropertiesFn>(proxy_vtbl[26]);
289 const D3D12_HEAP_TYPE heap_types[] = {D3D12_HEAP_TYPE_DEFAULT, D3D12_HEAP_TYPE_UPLOAD, D3D12_HEAP_TYPE_READBACK}; 524 const D3D12_HEAP_TYPE heap_types[] = {D3D12_HEAP_TYPE_DEFAULT, D3D12_HEAP_TYPE_UPLOAD, D3D12_HEAP_TYPE_READBACK};
525 const auto slot26_hits_before = rt::d3d12_xs::device_slot_hits(26);
290 bool console_returns_buffer = true, console_matches_pc = real != nullptr, console_writes_only_struct = true; 526 bool console_returns_buffer = true, console_matches_pc = real != nullptr, console_writes_only_struct = true;
291 for (const D3D12_HEAP_TYPE type : heap_types) { 527 for (const D3D12_HEAP_TYPE type : heap_types) {
292 struct Padded { 528 struct Padded {
@@ -304,15 +540,18 @@ int main(int argc, char** argv) {
304 check(console_returns_buffer, "slot 26 console form returns the buffer's address"); 540 check(console_returns_buffer, "slot 26 console form returns the buffer's address");
305 check(console_matches_pc, "slot 26 console form writes the PC device's DEFAULT, UPLOAD and READBACK properties"); 541 check(console_matches_pc, "slot 26 console form writes the PC device's DEFAULT, UPLOAD and READBACK properties");
306 check(console_writes_only_struct, "slot 26 console form writes nothing past the struct"); 542 check(console_writes_only_struct, "slot 26 console form writes nothing past the struct");
307 check(rt::d3d12_xs::device_slot_hits(26) == 3, "slot 26 hit counter is 3"); 543 check(rt::d3d12_xs::device_slot_hits(26) == slot26_hits_before + 3,
544 "slot 26 hit counter increases by three for the console calls");
308 check(rt::d3d12_xs::is_device_proxy(device.Get()) && rt::d3d12_xs::device_proxy_real_device(device.Get()) == real, 545 check(rt::d3d12_xs::is_device_proxy(device.Get()) && rt::d3d12_xs::device_proxy_real_device(device.Get()) == real,
309 "the proxy is intact after the console-form calls"); 546 "the proxy is intact after the console-form calls");
310 // A PC C++ call through the proxy (the proxy in RCX, the buffer in RDX) is still forwarded. 547 // A PC C++ call through the proxy (the proxy in RCX, the buffer in RDX) is still forwarded.
311 const D3D12_HEAP_PROPERTIES pc_form = device->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD); 548 const D3D12_HEAP_PROPERTIES pc_form = device->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD);
312 const D3D12_HEAP_PROPERTIES pc_want = real ? real->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD) 549 const D3D12_HEAP_PROPERTIES pc_want = real ? real->GetCustomHeapProperties(1, D3D12_HEAP_TYPE_UPLOAD)
313 : D3D12_HEAP_PROPERTIES{}; 550 : D3D12_HEAP_PROPERTIES{};
314 check(real && std::memcmp(&pc_form, &pc_want, sizeof(pc_want)) == 0 && rt::d3d12_xs::device_slot_hits(26) == 4, 551 check(real && std::memcmp(&pc_form, &pc_want, sizeof(pc_want)) == 0,
315 "slot 26 PC form still forwards GetCustomHeapProperties"); 552 "slot 26 PC form still forwards GetCustomHeapProperties");
553 check(rt::d3d12_xs::device_slot_hits(26) == slot26_hits_before + 4,
554 "slot 26 hit counter also includes the PC call");
316 555
317 // Slot 14: descriptor heap through the proxy. The proxy wraps the real PC heap for the handle ABI. 556 // Slot 14: descriptor heap through the proxy. The proxy wraps the real PC heap for the handle ABI.
318 D3D12_DESCRIPTOR_HEAP_DESC heap_desc{}; 557 D3D12_DESCRIPTOR_HEAP_DESC heap_desc{};
@@ -590,6 +829,7 @@ int main(int argc, char** argv) {
590 "slot 86 creates a PSO via PC ID3D12Device2::CreatePipelineState (slot 47)"); 829 "slot 86 creates a PSO via PC ID3D12Device2::CreatePipelineState (slot 47)");
591 check(rt::d3d12_xs::device_slot_hits(86) == 1, "slot 86 hit counter is 1"); 830 check(rt::d3d12_xs::device_slot_hits(86) == 1, "slot 86 hit counter is 1");
592 check(rt::d3d12_xs::device_slot_first_call_seen(86), "slot 86 first-call flag is set"); 831 check(rt::d3d12_xs::device_slot_first_call_seen(86), "slot 86 first-call flag is set");
832 check_derived_states(device.Get(), proxy_vtbl, root_signature.Get(), pso86.Get(), pso67.Get());
593 } 833 }
594 } 834 }
595 835
@@ -967,6 +1207,151 @@ int main(int argc, char** argv) {
967 } 1207 }
968 1208
969 // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. 1209 // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own.
1210 // Slot 57 (DeserializeGraphicsPipelineStateX):
1211 // 1. Guard-page test on the measured 188-byte (0xbc) pipeline blob from PSO #1 (record 8916), whose third element
1212 // ("TEXCOORD" at 0x9b, fields at 0xa4) has its 28-byte block truncated by 4 bytes at 0xbc. Placing the 188-byte
1213 // blob at the very end of a committed page right before a PAGE_NOACCESS guard page proves the parser decodes all
1214 // 3 elements (POSITION R32G32B32_FLOAT @0, COLOR R8G8B8A8_UNORM @12, TEXCOORD R32G32_FLOAT @16) without reading
1215 // past `blob_size`.
1216 // 2. ShaderLibrary `fspx_gfx/index.json` loading and separate `fspx_size()`, `fspx_gfx_size()`, `bvh_size()` counters.
1217 {
1218 auto put_u32 = [](std::uint8_t* dst, std::size_t off, std::uint32_t v) {
1219 std::memcpy(dst + off, &v, 4);
1220 };
1221 std::uint8_t blob188[0xbc]{};
1222 put_u32(blob188, 0x00, 0xea6f6e56u); // PSO 1 magic/checksum
1223 put_u32(blob188, 0x04, 0x00001000u); // constant
1224 put_u32(blob188, 0x08, 0u);
1225 put_u32(blob188, 0x0c, 1u);
1226 put_u32(blob188, 0x10, 0x0000001cu); // 28 bytes (7 u32) per element
1227 put_u32(blob188, 0x1c, 0xffffffffu);
1228 put_u32(blob188, 0x24, 0x00130100u);
1229 put_u32(blob188, 0x28, 1u);
1230 put_u32(blob188, 0x38, 0x00000070u); // 4 + 3*28 + (9+6+9) = 112
1231 put_u32(blob188, 0x3c, 0x0000000cu);
1232 put_u32(blob188, 0x4c, 3u); // 3 input elements
1233 put_u32(blob188, 0x50, 9u); // max name stride including NUL
1234
1235 // Element 0 @ 0x54: "POSITION\0" (9 B) + 28 B fields @ 0x5d..0x79
1236 std::memcpy(blob188 + 0x54, "POSITION", 9);
1237 put_u32(blob188, 0x5d + 0x00, 0u); // InputSlot 0
1238 put_u32(blob188, 0x5d + 0x04, static_cast<std::uint32_t>(DXGI_FORMAT_R32G32B32_FLOAT)); // 6
1239 put_u32(blob188, 0x5d + 0x08, 0u); // SemanticIndex 0
1240 put_u32(blob188, 0x5d + 0x0c, 0u); // AlignedByteOffset 0
1241 put_u32(blob188, 0x5d + 0x18, 6u);
1242
1243 // Element 1 @ 0x79: "COLOR\0" (6 B) + 28 B fields @ 0x7f..0x9b
1244 std::memcpy(blob188 + 0x79, "COLOR", 6);
1245 put_u32(blob188, 0x7f + 0x00, 0u);
1246 put_u32(blob188, 0x7f + 0x04, static_cast<std::uint32_t>(DXGI_FORMAT_R8G8B8A8_UNORM)); // 28
1247 put_u32(blob188, 0x7f + 0x08, 0u);
1248 put_u32(blob188, 0x7f + 0x0c, 12u);
1249 put_u32(blob188, 0x7f + 0x18, 9u);
1250
1251 // Element 2 @ 0x9b: "TEXCOORD\0" (9 B) + 24 B fields @ 0xa4..0xbc (truncated by 4 B at 0xbc!)
1252 std::memcpy(blob188 + 0x9b, "TEXCOORD", 9);
1253 put_u32(blob188, 0xa4 + 0x00, 0u);
1254 put_u32(blob188, 0xa4 + 0x04, static_cast<std::uint32_t>(DXGI_FORMAT_R32G32_FLOAT)); // 16
1255 put_u32(blob188, 0xa4 + 0x08, 0u);
1256 put_u32(blob188, 0xa4 + 0x0c, 16u);
1257
1258 SYSTEM_INFO si{};
1259 GetSystemInfo(&si);
1260 const std::size_t page = si.dwPageSize ? si.dwPageSize : 4096;
1261 auto* pages = static_cast<std::uint8_t*>(
1262 VirtualAlloc(nullptr, page * 2, MEM_RESERVE, PAGE_NOACCESS));
1263 check(pages != nullptr, "slot 57 pipeline blob guard pages reserved");
1264 if (pages) {
1265 void* committed = VirtualAlloc(pages, page, MEM_COMMIT, PAGE_READWRITE);
1266 check(committed == pages, "slot 57 pipeline blob first page committed before guard page");
1267 std::uint8_t* guarded_blob = pages + page - sizeof(blob188);
1268 std::memcpy(guarded_blob, blob188, sizeof(blob188));
1269 std::vector<rt::d3d12_xs::XboxPipelineInputElement> elems;
1270 const bool ok = rt::d3d12_xs::parse_xbox_pipeline_blob_input_layout(
1271 guarded_blob, sizeof(blob188), elems);
1272 check(ok && elems.size() == 3,
1273 "slot 57 pipeline blob parser decodes 3 elements from truncated 188-byte blob adjacent to PAGE_NOACCESS");
1274 if (elems.size() == 3) {
1275 check(elems[0].semantic_name == "POSITION" && elems[0].format == DXGI_FORMAT_R32G32B32_FLOAT &&
1276 elems[0].aligned_byte_offset == 0 && elems[0].input_slot == 0,
1277 "slot 57 pipeline blob element 0 is POSITION R32G32B32_FLOAT @0");
1278 check(elems[1].semantic_name == "COLOR" && elems[1].format == DXGI_FORMAT_R8G8B8A8_UNORM &&
1279 elems[1].aligned_byte_offset == 12 && elems[1].input_slot == 0,
1280 "slot 57 pipeline blob element 1 is COLOR R8G8B8A8_UNORM @12");
1281 check(elems[2].semantic_name == "TEXCOORD" && elems[2].format == DXGI_FORMAT_R32G32_FLOAT &&
1282 elems[2].aligned_byte_offset == 16 && elems[2].input_slot == 0,
1283 "slot 57 pipeline blob element 2 is TEXCOORD R32G32_FLOAT @16");
1284 }
1285 VirtualFree(pages, 0, MEM_RELEASE);
1286 }
1287
1288 // Slot 57 null-argument checks on the device proxy.
1289 void** vtable = *reinterpret_cast<void***>(device.Get());
1290 using Slot57Fn = HRESULT(WINAPI*)(void*, const void*, const IID*, void**);
1291 const IID iid_pso = __uuidof(ID3D12PipelineState);
1292 void* pso_out = reinterpret_cast<void*>(0x1111);
1293 hr = reinterpret_cast<Slot57Fn>(vtable[57])(device.Get(), nullptr, &iid_pso, &pso_out);
1294 check(FAILED(hr) && pso_out == nullptr, "slot 57 nulls its output for a null description");
1295
1296 // Synthetic test for ShaderLibrary loading `fspx_gfx/index.json` alongside `fspx/index.json` and `bvh/index.json`
1297 // while keeping `fspx_size()`, `fspx_gfx_size()`, and `bvh_size()` independent.
1298 wchar_t temp_root[MAX_PATH]{};
1299 if (GetTempPathW(MAX_PATH, temp_root)) {
1300 const std::filesystem::path tmp_dir =
1301 std::filesystem::path(temp_root) / ("leonida_fspx_gfx_test_" + std::to_string(GetCurrentProcessId()));
1302 std::error_code ec;
1303 std::filesystem::create_directories(tmp_dir / "fspx", ec);
1304 std::filesystem::create_directories(tmp_dir / "fspx_gfx", ec);
1305 std::filesystem::create_directories(tmp_dir / "bvh", ec);
1306 {
1307 std::ofstream(tmp_dir / "index.json") << "{\n}\n";
1308 std::ofstream(tmp_dir / "fspx" / "index.json")
1309 << "{\n"
1310 << " \"libcs_00001\": {\n"
1311 << " \"stage\": \"cs\",\n"
1312 << " \"fspx_code_sha256\": \"1111111111111111111111111111111111111111111111111111111111111111\"\n"
1313 << " }\n"
1314 << "}\n";
1315 std::ofstream(tmp_dir / "fspx_gfx" / "index.json")
1316 << "{\n"
1317 << " \"3333333333333333333333333333333333333333333333333333333333333333\": {\n"
1318 << " \"stage\": \"ps\",\n"
1319 << " \"spv\": \"3333333333333333_libps_00001.spv\",\n"
1320 << " \"fspx_code_sha256\": \"3333333333333333333333333333333333333333333333333333333333333333\"\n"
1321 << " },\n"
1322 << " \"4444444444444444444444444444444444444444444444444444444444444444\": {\n"
1323 << " \"stage\": \"vs\",\n"
1324 << " \"spv\": \"4444444444444444_libvs_00002.spv\",\n"
1325 << " \"fspx_code_sha256\": \"4444444444444444444444444444444444444444444444444444444444444444\"\n"
1326 << " }\n"
1327 << "}\n";
1328 std::ofstream(tmp_dir / "bvh" / "index.json")
1329 << "{\n"
1330 << " \"bvh_00001\": {\n"
1331 << " \"stage\": \"cs\",\n"
1332 << " \"fspx_code_sha256\": \"2222222222222222222222222222222222222222222222222222222222222222\"\n"
1333 << " }\n"
1334 << "}\n";
1335 }
1336 leonida::ShaderLibrary lib;
1337 std::string err;
1338 const bool loaded = lib.load(tmp_dir, &err);
1339 check(loaded, "ShaderLibrary::load loads index.json + fspx + fspx_gfx + bvh");
1340 check(lib.fspx_size() == 1, "ShaderLibrary::fspx_size() counts only fspx/ entries");
1341 check(lib.fspx_gfx_size() == 2, "ShaderLibrary::fspx_gfx_size() counts only fspx_gfx/ entries");
1342 check(lib.bvh_size() == 1, "ShaderLibrary::bvh_size() counts only bvh/ entries");
1343 const auto* ps_entry =
1344 lib.find_fspx_by_code("3333333333333333333333333333333333333333333333333333333333333333");
1345 const auto* vs_entry =
1346 lib.find_fspx_by_code("4444444444444444444444444444444444444444444444444444444444444444");
1347 check(ps_entry && ps_entry->stage == "ps" && ps_entry->spv_file == "fspx_gfx/3333333333333333_libps_00001.spv",
1348 "fspx_gfx PS entry preserves stage and its spv filename, under the folder prefix");
1349 check(vs_entry && vs_entry->stage == "vs" && vs_entry->spv_file == "fspx_gfx/4444444444444444_libvs_00002.spv",
1350 "fspx_gfx VS entry preserves stage and its spv filename, under the folder prefix");
1351 std::filesystem::remove_all(tmp_dir, ec);
1352 }
1353 }
1354
970 if (real) real->AddRef(); 1355 if (real) real->AddRef();
971 // Drop every device child before the proxy so the PC device is not released first. 1356 // Drop every device child before the proxy so the PC device is not released first.
972 root_signature.Reset(); 1357 root_signature.Reset();