From 58efccd883db4371fcefdaeef1b5c7a96481abe1 Mon Sep 17 00:00:00 2001 From: FIocker <4461558+FIocker@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:22:34 +0100 Subject: [PATCH] Scale the slope-scaled depth bias with the shadow map scale, so the larger maps keep the bias the game asked for. The slope term is a bias per unit of depth slope across one pixel of the target. A shadow map made k times larger on each side (shadow_scale 2 to 4) has 1/k of the slope across each texel, so the game's factor alone gave k times less bias on sloped surfaces than at the game's own size, which is what shows as acne. vk_exec_draw.c slope_bias multiplies the factor by the pass's scale for both the direct draws (record_fixed) and the queued mesh draws; the constant term counts the depth format's smallest steps and keeps its value. Nothing changes at shadow_scale 1. Co-Authored-By: Claude Opus 5.5 --- nx2navi.ini.example | 3 ++- runtime/vk_exec_draw.c | 13 +++++++++++-- runtime/vk_texture.c | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 006d119c..9c286fe6 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -468,7 +468,8 @@ # Shadow map resolution: the game's sun cascades are 960x960 (and its static shadow maps 1440 and 720) whatever the game # resolution; 2 to 4 draws them that many times larger on each side, for sharper shadows. 1 = the game's. The game's -# shadow edge filter is narrowed by the same factor (translator patch 0011), so close shadow edges don't look doubled. +# shadow edge filter is narrowed by the same factor (translator patch 0011), so close shadow edges don't look doubled, +# and the game's slope-scaled depth bias grows by it, so the larger maps don't shadow the surfaces they are cast on. # Read at start. # shadow_scale = 1 # Sun shadow cascade range in percent of the game's (its cascades end at 20, 40, 80 and 160 m), 50 to 400: higher moves diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index d87d6a3c..57201748 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -3852,6 +3852,15 @@ static VkViewport draw_viewport(VkExec *ex) { return v; } +// The slope-scaled depth bias of a draw. The term is the bias per unit of depth slope across one pixel of the target, +// and a shadow map made k times larger on each side (NX2NAVI_SHADOW_SCALE, vk_texture.c shadow_scale_of, pass_scale) +// has 1/k of the slope across each of its texels: the game's factor alone gives k times less bias than it asked for +// on sloped surfaces, which is what shows as acne (stripes of self-shadow over slanted ground and walls). The +// constant term counts the depth format's smallest steps, which do not change with the size, and keeps its value. +static float slope_bias(const VkExec *ex) { + return ex->pass_scale > 1 ? ex->poly_offset.factor * (float)ex->pass_scale : ex->poly_offset.factor; +} + static void record_fixed(VkExec *ex) { VkCommandBuffer cb = ex->cb; if (ex->dirty & VK_EXEC_DIRTY_VIEWPORT) { @@ -3874,7 +3883,7 @@ static void record_fixed(VkExec *ex) { bx = e && *e ? (float)atof(e) : 1.0f; if (bx != 1.0f) rt_log("vk: NX2NAVI_DEPTH_BIAS_X: constant depth bias multiplied by %.1f", bx); } - vkCmdSetDepthBias(cb, ex->poly_offset.units * bx, ex->poly_offset.clamp, ex->poly_offset.factor); + vkCmdSetDepthBias(cb, ex->poly_offset.units * bx, ex->poly_offset.clamp, slope_bias(ex)); ex->recorded++; } if (ex->dirty & VK_EXEC_DIRTY_BLEND_CONST) { @@ -5386,7 +5395,7 @@ static void mvobj_draw_(VkExec *ex, const p_draw_idx *d, VkBuffer ib, VkDeviceSi } q->st = (uint32_t)MQ.nst - 1; q->v = draw_viewport(ex), q->sc = vk_exec_scissor(ex); - q->bias[0] = ex->poly_offset.units, q->bias[1] = ex->poly_offset.clamp, q->bias[2] = ex->poly_offset.factor; + q->bias[0] = ex->poly_offset.units, q->bias[1] = ex->poly_offset.clamp, q->bias[2] = slope_bias(ex); q->vb = mo_vertex_buffers(ex); q->ib = ib, q->ib_off = ib_off; q->it = d->itype == NVN_INDEX_TYPE_UNSIGNED_SHORT ? VK_INDEX_TYPE_UINT16 : VK_INDEX_TYPE_UINT32; diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index c2972f8a..532ee4a6 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -933,8 +933,8 @@ static void init_new(Tex *e) { // sun cascades into a 960x960 DEPTH16 2D array of 4 layers and its static shadows into 1440x1440 and 720x720 arrays of 2, // whatever the output resolution, and shrinks the cascades under load (872, 848, 800, 480 and so on). Those are made // larger here: render passes into them scale the game's viewports and scissors (vk_exec.c resolve_rt, pass_scale), -// copies scale their regions (vk_exec_xfer.c sub_resolve), and shaders sample them with normalised coordinates, so the -// game sees nothing of it. A map whose storage the CPU writes (mirrored) or that aliases pool memory keeps its size, +// copies scale their regions (vk_exec_xfer.c sub_resolve), draws into them scale the game's slope-scaled depth bias +// (vk_exec_draw.c slope_bias), and shaders sample them with normalised coordinates, so the game sees nothing of it. A map whose storage the CPU writes (mirrored) or that aliases pool memory keeps its size, // as its texels must match the guest layout. static int shadow_scale(void) { static int scale = -1;