mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-25 06:05:56 +00:00
TOTK_SYNC_AHEAD=1 works without shadow storage: the frame wait is answered one frame late with the GPU reading the game's memory in place, as Savage Wind does
With shadow off the back end turns on guards, each with its own switch: the mirror syncs copy the synced pages into staging on the CPU at submit and wait for an earlier write-back into the same range first (TOTK_SYNC_AHEAD_MSYNC), a texture the CPU rewrote takes the CPU deswizzle (TOTK_SYNC_AHEAD_DESWZ), an invalidate of a range not read lately waits for the pending GPU write into it, from a bounded list pruned at completion (TOTK_SYNC_AHEAD_READS), and the frame wait waits up to the value of a one-off small target (Savage Wind's 16-frame, 64-entry rule, TOTK_SYNC_AHEAD_ONESHOT) or, off by default, of a samples-passed report (TOTK_SYNC_AHEAD_QUERIES). TOTK_SYNC_AHEAD_CHECK=1 runs the check-mode detector with the late wait itself. The shadow form and the default are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
df10f20fe4
commit
b558317a7a
10 files changed
+418
-52
No files matched your search
+217
-9
@@ -1373,7 +1373,80 @@ static SRead sread[SREAD_N]; // shadow_lock: the ranges the gam
|
||||
static uint64_t s_read_waits, s_read_ticks; // shadow_lock: since the last log
|
||||
static uint64_t r_read_waits, r_read_ticks, r_read_max; // shadow_lock: over the run (vk_mem_shadow_read_log)
|
||||
|
||||
// ---------------------------------------------------------------- TOTK_SYNC_AHEAD=1 in place (vk_ahead.h)
|
||||
// Without shadow storage a GPU write into CPU-visible memory lands in guest memory on the GPU timeline: directly, or
|
||||
// through the mirror's write-back at the end of its submission (vk_mem_mirror_end). Once the frame wait returns a frame
|
||||
// early those bytes can still be on their way. aw: the writes recorded and not known done, {lo, hi, fence}; pruned
|
||||
// when a value completes (aw_prune), bounded: a full list joins a new write with its nearest entry (wider, never
|
||||
// narrower, so a reader waits for at least what it must). Used by the invalidates below and the staged mirror syncs.
|
||||
#define AW_N 2048
|
||||
typedef struct { uint64_t lo, hi, fence; } AWrite;
|
||||
static PlatMutex aw_lock = PLAT_MUTEX_INIT; // after every other lock of this file
|
||||
static AWrite aw[AW_N];
|
||||
static int naw, aw_peak;
|
||||
static uint64_t aw_joined;
|
||||
// Run totals (aw_lock): invalidate waits, staged sync bytes and calls, CPU time of the copies, waits for an earlier
|
||||
// write-back into a synced range
|
||||
static uint64_t ah_rd_waits, ah_rd_ns, ah_rd_max, ah_stage_bytes, ah_stage_n, ah_stage_ns, ah_ov_waits, ah_ov_ns, ah_ov_max;
|
||||
static uint64_t ah_inplace_bytes; // mirror sync bytes still copied from guest memory on the GPU (TOTK_SYNC_AHEAD_MSYNC=0)
|
||||
|
||||
static void aw_prune(uint64_t done) { // aw_lock
|
||||
int k = 0;
|
||||
for (int i = 0; i < naw; i++)
|
||||
if (aw[i].fence > done) aw[k++] = aw[i];
|
||||
naw = k;
|
||||
}
|
||||
|
||||
static void aw_add(uint64_t lo, uint64_t hi, uint64_t fence) {
|
||||
if (hi <= lo) return;
|
||||
plat_mutex_lock(&aw_lock);
|
||||
for (int i = naw; i-- > 0 && i >= naw - 16;) // the same range again, as the replay notes it draw after draw
|
||||
if (aw[i].lo == lo && aw[i].hi == hi) {
|
||||
if (fence > aw[i].fence) aw[i].fence = fence;
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
return;
|
||||
}
|
||||
if (naw == AW_N) aw_prune(vk_sync_fence_done());
|
||||
if (naw == AW_N) {
|
||||
int m = 0;
|
||||
uint64_t best = UINT64_MAX;
|
||||
for (int i = 0; i < naw; i++) {
|
||||
uint64_t d = aw[i].hi < lo ? lo - aw[i].hi : aw[i].lo > hi ? aw[i].lo - hi : 0;
|
||||
if (d < best) best = d, m = i;
|
||||
}
|
||||
if (lo < aw[m].lo) aw[m].lo = lo;
|
||||
if (hi > aw[m].hi) aw[m].hi = hi;
|
||||
if (fence > aw[m].fence) aw[m].fence = fence;
|
||||
aw_joined++;
|
||||
} else {
|
||||
aw[naw++] = (AWrite){lo, hi, fence};
|
||||
if (naw > aw_peak) aw_peak = naw;
|
||||
}
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
}
|
||||
|
||||
// aw_lock: the newest value in (done, below) that writes into [lo, hi), 0: none
|
||||
static uint64_t aw_writer(uint64_t lo, uint64_t hi, uint64_t done, uint64_t below) {
|
||||
uint64_t w = 0;
|
||||
for (int i = 0; i < naw; i++)
|
||||
if (aw[i].fence > done && aw[i].fence < below && aw[i].fence > w && aw[i].lo < hi && aw[i].hi > lo) w = aw[i].fence;
|
||||
return w;
|
||||
}
|
||||
|
||||
static void ah_time(uint64_t *n, uint64_t *ns, uint64_t *mx, uint64_t dt) {
|
||||
plat_mutex_lock(&aw_lock);
|
||||
(*n)++, *ns += dt;
|
||||
if (mx && dt > *mx) *mx = dt;
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
}
|
||||
|
||||
// The in-place form's vk_mem_shadow_read: the rule of the shadow form (below), with the writer's own value waited for
|
||||
// instead of the guard, which the writer may be far behind of. Only values up to the guard: a write of a value the game
|
||||
// was not told is done is the game reading ahead of its own wait.
|
||||
static void ahead_read(uint64_t addr, uint64_t size);
|
||||
|
||||
void vk_mem_shadow_read(uint64_t addr, uint64_t size) {
|
||||
if (vk_ahead_g.reads && size) ahead_read(addr, size);
|
||||
if (vk_ahead_on) vk_ahead_invalidate(addr, size);
|
||||
if (!shadow_on || !size || !nvn_sync_ahead_guard()) return;
|
||||
plat_mutex_lock(&shadow_lock);
|
||||
@@ -1404,6 +1477,44 @@ void vk_mem_shadow_read(uint64_t addr, uint64_t size) {
|
||||
plat_mutex_unlock(&shadow_lock);
|
||||
}
|
||||
|
||||
static void ahead_read(uint64_t addr, uint64_t size) {
|
||||
uint64_t g = nvn_sync_ahead_guard();
|
||||
if (!g) return;
|
||||
uint64_t done = vk_sync_fence_done();
|
||||
plat_mutex_lock(&shadow_lock); // sread: the shadow form's table, unused without shadow storage
|
||||
uint64_t now = vk_sync_issued();
|
||||
SRead *e = &sread[0];
|
||||
for (int i = 0; i < SREAD_N; i++) {
|
||||
if (sread[i].addr == addr && sread[i].size == size) { e = &sread[i]; break; }
|
||||
if (sread[i].at < e->at) e = &sread[i];
|
||||
}
|
||||
int known = e->addr == addr && e->size == size && e->at + SREAD_AGE >= now;
|
||||
plat_mutex_lock(&aw_lock);
|
||||
aw_prune(done);
|
||||
uint64_t w = aw_writer(addr, addr + size, done, UINT64_MAX), wait = !known && w && w <= g ? w : 0;
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
if (known || !w || wait) *e = (SRead){addr, size, now};
|
||||
plat_mutex_unlock(&shadow_lock);
|
||||
if (!wait) return;
|
||||
uint64_t t0 = qpc();
|
||||
{
|
||||
NVN_BLOCKING();
|
||||
vk_sync_fence_wait(wait, UINT64_MAX);
|
||||
}
|
||||
ah_time(&ah_rd_waits, &ah_rd_ns, &ah_rd_max, qpc() - t0);
|
||||
}
|
||||
|
||||
void vk_mem_ahead_log(void) {
|
||||
plat_mutex_lock(&aw_lock);
|
||||
rt_log("mem: sync-ahead in place: the game waited for a GPU write it reads %llu times, %.1f ms in all, longest "
|
||||
"%.2f ms; mirror syncs staged on the CPU %.1f MiB in %llu prologues, %.1f ms, waited for an earlier "
|
||||
"write-back %llu times, %.1f ms, longest %.2f ms; synced in place %.1f MiB; pending writes at most %d, %llu "
|
||||
"joined", (unsigned long long)ah_rd_waits, qpc_ms(ah_rd_ns), qpc_ms(ah_rd_max), ah_stage_bytes / 1048576.0,
|
||||
(unsigned long long)ah_stage_n, qpc_ms(ah_stage_ns), (unsigned long long)ah_ov_waits, qpc_ms(ah_ov_ns),
|
||||
qpc_ms(ah_ov_max), ah_inplace_bytes / 1048576.0, aw_peak, (unsigned long long)aw_joined);
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
}
|
||||
|
||||
// The run total of the waits above, once at exit, while frame-ahead is in effect: without TOTK_TRACE=perf the shadow
|
||||
// log lines come only at frames 600 and 1800.
|
||||
void vk_mem_shadow_read_log(void) {
|
||||
@@ -1450,6 +1561,10 @@ int vk_mem_shadow_deferred(void) { return ndq; }
|
||||
// have written those shadow bytes since.
|
||||
void vk_mem_gpu_pending(uint64_t addr, uint64_t size) {
|
||||
if (vk_ahead_on) vk_ahead_gpu_write(addr, size, AHW_WRITE, 0);
|
||||
if (vk_ahead_g.lag && size) {
|
||||
uint64_t f = vk_exec_recording_fence();
|
||||
aw_add(addr, addr + size, f ? f : vk_sync_issued() + 1);
|
||||
}
|
||||
if (!shadow_on || !size) return;
|
||||
uint64_t lo = addr, hi = addr + size;
|
||||
plat_mutex_lock_shared(&pool_lock);
|
||||
@@ -2429,17 +2544,111 @@ int vk_mem_mirror_pending(void) {
|
||||
return n;
|
||||
}
|
||||
|
||||
#define STG_BLOCK (64ull << 20) // staging pool blocks (vk_staging)
|
||||
typedef struct { VkBuffer b; VmaAllocation a; } StgFree;
|
||||
static void free_shadow_stg(void *arg) {
|
||||
StgFree *f = arg;
|
||||
vmaDestroyBuffer(vk_ctx()->vma, f->b, f->a);
|
||||
free(f);
|
||||
}
|
||||
|
||||
// TOTK_SYNC_AHEAD=1 in place (vk_ahead_g.msync): the sealed sync ranges a[0..n) are copied out of guest memory into
|
||||
// staging now, on the replay thread, and the GPU copies the staging into the mirrors: the frame wait returns only after
|
||||
// the replay submitted the value (nvn_api_core.c gfx_replay_wait_value), so the GPU gets this frame's bytes even when
|
||||
// it runs the copy after the game wrote the next frame's. A range an earlier value still writes back into (aw) would
|
||||
// lose those GPU words, which a copy on the GPU timeline would have read after them: that value is waited for first
|
||||
// (mirror_lock released meanwhile; the sealed entries stay, as only the prologue takes them). Staging buffers of at
|
||||
// most STG_BLOCK / 2, freed when fence completes. Replay thread, mirror_lock held.
|
||||
static void stage_ranges(VkCommandBuffer cb, MRange *a, int n, uint64_t fence) {
|
||||
uint64_t done = vk_sync_fence_done(), w = 0;
|
||||
plat_mutex_lock(&aw_lock);
|
||||
aw_prune(done);
|
||||
for (int i = 0; i < n && naw; i++) {
|
||||
uint64_t lo = a[i].r->import_lo + a[i].off, x = aw_writer(lo, lo + a[i].size, done, fence);
|
||||
if (x > w) w = x;
|
||||
}
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
if (w) {
|
||||
plat_mutex_unlock(&mirror_lock);
|
||||
uint64_t t0 = qpc();
|
||||
vk_sync_fence_wait(w, UINT64_MAX);
|
||||
ah_time(&ah_ov_waits, &ah_ov_ns, &ah_ov_max, qpc() - t0);
|
||||
plat_mutex_lock(&mirror_lock);
|
||||
a = pend; // the list may have grown meanwhile
|
||||
}
|
||||
uint64_t t0 = qpc(), bytes = 0;
|
||||
qsort(a, (size_t)n, sizeof *a, range_cmp);
|
||||
const uint64_t lim = STG_BLOCK / 2;
|
||||
int i = 0;
|
||||
uint64_t at_i = 0; // bytes of a[i] already staged
|
||||
while (i < n) {
|
||||
uint64_t total = 0, jo = at_i;
|
||||
for (int j = i; j < n && total < lim;) {
|
||||
uint64_t lim_j = a[j].r->import_hi - a[j].r->import_lo, sz = a[j].off >= lim_j ? 0
|
||||
: a[j].off + a[j].size > lim_j ? lim_j - a[j].off : a[j].size;
|
||||
uint64_t left = sz - jo, take = left < lim - total ? left : lim - total;
|
||||
total += take;
|
||||
if (take == left) j++, jo = 0;
|
||||
else jo += take;
|
||||
}
|
||||
if (!total) break;
|
||||
VkBuffer sb;
|
||||
VmaAllocation sa;
|
||||
uint8_t *p = vk_staging(total, &sb, &sa);
|
||||
int k = 0;
|
||||
PoolRec *kr = NULL;
|
||||
for (uint64_t at = 0; at < total;) {
|
||||
PoolRec *r = a[i].r;
|
||||
uint64_t lim_i = r->import_hi - r->import_lo, sz = a[i].off >= lim_i ? 0
|
||||
: a[i].off + a[i].size > lim_i ? lim_i - a[i].off : a[i].size;
|
||||
uint64_t left = sz - at_i, take = left < total - at ? left : total - at;
|
||||
if (take) {
|
||||
if (kr != r && k) vkCmdCopyBuffer(cb, sb, kr->mbuf, (uint32_t)k, mregions), k = 0;
|
||||
kr = r;
|
||||
memcpy(p + at, (const void *)(uintptr_t)(r->import_lo + a[i].off + at_i), take);
|
||||
if (k == cregions) grow((void **)&mregions, &cregions, sizeof *mregions);
|
||||
mregions[k++] = (VkBufferCopy){at, a[i].off + at_i, take};
|
||||
}
|
||||
at += take, at_i += take;
|
||||
if (at_i == sz) i++, at_i = 0;
|
||||
}
|
||||
if (k) vkCmdCopyBuffer(cb, sb, kr->mbuf, (uint32_t)k, mregions);
|
||||
vmaFlushAllocation(vk_ctx()->vma, sa, 0, VK_WHOLE_SIZE);
|
||||
StgFree *f = malloc(sizeof *f);
|
||||
if (!f) rt_fatal("vk: out of memory");
|
||||
f->b = sb, f->a = sa;
|
||||
vk_completion_add(fence, free_shadow_stg, f);
|
||||
bytes += total;
|
||||
mrep.upload_copies += (uint64_t)k;
|
||||
}
|
||||
mrep.upload_bytes += bytes;
|
||||
uint64_t dt = qpc() - t0;
|
||||
plat_mutex_lock(&aw_lock);
|
||||
ah_stage_bytes += bytes, ah_stage_n++, ah_stage_ns += dt;
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
}
|
||||
|
||||
void vk_mem_mirror_prologue(VkCommandBuffer cb, uint64_t fence) {
|
||||
plat_mutex_lock(&mirror_lock);
|
||||
if (!pend_seal && snl_copied == snl_seal) {
|
||||
plat_mutex_unlock(&mirror_lock);
|
||||
return;
|
||||
}
|
||||
if (vk_ahead_on) // the copies read guest memory when the GPU runs them
|
||||
int staged = vk_ahead_g.msync && pend_seal;
|
||||
if (vk_ahead_on && !staged) // the copies read guest memory when the GPU runs them
|
||||
for (int i = 0; i < pend_seal; i++) vk_ahead_read(pend[i].r->import_lo + pend[i].off, pend[i].size, AH_MSYNC, fence);
|
||||
mbarrier(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT,
|
||||
VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT);
|
||||
copy_ranges(cb, pend, pend_seal, 1, &mrep.upload_bytes, &mrep.upload_copies);
|
||||
if (staged) stage_ranges(cb, pend, pend_seal, fence);
|
||||
else {
|
||||
uint64_t b0 = mrep.upload_bytes;
|
||||
copy_ranges(cb, pend, pend_seal, 1, &mrep.upload_bytes, &mrep.upload_copies);
|
||||
if (vk_ahead_g.lag) {
|
||||
plat_mutex_lock(&aw_lock);
|
||||
ah_inplace_bytes += mrep.upload_bytes - b0;
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
}
|
||||
}
|
||||
if (snl_copied < snl_seal) { // the snapshots read what the uploads above wrote
|
||||
if (pend_seal) mbarrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT,
|
||||
VK_ACCESS_2_TRANSFER_READ_BIT);
|
||||
@@ -2550,6 +2759,9 @@ int vk_mem_mirror_end(VkCommandBuffer cb, uint64_t fence) {
|
||||
mbarrier(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT,
|
||||
VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT, VK_ACCESS_2_SHADER_STORAGE_READ_BIT);
|
||||
int m = wb_merge_record(cb);
|
||||
if (m && vk_ahead_g.lag)
|
||||
for (int i = 0; i < nsnl; i++)
|
||||
aw_add(snl[i].r->import_lo + snl[i].off, snl[i].r->import_lo + snl[i].off + snl[i].size, fence);
|
||||
if (m && vk_ahead_on) // the merge writes guest memory when the GPU runs it
|
||||
for (int i = 0; i < nsnl; i++) vk_ahead_read(snl[i].r->import_lo + snl[i].off, snl[i].size, AH_WBM, fence);
|
||||
if (!m) { // no range list: copy them plainly
|
||||
@@ -2571,6 +2783,9 @@ int vk_mem_mirror_end(VkCommandBuffer cb, uint64_t fence) {
|
||||
mbarrier(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT,
|
||||
VK_ACCESS_2_TRANSFER_READ_BIT);
|
||||
for (int i = 0; i < nwbl; i++) wbl[i].r->wb_sub = msub;
|
||||
if (vk_ahead_g.lag)
|
||||
for (int i = 0; i < nwbl; i++)
|
||||
aw_add(wbl[i].r->import_lo + wbl[i].off, wbl[i].r->import_lo + wbl[i].off + wbl[i].size, fence);
|
||||
if (vk_ahead_on)
|
||||
for (int i = 0; i < nwbl; i++) vk_ahead_read(wbl[i].r->import_lo + wbl[i].off, wbl[i].size, AH_WB, fence);
|
||||
mrep.wb_ranges += (uint64_t)nwbl;
|
||||
@@ -2843,7 +3058,6 @@ void vk_upload_end(void) { plat_rmutex_unlock(&cmd_lock); }
|
||||
// the headroom thread (vk_headroom.c) or a burst of uploads held every staging allocation of the render thread: one
|
||||
// took 47-50 ms, for a few bytes, in the first frame after a save load. Larger buffers than a block go to the default
|
||||
// pool.
|
||||
#define STG_BLOCK (64ull << 20)
|
||||
#define STG_MIN_BLOCKS 2
|
||||
static VmaPool stg_pool;
|
||||
static int stg_pool_tried;
|
||||
@@ -2911,12 +3125,6 @@ void *vk_staging(VkDeviceSize size, VkBuffer *buf, VmaAllocation *alloc) {
|
||||
return info.pMappedData;
|
||||
}
|
||||
|
||||
typedef struct { VkBuffer b; VmaAllocation a; } StgFree;
|
||||
static void free_shadow_stg(void *arg) {
|
||||
StgFree *f = arg;
|
||||
vmaDestroyBuffer(vk_ctx()->vma, f->b, f->a);
|
||||
free(f);
|
||||
}
|
||||
|
||||
// Records the deferred shadow copies (vk_mem_shadow_sync queued the ranges): the guest bytes go into staging buffers
|
||||
// now, the GPU copies them into the pools' shadows. The barrier before waits for every earlier command, writes and
|
||||
|
||||
Reference in new issue
Block a user