Files
Triforce-Source/gpu/vk/capgap.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

119 lines
5.9 KiB
C++

// ((Triforce, 11-26, the GPU audit's C09) The left-out ranges' check answered from the gaps it found before (no game, no
// GPU: a rule; vk_memory.cpp cap_excl_note -> cap_excl_hit_gap).
//
// What it does: with RX_VK_CAP_ONLY_GPU=1 (the default) or 2 every buffer binding, every replay read as submitted and every
// texture upload asks whether its bytes reach into the memory the capture leaves out (cap_excl_note: a count and the
// first 20 log lines, nothing else). The answer was a binary search at every question over the thread's copy of the
// left-out ranges, 2,400-3,650 of them on this PC (about 12 steps over a 38-58 KB list): cap_excl_hit was 2.3% of the
// Presentation Thread's own time at Lake Hylia and 2.6% in the Market, for an answer that is "no" nearly always. A draw's
// bindings walk rings inside a few pools, so a question nearly always falls into a gap between two neighbouring
// left-out ranges that an earlier question already found. The memo keeps the last 8 such gaps a thread; a question
// wholly inside one of them is answered "no" after a few compares, any other question is searched as before, and a
// search that says "no" keeps the gap it found.
//
// Why it is the same answer: the ranges are sorted by their start and none ends before it starts. A search for [lo, hi)
// that does not find lo inside a range finds the range before lo (ends at or before lo) and the range after it (starts
// past lo); the gap is [the one's end, the other's start), 0 and ~0 at the list's ends. For any [lo', hi') with
// gap_lo <= lo' < hi' <= gap_hi the search lands between the same two ranges and says "no". A question that crosses a
// gap's edge, an empty or wrapped one, and the questions after the list changed, until the memo has found gaps again
// (the caller clears the memo with its copy of the list, at every cap_excl_ver change), go to the search. Only searches
// that said "no" are kept, so the questions that do reach into the ranges do not push the gaps out; a question inside a
// kept gap is not searched, so its gap is not kept twice, but an empty or wrapped one is searched and can keep a gap
// the memo holds already.
//
// Only the count's question goes by the memo. The write-back's clip (gpu_buffer_bind, wb_excl_clip.h) and
// RX_VK_CAP_ONLY_GPU=2's page count keep cap_excl_hit's plain search.
//
// The switch: RX_VK_CAP_EXCL_GAP (not set or 1: the memo; 0: the search at every question, as before; check: both at
// every question, the search's answer used and the differences counted, the first 20 logged). The lab's knob "nocapgap"
// is 0 in its phases (asked again only when the phase moves). The count: the log's "capture (RX_VK_CAP_ONLY_GPU" line
// every 10 s carries "left-out checks in all from the gap memo N, searched M, differing D (RX_VK_CAP_EXCL_GAP)":
// totals since the start, which leave out the questions asked with =0 and in the knob's phases.)
#pragma once
#include <algorithm>
#include <cstdint>
#include <cstring>
#include <utility>
#include <vector>
namespace mkwvk::capgap {
using Range = std::pair<uint64_t, uint64_t>;
enum Mode { kOff = 0, kOn = 1, kCheck = 2 };
/// RX_VK_CAP_EXCL_GAP: not set, "" or anything else: on; "0": off; "check": both and compared
inline Mode mode(const char* env) {
if (!env || !env[0]) return kOn;
if (env[0] == '0' && !env[1]) return kOff;
if (!std::strcmp(env, "check")) return kCheck;
return kOn;
}
/// Whether a question goes by the memo: the switch not 0, and not in the lab's "nocapgap" phases
inline bool use(Mode m, bool lab_off) {
return m != kOff && !lab_off;
}
inline constexpr int kGaps = 8;
/// A thread's kept gaps: [lo[k], hi[k]) holds no byte of a left-out range. An unused entry has lo > hi and holds nothing
struct Memo {
uint64_t lo[kGaps], hi[kGaps];
uint32_t last = 0, next = 0; // (the entry that answered last, asked first; the entry the next gap replaces)
Memo() { clear(); }
void clear() {
for (int k = 0; k < kGaps; k++) lo[k] = 1, hi[k] = 0;
last = next = 0;
}
};
/// The search, as vk_memory.cpp's ranges_hit: whether [lo, hi) reaches into one of v's ranges (sorted by start). Where lo
/// is in none of them, the gap around lo in *gap_lo, *gap_hi; where it is in one, *gap_lo > *gap_hi
inline bool search(const std::vector<Range>& v, uint64_t lo, uint64_t hi, uint64_t* gap_lo, uint64_t* gap_hi) {
auto it = std::upper_bound(v.begin(), v.end(), std::make_pair(lo, ~uint64_t(0)));
*gap_lo = 1, *gap_hi = 0;
if (it != v.begin() && lo < std::prev(it)->second) return true;
*gap_lo = it != v.begin() ? std::prev(it)->second : 0;
*gap_hi = it != v.end() ? it->first : ~uint64_t(0);
return it != v.end() && it->first < hi;
}
/// Whether [lo, hi) is wholly inside one of the kept gaps (the answer is then "no"): never for an empty or wrapped one
inline bool inside(Memo& m, uint64_t lo, uint64_t hi) {
if (hi <= lo) return false;
const uint32_t l = m.last;
if (m.lo[l] <= lo && hi <= m.hi[l]) return true;
for (uint32_t k = 0; k < (uint32_t)kGaps; k++)
if (m.lo[k] <= lo && hi <= m.hi[k]) {
m.last = k;
return true;
}
return false;
}
/// A gap found by a search that said "no", kept over the oldest
inline void keep(Memo& m, uint64_t gap_lo, uint64_t gap_hi) {
if (gap_lo >= gap_hi) return;
const uint32_t k = m.next;
m.lo[k] = gap_lo, m.hi[k] = gap_hi;
m.last = k;
m.next = (k + 1) % (uint32_t)kGaps;
}
/// Whether [lo, hi) reaches into one of v's ranges: by the memo, else by the search (*searched set). The memo must have
/// been cleared since v last changed
inline bool hit(Memo& m, const std::vector<Range>& v, uint64_t lo, uint64_t hi, bool* searched) {
if (inside(m, lo, hi)) {
*searched = false;
return false;
}
*searched = true;
uint64_t gap_lo, gap_hi;
const bool r = search(v, lo, hi, &gap_lo, &gap_hi);
if (!r) keep(m, gap_lo, gap_hi);
return r;
}
} // namespace mkwvk::capgap