diff --git a/docs/maxwell.md b/docs/maxwell.md index e8eba067..bd2bb90b 100644 --- a/docs/maxwell.md +++ b/docs/maxwell.md @@ -88,7 +88,7 @@ Source shorthands: | 3.9 | **Integer vertex inputs read as raw bits** (bone indices `R8UI`). | Float-declared inputs convert integers. | NXN Mario: stretched characters; re-translated with `uint` inputs. Ours: attribute types passed to `QueryGraphicsState` (`Program.cs`). | NXN | | 3.10 | **BFE / BFI fields past bit 31** (as PTX `bfe` / `bfi`): a field that runs past bit 31 is cut there; one that starts past it extracts 0 (signed: bit 31 repeated) and inserts nothing. TotK 1.4.3's Depths terrain vertex shaders (42 variants) take the position from a register, `((2y + x) * 8) & 0xff` with 5-bit x and y, so up to 248. | `OpBitField*Extract` / `OpBitFieldInsert` undefined when offset or offset + count exceeds 32; AMD's bitfield instructions take offset and width modulo 32. | Ryujinx passes the fields through. UltraTears saw the Depths walls and ground missing on the Steam Deck and a Linux RX 570 (RADV). Ours: clamped where the field is not a constant inside the word (translator patch 0012). RX 9070 on Windows drew the Depths the same before and after. | PTX ISA `bfe`, `bfi` | | 3.11 | **SHF by 0** gives the unshifted word (C for `SHF.L`, A for `SHF.R`). | Ryujinx's funnel shifts the other word by 32: undefined in SPIR-V; AMD shifts take the low 5 bits and give A \| C. | yuzu fixed Depths terrain gaps in its funnel shift (PR 10459). Ours: a select on a zero amount (translator patch 0013). In 1.4.3 every register SHF pairs its word with 0, so the result was right already. | — | -| 3.12 | **Registers read before any write** on a path (paths the structurizer adds included) have no defined value in the game's code; UltraTears reports 0 from the NVIDIA driver. | An `OpVariable` without an initializer loads an undefined value; Mesa folds undefined values freely. | Ours: none for now. Translator patch 0014 started every function-local variable at 0 (`OpConstantNull`); on NVIDIA (RTX 4080 SUPER, 1.4.3) that made the whole picture shake by about 0.9 output pixels a frame with DLSS or DLAA (#612; no shake with `TOTK_DLAA_JITTER=0` or without the patch), so it was taken out. A zero start for Mesa only, or for the shaders that need it, is still open. | — | +| 3.12 | **Registers read before any write** on a path (paths the structurizer adds included) have no defined value in the game's code; UltraTears reports 0 from the NVIDIA driver. | An `OpVariable` without an initializer loads an undefined value; Mesa folds undefined values freely. | Ours: on Mesa drivers (RADV, ANV, NVK; `vk_caps_driver`) every function-local variable starts at 0 (`OpConstantNull`, job flag `zero_locals`, translator patch 0016; `TOTK_ZERO_LOCALS` overrides). Elsewhere the load stays undefined: a zero start on every driver (the old patch 0014) made the whole picture shake by about 0.9 output pixels a frame with DLSS or DLAA on an RTX 4080 SUPER (#612). It also moved the SPIR-V hash of the game's FXAA shader out of `FXAA_FS` (`vk_dlaa.c` `vk_dlaa_skip_draw`), the likely cause of the shake; that path is not traced. | — | ## 4. Memory model diff --git a/runtime/gfx.h b/runtime/gfx.h index fae3de28..0bed28d1 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -53,7 +53,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], uint32_t gather_bits); +void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgroup[3], uint32_t gather_bits, int zero_locals); 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 881fa86d..e5c0359c 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -119,7 +119,7 @@ static struct { // translations in fl 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 int host_f64 = 1, host_zero_locals, host_set; // gfx_shader_set_host static uint32_t host_sbuf, host_sg[3] = {32, 32, 32}, host_gather; static PlatOnce paths_once = PLAT_ONCE_INIT; @@ -313,18 +313,19 @@ 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, 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) { +// exact storage offsets, 32 lanes, no gather bias, undefined locals) 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, int zero_locals) { paths(); if (host_set++) return; - host_f64 = float64, host_sbuf = sbuf_align, host_gather = gather_bits; + host_f64 = float64, host_sbuf = sbuf_align, host_gather = gather_bits, host_zero_locals = zero_locals; memcpy(host_sg, subgroup, sizeof host_sg); - if (float64 && !sbuf_align && subgroup[0] == 32 && subgroup[1] == 32 && subgroup[2] == 32 && !gather_bits) return; + if (float64 && !sbuf_align && subgroup[0] == 32 && subgroup[1] == 32 && subgroup[2] == 32 && !gather_bits && !zero_locals) 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); + if (zero_locals) n += snprintf((char *)buf + 32 + n, (size_t)(96 - n), " zero_locals"); 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); @@ -1174,6 +1175,7 @@ static size_t build_request(char *o0, const char *hex, const char *stage, const 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); + if (host_zero_locals) o += sprintf(o, ",\"zero_locals\":true"); o += sprintf(o, "},\"out\":\""); json_esc(&o, cache_dir); o += sprintf(o, "\"}"); diff --git a/runtime/vk.h b/runtime/vk.h index 3bc6f7ef..a544a0d5 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -57,9 +57,10 @@ typedef struct { int gpl; // VK_EXT_graphics_pipeline_library enabled with fast linking (TOTK_GPL=0: off) int amd; // an AMD GPU (vendor 0x1002), any driver int amd_sc; // AMD's Windows driver branch for RDNA 1 and 2 ("AMD proprietary shader compiler") + int mesa; // a Mesa driver the game runs on: RADV, ANV or NVK } VkCaps; extern VkCaps vk_caps; -void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc +void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, mesa void vk_caps_features(VkPhysicalDeviceFeatures *f); // before vkCreateDevice (TOTK_VK_NO_FLOAT64 clears shaderFloat64) // Before vkCreateDevice, with the driver's graphics pipeline library feature and properties: sets vk_caps.gpl, // 1 = enable the extensions (VK_KHR_pipeline_library, VK_EXT_graphics_pipeline_library) and the feature. diff --git a/runtime/vk_caps.c b/runtime/vk_caps.c index 0c3a86d8..17fe85d3 100644 --- a/runtime/vk_caps.c +++ b/runtime/vk_caps.c @@ -43,6 +43,8 @@ void vk_caps_driver(VkPhysicalDevice p) { // AMD split RDNA 1 and 2 off into a driver branch of their own; its driverInfo reads // "26.10.1 (AMD proprietary shader compiler)" where the RDNA 3 and 4 driver names LLPC. vk_caps.amd_sc = drv.driverID == VK_DRIVER_ID_AMD_PROPRIETARY && strstr(drv.driverInfo, "shader compiler"); + vk_caps.mesa = drv.driverID == VK_DRIVER_ID_MESA_RADV || drv.driverID == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA || + drv.driverID == VK_DRIVER_ID_MESA_NVK; } // TOTK_GPL=0 turns graphics pipeline libraries off; they are on wherever the driver has fast linking. AMD's RDNA 1/2 @@ -114,7 +116,10 @@ void vk_caps_init(VkCtx *c) { // 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); + // A register read before any write is an undefined load in SPIR-V, and Mesa folds undefined values freely, so on + // Mesa function-local variables start at 0 (docs/maxwell.md 3.12). TOTK_ZERO_LOCALS=1 forces it, 0 turns it off. + uint32_t zero_locals = env_u32("TOTK_ZERO_LOCALS", (uint32_t)vk_caps.mesa); + gfx_shader_set_host(vk_caps.float64, vk_caps.sbuf_align, told, gather, zero_locals != 0); if (gather) rt_log("vk caps: gather coordinates biased for %u sub-texel bits (TOTK_GATHER_BIAS)", gather); char dv[32]; diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index db375da9..7ffff8c8 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -1218,10 +1218,12 @@ void vk_dlaa_frame(void) { // scaled picture into the output-sized target, in a pass without the depth attachment the in-place FXAA has: there it // is always drawn, whatever fxaa says (skipping it would leave the 3D picture out), unless DLSS already wrote that // target this frame. -// The stock translator's hash, the one with tools/xlate patch 0010 (PCSS, PR #89) applied, and the one with patches -// 0010 and 0011 (the shadow filter spread) applied. A scaling draw this list misses still scales DLSS's picture, not +// The stock translator's hash, the one with tools/xlate patch 0010 (PCSS, PR #89) applied, the one with patches 0010 +// and 0011 (the shadow filter spread) applied, and that one with the zero_locals job flag (Mesa, vk_caps.c). +// A scaling draw this list misses still scales DLSS's picture, not // the jittered scene: after DLSS ran, passes larger than the light target sample DLSS's output (vk_dlaa_swap_img). -static const uint64_t FXAA_FS[] = {0xf450003ebeb3dc52ull, 0x0a12f4bc2a9a4fbeull, 0x5c43ae18948d52cbull}; +static const uint64_t FXAA_FS[] = {0xf450003ebeb3dc52ull, 0x0a12f4bc2a9a4fbeull, 0x5c43ae18948d52cbull, + 0xaad42238801d31d8ull}; static uint64_t spirv_hash(const GfxShader *s) { static struct { const GfxShader *s; uint64_t h; } tab[8192]; diff --git a/tools/build_xlate.py b/tools/build_xlate.py index c13ed192..9e3381e2 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-0013 and 0015 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build +byte-identical; 0001-0004 are MNG's, 0005-0013, 0015 and 0016 are ours), then runs `dotnet publish`. Needs the .NET 10 SDK, the MSVC build tools and vswhere.exe (docs/building.md). Run with .venv. On Linux it builds build/xlate/totk_xlate for linux-x64, which NativeAOT links with the system's clang (no MSVC); NativeAOT builds for the OS it runs on only. @@ -99,8 +99,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) != 14: - sys.exit(f"expected 14 patches, found {len(patches)}") + if len(patches) != 15: + sys.exit(f"expected 15 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/Program.cs b/tools/xlate/Program.cs index 75ee5cb2..df74cb45 100644 --- a/tools/xlate/Program.cs +++ b/tools/xlate/Program.cs @@ -8,7 +8,7 @@ // totk_xlate batch mode: a JSON array of jobs, prints a summary, exit 1 if any failed. // job: { "key", "stage": "vs|ps|cs", "code": path (graphics = 0x50-byte SPH + instructions, compute = instructions), "c1": hex, // "tex": {slot: type}, "img": {slot: {type, fmt}}, "attr": {loc: float|sint|uint}, "fout": {idx: float|sint|uint}, -// "local": [x, y, z], "shared": bytes, "debug": bool, "reduced": bool, "softfloat64": bool, +// "local": [x, y, z], "shared": bytes, "debug": bool, "reduced": bool, "softfloat64": bool, "zero_locals": bool, // "sbuf_align": int, "subgroup": int } // Output: /.spv and /.res.json (bindings, queried slots, notes). using Ryujinx.Common.Memory; @@ -121,6 +121,8 @@ static class SwshXlate // the user banks c3-c16 (patch 0015, runtime/vk_mvprev.c); res.json's "mvprev" names the output location (-1: // none) and "mvprev_banks" the banks the position depends on (bit n: cn). public bool QueryMotionPrevious() => _job["mvprev"] is JsonValue v && v.TryGetValue(out bool b) && b; + // "zero_locals": true: function-local variables start at 0 (patch 0016, runtime/vk_caps.c decides per driver). + public bool QueryZeroLocals() => _job["zero_locals"] is JsonValue v && v.TryGetValue(out bool b) && b; public int MotionPreviousLocation = -1, MotionPreviousBanks; public void ReportMotionPrevious(int location, int banks) => (MotionPreviousLocation, MotionPreviousBanks) = (location, banks); } diff --git a/tools/xlate/patches/0016-zero-locals-job-flag.patch b/tools/xlate/patches/0016-zero-locals-job-flag.patch new file mode 100644 index 00000000..eee136ed --- /dev/null +++ b/tools/xlate/patches/0016-zero-locals-job-flag.patch @@ -0,0 +1,71 @@ +--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs ++++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs +@@ -33,8 +33,14 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv + { + foreach (AstOperand local in function.Locals) + { +- SpvInstruction localPointerType = context.TypePointer(StorageClass.Function, context.GetType(local.VarType)); +- SpvInstruction spvLocal = context.Variable(localPointerType, StorageClass.Function); ++ SpvInstruction localType = context.GetType(local.VarType); ++ SpvInstruction localPointerType = context.TypePointer(StorageClass.Function, localType); ++ ++ // TOTK: on hosts that ask for it (Mesa's compilers fold undefined values freely), a register read on a ++ // path that did not write it reads 0 instead of an undefined value. ++ SpvInstruction spvLocal = context.HostCapabilities.ZeroLocals ++ ? context.Variable(localPointerType, StorageClass.Function, context.ConstantNull(localType)) ++ : context.Variable(localPointerType, StorageClass.Function); + + context.AddLocalVariable(spvLocal); + context.DeclareLocal(local, spvLocal); +--- a/src/Ryujinx.Graphics.Shader/IGpuAccessor.cs ++++ b/src/Ryujinx.Graphics.Shader/IGpuAccessor.cs +@@ -188,6 +188,9 @@ namespace Ryujinx.Graphics.Shader + // the user output location it went to (-1: none) and the banks the position depends on (bit n: cn). + bool QueryMotionPrevious() => false; + ++ // TOTK: true when function-local variables start at 0 (CodeGen/Spirv/Declarations.cs DeclareLocals). ++ bool QueryZeroLocals() => false; ++ + void ReportMotionPrevious(int location, int banks) + { + } +--- a/src/Ryujinx.Graphics.Shader/Translation/HostCapabilities.cs ++++ b/src/Ryujinx.Graphics.Shader/Translation/HostCapabilities.cs +@@ -12,6 +12,7 @@ namespace Ryujinx.Graphics.Shader.Translation + public readonly bool SupportsShaderNonUniformIndexing; + public readonly bool SupportsTextureShadowLod; + public readonly bool SupportsViewportMask; ++ public readonly bool ZeroLocals; + + public HostCapabilities( + bool reducedPrecision, +@@ -23,7 +24,8 @@ namespace Ryujinx.Graphics.Shader.Translation + bool supportsShaderFloat64, + bool supportsShaderNonUniformIndexing, + bool supportsTextureShadowLod, +- bool supportsViewportMask) ++ bool supportsViewportMask, ++ bool zeroLocals) + { + ReducedPrecision = reducedPrecision; + SupportsFragmentShaderInterlock = supportsFragmentShaderInterlock; +@@ -35,6 +37,7 @@ namespace Ryujinx.Graphics.Shader.Translation + SupportsShaderNonUniformIndexing = supportsShaderNonUniformIndexing; + SupportsTextureShadowLod = supportsTextureShadowLod; + SupportsViewportMask = supportsViewportMask; ++ ZeroLocals = zeroLocals; + } + } + } +--- a/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs ++++ b/src/Ryujinx.Graphics.Shader/Translation/TranslatorContext.cs +@@ -372,7 +372,8 @@ namespace Ryujinx.Graphics.Shader.Translation + GpuAccessor.QueryHostSupportsShaderFloat64(), + GpuAccessor.QueryHostSupportsShaderNonUniformIndexing(), + GpuAccessor.QueryHostSupportsTextureShadowLod(), +- GpuAccessor.QueryHostSupportsViewportMask()); ++ GpuAccessor.QueryHostSupportsViewportMask(), ++ GpuAccessor.QueryZeroLocals()); + + CodeGenParameters parameters = new CodeGenParameters(attributeUsage, definitions, resourceManager.Properties, hostCapabilities, GpuAccessor, Options.TargetApi); +