From 119884c52e3b0df309950c8fe7bf5639884dc43b Mon Sep 17 00:00:00 2001 From: Wellington Dias Date: Thu, 29 Oct 2026 22:32:40 -0300 Subject: [PATCH] Bias gather coordinates on non-NVIDIA GPUs, so shadows lose the dashed lines along edges. AMD and Intel round a gather coordinate that sits between two texels the other way from Maxwell, so shadow-map filtering picked the neighbouring texel and drew dark dashes along wall and floor edges that moved with the camera (docs/maxwell.md 2.10; BotW had the same on AMD). vk_caps.c passes the device's subTexelPrecisionBits to the translator for every non-NVIDIA GPU, the job carries it as gather_bias, and totk_xlate answers QueryHostGatherBiasPrecision with it, which turns on Ryujinx's own coordinate bias (TexturePass.InsertCoordGatherBias). The answer joins the translator identity, so those shaders get their own cache keys. TOTK_GATHER_BIAS=0 turns it off. --- docs/maxwell.md | 3 ++- runtime/gfx.h | 2 +- runtime/gfx_shader.c | 13 ++++++++----- runtime/vk_caps.c | 8 +++++++- tools/xlate/Program.cs | 3 +++ 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/maxwell.md b/docs/maxwell.md index c8fa4886..dc71a041 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -66,7 +66,7 @@ Source shorthands: | 2.7 | **Swizzles** per texture and per view (`TextureBuilderSetSwizzle`, `TextureViewSetSwizzle`); BotW layout captures rebuild a texture on the same storage with (1, 1, 1, R) or alpha 1. Default per-format mapping R8 → (R, 0, 0, 1). | `VkComponentMapping` on the view. | NXN: font atlas white boxes, HUD dark boxes without it. Ours: mapped (`vk_exec_draw.c` `get_view`). | NXN | | 2.8 | **Sample dimension comes from the instruction**: a non-array sample of an array texture reads layer 0; 1,512 BotW shaders sample a cube as a cube array. | View type must match the declared type. | NXN: 2D array bound as 2D gave one pale colour (washed-out scene); cubes created as 6-layer arrays with cube / cube-array views. Ours: views per resource type (`get_view`). | NXN | | 2.9 | **LOD**: TSC min/max LOD clamp, LOD bias, `TWO_D_NO_MIPMAP` texture type, a min filter without mips stays on the base level; TIC carries its own max anisotropy and aniso quality fields. | `minLod`/`maxLod`, `mipLodBias` limited by `maxSamplerLodBias`. | yuzu: anisotropy heuristics for mods forcing trilinear (YZ-2306, PR 10744). Ours: non-mip filter → `maxLod = minLod + 0.25`, bias clamped (`vk_texture.c` `make_sampler`). | G80TEX | -| 2.10 | **Texture gather rounding**: Maxwell sub-texel precision differs from AMD/Intel; edge texels picked by TLD4 differ by one. | Implementation-defined `subTexelPrecisionBits`. | yuzu adds a sub-pixel offset to gathers on non-NVIDIA hosts: fixed BotW shadow artifacts at object edges ("shader_recompiler: Add subpixel offset for correct rounding at ImageGather", , title only). | — | +| 2.10 | **Texture gather rounding**: Maxwell sub-texel precision differs from AMD/Intel; edge texels picked by TLD4 differ by one. | Implementation-defined `subTexelPrecisionBits`. | yuzu adds a sub-pixel offset to gathers on non-NVIDIA hosts: fixed BotW shadow artifacts at object edges ("shader_recompiler: Add subpixel offset for correct rounding at ImageGather", , title only). Ours: the translator biases gather coordinates by the host's `subTexelPrecisionBits` on every non-NVIDIA GPU (`vk_caps.c` → job `gather_bias` → Ryujinx `TexturePass.InsertCoordGatherBias`); TotK on RADV drew dashed dark lines along wall and floor edges in the prologue caves without it. `TOTK_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 Ryujinx emulate register offsets (`tools/xlate/Program.cs`). | RYU-SH `Translation/Optimizations` (TexturePass) | | 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`). | G80TEX | | 2.13 | **ASTC / BC**: native ASTC; zero ASTC block = error colour on hosts. | AMD and NVIDIA desktop GPUs lack ASTC: decode to RGBA8. yuzu offered ASTC recompression to BC1/BC3 for low VRAM (YZ-2305, PR 10398). | Ours: native upload where the device samples ASTC, else a compute-shader decode (`vk_astc.c`, `TOTK_ASTC`), CPU decode (`tex_astc.c`) as the fallback. | `runtime/vk_astc.c` | @@ -141,6 +141,7 @@ Source shorthands: | TotK | AO broken above 1008p mods | rescale thresholds | YZ-2306 PR 10675 | | TotK | "detective mode" Link on AMD | AMD EDS3 colour-blend driver regression | YZ-2306 PR 10946 | | BotW | shadow artifacts at object edges (AMD) | gather rounding | yuzu commit 780240e (2.10) | +| TotK | dashed dark lines along wall and floor edges, changing with the camera angle (AMD, RADV) | gather rounding | gather bias (2.10) | | BotW | black textures on GCN | HTILE (depth compression) | `AMD_DEBUG=nohyperz` wiki (1.9) | | BotW | 3D scene black | deferred texture handles | NXN | | BotW | albedo scattered squares | duplicate colour attachments | NXN | diff --git a/runtime/gfx.h b/runtime/gfx.h index 4004d46b..b875bffb 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -50,7 +50,7 @@ const GfxShader *gfx_shader_await(int stage, const uint8_t *control, const uint8 uint64_t gfx_shader_async_done(void); // translations finished by the background worker void gfx_shader_counts(uint64_t *keys, uint64_t *translated); // distinct keys in memory, translations done (TOTK_TRACE=perf) // Translator answers for this device (vk_caps.c): shaderFloat64, storage offset rounding, subgroup size per vs/fs/cs. -void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgroup[3]); +void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgroup[3], uint32_t gather_bits); const uint8_t *gfx_shader_key(const GfxShader *s); // the 32-byte cache key (stable while the translator is) const uint8_t *gfx_shader_input_id(const GfxShader *s); // the 32-byte input id (stable across translators) // By input id from memory or disk; xlate 1 also translates it from its stored inputs. NULL if absent. diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index 05fa5b82..08321d70 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -114,7 +114,7 @@ static char cache_dir[PATH_CAP]; static char xlate_exe[PATH_CAP]; static uint8_t xlate_id[32]; // translator identity (see the keys at the top) static int host_f64 = 1, host_set; // gfx_shader_set_host -static uint32_t host_sbuf, host_sg[3] = {32, 32, 32}; +static uint32_t host_sbuf, host_sg[3] = {32, 32, 32}, host_gather; static PlatOnce paths_once = PLAT_ONCE_INIT; // ---- small helpers @@ -224,16 +224,18 @@ static void init_paths(void *param) { static void paths(void) { plat_once(&paths_once, init_paths, NULL); } // Host answers (vk_caps.c, at device creation, before any translation). Answers other than the defaults (Float64, -// exact storage offsets, 32 lanes) are mixed into the translator identity, so they get their own cache keys. -void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgroup[3]) { +// exact storage offsets, 32 lanes, no gather bias) are mixed into the translator identity, so they get their own cache +// keys. +void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgroup[3], uint32_t gather_bits) { paths(); if (host_set++) return; - host_f64 = float64, host_sbuf = sbuf_align; + host_f64 = float64, host_sbuf = sbuf_align, host_gather = gather_bits; memcpy(host_sg, subgroup, sizeof host_sg); - if (float64 && !sbuf_align && subgroup[0] == 32 && subgroup[1] == 32 && subgroup[2] == 32) return; + if (float64 && !sbuf_align && subgroup[0] == 32 && subgroup[1] == 32 && subgroup[2] == 32 && !gather_bits) return; uint8_t buf[32 + 96]; int n = snprintf((char *)buf + 32, 96, "TKHO f64=%d sbuf=%u sg=%u,%u,%u", float64, sbuf_align, subgroup[0], subgroup[1], subgroup[2]); + if (gather_bits) n += snprintf((char *)buf + 32 + n, (size_t)(96 - n), " gather=%u", gather_bits); memcpy(buf, xlate_id, 32); rt_sha256(buf, 32 + (size_t)n, xlate_id); rt_log("gfx_shader: host options %s: own shader cache keys", (char *)buf + 37); @@ -973,6 +975,7 @@ static size_t build_request(char *o0, const char *hex, const char *stage, const if (!host_f64) o += sprintf(o, ",\"softfloat64\":true"); if (host_sbuf) o += sprintf(o, ",\"sbuf_align\":%u", host_sbuf); if (host_sg[si] != 32) o += sprintf(o, ",\"subgroup\":%u", host_sg[si]); + if (host_gather) o += sprintf(o, ",\"gather_bias\":%u", host_gather); o += sprintf(o, "},\"out\":\""); json_esc(&o, cache_dir); o += sprintf(o, "\"}"); diff --git a/runtime/vk_caps.c b/runtime/vk_caps.c index c07b32dc..6a94400e 100644 --- a/runtime/vk_caps.c +++ b/runtime/vk_caps.c @@ -93,7 +93,13 @@ void vk_caps_init(VkCtx *c) { static const VkShaderStageFlags stage[3] = {VK_SHADER_STAGE_VERTEX_BIT, VK_SHADER_STAGE_FRAGMENT_BIT, VK_SHADER_STAGE_COMPUTE_BIT}; uint32_t told[3]; for (int i = 0; i < 3; i++) told[i] = vk_caps.sg_pin_stages & stage[i] ? want : sg.subgroupSize; - gfx_shader_set_host(vk_caps.float64, vk_caps.sbuf_align, told); + // Gathers (TLD4) pick their four texels with Maxwell's rounding; AMD and Intel round a coordinate that sits between + // two texels the other way, which draws dark lines along object edges in shadow-map filtering (docs/maxwell.md 2.10; + // BotW and TotK on AMD). The translator biases gather coordinates by the host's sub-texel precision; NVIDIA rounds + // as Maxwell does. TOTK_GATHER_BIAS= forces it (0: off). + uint32_t gather = env_u32("TOTK_GATHER_BIAS", c->props.vendorID == 0x10de ? 0 : l->subTexelPrecisionBits); + gfx_shader_set_host(vk_caps.float64, vk_caps.sbuf_align, told, gather); + if (gather) rt_log("vk caps: gather coordinates biased for %u sub-texel bits (TOTK_GATHER_BIAS)", gather); char dv[32]; driver_version(dv, sizeof dv, c->props.vendorID, c->props.driverVersion); diff --git a/tools/xlate/Program.cs b/tools/xlate/Program.cs index f0301d91..75e0db19 100644 --- a/tools/xlate/Program.cs +++ b/tools/xlate/Program.cs @@ -104,6 +104,9 @@ static class SwshXlate // "softfloat64": true lowers FP64 operations to FP32 (Ryujinx's DoubleToFloat pass), for a host without shaderFloat64. public bool QueryHostSupportsShaderFloat64() => _job["softfloat64"] is not JsonValue v || !(v.TryGetValue(out bool b) && b); public bool QueryHostSupportsTextureShadowLod() => true; + // Sub-texel precision bits of a host whose gathers round differently from Maxwell (AMD, Intel): Ryujinx then + // biases gather coordinates so the same texels are picked (TexturePass.InsertCoordGatherBias). 0: no bias. + public int QueryHostGatherBiasPrecision() => _job["gather_bias"] is JsonNode n ? (int)n : 0; // 作业 reduced = true:放宽精度——翻译器只给强制精确的运算打 NoContraction(缺省全部打:保持 Maxwell 分开舍入的逐位结果,代价是 MSL 里大量禁优化的 spvFMul 等) // Job reduced = true: relaxed precision - the translator marks NoContraction only on operations that must be exact (the default marks all: keeps the bit-exact results of Maxwell's separate rounding, at the cost of many optimization-disabled spvFMul etc. in the MSL) public bool QueryHostReducedPrecision() => _job["reduced"] is JsonNode n && (bool)n;