From 5aaa718eb66317b895eccde9af876a9896714424 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 24 Nov 2026 09:04:48 +0300 Subject: [PATCH] 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