diff options
| author | Kam471 <115741199+Kam471@users.noreply.github.com> | 2026-11-21 18:05:29 +0000 |
|---|---|---|
| committer | Kam471 <115741199+Kam471@users.noreply.github.com> | 2026-11-21 18:26:19 +0000 |
| commit | 9015a62748565bd2af0622a66f7f55b2a3700f49 (patch) | |
| tree | da68093239ca845fba43d5c27d053232e0fd5ed3 /runtime/test/device_proxy_test.cpp | |
| parent | 793b06f12765571ed5e126d1d8944cec6a4dc805 (diff) | |
Runtime: Xbox-ABI device proxy (slots 9/10/62/76)
- Wrap the PC ID3D12Device in a copied-vtable proxy: per-slot thunks swap `this` to the PC device, with IUnknown refcounting/QueryInterface, per-slot hit counters and XS_TRACE=1 first-call logging.
- Override Xbox slot 62 to the PC CreateComputePipelineState (PC slot 62 is CreateStateObject) and Xbox slot 76 to CreateCommittedResource (PC slot 27). The 56-byte description prefix is copied and its 0x101/0x1 layout word replaced by ROW_MAJOR for buffers and UNKNOWN for textures.
- Slot 14 wraps the created descriptor heap. On the measured PC runtime the heap getters take the hidden return pointer in RDX, while the Xbox ABI expects the 8-byte handle in RAX; heap slots 9/10 convert between them. Device slots 9/10 stay CreateCommandAllocator/CreateGraphicsPipelineState, because the measured handle getters are ID3D12DescriptorHeap methods.
- Heap slot 7 GetDevice returns the device proxy, not the raw PC device. The heap holds a device-proxy reference, so GetDevice cannot dangle; the device proxy does not reference heap proxies, so there is no cycle.
- Add device_proxy_test (WARP, no game executable): shim-created proxy, QI/refcount, slot 14 heap, heap GetDevice/refcount, raw RAX handles vs the PC hidden-pointer result, slot 9/16/30/43 forwarding, slot 76 buffer and texture, slot 62 compute PSO, and counter checks. Register it with ctest.
- Wire both CMake and scripts/build_runtime.ps1.
Verified against Windows SDK 10.0.26100 d3d12.h (slot counts through ID3D12Device14; no Device15/16 exists there) and a live WARP device. Still unknown: PresentX, queue/fence/command-list extensions, Xbox vtable order beyond the measured slots, and struct returns other than the heap handles.
Clean-room: implemented from the measured ABI-facts spec; no americas-recomp or AnyPS5 source was read or copied.
Diffstat (limited to 'runtime/test/device_proxy_test.cpp')
| -rw-r--r-- | runtime/test/device_proxy_test.cpp | 358 |
1 files changed, 358 insertions, 0 deletions
diff --git a/runtime/test/device_proxy_test.cpp b/runtime/test/device_proxy_test.cpp new file mode 100644 index 000000000..b05254ab2 --- /dev/null +++ b/runtime/test/device_proxy_test.cpp | |||
| @@ -0,0 +1,358 @@ | |||
| 1 | // Exercises the Xbox-ABI device proxy against a real WARP PC device, with no game executable. | ||
| 2 | // | ||
| 3 | // It creates the proxy through the registered d3d12_xs!D3D12XboxCreateDevice shim, then calls the measured Xbox | ||
| 4 | // slots exactly as the guest would (raw function pointers, not the C++ interface): | ||
| 5 | // slot 14 CreateDescriptorHeap (returns a heap proxy) | ||
| 6 | // heap 9/10 GetCPU/GetGPUDescriptorHandleForHeapStart (Xbox: 8-byte handle in RAX; the PC heap uses the | ||
| 7 | // hidden return pointer in RDX, so slot 14 wraps it) | ||
| 8 | // slot 62 CreateComputePipelineState | ||
| 9 | // slot 76 resource creation with an Xbox layout word (0x101) | ||
| 10 | // Slot 9 on the device is CreateCommandAllocator on the PC vtable and must still forward, which proves the handle | ||
| 11 | // getters were not wedged into the device slots. Console output also shows the proxy's first-call slot log. | ||
| 12 | #include <windows.h> | ||
| 13 | #include <d3d12.h> | ||
| 14 | #include <d3dcompiler.h> | ||
| 15 | #include <dxgi1_4.h> | ||
| 16 | |||
| 17 | #include <cstdint> | ||
| 18 | #include <cstdio> | ||
| 19 | #include <cstring> | ||
| 20 | |||
| 21 | #include <wrl/client.h> | ||
| 22 | |||
| 23 | #include "d3d12_xs_device.h" | ||
| 24 | #include "shims.h" | ||
| 25 | |||
| 26 | namespace { | ||
| 27 | |||
| 28 | int g_failures = 0; | ||
| 29 | |||
| 30 | void check(bool condition, const char* what) { | ||
| 31 | std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); | ||
| 32 | std::fflush(stdout); | ||
| 33 | if (!condition) ++g_failures; | ||
| 34 | } | ||
| 35 | |||
| 36 | using Microsoft::WRL::ComPtr; | ||
| 37 | |||
| 38 | using XboxCreateDeviceFn = HRESULT(WINAPI*)(IUnknown*, const void*, const IID*, void**); | ||
| 39 | using XboxCreateResourceFn = HRESULT(WINAPI*)(void*, const D3D12_HEAP_PROPERTIES*, D3D12_HEAP_FLAGS, | ||
| 40 | const D3D12_RESOURCE_DESC*, D3D12_RESOURCE_STATES, | ||
| 41 | const D3D12_CLEAR_VALUE*, REFIID, void**); | ||
| 42 | using XboxCreateComputePsoFn = HRESULT(WINAPI*)(void*, const D3D12_COMPUTE_PIPELINE_STATE_DESC*, REFIID, void**); | ||
| 43 | using GetCpuHandleFn = D3D12_CPU_DESCRIPTOR_HANDLE(WINAPI*)(ID3D12DescriptorHeap*); | ||
| 44 | using GetGpuHandleFn = D3D12_GPU_DESCRIPTOR_HANDLE(WINAPI*)(ID3D12DescriptorHeap*); | ||
| 45 | |||
| 46 | D3D12_RESOURCE_DESC buffer_desc() { | ||
| 47 | D3D12_RESOURCE_DESC desc{}; | ||
| 48 | desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; | ||
| 49 | desc.Width = 4096; | ||
| 50 | desc.Height = 1; | ||
| 51 | desc.DepthOrArraySize = 1; | ||
| 52 | desc.MipLevels = 1; | ||
| 53 | desc.Format = DXGI_FORMAT_UNKNOWN; | ||
| 54 | desc.SampleDesc.Count = 1; | ||
| 55 | desc.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x101); // measured Xbox tiled/buffer word | ||
| 56 | desc.Flags = D3D12_RESOURCE_FLAG_NONE; | ||
| 57 | return desc; | ||
| 58 | } | ||
| 59 | |||
| 60 | D3D12_RESOURCE_DESC texture_desc() { | ||
| 61 | D3D12_RESOURCE_DESC desc{}; | ||
| 62 | desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 63 | desc.Width = 4; | ||
| 64 | desc.Height = 4; | ||
| 65 | desc.DepthOrArraySize = 1; | ||
| 66 | desc.MipLevels = 1; | ||
| 67 | desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 68 | desc.SampleDesc.Count = 1; | ||
| 69 | desc.Layout = static_cast<D3D12_TEXTURE_LAYOUT>(0x101); | ||
| 70 | desc.Flags = D3D12_RESOURCE_FLAG_NONE; | ||
| 71 | return desc; | ||
| 72 | } | ||
| 73 | |||
| 74 | D3D12_HEAP_PROPERTIES default_heap() { | ||
| 75 | D3D12_HEAP_PROPERTIES heap{}; | ||
| 76 | heap.Type = D3D12_HEAP_TYPE_DEFAULT; | ||
| 77 | heap.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; | ||
| 78 | heap.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; | ||
| 79 | heap.CreationNodeMask = 1; | ||
| 80 | heap.VisibleNodeMask = 1; | ||
| 81 | return heap; | ||
| 82 | } | ||
| 83 | |||
| 84 | bool load_compute_shader(ComPtr<ID3DBlob>& bytecode) { | ||
| 85 | HMODULE compiler = LoadLibraryExA("d3dcompiler_47.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); | ||
| 86 | check(compiler != nullptr, "d3dcompiler_47.dll loads"); | ||
| 87 | auto compile = compiler ? reinterpret_cast<pD3DCompile>(GetProcAddress(compiler, "D3DCompile")) : nullptr; | ||
| 88 | check(compile != nullptr, "d3dcompiler_47.dll exports D3DCompile"); | ||
| 89 | if (!compile) return false; | ||
| 90 | |||
| 91 | static const char kSource[] = | ||
| 92 | "[numthreads(1, 1, 1)]\n" | ||
| 93 | "void main(uint3 id : SV_DispatchThreadID) { }\n"; | ||
| 94 | ComPtr<ID3DBlob> errors; | ||
| 95 | HRESULT hr = compile(kSource, sizeof(kSource) - 1, "device_proxy_test.hlsl", nullptr, nullptr, "main", | ||
| 96 | "cs_5_0", D3DCOMPILE_ENABLE_STRICTNESS, 0, bytecode.GetAddressOf(), | ||
| 97 | errors.GetAddressOf()); | ||
| 98 | if (FAILED(hr) && errors) { | ||
| 99 | std::printf("shader compile error: %s\n", static_cast<const char*>(errors->GetBufferPointer())); | ||
| 100 | } | ||
| 101 | check(SUCCEEDED(hr) && bytecode, "trivial compute shader compiles (cs_5_0)"); | ||
| 102 | return SUCCEEDED(hr) && bytecode; | ||
| 103 | } | ||
| 104 | |||
| 105 | } // namespace | ||
| 106 | |||
| 107 | int main() { | ||
| 108 | std::setvbuf(stdout, nullptr, _IONBF, 0); | ||
| 109 | std::printf("device_proxy_test: Xbox-ABI device proxy over WARP\n"); | ||
| 110 | |||
| 111 | ComPtr<IDXGIFactory4> factory; | ||
| 112 | HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(factory.GetAddressOf())); | ||
| 113 | check(SUCCEEDED(hr) && factory, "DXGI factory created"); | ||
| 114 | ComPtr<IDXGIAdapter> warp; | ||
| 115 | if (factory) hr = factory->EnumWarpAdapter(IID_PPV_ARGS(warp.GetAddressOf())); | ||
| 116 | check(SUCCEEDED(hr) && warp, "WARP adapter enumerated"); | ||
| 117 | if (!warp) { | ||
| 118 | std::printf("device_proxy_test: FAILED (no WARP)\n"); | ||
| 119 | return 1; | ||
| 120 | } | ||
| 121 | |||
| 122 | auto create_device = reinterpret_cast<XboxCreateDeviceFn>( | ||
| 123 | rt::shims::find("d3d12_xs.dll", "D3D12XboxCreateDevice")); | ||
| 124 | check(create_device != nullptr, "shim registry exposes d3d12_xs!D3D12XboxCreateDevice"); | ||
| 125 | if (!create_device) return 1; | ||
| 126 | |||
| 127 | std::uint32_t params[24] = {}; // logged only; the measured layout is not decoded | ||
| 128 | const IID iid_device = __uuidof(ID3D12Device); | ||
| 129 | void* created = nullptr; | ||
| 130 | hr = create_device(warp.Get(), params, &iid_device, &created); | ||
| 131 | check(SUCCEEDED(hr) && created != nullptr, "D3D12XboxCreateDevice returns a device"); | ||
| 132 | if (!created) { | ||
| 133 | std::printf("device_proxy_test: FAILED (no device)\n"); | ||
| 134 | return 1; | ||
| 135 | } | ||
| 136 | |||
| 137 | ComPtr<ID3D12Device> device; | ||
| 138 | device.Attach(static_cast<ID3D12Device*>(created)); | ||
| 139 | check(rt::d3d12_xs::is_device_proxy(device.Get()), "returned pointer is an Xbox-ABI proxy"); | ||
| 140 | |||
| 141 | ID3D12Device* real = rt::d3d12_xs::device_proxy_real_device(device.Get()); | ||
| 142 | check(real != nullptr, "proxy exposes its PC device"); | ||
| 143 | unsigned slots = rt::d3d12_xs::device_proxy_slot_count(device.Get()); | ||
| 144 | std::printf("proxy forwards %u PC vtable slots (ID3D12Device..Device14 max is 83)\n", slots); | ||
| 145 | check(slots >= 44, "proxy forwards at least the base ID3D12Device vtable"); | ||
| 146 | check(!rt::d3d12_xs::device_slot_overridden(9), "device slot 9 is forwarded (PC CreateCommandAllocator)"); | ||
| 147 | check(!rt::d3d12_xs::device_slot_overridden(10), "device slot 10 is forwarded (PC CreateGraphicsPipelineState)"); | ||
| 148 | check(rt::d3d12_xs::device_slot_overridden(14), "device slot 14 is the descriptor-heap override"); | ||
| 149 | check(rt::d3d12_xs::device_slot_overridden(62), "device slot 62 is the compute-PSO override"); | ||
| 150 | check(rt::d3d12_xs::device_slot_overridden(76), "device slot 76 is the resource override"); | ||
| 151 | |||
| 152 | void** proxy_vtbl = *reinterpret_cast<void***>(device.Get()); | ||
| 153 | void** real_vtbl = real ? *reinterpret_cast<void***>(real) : nullptr; | ||
| 154 | check(proxy_vtbl != nullptr && proxy_vtbl[14] != real_vtbl[14], "slot 14 thunk differs from the PC entry"); | ||
| 155 | if (slots > 62) check(proxy_vtbl[62] != real_vtbl[62], "slot 62 thunk differs from PC CreateStateObject"); | ||
| 156 | if (slots > 76) check(proxy_vtbl[76] != real_vtbl[76], "slot 76 thunk differs from PC CreateCommittedResource3"); | ||
| 157 | |||
| 158 | rt::d3d12_xs::reset_device_proxy_stats(); | ||
| 159 | |||
| 160 | // QI/refcount behaviour: device interfaces resolve to the same proxy, non-device IIDs do not. | ||
| 161 | ID3D12Device* same = nullptr; | ||
| 162 | hr = device->QueryInterface(__uuidof(ID3D12Device), reinterpret_cast<void**>(&same)); | ||
| 163 | check(SUCCEEDED(hr) && same == device.Get(), "QueryInterface(IID_ID3D12Device) returns the same proxy"); | ||
| 164 | if (same) same->Release(); | ||
| 165 | ComPtr<ID3D12Device1> device1; | ||
| 166 | hr = device->QueryInterface(IID_PPV_ARGS(device1.GetAddressOf())); | ||
| 167 | check(SUCCEEDED(hr) && device1 != nullptr, "QueryInterface(IID_ID3D12Device1) succeeds"); | ||
| 168 | ComPtr<ID3D12CommandQueue> not_a_device; | ||
| 169 | hr = device->QueryInterface(IID_PPV_ARGS(not_a_device.GetAddressOf())); | ||
| 170 | check(FAILED(hr) && !not_a_device, "QueryInterface rejects a non-device IID"); | ||
| 171 | check(rt::d3d12_xs::device_slot_hits(0) >= 3, "slot 0 hit counter moves"); | ||
| 172 | |||
| 173 | // Generic-forwarder coverage: slot 43 returns an 8-byte struct through the hidden pointer, and slot 30 has | ||
| 174 | // two stack arguments. Both must reach the PC device unchanged (they are not measured overrides). | ||
| 175 | const LUID proxy_luid = device->GetAdapterLuid(); | ||
| 176 | const LUID real_luid = real ? real->GetAdapterLuid() : LUID{}; | ||
| 177 | check(proxy_luid.LowPart == real_luid.LowPart && proxy_luid.HighPart == real_luid.HighPart, | ||
| 178 | "slot 43 forwards GetAdapterLuid (struct return)"); | ||
| 179 | D3D12_RESOURCE_DESC reserved_desc = buffer_desc(); | ||
| 180 | reserved_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; | ||
| 181 | ComPtr<ID3D12Resource> reserved; | ||
| 182 | hr = device->CreateReservedResource(&reserved_desc, D3D12_RESOURCE_STATE_COMMON, nullptr, | ||
| 183 | IID_PPV_ARGS(reserved.GetAddressOf())); | ||
| 184 | check(SUCCEEDED(hr) && reserved != nullptr, "slot 30 forwards CreateReservedResource (stack arguments)"); | ||
| 185 | |||
| 186 | // Slot 14: descriptor heap through the proxy. The proxy wraps the real PC heap for the handle ABI. | ||
| 187 | D3D12_DESCRIPTOR_HEAP_DESC heap_desc{}; | ||
| 188 | heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; | ||
| 189 | heap_desc.NumDescriptors = 8; | ||
| 190 | heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; | ||
| 191 | ComPtr<ID3D12DescriptorHeap> heap; | ||
| 192 | const unsigned long refs_before_heap = rt::d3d12_xs::device_proxy_ref_count(device.Get()); | ||
| 193 | hr = device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(heap.GetAddressOf())); | ||
| 194 | check(SUCCEEDED(hr) && heap != nullptr, "slot 14 creates a descriptor heap"); | ||
| 195 | check(rt::d3d12_xs::device_slot_hits(14) == 1, "slot 14 hit counter is 1"); | ||
| 196 | check(heap && rt::d3d12_xs::device_proxy_ref_count(device.Get()) == refs_before_heap + 1, | ||
| 197 | "heap holds one device proxy reference"); | ||
| 198 | |||
| 199 | if (heap) { | ||
| 200 | check(rt::d3d12_xs::is_heap_proxy(heap.Get()), "slot 14 returns a heap proxy"); | ||
| 201 | ID3D12DescriptorHeap* real_heap = rt::d3d12_xs::heap_proxy_real_heap(heap.Get()); | ||
| 202 | check(real_heap != nullptr, "heap proxy exposes the PC heap"); | ||
| 203 | check(rt::d3d12_xs::heap_slot_overridden(9), "heap slot 9 is the CPU-handle override"); | ||
| 204 | check(rt::d3d12_xs::heap_slot_overridden(10), "heap slot 10 is the GPU-handle override"); | ||
| 205 | ID3D12DescriptorHeap* same_heap = nullptr; | ||
| 206 | hr = heap->QueryInterface(__uuidof(ID3D12DescriptorHeap), reinterpret_cast<void**>(&same_heap)); | ||
| 207 | check(SUCCEEDED(hr) && same_heap == heap.Get(), "heap QueryInterface returns the same heap proxy"); | ||
| 208 | if (same_heap) same_heap->Release(); | ||
| 209 | |||
| 210 | // ID3D12DeviceChild::GetDevice must return the Xbox-ABI device, not the raw PC device, and AddRef it. | ||
| 211 | check(rt::d3d12_xs::heap_slot_overridden(7), "heap slot 7 is the GetDevice override"); | ||
| 212 | ID3D12Device* heap_device = nullptr; | ||
| 213 | const unsigned long refs_before_get = rt::d3d12_xs::device_proxy_ref_count(device.Get()); | ||
| 214 | hr = heap->GetDevice(__uuidof(ID3D12Device), reinterpret_cast<void**>(&heap_device)); | ||
| 215 | check(SUCCEEDED(hr) && heap_device == device.Get() && rt::d3d12_xs::is_device_proxy(heap_device), | ||
| 216 | "heap GetDevice returns the device proxy"); | ||
| 217 | check(rt::d3d12_xs::device_proxy_ref_count(device.Get()) == refs_before_get + 1, | ||
| 218 | "GetDevice AddRefs the device proxy"); | ||
| 219 | check(rt::d3d12_xs::heap_slot_hits(7) == 1, "heap slot 7 hit counter is 1"); | ||
| 220 | if (heap_device) heap_device->Release(); | ||
| 221 | check(rt::d3d12_xs::device_proxy_ref_count(device.Get()) == refs_before_get, | ||
| 222 | "GetDevice release balances"); | ||
| 223 | |||
| 224 | // The Xbox call shape: `this` only, 8-byte handle in RAX. The proxy converts from the PC heap's | ||
| 225 | // hidden return pointer (RDX). A PC C++ call through the proxy uses the PC convention, so it is not | ||
| 226 | // used here; the real heap below shows that convention and the conversion is compared against it. | ||
| 227 | void** proxy_heap_vtbl = *reinterpret_cast<void***>(heap.Get()); | ||
| 228 | auto get_cpu = reinterpret_cast<GetCpuHandleFn>(proxy_heap_vtbl[9]); | ||
| 229 | auto get_gpu = reinterpret_cast<GetGpuHandleFn>(proxy_heap_vtbl[10]); | ||
| 230 | D3D12_CPU_DESCRIPTOR_HANDLE cpu = get_cpu(heap.Get()); | ||
| 231 | D3D12_GPU_DESCRIPTOR_HANDLE gpu = get_gpu(heap.Get()); | ||
| 232 | check(cpu.ptr != 0, "heap proxy slot 9 returns a CPU handle in RAX"); | ||
| 233 | check(gpu.ptr != 0, "heap proxy slot 10 returns a GPU handle in RAX"); | ||
| 234 | if (real_heap) { | ||
| 235 | void** real_heap_vtbl = *reinterpret_cast<void***>(real_heap); | ||
| 236 | check(proxy_heap_vtbl[9] != real_heap_vtbl[9], "heap slot 9 thunk differs from the PC heap entry"); | ||
| 237 | check(proxy_heap_vtbl[10] != real_heap_vtbl[10], "heap slot 10 thunk differs from the PC heap entry"); | ||
| 238 | } | ||
| 239 | |||
| 240 | // PC reality check: the real heap takes a hidden return pointer in RDX. This is the deviation that | ||
| 241 | // makes the wrapper necessary; a one-argument call on the real heap is invalid and is not made. | ||
| 242 | if (real_heap) { | ||
| 243 | void** real_heap_vtbl = *reinterpret_cast<void***>(real_heap); | ||
| 244 | using GetCpuOutFn = void(WINAPI*)(ID3D12DescriptorHeap*, D3D12_CPU_DESCRIPTOR_HANDLE*); | ||
| 245 | using GetGpuOutFn = void(WINAPI*)(ID3D12DescriptorHeap*, D3D12_GPU_DESCRIPTOR_HANDLE*); | ||
| 246 | constexpr SIZE_T kSentinel = 0x1111111111111111ull; | ||
| 247 | D3D12_CPU_DESCRIPTOR_HANDLE cpu_pc{}; | ||
| 248 | D3D12_GPU_DESCRIPTOR_HANDLE gpu_pc{}; | ||
| 249 | cpu_pc.ptr = kSentinel; | ||
| 250 | gpu_pc.ptr = kSentinel; | ||
| 251 | reinterpret_cast<GetCpuOutFn>(real_heap_vtbl[9])(real_heap, &cpu_pc); | ||
| 252 | reinterpret_cast<GetGpuOutFn>(real_heap_vtbl[10])(real_heap, &gpu_pc); | ||
| 253 | check(cpu_pc.ptr != kSentinel, "PC heap slot 9 writes through the RDX return pointer"); | ||
| 254 | check(gpu_pc.ptr != kSentinel, "PC heap slot 10 writes through the RDX return pointer"); | ||
| 255 | check(cpu_pc.ptr == cpu.ptr, "proxy CPU handle equals the PC heap's hidden-pointer result"); | ||
| 256 | check(gpu_pc.ptr == gpu.ptr, "proxy GPU handle equals the PC heap's hidden-pointer result"); | ||
| 257 | } | ||
| 258 | |||
| 259 | const D3D12_DESCRIPTOR_HEAP_DESC got_desc = heap->GetDesc(); | ||
| 260 | check(got_desc.NumDescriptors == heap_desc.NumDescriptors, "heap slot 8 forwards GetDesc"); | ||
| 261 | check(rt::d3d12_xs::heap_slot_hits(9) >= 1, "heap slot 9 hit counter moves"); | ||
| 262 | check(rt::d3d12_xs::heap_slot_hits(10) >= 1, "heap slot 10 hit counter moves"); | ||
| 263 | check(rt::d3d12_xs::heap_slot_first_call_seen(9), "heap slot 9 first-call flag is set"); | ||
| 264 | } | ||
| 265 | |||
| 266 | // Slot 9 on the device must remain CreateCommandAllocator, not a handle getter. | ||
| 267 | ComPtr<ID3D12CommandAllocator> allocator; | ||
| 268 | hr = device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(allocator.GetAddressOf())); | ||
| 269 | check(SUCCEEDED(hr) && allocator != nullptr, "device slot 9 forwards CreateCommandAllocator"); | ||
| 270 | check(rt::d3d12_xs::device_slot_hits(9) == 1, "slot 9 hit counter is 1"); | ||
| 271 | |||
| 272 | // Slot 76: buffer resource with the measured Xbox layout word 0x101; PC committed resources take ROW_MAJOR. | ||
| 273 | const IID iid_resource = __uuidof(ID3D12Resource); | ||
| 274 | auto create_resource = reinterpret_cast<XboxCreateResourceFn>(proxy_vtbl[76]); | ||
| 275 | const D3D12_HEAP_PROPERTIES heap_properties = default_heap(); | ||
| 276 | D3D12_RESOURCE_DESC xbox_buffer = buffer_desc(); | ||
| 277 | ComPtr<ID3D12Resource> buffer; | ||
| 278 | hr = create_resource(device.Get(), &heap_properties, D3D12_HEAP_FLAG_NONE, &xbox_buffer, | ||
| 279 | D3D12_RESOURCE_STATE_COMMON, nullptr, iid_resource, | ||
| 280 | reinterpret_cast<void**>(buffer.GetAddressOf())); | ||
| 281 | check(SUCCEEDED(hr) && buffer != nullptr, "slot 76 creates a committed buffer"); | ||
| 282 | if (buffer) { | ||
| 283 | const D3D12_RESOURCE_DESC got = buffer->GetDesc(); | ||
| 284 | check(got.Layout == D3D12_TEXTURE_LAYOUT_ROW_MAJOR, "slot 76 maps 0x101 to ROW_MAJOR for a buffer"); | ||
| 285 | check(got.Width == 4096, "slot 76 preserves the resource width"); | ||
| 286 | } | ||
| 287 | |||
| 288 | // The same Xbox word on a texture maps to UNKNOWN, which is what PC committed textures require. | ||
| 289 | D3D12_RESOURCE_DESC xbox_texture = texture_desc(); | ||
| 290 | ComPtr<ID3D12Resource> texture; | ||
| 291 | hr = create_resource(device.Get(), &heap_properties, D3D12_HEAP_FLAG_NONE, &xbox_texture, | ||
| 292 | D3D12_RESOURCE_STATE_COPY_DEST, nullptr, iid_resource, | ||
| 293 | reinterpret_cast<void**>(texture.GetAddressOf())); | ||
| 294 | check(SUCCEEDED(hr) && texture != nullptr, "slot 76 creates a committed texture"); | ||
| 295 | if (texture) { | ||
| 296 | check(texture->GetDesc().Layout == D3D12_TEXTURE_LAYOUT_UNKNOWN, | ||
| 297 | "slot 76 maps 0x101 to UNKNOWN for a texture"); | ||
| 298 | } | ||
| 299 | check(rt::d3d12_xs::device_slot_hits(76) == 2, "slot 76 hit counter is 2"); | ||
| 300 | check(rt::d3d12_xs::device_slot_first_call_seen(76), "slot 76 first-call flag is set"); | ||
| 301 | |||
| 302 | // Slot 62: an empty root signature and a trivial compute shader through the Xbox signature. The PC entry at | ||
| 303 | // slot 62 is CreateStateObject, so success here proves the override reached CreateComputePipelineState. | ||
| 304 | D3D12_ROOT_SIGNATURE_DESC root_desc{}; | ||
| 305 | root_desc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE; | ||
| 306 | ComPtr<ID3DBlob> root_blob; | ||
| 307 | ComPtr<ID3DBlob> root_error; | ||
| 308 | hr = D3D12SerializeRootSignature(&root_desc, D3D_ROOT_SIGNATURE_VERSION_1_0, root_blob.GetAddressOf(), | ||
| 309 | root_error.GetAddressOf()); | ||
| 310 | check(SUCCEEDED(hr) && root_blob != nullptr, "empty root signature serializes"); | ||
| 311 | ComPtr<ID3D12RootSignature> root_signature; | ||
| 312 | if (root_blob) { | ||
| 313 | hr = device->CreateRootSignature(0, root_blob->GetBufferPointer(), root_blob->GetBufferSize(), | ||
| 314 | IID_PPV_ARGS(root_signature.GetAddressOf())); | ||
| 315 | } | ||
| 316 | check(SUCCEEDED(hr) && root_signature != nullptr, "device slot 16 forwards CreateRootSignature"); | ||
| 317 | |||
| 318 | ComPtr<ID3DBlob> bytecode; | ||
| 319 | if (load_compute_shader(bytecode) && root_signature) { | ||
| 320 | D3D12_COMPUTE_PIPELINE_STATE_DESC pso_desc{}; | ||
| 321 | pso_desc.pRootSignature = root_signature.Get(); | ||
| 322 | pso_desc.CS.pShaderBytecode = bytecode->GetBufferPointer(); | ||
| 323 | pso_desc.CS.BytecodeLength = bytecode->GetBufferSize(); | ||
| 324 | pso_desc.NodeMask = 0; | ||
| 325 | pso_desc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE; | ||
| 326 | |||
| 327 | auto create_pso = reinterpret_cast<XboxCreateComputePsoFn>(proxy_vtbl[62]); | ||
| 328 | const IID iid_pso = __uuidof(ID3D12PipelineState); | ||
| 329 | ComPtr<ID3D12PipelineState> pso; | ||
| 330 | hr = create_pso(device.Get(), &pso_desc, iid_pso, reinterpret_cast<void**>(pso.GetAddressOf())); | ||
| 331 | check(SUCCEEDED(hr) && pso != nullptr, "slot 62 creates a compute PSO via CreateComputePipelineState"); | ||
| 332 | check(rt::d3d12_xs::device_slot_hits(62) == 1, "slot 62 hit counter is 1"); | ||
| 333 | check(rt::d3d12_xs::device_slot_first_call_seen(62), "slot 62 first-call flag is set"); | ||
| 334 | } | ||
| 335 | |||
| 336 | // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. | ||
| 337 | if (real) real->AddRef(); | ||
| 338 | // Drop every device child before the proxy so the PC device is not released first. | ||
| 339 | root_signature.Reset(); | ||
| 340 | reserved.Reset(); | ||
| 341 | texture.Reset(); | ||
| 342 | buffer.Reset(); | ||
| 343 | allocator.Reset(); | ||
| 344 | device1.Reset(); | ||
| 345 | const unsigned long refs_before_heap_release = rt::d3d12_xs::device_proxy_ref_count(device.Get()); | ||
| 346 | heap.Reset(); | ||
| 347 | check(rt::d3d12_xs::device_proxy_ref_count(device.Get()) + 1 == refs_before_heap_release, | ||
| 348 | "heap release drops its device reference"); | ||
| 349 | device.Reset(); | ||
| 350 | if (real) { | ||
| 351 | check(real->GetNodeCount() > 0, "PC device survives the proxy's final Release"); | ||
| 352 | real->Release(); | ||
| 353 | } | ||
| 354 | |||
| 355 | std::printf("device_proxy_test: %s (%d failure%s)\n", g_failures ? "FAILED" : "OK", g_failures, | ||
| 356 | g_failures == 1 ? "" : "s"); | ||
| 357 | return g_failures ? 1 : 0; | ||
| 358 | } | ||