Skip to content
Open
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
49 changes: 47 additions & 2 deletions runtime/shims/d3d12_geometry_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<class Copy>
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<const std::uint8_t*>(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<class Copy>
void fill(View& v, Copy& copy) {
const auto result = copy(v.dst, reinterpret_cast<const std::uint8_t*>(v.source), v.size);
Expand Down Expand Up @@ -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';
Expand All @@ -243,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
Expand All @@ -267,6 +300,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 (std::size_t w = 0; w < written_vas_.size(); ++w) {
const Address va = written_vas_[w];
Expand Down Expand Up @@ -309,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();
Expand Down
22 changes: 21 additions & 1 deletion runtime/test/geometry_views_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
// 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);
// 6. a run is capped at 1 MiB: 32 adjacent written tiles are two 1 MiB copies, not one 2 MiB allocation.
#include <windows.h>

#include <cstdint>
Expand Down Expand Up @@ -160,6 +162,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");

// 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");

// 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;
Expand Down