Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Require the Metal compiler the shaders were tested with
Users building on macOS 26 got corrupted pictures (#242): the shader chain is tested with the
Metal compiler in dmsh_chain.lock (32023.921, Xcode 27.1), but own_compilers.py, through which
the NSP script builds every NVN game's shaders, let any Metal compiler through with one note
line, and the README named no Xcode version.

- own_compilers.py: another Metal compiler (macOS or iOS) stops the build with a message naming
  the Xcode to install and the Metal toolchain download, unless DM_ALLOW_OTHER_METAL=1, which
  builds with it and says so in the note line. spirv-cross may still differ (a note, as before).
  A missing compiler or another translator fails as before. own_compilers.py is not one of the
  chain's fingerprinted files: the chain version does not change.
- NSP script: check_metal runs own_compilers.py --check-metal before an NVN game's captures and
  shader split, so a Mac with another Xcode stops in seconds instead of after the split;
  ALLOW_OTHER_METAL / --allow-other-metal skips it and passes DM_ALLOW_OTHER_METAL to the shader
  step (modelled on ALLOW_SHADER_REGRESSIONS). Vulkan and OpenGL games do not use the lock and
  are not checked.
- README: the build Mac needs macOS 26.6 or later with Xcode 27.1, and the Metal toolchain
  (xcodebuild -downloadComponent MetalToolchain). docs/development.md, dm/gfx/README.md and
  docs/env_switches.tsv describe the check and the override.
- dm/tools/tests/test_own_compilers.py: the stop (macOS and iOS), the override, spirv-cross as
  a note, missing compiler and translator mismatches, --check-metal, and the NSP script's check,
  flag and environment.
  • Loading branch information
v0lumehi committed Oct 26, 2026
commit e4a6ea139ef60a5368be5bfdace208f0b4eb5cb2
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,10 @@ Under each game: the IPA's size, and "60 fps with 1.2.0" where the game runs at

Prefer a graphical interface to the command line? **[NX2Apple](https://github.com/LNPANDY/NX2Apple)** is a community fork with a macOS app that runs this whole pipeline for you and packs the game files into a sharded RomFS archive for a slimmer IPA. It is a separate project that follows this one; the steps below are nxnative's own command-line way.

**1. Get nxnative and its tools** (once, on a Mac with [Xcode](https://apps.apple.com/app/xcode/id497799835) and [Homebrew](https://brew.sh)):
**1. Get nxnative and its tools** (once, on a Mac with macOS 26.6 or later, [Xcode](https://apps.apple.com/app/xcode/id497799835) 27.1 and [Homebrew](https://brew.sh)). Xcode 27.1's Metal compiler is the one the games' shaders are tested with: with another one, shaders can come out wrong and the game shows a corrupted picture ([#242](https://github.com/seedds/nxnative/issues/242)), so the script stops and tells you.

```
xcodebuild -downloadComponent MetalToolchain
git clone https://github.com/seedds/nxnative.git && cd nxnative
brew install xcodegen zstd spirv-cross glslang
brew install --cask dotnet-sdk
Expand Down
4 changes: 2 additions & 2 deletions dm/gfx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ The whole chain is driven by `tools/dmsh_build.py`; `scripts/build_ipa_from_nsp.
- `dmsh_chain.py --update-lock`: rewrites the lock file according to the local tools.
- `dmsh_chain.py --header`: rewrites `nvn/dmsh_chain.h` (commit it with the chain change; `dm/tools/tests/test_nsp_chain.py` fails on a stale one).
- `dmsh_chain.py --inputs`: prints the digest of the current profile's inputs (`DM_GAME`), as a full build stamps it.
- It is normal for the local Xcode or spirv-cross version to differ from the one registered in the repository; `dmsh_chain.py --update-lock` re-registers the lock file with the local tools (commit it afterwards).
- `tools/own_compilers.py <dmsh_build.py arguments>` runs `dmsh_build.py` with the local Metal compiler and spirv-cross even when they differ from the lock, printing one `note:` line that names them; the translator must still match. Neither the lock nor V changes, so the committed `dmsh_chain.h` stays valid. The NSP script builds the iOS shaders this way, so a player with another Xcode or a newer spirv-cross needs no `--update-lock`.
- Moving the chain to another Xcode or spirv-cross: `dmsh_chain.py --update-lock` re-registers the lock file with the local tools (commit it afterwards, with the Xcode version that `README.md` and `tools/own_compilers.py` name).
- `tools/own_compilers.py <dmsh_build.py arguments>` runs `dmsh_build.py` with the local spirv-cross even when it differs from the lock, printing one `note:` line that names it; the translator and the Metal compiler must still match. Another Metal compiler stops it with a message naming the Xcode to install, as shaders built with it rendered wrong pictures (GitHub issue #242); `DM_ALLOW_OTHER_METAL=1` builds with it anyway. Neither the lock nor V changes, so the committed `dmsh_chain.h` stays valid. The NSP script builds the iOS shaders this way, so a player with a newer spirv-cross needs no `--update-lock`.
- **`dm/games/<id>/c1_full.tsv`**: the index list for the shaders' built-in constant section (c1), generated by `tools/c1_guard.py` scanning the translated MSL; it lists the shader keys whose "run-time index could not be proven bounded". An index counts as proven only when interval arithmetic over the whole expression (every variable followed to its single definition; literals, `& K`, `% K`, shifts, `min`, `max`, `clamp` and casts bound it) keeps it non-negative and inside the span a shared bank gives that shader: its c1 data rounded up to 256 bytes plus the 256-byte zero gap. Every stage counts (`vp_`, `fp_`, `cp_` …). Its first line records the `b-<V>` and the inputs stamp of the library it was made from. For the shaders on the list the replay layer allocates a separate whole 64 KiB buffer for c1; when the list is missing or does not name the loaded library's version and stamp, it reports red and conservatively treats all shaders this way. The list is not committed (git ignores it): each checkout makes it from its own build's Mac outputs (a full `dmsh_build.py` run does).
- **`dm/games/<id>/cb_extent.tsv`**: how far into each uniform slot (cbuf binding) every shader can read, generated by `tools/cb_extent.py` from the translated MSL with `c1_guard.py`'s interval arithmetic: per shader key, `reg:bytes` for each slot whose reads are all proven to stay inside its first bytes, and `s<reg>` for each storage slot the shader never writes (SPIRV-Cross types every storage buffer Ryujinx emits as writable, so the host counts the others as written). When the game updates part of a bound uniform block inside a render pass, the replay layer copies only that much of the binding to give the draws after the update their data without ending the pass (`nvn_mtl.m` `ubp_get`; Breath of the Wild binds a 64 KiB block and its vertex shaders read at most 544 bytes of it). Its first line records the `b-<V>` and inputs stamp, like `c1_full.tsv`'s; when it is missing or stale, bindings are copied whole (up to 64 KiB). Like `c1_full.tsv`, it is not committed, and a full `dmsh_build.py` run makes it.
- **`dm/games/<id>/shader_pairs.tsv` and `pso_pairs.tsv`**: the (vertex key, pixel key) of every BNSH variation (Unity: of every program) and agl BAHS program variation, written by `tools/shader_split.py`. Metal only builds a pipeline when every pixel shader input is a vertex shader output, while Maxwell gives an unwritten attribute a default; `dmsh_build.py` therefore adds the outputs a vertex shader's partners read but it doesn't write, set to (0, 0, 0, 1) (`vs_pad`). The profile tables are part of its inputs digest and the per-shader cache keys. A profile without `pso_pairs.tsv` takes the generated table beside the shader package.
Expand Down
68 changes: 55 additions & 13 deletions dm/gfx/tools/own_compilers.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,66 @@
#!/usr/bin/env python3
# dmsh_build.py with this Mac's own compilers: the NSP script builds the iOS shader libraries through this file. dmsh_build.py stops when any tool differs from dmsh_chain.lock;
# here spirv-cross and the Metal compiler (macOS and iOS) may differ, with one note line naming them, while the translator (dmsh_xlate_bin) must still be the locked build.
# dmsh_build.py as a player's Mac runs it: the NSP script builds the shader libraries through this file. dmsh_build.py stops when any tool differs from dmsh_chain.lock;
# here spirv-cross may differ, with one note line naming it, while the translator (dmsh_xlate_bin) and the Metal compiler (macOS and iOS) must still be the locked ones.
# The Metal compiler is the one the shaders were tested with: shaders built with another one rendered wrong pictures (GitHub issue #242, Xcode 26 on macOS 26), so another one
# stops the build with a message naming the Xcode to install, unless DM_ALLOW_OTHER_METAL=1 (the NSP script's ALLOW_OTHER_METAL / --allow-other-metal), which builds with it and says so.
# Neither the chain version nor any repository file changes: a player builds the shader libraries and the host on the same Mac, so the two agree whatever the compilers,
# and an Xcode or spirv-cross other than the maintainer's (a beta, a brew upgrade) no longer needs dmsh_chain.py --update-lock, which changes the chain version (one for every game).
# The lock stays the record of the compilers the libraries were tested with; on the maintainer's Mac dmsh_build.py keeps the strict check.
# The per-shader cache takes spirv-cross's version into its keys but the Metal compiler only through the lock, so after a Metal compiler change the shaders already in the cache
# and a spirv-cross other than the maintainer's (a brew upgrade) needs no dmsh_chain.py --update-lock, which changes the chain version (one for every game).
# The lock stays the record of the compilers the libraries were tested with; on the maintainer's Mac dmsh_build.py keeps the strict check. This file is not part of the chain version.
# The per-shader cache takes spirv-cross's version into its keys but the Metal compiler only through the lock, so after a Metal compiler change (allowed) the shaders already in the cache
# keep the libraries the earlier compiler made and only new ones are compiled with this Mac's (the Metal compile cache, dmsh_mtlcache.py, is keyed by the compiler itself).
# Usage: own_compilers.py <dmsh_build.py arguments> (the environment as for dmsh_build.py)
import os, re, sys, runpy
# own_compilers.py --check-metal only the Metal compiler check: exit 1 with the message when it would stop the build (the NSP script runs it before its shader steps)
import os, re, sys, runpy, subprocess
HERE = os.path.dirname(os.path.abspath(__file__)); sys.path.insert(0, HERE); import dmsh_chain
OWN = re.compile(r'(spirv-cross|metal_macosx|metal_iphoneos): lock (.*) / actual (?!missing$)(.*)') # a missing compiler still stops the build
ALLOW = 'DM_ALLOW_OTHER_METAL'
XCODE = 'Xcode 27.1 (macOS 26.6 or later)' # the Xcode whose Metal compiler dmsh_chain.lock records; README.md names it too
strict = dmsh_chain.check_tools

def allowed(): return os.environ.get(ALLOW) == '1'

def metal_version(sdk): # as dmsh_chain.tools_now records it: the first line of metal --version, 'missing' without one
r = subprocess.run(['xcrun', '-sdk', sdk, 'metal', '--version'], capture_output=True, text=True)
return (r.stdout.splitlines() or ['missing'])[0]

def other_metal_message(metal):
"""The stop message for the Metal compilers that differ from the lock: [(lock key, tested, actual)]"""
return ('[RED] this Mac\'s Metal compiler is not the one the shaders were tested with (dm/gfx/tools/dmsh_chain.lock): ' +
'; '.join(f'{k} {now} (tested: {want})' for k, want, now in metal) +
f'. Shaders built with another Metal compiler can render wrong pictures (GitHub issue #242). Install {XCODE} and its Metal toolchain '
f'(xcodebuild -downloadComponent MetalToolchain), or set {ALLOW}=1 (the NSP script\'s --allow-other-metal) to build with this one anyway')

def other_metal():
"""The Metal compilers (macOS, iOS) that differ from the lock and are present: [(lock key, tested, actual)]; a missing one is dmsh_build.py's to report"""
with open(dmsh_chain.LOCK) as f: want = dict(l.rstrip('\n').split('\t', 1) for l in f if '\t' in l and not l.startswith('#'))
out = []
for sdk in ('macosx', 'iphoneos'):
now = metal_version(sdk)
if now != 'missing' and now != want.get('metal_' + sdk): out.append(('metal_' + sdk, want.get('metal_' + sdk), now))
return out

def check_metal(): # --check-metal: the stop message, '' when the build may go on
metal = other_metal()
return other_metal_message(metal) if metal and not allowed() else ''

def check_tools():
bad, own = [], []
for b in strict(): (own if OWN.fullmatch(b) else bad).append(b)
if own and not bad: print('note: built with this Mac\'s compilers, not the ones the shaders were tested with (dmsh_chain.lock): ' +
'; '.join(f'{m[1]} {m[3]} (tested: {m[2]})' for m in map(OWN.fullmatch, own)), flush=True)
return bad
for b in strict():
m = OWN.fullmatch(b)
if m: own.append((m[1], m[2], m[3])) # (lock key, tested, actual)
else: bad.append(b)
if bad: return bad
metal = [o for o in own if o[0] != 'spirv-cross']
if metal and not allowed(): sys.exit(other_metal_message(metal))
if own: print('note: built with this Mac\'s compilers, not the ones the shaders were tested with (dmsh_chain.lock): ' +
'; '.join(f'{k} {now} (tested: {want})' for k, want, now in own) + (f' ({ALLOW}=1)' if metal else ''), flush=True)
return []

dmsh_chain.check_tools = check_tools # dmsh_build.py imports the same module object and calls it through the module
sys.argv = [os.path.join(HERE, 'dmsh_build.py'), *sys.argv[1:]]
runpy.run_path(sys.argv[0], run_name='__main__')
if __name__ == '__main__':
if sys.argv[1:] == ['--check-metal']:
msg = check_metal()
if msg: print(msg)
sys.exit(1 if msg else 0)
dmsh_chain.check_tools = check_tools # dmsh_build.py imports the same module object and calls it through the module
sys.argv = [os.path.join(HERE, 'dmsh_build.py'), *sys.argv[1:]]
runpy.run_path(sys.argv[0], run_name='__main__')
1 change: 0 additions & 1 deletion dm/tools/lint_python_baseline.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ pyright dm/gfx/tools/dmsh_build.py reportOptionalMemberAccess 4 "group" is not a
pyright dm/gfx/tools/dmsh_build.py reportOptionalSubscript 13 Object of type "None" is not subscriptable
pyright dm/gfx/tools/gl_shaders.py reportArgumentType 1 Argument of type "list[str]" cannot be assigned to parameter "iterable" of type "Iterable[LiteralString]" in function "join" "list[str]" is not assignable to "Iterable[LiteralString]" Type parameter "_T_co@Iterable" is covariant, but "str" is not a subtype of "LiteralString" "str" is not assignable to "LiteralString"
pyright dm/gfx/tools/gl_shaders.py reportCallIssue 1 No overloads for "join" match the provided arguments
pyright dm/gfx/tools/own_compilers.py reportOptionalSubscript 3 Object of type "None" is not subscriptable
pyright dm/gfx/tools/shader_split.py reportArgumentType 1 Argument of type "Literal['m_Name']" cannot be assigned to parameter "s" of type "slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None]" in function "__getitem__" "Literal['m_Name']" is not assignable to "slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None]"
pyright dm/gfx/tools/shader_split.py reportArgumentType 1 Argument of type "Literal['m_ParsedForm']" cannot be assigned to parameter "s" of type "slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None]" in function "__getitem__" "Literal['m_ParsedForm']" is not assignable to "slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None]"
pyright dm/gfx/tools/shader_split.py reportArgumentType 1 Argument of type "Literal['variants']" cannot be assigned to parameter "s" of type "slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None]" in function "__getitem__" "Literal['variants']" is not assignable to "slice[SupportsIndex | None, SupportsIndex | None, SupportsIndex | None]"
Expand Down
Loading