From 1f7cbeff9a0461c037287e83297e9add75d2646a Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Thu, 26 Nov 2026 09:57:50 -0500 Subject: [PATCH 1/3] xmem: a remap retires geometry copies only with LEONIDA_GEOMETRY_REMAP_RELEASE=1 Since #2350 a remap of physical pages that gives a page another frame retired the geometry copies over it, on by default. In the first world run of that main on an RX 7900 XTX (RADV) the copies were retired by the thousand (evicted 15010 in about 40 s) and the run ended in a GPUVM page fault (TCP read) and a lost device; the same build settings without it ran eight minutes with evicted 0 and no fault. A remap leaves the address valid on the console, so the title keeps using what names it, and not everything that names a copy's address is rewritten when the copy goes: - a root view is resolved while its list is recorded (d3d12_com.cpp, GraphicsCommandList::address); a list recorded before the retirement and executed in an ExecuteCommandLists begun after it is not among the replays the retirement waits for (kAfterExecutes covers only those in progress), so it reads the freed copy; - a placed buffer view written outside a console heap carries the copy's address for as long as the descriptor lives. The remap release is now off unless the variable is 1, read at every remap; the copies follow the new pages through the refresh, as before #2350. Freeing, decommitting and unmapping still retire copies. Tests: xmem_test test_remap_releases checks that a remap releases nothing by default and with 0, then the existing checks with 1. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 17 ++++++++++++----- runtime/README.md | 2 +- runtime/shims/xmem.cpp | 14 ++++++++------ runtime/test/xmem_test.cpp | 17 +++++++++++++++++ 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 98cf32aad2..c402246939 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -1737,11 +1737,18 @@ The copies: as VIctory keeps its copies of title memory (approach only). In a world run on the RX 7900 XTX the copies held 4.4-6 GB of VRAM. The cost moves to the bus: the GPU reads every vertex and index of a copy from system memory, so a geometry-heavy pass can take longer; measure frame time with and without it before making it a default. - - **When a copy goes**: a view is retired when its title range is freed, decommitted or unmapped, and also when a - remap gives one of its pages another frame or none (`XMemMapPhysicalPages` with pages, the scatter call, - `CopyPageMappingsBatchX`; xmem's `RemapReleases`, `LEONIDA_GEOMETRY_REMAP_RELEASE=0` turns it off): the title - streams by remapping its pools' frames, and before this the copies of what those pages held stayed for the - device's life (`evicted 0` in every world run). A page mapped again to the frame it had is no change. A retired + - **When a copy goes**: a view is retired when its title range is freed, decommitted or unmapped. With + `LEONIDA_GEOMETRY_REMAP_RELEASE=1` (off by default) also when a remap gives one of its pages another frame or + none (`XMemMapPhysicalPages` with pages, the scatter call, `CopyPageMappingsBatchX`; xmem's `RemapReleases`): + the title streams by remapping its pools' frames, and otherwise the copies of what those pages held stay for the + device's life (`evicted 0` in every world run). It is off because a remap leaves the address valid on the + console, and not everything that names a copy's address is rewritten when the copy goes: a root view is + resolved while its list is recorded (`d3d12_com.cpp`, `GraphicsCommandList::address`), so a list recorded before the + retirement and executed after it reads the freed copy, and so does a placed buffer view written outside a + console heap. On by default, it retired 15 010 copies in 40 s of a world run on the + RX 7900 XTX (RADV) and ended it in a GPUVM page fault and a lost device; the same run without it went eight + minutes with no fault. Making it safe needs a copy kept until nothing recorded or written can still name it. + A page mapped again to the frame it had is no change. A retired view leaves the lookups at once, its alias ranges and translated heap slots are invalidated, its stream-pool extent is forgotten, and its memory is freed only after every ExecuteCommandLists replay in progress at the retirement has recorded its submissions and those have completed (`kAfterExecutes`). Before it leaves the diff --git a/runtime/README.md b/runtime/README.md index 84190039ca..76df56ff4c 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -383,7 +383,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_GEOMETRY_ASYNC` | unset | Trades correctness for speed. `1` refreshes the geometry copies (`shims/d3d12_geometry_views.h`) on a worker thread of the layer, started at submission and not waited for, instead of on the title's submitting thread. A draw may read a copy the worker is still writing: wrong vertices for a frame. | | `LEONIDA_GEOMETRY_WHOLE_VIEWS` | unset | A geometry copy that is already filled copies only the 64 KiB tiles of title memory written since the last refresh (`shims/d3d12_geometry_views.h`). Any value other than `0` copies every view over a written tile whole, as before. | | `LEONIDA_GEOMETRY_HOST` | unset | Where the geometry copies (`shims/d3d12_geometry_views.h`, under `LEONIDA_GEOMETRY_MB`) live. Unset or `0`: device-local memory through the BAR while the driver gives it (several GB of VRAM in a world run). `1`: system memory outside VRAM, write-back where the device offers a cached host type; `2`: system memory, write-combined. The GPU then reads every vertex and index of a copy over the bus, which can cost frame time in geometry-heavy passes. A device whose host-visible memory is all device-local (an integrated GPU) keeps its types. The choice is logged once and in the `geometry buffers` line. | -| `LEONIDA_GEOMETRY_REMAP_RELEASE` | on | A remap of physical pages (`XMemMapPhysicalPages`, the scatter call, `CopyPageMappingsBatchX`) that gives a page another frame, or none, retires the geometry copies over it, as freeing or unmapping it does: the title streams by remapping its pools' frames, and the copies of what they held were kept for the device's life. A retired copy is copied once more from the new pages if they are readable, then freed once every submission that may name it has finished. A page mapped again to the frame it had is no change. `0` turns it off. | +| `LEONIDA_GEOMETRY_REMAP_RELEASE` | unset | Experimental, unsafe in world runs. `1`: a remap of physical pages (`XMemMapPhysicalPages`, the scatter call, `CopyPageMappingsBatchX`) that gives a page another frame, or none, retires the geometry copies over it, as freeing or unmapping it does, so the copies of what the title's stream pools held do not stay for the device's life. A retired copy is copied once more from the new pages if they are readable, then freed once the submissions in progress at the retirement have finished. A remap leaves the address valid, though, and a root view resolved while a list was recorded (executed after the retirement) or a placed buffer view written outside a console heap still names the freed copy: on by default it ended a world run on an RX 7900 XTX in a GPUVM page fault and a lost device. Unset or any other value: a remap retires nothing, and the copies follow the new pages through the refresh. | | `LEONIDA_TITLE_FILL_ASYNC` | unset | Trades correctness for speed. `1` stops device slot 70 views from hashing and filling the texture they name on the title's thread: a texture never filled or known stale is queued for a worker thread that fills one texture at a time. Until then a shader samples the image as it is, empty or a frame behind. | | `LEONIDA_TITLE_VIEW_REFRESH_MS` | `0` | The shortest time, in milliseconds, between two checks of a title texture's bytes when a device slot 70 view of it is written. `0` checks at every view. The periodic pass still checks the texture on its own period, and a texture known stale is checked at once. Has no effect with `LEONIDA_TITLE_FILL_ASYNC=1`. | | `LEONIDA_TITLE_EVERY_BUDGET_US` | `0` | Microseconds one pass over the textures filled from the title's memory may spend on the textures it visits every pass (mapped by the title, or known stale), resuming where the last pass stopped; the texture that crosses the limit is finished. A mapped texture never filled yet is visited first whatever the limit. `0` visits all of them every pass. It limits the synchronous pass only: with `LEONIDA_TEXTURE_REFRESH_ASYNC=1` the pass runs on the refresh worker, and the limit does not apply. | diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index cbd857fce9..0d982be4b1 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp @@ -1039,13 +1039,15 @@ thread_local const char* t_unmap_cause = "unmap"; // page now mapped to another frame than it had (or to none) is reported as released, in runs, so the geometry copies // over it are retired as over freed or unmapped memory (geometry_writes::note_released, GeometryViews::drain_released): // the title streams by remapping the frames of its pools, and the copies of what those pages held before were kept -// for the device's life. A page mapped again to the frame it had is no change. LEONIDA_GEOMETRY_REMAP_RELEASE=0: off. +// for the device's life. A page mapped again to the frame it had is no change. +// Off unless LEONIDA_GEOMETRY_REMAP_RELEASE=1: a remap keeps the address valid on the console, so the title goes on +// using what names it, and some of what names a copy's address is never rewritten when the copy is retired (a root +// view a list resolved while it was recorded, executed after the retirement; a placed buffer view written outside a +// console heap). On by default, it freed copies under such views: a GPUVM page fault and a lost device about 40 s +// into the world on an RX 7900 XTX. Read at every remap, so a test can switch it. bool remap_releases_enabled() { - static const bool on = [] { - const char* v = std::getenv("LEONIDA_GEOMETRY_REMAP_RELEASE"); - return !v || std::strcmp(v, "0") != 0; - }(); - return on; + char value[2]{}; + return GetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", value, sizeof(value)) == 1 && value[0] == '1'; } struct RemapReleases; thread_local RemapReleases* t_remap_releases = nullptr; diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index adc24cca3e..61e8732e10 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp @@ -464,7 +464,22 @@ void test_remap_releases(const PhysicalApi& api) { return false; }; + // Off by default: a remap keeps the address valid, and a root view recorded before it (or a placed buffer view) + // names the copy's address after it, so a copy freed then is read by the GPU (a page fault and a lost device). + char previous[8]{}; + const DWORD had = GetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", previous, sizeof(previous)); + SetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", nullptr); const uint64_t first[kPages] = {pages[0], pages[1], pages[2]}; + const uint64_t other[kPages] = {pages[3], pages[2], pages[1]}; + check(api.map(window, kPages, first) == window && api.map(window, kPages, other) == window && take().empty(), + "remap releases: off by default, a remap releases nothing"); + SetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", "0"); + check(api.map(window, kPages, first) == window && take().empty(), + "remap releases: LEONIDA_GEOMETRY_REMAP_RELEASE=0 releases nothing"); + check(api.map(window, kPages, nullptr) == window && !take().empty() && covers(base, base + 3 * kPage64k), + "remap releases: unmapping releases the range whatever the setting"); + SetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", "1"); + check(api.map(window, kPages, first) == window && take().empty(), "remap releases: a first mapping of unmapped pages releases nothing"); check(api.map(window + kPage64k, 1, &pages[1]) == window + kPage64k && take().empty(), @@ -496,6 +511,8 @@ void test_remap_releases(const PhysicalApi& api) { check(api.map(window, kPages, nullptr) == window && !take().empty() && covers(base, base + 3 * kPage64k), "remap releases: unmapping releases the range"); + SetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", + had > 0 && had < sizeof(previous) ? previous : nullptr); for (uint64_t i = 0; i < kPages; ++i) watch::release(base + i * kPage64k); watch::unsubscribe(queue); api.free_pages(4, pages); From 4a6a8a633e71ceb5af7afbaadcf9ad2e4ba9a377 Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Thu, 26 Nov 2026 10:05:43 -0500 Subject: [PATCH 2/3] xmem: read LEONIDA_GEOMETRY_REMAP_RELEASE once, with a test seam to read it again GetEnvironmentVariableA at every remap ran under g_phys_mutex in every scatter call; with it, xmem_test's split race ("the reader of the views' last pages never faults or misreads") failed in two full Wine runs in a row, where main passed it twice. The value is cached as before #2350's default changed; rt::xmem::reload_remap_release_setting lets test_remap_releases switch it. Two full runs since: no change against main. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/xmem.cpp | 14 +++++++++++--- runtime/shims/xmem.h | 2 ++ runtime/test/xmem_test.cpp | 4 ++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 0d982be4b1..d6d6f408c4 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp @@ -1044,10 +1044,16 @@ thread_local const char* t_unmap_cause = "unmap"; // using what names it, and some of what names a copy's address is never rewritten when the copy is retired (a root // view a list resolved while it was recorded, executed after the retirement; a placed buffer view written outside a // console heap). On by default, it freed copies under such views: a GPUVM page fault and a lost device about 40 s -// into the world on an RX 7900 XTX. Read at every remap, so a test can switch it. +// into the world on an RX 7900 XTX. Read once; rt::xmem::reload_remap_release_setting reads it again (for a test). +std::atomic g_remap_releases{-1}; bool remap_releases_enabled() { - char value[2]{}; - return GetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", value, sizeof(value)) == 1 && value[0] == '1'; + int on = g_remap_releases.load(std::memory_order_relaxed); + if (on < 0) { + char value[2]{}; + on = GetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", value, sizeof(value)) == 1 && value[0] == '1'; + g_remap_releases.store(on, std::memory_order_relaxed); + } + return on != 0; } struct RemapReleases; thread_local RemapReleases* t_remap_releases = nullptr; @@ -2423,6 +2429,8 @@ bool host_page(std::uintptr_t address, HostPage& out) { namespace rt::xmem { +void reload_remap_release_setting() { g_remap_releases.store(-1, std::memory_order_relaxed); } + void note_stream_reservation(uintptr_t base, size_t size, uintptr_t caller_rva) { if (caller_rva != 0x1e1d00f && caller_rva != 0x1e1d03e) return; const std::lock_guard physical(g_phys_mutex); diff --git a/runtime/shims/xmem.h b/runtime/shims/xmem.h index 6e0017f2ba..d9251c5f0e 100644 --- a/runtime/shims/xmem.h +++ b/runtime/shims/xmem.h @@ -29,6 +29,8 @@ bool copy_page_mappings(std::uint32_t count, const PageBatch* batches, std::uint std::uint32_t pool_pages, std::uint32_t flags); void geometry_aliases(std::uintptr_t va, std::vector& addresses); +// Test seam: LEONIDA_GEOMETRY_REMAP_RELEASE is read again at the next remap. +void reload_remap_release_setting(); bool commit_physical_range(const void* address, std::size_t size); // A lifetime-pinned alias of a contiguous range of the title's writable mapped physical pages. std::shared_ptr pin_mapped_data(const void* address, std::size_t size); diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index 61e8732e10..d5e866a767 100644 --- a/runtime/test/xmem_test.cpp +++ b/runtime/test/xmem_test.cpp @@ -469,16 +469,19 @@ void test_remap_releases(const PhysicalApi& api) { char previous[8]{}; const DWORD had = GetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", previous, sizeof(previous)); SetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", nullptr); + rt::xmem::reload_remap_release_setting(); const uint64_t first[kPages] = {pages[0], pages[1], pages[2]}; const uint64_t other[kPages] = {pages[3], pages[2], pages[1]}; check(api.map(window, kPages, first) == window && api.map(window, kPages, other) == window && take().empty(), "remap releases: off by default, a remap releases nothing"); SetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", "0"); + rt::xmem::reload_remap_release_setting(); check(api.map(window, kPages, first) == window && take().empty(), "remap releases: LEONIDA_GEOMETRY_REMAP_RELEASE=0 releases nothing"); check(api.map(window, kPages, nullptr) == window && !take().empty() && covers(base, base + 3 * kPage64k), "remap releases: unmapping releases the range whatever the setting"); SetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", "1"); + rt::xmem::reload_remap_release_setting(); check(api.map(window, kPages, first) == window && take().empty(), "remap releases: a first mapping of unmapped pages releases nothing"); @@ -513,6 +516,7 @@ void test_remap_releases(const PhysicalApi& api) { "remap releases: unmapping releases the range"); SetEnvironmentVariableA("LEONIDA_GEOMETRY_REMAP_RELEASE", had > 0 && had < sizeof(previous) ? previous : nullptr); + rt::xmem::reload_remap_release_setting(); for (uint64_t i = 0; i < kPages; ++i) watch::release(base + i * kPage64k); watch::unsubscribe(queue); api.free_pages(4, pages); From 248b58c42d2cc5bbdb9aaf81ed122a379926d518 Mon Sep 17 00:00:00 2001 From: Jan Trencansky Date: Thu, 26 Nov 2026 10:28:43 -0500 Subject: [PATCH 3/3] d3d12: the console heap's host-RAM mirror only with LEONIDA_HEAP_MIRROR=1 The mirror (746059011, in #2350) was on by default. World runs on merged main on an RX 7900 XTX (RADV), the same settings otherwise: - remap release on, mirror on: GPU page fault and a lost device about 40 s into the world; - remap release off, mirror on: no evictions and no page fault, but a hang in a library compute program on the direct queue about a minute in, a ring timeout and a lost device; - both off: past five minutes in the world with no fault or hang, ending where the build before #2350 ended, on the title's own breakpoint. Why the mirror hangs the GPU is not known. It is off unless the variable is 1; the release sweep then reads the copy, as before the mirror. Tests: d3d12_console_heap_mirror sets LEONIDA_HEAP_MIRROR=1; a new ctest entry, d3d12_console_heap_mirror_default, runs the same program without the variable and expects no mirror (it fails on main). Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 6 +++++- runtime/CMakeLists.txt | 6 +++++- runtime/README.md | 2 +- runtime/shims/d3d12_vulkan_device.cpp | 7 ++++--- runtime/src/console_heap_copy.h | 3 ++- runtime/test/d3d12_console_heap_mirror_test.cpp | 9 +++++---- 6 files changed, 22 insertions(+), 11 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index c402246939..375278169a 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2062,7 +2062,11 @@ The game can use D3D12 descriptors in three ways, and the layer must handle all arguments for direct and indirect compute dispatches with an inline signature. Bound console descriptor heap base addresses are remapped through `console_descriptor_table_va` so that user SGPRs receive the arena copy's shader virtual address (where GPU descriptor reads land, preventing `READ_INVALID` device loss during world - load), while unshadowed host heaps remain unchanged. An unbound heap contributes + load), while unshadowed host heaps remain unchanged. The CPU reads that copy too (the release sweep of + `LEONIDA_CONSOLE_RELEASE_FILTER=1`), uncached across PCIe when it is in ReBAR memory. `LEONIDA_HEAP_MIRROR=1` + gives those reads a host-RAM mirror of the copy (`src/console_heap_copy.h`). It is off by default: with it, a + world run on the RX 7900 XTX (RADV) hung in a library compute program about a minute in and lost the device, + and without it the same run went on for more than five minutes. Why is not known yet. An unbound heap contributes zero, and rebinding affects later commands. A heap that cannot fit in the inline user data is reported once, and a signature whose compute arguments spill gets nothing written (logged once). Set `LEONIDA_COMPUTE_DIRECT_HEAPS=0` to restore the previous compute arguments. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index b8cef55c65..be7e48c858 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -3740,10 +3740,14 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_console_heap_mirror_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_console_heap_mirror COMMAND d3d12_console_heap_mirror_test) set_tests_properties(d3d12_console_heap_mirror PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT - "LEONIDA_CONSOLE_FIX_US=0;LEONIDA_CONSOLE_RELEASE_FILTER=1;LEONIDA_HEAP_MIRROR_CHECK=1") + "LEONIDA_CONSOLE_FIX_US=0;LEONIDA_CONSOLE_RELEASE_FILTER=1;LEONIDA_HEAP_MIRROR=1;LEONIDA_HEAP_MIRROR_CHECK=1") add_test(NAME d3d12_console_heap_mirror_off COMMAND d3d12_console_heap_mirror_test) set_tests_properties(d3d12_console_heap_mirror_off PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT "LEONIDA_CONSOLE_FIX_US=0;LEONIDA_CONSOLE_RELEASE_FILTER=1;LEONIDA_HEAP_MIRROR=0") + # The default: no mirror unless LEONIDA_HEAP_MIRROR=1. + add_test(NAME d3d12_console_heap_mirror_default COMMAND d3d12_console_heap_mirror_test) + set_tests_properties(d3d12_console_heap_mirror_default PROPERTIES SKIP_RETURN_CODE 77 ENVIRONMENT + "LEONIDA_CONSOLE_FIX_US=0;LEONIDA_CONSOLE_RELEASE_FILTER=1") add_executable(d3d12_indirect_zero_test test/d3d12_indirect_zero_test.cpp) target_link_libraries(d3d12_indirect_zero_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) target_compile_definitions(d3d12_indirect_zero_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) diff --git a/runtime/README.md b/runtime/README.md index 76df56ff4c..5822dba8f8 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -425,7 +425,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_CONSOLE_GPU_TSHARPS` | on | With `LEONIDA_CONSOLE_HEAP_GPU=1`, the T#s of changed slots are registered by the resolver thread and the refresh only decodes them. `0` registers them on the submitting thread, under the fix budget. | | `LEONIDA_CONSOLE_GPU_RETRY_MS` | `2000` | With `LEONIDA_CONSOLE_HEAP_GPU=1`, how often V# words that got no backing are asked for again, on a timer that bounds the cost per submission. | | `LEONIDA_CONSOLE_GPU_FILTER` | `LEONIDA_CONSOLE_RELEASE_FILTER`'s value | With `LEONIDA_CONSOLE_HEAP_GPU=1`, `1`: a released backing, an alias range that went or a word answered since asks for a rescan of only the slots it touches, not of every V# slot. Overflows and key resets still rescan everything. | -| `LEONIDA_HEAP_MIRROR` | `1` | `0`: no host-RAM mirror of a console resource heap's copy in the arena (`src/console_heap_copy.h`). With the mirror, the CPU reads of the copy (the `LEONIDA_CONSOLE_RELEASE_FILTER` sweep) come from host RAM, not uncached device memory across PCIe. A copy in an import of host memory, and one the `LEONIDA_CONSOLE_HEAP_GPU` pass writes, never has one. | +| `LEONIDA_HEAP_MIRROR` | unset | Experimental. `1`: a host-RAM mirror of a console resource heap's copy in the arena (`src/console_heap_copy.h`), so the CPU reads of the copy (the `LEONIDA_CONSOLE_RELEASE_FILTER` sweep) come from host RAM, not uncached device memory across PCIe. Off by default: with it, a world run on an RX 7900 XTX hung in a compute program about a minute in and lost the device; without it the same run went on for more than five minutes. Unset or any other value: no mirror, and the sweep reads the copy. A copy in an import of host memory, and one the `LEONIDA_CONSOLE_HEAP_GPU` pass writes, never has one. | | `LEONIDA_HEAP_MIRROR_CHECK` | unset | `1`: at every release sweep the mirror is compared with the copy (a read of the whole copy); the first differing slots are logged, and the heap log line counts the checks and differing slots. | | `LEONIDA_CONSOLE_RELEASE_FILTER` | unset | `1`: when a backing is released, only the console-heap slots whose V#s point into it are fixed again (by actual Vulkan address), instead of every V# slot of the heap. The heap log line counts the sweeps, live slots, fixes and full fallbacks. | | `LEONIDA_SHADOW_GPU` | unset | `1`: aliased pages of the slot-133 shadows are copied on the GPU at the start of a submission, from an import of the mapped title section (`VK_EXT_external_memory_host`, per 2 MiB window, kept while xmem's map journal shows no change over it). Destinations outside the arena and pages that do not import are copied by the CPU as before. | diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index eedc0662e1..0b9b211dbb 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -4531,12 +4531,13 @@ struct DeviceState { unbound_tsharp_tracker().note(heap.console_va, offset, words, why); } - // LEONIDA_HEAP_MIRROR=0: no host-RAM mirror of a console heap's copy in the arena (src/console_heap_copy.h); the - // release sweep then reads the copy itself, as before the mirror. + // LEONIDA_HEAP_MIRROR=1: a host-RAM mirror of a console heap's copy in the arena (src/console_heap_copy.h), which + // the release sweep reads instead of the copy. Off by default: on, a world run on the RX 7900 XTX hung in a + // compute program about a minute in and lost the device; off, the same run went past five minutes. static bool heap_mirror_enabled() { static const bool on = [] { char value[4] = {}; - return !(GetEnvironmentVariableA("LEONIDA_HEAP_MIRROR", value, sizeof(value)) == 1 && value[0] == '0'); + return GetEnvironmentVariableA("LEONIDA_HEAP_MIRROR", value, sizeof(value)) == 1 && value[0] == '1'; }(); return on; } diff --git a/runtime/src/console_heap_copy.h b/runtime/src/console_heap_copy.h index 96a1f1ad4a..7eae03aa52 100644 --- a/runtime/src/console_heap_copy.h +++ b/runtime/src/console_heap_copy.h @@ -9,7 +9,8 @@ // 0.9-1.0 s a sweep. Every CPU write goes through write() and zero(), which write both the copy and the mirror, so // view() (the mirror, or the copy when there is none) holds what the GPU reads. // -// No mirror when the copy is already host RAM (the import of the heap's own scratch), when LEONIDA_HEAP_MIRROR=0, or +// The mirror is made only with LEONIDA_HEAP_MIRROR=1 (off by default: a world run with it hung the GPU). Even then, +// no mirror when the copy is already host RAM (the import of the heap's own scratch), or // once the copy is written by the GPU pass (LEONIDA_CONSOLE_HEAP_GPU: drop_mirror(); the CPU neither writes nor reads // the copy then). The mirror covers the heap's bytes only, not the null guard after them (written once, at creation). // diff --git a/runtime/test/d3d12_console_heap_mirror_test.cpp b/runtime/test/d3d12_console_heap_mirror_test.cpp index 9a1319bb3e..3e8f3793ff 100644 --- a/runtime/test/d3d12_console_heap_mirror_test.cpp +++ b/runtime/test/d3d12_console_heap_mirror_test.cpp @@ -1,9 +1,9 @@ // The host-RAM mirror of a console descriptor heap's GPU copy (src/console_heap_copy.h) through a heap's life, with // LEONIDA_CONSOLE_RELEASE_FILTER=1 (the release sweep reads the mirror to pick the slots to fix): the first refresh, // slots the title rewrites and zeroes after it, and a released buffer whose V#s the sweep must find and null. After -// each step the GPU copy holds what it should and the mirror agrees with it byte for byte. Run twice by ctest: with -// the mirror, and with LEONIDA_HEAP_MIRROR=0 (no mirror; the sweep reads the copy, as before it), where the copy must -// come out the same. Nothing dispatches through a descriptor. No game data. +// each step the GPU copy holds what it should and the mirror agrees with it byte for byte. Run three times by ctest: +// with LEONIDA_HEAP_MIRROR=1 (the mirror), with 0 and with the variable unset (no mirror, the default; the sweep reads +// the copy), where the copy must come out the same. Nothing dispatches through a descriptor. No game data. #include "vk_test_device.h" #include "../shims/d3d12_com.h" #include "../src/gfx10_descriptors.h" @@ -183,7 +183,8 @@ int run(ID3D12Device* device, bool mirrored) { int main() { char value[4] = {}; - const bool mirrored = !(GetEnvironmentVariableA("LEONIDA_HEAP_MIRROR", value, sizeof(value)) == 1 && value[0] == '0'); + // Only LEONIDA_HEAP_MIRROR=1 mirrors: unset (the default) and 0 must leave the heap without one. + const bool mirrored = GetEnvironmentVariableA("LEONIDA_HEAP_MIRROR", value, sizeof(value)) == 1 && value[0] == '1'; vktest::Device d; const auto status = d.initialize(); if (status == vktest::DeviceStatus::unavailable) {