From 47e50b587e3e08ceb9ec6ef62487770a8a004e45 Mon Sep 17 00:00:00 2001 From: Ryx <118130241+Ryx69@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:35:59 +0100 Subject: [PATCH 1/2] d3d12_xs: a shader-visible view heap past the PC limit is made at the limit The retail executable asks device slot 14 for a shader-visible CBV/SRV/UAV heap of 1,245,184 descriptors. A PC device refuses more than MaxViewDescriptorHeapSize (D3D12_OPTIONS19; 1,000,000 below resource binding tier 3, which is what an RTX 5060 Ti reports) with E_INVALIDARG, and the title then reads the null heap. The proxy now makes such a heap at the device's limit (logged once) and logs a refused heap with its description. xs_proxy_mock_test checks the capped, the CPU-only and the in-limit heap; the capped case fails without this change. AI-assisted. --- runtime/shims/d3d12_xs_device.cpp | 33 ++++++++++++++++++++++++++++- runtime/test/xs_proxy_mock_test.cpp | 30 +++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index e17a80353b1..cdc18946d69 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1342,9 +1342,40 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H // No output: a capability check, answered by the device (S_FALSE, as on the PC). if (!out) return self->real->CreateDescriptorHeap(desc, *riid, nullptr); + // The console takes larger shader-visible CBV/SRV/UAV heaps than a PC device: the retail executable asks for + // 1,245,184 descriptors, and the PC runtime refuses a heap over the device's MaxViewDescriptorHeapSize + // (D3D12_OPTIONS19; 1,000,000 below resource binding tier 3, and an RTX 5060 Ti reports no more) with + // E_INVALIDARG; the title then reads the null heap. ASSUMPTION: the title does not use descriptors past the PC + // limit before it renders, so the heap is made at the limit rather than refused. Logged once. + D3D12_DESCRIPTOR_HEAP_DESC capped{}; + if (desc && desc->Type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV && + (desc->Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)) { + UINT limit = D3D12_MAX_SHADER_VISIBLE_DESCRIPTOR_HEAP_SIZE_TIER_2; + D3D12_FEATURE_DATA_D3D12_OPTIONS19 options19{}; + if (SUCCEEDED(self->real->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS19, &options19, sizeof(options19))) && + options19.MaxViewDescriptorHeapSize > limit) + limit = options19.MaxViewDescriptorHeapSize; + if (desc->NumDescriptors > limit) { + static std::atomic logged{false}; + if (!logged.exchange(true, std::memory_order_relaxed)) + std::fprintf(stderr, "[rt] d3d12_xs: slot 14 shader-visible CBV/SRV/UAV heap of %u descriptors capped " + "to the device limit %u (logged once)\n", desc->NumDescriptors, limit); + capped = *desc; + capped.NumDescriptors = limit; + desc = &capped; + } + } void* created = nullptr; HRESULT hr = self->real->CreateDescriptorHeap(desc, __uuidof(ID3D12DescriptorHeap), &created); - if (FAILED(hr) || !created) return FAILED(hr) ? hr : E_FAIL; + if (FAILED(hr) || !created) { + static std::atomic refused_logs{0}; + if (desc && FAILED(hr) && refused_logs.fetch_add(1, std::memory_order_relaxed) < 8) + std::fprintf(stderr, "[rt] d3d12_xs: slot 14 CreateDescriptorHeap refused: type=%u num=%u flags=0x%x " + "node=0x%x -> 0x%08lx\n", + static_cast(desc->Type), desc->NumDescriptors, static_cast(desc->Flags), + desc->NodeMask, static_cast(hr)); + return FAILED(hr) ? hr : E_FAIL; + } ID3D12DescriptorHeap* heap = static_cast(created); hr = create_heap_proxy(heap, self, *riid, out); diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index f0030116bd7..93219c958b6 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -197,6 +197,9 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) { case 34: case 35: case 46: copy_array(m, slot, a2, a1); break; // (count, objects, ...) case 45: copy_array(m, slot, a1, a3); break; // (fences, values, count, ...) case 50: copy_array(m, slot, a3, a2); break; // (flags, count, objects, fence, value) + case 14: // CreateDescriptorHeap(desc, riid, out) + if (a1) m->array[14][0] = reinterpret_cast(a1)->NumDescriptors; + break; default: break; } return S_OK; @@ -938,6 +941,31 @@ static void no_dialogs() { SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); } +// Slot 14: a shader-visible CBV/SRV/UAV heap past the PC device's limit (the mock reports none, so 1,000,000) is +// made at the limit; a CPU-only heap of the same size is passed as asked. +int check_view_heap_cap() { + Mock* mock = nullptr; + void* device = make_device(mock, 0, false); + EXPECT(device); + D3D12_DESCRIPTOR_HEAP_DESC heap{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1245184, + D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; + const IID heap_iid = __uuidof(ID3D12DescriptorHeap); + void* made = nullptr; + raw(device, 14, word(&heap), word(&heap_iid), word(&made)); + EXPECT(mock->calls[14] == 1 && mock->array[14][0] == D3D12_MAX_SHADER_VISIBLE_DESCRIPTOR_HEAP_SIZE_TIER_2); + EXPECT(heap.NumDescriptors == 1245184); // the title's description is left as it was + heap.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; + raw(device, 14, word(&heap), word(&heap_iid), word(&made)); + EXPECT(mock->calls[14] == 2 && mock->array[14][0] == 1245184); + heap = {D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4096, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; + raw(device, 14, word(&heap), word(&heap_iid), word(&made)); + EXPECT(mock->calls[14] == 3 && mock->array[14][0] == 4096); // within the limit: unchanged + EXPECT(release(device) == 0); + std::printf("%s: a shader-visible view heap past the PC limit is made at the limit; others pass as asked\n", + g_label); + return 0; +} + int main(int argc, char** argv) { no_dialogs(); std::setvbuf(stdout, nullptr, _IONBF, 0); @@ -948,7 +976,7 @@ int main(int argc, char** argv) { } if (argc != 1) return 2; if (check_tail(true) || check_levels() || check_fence1() || check_create_failures() || check_open_shared() || - check_alloc_failures() || check_allocators() || check_xbox_slots()) + check_alloc_failures() || check_allocators() || check_xbox_slots() || check_view_heap_cap()) return 1; std::printf("xs_proxy_mock_test: PASS (%d allocations refused on purpose)\n", g_failed_news); return 0; From 50e53f0b20ddbf51ca29ca434b6cca1c4b9b604b Mon Sep 17 00:00:00 2001 From: Ryx <118130241+Ryx69@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:46:17 +0100 Subject: [PATCH 2/2] d3d12_xs: cap the view heap only after the device refuses the full size Review (Envyre, #7821): the Vulkan device makes a shader-visible view heap of any size but reports MaxViewDescriptorHeapSize = 1,000,000, so capping before the call shrank a heap it would have made, and descriptor writes past the cap land outside the heap there. The proxy now asks for the full size first and makes the heap at the limit only when the device refuses it with E_INVALIDARG, as native D3D12 does. The limit is OPTIONS19's when the device reports one (1,000,000 measured at tier 3 too), else 1,000,000. xs_proxy_mock_test: the mock device can refuse past a limit; a device without one keeps the full heap in one request, one with it gets the request at the limit after the refusal, and CPU-only or in-limit heaps are passed once. AI-assisted. --- runtime/shims/d3d12_xs_device.cpp | 26 +++++++++-------- runtime/test/xs_proxy_mock_test.cpp | 44 +++++++++++++++++++++-------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index cdc18946d69..4e8b4366cf7 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -1342,31 +1342,35 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H // No output: a capability check, answered by the device (S_FALSE, as on the PC). if (!out) return self->real->CreateDescriptorHeap(desc, *riid, nullptr); - // The console takes larger shader-visible CBV/SRV/UAV heaps than a PC device: the retail executable asks for - // 1,245,184 descriptors, and the PC runtime refuses a heap over the device's MaxViewDescriptorHeapSize - // (D3D12_OPTIONS19; 1,000,000 below resource binding tier 3, and an RTX 5060 Ti reports no more) with - // E_INVALIDARG; the title then reads the null heap. ASSUMPTION: the title does not use descriptors past the PC - // limit before it renders, so the heap is made at the limit rather than refused. Logged once. + void* created = nullptr; + HRESULT hr = self->real->CreateDescriptorHeap(desc, __uuidof(ID3D12DescriptorHeap), &created); + + // The console takes larger shader-visible CBV/SRV/UAV heaps than a native PC device: the retail executable asks + // for 1,245,184 descriptors, and the native runtime refuses a heap over MaxViewDescriptorHeapSize (D3D12_OPTIONS19; + // 1,000,000 on an RTX 5060 Ti and, measured by Envyre in #7821, on a tier 3 RTX 4070 Ti SUPER) with E_INVALIDARG; + // the title then reads the null heap. Only then is the heap made again at that limit: a device that makes the + // full heap (the Vulkan device has no such limit) keeps it, since a write past a cap would land outside the heap. + // ASSUMPTION: on a native device the title does not use descriptors past the limit before it renders. Logged once. D3D12_DESCRIPTOR_HEAP_DESC capped{}; - if (desc && desc->Type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV && + if (hr == E_INVALIDARG && desc && desc->Type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV && (desc->Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE)) { UINT limit = D3D12_MAX_SHADER_VISIBLE_DESCRIPTOR_HEAP_SIZE_TIER_2; D3D12_FEATURE_DATA_D3D12_OPTIONS19 options19{}; if (SUCCEEDED(self->real->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS19, &options19, sizeof(options19))) && - options19.MaxViewDescriptorHeapSize > limit) + options19.MaxViewDescriptorHeapSize != 0) limit = options19.MaxViewDescriptorHeapSize; if (desc->NumDescriptors > limit) { static std::atomic logged{false}; if (!logged.exchange(true, std::memory_order_relaxed)) - std::fprintf(stderr, "[rt] d3d12_xs: slot 14 shader-visible CBV/SRV/UAV heap of %u descriptors capped " - "to the device limit %u (logged once)\n", desc->NumDescriptors, limit); + std::fprintf(stderr, "[rt] d3d12_xs: slot 14 shader-visible CBV/SRV/UAV heap of %u descriptors refused; " + "made at the device limit %u (logged once)\n", desc->NumDescriptors, limit); capped = *desc; capped.NumDescriptors = limit; desc = &capped; + created = nullptr; + hr = self->real->CreateDescriptorHeap(desc, __uuidof(ID3D12DescriptorHeap), &created); } } - void* created = nullptr; - HRESULT hr = self->real->CreateDescriptorHeap(desc, __uuidof(ID3D12DescriptorHeap), &created); if (FAILED(hr) || !created) { static std::atomic refused_logs{0}; if (desc && FAILED(hr) && refused_logs.fetch_add(1, std::memory_order_relaxed) < 8) diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 93219c958b6..bd38d923142 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -87,6 +87,7 @@ struct Mock { bool allocator_tag; // allocator: answers kLeonidaCommandAllocatorTag; device: the allocators it makes do bool create_null; // device: creation returns create_result with no object HRESULT create_result; // device: what creation returns (S_OK makes the object) + unsigned view_heap_limit; // device: refuses a larger shader-visible CBV/SRV/UAV heap (0: no limit), as native D3D12 Mock* last_child; // device: the object its last creation or open made unsigned calls[kMockSlots]; std::uint64_t args[kMockSlots][5]; @@ -198,7 +199,12 @@ std::uint64_t mock_call(Mock* m, unsigned slot, const std::uint64_t* words) { case 45: copy_array(m, slot, a1, a3); break; // (fences, values, count, ...) case 50: copy_array(m, slot, a3, a2); break; // (flags, count, objects, fence, value) case 14: // CreateDescriptorHeap(desc, riid, out) - if (a1) m->array[14][0] = reinterpret_cast(a1)->NumDescriptors; + if (const auto* d = reinterpret_cast(a1)) { + m->array[14][m->calls[14] > 1 ? 1 : 0] = d->NumDescriptors; // first and latest request + if (m->view_heap_limit && d->Type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV && + (d->Flags & D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE) && d->NumDescriptors > m->view_heap_limit) + return static_cast(E_INVALIDARG); + } break; default: break; } @@ -941,28 +947,42 @@ static void no_dialogs() { SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); } -// Slot 14: a shader-visible CBV/SRV/UAV heap past the PC device's limit (the mock reports none, so 1,000,000) is -// made at the limit; a CPU-only heap of the same size is passed as asked. +// Slot 14: the title's 1,245,184-descriptor shader-visible CBV/SRV/UAV heap. A device that makes it (the Vulkan +// device has no limit) keeps the full size. A device that refuses it, as native D3D12 does past +// MaxViewDescriptorHeapSize (the mock reports none, so 1,000,000), gets a second request at that limit. CPU-only +// heaps and heaps within the limit are passed through once, as asked. int check_view_heap_cap() { + const IID heap_iid = __uuidof(ID3D12DescriptorHeap); + void* made = nullptr; + D3D12_DESCRIPTOR_HEAP_DESC heap{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1245184, + D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; + { + Mock* mock = nullptr; + void* device = make_device(mock, 0, false); // no view-heap limit + EXPECT(device); + raw(device, 14, word(&heap), word(&heap_iid), word(&made)); + EXPECT(mock->calls[14] == 1 && mock->array[14][0] == 1245184); // not capped when the device takes it + EXPECT(release(device) == 0); + } Mock* mock = nullptr; void* device = make_device(mock, 0, false); EXPECT(device); - D3D12_DESCRIPTOR_HEAP_DESC heap{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1245184, - D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; - const IID heap_iid = __uuidof(ID3D12DescriptorHeap); - void* made = nullptr; + mock->view_heap_limit = D3D12_MAX_SHADER_VISIBLE_DESCRIPTOR_HEAP_SIZE_TIER_2; raw(device, 14, word(&heap), word(&heap_iid), word(&made)); - EXPECT(mock->calls[14] == 1 && mock->array[14][0] == D3D12_MAX_SHADER_VISIBLE_DESCRIPTOR_HEAP_SIZE_TIER_2); + EXPECT(mock->calls[14] == 2 && mock->array[14][0] == 1245184 && + mock->array[14][1] == D3D12_MAX_SHADER_VISIBLE_DESCRIPTOR_HEAP_SIZE_TIER_2); // refused, then at the limit EXPECT(heap.NumDescriptors == 1245184); // the title's description is left as it was + mock->calls[14] = 0; heap.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; raw(device, 14, word(&heap), word(&heap_iid), word(&made)); - EXPECT(mock->calls[14] == 2 && mock->array[14][0] == 1245184); + EXPECT(mock->calls[14] == 1 && mock->array[14][0] == 1245184); // CPU-only: no limit, one request + mock->calls[14] = 0; heap = {D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4096, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; raw(device, 14, word(&heap), word(&heap_iid), word(&made)); - EXPECT(mock->calls[14] == 3 && mock->array[14][0] == 4096); // within the limit: unchanged + EXPECT(mock->calls[14] == 1 && mock->array[14][0] == 4096); // within the limit: one request, unchanged EXPECT(release(device) == 0); - std::printf("%s: a shader-visible view heap past the PC limit is made at the limit; others pass as asked\n", - g_label); + std::printf("%s: a view heap the device refuses past its limit is made again at the limit; one it takes is kept " + "at full size\n", g_label); return 0; }