mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-29 04:18:49 -04:00
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.
333 lines
21 KiB
C++
333 lines
21 KiB
C++
// (Triforce, wp20-onepct; the handoff's §12.9: a read waits for its writer's part, not its whole submission) A wait for a
|
|
// write-back still on the GPU (vk_replay.cpp vk_timeline_wait: a capture's late pages on the game's submitting thread, a read or
|
|
// a binding of a page put off) waited for the submissions' timeline to reach the writer's value. That timeline is signalled only
|
|
// as far as no older submission is stopped at an event wait (vk_replay.cpp finish_recording): with another queue's work stopped,
|
|
// the wait went on after the GPU had done the write-back, until that work was let on and recorded. The lab's steady Market had a
|
|
// late-page wait of 20 to 46 ms about once a minute with the GPU 4 submissions behind at 5.5 ms of GPU time a frame (r37, r38,
|
|
// r41: 186 waits of 8 ms or more in 15 runs, the median 11.9 ms), each a stall of the game's thread.
|
|
//
|
|
// The recordings' timeline (vk_replay.cpp rec_timeline) is signalled at the end of every recording, in the queue's order, and a
|
|
// signal covers all that was submitted before it; so the recording a submission was finished in, done, means all that submission
|
|
// recorded is done, its write-backs too. The write-backs a recording makes are noted under its value (vk_memory.cpp
|
|
// wb_pages_note, mem_rec_value), and only a submission finished in a recording is known here (Finished), never one still being
|
|
// recorded or stopped at a wait: those are waited for on the timeline as before. The timeline cannot reach a value before the
|
|
// recording that finished it is done, so waiting for the recording first never makes the wait longer: the part after it is the
|
|
// timeline held (held_ns), counted always (the stall line, the frame trace, the capture's late line). RX_VK_WB_WAIT_REC=1 ends the
|
|
// wait there. Off by default: the captures' late-page wait orders something the game's frame depends on that is not known yet
|
|
// (vk_memory.cpp take_capture, RX_VK_LATE_NEW), and what the stopped work writes back lands after the copy taken without its
|
|
// wait. Tried with the lab's flicker count (scenario.ps1 -Flick) before it is turned on. tests/test_wb_rec_wait.py
|
|
#pragma once
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <map>
|
|
#include <vector>
|
|
|
|
namespace mkwvk::wb_rec_wait {
|
|
|
|
/// The longest wait (ns): vk_timeline_wait's bound, both waits together
|
|
constexpr uint64_t kBoundNs = 250000000ull;
|
|
/// A held part shorter than this is the two signals' own latency, not the timeline held (ns)
|
|
constexpr int64_t kHeldMinNs = 100000;
|
|
|
|
/// RX_VK_WB_WAIT_REC's text: on with a number other than 0
|
|
inline bool on(const char* text) { return text && *text && std::atoi(text) != 0; }
|
|
|
|
/// What is left of the bound for the timeline's wait after the recording's took spent_ns
|
|
inline uint64_t budget_left(uint64_t bound_ns, int64_t spent_ns) {
|
|
if (spent_ns <= 0) return bound_ns;
|
|
return (uint64_t)spent_ns >= bound_ns ? 0 : bound_ns - (uint64_t)spent_ns;
|
|
}
|
|
|
|
/// Whether the wait ends with the recording's: the switch on, the recording known, and done within the bound
|
|
inline bool ends_at_recording(bool switch_on, bool known, bool done) { return switch_on && known && done; }
|
|
|
|
/// The part of a wait of total_ns after its writer's recording was done (rec_ns into it): 0 when the recording is not known, or
|
|
/// was not done within the bound, or the rest is the signals' latency alone
|
|
inline int64_t held_ns(bool known, bool done, int64_t rec_ns, int64_t total_ns) {
|
|
if (!known || !done || total_ns <= rec_ns) return 0;
|
|
const int64_t h = total_ns - rec_ns;
|
|
return h >= kHeldMinNs ? h : 0;
|
|
}
|
|
|
|
/// The finished submissions: value -> the recording (its number on the recordings' timeline) it was finished in; the oldest go
|
|
/// past cap. Noted at the submit that ends the recording they were finished in (submitted), never for one still being recorded
|
|
struct Finished {
|
|
std::map<uint64_t, uint64_t> by_value;
|
|
size_t cap = 8192;
|
|
/// ((Triforce) the submissions finished in a recording that goes on in another part. A part's submit (a split, the early
|
|
/// tail's start, a parallel replay's setup before its segments) is not the end of their write-backs: a burst's are noted
|
|
/// under its value, and the last of them are recorded at the burst's end (vk_replay.cpp finish_recording,
|
|
/// memory_end_submission), a later part than the piece that finished them; a parallel replay's setup records none. Noted at
|
|
/// the part's number, a wait ended there (RX_VK_WB_WAIT_REC=1) before its writer's part had run)
|
|
std::vector<uint64_t> pending;
|
|
/// A recording's submit, its number rec: values, the submissions finished in it since its submit before. ends: the recording
|
|
/// ends with it (finish_recording, a present's); else a part of it, and they are noted at the submit that ends the recording
|
|
void submitted(const std::vector<uint64_t>& values, uint64_t rec, bool ends) {
|
|
pending.insert(pending.end(), values.begin(), values.end());
|
|
if (!ends) return;
|
|
for (uint64_t v : pending) note(v, rec);
|
|
pending.clear();
|
|
}
|
|
void note(uint64_t value, uint64_t rec) {
|
|
if (!value || !rec) return;
|
|
by_value[value] = rec;
|
|
while (by_value.size() > cap) by_value.erase(by_value.begin());
|
|
}
|
|
/// The recording value was finished in; 0: not known (not finished yet, or too old)
|
|
uint64_t of(uint64_t value) const {
|
|
const auto it = by_value.find(value);
|
|
return it == by_value.end() ? 0 : it->second;
|
|
}
|
|
};
|
|
|
|
// ---- (Triforce, 11-22, the performance audit's SYNC-122, after Source B's) The reads' side of the same held timeline ----
|
|
// A CPU readback (vk_replay.cpp invalidate) waits for its own write-back's value. Where the recording that carries it is known
|
|
// it waits on the recordings' timeline (wait_recorded) and a stopped queue does not hold it. Where it is not known yet (the
|
|
// writer still being recorded, or stopped at an event wait) it fell back to the submissions' timeline with no bound and no
|
|
// second look: recorded and done a moment later, the read still waited until every older stopped submission had gone on. The
|
|
// reads and bindings of pages put off (vk_memory.cpp late_unresolved_read, late_deferred_bound) wait in vk_timeline_wait, whose
|
|
// held part was counted for all its callers together.
|
|
//
|
|
// RX_VK_WB_WAIT_REC is a mode now: 1 as it was (the waits end at the writer's recording), 2 the check: nothing ends earlier,
|
|
// and the readback's fallback counts what 1 would not have waited. Unset, 0 or any other text: off, the fallback is the one
|
|
// unbounded wait it was. Under 1 and 2 the fallback waits in slices of 1 ms and looks the recording up between them.
|
|
// RX_VK_TL_REC=1 (and the lab knob tlrec) ended a capture's wait at value_rec's recording, a submission's FIRST part unless
|
|
// RX_VK_REC_LAST: before the burst's write-backs are recorded. It now ends the wait as RX_VK_WB_WAIT_REC=1 does, at the
|
|
// recording the submission was finished in (Finished); RX_VK_TL_REC=2 keeps the old lookup.
|
|
|
|
/// RX_VK_WB_WAIT_REC's modes
|
|
enum Mode { kOff = 0, kOn = 1, kCheck = 2 };
|
|
/// Its text: "1" on, "2" the check; unset, empty, "0" and anything else (a word, another number, a number with a tail): off
|
|
inline int mode(const char* text) {
|
|
if (!text || !text[0] || text[1]) return kOff;
|
|
return text[0] == '1' ? kOn : text[0] == '2' ? kCheck : kOff;
|
|
}
|
|
/// RX_VK_TL_REC's text names the old lookup (value_rec: a first part): the number 2
|
|
inline bool tl_rec_first_part(const char* text) { return text && std::atoi(text) == 2; }
|
|
/// Whether a capture's wait ends at the recording its submission was finished in: RX_VK_WB_WAIT_REC=1, or RX_VK_TL_REC (its
|
|
/// switch or its knob: tl_rec) unless that asks for the old lookup, which has its own wait before this one
|
|
inline bool ends_by_switch(int wb_mode, bool tl_rec, bool tl_first_part) {
|
|
return wb_mode == kOn || (tl_rec && !tl_first_part);
|
|
}
|
|
|
|
/// The waits' callers, for the held part by caller: a capture's take, a read of a page put off, a binding of one, a readback
|
|
enum Caller { kTake = 0, kRead = 1, kBind = 2, kReadback = 3, kCallers = 4 };
|
|
inline const char* caller_name(int who) {
|
|
static const char* const names[kCallers] = {"takes", "reads", "binds", "readbacks"};
|
|
return who >= 0 && who < kCallers ? names[who] : "?";
|
|
}
|
|
|
|
/// A slice of the readback's fallback wait (ns)
|
|
constexpr uint64_t kSliceNs = 1000000ull;
|
|
/// Whether the fallback waits in slices and looks the recording up between them: the mode on or the check, and a recordings'
|
|
/// timeline to look on. Otherwise the one unbounded wait on the submissions' timeline, as before
|
|
inline bool fallback_sliced(int wb_mode, bool has_rec_timeline) { return wb_mode != kOff && has_rec_timeline; }
|
|
|
|
/// What a fallback wait did
|
|
struct FallbackResult {
|
|
bool sliced = false; // (it waited in slices)
|
|
bool known = false; // (the writer's recording became known during the wait)
|
|
bool at_recording = false; // (it ended with the recording's wait: mode 1)
|
|
bool error = false; // (a wait gave something other than done or a timeout: a lost device; the wait ends)
|
|
int64_t rec_ns = -1; // (when the recording was seen done, from the wait's start; -1: never seen)
|
|
int64_t total_ns = 0;
|
|
int64_t slices = 0;
|
|
};
|
|
/// The fallback wait over the calls it makes, so that a test can run it against a made-up GPU. d has:
|
|
/// void plain() the old wait: the held submits flushed, then the submissions' timeline, no bound
|
|
/// void flush() the held submits flushed (nothing when none is held)
|
|
/// uint64_t lookup() the recording the value was finished in, 0 while not known
|
|
/// int rec_wait(seq) the recordings' timeline waited for with no bound: 0 done, 2 an error
|
|
/// bool rec_done(seq) the recordings' timeline has passed seq (a poll)
|
|
/// int slice() the submissions' timeline waited for kSliceNs: 0 reached, 1 not yet, 2 an error
|
|
/// int64_t now() ns since the wait's start
|
|
/// Mode 1 ends at the recording once it is known (no overall bound: the read needs its bytes). Mode 2 ends where the old wait
|
|
/// ends, when the submissions' timeline reaches the value, and only notes when the recording was done. Mode 0: plain()
|
|
template <class D>
|
|
FallbackResult fallback_wait(int wb_mode, bool has_rec_timeline, D& d) {
|
|
FallbackResult r;
|
|
if (!fallback_sliced(wb_mode, has_rec_timeline)) {
|
|
d.plain();
|
|
r.total_ns = d.now();
|
|
return r;
|
|
}
|
|
r.sliced = true;
|
|
uint64_t seq = 0;
|
|
for (;;) {
|
|
d.flush(); // (a batched submit holds both timelines' signals; the writer may have been recorded and held since)
|
|
if (!seq) seq = d.lookup();
|
|
if (seq) {
|
|
r.known = true;
|
|
if (wb_mode == kOn) {
|
|
r.error = d.rec_wait(seq) != 0;
|
|
r.at_recording = !r.error;
|
|
r.rec_ns = d.now();
|
|
break;
|
|
}
|
|
if (r.rec_ns < 0 && d.rec_done(seq)) r.rec_ns = d.now();
|
|
}
|
|
const int s = d.slice();
|
|
r.slices++;
|
|
if (s == 1) continue;
|
|
r.error = s != 0;
|
|
// (the timeline cannot reach the value before the recording that finished it is done: seen now if not before)
|
|
if (!r.error && seq && r.rec_ns < 0 && d.rec_done(seq)) r.rec_ns = d.now();
|
|
break;
|
|
}
|
|
r.total_ns = d.now();
|
|
return r;
|
|
}
|
|
/// The part of a checked fallback wait (mode 2) after its writer's recording was seen done: what mode 1 would not have waited.
|
|
/// To a slice's grain: the recording is looked at between slices
|
|
inline int64_t fallback_held_ns(const FallbackResult& r) {
|
|
if (!r.sliced || r.at_recording || r.error || r.rec_ns < 0) return 0;
|
|
return held_ns(true, true, r.rec_ns, r.total_ns);
|
|
}
|
|
|
|
// ---- (Triforce, 11-27) A readback waits for the LAST part of the recording that writes it back ----
|
|
// The owner's random brightness changes: in a still view the picture flashed brighter for a frame, then stepped down, a few times
|
|
// a minute (3 times at Hyrule Field in a four-scene held-camera benchmark run, on v0.8.4 as on the GPU pass). Each flash is
|
|
// one present after the game read its exposure meter (a 64-byte sum the GPU adds to each frame; the CPU reads it and sets it to 0)
|
|
// back as all zeros, after a wait for the meter's write-back (RX_VK_LOG_READBACK_ZERO: "waited 1", and the GPU's done value still
|
|
// below the write-back's value after the wait). A recording made in parts (vk_replay.cpp maybe_split: RX_VK_CHUNK's counted parts,
|
|
// 240 draws on NVIDIA's driver, or Threaded rendering's pass-end parts) writes back at each part's end, and every part's
|
|
// write-backs are noted under the recording's own value (Recording::value, vk_memory.cpp mem_rec_value). The readback's wait
|
|
// (vk_replay.cpp invalidate, on the game's submitting thread's replay with nothing forced: the default) looked that value up in
|
|
// value_rec, which keeps the recording's FIRST part (emplace; RX_VK_REC_LAST, off, keeps a later one only when the value comes
|
|
// again). So a read that came between the first part's submit and the last one's waited for the first part alone and read what
|
|
// guest memory held then: the CPU's 0, the GPU's sum still in a later part. The sum landed after the read, over the CPU's next 0,
|
|
// and the next frame added to it (2x): the step down. The capture's wait (vk_timeline_wait) already looks its writer up at the
|
|
// recording's end (Finished); this is the readback's side.
|
|
// Now the readback looks up the newest part submitted of the recording by its value, and whether that part ended the recording
|
|
// (Parts, noted at each submit). An ended recording: its last part is waited for, as the old wait waited for the first. A
|
|
// recording that goes on: the part known is waited for (the old wait, never longer), then the readback looks again between 1 ms
|
|
// slices of the submissions' timeline, until the recording has ended (then its last part) or the timeline reaches the value
|
|
// (everything up to it done). A value not known at all is the caller's fallback, as before. RX_VK_RB_REC_END=0: the first part, as
|
|
// before. tests/test_readback_rec_end.py
|
|
|
|
/// A recording's parts by its own value: the newest part submitted (its number on the recordings' timeline) and whether that part
|
|
/// ended the recording. A later recording made under the same value (a stopped submission's, replayed again) takes the entry over
|
|
/// from its first part on. The oldest go past cap
|
|
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;
|
|
/// A submit of recording value as number seq: ends, the submit that ends it (finish_recording, a present's); else a part
|
|
void submitted(uint64_t value, uint64_t seq, bool ends) {
|
|
if (!value || !seq) return;
|
|
Entry& e = by_value[value];
|
|
if (seq < e.seq) return; // (the numbers rise: an older submit's note after a newer one changes nothing)
|
|
e.seq = seq, e.ended = ends;
|
|
while (by_value.size() > cap) by_value.erase(by_value.begin());
|
|
}
|
|
/// 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 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 {
|
|
bool known = false; // (the writer's recording was known: false, the caller's fallback waits)
|
|
bool ok = false; // (the wait came back done: false after an error, a lost device)
|
|
bool went_on = false; // (its recording went on past the part first found: what the old wait would have stopped at)
|
|
int64_t slices = 0; // (1 ms slices of the submissions' timeline while the recording went on)
|
|
};
|
|
/// The readback's wait over the calls it makes, so that a test can run it against a made-up GPU. d has:
|
|
/// uint64_t first_part() the old lookup (value_rec): the recording's first part, 0 while not known
|
|
/// Parts::Entry look() the newest part of the recording by its value, and whether it ended the recording
|
|
/// int rec_wait(seq) the held submits flushed, the recordings' timeline waited for with no bound: 0 done, 2 an error
|
|
/// void flush() the held submits flushed (nothing when none is held)
|
|
/// int slice() the submissions' timeline waited for kSliceNs: 0 reached, 1 not yet, 2 an error
|
|
/// rec_end: the rule (RX_VK_RB_REC_END); off, the first part alone, as before
|
|
template <class D>
|
|
ReadbackWait readback_wait(bool rec_end, D& d) {
|
|
ReadbackWait r;
|
|
if (!rec_end) {
|
|
const uint64_t seq = d.first_part();
|
|
if (!seq) return r;
|
|
r.known = true;
|
|
r.ok = d.rec_wait(seq) == 0;
|
|
return r;
|
|
}
|
|
Parts::Entry e = d.look();
|
|
if (!e.seq) return r;
|
|
r.known = true;
|
|
for (;;) {
|
|
// ((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 || n.scene_seq || n.scene_part != e.scene_part) {
|
|
e = n;
|
|
break;
|
|
}
|
|
const int s = d.slice();
|
|
r.slices++;
|
|
if (s == 0) { // (the timeline at the value: every recording up to it done, its last part too)
|
|
r.ok = true;
|
|
return r;
|
|
}
|
|
if (s != 1) return r;
|
|
}
|
|
}
|
|
r.ok = true;
|
|
return r;
|
|
}
|
|
|
|
} // namespace mkwvk::wb_rec_wait
|