mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 04:49:57 -04:00
Sample terrain with the right mip levels, keep NaNs out of min, max and clamp, and give shadow gathers their own tap offsets.
The runtime's TXD gradient transpose (gfx_shader.c patch_txd_transpose) is off for Switch 2 titles: the SM86 lowering already orders the interleaved derivatives for the translator, so transposing again swapped them back. The translator's float min, max and clamp are the NaN-safe forms (patch 0031), as Maxwell FMNMX and saturate behave, so a NaN no longer spreads through lighting and bloom on drivers that pass it on. Depth-compare gathers with per-tap offsets are split into one gather per tap like the others (patch 0032), where all four taps took the first tap's offset. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -54,7 +54,7 @@ Source shorthands:
|
||||
| 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`. | Ours: non-mip filter → `maxLod = minLod + 0.25`, bias clamped (`vk_texture.c` `make_sampler`). | — |
|
||||
| 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.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. A Switch 2 title (NVN 200 and up) is left out: `AmpereLowering.cs` `Txd` puts SM86's interleaved derivatives in the translator's order already, and transposing again swapped them back (`=1` forces it on). | — |
|
||||
| 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. | — |
|
||||
|
||||
@@ -800,7 +800,10 @@ out:
|
||||
// builds dx = (first two registers), dy = (last two) = (du/dx, du/dy) and
|
||||
// (dv/dx, dv/dy): a mip level chosen from the wrong derivatives, too sharp on terrain faces turned across the screen,
|
||||
// which crawled in patches that followed the faces. NX2NAVI_TXD_TRANSPOSE=0 turns it off.
|
||||
// Applied to the SPIR-V after it is loaded, so the shader cache is untouched.
|
||||
// Applied to the SPIR-V after it is loaded, so the shader cache is untouched. Not for a Switch 2 title (NVN 200 and up):
|
||||
// its SM86 programs are lowered by tools/xlate/AmpereLowering.cs (Txd), which puts SM86's interleaved derivatives
|
||||
// (dx.x, dy.x, dx.y, dy.y) in the translator's order already, so transposing again here swapped them back and chose
|
||||
// Ocarina's mip levels from the wrong derivatives. NX2NAVI_TXD_TRANSPOSE=1 forces it on.
|
||||
enum { OP_COMPOSITE_CONSTRUCT = 80, OP_SAMPLE_EXPLICIT = 88, IO_GRAD = 4 };
|
||||
|
||||
static void patch_txd_transpose(GfxShader *s) {
|
||||
@@ -808,7 +811,7 @@ static void patch_txd_transpose(GfxShader *s) {
|
||||
static volatile int32_t n_logged;
|
||||
if (on < 0) {
|
||||
const char *e = cfg_get("NX2NAVI_TXD_TRANSPOSE");
|
||||
on = !(e && *e == '0');
|
||||
on = e && *e ? *e != '0' : rt_nvn_api_version[0] < 200;
|
||||
}
|
||||
uint32_t *w = s->spirv;
|
||||
uint64_t n = s->words;
|
||||
|
||||
@@ -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) != 29:
|
||||
sys.exit(f"expected 29 patches, found {len(patches)}")
|
||||
if len(patches) != 31:
|
||||
sys.exit(f"expected 31 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"], []):
|
||||
|
||||
26
tools/xlate/patches/0031-nan-safe-min-max-clamp.patch
Normal file
26
tools/xlate/patches/0031-nan-safe-min-max-clamp.patch
Normal file
@@ -0,0 +1,26 @@
|
||||
--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvDelegates.cs
|
||||
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SpirvDelegates.cs
|
||||
@@ -171,9 +171,12 @@
|
||||
IAdd = context.IAdd;
|
||||
FDiv = context.FDiv;
|
||||
SDiv = context.SDiv;
|
||||
- GlslFMax = context.GlslFMax;
|
||||
+ // NX2NAVI: NaN-safe minimum and maximum. Maxwell FMNMX returns the other operand when one is NaN (and a
|
||||
+ // saturate takes NaN to 0); GLSL.std.450 FMin, FMax and FClamp may return either, and AMD's Windows compiler
|
||||
+ // passes the NaN on, so one NaN texel spreads through lighting and bloom. NMin, NMax and NClamp match FMNMX.
|
||||
+ GlslFMax = context.GlslNMax;
|
||||
GlslSMax = context.GlslSMax;
|
||||
- GlslFMin = context.GlslFMin;
|
||||
+ GlslFMin = context.GlslNMin;
|
||||
GlslSMin = context.GlslSMin;
|
||||
FMod = context.FMod;
|
||||
FMul = context.FMul;
|
||||
@@ -210,7 +213,7 @@
|
||||
AtomicXor = context.AtomicXor;
|
||||
|
||||
// Ternary
|
||||
- GlslFClamp = context.GlslFClamp;
|
||||
+ GlslFClamp = context.GlslNClamp;
|
||||
GlslSClamp = context.GlslSClamp;
|
||||
GlslFma = context.GlslFma;
|
||||
|
||||
14
tools/xlate/patches/0032-shadow-gather-per-tap-offsets.patch
Normal file
14
tools/xlate/patches/0032-shadow-gather-per-tap-offsets.patch
Normal file
@@ -0,0 +1,14 @@
|
||||
--- a/src/Ryujinx.Graphics.Shader/Translation/Transforms/TexturePass.cs
|
||||
+++ b/src/Ryujinx.Graphics.Shader/Translation/Transforms/TexturePass.cs
|
||||
@@ -431,7 +431,10 @@
|
||||
|
||||
LinkedListNode<INode> oldNode = node;
|
||||
|
||||
- if (isGather && !isShadow && hasOffsets)
|
||||
+ // NX2NAVI: depth-compare gathers too. With per-tap offsets (TLD4.PTP) the else branch below adds only tap 0's
|
||||
+ // offset, so a shadow map's four taps all came from one place (blocky shadow edges); one gather per tap with
|
||||
+ // that tap's offset keeps its compared texel, the reference value staying in the copied sources.
|
||||
+ if (isGather && hasOffsets)
|
||||
{
|
||||
Operand[] newSources = new Operand[sources.Length];
|
||||
|
||||
Reference in New Issue
Block a user