"""A draw that only bound uniform buffers anew resolves its user uniform buffers alone, and only where a whole build of its descriptors would have given the same (gpu/vk/uboonly.h; vk_draw.cpp ubo_only_build and draw; no game, no GPU). (Triforce, 11-26.) draw() keeps the last descriptors only while bind_gen is the same, and every uniform buffer bind moves it, so the next draw of the same program with one per-object uniform buffer ran the whole build_descriptors. Now it resolves the user uniform buffers (through kept_uniform, as the build does) and leaves the rest of the thread's last build as it is. 1. A driver compiled against the header: - the switch's values; the mode in a draw (off with the per-binding reuse off or checked, the signature skip on, snapshots, RX_VK_BANK_GEN_OFF, a dump, the upscaler's second recording, a strict draw, one that may not defer, the lab's phase); - the plan of a program (Maker): the user uniform buffers' places in the build's order, the descriptors beside them; a texture handle read through a handle program or from c3 on, a storage buffer named in c3 on, written, a table, a run-time pointer's, a turf list, the miss binding, a texel buffer, a storage image, a constant buffer past c18, compute, the mesh fallback, RenoDX's, a stage past 4: never; a plan made again for another program in its slot keeps its lists' room and is what a new plan would be; - the rule on a model of build_descriptors (the banks' cache by the stages' generations, desc_keep.h's take and keep for the rest): only uniform buffer slot 2 moved: that one alone is resolved and the rest is what a whole build makes; a texture bound between two draws: a whole build; a stamp moved, a slot registered anew, a dispatch's build in between, a build whose bind failed, a result dropped, a sampler or bank missing: a whole build; a mirror made while the uniform buffers resolve: a whole build follows; - 30000 random draws of four programs (and a fifth built by a dispatch's stand-in) with every input and stamp moved at random: each uniform-buffers-only build equals the whole build of the same state, descriptor for descriptor. This is the only place the default mode's chain (a build of the uniform buffers alone over another) is compared: in check every such draw is followed by a whole build, so check compares only the first build after a whole one. 2. Mutations of the header (each rule taken out in turn) make the driver fail. 3. The source: the hook in draw() (the rule before the build, the whole build otherwise, the stamps read before it, the bookkeeping after the bind), ubo_only_build (the program's key first, every user uniform buffer through kept_uniform, the stamps again after them, the sampled textures read again, counted only when it is used: not in check), the plan made as sig_shape reads a program, the switch read once with what turns it off and a value it does not know told, the lab's knob, the counters' line, and the switches table's row (with what check does not try). python -I -B tests/test_uboonly.py [--cxx clang++] [--no-mutations] """ import argparse import re import shutil import subprocess import tempfile from pathlib import Path ROOT = Path(__file__).resolve().parents[1] RUN_ALL_ARGS = ["--cxx", "{cxx}"] DRIVER = r''' #include "uboonly.h" #include #include #include using namespace uboonly; using desc_keep::Stamps; static int checks, fails; static void want(const char* what, bool ok) { checks++; if (!ok) fails++, printf("FAIL %s\n", what); } // ---- a model of build_descriptors and of the uniform-buffers-only build, over the real rule and desc_keep.h ---- enum ItemKind { BANK0, BANK2, C1, UBO, SSBO, SSBO_WRITTEN, TEX, TEX_FROM_C3 }; struct Item { int kind; uint32_t index; }; // (UBO: the constant buffer index; TEX: the stage's texture slot) struct MStage { int nvn; std::vector items; }; // (in the build's order: constant buffers, storage buffers, textures) struct MProg { int id; std::vector stages; Plan plan; }; static void make_plan(MProg& p) { Maker m(p.plan); m.begin(&p, false, false, false); for (const MStage& st : p.stages) { m.stage(st.nvn); for (const Item& it : st.items) switch (it.kind) { case BANK0: m.cbuf(0); break; case BANK2: m.cbuf(2); break; case C1: m.cbuf(1); break; case UBO: m.cbuf(it.index); break; case SSBO: m.storage(1, false, false, false, 0, 0x100); break; case SSBO_WRITTEN: m.storage(1, true, false, false, 0, 0x100); break; case TEX: m.texture(1, false, 2, false, 0); break; case TEX_FROM_C3: m.texture(1, false, 3, false, 0); break; } } m.end(); } struct Out { uint64_t a = 0, b = 0, c = 0; bool operator==(const Out& o) const { return a == o.a && b == o.b && c == o.c; } }; enum : uint64_t { BANKBUF = 0x1000, C1BUF = 0x2000, POOL = 0x3000, STREAM = 0x4000, SSBUF = 0x5000 }; struct World { // the stamps, the registrations and the state's bindings Stamps st; uint64_t reg = 1; uint32_t slotver[8]{}; uint64_t bind_gen = 1, tex_gen[8]{}, ssbo_gen[8]{}; uint64_t ubo[8][16]{}; // (an address; odd: one copied into the recording's stream, never kept) uint32_t tex[8][4]{}; // (handles; 0: none) uint64_t ssbo[8]{}; bool mirror_next = false; // (the next uniform buffer resolved makes a mirror: the bind stamp moves) // the recording thread's std::array kept; struct Bank { bool on = false; uint64_t gen = 0, at = 0; } bank[2][8]; uint64_t bank_at = 0, stream_at = 0; std::vector bufs, imgs; // (dw()) const void* built_for = nullptr; Last last; int ubo_resolves = 0; // draw()'s bookkeeping bool last_valid = false; const void* last_prog = nullptr; uint64_t last_bind = 0, last_ub = 0, last_reg = 0, last_destroy = 0; }; static Out ubo_out(World& w, MProg& p, size_t j, int nvn, uint32_t index) { // (kept_uniform) desc_keep::Elem& e = desc_keep::prog_of(w.kept, &p, p.plan.n).e[j]; const uint64_t addr = w.ubo[nvn][index - 3]; Stamps now; now.rec = w.st.rec, now.bind = w.st.bind, now.ub = w.st.ub; if (desc_keep::take(e, desc_keep::kUbo, addr, 64, 0, now)) return {e.out[0], e.out[1], e.out[2]}; w.ubo_resolves++; Out o; if (!addr) { desc_keep::drop(e); return o; } if (addr & 1) o = {STREAM, w.stream_at += 64, 64}, desc_keep::drop(e); else o = {POOL, addr * 16 + w.st.bind * 3 + w.st.ub * 5, 64}, desc_keep::keep(e, desc_keep::kUbo, addr, 64, 0, now, o.a, o.b, o.c); if (w.mirror_next) w.mirror_next = false, w.st.bind++; return o; } static void whole_build(World& w, MProg& p) { // (build_descriptors) w.bufs.clear(), w.imgs.clear(); w.built_for = &p; desc_keep::Prog& kp = desc_keep::prog_of(w.kept, &p, p.plan.n); size_t j = 0; for (size_t si = 0; si < p.stages.size(); si++) { const int nvn = p.stages[si].nvn; for (const Item& it : p.stages[si].items) { desc_keep::Elem& e = kp.e[j]; switch (it.kind) { case BANK0: case BANK2: { // (uniform_range's cache: by the stage's generation) World::Bank& c = w.bank[it.kind == BANK2][nvn]; const uint64_t gen = it.kind == BANK2 ? w.tex_gen[nvn] : w.ssbo_gen[nvn]; if (!c.on || c.gen != gen) c = {true, gen, ++w.bank_at}; w.bufs.push_back({BANKBUF, c.at, 16}); break; } case C1: { Stamps now; now.rec = w.st.rec; const uint64_t in = (uint64_t)p.id * 8 + si + 1; if (!desc_keep::take(e, desc_keep::kC1, in, 0, 0, now)) desc_keep::keep(e, desc_keep::kC1, in, 0, 0, now, C1BUF, w.st.rec * 100 + in, 16); w.bufs.push_back({e.out[0], e.out[1], e.out[2]}); break; } case UBO: w.bufs.push_back(ubo_out(w, p, j, nvn, it.index)); break; case SSBO: case SSBO_WRITTEN: { const uint64_t addr = w.ssbo[nvn]; Stamps now; now.rec = w.st.rec, now.bind = w.st.bind; const bool kept = it.kind == SSBO; if (kept && desc_keep::take(e, desc_keep::kSsbo, addr, 64, 0, now)) { w.bufs.push_back({e.out[0], e.out[1], e.out[2]}); break; } const Out o = addr ? Out{SSBUF, addr * 8 + w.st.bind * 1000003 + w.st.rec * 7, 64} : Out{}; if (kept && addr) desc_keep::keep(e, desc_keep::kSsbo, addr, 64, 0, now, o.a, o.b, o.c); else desc_keep::drop(e); w.bufs.push_back(o); break; } case TEX: case TEX_FROM_C3: { const uint32_t handle = it.kind == TEX ? w.tex[nvn][it.index] : (uint32_t)((w.ubo[nvn][0] >> 1) % 7); if (!handle) { // (no result kept: the dummy and its sampler each time) w.imgs.push_back({5 + w.st.samp * 1000, 1, 0}); break; } Stamps now; now.samp = w.st.samp, now.destroy = w.st.destroy, now.dummy = w.st.dummy, now.epoch = w.st.epoch; const uint32_t version = w.slotver[handle]; if (!desc_keep::take(e, desc_keep::kTex, handle, 0, version, now)) desc_keep::keep(e, desc_keep::kTex, handle, 0, version, now, handle * 31 + w.st.samp * 1000, handle * 1000003ull + version * 7 + w.st.destroy * 13 + w.st.epoch * 17 + w.st.dummy * 19, handle); w.imgs.push_back({e.out[0], e.out[1], e.out[2]}); break; } } j++; } } } static Why try_fast(World& w, MProg& p) { // (ubo_only_build, up to the uniform buffers) return decide(w.last, p.plan, &p, w.built_for, w.last_valid && w.last_prog == &p, w.tex_gen, w.ssbo_gen, [&] { return w.st; }, w.reg, [&](const Stamps& now) { if (w.bufs.size() != p.plan.nbuf || w.imgs.size() != p.plan.nimg) return false; return stands(p.plan, desc_keep::prog_of(w.kept, &p, p.plan.n).e, now, [&](uint32_t slot) { return w.slotver[slot]; }, [&](uint32_t at) { return w.bufs[at].a == 0; }, [&](uint32_t at) { return w.imgs[at].a == 0; }); }); } static long mismatches, fasts, compared; enum { REUSED = 100, MIRROR = 101 }; /// draw()'s descriptors: kept, the uniform buffers alone (checked against the whole build of the same state), or whole. /// bind_ok: bind_descriptors did not leave the draw out static int draw(World& w, MProg& p, bool bind_ok = true) { if (w.last_valid && w.last_prog == &p && w.last_bind == w.bind_gen && w.last_ub == w.st.ub && w.last_reg == w.reg && w.last_destroy == w.st.destroy) return REUSED; World pre = w; int why = try_fast(w, p); bool fast = false; if (why == kFast) { for (const Ubo& u : p.plan.ubos) w.bufs[u.buf] = ubo_out(w, p, u.j, p.stages[u.stage].nvn, u.index); fast = still(w.last, w.st); if (fast) { whole_build(pre, p); compared++; if (!(pre.bufs == w.bufs) || !(pre.imgs == w.imgs)) mismatches++; } else { why = MIRROR; } } if (!fast) { const Stamps before = w.st; whole_build(w, p); built(w.last, &p, w.tex_gen, w.ssbo_gen, before, 0, 0); } if (!bind_ok) return why; if (fast) { fasts++; w.last_bind = w.bind_gen, w.last_ub = w.st.ub; } else { w.last_valid = true, w.last_prog = &p, w.last_bind = w.bind_gen, w.last_ub = w.st.ub, w.last_reg = w.reg; w.last_destroy = w.st.destroy; bound(w.last, &p); } return why; } static void bind_ubo(World& w, int nvn, uint32_t index, uint64_t addr) { if (w.ubo[nvn][index - 3] != addr) w.bind_gen++; w.ubo[nvn][index - 3] = addr; } static void bind_tex(World& w, int nvn, uint32_t slot, uint32_t handle) { if (w.tex[nvn][slot] != handle) w.bind_gen++, w.tex_gen[nvn] = w.bind_gen; w.tex[nvn][slot] = handle; } static void bind_ssbo(World& w, int nvn, uint64_t addr) { if (w.ssbo[nvn] != addr) w.bind_gen++, w.ssbo_gen[nvn] = w.bind_gen; w.ssbo[nvn] = addr; } static void new_recording(World& w) { // (draw_begin_recording) w.st.rec++; w.last_valid = false; for (auto& row : w.bank) for (auto& c : row) c = World::Bank{}; } static void world_begin(World& w) { for (int s = 0; s < 2; s++) { for (uint32_t i = 3; i < 7; i++) w.ubo[s][i - 3] = 0x1000 * (s + 1) + 0x10 * i; for (uint32_t t = 0; t < 4; t++) w.tex[s][t] = 1 + (s + t) % 6; w.ssbo[s] = 0x9000 + s * 0x100; } } static std::vector vertex_items() { return {{BANK0, 0}, {C1, 0}, {BANK2, 0}, {UBO, 3}, {UBO, 4}, {SSBO, 0}, {TEX, 0}, {TEX, 1}}; } static std::vector fragment_items() { return {{BANK2, 0}, {UBO, 3}, {UBO, 5}, {TEX, 0}, {TEX, 1}, {TEX, 2}}; } static bool impure(void (*f)(Maker&)) { Plan plan; Maker m(plan); m.begin(&plan, false, false, false); m.stage(0); m.cbuf(3); f(m); m.end(); return !plan.pure && plan.ubos.empty() && plan.rest.empty(); } int main() { // the switch and the mode in a draw want("the switch: on unless 0, check", mode_of(nullptr) == 1 && mode_of("1") == 1 && mode_of("") == 1 && mode_of("0") == 0 && mode_of("check") == 2 && mode_of("00") == 1); Now n; want("on by default", mode_now(n) == 1); n.mode = 2; want("check stays check", mode_now(n) == 2); { Now a = n; a.mode = 0; want("off with the switch", mode_now(a) == 0); a = n; a.lab_off = true; want("off in the lab's phase", mode_now(a) == 0); a = n; a.keep_mode = 0; want("off with the per-binding reuse off", mode_now(a) == 0); a = n; a.keep_mode = 2; want("off with the per-binding reuse checked", mode_now(a) == 0); a = n; a.sig_mode = 1; want("off with the signature skip on", mode_now(a) == 0); a = n; a.snapshot = true; want("off with uniform buffer snapshots (Intel)", mode_now(a) == 0); a = n; a.bank_gen_off = true; want("off with the banks kept by bind_gen", mode_now(a) == 0); a = n; a.dump = true; want("off in a dump", mode_now(a) == 0); a = n; a.retarget = true; want("off in the upscaler's second recording", mode_now(a) == 0); a = n; a.strict = true; want("off for a strict draw", mode_now(a) == 0); a = n; a.may_defer = false; want("off for a draw that may not leave textures for later", mode_now(a) == 0); } // the plan MProg A{1, {{0, vertex_items()}, {1, fragment_items()}}, {}}; make_plan(A); const Plan& pa = A.plan; want("a plain program is taken", pa.pure && pa.prog == &A && pa.stages == 3 && pa.n == 14 && pa.nbuf == 9 && pa.nimg == 5); want("its user uniform buffers, in the build's order", pa.ubos.size() == 4 && pa.ubos[0].stage == 0 && pa.ubos[0].index == 3 && pa.ubos[0].j == 3 && pa.ubos[0].buf == 3 && pa.ubos[1].stage == 0 && pa.ubos[1].index == 4 && pa.ubos[1].j == 4 && pa.ubos[1].buf == 4 && pa.ubos[2].stage == 1 && pa.ubos[2].index == 3 && pa.ubos[2].j == 9 && pa.ubos[2].buf == 7 && pa.ubos[3].stage == 1 && pa.ubos[3].index == 5 && pa.ubos[3].j == 10 && pa.ubos[3].buf == 8); bool rest_ok = pa.rest.size() == 10; if (rest_ok) { const Rest want_rest[10] = {{desc_keep::kNone, 0, 0}, {desc_keep::kC1, 1, 1}, {desc_keep::kNone, 2, 2}, {desc_keep::kSsbo, 5, 5}, {desc_keep::kTex, 6, 0}, {desc_keep::kTex, 7, 1}, {desc_keep::kNone, 8, 6}, {desc_keep::kTex, 11, 2}, {desc_keep::kTex, 12, 3}, {desc_keep::kTex, 13, 4}}; for (int i = 0; i < 10; i++) rest_ok &= pa.rest[i].kind == want_rest[i].kind && pa.rest[i].j == want_rest[i].j && pa.rest[i].at == want_rest[i].at; } want("the descriptors beside them: banks, c1, the storage buffer, the textures, each at its place", rest_ok); want("a handle read from c3 is never taken", impure([](Maker& m) { m.texture(1, false, 3, false, 0); })); want("nor one from c4, nor a secondary word from c3", impure([](Maker& m) { m.texture(1, false, 4, false, 0); }) && impure([](Maker& m) { m.texture(1, false, 2, true, 3); })); want("a secondary word that is not read does not count", !impure([](Maker& m) { m.texture(1, false, 2, false, 7); })); want("a handle program's texture is never taken", impure([](Maker& m) { m.texture(1, true, 2, false, 0); }) && impure([](Maker& m) { m.texture(64, true, 0, false, 0); })); want("a storage buffer named in c3 on", impure([](Maker& m) { m.storage(1, false, false, false, 3, 0x40); }) && impure([](Maker& m) { m.storage(1, false, false, false, 4, 0); })); want("a storage buffer named in c0, c1 or c2 is taken", !impure([](Maker& m) { m.storage(1, false, false, false, 0, 0x100); }) && !impure([](Maker& m) { m.storage(1, false, false, false, 1, 0x20); }) && !impure([](Maker& m) { m.storage(1, false, false, false, 2, 0x20); })); want("a written storage buffer", impure([](Maker& m) { m.storage(1, true, false, false, 0, 0x100); })); want("a storage buffer table", impure([](Maker& m) { m.storage(3, false, false, false, 0, 0x880); })); want("a run-time pointer's", impure([](Maker& m) { m.storage(1, false, true, false, 0, 0x100); })); want("a turf list", impure([](Maker& m) { m.storage(1, false, false, true, 0, 0x100); })); want("the miss binding", impure([](Maker& m) { m.storage(1, false, false, false, 0, 0xff0); })); want("a texel buffer", impure([](Maker& m) { m.texel(1); })); want("a storage image", impure([](Maker& m) { m.image(1); })); want("a constant buffer past c18", impure([](Maker& m) { m.cbuf(19); }) && !impure([](Maker& m) { m.cbuf(18); })); want("a compute, task or mesh stage", impure([](Maker& m) { m.stage(5); }) && impure([](Maker& m) { m.stage(6); }) && impure([](Maker& m) { m.stage(7); }) && impure([](Maker& m) { m.stage(-1); }) && !impure([](Maker& m) { m.stage(4); })); { Plan plan; Maker m(plan); m.begin(&plan, true, false, false), m.stage(0), m.cbuf(3), m.end(); const bool compute = !plan.pure; m.begin(&plan, false, true, false), m.stage(0), m.cbuf(3), m.end(); const bool fallback = !plan.pure; m.begin(&plan, false, false, true), m.stage(0), m.cbuf(3), m.end(); const bool renodx = !plan.pure; m.begin(&plan, false, false, false), m.end(); const bool empty = !plan.pure; m.begin(&plan, false, false, false), m.stage(0), m.cbuf(3), m.end(); want("compute, the mesh fallback, RenoDX's, a program with no stage: never; and the maker begins anew", compute && fallback && renodx && empty && plan.pure && plan.ubos.size() == 1 && plan.n == 1); } { // a slot another program took over: the plan made again in place, its lists' room kept (nothing allocated), every field anew Plan plan, fresh; Maker m(plan), f(fresh); m.begin(&plan, false, false, false), m.stage(0), m.stage(1); for (uint32_t i = 3; i < 19; i++) m.cbuf(i); m.cbuf(1), m.storage(1, false, false, false, 0, 0x100), m.texture(6, false, 2, false, 0), m.end(); want("the first program's plan", plan.pure && plan.stages == 3 && plan.ubos.size() == 16 && plan.rest.size() == 8 && plan.n == 24 && plan.nbuf == 18 && plan.nimg == 6); const Ubo* const ubos = plan.ubos.data(); const Rest* const rest = plan.rest.data(); const size_t room = plan.ubos.capacity(), rest_room = plan.rest.capacity(); m.begin(&fresh, false, false, false), m.stage(2), m.cbuf(4), m.texture(1, false, 0, false, 0), m.end(); f.begin(&fresh, false, false, false), f.stage(2), f.cbuf(4), f.texture(1, false, 0, false, 0), f.end(); want("made again in place: the lists' room kept", plan.ubos.data() == ubos && plan.rest.data() == rest && plan.ubos.capacity() == room && plan.rest.capacity() == rest_room); bool same = plan.prog == fresh.prog && plan.pure == fresh.pure && plan.stages == fresh.stages && plan.n == fresh.n && plan.nbuf == fresh.nbuf && plan.nimg == fresh.nimg && plan.ubos.size() == fresh.ubos.size() && plan.rest.size() == fresh.rest.size(); for (size_t i = 0; same && i < plan.ubos.size(); i++) same = plan.ubos[i].stage == fresh.ubos[i].stage && plan.ubos[i].index == fresh.ubos[i].index && plan.ubos[i].j == fresh.ubos[i].j && plan.ubos[i].buf == fresh.ubos[i].buf; for (size_t i = 0; same && i < plan.rest.size(); i++) same = plan.rest[i].kind == fresh.rest[i].kind && plan.rest[i].j == fresh.rest[i].j && plan.rest[i].at == fresh.rest[i].at; want("made again in place: every field as a new plan's", same && fresh.pure && fresh.stages == 4 && fresh.n == 2); m.begin(&plan, false, false, false), m.stage(0), m.texel(1), m.end(); want("an impure program after it: nothing of the others left", !plan.pure && plan.ubos.empty() && plan.rest.empty() && plan.n == 1 && plan.stages == 1 && plan.nbuf == 0 && plan.nimg == 0); } // the generations and the stamps { uint64_t t0[5] = {1, 2, 3, 4, 5}, s0[5] = {6, 7, 8, 9, 10}, t1[5] = {1, 2, 3, 4, 5}, s1[5] = {6, 7, 8, 9, 10}; want("the same generations", gens_same(0x1f, t0, s0, t1, s1)); t1[1] = 99; want("a texture bound to a stage of the program", !gens_same(3, t0, s0, t1, s1) && gens_same(1, t0, s0, t1, s1)); t1[1] = 2, s1[0] = 99; want("a storage buffer bound to a stage of the program", !gens_same(3, t0, s0, t1, s1) && gens_same(2, t0, s0, t1, s1)); Stamps a, b; want("the same stamps", stamps_same(a, b)); b.ub = 7; want("the inline updates' generation is the uniform buffers' own", stamps_same(a, b)); b = a, b.rec = 1; const bool rec = !stamps_same(a, b); b = a, b.bind = 1; const bool bind = !stamps_same(a, b); b = a, b.samp = 1; const bool samp = !stamps_same(a, b); b = a, b.destroy = 1; const bool destroy = !stamps_same(a, b); b = a, b.dummy = 1; const bool dummy = !stamps_same(a, b); b = a, b.epoch = 1; const bool epoch = !stamps_same(a, b); want("every other stamp stops it", rec && bind && samp && destroy && dummy && epoch); } // the rule on the model MProg B{2, {{0, vertex_items()}, {1, fragment_items()}}, {}}; // (the same shape: another program) MProg C{3, {{0, {{BANK2, 0}, {UBO, 3}, {UBO, 4}, {TEX_FROM_C3, 0}, {TEX, 1}}}, {1, fragment_items()}}, {}}; // (a handle in c3) MProg D{4, {{0, {{BANK0, 0}, {UBO, 3}, {SSBO_WRITTEN, 0}}}, {1, fragment_items()}}, {}}; // (writes a storage buffer) MProg E{5, {{0, vertex_items()}, {1, fragment_items()}}, {}}; // (a dispatch's stand-in: built, never bound by a draw) make_plan(B), make_plan(C), make_plan(D), make_plan(E); want("the handle from c3 and the written storage buffer make their programs whole builds", !C.plan.pure && !D.plan.pure && B.plan.pure); { World w; world_begin(w); want("a program's first draw builds whole", draw(w, A) == kOther && w.bufs.size() == 9 && w.imgs.size() == 5); want("nothing bound: kept as before", draw(w, A) == REUSED); const std::vector bufs0 = w.bufs, imgs0 = w.imgs; const int r0 = w.ubo_resolves; bind_ubo(w, 1, 5, 0x7770); // (the fragment stage's slot 2) want("only uniform buffer slot 2 moved: the uniform buffers alone", draw(w, A) == kFast && mismatches == 0 && compared == 1); bool others = w.imgs == imgs0 && w.bufs.size() == bufs0.size(); for (size_t i = 0; others && i < w.bufs.size(); i++) others = i == 8 ? !(w.bufs[i] == bufs0[i]) : w.bufs[i] == bufs0[i]; want("exactly that one resolved, the rest as they were", w.ubo_resolves == r0 + 1 && others); bind_ubo(w, 0, 3, 0x7781); // (an odd address: a copy in the stream, never kept) const int r1 = w.ubo_resolves; want("a stream copy: the uniform buffers alone", draw(w, A) == kFast && w.ubo_resolves == r1 + 1 && w.bufs[3].a == STREAM); bind_ubo(w, 1, 5, 0x7790); want("and it is made again by the next such build, as a whole build does", draw(w, A) == kFast && w.ubo_resolves == r1 + 3 && mismatches == 0); bind_tex(w, 1, 2, 3); bind_ubo(w, 1, 5, 0x77a0); want("a texture bound between two draws: a whole build", draw(w, A) == kBound); bind_ubo(w, 1, 5, 0x77b0); want("then the uniform buffers alone again", draw(w, A) == kFast); bind_tex(w, 4, 0, 3); bind_ubo(w, 1, 5, 0x77c0); want("a texture bound to a stage the program has not: still alone", draw(w, A) == kFast); bind_ssbo(w, 0, 0x9990); want("a storage buffer bound: a whole build", draw(w, A) == kBound); for (int k = 0; k < 6; k++) { bind_ubo(w, 0, 4, 0x8000 + 0x10 * k); if (k == 0) w.st.bind++; if (k == 1) w.st.samp++; if (k == 2) w.st.destroy++; if (k == 3) w.st.dummy++; if (k == 4) w.st.epoch++; if (k == 5) new_recording(w); const int why = draw(w, A); want("a stamp moved: a whole build", k == 5 ? why == kOther : why == kStale); bind_ubo(w, 0, 4, 0x8008 + 0x10 * k); want("and alone again after it", draw(w, A) == kFast); } bind_ubo(w, 0, 4, 0x8100); w.st.ub++; want("an inline update: the uniform buffers alone, resolved again", draw(w, A) == kFast && mismatches == 0); bind_ubo(w, 0, 4, 0x8110); w.reg++, w.slotver[7]++; want("a slot registered that the program does not sample: alone, after a walk", draw(w, A) == kFast); bind_ubo(w, 0, 4, 0x8120); w.reg++, w.slotver[w.tex[1][0]]++; want("a slot it samples registered anew: a whole build", draw(w, A) == kStale); bind_ubo(w, 0, 4, 0x8130); want("alone again", draw(w, A) == kFast); whole_build(w, E); // (a dispatch builds its own over the draw's) bind_ubo(w, 0, 4, 0x8140); want("a dispatch's build in between: a whole build", draw(w, A) == kOther); bind_ubo(w, 0, 4, 0x8150); want("another program's draw in between: a whole build", draw(w, B) == kOther && draw(w, A) == kOther); bind_ubo(w, 0, 4, 0x8160); bind_tex(w, 0, 0, 2); want("a whole build whose bind failed is not stood on", draw(w, A, false) == kBound); bind_ubo(w, 0, 4, 0x8170); want("(the next draw builds whole)", draw(w, A) == kOther); bind_ubo(w, 0, 4, 0x8180); w.mirror_next = true; want("a mirror made while the uniform buffers resolve: the whole build follows", draw(w, A) == MIRROR); bind_ubo(w, 0, 4, 0x8190); want("then alone again", draw(w, A) == kFast); bind_tex(w, 1, 0, 6); w.mirror_next = true; want("a whole build with a mirror made under it", draw(w, A) == kBound); bind_ubo(w, 0, 4, 0x8198); want("is not stood on: its stamps moved while it resolved", draw(w, A) == kStale); bind_ubo(w, 0, 4, 0x81a0); want("then alone", draw(w, A) == kFast); bind_tex(w, 1, 1, 0); bind_ubo(w, 0, 4, 0x81b0); draw(w, A); bind_ubo(w, 0, 4, 0x81c0); want("a texture with no handle (the dummy): alone", draw(w, A) == kFast && w.imgs[3].b == 1); draw(w, C); bind_ubo(w, 0, 4, 0x81d0); want("the program with a handle in c3: whole, always", draw(w, C) == kImpure); bind_ubo(w, 0, 3, 0x81e4); want("(its texture follows the uniform buffer)", draw(w, C) == kImpure); draw(w, D); bind_ubo(w, 0, 3, 0x81f0); want("the program that writes a storage buffer: whole, always", draw(w, D) == kImpure); want("every uniform-buffers-only build was the whole build's", mismatches == 0 && compared == fasts && fasts >= 14); } { // stands, on its own: what a build dropped, and what is missing World w; world_begin(w); draw(w, A); std::vector kept = desc_keep::prog_of(w.kept, &A, pa.n).e; const auto ver = [&](uint32_t slot) { return w.slotver[slot]; }; const auto none = [](uint32_t) { return false; }; Stamps now = w.st; want("a whole build's results stand", stands(pa, kept, now, ver, none, none)); want("not for another count of descriptors", !stands(pa, std::vector(pa.n + 1), now, ver, none, none)); auto k2 = kept; desc_keep::drop(k2[5]); want("a storage buffer dropped", !stands(pa, k2, now, ver, none, none)); k2 = kept, desc_keep::drop(k2[1]); want("c1 dropped", !stands(pa, k2, now, ver, none, none)); k2 = kept, k2[5].kind = desc_keep::kUbo; want("another kind's result", !stands(pa, k2, now, ver, none, none)); k2 = kept, desc_keep::drop(k2[3]), desc_keep::drop(k2[10]); want("a uniform buffer dropped does not matter: it is asked again", stands(pa, k2, now, ver, none, none)); k2 = kept, desc_keep::drop(k2[6]); want("a texture with nothing kept and its sampler there: the dummy", stands(pa, k2, now, ver, none, none)); want("a sampler missing", !stands(pa, kept, now, ver, none, [](uint32_t at) { return at == 2; })); want("a bank not made", !stands(pa, kept, now, ver, [](uint32_t at) { return at == 6; }, none) && stands(pa, kept, now, ver, [](uint32_t at) { return at == 3 || at == 1; }, none)); Stamps later = now; later.epoch++; want("a texture kept at another epoch", !stands(pa, kept, later, ver, none, none)); later = now, later.bind++; want("a storage buffer kept at another bind stamp", !stands(pa, kept, later, ver, none, none)); later = now, later.rec++; want("c1 kept in another recording", !stands(pa, kept, later, ver, none, none)); w.slotver[w.tex[0][1]]++; want("a slot's version moved", !stands(pa, kept, now, ver, none, none)); want("never for a program that is not taken", !stands(C.plan, desc_keep::prog_of(w.kept, &C, C.plan.n).e, now, ver, none, none)); Last l; want("no build yet: no candidate", !candidate(l, &A, &A, true)); built(l, &A, w.tex_gen, w.ssbo_gen, now, 2, 1); want("built, not bound yet: no candidate", !candidate(l, &A, &A, true) && l.dummies == 2 && l.dummies_known == 1); bound(l, &B); want("another program bound: none", !candidate(l, &A, &A, true)); bound(l, &A); want("bound: a candidate", candidate(l, &A, &A, true)); want("not with another program's build in dw(), nor other bookkeeping, nor for another program", !candidate(l, &A, &E, true) && !candidate(l, &A, &A, false) && !candidate(l, &B, &B, true) && !candidate(l, nullptr, nullptr, true)); forget(l); want("forgotten: none", !candidate(l, &A, &A, true)); } { // random: every input and stamp moved, five programs World w; world_begin(w); std::mt19937 rng(20261126); MProg* progs[5] = {&A, &B, &C, &D, &E}; MProg* at = &A; const long before = fasts; long stale = 0, boundn = 0, mirrors = 0; for (int i = 0; i < 30000; i++) { const uint32_t r = rng() % 100; const int s = (int)(rng() % 2); if (r < 50) bind_ubo(w, s, 3 + rng() % 4, 0x1000 + (rng() % 64) * 2 + (rng() % 9 == 0)); else if (r < 56) bind_tex(w, s, rng() % 4, rng() % 7); else if (r < 59) bind_ssbo(w, s, 0x9000 + (rng() % 5) * 0x40); else if (r < 61) new_recording(w); else if (r < 63) w.st.bind++; else if (r < 66) w.st.ub++; else if (r < 68) w.st.samp++; else if (r < 70) w.st.destroy++; else if (r < 72) w.st.dummy++; else if (r < 74) w.st.epoch++; else if (r < 78) w.reg++, w.slotver[1 + rng() % 6]++; else if (r < 80) w.mirror_next = true; else if (r < 82) whole_build(w, E); // (a dispatch) if (rng() % 4 == 0) at = progs[rng() % 4]; const int why = draw(w, *at, rng() % 20 != 0); stale += why == kStale, boundn += why == kBound, mirrors += why == MIRROR; } want("random draws: each uniform-buffers-only build equals the whole build of the same state", mismatches == 0); want("random draws: the rule is taken, and every way out of it too", fasts - before > 1500 && stale > 100 && boundn > 100 && mirrors > 10); printf("random: %ld uniform-buffers-only builds of 30000 draws (%ld stale, %ld bound, %ld mirrors)\n", fasts - before, stale, boundn, mirrors); } // the check's buffer compare want("check: the same buffer", buffer_same(false, 5, 16, 64, false, 5, 16, 64, false, true)); want("check: another offset", !buffer_same(false, 5, 16, 64, false, 5, 32, 64, false, true) && !buffer_same(true, 5, 16, 64, false, 5, 32, 64, false, true)); want("check: a uniform buffer copied twice into the stream: its range and bytes", buffer_same(true, 5, 16, 64, true, 5, 96, 64, true, true) && !buffer_same(true, 5, 16, 64, true, 5, 96, 64, true, false) && !buffer_same(true, 5, 16, 64, true, 5, 96, 128, true, true)); want("check: not for a buffer beside them, nor a copy against a pool's range", !buffer_same(false, 5, 16, 64, true, 5, 96, 64, true, true) && !buffer_same(true, 5, 16, 64, true, 6, 96, 64, false, true) && !buffer_same(true, 0, 0, 64, true, 5, 96, 64, true, true)); if (fails) printf("uboonly: FAILED (%d of %d checks)\n", fails, checks); else printf("uboonly: all %d checks passed\n", checks); return fails != 0; } ''' # (a rule of the header, and what it is replaced by: each must make the driver fail) MUTATIONS = [ ("(tex_was[k] != tex_now[k] || ssbo_was[k] != ssbo_now[k])", "(ssbo_was[k] != ssbo_now[k])", "a texture bound to the program's stage"), ("(tex_was[k] != tex_now[k] || ssbo_was[k] != ssbo_now[k])", "(tex_was[k] != tex_now[k])", "a storage buffer bound to the program's stage"), ("return a.rec == b.rec && a.bind == b.bind && ", "return a.bind == b.bind && ", "a new recording"), ("a.rec == b.rec && a.bind == b.bind && ", "a.rec == b.rec && ", "the bind stamp"), ("a.samp == b.samp && ", "", "the samplers' generation"), ("a.destroy == b.destroy && ", "", "a texture destroyed"), ("a.dummy == b.dummy && a.epoch == b.epoch;", "a.epoch == b.epoch;", "a texture left for later"), ("a.dummy == b.dummy && a.epoch == b.epoch;", "a.dummy == b.dummy;", "the next submission's epoch"), (" if (!stamps_same(l.at, now)) return kStale;\n", "", "the stamps since the whole build"), (" if (l.walked && l.reg == reg_gen) return kFast;", " if (l.walked) return kFast;", "a slot registered anew after the walk"), (" if (k == desc_keep::kTex && e.version != slot_version((uint32_t)(e.in[0] & 0xFFFFF))) return false;\n", " (void)slot_version;\n", "a slot's version"), (" if (e.kind != k || e.at != desc_keep::stamps_of(k, now)) return false;", " if (e.at != desc_keep::stamps_of(k, now)) return false;", "a result dropped"), (" if (sampler_missing(r.at)) return false;\n", " (void)sampler_missing;\n", "a sampler missing"), (" if (buffer_missing(r.at)) return false;\n", " (void)buffer_missing;\n", "a bank not made"), ("return prog && l.prog == prog && l.bound && built_for == prog && last_same;", "return prog && l.prog == prog && l.bound && last_same;", "another build in dw()"), ("return prog && l.prog == prog && l.bound && built_for == prog && last_same;", "return prog && l.prog == prog && built_for == prog && last_same;", "a whole build that was not bound"), (" return stamps_same(l.at, after);\n}", " (void)l, (void)after;\n return true;\n}", "a mirror made while the uniform buffers resolve"), (" if (cbuf_index >= 3 || (has_secondary && secondary_cbuf_index >= 3)) ok = false;", " (void)cbuf_index, (void)has_secondary, (void)secondary_cbuf_index;", "a handle read from c3 on"), (" if (handle_program) ok = false;", " (void)handle_program;", "a handle program"), (" if (cbuf_index >= 3) ok = false;", "", "a storage buffer named in c3 on"), (" if (count != 1 || written || native_bounds", " (void)written;\n if (count != 1 || native_bounds", "a written storage buffer"), (" if (nvn_stage < 0 || nvn_stage >= kStages) ok = false;", " if (nvn_stage < 0 || nvn_stage >= 8) ok = false;", "a task or mesh stage"), (" ok = !compute && !fallback && !renodx;", " ok = !compute && !renodx; (void)fallback;", "the mesh fallback"), (" if (n.dump || n.retarget || n.strict || !n.may_defer) return 0;", " if (n.dump || n.retarget || n.strict) return 0;", "a draw that may not leave textures for later"), (" if (n.keep_mode != 1 || n.sig_mode != 0 || n.snapshot || n.bank_gen_off) return 0;", " if (n.keep_mode != 1 || n.sig_mode != 0 || n.bank_gen_off) return 0;", "uniform buffer snapshots (Intel)"), (" p.ubos.clear(), p.rest.clear(); // (their room kept: a slot another program takes over allocates nothing)\n" " p.prog = prog, p.pure = false, p.stages = 0, p.n = p.nbuf = p.nimg = 0;\n", " p = Plan{};\n p.prog = prog;\n", "the lists' room kept when another program takes the slot"), ("p.prog = prog, p.pure = false, p.stages = 0, p.n", "p.prog = prog, p.pure = false, p.n", "a plan's stages made anew"), ] def block(text, marker): start = text.index(marker) at = text.index("{", start) + 1 depth = 1 while depth: depth += (text[at] == "{") - (text[at] == "}") at += 1 return text[start:at] def need(ok, why): if not ok: raise SystemExit("FAIL: " + why) def ordered(text, parts, why): last = 0 for p in parts: k = text.find(p, last) need(k >= 0, why + " (missing, or out of order: " + p + ")") last = k + len(p) def check_source(): draw_cpp = (ROOT / "gpu/vk/vk_draw.cpp").read_text(encoding="utf-8") need('#include "uboonly.h"' in draw_cpp, "vk_draw.cpp does not include uboonly.h") mode = block(draw_cpp, "int ubo_only_mode() {") ordered(mode, ["static const int mode = [] {", 'const char* const value = getenv("RX_VK_DESC_UBO_ONLY");', "n.mode = uboonly::mode_of(value);", 'logf("vk: RX_VK_DESC_UBO_ONLY=%s is not 0, 1 or check: on", value);', "n.keep_mode = desc_keep_mode(), n.sig_mode = desc_sig_mode(), n.snapshot = ub_snapshot_wanted();", 'n.bank_gen_off = getenv("RX_VK_BANK_GEN_OFF") != nullptr;', "const int m = uboonly::mode_now(n);", "if (m != 1)", 'logf("vk: uniform-buffer-only descriptor builds %s%s%s"', "return m;"], "the switch is not RX_VK_DESC_UBO_ONLY read once, off with what the rule does not hold under, and said when not on") now = block(draw_cpp, "int ubo_only_now(bool strict) {") ordered(now, ["n.mode = ubo_only_mode();", "if (!n.mode) return 0;", 'n.lab_off = lab_ab::on("nouboonly");', "n.dump = dump_on(), n.retarget = pre_retarget_on, n.strict = strict, n.may_defer = draw_may_defer;", "return uboonly::mode_now(n);"], "the mode in a draw does not read the lab's knob, the dump, the upscaler's second recording and the draw's deferral") plan = block(draw_cpp, "const uboonly::Plan& ubo_only_plan(const Program& p) {") ordered(plan, ["if (plan.prog == &p) return plan;", "if (!p.ready.load(std::memory_order_acquire)) {", "m.begin(&p, p.compute, p.fallback, p.renodx != 0);", "for (const Stage& st : p.stages) {", "m.stage(st.nvn_stage);", "for (const auto& d : info.constant_buffer_descriptors)", "for (uint32_t k = 0; k < d.count; k++) m.cbuf(d.index + k);", "for (const auto& d : info.storage_buffers_descriptors)", "m.storage(d.count, d.is_written, info.native_global_bounds, Shader::IsSimTurfStorage(d), d.cbuf_index, d.cbuf_offset);", "for (const auto& d : info.texture_buffer_descriptors) m.texel(d.count);", "for (const auto& d : info.image_buffer_descriptors) m.texel(d.count);", "for (const auto& d : info.texture_descriptors)", "m.texture(d.count, d.handle_program != ~0U, d.cbuf_index, d.has_secondary, d.secondary_cbuf_index);", "for (const auto& d : info.image_descriptors) m.image(d.count);", "m.end();"], "the plan is not made from the program's lists in build_descriptors' order") # (the build's own order, which the plan's places stand on: constant buffers, storage buffers, texel buffers, textures, images) build = block(draw_cpp, "void build_descriptors(") ordered(build, ["for (const auto& d : info.constant_buffer_descriptors) {", "for (const auto& d : info.storage_buffers_descriptors) {", "texel_views(info.texture_buffer_descriptors", "texel_views(info.image_buffer_descriptors", "for (const auto& d : info.texture_descriptors) {", "for (const auto& d : info.image_descriptors) {"], "build_descriptors walks a stage's lists in another order than the plan's") need("ub_prog_key = p.key;" in build and "dwr.prog = &p;" in build, "build_descriptors no longer sets the program's key and the writer's program") stamps = block(draw_cpp, "desc_keep::Stamps ubo_only_stamps() {") for part in ["now.rec = desc_rec", "now.bind = gpu_bind_stamp();", "now.samp = sampler_registrations_generation()", "now.destroy = texture_destroy_generation();", "now.dummy = texture_dummy_generation()", "now.epoch = texture_epoch();"]: need(part in stamps, "the stamps read for the rule lack " + part) fn = block(draw_cpp, "bool ubo_only_build(") lines = [line for line in fn.split("\n")[1:] if line.strip()] need(lines[0].strip().startswith("auto& dwr = dw();") and len(re.findall(r"(?(t)->gpu_written) sampled.push_back(static_cast(t));", "if (check) {", "uo_check_armed = true;", "return false;", "count_add(st_uo_fast, 1), count_add(st_uo_ubos, (int64_t)plan.ubos.size());", "return true;"], "ubo_only_build: the rule, the key, every user uniform buffer through kept_uniform, the stamps again, the sampled textures") need("build_descriptors(" not in fn and "uniform_range(" not in fn and "texture_view_of(" not in fn, "ubo_only_build resolves something itself") built = block(draw_cpp, "void ubo_only_built(") ordered(built, ["if (uo) uboonly::built(uo_last, &p, s.tex_gen, s.ssbo_gen, before, dummies, dummies_known);", "else uboonly::forget(uo_last);", "if (uo_check_armed) ubo_only_compare(p);"], "a whole build is not what the next draws' rule stands on, or is with the rule off") cmp_fn = block(draw_cpp, "void ubo_only_compare(") for part in ["count_add(st_uo_checked, 1);", "uboonly::buffer_same(", "c.imgs[i].sampler != dwr.imgs[i].sampler || c.imgs[i].imageView != dwr.imgs[i].imageView", "c.sampled != draw_sampled()", "count_add(st_uo_bad, 1);", '"vk: uniform-buffer-only build check:']: need(part in cmp_fn, "the check does not compare or count: " + part) draw = block(draw_cpp, "void draw(VkCommandBuffer") need(len(re.findall(r"(? st_uo_fast\{0\}", draw_cpp), "the counters are not relaxed atomics of the file") header = (ROOT / "gpu/vk/uboonly.h").read_text(encoding="utf-8") need("RX_VK_DESC_UBO_ONLY" in header and '"nouboonly"' in header and "(Triforce, 11-26)" in header, "the header's comment lacks the switch or the knob") docs = (ROOT / "docs/development.md").read_text(encoding="utf-8") row = [line for line in docs.splitlines() if line.startswith("| `RX_VK_DESC_UBO_ONLY=0` or `check` |")] need(len(row) == 1 and "`gpu/vk/uboonly.h`" in row[0] and "`nouboonly`" in row[0] and "a frame's uniform-buffer-only builds" in row[0], "docs/development.md: the switches table has no RX_VK_DESC_UBO_ONLY row with the header, the knob and the line") lines = docs.splitlines() need(lines[lines.index(row[0]) - 1].startswith("| `RX_VK_DESC_SIG=0` or `check` |"), "the row is not beside the descriptor switches") for part, why in [("never over a build of the uniform buffers alone", "what check does not try (the default mode's chain)"), ("`tests/test_uboonly.py` alone", "that the chain is tried by the test alone"), ("resolves its user uniform buffers twice", "that check resolves the uniform buffers twice"), ("`RX_VK_DESC_SIG=1` or `check` (the signature skip, off by default)", "the signature skip's values that turn the rule off"), ("only stages 0 to 4 (no task or mesh stage) and set programs as well as push ones", "the stages and programs the rule takes"), ("nor the miss binding", "the miss binding"), ("any other integrated GPU whose pools have no VRAM mirror", "where uniform buffers are snapshots besides Intel"), ("only their uniform buffers count as resolved or taken", "what such builds add to the descriptors' line"), ("Any other value: on (told once)", "a value it does not know")]: need(part in row[0], "docs/development.md: the RX_VK_DESC_UBO_ONLY row does not say " + why) def compile_run(cxx, include, cpp, exe, quiet): # (a mutation may leave a parameter unread: only that is let through) r = subprocess.run([cxx, "-std=c++20", "-O1", "-Wall", "-Wextra", "-Werror"] + (["-Wno-unused-parameter"] if quiet else []) + ["-I" + str(include), str(cpp), "-o", str(exe)], capture_output=quiet, text=True) if r.returncode: return None, r.stderr or "" try: r = subprocess.run([str(exe)], capture_output=True, text=True, timeout=300) except subprocess.TimeoutExpired: return 1, "timed out" return r.returncode, r.stdout def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument("--cxx", default="clang++") parser.add_argument("--no-mutations", action="store_true", help="the driver and the source only") args = parser.parse_args() with tempfile.TemporaryDirectory(prefix="uboonly-") as tmp: tmp = Path(tmp) cpp = tmp / "uboonly_test.cpp" cpp.write_text(DRIVER, encoding="utf-8") rc, out = compile_run(args.cxx, ROOT / "gpu/vk", cpp, tmp / "uboonly.exe", False) if rc is None: raise SystemExit("FAIL: the driver does not compile against gpu/vk/uboonly.h") print(out.rstrip()) if rc: raise SystemExit("FAIL: the uniform-buffers-only build's checks failed") survived = [] if not args.no_mutations: header = (ROOT / "gpu/vk/uboonly.h").read_text(encoding="utf-8") for i, (old, new, what) in enumerate(MUTATIONS): need(header.count(old) == 1, "the mutation's text is not in the header once: %r" % old) inc = tmp / ("m%d" % i) inc.mkdir() shutil.copy(ROOT / "gpu/vk/desc_keep.h", inc / "desc_keep.h") (inc / "uboonly.h").write_text(header.replace(old, new), encoding="utf-8") rc, out = compile_run(args.cxx, inc, cpp, tmp / ("m%d.exe" % i), True) if rc is None: raise SystemExit("FAIL: the mutation does not compile (%s):\n%s" % (what, out[-2000:])) if rc == 0: survived.append(what) if survived: raise SystemExit("FAIL: no check fails without: " + "; ".join(survived)) check_source() print("PASS: a draw that only bound uniform buffers anew resolves those alone where a whole build would give the same (%d mutations " "caught); RX_VK_DESC_UBO_ONLY=0 builds whole as before" % (0 if args.no_mutations else len(MUTATIONS))) if __name__ == "__main__": main()