Triforce v0.8.5.2

Hotfix: the brightness flashes fixed without the fps cost (the readback wait only for ranges the CPU resets), and the Discord status names where Link is by default.
This commit is contained in:
ruckus7382sfgsgdf
2026-11-28 15:45:28 -06:00
parent 0f7d57ac14
commit 8cb23131f9
13 changed files with 412 additions and 54 deletions

View File

@@ -415,6 +415,10 @@ uint64_t memory_writeback_value(uint64_t guest, uint64_t size, bool* ever);
/// ((Triforce, THREAD-25) page_marks.h, no lock) false: no write-back ever reached a page of the range: *ever above is false
bool memory_writeback_marked(uint64_t guest, uint64_t size);
void memory_note_readback(uint64_t guest, uint64_t size); // (PR 430) a CPU read of GPU results: see memory_rb_refresh
/// ((Triforce, 11-28) a collection's CPU-written pages (capture_make, memory_collect_written); whether the CPU wrote a read-back
/// range lately, after reading it: its readback waits for the last part of its writer's recording (readback_recorded_wait)
void memory_rb_cpu_collected(const uint32_t* pages, int64_t n);
bool memory_rb_cpu_written(uint64_t guest, uint64_t size);
/// (Triforce) around the write of an NVN event's word by a command buffer's signal: its write-back is not one the captures of
/// its page wait for (vk_memory.cpp). This thread's bindings while on
void memory_bind_event_write(bool on);

View File

