diff options
| author | Khuraie <83483964+Khuraie@users.noreply.github.com> | 2026-11-23 23:42:39 +0800 |
|---|---|---|
| committer | Mumogg <104529023+Mumogg@users.noreply.github.com> | 2026-11-24 03:47:22 -0600 |
| commit | 522faa2ee4ad77b00d11057543497f9e31c3ff05 (patch) | |
| tree | 548eda8cf594a5889b7cf861f8820acd5e6356b0 /runtime/test/device_proxy_test.cpp | |
| parent | d7576d56ae2c77564c84ffae724ffe9bdd2e6701 (diff) | |
d3d12_xs: honour __XMemAllocationHooks__ for descriptor heaps and translate CPU handles
(cherry picked from commit 355201c51e6aba95c582e2df344782b22e97bb31)
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 b3b3f0fc3..6b91a7a1f 100644 --- a/runtime/test/device_proxy_test.cpp +++ b/runtime/test/device_proxy_test.cpp | |||
| @@ -577,6 +577,71 @@ int main(int argc, char** argv) { | |||
| 577 | } | 577 | } |
| 578 | } | 578 | } |
| 579 | 579 | ||
| 580 | // Title-exported __XMemAllocationHooks__ backing for CreateDescriptorHeap (slot 14) + CPU handle translation | ||
| 581 | // against the real WARP D3D12 runtime. | ||
| 582 | { | ||
| 583 | struct HookState { | ||
| 584 | alignas(64) std::uint8_t backing[4096]{}; | ||
| 585 | std::size_t alloc_size = 0; | ||
| 586 | std::uint64_t alloc_attr = 0; | ||
| 587 | void* freed_ptr = nullptr; | ||
| 588 | std::uint64_t freed_attr = 0; | ||
| 589 | int alloc_count = 0; | ||
| 590 | int free_count = 0; | ||
| 591 | }; | ||
| 592 | static HookState hook_state; | ||
| 593 | hook_state = {}; | ||
| 594 | struct HookCallbacks { | ||
| 595 | static void* __stdcall alloc(std::size_t size, std::uint64_t attr) { | ||
| 596 | ++hook_state.alloc_count; | ||
| 597 | hook_state.alloc_size = size; | ||
| 598 | hook_state.alloc_attr = attr; | ||
| 599 | if ((attr & 0x3FFFull) == 4 && size <= sizeof(hook_state.backing)) return hook_state.backing; | ||
| 600 | return nullptr; | ||
| 601 | } | ||
| 602 | static void __stdcall free(void* ptr, std::uint64_t attr) { | ||
| 603 | ++hook_state.free_count; | ||
| 604 | hook_state.freed_ptr = ptr; | ||
| 605 | hook_state.freed_attr = attr; | ||
| 606 | } | ||
| 607 | }; | ||
| 608 | const rt::shims::XMemAllocationHooks title_hooks{&HookCallbacks::alloc, &HookCallbacks::free}; | ||
| 609 | rt::shims::add_xmem_allocation_hooks(&title_hooks, &title_hooks); | ||
| 610 | |||
| 611 | D3D12_DESCRIPTOR_HEAP_DESC hooked_desc{}; | ||
| 612 | hooked_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; | ||
| 613 | hooked_desc.NumDescriptors = 4; | ||
| 614 | hooked_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; | ||
| 615 | const UINT inc = device->GetDescriptorHandleIncrementSize(hooked_desc.Type); | ||
| 616 | ComPtr<ID3D12DescriptorHeap> hooked_heap; | ||
| 617 | hr = device->CreateDescriptorHeap(&hooked_desc, IID_PPV_ARGS(hooked_heap.GetAddressOf())); | ||
| 618 | check(SUCCEEDED(hr) && hooked_heap != nullptr, "hooked slot 14 creates a descriptor heap"); | ||
| 619 | check(hook_state.alloc_count == 1 && hook_state.alloc_size == hooked_desc.NumDescriptors * inc && | ||
| 620 | hook_state.alloc_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes, | ||
| 621 | "slot 14 invokes __XMemAllocationHooks__ with heap byte size and D3D descriptor-heap attributes"); | ||
| 622 | if (hooked_heap) { | ||
| 623 | void** hooked_vtbl = *reinterpret_cast<void***>(hooked_heap.Get()); | ||
| 624 | const auto hooked_cpu = reinterpret_cast<GetCpuHandleFn>(hooked_vtbl[9])(hooked_heap.Get()); | ||
| 625 | const auto hooked_gpu = reinterpret_cast<GetGpuHandleFn>(hooked_vtbl[10])(hooked_heap.Get()); | ||
| 626 | const auto expected_base = reinterpret_cast<SIZE_T>(hook_state.backing); | ||
| 627 | check(hooked_cpu.ptr == expected_base, | ||
| 628 | "hooked heap slot 9 returns the title-supplied CPU descriptor base"); | ||
| 629 | check(hooked_gpu.ptr != 0, "hooked heap slot 10 returns a valid GPU descriptor start"); | ||
| 630 | |||
| 631 | // Create a CBV at slot 1 of the title-backed heap; WARP validates the translated real handle. | ||
| 632 | D3D12_CONSTANT_BUFFER_VIEW_DESC cbv{}; | ||
| 633 | cbv.BufferLocation = 0; | ||
| 634 | cbv.SizeInBytes = 256; | ||
| 635 | device->CreateConstantBufferView(&cbv, D3D12_CPU_DESCRIPTOR_HANDLE{expected_base + inc}); | ||
| 636 | check(true, "CreateConstantBufferView translates a title-backed CPU descriptor handle on WARP"); | ||
| 637 | } | ||
| 638 | hooked_heap.Reset(); | ||
| 639 | check(hook_state.free_count == 1 && hook_state.freed_ptr == hook_state.backing && | ||
| 640 | hook_state.freed_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes, | ||
| 641 | "releasing the hooked heap frees its backing through __XMemAllocationHooks__"); | ||
| 642 | rt::shims::remove_xmem_allocation_hooks(&title_hooks); | ||
| 643 | } | ||
| 644 | |||
| 580 | // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. | 645 | // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. |
| 581 | if (real) real->AddRef(); | 646 | if (real) real->AddRef(); |
| 582 | // Drop every device child before the proxy so the PC device is not released first. | 647 | // Drop every device child before the proxy so the PC device is not released first. |