From 1d7bc90848024e087ff0562673cce2bc08aaea02 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Sat, 7 Nov 2026 10:56:46 -0500 Subject: [PATCH] Fix shadows, colour writes and present pacing on drivers that read Vulkan's undefined cases differently. A depth-compare read through a sampler whose compare is off now gets the slot's comparing sampler, as the Switch compares there and only NVIDIA's driver did so on its own. Colour components a fragment shader does not write are masked out, as Maxwell leaves them alone. Without mailbox presentation the swapchain takes relaxed FIFO, or immediate, so a late frame no longer halves the frame rate. NX2NAVI_VK_NOOPT=1 builds pipelines without the driver's optimiser, for a driver whose compiler fails on one. Co-Authored-By: Claude Opus 5.5 --- docs/maxwell.md | 4 ++-- runtime/cfg.c | 7 ++++--- runtime/gfx.h | 1 + runtime/gfx_present.c | 14 ++++++++++++-- runtime/gfx_shader.c | 5 +++++ runtime/vk.h | 3 +++ runtime/vk_exec_draw.c | 3 ++- runtime/vk_pipeline.c | 24 +++++++++++++++++++++--- runtime/vk_texture.c | 33 +++++++++++++++++++++++++++++---- 9 files changed, 79 insertions(+), 15 deletions(-) diff --git a/docs/maxwell.md b/docs/maxwell.md index a607d605..fbe0a58b 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -19,7 +19,7 @@ Source shorthands: | # | Maxwell / NVN | Vulkan / AMD | Effect / ours | Source | |---|---|---|---|---| -| 1.1 | **Colour target the fragment shader does not write keeps its old texel**, even with blending enabled. | Undefined value written/blended. | Zero write mask and no blend for unwritten outputs. Already fixed here. | — | +| 1.1 | **Colour target the fragment shader does not write keeps its old texel**, even with blending enabled. | Undefined value written/blended. | Zero write mask and no blend for unwritten outputs. The same holds per component: the shader header's omap names the components a target gets, and a shader that writes RGB leaves the alpha alone; AMD writes its export registers' leftovers there (NaN and saturated pixels the bloom spreads). Ours: `vk_pipeline.c` `target_wmask` masks the target and the components outside the translator's `frag_out_map`. | — | | 1.2 | **Unwritten vertex outputs read as (0, 0, 0, 1)** in the next stage, including the unwritten components of a partly written output (VS writes xyz, FS divides by w). | FS input with no VS output: interface mismatch, value undefined; an unwritten component of a written output is undefined (AMD: whatever the register holds, often 0). | The translator initialises exactly the components the next stage reads, but only when stages are linked (XL `Translation/Translator.cs:212-227`, `NextInputAttributesComponents`); position starts (0,0,0,1) (`:259`). Symptom: sun shadow lookups divided by w = 0, scene dim, no shadows. Ours: missing outputs padded with (0, 0, 0, 1), unlinked outputs initialised by translator patch 0006 (section 6, check 2). | XL | | 1.3 | **Clears honour the scissor and the clear's own channel / stencil write mask**, and may come between draws inside a pass. `SET_CLEAR_SURFACE_CONTROL` (0x10f8) has `RESPECT_STENCIL_MASK`, `USE_CLEAR_RECT`, `USE_SCISSOR0`, `USE_VIEWPORT_CLIP0`. | `vkCmdClearAttachments` takes a rect but **ignores the colour write mask**; load-op clears are whole-attachment. | A triangle drawn with the clear's mask reproduces it. NX2navi uses mid-pass, partial-mask and partial-scissor clears. Hardware recipe: `CLEAR_BUFFERS` (`0x674` = 0x19d0) carries the colour channel mask (bits 2-5), depth (0), stencil (1), target id (6-9) and layer (10-20) in one word; with `ClearBufferFlags` (`0x43E` = 0x10f8) set once to `StencilMask \| Scissor`, every clear is cut by scissor 0 and its stencil bits by the stencil front write mask register, and a colour clear does not program `ColorWriteMask` (`0x680`). A stencil clear writes `StencilFrontMask` (`0x4E7`) and `ClearStencil` (`0x368`) just before the clear and restores the shadowed state after. Multisample raster is switched off around a colour clear. Arrays: a macro repeats the clear for every layer. No pass concept: clears sit between draws. Ours: masked colour and stencil clears are **skipped** (check 3). | OGD 0x10f8, 0x19d0 | | 1.4 | **Conditional rendering** (`SET_RENDER_ENABLE_A..C` 0x1550-0x1558, NVN `SetRenderEnableConditional`) compares two 64-bit counters at `addr` and `addr+16` on the GPU, in command order. NX2navi wraps nearly every clear in it. A `TIMESTAMP` report writes 0 to the counter field. | `VK_EXT_conditional_rendering` tests one 32-bit word against zero. | Ignoring it leaves light-volume depth uncleared: black 3D scene. Ours: evaluated on the CPU at record time; a report from an unfinished submission counts as 0 (check 5). | OGD | @@ -55,7 +55,7 @@ Source shorthands: | 2.10 | **Texture gather rounding**: Maxwell sub-texel precision differs from AMD/Intel; edge texels picked by TLD4 differ by one. | Implementation-defined `subTexelPrecisionBits`. | A sub-pixel offset on gathers on non-NVIDIA hosts fixes shadow artifacts at object edges. Ours: the translator biases gather coordinates by the host's `subTexelPrecisionBits` on every non-NVIDIA GPU (`vk_caps.c` → job `gather_bias` → XL `TexturePass.InsertCoordGatherBias`); without it RADV drew dashed dark lines along wall and floor edges that changed with the camera angle. `NX2NAVI_GATHER_BIAS=0` turns it off. | — | | 2.11 | **Gather offsets**: `TLD4.PTP` takes four per-texel offsets, possibly from registers. | `ConstOffsets` must be constant. | Ours: `QueryHostSupportsNonConstantTextureOffset() => false` makes the translator emulate register offsets (`tools/xlate/Program.cs`). | XL `Translation/Optimizations` (TexturePass) | | 2.13 | **TXD gradient register order**: NX2navi's terrain shaders fill the gradient registers of a `TXD` per coordinate (du/dx, du/dy, dv/dx, dv/dy). | Vulkan's `Grad` takes dPdx (du/dx, dv/dx) and dPdy (du/dy, dv/dy). | The translator's `EmitTxd` takes the first two registers as dPdx and the last two as dPdy, so the mip level came from the wrong derivatives and was too sharp on terrain faces turned across the screen: patches that followed the faces crawled when the camera moved (found from an F9 frame: a gradient sample's coordinate and gradients traced back to `DPdx` and `DPdy` of the interpolants). Ours: `gfx_shader.c` `patch_txd_transpose` swaps the two inner components of every two-component gradient pair in the loaded SPIR-V (the shader cache is untouched). `NX2NAVI_TXD_TRANSPOSE=0` turns it off. Only 2D gradients are handled; 3D and cube ones are left as translated. | — | -| 2.12 | **Depth compare** (`TSC` depth compare + `TIC_4_DEPTH_TEXTURE`), min/max reduction filtering. | `compareEnable`; reduction mode needs `samplerFilterMinmax` and is not allowed with compare. | Ours: reduction dropped when compare is on (`vk_texture.c` `make_sampler`). | — | +| 2.12 | **Depth compare** (`TSC` depth compare + `TIC_4_DEPTH_TEXTURE`), min/max reduction filtering. | `compareEnable`; reduction mode needs `samplerFilterMinmax` and is not allowed with compare. | Ours: reduction dropped when compare is on (`vk_texture.c` `make_sampler`). A depth-compare instruction through a sampler whose compare is off still compares (the game's shadow reads do this); in Vulkan that is undefined, NVIDIA's driver compares and AMD's returns the depth. Ours: such a read through a bound sampler gets the slot's sampler with the compare on (`vk_sampler_compare_by_id_locked`; the game's function, else LEQUAL; `NX2NAVI_SHADOW_COMPARE=0` off). Reads through the sampler pool (bindless) still take the game's sampler as it is. | — | | 2.13 | **ASTC / BC**: native ASTC; zero ASTC block = error colour on hosts. | AMD and NVIDIA desktop GPUs lack ASTC: decode to RGBA8. | Ours: native upload where the device samples ASTC, else a compute-shader decode (`vk_astc.c`, `NX2NAVI_ASTC`), CPU decode (`tex_astc.c`) as the fallback. | `runtime/vk_astc.c` | | 2.14 | **Linear CPU-written textures** (`FLAGS_LINEAR`, small ramps, glyph caches): CPU writes visible to the GPU directly. | Must re-upload or use linear images over host memory. | Without them a sky drawn from 64×1 RGBA16F ramps is black. | — | diff --git a/runtime/cfg.c b/runtime/cfg.c index c815a09a..c8ba9566 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -54,10 +54,10 @@ static const char *const known[] = { "RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", - "SHADER_MEMO", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", + "SHADER_MEMO", "SHADOW_COMPARE", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", - "SWKBD_TEST", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", + "SWKBD_TEST", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NOOPT", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", @@ -133,7 +133,7 @@ static const CfgMeta metas[] = { {"HDR_UI_NITS", "203", "80..500", LIVE}, {"HDR_HUE", "80", "0..100", LIVE}, {"DEVICE_PROFILE", "auto", "auto|desktop|deck", RESTART}, {"SHADOW_RES", "game", "game|256|512|768|1024|1536|2048", RESTART}, - {"SHADOW_SCALE", "1", "1..4", RESTART}, + {"SHADOW_SCALE", "1", "1..4", RESTART}, {"SHADOW_COMPARE", "1", "0|1", RESTART}, // vk_texture.c {"SHADOW_RANGE", "100", "50..400", RESTART}, {"CONTACT_SHADOWS", "0", "0..100", RESTART}, {"PCSS", "0", "0..200", RESTART}, // Game @@ -179,6 +179,7 @@ static const CfgMeta metas[] = { // Vulkan {"VK_VALIDATION", "0", "0|1", RESTART}, {"VK_DYN_VERTEX", "", "0|1", RESTART}, {"VK_DYN_A2C", "", "0|1", RESTART}, {"VK_SUBGROUP_PIN", "1", "0|1", RESTART}, + {"VK_NOOPT", "0", "0|1", RESTART}, // vk_pipeline.c noopt_flag // diagnostics {"TRACE", "", NULL, RESTART}, {"FLIGHT", "0", "0|1", RESTART}, {"FLIGHT_SPIKE_MS", "100", "0..60000", RESTART}, {"WB_FENCE_LIMIT", "65536", "0..1073741824", RESTART}, {"WB_MERGE", "1", "0|1", RESTART}, {"WB_EXACT", "1", "0|1", RESTART}, diff --git a/runtime/gfx.h b/runtime/gfx.h index fe91ad0d..7c7e1657 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -115,6 +115,7 @@ int gfx_shader_resources(const GfxShader *s, int kind, const GfxShaderRes **res) uint32_t gfx_shader_slot_mask(const GfxShader *s); // texture/image slots whose type its translation read uint32_t gfx_shader_inputs(const GfxShader *s, GfxShaderIo io[32]); // Input Locations (VS attributes, FS varyings) as a mask uint32_t gfx_shader_outputs(const GfxShader *s, GfxShaderIo io[32]); +uint32_t gfx_shader_omap(const GfxShader *s); // FS: colour components it writes, 4 bits per target (SPH omap); 0 unknown uint32_t gfx_shader_mvprev_banks(const GfxShader *s); // a last-frame variant: the banks (bit n: cn) its position reads // Output Locations (VS varyings, FS colour targets) void gfx_shader_local_size(const GfxShader *s, uint32_t local[3]); // CS: the LocalSize execution mode; 1, 1, 1 otherwise diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 6b837705..f2b02a7e 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -256,13 +256,23 @@ static VkPresentModeKHR pick_present_mode(VkPhysicalDevice pd) { if (n > 16) n = 16; vkGetPhysicalDeviceSurfacePresentModesKHR(pd, g_surface, &n, m); VkPresentModeKHR got = VK_PRESENT_MODE_FIFO_KHR; - for (uint32_t i = 0; i < n; i++) if (m[i] == want) got = want; + int relaxed = 0, immediate = 0; + for (uint32_t i = 0; i < n; i++) { + if (m[i] == want) got = want; + relaxed |= m[i] == VK_PRESENT_MODE_FIFO_RELAXED_KHR, immediate |= m[i] == VK_PRESENT_MODE_IMMEDIATE_KHR; + } + // Mailbox missing (several AMD drivers): plain FIFO holds a frame that misses a refresh to the next one, and the + // game, paced at 60 on a 60 Hz display, drops to 30 at every slow frame. Relaxed FIFO shows a late frame at once + // (a tear that once) and waits for the refresh otherwise; immediate where that is missing too. + if (want == VK_PRESENT_MODE_MAILBOX_KHR && got != want) + got = relaxed ? VK_PRESENT_MODE_FIFO_RELAXED_KHR : immediate ? VK_PRESENT_MODE_IMMEDIATE_KHR : got; const char *name = got == VK_PRESENT_MODE_MAILBOX_KHR ? "mailbox" : got == VK_PRESENT_MODE_IMMEDIATE_KHR ? "immediate" : "fifo"; __atomic_store_n(&g_present_got, name, __ATOMIC_RELEASE); static int logged = -1; // the mode last logged: a change from the menu shows in the log if ((int)got != logged) { logged = (int)got; - rt_log("gfx: present mode %s (NX2NAVI_VK_PRESENT=%s)", name, e); + rt_log("gfx: present mode %s (NX2NAVI_VK_PRESENT=%s)", + got == VK_PRESENT_MODE_FIFO_RELAXED_KHR ? "fifo relaxed" : name, e); } return got; } diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index 5ccfe053..80adde14 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -88,6 +88,8 @@ struct GfxShader { GfxShaderIo in[32], out[32]; uint32_t local[3]; uint32_t mvprev_banks; // .res.json "mvprev_banks" (patch 0015), 0 none + uint32_t omap; // FS: .res.json "frag_out_map", the colour components it writes + // (4 bits per target, the SPH omap); 0 unknown (older translations) }; static PlatMutex cache_lock = PLAT_MUTEX_INIT; // memory cache @@ -861,6 +863,7 @@ static void reflect(GfxShader *s) { s->stage = !strcmp(stage, "cs") ? GFX_STAGE_CS : !strcmp(stage, "ps") ? GFX_STAGE_FS : !strcmp(stage, "ts") ? GFX_STAGE_TS : !strcmp(stage, "ms") ? GFX_STAGE_MS : GFX_STAGE_VS; s->mvprev_banks = (uint32_t)json_int(js, js + strlen(js), "mvprev_banks", 0); + s->omap = s->stage == GFX_STAGE_FS ? (uint32_t)json_int(js, js + strlen(js), "frag_out_map", 0) : 0; for (int k = 0; k < GFX_RES_KINDS; k++) { int count = json_objects(js, kinds[k], NULL, s, k); s->res[k] = calloc(count ? count : 1, sizeof *s->res[k]); @@ -1886,6 +1889,8 @@ uint32_t gfx_shader_inputs(const GfxShader *s, GfxShaderIo io[32]) { uint32_t gfx_shader_mvprev_banks(const GfxShader *s) { return s->mvprev_banks; } +uint32_t gfx_shader_omap(const GfxShader *s) { return s->omap; } + uint32_t gfx_shader_outputs(const GfxShader *s, GfxShaderIo io[32]) { if (io) memcpy(io, s->out, sizeof s->out); return s->out_mask; diff --git a/runtime/vk.h b/runtime/vk.h index ed0c15fb..205eb09b 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -407,6 +407,9 @@ VkSampler vk_sampler_by_id_locked(int id); // on the Switch although the game binds a linear sampler. VK_NULL_HANDLE // for an empty slot. VkSampler vk_sampler_point_by_id_locked(int id); +// The slot's sampler with its compare on, for a depth-compare read (vk_texture.c, at the definition); the slot's own +// sampler when it compares already. VK_NULL_HANDLE for an empty slot. +VkSampler vk_sampler_compare_by_id_locked(int id); uint32_t vk_texture_slots_gen(void); int vk_texture_slot_count_locked(void); void vk_texture_create_budget_locked(int n); // slot lookups create at most n entries (mapped storage only); -1 any diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 4b8586fb..21f3fb92 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -1564,7 +1564,8 @@ static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int sto int sid = (int)(h >> 20 & 0xfff); int point = img && (img->aspect & VK_IMAGE_ASPECT_DEPTH_BIT) && !(r->type & 0x400) && vk_exec_view_aspect(img, &v, hv) != VK_IMAGE_ASPECT_STENCIL_BIT; - samp = point ? vk_sampler_point_by_id_locked(sid) : vk_sampler_by_id_locked(sid); + samp = point ? vk_sampler_point_by_id_locked(sid) + : r->type & 0x400 ? vk_sampler_compare_by_id_locked(sid) : vk_sampler_by_id_locked(sid); } } if (!view) { diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 24a9d08a..453ea21a 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -1330,6 +1330,17 @@ static int target_count(const VkPipeState *s) { static int is_src1(unsigned f) { return f >= NVN_BLEND_FUNC_SRC1_COLOR && f <= NVN_BLEND_FUNC_ONE_MINUS_SRC1_ALPHA; } // Runs per draw (gkey_fill, target_wmask): only the blend factors of blend-enabled targets are read until one is SRC1, // which NX2navi never sets (0 of 876 pipelines in the field and fight saves). +// NX2NAVI_VK_NOOPT=1: pipelines built without the driver's optimiser, for a driver whose shader compiler crashes on +// one (the test switch for such a report); slower shaders, the same picture. +static VkPipelineCreateFlags noopt_flag(void) { + static int on = -1; + if (on < 0) { + on = cfg_int("NX2NAVI_VK_NOOPT") != 0; + if (on) rt_log("vk_pipeline: pipelines built without the driver's optimiser (NX2NAVI_VK_NOOPT)"); + } + return on ? VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT : 0; +} + static int uses_src1(const VkPipeState *s) { for (uint32_t m = s->color.blend_en & 0xffu; m; m &= m - 1) { int i = __builtin_ctz(m); @@ -1347,9 +1358,16 @@ static int uses_src1(const VkPipeState *s) { // too: NX2NAVI's half-res transparency pass binds its linear depth (R32F) as target 7, samples it in the same draws and // writes only targets 0-2, 4 and 5; zeroed, the depth that later passes rebuild from it was 0 in tiles, and the fog // over distant geometry showed rectangles of clear sky. +// The same holds per component: a shader that writes a target's RGB and not its alpha leaves the alpha alone on +// Maxwell, while Vulkan writes undefined values, and AMD's driver writes whatever its export registers held (NaN and +// saturated pixels the bloom then spreads). Components outside the shader's omap are masked out, where it is known. static uint32_t target_wmask(const VkPipeState *s, int i) { uint32_t m = s->chmask.mask >> (4 * i) & 0xf; if (s->fs && !(gfx_shader_outputs(s->fs, NULL) >> i & 1)) m = 0; + if (s->fs) { + uint32_t c = gfx_shader_omap(s->fs) >> (4 * i) & 0xf; + if (c) m &= c; + } if (i == 1 && s->fs && P.f_dual_src && uses_src1(s)) m = 0; // output 1 is the second blend source (spirv_dual_source) return m; } @@ -1563,7 +1581,7 @@ static VkPipeline create_graphics(const VkPipeState *s, const GKey *k, const Lay .stageCount = n_stages, .pStages = stages, .pVertexInputState = mesh ? NULL : &vi, .pInputAssemblyState = mesh ? NULL : &ia, .pViewportState = &vp, .pRasterizationState = &rs, .pMultisampleState = &ms, .pDepthStencilState = &ds, .pColorBlendState = &cb, - .pDynamicState = &dy, .layout = l->layout}; + .pDynamicState = &dy, .layout = l->layout, .flags = noopt_flag()}; if (k->fbl & 1) pi.flags |= VK_PIPELINE_CREATE_COLOR_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT; if (k->fbl & 2) pi.flags |= VK_PIPELINE_CREATE_DEPTH_STENCIL_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT; uint64_t p0 = perf_begin(); @@ -1633,7 +1651,7 @@ static VkPipeline lib_create(int kind, const VkPipeState *s, const GKey *k, VkPi VkPipelineDynamicStateCreateInfo dy = {.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, .dynamicStateCount = P.n_dyn, .pDynamicStates = P.dyn}; VkGraphicsPipelineCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, .pNext = &ri, - .flags = VK_PIPELINE_CREATE_LIBRARY_BIT_KHR, .pDynamicState = &dy}; + .flags = VK_PIPELINE_CREATE_LIBRARY_BIT_KHR | noopt_flag(), .pDynamicState = &dy}; VkShaderModule m = VK_NULL_HANDLE; VkPipelineShaderStageCreateInfo st = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .pName = "main"}; VkPipelineShaderStageRequiredSubgroupSizeCreateInfo rq; @@ -2728,7 +2746,7 @@ static VkPipeline compute_create(const GfxShader *cs, const Layout *l) { VkComputePipelineCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, .stage = {.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, .stage = VK_SHADER_STAGE_COMPUTE_BIT, .module = m, .pName = "main"}, - .layout = l->layout}; + .layout = l->layout, .flags = noopt_flag()}; pin_subgroup32(&ci.stage, &rqc, code, words); // Subgroup operations in a workgroup whose width is a whole number of subgroups need every subgroup full: the // driver may otherwise run partial ones, and a ballot over the culling's candidates then counted differently from diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index b44b0e13..cff7b7b8 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -75,8 +75,9 @@ typedef struct { view_t v; tex_t t; } TexSlot; -typedef struct { samp_par p; VkSampler s, sp; uint8_t used; } SampSlot; // s: vk_sampler_for(&p) once looked up; sp: - // its point-sampled form (vk_sampler_point_by_id_locked) +typedef struct { samp_par p; VkSampler s, sp, sc; uint8_t used; } SampSlot; // s: vk_sampler_for(&p) once looked up; sp: + // its point-sampled form (vk_sampler_point_by_id_locked), + // sc its comparing form (vk_sampler_compare_by_id_locked) static PlatRMutex tex_lock; static PlatOnce tex_once = PLAT_ONCE_INIT; @@ -2023,7 +2024,7 @@ void vk_sampler_register(int id, const samp_par *s) { sslots = p, nsslots = n; } sslots[id].p = *s; - sslots[id].s = sslots[id].sp = VK_NULL_HANDLE; + sslots[id].s = sslots[id].sp = sslots[id].sc = VK_NULL_HANDLE; sslots[id].used = 1; samp_gen++; unlock(); @@ -2043,7 +2044,7 @@ void vk_sampler_dlss_bias(float b) { lock(); if (b != dlss_bias) { dlss_bias = b; - for (int i = 0; i < nsslots; i++) sslots[i].s = sslots[i].sp = VK_NULL_HANDLE; // looked up again (the key moves) + for (int i = 0; i < nsslots; i++) sslots[i].s = sslots[i].sp = sslots[i].sc = VK_NULL_HANDLE; // looked up again (the key moves) samp_gen++; } unlock(); @@ -2100,6 +2101,30 @@ VkSampler vk_sampler_point_by_id_locked(int id) { return s->sp; } +// A depth-compare read through a sampler without a compare is undefined in Vulkan. NVIDIA's driver compares anyway; AMD's +// returns the raw depth, so the game's shadow tests (which sample this way) come out wrong there. Such a read gets the +// slot's sampler with the compare on: the game's function when it set a valid one, LEQUAL otherwise, as NVIDIA's driver +// compares. NX2NAVI_SHADOW_COMPARE=0 binds the game's sampler as it is. +VkSampler vk_sampler_compare_by_id_locked(int id) { + VkCtx *c = vk_ctx(); + if (!c || id < 0 || id >= nsslots || !sslots[id].used) return VK_NULL_HANDLE; + SampSlot *s = &sslots[id]; + static int on = -1; + if (on < 0) on = cfg_int("NX2NAVI_SHADOW_COMPARE") != 0; + if (s->p.cmp_mode || !on) return vk_sampler_by_id_locked(id); + if (!s->sc) { + samp_par k = s->p; + k.cmp_mode = 1; + if (k.cmp_func < NVN_COMPARE_FUNC_NEVER || k.cmp_func > NVN_COMPARE_FUNC_ALWAYS) k.cmp_func = NVN_COMPARE_FUNC_LEQUAL; + static int told; + if (told++ < 8) + rt_log("vk: a depth-compare read through sampler slot %d without a compare (function %u): compared with %u", id, + (unsigned)s->p.cmp_func, (unsigned)k.cmp_func); + s->sc = sampler_for(c, &k); + } + return s->sc; +} + VkSampler vk_sampler_by_id(int id) { lock(); VkSampler v = vk_sampler_by_id_locked(id);