From 8cb23131f9117bb6dd358d7cfcbb206eafea0010 Mon Sep 17 00:00:00 2001 From: ruckus7382sfgsgdf <355121112+ruckus7382sfgsgdf@users.noreply.github.com> Date: Sat, 28 Nov 2026 15:45:28 -0600 Subject: [PATCH] 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. --- docs/development.md | 2 +- gpu/vk/vk_internal.h | 4 + gpu/vk/vk_memory.cpp | 114 ++++++++++++++++++++++++++- gpu/vk/vk_replay.cpp | 99 ++++++++++++++++++++---- gpu/vk/wb_rec_wait.h | 54 +++++++++++-- host/discord_activity.h | 6 +- readme.md | 8 +- tests/tail_early_test.cpp | 4 + tests/test_discord_activity.py | 10 ++- tests/test_readback_rec_end.cpp | 123 +++++++++++++++++++++++++++++- tests/test_readback_rec_end.py | 28 ++++--- tests/test_recorded_wait_flush.py | 8 +- tests/test_tail_early.py | 6 +- 13 files changed, 412 insertions(+), 54 deletions(-) diff --git a/docs/development.md b/docs/development.md index 107ea4f..7873419 100644 --- a/docs/development.md +++ b/docs/development.md @@ -201,7 +201,7 @@ case-sensitive, and an unknown one is ignored. | `RX_VK_SETTLE_GATE=1`, `=2` | (Triforce, 11-22, off by default; the performance audit's SYNC-29, after Source A's; `gpu/vk/settle_gate.h`) A CPU read that lets the stopped queues whose waits are met go on (the readback's look, which `RX_VK_RB_SETTLE=1` turns on, and the two looks made for a range a texture may be in) takes the order's lock whenever a queue is stopped, and that lock is held through whole replays and presents. With the gate a site looks only while a queue is stopped **and** the CPU has signalled an event since the last finished look, or a stopped queue waits on an event whose value is read from guest memory; any other met wait was met by a signal in the stream, and the thread that replayed it looked before it let the lock go. `1`: the readback's look; `2`: the texture sites' too (new ground: those looks order the exposure meter's readback, so `2` needs the lab's flicker count). Unset or `0`: every look as before. The `RX_VK_LAB_AB` knobs `settlegate` and `texgate` are `1` and `2` in their phases (`rbsettle+settlegate` is the pair the audit asks for). The 3000-readback line and the texture catch-ups' 300-call line count the looks made and skipped | | `RX_VK_READBACK_LATE=[,]`, `RX_VK_LOG_READBACK_RANGES=1` | (Triforce, 11-22, the performance audit's SYNC-19, after Source A's; `gpu/vk/readback_late.h`) Ranges the CPU reads back **without** the wait for their write-back, from what guest memory holds (the last write-back made): a rule is a size in hex (`3980`: every range of 0x3980 bytes, the culling camera block Source A reads late by default) or `newest` (a range whose writer is the newest submission handed out: the game cannot have waited for it). **Unset: no rule**, every readback waits as before, until the lab has run it where the waits are long (a handheld, an AMD iGPU, a weak discrete card); `=0`: none either. The `RX_VK_LAB_AB` knob `rblate` is `3980` in its phases. A late read hands the game the frame before's values, and when the CPU resets a range read late the pending write-back lands over its reset (the exposure meter's flashes): `newest` can catch that meter, so it is never a default. `RX_VK_LOG_READBACK_RANGES=1`: with every 3000-readback line, the 12 ranges that waited longest (readbacks, waits, ms, how often their writer was the newest submission, how often read late), to find such ranges on other hardware; nothing is noted without it | | `RX_VK_WB_WAIT_REC=1`, `=2` | (Triforce, off by default; the handoff's §12.9, `gpu/vk/wb_rec_wait.h`) A wait for a write-back still on the GPU (a capture's late pages on the game's submitting thread, a read or binding of a page put off) ends once the recording its submission was finished in is done on the GPU, not when the submissions' timeline reaches it: that timeline is held below any submission stopped at an event wait, and the lab's Market had 20-46 ms waits of the game's thread with the GPU 4 submissions behind. Whatever the switch, the wait for the recording comes first and the rest is counted: the stall line's `N of them X ms after their writer's recording was done`, the `late:` line's `ms of the wait after the recording that finished it was done`, the frame trace's `wb_held_ms`. Off: the captures' late-page wait orders something not known yet (`RX_VK_LATE_NEW`), so it is tried with the lab's flicker count first. (Triforce, 11-22, the performance audit's SYNC-122, after Source B's) The reads' side: a readback whose write-back is not recorded yet waited on the submissions' timeline with no bound and no second look; it is counted now (the 3000-readback line's second line), and the stall line names the held part by caller (takes, reads, binds, readbacks). The variable is a mode: `1` as above, and such a readback waits in 1 ms slices, looks its writer's recording up between them and ends with it; `2` is the check, where no wait ends earlier and the readback's slices count the part after the recording was done (what `1` would not have waited); unset, `0` or any other text: off, one wait as before. The check of the pages a later write-back changes (the audit's SYNC-101) is not built. `RX_VK_TL_REC=1` and the lab knob `tlrec` ended a capture's wait at a submission's first part, before its burst's write-backs were recorded: they now end it at the recording the submission was finished in, as `=1` here does; `RX_VK_TL_REC=2` keeps the old lookup | -| `RX_VK_RB_REC_END=1` | (Triforce, 0.8.5.1: OFF by default; =1 turns it on, its worker-thread reads waited ~5 ms each for the frame's whole recording, 70 fps at the owner's save for 85-91. 11-27; `gpu/vk/wb_rec_wait.h` `readback_wait`, `tests/test_readback_rec_end.py`) A CPU readback of GPU results waits for the **last** part of the recording that writes it back. A recording made in parts (`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, all under the recording's one value, and the readback's wait looked that value up in a table that keeps the first part (`RX_VK_REC_LAST`, off, keeps a later one only when the value comes again). A read that came between the first part's submit and the last one's got what guest memory held then: OoT's exposure meter read as 0 where the GPU's sum was still to come, then the sum landed over the CPU's next 0 and the next frame added to it. In a still view the picture flashed brighter for a frame and stepped down: 3 times at Hyrule Field in a four-scene held-camera benchmark run (`HOS_BENCH_PAN=0`), on v0.8.4 too. Now the readback looks up the newest part submitted and whether it ended the recording: an ended recording's last part is waited for; one that goes on, the part found (the old wait), then 1 ms slices of the submissions' timeline between looks until it ends or the timeline reaches the value. A value not recorded yet goes to the fallback as before. `RX_VK_LOG_READBACK_ZERO=1` now says the writer's recording's first part, newest part and the recordings done at a zero read; the 3000-readback line counts the reads that waited past a part. Measured on an RTX 5090 (held camera): the meter's zero reads after a wait went from 0-10 a run to 0 in 4 runs, and Hyrule Field's flashes from 3 a run to 0. The rule waits 350-550 times a run, 2-5 ms each, on the game's worker threads, nearly all at a scene's load and in its first seconds. Frame rate unchanged (144.5 against 143.2 fps, 3 runs each); Hyrule Field's p99 frame time 9-13% higher. Waiting only for the part that wrote the range would be cheaper (not done). The rule's branches stay out of `invalidate` and submit_recording's registration, so that the renderer's play profile still matches them. `0`: the first part, as before | +| `RX_VK_RB_REC_END=0` | (Triforce, 11-27; `gpu/vk/wb_rec_wait.h` `readback_wait`, `tests/test_readback_rec_end.py`) **11-28: modes.** Unset or `=2` (the default): the last-part wait below only for a range the CPU writes after reading it (the exposure meter; the pages the write watch's collections name, `vk_memory.cpp` `memory_rb_cpu_written`), the first part, as before, for a range it only reads (a frame late at worst). `=1`: every readback, as 0.8.5 (its worker-thread reads of read-only results waited ~5 ms each, 98 -> 82 fps in Hyrule Field at the owner's settings). `=0`: off, as 0.8.5.1. A CPU readback of GPU results waits for the **last** part of the recording that writes it back. A recording made in parts (`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, all under the recording's one value, and the readback's wait looked that value up in a table that keeps the first part (`RX_VK_REC_LAST`, off, keeps a later one only when the value comes again). A read that came between the first part's submit and the last one's got what guest memory held then: OoT's exposure meter read as 0 where the GPU's sum was still to come, then the sum landed over the CPU's next 0 and the next frame added to it. In a still view the picture flashed brighter for a frame and stepped down: 3 times at Hyrule Field in a four-scene held-camera benchmark run (`HOS_BENCH_PAN=0`), on v0.8.4 too. Now the readback looks up the newest part submitted and whether it ended the recording: an ended recording's last part is waited for; one that goes on, the part found (the old wait), then 1 ms slices of the submissions' timeline between looks until it ends or the timeline reaches the value. A value not recorded yet goes to the fallback as before. `RX_VK_LOG_READBACK_ZERO=1` now says the writer's recording's first part, newest part and the recordings done at a zero read; the 3000-readback line counts the reads that waited past a part. Measured on an RTX 5090 (held camera): the meter's zero reads after a wait went from 0-10 a run to 0 in 4 runs, and Hyrule Field's flashes from 3 a run to 0. The rule waits 350-550 times a run, 2-5 ms each, on the game's worker threads, nearly all at a scene's load and in its first seconds. Frame rate unchanged (144.5 against 143.2 fps, 3 runs each); Hyrule Field's p99 frame time 9-13% higher. Waiting only for the part that wrote the range would be cheaper (not done). The rule's branches stay out of `invalidate` and submit_recording's registration, so that the renderer's play profile still matches them. `0`: the first part, as before | | `RX_VK_LATE_QUEUE=1` or `0` | (Triforce, 11-20; 11-23) A capture's late pages (pages a submission still on the GPU writes back: OoT's exposure meter and the per-frame pools) waited for at the queue's next submit, not at the capture's take (`gpu/vk/vk_memory.cpp` `late_queue_on`). Unset: at the queue, except while the replay's submits are held (`RX_VK_BATCH`, on by default with Mesa's RADV): there a held submit applies no fix, the fixes wait for the batch's flush and copy guest memory the game has written again for its next frame, and the replay reads those slots live meanwhile: the lighting and exposure flicker on Linux AMD from 50-90 s into play in v0.7.0. `1`: at the queue with held submits too (v0.7.0 as shipped); `0`: at the take on every driver, as before 11-20. The log says `vk: late pages' wait ...` when it is first asked and when the answer changes | | `RX_VK_LATE_INLINE=1` | (Triforce, 11-22, the performance audit's MEM-80, after Source B's; `gpu/vk/late_inline.h`; **off unless set**, not yet run in the game) A late page (one the CPU wrote while a submission still on the GPU writes it back) is waited for and taken again by default (`RX_VK_LATE_QUEUE`: at the queue's next submit). `RX_VK_LATE_NEW=1` (knob `latenew`) turns four no-wait rules on, each of which brought brightness steps back at the Castle Town save. This turns on the partial rule alone (knob `lateinline`): a page only part of which a submission on the GPU writes back has its other parts synced from the capture, no wait; pages of pools with no mirror, texture storage, pages written back whole and pages whose write-backs' ranges are not known are waited for as by default. The partial rule, here and under `RX_VK_LATE_NEW`, now asks which ranges are still on the GPU against the progress the capture read before its copies, not a second read at the take: a write-back done between the two counts as in flight, and its range is not synced from the older copy. `RX_VK_LOG_LATE` shows the pages waited for | | `RX_VK_PASS_TIMING=1` or `2` | GPU time per pass (2: per program too), logged every 600 presents | diff --git a/gpu/vk/vk_internal.h b/gpu/vk/vk_internal.h index 06f6e40..add44b8 100644 --- a/gpu/vk/vk_internal.h +++ b/gpu/vk/vk_internal.h @@ -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); diff --git a/gpu/vk/vk_memory.cpp b/gpu/vk/vk_memory.cpp index afc5a43..22a592d 100644 --- a/gpu/vk/vk_memory.cpp +++ b/gpu/vk/vk_memory.cpp @@ -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 wb_batch_now{0}; +std::map wb_batch_seq; // batch -> its part's number on the recordings' timeline std::vector 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_make(bool behind) { n = host->collect_written_in(reinterpret_cast(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(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 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 rb_ranges_seen; +std::atomic rb_collections{0}; +std::atomic 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(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 diff --git a/gpu/vk/vk_replay.cpp b/gpu/vk/vk_replay.cpp index 85c8811..49a28c8 100644 --- a/gpu/vk/vk_replay.cpp +++ b/gpu/vk/vk_replay.cpp @@ -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 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 q_last; // queue -> its last submission's value (sync_order_mu) std::map value_queue; // a submission's value -> its queue (sync_order_mu) std::atomic 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 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 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 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 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(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 diff --git a/gpu/vk/wb_rec_wait.h b/gpu/vk/wb_rec_wait.h index 6333975..51371b4 100644 --- a/gpu/vk/wb_rec_wait.h +++ b/gpu/vk/wb_rec_wait.h @@ -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 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; } diff --git a/host/discord_activity.h b/host/discord_activity.h index 8992b71..b9177cb 100644 --- a/host/discord_activity.h +++ b/host/discord_activity.h @@ -15,8 +15,10 @@ {name|text} text (itself a template) when name is not empty, else nothing {!name|text} text when name is empty (the title: {!location|...}) {{ and }} a brace */ -/* ((Triforce) the defaults: the status as it was before these settings, "Triforce v0.7.1" with Triforce's logo) */ -#define DISCORD_TEXT_DEFAULT "Triforce {release}" +/* ((Triforce) the defaults: Triforce's logo, and (11-28) the status that names where Link is (DISCORD_TEXT_PLACE below). It was + "Triforce {release}" (the status from before these settings), which asks for no place: the game's state was never read + and every player saw "Triforce v0.8.5" alone, the place, age and stats only with a discord_text= line of their own) */ +#define DISCORD_TEXT_DEFAULT DISCORD_TEXT_PLACE #define DISCORD_IMAGE_DEFAULT "triangle_emblem_discord_zoomed_out_transparent" /* (the logo's art asset key on Discord's side) */ #define DISCORD_IMAGE_TEXT_DEFAULT "Triforce {release}" /* (a template that names where Link is, his age and his stats: "Kakariko Village - Adult" over hearts, Gold Skulltula tokens diff --git a/readme.md b/readme.md index 3a44401..20b0626 100644 --- a/readme.md +++ b/readme.md @@ -207,8 +207,9 @@ the game asks. They're saved in `triforce_options.ini` beside the game. In game, **F10** or **Alt+Enter** switches between a window and fullscreen. **F1** opens the same options from anywhere. **F8** takes a bug capture. -**Discord status.** With Discord open on your PC (and the menu's Discord status row on), your status says "Triforce" and -the version. You can write your own in `triforce_options.ini`, beside the game: +**Discord status.** With Discord open on your PC (and the menu's Discord status row on), your status says "Triforce", where +Link is, his age, hearts, Gold Skulltula tokens and pieces of heart ("Kakariko Village - Adult"), and the version on the +title screen. You can write your own in `triforce_options.ini`, beside the game: | Line | What it is | | --- | --- | @@ -216,7 +217,8 @@ the version. You can write your own in `triforce_options.ini`, beside the game: | `discord_image=` | The picture: an image key of the Discord application, or an `https://` link | | `discord_image_text=` | The picture's text when you point at it | -For example, the place and Link's stats over the version on the title screen: +The default is this one, the place and Link's stats over the version on the title screen (`discord_text=Triforce {release}` +is the old status, the version alone): `discord_text={location|{location} - {age}{break}{hearts|❤️ {hearts}/{max_hearts} · }{skulltulas|🕷️ {skulltulas} · }{heart_pieces|💗 {heart_pieces}}}{!location|{version} {build}}` Only that text goes to the Discord app on your PC; the game reads where Link is and his stats only while your text asks diff --git a/tests/tail_early_test.cpp b/tests/tail_early_test.cpp index 3d33d5b..5b7226c 100644 --- a/tests/tail_early_test.cpp +++ b/tests/tail_early_test.cpp @@ -172,6 +172,10 @@ void memory_end_submission(VkCommandBuffer cmd) { noted.clear(); } void memory_begin_submission() { noted.clear(); } +// ((11-28) the replay's calls the cut functions make that the model has no part in: the readbacks' tail notes (vk_replay.cpp +// rb_tail_note) and the last piece of a recording's uploads (vk_texture.cpp texture_upload_last: one piece here) +void rb_tail_note(uint64_t, bool) {} +VkCommandBuffer texture_upload_last(VkCommandBuffer base) { return base; } Recording& begin_recording(uint64_t value) { recordings.push_back(std::make_unique()); Recording& r = *recordings.back(); diff --git a/tests/test_discord_activity.py b/tests/test_discord_activity.py index 04e784c..11dc17f 100644 --- a/tests/test_discord_activity.py +++ b/tests/test_discord_activity.py @@ -90,10 +90,12 @@ int main(int argc, char **argv) { const DiscordFields no_tokens = {"Kokiri Forest", "KokiriForest_Child", "Child", "v3.0.0", "", "", "v3.0.0", "3", "3", "0", "", ""}; DiscordSettings s; discord_settings_default(&s); - /* (the default: as before, "Triforce" and the release, in a map or not) */ - CHECK(lines(s.status, &play, "Triforce v3.0.0-rc.7-25-g7a029028", "") && lines(s.status, &title, "Triforce v3.0.0-rc.7-25-g7a029028", "")); + /* ((Triforce, 11-28) the default: the place (DISCORD_TEXT_PLACE) in a map, the version on the title screen */ + CHECK(!strcmp(s.status, DISCORD_TEXT_PLACE)); + CHECK(lines(s.status, &play, "Kakariko Village - Adult", "") && lines(s.status, &title, "v3.0.0 RC7", "")); CHECK(lines(s.image_text, &release, "Triforce v3.0.0", "") && !strcmp(s.image, DISCORD_IMAGE_DEFAULT)); - CHECK(!discord_settings_use_game(&s)); /* (the game's state is not read for it) */ + CHECK(discord_settings_use_game(&s)); /* (the game's state is read for it) */ + CHECK(discord_settings_line(&s, "discord_text=Triforce {release}\n") && !discord_settings_use_game(&s)); /* (the old one: not read) */ CHECK(discord_settings_line(&s, "discord_text=" DISCORD_TEXT_PLACE "\n") && discord_settings_use_game(&s)); CHECK(discord_settings_line(&s, "discord_text={!age|x}") && discord_settings_use_game(&s)); CHECK(discord_settings_line(&s, "discord_text={version} {mapx}") && !discord_settings_use_game(&s)); @@ -273,7 +275,7 @@ def main(): stamp = {"start": 1700000000} logo = "triangle_emblem_discord_zoomed_out_transparent" # (Triforce's art, as before) want = { - "plain": {"details": "Triforce v3.0.0", "timestamps": stamp, + "plain": {"details": "Kakariko Village - Adult", "timestamps": stamp, # ((Triforce, 11-28) the default names the place) "assets": {"large_image": logo, "large_text": "Triforce v3.0.0"}}, "play": {"details": "Kakariko Village - Adult", "timestamps": stamp, "assets": {"large_image": logo, "large_text": "Triforce v3.0.0 RC7"}}, diff --git a/tests/test_readback_rec_end.cpp b/tests/test_readback_rec_end.cpp index 73af725..9cb5208 100644 --- a/tests/test_readback_rec_end.cpp +++ b/tests/test_readback_rec_end.cpp @@ -28,6 +28,7 @@ struct Gpu { int rec_waits = 0, flushes = 0, slices = 0, hangs = 0; wr::Parts table; uint64_t first_noted = 0; // (value_rec: the first part, emplace) + uint64_t range = 0; // ((11-28) the part whose write-back last covered the read's range, 0: none known) void submit_up_to(size_t n) { for (; sent < n && sent < parts.size(); sent++) note(sent); @@ -45,6 +46,7 @@ struct Gpu { // the calls readback_wait makes uint64_t first_part() { return first_noted; } wr::Parts::Entry look() { return table.of(value); } + uint64_t range_seq() { return range; } int rec_wait(uint64_t seq) { rec_waits++; if (rec_wait_error_at >= 0 && rec_waits > rec_wait_error_at) return 2; @@ -76,13 +78,25 @@ struct Gpu { int main() { // the switch: on unset or empty; set, it is its number (atoi, as the renderer's other switches: a word reads as 0, off) - CHECK("unset: off (0.8.5.1)", !wr::rec_end_on(nullptr)); - CHECK("empty: off (0.8.5.1)", !wr::rec_end_on("")); + CHECK("unset: on", wr::rec_end_on(nullptr)); + CHECK("empty: on", wr::rec_end_on("")); CHECK("0: off", !wr::rec_end_on("0")); CHECK("00: off", !wr::rec_end_on("00")); CHECK("1: on", wr::rec_end_on("1")); CHECK("2: on", wr::rec_end_on("2")); CHECK("a word: off", !wr::rec_end_on("off")); + // ((Triforce, 11-28) the modes: unset, the CPU-written ranges; 0 off; 1 every readback (0.8.5); another number, 2 + CHECK("mode unset: 2", wr::rec_end_mode(nullptr) == wr::kRecEndCpuWritten); + CHECK("mode empty: 2", wr::rec_end_mode("") == wr::kRecEndCpuWritten); + CHECK("mode 0: off", wr::rec_end_mode("0") == wr::kRecEndOff); + CHECK("mode a word: off", wr::rec_end_mode("off") == wr::kRecEndOff); + CHECK("mode 1: every readback", wr::rec_end_mode("1") == wr::kRecEndAll); + CHECK("mode 2", wr::rec_end_mode("2") == wr::kRecEndCpuWritten); + CHECK("mode 7: 2", wr::rec_end_mode("7") == wr::kRecEndCpuWritten); + CHECK("mode 2, the CPU writes it: the rule", wr::rec_end_applies(wr::kRecEndCpuWritten, true)); + CHECK("mode 2, the CPU only reads it: the first part", !wr::rec_end_applies(wr::kRecEndCpuWritten, false)); + CHECK("mode 1: the rule either way", wr::rec_end_applies(wr::kRecEndAll, false) && wr::rec_end_applies(wr::kRecEndAll, true)); + CHECK("mode 0: never", !wr::rec_end_applies(wr::kRecEndOff, true)); // the parts' table: the newest part and whether it ended the recording; an older note changes nothing; a later recording // under the same value takes the entry over from its first part; nothing for no value or no number; the oldest past the cap @@ -167,6 +181,111 @@ int main() { CHECK("error: not done", r.known && !r.ok && g.rec_waits == 2); } + // ((11-28) Start next frame early: the tail began after part 11 (the scene's last), part 12 the tail. A tail that passed its + // verdict wrote nothing the game reads: the read stops at the scene's part, never waits for the tail. The table's notes) + { + wr::Parts t2; + t2.submitted(9, 10, false), t2.submitted(9, 11, false); + t2.tail_began(9); + CHECK("tail began: the scene's part noted, not passed yet", t2.of(9).scene_part == 11 && t2.of(9).scene_seq == 0); + t2.submitted(9, 12, true); + t2.tail_passed(9); + CHECK("tail passed: the scene's part is the readbacks' end", t2.of(9).scene_seq == 11 && t2.of(9).seq == 12); + t2.tail_passed(5); + CHECK("a pass for an unknown value: nothing", t2.of(5).seq == 0); + wr::Parts t3; + t3.submitted(4, 30, false), t3.tail_passed(4); + CHECK("passed with no tail begun: nothing (the last part as before)", t3.of(4).scene_seq == 0); + } + { // the tail passed before the read: one wait, for the scene's part; the tail (12) is not waited for + Gpu g; + g.parts = {10, 11, 12}; + g.sent = 3; + g.begin(); + g.table.tail_began(g.value); // (noted after part 11's submit in the replay: here the entry's newest is 12, so set it) + g.table.by_value[g.value].scene_part = 11; + g.table.tail_passed(g.value); + g.rec_done = 0; + const wr::ReadbackWait r = wr::readback_wait(true, g); + CHECK("tail passed: known and done", r.known && r.ok); + CHECK("tail passed: one wait, for the scene's part", g.rec_waits == 1 && g.rec_done == 11 && !r.went_on); + } + { // the read during the tail, before its verdict: waits for the scene's part, then stops once the tail is judged and passed + Gpu g; + g.parts = {10, 11, 12}; + g.sent = 2; + g.step = 0; // (the tail's part not submitted while the reader waits) + g.begin(); + g.table.tail_began(g.value); // (after part 11, the newest submitted) + struct Judged : Gpu { + int left = 2; + void flush() { + Gpu::flush(); + if (--left == 0) table.tail_passed(value); // (the verdict comes while the reader slices) + } + } j; + static_cast(j) = g; + const wr::ReadbackWait r = wr::readback_wait(true, j); + CHECK("judged during the wait: known and done", r.known && r.ok); + CHECK("judged during the wait: ends at the scene's part, the tail never waited for", j.rec_done == 11 && j.hangs == 0); + } + { // a tail that failed its verdict (no pass noted): the last part, as the rule was + Gpu g; + g.parts = {10, 11, 12}; + g.sent = 2; + g.begin(); + g.table.tail_began(g.value); + const wr::ReadbackWait r = wr::readback_wait(true, g); + CHECK("tail failed: the last part", r.known && r.ok && g.rec_done == 12); + } + + // ((11-28) the tail begun, its verdict not in yet (the worker threads' reads at the owner's save): the read's range was copied + // back by a scene part (10): one wait, for that part; the tail (12) never waited for, never submitted while the read waits) + { + Gpu g; + g.parts = {10, 11, 12}; + g.sent = 2; + g.step = 0; + g.begin(); + g.table.tail_began(g.value); // (after part 11) + g.range = 10; + const wr::ReadbackWait r = wr::readback_wait(true, g); + CHECK("range in a scene part: known and done", r.known && r.ok && !r.went_on); + CHECK("range in a scene part: one wait, for its part", g.rec_waits == 1 && g.rec_done == 10 && g.hangs == 0 && g.slices == 0); + } + { // the same read with no scene part known to have covered its range: the tail's end, as before (here the timeline ends it) + Gpu g; + g.parts = {10, 11, 12}; + g.sent = 2; + g.step = 1; + g.begin(); + g.table.tail_began(g.value); + const wr::ReadbackWait r = wr::readback_wait(true, g); + CHECK("range not in a scene part: the last part", r.known && r.ok && g.rec_done == 12); + } + { // a range the tail itself copied back (its part after the scene's): the last part + Gpu g; + g.parts = {10, 11, 12}; + g.sent = 2; + g.step = 1; + g.begin(); + g.table.tail_began(g.value); + g.range = 12; + const wr::ReadbackWait r = wr::readback_wait(true, g); + CHECK("range in the tail: the last part", r.known && r.ok && g.rec_done == 12); + } + { // the read during the scene (the tail not begun): as the rule was, the last part; then, once the tail begins with its + // range a scene part's, it stops at that part + Gpu g; + g.parts = {10, 11, 12}; + g.sent = 1; + g.step = 1; + g.begin(); + g.range = 10; + const wr::ReadbackWait r = wr::readback_wait(true, g); + CHECK("no tail: the last part", r.known && r.ok && g.rec_done == 12); + } + // 20,000 random recordings: 1 to 8 parts, the read after 1 to all of them are submitted, the replay going on 0 to 3 parts at // each call (0: stuck, the timeline reaching the value after some slices). The rule never ends before the last part is done // (or the timeline at the value), never waits for a part not submitted, and never takes more waits than parts plus one; the diff --git a/tests/test_readback_rec_end.py b/tests/test_readback_rec_end.py index a9d71f9..5dba05d 100644 --- a/tests/test_readback_rec_end.py +++ b/tests/test_readback_rec_end.py @@ -71,10 +71,18 @@ def check_source(): check(replay.count("rec_parts_by_value.part_submitted(") == 1 and "rec_parts_by_value.submitted(" not in replay, "vk_replay.cpp: the parts are noted somewhere past the registration") - on = function(replay, "bool rb_rec_end_on() {") - check('::mkwvk::wb_rec_wait::rec_end_on(getenv("RX_VK_RB_REC_END"))' in on and "static const bool v" in on, + on = function(replay, "int rb_rec_end_mode() {") + check('wr::rec_end_mode(getenv("RX_VK_RB_REC_END"))' in on and "static const int v" in on, "RX_VK_RB_REC_END is not read once through the header's rule") - w = function(replay, "bool readback_rec_wait(uint64_t v, int* ok) {") + # ((Triforce, 11-28) mode 2, the default: the last-part rule only where the CPU writes the range after reading it) + fr = function(replay, "bool rb_rec_end_for(uint64_t guest, uint64_t size) {") + check("m == wr::kRecEndCpuWritten && memory_rb_cpu_written(guest, size)" in fr and "return wr::rec_end_applies(m, cpu);" in fr, + "rb_rec_end_for does not ask the memory whether the CPU writes the range (mode 2) through the header's rule") + mem = (ROOT / "gpu/vk/vk_memory.cpp").read_text(encoding="utf-8") + check(mem.count("memory_rb_cpu_collected(pages, n);") == 2, "the two collections of the CPU's pages do not both note the read-back pages") + check("memory_rb_watch(guest, size);" in function(mem, "void memory_note_readback(uint64_t guest, uint64_t size) {"), + "a readback's pages are not watched for the CPU's writes") + w = function(replay, "bool readback_rec_wait(uint64_t v, uint64_t guest, uint64_t size, int* ok) {") for needle, why in ( ("if (!rec_timeline || per_queue_fences_off()) return false;", "it waits by recordings where there are none"), ("::mkwvk::wb_rec_wait::readback_wait(rb_rec_end_on(), calls);", "it does not run the header's rule by the switch"), @@ -92,15 +100,15 @@ def check_source(): "readback_rec_wait waits on the recordings' timeline before it flushes the held submits: a batched signal never comes") check("pSemaphores = &rec_timeline;" in rw and "UINT64_MAX" in rw, "the recording's wait is not the recordings' timeline, unbounded") - both = function(replay, "bool readback_recorded_wait(uint64_t v, int* ok) {") - check("return rb_rec_end_on() ? readback_rec_wait(v, ok) : wait_recorded(v, UINT64_MAX, ok);" in both, + both = function(replay, "bool readback_recorded_wait(uint64_t v, uint64_t guest, uint64_t size, int* ok) {") + check("return rb_rec_end_for(guest, size) ? readback_rec_wait(v, guest, size, ok) : wait_recorded(v, UINT64_MAX, ok);" in both, "readback_recorded_wait does not take the rule by the switch (the old wait kept for =0)") inv = function(replay, "void invalidate(uint64_t gpu, uint64_t size) {") - check(inv.count("readback_recorded_wait(v, &ok)") == 2, "invalidate's two waits by recordings do not both take the rule") - check("if (!(unforced && readback_recorded_wait(v, &ok))) {" in inv, "the submitting thread's wait does not take the rule") - check("while (completed() < v && !readback_recorded_wait(v, &ok) && async_wait_step(v)) {}" in inv, + check(inv.count("readback_recorded_wait(v, host->gpu_guest(gpu), size, &ok)") == 2, "invalidate's two waits by recordings do not both take the rule") + check("if (!(unforced && readback_recorded_wait(v, host->gpu_guest(gpu), size, &ok))) {" in inv, "the submitting thread's wait does not take the rule") + check("while (completed() < v && !readback_recorded_wait(v, host->gpu_guest(gpu), size, &ok) && async_wait_step(v)) {}" in inv, "the replay thread's wait does not take the rule") - for needle in ("readback_rec_wait(", "wait_recorded(", "rb_rec_end_on(", "rec_parts_by_value", "st_rb_went_on"): + for needle in ("readback_rec_wait(", "wait_recorded(", "rb_rec_end_on(", "rb_rec_end_for(", "memory_rb_cpu_written(", "rec_parts_by_value", "st_rb_went_on"): check(needle not in inv, "invalidate holds %s itself: its branches change invalidate's control-flow hash, and the " "renderer's play profile no longer matches it (keep them in the helpers)" % needle) check("readback_zero_line((long long)presents_now, gpu, size, f0, v, (int)behind0, (int)w);" in inv, @@ -114,7 +122,7 @@ def check_source(): "the 3000-readback line does not count the reads that waited past a part") dev = (ROOT / "docs/development.md").read_text(encoding="utf-8") - rows = [line for line in dev.splitlines() if line.startswith("| `RX_VK_RB_REC_END=1` |")] + rows = [line for line in dev.splitlines() if line.startswith("| `RX_VK_RB_REC_END=0` |")] check(len(rows) == 1, "docs/development.md: RX_VK_RB_REC_END is not in the switches table once") check("tests/test_readback_rec_end.py" in rows[0] and "wb_rec_wait.h" in rows[0], "docs/development.md: the row names neither the rule nor its test") diff --git a/tests/test_recorded_wait_flush.py b/tests/test_recorded_wait_flush.py index 0260a3d..b318941 100644 --- a/tests/test_recorded_wait_flush.py +++ b/tests/test_recorded_wait_flush.py @@ -27,16 +27,16 @@ def main(): raise SystemExit("wait_recorded waits without sending the held batch: a batched recording's signal never comes") # ((Triforce, 11-27) invalidate reaches it through readback_recorded_wait, kept out of invalidate for its renderer profile) inv = body(replay, "void invalidate(uint64_t gpu, uint64_t size) {") - rrw = body(replay, "bool readback_recorded_wait(uint64_t v, int* ok) {") - if "readback_recorded_wait(v, &ok)" not in inv or "wait_recorded(v, UINT64_MAX, ok)" not in rrw: + rrw = body(replay, "bool readback_recorded_wait(uint64_t v, uint64_t guest, uint64_t size, int* ok) {") + if "readback_recorded_wait(v, host->gpu_guest(gpu), size, &ok)" not in inv or "wait_recorded(v, UINT64_MAX, ok)" not in rrw: raise SystemExit("invalidate's readback wait no longer goes through wait_recorded: update this test") # ((Triforce, 11-27) RX_VK_RB_REC_END: the readback's wait by its own rule, readback_rec_wait, flushes the same way) - rb = body(replay, "bool readback_rec_wait(uint64_t v, int* ok) {") + rb = body(replay, "bool readback_rec_wait(uint64_t v, uint64_t guest, uint64_t size, int* ok) {") rw = rb[rb.index("int rec_wait(uint64_t seq) {"):] flush = rw.find("queue_flush_for(") if "vkWaitSemaphores(" not in rw or flush < 0 or flush > rw.index("vkWaitSemaphores("): raise SystemExit("readback_rec_wait waits without sending the held batch: a batched recording's signal never comes") - if "readback_rec_wait(v, ok)" not in rrw: + if "readback_rec_wait(v, guest, size, ok)" not in rrw: raise SystemExit("invalidate's readback wait no longer goes through readback_rec_wait: update this test") print("PASS: wait_recorded and readback_rec_wait send the held batch before they wait") diff --git a/tests/test_tail_early.py b/tests/test_tail_early.py index 594f356..ff02dda 100644 --- a/tests/test_tail_early.py +++ b/tests/test_tail_early.py @@ -81,9 +81,9 @@ MUTATIONS = [ " if (false && list_[i].gpu == gpu) return *v = list_[i].v, true;\n", "clean", "values"), ("cut", " if (why == tail_early::EARLY && !tail_words()) why = tail_early::EVENT;\n", " if (why == tail_early::EARLY) tail_words();\n", "outside", "values"), - ("cut", " memory_end_submission(cmd);\n full_barrier(cmd, \"barrier (recording end)\");\n submit_recording(*rec_cur, false, 0, true);", - " full_barrier(cmd, \"barrier (recording end)\");\n submit_recording(*rec_cur, false, 0, true);", "clean", "values"), - ("cut", " tail_words_record(r.upload);", " tail_words_record(r.cmd);", "clean", "order"), + ("cut", " memory_end_submission(cmd);\n full_barrier(cmd, \"barrier (recording end)\");\n const uint64_t scene_value = rec_cur->value;\n submit_recording(*rec_cur, false, 0, true);", + " full_barrier(cmd, \"barrier (recording end)\");\n const uint64_t scene_value = rec_cur->value;\n submit_recording(*rec_cur, false, 0, true);", "clean", "values"), + ("cut", " tail_words_record(texture_upload_last(r.upload));", " tail_words_record(r.cmd);", "clean", "order"), ("cut", " full_barrier(cmd, \"barrier (an event signal in the early tail: its word held)\");\n", "", "clean", "barrier"), ("cut", " tl.words.push_back({mirror.buffer, mirror.offset, g.v});\n", "", "clean", "values"), ("cut", " gpu_write(r.cmd, g.gpu, &g.v, 4);\n", " (void)g;\n", "written", "values"),