mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-22 13:05:57 +00:00
The in-place frame ahead merges every mirror write-back by words, is refused with import memory, and the probe checks its pending-write list
Without shadow storage a plain GPU copy back into a mirrored range lands after the game, told the value is done a frame early, may have written other words of that range for the next frame; with TOTK_SYNC_AHEAD_WBMERGE (1 by default) the copies' and updates' write-backs go through the word merge as a shader's do. With memory = import the GPU reads vertex, index and storage data in place, and a check run on the islands saw the game rewrite about 10 KiB of them a frame after the wait, so the switch is refused there unless shadow storage is on. --probe-vkmem checks the pending-write list (newest writer below a bound, pruning, joining when full). The one-off readback rule copied from Savage Wind gets its THIRD_PARTY_NOTICES.md row. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b558317a7a
commit
54b4e092cd
10 files changed
+79
-16
No files matched your search
@@ -51,6 +51,7 @@ void nvn_sync_ahead_present(void);
|
||||
uint64_t nvn_sync_ahead_guard(void);
|
||||
int nvn_sync_ahead_on(void); // 1 when the frame-ahead wait is in effect: TOTK_SYNC_AHEAD=1 and a wait site known for this version
|
||||
int vk_mem_shadow_on(void); // vk_memory.c: 1 with shadow storage on (valid once the Vulkan back end is up)
|
||||
int vk_mem_mirror_on(void); // vk_memory.c: 1 when pools used as buffers get VRAM mirrors (memory = mirror or auto on a discrete GPU)
|
||||
void vk_mem_shadow_fence(void); // vk_memory.c: returns once every shadow sync that read the old guard is done
|
||||
// vk_memory.c: the game is about to read [addr, addr+size) of pool storage; waits for a GPU write into it that the game
|
||||
// was told is done, unless the range still holds the result of the write before
|
||||
|
||||
@@ -509,6 +509,14 @@ static int sync_mode(void) {
|
||||
if (mode < 0) {
|
||||
const char *v = cfg_value("TOTK_SYNC_AHEAD");
|
||||
int on = strcmp(v, "0") != 0 && strcmp(v, "check") != 0, check = !strcmp(v, "check");
|
||||
// In place (no shadow storage) the guards cover the VRAM mirrors' copies; with memory = import the GPU reads
|
||||
// vertex, index and storage data straight from the game's memory, and a check run (TOTK_MEMORY=import,
|
||||
// islands) saw the game rewrite ~10 KiB of them a frame after the wait, particles mostly.
|
||||
if (on && !vk_mem_shadow_on() && !vk_mem_mirror_on()) {
|
||||
rt_log("nvn: TOTK_SYNC_AHEAD needs shadow storage or the VRAM mirrors (memory = import reads the game's "
|
||||
"vertex and storage data in place); the game waits for the GPU as before");
|
||||
on = 0;
|
||||
}
|
||||
if ((on || check) && !frame_wait_lr()) {
|
||||
rt_log("nvn: TOTK_SYNC_AHEAD: no frame wait site is known for this game version; the game waits for the "
|
||||
"GPU as before");
|
||||
|
||||
@@ -376,6 +376,11 @@ static int test_import(pool_t *pool) {
|
||||
GCHECK(!ah, "sync-ahead check self test failed at check %d", ah);
|
||||
printf("probe-vkmem: sync-ahead check: a write after the frame wait into the read set is flagged, one before it is not\n");
|
||||
g_gpu_checks++;
|
||||
int aw = vk_mem_ahead_selftest();
|
||||
GCHECK(!aw, "sync-ahead pending-write list self test failed at check %d", aw);
|
||||
printf("probe-vkmem: sync-ahead in place: the pending-write list names the newest writer below a bound, prunes at "
|
||||
"completion and joins when full\n");
|
||||
g_gpu_checks++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -283,6 +283,7 @@ uint64_t vk_mem_ahead_written(uint64_t lo, uint64_t hi, uint32_t since, void (*f
|
||||
void *arg);
|
||||
int vk_mem_pool_info(uint64_t addr, uint64_t *lo, uint64_t *size, uint32_t *flags, int *mirrored); // 0: no pool
|
||||
void vk_mem_ahead_log(void); // TOTK_SYNC_AHEAD=1 in place: the guards' waits and copies over the run, at exit
|
||||
int vk_mem_ahead_selftest(void); // --probe-vkmem: the in-place form's pending-write list; 0 passed
|
||||
void vk_mem_gpu_wrote(const VkMemSpan *sp, uint64_t size);
|
||||
// A shader's storage-buffer write: goes back through the write-back merge (vk_memory.c), which keeps the words the GPU
|
||||
// did not change.
|
||||
|
||||
+5
-7
@@ -107,6 +107,7 @@ void vk_ahead_init(void) {
|
||||
VkAheadGuards *g = &vk_ahead_g;
|
||||
g->lag = 1;
|
||||
g->msync = rt_knob("SYNC_AHEAD_MSYNC", "1");
|
||||
g->wbmerge = rt_knob("SYNC_AHEAD_WBMERGE", "1");
|
||||
g->deswz = rt_knob("SYNC_AHEAD_DESWZ", "1");
|
||||
g->reads = rt_knob("SYNC_AHEAD_READS", "1");
|
||||
g->oneshot = rt_knob("SYNC_AHEAD_ONESHOT", "1");
|
||||
@@ -114,13 +115,10 @@ void vk_ahead_init(void) {
|
||||
// goes 1 -> 2 or 3 at one update, 2 -> 3 at the next, and the report is read in state 3), by then the frame
|
||||
// wait one frame late has covered that frame. Savage Wind (BOTW reads them right after the wait) has it on Mali.
|
||||
g->queries = rt_knob("SYNC_AHEAD_QUERIES", "0");
|
||||
rt_log("sync-ahead: in place (no shadow storage), memory %s; guards: mirror syncs staged %d, deswizzle of "
|
||||
"rewritten textures on the CPU %d, invalidates wait for the writer %d, one-off small targets %d, "
|
||||
"samples-passed reports %d", vk_mirror_on ? "mirror" : "import", g->msync, g->deswz, g->reads, g->oneshot,
|
||||
g->queries);
|
||||
if (!vk_mirror_on)
|
||||
rt_log("sync-ahead: warning: import memory: vertex, index and storage data are read in place on the GPU, "
|
||||
"no guard covers them");
|
||||
rt_log("sync-ahead: in place (no shadow storage), memory %s; guards: mirror syncs staged %d, write-backs "
|
||||
"merged %d, deswizzle of rewritten textures on the CPU %d, invalidates wait for the writer %d, one-off "
|
||||
"small targets %d, samples-passed reports %d", vk_mirror_on ? "mirror" : "import", g->msync, g->wbmerge,
|
||||
g->deswz, g->reads, g->oneshot, g->queries);
|
||||
}
|
||||
vk_ahead_on = check || (vk_ahead_g.lag && rt_knob("SYNC_AHEAD_CHECK", "0"));
|
||||
if (!vk_ahead_on) return;
|
||||
|
||||
+2
-1
@@ -16,11 +16,12 @@ enum { AHW_WRITE, AHW_COUNTER, AHW_SAMPLES, AHW_EVENT, AHW_N };
|
||||
extern int vk_ahead_on;
|
||||
// The in-place form's switches, read once by vk_ahead_init; every guard is 0 without lag. A/B: TOTK_SYNC_AHEAD_<NAME>.
|
||||
// msync: the mirror syncs copy the synced pages into staging on the CPU at submit (vk_memory.c vk_mem_mirror_prologue);
|
||||
// wbmerge: every GPU write into a mirror goes back through the word merge, not only a shader's (vk_memory.c gpu_wrote);
|
||||
// deswz: a texture re-uploaded after the CPU rewrote it takes the CPU deswizzle (vk_texture.c upload_gpu); reads: an
|
||||
// invalidate of a range not read lately waits for the pending GPU write into it (vk_memory.c vk_mem_shadow_read);
|
||||
// oneshot: a one-off small target is waited for, up to its value (vk_ahead_oneshot); queries (default 0): so is a
|
||||
// samples-passed report.
|
||||
typedef struct { int lag, msync, deswz, reads, oneshot, queries; } VkAheadGuards;
|
||||
typedef struct { int lag, msync, wbmerge, deswz, reads, oneshot, queries; } VkAheadGuards;
|
||||
extern VkAheadGuards vk_ahead_g;
|
||||
void vk_ahead_init(void); // vk_memory.c mirror_mode, once at device start, shadow mode known: reads TOTK_SYNC_AHEAD
|
||||
// The newest value in (lo, hi] whose GPU result the game reads right after the frame wait (0: none): the in-place
|
||||
|
||||
+43
-2
@@ -1504,6 +1504,43 @@ static void ahead_read(uint64_t addr, uint64_t size) {
|
||||
ah_time(&ah_rd_waits, &ah_rd_ns, &ah_rd_max, qpc() - t0);
|
||||
}
|
||||
|
||||
// --probe-vkmem: the pending-write list with values no submission has: the newest overlapping writer below a bound,
|
||||
// pruning at completion, and a full list joining a write into its nearest entry, which still covers it. The list is
|
||||
// restored after. 0: passed, else the failed check.
|
||||
int vk_mem_ahead_selftest(void) {
|
||||
static AWrite saved[AW_N];
|
||||
plat_mutex_lock(&aw_lock);
|
||||
int n0 = naw, peak0 = aw_peak, bad = 0;
|
||||
uint64_t joined0 = aw_joined, f = vk_sync_issued() + 1000000, a = 0x10000000;
|
||||
memcpy(saved, aw, sizeof aw);
|
||||
naw = 0;
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
aw_add(a, a + 0x100, f);
|
||||
aw_add(a + 0x80, a + 0x200, f + 1);
|
||||
aw_add(a, a + 0x100, f); // the same range again: no new entry
|
||||
plat_mutex_lock(&aw_lock);
|
||||
if (naw != 2) bad = 1;
|
||||
else if (aw_writer(a, a + 0x40, 0, UINT64_MAX) != f) bad = 2;
|
||||
else if (aw_writer(a + 0xc0, a + 0xd0, 0, UINT64_MAX) != f + 1) bad = 3;
|
||||
else if (aw_writer(a + 0xc0, a + 0xd0, 0, f + 1) != f) bad = 4; // below the value being recorded
|
||||
else if (aw_writer(a + 0x200, a + 0x300, 0, UINT64_MAX)) bad = 5; // touching, not overlapping
|
||||
if (!bad) {
|
||||
aw_prune(f);
|
||||
if (naw != 1 || aw_writer(a, a + 0x40, f, UINT64_MAX)) bad = 6;
|
||||
}
|
||||
int fill = AW_N - naw; // the entry check 6 left stays: fill up to exactly AW_N, so only the next add joins
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
for (int i = 0; !bad && i < fill; i++) aw_add(a + 0x1000 * (uint64_t)(i + 1), a + 0x1000 * (uint64_t)(i + 1) + 0x10, f + 2);
|
||||
uint64_t lo = a + 0x1000 * (AW_N + 8);
|
||||
if (!bad) aw_add(lo, lo + 0x10, f + 3); // full: joined with the nearest
|
||||
plat_mutex_lock(&aw_lock);
|
||||
if (!bad && (naw != AW_N || aw_joined != joined0 + 1 || aw_writer(lo, lo + 0x10, 0, UINT64_MAX) != f + 3)) bad = 7;
|
||||
memcpy(aw, saved, sizeof aw);
|
||||
naw = n0, aw_peak = peak0, aw_joined = joined0;
|
||||
plat_mutex_unlock(&aw_lock);
|
||||
return bad;
|
||||
}
|
||||
|
||||
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 "
|
||||
@@ -1527,6 +1564,7 @@ void vk_mem_shadow_read_log(void) {
|
||||
}
|
||||
|
||||
int vk_mem_shadow_on(void) { return shadow_on; }
|
||||
int vk_mem_mirror_on(void) { return vk_mirror_on; }
|
||||
|
||||
void vk_mem_shadow_sync(uint64_t fence) {
|
||||
if (!shadow_on) return;
|
||||
@@ -2382,8 +2420,11 @@ static void gpu_wrote(const VkMemSpan *sp, uint64_t size, int shader) {
|
||||
if (r && size) gpu_writes(r, sp->offset, size);
|
||||
if (!r || r->mstate != MS_ON || sp->buffer != r->mbuf || !size) return;
|
||||
plat_mutex_lock(&mirror_lock);
|
||||
// A binding to the end of its pool (no size) can span hundreds of MiB: those stay copies.
|
||||
if (shader && !(sp->offset & 3) && !(size & 3) && size <= MERGE_MAX && merging() && snap_add(r, sp->offset, size)) {
|
||||
// A binding to the end of its pool (no size) can span hundreds of MiB: those stay copies. TOTK_SYNC_AHEAD=1 in place
|
||||
// (vk_ahead_g.wbmerge): the copies' and updates' writes merge too. A plain copy back lands when the GPU runs it, after
|
||||
// the game, told the value is done a frame early, may have written other words of the range for the next frame,
|
||||
// and would put the older words back; the merge writes only the words this value changed.
|
||||
if ((shader || vk_ahead_g.wbmerge) && !(sp->offset & 3) && !(size & 3) && size <= MERGE_MAX && merging() && snap_add(r, sp->offset, size)) {
|
||||
plat_mutex_unlock(&mirror_lock);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user