"""(Triforce, 11-18) A draw may leave a texture it samples for the next frame (the new-texture budget) only when every target it writes was written in the frame before too and its program writes no memory (gpu/vk/redrawn.h; vk_draw.cpp draw_may_defer). The game composes each text box once into a target of its own and keeps it: a compose in a frame over the budget kept the dummy for the whole session (empty title buttons, option values, dialogue boxes). The rule's cases: a scene target drawn every frame is redrawn before its pass's mark and after it (prev_written); a target composed for the first time is not, for its first draw nor its second in that frame (begin_pass marks it written before the second draw builds its descriptors); one composed again 300 frames later is not; a two-frame gap is not; contents begun anew (released, or never the GPU's) have no frame before. And the source keeps every gate: kept_texture's look and its kept dummy, texture_view_of's handle cache, the whole-draw reuse and the signature hit. python -I -B tests/test_redrawn.py """ import re import shutil import subprocess import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[1] VK = ROOT / "gpu" / "vk" FIXTURE = r""" #include "redrawn.h" #include static int fails; #define CHECK(c) do { if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); fails++; } } while (0) struct T { uint64_t prev = 0, last = 0; bool gpu = false, released = false; }; static void write(T& t, uint64_t f) { // (as texture_written: prev before last, read before gpu/released are set) t.prev = redrawn::prev_on_write(t.prev, t.last, t.gpu && !t.released, f); t.last = f, t.gpu = true, t.released = false; } static bool redrawn_now(const T& t, uint64_t f) { return redrawn::drawn_before(t.prev, t.last, t.gpu && !t.released, f); } int main() { { // (a scene target, drawn every frame from frame 10 on) T s; for (uint64_t f = 10; f < 20; f++) write(s, f); CHECK(redrawn_now(s, 20)); // (frame 20, its first draw: before begin_pass marks it) write(s, 20); CHECK(redrawn_now(s, 20)); // (its second draw in frame 20: after the mark) write(s, 20); CHECK(redrawn_now(s, 20)); CHECK(redrawn_now(s, 21)); } { // (a text box composed for the first time at frame 50) T b; CHECK(!redrawn_now(b, 50)); // (its first draw) write(b, 50); CHECK(!redrawn_now(b, 50)); // (its second draw: marked written already, but not in the frame before) write(b, 50); CHECK(!redrawn_now(b, 50)); CHECK(redrawn_now(b, 51)); // (were it drawn again in the next frame, that frame's draws could leave textures) // (composed again 300 frames later) CHECK(!redrawn_now(b, 350)); write(b, 350); CHECK(!redrawn_now(b, 350)); } { // (a two-frame gap) T g; write(g, 100); CHECK(!redrawn_now(g, 102)); write(g, 102); CHECK(!redrawn_now(g, 102)); } { // (released after its last write: its contents begin anew) T r; write(r, 200); r.released = true; CHECK(!redrawn_now(r, 201)); write(r, 201); CHECK(!redrawn_now(r, 201)); write(r, 202); CHECK(redrawn_now(r, 202) && redrawn_now(r, 203)); } if (!fails) std::printf("PASS\n"); return fails != 0; } """ class RedrawnTests(unittest.TestCase): def test_rule(self): compiler = shutil.which("g++") or shutil.which("clang++") if not compiler: self.skipTest("no C++ compiler") with tempfile.TemporaryDirectory(prefix="triforce-redrawn-") as tmp: tmp = Path(tmp) (tmp / "t.cpp").write_text(FIXTURE, encoding="utf-8") exe = tmp / "t.exe" build = subprocess.run([compiler, "-std=c++17", "-Wall", "-O1", "-I", str(VK), str(tmp / "t.cpp"), "-o", str(exe)], text=True, capture_output=True) self.assertEqual(build.returncode, 0, build.stderr) run = subprocess.run([str(exe)], text=True, capture_output=True, timeout=30) self.assertEqual(run.returncode, 0, run.stdout + run.stderr) def test_gates_in_the_source(self): draw = (VK / "vk_draw.cpp").read_text(encoding="utf-8") tex = (VK / "vk_texture.cpp").read_text(encoding="utf-8") # (prev_written kept at every write, before last_written moves) self.assertRegex(tex, r"tex->prev_written = redrawn::prev_on_write\(tex->prev_written, tex->last_written, " r"tex->gpu_written && !tex->released, frame_no\);\n tex->last_written = frame_no;") # (the draw's decision: every colour target and the depth target redrawn, no memory written) self.assertIn("bool may = !program_writes_memory(p);", draw) self.assertIn("may = texture_redrawn(at.color[i]);", draw) self.assertIn("may = texture_redrawn(at.depth);", draw) # (the one gate every deferral path checks, set from it) self.assertIn("upload_defer_ok = !compute && draw_may_defer;", draw) self.assertEqual(len(re.findall(r"upload_defer_ok = !compute", draw)), 1) # (no kept dummy, nor a kept texture owed its bytes, for a draw that may not defer) self.assertIn("if (took && !compute && !draw_may_defer && (!e->out[1] || texture_owes(", draw) # (the handle cache's dummy only for a look that may defer; an owed texture looked at again by one that may not) self.assertIn("(it->second.tex_epoch == texture_dummy_generation() && upload_defer_ok)", draw) self.assertIn("if (it->second.tex && !upload_defer_ok && texture_owes(it->second.tex)) {", draw) # (no whole-draw reuse nor signature hit for such a draw in a frame that left textures) self.assertIn("const bool strict = !draw_may_defer && texture_frame_left_any();", draw) self.assertIn("const bool same_gens = !no_reuse && !strict &&", draw) self.assertIn("const int sig_mode = !same_gens && !no_reuse && !strict ? desc_sig_mode() : 0;", draw) # (the decision comes before the reuse checks read it) self.assertLess(draw.index("draw_may_defer = may;"), draw.index("const bool same_gens = !no_reuse")) # (every deferral path still checks the gate) for fn in ("bool new_texture_later(const Texture&) {", "bool new_texture_waits() {", "bool create_later(bool for_sampling) {", "bool upload_again_later(const Texture& tex) {", "bool upload_defer(const Texture& tex) {"): at = tex.index(fn) self.assertIn("upload_defer_ok", tex[at:at + 600], fn) if __name__ == "__main__": raise SystemExit(unittest.main(verbosity=1))