Triforce v0.3.3

This commit is contained in:
ruckus7382sfgsgdf
2026-11-12 10:54:34 -06:00
parent afbb0d140a
commit 83b5be58d6
37 changed files with 5438 additions and 145 deletions

View File

@@ -78,7 +78,25 @@ std::unordered_map<uint64_t, uint64_t> q_last; // queue -> its last submission
std::map<uint64_t, uint64_t> value_queue; // a submission's value -> its queue (sync_order_mu)
std::atomic<uint64_t> pushed{0};
std::atomic<uint64_t> q_values{0}; // the fence values handed out (a submit or a present takes one each)
std::mutex replay_mu; // one replay at a time (the game submits from several threads)
::mkwvk::rec::DrainingMutex replay_mu; // one replay at a time (the game submits from several threads; RX_VK_RECORDER=defer: what
// a thread recorded is drained before another may take it)
// (RX_VK_RECORDER=thread: the recorder thread named, and above normal priority as par_worker_main's threads)
// RX_VK_RECORDER_PIN (experiment): next = the physical core below the reserved one (the replay thread's: hos_reserved_cpus),
// sibling = the reserved core itself (shares it with the replay thread), or a hex mask; unset: not pinned
const bool rec_thread_hook = (::mkwvk::rec::g_thread_start_hook = [] {
SetThreadDescription(GetCurrentThread(), L"mkw_vk recorder");
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
const char* pin = getenv("RX_VK_RECORDER_PIN");
if (!pin || !*pin) return;
const uint64_t reserved = host && host->reserved_cpus ? host->reserved_cpus() : 0;
uint64_t m = 0;
if (!strcmp(pin, "next")) m = reserved ? reserved >> __builtin_popcountll(reserved) : 0;
else if (!strcmp(pin, "sibling")) m = reserved;
else m = strtoull(pin, nullptr, 16);
const bool ok = m && SetThreadAffinityMask(GetCurrentThread(), (DWORD_PTR)m) != 0;
::mkwvk::logf("vk: RX_VK_RECORDER_PIN=%s: the recorder %s %#llx (reserved %#llx)", pin, ok ? "pinned to" : "NOT pinned, mask",
(unsigned long long)m, (unsigned long long)reserved);
}, true);
// (RX_VK_BATCH: see queue_flush_locked)
VkSemaphore batch_tl = VK_NULL_HANDLE;
uint64_t batch_next = 0, batch_flushed = 0;
@@ -1723,8 +1741,13 @@ void bench_frame(int64_t presents) {
struct SlowWatch {
std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
int64_t prof0[PROF_COUNT];
int64_t ms0[4]; // (hookshot: memory sync's parts and the lock wait, at the start)
const char* what;
explicit SlowWatch(const char* w) : what(w) { std::memcpy(prof0, prof_ns, sizeof prof0); }
explicit SlowWatch(const char* w) : what(w) {
std::memcpy(prof0, prof_ns, sizeof prof0);
for (int i = 0; i < 3; i++) ms0[i] = memsync_part_ns[i].load(std::memory_order_relaxed);
ms0[3] = memsync_lock_wait_ns.load(std::memory_order_relaxed);
}
~SlowWatch() {
const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
static int logged;
@@ -1739,6 +1762,15 @@ struct SlowWatch {
const double d = (prof_ns[i] - prof0[i]) / 1e6;
if (d >= 1.0) k += snprintf(line + k, sizeof line - k, " %s %.1f", names[i], d);
}
{ // (hookshot: which part of "memory sync" it was: the pools' refresh [and its lock waits], the capture, the written pages collected.
// Counted across threads, so a part another thread spent meanwhile shows too)
const double a = (memsync_part_ns[0].load(std::memory_order_relaxed) - ms0[0]) / 1e6,
b = (memsync_part_ns[1].load(std::memory_order_relaxed) - ms0[1]) / 1e6,
c = (memsync_part_ns[2].load(std::memory_order_relaxed) - ms0[2]) / 1e6,
w = (memsync_lock_wait_ns.load(std::memory_order_relaxed) - ms0[3]) / 1e6;
if (a + b + c >= 1.0 || w >= 1.0)
k += snprintf(line + k, sizeof line - k, " [memory sync: maps %.1f (lock waits %.1f), capture %.1f, collect %.1f]", a, w, b, c);
}
logf("%s", line);
}
};
@@ -1749,6 +1781,8 @@ void submit_memory_work(uint64_t value) {
PROF(PROF_MEMSYNC);
const auto m0 = std::chrono::steady_clock::now();
memory_sync();
const auto m1a = std::chrono::steady_clock::now();
memsync_part_ns[0] += std::chrono::duration_cast<std::chrono::nanoseconds>(m1a - m0).count();
// (the capture takes only the mirrored pools' pages, the collection the rest: skipping it whenever a capture was
// taken, which with the replay thread is at every submit, left every other CPU write uncollected. Textures never
// saw them: MK World's font cache kept its first glyphs (the menus lost letters), CPU-streamed terrain went stale)
@@ -1757,6 +1791,8 @@ void submit_memory_work(uint64_t value) {
const auto m2 = std::chrono::steady_clock::now();
memory_collect_written();
const auto m3 = std::chrono::steady_clock::now();
memsync_part_ns[1] += std::chrono::duration_cast<std::chrono::nanoseconds>(m2 - m1).count();
memsync_part_ns[2] += std::chrono::duration_cast<std::chrono::nanoseconds>(m3 - m2).count();
{ // (every 600 submits: what the memory work before a submit's recording cost)
static double a, b, c;
static int64_t k;