"""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), 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) {", "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, "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, "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) {") 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"), ("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, int* ok) {") check("return rb_rec_end_on() ? readback_rec_wait(v, 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, "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"): 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") 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` |")] 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") 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()