// (Triforce, 11-26) The uniform-buffers-only build of a draw's descriptors (vk_draw.cpp ubo_only_build and draw), apart from Vulkan // so that tests/test_uboonly.py can run it. The rule is this renderer's own, on desc_keep.h's. // // A draw keeps the descriptors of the draw before it only while nothing at all was bound anew (vk_draw.cpp same_gens: bind_gen), // and every changed uniform buffer bind moves bind_gen (vk_replay.cpp NOP_BIND_UBO). So a draw of the same program that bound one // per-object uniform buffer of its own ran the whole build_descriptors: every texture's handle read and looked for among the kept // results, every storage buffer's three words read, the banks' cache asked, the writes made again, about 2,100 builds a frame at // Lake Hylia, 1,600 to 1,700 of them pushing 1.5 descriptors each ("a frame's descriptors"), on the thread the frame waits for // (the measured map: build_descriptors 23% of the Presentation Thread's samples there). The program pick has had this split since // 0.6 (vk_shader.cpp: kept by its stages' tex_gen), and so have the banks (uniform_range: tex_gen, ssbo_gen). // // Now such a draw resolves its user uniform buffers alone (c3 to c18, each through kept_uniform as a whole build does, so a stream // copy is made again and a kept range is taken) and writes them over the thread's last build (dw()), which is otherwise left as it // is. That is done only where a whole build would have taken every other descriptor from what was kept, so that the two give the // same writes: // - the program is one whose other descriptors stand on nothing a uniform buffer bind moves (Maker, made once a program a // thread, and again after another program took its slot): the kinds of descriptor vk_draw.cpp sig_shape keeps (not compute, // the mesh fallback or RenoDX's; no texel buffer, no storage image; storage buffers single, read-only, not a simulated turf // list, not a run-time pointer's, not the miss binding; no constant buffer past c18), but only stages 0 to 4, whose banks are // kept by their stage's tex_gen and ssbo_gen (sig_shape takes task and mesh stages too), and set programs as well as push // ones (the signature skip reads push programs only); and besides: no texture handle read through a handle program or from a // word of c3 on, no storage buffer's address read from a word of c3 on (those are words of the very buffers that were bound // anew); // - the thread's last build was this program's, a whole one or one of these after it, and it was bound (Last); // - no texture, sampler, image or storage buffer was bound to one of the program's stages since (gens_same): then every handle // and every storage buffer's address and size are the same, and the banks' cache answers as it did; // - desc_keep.h's stamps are what they were before that whole build began (stamps_same: the recording, the bind stamp, the // samplers', the destroy and dummy generations, the texture epoch; not ub_generation, which only the uniform buffers stand on, // and those are asked each time); // - every descriptor left as it is has a kept result that a build would take now (stands: its kind, the stamps desc_keep.h's // stamps_of gives that kind, a texture's slot version), no bank or sampler missing: nothing was dropped. Walked once after a // whole build, and again only when a texture was registered since (a slot's version moves with registrations_generation); // - the draw may leave textures for later (redrawn.h: one that may not takes no kept dummy), is not a dump's, not the // upscaler's second recording; the per-binding reuse is on and not checked, the signature skip off, the banks' stage // generations on, the uniform buffers not snapshots (ub_snapshot_wanted: every Intel GPU, and any other integrated GPU whose // pools have no VRAM mirror; the rule is not tried there). // A stamp that moved while the uniform buffers were resolved (a mirror made by one of their binds) makes the whole build follow. // The textures the draw samples that the GPU wrote are read again from the last whole build's textures, as a build would. // Anything else: the whole build, as before. // RX_VK_DESC_UBO_ONLY=0: every such draw builds whole, as before. =check: such a draw is built both ways, the two compared // descriptor for descriptor and sampled texture for sampled texture, a difference counted and logged ("vk: uniform-buffer-only // build check"), and the whole build is the one used. So in check every such draw ends in a whole build, and the rule is only // ever tried right after a whole build: never over a build of the uniform buffers alone, the chain the default mode runs (that // state is tried by tests/test_uboonly.py alone). Each compared draw resolves its user uniform buffers twice, once a build, so a // check run's descriptor counts and stream use are neither mode's. Any other value (not 0, 1 or check) is on, and is told once. // The lab's knob "nouboonly" (RX_VK_LAB_AB) is =0 in its phases. The frame statistics' "a frame's uniform-buffer-only builds" // line counts them against all builds, and why the others were not (in check, none counts as resolved alone: each was built // whole). #pragma once #include #include #include #include #include #include "desc_keep.h" namespace uboonly { /// RX_VK_DESC_UBO_ONLY: unset or anything else, on (1); 0, off; check, both ways and compared (2) inline int mode_of(const char* env) { if (!env) return 1; if (!std::strcmp(env, "check")) return 2; return env[0] == '0' && !env[1] ? 0 : 1; } /// The mode in this draw: off where a rule above says so, or in the lab's "nouboonly" phases struct Now { int mode = 1; // (mode_of) bool lab_off = false; // (lab_ab::on("nouboonly")) int keep_mode = 1; // (RX_VK_DESC_KEEP: 1 on, 0 off, 2 checked) int sig_mode = 0; // (RX_VK_DESC_SIG: 0 off) bool snapshot = false; // (ub_snapshot_wanted) bool bank_gen_off = false; // (RX_VK_BANK_GEN_OFF) bool dump = false, retarget = false; // (dump_on, pre_retarget_on) bool strict = false; // (a frame left textures and the draw may not: its descriptors are its own; implied by !may_defer) bool may_defer = true; // (draw_may_defer) }; inline int mode_now(const Now& n) { if (n.mode == 0 || n.lab_off) return 0; if (n.keep_mode != 1 || n.sig_mode != 0 || n.snapshot || n.bank_gen_off) return 0; if (n.dump || n.retarget || n.strict || !n.may_defer) return 0; return n.mode; } inline constexpr int kStages = 5; // (the NVN stages whose banks stand on their own generations: 0 to 4) /// A user uniform buffer of the program: the stage's place in Program::stages, its constant buffer index (3 to 18), its place in /// the build's order (desc_keep::Prog::e) and among the build's buffers (DescWriter::bufs) struct Ubo { uint16_t stage = 0, index = 0; uint32_t j = 0, buf = 0; }; /// A descriptor the build leaves as it is: its kind (kC1, kSsbo, kTex; kNone a driver bank, kept by uniform_range's own cache), /// its place in the build's order and among the build's buffers (a texture: its images) struct Rest { uint8_t kind = desc_keep::kNone; uint32_t j = 0, at = 0; }; /// What the uniform-buffers-only build of a program does, made once a program a thread, and again after another program took its /// slot (its stages do not change once it is translated) struct Plan { const void* prog = nullptr; bool pure = false; // (the rule's first point: false, every build of it is whole) uint8_t stages = 0; // (its NVN stages, a bit each) uint32_t n = 0, nbuf = 0, nimg = 0; // (its descriptors, and how many of them are buffers and images) std::vector ubos; std::vector rest; }; inline constexpr size_t kPlans = 64; inline size_t plan_slot(const void* prog) { const uintptr_t p = (uintptr_t)prog; return (size_t)((p >> 6) ^ (p >> 14)) % kPlans; } /// Makes a program's plan from its stages' lists, called in build_descriptors' order: begin, then for each stage: stage, its /// constant buffers (one call an element), its storage buffers, its texel buffers, its sampled textures, its storage images /// (one call a descriptor); end struct Maker { Plan& p; bool ok = true; uint16_t at_stage = 0; bool first_stage = true; explicit Maker(Plan& plan) : p(plan) {} void begin(const void* prog, bool compute, bool fallback, bool renodx) { p.ubos.clear(), p.rest.clear(); // (their room kept: a slot another program takes over allocates nothing) p.prog = prog, p.pure = false, p.stages = 0, p.n = p.nbuf = p.nimg = 0; ok = !compute && !fallback && !renodx; first_stage = true, at_stage = 0; } void stage(int nvn_stage) { if (!first_stage) at_stage++; first_stage = false; if (nvn_stage < 0 || nvn_stage >= kStages) ok = false; // (compute, task, mesh: their banks stand on bind_gen) else p.stages |= (uint8_t)(1u << nvn_stage); } void cbuf(uint32_t index) { if (index == 0 || index == 2) p.rest.push_back({desc_keep::kNone, p.n, p.nbuf}); else if (index == 1) p.rest.push_back({desc_keep::kC1, p.n, p.nbuf}); else if (index < 3 + 16) p.ubos.push_back({at_stage, (uint16_t)index, p.n, p.nbuf}); else ok = false; // (no buffer there: nothing kept) p.n++, p.nbuf++; } void storage(uint32_t count, bool written, bool native_bounds, bool sim_turf, uint32_t cbuf_index, uint32_t cbuf_offset) { // (vk_draw.cpp sig_shape's: a single read-only one, the kind build_descriptors keeps) if (count != 1 || written || native_bounds || sim_turf || (cbuf_index == 0 && cbuf_offset == 0xff0)) ok = false; if (cbuf_index >= 3) ok = false; // (its address and size are words of a user uniform buffer: read anew with each bind) for (uint32_t k = 0; k < count; k++) p.rest.push_back({desc_keep::kSsbo, p.n + k, p.nbuf + k}); p.n += count, p.nbuf += count; } void texel(uint32_t count) { // (texel buffers, uniform or storage: resolved at every build, not kept) ok = false; p.n += count; } void texture(uint32_t count, bool handle_program, uint32_t cbuf_index, bool has_secondary, uint32_t secondary_cbuf_index) { if (handle_program) ok = false; // (its handle is computed from storage and constant words at each draw) if (cbuf_index >= 3 || (has_secondary && secondary_cbuf_index >= 3)) ok = false; // (a handle in a user uniform buffer) for (uint32_t k = 0; k < count; k++) p.rest.push_back({desc_keep::kTex, p.n + k, p.nimg + k}); p.n += count, p.nimg += count; } void image(uint32_t count) { // (storage images: resolved at every build, the written ones marked) ok = false; p.n += count, p.nimg += count; } void end() { p.pure = ok && p.stages != 0; if (!p.pure) p.ubos.clear(), p.rest.clear(); // (never read) } }; /// Whether no texture, sampler, image or storage buffer was bound to one of the program's stages since the gens were kept /// (NvnState::tex_gen, ssbo_gen: what bind_gen was when a stage's last changed) inline bool gens_same(uint8_t stages, const uint64_t* tex_was, const uint64_t* ssbo_was, const uint64_t* tex_now, const uint64_t* ssbo_now) { for (int k = 0; k < kStages; k++) if ((stages >> k & 1) && (tex_was[k] != tex_now[k] || ssbo_was[k] != ssbo_now[k])) return false; return true; } /// Whether the stamps the descriptors left as they are stand on are the same: all of desc_keep's but ub (the inline uniform /// updates' generation: only a user uniform buffer stands on it, and each of those is asked again) inline bool stamps_same(const desc_keep::Stamps& a, const desc_keep::Stamps& b) { return a.rec == b.rec && a.bind == b.bind && a.samp == b.samp && a.destroy == b.destroy && a.dummy == b.dummy && a.epoch == b.epoch; } /// Whether a whole build would take, now, every descriptor the plan leaves as it is, from kept (the program's kept results, in the /// build's order): each has a result of its kind kept at the stamps its kind stands on now, a texture at its slot's version now /// (slot_version(slot)); a texture with no result kept is one whose handle read 0, which resolves to the dummy and its sampler each /// time. A handle that read 0 leaves its element as it was: one that still holds an older result is judged by it (stale, the /// program builds whole while the slot stays unbound; standing, dw()'s dummy is what a build would make anyway). A sampler missing /// (sampler_missing(at)) is a defence only: a whole build with one fails bind_descriptors and is never bound, so no candidate has /// one. A driver bank that was not made (buffer_missing(at)) is made again. The inputs are not compared: gens_same says they are /// the same template bool stands(const Plan& plan, const std::vector& kept, const desc_keep::Stamps& now, SlotVersion&& slot_version, BufferMissing&& buffer_missing, SamplerMissing&& sampler_missing) { if (!plan.pure || kept.size() != plan.n) return false; for (const Rest& r : plan.rest) { const desc_keep::Elem& e = kept[r.j]; const desc_keep::Kind k = (desc_keep::Kind)r.kind; if (k == desc_keep::kNone) { if (buffer_missing(r.at)) return false; continue; } if (k == desc_keep::kTex) { if (sampler_missing(r.at)) return false; if (e.kind == desc_keep::kNone) continue; } if (e.kind != k || e.at != desc_keep::stamps_of(k, now)) return false; if (k == desc_keep::kTex && e.version != slot_version((uint32_t)(e.in[0] & 0xFFFFF))) return false; } return true; } /// A recording thread's last whole build by a draw: whose it was, the stages' generations and the stamps it was made at (the /// stamps read before it began: one that moves while it resolves makes the next draw build whole again), whether its descriptors /// were bound (the draw's bookkeeping is that build's), whether a walk found them standing (stands) and the registrations' /// generation at that walk; the dummies it counted (the frame statistics', counted again by each such draw) struct Last { const void* prog = nullptr; bool bound = false, walked = false; uint64_t tex[kStages]{}, ssbo[kStages]{}; desc_keep::Stamps at; uint64_t reg = 0; int64_t dummies = 0, dummies_known = 0; }; /// A whole build of prog was made (tex_gen, ssbo_gen: the state's, kStages of each at least; before: the stamps read before it) inline void built(Last& l, const void* prog, const uint64_t* tex_gen, const uint64_t* ssbo_gen, const desc_keep::Stamps& before, int64_t dummies, int64_t dummies_known) { l.prog = prog, l.bound = false, l.walked = false; for (int k = 0; k < kStages; k++) l.tex[k] = tex_gen[k], l.ssbo[k] = ssbo_gen[k]; l.at = before; l.dummies = dummies, l.dummies_known = dummies_known; } /// A whole build the rule does not stand on (a dump's, the upscaler's second recording's, a strict draw's, the switch off) inline void forget(Last& l) { l.prog = nullptr, l.bound = false, l.walked = false; } /// Its descriptors were bound (prog: the program bound) inline void bound(Last& l, const void* prog) { if (l.prog == prog) l.bound = true; } /// Why a build is whole, or that it is not: counted in the frame statistics' line enum Why : uint8_t { kFast = 0, kOther, kImpure, kBound, kStale }; /// The first part of the rule, before the plan is asked for: whether the thread's last build (built_for: DescWriter::prog) and /// the draw's bookkeeping (last_same: last_desc_valid and last_desc_prog) are this program's last whole build, bound inline bool candidate(const Last& l, const void* prog, const void* built_for, bool last_same) { return prog && l.prog == prog && l.bound && built_for == prog && last_same; } /// The rule before the uniform buffers are resolved: kFast, or why not. stamps_now(): desc_keep's stamps, read only once the /// cheaper points hold: they must be the whole build's. walk(now): stands, asked once after a whole build and again when a /// texture was registered since (reg_gen: a slot's version moves only with it) template Why decide(Last& l, const Plan& plan, const void* prog, const void* built_for, bool last_same, const uint64_t* tex_gen, const uint64_t* ssbo_gen, StampsNow&& stamps_now, uint64_t reg_gen, Walk&& walk) { if (!candidate(l, prog, built_for, last_same)) return kOther; if (plan.prog != prog || !plan.pure) return kImpure; if (!gens_same(plan.stages, l.tex, l.ssbo, tex_gen, ssbo_gen)) return kBound; const desc_keep::Stamps now = stamps_now(); if (!stamps_same(l.at, now)) return kStale; if (l.walked && l.reg == reg_gen) return kFast; l.walked = false; if (!walk(now)) return kStale; l.walked = true, l.reg = reg_gen; return kFast; } /// After the uniform buffers were resolved: whether the stamps are still the whole build's (false: a whole build follows) inline bool still(const Last& l, const desc_keep::Stamps& after) { return stamps_same(l.at, after); } /// (RX_VK_DESC_UBO_ONLY=check) Whether a buffer descriptor of the two builds is the same: {buffer, offset, range} equal; or, for a /// user uniform buffer (ubo) that both builds copied into the recording's stream (stream_a, stream_b), the same range, and the /// same bytes where both can be read (bytes_same) inline bool buffer_same(bool ubo, uint64_t buf_a, uint64_t off_a, uint64_t range_a, bool stream_a, uint64_t buf_b, uint64_t off_b, uint64_t range_b, bool stream_b, bool bytes_same) { if (buf_a == buf_b && off_a == off_b && range_a == range_b) return true; return ubo && stream_a && stream_b && buf_a && buf_b && range_a == range_b && bytes_same; } } // namespace uboonly