A pixel shader's depth comes after four registers per enabled colour target: Ocarina of Time's agl_common depth pass no longer writes the step before its depth (chain daf7512cdfc6)

TranslatorContext.GetDepthRegister counted enabled colour components plus one, while the colours are read four registers per target with any component enabled (component c of the n-th such target in R(4n + c), EmitterContext.PrepareForReturn), then the sample mask register, then the depth. The two agree only when each enabled target has all four. Ocarina of Time's agl_common depth pass (3a4a0270fc35b848, OmapTargets 0x10f: RT0 RGBA in R0..R3, RT2 R in R4) computes R6 = 1 / -(z * a + b), then R9 = (R6 * c3[32].z + 1) * c3[32].w, and ends: R6 was read as its depth. It now returns 4 x the enabled targets + 1.

Reach: 46 of Ocarina of Time's 126 depth-writing pixel shaders (mask 0x10f x24, 0x100 x16, 0x3f x4, 0x30 x2), 16 of them in its seen shaders; none in the 1,105 depth-writing pixel shaders of the seven local Switch 1 games, which enable whole targets. All 46 translate and compile on the new chain.

Checked: test_dmsh_ampere_mem DepthRegister (R6 0.25, R9 0.75: fails on the translator before), the translator test files (test_dmsh_pairs' two NameErrors as before), test_nsp_chain, test_xlate_anycpu, test_docs. Regression: no game run; the reach is the 46 shaders above. The picture has not been compared in play.

Co-authored-by: flaviopz <[email protected]>
This commit is contained in:
seeddsandflaviopz committed 2026-11-07 12:18:38 +08:00
1 parent ffebfd5934
commit 473c90266b
12 files changed
+47 -8

No files matched your search

+1
View File
@@ -96,6 +96,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F
- [Task and mesh shaders in BNSH, and indirect mesh draws](../../../docs/reference/shaders.md#task-and-mesh-shaders-in-bnsh-and-indirect-mesh-draws)
- [Indirect task / mesh draws on the encode thread](../../../docs/reference/rendering.md#indirect-task--mesh-draws-on-the-encode-thread): the iOS app aborted when a save loaded.
- [F2IP](../../../docs/reference/shaders.md#f2ip)
- [A pixel shader's depth after a partly written target](../../../docs/reference/shaders.md#a-pixel-shaders-depth-after-a-partly-written-target): 46 depth-writing pixel shaders read their depth from the wrong register, agl_common's depth pass among them.
- [An HMMA outside a chain overwrote the chain's accumulator](../../../docs/reference/shaders.md#an-hmma-outside-a-chain-overwrote-the-chains-accumulator): dotted rows across fog and light in play.
- [LDSM reads rows other lanes stored](../../../docs/reference/shaders.md#ldsm-reads-rows-other-lanes-stored): white blotches over Kokiri Forest through agl's upscaler on an M4 Pro.
- [A shader that writes a whole frame through raw global memory](../../../docs/reference/frame-rate.md#a-shader-that-writes-a-whole-frame-through-raw-global-memory)
+1 -1
View File
@@ -1,3 +1,3 @@
// Generated file (dm/gfx/tools/dmsh_chain.py --header): the B shader library translation chain version this source tree expects (default options), one for every game;
// the host loads only <library dir>/b-<V>/. Run dmsh_chain.py --header after changing the chain's tools and commit this file with the change
#define DMSH_CHAIN "f9f01e4f9994"
#define DMSH_CHAIN "daf7512cdfc6"
+18 -2
View File
@@ -181,9 +181,9 @@ class PixelCovered(unittest.TestCase):
OTHER = (0x040e0200000e8000, 0x0000000000007925) # bits 78..80 = 2, not seen
SPH = struct.pack('<32I', *([0x000c5482] + [0] * 4 + [0xb0000000] + [0] * 12 + [0xf] + [0] * 13)) # the game's: writes colour 0
def run_job(self, op):
def run_job(self, *ops):
with tempfile.TemporaryDirectory(prefix='ampere_px_') as d:
work = Path(d); (work / 'k.bin').write_bytes(self.SPH + inst(*op) + inst(*EXIT))
work = Path(d); (work / 'k.bin').write_bytes(self.SPH + b''.join(inst(*op) for op in ops) + inst(*EXIT))
(work / 'jobs.json').write_text(json.dumps([dict(key='k', stage='ps', arch='sm86', code=str(work / 'k.bin'), tex={}, img={}, attr={}, fout={}, debug=True)]))
r = subprocess.run([os.environ['DMSH_TEST_XLATE'], str(work / 'jobs.json'), d], capture_output=True, text=True)
if not (work / 'k.spv').exists(): return None, r.stdout + r.stderr
@@ -205,6 +205,22 @@ class PixelCovered(unittest.TestCase):
self.assertIn('0x925 form 2', log)
@unittest.skipUnless(os.environ.get('DMSH_TEST_XLATE'), 'set DMSH_TEST_XLATE to the offline translator')
class DepthRegister(unittest.TestCase):
"""A pixel shader's depth output follows its colours at four registers per enabled render target, then the sample mask register. Ocarina of Time's agl_common depth pass
(ps_lib 519, OmapTargets 0x10f: RT0 RGBA in R0..R3, RT2 R in R4) writes its depth to R9; the component count put it in R6, which holds a step before"""
run_job = PixelCovered.run_job
SPH = struct.pack('<32I', *([0x000c5482] + [0] * 4 + [0xb0000000] + [0] * 12 + [0x10f, 0x2] + [0] * 12)) # colours 0x10f, depth written (+0x4c bit 1)
@staticmethod
def mov(reg, value): return (0x000fe20000000f00, value << 32 | reg << 16 | 0x7802) # MOV Rd, imm32
def test_depth_comes_after_the_last_target_block(self):
msl, log = self.run_job(self.mov(6, 0x3e800000), self.mov(9, 0x3f400000)) # R6 = 0.25, R9 = 0.75
self.assertIsNotNone(msl, log)
self.assertRegex(msl, r'gl_FragDepth = 0\.75')
class MufuF16(Translated):
"""MUFU.F16 (Splatoon Raiders' upscaler network): the source is the half bits 60..61 select (bit 62 its abs, as in every B slot), the result the low half"""
LOG2_HIGH = (0x000fe60000000e00, 0x5000000c000c7308) # MUFU.F16.LOG2 R12, |R12.H1|: the game's encoding (c1b8edc09e3313b1 +0x5970) on the register the atomic reads
+3 -3
View File
@@ -1,7 +1,7 @@
# Translation chain tool fingerprints (written by tool_lock.py --update-lock; regenerate after changing tools and commit it)
dmsh_xlate.dll ce66ce42a76c4830
Ryujinx.Graphics.Shader.dll b29592a4a612fbf5
Ryujinx.Common.dll a917971a56f4a8fb
dmsh_xlate.dll 0c4a312520b5aa7f
Ryujinx.Graphics.Shader.dll 63adda80b4b3bd6e
Ryujinx.Common.dll eea41bd3312d4f2a
xlate_source 0bd66426675438a9
spirv-cross e37945d8e441d34d
metal_macosx Apple metal version 32023.922 (metalfe-32023.922.4)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -160,8 +160,21 @@ namespace Ryujinx.Graphics.Shader.Translation
internal int GetDepthRegister()
{
// The depth register is always two registers after the last color output.
return BitOperations.PopCount((uint)Definitions.OmapTargets) + 1;
// nxnative: the colours are read four registers per render target with any component enabled (component c of the n-th
// such target in R(4n + c), as PrepareForReturn reads them), then the sample mask register, then the depth. Upstream
// counted enabled components, which agrees only when every enabled target has all four: Ocarina of Time's agl_common
// depth pass (OmapTargets 0x10f, RT0 RGBA and RT2 R) computes its depth into R9, and R6, which was read, holds a step before it
int targets = 0;
for (int rt = 0; rt < 8; rt++)
{
if ((Definitions.OmapTargets & (0xf << (rt * 4))) != 0)
{
targets++;
}
}
return targets * 4 + 1;
}
public void SetLayerOutputAttribute(int attr)
+1
View File
@@ -819,6 +819,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- the shader build prints `fatal error: bracket nesting level exceeded maximum of 256` and `<key>.metal: does not compile`, and the shader is left out. → [Bracket nesting past the front end's 256](../reference/shaders.md#bracket-nesting-past-the-front-ends-256)
- the half of Mario facing the camera's left was white (GitHub issue 72, on an iPhone). → [min, max and clamp with a NaN operand](../reference/shaders.md#min-max-and-clamp-with-a-nan-operand)
- a pipeline red line for a float output meeting an integer attachment (the red line prints the row to add); a BC encoder's pipeline fails. → [Integer fragment outputs declared by hand](../reference/shaders.md#integer-fragment-outputs-declared-by-hand)
- a pixel shader that writes depth and only some components of a colour target writes the wrong depth: in the translated MSL, `gl_FragDepth` takes a value the shader computes a step before its last one, or a colour. → [A pixel shader's depth after a partly written target](../reference/shaders.md#a-pixel-shaders-depth-after-a-partly-written-target)
- `failed to create render pipeline … output of type float4 is not compatible with a MTLPixelFormatR32Uint color attachment` (or `MTLPixelFormatRGBA16Uint`); every draw into the target skipped. → [Integer entry points for integer colour outputs](../reference/shaders.md#integer-entry-points-for-integer-colour-outputs)
- a deferred scene with part of its materials unlit or black: grass, ground and trees dark while wood and characters look right, whole areas of a map black. There may be no red line. A texture dump shows an integer G-buffer target (`fmt=73`, RGBA8Uint) holding only zeros, and the lighting pass reads it. Since this entry, `[RED] shader … writes colour target …, an integer format, as a float`. → [Integer outputs told from float bits](../reference/shaders.md#integer-outputs-told-from-float-bits)
- `failed to create render pipeline … Cannot convert attribute …`; draws missing (a pipeline that fails to build). → [Integer-typed vertex inputs fed normalized formats](../reference/shaders.md#integer-typed-vertex-inputs-fed-normalized-formats)
+8
View File
@@ -740,6 +740,14 @@ Finding, keying and translating shaders (`dm/gfx/tools/shader_split.py`, `dmsh_b
- **Found on:** [The Legend of Zelda: Breath of the Wild](../../dm/games/01007EF00011E000/README.md) (agl's BC4 encoder `8e2cc2671cae6020` and its twin for a 2D source `93aa5e22a8202409`, whose failure showed up in an iPhone run); [The Legend of Zelda: Tears of the Kingdom](../../dm/games/0100F2C0115B6000/README.md) (the BC4 encoder `0544282c162305db` and the Ultrahand mask `e3fb0bb58a691e10`, R8Uint, found by PR #11; the Mac route does not reach it); [Pokémon Legends: Z-A](../../dm/games/0100F43008C44000/README.md) (two system pixel shaders write an ID into an `R8_UI` target on the character creation screen); [DRAGON QUEST VII Reimagined](../../dm/games/0100A9D01C446000/README.md) (one global shader writing an `R8Uint` target, [Unreal Engine 4.27 shader code archives](#unreal-engine-427-shader-code-archives-lz4)).
- **Note:** the compressed render target side is in [Compressed textures as render targets through an uncompressed shadow](rendering.md#compressed-textures-as-render-targets-through-an-uncompressed-shadow).
### A pixel shader's depth after a partly written target
- **Symptom:** a pixel shader that writes depth and only some components of a colour target writes the wrong depth: in the translated MSL, `gl_FragDepth` takes a value the shader computes a step before its last one, or a colour.
- **Cause:** the colours are read four registers per render target with any component enabled (component c of the n-th such target in R(4n + c)), then comes the sample mask register, then the depth. `TranslatorContext.GetDepthRegister` counted enabled components instead, which agrees only when each enabled target has all four. With OmapTargets 0x10f (RT0 RGBA, RT2 R) the shader computes its depth into R9, `R6 = 1 / -(z * a + b)` then two constant adds, and R6 was read.
- **Fix:** `GetDepthRegister` returns 4 × the enabled targets + 1. A chain change.
- **Check:** `DMSH_TEST_XLATE=<translator> python3 dm/gfx/tests/test_dmsh_ampere_mem.py DepthRegister` (R6 0.25, R9 0.75: the old translator writes 0.25).
- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md): 46 of the 126 depth-writing pixel shaders in its shader package (mask 0x10f ×24, 0x100 ×16, 0x3f ×4, 0x30 ×2), 16 of them in its seen shaders, among them agl_common's depth pass `3a4a0270fc35b848`, whose depth now takes the step after R6 too, `(R6 * c3[32].z + 1) * c3[32].w`. Its effect on the picture has not been compared. No other local game has such a shader: the 1,105 depth-writing pixel shaders of seven Switch 1 games enable whole targets. Found by the flaviopz fork.
### Integer entry points for integer colour outputs
- **Symptom:** `failed to create render pipeline … output of type float4 is not compatible with a MTLPixelFormatR32Uint color attachment` (or `MTLPixelFormatRGBA16Uint`); every draw into the target skipped.