Mesh shaders' position is invariant, as a vertex shader's: Ocarina of Time's foliage no longer flashes white for single frames (chain 3ee52af3ca18, PR #1673)

The game draws its foliage twice with two different mesh shaders: a depth pre-pass (depth LESS, no colour) and a colour
pass with depth EQUAL and no depth write. A vertex shader's position is [[position, invariant]], compiled with
-fpreserve-invariance, but the mesh stage wrapper (dmsh_meshstage.to_stage) declared a plain [[position]], so Metal could
compile the two mesh shaders' position math to slightly different depths; pixels where the colour pass missed EQUAL
showed white for one frame. The wrapper now declares [[position, invariant]].

Checked: test_dmsh_meshstage.py (the new test fails without the mark and needs no Metal compiler; the compile test keeps
its skip), test_docs.py, test_nsp_chain.py. Ocarina of Time 1.1.1 on the Mac: no flashes in five 121-frame windows on an
M4 Max (14 on main); an overlay with the mark showed none in 240 frames at a flashing flower on an M4 Pro.

Co-authored-by: cocoman93 <[email protected]>
This commit is contained in:
seeddsandcocoman93 committed 2026-11-08 01:06:49 +08:00
1 parent e5f03403bf
commit e5f1adee69
6 files changed
+12 -5

No files matched your search

