Files
Triforce-Source/gpu/vk/bakescan.h
ruckus7382sfgsgdf b527e29adb Triforce v0.8.5
A Randomizer page in the launcher, a new title screen with Mods and Save Select tabs, scaling changes applied in game, the Jabu-Jabu bars fixed, renderer frame-time work and the brightness-flash fix, and fixes from the 0.8.4 reports.
2026-11-28 10:54:41 -06:00

127 lines
6.9 KiB
C++

// (Triforce, 11-26, the GPU audit's C36) The bake write-back's scan walks a list, not the whole texture cache.
//
// bake_writeback_step (vk_texture.cpp) puts into the game's memory the texels of a texture the GPU drew in one frame and the
// game still samples (the terrain's normal maps). Every fourth present it walked every texture of the cache with a dozen
// tests each to find them: 1.1% of the Presentation Thread at Lake Hylia, 0.9% at the Market (about 0.35 / 0.2 ms on each
// fourth present), for a walk that finds nothing where the recordings that drew them write them back themselves ("by the
// CPU: 0" on the card this was measured on).
// Here: the textures the step can ever take are kept in a list, and the step walks that.
//
// The walk's test in two halves, unchanged together:
// - armed(): what does not pass with time. The GPU's contents began and ended in one frame (first_written ==
// last_written), and are not in the memory or on their way (wb_written != last_written). These four fields can make a
// texture armed in three places only, and each tells the list (List::note): texture_written, and the two places of the
// step that put wb_written back to 0 (a copy lost, or finalized or drawn again while it was under way). Their other
// writes (wb_written = last_written for a write-back under way, gpu_written = false) only disarm it, and a step drops it.
// - ripe(): what does pass with time, or is another's to change: four presents old, sampled in the last four, not
// released, not only cleared, small, in the guest window. Asked again at each step for each listed texture. A listed one
// stays listed while it is armed, however long it is not ripe: one not sampled for a hundred presents is taken when it
// is sampled again, as the walk took it.
// A texture leaves the list when a step finds it armed no more (written back, drawn in a second frame, its contents
// dropped: any way back is through a place that tells the list), or when it is destroyed (forget_gpu: the list holds
// pointers). The caps of a step are the walk's (room()); when they bite, the list takes the oldest listed first where the
// walk took whatever the hash map gave first, and the rest four presents on, as before. One no step can ever take (more
// than a step's 8 MB of storage, small in texels) is passed over in the list, where it would stop every newer one for as
// long as it is armed; the walk of the whole cache stops at it, as before, and does not reach those behind it in the hash
// map's order while it is ripe. So the same textures are taken, in another order, but for those: the list takes them.
//
// RX_VK_BAKE_SCAN=all (or 0): the walk of the whole cache, and no list, as before. =check: the walk of the whole cache,
// and every texture of it that is armed and not listed is counted and logged, each once (must be 0: one would have been
// missed by the list). The lab knob nobakescan (RX_VK_LAB_AB) walks the whole cache in its phases. The list is kept
// (kept()) by default, with =check and under the knob, whose phases go back to it; not with =all, nor with
// RX_VK_BAKE_WRITEBACK=0, where there is no step: nothing reads it there.
// The count is in a line of its own every 600 presents ("vk: the bake write-back's scan: ...").
//
// The rule alone: tests/test_bakescan.py compiles it against the walk's own test over a script of events.
#pragma once
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <mutex>
#include <vector>
namespace bakescan {
enum Mode { kList, kAll, kCheck };
/// RX_VK_BAKE_SCAN: "all" (or 0) the whole cache, "check" the whole cache and the list's misses counted, else the list
inline Mode mode_of(const char* asked) {
if (!asked || !*asked) return kList;
if (!std::strcmp(asked, "all") || !std::strcmp(asked, "0")) return kAll;
if (!std::strcmp(asked, "check")) return kCheck;
return kList;
}
/// What time alone does not change: drawn by the GPU in one frame, its texels not in the game's memory nor on their way
constexpr bool armed(bool gpu_written, uint64_t first_written, uint64_t last_written, uint64_t wb_written) {
return gpu_written && first_written && first_written == last_written && wb_written != last_written;
}
/// The rest of the walk's test (but the image and its format, which the step asks itself): still the game's, drawn four
/// presents ago or more, sampled in the last four, small, in the guest window
constexpr bool ripe(bool released, bool only_cleared, uint64_t last_written, uint64_t last_frame, uint64_t bytes, uint64_t gpu,
uint64_t frame_no) {
return !released && !only_cleared && !(frame_no < last_written + 4) && !(frame_no - last_frame > 4) && !(bytes > (4ull << 20)) &&
!(gpu >= (1ull << 39));
}
/// A step's caps: 24 textures and 8 MB; one more of guest_size bytes fits (room(0, 0, guest_size) false: no step can take it)
constexpr bool room(size_t items, uint64_t bytes, uint64_t guest_size) {
return !(items >= 24 || bytes + guest_size > (8ull << 20));
}
/// Whether the list is kept: only where something reads it, the write-back on (RX_VK_BAKE_WRITEBACK) and RX_VK_BAKE_SCAN
/// not "all". =check asks it, and the knob's phases go back to it
constexpr bool kept(bool writeback_on, Mode m) { return writeback_on && m != kAll; }
/// The armed textures. T has gpu_written, first_written, last_written, wb_written and a bool wb_listed of the list's own.
/// note() from whichever thread writes those fields (where kept()); forget() before a listed texture is freed; settle() at
/// a step.
template <class T>
struct List {
std::mutex mu;
std::vector<T*> items;
std::atomic<uint64_t> added{0};
static bool armed_now(const T& t) { return armed(t.gpu_written, t.first_written, t.last_written, t.wb_written); }
/// After a write of the fields armed() reads: listed if it is armed now (once)
void note(T* t) {
if (!armed_now(*t) || t->wb_listed) return;
std::lock_guard lk(mu);
if (t->wb_listed) return;
t->wb_listed = true;
items.push_back(t);
added.fetch_add(1, std::memory_order_relaxed);
}
/// The texture goes: its pointer is not kept
void forget(T* t) {
if (!t->wb_listed) return;
std::lock_guard lk(mu);
for (size_t i = 0; i < items.size(); i++)
if (items[i] == t) {
items.erase(items.begin() + (ptrdiff_t)i);
break;
}
t->wb_listed = false;
}
/// A step: those armed no more leave; the rest, oldest listed first, into out (the step's own copy)
void settle(std::vector<T*>& out) {
std::lock_guard lk(mu);
size_t n = 0;
for (T* t : items) {
if (armed_now(*t)) items[n++] = t;
else t->wb_listed = false;
}
items.resize(n);
out.assign(items.begin(), items.end());
}
size_t size() {
std::lock_guard lk(mu);
return items.size();
}
};
} // namespace bakescan