Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,42 @@ HRESULT proxy_create_descriptor_heap(DeviceProxy* self, const D3D12_DESCRIPTOR_H

void* created = nullptr;
HRESULT hr = self->real->CreateDescriptorHeap(desc, __uuidof(ID3D12DescriptorHeap), &created);
if (FAILED(hr) || !created) return FAILED(hr) ? hr : E_FAIL;

// 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 (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 != 0)
limit = options19.MaxViewDescriptorHeapSize;
if (desc->NumDescriptors > limit) {
static std::atomic<bool> 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 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);
}
}
if (FAILED(hr) || !created) {
static std::atomic<int> 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<unsigned>(desc->Type), desc->NumDescriptors, static_cast<unsigned>(desc->Flags),
desc->NodeMask, static_cast<unsigned long>(hr));
return FAILED(hr) ? hr : E_FAIL;
}

ID3D12DescriptorHeap* heap = static_cast<ID3D12DescriptorHeap*>(created);
hr = create_heap_proxy(heap, self, *riid, out);
Expand Down
50 changes: 49 additions & 1 deletion runtime/test/xs_proxy_mock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -197,6 +198,14 @@ 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 (const auto* d = reinterpret_cast<const D3D12_DESCRIPTOR_HEAP_DESC*>(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<std::uint32_t>(E_INVALIDARG);
}
break;
default: break;
}
return S_OK;
Expand Down Expand Up @@ -938,6 +947,45 @@ static void no_dialogs() {
SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
}

// 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);
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] == 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] == 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] == 1 && mock->array[14][0] == 4096); // within the limit: one request, unchanged
EXPECT(release(device) == 0);
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;
}

int main(int argc, char** argv) {
no_dialogs();
std::setvbuf(stdout, nullptr, _IONBF, 0);
Expand All @@ -948,7 +996,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;
Expand Down
Loading