From 6d66718f3efbf799fe2d2e8a853702b72368e832 Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:21:56 -0600 Subject: [PATCH] d3d12: title memory past the window cap names a zeroed scratch window (RT-TITLE-WINDOW-OVERFLOW) Past LEONIDA_TITLE_WINDOWS_MB, or with no arena room for a window, resolve_title_memory handed the GPU the title's raw VA. That names nothing of the device's, and one read of it lost the device at the world start (#1537). Reserve 16 MiB + 64 KiB of zero-filled arena with the first window and point such addresses into it instead: the cost is a wrong result, not the device. The raw VA is kept only when even the scratch did not fit (logged once). d3d12_title_window_cap runs d3d12_title_va_test --window-cap with a cap of 0: two views of the title's memory name the arena, share the scratch, and a dispatch through one reads zeros. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 6 +- runtime/CMakeLists.txt | 3 + runtime/shims/d3d12_vulkan_device.cpp | 42 ++++++++++-- runtime/test/d3d12_title_va_test.cpp | 99 +++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 6 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index e23985ecb3..0593533906 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1617,7 +1617,11 @@ The copies: - Any **other address in the title's memory** gets a window: the 64 KiB-aligned pages it covers, clipped to the title's range, copied into the arena now and before every submission, through the same shadow refresh as slot 133 (so dirty-page tracking for that refresh applies to both). Windows stay until the device goes, up to - `LEONIDA_TITLE_WINDOWS_MB` (64 MiB by default). A window that + `LEONIDA_TITLE_WINDOWS_MB` (64 MiB by default). Past the cap, or with no room for a window, the address names the + **scratch window** instead: 16 MiB + 64 KiB of zero-filled arena reserved with the first window, shared by every + such address and never refreshed, so a shader reads zeros (or what another such shader wrote) and the run goes on + with a wrong result. The title's raw VA names nothing of the device's: on an RTX 5080 one read of it lost the + device at the world start (#1537). Without arena room for the scratch the raw VA is kept (logged once). A window that cannot fit between the title's address reservations can use a separate mapped buffer. The buffer's device address must fit as a positive 47-bit offset from the arena base. The same window cap and dirty-page refresh apply. `LEONIDA_STREAM_BUFFER_WINDOWS=0` requires the window to fit in the arena. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 2763e9d1b0..f4ccf90fae 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3431,6 +3431,9 @@ if(TARGET Vulkan::Headers) add_test(NAME d3d12_geometry_views COMMAND d3d12_title_va_test) set_tests_properties(d3d12_geometry_views PROPERTIES ENVIRONMENT "LEONIDA_GEOMETRY_MB=16" SKIP_RETURN_CODE 77) set_tests_properties(d3d12_title_va PROPERTIES SKIP_RETURN_CODE 77) + # Past LEONIDA_TITLE_WINDOWS_MB a view names the zeroed scratch window, not the title's raw VA (#1537). + add_test(NAME d3d12_title_window_cap COMMAND d3d12_title_va_test --window-cap) + set_tests_properties(d3d12_title_window_cap PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_TITLE_WINDOWS_MB=0") add_test(NAME d3d12_api_descriptor_copy COMMAND d3d12_title_va_test --api-descriptor-copy) set_tests_properties(d3d12_api_descriptor_copy PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_DESC_MIRROR=1") add_test(NAME d3d12_api_descriptor_copy_off COMMAND d3d12_title_va_test --api-descriptor-copy-off) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 12494d201d..c05d791e39 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -2072,7 +2072,9 @@ struct DeviceState { // slot-133 buffer (a per-frame constant ring, #8862 section 3.5) gets a copy of the 64 KiB-aligned pages it // covers, refreshed before every submission like a slot-133 shadow, and the address of that copy. Windows are // kept until the device goes (the ring comes round to the same pages), up to LEONIDA_TITLE_WINDOWS_MB (64 MiB - // by default); past that, or with the arena full, the address is left as it was and counted. + // by default). Past that, or with the arena full, the address names the scratch window instead: zero-filled + // arena pages reserved with the first window, shared by every such address. The title's raw VA would name + // nothing of the device's, and one read of it loses the device (#1537); the scratch costs a wrong result. struct TitleWindow { std::uint64_t start; std::uint64_t shader_va; @@ -2085,6 +2087,15 @@ struct DeviceState { std::atomic title_window_count{0}; // title_windows.size(), read without the mutex std::uint64_t title_window_bytes = 0; std::uint64_t title_window_cap = 64ull << 20; + static constexpr std::uint64_t kTitleScratchBytes = kTitleWindowMax + kTitleWindowAlign; // any window's size + std::uint64_t title_scratch = UINT64_MAX; // arena offset of the scratch window; UINT64_MAX: none yet + bool title_scratch_tried = false; + // Under title_window_mutex. The scratch window for `va`, whose window would start at `start`; false: no scratch. + bool title_scratch_va(std::uint64_t va, std::uint64_t start, std::uint64_t& out) { + if (title_scratch == UINT64_MAX) return false; + out = title_scratch + (va - start); + return true; + } rt::d3d12::GeometryViews geometry; static rt::d3d12::GeometryCopy copy_geometry(std::uint8_t* to, const std::uint8_t* from, std::size_t size) { @@ -2152,10 +2163,28 @@ struct DeviceState { end = std::min(end, hi); if (end <= va) return true; const std::uint64_t size = end - start; + if (!title_scratch_tried) { + // Reserved with the first window, while the arena has room; never refreshed (no title pages behind it). + title_scratch_tried = true; + VkDeviceSize scratch = 0; + if (arena_allocate(kTitleScratchBytes, kTitleWindowAlign, scratch, arena_buffer)) { + std::memset(arena.mapped + scratch, 0, std::size_t(kTitleScratchBytes)); + title_scratch = scratch; + } else { + log_once("GPU address arena full: no scratch window for title memory past the window cap; KiB:", + int(kTitleScratchBytes >> 10)); + } + } if (title_window_bytes + size > title_window_cap) { - log_once("title memory named by a shader address is past LEONIDA_TITLE_WINDOWS_MB, left untranslated; " - "MiB copied so far:", - int(title_window_bytes >> 20)); + if (title_scratch_va(va, start, out)) { + log_once("title memory named by a shader address is past LEONIDA_TITLE_WINDOWS_MB, given the zeroed " + "scratch window (raise LEONIDA_TITLE_WINDOWS_MB); MiB copied so far:", + int(title_window_bytes >> 20)); + } else { + log_once("title memory named by a shader address is past LEONIDA_TITLE_WINDOWS_MB, left untranslated; " + "MiB copied so far:", + int(title_window_bytes >> 20)); + } return true; } VkDeviceSize offset = 0; @@ -2163,7 +2192,10 @@ struct DeviceState { if (arena_allocate(size, kTitleWindowAlign, offset, arena_buffer)) { mapped = arena.mapped + offset; } else if (!stream_windows || !external_windows.allocate(*this, size, offset, mapped)) { - log_once("GPU address arena full (raise LEONIDA_ARENA_MB): title memory left untranslated; window KiB:", + log_once(title_scratch_va(va, start, out) + ? "GPU address arena full (raise LEONIDA_ARENA_MB): title memory given the zeroed scratch " + "window; window KiB:" + : "GPU address arena full (raise LEONIDA_ARENA_MB): title memory left untranslated; window KiB:", int(std::min(size >> 10, INT_MAX))); return true; } diff --git a/runtime/test/d3d12_title_va_test.cpp b/runtime/test/d3d12_title_va_test.cpp index 0adb72d255..383ec9e5fc 100644 --- a/runtime/test/d3d12_title_va_test.cpp +++ b/runtime/test/d3d12_title_va_test.cpp @@ -310,6 +310,88 @@ static std::uintptr_t source_tile, peer_tile; void aliases_of_test_tile(std::uintptr_t va, std::vector& peers) { if (va == source_tile) peers.push_back(peer_tile); } +// LEONIDA_TITLE_WINDOWS_MB=0: no window fits the cap. A view of the title's memory then names the zeroed scratch +// window in the arena, not the title's raw VA (one GPU read of that loses the device, #1537): two views of +// different pages share it, and a dispatch through one reads zeros, not the title's bytes. +int check_window_overflow(Context& c) { + constexpr UINT64 ring_bytes = 256 * 1024; + auto* ring = static_cast(VirtualAlloc(nullptr, ring_bytes, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); + EXPECT(ring); + const auto ring_va = reinterpret_cast(ring); + EXPECT(ring_va >= c.arena && !(ring_va & 0xFFFF)); + for (UINT i = 0; i < ring_bytes / 4; ++i) ring[i] = 0xC0000000u + i * 7u; // never zero + auto& aliases = leonida::title_va_aliases(); + aliases.add_title_memory(ring_va, ring_bytes); + + D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 8, + D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; + Ref heap; + EXPECT(SUCCEEDED(c.device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap.p)))); + const std::uint64_t cpu = reinterpret_cast(slot(heap.p, 9))(heap.p); + const std::uint64_t gpu = reinterpret_cast(slot(heap.p, 10))(heap.p); + const UINT inc = c.device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + const D3D12_CONSTANT_BUFFER_VIEW_DESC first{ring_va + 0x2000, 256}, second{ring_va + 0x30000, 256}; + c.device->CreateConstantBufferView(&first, D3D12_CPU_DESCRIPTOR_HANDLE{cpu}); + c.device->CreateConstantBufferView(&second, D3D12_CPU_DESCRIPTOR_HANDLE{cpu + inc}); + const auto a = vsharp_base(cpu), b = vsharp_base(cpu + inc); + std::printf("window cap 0: views of the title's memory named %s and %s\n", + a == first.BufferLocation ? "the raw VA" : a < c.arena ? "the arena" : "elsewhere", + b == second.BufferLocation ? "the raw VA" : b < c.arena ? "the arena" : "elsewhere"); + EXPECT(a != first.BufferLocation && a < c.arena); + EXPECT(b != second.BufferLocation && b < c.arena); + EXPECT(a == b + 0x2000); // one scratch window, each address at its offset into its 64 KiB page + EXPECT(aliases.shader_va(first.BufferLocation) == first.BufferLocation); // no window was made + + const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}, readback_heap{D3D12_HEAP_TYPE_READBACK}; + const auto uav = buffer_desc(kBytes, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); + const auto readback_desc = buffer_desc(kBytes); + Ref out, readback; + EXPECT(SUCCEEDED(c.device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &uav, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr, + IID_PPV_ARGS(&out.p)))); + EXPECT(SUCCEEDED(c.device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &readback_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&readback.p)))); + Ref allocator; + Ref list; + EXPECT(SUCCEEDED(c.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); + EXPECT(SUCCEEDED(c.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, c.pso, + IID_PPV_ARGS(&list.p)))); + ID3D12DescriptorHeap* heaps[] = {heap.p}; + list->SetDescriptorHeaps(1, heaps); + list->SetComputeRootSignature(c.root); + list->SetComputeRoot32BitConstant(0, 0, 0); // the CBV mode of check_title_memory: its first four words + list->SetComputeRoot32BitConstant(0, 0, 1); + list->SetComputeRootUnorderedAccessView(1, out->GetGPUVirtualAddress()); + list->SetComputeRootDescriptorTable(2, D3D12_GPU_DESCRIPTOR_HANDLE{gpu}); + list->Dispatch(1, 1, 1); + D3D12_RESOURCE_BARRIER to_copy{}; + to_copy.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; + to_copy.Transition.pResource = out.p; + to_copy.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; + to_copy.Transition.StateBefore = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; + to_copy.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; + list->ResourceBarrier(1, &to_copy); + list->CopyBufferRegion(readback.p, 0, out.p, 0, kBytes); + EXPECT(list->Close() == S_OK); + ID3D12CommandList* lists[] = {list.p}; + c.queue->ExecuteCommandLists(1, lists); + EXPECT(rt::d3d12::last_execute_result(c.queue) == leonida::ReplayResult::success); + EXPECT(SUCCEEDED(c.queue->Signal(c.fence, ++c.fence_value))); + EXPECT(SUCCEEDED(c.fence->SetEventOnCompletion(c.fence_value, nullptr))); + void* mapped = nullptr; + EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped))); + const auto* got = static_cast(mapped); + UINT zeros = 0; + for (UINT i = 0; i < kElements; ++i) zeros += got[i] == 0; + readback->Unmap(0, nullptr); + std::printf("window cap 0: a dispatch through the scratch window read %u/%u zero words\n", zeros, kElements); + EXPECT(zeros == kElements); + aliases.remove_title_memory(ring_va); + VirtualFree(ring, 0, MEM_RELEASE); + return 0; +} + int check_title_memory(Context& c, int& checks, bool geometry) { constexpr UINT64 ring_bytes = 256 * 1024; auto* ring = static_cast(VirtualAlloc(nullptr, ring_bytes, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); @@ -696,6 +778,14 @@ int main(int argc, char** argv) { const bool second_device = argc == 2 && std::strcmp(argv[1], "--rebound-second-device") == 0; const bool api_copy = api_copy_off || (argc == 2 && std::strcmp(argv[1], "--api-descriptor-copy") == 0); const bool rebind = api_copy || mirror_off || second_device || (argc == 2 && std::strcmp(argv[1], "--rebound-tables") == 0); + const bool window_cap = argc == 2 && std::strcmp(argv[1], "--window-cap") == 0; + if (window_cap) { + const char* cap = std::getenv("LEONIDA_TITLE_WINDOWS_MB"); + if (!cap || std::strcmp(cap, "0")) { + std::fprintf(stderr, "FAIL: --window-cap needs LEONIDA_TITLE_WINDOWS_MB=0\n"); + return 1; + } + } const char* setting = std::getenv("LEONIDA_GEOMETRY_MB"); const bool geometry = setting && std::atoi(setting) > 0; vktest::Device d; @@ -794,6 +884,15 @@ int main(int argc, char** argv) { EXPECT(d.validation_errors() == 0); return 0; } + if (window_cap) { + if (check_window_overflow(c)) return 1; + if (d.validation_errors()) { + std::fprintf(stderr, "%u validation errors\n", d.validation_errors()); + return 1; + } + std::printf("title memory past the window cap: the shader read the zeroed scratch window\n"); + return 0; + } if (check_kind(c, "read-write", PAGE_READWRITE, checks)) return 1; if (check_kind(c, "write-combined", PAGE_READWRITE | PAGE_WRITECOMBINE, checks)) return 1; if (check_title_memory(c, checks, geometry)) return 1;