From 5aaa718eb66317b895eccde9af876a9896714424 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 24 Nov 2026 09:04:48 +0300 Subject: [PATCH 1/2] barrier_merge ends the pass when a draw samples a target written in the pass that it does not write itself The by-region barrier inside a merged pass orders only the pixel a fragment writes. A draw that reads a target other draws wrote, without writing it, may read neighbouring pixels (blur, ambient occlusion, light composites), which only the game's full Barrier orders on every driver. Such a draw now ends the pass, as Savage Wind does for the same case. TOTK_TRACE=gfx counts these restarts by colour and depth. Shrine, town and field on 1.4.3: 0 restarts; every read after a merged Barrier comes from a draw that writes the target it samples. Co-Authored-By: Claude Opus 5.5 --- runtime/vk_exec.c | 4 ++-- runtime/vk_exec.h | 2 ++ runtime/vk_exec_draw.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index be372f99..64fdda93 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -90,12 +90,12 @@ void vk_exec_frame_log(const tex_t *p, uint64_t frame) { fw[i].h, fw[i].fmt, fw[i].n); } rt_log("frame %llu: submits %llu passes %llu draws %llu/%llu clears %llu copies %llu dispatches %llu; present %p " - "%ux%u/%#x written %d; pass ends: targets %llu barrier %llu (%llu merged, %llu undone) bits %llu/%llu/%llu/%llu/%llu/%llu/%llu/%llu; feedback draws %llu barriers %llu restarts %llu layouts %llu; targets%s", + "%ux%u/%#x written %d; pass ends: targets %llu barrier %llu (%llu merged, %llu undone, unwritten reads %llu colour %llu depth) bits %llu/%llu/%llu/%llu/%llu/%llu/%llu/%llu; feedback draws %llu barriers %llu restarts %llu layouts %llu; targets%s", (unsigned long long)frame, (unsigned long long)f->submits, (unsigned long long)f->passes, (unsigned long long)f->draws, (unsigned long long)f->draws_seen, (unsigned long long)f->clears, (unsigned long long)f->copies, (unsigned long long)f->dispatches, p ? (const void *)p->mem : NULL, p ? p->d.width : 0, p ? p->d.height : 0, p ? (uint32_t)p->d.format : 0, - hit, (unsigned long long)f->pass_end_rt, (unsigned long long)f->pass_end_barrier, (unsigned long long)f->barrier_merged, (unsigned long long)f->barrier_undone, (unsigned long long)f->barrier_bit[0], (unsigned long long)f->barrier_bit[1], (unsigned long long)f->barrier_bit[2], (unsigned long long)f->barrier_bit[3], (unsigned long long)f->barrier_bit[4], (unsigned long long)f->barrier_bit[5], (unsigned long long)f->barrier_bit[6], (unsigned long long)f->barrier_bit[7], (unsigned long long)f->fb_draws, (unsigned long long)f->fb_barriers, (unsigned long long)f->fb_restarts, + hit, (unsigned long long)f->pass_end_rt, (unsigned long long)f->pass_end_barrier, (unsigned long long)f->barrier_merged, (unsigned long long)f->barrier_undone, (unsigned long long)f->undone_color, (unsigned long long)f->undone_depth, (unsigned long long)f->barrier_bit[0], (unsigned long long)f->barrier_bit[1], (unsigned long long)f->barrier_bit[2], (unsigned long long)f->barrier_bit[3], (unsigned long long)f->barrier_bit[4], (unsigned long long)f->barrier_bit[5], (unsigned long long)f->barrier_bit[6], (unsigned long long)f->barrier_bit[7], (unsigned long long)f->fb_draws, (unsigned long long)f->fb_barriers, (unsigned long long)f->fb_restarts, (unsigned long long)f->fb_layouts, list); memset(f, 0, sizeof *f); nfw = 0; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 33680bac..6bf1c9fe 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -57,6 +57,8 @@ typedef struct { uint64_t barrier_bit[8]; // the game's Barrier calls that carry NVN barrier bit 0..7 (TOTK_TRACE=gfx) uint64_t barrier_merged; // the game's Barrier calls the open pass went on through (barrier_merge) uint64_t barrier_undone; // passes a draw then ended for a read the merge cannot order (merge_guard) + uint64_t undone_color, undone_depth; // of those, for sampling a colour target / the depth target the draw does + // not write itself (vk_exec_draw.c merge_unwritten_reads) uint64_t pass_end_rt, pass_end_barrier; // passes ended by other render targets, by the game's own Barrier (TOTK_TRACE=gfx) uint64_t fb_draws, fb_barriers, fb_restarts, fb_layouts; // feedback loops: draws sampling a written target of // their pass, in-pass barriers, pass restarts, layout diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 5bd62d56..433dfb6f 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -3128,6 +3128,23 @@ static int merge_guard(VkExec *ex, const GfxShader *vs, const GfxShader *fs) { return m != 0; } +// barrier_merge: the attachments of fbl (sampled in the feedback layout) that a draw with state s reads after other +// draws of the pass wrote them, without writing them itself. A read of a target the draw also writes (G-buffer bits +// ORed in, blending in the shader) is a read of its own pixel, which the by-region barrier orders. A read of a target +// the draw leaves alone is a read of what earlier draws left there, often at neighbouring pixels (blur, ambient +// occlusion, light composites over the G-buffer, depth-based effects), which only the game's Barrier, a full one, +// orders on every driver; the caller ends the pass for it. The rule is Savage Wind's (runtime/nvn/vulkan/exec.c, the +// sampled-texture loop of the draw: a colour target the draw does not write ends the pass when it was written in the +// pass, a read-only depth target when depth was), which that port runs on every GPU. A colour target counts as +// written when the pipeline's write mask for it is not zero, depth as feedback_written counts it. +static uint32_t merge_unwritten_reads(const VkExec *ex, const VkPipeState *s, uint32_t fbl) { + uint32_t r = fbl & ex->fb_dirty; + for (uint32_t i = 0; r & 0xff && i < ex->pass_ncolor && i < 8; i++) + if ((r >> i & 1) && vk_pipeline_target_wmask(s, (int)i)) r &= ~(1u << i); + if ((r & 0x100) && ((s->ds.dt && s->ds.dw) || s->ds.st)) r &= ~0x100u; + return r; +} + // ---- incremental draw preparation (TOTK_BIND_INC) // A draw is prepared from what changed since the context's last prepared draw in the same command buffer: the bind // ops mark the slots whose bytes they changed (VkExec.dirt, vk_exec_bind), every write that changes the pipeline state @@ -3450,6 +3467,18 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { if (memcmp(&s, &D->PM.s, sizeof s)) bi_mismatch(ex, BIM_PIPE, "pipeline memo: state differs from the memo its generation matched"); } } + // barrier_merge: a read of a target this draw does not write, made after a merged Barrier, ends the pass; nothing of + // the draw is recorded yet, and the new pass keeps the targets and their feedback layout, so fbl and the state hold. + if (ex->pass_merged && fbl & ex->fb_dirty) { + uint32_t r = merge_unwritten_reads(ex, sp, fbl); + if (r) { + vk_exec_pass_end(ex); + vk_exec_pass_begin(ex); + ex->fr.barrier_undone++; + if (r & 0xff) ex->fr.undone_color++; + else ex->fr.undone_depth++; + } + } const VkPipeInfo *info = NULL; int pending; // A draw of at most 6 vertices over half the render area or more (a full-screen triangle or quad, a tile) waits for From dca69306fbd72878d85e7626bffbbf3007001785 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 24 Nov 2026 09:15:23 +0300 Subject: [PATCH 2/2] barrier_merge is on by default With the unwritten-read rule every read that stays merged comes from a draw that samples a target it also writes, the case Savage Wind runs the same way on every GPU. Shrine on the RDNA2 iGPU (AMD Windows driver, 1080p): passes 581 -> 128 a frame, GPU time unchanged (37.2 ms off, 38.0 and 36.5 ms on), the picture the same. PR #1237 measured the waterfall on RADV at 1105 -> 117 passes and GPU 8.3 -> 4.1 ms, and an RTX 4080 shrine at +3% mean fps with the 1% low 83 -> 98. barrier_merge = 0 brings back a full barrier at each such Barrier. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 2 +- runtime/cfg.c | 2 +- runtime/vk_exec.c | 14 +++++++++----- totk.ini.example | 18 ++++++++++-------- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 19ff6519..2e1d1c8a 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -28,7 +28,7 @@ Many rows below are developer tools that only act together with another setting | `bind_stats` | `1` counts bindings and descriptors per draw of each stage and how much of the bound state changes between draws. | Adds per-draw bookkeeping to the render thread and extra log output; frames get slower. | Caution | | `contact_debug` | `1` draws the contact-shadow factor in grey in place of the picture; `2` draws the sun visibility the shadows are scaled by (white in the sun, black in shade and underground). | The frame shows a grey debug image while it is set; has no effect when contact shadows are off. | Caution | | `core_diag` | `1` records why ready guest threads wait for a virtual core and for how long, with a histogram every 60 presents. | Turns on host-call and SVC depth tracking and writes many log lines (one per present over 25 ms); frames get slower. | Caution | -| `barrier_merge` (default `0`) | Experimental. `1` keeps a render pass open through the game's Barrier calls that ask only for fragment ordering and texture invalidation, when no draw of the pass can write a storage buffer or image; the next draw that samples a written target gets a by-region barrier in the pass (the feedback layout). In scenes with about a thousand such barriers a frame this cuts the passes from about 1200 to 130 and the GPU time from 24.5 to 7.8 ms. | The game's barrier orders every pixel, the in-pass by-region barrier only the pixel a draw writes: a driver that runs it per screen tile can show flicker or squares at the edges of water, decals or particles. | Caution | +| `barrier_merge` (default `1`) | `1` keeps a render pass open through the game's Barrier calls that ask only for fragment ordering and texture invalidation, when no draw of the pass can write a storage buffer or image; the next draw that samples a target it also writes gets a by-region barrier in the pass (the feedback layout), and a draw that samples a written target without writing it ends the pass. In scenes with about a thousand such barriers a frame this cuts the passes from about 1200 to 130 and, on RADV, the GPU time from 24.5 to 7.8 ms; AMD's Windows driver shows no GPU time change. `0` ends the pass at every such Barrier. | The game's barrier orders every pixel, the in-pass by-region barrier only the pixel a draw writes: a driver that runs it per screen tile could show flicker or squares at the edges of water, decals or particles; `0` rules that out. | Caution | | `menu_cursor_log` | `1` logs what the menu cursor code reads at every screen change. | Only adds log lines. | Safe | | `plugin_tick_offset` (default `0`) | Added to every GetSystemTick in the plugin service run (`totk.exe --plugin-init DIR`), so `tools/exefs_plugins.py` can run a plugin's init twice with other ticks and compare the records. | Has no effect outside `--plugin-init`; a plugin's init at the start of the game gets the plain ticks. | Safe | | `plugin_trace` (default `0`) | `1` logs the first call of each entry of a mod plugin's hooks (its callbacks, replace and call targets) as `plugins: hook entry ... reached`. | Each such call goes through a counting step; nothing changes without a mod plugin. | Safe | diff --git a/runtime/cfg.c b/runtime/cfg.c index 824fa9ab..9833dddf 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -222,7 +222,7 @@ static const CfgMeta metas[] = { {"FREE_CORES", "0", "0|1", RESTART}, {"TIMER_1MS", "1", "0|1", RESTART}, // shaders and pipelines {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "fullscreen", "strict|any|fullscreen", RESTART}, - {"BARRIER_MERGE", "0", "0|1", RESTART}, {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, + {"BARRIER_MERGE", "1", "0|1", RESTART}, {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART}, // textures and memory {"ASTC", "auto", "auto|gpu|native|cpu", RESTART}, {"MEMORY", "auto", "auto|import|mirror|verify", RESTART}, diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 64fdda93..e971c92c 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -984,7 +984,7 @@ static void clear_ds(VkExec *ex, const p_clear_ds *c) { // ---- the op loop -// barrier_merge (TOTK_BARRIER_MERGE, off by default): TotK calls Barrier(ORDER_FRAGMENTS | INVALIDATE_TEXTURE) about +// barrier_merge (TOTK_BARRIER_MERGE, on by default): TotK calls Barrier(ORDER_FRAGMENTS | INVALIDATE_TEXTURE) about // 1200 times a frame in some scenes (water, decals): the next draw reads the render targets the last one wrote. A full // barrier there ends the Vulkan pass and flips every target's layout. What such a barrier asks for, an attachment write // made visible to a later fragment shader read of the open pass, feedback_prepare / feedback_barrier already do before the @@ -992,10 +992,14 @@ static void clear_ds(VkExec *ex, const p_clear_ds *c) { // that may write a storage buffer or image (pass_storage), which only a full barrier makes visible, or the barrier asks for // more than those two bits, or the feedback layout is not in use, or a target is a 3D BC stand-in (its texture gets the // pass's writes only at the pass end, bc_copy_back). The reads the by-region barrier does not cover end the pass at the -// draw that makes them (vk_exec_draw.c merge_guard). The by-region barrier orders only reads of the pixel a fragment -// writes; NVN's ORDER_FRAGMENTS orders every pixel (ORDER_FRAGMENTS_TILED is the per-region one), so a draw that reads a -// neighbouring pixel of a target written in the pass relies on the driver running the barrier for the whole target, as -// RADV does; hence experimental. +// draw that makes them (vk_exec_draw.c merge_guard), and so do reads of a target the draw does not write itself +// (vk_exec_draw.c merge_unwritten_reads): the by-region barrier orders only reads of the pixel a fragment writes, while +// NVN's ORDER_FRAGMENTS orders every pixel (ORDER_FRAGMENTS_TILED is the per-region one). What stays merged is a draw +// that samples a target it also writes, which Savage Wind runs the same way on every GPU. If such a draw read a +// neighbouring pixel, it would rely on the driver running the barrier for the whole target, as RADV and the desktop +// drivers do. On 1.4.3 every read after a merged Barrier in the shrine, the town and the field is of that kind. Passes +// a frame in the shrine: 581 -> 128 on an RDNA2 iGPU with no GPU time change on AMD's Windows driver; the waterfall +// on RADV: 1105 -> 117, GPU 8.3 -> 4.1 ms. int vk_exec_barrier_merge_on(void) { static int on = -1; if (on < 0) on = cfg_bool("TOTK_BARRIER_MERGE"); diff --git a/totk.ini.example b/totk.ini.example index 3756bd4b..e5ae0d78 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -98,14 +98,16 @@ # the CPU threads). # lib_prebuild = 1 # lib_prebuild_threads = 4 -# Experimental: 1 keeps a render pass open through the game's own Barrier calls (ORDER_FRAGMENTS | INVALIDATE_TEXTURE) when -# none of the pass's draws can write a storage buffer or image, making the writes visible to the next draw with a by-region -# barrier instead of ending the pass. Scenes where the game issues about a thousand such barriers a frame (the water in -# the Nachoyah Shrine, terrain decals) go from about 1200 passes and 10000 layout changes to about 130 passes and 150; -# the GPU time there fell from 24.5 to 7.8 ms a frame. The game's barrier orders every pixel, the one inside the pass only -# the pixel a draw writes: a driver that runs it per screen tile can show flicker or squares at the edges of water, decals -# or particles (report any). Needs the feedback layout (the default). Read at start. Default: 0. -# barrier_merge = 0 +# 1 keeps a render pass open through the game's own Barrier calls (ORDER_FRAGMENTS | INVALIDATE_TEXTURE) when none of the +# pass's draws can write a storage buffer or image, making the writes visible to the next draw with a by-region barrier +# instead of ending the pass. A draw that samples a target written in the pass without writing it itself still ends the +# pass. Scenes where the game issues about a thousand such barriers a frame (the water in the Nachoyah Shrine, terrain +# decals) go from about 1200 passes and 10000 layout changes to about 130 passes and 150; the GPU time there fell from +# 24.5 to 7.8 ms a frame on Linux (RADV). The game's barrier orders every pixel, the one inside the pass only the pixel a +# draw writes: a driver that runs it per screen tile could show flicker or squares at the edges of water, decals or +# particles; 0 brings back a full barrier each time (report it). Needs the feedback layout (the default). Read at start. +# Default: 1. +# barrier_merge = 1 # Build known pipelines ahead of time. # pipe_prebuild = 1 # Threads that translate known shaders at start (1 to 64; default: half the CPU threads, at most 16).