diff options
| author | Khuraie <83483964+Khuraie@users.noreply.github.com> | 2026-11-23 23:42:39 +0800 |
|---|---|---|
| committer | Khuraie <83483964+Khuraie@users.noreply.github.com> | 2026-11-23 23:42:40 +0800 |
| commit | 355201c51e6aba95c582e2df344782b22e97bb31 (patch) | |
| tree | 880ae4d0fcf306644eb79229f20eabb2d475c996 /runtime/test/device_proxy_test.cpp | |
| parent | 52c906dc827f787f417ca56d3af29a53e8610497 (diff) | |
d3d12_xs: honour __XMemAllocationHooks__ for descriptor heaps and translate CPU handles
Diffstat (limited to 'runtime/test/device_proxy_test.cpp')
| -rw-r--r-- | runtime/test/device_proxy_test.cpp | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/runtime/test/device_proxy_test.cpp b/runtime/test/device_proxy_test.cpp index d5a6ec04e..2651893af 100644 --- a/runtime/test/device_proxy_test.cpp +++ b/runtime/test/device_proxy_test.cpp | |||
| @@ -442,6 +442,71 @@ int main(int argc, char** argv) { | |||
| 442 | } | 442 | } |
| 443 | } | 443 | } |
| 444 | 444 | ||
| 445 | // Title-exported __XMemAllocationHooks__ backing for CreateDescriptorHeap (slot 14) + CPU handle translation | ||
| 446 | // against the real WARP D3D12 runtime. | ||
| 447 | { | ||
| 448 | struct HookState { | ||
| 449 | alignas(64) std::uint8_t backing[4096]{}; | ||
| 450 | std::size_t alloc_size = 0; | ||
| 451 | std::uint64_t alloc_attr = 0; | ||
| 452 | void* freed_ptr = nullptr; | ||
| 453 | std::uint64_t freed_attr = 0; | ||
| 454 | int alloc_count = 0; | ||
| 455 | int free_count = 0; | ||
| 456 | }; | ||
| 457 | static HookState hook_state; | ||
| 458 | hook_state = {}; | ||
| 459 | struct HookCallbacks { | ||
| 460 | static void* __stdcall alloc(std::size_t size, std::uint64_t attr) { | ||
| 461 | ++hook_state.alloc_count; | ||
| 462 | hook_state.alloc_size = size; | ||
| 463 | hook_state.alloc_attr = attr; | ||
| 464 | if ((attr & 0x3FFFull) == 4 && size <= sizeof(hook_state.backing)) return hook_state.backing; | ||
| 465 | return nullptr; | ||
| 466 | } | ||
| 467 | static void __stdcall free(void* ptr, std::uint64_t attr) { | ||
| 468 | ++hook_state.free_count; | ||
| 469 | hook_state.freed_ptr = ptr; | ||
| 470 | hook_state.freed_attr = attr; | ||
| 471 | } | ||
| 472 | }; | ||
| 473 | const rt::shims::XMemAllocationHooks title_hooks{&HookCallbacks::alloc, &HookCallbacks::free}; | ||
| 474 | rt::shims::add_xmem_allocation_hooks(&title_hooks, &title_hooks); | ||
| 475 | |||
| 476 | D3D12_DESCRIPTOR_HEAP_DESC hooked_desc{}; | ||
| 477 | hooked_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; | ||
| 478 | hooked_desc.NumDescriptors = 4; | ||
| 479 | hooked_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; | ||
| 480 | const UINT inc = device->GetDescriptorHandleIncrementSize(hooked_desc.Type); | ||
| 481 | ComPtr<ID3D12DescriptorHeap> hooked_heap; | ||
| 482 | hr = device->CreateDescriptorHeap(&hooked_desc, IID_PPV_ARGS(hooked_heap.GetAddressOf())); | ||
| 483 | check(SUCCEEDED(hr) && hooked_heap != nullptr, "hooked slot 14 creates a descriptor heap"); | ||
| 484 | check(hook_state.alloc_count == 1 && hook_state.alloc_size == hooked_desc.NumDescriptors * inc && | ||
| 485 | hook_state.alloc_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes, | ||
| 486 | "slot 14 invokes __XMemAllocationHooks__ with heap byte size and D3D descriptor-heap attributes"); | ||
| 487 | if (hooked_heap) { | ||
| 488 | void** hooked_vtbl = *reinterpret_cast<void***>(hooked_heap.Get()); | ||
| 489 | const auto hooked_cpu = reinterpret_cast<GetCpuHandleFn>(hooked_vtbl[9])(hooked_heap.Get()); | ||
| 490 | const auto hooked_gpu = reinterpret_cast<GetGpuHandleFn>(hooked_vtbl[10])(hooked_heap.Get()); | ||
| 491 | const auto expected_base = reinterpret_cast<SIZE_T>(hook_state.backing); | ||
| 492 | check(hooked_cpu.ptr == expected_base, | ||
| 493 | "hooked heap slot 9 returns the title-supplied CPU descriptor base"); | ||
| 494 | check(hooked_gpu.ptr != 0, "hooked heap slot 10 returns a valid GPU descriptor start"); | ||
| 495 | |||
| 496 | // Create a CBV at slot 1 of the title-backed heap; WARP validates the translated real handle. | ||
| 497 | D3D12_CONSTANT_BUFFER_VIEW_DESC cbv{}; | ||
| 498 | cbv.BufferLocation = 0; | ||
| 499 | cbv.SizeInBytes = 256; | ||
| 500 | device->CreateConstantBufferView(&cbv, D3D12_CPU_DESCRIPTOR_HANDLE{expected_base + inc}); | ||
| 501 | check(true, "CreateConstantBufferView translates a title-backed CPU descriptor handle on WARP"); | ||
| 502 | } | ||
| 503 | hooked_heap.Reset(); | ||
| 504 | check(hook_state.free_count == 1 && hook_state.freed_ptr == hook_state.backing && | ||
| 505 | hook_state.freed_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes, | ||
| 506 | "releasing the hooked heap frees its backing through __XMemAllocationHooks__"); | ||
| 507 | rt::shims::remove_xmem_allocation_hooks(&title_hooks); | ||
| 508 | } | ||
| 509 | |||
| 445 | // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. | 510 | // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. |
| 446 | if (real) real->AddRef(); | 511 | if (real) real->AddRef(); |
| 447 | // Drop every device child before the proxy so the PC device is not released first. | 512 | // Drop every device child before the proxy so the PC device is not released first. |