Skip to content

feat(ios): shader compilation on the iPhone and iPad with maxmetal - #1468

Closed
svich wants to merge 1 commit into
ly05010419:mainfrom
nx-ports:totk-maxmetal
Closed

svich wants to merge 1 commit into
ly05010419:mainfrom
nx-ports:totk-maxmetal

Conversation

@svich

@svich svich commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The iPhone / iPad app now compiles shaders itself. maxmetal (Maxwell to Metal Shading Language, plain C, MIT) is built into the app. When the game first asks for a variant the startup shaders lack, maxmetal translates it on the device and Metal compiles it there; the device keeps both. Before, such a variant was skipped until a Mac filled it in (fill_shaders.py).

  • Same results as Ryujinx: of TOTK 1.4.3's 31,307 variants, 31,305 give GPU output bit-identical to Ryujinx + SPIRV-Cross.
  • Faster GPU code: the heaviest fragment shaders of the frame take about 20% less GPU time than the Ryujinx route's (−9 to −27% each, maxmetal ADR 0003).
  • Light enough for the device: one C library, no C# translator, no SPIR-V and no SPIRV-Cross stage, so it builds into the app.
 a draw needs a shader variant
   in the startup shaders or the device's cache → use it
-  else (iPhone)  skip the draw; request it for fill_shaders.py on the Mac
+  else (iPhone)  maxmetal translates it in the app → Metal compiles it → both cached
+                 maxmetal cannot → skip the draw and request it, as before

iPhone apps without startup shaders. build_app.py --platform ios --no-startup-shaders builds an app with none: the build skips about an hour of translating and compiling them, and every shader is translated and compiled on the fly on the device.

build_app.py --no-startup-shaders     (iPhone / iPad only)
  skip step_shaders                   # no Ryujinx translation of ~31,000 shaders
  build_ipa.py --no-seed              # no compile_seed.py, warm lists or pipeline archive
    GameData/no-startup-shaders       # marker for install_device.py
  no shader-contribution reminder
install_device.py
  marker → Documents/GameData/shader-seed.txt = "none"   # an earlier install's seed is not used
main.m
  no seed → play on (was: "No shaders" screen); "none" → remove the old seed folders
  copy cut short ("stale") → "No shaders" screen, as before
totk_shader_rt.m contrib_wanted, totk_rt_contrib_on
  no seed → no shader or pipeline rows   # every one would look new to the shared list

Overlay. The shader box on the device gets a second line: Compiled shaders N (X s) maxmetal N (X s). These are the Metal libraries compiled from MSL on the device this run and the variants maxmetal translated, each with its total time.

The Mac keeps the Ryujinx translator. maxmetal is pinned in build_app.py (MAXMETAL, 919805d).

Evidence

The shader compiler was tested successfully on the Mac and on an iPhone 13 (A15, iOS 26) with TOTK 1.4.3. The iPhone build used --no-startup-shaders, so maxmetal translated every shader the game used.

  • Before: an iPhone build needed the startup shaders; without them the app showed "No shaders" and did not start. A variant they lacked was skipped until the Mac filled it in.
    After: the game starts and renders with no startup shaders at all. "Missing shaders" stays at 0, and none of maxmetal's translations failed.

Time per shader on the iPhone, first launch (478 variants):

Step Median p90 Max Total
maxmetal translation 4.0 ms 16 ms 378 ms 4 s
Metal library compile 86 ms 375 ms 2.5 s 76 s
Render pipeline compile 56 ms 421 ms 8.3 s 98 s

On the second launch the device's caches serve almost everything:

