diff options
| author | Tyrannys <80750971+Tyrannys@users.noreply.github.com> | 2026-11-25 00:21:56 -0600 |
|---|---|---|
| committer | Tyrannys <80750971+Tyrannys@users.noreply.github.com> | 2026-11-25 00:21:56 -0600 |
| commit | 6d66718f3efbf799fe2d2e8a853702b72368e832 (patch) | |
| tree | bf953edcbcde9fe77601213f26b3e0a936a828e1 /runtime | |
| parent | 42537e7b6f84c233ac009b924d3a25d2a1178197 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | runtime/shims/d3d12_vulkan_device.cpp | 42 | ||||
| -rw-r--r-- | runtime/test/d3d12_title_va_test.cpp | 99 |
3 files changed, 139 insertions, 5 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 2763e9d1b..f4ccf90fa 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -3431,6 +3431,9 @@ if(TARGET Vulkan::Headers) | |||
| 3431 | add_test(NAME d3d12_geometry_views COMMAND d3d12_title_va_test) | 3431 | add_test(NAME d3d12_geometry_views COMMAND d3d12_title_va_test) |
| 3432 | set_tests_properties(d3d12_geometry_views PROPERTIES ENVIRONMENT "LEONIDA_GEOMETRY_MB=16" SKIP_RETURN_CODE 77) | 3432 | set_tests_properties(d3d12_geometry_views PROPERTIES ENVIRONMENT "LEONIDA_GEOMETRY_MB=16" SKIP_RETURN_CODE 77) |
| 3433 | set_tests_properties(d3d12_title_va PROPERTIES SKIP_RETURN_CODE 77) | 3433 | set_tests_properties(d3d12_title_va PROPERTIES SKIP_RETURN_CODE 77) |
| 3434 | # Past LEONIDA_TITLE_WINDOWS_MB a view names the zeroed scratch window, not the title's raw VA (#1537). | ||
| 3435 | add_test(NAME d3d12_title_window_cap COMMAND d3d12_title_va_test --window-cap) | ||
| 3436 | set_tests_properties(d3d12_title_window_cap PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_TITLE_WINDOWS_MB=0") | ||
| 3434 | add_test(NAME d3d12_api_descriptor_copy COMMAND d3d12_title_va_test --api-descriptor-copy) | 3437 | add_test(NAME d3d12_api_descriptor_copy COMMAND d3d12_title_va_test --api-descriptor-copy) |
| 3435 | set_tests_properties(d3d12_api_descriptor_copy PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_DESC_MIRROR=1") | 3438 | set_tests_properties(d3d12_api_descriptor_copy PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_DESC_MIRROR=1") |
| 3436 | add_test(NAME d3d12_api_descriptor_copy_off COMMAND d3d12_title_va_test --api-descriptor-copy-off) | 3439 | 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 12494d201..c05d791e3 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp | |||
| @@ -2072,7 +2072,9 @@ struct DeviceState { | |||
| 2072 | // slot-133 buffer (a per-frame constant ring, #8862 section 3.5) gets a copy of the 64 KiB-aligned pages it | 2072 | // slot-133 buffer (a per-frame constant ring, #8862 section 3.5) gets a copy of the 64 KiB-aligned pages it |
| 2073 | // covers, refreshed before every submission like a slot-133 shadow, and the address of that copy. Windows are | 2073 | // covers, refreshed before every submission like a slot-133 shadow, and the address of that copy. Windows are |
| 2074 | // kept until the device goes (the ring comes round to the same pages), up to LEONIDA_TITLE_WINDOWS_MB (64 MiB | 2074 | // kept until the device goes (the ring comes round to the same pages), up to LEONIDA_TITLE_WINDOWS_MB (64 MiB |
| 2075 | // by default); past that, or with the arena full, the address is left as it was and counted. | 2075 | // by default). Past that, or with the arena full, the address names the scratch window instead: zero-filled |
| 2076 | // arena pages reserved with the first window, shared by every such address. The title's raw VA would name | ||
| 2077 | // nothing of the device's, and one read of it loses the device (#1537); the scratch costs a wrong result. | ||
| 2076 | struct TitleWindow { | 2078 | struct TitleWindow { |
| 2077 | std::uint64_t start; | 2079 | std::uint64_t start; |
| 2078 | std::uint64_t shader_va; | 2080 | std::uint64_t shader_va; |
| @@ -2085,6 +2087,15 @@ struct DeviceState { | |||
| 2085 | std::atomic<std::size_t> title_window_count{0}; // title_windows.size(), read without the mutex | 2087 | std::atomic<std::size_t> title_window_count{0}; // title_windows.size(), read without the mutex |
| 2086 | std::uint64_t title_window_bytes = 0; | 2088 | std::uint64_t title_window_bytes = 0; |
| 2087 | std::uint64_t title_window_cap = 64ull << 20; | 2089 | std::uint64_t title_window_cap = 64ull << 20; |
| 2090 | static constexpr std::uint64_t kTitleScratchBytes = kTitleWindowMax + kTitleWindowAlign; // any window's size | ||
| 2091 | std::uint64_t title_scratch = UINT64_MAX; // arena offset of the scratch window; UINT64_MAX: none yet | ||
| 2092 | bool title_scratch_tried = false; | ||
| 2093 | // Under title_window_mutex. The scratch window for `va`, whose window would start at `start`; false: no scratch. | ||
| 2094 | bool title_scratch_va(std::uint64_t va, std::uint64_t start, std::uint64_t& out) { | ||
| 2095 | if (title_scratch == UINT64_MAX) return false; | ||
| 2096 | out = title_scratch + (va - start); | ||
| 2097 | return true; | ||
| 2098 | } | ||
| 2088 | 2099 | ||
| 2089 | rt::d3d12::GeometryViews geometry; | 2100 | rt::d3d12::GeometryViews geometry; |
| 2090 | static rt::d3d12::GeometryCopy copy_geometry(std::uint8_t* to, const std::uint8_t* from, std::size_t size) { | 2101 | 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 { | |||
| 2152 | end = std::min(end, hi); | 2163 | end = std::min(end, hi); |
| 2153 | if (end <= va) return true; | 2164 | if (end <= va) return true; |
| 2154 | const std::uint64_t size = end - start; | 2165 | const std::uint64_t size = end - start; |
| 2166 | if (!title_scratch_tried) { | ||
| 2167 | // Reserved with the first window, while the arena has room; never refreshed (no title pages behind it). | ||
| 2168 | title_scratch_tried = true; | ||
| 2169 | VkDeviceSize scratch = 0; | ||
| 2170 | if (arena_allocate(kTitleScratchBytes, kTitleWindowAlign, scratch, arena_buffer)) { | ||
| 2171 | std::memset(arena.mapped + scratch, 0, std::size_t(kTitleScratchBytes)); | ||
| 2172 | title_scratch = scratch; | ||
| 2173 | } else { | ||
| 2174 | log_once("GPU address arena full: no scratch window for title memory past the window cap; KiB:", | ||
| 2175 | int(kTitleScratchBytes >> 10)); | ||
| 2176 | } | ||
| 2177 | } | ||
| 2155 | if (title_window_bytes + size > title_window_cap) { | 2178 | if (title_window_bytes + size > title_window_cap) { |
| 2156 | log_once("title memory named by a shader address is past LEONIDA_TITLE_WINDOWS_MB, left untranslated; " | 2179 | if (title_scratch_va(va, start, out)) { |
| 2157 | "MiB copied so far:", | 2180 | log_once("title memory named by a shader address is past LEONIDA_TITLE_WINDOWS_MB, given the zeroed " |
| 2158 | int(title_window_bytes >> 20)); | 2181 | "scratch window (raise LEONIDA_TITLE_WINDOWS_MB); MiB copied so far:", |
| 2182 | int(title_window_bytes >> 20)); | ||
| 2183 | } else { | ||
| 2184 | log_once("title memory named by a shader address is past LEONIDA_TITLE_WINDOWS_MB, left untranslated; " | ||
| 2185 | "MiB copied so far:", | ||
| 2186 | int(title_window_bytes >> 20)); | ||
| 2187 | } | ||
| 2159 | return true; | 2188 | return true; |
| 2160 | } | 2189 | } |
| 2161 | VkDeviceSize offset = 0; | 2190 | VkDeviceSize offset = 0; |
| @@ -2163,7 +2192,10 @@ struct DeviceState { | |||
| 2163 | if (arena_allocate(size, kTitleWindowAlign, offset, arena_buffer)) { | 2192 | if (arena_allocate(size, kTitleWindowAlign, offset, arena_buffer)) { |
| 2164 | mapped = arena.mapped + offset; | 2193 | mapped = arena.mapped + offset; |
| 2165 | } else if (!stream_windows || !external_windows.allocate(*this, size, offset, mapped)) { | 2194 | } else if (!stream_windows || !external_windows.allocate(*this, size, offset, mapped)) { |
| 2166 | log_once("GPU address arena full (raise LEONIDA_ARENA_MB): title memory left untranslated; window KiB:", | 2195 | log_once(title_scratch_va(va, start, out) |
| 2196 | ? "GPU address arena full (raise LEONIDA_ARENA_MB): title memory given the zeroed scratch " | ||
| 2197 | "window; window KiB:" | ||
| 2198 | : "GPU address arena full (raise LEONIDA_ARENA_MB): title memory left untranslated; window KiB:", | ||
| 2167 | int(std::min<std::uint64_t>(size >> 10, INT_MAX))); | 2199 | int(std::min<std::uint64_t>(size >> 10, INT_MAX))); |
| 2168 | return true; | 2200 | return true; |
| 2169 | } | 2201 | } |
diff --git a/runtime/test/d3d12_title_va_test.cpp b/runtime/test/d3d12_title_va_test.cpp index 0adb72d25..383ec9e5f 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; | |||
| 310 | void aliases_of_test_tile(std::uintptr_t va, std::vector<std::uintptr_t>& peers) { | 310 | void aliases_of_test_tile(std::uintptr_t va, std::vector<std::uintptr_t>& peers) { |
| 311 | if (va == source_tile) peers.push_back(peer_tile); | 311 | if (va == source_tile) peers.push_back(peer_tile); |
| 312 | } | 312 | } |
| 313 | // LEONIDA_TITLE_WINDOWS_MB=0: no window fits the cap. A view of the title's memory then names the zeroed scratch | ||
| 314 | // window in the arena, not the title's raw VA (one GPU read of that loses the device, #1537): two views of | ||
| 315 | // different pages share it, and a dispatch through one reads zeros, not the title's bytes. | ||
| 316 | int check_window_overflow(Context& c) { | ||
| 317 | constexpr UINT64 ring_bytes = 256 * 1024; | ||
| 318 | auto* ring = static_cast<std::uint32_t*>(VirtualAlloc(nullptr, ring_bytes, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | ||
| 319 | EXPECT(ring); | ||
| 320 | const auto ring_va = reinterpret_cast<std::uint64_t>(ring); | ||
| 321 | EXPECT(ring_va >= c.arena && !(ring_va & 0xFFFF)); | ||
| 322 | for (UINT i = 0; i < ring_bytes / 4; ++i) ring[i] = 0xC0000000u + i * 7u; // never zero | ||
| 323 | auto& aliases = leonida::title_va_aliases(); | ||
| 324 | aliases.add_title_memory(ring_va, ring_bytes); | ||
| 325 | |||
| 326 | D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 8, | ||
| 327 | D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0}; | ||
| 328 | Ref<ID3D12DescriptorHeap> heap; | ||
| 329 | EXPECT(SUCCEEDED(c.device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&heap.p)))); | ||
| 330 | const std::uint64_t cpu = reinterpret_cast<XboxHeapHandleFn>(slot(heap.p, 9))(heap.p); | ||
| 331 | const std::uint64_t gpu = reinterpret_cast<XboxHeapHandleFn>(slot(heap.p, 10))(heap.p); | ||
| 332 | const UINT inc = c.device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); | ||
| 333 | const D3D12_CONSTANT_BUFFER_VIEW_DESC first{ring_va + 0x2000, 256}, second{ring_va + 0x30000, 256}; | ||
| 334 | c.device->CreateConstantBufferView(&first, D3D12_CPU_DESCRIPTOR_HANDLE{cpu}); | ||
| 335 | c.device->CreateConstantBufferView(&second, D3D12_CPU_DESCRIPTOR_HANDLE{cpu + inc}); | ||
| 336 | const auto a = vsharp_base(cpu), b = vsharp_base(cpu + inc); | ||
| 337 | std::printf("window cap 0: views of the title's memory named %s and %s\n", | ||
| 338 | a == first.BufferLocation ? "the raw VA" : a < c.arena ? "the arena" : "elsewhere", | ||
| 339 | b == second.BufferLocation ? "the raw VA" : b < c.arena ? "the arena" : "elsewhere"); | ||
| 340 | EXPECT(a != first.BufferLocation && a < c.arena); | ||
| 341 | EXPECT(b != second.BufferLocation && b < c.arena); | ||
| 342 | EXPECT(a == b + 0x2000); // one scratch window, each address at its offset into its 64 KiB page | ||
| 343 | EXPECT(aliases.shader_va(first.BufferLocation) == first.BufferLocation); // no window was made | ||
| 344 | |||
| 345 | const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}, readback_heap{D3D12_HEAP_TYPE_READBACK}; | ||
| 346 | const auto uav = buffer_desc(kBytes, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS); | ||
| 347 | const auto readback_desc = buffer_desc(kBytes); | ||
| 348 | Ref<ID3D12Resource> out, readback; | ||
| 349 | EXPECT(SUCCEEDED(c.device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &uav, | ||
| 350 | D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr, | ||
| 351 | IID_PPV_ARGS(&out.p)))); | ||
| 352 | EXPECT(SUCCEEDED(c.device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &readback_desc, | ||
| 353 | D3D12_RESOURCE_STATE_COPY_DEST, nullptr, | ||
| 354 | IID_PPV_ARGS(&readback.p)))); | ||
| 355 | Ref<ID3D12CommandAllocator> allocator; | ||
| 356 | Ref<ID3D12GraphicsCommandList> list; | ||
| 357 | EXPECT(SUCCEEDED(c.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); | ||
| 358 | EXPECT(SUCCEEDED(c.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, c.pso, | ||
| 359 | IID_PPV_ARGS(&list.p)))); | ||
| 360 | ID3D12DescriptorHeap* heaps[] = {heap.p}; | ||
| 361 | list->SetDescriptorHeaps(1, heaps); | ||
| 362 | list->SetComputeRootSignature(c.root); | ||
| 363 | list->SetComputeRoot32BitConstant(0, 0, 0); // the CBV mode of check_title_memory: its first four words | ||
| 364 | list->SetComputeRoot32BitConstant(0, 0, 1); | ||
| 365 | list->SetComputeRootUnorderedAccessView(1, out->GetGPUVirtualAddress()); | ||
| 366 | list->SetComputeRootDescriptorTable(2, D3D12_GPU_DESCRIPTOR_HANDLE{gpu}); | ||
| 367 | list->Dispatch(1, 1, 1); | ||
| 368 | D3D12_RESOURCE_BARRIER to_copy{}; | ||
| 369 | to_copy.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; | ||
| 370 | to_copy.Transition.pResource = out.p; | ||
| 371 | to_copy.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; | ||
| 372 | to_copy.Transition.StateBefore = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; | ||
| 373 | to_copy.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; | ||
| 374 | list->ResourceBarrier(1, &to_copy); | ||
| 375 | list->CopyBufferRegion(readback.p, 0, out.p, 0, kBytes); | ||
| 376 | EXPECT(list->Close() == S_OK); | ||
| 377 | ID3D12CommandList* lists[] = {list.p}; | ||
| 378 | c.queue->ExecuteCommandLists(1, lists); | ||
| 379 | EXPECT(rt::d3d12::last_execute_result(c.queue) == leonida::ReplayResult::success); | ||
| 380 | EXPECT(SUCCEEDED(c.queue->Signal(c.fence, ++c.fence_value))); | ||
| 381 | EXPECT(SUCCEEDED(c.fence->SetEventOnCompletion(c.fence_value, nullptr))); | ||
| 382 | void* mapped = nullptr; | ||
| 383 | EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped))); | ||
| 384 | const auto* got = static_cast<const std::uint32_t*>(mapped); | ||
| 385 | UINT zeros = 0; | ||
| 386 | for (UINT i = 0; i < kElements; ++i) zeros += got[i] == 0; | ||
| 387 | readback->Unmap(0, nullptr); | ||
| 388 | std::printf("window cap 0: a dispatch through the scratch window read %u/%u zero words\n", zeros, kElements); | ||
| 389 | EXPECT(zeros == kElements); | ||
| 390 | aliases.remove_title_memory(ring_va); | ||
| 391 | VirtualFree(ring, 0, MEM_RELEASE); | ||
| 392 | return 0; | ||
| 393 | } | ||
| 394 | |||
| 313 | int check_title_memory(Context& c, int& checks, bool geometry) { | 395 | int check_title_memory(Context& c, int& checks, bool geometry) { |
| 314 | constexpr UINT64 ring_bytes = 256 * 1024; | 396 | constexpr UINT64 ring_bytes = 256 * 1024; |
| 315 | auto* ring = static_cast<std::uint32_t*>(VirtualAlloc(nullptr, ring_bytes, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | 397 | auto* ring = static_cast<std::uint32_t*>(VirtualAlloc(nullptr, ring_bytes, MEM_TOP_DOWN | MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); |
| @@ -696,6 +778,14 @@ int main(int argc, char** argv) { | |||
| 696 | const bool second_device = argc == 2 && std::strcmp(argv[1], "--rebound-second-device") == 0; | 778 | const bool second_device = argc == 2 && std::strcmp(argv[1], "--rebound-second-device") == 0; |
| 697 | const bool api_copy = api_copy_off || (argc == 2 && std::strcmp(argv[1], "--api-descriptor-copy") == 0); | 779 | const bool api_copy = api_copy_off || (argc == 2 && std::strcmp(argv[1], "--api-descriptor-copy") == 0); |
| 698 | const bool rebind = api_copy || mirror_off || second_device || (argc == 2 && std::strcmp(argv[1], "--rebound-tables") == 0); | 780 | const bool rebind = api_copy || mirror_off || second_device || (argc == 2 && std::strcmp(argv[1], "--rebound-tables") == 0); |
| 781 | const bool window_cap = argc == 2 && std::strcmp(argv[1], "--window-cap") == 0; | ||
| 782 | if (window_cap) { | ||
| 783 | const char* cap = std::getenv("LEONIDA_TITLE_WINDOWS_MB"); | ||
| 784 | if (!cap || std::strcmp(cap, "0")) { | ||
| 785 | std::fprintf(stderr, "FAIL: --window-cap needs LEONIDA_TITLE_WINDOWS_MB=0\n"); | ||
| 786 | return 1; | ||
| 787 | } | ||
| 788 | } | ||
| 699 | const char* setting = std::getenv("LEONIDA_GEOMETRY_MB"); | 789 | const char* setting = std::getenv("LEONIDA_GEOMETRY_MB"); |
| 700 | const bool geometry = setting && std::atoi(setting) > 0; | 790 | const bool geometry = setting && std::atoi(setting) > 0; |
| 701 | vktest::Device d; | 791 | vktest::Device d; |
| @@ -794,6 +884,15 @@ int main(int argc, char** argv) { | |||
| 794 | EXPECT(d.validation_errors() == 0); | 884 | EXPECT(d.validation_errors() == 0); |
| 795 | return 0; | 885 | return 0; |
| 796 | } | 886 | } |
| 887 | if (window_cap) { | ||
| 888 | if (check_window_overflow(c)) return 1; | ||
| 889 | if (d.validation_errors()) { | ||
| 890 | std::fprintf(stderr, "%u validation errors\n", d.validation_errors()); | ||
| 891 | return 1; | ||
| 892 | } | ||
| 893 | std::printf("title memory past the window cap: the shader read the zeroed scratch window\n"); | ||
| 894 | return 0; | ||
| 895 | } | ||
| 797 | if (check_kind(c, "read-write", PAGE_READWRITE, checks)) return 1; | 896 | if (check_kind(c, "read-write", PAGE_READWRITE, checks)) return 1; |
| 798 | if (check_kind(c, "write-combined", PAGE_READWRITE | PAGE_WRITECOMBINE, checks)) return 1; | 897 | if (check_kind(c, "write-combined", PAGE_READWRITE | PAGE_WRITECOMBINE, checks)) return 1; |
| 799 | if (check_title_memory(c, checks, geometry)) return 1; | 898 | if (check_title_memory(c, checks, geometry)) return 1; |