mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-28 20:08:51 -04:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user