Files
Triforce-Source/tests/test_readback_rec_end.py
ruckus7382sfgsgdf 822ee913bc Triforce v0.9.0
Character tab, faster renderer defaults, HD rumble shipped, and the 11-30 bug pass: ending softlock below 60 fps, adult first-night lock, Intel Arc robust buffers, adult map, randomizer load order.
2026-11-30 20:55:14 -06:00

196 lines
15 KiB
Python

"""A readback waits for the LAST part of the recording that writes it back (gpu/vk/wb_rec_wait.h readback_wait, vk_replay.cpp
readback_rec_wait and invalidate; RX_VK_RB_REC_END). No game, no GPU.
The owner's random brightness changes (11-27): in a still view the picture flashed brighter for a frame and stepped down, a few
times a minute, on v0.8.4 as on the GPU pass. Each flash came one present after the game read its exposure meter back as all
zeros after a wait for its write-back, with the GPU's done value still below the write-back's value. A recording made in parts
writes back at each part's end, all under the recording's one value; the readback's wait looked that value up in value_rec, which
keeps the FIRST part. A read between the first part's submit and the last one's came back with the CPU's 0, the GPU's sum still
to come in a later part.
Compiles and runs the header's rule against a made-up GPU (tests/test_readback_rec_end.cpp: the parts' table, the meter's case,
an ended recording, one part, not known, a stuck replay ended by the timeline, an error, and 20,000 random recordings: never back
before the last part is done, never a wait for a part not submitted; the old wait ends at the first part at every read that came
before the recording's end; (11-29) RX_VK_RB_REC_END=3's wait for a range the CPU only reads, readback_range_wait: at the part
that copied the range back, never before it, past it only by the timeline, the last part when none did; 20,000 random recordings
again), and reads the source for what needs the game:
- submit_recording's registration notes every submit, part or end, by the recording's own value, under rec_map_mu;
- readback_rec_wait runs the header's rule by RX_VK_RB_REC_END, looks the tables up under rec_map_mu, flushes the held submits
before it waits on the recordings' timeline (a batched recording's signal goes out only with the batch), and slices the
submissions' timeline 1 ms at a time;
- invalidate's two waits by recordings (the submitting thread's and the replay thread's) take the rule, the old wait kept for =0;
- the zero diagnostic says the writer's recording's parts, the 3000-readback line counts the reads that waited past a part, and
the switch is in the development guide;
- invalidate and submit_recording's registration gain calls only, no if, ?: or !: the renderer's play profile matches a function
by a hash of its control flow, and the first form of this change left both at plain -O2 (invalidate is called ~460,000 times
in a benchmark run). The branches are in readback_recorded_wait, readback_zero_line, readback_went_on_line and Parts.
python -I -B tests/test_readback_rec_end.py [--cxx g++]
"""
import argparse
import subprocess
import tempfile
from pathlib import Path
RUN_ALL_ARGS = ["--cxx", "{cxx}"]
ROOT = Path(__file__).resolve().parents[1]
def check(ok, what):
if not ok:
raise SystemExit("FAIL: " + what)
def function(text, head):
at = text.index(head)
return text[at:text.index("\n}\n", at)]
def in_order(text, needles, where):
at = 0
for n in needles:
i = text.find(n, at)
check(i >= 0, "%s: %r missing or out of order" % (where, n))
at = i + len(n)
def check_source():
head = (ROOT / "gpu/vk/wb_rec_wait.h").read_text(encoding="utf-8")
for needle in ("struct Parts {", "inline bool rec_end_on(const char* text)", "struct ReadbackWait {",
"ReadbackWait readback_wait(bool rec_end, D& d) {", "ReadbackWait readback_range_wait(D& d) {",
"inline bool range_applies(int mode, bool cpu_written) { return mode == kRecEndRange && !cpu_written; }",
"void part_submitted(uint64_t value, uint64_t seq, bool part) { submitted(value, seq, !part); }"):
check(needle in head, "gpu/vk/wb_rec_wait.h: %s is missing" % needle)
replay = (ROOT / "gpu/vk/vk_replay.cpp").read_text(encoding="utf-8")
check("::mkwvk::wb_rec_wait::Parts rec_parts_by_value;" in replay, "vk_replay.cpp: the recordings' parts table is missing")
sub = function(replay, "void submit_recording(")
reg = sub[sub.index("const auto register_recording = [&] {"):]
reg = reg[:reg.index("\n };\n")]
# (a call with part as it is: a `!` here would change the registration's control-flow hash and lose its profile)
in_order(reg, ["std::lock_guard mk(rec_map_mu);", "rec_parts_by_value.part_submitted(r.value, seq, part);"],
"submit_recording's registration (every part and the end, by the recording's value, under the lock)")
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, "int rb_rec_end_mode() {")
check('const char* e = 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")
# ((Triforce, 11-29, the owner's decision) unset: 3 where the parts end at the game's render passes (their first part is
# ~32 draws, and mode 2 read a later part's result a frame late: the brightness pulses), else 2 as before
check("const int m = wr::rec_end_mode(e, parts ? wr::kRecEndRange : wr::kRecEndCpuWritten);" in on
and "const bool parts = pass_end_parts_on();" in on,
"RX_VK_RB_REC_END's default is not 3 with the pass-end parts and 2 with counted parts")
ends = function(replay, "PassEndParts& pass_end_parts() {")
check("rb_rec_end_mode(" not in ends and "rb_rec_end_on(" not in ends,
"pass_end_parts asks the readback mode's reader, which asks pass_end_parts: the two statics would wait on each other")
check('rec_end_mode(getenv("RX_VK_RB_REC_END"), ::mkwvk::wb_rec_wait::kRecEndRange)' in ends
and 'num("RX_VK_PART_RB", range ? 32 : 0)' in ends,
"RX_VK_PART_RB's default does not follow mode 3 (32 there, off with another mode set)")
# ((Triforce, 11-28) mode 2, the default: the last-part rule only where the CPU writes the range after reading it)
# ((11-29) mode 3: the same, and a range the CPU only reads at the part that copied it back, by the header's range_applies)
fr = function(replay, "bool rb_rec_end_for(uint64_t guest, uint64_t size, bool* range) {")
check("(m == wr::kRecEndCpuWritten || m == wr::kRecEndRange) && 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 (modes 2 and 3) through the header's rule")
check("*range = wr::range_applies(m, cpu);" in fr, "rb_rec_end_for does not say a read-only range's wait (mode 3) by 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, bool range) {")
for needle, why in (
("if (!rec_timeline || per_queue_fences_off()) return false;", "it waits by recordings where there are none"),
("range ? ::mkwvk::wb_rec_wait::readback_range_wait(calls) : ::mkwvk::wb_rec_wait::readback_wait(rb_rec_end_on(), calls);",
"it does not run the header's rule by the switch, or a read-only range's wait (mode 3) by the header's"),
("uint64_t range_seq() { return memory_wb_part_seq(guest, size, v); }",
"the range's part is not the memory's part that copied the range back under v"),
("st_rb_range.fetch_add(1, std::memory_order_relaxed);", "the read-only ranges' waits (mode 3) are not counted"),
("return rec_parts_by_value.of(v);", "it does not look the parts up"),
("const auto it = value_rec.find(v);", "the old lookup is not value_rec's"),
("void flush() { queue_flush_for(FR_WAIT); }", "its looks do not flush the held submits"),
("const VkResult res = vkWaitSemaphores(dev.device, &wi, ::mkwvk::wb_rec_wait::kSliceNs);",
"the slices are not 1 ms waits on the submissions' timeline"),
("st_rb_went_on.fetch_add(1, std::memory_order_relaxed)", "the reads that waited past a part are not counted")):
check(needle in w, "vk_replay.cpp readback_rec_wait: " + why)
check(w.count("std::lock_guard mk(rec_map_mu);") == 2, "readback_rec_wait reads a table without rec_map_mu")
rw = w[w.index("int rec_wait(uint64_t seq) {"):]
rw = rw[:rw.index("\n }\n")]
check(0 <= rw.index("queue_flush_for(FR_WAIT);") < rw.index("vkWaitSemaphores("),
"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, uint64_t guest, uint64_t size, int* ok) {")
in_order(both, ["bool range = false;",
"if (rb_rec_end_for(guest, size, &range)) return readback_rec_wait(v, guest, size, ok, false);",
"return range ? readback_rec_wait(v, guest, size, ok, true) : wait_recorded(v, UINT64_MAX, ok);"],
"readback_recorded_wait (the rule by the switch, a read-only range at its part for =3, the old wait kept for =0 and 2)")
inv = function(replay, "void invalidate(uint64_t gpu, uint64_t size) {")
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(", "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,
"the zero diagnostic is not readback_zero_line")
check("readback_went_on_line();" in inv[inv.index("if (++calls % 3000 == 0) {"):], "the 3000-readback line does not call readback_went_on_line")
zl = function(replay, "void readback_zero_line(")
check("its recording's first part %llu, newest %llu%s" in zl and "e = rec_parts_by_value.of(v);" in zl
and "std::lock_guard mk(rec_map_mu);" in zl, "the zero diagnostic does not say the writer's recording's parts")
gl = function(replay, "void readback_went_on_line() {")
check("st_rb_went_on.exchange(0)" in gl and "(RX_VK_RB_REC_END=0: the first part, as before)" in gl,
"the 3000-readback line does not count the reads that waited past a part")
check("st_rb_range.exchange(0)" in gl and "st_rb_range_later.exchange(0)" in gl,
"the 3000-readback line does not count the read-only ranges' waits (mode 3) and those past the first 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=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")
check("`=3`" in rows[0], "docs/development.md: the RX_VK_RB_REC_END row does not say mode 3")
# ((Triforce, 11-29) RX_VK_LATE_PART: the queue's late-page wait at the part that wrote the pages back last, the value's
# wait as before whenever that part is not known; off unless set (or the lab knob latepart))
flush = function(replay, "void late_fix_flush_locked() {")
in_order(flush, ["const uint64_t v = memory_late_fix_pending();", "if (completed() < v && !late_fix_part_wait()) {",
"vk_timeline_wait_sent(v)", "memory_late_fix_apply(UINT64_MAX);"],
"late_fix_flush_locked (the part's wait first, the value's when it was not made, then the fixes)")
pw = function(replay, "bool late_fix_part_wait() {")
in_order(pw, ["if (!rec_timeline || !memory_late_part_on()) return false;", "const uint64_t seq = memory_late_fix_part();",
"if (!seq) {", "wi.pSemaphores = &rec_timeline, wi.pValues = &seq;", "return done;"],
"late_fix_part_wait (off, no timeline or no part known: the value's wait)")
mem_on = function(mem, "bool memory_late_part_on() {")
check('getenv("RX_VK_LATE_PART")' in mem_on and 'lab_ab::on("latepart")' in mem_on and "atoi(e) != 0" in mem_on,
"RX_VK_LATE_PART is not off unless set, with the lab knob latepart")
lp = function(mem, "uint64_t late_pages_part(const std::vector<std::pair<uint32_t, uint32_t>>& slots, uint64_t done) {")
in_order(lp, ["std::lock_guard lk(wb_pages_mu);", "if (wb_runs_lost > done) return 0;", "if (r.value <= done",
"if (b == wb_batch_seq.end()) return 0;", "part = std::max(part, b->second);"],
"late_pages_part (a run cut or a part not submitted: not known)")
fp = function(mem, "uint64_t memory_late_fix_part() {")
check("if (!f.part) return 0;" in fp, "memory_late_fix_part does not give 0 when one pending fix's part is not known")
check("if (memory_late_part_on()) f.part = late_pages_part(f.slots, vk_timeline_completed());" in mem,
"the take does not note a put-off fix's part")
lrows = [line for line in dev.splitlines() if line.startswith("| `RX_VK_LATE_PART=1` |")]
check(len(lrows) == 1 and "off unless set" in lrows[0] and "tests/test_readback_rec_end.py" in lrows[0],
"docs/development.md: no RX_VK_LATE_PART row naming its default and its test")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--cxx", default="g++")
args = parser.parse_args()
check_source()
with tempfile.TemporaryDirectory(prefix="readback-rec-end-") as tmp:
exe = Path(tmp) / "readback-rec-end"
subprocess.run([args.cxx, "-std=c++20", "-Wall", "-Wextra", "-Werror",
str(ROOT / "tests/test_readback_rec_end.cpp"), "-o", str(exe)], check=True)
subprocess.run([str(exe)], check=True)
print("PASS: a readback waits for the last part of the recording that writes it back")
if __name__ == "__main__":
main()