Repository navigation
Compile the startup shaders into the app when the Metal toolchain is present - #16
Merged
Merged
Conversation
…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]>
Owner
|
非常感谢 很好的改进 |
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
xcrun --find metalworks (Xcode's optional Metal toolchain:xcodebuild -downloadComponent MetalToolchain),step_shader_libscompiles each seed shader with the runtime's options (-fmetal-math-mode=safe) intobuild/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.shader_loadhashes 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.Measured
1.4.3, 8,290 startup shaders, M4 Pro, macOS 27.0.1:
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:
🤖 Generated with Claude Code