From 02752376f7a0b3dfde0499f7bcbe7f1b1d3402a9 Mon Sep 17 00:00:00 2001 From: corne-ac <127402474+corne-ac@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:01:05 +0200 Subject: [PATCH 1/2] Sync shadow pools from the per-submission write-watch snapshots Each 2 MiB region is now queried at most once per guest submission, shared with the texture checks, instead of once per pool at every part of a submission. On a Steam Deck at Lookout Landing the replay thread's write tracking fell from about 4.4 to 1.3 ms a frame and the frame rate rose about 7.5%. TOTK_SHADOW_SNAP=0 restores the old queries. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 1 + runtime/cfg.c | 2 +- runtime/vk_memory.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/settings.md b/docs/settings.md index 3fee78a3..182873fa 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -124,6 +124,7 @@ Many rows below are developer tools that only act together with another setting | `dyn_delta` (default `1`) | Records only the pipeline dynamic states (cull mode, depth and stencil tests, vertex input, blend enables, equations and write masks, ...) whose values changed since the last draw in the command buffer, also across pipeline changes; `0` records the full set at every change of pipeline state, as before; `ab` alternates with the `ab` blocks. | Fewer commands per pipeline change on the render and recorder threads; a driver that loses dynamic state across pipeline binds would draw with stale state. `TOTK_TRACE=perf` prints the calls and commands per submission. | Caution | | `feedback` | `0` samples a target that an earlier draw of the same pass wrote with no barrier; `restart` ends and restarts the pass behind a full barrier; otherwise the feedback-loop layout is used when the GPU supports it. | `0` brings back the old behaviour that the barrier fixed, so the picture can break in passes that read a target they draw into; `restart` costs extra passes. | Risky | | `flush_span` (default `0`) | `1` copies one span from the lowest to the highest flushed address of a pool, as an older version did. | Copies far more data to the GPU in crowded towns: an estimated 30-50 ms of GPU time per large copy over PCIe 3.0 x8. | Caution | +| `shadow_snap` (default `1`) | Shadow storage (Linux, Wine): `1` syncs the pools from per-submission write-watch snapshots, each 2 MiB region queried at most once per submission and shared with the texture checks; `0` queries each pool at every part of a submission, as before. | `1` cut the replay thread's write tracking from about 4.4 to 1.3 ms a frame on a Steam Deck; `0` is for comparison runs. No effect where shadow storage is off (Windows by default). | Caution | | `freeze_idle` (default `60`) | Number of submissions a 2 MiB region stays unwritten before it is made read-only; `0` turns the freeze off. | A larger value keeps more regions writable and costs more write tracking; a write to a frozen region costs about 60 microseconds. | Caution | | `gpl_draw_build` (default `1`) | `0` leaves the missing shader libraries of a waiting draw to its background job instead of building them in parallel at once. | Draws whose pipeline is not ready are skipped meanwhile, until the job finishes. | Caution | | `lib_prebuild_keep` (default `0`) | Number of recorded graphics shader library variants that stay in memory after the boot prebuild. | Each kept library costs 140-460 KB of memory; the driver cache already makes a rebuild cheap. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index a660c26c..396106d1 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -56,7 +56,7 @@ static const char *const known[] = { "RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", - "SHADER_MEMO", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", + "SHADER_MEMO", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHADOW_SNAP", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UB_OVERLAY_OUT", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 3e30682f..449f6b02 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -953,9 +953,40 @@ static void shadow_copy(PoolRec *r, uint64_t a, uint64_t b, int i) { s_copied += b - a; } +// TOTK_SHADOW_SNAP (default 1): a pool's sync takes its written pages from the per-submission write-watch snapshots +// (snap_regions, shared with the texture checks): each 2 MiB region is queried at most once per guest submission, however +// many pools share it and however many parts of the submission touch them. The GPU then reads a pool as of the first +// part that syncs it, nearer the submit call, the moment the game means (vk_usnap.c). 0 queries each pool at every +// sync, as before. Measured on a Steam Deck at Lookout Landing: 716 pool syncs and 729 queries a frame took 4.8 ms of +// the replay thread to copy 2.3 MiB, the query walk costing about 4 us per MiB there; with the snapshots, about 58 +// queries and 1.7 ms for the same bytes. +static int shadow_snap = -1; +static void shadow_copy_newer(PoolRec *r, uint32_t since); + // shadow_lock and gen_lock held: stamps the pool's written pages and copies those newer than its shadow. static void shadow_sync_pool(PoolRec *r) { uint64_t lo = r->import_lo, hi = r->import_hi; + if (shadow_snap < 0) { + shadow_snap = rt_knob("SHADOW_SNAP", "1"); + rt_log("mem: shadow sync: %s (TOTK_SHADOW_SNAP)", + shadow_snap ? "per-submission region snapshots" : "a query per pool at every part"); + } + uint32_t epoch = shadow_snap ? vk_texture_epoch() : 0; + uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT; + if (epoch && r1 < SNAP_REGIONS) { + s_queries += snap_regions(r0, r1, epoch); // stamps what the snapshot found, if the region was not seen yet + int untracked = 0; + for (uint64_t k = r0; k <= r1; k++) untracked |= snap_untracked[k]; + uint32_t since = r->sgen; + r->sgen = gen_now; + if (untracked) { // nothing tells which pages changed: all of them + s_untracked++; + shadow_copy(r, lo, hi, 0); + return; + } + shadow_copy_newer(r, since); + return; + } uint32_t g = ++gen_now; int tracked = 1; for (uint64_t a = lo; a < hi;) { // one query per run of regions that are not frozen @@ -974,6 +1005,12 @@ static void shadow_sync_pool(PoolRec *r) { shadow_copy(r, lo, hi, 0); return; } + shadow_copy_newer(r, since); +} + +// shadow_lock and gen_lock held: copies the pool's pages stamped after since into its shadow, in runs. +static void shadow_copy_newer(PoolRec *r, uint32_t since) { + uint64_t lo = r->import_lo, hi = r->import_hi; uint64_t run = 0; for (uint64_t k = lo >> REG_SHIFT; k <= (hi - 1) >> REG_SHIFT; k++) { uint64_t a = k << REG_SHIFT, b = a + (1ull << REG_SHIFT); From a733b946459a7926b803dafed148229871e8e082 Mon Sep 17 00:00:00 2001 From: corne-ac <127402474+corne-ac@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:20:03 +0200 Subject: [PATCH 2/2] Keep shadow_snap off by default under Wine until it has been measured there Shadow storage is on by default under Wine as well, and the region snapshots were only measured on a native Linux host. TOTK_SHADOW_SNAP=1 still turns them on under Wine. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 2 +- runtime/vk_memory.c | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 182873fa..9c7feca6 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -124,7 +124,7 @@ Many rows below are developer tools that only act together with another setting | `dyn_delta` (default `1`) | Records only the pipeline dynamic states (cull mode, depth and stencil tests, vertex input, blend enables, equations and write masks, ...) whose values changed since the last draw in the command buffer, also across pipeline changes; `0` records the full set at every change of pipeline state, as before; `ab` alternates with the `ab` blocks. | Fewer commands per pipeline change on the render and recorder threads; a driver that loses dynamic state across pipeline binds would draw with stale state. `TOTK_TRACE=perf` prints the calls and commands per submission. | Caution | | `feedback` | `0` samples a target that an earlier draw of the same pass wrote with no barrier; `restart` ends and restarts the pass behind a full barrier; otherwise the feedback-loop layout is used when the GPU supports it. | `0` brings back the old behaviour that the barrier fixed, so the picture can break in passes that read a target they draw into; `restart` costs extra passes. | Risky | | `flush_span` (default `0`) | `1` copies one span from the lowest to the highest flushed address of a pool, as an older version did. | Copies far more data to the GPU in crowded towns: an estimated 30-50 ms of GPU time per large copy over PCIe 3.0 x8. | Caution | -| `shadow_snap` (default `1`) | Shadow storage (Linux, Wine): `1` syncs the pools from per-submission write-watch snapshots, each 2 MiB region queried at most once per submission and shared with the texture checks; `0` queries each pool at every part of a submission, as before. | `1` cut the replay thread's write tracking from about 4.4 to 1.3 ms a frame on a Steam Deck; `0` is for comparison runs. No effect where shadow storage is off (Windows by default). | Caution | +| `shadow_snap` (default `1`) | Shadow storage (Linux, Wine): `1` syncs the pools from per-submission write-watch snapshots, each 2 MiB region queried at most once per submission and shared with the texture checks; `0` queries each pool at every part of a submission, as before. | `1` cut the replay thread's write tracking from about 4.4 to 1.3 ms a frame on a Steam Deck; `0` is for comparison runs. Under Wine/Proton the default is `0` until it has been measured there; `1` turns it on. No effect where shadow storage is off (Windows by default). | Caution | | `freeze_idle` (default `60`) | Number of submissions a 2 MiB region stays unwritten before it is made read-only; `0` turns the freeze off. | A larger value keeps more regions writable and costs more write tracking; a write to a frozen region costs about 60 microseconds. | Caution | | `gpl_draw_build` (default `1`) | `0` leaves the missing shader libraries of a waiting draw to its background job instead of building them in parallel at once. | Draws whose pipeline is not ready are skipped meanwhile, until the job finishes. | Caution | | `lib_prebuild_keep` (default `0`) | Number of recorded graphics shader library variants that stay in memory after the boot prebuild. | Each kept library costs 140-460 KB of memory; the driver cache already makes a rebuild cheap. | Caution | diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 449f6b02..c4cd6065 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -953,7 +953,7 @@ static void shadow_copy(PoolRec *r, uint64_t a, uint64_t b, int i) { s_copied += b - a; } -// TOTK_SHADOW_SNAP (default 1): a pool's sync takes its written pages from the per-submission write-watch snapshots +// TOTK_SHADOW_SNAP (default 1 on a native host, 0 under Wine, where it has not been measured): a pool's sync takes its written pages from the per-submission write-watch snapshots // (snap_regions, shared with the texture checks): each 2 MiB region is queried at most once per guest submission, however // many pools share it and however many parts of the submission touch them. The GPU then reads a pool as of the first // part that syncs it, nearer the submit call, the moment the game means (vk_usnap.c). 0 queries each pool at every @@ -967,9 +967,10 @@ static void shadow_copy_newer(PoolRec *r, uint32_t since); static void shadow_sync_pool(PoolRec *r) { uint64_t lo = r->import_lo, hi = r->import_hi; if (shadow_snap < 0) { - shadow_snap = rt_knob("SHADOW_SNAP", "1"); - rt_log("mem: shadow sync: %s (TOTK_SHADOW_SNAP)", - shadow_snap ? "per-submission region snapshots" : "a query per pool at every part"); + shadow_snap = rt_knob("SHADOW_SNAP", plat_is_wine() ? "0" : "1"); + rt_log("mem: shadow sync: %s (TOTK_SHADOW_SNAP%s)", + shadow_snap ? "per-submission region snapshots" : "a query per pool at every part", + plat_is_wine() ? "; off by default under Wine" : ""); } uint32_t epoch = shadow_snap ? vk_texture_epoch() : 0; uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT;