diff options
| author | ASTROWwwW <astrxwledev@gmail.com> | 2026-11-23 19:55:34 +0100 |
|---|---|---|
| committer | ASTROWwwW <astrxwledev@gmail.com> | 2026-11-23 19:55:34 +0100 |
| commit | bd6b147b16b62e6903a532084b3e53b89b5a2ad8 (patch) | |
| tree | ea9957b13486ad51f8d7eec5c853d7595ad63422 /runtime/test/device_proxy_test.cpp | |
| parent | bee47186f68c995b196a5215ccdc5abcf80660e7 (diff) | |
| parent | 0aba6c3611541f169c25f0e3a1de90db25bdd2d4 (diff) | |
Merge branch 'pr7880' into ladder-frame
# Conflicts:
# runtime/shims/d3d12_xs_device.cpp
# runtime/shims/d3d12_xs_device.h
# runtime/test/xs_proxy_mock_test.cpp
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 e9b19b7a8..ba704d6cc 100644 --- a/runtime/test/device_proxy_test.cpp +++ b/runtime/test/device_proxy_test.cpp | |||
| @@ -16,6 +16,7 @@ | |||
| 16 | #include <d3dcompiler.h> | 16 | #include <d3dcompiler.h> |
| 17 | #include <dxgi1_4.h> | 17 | #include <dxgi1_4.h> |
| 18 | 18 | ||
| 19 | #include <cmath> | ||
| 19 | #include <cstdint> | 20 | #include <cstdint> |
| 20 | #include <cstdio> | 21 | #include <cstdio> |
| 21 | #include <cstring> | 22 | #include <cstring> |
| @@ -761,7 +762,187 @@ int main(int argc, char** argv) { | |||
| 761 | check(hook_state.free_count == 1 && hook_state.freed_ptr == hook_state.backing && | 762 | check(hook_state.free_count == 1 && hook_state.freed_ptr == hook_state.backing && |
| 762 | hook_state.freed_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes, | 763 | hook_state.freed_attr == rt::d3d12_xs::kXmemDescriptorHeapAttributes, |
| 763 | "releasing the hooked heap frees its backing through __XMemAllocationHooks__"); | 764 | "releasing the hooked heap frees its backing through __XMemAllocationHooks__"); |
| 764 | rt::shims::remove_xmem_allocation_hooks(&title_hooks); | 765 | |
| 766 | // End-to-end console command list clear + readback with title-backed RTV and DSV heaps: | ||
| 767 | // verifies CPU handle translation on console slots 50/51/52 and FLOAT Depth (XMM3) on slot 51. | ||
| 768 | { | ||
| 769 | struct DualHeapState { | ||
| 770 | alignas(64) std::uint8_t rtv_backing[1024]{}; | ||
| 771 | alignas(64) std::uint8_t dsv_backing[1024]{}; | ||
| 772 | int next_slice = 0; | ||
| 773 | }; | ||
| 774 | static DualHeapState dual_state; | ||
| 775 | dual_state = {}; | ||
| 776 | struct DualHooks { | ||
| 777 | static void* __stdcall alloc(std::size_t size, std::uint64_t attr) { | ||
| 778 | if ((attr & 0x3FFFull) != 4 || size > 1024) return nullptr; | ||
| 779 | return dual_state.next_slice++ == 0 ? dual_state.rtv_backing : dual_state.dsv_backing; | ||
| 780 | } | ||
| 781 | static void __stdcall free(void*, std::uint64_t) {} | ||
| 782 | }; | ||
| 783 | const rt::shims::XMemAllocationHooks dual_hooks{&DualHooks::alloc, &DualHooks::free}; | ||
| 784 | rt::shims::remove_xmem_allocation_hooks(&title_hooks); | ||
| 785 | rt::shims::add_xmem_allocation_hooks(&dual_hooks, &dual_hooks); | ||
| 786 | |||
| 787 | D3D12_DESCRIPTOR_HEAP_DESC rtv_hd{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; | ||
| 788 | D3D12_DESCRIPTOR_HEAP_DESC dsv_hd{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; | ||
| 789 | ComPtr<ID3D12DescriptorHeap> rtv_heap, dsv_heap; | ||
| 790 | hr = device->CreateDescriptorHeap(&rtv_hd, IID_PPV_ARGS(rtv_heap.GetAddressOf())); | ||
| 791 | HRESULT hr_dsv = device->CreateDescriptorHeap(&dsv_hd, IID_PPV_ARGS(dsv_heap.GetAddressOf())); | ||
| 792 | check(SUCCEEDED(hr) && SUCCEEDED(hr_dsv) && rtv_heap && dsv_heap, | ||
| 793 | "hooked RTV and DSV heaps create through slot 14"); | ||
| 794 | |||
| 795 | const D3D12_HEAP_PROPERTIES default_hp = default_heap(); | ||
| 796 | D3D12_HEAP_PROPERTIES readback_hp = default_heap(); | ||
| 797 | readback_hp.Type = D3D12_HEAP_TYPE_READBACK; | ||
| 798 | D3D12_RESOURCE_DESC rt_desc{}; | ||
| 799 | rt_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; | ||
| 800 | rt_desc.Width = 4; | ||
| 801 | rt_desc.Height = 4; | ||
| 802 | rt_desc.DepthOrArraySize = 1; | ||
| 803 | rt_desc.MipLevels = 1; | ||
| 804 | rt_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; | ||
| 805 | rt_desc.SampleDesc = {1, 0}; | ||
| 806 | rt_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; | ||
| 807 | |||
| 808 | D3D12_RESOURCE_DESC ds_desc = rt_desc; | ||
| 809 | ds_desc.Format = DXGI_FORMAT_D32_FLOAT; | ||
| 810 | ds_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; | ||
| 811 | |||
| 812 | D3D12_RESOURCE_DESC rb_desc = buffer_desc(); | ||
| 813 | rb_desc.Width = 4096; | ||
| 814 | rb_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; | ||
| 815 | |||
| 816 | ComPtr<ID3D12Resource> rt_tex, ds_tex, rb_buf; | ||
| 817 | hr = device->CreateCommittedResource(&default_hp, D3D12_HEAP_FLAG_NONE, &rt_desc, | ||
| 818 | D3D12_RESOURCE_STATE_RENDER_TARGET, nullptr, | ||
| 819 | IID_PPV_ARGS(rt_tex.GetAddressOf())); | ||
| 820 | HRESULT hr2 = device->CreateCommittedResource(&default_hp, D3D12_HEAP_FLAG_NONE, &ds_desc, | ||
| 821 | D3D12_RESOURCE_STATE_DEPTH_WRITE, nullptr, | ||
| 822 | IID_PPV_ARGS(ds_tex.GetAddressOf())); | ||
| 823 | HRESULT hr3 = device->CreateCommittedResource(&readback_hp, D3D12_HEAP_FLAG_NONE, &rb_desc, | ||
| 824 | D3D12_RESOURCE_STATE_COPY_DEST, nullptr, | ||
| 825 | IID_PPV_ARGS(rb_buf.GetAddressOf())); | ||
| 826 | check(SUCCEEDED(hr) && SUCCEEDED(hr2) && SUCCEEDED(hr3), | ||
| 827 | "RT, DS and readback resources create for console list clear test"); | ||
| 828 | |||
| 829 | if (rtv_heap && dsv_heap && rt_tex && ds_tex && rb_buf) { | ||
| 830 | const auto rtv_cpu = reinterpret_cast<GetCpuHandleFn>( | ||
| 831 | (*reinterpret_cast<void***>(rtv_heap.Get()))[9])(rtv_heap.Get()); | ||
| 832 | const auto dsv_cpu = reinterpret_cast<GetCpuHandleFn>( | ||
| 833 | (*reinterpret_cast<void***>(dsv_heap.Get()))[9])(dsv_heap.Get()); | ||
| 834 | const UINT rtv_inc = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); | ||
| 835 | const UINT dsv_inc = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV); | ||
| 836 | const D3D12_CPU_DESCRIPTOR_HANDLE rtv_h{rtv_cpu.ptr + rtv_inc}; | ||
| 837 | const D3D12_CPU_DESCRIPTOR_HANDLE dsv_h{dsv_cpu.ptr + dsv_inc}; | ||
| 838 | check(rtv_cpu.ptr == reinterpret_cast<SIZE_T>(dual_state.rtv_backing) && | ||
| 839 | dsv_cpu.ptr == reinterpret_cast<SIZE_T>(dual_state.dsv_backing), | ||
| 840 | "RTV and DSV heaps report their title-backed CPU descriptor bases"); | ||
| 841 | |||
| 842 | device->CreateRenderTargetView(rt_tex.Get(), nullptr, rtv_h); | ||
| 843 | device->CreateDepthStencilView(ds_tex.Get(), nullptr, dsv_h); | ||
| 844 | |||
| 845 | using CreateQueueXFn = HRESULT(WINAPI*)(void*, const D3D12_COMMAND_QUEUE_DESC*, const IID*, void**); | ||
| 846 | using CreateListXFn = HRESULT(WINAPI*)(void*, const void*, ID3D12CommandAllocator*, | ||
| 847 | ID3D12PipelineState*, const IID*, void**); | ||
| 848 | using CloseFn = HRESULT(WINAPI*)(void*); | ||
| 849 | using BarrierFn = void(WINAPI*)(void*, UINT, const D3D12_RESOURCE_BARRIER*); | ||
| 850 | using CopyTexFn = void(WINAPI*)(void*, const D3D12_TEXTURE_COPY_LOCATION*, UINT, UINT, UINT, | ||
| 851 | const D3D12_TEXTURE_COPY_LOCATION*, const D3D12_BOX*); | ||
| 852 | using OmSetRtFn = void(WINAPI*)(void*, UINT, const D3D12_CPU_DESCRIPTOR_HANDLE*, BOOL, | ||
| 853 | const D3D12_CPU_DESCRIPTOR_HANDLE*); | ||
| 854 | using ClearDsvFn = void(WINAPI*)(void*, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_CLEAR_FLAGS, FLOAT, UINT8, | ||
| 855 | UINT, const D3D12_RECT*); | ||
| 856 | using ClearRtvFn = void(WINAPI*)(void*, D3D12_CPU_DESCRIPTOR_HANDLE, const FLOAT[4], UINT, | ||
| 857 | const D3D12_RECT*); | ||
| 858 | |||
| 859 | const D3D12_COMMAND_QUEUE_DESC qd{}; | ||
| 860 | const IID iid_q = __uuidof(ID3D12CommandQueue); | ||
| 861 | const IID iid_l = __uuidof(ID3D12GraphicsCommandList); | ||
| 862 | ComPtr<ID3D12CommandQueue> clear_q; | ||
| 863 | ComPtr<ID3D12CommandAllocator> clear_alloc; | ||
| 864 | ComPtr<ID3D12GraphicsCommandList> clear_list; | ||
| 865 | ComPtr<ID3D12Fence> clear_fence; | ||
| 866 | hr = reinterpret_cast<CreateQueueXFn>(proxy_vtbl[48])( | ||
| 867 | device.Get(), &qd, &iid_q, reinterpret_cast<void**>(clear_q.GetAddressOf())); | ||
| 868 | HRESULT hra = device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, | ||
| 869 | IID_PPV_ARGS(clear_alloc.GetAddressOf())); | ||
| 870 | struct { | ||
| 871 | UINT Type; | ||
| 872 | UINT NodeMask; | ||
| 873 | } ld{D3D12_COMMAND_LIST_TYPE_DIRECT, 0}; | ||
| 874 | HRESULT hrl = SUCCEEDED(hra) ? reinterpret_cast<CreateListXFn>(proxy_vtbl[73])( | ||
| 875 | device.Get(), &ld, clear_alloc.Get(), nullptr, &iid_l, | ||
| 876 | reinterpret_cast<void**>(clear_list.GetAddressOf())) | ||
| 877 | : hra; | ||
| 878 | HRESULT hrf = device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(clear_fence.GetAddressOf())); | ||
| 879 | check(SUCCEEDED(hr) && SUCCEEDED(hra) && SUCCEEDED(hrl) && SUCCEEDED(hrf) && | ||
| 880 | rt::d3d12_xs::is_list_proxy(clear_list.Get()), | ||
| 881 | "console list proxy created for hooked RTV/DSV clear"); | ||
| 882 | |||
| 883 | if (clear_q && clear_list && clear_fence) { | ||
| 884 | void** lv = *reinterpret_cast<void***>(clear_list.Get()); | ||
| 885 | const FLOAT rgba[4] = {1.0f, 0.0f, 0.5f, 1.0f}; | ||
| 886 | reinterpret_cast<OmSetRtFn>(lv[50])(clear_list.Get(), 1, &rtv_h, FALSE, &dsv_h); | ||
| 887 | reinterpret_cast<ClearRtvFn>(lv[52])(clear_list.Get(), rtv_h, rgba, 0, nullptr); | ||
| 888 | reinterpret_cast<ClearDsvFn>(lv[51])(clear_list.Get(), dsv_h, D3D12_CLEAR_FLAG_DEPTH, 0.25f, 0, 0, | ||
| 889 | nullptr); | ||
| 890 | |||
| 891 | D3D12_RESOURCE_BARRIER b[2]{}; | ||
| 892 | b[0].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; | ||
| 893 | b[0].Transition = {rt_tex.Get(), D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, | ||
| 894 | D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE}; | ||
| 895 | b[1].Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; | ||
| 896 | b[1].Transition = {ds_tex.Get(), D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, | ||
| 897 | D3D12_RESOURCE_STATE_DEPTH_WRITE, D3D12_RESOURCE_STATE_COPY_SOURCE}; | ||
| 898 | reinterpret_cast<BarrierFn>(lv[26])(clear_list.Get(), 2, b); | ||
| 899 | |||
| 900 | D3D12_TEXTURE_COPY_LOCATION dst_rt{}, src_rt{}, dst_ds{}, src_ds{}; | ||
| 901 | dst_rt.pResource = rb_buf.Get(); | ||
| 902 | dst_rt.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; | ||
| 903 | dst_rt.PlacedFootprint = {0, {DXGI_FORMAT_R8G8B8A8_UNORM, 4, 4, 1, 256}}; | ||
| 904 | src_rt.pResource = rt_tex.Get(); | ||
| 905 | src_rt.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; | ||
| 906 | dst_ds.pResource = rb_buf.Get(); | ||
| 907 | dst_ds.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; | ||
| 908 | dst_ds.PlacedFootprint = {1536, {DXGI_FORMAT_R32_TYPELESS, 4, 4, 1, 256}}; | ||
| 909 | src_ds.pResource = ds_tex.Get(); | ||
| 910 | src_ds.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; | ||
| 911 | reinterpret_cast<CopyTexFn>(lv[16])(clear_list.Get(), &dst_rt, 0, 0, 0, &src_rt, nullptr); | ||
| 912 | reinterpret_cast<CopyTexFn>(lv[16])(clear_list.Get(), &dst_ds, 0, 0, 0, &src_ds, nullptr); | ||
| 913 | |||
| 914 | const HRESULT hr_close = reinterpret_cast<CloseFn>(lv[9])(clear_list.Get()); | ||
| 915 | check(hr_close == S_OK, "console list Close succeeds after title-backed RTV/DSV clears"); | ||
| 916 | |||
| 917 | ID3D12CommandList* lists[] = {clear_list.Get()}; | ||
| 918 | clear_q->ExecuteCommandLists(1, lists); | ||
| 919 | HANDLE ev = CreateEventW(nullptr, FALSE, FALSE, nullptr); | ||
| 920 | const bool done = ev && SUCCEEDED(clear_q->Signal(clear_fence.Get(), 1)) && | ||
| 921 | SUCCEEDED(clear_fence->SetEventOnCompletion(1, ev)) && | ||
| 922 | WaitForSingleObject(ev, 5000) == 0; | ||
| 923 | if (ev) CloseHandle(ev); | ||
| 924 | check(done, "console list clear commands complete on the GPU"); | ||
| 925 | |||
| 926 | void* mapped = nullptr; | ||
| 927 | const D3D12_RANGE range{0, 4096}; | ||
| 928 | if (done && SUCCEEDED(rb_buf->Map(0, &range, &mapped)) && mapped) { | ||
| 929 | const auto* bytes = static_cast<const std::uint8_t*>(mapped); | ||
| 930 | const bool rt_ok = bytes[0] == 255 && bytes[1] == 0 && (bytes[2] == 127 || bytes[2] == 128) && | ||
| 931 | bytes[3] == 255; | ||
| 932 | float depth_got = -1.0f; | ||
| 933 | std::memcpy(&depth_got, bytes + 1536, sizeof(depth_got)); | ||
| 934 | const bool ds_ok = std::fabs(depth_got - 0.25f) < 1e-6f; | ||
| 935 | check(rt_ok, "console slot 52 ClearRenderTargetView writes expected RGBA texel via hooked heap"); | ||
| 936 | check(ds_ok, "console slot 51 ClearDepthStencilView preserves FLOAT depth 0.25 via XMM3"); | ||
| 937 | const D3D12_RANGE empty{0, 0}; | ||
| 938 | rb_buf->Unmap(0, &empty); | ||
| 939 | } | ||
| 940 | } | ||
| 941 | } | ||
| 942 | rtv_heap.Reset(); | ||
| 943 | dsv_heap.Reset(); | ||
| 944 | rt::shims::remove_xmem_allocation_hooks(&dual_hooks); | ||
| 945 | } | ||
| 765 | } | 946 | } |
| 766 | 947 | ||
| 767 | // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. | 948 | // Hold a reference to the PC device so the final check can prove the proxy's Release balances its own. |