Skip to content
Merged
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
23 changes: 17 additions & 6 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2055,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.
Expand Down
6 changes: 5 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down Expand Up @@ -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. |
Expand Down
7 changes: 4 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 16 additions & 6 deletions runtime/shims/xmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1039,13 +1039,21 @@ 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 once; rt::xmem::reload_remap_release_setting reads it again (for a test).
std::atomic<int> g_remap_releases{-1};
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;
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;
Expand Down Expand Up @@ -2421,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);
Expand Down
2 changes: 2 additions & 0 deletions runtime/shims/xmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::uintptr_t>& 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<std::uint8_t> pin_mapped_data(const void* address, std::size_t size);
Expand Down
3 changes: 2 additions & 1 deletion runtime/src/console_heap_copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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).
//
Expand Down
9 changes: 5 additions & 4 deletions runtime/test/d3d12_console_heap_mirror_test.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand Down
Loading
Loading