1st launch 2nd launch
First frame 33 s (one-time game-data check) 19 s
maxmetal 478 translated, 4 s 46 new, 1 s
Metal library compile 76 s 12 s (median 1.7 ms, iOS's own cache)
Render pipelines 98 s 23 s

maxmetal at 919805d builds for the Mac and iOS; its golden tests pass 22/22 and the Metal compiles 18/18. python -m unittest discover -s tests: 1,302 tests OK (169 skipped).

Merge Danger

Door: two-way

A build without --no-startup-shaders is unchanged, except that a variant its startup shaders lack is translated instead of skipped. Reverting brings the skipping back. Translations that maxmetal has already left in a device's cache stay there and remain valid MSL.

Blast Radius: iOS

  • Breath of the Wild iPhone / iPad builds translate with maxmetal too, but its bit-exactness was checked on TOTK's shaders only.
  • maxmetal writes into the same cache folder as the Ryujinx translations (rt-totk-native-v8-unverified), because its output is meant to be bit-identical. A later maxmetal pin does not invalidate what devices have cached.
  • Without startup shaders the first play stutters while Metal compiles (single frames of up to 19 s on the iPhone 13). Some draws are skipped while their pipeline is still compiling in the background. This only happens with --no-startup-shaders, which is opt-in.
  • The Mac only gains a compile counter in totk_msl_library; its overlay is unchanged.

The iPhone / iPad app now translates shaders itself: maxmetal (Maxwell to Metal Shading Language, plain C) is built
into the app and translates on the device every shader variant the startup shaders lack, as the game first asks for
it. Before, such a variant was skipped until a Mac filled it in.

maxmetal gives the same results as the Ryujinx + SPIRV-Cross route: 31,305 of the game's 31,307 variants produce
bit-identical GPU output. Its heaviest fragment shaders take about 20% less GPU time (maxmetal docs/adr/0003). It has no
C# translator, SPIR-V or SPIRV-Cross step, which is what lets it run on the device at all.

build_app.py --no-startup-shaders builds an iPhone / iPad app with no startup shaders: the build skips about an hour of
translating and compiling them, maxmetal translates every shader on the device and Metal compiles it there; the device
keeps them for later starts. install_device.py makes the device stop using startup shaders an earlier install left.

The shader box on the device gets a second line: the Metal libraries compiled on the device this run and the variants
maxmetal translated, each with the seconds it took.

The Mac keeps the Ryujinx translator.

Tested: the shader compiler on the Mac, and on an iPhone 13 (iOS 26, TOTK 1.4.3) built with no startup shaders, so
maxmetal translated every shader: the game started and rendered, and none of the 478 variants of the session failed.
ly05010419 added a commit that referenced this pull request Nov 3, 2026
… lack, with the Mac's own translator (not yet tested on a device)

PR #1468 proposed a second translator (maxmetal) on the device. This links the one the Mac uses instead:
totk_xlate (Ryujinx's translator, the project's patches, Service.cs, SPIRV-Cross in process) as a NativeAOT static
library for ios-arm64 (prebuilt/ios-arm64/libtotk_xlate.a, tools/totk_xlate/publish_ios_lib.sh; Service.cs's request
handling moved into HandleLine, which the command-line service and LibExports.cs both call). build_app.py builds
SPIRV-Cross for the device from the pinned sources; build_ipa.py links both and defines TOTK_DEVICE_XLATE.
totk_shader_rt.m then sets the library up and calls it where the Mac writes to the service's pipe, and the rest is the
Mac's: background jobs (2 + 2 on the device), the Metal library, the variant index, contributions. nvn_mtl.m turns
async shaders on there (TOTK_ASYNC_SHADERS=0 turns them off), so a missing variant's draws are skipped for a few frames
instead of until the Mac fills it in. TOTK_SHADER_NO_TRANSLATE=1 keeps the old behaviour; visionOS is unchanged.
Checked: the library form translates 551 Breath of the Wild variants to the same 2,204 files as the shipped totk_xlate,
byte for byte, on the Mac and in the iOS simulator; the Mac's nvn_mtl.m and totk_shader_rt.m preprocess to the same
code as before (line numbers aside); iOS syntax with and without the translator. Not yet: an iPhone build and play.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ly05010419

Copy link
Copy Markdown
Owner

Thank you for maxmetal and the careful comparison: 31,305 of 31,307 variants matching Ryujinx bit for bit, and the faster fragment shaders, are impressive work.

It made us look again at the gap it fills: a variant the startup shaders lack is skipped on the device until the Mac fills it in. We closed that gap with the translator the Mac already uses instead of a second one (f04e947 on main): totk_xlate (Ryujinx's translator with our patches, Service.cs, SPIRV-Cross in process) is now also built as a NativeAOT static library for ios-arm64 (games/totk/prebuilt/ios-arm64, tools/totk_xlate/publish_ios_lib.sh), and the iPhone / iPad app translates such a variant in the background with it, its draws skipped only until it is ready. Everything else stays as it is: the startup shaders and the device pipeline archive are made on the Mac, and nothing is compiled on the submit thread.

On an iPhone 14 (Breath of the Wild 1.6.0, startup shaders turned off to force it): 606 variants translated in 2 minutes, none failed, translation plus Metal library median 141 ms, p90 442 ms; every MSL, SPIR-V and reflection file equals what the Mac's translator makes from the same requests. Being the same translator, its output and caches cannot drift from the Mac's, and there is one translation chain to fix and maintain.

So we will not merge this PR; closing it with thanks. The idea and your measurements were what led to this.

@ly05010419 ly05010419 closed this Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants