mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-28 20:08:51 -04:00
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.
176 lines
11 KiB
C++
176 lines
11 KiB
C++
// A read binding answered from a span of its pool's mirror this thread already resolved (no game, no GPU: a rule;
|
|
// vk_memory.cpp gpu_buffer_bind).
|
|
//
|
|
// (Triforce, 11-26, the GPU pass on v0.8.3, C08.) Every buffer a draw binds goes through gpu_buffer_bind: the uniform
|
|
// buffers of each stage, the vertex streams, the index buffer, indirect arguments. A read binding the memo does not know
|
|
// (a per-draw uniform range is new at every draw) takes the lock-free path: the thread's pool table (256 KB, by 64 KB
|
|
// page), the pool's bounds, a write of Pool::last_value, a lock-prefixed add on Pool::binds, the mirror's state, the
|
|
// submission and the segment it was synced in, and a walk of the resident bits of a mirror in pieces. All of that only
|
|
// to find again what the binding before it in the same pool found: this mirror, at guest - pool.lo. gpu_buffer_bind
|
|
// was 9.2% of the Presentation Thread's samples at Lake Hylia (4.5% self), 10.0% / 4.7% at the Market.
|
|
//
|
|
// So the thread keeps a few spans (kSpans): [lo, hi) of guest addresses whose read bindings are given `buffer` at
|
|
// guest - base, each filled by a binding that path just gave the mirror. A later binding inside one is answered by two
|
|
// compares, while everything the answer depended on still stands:
|
|
// - the key: gpu_bind_stamp() (pools leaving, a mirror made or dropped, a piece taken out of a mirror, the next
|
|
// submission: what the memo trusts), pools_gen itself (the Pool pointer), the thread's texture_segment() (a pool is
|
|
// synced for a segment and those after it: equal, not later, since a worker can be handed an earlier one) and
|
|
// mem_rec_value (the path raises Pool::last_value to it: a span filled under the same value has done so), and
|
|
// pools_overlap_gen (imports left overlapping, below). One key for the whole set: a change of it empties the set;
|
|
// - what is read at every binding, not kept: mirror_upload (false between a submission's end and the next one's
|
|
// start, where the path gives the import; the stamp does not move there), and the cases that never use a span
|
|
// (eligible, below);
|
|
// - the span's bounds: the pool's, or for a mirror in pieces the run of resident pieces around the binding that
|
|
// filled it (resident_run): a resident bit is only ever cleared together with a move of pools_gen.
|
|
// Imports are kept apart, but where pool_lookup's plan to take in the imports a binding's range overlaps would pass
|
|
// 1 GB, the new import is made beside them: a page then has two imports, and a binding of it gets the one its
|
|
// thread's per-page slot or pool_of gives. A span of either import would answer for the other's pages too, so neither
|
|
// gets one (Pool::left_overlapping, pools_flag_overlapping), and pools_overlap_gen moves so that a span filled before
|
|
// is not asked again; their bindings take the whole path as before.
|
|
// The memo is asked first and is written at a hit as before, so it holds what it held: a hit changes neither what a
|
|
// later binding is given nor Pool::last_value, only when Pool::binds is added to (the hits are counted on the span and
|
|
// folded into the pool when the span is replaced or the key moves, and dropped when pools_gen moved, the pool being
|
|
// possibly gone; binds is a logged average, "approximate").
|
|
//
|
|
// Never from a span, the whole path as before (eligible): a written binding, a binding of no bytes, outside a
|
|
// submission's uploads or without mirrors, RX_VK_LOG_MIRROR_USE (every binding is noted), RX_VK_MIRROR_HEAT (every
|
|
// binding adds its bytes to the pool's heat, which chooses the mirrors) and RX_VK_MIRROR_PIECE_IDLE (every binding
|
|
// stamps its pieces). cap_excl_note, the translation of an address at or above 1 TB and late_deferred_bound run ahead
|
|
// of the span at every binding.
|
|
//
|
|
// RX_VK_BIND_SPAN=0: no spans, as before. =check: a hit is also resolved the whole way, the two results compared and
|
|
// the differences counted (the first 20 logged with the span and its key); the whole way's result is the one
|
|
// returned (cap_excl_note is not asked again for it), and the hit is counted as checked, not as answered from a span.
|
|
// Any other value, empty included: spans, as unset (logged when not 1 or empty). The lab's knob "nobindspan" is =0 in
|
|
// its phases (read once a submission, memory_begin_submission). The count is in the log every 600 submissions, with
|
|
// mirrors: "vk: bind spans (600 submits): ...".
|
|
#pragma once
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
|
|
namespace mkwvk::bindspan {
|
|
|
|
inline constexpr int kSpans = 8; // a draw's bindings alternate between a handful of pools
|
|
inline constexpr uint64_t kRunMost = 16; // pieces a fill looks at on each side of its binding (4 MB of 256 KB pieces: a fill is a miss's cost)
|
|
|
|
inline constexpr uint64_t kTakeAt = 256; // span questions a thread counts by itself before its counts go to the shared sums
|
|
|
|
enum Mode : int { kOff = 0, kOn = 1, kCheck = 2 };
|
|
|
|
/// RX_VK_BIND_SPAN: not set, "" or any other value: on; "0": off; "check": both ways, compared
|
|
inline int mode(const char* env) {
|
|
if (!env || !*env) return kOn;
|
|
if (!std::strcmp(env, "check")) return kCheck;
|
|
return env[0] == '0' && !env[1] ? kOff : kOn;
|
|
}
|
|
|
|
/// Whether this binding may be answered from a span, or fill one (see the list above). mirrors_uploading: mirrors are
|
|
/// on and a submission's uploads are being recorded
|
|
constexpr bool eligible(bool written, uint64_t size, bool mirrors_uploading, bool use_logged, bool heat_on, bool piece_idle_on) {
|
|
return !written && size != 0 && mirrors_uploading && !use_logged && !heat_on && !piece_idle_on;
|
|
}
|
|
|
|
/// What the answer depends on that a span cannot see change (all five equal, or the set is empty)
|
|
struct Key {
|
|
uint64_t stamp = 0, pools_gen = 0, segment = 0, rec_value = 0, overlaps = 0;
|
|
constexpr bool operator==(const Key& o) const {
|
|
return stamp == o.stamp && pools_gen == o.pools_gen && segment == o.segment && rec_value == o.rec_value &&
|
|
overlaps == o.overlaps;
|
|
}
|
|
};
|
|
|
|
/// [guest, guest + size) lies in [lo, hi), without a wrap-around for a huge size (as gpu_buffer_bind's pool test)
|
|
constexpr bool inside(uint64_t lo, uint64_t hi, uint64_t guest, uint64_t size) {
|
|
return size != 0 && guest >= lo && guest <= hi && size <= hi - guest;
|
|
}
|
|
|
|
/// A mirror in pieces: the run of resident pieces [*i0, *i1] around the pieces [a, b] a binding was just found
|
|
/// resident in (bits: a bit a piece, read without the lock; n: the pool's pieces), at most kRunMost further each way
|
|
inline void resident_run(const uint64_t* bits, uint64_t n, uint64_t a, uint64_t b, uint64_t* i0, uint64_t* i1) {
|
|
const auto set = [&](uint64_t i) { return (__atomic_load_n(&bits[i >> 6], __ATOMIC_RELAXED) >> (i & 63)) & 1; };
|
|
uint64_t lo = a, hi = b;
|
|
for (uint64_t k = 0; k < kRunMost && lo > 0 && set(lo - 1); k++) lo--;
|
|
for (uint64_t k = 0; k < kRunMost && hi + 1 < n && set(hi + 1); k++) hi++;
|
|
*i0 = lo, *i1 = hi;
|
|
}
|
|
|
|
/// A pool's span for a binding of [off, off + size) of it (size != 0) that was just given its mirror: the whole pool
|
|
/// [pool_lo, pool_hi), or with a mirror in pieces (bits != nullptr, a piece 1 << shift bytes) the resident run around it
|
|
inline void bounds(uint64_t pool_lo, uint64_t pool_hi, uint64_t off, uint64_t size, const uint64_t* bits, uint64_t bit_words,
|
|
uint32_t shift, uint64_t* lo, uint64_t* hi) {
|
|
*lo = pool_lo, *hi = pool_hi;
|
|
if (!bits) return;
|
|
const uint64_t piece = 1ull << shift;
|
|
const uint64_t n = (pool_hi - pool_lo + piece - 1) >> shift;
|
|
const uint64_t a = off >> shift, b = (off + size - 1) >> shift;
|
|
if (b >= n || b >= bit_words * 64) { // (not a range of its pieces: the binding alone)
|
|
*lo = pool_lo + off, *hi = pool_lo + off + size;
|
|
return;
|
|
}
|
|
uint64_t i0 = a, i1 = b;
|
|
resident_run(bits, n < bit_words * 64 ? n : bit_words * 64, a, b, &i0, &i1);
|
|
*lo = pool_lo + (i0 << shift);
|
|
const uint64_t end = pool_lo + ((i1 + 1) << shift);
|
|
*hi = end < pool_hi ? end : pool_hi;
|
|
}
|
|
|
|
/// A thread's spans. Buf: the mirror's buffer handle; PoolPtr: the pool, for the fold of its counted hits
|
|
template <class Buf, class PoolPtr>
|
|
struct Set {
|
|
struct Span {
|
|
uint64_t lo = 0, hi = 0, base = 0; // (guest addresses; base: the pool's lo, the buffer's offset 0)
|
|
Buf buffer{};
|
|
PoolPtr pool{};
|
|
uint32_t pending = 0; // (hits not yet added to the pool's bindings)
|
|
};
|
|
Key key{};
|
|
Span spans[kSpans];
|
|
int n = 0, last = 0, next = 0; // (in use; the one hit last, asked first; the one replaced next)
|
|
uint64_t hits = 0, misses = 0, fills = 0; // (the thread's own, taken for the status line's sums now and then: take_counts)
|
|
|
|
/// The span holding this read binding under this key, its hit counted; nullptr: the whole path
|
|
Span* find(const Key& k, uint64_t guest, uint64_t size) {
|
|
if (n && k == key) {
|
|
if (inside(spans[last].lo, spans[last].hi, guest, size)) return hits++, spans[last].pending++, &spans[last];
|
|
for (int i = 0; i < n; i++)
|
|
if (inside(spans[i].lo, spans[i].hi, guest, size)) return last = i, hits++, spans[i].pending++, &spans[i];
|
|
}
|
|
misses++;
|
|
return nullptr;
|
|
}
|
|
/// A binding the whole path just gave the pool's mirror: [lo, hi) of it is a span under this key (k.pools_gen is
|
|
/// the table's generation now). fold(pool, hits): the counted hits of a span that goes, called only while the
|
|
/// generation its pool was found under still stands
|
|
template <class Fold>
|
|
void fill(const Key& k, uint64_t lo, uint64_t hi, uint64_t base, Buf buffer, PoolPtr pool, Fold&& fold) {
|
|
if (!(k == key)) {
|
|
const bool pools_stand = k.pools_gen == key.pools_gen;
|
|
for (int i = 0; i < n; i++) {
|
|
if (spans[i].pending && pools_stand) fold(spans[i].pool, spans[i].pending);
|
|
spans[i].pending = 0; // (folded, or dropped: the pool may be gone)
|
|
}
|
|
key = k, n = 0, last = 0, next = 0;
|
|
}
|
|
int at = -1;
|
|
for (int i = 0; i < n && at < 0; i++) // (the same part of its pool again, wider or moved: not a second span)
|
|
if (spans[i].pool == pool && lo < spans[i].hi && spans[i].lo < hi) at = i;
|
|
if (at < 0) at = n < kSpans ? n++ : (next = (next + 1) % kSpans);
|
|
Span& s = spans[at];
|
|
if (s.pending) fold(s.pool, s.pending); // (a span of this key: its pool was found under this generation)
|
|
s = Span{lo, hi, base, buffer, pool, 0};
|
|
last = at, fills++;
|
|
}
|
|
/// (=check) the hit s is resolved the whole way too, which adds to the pool's bindings itself: neither counted as a
|
|
/// span's answer nor folded
|
|
void checked(Span* s) { hits--, s->pending--; }
|
|
/// The thread's counts since they were last taken, once they are worth an add on the shared sums (kTakeAt questions)
|
|
bool take_counts(uint64_t* h, uint64_t* m, uint64_t* f) {
|
|
if (hits + misses < kTakeAt) return false;
|
|
*h = hits, *m = misses, *f = fills, hits = misses = fills = 0;
|
|
return true;
|
|
}
|
|
};
|
|
|
|
} // namespace mkwvk::bindspan
|