From e9a13181f4b5e84a860dada2fc18055c519e5627 Mon Sep 17 00:00:00 2001 From: justr4mzi <243612488+justr4mzi@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:22:45 +0000 Subject: [PATCH 1/2] d3d12_xs: route device slots 86, 101, and 108 to PC Device2/Device5/Device8 Reopened from #7201 (closed in the PR reset), rebased on current main. Slot 86 (CreatePipelineState stream) -> PC slot 47, slot 101 (CreateStateObject) -> PC slot 62, slot 108 (GetResourceAllocationInfo2) -> PC slot 68. Slot 108 accepts the console C-ABI order (return buffer first) and the PC order, reads the title's 56-byte D3D12_RESOURCE_DESC entries, expands each into a zeroed D3D12_RESOURCE_DESC1, normalizes Xbox layout words and flag bits, and refuses a count with a null array. Rebase: kept main's slot 133 (CreatePlacedResourceX) next to the new slots, and slot 108 now masks flags with slot 133's PC set (0x5FF, which keeps USE_TIGHT_ALIGNMENT and drops 0x200) instead of 0x1FF. (cherry picked from commit 1b07215c84443b87ad152ffdc8d23b0356125456) --- docs/d3d12_vulkan.md | 8 +- runtime/shims/d3d12_xs_device.cpp | 109 +++++++++++++++++++++- runtime/shims/d3d12_xs_device.h | 10 +- runtime/test/device_proxy_test.cpp | 98 ++++++++++++++++++++ runtime/test/xs_proxy_mock_test.cpp | 139 +++++++++++++++++++++++++++- 5 files changed, 360 insertions(+), 4 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8bf2bd38ea..fea374e540 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -90,7 +90,13 @@ enough for the bundled middleware's `d3d12_xs` calls, not for the game. The laye desktop order, but also an extra texture-creation method at slot 76, a `CreateComputePipelineState` call at slot 62 where the PC interface it forwarded to presents `CreateStateObject` (different arity), and descriptor-heap start handles returned in RAX at heap slots 9/10. Implement the public chain plus those measured overrides - explicitly; do not assume every slot. Device 11 adds `CreateSampler2`; command list 8 adds + explicitly; do not assume every slot. On the retail executable's higher device slots (`runtime/shims/d3d12_xs_device.cpp`), + slot 86 routes `CreatePipelineState(desc, riid, out)` to PC slot 47 (`ID3D12Device2::CreatePipelineState`), + slot 101 routes `CreateStateObject(desc, riid, out)` to PC slot 62 (`ID3D12Device5::CreateStateObject`), and + slot 108 routes `GetResourceAllocationInfo2` (accepting the console C-ABI struct return with the return buffer in + `RCX` and the device in `RDX`, expanding 56-byte `D3D12_RESOURCE_DESC` entries to `D3D12_RESOURCE_DESC1` and + normalizing Xbox layout words and flag bits on the copy) to PC + slot 68 (`ID3D12Device8::GetResourceAllocationInfo2`). Device 11 adds `CreateSampler2`; command list 8 adds `OMSetFrontAndBackStencilRef` (public SDK). - **Unknown IIDs and slots.** `QueryInterface` must log the IID and return `E_NOINTERFACE` if it is not in the public chain. If the game then fails, the IID and the call site identify the Xbox-specific interface to reverse. diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 85aece0bd2..f282ddbfeb 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -174,6 +174,9 @@ const char* slot_name(unsigned slot) { case 78: return "Xbox SetFrameIntervalX"; case 79: return "Xbox ScheduleFrameEventX"; case 80: return "Xbox WaitFrameEventX"; + case 86: return "Xbox CreatePipelineState -> PC CreatePipelineState (slot 47)"; + case 101: return "Xbox CreateStateObject -> PC CreateStateObject (slot 62)"; + case 108: return "Xbox GetResourceAllocationInfo2 -> PC GetResourceAllocationInfo2 (slot 68)"; case 133: return "Xbox CreatePlacedResourceX (a buffer on the title's pages)"; default: return "forwarded"; } @@ -2048,6 +2051,100 @@ HRESULT proxy_create_committed_resource3_x(DeviceProxy* self, std::uint64_t heap riid, out); } +// Xbox slot 86: CreatePipelineState(desc, riid, out), forwarded to PC ID3D12Device2::CreatePipelineState (PC slot 47). +HRESULT proxy_create_pipeline_state(DeviceProxy* self, const D3D12_PIPELINE_STATE_STREAM_DESC* desc, const IID* riid, + void** out) { + note_slot(86); + if (out) *out = nullptr; + if (self->slot_count <= 47) return refuse_beyond_device(86, self->slot_count); // predates ID3D12Device2 + if (!riid) return E_POINTER; + using Fn = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, const IID*, void**); + return reinterpret_cast(self->real_vtable[47])(self->real, desc, riid, out); +} + +// Xbox slot 101: CreateStateObject(desc, riid, out), forwarded to PC ID3D12Device5::CreateStateObject (PC slot 62, +// displaced on Xbox by compute-PSO slot 62). +HRESULT proxy_create_state_object(DeviceProxy* self, const D3D12_STATE_OBJECT_DESC* desc, const IID* riid, void** out) { + note_slot(101); + if (out) *out = nullptr; + if (self->slot_count <= 62) return refuse_beyond_device(101, self->slot_count); // predates ID3D12Device5 + if (!riid) return E_POINTER; + using Fn = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_STATE_OBJECT_DESC*, const IID*, void**); + return reinterpret_cast(self->real_vtable[62])(self->real, desc, riid, out); +} + +// Xbox slot 108: GetResourceAllocationInfo2, forwarded to PC ID3D12Device8::GetResourceAllocationInfo2 (PC slot 68). +// The retail executable calls struct-returning device methods with the C ABI (return buffer in RCX, device proxy in +// RDX); a PC C++ caller passes the proxy in RCX and the return buffer in RDX. Both orders are accepted. The title's +// descriptor builder populates 56-byte D3D12_RESOURCE_DESC entries, so the input array is indexed with a 56-byte +// stride (sizeof(D3D12_RESOURCE_DESC)), each entry is expanded into a zeroed D3D12_RESOURCE_DESC1 with +// SamplerFeedbackMipRegion cleared, and Xbox layout words (e.g. 0x101, 0x105) and console flag bits above the PC +// D3D12_RESOURCE_FLAGS are normalized on the copy before the PC call. +std::uint64_t proxy_get_resource_allocation_info2(DeviceProxy* self, std::uint64_t a1, std::uint64_t a2, + std::uint64_t a3, std::uint64_t a4, std::uint64_t a5) { + note_slot(108); + DeviceProxy* proxy = nullptr; + D3D12_RESOURCE_ALLOCATION_INFO* ret = nullptr; + if (is_device_proxy(self)) { + proxy = self; + ret = reinterpret_cast(a1); + } else if (is_device_proxy(reinterpret_cast(a1))) { + proxy = reinterpret_cast(a1); + ret = reinterpret_cast(self); + } + if (!ret) return 0; + if (!proxy) { + *ret = {UINT64_MAX, 0}; + return reinterpret_cast(ret); + } + if (proxy->slot_count <= 68) { + refuse_beyond_device(108, proxy->slot_count); + *ret = {UINT64_MAX, 0}; + return reinterpret_cast(ret); + } + const auto visible_mask = static_cast(a2); + const auto count = static_cast(a3); + const auto* raw_descs = reinterpret_cast(a4); + auto* info1 = reinterpret_cast(a5); + if (count > 0 && !raw_descs) { // a count with no descriptor array: refuse like the other bad-input paths + *ret = {UINT64_MAX, 0}; + return reinterpret_cast(ret); + } + + std::vector cleaned; + const D3D12_RESOURCE_DESC1* descs_to_pass = nullptr; + if (raw_descs && count > 0) { + try { + cleaned.resize(count); + } catch (...) { + *ret = {UINT64_MAX, 0}; + return reinterpret_cast(ret); + } + for (UINT i = 0; i < count; ++i) { + D3D12_RESOURCE_DESC1 d{}; + std::memcpy(&d, &raw_descs[i], sizeof(D3D12_RESOURCE_DESC)); + d.SamplerFeedbackMipRegion = {}; + if (static_cast(d.Layout) > + static_cast(D3D12_TEXTURE_LAYOUT_64KB_STANDARD_SWIZZLE)) { + d.Layout = d.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? D3D12_TEXTURE_LAYOUT_ROW_MAJOR + : D3D12_TEXTURE_LAYOUT_UNKNOWN; + } + // The same PC flag set as slot 133: up to RAYTRACING_ACCELERATION_STRUCTURE (0x100) and + // USE_TIGHT_ALIGNMENT (0x400), with 0x200 dropped. + constexpr std::uint32_t kPcResourceFlagsMask = 0x5FFu; + d.Flags = static_cast(static_cast(d.Flags) & kPcResourceFlagsMask); + cleaned[i] = d; + } + descs_to_pass = cleaned.data(); + } + + using Fn = D3D12_RESOURCE_ALLOCATION_INFO*(WINAPI*)( + ID3D12Device*, D3D12_RESOURCE_ALLOCATION_INFO*, UINT, UINT, const D3D12_RESOURCE_DESC1*, + D3D12_RESOURCE_ALLOCATION_INFO1*); + reinterpret_cast(proxy->real_vtable[68])(proxy->real, ret, visible_mask, count, descs_to_pass, info1); + return reinterpret_cast(ret); +} + template std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64_t a2, std::uint64_t a3, std::uint64_t a4, std::uint64_t a5, std::uint64_t a6, std::uint64_t a7, @@ -2119,6 +2216,16 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 reinterpret_cast(a7))); } else if constexpr (Slot == kCreateCommittedResource3X) { return hr_to_u64(proxy_create_committed_resource3_x(self, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)); + } else if constexpr (Slot == 86) { + return hr_to_u64(proxy_create_pipeline_state( + self, reinterpret_cast(a1), + reinterpret_cast(a2), reinterpret_cast(a3))); + } else if constexpr (Slot == 101) { + return hr_to_u64(proxy_create_state_object( + self, reinterpret_cast(a1), + reinterpret_cast(a2), reinterpret_cast(a3))); + } else if constexpr (Slot == 108) { + return proxy_get_resource_allocation_info2(self, a1, a2, a3, a4, a5); } else { if constexpr (Slot == 26) { // The console form has the device in RDX. A PC C++ caller has the proxy in RCX and its return buffer in @@ -2386,7 +2493,7 @@ ID3D12Device* device_proxy_real_device(const void* device) { bool device_slot_overridden(unsigned slot) { if (slot <= 2 || slot == 8 || slot == 9 || slot == 14 || slot == 25 || slot == 26 || slot == 31 || slot == 32 || slot == 34 || slot == 35 || slot == 36 || slot == 45 || slot == 46 || slot == 50 || slot == 62 || slot == 67 || - slot == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X) + slot == 70 || slot == 75 || slot == 76 || slot == 133 || slot == kCreateCommittedResource3X || slot == 86 || slot == 101 || slot == 108) return true; const XboxDeviceSlotRoutes& routes = xbox_device_slot_routes(); return slot == routes.frame_interval || slot == routes.schedule_event || slot == routes.wait_event || diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index b7f020bebe..4747c04152 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -27,6 +27,14 @@ // slots 78/79/80 SetFrameIntervalX, ScheduleFrameEventX, WaitFrameEventX. A per-device token state machine answers // them before the PC vtable is indexed; WaitFrameEventX returns at once with a fresh non-zero token. // Slot 80 is outside a PC ID3D12Device11 table, so it is checked before the beyond-table refusal. +// slot 86 CreatePipelineState(desc, riid, out) -> PC ID3D12Device2::CreatePipelineState (PC slot 47). +// slot 101 CreateStateObject(desc, riid, out) -> PC ID3D12Device5::CreateStateObject (PC slot 62, displaced on +// Xbox by compute-PSO slot 62). +// slot 108 GetResourceAllocationInfo2(ret, this, visible_mask, count, descs, info1) -> PC +// ID3D12Device8::GetResourceAllocationInfo2 (PC slot 68), accepting the console C-ABI struct return +// (return buffer in RCX, device in RDX) as well as the PC C++ order, with each 56-byte +// D3D12_RESOURCE_DESC expanded to a zero-initialized D3D12_RESOURCE_DESC1 and Xbox layout words and +// flag bits normalized on the copy. // The slots 48/73/78/79/80 are the public HLE Freminet/ScarlettX's observation, not Microsoft documentation, and can // be moved with LEONIDA_XS_QUEUE_X_SLOT, LEONIDA_XS_LIST_X_SLOT, LEONIDA_XS_FRAME_INTERVAL_SLOT, // LEONIDA_XS_SCHEDULE_EVENT_SLOT and LEONIDA_XS_WAIT_EVENT_SLOT (3..127; anything else is logged once and ignored). @@ -153,7 +161,7 @@ bool set_presentation_handler(void* device, const PresentationHandler* handler); // True for slots the proxy handles itself rather than forwarding verbatim: IUnknown (0-2), 8 and 75 (queue // proxies), 9 (allocator proxies), 14, 26 (the console's struct-return form), // 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 48/73 and -// 78/79/80 (the Xbox slots above, at their configured indices), 62, 67, 70, 76 and 127. Slot 10 is intentionally false: the +// 78/79/80 (the Xbox slots above, at their configured indices), 62, 67, 70, 76, 86, 101, 108 and 127. Slot 10 is intentionally false: the // measured 9/10 handle conversion is on the heap proxy, and device slot 9 is CreateCommandAllocator, which the proxy // handles for the allocator proxy. bool device_slot_overridden(unsigned slot); diff --git a/runtime/test/device_proxy_test.cpp b/runtime/test/device_proxy_test.cpp index b3b3f0fc3e..1c68dc5da9 100644 --- a/runtime/test/device_proxy_test.cpp +++ b/runtime/test/device_proxy_test.cpp @@ -160,6 +160,9 @@ int main(int argc, char** argv) { check(rt::d3d12_xs::device_slot_overridden(14), "device slot 14 is the descriptor-heap override"); check(rt::d3d12_xs::device_slot_overridden(62), "device slot 62 is the compute-PSO override"); check(rt::d3d12_xs::device_slot_overridden(76), "device slot 76 is the resource override"); + check(rt::d3d12_xs::device_slot_overridden(86), "device slot 86 is the CreatePipelineState override"); + check(rt::d3d12_xs::device_slot_overridden(101), "device slot 101 is the CreateStateObject override"); + check(rt::d3d12_xs::device_slot_overridden(108), "device slot 108 is the GetResourceAllocationInfo2 override"); check(rt::d3d12_xs::device_slot_overridden(67), "device slot 67 is the DeserializeComputePipelineStateX override (console native-code compute pipeline, not PC CreateProtectedResourceSession1)"); @@ -516,6 +519,101 @@ int main(int argc, char** argv) { check(SUCCEEDED(hr) && pso67 != nullptr, "slot 67 creates a compute PSO via CreateComputePipelineState"); check(rt::d3d12_xs::device_slot_hits(67) == 1, "slot 67 hit counter is 1"); check(rt::d3d12_xs::device_slot_first_call_seen(67), "slot 67 first-call flag is set"); + + // Slot 86: CreatePipelineState(const D3D12_PIPELINE_STATE_STREAM_DESC*, REFIID, void**), forwarded to PC + // ID3D12Device2::CreatePipelineState (PC slot 47). + if (slots > 47) { + struct alignas(void*) ComputeStream { + alignas(void*) D3D12_PIPELINE_STATE_SUBOBJECT_TYPE root_type = + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_ROOT_SIGNATURE; + ID3D12RootSignature* root_sig = nullptr; + alignas(void*) D3D12_PIPELINE_STATE_SUBOBJECT_TYPE cs_type = D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_CS; + D3D12_SHADER_BYTECODE cs{}; + } stream; + stream.root_sig = root_signature.Get(); + stream.cs.pShaderBytecode = bytecode->GetBufferPointer(); + stream.cs.BytecodeLength = bytecode->GetBufferSize(); + D3D12_PIPELINE_STATE_STREAM_DESC stream_desc{sizeof(stream), &stream}; + using XboxCreatePipelineStateFn = + HRESULT(WINAPI*)(void*, const D3D12_PIPELINE_STATE_STREAM_DESC*, const IID*, void**); + auto create_stream_pso = reinterpret_cast(proxy_vtbl[86]); + ComPtr pso86; + hr = create_stream_pso(device.Get(), &stream_desc, &iid_pso, + reinterpret_cast(pso86.GetAddressOf())); + check(SUCCEEDED(hr) && pso86 != nullptr, + "slot 86 creates a PSO via PC ID3D12Device2::CreatePipelineState (slot 47)"); + check(rt::d3d12_xs::device_slot_hits(86) == 1, "slot 86 hit counter is 1"); + check(rt::d3d12_xs::device_slot_first_call_seen(86), "slot 86 first-call flag is set"); + } + } + + // Slot 101: CreateStateObject(const D3D12_STATE_OBJECT_DESC*, REFIID, void**), forwarded to PC + // ID3D12Device5::CreateStateObject (PC slot 62). + if (slots > 62) { + using XboxCreateStateObjectFn = HRESULT(WINAPI*)(void*, const D3D12_STATE_OBJECT_DESC*, const IID*, void**); + auto create_state_object = reinterpret_cast(proxy_vtbl[101]); + D3D12_STATE_OBJECT_DESC empty_so{}; + empty_so.Type = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE; + void* so_out = reinterpret_cast(0x1111); + hr = create_state_object(device.Get(), &empty_so, nullptr, &so_out); + check(hr == E_POINTER && so_out == nullptr, "slot 101 nulls its output for a null IID"); + check(rt::d3d12_xs::device_slot_hits(101) == 1, "slot 101 hit counter is 1"); + } + + // Slot 108: GetResourceAllocationInfo2(ret, this, visible_mask, count, descs, info1) with C-ABI struct return, + // 56-byte D3D12_RESOURCE_DESC entries, and Xbox layout/flag words, compared against direct PC + // ID3D12Device8::GetResourceAllocationInfo2 (PC slot 68). + if (slots > 68) { + ComPtr real_device8; + if (real && SUCCEEDED(real->QueryInterface(IID_PPV_ARGS(real_device8.GetAddressOf()))) && real_device8) { + D3D12_RESOURCE_DESC xbox_descs[2]{}; + xbox_descs[0].Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + xbox_descs[0].Width = 64; + xbox_descs[0].Height = 64; + xbox_descs[0].DepthOrArraySize = 1; + xbox_descs[0].MipLevels = 1; + xbox_descs[0].Format = DXGI_FORMAT_R8G8B8A8_UNORM; + xbox_descs[0].SampleDesc.Count = 1; + xbox_descs[0].Layout = static_cast(0x105); + xbox_descs[0].Flags = static_cast( + D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | 0x300000u); + xbox_descs[1].Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + xbox_descs[1].Width = 4096; + xbox_descs[1].Height = 1; + xbox_descs[1].DepthOrArraySize = 1; + xbox_descs[1].MipLevels = 1; + xbox_descs[1].SampleDesc.Count = 1; + xbox_descs[1].Layout = static_cast(0x101); + xbox_descs[1].Flags = static_cast(0x200000u); + + D3D12_RESOURCE_DESC1 pc_descs[2]{}; + std::memcpy(&pc_descs[0], &xbox_descs[0], sizeof(D3D12_RESOURCE_DESC)); + pc_descs[0].SamplerFeedbackMipRegion = {}; + pc_descs[0].Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; + pc_descs[0].Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + std::memcpy(&pc_descs[1], &xbox_descs[1], sizeof(D3D12_RESOURCE_DESC)); + pc_descs[1].SamplerFeedbackMipRegion = {}; + pc_descs[1].Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + pc_descs[1].Flags = D3D12_RESOURCE_FLAG_NONE; + + D3D12_RESOURCE_ALLOCATION_INFO1 pc_info1[2]{}; + const D3D12_RESOURCE_ALLOCATION_INFO pc_info = + real_device8->GetResourceAllocationInfo2(1, 2, pc_descs, pc_info1); + + using XboxGetAllocInfo2CAbiFn = D3D12_RESOURCE_ALLOCATION_INFO*(WINAPI*)( + D3D12_RESOURCE_ALLOCATION_INFO*, void*, UINT, UINT, const D3D12_RESOURCE_DESC*, + D3D12_RESOURCE_ALLOCATION_INFO1*); + auto get_alloc_info2 = reinterpret_cast(proxy_vtbl[108]); + D3D12_RESOURCE_ALLOCATION_INFO xbox_info{}; + D3D12_RESOURCE_ALLOCATION_INFO1 xbox_info1[2]{}; + D3D12_RESOURCE_ALLOCATION_INFO* ret_ptr = + get_alloc_info2(&xbox_info, device.Get(), 1, 2, xbox_descs, xbox_info1); + check(ret_ptr == &xbox_info && xbox_info.SizeInBytes == pc_info.SizeInBytes && + xbox_info.Alignment == pc_info.Alignment && xbox_info1[0].SizeInBytes == pc_info1[0].SizeInBytes && + xbox_info1[1].SizeInBytes == pc_info1[1].SizeInBytes, + "slot 108 C-ABI GetResourceAllocationInfo2 with 56-byte Xbox descs matches PC Device8"); + check(rt::d3d12_xs::device_slot_hits(108) == 1, "slot 108 hit counter is 1"); + } } // _COM_Outptr_ (d3d12.h): a failed create leaves NULL in the output. WARP accepts invalid heap descriptions, diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 888bedf244..6519c189f5 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -235,6 +235,27 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) { return a1; // a struct-returning method hands the buffer's address back in RAX } case 50: copy_array(m, slot, a3, a2); break; // (flags, count, objects, fence, value) + case 68: { // GetResourceAllocationInfo2 + auto* ret = reinterpret_cast(a1); + const auto count = static_cast(a3); + const auto* descs = reinterpret_cast(a4); + auto* info1 = reinterpret_cast(words[4]); + if (descs && count > 0) { + m->array[68][0] = static_cast(descs[0].Layout); + m->array[68][1] = static_cast(descs[0].Flags); + m->array[68][2] = descs[0].SamplerFeedbackMipRegion.Width | + descs[0].SamplerFeedbackMipRegion.Height | + descs[0].SamplerFeedbackMipRegion.Depth; + if (count > 1) { + const auto& last = descs[count - 1]; + m->array[68][3] = (static_cast(last.Layout) << 32) | + static_cast(last.Flags); + } + } + if (ret) *ret = {0x40000, 0x10000}; + if (info1 && count > 0) info1[0] = {0, 0x10000, 0x40000}; + return a1; + } default: break; } return S_OK; @@ -570,6 +591,35 @@ int check_levels() { xs::set_placed_at_va_handler(nullptr); } + // 86: Xbox CreatePipelineState -> PC slot 47 (ID3D12Device2) + void* pso_out = reinterpret_cast(1); + const HRESULT r86 = hr_of(raw(device, 86, 0, word(&kUnknownIid), word(&pso_out))); + if (l.level >= 2) { + EXPECT(r86 == S_OK && mock->calls[47] == 1 && mock->calls[86] == 0); + } else { + EXPECT(r86 == E_NOTIMPL && !pso_out && mock->calls[47] == 0 && mock->calls[86] == 0); + } + + // 101: Xbox CreateStateObject -> PC slot 62 (ID3D12Device5) + void* so_out = reinterpret_cast(1); + const HRESULT r101 = hr_of(raw(device, 101, 0, word(&kUnknownIid), word(&so_out))); + if (l.level >= 5) { + EXPECT(r101 == S_OK && mock->calls[62] == 1 && mock->calls[101] == 0); + } else { + EXPECT(r101 == E_NOTIMPL && !so_out && mock->calls[62] == 0 && mock->calls[101] == 0); + } + + // 108: Xbox GetResourceAllocationInfo2 -> PC slot 68 (ID3D12Device8) + D3D12_RESOURCE_ALLOCATION_INFO alloc_info{1, 1}; + const std::uint64_t r108 = raw(device, 108, word(&alloc_info), 1, 0, 0, 0); + if (l.level >= 8) { + EXPECT(r108 == word(&alloc_info) && alloc_info.SizeInBytes == 0x40000 && alloc_info.Alignment == 0x10000 && + mock->calls[68] == 1 && mock->calls[108] == 0); + } else { + EXPECT(r108 == word(&alloc_info) && alloc_info.SizeInBytes == UINT64_MAX && alloc_info.Alignment == 0 && + mock->calls[68] == 0 && mock->calls[108] == 0); + } + Mock* const queue_mock = reinterpret_cast(real_queue); Mock* const fence_mock = reinterpret_cast(real_fence); EXPECT(release(queue) == 0 && release(fence) == 0); @@ -969,7 +1019,94 @@ int check_xbox_slots() { EXPECT(hr_of(raw(device, 73, word(&desc), word(allocator), 0, word(&list_iid), 0)) == E_POINTER); EXPECT(xs::device_slot_overridden(48) && xs::device_slot_overridden(73) && xs::device_slot_overridden(78) && - xs::device_slot_overridden(79) && xs::device_slot_overridden(80)); + xs::device_slot_overridden(79) && xs::device_slot_overridden(80) && xs::device_slot_overridden(86) && + xs::device_slot_overridden(101) && xs::device_slot_overridden(108)); + + // 86 CreatePipelineState(desc, riid, out) -> PC slot 47 (ID3D12Device2::CreatePipelineState). + D3D12_PIPELINE_STATE_STREAM_DESC stream_desc{}; + const IID pso_iid = __uuidof(ID3D12PipelineState); + void* pso_stream_out = nullptr; + EXPECT(hr_of(raw(device, 86, word(&stream_desc), word(&pso_iid), word(&pso_stream_out))) == S_OK); + EXPECT(mock->calls[86] == 0 && mock->calls[47] == 1 && xs::device_slot_hits(86) == 1); + EXPECT(mock->args[47][0] == word(&stream_desc) && mock->args[47][1] == word(&pso_iid) && + mock->args[47][2] == word(&pso_stream_out)); + pso_stream_out = reinterpret_cast(1); + EXPECT(hr_of(raw(device, 86, word(&stream_desc), 0, word(&pso_stream_out))) == E_POINTER && !pso_stream_out); + + // 101 CreateStateObject(desc, riid, out) -> PC slot 62 (ID3D12Device5::CreateStateObject). + D3D12_STATE_OBJECT_DESC state_obj_desc{}; + const IID so_iid = __uuidof(ID3D12StateObject); + void* so_out = nullptr; + EXPECT(hr_of(raw(device, 101, word(&state_obj_desc), word(&so_iid), word(&so_out))) == S_OK); + EXPECT(mock->calls[101] == 0 && mock->calls[62] == 1 && xs::device_slot_hits(101) == 1); + EXPECT(mock->args[62][0] == word(&state_obj_desc) && mock->args[62][1] == word(&so_iid) && + mock->args[62][2] == word(&so_out)); + so_out = reinterpret_cast(1); + EXPECT(hr_of(raw(device, 101, word(&state_obj_desc), 0, word(&so_out))) == E_POINTER && !so_out); + + // 108 GetResourceAllocationInfo2 -> PC slot 68 (ID3D12Device8::GetResourceAllocationInfo2), called with the + // retail executable's C-ABI struct return (return buffer in RCX, device proxy in RDX), 56-byte D3D12_RESOURCE_DESC + // entries, and an Xbox layout/flag word, as well as the PC C++ argument order. + void* const* proxy_vtable = *reinterpret_cast(device); + using CAbiSlot108Fn = D3D12_RESOURCE_ALLOCATION_INFO*(WINAPI*)( + D3D12_RESOURCE_ALLOCATION_INFO*, void*, UINT, UINT, const D3D12_RESOURCE_DESC*, + D3D12_RESOURCE_ALLOCATION_INFO1*); + const auto call_slot108_c_abi = reinterpret_cast(proxy_vtable[108]); + D3D12_RESOURCE_DESC xbox_descs[2]{}; + xbox_descs[0].Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + xbox_descs[0].Width = 3840; + xbox_descs[0].Height = 2160; + xbox_descs[0].DepthOrArraySize = 1; + xbox_descs[0].MipLevels = 1; + xbox_descs[0].Format = DXGI_FORMAT_R8G8B8A8_UNORM; + xbox_descs[0].SampleDesc.Count = 1; + xbox_descs[0].Layout = static_cast(0x105); + xbox_descs[0].Flags = static_cast( + D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | 0x300000u); + xbox_descs[1].Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + xbox_descs[1].Width = 4096; + xbox_descs[1].Height = 1; + xbox_descs[1].DepthOrArraySize = 1; + xbox_descs[1].MipLevels = 1; + xbox_descs[1].SampleDesc.Count = 1; + xbox_descs[1].Layout = static_cast(0x101); + xbox_descs[1].Flags = static_cast(D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | 0x200000u); + D3D12_RESOURCE_ALLOCATION_INFO ret_info{}; + D3D12_RESOURCE_ALLOCATION_INFO1 detail_info[2]{}; + EXPECT(call_slot108_c_abi(&ret_info, device, 1, 2, xbox_descs, detail_info) == &ret_info); + EXPECT(ret_info.SizeInBytes == 0x40000 && ret_info.Alignment == 0x10000); + EXPECT(detail_info[0].SizeInBytes == 0x40000 && detail_info[0].Alignment == 0x10000); + EXPECT(mock->calls[108] == 0 && mock->calls[68] == 1 && xs::device_slot_hits(108) == 1); + EXPECT(mock->array[68][0] == static_cast(D3D12_TEXTURE_LAYOUT_UNKNOWN)); + EXPECT(mock->array[68][1] == static_cast( + D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)); + EXPECT(mock->array[68][2] == 0); + EXPECT(mock->array[68][3] == ((static_cast(D3D12_TEXTURE_LAYOUT_ROW_MAJOR) << 32) | + static_cast(D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))); + + D3D12_RESOURCE_DESC xbox_buf_desc = xbox_descs[1]; + xbox_buf_desc.Flags = static_cast(0x200000u); + D3D12_RESOURCE_ALLOCATION_INFO buf_ret{}; + EXPECT(raw(device, 108, word(&buf_ret), 1, 1, word(&xbox_buf_desc), 0) == word(&buf_ret)); + EXPECT(buf_ret.SizeInBytes == 0x40000 && mock->calls[68] == 2 && xs::device_slot_hits(108) == 2); + EXPECT(mock->array[68][0] == static_cast(D3D12_TEXTURE_LAYOUT_ROW_MAJOR)); + EXPECT(mock->array[68][1] == static_cast(D3D12_RESOURCE_FLAG_NONE)); + + D3D12_RESOURCE_DESC batch1025[1025]{}; + for (int i = 0; i < 1024; ++i) batch1025[i] = xbox_descs[0]; + batch1025[1024] = xbox_descs[1]; + D3D12_RESOURCE_ALLOCATION_INFO batch_ret{}; + EXPECT(call_slot108_c_abi(&batch_ret, device, 1, 1025, batch1025, nullptr) == &batch_ret); + EXPECT(batch_ret.SizeInBytes == 0x40000 && mock->calls[68] == 3 && xs::device_slot_hits(108) == 3); + EXPECT(mock->array[68][0] == static_cast(D3D12_TEXTURE_LAYOUT_UNKNOWN)); + EXPECT(mock->array[68][3] == ((static_cast(D3D12_TEXTURE_LAYOUT_ROW_MAJOR) << 32) | + static_cast(D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))); + + // A count with a null descriptor array is refused with {UINT64_MAX, 0} and never reaches PC slot 68. + D3D12_RESOURCE_ALLOCATION_INFO null_ret{}; + EXPECT(call_slot108_c_abi(&null_ret, device, 1, 2, nullptr, nullptr) == &null_ret); + EXPECT(null_ret.SizeInBytes == UINT64_MAX && null_ret.Alignment == 0); + EXPECT(mock->calls[68] == 3 && xs::device_slot_hits(108) == 4); EXPECT(release(allocator) == 0 && real_allocator->refs == 0); EXPECT(release(queue) == 0); From ac73a84127ee49b5ecf9702bed16603b34b7f7ec Mon Sep 17 00:00:00 2001 From: DesertGamer Date: Tue, 24 Nov 2026 18:45:06 +0400 Subject: [PATCH 2/2] d3d12_xs: configured Xbox device routes take precedence over the fixed slot 86, 101 and 108 handlers The fixed handlers for slots 86, 101 and 108 were matched before the generic branch, which is where xbox_device_slot_routes() is read, so a route that LEONIDA_XS_FRAME_STATISTICS_SLOT or another LEONIDA_XS_*_SLOT variable put on one of those slots never ran. After the rebase onto main, xs_proxy_mock_env_slots failed for that reason: slot 101 reached CreateStateObject instead of GetFrameStatisticsX. The three handlers now sit after the route checks and before the refusal for slots beyond the PC device table, and their own note_slot calls are gone because the generic branch already counts the slot once, so the hit counters stay at 1. --- runtime/shims/d3d12_xs_device.cpp | 43 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index f282ddbfeb..4d128d5d5b 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -2054,7 +2054,6 @@ HRESULT proxy_create_committed_resource3_x(DeviceProxy* self, std::uint64_t heap // Xbox slot 86: CreatePipelineState(desc, riid, out), forwarded to PC ID3D12Device2::CreatePipelineState (PC slot 47). HRESULT proxy_create_pipeline_state(DeviceProxy* self, const D3D12_PIPELINE_STATE_STREAM_DESC* desc, const IID* riid, void** out) { - note_slot(86); if (out) *out = nullptr; if (self->slot_count <= 47) return refuse_beyond_device(86, self->slot_count); // predates ID3D12Device2 if (!riid) return E_POINTER; @@ -2065,7 +2064,6 @@ HRESULT proxy_create_pipeline_state(DeviceProxy* self, const D3D12_PIPELINE_STAT // Xbox slot 101: CreateStateObject(desc, riid, out), forwarded to PC ID3D12Device5::CreateStateObject (PC slot 62, // displaced on Xbox by compute-PSO slot 62). HRESULT proxy_create_state_object(DeviceProxy* self, const D3D12_STATE_OBJECT_DESC* desc, const IID* riid, void** out) { - note_slot(101); if (out) *out = nullptr; if (self->slot_count <= 62) return refuse_beyond_device(101, self->slot_count); // predates ID3D12Device5 if (!riid) return E_POINTER; @@ -2082,7 +2080,6 @@ HRESULT proxy_create_state_object(DeviceProxy* self, const D3D12_STATE_OBJECT_DE // D3D12_RESOURCE_FLAGS are normalized on the copy before the PC call. std::uint64_t proxy_get_resource_allocation_info2(DeviceProxy* self, std::uint64_t a1, std::uint64_t a2, std::uint64_t a3, std::uint64_t a4, std::uint64_t a5) { - note_slot(108); DeviceProxy* proxy = nullptr; D3D12_RESOURCE_ALLOCATION_INFO* ret = nullptr; if (is_device_proxy(self)) { @@ -2216,16 +2213,6 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 reinterpret_cast(a7))); } else if constexpr (Slot == kCreateCommittedResource3X) { return hr_to_u64(proxy_create_committed_resource3_x(self, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)); - } else if constexpr (Slot == 86) { - return hr_to_u64(proxy_create_pipeline_state( - self, reinterpret_cast(a1), - reinterpret_cast(a2), reinterpret_cast(a3))); - } else if constexpr (Slot == 101) { - return hr_to_u64(proxy_create_state_object( - self, reinterpret_cast(a1), - reinterpret_cast(a2), reinterpret_cast(a3))); - } else if constexpr (Slot == 108) { - return proxy_get_resource_allocation_info2(self, a1, a2, a3, a4, a5); } else { if constexpr (Slot == 26) { // The console form has the device in RDX. A PC C++ caller has the proxy in RCX and its return buffer in @@ -2279,16 +2266,28 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 return hr_to_u64(proxy_create_command_list_x(self, a1, a2, a3, reinterpret_cast(a4), reinterpret_cast(a5))); } - if (Slot >= self->slot_count) return hr_to_u64(refuse_beyond_device(Slot, self->slot_count)); + if constexpr (Slot == 86) { + return hr_to_u64(proxy_create_pipeline_state( + self, reinterpret_cast(a1), + reinterpret_cast(a2), reinterpret_cast(a3))); + } else if constexpr (Slot == 101) { + return hr_to_u64(proxy_create_state_object( + self, reinterpret_cast(a1), + reinterpret_cast(a2), reinterpret_cast(a3))); + } else if constexpr (Slot == 108) { + return proxy_get_resource_allocation_info2(self, a1, a2, a3, a4, a5); + } else { + if (Slot >= self->slot_count) return hr_to_u64(refuse_beyond_device(Slot, self->slot_count)); - // The PC vtable entry, called with the PC device as `this`. Integer/pointer arguments, the hidden - // struct-return pointer and any stack arguments keep their positions; XMM registers are untouched. - using ForwardFn = std::uint64_t(WINAPI*)(void*, std::uint64_t, std::uint64_t, std::uint64_t, - std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, - std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, - std::uint64_t, std::uint64_t, std::uint64_t); - auto fn = reinterpret_cast(self->real_vtable[Slot]); - return fn(self->real, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14); + // The PC vtable entry, called with the PC device as `this`. Integer/pointer arguments, the hidden + // struct-return pointer and any stack arguments keep their positions; XMM registers are untouched. + using ForwardFn = std::uint64_t(WINAPI*)(void*, std::uint64_t, std::uint64_t, std::uint64_t, + std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, + std::uint64_t, std::uint64_t, std::uint64_t, std::uint64_t, + std::uint64_t, std::uint64_t, std::uint64_t); + auto fn = reinterpret_cast(self->real_vtable[Slot]); + return fn(self->real, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14); + } } }