mirror of
https://github.com/Eglantine-Community/Eglantine.git
synced 2026-11-23 05:39:37 -10:00
nvn: cut per-draw replay costs that slowed Ocarina of Time HLE to ~9 fps
Ocarina of Time on VTX-HLE dropped from ~40 fps (df590b2009) to ~9 fps in Hyrule Field with a CPU-bound replay thread. Three costs added since then: - Pending GPU pool writes (15b4bde789,c2c72c2375): up to 4096 entries were walked for every uniform binding, handle word read and pool write. Keep a 16 KiB block index updated incrementally; removed writes stay as dead entries until compaction. Prune only when gpu_completed_seq moves. - Bindless table (359335ad98): every entry was rebuilt about once per present. Entries remember what they were written from and are skipped when unchanged; BindlessPrime stops after an empty lap per submission. - Texture trust: a completed raw global store marked every page of every split pool as CPU-written, so each check re-hashed whole textures with no budget. A raw store now sends the pool's textures through the untracked check (signature, full compare for small uncompressed ones, budgeted full compare otherwise). Tracked CPU writes are still compared page by page. - Check clock: an "ordinary frame" is max(64 ms, 2x the recent frame time), so at low frame rates the clock no longer runs several ticks per present. OoT Hyrule Field: ~9 -> 18-20 fps. Emulated path unchanged. Tests [nvn],[vtxhle]: same 10 known failures as before. Investigation and most of the fix: Claude Fable 5.2.
This commit is contained in:
1 parent
04d599c17c
commit
b7600e58d3
6 files changed
+425
-82
No files matched your search
@@ -248,22 +248,83 @@ void DrawContext::BindlessSync(bool force) {
|
||||
std::vector<Run> runs;
|
||||
std::vector<VkDescriptorImageInfo> infos;
|
||||
std::vector<std::shared_ptr<Storage>> storages;
|
||||
storages.reserve(state.storages.size());
|
||||
std::array<VkImageView, TEXTURE_BINDINGS + 1> want{};
|
||||
std::array<VkImageLayout, TEXTURE_BINDINGS + 1> want_layout{};
|
||||
// The textures erased since the last sync: their node pointers are dead. With more erased
|
||||
// than the ring holds, every entry is looked up again.
|
||||
bool lookup_all = state.entries.empty();
|
||||
if (lookup_all) {
|
||||
state.entries.resize(Shader::BINDLESS_TEXTURE_CAPACITY);
|
||||
}
|
||||
thread_local std::vector<u64> erased;
|
||||
erased.clear();
|
||||
if (!lookup_all) {
|
||||
const u64 erases = impl.texture_epoch - state.entries_epoch;
|
||||
if (erases > impl.texture_erased.size()) {
|
||||
lookup_all = true;
|
||||
} else {
|
||||
for (u64 n = state.entries_epoch - 1; n != impl.texture_epoch - 1; ++n) {
|
||||
erased.push_back(impl.texture_erased[n % impl.texture_erased.size()]);
|
||||
}
|
||||
std::ranges::sort(erased);
|
||||
}
|
||||
}
|
||||
state.entries_epoch = impl.texture_epoch;
|
||||
const u64 erased_min = erased.empty() ? ~u64{0} : erased.front();
|
||||
const u64 erased_max = erased.empty() ? 0 : erased.back();
|
||||
const auto has_data = [](const Storage& s) {
|
||||
// Only what has an image holding the game's data: the table never creates one (a texture
|
||||
// the game registers and never uses would cost its memory).
|
||||
return s.image && !s.image_failed && s.contents_valid && s.samples == VK_SAMPLE_COUNT_1_BIT;
|
||||
};
|
||||
for (u32 id = 0; id < count; ++id) {
|
||||
// Pool slot 0 is "no texture" (the rebased handle keeps id 0 at 0).
|
||||
const u64 serial = id != 0 && id < texture_ids.size() ? texture_ids[id] : 0;
|
||||
BindlessState::Entry& entry = state.entries[id];
|
||||
if (!lookup_all && serial == entry.serial) {
|
||||
if (serial == 0) {
|
||||
continue; // (written as the stand-ins when the registration went)
|
||||
}
|
||||
const bool alive =
|
||||
entry.texture != nullptr &&
|
||||
(serial < erased_min || serial > erased_max ||
|
||||
!std::ranges::binary_search(erased, serial));
|
||||
if (alive) {
|
||||
const Texture& texture = **entry.texture;
|
||||
const Storage* const s = texture.storage.get();
|
||||
if (s == entry.storage &&
|
||||
(s == nullptr || (s->image_gen == entry.image_gen &&
|
||||
s->host.format == entry.format &&
|
||||
has_data(*s) == entry.ready &&
|
||||
(!entry.ready || ReadLayoutOf(*s) == entry.layout)))) {
|
||||
if (entry.any) {
|
||||
storages.push_back(texture.storage);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (u32 binding = 1; binding <= TEXTURE_BINDINGS; ++binding) {
|
||||
want[binding] = state.dummy[binding];
|
||||
want_layout[binding] = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
}
|
||||
// Pool slot 0 is "no texture" (the rebased handle keeps id 0 at 0).
|
||||
if (id != 0 && id < texture_ids.size() && texture_ids[id] != 0) {
|
||||
entry = {};
|
||||
entry.serial = serial;
|
||||
if (serial != 0) {
|
||||
const std::shared_ptr<Texture>& texture = TextureOfId(id);
|
||||
if (texture) {
|
||||
// (TextureOfId returns the node of Impl::textures)
|
||||
entry.texture = &texture;
|
||||
entry.storage = texture->storage.get();
|
||||
}
|
||||
if (texture && texture->storage) {
|
||||
Storage& s = *texture->storage;
|
||||
// Only what has an image holding the game's data: the table never creates one
|
||||
// (a texture the game registers and never uses would cost its memory).
|
||||
if (s.image && !s.image_failed && s.contents_valid &&
|
||||
s.samples == VK_SAMPLE_COUNT_1_BIT) {
|
||||
entry.image_gen = s.image_gen;
|
||||
entry.format = s.host.format;
|
||||
entry.ready = has_data(s);
|
||||
if (entry.ready) {
|
||||
entry.layout = ReadLayoutOf(s);
|
||||
const s32 target = texture->desc.view.set_target ? texture->desc.view.target
|
||||
: texture->desc.target;
|
||||
const bool is_integer =
|
||||
@@ -279,9 +340,11 @@ void DrawContext::BindlessSync(bool force) {
|
||||
}
|
||||
const VkImageView view = resources.GetView(*texture, key);
|
||||
if (!view) {
|
||||
// (not remembered: the next sync asks for the view again)
|
||||
entry.texture = nullptr;
|
||||
continue;
|
||||
}
|
||||
const VkImageLayout layout = ReadLayoutOf(s);
|
||||
const VkImageLayout layout = entry.layout;
|
||||
for (const u32 klass : {0u, 1u, 2u}) {
|
||||
// Float and depth-compare samplers share the views; an integer
|
||||
// sampler takes only an integer format.
|
||||
@@ -295,6 +358,7 @@ void DrawContext::BindlessSync(bool force) {
|
||||
any = true;
|
||||
}
|
||||
if (any) {
|
||||
entry.any = true;
|
||||
storages.push_back(texture->storage);
|
||||
}
|
||||
}
|
||||
@@ -430,7 +494,8 @@ void DrawContext::BindlessPrime(DrawPlan& plan) {
|
||||
state.prime_bytes = 0;
|
||||
state.prime_count = 0;
|
||||
}
|
||||
if (budget_bytes == 0 || state.prime_count >= BUDGET_COUNT || state.prime_bytes >= budget_bytes) {
|
||||
if (budget_bytes == 0 || state.prime_count >= BUDGET_COUNT || state.prime_bytes >= budget_bytes ||
|
||||
state.prime_exhausted_seq == seq) {
|
||||
return;
|
||||
}
|
||||
const std::vector<u64>& texture_ids = submission->texture_pool_snapshot.Values();
|
||||
@@ -466,10 +531,15 @@ void DrawContext::BindlessPrime(DrawPlan& plan) {
|
||||
state.dirty = true;
|
||||
if (++state.prime_count >= BUDGET_COUNT || state.prime_bytes >= budget_bytes) {
|
||||
id = id + 1 < count ? id + 1 : 1;
|
||||
break;
|
||||
state.prime_cursor = id;
|
||||
return;
|
||||
}
|
||||
}
|
||||
state.prime_cursor = id;
|
||||
// A whole lap found nothing more to create: the registrations change only between
|
||||
// submissions, so the rest of this one skips the walk. Every bindless draw walked the whole
|
||||
// pool once each texture had its image (Ocarina of Time: ~13% of the replay thread).
|
||||
state.prime_exhausted_seq = seq;
|
||||
}
|
||||
|
||||
bool DrawContext::BindlessPrepare(DrawPlan& plan) {
|
||||
|
||||
@@ -2372,12 +2372,156 @@ void Context::CollectStats(std::vector<VideoCommon::Nvn::SinkStat>& out) const {
|
||||
out.push_back({"raw_global_ranges_dropped", s.raw_global_ranges_dropped});
|
||||
}
|
||||
|
||||
static void CollapsePendingPoolWrites(std::vector<Context::Impl::PendingPoolWrite>& writes) {
|
||||
if (writes.size() < 4096) {
|
||||
namespace {
|
||||
|
||||
constexpr u32 PENDING_BLOCK_SHIFT = 14;
|
||||
constexpr u64 PENDING_MAX_BLOCKS = 64;
|
||||
constexpr size_t PENDING_MAX_WRITES = 4096;
|
||||
|
||||
bool PendingLive(const Context::Impl::PendingPoolWrite& write) {
|
||||
return write.end > write.begin;
|
||||
}
|
||||
|
||||
} // Anonymous namespace
|
||||
|
||||
template <typename Visitor>
|
||||
bool Context::Impl::VisitPendingPoolWrites(u64 begin, u64 end, Visitor&& visit) const {
|
||||
if (end <= begin || pending_pool_writes.size() == pending_dead) {
|
||||
return false;
|
||||
}
|
||||
const u64 first = begin >> PENDING_BLOCK_SHIFT;
|
||||
const u64 last = (end - 1) >> PENDING_BLOCK_SHIFT;
|
||||
if (last - first >= PENDING_MAX_BLOCKS) {
|
||||
for (u32 index = 0; index < static_cast<u32>(pending_pool_writes.size()); ++index) {
|
||||
if (PendingLive(pending_pool_writes[index]) && visit(index)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
for (const u32 index : pending_wide) {
|
||||
if (visit(index)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (u64 block = first; block <= last; ++block) {
|
||||
const auto it = pending_blocks.find(block);
|
||||
if (it == pending_blocks.end()) {
|
||||
continue;
|
||||
}
|
||||
for (const u32 index : it->second) {
|
||||
if (visit(index)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Context::Impl::IndexPendingPoolWrite(u32 index, bool add) {
|
||||
const PendingPoolWrite& write = pending_pool_writes[index];
|
||||
const auto apply = [&](std::vector<u32>& list) {
|
||||
if (add) {
|
||||
list.push_back(index);
|
||||
} else if (const auto it = std::ranges::find(list, index); it != list.end()) {
|
||||
*it = list.back();
|
||||
list.pop_back();
|
||||
}
|
||||
};
|
||||
const u64 first = write.begin >> PENDING_BLOCK_SHIFT;
|
||||
const u64 last = (write.end - 1) >> PENDING_BLOCK_SHIFT;
|
||||
if (last - first >= PENDING_MAX_BLOCKS) {
|
||||
apply(pending_wide);
|
||||
return;
|
||||
}
|
||||
std::vector<Context::Impl::PendingPoolWrite> collapsed;
|
||||
for (const Context::Impl::PendingPoolWrite& write : writes) {
|
||||
for (u64 block = first; block <= last; ++block) {
|
||||
if (add) {
|
||||
apply(pending_blocks[block]);
|
||||
} else if (const auto it = pending_blocks.find(block); it != pending_blocks.end()) {
|
||||
apply(it->second);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Context::Impl::PushPendingPoolWrite(PendingPoolWrite&& write) {
|
||||
if (!write.inline_bytes.empty()) {
|
||||
++pending_inline;
|
||||
}
|
||||
// (a write of a submission already passed is dropped by the next prune, as it always was)
|
||||
if (write.seq <= pending_pruned_done) {
|
||||
pending_prune_due = true;
|
||||
}
|
||||
pending_pool_writes.push_back(std::move(write));
|
||||
IndexPendingPoolWrite(static_cast<u32>(pending_pool_writes.size() - 1), true);
|
||||
}
|
||||
|
||||
void Context::Impl::RemovePendingPoolWrite(u32 index) {
|
||||
PendingPoolWrite& write = pending_pool_writes[index];
|
||||
if (!PendingLive(write)) {
|
||||
return;
|
||||
}
|
||||
IndexPendingPoolWrite(index, false);
|
||||
if (!write.inline_bytes.empty()) {
|
||||
--pending_inline;
|
||||
}
|
||||
write = {};
|
||||
++pending_dead;
|
||||
}
|
||||
|
||||
void Context::Impl::RemovePendingPoolWritesWithin(u64 begin, u64 end) {
|
||||
thread_local std::vector<u32> contained;
|
||||
contained.clear();
|
||||
VisitPendingPoolWrites(begin, end, [&](u32 index) {
|
||||
const PendingPoolWrite& write = pending_pool_writes[index];
|
||||
if (write.begin >= begin && write.end <= end) {
|
||||
contained.push_back(index);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
for (const u32 index : contained) {
|
||||
RemovePendingPoolWrite(index);
|
||||
}
|
||||
SettlePendingPoolWrites();
|
||||
}
|
||||
|
||||
void Context::Impl::SettlePendingPoolWrites() {
|
||||
while (!pending_pool_writes.empty() && !PendingLive(pending_pool_writes.back())) {
|
||||
pending_pool_writes.pop_back();
|
||||
--pending_dead;
|
||||
}
|
||||
if (pending_dead > 64 && pending_dead * 2 > pending_pool_writes.size()) {
|
||||
std::erase_if(pending_pool_writes,
|
||||
[](const PendingPoolWrite& write) { return !PendingLive(write); });
|
||||
pending_dead = 0;
|
||||
RebuildPendingPoolIndex();
|
||||
}
|
||||
}
|
||||
|
||||
void Context::Impl::RebuildPendingPoolIndex() {
|
||||
if (pending_blocks.size() > 4 * pending_pool_writes.size() + 1024) {
|
||||
pending_blocks.clear();
|
||||
} else {
|
||||
for (auto& [block, list] : pending_blocks) {
|
||||
list.clear();
|
||||
}
|
||||
}
|
||||
pending_wide.clear();
|
||||
for (u32 index = 0; index < static_cast<u32>(pending_pool_writes.size()); ++index) {
|
||||
if (PendingLive(pending_pool_writes[index])) {
|
||||
IndexPendingPoolWrite(index, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Context::Impl::CollapsePendingPoolWrites() {
|
||||
if (pending_pool_writes.size() - pending_dead < PENDING_MAX_WRITES) {
|
||||
return;
|
||||
}
|
||||
std::vector<PendingPoolWrite> collapsed;
|
||||
for (const PendingPoolWrite& write : pending_pool_writes) {
|
||||
if (!PendingLive(write)) {
|
||||
continue;
|
||||
}
|
||||
const auto it = std::ranges::find_if(
|
||||
collapsed, [&](const auto& hull) { return hull.seq == write.seq; });
|
||||
if (it == collapsed.end()) {
|
||||
@@ -2387,13 +2531,29 @@ static void CollapsePendingPoolWrites(std::vector<Context::Impl::PendingPoolWrit
|
||||
it->end = (std::max)(it->end, write.end);
|
||||
}
|
||||
}
|
||||
writes = std::move(collapsed);
|
||||
pending_pool_writes = std::move(collapsed);
|
||||
pending_dead = 0;
|
||||
pending_inline = 0;
|
||||
pending_prune_due = true;
|
||||
RebuildPendingPoolIndex();
|
||||
}
|
||||
|
||||
void Context::Impl::PrunePendingPoolWrites() {
|
||||
// Every write kept is of a submission that had not completed when the list was last pruned
|
||||
// (or raised pending_prune_due), so there is nothing to drop until gpu_completed_seq moves.
|
||||
const u64 done = gpu_completed_seq.load(std::memory_order_acquire);
|
||||
std::erase_if(pending_pool_writes,
|
||||
[done](const PendingPoolWrite& write) { return write.seq <= done; });
|
||||
if (done == pending_pruned_done && !pending_prune_due) {
|
||||
return;
|
||||
}
|
||||
pending_pruned_done = done;
|
||||
pending_prune_due = false;
|
||||
for (u32 index = 0; index < static_cast<u32>(pending_pool_writes.size()); ++index) {
|
||||
const PendingPoolWrite& write = pending_pool_writes[index];
|
||||
if (PendingLive(write) && write.seq <= done) {
|
||||
RemovePendingPoolWrite(index);
|
||||
}
|
||||
}
|
||||
SettlePendingPoolWrites();
|
||||
}
|
||||
|
||||
void Context::Impl::NoteFlushedInlinePoolWrite(u64 address, const u8* data, u64 size, u64 seq) {
|
||||
@@ -2402,11 +2562,9 @@ void Context::Impl::NoteFlushedInlinePoolWrite(u64 address, const u8* data, u64
|
||||
}
|
||||
PrunePendingPoolWrites();
|
||||
const u64 end = address + size;
|
||||
std::erase_if(pending_pool_writes, [address, end](const PendingPoolWrite& write) {
|
||||
return write.begin >= address && write.end <= end;
|
||||
});
|
||||
CollapsePendingPoolWrites(pending_pool_writes);
|
||||
pending_pool_writes.push_back(
|
||||
RemovePendingPoolWritesWithin(address, end);
|
||||
CollapsePendingPoolWrites();
|
||||
PushPendingPoolWrite(
|
||||
{address, end, seq, std::vector<u8>(data, data + static_cast<size_t>(size))});
|
||||
}
|
||||
|
||||
@@ -2416,88 +2574,117 @@ void Context::Impl::NoteNonInlinePoolWrite(u64 address, u64 size, u64 seq) {
|
||||
}
|
||||
PrunePendingPoolWrites();
|
||||
const u64 end = address + size;
|
||||
std::erase_if(pending_pool_writes, [address, end](const PendingPoolWrite& write) {
|
||||
return write.begin >= address && write.end <= end;
|
||||
});
|
||||
for (PendingPoolWrite& write : pending_pool_writes) {
|
||||
if (!write.inline_bytes.empty() && write.begin < end && address < write.end) {
|
||||
write.inline_bytes.clear();
|
||||
}
|
||||
RemovePendingPoolWritesWithin(address, end);
|
||||
if (pending_inline != 0) {
|
||||
VisitPendingPoolWrites(address, end, [&](u32 index) {
|
||||
PendingPoolWrite& write = pending_pool_writes[index];
|
||||
if (!write.inline_bytes.empty() && write.begin < end && address < write.end) {
|
||||
write.inline_bytes = {};
|
||||
--pending_inline;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
if (!pending_pool_writes.empty()) {
|
||||
// (settled above: the last entry is a live write)
|
||||
PendingPoolWrite& last = pending_pool_writes.back();
|
||||
if (last.seq == seq && last.inline_bytes.empty() && address <= last.end &&
|
||||
last.begin <= end) {
|
||||
const u32 index = static_cast<u32>(pending_pool_writes.size() - 1);
|
||||
IndexPendingPoolWrite(index, false);
|
||||
last.begin = (std::min)(last.begin, address);
|
||||
last.end = (std::max)(last.end, end);
|
||||
IndexPendingPoolWrite(index, true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
CollapsePendingPoolWrites(pending_pool_writes);
|
||||
pending_pool_writes.push_back({address, end, seq, {}});
|
||||
CollapsePendingPoolWrites();
|
||||
PushPendingPoolWrite({address, end, seq, {}});
|
||||
}
|
||||
|
||||
void Context::Impl::InvalidatePendingInlineBytes(u64 seq) {
|
||||
PrunePendingPoolWrites();
|
||||
if (pending_inline == 0) {
|
||||
return;
|
||||
}
|
||||
for (PendingPoolWrite& write : pending_pool_writes) {
|
||||
if (seq == 0 || write.seq == seq) {
|
||||
write.inline_bytes.clear();
|
||||
if (!write.inline_bytes.empty() && (seq == 0 || write.seq == seq)) {
|
||||
write.inline_bytes = {};
|
||||
--pending_inline;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Context::Impl::InvalidatePendingInlineBytesOverlapping(u64 address, u64 size) {
|
||||
if (size == 0 || pending_pool_writes.empty()) {
|
||||
if (size == 0 || pending_pool_writes.size() == pending_dead) {
|
||||
return;
|
||||
}
|
||||
PrunePendingPoolWrites();
|
||||
const u64 end = address + size;
|
||||
for (PendingPoolWrite& write : pending_pool_writes) {
|
||||
if (!write.inline_bytes.empty() && write.begin < end && address < write.end) {
|
||||
write.inline_bytes.clear();
|
||||
}
|
||||
if (pending_inline == 0) {
|
||||
return;
|
||||
}
|
||||
const u64 end = address + size;
|
||||
VisitPendingPoolWrites(address, end, [&](u32 index) {
|
||||
PendingPoolWrite& write = pending_pool_writes[index];
|
||||
if (!write.inline_bytes.empty() && write.begin < end && address < write.end) {
|
||||
write.inline_bytes = {};
|
||||
--pending_inline;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
void Context::Impl::ErasePendingPoolWritesForSeq(u64 seq) {
|
||||
std::erase_if(pending_pool_writes,
|
||||
[seq](const PendingPoolWrite& write) { return write.seq == seq; });
|
||||
for (u32 index = 0; index < static_cast<u32>(pending_pool_writes.size()); ++index) {
|
||||
const PendingPoolWrite& write = pending_pool_writes[index];
|
||||
if (PendingLive(write) && write.seq == seq) {
|
||||
RemovePendingPoolWrite(index);
|
||||
}
|
||||
}
|
||||
SettlePendingPoolWrites();
|
||||
}
|
||||
|
||||
bool Context::Impl::NeedsGpuFlushForUniform(u64 begin, u64 end, u64 current_seq) {
|
||||
if (end <= begin || pending_pool_writes.empty()) {
|
||||
if (end <= begin || pending_pool_writes.size() == pending_dead) {
|
||||
return false;
|
||||
}
|
||||
PrunePendingPoolWrites();
|
||||
for (const PendingPoolWrite& write : pending_pool_writes) {
|
||||
if (write.begin < end && begin < write.end &&
|
||||
(write.inline_bytes.empty() || write.seq != current_seq)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return VisitPendingPoolWrites(begin, end, [&](u32 index) {
|
||||
const PendingPoolWrite& write = pending_pool_writes[index];
|
||||
return write.begin < end && begin < write.end &&
|
||||
(write.inline_bytes.empty() || write.seq != current_seq);
|
||||
});
|
||||
}
|
||||
|
||||
void Context::Impl::ApplyPendingInlineWrites(u64 base_address, u8* dst, u64 size,
|
||||
u64 current_seq,
|
||||
std::vector<std::pair<u64, u64>>* written) const {
|
||||
if (size == 0 || dst == nullptr || pending_pool_writes.empty()) {
|
||||
if (size == 0 || dst == nullptr || pending_inline == 0) {
|
||||
return;
|
||||
}
|
||||
const u64 end = base_address + size;
|
||||
for (const PendingPoolWrite& write : pending_pool_writes) {
|
||||
if (write.seq != current_seq || write.inline_bytes.empty()) {
|
||||
continue;
|
||||
// In the order noted: a later update of the same bytes wins.
|
||||
thread_local std::vector<u32> overlapping;
|
||||
overlapping.clear();
|
||||
VisitPendingPoolWrites(base_address, end, [&](u32 index) {
|
||||
const PendingPoolWrite& write = pending_pool_writes[index];
|
||||
if (write.seq == current_seq && !write.inline_bytes.empty() && write.begin < end &&
|
||||
base_address < write.end) {
|
||||
overlapping.push_back(index);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
std::ranges::sort(overlapping);
|
||||
const auto repeated = std::ranges::unique(overlapping);
|
||||
overlapping.erase(repeated.begin(), repeated.end());
|
||||
for (const u32 index : overlapping) {
|
||||
const PendingPoolWrite& write = pending_pool_writes[index];
|
||||
const u64 lo = (std::max)(write.begin, base_address);
|
||||
const u64 hi = (std::min)(write.end, end);
|
||||
if (lo < hi) {
|
||||
std::memcpy(dst + (lo - base_address),
|
||||
write.inline_bytes.data() + (lo - write.begin),
|
||||
static_cast<size_t>(hi - lo));
|
||||
if (written != nullptr) {
|
||||
written->emplace_back(lo - base_address, hi - base_address);
|
||||
}
|
||||
std::memcpy(dst + (lo - base_address), write.inline_bytes.data() + (lo - write.begin),
|
||||
static_cast<size_t>(hi - lo));
|
||||
if (written != nullptr) {
|
||||
written->emplace_back(lo - base_address, hi - base_address);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -743,6 +743,23 @@ private:
|
||||
std::vector<VkSampler> written_samplers;
|
||||
/// The storages of the entries that hold an image (their layouts are checked per draw).
|
||||
std::vector<std::shared_ptr<Storage>> storages;
|
||||
/// What entry `id` was last written from. While the registration, the texture, its
|
||||
/// storage and that storage's image are the same, the entry holds what it should and the
|
||||
/// sync leaves it (the pool has tens of thousands of registrations; walking each one's
|
||||
/// views at every submission was ~10% of the replay thread in Ocarina of Time).
|
||||
struct Entry {
|
||||
u64 serial{};
|
||||
/// The texture's node of Impl::textures (null: none); dies when the serial is erased.
|
||||
const std::shared_ptr<Texture>* texture{};
|
||||
const Storage* storage{};
|
||||
u32 image_gen{};
|
||||
VkFormat format{};
|
||||
VkImageLayout layout{};
|
||||
bool ready{}; ///< the storage had an image holding the game's data
|
||||
bool any{}; ///< the entry holds a view of it
|
||||
};
|
||||
std::vector<Entry> entries;
|
||||
u64 entries_epoch{}; ///< Impl::texture_epoch the node pointers were checked at
|
||||
u64 synced_seq{~u64{0}};
|
||||
const void* synced_textures{};
|
||||
const void* synced_samplers{};
|
||||
@@ -757,6 +774,8 @@ private:
|
||||
u64 prime_bytes{};
|
||||
u32 prime_count{};
|
||||
u32 prime_cursor{1};
|
||||
/// Submission whose walk of the pool found nothing left to create (BindlessPrime).
|
||||
u64 prime_exhausted_seq{~u64{0}};
|
||||
u32 primed_total{};
|
||||
} bindless;
|
||||
/// Creates the table on first use; false when the device cannot hold it.
|
||||
|
||||
@@ -178,6 +178,11 @@ struct Pool {
|
||||
/// Lets a CPU-sourced texture skip its hash (Storage::cpu_trusted_gen). Replay thread.
|
||||
std::vector<u32> page_write_gen;
|
||||
u32 cpu_write_gen{1};
|
||||
/// Split pools: `cpu_write_gen` when a completed raw store last may have changed any guest
|
||||
/// byte of the pool past the tracker (0: never). Names no page, so it is kept apart from
|
||||
/// `page_write_gen`: a texture full-compared before it is checked as an untracked one until
|
||||
/// its next full compare (Storage::cpu_full_gen).
|
||||
u32 raw_write_gen{0};
|
||||
/// Imported pools: the persistent device-local copy that storage bindings and indirect
|
||||
/// arguments use (EDEN_NX2_NVN_POOL_MIRROR, nvn_pool_mirror.h; not Backing::Mirror). Null until
|
||||
/// the first use that wants one. Replay thread only.
|
||||
@@ -265,6 +270,9 @@ inline constexpr u64 DEFAULT_STORAGE_TRIM_AGE_PRESENTS = 0;
|
||||
|
||||
struct Storage : std::enable_shared_from_this<Storage> {
|
||||
vk::Image image;
|
||||
/// Counts the images made for the storage (EnsureImage): what was derived from `image` (the
|
||||
/// bindless table's entries) is stale when it moved.
|
||||
u32 image_gen{0};
|
||||
HostFormat host;
|
||||
TextureLayoutDesc desc;
|
||||
TextureLayout layout;
|
||||
@@ -332,6 +340,9 @@ struct Storage : std::enable_shared_from_this<Storage> {
|
||||
/// later `page_write_gen`, the CPU did not write it and a check needs no hash
|
||||
/// (Impl::tex_hash_tracked).
|
||||
u32 cpu_trusted_gen{0};
|
||||
/// The pool's `cpu_write_gen` at the full hash behind `cpu_trusted_gen` (the page compares
|
||||
/// move only that one on). A later Pool::raw_write_gen: bytes no page write names may differ.
|
||||
u32 cpu_full_gen{0};
|
||||
/// CityHash64 of the storage's part of each pool page (Pool::MIRROR_PAGE grid, first page at
|
||||
/// pool_offset / MIRROR_PAGE), taken with the full hash that set `cpu_trusted_gen`: a check
|
||||
/// after a tracked write rehashes only the written pages. Empty: not taken.
|
||||
@@ -1066,8 +1077,33 @@ struct Context::Impl {
|
||||
u64 seq;
|
||||
std::vector<u8> inline_bytes;
|
||||
};
|
||||
/// In the order noted. A removed write stays as a dead entry (begin == end) until the list is
|
||||
/// compacted, so the positions the index holds stay valid.
|
||||
std::vector<PendingPoolWrite> pending_pool_writes;
|
||||
size_t pending_dead{}; ///< dead entries of the list
|
||||
size_t pending_inline{}; ///< live writes that hold inline bytes
|
||||
/// gpu_completed_seq at the last prune, and whether a write at or below it was noted since
|
||||
/// (nothing else can be pruned until it moves).
|
||||
u64 pending_pruned_done{~u64{0}};
|
||||
bool pending_prune_due{false};
|
||||
/// Positions of the live writes by 16 KiB block of their range, and of those spanning too
|
||||
/// many blocks. Kept current with every change: draws ask per uniform binding and note
|
||||
/// several writes each, with thousands of writes pending (a scan of the list per question
|
||||
/// was ~25% of the replay thread in Ocarina of Time).
|
||||
std::unordered_map<u64, std::vector<u32>> pending_blocks;
|
||||
std::vector<u32> pending_wide;
|
||||
bool noting_inline_pool_write{false};
|
||||
void IndexPendingPoolWrite(u32 index, bool add);
|
||||
void PushPendingPoolWrite(PendingPoolWrite&& write);
|
||||
void RemovePendingPoolWrite(u32 index);
|
||||
void RemovePendingPoolWritesWithin(u64 begin, u64 end);
|
||||
void SettlePendingPoolWrites();
|
||||
void RebuildPendingPoolIndex();
|
||||
void CollapsePendingPoolWrites();
|
||||
/// visit(position) for each live write that may overlap [begin, end), until it returns true
|
||||
/// (a write may come more than once). The visitor must not add or remove writes.
|
||||
template <typename Visitor>
|
||||
bool VisitPendingPoolWrites(u64 begin, u64 end, Visitor&& visit) const;
|
||||
void PrunePendingPoolWrites();
|
||||
void NoteFlushedInlinePoolWrite(u64 address, const u8* data, u64 size, u64 seq);
|
||||
void NoteNonInlinePoolWrite(u64 address, u64 size, u64 seq);
|
||||
@@ -1098,6 +1134,9 @@ struct Context::Impl {
|
||||
std::unordered_map<u64, u64> last_use; ///< serial -> last seq that retained it
|
||||
/// Bumped whenever an entry of `textures` is erased (cached node pointers die).
|
||||
u64 texture_epoch{1};
|
||||
/// The serials erased last: erase number n (texture_epoch - 1 when it happened) is at
|
||||
/// n % size. Lets a holder of many node pointers drop only the dead ones.
|
||||
std::array<u64, 4096> texture_erased{};
|
||||
/// Bumped when a `storage_index` node is erased (Storage::pool_index).
|
||||
u64 storage_index_epoch{1};
|
||||
/// Replay thread: recently used (serial -> seq) so that NoteUse of an already noted serial does
|
||||
@@ -1217,6 +1256,8 @@ struct Context::Impl {
|
||||
/// `poll_tick` at the last new present: the idle ticks of the current stretch count from here.
|
||||
u64 poll_tick_at_change{0};
|
||||
std::chrono::steady_clock::time_point poll_last_change{};
|
||||
/// Recent time between presents in eighths of a tick: what an ordinary frame is (PollTick).
|
||||
u64 poll_frame_ticks_x8{0};
|
||||
u64 full_hash_tick{~u64{0}};
|
||||
u64 full_hash_bytes{0};
|
||||
[[nodiscard]] u64 PollTick();
|
||||
|
||||
@@ -1236,6 +1236,7 @@ void Context::Impl::DestroyNow(u64 serial) {
|
||||
if (const auto it = textures.find(serial); it != textures.end()) {
|
||||
std::shared_ptr<Texture> texture = std::move(it->second);
|
||||
textures.erase(it);
|
||||
texture_erased[(texture_epoch - 1) % texture_erased.size()] = serial;
|
||||
++texture_epoch;
|
||||
// The image views of the draw path go with the texture, after the last submission that
|
||||
// used them completed.
|
||||
|
||||
@@ -37,8 +37,11 @@ u32 Mip(u32 size, u32 level) {
|
||||
} // Anonymous namespace
|
||||
|
||||
u64 Context::Impl::PollTick() {
|
||||
// Idle ticks of one ordinary frame (64 ms, ~15 fps): not kept when the next present comes, so
|
||||
// at a steady frame rate the clock moves one tick per present.
|
||||
// Idle ticks of one ordinary frame are not kept when the next present comes, so at a steady
|
||||
// frame rate the clock moves one tick per present. An ordinary frame is up to twice the
|
||||
// recent frame length, and 64 ms at least. With the 64 ms alone every frame below ~15 fps
|
||||
// kept its idle ticks: the clock ran by wall time (~7 ticks per present at 9 fps) and each
|
||||
// present paid the texture checks of ~7 (Ocarina of Time, Hyrule Field).
|
||||
constexpr u64 IDLE_FRAME_TICKS = 4;
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
const u64 presents = stats.presents.load(std::memory_order_relaxed);
|
||||
@@ -55,7 +58,13 @@ u64 Context::Impl::PollTick() {
|
||||
// stretch counts from the clock as it stands.
|
||||
const u64 idle_ticks = poll_tick - poll_tick_at_change;
|
||||
const u64 advance = presents > poll_presents_seen ? presents - poll_presents_seen : 0;
|
||||
poll_tick_at_change += advance + (idle_ticks > IDLE_FRAME_TICKS ? idle_ticks : 0);
|
||||
const u64 ordinary = (std::max)(IDLE_FRAME_TICKS, poll_frame_ticks_x8 / 4);
|
||||
// The recent frame length (a stall counts as an ordinary frame at most).
|
||||
const u64 frame_ms = static_cast<u64>(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(now - poll_last_change).count());
|
||||
const u64 frame_x8 = (std::min)(frame_ms / 2 / (std::max)(advance, u64{1}), ordinary * 8);
|
||||
poll_frame_ticks_x8 = (poll_frame_ticks_x8 * 7 + frame_x8 + 4) / 8;
|
||||
poll_tick_at_change += advance + (idle_ticks > ordinary ? idle_ticks : 0);
|
||||
poll_tick = (std::max)(poll_tick, poll_tick_at_change);
|
||||
poll_presents_seen = presents;
|
||||
poll_last_change = now;
|
||||
@@ -88,10 +97,11 @@ bool Context::Impl::TakePoolCpuWrites(Pool& pool) {
|
||||
}
|
||||
}
|
||||
// Textures of the pool must see raw writes even in Mario Kart World; only the mirror
|
||||
// page exception is Mario Kart World specific.
|
||||
++pool.cpu_write_gen;
|
||||
std::fill(pool.page_write_gen.begin(), pool.page_write_gen.end(),
|
||||
pool.cpu_write_gen);
|
||||
// page exception is Mario Kart World specific. The store names no page: the textures
|
||||
// lose their full baseline (DetectGuestChange), the pages are not marked written.
|
||||
// Marking every page made each check of each texture of every split pool rehash the
|
||||
// whole texture, at every raw store (Ocarina of Time: ~130 MB hashed per present).
|
||||
pool.raw_write_gen = ++pool.cpu_write_gen;
|
||||
}
|
||||
}
|
||||
std::vector<std::pair<u64, u64>> writebacks;
|
||||
@@ -265,8 +275,14 @@ void Context::Impl::DetectGuestChange(Storage& s, u64 seq) {
|
||||
const bool tracked = tex_hash_tracked && !watching && !pool.IsVirtual() &&
|
||||
(pool.flags & PoolFlag::CpuNoAccess) == 0 && TakePoolCpuWrites(pool);
|
||||
const u32 tracked_gen = pool.cpu_write_gen;
|
||||
if (tracked && !forced && s.cpu_trusted_gen != 0 &&
|
||||
!PoolWrittenSince(pool, s.pool_offset, size, s.cpu_trusted_gen)) {
|
||||
const bool trusted = tracked && !forced && s.cpu_trusted_gen != 0;
|
||||
// A raw store completed since the full compare: it may have written any byte, so the check
|
||||
// below is the one of an untracked texture (signature, full compare when due). Pages the
|
||||
// tracker saw written are still compared first.
|
||||
const bool raw_pending = trusted && pool.raw_write_gen > s.cpu_full_gen;
|
||||
const bool written =
|
||||
trusted && PoolWrittenSince(pool, s.pool_offset, size, s.cpu_trusted_gen);
|
||||
if (trusted && !written && !raw_pending) {
|
||||
stats.cpu_checks_trusted.fetch_add(1, std::memory_order_relaxed);
|
||||
schedule_unchanged();
|
||||
return;
|
||||
@@ -280,14 +296,13 @@ void Context::Impl::DetectGuestChange(Storage& s, u64 seq) {
|
||||
return Common::CityHash64(reinterpret_cast<const char*>(data + (begin - s.pool_offset)),
|
||||
static_cast<size_t>(end - begin));
|
||||
};
|
||||
if (tracked && !forced && s.cpu_trusted_gen != 0) {
|
||||
if (written) {
|
||||
s.wants_page_hashes = true;
|
||||
}
|
||||
// Some pages were written since the bytes were trusted: compare those pages only. Their old
|
||||
// hashes describe the contents (an upload drops them), so a page that differs means the
|
||||
// contents are stale, and equal pages mean the bytes are the trusted ones again.
|
||||
if (tracked && !forced && s.cpu_trusted_gen != 0 &&
|
||||
s.page_hashes.size() == last_page - first_page + 1 &&
|
||||
if (written && s.page_hashes.size() == last_page - first_page + 1 &&
|
||||
last_page < pool.page_write_gen.size()) {
|
||||
std::vector<u8> page_scratch;
|
||||
const auto page_begin = std::chrono::steady_clock::now();
|
||||
@@ -311,18 +326,20 @@ void Context::Impl::DetectGuestChange(Storage& s, u64 seq) {
|
||||
std::memory_order_relaxed);
|
||||
stats.hash_bytes.fetch_add(rehashed * Pool::MIRROR_PAGE, std::memory_order_relaxed);
|
||||
stats.cpu_pages_rehashed.fetch_add(rehashed, std::memory_order_relaxed);
|
||||
if (!differs) {
|
||||
stats.cpu_checks_pages.fetch_add(1, std::memory_order_relaxed);
|
||||
s.cpu_trusted_gen = tracked_gen;
|
||||
if (differs) {
|
||||
stats.cpu_changes.fetch_add(1, std::memory_order_relaxed);
|
||||
s.cpu_trusted_gen = 0;
|
||||
s.page_hashes.clear();
|
||||
s.dynamic = true;
|
||||
s.force_upload = true;
|
||||
return;
|
||||
}
|
||||
stats.cpu_checks_pages.fetch_add(1, std::memory_order_relaxed);
|
||||
s.cpu_trusted_gen = tracked_gen;
|
||||
if (!raw_pending) {
|
||||
schedule_unchanged();
|
||||
return;
|
||||
}
|
||||
stats.cpu_changes.fetch_add(1, std::memory_order_relaxed);
|
||||
s.cpu_trusted_gen = 0;
|
||||
s.page_hashes.clear();
|
||||
s.dynamic = true;
|
||||
s.force_upload = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// A texture the CPU keeps writing parts of (a glyph atlas) shows new content only in a few
|
||||
@@ -386,8 +403,14 @@ void Context::Impl::DetectGuestChange(Storage& s, u64 seq) {
|
||||
s.next_full_tick = tick + FULL_VERIFY_PRESENTS;
|
||||
// Taken before the bytes were read: a write since shows with a later generation.
|
||||
s.cpu_trusted_gen = (!changed && tracked) ? tracked_gen : 0;
|
||||
s.page_hashes.clear();
|
||||
if (s.cpu_trusted_gen != 0 && tex_page_hash && s.wants_page_hashes) {
|
||||
s.cpu_full_gen = s.cpu_trusted_gen;
|
||||
// The bytes are the baseline's again: page hashes taken of it still describe them.
|
||||
const bool pages_current = s.cpu_trusted_gen != 0 &&
|
||||
s.page_hashes.size() == last_page - first_page + 1;
|
||||
if (!pages_current) {
|
||||
s.page_hashes.clear();
|
||||
}
|
||||
if (s.cpu_trusted_gen != 0 && tex_page_hash && s.wants_page_hashes && !pages_current) {
|
||||
s.page_hashes.resize(static_cast<size_t>(last_page - first_page + 1));
|
||||
for (u64 page = first_page; page <= last_page; ++page) {
|
||||
s.page_hashes[static_cast<size_t>(page - first_page)] = page_hash(data, page);
|
||||
@@ -591,6 +614,7 @@ bool Context::Impl::EnsureImage(Storage& s) {
|
||||
const char* reason = "the device cannot create a sparse residency image";
|
||||
// Shared tile pages (SparseShareEnabled) need SPARSE_ALIASED images.
|
||||
s.image = allocator->CreateUnbackedImage(image_info, SparseShareEnabled());
|
||||
++s.image_gen;
|
||||
if (s.image && s.sparse->Attach(*this, s, reason)) {
|
||||
s.current_layout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
stats.storages_created.fetch_add(1, std::memory_order_relaxed);
|
||||
@@ -607,6 +631,7 @@ bool Context::Impl::EnsureImage(Storage& s) {
|
||||
s.origin = StorageOrigin::None;
|
||||
}
|
||||
s.image = allocator->CreateImage(image_info, false, AllocationKind::StorageImage);
|
||||
++s.image_gen;
|
||||
} catch (const vk::Exception& exception) {
|
||||
s.image_failed = true;
|
||||
stats.textures_failed.fetch_add(1, std::memory_order_relaxed);
|
||||
|
||||
Reference in new issue
Block a user