diff --git a/runtime/cfg.c b/runtime/cfg.c index f355b982..b122690a 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -50,7 +50,7 @@ static const char *const known[] = { "SHADER_MEMO", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "SWKBD_ANSWER", "SWKBD_TEST", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TIMER_1MS", "TRACE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_VERTEX", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", - "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", + "WB_FENCE_LIMIT", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", }; // The totk.ini section a key cfg_set may write goes to when the file lacks it (totk.ini.example's sections). @@ -79,6 +79,7 @@ static const struct { const char *key, *section; } sections[] = { {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"}, {"VK_DYN_VERTEX", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, }; // The settings the menu shows: the value in effect when the key is unset, and what the key accepts. The only place a @@ -126,6 +127,7 @@ static const CfgMeta metas[] = { {"VK_SUBGROUP_PIN", "1", "0|1", RESTART}, // diagnostics {"TRACE", "", NULL, RESTART}, {"FLIGHT", "0", "0|1", RESTART}, {"FLIGHT_SPIKE_MS", "100", "0..60000", RESTART}, + {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"INPUT_RECORD", "", NULL, RESTART}, }; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index b65f21e7..7b68ffe2 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -841,9 +841,22 @@ const VkTexImage *vk_texture_get(const tex_t *t) { } // Small render targets the CPU reads without NVN's invalidate (vk_texture_write_back_small); small_pending: one may be -// dirty. -enum { WB_SMALL = 64 << 10 }; +// dirty. wb_limit is the largest target (bytes) that goes back when the CPU waits for a fence: TOTK_WB_FENCE_LIMIT, +// read once. wb_trace: TOTK_TRACE=gfx, read together with it. +enum { WB_SMALL_DEFAULT = 64 << 10 }; static int small_pending; +static uint64_t wb_limit = WB_SMALL_DEFAULT; +static int wb_trace; +static int wb_cfg_read; + +static void wb_cfg_init(void) { + if (wb_cfg_read) return; + int v = cfg_int("TOTK_WB_FENCE_LIMIT"); + wb_limit = v < 0 ? 0 : (uint64_t)v; + wb_trace = (rt_trace & RT_TRACE_GFX) != 0; + wb_cfg_read = 1; + if (wb_limit != WB_SMALL_DEFAULT) rt_log("vk: fence write-back limit %llu bytes", (unsigned long long)wb_limit); +} void vk_texture_gpu_written(const tex_t *t) { if (!t || !t->mem) return; @@ -860,7 +873,8 @@ void vk_texture_gpu_written(const tex_t *t) { storage_rewritten(e, 0, &e->gpu_gen); // CPU writes up to here are older than this render e->gpu_seq = ++gpu_writes; e->gpu_fence = vk_exec_recording_fence(); - if (e->t.L.size <= WB_SMALL) __atomic_store_n(&small_pending, 1, __ATOMIC_RELEASE); + wb_cfg_init(); + if (e->t.L.size <= wb_limit) __atomic_store_n(&small_pending, 1, __ATOMIC_RELEASE); own_add(e); } } @@ -1367,24 +1381,54 @@ void vk_texture_write_back(uint64_t addr, uint64_t size) { // TotK's grass system renders a 128x128 RGBA8 map of cut and trampled grass into a CPU-cached pool every frame and its // CPU code reads the pool bytes without nvnMemoryPoolInvalidateMappedRange (cut reactions: blades, insects, the cut // itself). On the Switch the bytes are there once the frame's fence has passed. So when the -// CPU waits for a fence, mirrored targets up to WB_SMALL bytes rendered by work up to that fence go back to their +// CPU waits for a fence, mirrored targets up to wb_limit bytes rendered by work up to that fence go back to their // storage, unless the CPU rewrote it since. Larger targets still go back on an invalidate, memcpy or ReadTexels. int vk_texture_small_dirty(void) { return __atomic_load_n(&small_pending, __ATOMIC_ACQUIRE); } +// TOTK_TRACE=gfx: a dirty target the fence write-back leaves alone because it is over the limit (what a CPU read +// after the wait would miss), or sends back although it is over the default limit (what a raised limit changed). One +// line per target address and outcome; the table is small and a full one stops logging. +static void wb_note(const Tex *e, int skipped) { + static uint64_t seen[2][64]; + static int n_seen[2]; + for (int i = 0; i < n_seen[skipped]; i++) + if (seen[skipped][i] == e->pub.addr) return; + if (n_seen[skipped] == 64) return; + seen[skipped][n_seen[skipped]++] = e->pub.addr; + const pool_t *p = e->t.pool; + unsigned fl = p ? p->flags : 0; + rt_log("vk: fence write-back %s %#llx %ux%u fmt %#x size %llu pool %s (limit %llu)", + skipped ? "skipped-for-size" : "over-default", (unsigned long long)e->pub.addr, e->t.d.width, e->t.d.height, + e->t.d.format, (unsigned long long)e->t.L.size, + !p ? "none" : (fl & NVN_MEMORY_POOL_FLAGS_CPU_CACHED) ? "cpu-cached" : (fl & NVN_MEMORY_POOL_FLAGS_CPU_UNCACHED) ? "cpu-uncached" : "other", + (unsigned long long)wb_limit); +} + void vk_texture_write_back_small(uint64_t fence) { lock(); + wb_cfg_init(); __atomic_store_n(&small_pending, 0, __ATOMIC_RELAXED); AcquireSRWLockShared(&owned_lock); int n = 0, later = 0; - for (int i = 0; i < nowned; i++) n += owned[i]->gpu_dirty && owned[i]->t.L.size <= WB_SMALL; + for (int i = 0; i < nowned; i++) n += owned[i]->gpu_dirty && owned[i]->t.L.size <= wb_limit; Tex **hit = n ? malloc(sizeof *hit * (size_t)n) : NULL; if (n && !hit) rt_fatal("vk: out of memory for a texture write-back"); n = 0; for (int i = 0; i < nowned; i++) { Tex *e = owned[i]; - if (!e->gpu_dirty || e->t.L.size > WB_SMALL) continue; + if (!e->gpu_dirty) continue; + if (e->t.L.size > wb_limit) { + if (wb_trace && e->gpu_fence <= fence) { + wb_note(e, 1); + perf_count(PERF_CNT_WB_SKIP); + } + continue; + } if (e->gpu_fence > fence) later = 1; - else hit[n++] = e; + else { + if (wb_trace && e->t.L.size > WB_SMALL_DEFAULT) wb_note(e, 0); + hit[n++] = e; + } } ReleaseSRWLockShared(&owned_lock); if (later) __atomic_store_n(&small_pending, 1, __ATOMIC_RELEASE); diff --git a/totk.ini.example b/totk.ini.example index d55d52e9..411871e2 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -180,3 +180,8 @@ # takes longer than flight_spike_ms (0 to 60000). # flight = 0 # flight_spike_ms = 100 +# Largest render target (bytes, 0 to 1073741824) that goes back to its game memory when the game waits for a GPU fence. +# A game that reads a larger target without invalidating it sees stale bytes at the default. With trace = gfx the log +# names each dirty target over the limit that stays on the GPU ("fence write-back skipped-for-size") and each one over +# 65536 bytes that went back ("over-default"). Raising it costs a copy per such target and wait. +# wb_fence_limit = 65536