// The descriptor templates' per-thread table (no game, no GPU: a rule; vk_draw.cpp set_template, push_template). // // ((Triforce, 11-26, the GPU audit's C31, its part (a)) A program's descriptors go to the driver through a template of // its own: an update template for a program over the push limit (set_template), a push template a bind point for one // within it (push_template). Both are kept in maps by the program's address behind one mutex, tpl_mu, and each thread // had 8 entries a kind of template in front of them (one table for sets, one for each bind point's pushes), by // ((address >> 6) & 7). A frame pushes the descriptors of far more than 8 programs (335 to 535 sets pushed in full a // frame, each through its template), so most lookups missed the 8, took the global mutex and searched a // std::unordered_map: that map's lookup alone was 0.8% of the Presentation Thread's time at Lake Hylia and 1.1% in the // Market (about 0.07 and 0.06 ms a frame), and the replay's helper threads met on the one mutex at every whole push. // // Each thread that looks templates up now has one table of kSets sets of kWays entries (512 x 2, 16 KB, for the three // kinds of template), made at its first lookup and freed when the thread ends (none with RX_VK_TPL_TABLE=0, nor with // RX_VK_DESC_TEMPLATE=0, which makes no templates), by a multiplicative hash of the key: the program's address with the // kind in its two low bits. What an entry holds is what the 8 held, a key and the template the locked path gave for it, // and the caller checks what it finds the same way before using it (the template's layout is still the program's: a // program whose layout was made again misses, takes the lock and has a new template made, as before). A template is // immutable once made and never freed in play (a replaced one is kept for its readers), so an entry never names freed // memory. A miss is the locked path as it was, and its answer is put: in place where the key has an entry, else first // in its set, the set's first moved to second and the second dropped. Nothing but where a template is found changes: // the picture cannot. // // A set keeps its two keys first in, first out, and a hit does not move a key: a set that three or more of a frame's // keys fall in misses on each of them every frame. The table does not take every lookup off the lock: in a play test's // frame statistics, the lines of 400 lookups a frame or more still took it on about 10% of them (21% at worst), and 300 // programs at scattered addresses lock on about 12% of lookups after the first round (tests/test_tpltable.py). More // ways or another hash would change that; that is a change of its own, to be measured. // // The table is beside the program, not on it: sizeof(Program) is in the shader caches' stamp. // // RX_VK_TPL_TABLE=0: the 8 entries, as before. =2 or =check: the check, every find also asked of the map under the lock // (so every lookup takes it) and the differences counted; the table's template is the one used, as by default. Another // value is read by its leading number, as atoi reads it: 2 (as in 02 or +2) is the check, 0 the 8 entries when the value // starts with 0 (as in 00), anything else the wide table, as by default. Any value other than 1 or empty is logged once, // with what it gives. The lab's knob "notpltable" is the 8 entries in its phases. The frame statistics' line "a frame's // descriptor templates" counts the lookups and how many of them missed and took the lock for the map, by the switch (not // the lab's phase at the moment of the line).) #pragma once #include #include #include #include namespace mkwvk::tpltable { enum Mode : int { kOld = 0, kWide = 1, kCheck = 2 }; /// RX_VK_TPL_TABLE: the wide table unless set to 0 (the 8 entries) or 2 or check (the wide table, checked against the map) inline int mode(const char* env) { if (!env || !*env) return kWide; if (!std::strcmp(env, "check")) return kCheck; const int v = std::atoi(env); return v == 2 ? kCheck : v == 0 && env[0] == '0' ? kOld : kWide; } /// The table before: 8 entries by the program's address inline constexpr size_t kOldSlots = 8; constexpr size_t old_slot(uintptr_t p) { return (p >> 6) & 7; } /// The kinds of template a program has, in its key's two low bits (a program's address is a multiple of 4 at least) enum Kind : uintptr_t { kSet = 0, kPushGraphics = 1, kPushCompute = 2 }; inline uintptr_t key_of(const void* program, uintptr_t kind) { return (uintptr_t)program | kind; } inline constexpr int kSetBits = 9; inline constexpr size_t kSets = (size_t)1 << kSetBits, kWays = 2; /// A key's set: the top bits of its product with 2^64 / the golden ratio (every bit of the key moves them) constexpr size_t set_of(uintptr_t key) { return (size_t)(((uint64_t)key * 0x9E3779B97F4A7C15ull) >> (64 - kSetBits)); } struct Entry { uintptr_t key; // (0: empty; no program is at 0) const void* t; // (its template, as the locked path gave it) }; /// A thread's table (zeroed: empty). find gives what was put for the key, or null; the caller checks it is still the /// program's before using it, and puts the locked path's answer after a miss struct Table { Entry e[kSets * kWays]; const void* find(uintptr_t key) const { const Entry* s = &e[set_of(key) * kWays]; if (s[0].key == key) return s[0].t; if (s[1].key == key) return s[1].t; return nullptr; } void put(uintptr_t key, const void* t) { Entry* s = &e[set_of(key) * kWays]; if (s[1].key == key) { // (in place: never two entries for one key) s[1].t = t; return; } if (s[0].key != key) s[1] = s[0]; s[0] = {key, t}; } }; } // namespace mkwvk::tpltable