diff --git a/runtime/vk.h b/runtime/vk.h index fcba861b..4b39b55f 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -258,6 +258,9 @@ void vk_texture_lock(void); void vk_texture_unlock(void); const VkTexImage *vk_texture_by_id_locked(int id, view_t *view, int *has_view); VkSampler vk_sampler_by_id_locked(int id); +// The slot's sampler with nearest filtering and no anisotropy: a depth texture read without a compare lands on one texel +// on the Switch although the game binds a linear sampler (MacNativeGames 6283599). VK_NULL_HANDLE for an empty slot. +VkSampler vk_sampler_point_by_id_locked(int id); uint32_t vk_texture_slots_gen(void); uint32_t vk_sampler_gen(void); // changes whenever a sampler slot is registered void vk_texture_gpu_written(const tex_t *t); // rendered or copied into by the GPU: no re-upload from storage diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index a10c2b44..85109c34 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -978,7 +978,14 @@ static void tex_info(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, if (img) view = get_view(ex, img, &v, hv, r->type, storage); if (img && storage) vk_texture_storage_bound_locked(img); if (img && !storage && ex->nfbl && in_fbl(ex, img->image)) layout = VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT; - if (!storage && !separate) samp = vk_sampler_by_id_locked((int)(h >> 20 & 0xfff)); + if (!storage && !separate) { + // A depth texture sampled without a compare reads one texel on the Switch, whatever the sampler's filter: + // blending neighbouring depths puts reconstructed positions in mid-air at object edges (shadow masks). + 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); + } } if (!view) { int di = dummy_index(res_view_type(r->type), ms); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 03e73e40..1e11761c 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -70,7 +70,8 @@ typedef struct { view_t v; tex_t t; } TexSlot; -typedef struct { samp_par p; VkSampler s; uint8_t used; } SampSlot; // s: vk_sampler_for(&p) once looked up +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) static CRITICAL_SECTION tex_lock; static INIT_ONCE tex_once = INIT_ONCE_STATIC_INIT; @@ -1462,7 +1463,7 @@ void vk_sampler_register(int id, const samp_par *s) { sslots = p, nsslots = n; } sslots[id].p = *s; - sslots[id].s = VK_NULL_HANDLE; + sslots[id].s = sslots[id].sp = VK_NULL_HANDLE; sslots[id].used = 1; samp_gen++; unlock(); @@ -1503,6 +1504,21 @@ VkSampler vk_sampler_by_id_locked(int id) { return v; } +VkSampler vk_sampler_point_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]; + if (!s->sp) { + samp_par k = s->p; + k.magf = NVN_MAG_FILTER_NEAREST; + k.minf = k.minf == NVN_MIN_FILTER_LINEAR ? NVN_MIN_FILTER_NEAREST : + k.minf > NVN_MIN_FILTER_LINEAR ? NVN_MIN_FILTER_NEAREST_MIPMAP_NEAREST : k.minf; + k.aniso = 1; + s->sp = sampler_for(c, &k); + } + return s->sp; +} + VkSampler vk_sampler_by_id(int id) { lock(); VkSampler v = vk_sampler_by_id_locked(id); diff --git a/tools/build_xlate.py b/tools/build_xlate.py index 64540466..31ecbbe5 100644 --- a/tools/build_xlate.py +++ b/tools/build_xlate.py @@ -1,7 +1,7 @@ """Build the shader translator: build/xlate/totk_xlate.exe (Ryujinx 1.3.2 + patches, NativeAOT, SPIR-V for Vulkan). Copies tools/vendor/ryujinx to build/xlate/ryujinx, applies tools/xlate/patches/*.patch there (tools/vendor stays -byte-identical; 0001-0004 are MNG's, 0005 and 0006 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build +byte-identical; 0001-0004 are MNG's, 0005-0009 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build tools and vswhere.exe (docs/toolchain-windows.md). Run with .venv. Next to the exe it writes totk_xlate.id: the translator identity, a sha256 over everything the build reads (this script, @@ -96,8 +96,8 @@ def prepare_sources() -> Path: for junk in list(src.glob("src/*/obj")) + list(src.glob("src/*/bin")): shutil.rmtree(junk) patches = sorted((XLATE / "patches").glob("*.patch")) - if len(patches) != 8: - sys.exit(f"expected 8 patches, found {len(patches)}") + if len(patches) != 9: + sys.exit(f"expected 9 patches, found {len(patches)}") for patch in patches: # git apply works outside a repository when given a directory; --check first so a failure names the patch. for extra in (["--check"], []): diff --git a/tools/xlate/patches/0009-round-d16-array-compare-reference.patch b/tools/xlate/patches/0009-round-d16-array-compare-reference.patch new file mode 100644 index 00000000..ec4e1edc --- /dev/null +++ b/tools/xlate/patches/0009-round-d16-array-compare-reference.patch @@ -0,0 +1,21 @@ +--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs ++++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs +@@ -1283,6 +1283,18 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv + if (isShadow) + { + dRef = Src(AggregateType.FP32); ++ ++ // TOTK: the shadow cascades are D16 array textures. Maxwell rounds the compare reference to the texture's ++ // 16-bit precision before the compare; Vulkan drivers may compare the full float (AMD does), so a lit ++ // surface fails its own compare by a fraction of a step and the whole face reads as shadowed. Array ++ // compares only: in TotK those are the D16 cascades (MacNativeGames 6283599 rounds the same ones). ++ if (isArray) ++ { ++ SpvInstruction d16Fp32 = context.TypeFP32(); ++ SpvInstruction d16Clamped = context.GlslFClamp(d16Fp32, dRef, context.Constant(d16Fp32, 0f), context.Constant(d16Fp32, 1f)); ++ SpvInstruction d16Scaled = context.FAdd(d16Fp32, context.FMul(d16Fp32, d16Clamped, context.Constant(d16Fp32, 65535f)), context.Constant(d16Fp32, 0.5f)); ++ dRef = context.FMul(d16Fp32, context.GlslFloor(d16Fp32, d16Scaled), context.Constant(d16Fp32, 1f / 65535f)); ++ } + } + + SpvInstruction[] derivatives = null;