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
112 changes: 50 additions & 62 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3366,77 +3366,59 @@ struct DeviceState {
// held.
bool resolve_pool_memory(std::uint64_t va, std::uint64_t bytes, std::uint64_t& out) {
leonida::title_gpu_views::Range view{};
if (!leonida::title_gpu_views::view_at(static_cast<std::uintptr_t>(va),
static_cast<std::uintptr_t>(std::max<std::uint64_t>(bytes, 1)), view))
return false;
const std::uint64_t offset = va >= view.gpu && va - view.gpu < view.size ? va - view.gpu : va - view.cpu;
// Rule B: the CPU view where the host reads the frames, the GPU-only pages only when that view has none.
const std::uint64_t source = leonida::title_gpu_views::to_host_view(static_cast<std::uintptr_t>(va),
static_cast<std::uintptr_t>(
std::max<std::uint64_t>(bytes, 1)));
const std::uint64_t twin = leonida::title_gpu_views::other_view(static_cast<std::uintptr_t>(source));
const std::uint64_t twin_view = twin ? twin : source; // the pair always has two addresses
if (!readable_page(source)) {
if (!leonida::title_gpu_views::view_at(static_cast<std::uintptr_t>(va), 1, view)) return false;
const auto want = std::max<std::uint64_t>(bytes, 1);
const auto unbacked = [&] {
out = 0;
++unbacked_requests;
note_unbacked("stream pool", va, bytes);
return true;
}
};
const std::uint64_t offset = va >= view.gpu && va - view.gpu < view.size ? va - view.gpu : va - view.cpu;
if (want > view.size - offset) return unbacked();
// Rule B: the CPU view where the host reads the frames, the GPU-only pages only when that view has none.
const std::uint64_t source = leonida::title_gpu_views::to_host_view(static_cast<std::uintptr_t>(va), want);
const std::uint64_t twin = leonida::title_gpu_views::other_view(static_cast<std::uintptr_t>(source));
const std::uint64_t twin_view = twin ? twin : source; // the pair always has two addresses
auto& aliases = leonida::title_va_aliases();
const auto other = source == va ? twin_view : source;
const auto max_size = std::min<std::uint64_t>(view.size - offset, kPoolExtentMax);
const auto reuse = [&] { return aliases.reuse_pool_mapping(va, other, want, max_size, out); };
if (reuse()) return true;
if (!readable_page(source)) return unbacked();
const std::uint64_t pool_end = (source >= view.gpu && source - view.gpu < view.size ? view.gpu : view.cpu) +
view.size;
const std::uint64_t run_end =
readable_run_end(source & ~(kTitleWindowAlign - 1), std::min(pool_end, source + kPoolExtentMax));
leonida::TitleWindowRange extent{};
if (!leonida::title_pool_extent(source - offset, pool_end, source, run_end, kTitleWindowAlign, kPoolExtentMax,
extent)) {
out = 0;
++unbacked_requests;
note_unbacked("stream pool", va, bytes);
return true;
}
const std::uint64_t twin_start = twin_view - (source - extent.start); // the same pages in the other view
auto& aliases = leonida::title_va_aliases();
// A mapping that already holds the extent's start (a buffer made over these pages, or a window from another
// request) serves the request only as far as it reaches: claim that much, never bytes it does not cover.
std::uint64_t shader = 0, available = 0;
if (aliases.range_of(extent.start, shader, available) ||
aliases.range_of(twin_start, shader, available)) {
if (available < extent.size) {
if (available <= source - extent.start) shader = 0;
else extent.size = available;
}
}
if (shader) {
// Both addresses must reach it. The address the mapping was found at keeps its own (possibly wider)
// range: adding a shorter one over it would drop bytes another view still uses.
if (aliases.shader_va(twin_start) == twin_start) aliases.add(twin_start, extent.size, shader);
if (aliases.shader_va(extent.start) == extent.start) aliases.add(extent.start, extent.size, shader);
} else {
bool geometry_copy = false;
if (geometry.cap() && watch_shadow_writes() && !context->knows_buffer_va(extent.start, extent.size)) {
shader = geometry.resolve(*this, extent.start, extent.size, copy_geometry);
geometry_copy = shader != 0;
}
if (!shader) shader = make_title_window(extent.start, extent.size);
if (!shader) {
out = 0;
++unbacked_requests;
note_unbacked("stream pool", va, bytes);
return true;
}
aliases.add_twin(extent.start, twin_start, extent.size, shader);
pool_spans.push_back({extent.start, twin_start, shader, extent.size, geometry_copy});
pool_span_count.store(pool_spans.size(), std::memory_order_relaxed);
pool_span_bytes += extent.size;
if (pool_spans.size() <= 4 || !(pool_spans.size() & (pool_spans.size() - 1)))
std::fprintf(stderr,
"[rt] d3d12: a %llu KiB stream-pool extent is one %s mapping (alias 0x%llx and CPU view "
"0x%llx share shader VA 0x%llx; %zu extents, %llu KiB)\n",
static_cast<unsigned long long>(extent.size >> 10),
geometry_copy ? "geometry-copy" : "window", static_cast<unsigned long long>(twin_view),
static_cast<unsigned long long>(source), static_cast<unsigned long long>(shader),
pool_spans.size(), static_cast<unsigned long long>(pool_span_bytes >> 10));
}
extent))
return unbacked();
std::uint64_t source_lo, source_hi, twin_lo, twin_hi;
aliases.gap(source, source_lo, source_hi);
aliases.gap(twin_view, twin_lo, twin_hi);
if (!leonida::clip_title_pool_extent(extent, source, twin_view, source_lo, source_hi, twin_lo, twin_hi))
return reuse() || unbacked();
if (want > std::min(extent.size - (source - extent.start), run_end - source)) return reuse() || unbacked();
const auto twin_start = twin_view - (source - extent.start);
std::uint64_t shader = 0;
if (geometry.cap() && watch_shadow_writes() && !context->knows_buffer_va(extent.start, extent.size))
shader = geometry.resolve(*this, extent.start, extent.size, copy_geometry);
const bool geometry_copy = shader != 0;
if (!shader) shader = make_title_window(extent.start, extent.size);
if (!shader || !aliases.add_twin(extent.start, twin_start, extent.size, shader))
return reuse() || unbacked();
pool_spans.push_back({extent.start, twin_start, shader, extent.size, geometry_copy});
pool_span_count.store(pool_spans.size(), std::memory_order_relaxed);
pool_span_bytes += extent.size;
if (pool_spans.size() <= 4 || !(pool_spans.size() & (pool_spans.size() - 1)))
std::fprintf(stderr,
"[rt] d3d12: a %llu KiB stream-pool extent is one %s mapping (alias 0x%llx and CPU view "
"0x%llx share shader VA 0x%llx; %zu extents, %llu KiB)\n",
static_cast<unsigned long long>(extent.size >> 10),
geometry_copy ? "geometry-copy" : "window", static_cast<unsigned long long>(twin_view),
static_cast<unsigned long long>(source), static_cast<unsigned long long>(shader),
pool_spans.size(), static_cast<unsigned long long>(pool_span_bytes >> 10));
out = shader + (source - extent.start);
return true;
}
Expand Down Expand Up @@ -3507,12 +3489,18 @@ struct DeviceState {
}
const std::uint64_t want = std::max<std::uint64_t>(bytes, 1);
std::lock_guard lock(title_window_mutex);
if (const std::uint64_t made = aliases.shader_va(va); made != va) { // another thread made it meanwhile
std::uint64_t made = 0, available = 0;
const bool has_mapping = aliases.range_of(va, made, available) && made != va;
if (has_mapping && available >= want) {
out = made;
return true;
}
// A stream pool's address: one mapping for its readable run, shared by both of its views.
if (resolve_pool_memory(va, bytes, out)) return true;
if (has_mapping) {
out = made;
return true;
}
if (readable_page(va)) {
std::uint64_t lo = 0, hi = UINT64_MAX;
aliases.gap(va, lo, hi); // never over a slot-133 buffer's range or another window
Expand Down
84 changes: 76 additions & 8 deletions runtime/src/title_va_alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,43 @@ class TitleVaAliases {
// The two addresses of one stream-pool extent (a GPU-only alias and the CPU view of the same pages, gfxgeom
// section 3): both name the same shader VA, so a title view carrying either resolves to the one mapping.
bool add_twin(std::uint64_t va, std::uint64_t twin, std::uint64_t bytes, std::uint64_t shader_va) {
if (va == twin) return add(va, bytes, shader_va);
const bool first = add(va, bytes, shader_va);
return add(twin, bytes, shader_va) && first;
if (!bytes || va > UINT64_MAX - bytes || twin > UINT64_MAX - bytes) return false;
if (va != twin && va < twin + bytes && twin < va + bytes) return false;
std::unique_lock lock(mutex_);
if (overlaps(va, va + bytes) || (va != twin && overlaps(twin, twin + bytes))) return false;
decltype(ranges_) pending;
pending.try_emplace(va, Range{va + bytes, shader_va});
pending.try_emplace(twin, Range{twin + bytes, shader_va});
publish_pool_ranges(pending);
return true;
}

bool reuse_pool_mapping(std::uint64_t va, std::uint64_t twin, std::uint64_t want, std::uint64_t max_size,
std::uint64_t& shader_va) {
if (!want) return false;
std::unique_lock lock(mutex_);
const auto next = ranges_.upper_bound(va);
if (next != ranges_.begin()) {
const auto existing = std::prev(next);
if (va < existing->second.end) {
if (want > existing->second.end - va) return false;
shader_va = existing->second.shader_va + (va - existing->first);
return true;
}
}
const auto other_next = ranges_.upper_bound(twin);
if (other_next == ranges_.begin()) return false;
const auto other = std::prev(other_next);
if (twin >= other->second.end) return false;
const auto end = next == ranges_.end() ? UINT64_MAX : next->first;
const auto bytes = std::min({max_size, other->second.end - twin, end - va});
if (bytes < want) return false;
const auto shader = other->second.shader_va + (twin - other->first);
decltype(ranges_) pending;
pending.try_emplace(va, Range{va + bytes, shader});
publish_pool_ranges(pending);
shader_va = shader;
return true;
}

// Forgets the range that starts at title_va, if it still maps to shader_va (a newer range at the same VA keeps
Expand All @@ -92,6 +126,16 @@ class TitleVaAliases {
void forget_copy(std::uint64_t title_va, std::uint64_t bytes, std::uint64_t shader_va) {
if (!bytes) return;
std::unique_lock lock(mutex_);
for (auto it = ranges_.lower_bound(title_va); it != ranges_.end() && it->first - title_va < bytes;) {
if (it->second.shader_va - shader_va != it->first - title_va) {
++it;
continue;
}
by_shader_.erase(it->second.shader_va, it->first);
notify(Change::removed, it->first, it->second.end - it->first, it->second.shader_va);
it = ranges_.erase(it);
}
count_.store(ranges_.size(), std::memory_order_release);
notify(Change::removed, title_va, bytes, shader_va);
note_removed(shader_va, bytes);
generation_.fetch_add(1, std::memory_order_release);
Expand Down Expand Up @@ -177,11 +221,12 @@ class TitleVaAliases {
// read or write). Counted by kind: translated, or in the title's memory and left as it was.
std::uint64_t resolve(std::uint64_t va, std::uint64_t bytes, Kind kind) {
if (!va) return va;
std::uint64_t to = shader_va(va);
if (to != va) {
std::uint64_t mapped = va, available = 0;
const bool has_mapping = range_of(va, mapped, available) && mapped != va;
if (has_mapping && bytes <= available) {
translated_[kind].fetch_add(1, std::memory_order_relaxed);
note_reached(va, bytes);
return to;
return mapped;
}
Resolver resolver;
void* context;
Expand All @@ -190,9 +235,13 @@ class TitleVaAliases {
resolver = resolver_;
context = hook_context_;
}
if (!resolver || !resolver(context, va, bytes, to)) return va;
std::uint64_t to = va;
if (!resolver || !resolver(context, va, bytes, to)) {
if (!has_mapping) return va;
to = mapped;
}
(to != va ? translated_ : untranslated_)[kind].fetch_add(1, std::memory_order_relaxed);
if (to != va) note_reached(va, bytes);
if (to && to != va) note_reached(va, bytes);
return to;
}

Expand Down Expand Up @@ -423,6 +472,25 @@ class TitleVaAliases {
std::uint64_t shader_va; // what the range's first byte becomes
};

void publish_pool_ranges(std::map<std::uint64_t, Range>& pending) {
try {
for (const auto& [base, range] : pending) by_shader_.insert(range.shader_va, range.end - base, base);
} catch (...) {
for (const auto& [base, range] : pending) by_shader_.erase(range.shader_va, base);
throw;
}
for (const auto& [base, range] : pending) notify(Change::added, base, range.end - base, range.shader_va);
ranges_.merge(pending);
count_.store(ranges_.size(), std::memory_order_release);
generation_.fetch_add(1, std::memory_order_release);
}

bool overlaps(std::uint64_t begin, std::uint64_t end) const {
const auto next = ranges_.lower_bound(begin);
return (next != ranges_.end() && next->first < end) ||
(next != ranges_.begin() && std::prev(next)->second.end > begin);
}

void erase_overlapping(std::uint64_t begin, std::uint64_t end) {
auto it = ranges_.lower_bound(begin);
if (it != ranges_.begin()) {
Expand Down
13 changes: 13 additions & 0 deletions runtime/src/title_window_plan.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,17 @@ inline bool title_pool_extent(std::uint64_t pool_lo, std::uint64_t pool_hi, std:
return true;
}

inline bool clip_title_pool_extent(TitleWindowRange& extent, std::uint64_t source, std::uint64_t twin,
std::uint64_t source_lo, std::uint64_t source_hi,
std::uint64_t twin_lo, std::uint64_t twin_hi) {
if (source < source_lo || source >= source_hi || twin < twin_lo || twin >= twin_hi ||
source < extent.start || source - extent.start >= extent.size)
return false;
const auto prefix = source - extent.start;
const auto before = std::min({prefix, source - source_lo, twin - twin_lo});
const auto after = std::min({extent.size - prefix, source_hi - source, twin_hi - twin});
extent = {source - before, before + after};
return true;
}

} // namespace leonida
14 changes: 14 additions & 0 deletions runtime/test/d3d12_title_va_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,18 @@ int check_stream_pool(Context& c, int& checks) {
aliases.add_title_memory(gpu_va, pool_bytes);
EXPECT(leonida::title_gpu_views::to_host_view(gpu_va + 0x2000, 0x100) == cpu_va + 0x2000);

constexpr UINT64 seam = pool_bytes / 2;
constexpr UINT64 existing = 0x100000;
const auto root = leonida::TitleVaAliases::root_view;
EXPECT(aliases.add(cpu_va + seam, 0x1000, existing));
const auto ranges_at_seam = aliases.size();
EXPECT(aliases.resolve(gpu_va + seam - 0x100, 0x200, root) == 0);
EXPECT(aliases.resolve(gpu_va + seam, 0x2000, root) == 0);
EXPECT(aliases.resolve(cpu_va + seam, 0x2000, root) == 0);
EXPECT(aliases.size() == ranges_at_seam);
EXPECT(aliases.shader_va(cpu_va + seam) == existing);
aliases.remove(cpu_va + seam, existing);

const D3D12_DESCRIPTOR_HEAP_DESC heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 4,
D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, 0};
Ref<ID3D12DescriptorHeap> heap;
Expand All @@ -984,6 +996,8 @@ int check_stream_pool(Context& c, int& checks) {
EXPECT(aliases.shader_va(gpu_va + 0x2000) == aliases.shader_va(cpu_va + 0x2000));
// Two ranges (the two views' addresses), each a page-aligned run of the pool: one mapping, not one per view.
EXPECT(aliases.size() == ranges_before + 2);
EXPECT(aliases.resolve(gpu_va + pool_bytes - 0x100, 0x200, root) == 0);
EXPECT(aliases.size() == ranges_before + 2);
std::printf("stream pool: GPU-only alias 0x%llx and CPU view 0x%llx share one %llu KiB mapping (V# base "
"0x%llx); a second view naming the twin made no mapping\n",
static_cast<unsigned long long>(gpu_va), static_cast<unsigned long long>(cpu_va),
Expand Down
Loading
Loading