Skip to content

fix: an update that makes startup shaders compiles their libraries, and update_runtime.py --shader-libs completes them - #2297

Merged
ly05010419 merged 1 commit into
ly05010419:mainfrom
oxyp1e:pr/totk-shader-libs
Nov 19, 2026
Merged

ly05010419 merged 1 commit into
ly05010419:mainfrom
oxyp1e:pr/totk-shader-libs

Conversation

@oxyp1e

@oxyp1e oxyp1e commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

What

Tears of the Kingdom / Breath of the Wild (and the Switch 2 games built from games/totk) on the Mac load their startup shaders from libraries compiled at packaging (step_shader_libs, PR #16). This PR makes sure an update that changes the startup shaders also gets their libraries, and adds a way to repair an app that is missing them:

  1. No Metal compiler stops update_runtime.py --seed (and --shader-libs) before the work folder or the app is touched, with the fix in the message (Xcode's Metal toolchain, xcodebuild -downloadComponent MetalToolchain, DEVELOPER_DIR). step_shader_libs gets required=; with it a missing compiler is a BuildError instead of a one-line note and return.
  2. update_runtime.py "<app>" --shader-libs completes the libraries of the seed the app already has: the app's seed and libraries are cloned into the work folder (APFS clones), only the shaders without a library are compiled (on all cores, as before), the ShaderData bundle is sealed again with the full set, and the pipeline archive is checked against the new libraries. It needs no RomFS or modules in the work folder. The seed must be of the runtime's chain (otherwise it says to use --seed). --seed also takes the app's libraries into an empty work folder first, as the list path (work_files_from_app, cee07b6) already does, so only the changed shaders compile.
  3. mac_pipelines: the names of build/shader-libs/*.metallib are part of the archive's input stamp. A new library set then runs the tool with TOTK_PAR_RECHECK=1, so pipelines made while those shaders were compiled from source are checked against the offline libraries the game now loads ("a wrong file is a miss", totk_pipeline_archive.h). With no libraries the stamp is byte-identical to before.
  4. docs/install.md and install.zh-CN.md: the fast update and --shader-libs need the Metal toolchain; when to use --shader-libs.

Why (root cause)

On my TotK 1.4.3 app, cold starts compiled shaders for minutes. The cause had three parts:

  • Stale libraries. The app's ShaderData held 4,397 libraries from translation chain v8 (Oct 26 to 29), while its seed was chain v9 (Nov 2 to 3). Libraries are named by the SHA-256 of their MSL, and the v9 MSL changed, so only 1,890 of the 17,884 shaders the warm-up loads (10.6 %) had a library. The others went through newLibraryWithSource on every cold start.
  • The quiet skip. That update's log is gone, but only one path in the code leaves a new seed with the old libraries: step_shader_libs returning when metal_compiler() finds nothing. It prints one line and returns, and the update finishes with the new seed and the old libraries. The Command Line Tools that xcode-select pointed at have no metal, and the Xcode on this Mac may not have had its Metal toolchain component yet. Nothing after that rebuilds the libraries: the express update moves the ShaderData bundle from app to app unchanged, so there was no way back short of another --seed with a toolchain (and work-folder game files).
  • The shared Metal cache. Source-compiled libraries then lived only in macOS's Metal cache, which on macOS 27 is per bundle ID ($(getconf DARWIN_USER_CACHE_DIR)/<bundle id>/com.apple.metal), with one shared root pool for processes without one. Before becf1249 the game binary had no bundle ID, so it shared that root pool with every bundle-less tool (other ports' build tools and runtimes), which evict each other's entries (the pool is capped at about 1 GiB per file; third-party reports in sharpemu-mac#50 and mullion#171, consistent with what I saw). becf1249 already gives the game binary its own id (org.macnativegames.totk-native), and it is on main. This PR does not touch it. That also means the --seed fallback without a compiler, step_warm_metal, now warms a cache the game no longer reads: its tool (build_warm_tool) has no __info_plist, so it fills the root pool. With no compiler, --seed made an app that compiles everything from source at its next cold start. That is why the fast update now stops instead.

Shipping AIR libraries is Apple's guidance for exactly this (WWDC22 10102: building a library from source "can be a CPU intensive operation, which you can move to app build time by pre-compiling your source to a Metal library"; WWDC20 10615 and Creating binary archives from device-built PSOs for the archive side). MNG already does this. The fix only makes sure it isn't skipped.

What changes for players

  • A fast update (--seed) on a Mac without Xcode's Metal toolchain now stops at its start (nothing changed, the message says what to install). Before, it went on and produced an app that compiles its startup shaders from source at every cold start. update_runtime.py --plan still answers fast there, and the stop comes when the update starts. Ocarina of Time puts the same check in plan() (c2334dc). I can add that for TotK/BotW here or in a follow-up. If you would rather keep --seed going without a compiler, it is one argument (required=args.seed in make_seed) and the rest of the PR stands.
  • A full build (build_app.py) without the toolchain goes on as documented, now with a WARNING: line. So does the express update's list path (new shaders from the newest shared list, list_adds_shaders) and the Launcher's Check shaders (shader_update.py): those keep the app's libraries and add a few shaders, so a missing compiler costs only those few.

What does not change

  • The runtime, the app layout, the shader lookup (shader_lib_prebuilt), the seed and the express update without flags.
  • The game binary's bundle id: becf1249 (games/totk/runtime/game_info.plist, GAME_INFO in all 7 link rules) is already on main. Nothing here touches it.
  • step_warm_metal still runs after --seed (and after --shader-libs). With complete libraries it skips itself (build_app.py "the startup shaders are compiled into the app"). Pointing its tool at the game's own cache id is a possible follow-up.
  • --shader-libs does not translate the newest list's new shaders (it short-circuits list_adds_shaders, as --seed does). Their pipelines still go into the warm-up lists without a library, the same as upstream does today when can_translate says no.
  • --shader-libs and --seed together are refused. Ocarina of Time, Skyward Sword and the standalone ports refuse --shader-libs.

Tests

  • New tests/test_update_shader_libs.py (12): the app's seed and libraries are adopted (through the ShaderData links), an older work seed and its .verified.json are replaced and the work folder's libraries kept, an app without a seed is refused, no compiler stops --seed and --shader-libs before prerequisites and the work folder, a plain update needs no compiler, both flags together are refused, and which steps main() runs for --shader-libs / --seed / list-only / plain.
  • tests/test_variant_libs.py NoMetalCompiler (2): required=True raises with the toolchain and DEVELOPER_DIR, and a build without it goes on with WARNING.
  • tests/test_mac_pipelines.py MacPipelinesStamp (1): an app without libraries keeps its old stamp, and a new library set changes the stamp and runs the tool with TOTK_PAR_RECHECK=1.
  • tests/test_shader_update.py: the two source lines it pins in main() follow the change.
  • Run locally on this branch and on a clean main @ 597cf54, with the same environment (python 3.14 venv from games/totk/requirements.txt, scratch HOME and game slot): 47 test modules (the 46 that import build_app / update_runtime / shader_update, plus test_express_update_speed). main: 511 tests, PR: 526. Same result on both: 2 errors in test_work_copy_links (rsync on this Mac cannot mkdir the nested target, exit 11; environment, not this change), 1 skipped. Everything else passes.
  • make check-c: passes on both. py_compile of the changed files, git diff --check: clean.
  • Not run locally: the full discover (it includes real Metal work, test_warm_metal, test_oot_hmma_calls, and I kept Metal off this Mac while it was in use). CI runs it.
  • CI: fork PRs wait for a maintainer to approve the workflow. The same workflow on my fork's push of this branch passed: run 44441422936 (Source tests core and settings, Launcher local-build regressions, and the test gate with make check-c, all green). That covers the full discover I did not run locally. Main at the base, 597cf54, is green too (run 44435769647).

Tested on a Mac

  • Apple M5 MacBook Pro, macOS 27, TotK 1.4.3. I rebuilt my installed app with this change as a local commit on an older base (821ce8c + my local patches + becf1249's plist part), with update_runtime.py "<app copy>" --shader-libs --work <work folder> and DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer (Xcode 27.2, Metal toolchain 27B47).
    • Coverage: 1,890 → 17,855 of the 17,884 warm-up shaders (99.8 %). The other 29 have no MSL in the seed (the runtime translates them when it meets them).
    • 25,352 shaders compiled in 18.5 min with 0 failures (10 threads). The libraries come to 460 MB (28,872 files). The pipeline archive was rebuilt: 27,161 pipelines, 0 failed. Whole update 25 min, and the app's own --check passed.
    • I played it: about 20 s from launch to gameplay on a later start. Earlier the same evening, cold loads with the stale libraries took minutes.
    • The game process showed up under its own bundle id (org.macnativegames.totk-native) in macOS's logs. That is becf1249's part, already on main.
  • Not tested: this rebased branch itself was not built into an app or played (the port onto current main is the code above plus the adaptations: cee07b61's library adoption reused instead of duplicated, TOTK_PAR_RECHECK instead of a full archive rebuild, the list path). Not tested on iPhone/iPad (the change is Mac packaging only; iOS builds skip step_shader_libs), BotW, the Switch 2 games, or other Macs.

Not done

  • plan() does not report a missing compiler for TotK/BotW fast updates (see above).
  • variant_folder is still hard-coded to rt-totk-native-v9-unverified, harmless until the next chain bump.
  • No -mmacosx-version-min pin for the libraries. A library that fails to load falls back to source.

🤖 Generated with Claude Code

https://claude.ai/code/session_01NnHGEHeQzgBps2MXvnkJC9

…nd update_runtime.py --shader-libs completes them

A TotK 1.4.3 app kept the libraries of its old translation chain after its seed was made again: it had libraries for
1,890 of the 17,884 shaders its warm-up loads, and compiled the rest from source at every cold start. The only path that
leaves a new seed with the old libraries is step_shader_libs returning when metal_compiler() finds nothing (one line,
then the update went on); the command line tools xcode-select pointed at have no metal. That run's log is gone.

- step_shader_libs(required=True): no compiler is an error naming the toolchain and DEVELOPER_DIR. update_runtime.py
  --seed and --shader-libs check it before the work folder or the app is touched. A full build still goes on, with a
  WARNING line; the express update's new list shaders too.
- update_runtime.py --shader-libs: the app's own startup seed and libraries are cloned into the work folder, only the
  shaders without a library are compiled, and the shader data bundle is made again. No RomFS or modules needed; the
  seed must be of the runtime's chain. --seed also takes the app's libraries first (as the list path already did).
- mac_pipelines: the library set is in the archive's input stamp, so a new set checks the archive against the
  libraries the game loads (TOTK_PAR_RECHECK). Without libraries the stamp is unchanged.
- docs/install: the fast update and --shader-libs need Xcode's Metal toolchain; when to use --shader-libs.

Tests: test_update_shader_libs (adopting the seed, no compiler stops --seed / --shader-libs first, which steps run),
test_variant_libs NoMetalCompiler, test_mac_pipelines MacPipelinesStamp; test_shader_update follows main()'s lines.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01NnHGEHeQzgBps2MXvnkJC9
@ly05010419
ly05010419 merged commit 0581c1e into ly05010419:main Nov 19, 2026
1 check passed
ly05010419 added a commit that referenced this pull request Nov 19, 2026
…es itself, and --seed goes on without a compiler (after PR #2297)

PR #2297 made --seed stop without a Metal compiler. But update_runtime.py --plan still answered "fast" on such a Mac,
and MNG Launcher runs --seed from that answer (tools/builder_jobs/build_job.py). So the update failed halfway, and a
player without Xcode's Metal toolchain was left with only the full build. The repair (--shader-libs) also had to be
run by hand, by someone who knew of it.

- missing_shader_libs counts the startup shaders of the app's seed that have no compiled library. It reads the app's
  seed-manifest.json alone (a library is named by its .metal's SHA-256, build_app.shader_libs_of), so nothing is
  hashed.
- An express update with gaps and a compiler takes the --shader-libs path by itself. A list with new shaders makes a
  new seed instead, which compiles every shader still without a library (make_seed).
- step_shader_libs records the sources its compiler refused (build/shader-libs/refused.json, tied to compiler.txt).
  These are not gaps for that compiler, so one shader it cannot take does not send every later express update
  through the completion.
- --seed without a compiler makes the seed as a full build does, with a warning. --shader-libs still stops without
  one.
- plan() says this first. The express reason names the gap and what the update does about it (shader_lib_gaps), and
  the fast reason names the toolchain on a Mac without it. No update is refused for want of a compiler.
- The update's last line warns about any library still missing.
- The docs follow.

The Tears of the Kingdom, Breath of the Wild 1.9, Bananza and Pokopia apps here have no gap (2026-11-19).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ly05010419

Copy link
Copy Markdown
Owner

Thank you for tracing this to the quiet skip in step_shader_libs: 1,890 libraries for 17,884 warm-up shaders is a clear finding, and the root cause still held on main. Merged, with a follow-up commit 96687df:

  • --seed no longer stops without a Metal compiler. update_runtime.py --plan still answers "fast" on such a Mac, and MNG Launcher runs the fast update from that answer (tools/builder_jobs/build_job.py), so the update failed halfway, and players without Xcode's Metal toolchain were left with only the full build. It now makes the seed as a full build does, and --plan says beforehand that the new shaders get no compiled library.
  • The express update now repairs an app like yours by itself. missing_shader_libs counts the seed's .metal files without a library, from the app's seed-manifest.json alone, so nothing is hashed. On a Mac with the compiler it takes the same path as your --shader-libs, and --plan reports the gap (shader_lib_gaps). Sources the compiler refused are recorded (build/shader-libs/refused.json) and are not gaps for that compiler. The update's last line warns about any library still missing. The four apps on our Mac had none missing.
  • --shader-libs stays as the manual entry, and it still stops without the toolchain. Your stamp change in mac_pipelines is kept as it is. The docs say this now.

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