From 61a407b0e1f0bcd26caa31dc93eceba618e60379 Mon Sep 17 00:00:00 2001 From: ripps818 Date: Fri, 13 Nov 2026 03:11:10 -0600 Subject: [PATCH 1/3] Add barrier_merge: keep a render pass open through the game's texture-invalidating barriers The runtime ended the Vulkan pass at every game Barrier. A Barrier that asks only for fragment ordering and texture invalidation (ORDER_FRAGMENTS | INVALIDATE_TEXTURE) now leaves the pass open when none of the pass's draws can write a storage buffer or image, and the next draw sees the writes through a by-region barrier in the feedback layout. Off by default (barrier_merge = 0, TOTK_BARRIER_MERGE). TOTK_TRACE=gfx also prints how many passes ended by a change of targets and by the game's Barrier, how many Barrier calls were merged and which NVN barrier bits they carry. Co-Authored-By: Claude Sonnet 5.5 --- docs/settings.md | 1 + runtime/cfg.c | 6 +++--- runtime/vk_exec.c | 29 ++++++++++++++++++++++++++--- runtime/vk_exec.h | 4 ++++ runtime/vk_exec_draw.c | 12 ++++++++++++ totk.ini.example | 8 ++++++++ 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 1b0193fe..188b3ba6 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -22,6 +22,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. | 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. | Edges differ by about a pixel in screenshots; not fully explained. | Caution | | `menu_cursor_log` | `1` logs what the menu cursor code reads at every screen change. | Only adds log lines. | Safe | | `perf_slow_ms` (default `50`) | Frames at least this many milliseconds long get a `perf: slow frame` log line with the time blocked per cause. | Changes how many frames are logged; a small value fills the log. | Safe | | `replay_check` | `1` reads every texture read-back a second time after a full drain of the replay queue and compares the two. | Stalls the replay queue at every read-back, so frames get much slower; mismatches are only counted and logged. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index 12a63007..cdb7a262 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -38,7 +38,7 @@ static const char *const known[] = { "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", "BIND_SHAKE", "BIND_INC", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", - "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLIMB_FIX", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", + "BIND_ZL", "BIND_ZR", "BARRIER_MERGE", "BLOCK_VIEWS_3D", "CLIMB_FIX", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DESWIZZLE", "DEVICE_PROFILE", "DIALOGUE_CLICK", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "DYNAMIC_RES", "DYN_DELTA", "EXEFS", "FEEDBACK", "FIXED_RES_PATCH", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", @@ -106,7 +106,7 @@ static const struct { const char *key, *section; } sections[] = { {"BIND_RSTICK_UP", "input"}, {"BIND_RSTICK_RIGHT", "input"}, {"BIND_RSTICK_DOWN", "input"}, {"BIND_SHAKE", "input"}, {"ASYNC_PREEMPT", "performance"}, {"REPLAY_ASYNC", "performance"}, {"REPLAY_WORKERS", "performance"}, {"FREE_CORES", "performance"}, {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, - {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"}, + {"BARRIER_MERGE", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_RELEASED_MIB", "performance"}, {"TEX_BLOCK_MIB", "performance"}, {"VMA_BLOCK_MIB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"MEMX", "performance"}, {"BIND_INC", "performance"}, {"TEX_MEMO", "performance"}, {"VB_USED", "performance"}, {"DESC_TEMPLATE", "performance"}, @@ -209,7 +209,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}, - {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, + {"BARRIER_MERGE", "0", "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 0df3ce01..03448928 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; feedback draws %llu barriers %llu restarts %llu layouts %llu; targets%s", + "%ux%u/%#x written %d; pass ends: targets %llu barrier %llu (%llu merged) 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->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_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; @@ -644,6 +644,7 @@ static VkImageMemoryBarrier2 layout_barrier(VkImage img, VkImageAspectFlags aspe // go back to GENERAL and the new ones leave it, in the same command. static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant) { vk_exec_pass_end(ex); + ex->pass_storage = 0; vk_exec_ub_flush(ex); // uniform updates overlaid in the pass reach their buffers before the barrier below VkImageMemoryBarrier2 ib[18]; uint32_t n = 0; @@ -889,8 +890,22 @@ 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 +// 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 +// draw that samples it, with a by-region barrier inside the pass. So the pass keeps going, unless a draw of it has a shader +// 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. +static int barrier_merges(const VkExec *ex, int bits) { + static int on = -1; + if (on < 0) on = cfg_bool("TOTK_BARRIER_MERGE"); + return on && ex->in_pass && !ex->pass_storage && (bits & 0x10) && !(bits & ~0x12) && vk_ctx()->feedback == VK_FEEDBACK_LAYOUT; +} + static void set_rt(VkExec *ex, const p_set_rt *r) { if (!memcmp(&ex->rt, r, sizeof *r)) return; // the same targets again: the pass goes on + if (ex->in_pass) ex->fr.pass_end_rt++; vk_exec_pass_end(ex); ex->rt = *r; ex->rt_resolved = 0; @@ -1054,7 +1069,15 @@ static int walk(VkExec *ex, const op_hdr *h, int depth, const op_hdr *stop) { case OP_SET_RT: set_rt(ex, a); break; case OP_CLEAR_COLOR: clear_color(ex, a); break; case OP_CLEAR_DS: clear_ds(ex, a); break; - case OP_BARRIER: vk_exec_barrier(ex); break; + case OP_BARRIER: + if (barrier_merges(ex, ((const p_barrier *)a)->bits)) { // barrier_merge: the pass goes on (see barrier_merges) + ex->fr.barrier_merged++; + break; + } + if (ex->in_pass) ex->fr.pass_end_barrier++; + for (int i = 0; i < 8; i++) ex->fr.barrier_bit[i] += (uint32_t)((const p_barrier *)a)->bits >> i & 1; + vk_exec_barrier(ex); + break; case OP_DRAW: case OP_DRAW_IDX: case OP_DISPATCH: case OP_UPDATE_UB: case OP_DRAW_IDX_IND: case OP_DISPATCH_IND: if (h->op == OP_DRAW || h->op == OP_DRAW_IDX || h->op == OP_DRAW_IDX_IND) ex->fr.draws_seen++, ex->split_draws++; if (h->op == OP_DISPATCH || h->op == OP_DISPATCH_IND) ex->fr.dispatches++, ex->split_draws++; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 865d89db..839d586b 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -54,6 +54,9 @@ typedef struct { // TOTK_TRACE=gfx: replay counters of the current frame, logged and reset at each present (vk_exec_frame_log). typedef struct { uint64_t passes, draws_seen, draws, clears, copies, dispatches, submits; + 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 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 // transitions @@ -155,6 +158,7 @@ typedef struct VkExec { int nfbl; uint32_t fbl_mask; // attachments of the open pass in that layout: bit i colour i, bit 8 depth uint32_t fb_dirty; // attachments of the open pass written by a draw or clear since the pass began + uint8_t pass_storage; // a draw of the open pass has a shader that may write a storage buffer or image (barrier_merge) // or the last in-pass barrier const void *fb_fs; // feedback_scan's memo: fragment shader, texture bind serial, pass serial uint32_t fb_serial, fb_pass, fb_sampled; diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index ddbb004c..c61f07a9 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2992,6 +2992,17 @@ static int pipe_memo_on(void) { return on; } +// A shader that may write a storage buffer or image: a written SSBO (flags bit 0) or any storage image. +static int shader_may_write_storage(const GfxShader *sh) { + const GfxShaderRes *r; + if (!sh) return 0; + if (gfx_shader_resources(sh, GFX_RES_IMG, &r)) return 1; + int n = gfx_shader_resources(sh, GFX_RES_SSBO, &r); + for (int i = 0; i < n; i++) + if (r[i].flags & 1) return 1; + return 0; +} + // ---- 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 @@ -3266,6 +3277,7 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { if (!vs || (has_fs && !fs)) return 0; if (vk_dlaa_skip_draw(ex, fs)) return 0; // the game's FXAA off, or its scaling replaced by DLSS (vk_dlaa.c) uint32_t fbl = feedback_prepare(ex, fs); + if (!ex->pass_storage && (shader_may_write_storage(vs) || shader_may_write_storage(fs))) ex->pass_storage = 1; uint8_t fb = (uint8_t)((fbl & 0xff ? 1 : 0) | (fbl & 0x100 ? 2 : 0)), dmw = (uint8_t)(nvn_depth_minus_w() != 0); // TOTK_BIND_INC: the pipeline memo's state is this draw's when pipe was not written since the memo's state was built // from it and the fields a draw sets are the same (y_flip is always 1): its pipeline is taken without building the diff --git a/totk.ini.example b/totk.ini.example index abb8db1d..71085e30 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -505,6 +505,14 @@ # cheat_0123abcd = 1 [diagnostics] +# 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 picture is the same to the eye and differs at edges by about a +# pixel in screenshots (not fully explained: report any glitch in water, decals or particles). Needs the feedback layout +# (the default). Read at start. Default: 0. +# barrier_merge = 0 # Extra lines in the log, comma separated: svc, host, ipc, fs, gfx, perf, sched, audio, present or all. The menu's # Performance log sets perf (frame timings). Default: none. # trace = perf From af1329440d171ada7b0ded1627327138a90f3754 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 16 Nov 2026 18:03:42 +0300 Subject: [PATCH 2/3] barrier_merge costs nothing per draw while off and guards the reads a merged Barrier cannot order The storage check in prepare_draw ran on every draw with the setting off; it now runs only when the context's cached flag is on, and after bind_resources, which may restart the pass (ub_written) and clear the note of a storage writer. A pass whose target is a 3D BC stand-in no longer merges: its texture gets the writes only at the pass end. After a merged Barrier, a draw with a storage image or a vertex texture that is an attachment of the pass ends the pass and begins the next behind the full barrier (merge_guard), since the in-pass by-region barrier orders only fragment shader sampling. The per-bit Barrier counters count only under TOTK_TRACE=gfx. Co-Authored-By: Claude Opus 5.5 --- runtime/vk_exec.c | 31 ++++++++++++++++++++++++------- runtime/vk_exec.h | 9 ++++++++- runtime/vk_exec_draw.c | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 10 deletions(-) diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 03448928..3ff91b2d 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) 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) 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_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->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; @@ -136,6 +136,7 @@ static void state_reset(VkExec *ex) { vk_pipe_state_defaults(&ex->pipe); ex->barrier_at = -1; ex->oq_open = -1; + ex->bmerge = (uint8_t)vk_exec_barrier_merge_on(); vk_exec_dirty_all(ex); } @@ -644,7 +645,7 @@ static VkImageMemoryBarrier2 layout_barrier(VkImage img, VkImageAspectFlags aspe // go back to GENERAL and the new ones leave it, in the same command. static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant) { vk_exec_pass_end(ex); - ex->pass_storage = 0; + ex->pass_storage = ex->pass_merged = 0; // barrier_merge: every pass begins behind this barrier vk_exec_ub_flush(ex); // uniform updates overlaid in the pass reach their buffers before the barrier below VkImageMemoryBarrier2 ib[18]; uint32_t n = 0; @@ -896,11 +897,25 @@ static void clear_ds(VkExec *ex, const p_clear_ds *c) { // made visible to a later fragment shader read of the open pass, feedback_prepare / feedback_barrier already do before the // draw that samples it, with a by-region barrier inside the pass. So the pass keeps going, unless a draw of it has a shader // 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. -static int barrier_merges(const VkExec *ex, int bits) { +// 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. +int vk_exec_barrier_merge_on(void) { static int on = -1; if (on < 0) on = cfg_bool("TOTK_BARRIER_MERGE"); - return on && ex->in_pass && !ex->pass_storage && (bits & 0x10) && !(bits & ~0x12) && vk_ctx()->feedback == VK_FEEDBACK_LAYOUT; + return on; +} + +static int barrier_merges(const VkExec *ex, int bits) { + if (!ex->bmerge || !ex->in_pass || ex->pass_storage || !(bits & 0x10) || (bits & ~0x12) || + vk_ctx()->feedback != VK_FEEDBACK_LAYOUT) + return 0; + for (int i = 0; i < ex->pass_ncolor && i < 8; i++) + if (ex->color[i].image && ex->color[i].img && ex->color[i].img->image != ex->color[i].image) return 0; + return 1; } static void set_rt(VkExec *ex, const p_set_rt *r) { @@ -1071,11 +1086,13 @@ static int walk(VkExec *ex, const op_hdr *h, int depth, const op_hdr *stop) { case OP_CLEAR_DS: clear_ds(ex, a); break; case OP_BARRIER: if (barrier_merges(ex, ((const p_barrier *)a)->bits)) { // barrier_merge: the pass goes on (see barrier_merges) + ex->pass_merged = 1; ex->fr.barrier_merged++; break; } if (ex->in_pass) ex->fr.pass_end_barrier++; - for (int i = 0; i < 8; i++) ex->fr.barrier_bit[i] += (uint32_t)((const p_barrier *)a)->bits >> i & 1; + if (rt_trace & RT_TRACE_GFX) + for (int i = 0; i < 8; i++) ex->fr.barrier_bit[i] += (uint32_t)((const p_barrier *)a)->bits >> i & 1; vk_exec_barrier(ex); break; case OP_DRAW: case OP_DRAW_IDX: case OP_DISPATCH: case OP_UPDATE_UB: case OP_DRAW_IDX_IND: case OP_DISPATCH_IND: diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 839d586b..ff3e5907 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -56,6 +56,7 @@ typedef struct { uint64_t passes, draws_seen, draws, clears, copies, dispatches, submits; 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 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 @@ -158,10 +159,15 @@ typedef struct VkExec { int nfbl; uint32_t fbl_mask; // attachments of the open pass in that layout: bit i colour i, bit 8 depth uint32_t fb_dirty; // attachments of the open pass written by a draw or clear since the pass began - uint8_t pass_storage; // a draw of the open pass has a shader that may write a storage buffer or image (barrier_merge) // or the last in-pass barrier const void *fb_fs; // feedback_scan's memo: fragment shader, texture bind serial, pass serial uint32_t fb_serial, fb_pass, fb_sampled; + // barrier_merge (vk_exec.c barrier_merges): bmerge is TOTK_BARRIER_MERGE, read once when the context is reset; + // pass_storage: a draw of the open pass may write a storage buffer or image; pass_merged: the open pass went on + // through a game Barrier. Both are cleared by the full barrier of every pass begin (barrier_to). + uint8_t bmerge, pass_storage, pass_merged; + const void *bm_vs; // merge_guard's memo: vertex shader, texture bind serial, pass serial, answer + uint32_t bm_serial, bm_pass, bm_hit; uint32_t tex_serial, pass_serial; // bumped by every texture or image bind and pass begin // TOTK_BIND_INC (vk_exec_draw.c): the slots the bind ops changed since this context's last prepared draw, cleared // by that draw; vk_exec_draw_new_cb and copy_state set every bit. pipe_gen: bumped by every write that changes @@ -203,6 +209,7 @@ void vk_exec_pass_end(VkExec *ex); // Full memory barrier (all commands, all memory); ends the open pass first. Copies and dispatches order themselves with // it. Skipped when nothing was recorded since the last one. void vk_exec_barrier(VkExec *ex); +int vk_exec_barrier_merge_on(void); // TOTK_BARRIER_MERGE, read once (vk_exec.c barrier_merges) // UpdateUniformBuffer overlays (vk_exec_draw.c): the copies into the real buffers that updates inside a pass left for // the next barrier (barrier_to records them first), and a GPU write of another kind into [addr, addr+n) of this // submission (a copy, a fill, a storage write), which retires the overlays it touches. diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index c61f07a9..42513cc2 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2992,7 +2992,8 @@ static int pipe_memo_on(void) { return on; } -// A shader that may write a storage buffer or image: a written SSBO (flags bit 0) or any storage image. +// barrier_merge (vk_exec.c barrier_merges). A shader that may write a storage buffer or image: a written SSBO (flags +// bit 0) or any storage image. gfx_shader_resources only reads the shader's parsed arrays. static int shader_may_write_storage(const GfxShader *sh) { const GfxShaderRes *r; if (!sh) return 0; @@ -3003,6 +3004,32 @@ static int shader_may_write_storage(const GfxShader *sh) { return 0; } +// barrier_merge: a draw in a pass that went on through a game Barrier whose reads the in-pass by-region barrier +// (feedback_barrier: fragment shader sampling only, found by feedback_scan) would not order. Those are a storage image +// of either stage (an imageLoad of a target) and a vertex shader texture that is an attachment of the pass. Such a draw +// ends the pass, and the next one begins behind the full barrier, as the game's Barrier would have done. The vertex +// scan is kept while the shader, the texture bindings and the pass stay the same, as feedback_scan's. +static int merge_guard(VkExec *ex, const GfxShader *vs, const GfxShader *fs) { + const GfxShaderRes *res; + if (gfx_shader_resources(vs, GFX_RES_IMG, &res) || (fs && gfx_shader_resources(fs, GFX_RES_IMG, &res))) return 1; + int n = gfx_shader_resources(vs, GFX_RES_TEX, &res); + if (!n) return 0; + if (ex->bm_vs == vs && ex->bm_serial == ex->tex_serial && ex->bm_pass == ex->pass_serial) return (int)ex->bm_hit; + uint32_t m = 0; + vk_texture_lock(); + for (int r = 0; r < n && !m; r++) + for (int k = 0; k < (res[r].count ? res[r].count : 1) && !m; k++) { + int slot = res[r].slot + k; + uint64_t h = slot >= 0 && slot < 128 ? ex->tex[NVN_STAGE_VS][slot] : 0; + if (!h) continue; + SubRange sr = sampled_sub(h); + m = attached_as(ex, &sr); + } + vk_texture_unlock(); + ex->bm_vs = vs, ex->bm_serial = ex->tex_serial, ex->bm_pass = ex->pass_serial, ex->bm_hit = m != 0; + return m != 0; +} + // ---- 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 @@ -3276,8 +3303,12 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { } if (!vs || (has_fs && !fs)) return 0; if (vk_dlaa_skip_draw(ex, fs)) return 0; // the game's FXAA off, or its scaling replaced by DLSS (vk_dlaa.c) + if (ex->pass_merged && merge_guard(ex, vs, fs)) { // barrier_merge: the full barrier the merged Barrier skipped + vk_exec_pass_end(ex); + vk_exec_pass_begin(ex); + ex->fr.barrier_undone++; + } uint32_t fbl = feedback_prepare(ex, fs); - if (!ex->pass_storage && (shader_may_write_storage(vs) || shader_may_write_storage(fs))) ex->pass_storage = 1; uint8_t fb = (uint8_t)((fbl & 0xff ? 1 : 0) | (fbl & 0x100 ? 2 : 0)), dmw = (uint8_t)(nvn_depth_minus_w() != 0); // TOTK_BIND_INC: the pipeline memo's state is this draw's when pipe was not written since the memo's state was built // from it and the fields a draw sets are the same (y_flip is always 1): its pipeline is taken without building the @@ -3412,6 +3443,8 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { if (vk_hdr_track) vk_hdr_draw(ex, info); vk_exec_oq_draw(ex); if (bim) bi_done(ex, info, sp, bgt, bgb, bsg); + // barrier_merge: noted last, in the pass the draw is recorded in (bind_resources may restart the pass, ub_written) + if (ex->bmerge && !ex->pass_storage && (shader_may_write_storage(vs) || shader_may_write_storage(fs))) ex->pass_storage = 1; return 1; } From 005881da206b7aa048dc63689e303b1e5e445403 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 16 Nov 2026 18:05:34 +0300 Subject: [PATCH 3/3] barrier_merge docs name the by-region limit instead of an unexplained edge difference Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 2 +- totk.ini.example | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index a2cbd89b..670c69f2 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -22,7 +22,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. | 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. | Edges differ by about a pixel in screenshots; not fully explained. | 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 | | `menu_cursor_log` | `1` logs what the menu cursor code reads at every screen change. | Only adds log lines. | Safe | | `perf_slow_ms` (default `50`) | Frames at least this many milliseconds long get a `perf: slow frame` log line with the time blocked per cause. | Changes how many frames are logged; a small value fills the log. | Safe | | `replay_check` | `1` reads every texture read-back a second time after a full drain of the replay queue and compares the two. | Stalls the replay queue at every read-back, so frames get much slower; mismatches are only counted and logged. | Caution | diff --git a/totk.ini.example b/totk.ini.example index 560f6797..61f74cf4 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -509,9 +509,9 @@ # 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 picture is the same to the eye and differs at edges by about a -# pixel in screenshots (not fully explained: report any glitch in water, decals or particles). Needs the feedback layout -# (the default). Read at start. Default: 0. +# 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 # Extra lines in the log, comma separated: svc, host, ipc, fs, gfx, perf, sched, audio, present or all. The menu's # Performance log sets perf (frame timings). Default: none.