diff --git a/runtime/shims/d3d12_geometry_views.h b/runtime/shims/d3d12_geometry_views.h index ae473915ee..3db2765976 100644 --- a/runtime/shims/d3d12_geometry_views.h +++ b/runtime/shims/d3d12_geometry_views.h @@ -250,6 +250,38 @@ 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) { + // Looked up again, as the per-tile pass does: a tile retired since the walk is gone from tiles_. + const auto tile_it = tiles_.find(written_vas_[i]); + if (tile_it == tiles_.end()) continue; + for (const auto id : tile_it->second.views) { + if (id >= marks_.size() || (marks_[id] & kPinned) || !views_[id].alive) 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); @@ -681,6 +713,7 @@ class GeometryViews { } guard{refreshing_}; // 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'; @@ -741,6 +774,20 @@ 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) { + 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() && 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; + } + } std::size_t changed_views = 0; for (const Address va : written_vas_) { const auto tile_it = tiles_.find(va); diff --git a/runtime/test/geometry_views_test.cpp b/runtime/test/geometry_views_test.cpp index a2f2fc906f..1185076988 100644 --- a/runtime/test/geometry_views_test.cpp +++ b/runtime/test/geometry_views_test.cpp @@ -21,7 +21,9 @@ // 15. a retired view is copied once more from its pages when they are readable, not when they are not; // 16. a full release queue joins touching ranges before it collapses to one covering range; // 17. a retired view stays live while a pre-retirement command_list_epochs::Stamp is alive, and gets stamped and -// reaped once that stamp drops (a post-retirement stamp does not hold it). +// reaped once that stamp drops (a post-retirement stamp does not hold it); +// 18. two address-adjacent written tiles of one view are one 128 KiB copy (the adjacent copy-range merge); +// 19. a run is capped at 1 MiB: 32 adjacent written tiles are two 1 MiB copies, not one 2 MiB allocation. #include #include @@ -215,6 +217,24 @@ 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"); + // 18. 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"); + + // 19. 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); VirtualFree(title, 0, MEM_RELEASE);