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
16 changes: 8 additions & 8 deletions runtime/shims/d3d12_geometry_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,17 +543,17 @@ class GeometryViews {
template<class Device, class OnRetire = NoopRetire, class Copy = NoCopy>
void drain_released(Device& device, std::uint64_t retire_ticket, std::uint64_t completed_ticket,
OnRetire&& on_retire = {}, Copy&& final_copy = {}) {
std::vector<std::pair<Address, Address>> ranges;
std::vector<std::pair<Address, Address>> ranges, at_cpu;
leonida::geometry_writes::take_released(releases_, ranges);
for (const auto& [begin, end] : ranges) {
retire_range(begin, end, retire_ticket, on_retire, final_copy);
leonida::title_gpu_views::Range pair{};
if (end > begin && leonida::title_gpu_views::view_at(begin, 1, pair) &&
begin >= pair.gpu && begin - pair.gpu < pair.size) {
const Address offset = begin - pair.gpu;
const Address span = (std::min)(end - begin, pair.size - offset);
retire_range(pair.cpu + offset, pair.cpu + offset + span, retire_ticket, on_retire, final_copy);
}
// The part of the range in each GPU-only twin it overlaps, at the twin's CPU view (where the views are
// keyed); collected first, as the twins' lock is not held while views are retired.
at_cpu.clear();
leonida::title_gpu_views::for_each_gpu_overlap(begin, end, [&](std::uintptr_t lo, std::uintptr_t hi) {
at_cpu.emplace_back(Address(lo), Address(hi));
});
for (const auto& [lo, hi] : at_cpu) retire_range(lo, hi, retire_ticket, on_retire, final_copy);
}
reap(device, completed_ticket);
}
Expand Down
14 changes: 7 additions & 7 deletions runtime/src/title_geometry_watch.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ inline void note_released(std::uintptr_t begin, std::uintptr_t end) {
const std::uintptr_t hi = s.watched_hi.load(std::memory_order_acquire);
if (end <= lo || begin >= hi) {
// Views are keyed by the CPU view's address, so a GPU-only range freed by its own address counts through its
// CPU view (GeometryViews::drain_released maps it there).
leonida::title_gpu_views::Range pair{};
if (!leonida::title_gpu_views::view_at(begin, 1, pair) || begin < pair.gpu || begin - pair.gpu >= pair.size)
return;
const std::uintptr_t cpu = pair.cpu + (begin - pair.gpu);
const std::uintptr_t span = (std::min)(end - begin, pair.size - (begin - pair.gpu));
if (cpu + span <= lo || cpu >= hi) return;
// CPU view (GeometryViews::drain_released maps it there): every pair the range overlaps, also one it starts
// before or one after the first.
bool watched = false;
leonida::title_gpu_views::for_each_gpu_overlap(begin, end, [&](std::uintptr_t cpu_lo, std::uintptr_t cpu_hi) {
if (cpu_hi > lo && cpu_lo < hi) watched = true;
});
if (!watched) return;
}
std::lock_guard subscribers_lock(s.subscribers_mutex);
for (ReleaseQueue* q : s.subscribers) {
Expand Down
16 changes: 16 additions & 0 deletions runtime/src/title_page_watch.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,22 @@ inline bool view_at(std::uintptr_t va, std::uintptr_t bytes, Range& out) {
return false;
}

// For every pair whose GPU-only range overlaps [begin, end): f(cpu_begin, cpu_end), the overlapping part named at the
// pair's CPU view. A range may start before a pair or cover several (a release of a wider span, a collapsed release
// queue). `f` runs under the views' shared lock: it only computes or records, it takes no other lock.
template <class F>
inline void for_each_gpu_overlap(std::uintptr_t begin, std::uintptr_t end, F&& f) {
State& s = state();
if (end <= begin || !s.count.load(std::memory_order_acquire)) return;
std::shared_lock lock(s.mutex);
for (const View& v : s.views) {
const std::uintptr_t v_end = v.gpu + v.size;
if (begin >= v_end || end <= v.gpu) continue;
const std::uintptr_t lo = (std::max)(begin, v.gpu), hi = (std::min)(end, v_end);
f(v.cpu + (lo - v.gpu), v.cpu + (hi - v.gpu));
}
}

// The other address of memory in a paired range: the CPU view's address for a GPU-only one and the GPU-only
// address for a CPU view's; 0 outside every pair.
inline std::uintptr_t other_view(std::uintptr_t va) {
Expand Down
20 changes: 20 additions & 0 deletions runtime/test/geometry_views_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,26 @@ int main() {
"draining the twin's range retires the view at its CPU address only");
const std::uint64_t va_w2b = watched.resolve(device, pbase + kView, kView, copy);
check(va_w2b != 0, "the retired view is made again on the next resolve");
// A GPU-only release that starts before the twin and runs into it: its part in the twin counts.
leonida::geometry_writes::note_released(gpu_twin - 4096, gpu_twin + 4096);
check(watched.has_released(), "a range starting before the GPU-only twin and overlapping it is queued");
watched.drain_released(device, 4, 4);
check(watched.find(pbase, kView) == 0 && watched.find(pbase + kView, kView) == va_w2b,
"draining it retires the view under the overlapped part only");
const std::uint64_t va_w1b = watched.resolve(device, pbase, kView, copy);
check(va_w1b != 0, "that view is made again");
// Two adjacent twins: a release from the first into the second retires the views under both CPU views.
leonida::title_gpu_views::note_reservation(pbase + 2 * kView, kView, false);
check(leonida::title_gpu_views::note_reservation(gpu_twin + 2 * kView, kView, true), "a second, adjacent twin pairs");
const std::uint64_t va_w3 = watched.resolve(device, pbase + 2 * kView, kView, copy);
check(va_w3 != 0 && watched.live_views() == 3, "a view under the second twin's CPU view");
leonida::geometry_writes::note_released(gpu_twin + kView, gpu_twin + 3 * kView);
watched.drain_released(device, 5, 5);
check(watched.find(pbase + kView, kView) == 0 && watched.find(pbase + 2 * kView, kView) == 0 &&
watched.find(pbase, kView) == va_w1b,
"a release spanning two twins retires the views under both, and not the one outside it");
const std::uint64_t va_w2c = watched.resolve(device, pbase + kView, kView, copy);
check(va_w2c != 0 && watched.live_views() == 2, "the first twin's view is made again; two live views as before");

// 9. Queue overflow: 65537 disjoint ranges collapse into one covering range; drain retires every covered view.
constexpr std::size_t kOverflowCount = 65537;
Expand Down
Loading