From cc84958cee68965bd47895115cfb9e77d688086e Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 05:41:05 +0300 Subject: [PATCH 1/2] Give TotK's water volume mask draws a depth bias toward the viewer, so a puddle's surface, on which the volume's top face lies, counts as inside the volume in every frame instead of by rounding and the camera's sub-millimetre motion, which turned the puddle's reflection on and off in patches from frame to frame, more with DLAA --- docs/agents/symptoms.md | 1 + runtime/vk.h | 6 ++++++ runtime/vk_exec.h | 1 + runtime/vk_exec_draw.c | 10 +++++++--- runtime/vk_pipeline.c | 27 +++++++++++++++++++++++++-- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 5c9fb005..d17d6c75 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -26,6 +26,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | no sun shadows, wrong lighting | unwritten vertex outputs read as zero instead of (0,0,0,1) | the shader's output map | | one flat ambient colour | a 1-layer 2D array sampled as 2D | the view type the shader expects | | upside-down text | lower-left window origin | origin mode in the log | +| puddles and still water flicker: the reflection turns on and off in patches from frame to frame while the camera stands still, more with DLAA | the game's water volume mask: a volume's top face lies on the water surface and is tested LEQUAL against a depth rebuilt at quarter size, so rounding and the camera's sub-millimetre motion decide it (fixed: `vk_pipeline.c` `vk_pipeline_volume_bias` gives such draws a depth bias toward the viewer) | a `--capture` run: the puddle's brightness swing (`capture.py` frames, a region's mean) with and without the fix; in a frame dump, the quarter-size mask after the volume draw changes from frame to frame | | flicker or a texture swapped with a similar one | the newest texture picked at shared storage instead of the one with the matching view | texture lookup by id in the dump | | with DLAA on, dust and small particles in fog (shrines) vanish, smear or behave oddly; the fog looks processed | DLSS ran at the bloom, after the fog and the particles, which have no motion vectors or jitter of their own (fixed: `vk_dlaa.c` effects_point runs DLSS at the first pass into the light target after the lighting, and the effects go over its picture) | `dlaa_effects_after = 0` (Screen tab) brings the old point back; the count "before the effects" in the `dlaa:` log line against the frames anti-aliased | | with DLAA on, the picture flickers from frame to frame: a little outdoors; the whole scene in a fogged shrine with an ability's targeting view (Ultrahand) open, where the fog also goes back under DLSS | DLSS ran before some effects in one frame and after them in the next: their order changes from frame to frame (the yard's half-size effects pass is in some frames only; the ability highlight, drawn into the light target with a D16 depth of its own, comes before or after the fog layer), and a rule that waited for one of them or took any D16 pass for the lighting followed it (fixed: `vk_dlaa.c` effects_point knows the lighting by its depth and runs DLSS right after it) | the `dlaa:` log line: "before the effects" must equal the frames anti-aliased in the world; a log of the passes into the light target per frame | diff --git a/runtime/vk.h b/runtime/vk.h index 39cf6ccf..0b105c78 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -678,6 +678,12 @@ int vk_pipeline_vertex_input_dynamic(void); // 1: vertex input is VK_EXT_vert void vk_pipeline_answers(const VkPipeState *s, GfxShaderAnswers *vs, GfxShaderAnswers *fs); int vk_pipeline_tex_type(int nvn_target); // NVN_TEXTURE_TARGET_* -> GFX_TEX_* (GfxShaderAnswers.tex_type) uint32_t vk_pipeline_target_wmask(const VkPipeState *s, int i); // colour write mask of target i the pipeline uses +// The sign of the depth bias a volume mask draw gets (vk_pipeline.c, see there), -1 toward smaller depth, 1 toward +// larger, 0 for any other draw. vk_pipeline_set_dynamic enables the bias for it; vk_exec_draw.c record_fixed sets the +// values, VOLUME_BIAS_UNITS and VOLUME_BIAS_SLOPE times the sign. +int vk_pipeline_volume_bias(const VkPipeState *s); +#define VOLUME_BIAS_UNITS 16.0f +#define VOLUME_BIAS_SLOPE 1.0f VkPrimitiveTopology vk_pipeline_topology(int nvn_prim); // quads -> triangle list, line loop -> strip: the replay // rewrites those draws; polygon -> fan VkFormat vk_vertex_format(int nvn_format, int *type); // attribute format (supported fallback), GFX_TYPE_* in *type diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index ff3e5907..9e475ccb 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -119,6 +119,7 @@ typedef struct VkExec { uint32_t st_vmask[2], st_wmask[2], st_ref[2]; // OP_STENCIL per face (0 front, 1 back): compare mask, write mask, ref float point_size; // OP_BIND_STATE kind 6: NVN point size for the support buffer; 0 = never set = 1 p_poly_offset poly_offset; // OP_POLY_OFFSET: depth bias factor, units, clamp + int8_t volume_bias; // the sign of the volume bias recorded in place of poly_offset (record_fixed) p_prim_restart prim_restart; // OP_PRIM_RESTART: applied to indexed strip and fan draws (op_draw_idx) float blend_color[4]; // OP_BLEND_COLOR int64_t copy_stride; // OP_COPY_STRIDE: row stride of buffer <-> texture copies (vk_exec_xfer.c) diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 42513cc2..95c74f32 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -2784,8 +2784,11 @@ static VkViewport draw_viewport(VkExec *ex) { return v; } -static void record_fixed(VkExec *ex) { +// s: the draw's state. A volume mask draw records its own depth bias (vk_pipeline_volume_bias) in place of the game's. +static void record_fixed(VkExec *ex, const VkPipeState *s) { VkCommandBuffer cb = ex->cb; + int vb = vk_pipeline_volume_bias(s); + if (vb != ex->volume_bias) ex->dirty |= VK_EXEC_DIRTY_DEPTH_BIAS, ex->volume_bias = (int8_t)vb; if (ex->dirty & VK_EXEC_DIRTY_VIEWPORT) { VkViewport v = draw_viewport(ex); vkCmdSetViewportWithCount(cb, 1, &v); @@ -2797,7 +2800,8 @@ static void record_fixed(VkExec *ex) { ex->recorded++; } if (ex->dirty & VK_EXEC_DIRTY_DEPTH_BIAS) { - vkCmdSetDepthBias(cb, ex->poly_offset.units, ex->poly_offset.clamp, ex->poly_offset.factor); + if (vb) vkCmdSetDepthBias(cb, VOLUME_BIAS_UNITS * (float)vb, 0.0f, VOLUME_BIAS_SLOPE * (float)vb); + else vkCmdSetDepthBias(cb, ex->poly_offset.units, ex->poly_offset.clamp, ex->poly_offset.factor); ex->recorded++; } if (ex->dirty & VK_EXEC_DIRTY_BLEND_CONST) { @@ -3431,7 +3435,7 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { vk_mvsurvey_draw(ex); vk_mvprev_draw(ex); if (mo) plat_mutex_unlock(&mo_lock); - record_fixed(ex); + record_fixed(ex, sp); repeat_stats(ex, info); if (bim) bi_gt(ex, bgt), bi_gb(ex, bgb); if (bim == 2) bi_census(ex, info, sp, bgt, bgb, bsg, sm_hit); // on counts nothing: it is what gets timed diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 08e4c066..7b366d69 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3234,7 +3234,7 @@ uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { compare_op(d->sf_b)); // NVN_POLYGON_OFFSET_ENABLE_* bit of the polygon mode in use: point 1, line 2, fill 4 unsigned mode = pl->mode <= NVN_POLYGON_MODE_FILL ? pl->mode : NVN_POLYGON_MODE_FILL; - vkCmdSetDepthBiasEnable(cb, (pl->offset_en >> mode & 1) != 0); + vkCmdSetDepthBiasEnable(cb, (pl->offset_en >> mode & 1) || vk_pipeline_volume_bias(s)); vkCmdSetRasterizerDiscardEnable(cb, s->raster_discard != 0); vkCmdSetPrimitiveRestartEnable(cb, s->prim_restart != 0); // The vertex bindings the draw reads: those of its vertex input, set here or made into the pipeline from the same @@ -3268,6 +3268,29 @@ uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { return streams; } +// TotK marks the pixels inside its water volumes (seen at puddles) in a quarter-size mask: it draws a volume's front +// faces with a LEQUAL depth test and no depth write, blending ONE/ONE with reverse subtract into one channel, against a +// depth it rebuilt at that size from its linear depth. The volume's top face lies on the water surface, so the test +// compares that surface with itself through two roundings and two sampling positions: on a puddle the face came out +// up to 7 ulp in front of or behind the rebuilt depth, and up to about 0.6 of a pixel of its depth slope (64 ulp) behind +// it. The camera's sub-millimetre idle motion moved that pattern every frame and the mask with it: the puddle's +// reflection, which the mask turns on, flickered over a few hundred pixels a frame, and more with DLAA, whose jitter +// moves the rebuilt depth but not the volume. Such a draw, when the game sets no polygon offset for it, gets a depth +// bias toward the viewer of VOLUME_BIAS_UNITS plus VOLUME_BIAS_SLOPE times its depth slope (one pixel of it), so a +// surface the face lies on counts as inside, as the test's equal case means; a surface farther from the face than a +// pixel of its slope is decided as before. +int vk_pipeline_volume_bias(const VkPipeState *s) { + const poly_st *pl = &s->poly; + const ds_st *d = &s->ds; + const blend_st *b = &s->blend[0]; + unsigned mode = pl->mode <= NVN_POLYGON_MODE_FILL ? pl->mode : NVN_POLYGON_MODE_FILL; + if (!d->dt || d->dw || (pl->offset_en >> mode & 1) || !(s->color.blend_en & 1) || + b->eq_rgb != NVN_BLEND_EQUATION_REVERSE_SUB || b->src_rgb != NVN_BLEND_FUNC_ONE || b->dst_rgb != NVN_BLEND_FUNC_ONE) + return 0; + if (d->df == NVN_DEPTH_FUNC_LESS || d->df == NVN_DEPTH_FUNC_LEQUAL) return -1; + return d->df == NVN_DEPTH_FUNC_GREATER || d->df == NVN_DEPTH_FUNC_GEQUAL; +} + // The values vk_pipeline_set_dynamic records for a state, as the commands take them. typedef struct { VkCullModeFlags cull; @@ -3296,7 +3319,7 @@ static void dyn_vals(const VkPipeState *s, DynVals *o) { o->sop[1][0] = stencil_op(d->fail_b), o->sop[1][1] = stencil_op(d->zpass_b), o->sop[1][2] = stencil_op(d->zfail_b); o->scmp[0] = compare_op(d->sf_f), o->scmp[1] = compare_op(d->sf_b); unsigned mode = pl->mode <= NVN_POLYGON_MODE_FILL ? pl->mode : NVN_POLYGON_MODE_FILL; - o->bias = (pl->offset_en >> mode & 1) != 0; + o->bias = (pl->offset_en >> mode & 1) || vk_pipeline_volume_bias(s); o->discard = s->raster_discard != 0, o->restart = s->prim_restart != 0; o->clamp = s->depth_clamp != 0, o->a2c = s->ms.a2c != 0; o->poly = polygon_mode(pl->mode); From 77cb787de78018ae43b5d130b37bcc8e9dea164c Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Wed, 18 Nov 2026 09:00:48 +0300 Subject: [PATCH 2/2] The water volume mask's depth bias can be turned off with volume_mask_bias = 0 (or TOTK_VOLUME_MASK_BIAS), a test setting to compare against --- docs/agents/symptoms.md | 2 +- docs/settings.md | 1 + runtime/cfg.c | 2 +- runtime/vk_pipeline.c | 5 ++++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index d17d6c75..483875d5 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -26,7 +26,7 @@ GPU semantics behind the graphics rows: [maxwell.md](../maxwell.md). The method | no sun shadows, wrong lighting | unwritten vertex outputs read as zero instead of (0,0,0,1) | the shader's output map | | one flat ambient colour | a 1-layer 2D array sampled as 2D | the view type the shader expects | | upside-down text | lower-left window origin | origin mode in the log | -| puddles and still water flicker: the reflection turns on and off in patches from frame to frame while the camera stands still, more with DLAA | the game's water volume mask: a volume's top face lies on the water surface and is tested LEQUAL against a depth rebuilt at quarter size, so rounding and the camera's sub-millimetre motion decide it (fixed: `vk_pipeline.c` `vk_pipeline_volume_bias` gives such draws a depth bias toward the viewer) | a `--capture` run: the puddle's brightness swing (`capture.py` frames, a region's mean) with and without the fix; in a frame dump, the quarter-size mask after the volume draw changes from frame to frame | +| puddles and still water flicker: the reflection turns on and off in patches from frame to frame while the camera stands still, more with DLAA | the game's water volume mask: a volume's top face lies on the water surface and is tested LEQUAL against a depth rebuilt at quarter size, so rounding and the camera's sub-millimetre motion decide it (fixed: `vk_pipeline.c` `vk_pipeline_volume_bias` gives such draws a depth bias toward the viewer) | a `--capture` run: the puddle's brightness swing (`capture.py` frames, a region's mean) with `TOTK_VOLUME_MASK_BIAS=0` and without; in a frame dump, the quarter-size mask after the volume draw changes from frame to frame | | flicker or a texture swapped with a similar one | the newest texture picked at shared storage instead of the one with the matching view | texture lookup by id in the dump | | with DLAA on, dust and small particles in fog (shrines) vanish, smear or behave oddly; the fog looks processed | DLSS ran at the bloom, after the fog and the particles, which have no motion vectors or jitter of their own (fixed: `vk_dlaa.c` effects_point runs DLSS at the first pass into the light target after the lighting, and the effects go over its picture) | `dlaa_effects_after = 0` (Screen tab) brings the old point back; the count "before the effects" in the `dlaa:` log line against the frames anti-aliased | | with DLAA on, the picture flickers from frame to frame: a little outdoors; the whole scene in a fogged shrine with an ability's targeting view (Ultrahand) open, where the fog also goes back under DLSS | DLSS ran before some effects in one frame and after them in the next: their order changes from frame to frame (the yard's half-size effects pass is in some frames only; the ability highlight, drawn into the light target with a D16 depth of its own, comes before or after the fog layer), and a rule that waited for one of them or took any D16 pass for the lighting followed it (fixed: `vk_dlaa.c` effects_point knows the lighting by its depth and runs DLSS right after it) | the `dlaa:` log line: "before the effects" must equal the frames anti-aliased in the world; a log of the passes into the light target per frame | diff --git a/docs/settings.md b/docs/settings.md index 18e61333..6f083969 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -164,6 +164,7 @@ Many rows below are developer tools that only act together with another setting | `vk_no_float64` (default `0`) | `1` clears the 64-bit float feature, so the translator lowers FP64 to FP32. | Shaders change, so the shader cache entries for it are made again; precision of FP64 shaders drops. | Caution | | `vk_ssbo_align` (default unset) | Power of two that storage buffer offsets are treated as needing, to test the unaligned path. | The translator and the draw code round storage offsets down to it; shaders are translated again. | Caution | | `vk_subgroup` (default `32`) | Subgroup size to pin where the driver allows it. | A value other than 32 changes what the translator is told about the GPU, and shaders are translated again. | Caution | +| `volume_mask_bias` (default `1`) | `0` leaves the game's water volume mask draws (a volume's front faces tested LEQUAL without depth writes, blended ONE/ONE with reverse subtract) without the depth bias toward the viewer that `vk_pipeline.c` gives them; `ab` alternates with `TOTK_AB`. | Puddles and still water then flicker again where the volume's top face lies on the water surface; a test setting. | Caution | | `vram_headroom` (default `1`) | `0` turns off the spare half-block of video memory that a background thread keeps per memory type. | Texture allocations on the render thread can wait for a new block again. | Caution | | `vs_outs` | `all` declares all 32 vertex shader output locations; otherwise only the union of the paired fragment inputs. | Larger vertex shaders and new pipelines; unwritten outputs read the same as before. | Caution | | `wb_eager` (default `1`) | With `sync_ahead` on, `1` copies small GPU-drawn targets that the game reads back to CPU-readable memory at the end of the frame that drew them, so the write-back at a fence wait does not wait for the GPU; `0` waits as before; `check` reads both ways and compares. Without `sync_ahead` it does nothing. | With `0` the game thread stalls about every ninth frame while the copy queues behind the frame on the GPU, which takes back part of the frame-ahead gain. `check` is slow and for testing. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index 7784a7e7..14b44d2e 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -66,7 +66,7 @@ static const char *const known[] = { "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", "SYNC_AHEAD", "SWKBD_TEST", "TEST_OOM", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_BLOCK_MIB", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_RELEASED_MIB", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UB_OVERLAY_OUT", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", - "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VMA_BLOCK_MIB", "VOLUME", "VOLUME_MUSIC", "VOLUME_SFX", "VOLUME_VOICE", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", + "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VMA_BLOCK_MIB", "VOLUME", "VOLUME_MASK_BIAS", "VOLUME_MUSIC", "VOLUME_SFX", "VOLUME_VOICE", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", "WALK_KEY", "WALK_SCALE", "WB_EAGER", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", }; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 7b366d69..ea1f420a 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3278,8 +3278,11 @@ uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { // moves the rebuilt depth but not the volume. Such a draw, when the game sets no polygon offset for it, gets a depth // bias toward the viewer of VOLUME_BIAS_UNITS plus VOLUME_BIAS_SLOPE times its depth slope (one pixel of it), so a // surface the face lies on counts as inside, as the test's equal case means; a surface farther from the face than a -// pixel of its slope is decided as before. +// pixel of its slope is decided as before. TOTK_VOLUME_MASK_BIAS=0 (test) leaves such draws as the game sets them. int vk_pipeline_volume_bias(const VkPipeState *s) { + static int knob = -1; + if (knob < 0) knob = rt_knob("VOLUME_MASK_BIAS", "1"); + if (!RT_KNOB_ON(knob)) return 0; const poly_st *pl = &s->poly; const ds_st *d = &s->ds; const blend_st *b = &s->blend[0];