diff options
| author | LeonidaBringup <bringup@local> | 2026-11-23 17:41:55 +0000 |
|---|---|---|
| committer | LeonidaBringup <bringup@local> | 2026-11-23 17:41:55 +0000 |
| commit | 0aba6c3611541f169c25f0e3a1de90db25bdd2d4 (patch) | |
| tree | a1bce43ced94709a8593fa3b735b4dab2045fe05 /runtime/test/device_proxy_test.cpp | |
| parent | ce3f2dbdef29ba6696a4bec00b026ba5937826a4 (diff) | |
d3d12_xs: translate title-owned CPU descriptor handles in ListProxy and preserve ClearDepthStencilView FLOAT Depth
#7021 translates title-owned CPU descriptor handles (__XMemAllocationHooks__) on
device slots 17-24, and #2490 wraps console-ABI command lists in ListProxy, but
neither wires translate_cpu_handle into the five graphics command-list methods
that consume CPU descriptor handles:
- console slot 50 (PC 46): OMSetRenderTargets (RTV array / single-range + DSV)
- console slot 51 (PC 47): ClearDepthStencilView (DSV handle)
- console slot 52 (PC 48): ClearRenderTargetView (RTV handle)
- console slot 53 (PC 49): ClearUnorderedAccessViewUint (CPU handle; GPU kept)
- console slot 54 (PC 50): ClearUnorderedAccessViewFloat (CPU handle; GPU kept)
Also installs a typed WINAPI thunk for console slot 51 (ClearDepthStencilView):
on the Windows x64 ABI, FLOAT Depth (4th argument) is passed in XMM3 rather than
R9, so the generic all-uint64_t list_slot template dropped/clobbered Depth before
forwarding to the real list.
Adds synthetic mock coverage across all five slots (including single-range RTVs,
null/zero-RTV cases, count > 8 bounds safety, outside-range passthrough, and
XMM3 float preservation) plus an end-to-end WARP/Vulkan GPU clear and readback
check in device_proxy_test.
Diffstat (limited to 'runtime/test/device_proxy_test.cpp')
| -rw-r--r-- | runtime/test/device_proxy_test.cpp | 183 |
1 files changed, 182 insertions, 1 deletions
diff --git a/runtime/test/device_proxy_test.cpp b/runtime/test/device_proxy_test.cpp index 36afc3c34..8b8eeeef7 100644 --- a/runtime/test/device_proxy_test.cpp +++ b/runtime/test/device_proxy_test.cpp | |||
| @@ -15,6 +15,7 @@ | |||
| 15 | #include <d3dcompiler.h> | 15 | #include <d3dcompiler.h> |
| 16 | #include <dxgi1_4.h> | 16 | #include <dxgi1_4.h> |
| 17 | 17 | ||
| 18 | #include <cmath> | ||
| 18 | #include <cstdint> | 19 | #include <cstdint> |
| 19 | #include <cstdio> | 20 | #include <cstdio> |
| 20 | #include <cstring> | 21 | #include <cstring> |
| @@ -559,7 +560,187 @@ int main(int argc, char** argv) { | |||
| 559 | check(hook_state.free_count == 1 && hook_state.freed_ptr == hook_state.backing && | 560 | check(hook_state.free_count == 1 && hook_state.freed_ptr == hook_state.backing && |
| 560 | hook_state.freed_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes, | 561 | hook_state.freed_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes, |
| 561 | "releasing the hooked heap frees its backing through __XMemAllocationHooks__"); | 562 | "releasing the hooked heap frees its backing through __XMemAllocationHooks__"); |
| 562 | rt::shims::remove_xmem_allocation_hooks(&title_hooks); | 563 | |
| 564 | // End-to-end console command list clear + readback with title-backed RTV and DSV heaps: | ||
| 565 | // verifies CPU handle translation on console slots 50/51/52 and FLOAT Depth (XMM3) on slot 51. | ||
| 566 | { | ||
| 567 | struct DualHeapState { | ||
| 568 | alignas(64) std::uint8_t rtv_backing[1024]{}; | ||
| 569 | alignas(64) std::uint8_t dsv_backing[1024]{}; | ||
| 570 | int next_slice = 0; | ||
| 571 | }; | ||
| 572 | static DualHeapState dual_state; | ||
| 573 | dual_state = {}; | ||
| 574 | struct DualHooks { | ||
| 575 | static void* __stdcall alloc(std::size_t size, std::uint64_t attr) { | ||
| 576 | if ((attr & 0x3FFFull) != 4 || size > 1024) return nullptr; | ||
| 577 | return dual_state.next_slice++ == 0 ? dual_state.rtv_backing : dual_state.dsv_backing; | ||
| 578 | } | ||
| 579 | static void __stdcall free(void*, std::uint64_t) {} | ||
| 580 | }; | ||
| 581 | const rt::shims::XMemAllocationHooks dual_hooks{&DualHooks::alloc, &DualHooks::free}; | ||
| 582 | rt::shims::remove_xmem_allocation_hooks(&title_hooks); | ||
| 583 | rt::shims::add_xmem_allocation_hooks(&dual_hooks, &dual_hooks); | ||
| 584 | |||
| 585 | D3D12_DESCRIPTOR_HEAP_DESC rtv_hd{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; | ||
| 586 | D3D12_DESCRIPTOR_HEAP_DESC dsv_hd{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; | ||
| 587 | ComPtr<ID3D12DescriptorHeap> rtv_heap, dsv_heap; | ||
| 588 | hr = device->CreateDescriptorHeap(&rtv_hd, IID_PPV_ARGS(rtv_heap.GetAddressOf())); | ||
| 589 | HRESULT hr_dsv = device->CreateDescriptorHeap(&dsv_hd, IID_PPV_ARGS(dsv_heap.GetAddressOf())); | ||
| 590 | check(SUCCEEDED(hr) && SUCCEEDED(hr_dsv) && rtv_heap && dsv_heap, | ||
| 591 | "hooked RTV and DSV heaps create through slot 14"); | ||
| 592 | |||
| 593 | const D3D12_HEAP_PROPERTIES default_hp = default_heap(); | ||
| 594 | D3D12_HEAP_PROPERTIES readback_hp = default_heap(); | ||
| 595 | readback_hp.Type = D3D12_HEAP_TYPE_READBACK; | ||
| 596 | D3D12_RESOURCE_DESC rt_desc{}; | ||
| 597 | rt_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 598 | rt_desc.Width = 4; | ||
| 599 | rt_desc.Height = 4; | ||
| 600 | rt_desc.DepthOrArraySize = 1; | ||
| 601 | rt_desc.MipLevels = 1; | ||
| 602 | rt_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 603 | rt_desc.SampleDesc = {1, 0}; | ||
| 604 | rt_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; | ||
| 605 | |||
| 606 | D3D12_RESOURCE_DESC ds_desc = rt_desc; | ||
| 607 | ds_desc.Format = DXGI_FORMAT_D32_FLOAT; | ||
| 608 | ds_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; | ||
| 609 | |||
| 610 | D3D12_RESOURCE_DESC rb_desc = buffer_desc(); | ||
| 611 | rb_desc.Width = 4096; | ||
| 612 | rb_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; | ||
| 613 | |||
| 614 | ComPtr<ID3D12Resource> rt_tex, ds_tex, rb_buf; | ||
| 615 | hr = device->CreateCommittedResource(&default_hp, D3D12_HEAP_FLAG_NONE, &rt_desc, | ||
| 616 | D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, | ||
| 617 | IID_PPV_ARGS(rt_tex.GetAddressOf())); | ||
| 618 | HRESULT hr2 = device->CreateCommittedResource(&default_hp, D3D12_HEAP_FLAG_NONE, &ds_desc, | ||
| 619 | D3D12_RESOURCE_STATE_DEPTH_WRITE, nullptr, | ||
| 620 | IID_PPV_ARGS(ds_tex.GetAddressOf())); | ||
| 621 | HRESULT hr3 = device->CreateCommittedResource(&readback_hp, D3D12_HEAP_FLAG_NONE, &rb_desc, | ||
| 622 | D3D12_RESOURCE_STATE_COPY_DEST, nullptr, | ||
| 623 | IID_PPV_ARGS(rb_buf.GetAddressOf())); | ||
| 624 | check(SUCCEEDED(hr) && SUCCEEDED(hr2) && SUCCEEDED(hr3), | ||
| 625 | "RT, DS and readback resources create for console list clear test"); | ||
| 626 | |||
| 627 | if (rtv_heap && dsv_heap && rt_tex && ds_tex && rb_buf) { | ||
| 628 | const auto rtv_cpu = reinterpret_cast<GetCpuHandleFn>( | ||
| 629 | (*reinterpret_cast<void***>(rtv_heap.Get()))[9])(rtv_heap.Get()); | ||
| 630 | const auto dsv_cpu = reinterpret_cast<GetCpuHandleFn>( | ||
| 631 | (*reinterpret_cast<void***>(dsv_heap.Get()))[9])(dsv_heap.Get()); | ||
| 632 | const UINT rtv_inc = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); | ||
| 633 | const UINT dsv_inc = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV); | ||
| 634 | const D3D12_CPU_DESCRIPTOR_HANDLE rtv_h{rtv_cpu.ptr + rtv_inc}; | ||
| 635 | const D3D12_CPU_DESCRIPTOR_HANDLE dsv_h{dsv_cpu.ptr + dsv_inc}; | ||
| 636 | check(rtv_cpu.ptr == reinterpret_cast<SIZE_T>(dual_state.rtv_backing) && | ||
| 637 | dsv_cpu.ptr == reinterpret_cast<SIZE_T>(dual_state.dsv_backing), | ||
| 638 | "RTV and DSV heaps report their title-backed CPU descriptor bases"); | ||
| 639 | |||
| 640 | device->CreateRenderTargetView(rt_tex.Get(), nullptr, rtv_h); | ||
| 641 | device->CreateDepthStencilView(ds_tex.Get(), nullptr, dsv_h); | ||
| 642 | |||
| 643 | using CreateQueueXFn = HRESULT(WINAPI*)(void*, const D3D12_COMMAND_QUEUE_DESC*, const IID*, void**); | ||
| 644 | using CreateListXFn = HRESULT(WINAPI*)(void*, const void*, ID3D12CommandAllocator*, | ||
| 645 | ID3D12PipelineState*, const IID*, void**); | ||
| 646 | using CloseFn = HRESULT(WINAPI*)(void*); | ||
| 647 | using BarrierFn = void(WINAPI*)(void*, UINT, const D3D12_RESOURCE_BARRIER*); | ||
| 648 | using CopyTexFn = void(WINAPI*)(void*, const D3D12_TEXTURE_COPY_LOCATION*, UINT, UINT, UINT, | ||
| 649 | const D3D12_TEXTURE_COPY_LOCATION*, const D3D12_BOX*); | ||
| 650 | using OmSetRtFn = void(WINAPI*)(void*, UINT, const D3D12_CPU_DESCRIPTOR_HANDLE*, BOOL, | ||
| 651 | const D3D12_CPU_DESCRIPTOR_HANDLE*); | ||
| 652 | using ClearDsvFn = void(WINAPI*)(void*, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_CLEAR_FLAGS, FLOAT, UINT8, | ||
| 653 | UINT, const D3D12_RECT*); | ||
| 654 | using ClearRtvFn = void(WINAPI*)(void*, D3D12_CPU_DESCRIPTOR_HANDLE, const FLOAT[4], UINT, | ||
| 655 | const D3D12_RECT*); | ||
| 656 | |||
| 657 | const D3D12_COMMAND_QUEUE_DESC qd{}; | ||
| 658 | const IID iid_q = __uuidof(ID3D12CommandQueue); | ||
| 659 | const IID iid_l = __uuidof(ID3D12GraphicsCommandList); | ||
| 660 | ComPtr<ID3D12CommandQueue> clear_q; | ||
| 661 | ComPtr<ID3D12CommandAllocator> clear_alloc; | ||
| 662 | ComPtr<ID3D12GraphicsCommandList> clear_list; | ||
| 663 | ComPtr<ID3D12Fence> clear_fence; | ||
| 664 | hr = reinterpret_cast<CreateQueueXFn>(proxy_vtbl[48])( | ||
| 665 | device.Get(), &qd, &iid_q, reinterpret_cast<void**>(clear_q.GetAddressOf())); | ||
| 666 | HRESULT hra = device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, | ||
| 667 | IID_PPV_ARGS(clear_alloc.GetAddressOf())); | ||
| 668 | struct { | ||
| 669 | UINT Type; | ||
| 670 | UINT NodeMask; | ||
| 671 | } ld{D3D12_COMMAND_LIST_TYPE_DIRECT, 0}; | ||
| 672 | HRESULT hrl = SUCCEEDED(hra) ? reinterpret_cast<CreateListXFn>(proxy_vtbl[73])( | ||
| 673 | device.Get(), &ld, clear_alloc.Get(), nullptr, &iid_l, | ||
| 674 | reinterpret_cast<void**>(clear_list.GetAddressOf())) | ||
| 675 | : hra; | ||
| 676 | HRESULT hrf = device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(clear_fence.GetAddressOf())); | ||
| 677 | check(SUCCEEDED(hr) && SUCCEEDED(hra) && SUCCEEDED(hrl) && SUCCEEDED(hrf) && | ||
| 678 | rt::d3d12_xs::is_list_proxy(clear_list.Get()), | ||
| 679 | "console list proxy created for hooked RTV/DSV clear"); | ||
| 680 | |||
| 681 | if (clear_q && clear_list && clear_fence) { | ||
| 682 | void** lv = *reinterpret_cast<void***>(clear_list.Get()); | ||
| 683 | const FLOAT rgba[4] = {1.0f, 0.0f, 0.5f, 1.0f}; | ||
| 684 | reinterpret_cast<OmSetRtFn>(lv[50])(clear_list.Get(), 1, &rtv_h, FALSE, &dsv_h); | ||
| 685 | reinterpret_cast<ClearRtvFn>(lv[52])(clear_list.Get(), rtv_h, rgba, 0, nullptr); | ||
| 686 | reinterpret_cast<ClearDsvFn>(lv[51])(clear_list.Get(), dsv_h, D3D12_CLEAR_FLAG_DEPTH, 0.25f, 0, 0, | ||
| 687 | nullptr); | ||
| 688 | |||
| 689 | D3D12_RESOURCE_BARRIER b[2]{}; | ||
| 690 | b[0].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; | ||
| 691 | b[0].Transition = {rt_tex.Get(), D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, | ||
| 692 | D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE}; | ||
| 693 | b[1].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; | ||
| 694 | b[1].Transition = {ds_tex.Get(), D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, | ||
| 695 | D3D12_RESOURCE_STATE_DEPTH_WRITE, D3D12_RESOURCE_STATE_COPY_SOURCE}; | ||
| 696 | reinterpret_cast<BarrierFn>(lv[26])(clear_list.Get(), 2, b); | ||
| 697 | |||
| 698 | D3D12_TEXTURE_COPY_LOCATION dst_rt{}, src_rt{}, dst_ds{}, src_ds{}; | ||
| 699 | dst_rt.pResource = rb_buf.Get(); | ||
| 700 | dst_rt.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; | ||
| 701 | dst_rt.PlacedFootprint = {0, {DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 1, 256}}; | ||
| 702 | src_rt.pResource = rt_tex.Get(); | ||
| 703 | src_rt.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; | ||
| 704 | dst_ds.pResource = rb_buf.Get(); | ||
| 705 | dst_ds.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; | ||
| 706 | dst_ds.PlacedFootprint = {1536, {DXGI_FORMAT_R32_TYPELESS, 4, 4, 1, 256}}; | ||
| 707 | src_ds.pResource = ds_tex.Get(); | ||
| 708 | src_ds.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; | ||
| 709 | reinterpret_cast<CopyTexFn>(lv[16])(clear_list.Get(), &dst_rt, 0, 0, 0, &src_rt, nullptr); | ||
| 710 | reinterpret_cast<CopyTexFn>(lv[16])(clear_list.Get(), &dst_ds, 0, 0, 0, &src_ds, nullptr); | ||
| 711 | |||
| 712 | const HRESULT hr_close = reinterpret_cast<CloseFn>(lv[9])(clear_list.Get()); | ||
| 713 | check(hr_close == S_OK, "console list Close succeeds after title-backed RTV/DSV clears"); | ||
| 714 | |||
| 715 | ID3D12CommandList* lists[] = {clear_list.Get()}; | ||
| 716 | clear_q->ExecuteCommandLists(1, lists); | ||
| 717 | HANDLE ev = CreateEventW(nullptr, FALSE, FALSE, nullptr); | ||
| 718 | const bool done = ev && SUCCEEDED(clear_q->Signal(clear_fence.Get(), 1)) && | ||
| 719 | SUCCEEDED(clear_fence->SetEventOnCompletion(1, ev)) && | ||
| 720 | WaitForSingleObject(ev, 5000) == 0; | ||
| 721 | if (ev) CloseHandle(ev); | ||
| 722 | check(done, "console list clear commands complete on the GPU"); | ||
| 723 | |||
| 724 | void* mapped = nullptr; | ||
| 725 | const D3D12_RANGE range{0, 4096}; | ||
| 726 | if (done && SUCCEEDED(rb_buf->Map(0, &range, &mapped)) && mapped) { | ||
| 727 | const auto* bytes = static_cast<const std::uint8_t*>(mapped); | ||
| 728 | const bool rt_ok = bytes[0] == 255 && bytes[1] == 0 && (bytes[2] == 127 || bytes[2] == 128) && | ||
| 729 | bytes[3] == 255; | ||
| 730 | float depth_got = -1.0f; | ||
| 731 | std::memcpy(&depth_got, bytes + 1536, sizeof(depth_got)); | ||
| 732 | const bool ds_ok = std::fabs(depth_got - 0.25f) < 1e-6f; | ||
| 733 | check(rt_ok, "console slot 52 ClearRenderTargetView writes expected RGBA texel via hooked heap"); | ||
| 734 | check(ds_ok, "console slot 51 ClearDepthStencilView preserves FLOAT depth 0.25 via XMM3"); | ||
| 735 | const D3D12_RANGE empty{0, 0}; | ||
| 736 | rb_buf->Unmap(0, &empty); | ||
| 737 | } | ||
| 738 | } | ||
| 739 | } | ||
| 740 | rtv_heap.Reset(); | ||
| 741 | dsv_heap.Reset(); | ||
| 742 | rt::shims::remove_xmem_allocation_hooks(&dual_hooks); | ||
| 743 | } | ||
| 563 | } | 744 | } |
| 564 | 745 | ||
| 565 | // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. | 746 | // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. |