From 4c95df372aa2ef3615c25dd1c6acc8a62aa2b874 Mon Sep 17 00:00:00 2001 From: mattheuspaivadini Date: Sun, 22 Nov 2026 13:45:13 -0300 Subject: [PATCH 1/3] On Mesa's ANV, put a by-region barrier before a draw in a pass whose vertex shader reads a storage buffer (#927) On an Iris Xe with ANV the half-resolution particle pass drew campfire smoke and clouds as white or dark blobs and turned the sky white for single frames. Such draws read stale data inside the pass unless the sampler cache is invalidated first; a stall alone or the attachment flush alone does not help. The barrier is feedback_barrier, which the feedback loops already record inside passes. TOTK_ANV_VS_BARRIER (anv_vs_barrier) turns it off or on elsewhere. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 1 + runtime/cfg.c | 2 +- runtime/vk_exec_draw.c | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/settings.md b/docs/settings.md index 5258f39c..181f9d49 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -138,6 +138,7 @@ Many rows below are developer tools that only act together with another setting | Setting | What it does | Effect of changing it | Safety | | --- | --- | --- | --- | +| `anv_vs_barrier` | A by-region barrier before each draw in a pass whose vertex shader reads a storage buffer. On by default on Intel GPUs with Mesa's ANV driver only, where such reads otherwise see stale data (campfire smoke and clouds drawn as white or dark blobs, a white sky for single frames, #927); `0` or `1` forces it off or on. | `0` on ANV brings the one-frame glitches back; `1` elsewhere adds barriers for nothing (about 5% of the frame rate on an Iris Xe). | Safe | | `bind_inc` (default `0`) | `1` (or `on`) prepares a draw from what changed since the draw before (the bind ops mark the slots whose bytes they changed) instead of from the whole bound state; `check` prepares every draw as before, takes every decision of `on` beside it, logs each kind of difference once and counts them in a `bind_inc:` line every 300 frames; `0` or `off`: as before. `check` also counts what changed between draws in that line. | Less work per draw on the render thread; a change the marks missed would leave a stale descriptor, a wrong picture or a lost device, which `check` would show first. Off by default: with it on, a GTX 1050 Ti lost the device at one place, after the validation layer reported a descriptor pool reset while a command buffer still used its sets; the cause is not found yet. | Caution | | `block_views_3d` (default `0`) | `1` gives 3D block-compressed images block views too. | On some AMD cards a GPU write past the end of the image's memory loses the Vulkan device, and the game stops. | Risky | | `deswizzle` (default `auto`) | Where block-linear textures are deswizzled: `auto` or `gpu` uses a compute pass where the texture's storage is an imported pool and the CPU elsewhere, `cpu` always uses the CPU, `check` uses the GPU and also deswizzles every upload on the CPU and compares the two. | `cpu` brings back the long frames on entering new areas (the replay thread deswizzles every texture); `check` waits for the GPU at every upload and is for testing only. The log names the mode in use. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index b3dcbb2b..5a1146b9 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -33,7 +33,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line, and gets a // description in totk.ini.example or docs/settings.md (tests/settings_doc_check.py checks both). static const char *const known[] = { - "AB", "ABILITY_GUARD", "ADDR_REPORT", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_DRAIN_FIX", "AUDIO_OFF", "AUDIO_PREFILL_MS", + "AB", "ABILITY_GUARD", "ADDR_REPORT", "AMIIBO_DIR", "ANISO", "ANV_VS_BARRIER", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_DRAIN_FIX", "AUDIO_OFF", "AUDIO_PREFILL_MS", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 64603975..fd8790c9 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -3328,6 +3328,21 @@ static void bi_done(VkExec *ex, const VkPipeInfo *info, const VkPipeState *s, co D->B.bi_valid = 1; } +// Mesa ANV (Intel iGPU, Linux): a draw whose vertex shader reads a storage buffer reads stale data inside a pass unless +// the sampler cache is invalidated first: the half-resolution particle pass drew campfire smoke and clouds as white or +// dark blobs and turned the sky white for single frames (#927). A by-region barrier before such a draw removes it +// (0 of 1460 frames broken against 57 of 779, about 2 fps of 40 on an Iris Xe); a stall alone or the attachment flush +// alone does not. TOTK_ANV_VS_BARRIER=0/1 forces it off or on. +static int anv_vs_barrier(void) { + static int on = -1; + if (on < 0) { + const char *e = cfg_get("TOTK_ANV_VS_BARRIER"); + on = e && *e ? strcmp(e, "0") != 0 : vk_caps.mesa && vk_ctx()->props.vendorID == 0x8086; + rt_log("vk_exec: barrier before vertex-stage storage reads in a pass %s", on ? "on" : "off"); + } + return on; +} + // Pass, shaders, pipeline, fixed state and resources for a draw. 0: the draw is skipped. static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { VkExecDraw *D = ex->draw; @@ -3540,6 +3555,8 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { if (bim == 2) bi_census(ex, info, sp, bgt, bgb, bsg, sm_hit); // on counts nothing: it is what gets timed bind_resources(ex, info, bim ? bgt : NULL, bgb); if (fbl & ex->fb_dirty) feedback_barrier(ex); + else if (ex->in_pass && anv_vs_barrier() && gfx_shader_resources(vs, GFX_RES_SSBO, &(const GfxShaderRes *){0})) + feedback_barrier(ex); feedback_written(ex, sp); bind_vertex_buffers(ex); if (vk_dump_armed) vk_dump_draw(ex, sp, p, info); From 5b15fc0df6a457d87163c404b7694c24fb0b48be Mon Sep 17 00:00:00 2001 From: mattheuspaivadini Date: Sun, 22 Nov 2026 21:05:13 -0300 Subject: [PATCH 2/3] Keep fb_dirty across the ANV vertex-storage barrier, so feedback_prepare still learns the feedback targets (#927) feedback_barrier clears fb_dirty, and feedback_prepare learns which targets go into the feedback layout from m & fb_dirty. A barrier recorded only for a vertex shader's storage read could hide a later attachment read from it. Co-Authored-By: Claude Sonnet 5.5 --- runtime/vk_exec_draw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index fd8790c9..08911b28 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -3556,7 +3556,7 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { bind_resources(ex, info, bim ? bgt : NULL, bgb); if (fbl & ex->fb_dirty) feedback_barrier(ex); else if (ex->in_pass && anv_vs_barrier() && gfx_shader_resources(vs, GFX_RES_SSBO, &(const GfxShaderRes *){0})) - feedback_barrier(ex); + { uint32_t keep = ex->fb_dirty; feedback_barrier(ex); ex->fb_dirty = keep; } // keep fb_dirty: feedback_prepare learns from it feedback_written(ex, sp); bind_vertex_buffers(ex); if (vk_dump_armed) vk_dump_draw(ex, sp, p, info); From d98996b6e2ae251ab1e2db2d8c8f4a189bdb6939 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 23 Nov 2026 14:17:26 +0300 Subject: [PATCH 3/3] Turn the ANV vertex-shader storage barrier off by default on every GPU; players opt in with anv_vs_barrier = 1 (#927) The barrier costs about 5% of the frame rate on an Iris Xe and was only tested there. TOTK_ANV_VS_BARRIER and anv_vs_barrier still force it on with 1 and off with 0. docs/settings.md says what it fixes, what it costs and that only Intel GPUs with Mesa ANV on Linux are affected. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 2 +- runtime/vk_exec_draw.c | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 181f9d49..c5d32e0a 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -138,7 +138,7 @@ Many rows below are developer tools that only act together with another setting | Setting | What it does | Effect of changing it | Safety | | --- | --- | --- | --- | -| `anv_vs_barrier` | A by-region barrier before each draw in a pass whose vertex shader reads a storage buffer. On by default on Intel GPUs with Mesa's ANV driver only, where such reads otherwise see stale data (campfire smoke and clouds drawn as white or dark blobs, a white sky for single frames, #927); `0` or `1` forces it off or on. | `0` on ANV brings the one-frame glitches back; `1` elsewhere adds barriers for nothing (about 5% of the frame rate on an Iris Xe). | Safe | +| `anv_vs_barrier` | A by-region barrier before each draw in a pass whose vertex shader reads a storage buffer. Off by default (`0`). Only Intel GPUs with Mesa's ANV driver on Linux need it: there such reads see stale data and campfire smoke and clouds are drawn as white or dark blobs, or the sky flashes white for single frames (#927). Set `1` to turn it on. | `1` costs about 5% of the frame rate on an Iris Xe and does nothing useful on other GPUs or on Windows; `0` leaves the one-frame glitches on ANV. | Safe | | `bind_inc` (default `0`) | `1` (or `on`) prepares a draw from what changed since the draw before (the bind ops mark the slots whose bytes they changed) instead of from the whole bound state; `check` prepares every draw as before, takes every decision of `on` beside it, logs each kind of difference once and counts them in a `bind_inc:` line every 300 frames; `0` or `off`: as before. `check` also counts what changed between draws in that line. | Less work per draw on the render thread; a change the marks missed would leave a stale descriptor, a wrong picture or a lost device, which `check` would show first. Off by default: with it on, a GTX 1050 Ti lost the device at one place, after the validation layer reported a descriptor pool reset while a command buffer still used its sets; the cause is not found yet. | Caution | | `block_views_3d` (default `0`) | `1` gives 3D block-compressed images block views too. | On some AMD cards a GPU write past the end of the image's memory loses the Vulkan device, and the game stops. | Risky | | `deswizzle` (default `auto`) | Where block-linear textures are deswizzled: `auto` or `gpu` uses a compute pass where the texture's storage is an imported pool and the CPU elsewhere, `cpu` always uses the CPU, `check` uses the GPU and also deswizzles every upload on the CPU and compares the two. | `cpu` brings back the long frames on entering new areas (the replay thread deswizzles every texture); `check` waits for the GPU at every upload and is for testing only. The log names the mode in use. | Caution | diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 08911b28..5bd62d56 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -3332,12 +3332,13 @@ static void bi_done(VkExec *ex, const VkPipeInfo *info, const VkPipeState *s, co // the sampler cache is invalidated first: the half-resolution particle pass drew campfire smoke and clouds as white or // dark blobs and turned the sky white for single frames (#927). A by-region barrier before such a draw removes it // (0 of 1460 frames broken against 57 of 779, about 2 fps of 40 on an Iris Xe); a stall alone or the attachment flush -// alone does not. TOTK_ANV_VS_BARRIER=0/1 forces it off or on. +// alone does not. The barrier costs about 5% of the frame rate on an Iris Xe and was only tested there, so it is off +// on every GPU by default; TOTK_ANV_VS_BARRIER=1 (ini: anv_vs_barrier = 1) turns it on, 0 keeps it off. static int anv_vs_barrier(void) { static int on = -1; if (on < 0) { const char *e = cfg_get("TOTK_ANV_VS_BARRIER"); - on = e && *e ? strcmp(e, "0") != 0 : vk_caps.mesa && vk_ctx()->props.vendorID == 0x8086; + on = e && *e && strcmp(e, "0") != 0; rt_log("vk_exec: barrier before vertex-stage storage reads in a pass %s", on ? "on" : "off"); } return on;