Skip to content

Compile the startup shaders into the app when the Metal toolchain is present - #16

Merged
ly05010419 merged 1 commit into
ly05010419:mainfrom
RVZO6:fix/totk-precompiled-shaders
Oct 18, 2026
Merged

ly05010419 merged 1 commit into
ly05010419:mainfrom
RVZO6:fix/totk-precompiled-shaders

Conversation

@RVZO6

@RVZO6 RVZO6 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Moving or rebuilding the app currently repeats the whole first-launch shader warm-up (about 4.5 minutes here). Metal's own cache of libraries compiled at launch starts over after a move, even when the app is otherwise identical.

Change

  • Build: if xcrun --find metal works (Xcode's optional Metal toolchain: xcodebuild -downloadComponent MetalToolchain), step_shader_libs compiles each seed shader with the runtime's options (-fmetal-math-mode=safe) into build/shader-libs/<SHA-256 of the MSL>.metallib. The app ships the libraries that match its seed. Rebuilds compile only shaders whose source changed. Failures are logged and left to the runtime.
  • Runtime (macOS): shader_load hashes the local MSL and loads the library with that hash, if the app has one. Otherwise it compiles from source as before. A stale or edited source can never pick up the wrong library.
  • Without the toolchain, the build prints one line, and the build and the game behave exactly as before.

Measured

1.4.3, 8,290 startup shaders, M4 Pro, macOS 27.0.1:

Before After
Warm-up, first launch at a new location 291 s 17.8 s
Warm-up after moving the app 274 s 6.6 s
Warm-up after a rebuild with a changed binary — 6.6 s (0 shaders recompiled)

The build step takes 2 min 22 s from scratch and adds 131 MB to the app. Rendering at the title screen was unchanged, with the same log errors as a source-compiled run. Gameplay was not tested beyond that.

Note on #13

If #13 lands, its parallel pass should skip shaders that have a precompiled library. Otherwise it recompiles them for nothing. I tested this combination locally:

NSData *raw = [NSData dataWithContentsOfFile:[rt stringByAppendingFormat:@"/%@.metal", order[i]]];
if (!raw || shader_lib_prebuilt_path(raw)) return;   // compiled with the app
NSString *msl = [[NSString alloc] initWithData:raw encoding:NSUTF8StringEncoding];

🤖 Generated with Claude Code

…present

Metal's own cache of libraries compiled at launch starts over when the app is
moved or rebuilt, which repeats the full 4.5-minute warm-up. With Xcode's Metal
toolchain installed, the build now compiles every startup shader with the
runtime's safe-math options into build/shader-libs/<source SHA-256>.metallib
and ships them in the app. The game loads a library only when the SHA-256 of
its local MSL matches, and compiles from source otherwise. A rebuild compiles
only changed shaders. Without the toolchain the build and the game behave as
before.

Measured on an M4 Pro (1.4.3, 8,290 startup shaders, 131 MB of libraries,
2 min 22 s to compile at build time):
- first launch at a new location: warm-up 291 s -> 17.8 s
- after moving the app: warm-up 274 s -> 6.6 s
- after a rebuild with a changed binary: 0 shaders recompiled, warm-up 6.6 s

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

Copy link
Copy Markdown
Owner

非常感谢 很好的改进

@ly05010419
ly05010419 merged commit f5cc151 into ly05010419:main Oct 18, 2026
1 check passed
ly05010419 added a commit that referenced this pull request Oct 19, 2026
… ones

Metal's offline compiler defaults to -fmetal-math-mode=fast. compile_seed.py
(the IPA's startup shaders) and fill_shaders.py (the device's misses) did not
pass a math mode, so every library the iPhone/iPad loads was built with fast
math, which the runtime avoids on purpose (totk_precise_opts: it breaks the
translated shaders' float integer-division idioms). Found while reviewing #16,
which passes -fmetal-math-mode=safe on the Mac.

- compile_seed.py: METAL_FLAGS = safe math, recorded in the manifest
  (ios_metal_flags); fill_shaders.py uses the same flags.
- build_ipa.py: reuses compiled libraries only when the flags match, so the
  next IPA rebuilds the old fast-math ones.
- totk_shader_seed.h: a seed or fill pack whose .metallib differs from the
  cached one replaces it (a device library is only ever made from its source
  on the Mac); other cached files are still kept. Test added.
- fill_shaders.py --with-done: rebuilds the variants filled before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ly05010419 added a commit that referenced this pull request Oct 19, 2026
… toolchain is present

Resolved against the local warm-up work: shader_load keeps its atomic
counters and totk_msl_library (retries an interrupted Metal compiler) and
tries the app's prebuilt library first. A prebuilt library is skipped under
TOTK_FAST_MATH=1, since it was built with safe math. step_warm_metal (the
packaging pre-compile into Metal's cache) is skipped when every seed shader
already has a library in the app. Docs and README keep both texts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ly05010419 added a commit that referenced this pull request Oct 19, 2026
…; only moving the app makes it cold

PR #16 said the startup shaders are compiled again after the app is moved or
rebuilt. Since 82912da the build compiles them into Metal's cache for the app's
location (step_warm_metal), and that cache key only depends on the length of
the program folder's path (claude.md lesson 105 in the original port): a rebuild
in place hits it, moving the app to a folder whose path has another length
does not. The build message, README (EN/ZH), docs and the nvn_mtl.m comment
now say that.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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