Merge task/bmerge: barrier_merge is on by default and ends the pass when a draw samples a target written in the pass that it does not write itself, as Savage Wind does; shrine passes 581 -> 128 a frame on an RDNA2 iGPU with the GPU time unchanged on AMD's Windows driver, RADV waterfall GPU 8.3 -> 4.1 ms (#1237)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Valiantsin DzerakhandClaude Opus 5.5 committed 2026-11-24 09:15:52 +03:00
commit 7da078d1ab
6 files changed
+54 -17

No files matched your search

+1 -1
View File
@@ -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},
+11 -7
View File
@@ -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;
@@ -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");
+2
View File
@@ -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
+29
View File
@@ -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