// A draw's program by a wider memo in front of program_for (no game, no GPU: a rule; vk_shader.cpp program_for_wide, // vk_draw.cpp draw and dry_draw_program). // // (Triforce, 11-26, the GPU audit's C02.) program_for (vk_shader.cpp) answers a graphics draw from its pair of last picks // when nothing was bound and no uniform was updated since, or, bound anew, when the textures of the stages its answers // are read from stand (NvnState::tex_gen); it compares ub_generation() all the same, which moves at every inline uniform // update, and holds two programs. A draw that misses takes the full pick: the inputs' hash (18 multiplies, and one more // with the serial for the key), the picks' table, every answer's handle read (answer_handles), and where a handle // differs every handle read a second time with the type of each compared (still_valid), then the pair written. About 7% // of the Presentation Thread at Lake Hylia. // // The memo here stands in front of that, a thread's own, 16 sets of 2 by the program object's serial, one entry a // program object and vertex inputs and colour types (the picks' key, compared whole, with the program's stage words): // - an entry stands (stands) as the pair does, and for a program whose answers are all handles in c2 of their own // stages (answer_stage_mask, from the program's descriptors, not by name) across uniform updates too: c2 is served // from the bound textures, samplers and images alone (vk_shader.cpp stage_cbuf_word, vk_bank.cpp driver_bank_word), // never from uniform memory, and the full pick does not read ub_generation either. A handle program (it reads // uniform memory) or another bank keeps the pair's rule. The textures' registrations (registrations_generation) // stay in the rule: a slot registered anew moves it. // - an entry of the same inputs that does not stand is asked in ONE pass over its answers (one_pass): each handle // read once; a slot with the id and version it was last seen with, and checked then, is not asked again (what a // slot answers is fixed for a version: vk_shader.cpp texture_answer keeps it so); any other bound slot's type is // compared with the variant's, canonical on both sides; an unbound slot takes any. That is still_valid's verdict, // answer for answer. All of them of the variant's types: the entry's program, and nothing else is read. // - anything else is program_for, unchanged, and its program only NOTED here (note: the inputs and the pointer; no // answer read, no type compared, no mask taken). A noted entry does not stand. At its next sight it is asked in one // pass, every answer (found: its mask taken and the generations stamped, it is kept from there as above); not of // the bound textures' types then (a program a translation finished under the pick, or textures bound since), it is // dropped and the draw is program_for's. So a program drawn once before its entry is replaced (more programs in a // stretch of the frame than the memo holds, three serials in one set) costs the find and the note on top of program_for, // and one drawn twice or more pays at its second draw the one pass it would have paid at its first. The set is by // the serial alone: one program object drawn with three or more sets of vertex inputs or colour types in turn in one // stretch takes its set's two ways in turn and never gets a memo answer ("by program_for" in the frame line shows // it). // So the memo returns a program only where it is the last one program_for gave these inputs on this thread AND it is of // the bound textures' types, which is when the full pick returns its own kept program. What it leaves out is the hash, // the table, the second walk and the pair. // // What the rule stands on: // - NvnState::bind_gen and tex_gen are one lineage a queue state, bumped the same in the dry pass and in a worker's // replay of a segment (vk_replay.cpp replay: bind program, uniform and storage buffers, textures, samplers, images, // pools, push constants; use_queue_state rebases them at a switch). An entry lives far longer than the pair's did // (that died at every uniform update), so a bump of either that depended on the mode, or a write to s.tex / s.samp / // s.img without tex_gen's bump, would let an entry stand over other textures. // - A handle program (mask ~0) stands only by the pair's first rule (bind_gen and ub_generation(), a thread's own // count, as they were: nothing bound, no uniform updated since), so only at the next draw of the same program object: // binding another program and this one again moves bind_gen. Its other draws are answered by one pass instead of the // full pick. // Where it can differ from RX_VK_PICK_WIDE=0, both programs being of the bound textures' types: program_for empties a // thread's picks when they pass 65536 keys and then takes the first variant of those types in the list's order; the // memo keeps the one it has. With an answer unbound (it takes any type) or types that are one canonically, two variants // can both be of the bound textures' types, and the program and its pipeline are then another variant of the same // shaders for the same textures. RX_VK_PICK_WIDE=check counts it as a difference that is not a wrong variant. // // Why in front and not in program_for: program_for, its pair and the picks are in the translation stamp (nx2/build.py // translation_text, program_carry.h), and a token changed there starts every player's program and pipeline caches // again at the update. program_for_wide is in a part of vk_shader.cpp outside the stamp and changes no token inside. // // RX_VK_PICK_WIDE=0: program_for alone, as before (its pair and the two walks). =check: a draw the memo answers is asked // of program_for too, whose program is drawn (the others are program_for's alone), and the draws where the memo would // have given another program are counted, and of those the ones whose program is not of the bound textures' types (a // wrong variant samples a float texture as integers: NaN, the yellow scene). Check mode calls program_for at every draw, // so its pair and its picks' handles move as with =0 and not as in the default, where program_for is called seldom: it // tries the memo's answers, not that state. The lab's knob "nopickwide" (RX_VK_LAB_AB) is =0 in its phases. // RX_VK_PICK_BIND_GEN (set to anything, as program_for reads it): an entry stands only while nothing at all was bound // and no uniform updated, the pair's old rule. RX_VK_DEFERRED_LATE (likewise; binds read from the game's arrays at the // replay: a generation is then no proof of what is bound, and the uniform updates' count is what told two passes apart): // an entry stands on its stages' textures, but not past a uniform update. #pragma once #include #include #include #include #include namespace mkwvk::pickwide { inline constexpr int kStages = 8; // NvnState::tex_gen's stages (vk_state.h kNvnStages: the hook asserts it) inline constexpr int kSets = 16, kWays = 2; // the memo: by the program object's serial inline constexpr uint32_t kNotC2 = ~0u; // answer_stage_mask's "not all in c2 of their own stages" inline constexpr uint32_t kSlotBits = 0xFFFFF; // a handle's texture slot (answer_handles' id) enum class Mode { Off, On, Check }; /// RX_VK_PICK_WIDE: unset or a number other than 0, on; check (exactly), both asked and compared; 0 or any other word /// (on, Check, empty), off (logged) inline Mode mode(const char* env) { if (!env) return Mode::On; if (!std::strcmp(env, "check")) return Mode::Check; return std::atoi(env) != 0 ? Mode::On : Mode::Off; } /// The mode of this phase: off in the lab's "nopickwide" phases constexpr Mode in_phase(Mode env, bool lab_off) { return lab_off ? Mode::Off : env; } struct Rules { bool by_tex; // an entry stands on its stages' textures across other binds (off: RX_VK_PICK_BIND_GEN) bool free_ub; // and across uniform updates (off: RX_VK_PICK_BIND_GEN, RX_VK_DEFERRED_LATE) }; constexpr Rules rules(bool pick_bind_gen, bool deferred_late) { return {!pick_bind_gen, !pick_bind_gen && !deferred_late}; } /// What a draw is picked by, now struct Now { uint64_t serial, sig; // the program object's serial and its stage words (prog_stage_sig) uint64_t bind, reg, ub; // NvnState::bind_gen, registrations_generation(), ub_generation() int nvattr; const uint32_t* vattr; // [16] const uint8_t* ct; // [8] const uint64_t* tex_gen; // [kStages] }; /// A program kept for its inputs, with what it was last found valid at struct Entry { const void* p = nullptr; // (the Program; null: nothing kept) bool walked = false; // (false: only noted, program_for's last for these inputs; its answers not asked yet) uint64_t serial = 0, sig = 0, bind = 0, reg = 0, ub = 0; int nvattr = 0; uint32_t vattr[16]{}; uint8_t ct[8]{}; uint32_t amask = kNotC2; // (answer_stage_mask of p) uint64_t tex_gen[kStages]{}; // (the stages' tex_gen it was last found valid at) std::vector stamps; // (an answer: its slot's id and version when last walked) std::vector ok; // (an answer: that slot, at that version, was bound and of the variant's type) std::vector canon; // (an answer: the variant's type, canonical: the hook's) }; /// Whether the entry is of these inputs: the picks' key compared whole, and the program object's stage words inline bool same_inputs(const Entry& e, const Now& n) { if (!e.p || e.serial != n.serial || e.sig != n.sig || e.nvattr != n.nvattr) return false; return !std::memcmp(e.vattr, n.vattr, sizeof e.vattr) && !std::memcmp(e.ct, n.ct, sizeof e.ct); } enum class Stand { No, // walk its answers (or pick in full) Bound, // nothing bound, nothing updated since: the pair's first rule Textures, // bound anew, its answers' stages' textures as they were: the pair's second rule PastUpdate, // and a uniform updated since, which its answers do not read: this memo's }; /// Whether the entry's program stands for the draw without a look at its answers inline Stand stands(const Entry& e, const Now& n, Rules r) { if (!same_inputs(e, n) || !e.walked || e.reg != n.reg) return Stand::No; const bool ub_same = e.ub == n.ub; if (e.bind == n.bind && ub_same) return Stand::Bound; if (!r.by_tex || e.amask == kNotC2) return Stand::No; if (!ub_same && !r.free_ub) return Stand::No; for (int k = 0; k < kStages; k++) if ((e.amask >> k & 1) && e.tex_gen[k] != n.tex_gen[k]) return Stand::No; return ub_same ? Stand::Textures : Stand::PastUpdate; } struct Walk { uint32_t answers = 0, moved = 0, checked = 0; // (walked; with another slot or version than last; type compared) }; /// One pass over the entry's n answers: read(i) the handle answer i reads now, version(id) its slot's version, /// check(i, handle) whether that bound texture is of the variant's type for answer i. True: every bound answer is of the /// variant's type (still_valid's verdict). False: one is not, and the entry is not to be used as it is (drop or take anew) template bool one_pass(Entry& e, size_t n, Read&& read, Version&& version, Check&& check, Walk* walk = nullptr) { if (e.stamps.size() != 2 * n || e.ok.size() != n) e.stamps.assign(2 * n, ~0u), e.ok.assign(n, 0); Walk w; bool good = true; for (size_t i = 0; i < n && good; i++) { const uint32_t h = read(i), id = h & kSlotBits, ver = version(id); uint32_t* const st = &e.stamps[2 * i]; w.answers++; if (st[0] != id || st[1] != ver) st[0] = id, st[1] = ver, e.ok[i] = 0, w.moved++; if (!h || e.ok[i]) continue; // (unbound: takes any. Checked at this slot and version: stands) w.checked++; if (check(i, h)) e.ok[i] = 1; else good = false; } if (walk) *walk = w; return good; } /// The next pass over the entry asks every answer inline void fresh(Entry& e) { e.stamps.clear(), e.ok.clear(); } /// The entry found valid now: it stands from here inline void restamp(Entry& e, const Now& n) { e.bind = n.bind, e.reg = n.reg, e.ub = n.ub; std::memcpy(e.tex_gen, n.tex_gen, sizeof e.tex_gen); } /// The entry notes the program program_for gave these inputs: nothing of it is read. It does not stand (stands) until /// one pass has found it of the bound textures' types (found), at its next sight inline void note(Entry& e, const Now& n, const void* p) { e.p = p, e.walked = false, e.serial = n.serial, e.sig = n.sig, e.nvattr = n.nvattr, e.amask = kNotC2; std::memcpy(e.vattr, n.vattr, sizeof e.vattr); std::memcpy(e.ct, n.ct, sizeof e.ct); fresh(e); } /// A noted entry's first pass found it valid: kept with its answers' stages (answer_stage_mask), it stands from here inline void found(Entry& e, const Now& n, uint32_t amask) { e.amask = amask, e.walked = true; restamp(e, n); } /// Noted and found at once (the tests' shorthand: the hook notes at one draw and finds at the next) inline void take(Entry& e, const Now& n, const void* p, uint32_t amask) { note(e, n, p); found(e, n, amask); } inline void drop(Entry& e) { e.p = nullptr; } struct Table { Entry e[kSets][kWays]; uint8_t next[kSets]{}; // (the way a new program takes: the one not used last) }; static_assert(kSets == 16 && kWays == 2, "set_of and next go by 16 sets of 2"); constexpr unsigned set_of(uint64_t serial) { return (unsigned)((serial * 0x9E3779B97F4A7C15ull) >> 60); } /// The entry of these inputs (there is at most one: place), or none inline Entry* find(Table& t, const Now& n) { const unsigned set = set_of(n.serial); for (int w = 0; w < kWays; w++) if (same_inputs(t.e[set][w], n)) { t.next[set] = (uint8_t)(1 - w); return &t.e[set][w]; } return nullptr; } /// Where a program for these inputs is kept: the entry of the same inputs, else an empty way, else the set's way not used last inline Entry& place(Table& t, const Now& n) { if (Entry* e = find(t, n)) return *e; const unsigned set = set_of(n.serial); int w = t.next[set]; for (int k = 0; k < kWays; k++) if (!t.e[set][k].p) w = k; t.next[set] = (uint8_t)(1 - w); return t.e[set][w]; } inline void clear(Table& t) { for (auto& set : t.e) for (Entry& e : set) drop(e); } } // namespace mkwvk::pickwide