@@ -1876,6 +1876,7 @@ void memory_collect_written() {
for (int64_t i = 0; i < n; i++) lazy_cpu_wrote(pages[i]);
if (lz_no > lz_collects.load()) lz_collects.store(lz_no);
}
memory_rb_cpu_collected(pages, n); // ((Triforce, 11-28) the read-back pages the CPU wrote: memory_rb_cpu_written)
if (n > 0) {
const WriteGen g = next_gen();
stamp_src_note(g, 1);
@@ -3661,7 +3662,14 @@ uint64_t wb_spans_value(uint64_t guest, uint64_t size, bool* ever) {
if (lo <= hi) v = std::max(v, wb_pages.max_in(lo, hi, ever));
return v;
}
struct WbRun { uint64_t lo, hi, value; }; // (a write-back's range in guest memory and its submission)
struct WbRun { uint64_t lo, hi, value, batch; }; // (a write-back's range in guest memory, its submission, and ((11-28)) the
// part's batch of write-backs it was recorded in: memory_end_submission)
// ((Triforce, 11-28) Which part of a recording copied a range back. Each memory_end_submission (a part's end, a recording's end, the
// early tail's start) is one batch; the replay notes the batch's number on the recordings' timeline when it submits the part
// (memory_part_submitted). A readback during an early tail asks for its range's batch (memory_wb_part_seq): the scene's part that
// wrote it back, not the tail's end (vk_replay.cpp readback_rec_wait, wb_rec_wait.h). wb_pages_mu)
std::atomic<uint64_t> wb_batch_now{0};
std::map<uint64_t, uint64_t> wb_batch_seq; // batch -> its part's number on the recordings' timeline
std::vector<WbRun> wb_runs_recent; // (wb_pages_mu held)
uint64_t wb_runs_lost = 0; // (the ranges of submissions up to this value were dropped unseen)
void wb_pages_note(uint64_t guest_lo, uint64_t guest_hi, uint64_t value) {
@@ -3677,7 +3685,7 @@ void wb_pages_note(uint64_t guest_lo, uint64_t guest_hi, uint64_t value) {
std::erase_if(wb_runs_recent, [&](const WbRun& r) { return r.value <= done; });
if (wb_runs_recent.size() >= 4096) wb_runs_recent.clear(), wb_runs_lost = value; // (none known up to this value: those pages wait)
}
wb_runs_recent.push_back({guest_lo, guest_hi, value});
wb_runs_recent.push_back({guest_lo, guest_hi, value, wb_batch_now.load(std::memory_order_relaxed)});
}
/// (Triforce) The byte ranges of a page that submissions not yet done on the GPU write back (offsets in the page, sorted and
/// merged into out, at most cap of them). False when they are not known (the list was cut while such a submission was on the
@@ -4173,6 +4181,7 @@ std::unique_ptr<Capture> capture_make(bool behind) {
n = host->collect_written_in(reinterpret_cast<const uint64_t(*)[2]>(mirrored.data()), (int)mirrored.size(), &pages);
}
if (lz_no) lazy_capture_taken(pages, n, lz_no);
memory_rb_cpu_collected(pages, n); // ((Triforce, 11-28) the read-back pages the CPU wrote: memory_rb_cpu_written)
if (cap_only_gpu()) { // (RX_VK_CAP_ONLY_GPU: the pages taken, and with 2 those 1 would leave out)
st_cap_takes.fetch_add(1, std::memory_order_relaxed);
st_cap_pages.fetch_add(std::max<int64_t>(n, 0), std::memory_order_relaxed);
@@ -6817,7 +6826,84 @@ void memory_note_event_word(uint64_t gpu) {
std::lock_guard lk(event_pages_mu);
if (event_pages.insert((uint32_t)(guest >> kPageShift)).second) event_pages_n.fetch_add(1, std::memory_order_relaxed);
}
// ---- ((Triforce, 11-28) Read-back pages the CPU writes. A readback of GPU results that came before its writer's recording
// ended reads what guest memory holds: the results of an older frame. For a range the CPU only reads that is a frame late at
// worst, and the next read has the new one (OoT's two 14 KB result buffers, 0x113b643000/648000 in build e0f3ccfa, read by
// the update's workers before the game waits for that frame: ~30 reads a frame). For a range the CPU writes after it reads it
// (the exposure meter: a sum the GPU adds into each frame, the CPU reads it and stores 0) it is not: the frame's write-back
// lands after the read, over the CPU's 0, and the next read gets two frames (the brightness steps). So the readback's wait
// for the last part of its writer's recording (RX_VK_RB_REC_END, vk_replay.cpp readback_recorded_wait) is for the second kind
// only: the 0.8.5 wait for every readback held those workers ~5 ms each, 98 -> 82 fps in Hyrule Field at the owner's
// settings. The CPU's writes are the ones the collections name (capture_make, memory_collect_written): a read-back page among
// them is one the CPU writes. A page is coarse (OoT's two result buffers share pages the CPU writes, and took the wait at 82.9
// fps), so the range itself is asked too: the CPU's reset is a store of 0 over the results it read, and the range reads all 0
// now where a read once found the GPU's results in it (the 32 bytes the zero diagnostic looks at, RX_VK_LOG_READBACK_ZERO).
// The result buffers never read all 0; the meters read all 0 between the CPU's reset and the frame's write-back, the reads that
// flashed. rb_cpu_mu is a leaf lock (never pools_mu: the captures stay off it) ----
namespace {
std::mutex rb_cpu_mu;
// (a read-back page -> the collection count at the CPU's last write of it, 0 none yet; the bounds of the pages, to skip a
// collection's pages fast)
std::unordered_map<uint32_t, uint64_t> rb_watch;
uint32_t rb_watch_lo = ~0u, rb_watch_hi = 0;
// (a range, by its start: 1 its words were not all 0 at some read (the GPU's results), 2 that and all 0 at its newest read)
std::unordered_map<uint64_t, uint8_t> rb_ranges_seen;
std::atomic<uint64_t> rb_collections{0};
std::atomic<bool> rb_watch_any{false};
constexpr uint64_t kRbCpuRecent = 4096; // (collections, a few seconds of play: a page the CPU stopped writing goes back)
constexpr uint64_t kRbWatchMax = 0x10000; // (the largest range watched: results, not textures)
}
void memory_rb_watch(uint64_t guest, uint64_t size) {
if (!size || size > kRbWatchMax) return;
uint32_t w[8]{}; // (the range's first words as the read comes, before its wait)
std::memcpy(w, host->guest_base() + guest, (size_t)std::min<uint64_t>(size, sizeof w));
bool nz = false;
for (const uint32_t x : w) nz |= x != 0;
std::lock_guard lk(rb_cpu_mu);
{
uint8_t& r = rb_ranges_seen[guest];
r = nz ? 1 : r ? 2 : 0;
}
for (uint64_t pg = guest >> kPageShift; pg <= (guest + size - 1) >> kPageShift; pg++) {
const uint32_t p = (uint32_t)pg;
if (rb_watch.emplace(p, 0).second) rb_watch_lo = std::min(rb_watch_lo, p), rb_watch_hi = std::max(rb_watch_hi, p);
}
if (rb_watch.size() > 4096 || rb_ranges_seen.size() > 16384) { // (a safety net: a game that reads back very many ranges starts over)
rb_watch.clear(), rb_watch_lo = ~0u, rb_watch_hi = 0, rb_ranges_seen.clear();
}
rb_watch_any.store(!rb_watch.empty(), std::memory_order_relaxed);
}
/// A collection's pages, the ones the CPU wrote since the one before: the read-back pages among them noted
void memory_rb_cpu_collected(const uint32_t* pages, int64_t n) {
const uint64_t no = rb_collections.fetch_add(1, std::memory_order_relaxed) + 1;
if (n <= 0 || !pages || !rb_watch_any.load(std::memory_order_relaxed)) return;
std::lock_guard lk(rb_cpu_mu);
for (int64_t i = 0; i < n; i++) {
const uint32_t p = pages[i];
if (p < rb_watch_lo || p > rb_watch_hi) continue;
const auto it = rb_watch.find(p);
if (it != rb_watch.end()) it->second = no;
}
}
/// Whether the CPU reset [guest, guest + size) after reading it back: a page of it written by the CPU lately, and the range all
/// 0 at this read where an earlier read found the GPU's results. The readback waits for the last part of its writer's recording
/// (vk_replay.cpp readback_recorded_wait)
bool memory_rb_cpu_written(uint64_t guest, uint64_t size) {
if (!size || !rb_watch_any.load(std::memory_order_relaxed)) return false;
const uint64_t now = rb_collections.load(std::memory_order_relaxed);
std::lock_guard lk(rb_cpu_mu);
{
const auto r = rb_ranges_seen.find(guest);
if (r == rb_ranges_seen.end() || r->second != 2) return false; // (not all 0 now, or never the GPU's results)
}
for (uint64_t pg = guest >> kPageShift; pg <= (guest + size - 1) >> kPageShift; pg++) {
const auto it = rb_watch.find((uint32_t)pg);
if (it != rb_watch.end() && it->second && now - it->second < kRbCpuRecent) return true;
}
return false;
}
void memory_note_readback(uint64_t guest, uint64_t size) {
memory_rb_watch(guest, size); // ((Triforce, 11-28) whether the CPU writes it: memory_rb_cpu_written)
static const bool off = getenv("RX_VK_NO_RB_REFRESH") != nullptr;
if (off || !size || size > 0x4000) return; // (small results only: meters and counters)
std::lock_guard lk(pools_mu);
@@ -9520,6 +9606,7 @@ void msync_pass_report() {
void memory_end_submission(VkCommandBuffer cmd) {
std::lock_guard lk(pools_mu);
wb_batch_now.fetch_add(1, std::memory_order_relaxed); // ((Triforce, 11-28) this part's batch of write-backs: WbRun.batch
{ // (every 600 submissions: how the mirror syncs' dirty pages were read)
static int64_t n, l_live, l_cap;
if (++n % 600 == 0) {
@@ -10309,3 +10396,26 @@ int mkwvk_memory_holders(char* out, size_t n) {
return 0;
}
}
namespace mkwvk { // ((Triforce, 11-28) the parts' batches of write-backs, beside wb_runs_recent
/// ((Triforce, 11-28) the replay submitted the part whose write-backs are the batch just ended, as number seq on the recordings'
/// timeline (vk_replay.cpp submit_recording)
void memory_part_submitted(uint64_t seq) {
if (!seq) return;
std::lock_guard lk(wb_pages_mu);
wb_batch_seq[wb_batch_now.load(std::memory_order_relaxed)] = seq;
while (wb_batch_seq.size() > 8192) wb_batch_seq.erase(wb_batch_seq.begin());
}
/// ((Triforce, 11-28) The part (its number on the recordings' timeline) whose write-back last covered any of [guest, guest + size)
/// among submission value's, 0 when none is known (the range not written back under value yet, or its part not submitted)
uint64_t memory_wb_part_seq(uint64_t guest, uint64_t size, uint64_t value) {
if (!size || !value) return 0;
std::lock_guard lk(wb_pages_mu);
uint64_t batch = 0;
for (const WbRun& r : wb_runs_recent)
if (r.value == value && r.lo < guest + size && r.hi > guest && r.batch > batch) batch = r.batch;
if (!batch) return 0;
const auto it = wb_batch_seq.find(batch);
return it == wb_batch_seq.end() ? 0 : it->second;
}
} // namespace mkwvk

View File

@@ -144,6 +144,8 @@ bool mesh_ahead_enabled(); // (Triforce, vk_draw.cpp: RX_VK_MESH_AHEAD, mesh_a
struct CondNote { int64_t present; uint64_t addr, x, y; int mode; bool on; }; // (the last render conditions, for a dark frame)
CondNote cond_ring[1 << 15];
uint32_t cond_ring_at = 0;
void memory_part_submitted(uint64_t seq); // ((Triforce, 11-28) vk_memory.cpp: a part's batch of write-backs by its number
uint64_t memory_wb_part_seq(uint64_t guest, uint64_t size, uint64_t value); // (and the part that wrote a range back)
namespace {
// ---- submissions: a timeline semaphore; value n = the n-th submit done ----
@@ -167,6 +169,22 @@ std::map<uint64_t, uint64_t> value_rec; // submission value (or a recording'
// ((Triforce, 11-27) the recordings by their own value, the value their write-backs are noted under: the newest part submitted and
// whether it ended the recording, for a readback's wait (readback_rec_wait; wb_rec_wait.h Parts). rec_map_mu)
::mkwvk::wb_rec_wait::Parts rec_parts_by_value;
/// ((Triforce, 11-28) the early tail's two notes for the readbacks (wb_rec_wait.h Parts::Entry scene_seq): passed false, the tail
/// began after the part just submitted (tail_begin); true, it passed its verdict (tail_end). RX_VK_RB_TAIL=0: not noted, the
/// readbacks wait for the tail's end as 0.8.5 did
void rb_tail_note(uint64_t value, bool passed) {
static const bool on = [] {
const char* e = getenv("RX_VK_RB_TAIL");
const bool v = !(e && *e && atoi(e) == 0);
logf("vk: a readback after an early tail that passed waits for %s (RX_VK_RB_TAIL%s)",
v ? "the scene's part, not the tail's end" : "the tail's end", v ? "=0: the tail's end" : "=0");
return v;
}();
if (!on || !value) return;
std::lock_guard mk(rec_map_mu);
if (passed) rec_parts_by_value.tail_passed(value);
else rec_parts_by_value.tail_began(value);
}
std::unordered_map<uint64_t, uint64_t> q_last; // queue -> its last submission's value (sync_order_mu)
std::map<uint64_t, uint64_t> value_queue; // a submission's value -> its queue (sync_order_mu)
std::atomic<uint64_t> pushed{0};
@@ -1852,6 +1870,7 @@ void submit_recording(Recording& r, bool signal_value = true, uint64_t signal_at
// queue. rec_map_mu takes no other lock inside it)
const auto register_recording = [&] {
if (!seq) return;
memory_part_submitted(seq); // ((Triforce, 11-28) this part's batch of write-backs, by its number: before rec_map_mu)
std::lock_guard mk(rec_map_mu);
// (upstream #2419, behind RX_VK_REC_LAST: a submission recorded in parts maps its value to its LAST part. emplace keeps
// the first, and a wait for the submission through wait_recorded comes back after its first part)
@@ -3802,7 +3821,9 @@ void tail_begin(VkCommandBuffer& cmd) {
texture_end_recording(cmd);
memory_end_submission(cmd);
full_barrier(cmd, "barrier (recording end)");
const uint64_t scene_value = rec_cur->value;
submit_recording(*rec_cur, false, 0, true); // (a part)
rb_tail_note(scene_value, false); // ((Triforce, 11-28) the scene's part, for the readbacks: wb_rec_wait.h scene_seq)
ub_first_of_recording = ub_here;
Recording& r = begin_recording(rec_cur_value);
memory_begin_submission();
@@ -3892,6 +3913,7 @@ void tail_end(Recording& r, const tail_early::Seen& seen, bool baked) {
tail_stats();
if (why == tail_early::EARLY) {
tl.early = true;
rb_tail_note(r.value, true); // ((Triforce, 11-28) it wrote nothing the game reads: readbacks stop at the scene's part)
return;
}
tl.words.clear();
@@ -6194,23 +6216,47 @@ bool wait_recorded(uint64_t f, uint64_t timeout_ns, int* ok) {
/// ((Triforce, 11-27) RX_VK_RB_REC_END: a readback's wait for its write-back ends at the LAST part of the recording that writes it
/// back, not the first (wb_rec_wait.h readback_wait: the exposure meter read as 0 between a recording's parts, the brightness
/// flashes). On unless =0, read once
bool rb_rec_end_on() {
static const bool v = [] {
const bool on = ::mkwvk::wb_rec_wait::rec_end_on(getenv("RX_VK_RB_REC_END"));
logf("vk: a readback waits for %s (RX_VK_RB_REC_END%s)",
on ? "the last part of the recording that writes it back" : "the first part of the recording that writes it back, as before",
on ? "=0 or unset: the first part, as before" : "=1: the last part; 0.8.5.1: off by default, its ~5 ms reads");
return on;
int rb_rec_end_mode() {
static const int v = [] {
namespace wr = ::mkwvk::wb_rec_wait;
const int m = wr::rec_end_mode(getenv("RX_VK_RB_REC_END"));
logf("vk: a readback waits for %s (RX_VK_RB_REC_END=%d; =0: the first part, as before; =1: the last part, every readback, "
"as 0.8.5; =2, the default: the last part where the CPU writes the range after reading it)",
m == wr::kRecEndAll ? "the last part of the recording that writes it back"
: m == wr::kRecEndCpuWritten ? "the last part of the recording that writes it back where the CPU writes the range, else the first part"
: "the first part of the recording that writes it back, as before",
m);
return m;
}();
return v;
}
bool rb_rec_end_on() { return rb_rec_end_mode() != ::mkwvk::wb_rec_wait::kRecEndOff; }
std::atomic<int64_t> st_rb_cpu_ranges{0}; // ((Triforce, 11-28) readbacks the last-part rule took, the CPU writing their range)
/// ((Triforce, 11-28) whether this readback takes the last-part rule (wb_rec_wait.h rec_end_applies)
bool rb_rec_end_for(uint64_t guest, uint64_t size) {
namespace wr = ::mkwvk::wb_rec_wait;
const int m = rb_rec_end_mode();
const bool cpu = m == wr::kRecEndCpuWritten && memory_rb_cpu_written(guest, size);
if (cpu) {
st_rb_cpu_ranges.fetch_add(1, std::memory_order_relaxed);
static std::mutex lm;
static std::unordered_set<uint64_t> told; // (the first 24 ranges that took it, once each)
std::lock_guard lk(lm);
if (told.size() < 24 && told.insert(guest).second)
logf("vk: readback %#llx +%#llx: the CPU writes it after reading it: it waits for its writer's last part (RX_VK_RB_REC_END=2)",
(unsigned long long)guest, (unsigned long long)size);
}
return wr::rec_end_applies(m, cpu);
}
std::atomic<int64_t> st_rb_went_on{0}, st_rb_went_on_ns{0}; // ((Triforce, 11-27) readbacks whose writer's recording went on past a part)
/// ((Triforce, 11-27) A readback's wait for write-back value v on the recordings' timeline, by the rule (rb_rec_end_on): true when
/// the writer's recording is known (the result in *ok, as wait_recorded), false when it is not (the caller's fallback)
bool readback_rec_wait(uint64_t v, int* ok) {
bool readback_rec_wait(uint64_t v, uint64_t guest, uint64_t size, int* ok) {
if (!rec_timeline || per_queue_fences_off()) return false;
struct Calls {
uint64_t v;
uint64_t v, guest, size;
// ((Triforce, 11-28) the part whose write-back last covered the read's range (vk_memory.cpp), for an early tail's reads
uint64_t range_seq() { return memory_wb_part_seq(guest, size, v); }
uint64_t first_part() {
std::lock_guard mk(rec_map_mu);
const auto it = value_rec.find(v);
@@ -6245,10 +6291,28 @@ bool readback_rec_wait(uint64_t v, int* ok) {
const VkResult res = vkWaitSemaphores(dev.device, &wi, ::mkwvk::wb_rec_wait::kSliceNs);
return res == VK_SUCCESS ? 0 : res == VK_TIMEOUT ? 1 : 2;
}
} calls{v};
} calls{v, guest, size};
GpuWaitTimer gwt("readback_rec_wait", v);
const auto t0 = std::chrono::steady_clock::now();
// ((Triforce, 11-28, diagnostic) RX_VK_LOG_RB_RANGE=1: the first 60 waits past a part, with the recording's entry and the
// range's part before and after, and what ended them)
static const bool rb_log = getenv("RX_VK_LOG_RB_RANGE") != nullptr;
const ::mkwvk::wb_rec_wait::Parts::Entry e0 = rb_log ? calls.look() : ::mkwvk::wb_rec_wait::Parts::Entry{};
const uint64_t rs0 = rb_log ? calls.range_seq() : 0;
const ::mkwvk::wb_rec_wait::ReadbackWait r = ::mkwvk::wb_rec_wait::readback_wait(rb_rec_end_on(), calls);
if (rb_log && r.went_on) {
static std::atomic<int> told{0};
if (told.fetch_add(1) < 60) {
const ::mkwvk::wb_rec_wait::Parts::Entry e1 = calls.look();
logf("vk: rb range: v %llu %#llx +%#llx: at the read part %llu ended %d scene_part %llu scene_seq %llu range %llu; "
"after %.2f ms part %llu ended %d scene_part %llu scene_seq %llu range %llu, slices %lld",
(unsigned long long)v, (unsigned long long)guest, (unsigned long long)size, (unsigned long long)e0.seq,
(int)e0.ended, (unsigned long long)e0.scene_part, (unsigned long long)e0.scene_seq, (unsigned long long)rs0,
std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count(), (unsigned long long)e1.seq,
(int)e1.ended, (unsigned long long)e1.scene_part, (unsigned long long)e1.scene_seq,
(unsigned long long)calls.range_seq(), (long long)r.slices);
}
}
if (!r.known) return false;
*ok = r.ok ? 1 : 0;
if (r.went_on)
@@ -6261,8 +6325,8 @@ bool readback_rec_wait(uint64_t v, int* ok) {
// by a hash of its control flow (if, ?:, !, &&, ||), and an `if` or `?:` added in invalidate (or `!` in submit_recording's
// note) left both at plain -O2, invalidate's ~460,000 calls a benchmark run among them. A call in place of a call keeps it.
/// A readback's wait on the recordings' timeline: by the rule (readback_rec_wait), or the first part as before (wait_recorded)
bool readback_recorded_wait(uint64_t v, int* ok) {
return rb_rec_end_on() ? readback_rec_wait(v, ok) : wait_recorded(v, UINT64_MAX, ok);
bool readback_recorded_wait(uint64_t v, uint64_t guest, uint64_t size, int* ok) {
return rb_rec_end_for(guest, size) ? readback_rec_wait(v, guest, size, ok) : wait_recorded(v, UINT64_MAX, ok);
}
/// RX_VK_LOG_READBACK_ZERO's line, with the writer's recording now: its first part (the old wait's), its newest part and
/// whether that ended it, and the recordings' timeline (a read between the parts waited for the first alone)
@@ -6285,10 +6349,13 @@ void readback_zero_line(long long presents, uint64_t gpu, uint64_t size, uint64_
/// The 3000-readback line's count of the readbacks whose writer's recording went on past the part first found (the old wait
/// ended there)
void readback_went_on_line() {
const int64_t cpu = st_rb_cpu_ranges.exchange(0);
if (const int64_t on = st_rb_went_on.exchange(0))
logf("vk: readbacks of GPU results: %lld of them found their writer's recording going on past a part and waited for "
"its last part, %.2f ms each (RX_VK_RB_REC_END=0: the first part, as before)",
(long long)on, st_rb_went_on_ns.exchange(0) / (double)on / 1e6);
"its last part, %.2f ms each; %lld took the rule, the CPU writing their range (RX_VK_RB_REC_END=0: the first part, as before)",
(long long)on, st_rb_went_on_ns.exchange(0) / (double)on / 1e6, (long long)cpu);
else if (cpu)
logf("vk: readbacks of GPU results: %lld took the last-part rule, the CPU writing their range; none waited past a part", (long long)cpu);
}
// ((Triforce, 11-22, the performance audit's SYNC-122, after Source B's) A readback whose write-back is not recorded yet
// (wait_recorded said so: its writer is still being recorded, or stopped at an event wait). It waited on the submissions'
@@ -6798,10 +6865,10 @@ void invalidate(uint64_t gpu, uint64_t size) {
} else if (!is_late && v > completed()) {
int ok = 0;
// ((Triforce, 11-27) RX_VK_RB_REC_END, on: the last part of the writer's recording, not its first (readback_recorded_wait)
if (!(unforced && readback_recorded_wait(v, &ok))) {
if (!(unforced && readback_recorded_wait(v, host->gpu_guest(gpu), size, &ok))) {
if (unforced && !async_on() && replay_behind()) wait_queue_empty(); // (not recorded whole: as before)
if (async_unforced()) { // (its recording is being made: when the replay thread has it, on its timeline)
while (completed() < v && !readback_recorded_wait(v, &ok) && async_wait_step(v)) {}
while (completed() < v && !readback_recorded_wait(v, host->gpu_guest(gpu), size, &ok) && async_wait_step(v)) {}
if (completed() < v && !ok) wait_value(v);
} else {
// ((Triforce, 11-22, SYNC-122) the fallback: counted; in slices that look the recording up under

View File

@@ -213,6 +213,12 @@ struct Parts {
struct Entry {
uint64_t seq = 0; // (0: not known)
bool ended = false;
// ((Triforce, 11-28) Start next frame early (tail_early.h): the part the scene ended with, where the tail began
// (scene_part), and that part again once the tail passed its verdict (scene_seq). A tail that passes wrote nothing the
// game reads back, so the scene's part is the last that writes back what a readback waits for: the readback stops
// there, not at the tail's end. Waiting for the tail cost the owner ~20 fps at his save (game-thread reads of ~6.5 ms))
uint64_t scene_part = 0;
uint64_t scene_seq = 0;
};
std::map<uint64_t, Entry> by_value;
size_t cap = 8192;
@@ -227,16 +233,39 @@ struct Parts {
/// The same from the submit's side (vk_replay.cpp submit_recording): part, a submit that does not end the recording. (The
/// `!` is here so that the submit's code keeps the control flow its renderer profile was recorded with)
void part_submitted(uint64_t value, uint64_t seq, bool part) { submitted(value, seq, !part); }
/// ((Triforce, 11-28) the early tail began after the part just submitted for value (vk_replay.cpp tail_begin)
void tail_began(uint64_t value) {
const auto it = by_value.find(value);
if (it != by_value.end()) it->second.scene_part = it->second.seq;
}
/// ((Triforce, 11-28) value's early tail passed its verdict (vk_replay.cpp tail_end): readbacks stop at the scene's part
void tail_passed(uint64_t value) {
const auto it = by_value.find(value);
if (it != by_value.end() && it->second.scene_part) it->second.scene_seq = it->second.scene_part;
}
Entry of(uint64_t value) const {
const auto it = by_value.find(value);
return it == by_value.end() ? Entry{} : it->second;
}
};
/// RX_VK_RB_REC_END's text: ((Triforce, 0.8.5.1)) OFF unless set to a number other than 0. On, the game's worker threads' reads of
/// its double-buffered GPU results waited ~5 ms for the whole frame's recording (the owner's save: 70 fps for 85-91). The fix for
/// the brightness flashes comes back without that cost (only the GPU work the game waited for)
inline bool rec_end_on(const char* text) { return text && *text && std::atoi(text) != 0; }
/// RX_VK_RB_REC_END's modes. ((Triforce, 11-28) 0.8.5 waited for the last part at every readback (kRecEndAll): the update's
/// workers' reads of results the CPU only reads waited ~5 ms each (98 -> 82 fps in Hyrule Field at the owner's settings);
/// 0.8.5.1 turned it off. Now, by default, a readback waits for the last part where the CPU writes the range after reading it
/// (kRecEndCpuWritten: the exposure meter, whose late read lost the CPU's 0 and flashed), and for the first part, as before,
/// where it only reads it (a frame late at worst, then the next read has it). vk_memory.cpp memory_rb_cpu_written
enum RecEnd { kRecEndOff = 0, kRecEndAll = 1, kRecEndCpuWritten = 2 };
/// The mode from the switch's text: unset or empty, 2 (the CPU-written ranges); 0 or a word (atoi, as the renderer's other
/// switches), off: the first part, as before; 1, every readback, as 0.8.5; another number, 2
inline int rec_end_mode(const char* text) {
if (!text || !*text) return kRecEndCpuWritten;
const int v = std::atoi(text);
return v == 0 ? kRecEndOff : v == 1 ? kRecEndAll : kRecEndCpuWritten;
}
/// Whether a readback takes the last-part rule: by the mode, and in mode 2 whether the CPU writes its range (cpu_written)
inline bool rec_end_applies(int mode, bool cpu_written) { return mode == kRecEndAll || (mode == kRecEndCpuWritten && cpu_written); }
/// The switch on at all (a mode other than 0)
inline bool rec_end_on(const char* text) { return rec_end_mode(text) != kRecEndOff; }
/// What a readback's wait by recordings did
struct ReadbackWait {
@@ -266,13 +295,24 @@ ReadbackWait readback_wait(bool rec_end, D& d) {
if (!e.seq) return r;
r.known = true;
for (;;) {
if (d.rec_wait(e.seq) != 0) return r;
if (e.ended) break;
// ((Triforce, 11-28) an early tail that passed: its scene's part is the last that writes back anything the game reads)
uint64_t target = e.scene_seq ? e.scene_seq : e.seq;
// ((Triforce, 11-28) the tail begun (every part of the scene submitted, their write-backs noted): a range one of the scene's
// parts copied back is waited for at that part. Nothing after the tail's start writes it back but the tail, which only
// writes what its verdict allows; a range no scene part covered is the tail's, the last part as before. The worker
// threads' reads came while the tail was still being recorded, before its verdict: ~6 ms each at the owner's save)
bool at_range = false;
if (!e.ended && !e.scene_seq && e.scene_part) {
const uint64_t rs = d.range_seq();
if (rs && rs <= e.scene_part) target = rs, at_range = true;
}
if (d.rec_wait(target) != 0) return r;
if (e.ended || e.scene_seq || at_range) break;
r.went_on = true; // (a part: the recording's later parts write back too)
for (;;) {
d.flush(); // (a batched submit's recording is noted once the batch goes out: RX_VK_PEND_HELD)
const Parts::Entry n = d.look();
if (n.ended || n.seq != e.seq) {
if (n.ended || n.seq != e.seq || n.scene_seq || n.scene_part != e.scene_part) {
e = n;
break;
}