From 194265665cda15e2f74c166d84fcd7c018fcb8ca Mon Sep 17 00:00:00 2001 From: BubsMcgee Date: Wed, 25 Nov 2026 14:50:17 -0500 Subject: [PATCH 1/2] d3d12: one guarded copy per run of adjacent written geometry tiles (RT-GEOMETRY-READBACK-RUN-MERGE) A GeometryViews refresh reads each written 64 KiB tile with its own readability probe and guarded copy, even when the title wrote a run of adjacent tiles across tile boundaries. Group the written tiles (already in address order) into maximal runs whose addresses differ by one tile and read each run a ready view needs as one guarded copy; the per-tile pass then reuses those bytes through read_tile(). A run that cannot be read whole falls back to the per-tile read, so an uncommitted page still zeroes only what it did before. Same behaviour as DexNom/VIctory's copy_fetch_runs (crates/victory-gpu/src/host.rs, GPL-3.0-or-later, commit 570a941), re-implemented for this header; no identifiers or code copied. Tested: geometry_views_test 14/14, including the new case (two adjacent written tiles are one 128 KiB copy); gta6rt_d3d12_com builds. Synthetic only, no game files or GPU. AI-assisted. --- runtime/shims/d3d12_geometry_views.h | 41 ++++++++++++++++++++++++++++ runtime/test/geometry_views_test.cpp | 12 +++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/runtime/shims/d3d12_geometry_views.h b/runtime/shims/d3d12_geometry_views.h index a9299d38cb..ca7685afe5 100644 --- a/runtime/shims/d3d12_geometry_views.h +++ b/runtime/shims/d3d12_geometry_views.h @@ -126,6 +126,35 @@ class GeometryViews { } return it->second == SIZE_MAX ? nullptr : tile_scratch_.data() + it->second; } + // A run of address-adjacent written tiles as one guarded copy, the way the Radeon runtime joins neighboring fetch + // ranges (DexNom/VIctory host.rs copy_fetch_runs, GPL-3.0-or-later): the title writes across 64 KiB boundaries, so + // one copy of the run skips a readability probe and a guarded copy per boundary. The tiles' read offsets are + // recorded so the per-tile read_tile() below reuses them; a run it cannot read whole is read tile by tile instead. + template + bool read_run(std::size_t first, std::size_t last, Copy& copy) { + const std::size_t at = tile_scratch_.size(); + const std::size_t bytes = (last - first) * tile_size; + tile_scratch_.resize(at + bytes); + if (copy(tile_scratch_.data() + at, reinterpret_cast(written_vas_[first]), bytes) != + GeometryCopy::copied) { + tile_scratch_.resize(at); + return false; + } + for (std::size_t i = first; i < last; ++i) + tile_reads_[written_vas_[i]] = at + (i - first) * tile_size; + return true; + } + // Whether any view over the run [first, last) wants a tile copy (not pinned, not whole, filled already): a run no + // view needs is not read. + bool run_needed(std::size_t first, std::size_t last) const { + for (std::size_t i = first; i < last; ++i) { + for (const auto id : written_[i]->views) { + if (id >= marks_.size() || (marks_[id] & kPinned)) continue; + if (views_[id].ready) return true; + } + } + return false; + } template void fill(View& v, Copy& copy) { const auto result = copy(v.dst, reinterpret_cast(v.source), v.size); @@ -234,6 +263,7 @@ class GeometryViews { retry_ = false; // The views over each tile written since, with those tiles (in address order, as tiles_ is): a ready view // copies only them, 64 KiB each, not the whole view again (LEONIDA_GEOMETRY_WHOLE_VIEWS=1: whole views). + // Neighboring written tiles are read as one run (read_run below), the copy-range merge VIctory uses. static const bool whole = [] { const char* v = std::getenv("LEONIDA_GEOMETRY_WHOLE_VIEWS"); return v && *v && *v != '0'; @@ -267,6 +297,17 @@ class GeometryViews { const std::size_t changed_tiles = written_vas_.size(); tile_reads_.clear(); tile_scratch_.clear(); + // Adjacent written tiles are one guarded copy. The written tiles are in address order, so group them into runs + // whose addresses differ by a tile, and read each run some view needs before the per-tile pass, which then + // reuses its bytes through read_tile(). Whole-view refreshes read nothing here: they fill whole views below. + if (!whole) { + for (std::size_t w = 0; w < written_vas_.size();) { + std::size_t e = w + 1; + while (e < written_vas_.size() && written_vas_[e] == written_vas_[e - 1] + tile_size) ++e; + if (e - w > 1 && run_needed(w, e)) read_run(w, e, copy); + w = e; + } + } std::size_t changed_views = 0; for (std::size_t w = 0; w < written_vas_.size(); ++w) { const Address va = written_vas_[w]; diff --git a/runtime/test/geometry_views_test.cpp b/runtime/test/geometry_views_test.cpp index 920283f41c..33d67d3542 100644 --- a/runtime/test/geometry_views_test.cpp +++ b/runtime/test/geometry_views_test.cpp @@ -4,7 +4,8 @@ // 1. a new view is copied whole; // 2. a write into one tile of a 1 MiB view (16 tiles) copies that tile's 64 KiB again, not the view; // 3. a write into a tile two views share is read once and copied into both; -// 4. two written tiles of one view are two tile copies (LEONIDA_GEOMETRY_WHOLE_VIEWS=1, whole views, fails 2-4). +// 4. two written tiles of one view are two tile copies (LEONIDA_GEOMETRY_WHOLE_VIEWS=1, whole views, fails 2-4); +// 5. two address-adjacent written tiles of one view are one 128 KiB copy (the adjacent copy-range merge). #include #include @@ -160,6 +161,15 @@ int main() { check(g_copies.size() == 2 && g_copies[0] == kTile && g_copies[1] == kTile, "two written tiles, two tile copies"); check(!std::memcmp(copy_bytes, title, kView), "the copy matches the title's pages"); + // 5. Writes in two address-adjacent tiles of one view: one 128 KiB copy, not two 64 KiB ones. + g_copies.clear(); + title_write(base + 6 * kTile + 7, 3); + title_write(base + 7 * kTile + 11, 4); + views.refresh(copy); + check(g_copies.size() == 1 && g_copies[0] == 2 * kTile, "two adjacent written tiles are one 128 KiB copy"); + check(copy_bytes[6 * kTile + 7] == 3 && copy_bytes[7 * kTile + 11] == 4, "both adjacent tiles reached the copy"); + check(!std::memcmp(copy_bytes, title, kView), "the copy matches the title's pages after the merged run"); + views.release(device); std::printf("%d checks, %d failures\n", g_checks, g_failures); return g_failures ? 1 : 0; From 82169e0e0311f61bed2ef6fd8561ee36bb681f2b Mon Sep 17 00:00:00 2001 From: BubsMcgee Date: Wed, 25 Nov 2026 16:03:59 -0500 Subject: [PATCH 2/2] d3d12: clear geometry readback lists at refresh start and cap runs at 1 MiB Each refresh now clears written_ and written_vas_ together at the start, so an exception from a read or a guarded copy cannot leave addresses the next refresh indexes past written_. Adjacent-tile runs are capped at 1 MiB (16 tiles), so a long contiguous write cannot force one large scratch resize. --- runtime/shims/d3d12_geometry_views.h | 10 +++++++--- runtime/test/geometry_views_test.cpp | 12 +++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/runtime/shims/d3d12_geometry_views.h b/runtime/shims/d3d12_geometry_views.h index ca7685afe5..5e9391220e 100644 --- a/runtime/shims/d3d12_geometry_views.h +++ b/runtime/shims/d3d12_geometry_views.h @@ -273,7 +273,10 @@ class GeometryViews { // 30-80 ms). A view over a tile that cannot be guarded yet (retry) is left as it is this time; a view not // filled yet is filled whole, once. marks_.assign(views_.size(), 0); + // Both lists are cleared here, at the start: if a read or a guarded copy below throws, the next refresh must + // not find addresses in written_vas_ whose tiles written_ no longer holds. written_.clear(); + written_vas_.clear(); // The pass over the watched tiles makes a VirtualQuery and a VirtualProtect for each written one (tens of // milliseconds to a few hundred under Wine). resolve() waits for mutex_, and a new V# resolved while a // submission fixes its console heap slots then waited for the whole pass (speed-35: 116 ms of fixing a @@ -301,9 +304,12 @@ class GeometryViews { // whose addresses differ by a tile, and read each run some view needs before the per-tile pass, which then // reuses its bytes through read_tile(). Whole-view refreshes read nothing here: they fill whole views below. if (!whole) { + constexpr std::size_t kMaxRunBytes = 1u << 20; // as the stream pool's extents: no run allocates more + const std::size_t max_tiles = (std::max)(std::size_t(1), kMaxRunBytes / tile_size); for (std::size_t w = 0; w < written_vas_.size();) { std::size_t e = w + 1; - while (e < written_vas_.size() && written_vas_[e] == written_vas_[e - 1] + tile_size) ++e; + while (e < written_vas_.size() && e - w < max_tiles && written_vas_[e] == written_vas_[e - 1] + tile_size) + ++e; if (e - w > 1 && run_needed(w, e)) read_run(w, e, copy); w = e; } @@ -350,8 +356,6 @@ class GeometryViews { } for (std::size_t id = 0; id < marks_.size(); ++id) if ((marks_[id] & (kWhole | kPinned)) == kWhole) fill(views_[id], copy); - written_.clear(); - written_vas_.clear(); if (uploaded_ != before) ++dirty_refreshes_; refilled_ += uploaded_ - before; const auto end = std::chrono::steady_clock::now(); diff --git a/runtime/test/geometry_views_test.cpp b/runtime/test/geometry_views_test.cpp index 33d67d3542..44c08568c9 100644 --- a/runtime/test/geometry_views_test.cpp +++ b/runtime/test/geometry_views_test.cpp @@ -5,7 +5,8 @@ // 2. a write into one tile of a 1 MiB view (16 tiles) copies that tile's 64 KiB again, not the view; // 3. a write into a tile two views share is read once and copied into both; // 4. two written tiles of one view are two tile copies (LEONIDA_GEOMETRY_WHOLE_VIEWS=1, whole views, fails 2-4); -// 5. two address-adjacent written tiles of one view are one 128 KiB copy (the adjacent copy-range merge). +// 5. two address-adjacent written tiles of one view are one 128 KiB copy (the adjacent copy-range merge); +// 6. a run is capped at 1 MiB: 32 adjacent written tiles are two 1 MiB copies, not one 2 MiB allocation. #include #include @@ -170,6 +171,15 @@ int main() { check(copy_bytes[6 * kTile + 7] == 3 && copy_bytes[7 * kTile + 11] == 4, "both adjacent tiles reached the copy"); check(!std::memcmp(copy_bytes, title, kView), "the copy matches the title's pages after the merged run"); + // 6. A run is capped at 1 MiB: 32 adjacent written tiles are two 1 MiB copies, not one 2 MiB allocation. + const std::uint64_t va3 = views.resolve(device, base, 32 * kTile, copy); + check(va3 != 0, "a two-MiB view over the watched tiles"); + g_copies.clear(); + for (std::size_t t = 0; t < 32; ++t) title_write(base + t * kTile + t, std::uint8_t(0x40 + t)); + views.refresh(copy); + check(g_copies.size() == 2 && g_copies[0] == 16 * kTile && g_copies[1] == 16 * kTile, + "a 32-tile adjacent run is two 1 MiB copies"); + views.release(device); std::printf("%d checks, %d failures\n", g_checks, g_failures); return g_failures ? 1 : 0;