+1
View File
@@ -80,6 +80,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F
## Found on this game
- [Position invariance across shaders](../../../docs/reference/shaders.md#position-invariance-across-shaders): white shapes on plants and bushes for single frames in Kokiri Forest, from the foliage's mesh pre-pass and mesh colour pass rounding the position apart.
- [An old NUP cache stamp makes the build unpack again](../../../docs/reference/packing.md#an-old-nup-cache-stamp-makes-the-build-unpack-again): after a packer update the v131072 image is checked against the NUP in 21.6 s and kept, instead of unpacked again.
- [Uncompressed storage beside storage the GPU writes](../../../docs/reference/nvn-objects.md#uncompressed-storage-beside-storage-the-gpu-writes): resolved, the ground opening in holes after walking out of a cave and back (the terrain's height and blend maps came out as zeros once the first normal map had been drawn into their pool); a reload had hidden it.
- [A damaged file in a NUP or a dump has no hash to fail](../../../docs/reference/packing.md#a-damaged-file-in-a-nup-or-a-dump-has-no-hash-to-fail): a contributor's NUP held three `.zs` textures beginning with zeros, and the background loader reloaded the fairy fountain model for ever (412% CPU); this repository's copy has none.
+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 "1d619d0f520b"
#define DMSH_CHAIN "3ee52af3ca18"
+3
View File
@@ -203,6 +203,9 @@ class Stage(unittest.TestCase):
src = (KERNEL % (ms.memory(i)[1] // 4, ms.memory(i)[0][2] // 4 + 1)).replace('main0(', 'main0(uint (&a)[4], ')
with self.assertRaises(ValueError): ms.to_stage(src, i)
def test_position_is_invariant(self): # two mesh shaders of one geometry must agree: a pre-pass and a colour pass testing depth for equality
self.assertIn('float4 position [[position, invariant]];', self.mesh()[0])
@unittest.skipUnless(shutil.which('xcrun') and subprocess.run(['xcrun', '-sdk', 'macosx', '-f', 'metal'], capture_output=True).returncode == 0, 'no Metal compiler')
def test_compiles(self):
cases = [self.task()[0], self.mesh()[0], self.mesh(task=True)[0], self.mesh(primitive=True)[0], self.mesh(vertices=32, primitives=32, pos=0xf8, verts=1, reads={5: 0x7})[0], self.mesh(primitive=True, reads={0: 1, 1: 0xf, 5: 0xf})[0], self.local_word()]
+3 -1
View File
@@ -178,7 +178,9 @@ def to_stage(msl, i, reads=None):
vw = {n: _width(c, read(n)) for n, c in vm.items() if isinstance(n, int)}
pw = {n: _width(c, read(n)) for n, c in pm.items()}
mw = {n: _width([None] * 4, read(n)) for n in miss}
head += ('struct dm_vertex_t {\n float4 position [[position]];\n' +
# The position is invariant, as a vertex shader's: a game may draw the same geometry with two mesh shaders and test the second for depth equality
# (Ocarina of Time draws its foliage with a mesh depth pre-pass and a mesh colour pass with depth EQUAL)
head += ('struct dm_vertex_t {\n float4 position [[position, invariant]];\n' +
(' float point_size [[point_size]];\n' if 'psiz' in vm else '') +
''.join(f' {_type(w)} a{n} [[user(locn{n})]];\n' for n, w in vw.items()) +
''.join(f' {_type(w)} dm_pad{n} [[user(locn{n})]];\n' for n, w in mw.items()) + '};\n' +
+1 -1
View File
@@ -842,7 +842,7 @@ One line per symptom in the [subsystem references](../reference/): what you see
- outdoors dim and nothing casts a sun shadow (every shadow-map lookup fails). → [Unwritten components of a written output start as (0, 0, 0, 1)](../reference/shaders.md#unwritten-components-of-a-written-output-start-as-0-0-0-1)
- rows of dark dots along contour lines of alpha-tested surfaces drawn after a depth pre-pass (Tears of the Kingdom: the stone lattice in front of the Great Sky Island start, as reported by MacNativeGames ae8f4f4; not reproduced here). → [Early fragment tests from the control section](../reference/shaders.md#early-fragment-tests-from-the-control-section)
- a simulation or filter that reads neighbouring texels misbehaves though every constant is right. In the translated MSL a variable is declared with no value and read at once (`int _139; int _141 = _139;`), used as a coordinate. On Ocarina of Time: grass whose blades shift at 10 Hz, with a 6-frame cycle in the depth buffer and a wind velocity texture of strong stripes. → [Dead-code removal dropped a coordinate of a multi-output instruction](../reference/shaders.md#dead-code-removal-dropped-a-coordinate-of-a-multi-output-instruction)
- the ground kept stale 32×32 blocks where the G-buffer draw failed the depth test: blocks of a stale image where a depth pre-pass and a later pass with other vertex shaders meet (Odyssey: 32×32 blocks on every fourth frame). → [Position invariance across shaders](../reference/shaders.md#position-invariance-across-shaders)
- the ground kept stale 32×32 blocks where the G-buffer draw failed the depth test: blocks of a stale image where a depth pre-pass and a later pass with other vertex shaders meet (Odyssey: 32×32 blocks on every fourth frame). With mesh shaders: white shapes on plants for single frames, at other places each frame while the leaves move (Ocarina of Time). → [Position invariance across shaders](../reference/shaders.md#position-invariance-across-shaders)
- making a pipeline archive prints `Max Unstruct ID 758 in function agc.main in shader 13941 exceeds supported limit`, and one pipeline takes many minutes, in `metal-tt` offline or in the host's prebuild on the device (`Preparing pipelines` holding one count for minutes). The line is a note, not an error: `metal-tt` goes on, exits 0 and writes that pipeline into the archive. → [Front end at -O1: Apple's control-flow lowering](../reference/shaders.md#front-end-at--o1-apples-control-flow-lowering)
- 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)
+3 -2
View File
@@ -735,10 +735,11 @@ Finding, keying and translating shaders (`dm/gfx/tools/shader_split.py`, `dmsh_b
### Position invariance across shaders
- **Symptom:** the ground kept stale 32×32 blocks where the G-buffer draw failed the depth test: blocks of a stale image where a depth pre-pass and a later pass with other vertex shaders meet (Odyssey: 32×32 blocks on every fourth frame).
- **Symptom:** the ground kept stale 32×32 blocks where the G-buffer draw failed the depth test: blocks of a stale image where a depth pre-pass and a later pass with other vertex shaders meet (Odyssey: 32×32 blocks on every fourth frame). With mesh shaders: white shapes on plants for single frames, at other places each frame while the leaves move (Ocarina of Time).
- **Cause:** a game may draw the same geometry with two programs and rely on identical depth, for example a depth pre-pass and then a less-or-equal G-buffer pass. Maxwell evaluates identical position math identically; Metal only does for outputs marked `[[invariant]]` (the translation marks `gl_Position`) when compiled with `-fpreserve-invariance`. Without it, Metal compiled two vertex shaders' identical position math to slightly different depths, so fragments of the second pass failed a less-or-equal depth test against the pre-pass and those pixels kept the previous content.
- **Fix:** `dmsh_build.py` (Mac) and `ios_metal.py` (iOS) pass `-fpreserve-invariance` to every compile.
- **Fix:** `dmsh_build.py` (Mac) and `ios_metal.py` (iOS) pass `-fpreserve-invariance` to every compile. A mesh shader's position is marked too: the mesh stage wrapper (`dmsh_meshstage.to_stage`) declares `[[position, invariant]]`, as the translation does for `gl_Position`.
- **Found on:** [Super Mario Odyssey](../../dm/games/0100000000010000/README.md) (Mac): the Cap Kingdom ground. Presents 4k were brighter and blocky, 4k+1 and 4k+2 clean, 4k+3 partly blocked, repeating to the decimal across runs. The blocks were already in the HDR target when the G-buffer pass ended, and only with the ground's material (`b6384a85afd3ef94`) drawn. In some frames the game first draws a depth pre-pass (`alRenderDepthOnly`) for objects its occlusion queries report visible (the queries rotate through three slots, hence the cycle), then draws the G-buffer with other vertex shaders and depth test less-or-equal, without clearing the colour targets.
[The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md) (Mac): the foliage in Kokiri Forest is drawn twice by two different mesh shaders, a depth pre-pass (depth LESS, no colour) and a colour pass with depth EQUAL and no depth write. Pixels where the colour pass missed EQUAL showed white for one frame; how the white reaches the picture was not traced (the scene target's clear value, alpha 0, is the likely source). Without the mark the wrapper's position was a plain `[[position]]`. With it: no flashes in five 121-frame windows on an M4 Max (14 on main), none in 240 frames at a flashing flower on an M4 Pro.
- **Note:** ruled out on the way: the TBDR optimizations, same-pass feedback, the present path. Evaluating the game's conditional rendering on the CPU from earlier frames' results made it worse (the condition belongs to the same frame on the GPU), so the host still ignores the condition there and draws.
### iOS math mode (relaxed)