Skip to content

Compile warm-up shader libraries on all cores - #13

Closed
joevans wants to merge 1 commit into
ly05010419:mainfrom
joevans:parallel-shader-warmup
Closed

joevans wants to merge 1 commit into
ly05010419:mainfrom
joevans:parallel-shader-warmup

Conversation

@joevans

@joevans joevans commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

warm_worker compiles the warm-up shader libraries one at a time on its own thread. With a cold system Metal cache, only one MTLCompilerService is busy (about 100% of one core) and the rest of the machine sits idle. On a first launch with 9,360 seed shaders, that's roughly 35 minutes before the first frame on an M4 Pro.

This PR adds warm_precompile, called at the start of warm_worker. On a background queue it compiles the same MSL sources with the same options (totk_precise_opts()) on every core, using dispatch_apply with shouldMaximizeConcurrentCompilation = YES, in the order warm_worker will visit them. The results are dropped, as in warm_light. Only the system Metal shader cache gets filled, so warm_worker's own newLibraryWithSource for each shader becomes a cache hit.

  • warm_worker is otherwise untouched: same order, progress, counts, priority gate and NvShader loading.
  • Shaders warm_shader would skip (double , unsupported_GLSLstd450) are skipped here too.
  • totk_precise_opts() is created on the calling thread first, because its lazy initialisation isn't thread-safe.
  • TOTK_WARM_PARALLEL=0 turns it off. iOS is unaffected (#if !TARGET_OS_IPHONE).
  • Docs: item 6 in docs/first-launch-shaders.md.

Measurements (Mac mini M4 Pro, 14 cores, macOS 27.0.1)

Compiling 400 real seed shaders from the user cache in a standalone process:

Per shader
Cold, one at a time (today's behaviour) 232 ms
Cold, parallel (dispatch_apply, 14 compile tasks) 27.8 ms effective
Already cached, one at a time ~0 ms

In the game (on top of #11, which macOS 27 needs to start at all):

  • 并行预编译完成:9360 个着色器库,0 个失败,用时 148.3 s
  • 管线预热完成:着色器 9362 个 … 后台用时 148844.8 ms, then first presented frame at 158 s.
  • The previous single-threaded launch on the same machine had warmed about 3,800 of 9,360 shaders after more than 15 minutes.

Caveat: the system Metal cache wasn't cleared before the in-game run, so about 4,200 of the 9,360 shaders were already cached from that earlier launch. The remaining ~5,100 cold compiles in 148 s (~29 ms each) match the standalone parallel figure. A fully cold run should take about 4–5 minutes on this machine instead of ~35.


中文摘要:预热线程逐个编译着色器库,系统 Metal 缓存全冷时只有一个编译进程在忙。本 PR 在后台按预热顺序用全部核心预编译同一批 MSL(同选项,结果丢弃,只填系统缓存),预热线程随后即为缓存命中;顺序、进度、计数不变,TOTK_WARM_PARALLEL=0 关闭。M4 Pro 实测冷编译 232 ms/个 → 并行 27.8 ms/个。

🤖 Generated with Claude Code

warm_worker compiles one MSL library at a time; with a cold system Metal
cache only one MTLCompilerService is busy and the other cores sit idle
(first launch: 9360 libraries, ~35 min on an M4 Pro).

warm_precompile now compiles the same sources with the same options on every
core, in the order warm_worker visits them, and drops the results, so
warm_worker's own compiles become system-cache hits. warm_worker, its order,
progress and counts are unchanged. TOTK_WARM_PARALLEL=0 turns it off.

Measured on 400 real seed shaders (M4 Pro, macOS 27): 232 ms each cold and
serial, 27.8 ms each in parallel, ~0 ms each once cached.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@RVZO6

RVZO6 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Tested on an M4 Pro Mac (12 cores, macOS 27.0.1, TOTK 1.4.3), with #3 and #12. 👍

I compiled the same 96 real seed shaders, renaming the entry points each pass so nothing came from the cache, two runs per setting:

Workers Mean (s)
1 17.80
4 5.16
12 (this PR) 2.64

That's about 6.7× faster than one worker and 2× faster than four, with no failures. With a full cache, in-game warm-up is unchanged: 5.69 s, against 5.75 s with four workers. This measures library compilation only, not a full cold startup.

tvc97 added a commit to tvc97/MacNativeGames that referenced this pull request Oct 19, 2026
…05010419#30)

Brings in upstream ly05010419#29 (bounded pipeline prediction without translation,
stale guesses reclaimed) and ly05010419#30 (in-game libraries and pipelines in the
background, plus the present residency and compile-options fixes).

Conflict resolution in nvn_mtl.m / hle_nvn.c: took the branch side,
re-applied what main has since ly05010419#29's base (ly05010419#13 warm_precompile, ly05010419#17
compact HUD), and kept main's prebuilt-library hit/miss diagnostic.
Our earlier prediction-table drain (pso_pend_drain into g_pso,
prog_get_cached, mtl_pp_stats and its log line) is replaced by ly05010419#29's
version. It no longer skips pso_user.tsv for drained pipelines.

Checked: make runtime-v143 (58 warnings, none new), 38 source tests,
probe-gfx pass, 3 empty-cache async boots without a GPU fault.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tvc97 added a commit to tvc97/MacNativeGames that referenced this pull request Oct 19, 2026
…y05010419#38)

Conflicts resolved toward the refreshed PR branches: nvn_mtl.m from
perf/async-shader-loading (ly05010419#30 on current upstream) plus ly05010419#13's parallel
warm-up and the precompiled-library hit/miss log; prepare_shader_seed.py
from perf/parallel-shader-seed (ly05010419#27); runtime/cheats.c kept in every
runtime target and in loader.c. AGENTS.md stays tracked here.

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

joevans commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

Closing: this is covered by 35606fe ("compile the background warm-up's shader libraries on several threads"), which parallelizes the same warm-up compiles directly in warm_worker. Thanks for picking it up.

@joevans joevans closed this Oct 19, 2026
@joevans
joevans deleted the parallel-shader-warmup branch October 23, 2026 00:06
nik-2002 added a commit to nik-2002/MacNativeGames that referenced this pull request Nov 2, 2026
…r picture mode

The renderer's per-frame update (main+0x581f58) compares the display mode it
wants with renderer+0xcd0 only when bit 13 of renderer+0xca4 is set
(main+0x581434 tbnz w9, ly05010419#13). render_reread wrote -1 to the mode byte but
left that bit to the game, so the size was read again only when the game
happened to set it: started in 720p, gone to 1080p, then 720p render and
720p + MetalFX kept the 1920x1080 scene (the overlay and the upscale's source
said 1920x1080). render_reread now sets the bit too, and a short-lived thread
sets it again (the game writes that word with plain loads and stores) until
the mode byte is no longer -1, for up to 10 s, logging the outcome.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014gG6xb2wdBKczP1NgMQwBU
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