Skip to content

The shader build runs as worker processes, and no shader waits for a batch (Ocarina of Time's cold Mac map: 3,002 s to 1,298 s) - #2035

Closed
Know-It-All-Brother wants to merge 1 commit into
seedds:mainfrom
Know-It-All-Brother:shader-build-speed
Closed

Know-It-All-Brother wants to merge 1 commit into
seedds:mainfrom
Know-It-All-Brother:shader-build-speed

Conversation

@Know-It-All-Brother

Copy link
Copy Markdown
Contributor

What and why

On an M5 Max with 18 cores, a cold shader build left about half the cores idle whatever J was, and dmsh_build.py's own process sat at one full core the whole time. There were two causes. Each shader needs about 40 ms of the builder's own Python, and one process runs that one shader at a time under the GIL. On top of that, every batch of 4 × J shaders waited for its slowest shader before the next one started.

The build now runs as DMSH_PROCS copies of itself (default J / 3, so 6 at J=18; DMSH_PROCS=1 keeps the old single process). Each copy builds a share of one phase and stages its outputs in the same cache, and the build that started them writes the library as before. Within a process, the misses are translated in chunks of 8, largest first, and each shader is finished as soon as its own chunk is translated, so no worker waits for a batch. Paired vertex shaders still run after every other shader.

This answers the open note under Rewrites that try every character of a large source, which asked to measure worker processes again on a machine with more cores. The new entry is A shader build's Python finishes one shader at a time.

Checked

  • Measurements: M5 Max with 18 cores, macOS 27, regular Python 3.14, every file on the internal disk, empty cache.
    • Ocarina of Time's whole Mac map (0400486024DEC000 v131072, 42,848 shaders): 3,002 s before, 1,298 s after. CPU went from 25,974 s to 20,859 s.
    • 1,714 keys (3,358 shaders): 193 s before, 157 s with the scheduling alone, 101–114 s with workers.
  • Identical output: all 173,316 files of the whole map, apart from the chain version in each _b.json and the digests in _b_cas.txt. c1_full.tsv and cb_extent.tsv match apart from their header line.
  • Tests:
    • The dm/tools/tests that cover the build pass. I updated test_shader_toolchain_cache.py to run the new scheduler instead of the batch loop.
    • test_dmsh_abi (test_no_new_abi_literals) and test_nsp_incremental (test_provisional_run_invalidates_derivation) fail the same way on main.
    • lint_python.py: 0 new findings.
  • Tried with no gain: translator chunks of 32, DOTNET_TieredPGO=0, and compiling a library straight from its source. The last one also gives other bytes.
  • regress.py check: I don't have it. Ocarina of Time is the only game on this Mac. The change only reorders the work and gives the same bytes, so the whole-map byte comparison above is what I'd offer instead.
  • Not measured: a Mac with fewer cores, and an iOS build (IOSOUT), whose libraries go through the same workers.

Docs

  • The finding is in docs/reference/shaders.md and in Ocarina of Time's note, and docs_symptoms.py was run.
  • A chain tool changed: dmsh_chain.py --header was run and dmsh_chain.h is part of the change.
  • No game files, keys or extracted data in the change.

@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) tools Tools and scripts (dm/tools, scripts) docs Documentation shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders game: 0400486024DEC000 A game labels Nov 9, 2026
…batch: Ocarina of Time's whole Mac map, built cold on an M5 Max with 18 cores, takes 1,298 s instead of 3,002

Each shader needs about 40 ms of dmsh_build.py's own Python, which one process runs one shader at a time under the GIL, so the cores were half idle whatever J was. A build now runs as DMSH_PROCS copies of itself (default J / 3), each building a share of a phase and staging its outputs in the same cache; the build that started them writes the library as before. Within a process the misses are translated in chunks of 8, largest first, and each shader is finished as soon as its chunk is translated, instead of every worker waiting for its batch's slowest shader. The outputs are byte-identical (all 173,316 files of the whole map, apart from the chain version); the chain version changes once.
Know-It-All-Brother added a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…ts for a batch) into best-attempt; chain 49ede9ecd9d6
FeReuss pushed a commit to FeReuss/nxnative-oot that referenced this pull request Nov 10, 2026
…d runs as worker processes, and no shader waits for a batch (Ocarina of Time's cold Mac map: 3,002 s to 1,298 s)

chain header

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tobiashenn

Copy link
Copy Markdown

Measured on a Mac with fewer cores: M3 Pro (11 cores, 5 performance + 6 efficiency), 18 GB

Setup:

  • Mac and toolchain: MacBook Pro Mac15,6 with the 14-core GPU, macOS 27.3 (26C5035r), Xcode 27.2 (Metal 32023.922), Homebrew Python 3.15.0 with the GIL. Every file is on the internal disk.
  • Sample: Ocarina of Time v131072, dmsh_build.py through own_compilers.py, Mac only. 1,714 base keys, every 25th of the sorted shader_map.tsv keys, which is 2,833 shaders with their pairs.
  • Each run: an empty DMSH_CAS, its own output and work folder, J=11 (the NSP script's default here), and DMSH_PROCS at its default (4).
  • Compared: main at 04811a6 and this PR at 73320a2 (04811a6 plus this commit), run in turn, two runs each.
main 04811a6 PR 73320a2
Wall time 294 / 288 s 207 / 205 s
CPU (user + sys) 1,875 / 1,865 s 2,111 / 2,105 s
Peak memory of the process tree (summed RSS, sampled every 2 s) 1.69 / 1.75 GB 3.03 / 2.96 GB
  • Wall time: about 29% less (1.41× faster), for about 13% more CPU time. On the M5 Max whole map, CPU time went down instead.
  • Memory: about 1.3 GB more at the peak. On 18 GB with other apps open that was fine, but 8 and 16 GB Macs are where to watch it.
  • Same work: both versions built 2,833 of 2,833 shaders, with the same counts of side libraries in the summary and no [RED] lines. I did not compare the output bytes.

For scale, the full cold Mac map of chain 39eb2f781d07 (main, J=11) took 1:18:51 here this morning. The CPU was about 70% busy on average, and the builder's process used up to one full core.

Not tried: other DMSH_PROCS or J values, and a whole-map run with this PR.

Know-It-All-Brother added a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 10, 2026
… nvn_handle_expr.h), wide and atomic rewrites, loop_guard, the worker-process shader build (PR seedds#2035), iOS Metal compile changes, and the chain version these sources give (dmsh_chain.h)
Fasen24-AI added a commit to Fasen24-AI/swordboiMacNative that referenced this pull request Nov 10, 2026
seedds#1901 seedds#1459 seedds#1928 seedds#1942 seedds#1940 seedds#1952 seedds#1611 seedds#2065 seedds#2083 seedds#2008 seedds#2079 seedds#2022 seedds#2075 seedds#2077 seedds#2020 seedds#2021 onto main 04811a6

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014eD437mxrMnW2UwLZhpwc7

Upstream pull requests applied in this commit (their authors' work, brought onto this tree with a three-way merge; the description is in each PR):
- seedds#1459 by PromethOps (open): DM_UPSCALER=metalfx takes agl's agl_ndsr_vr.pck networks too, and a variant with no curve and no power is drawn as one colour space: Ocarina of Time's upscaler is MetalFX's
- seedds#1611 by Esteban-V (open): An argument block whose only change is a uniform buffer is patched instead of encoded again
- seedds#1774 by uncraftablexd (closed): Compute threads that exit before a barrier keep arriving at it, and every shader loop has a cap (GPU hang defences), chain change
- seedds#1824 by cocoman93 (open): Clamp texel fetches past a sampled 2D texture's edge: no hard edge in Ocarina of Time's mist
- seedds#1901 by FifteenPen (open): The game's compute dispatches with no game barrier between them run side by side (profile key dispatch_overlap, on for Ocarina of Time): Kokiri Forest GPU p50 15.63 → 14.76 ms
- seedds#1928 by FeReuss (open): Ocarina of Time: enc_thread on, the replay encodes on a thread of its own
- seedds#1929 by FeReuss (open): Unused border corrections are compiled out of pipelines that bind no sampler with a twin (function constant DMSH_BORDER_FC)
- seedds#1930 by FeReuss (open): A float add to storage memory with no result read is one native atomic add instead of a compare-and-swap loop
- seedds#1940 by uncraftablexd (open): A prewarm list's lines after its #@after_first_frame line are built after the first frame, in the background, instead of in the prepare step (issue seedds#1747)
- seedds#1942 by uncraftablexd (open): Frame step: late frames with the GPU idle do not step up, and a longer hold the GPU does not explain is probed
- seedds#1952 by AdyaGMD (open): Compute moves ahead of scene passes past occlusion queries and conditional rendering; clears wait across dispatches
- seedds#2008 by bquilty25 (open): nvn: reactive memory watchdog, 16-way view cache, and texture upload batching
- seedds#2020 by padsiwono (open): Ocarina of Time v131072: Output Native draws the full output size instead of the game's 80 % frame (`DM_OUTPUT`)
- seedds#2021 by padsiwono (open): Ocarina of Time v131072 renders at the launcher's Resolution (`DM_RENDER_SIZE`), docked
- seedds#2022 by sleepyeyedjoe (closed): Ocarina of Time: large models no longer go black for one frame twice, 4–5 frames apart, when the game streams new textures into them (a texture ID a shader has read keeps its read mark when it gets another texture)
- seedds#2030 by humanperzeus (open): A NaN colour at alpha 0 is returned as 0, as NVIDIA's blender takes a zero factor: Ocarina of Time's white clouds over the ferns are gone
- seedds#2035 by Know-It-All-Brother (open): The shader build runs as worker processes, and no shader waits for a batch (Ocarina of Time's cold Mac map: 3,002 s to 1,298 s)
- seedds#2065 by Raymon82777 (closed): A vertex library with layer entry points of its own gets bases for its main0 pairs: Ocarina of Time's 230 ms camera-turn hitch in Hyrule Field goes to 39 ms
- seedds#2075 by sleepyeyedjoe (closed): Ocarina of Time: a patch of scenery no longer draws dark for one frame when the game gives it a sharper texture (a pool texture the walk does not make holds a stand-in of its own colours until its first read)
- seedds#2077 by roandegraaf (closed): Ocarina of Time: fix the "plaid" terrain / missing texture tiles (GPU-drawn textures now reach the storage the game copies)
- seedds#2079 by uncraftablexd (open): Render pipelines that share a vertex function are taken from the archive or compiled one at a time: the AGX driver crashes in createVertexProgramVariant's completion block
- seedds#2083 by FifteenPen (closed): Ocarina of Time's prewarm list gets 41 more task / mesh pipelines from later play
Know-It-All-Brother added a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 10, 2026
… nvn_handle_expr.h), wide and atomic rewrites, loop_guard, the worker-process shader build (PR seedds#2035), iOS Metal compile changes, and the chain version these sources give (dmsh_chain.h)
seedds added a commit that referenced this pull request Nov 11, 2026
…build uses the cores it has: finishing a shader (the MSL rewrites, its reflection, its outputs and the cache) is Python, and the threads that did it share one GIL, so one core finished every shader while the Metal compiles they waited on left the others idle. Ocarina of Time's whole Mac map (0400486024DEC000, 42,848 shaders) on an M3 Max with 16 cores, J=16: 3,506 s with the build at 104 % CPU and 4 to 5 cores busy, 1,826 s with 16 workers; every shader restored from the cache 888 to 191 s (the PR author's runs). dmsh_build.py now finishes them in DMSH_PROCS worker processes (default J; 0 = threads, as before): forks of the build made once, after the cache check and before its first thread, so they have every table and fingerprint it read (spawned workers would run the script again, and its start clears the outputs), each with its own connection to cas.db (shader_cache.Store.forked). The build translates the batches itself and hands each shader's job to a worker; results and counts come back in batch order, so the summary, the progress file and every output are those of the threads. The next batch is handed out before this one is done (two at most), so no worker waits for a batch's slowest shader; paired vertex shaders still wait for every other shader. The workers, the batch overlap, the diagnosis and those measurements are PR #2255's. Added here: the precompiled Metal headers are made before the fork (each worker made its own, three compiles a target, in a folder nothing removed without the PR's WORK override, which is gone: Red Dead Redemption's build peaked at 1.4 GB with them, 0.8 GB now); a worker that dies stops the build with one [RED] line instead of a traceback (checked by killing one); the reference entry (A shader build's Python finishes one shader at a time), which also resolves the open note under Rewrites that try every character of a large source, Ocarina of Time's note, and the memory advice in docs/development.md. PR #2035 solved the same problem by running the build as J / 3 copies of itself (Ocarina of Time on an M5 Max 3,002 to 1,298 s; 1.41 times faster on an M3 Pro for 1.3 GB more, tobiashenn's run); side by side on one Mac it gained less (Red Dead Redemption: Mac 75 to 58 s, Mac and iOS 104 to 95 s against its own older main) and compiled about 9 % of the Metal sources twice, so it is not taken; its measurements are in the entry. Checked on an M6 (12 cores, 16 GB, J=12, empty cache, another game running beside it) against main f75ac06: Red Dead Redemption's 3,628 shaders, Mac 54 to 35 s, Mac and iOS 84 s to 53 and 63 s, restored from the cache 11 to 13 s (the headers are made though nothing compiles); 1,920 of Metroid Prime 4: Beyond's shaders 105 to 72 s, 89 s with DMSH_PROCS=6. Peak memory (summed resident size of the process tree, which counts the forks' shared pages once each): Red Dead Redemption 0.45 to 0.8 GB, the Metroid Prime 4 shaders 1.1 to 3.4 GB, 2.0 GB with 6 workers; a worker holds about 35 MB, the rest is J compilers running at once. Identical: Red Dead Redemption's Mac library (15,542 files) apart from the chain version and _b_cas.txt, and its iOS library (11,912 files); the author's 173,316 files of Ocarina of Time. Tests: test_shader_cache (forked workers share the file), test_shader_toolchain_cache, test_shader_pch, test_nsp_chain, test_docs. The chain version changes (dmsh_build.py), so every game's shaders are translated again once, their Metal compiles reused. Not run: regress.py check (the outputs are the same bytes), a Mac with 8 GB, a whole large map with iOS libraries, a large warm build here. lint_python.py lists one finding outside its baseline, an unused import in dmsh_entry.py, which this does not touch (PR #2255)

Co-authored-by: cashcon57 <24542086+cashcon57@users.noreply.github.com>
@seedds

seedds commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Thank you for this, and @tobiashenn for the M3 Pro run. The same problem is now solved on main by 8b55ebb (PR #2255: forked worker processes for the finish step), so I am closing this one without merging it.

Why that design: both were run on one Mac (M6, 12 cores, 16 GB, J=12, empty cache), each against the main it was written on. Red Dead Redemption's 3,628 shaders: this PR, Mac 75 to 58 s and Mac with iOS 104 to 95 s; #2255, 52 to 34 s and 82 to 55 s. Both gave byte-identical libraries. Here the copies also compiled about 9% of the Metal sources twice (2,349 compiles against 2,161), and running the script as copies of itself needs a guard on every startup step, where the forked pool leaves one startup and one cache check.

Your measurements are kept: Ocarina of Time's map on the M5 Max (3,002 to 1,298 s), the scheduling-only figure, and the M3 Pro's time and memory are in the reference entry, A shader build's Python finishes one shader at a time. If the largest-first chunk scheduling still gains on top of the workers on your Mac, a measurement of that alone would be welcome.

@seedds seedds closed this Nov 11, 2026
Kayyluhh added a commit to Kayyluhh/nxnative that referenced this pull request Nov 14, 2026
…no shader waits for a batch: Ocarina of Time's whole Mac map, built cold on an M5 Max with 18 cores, takes 1,298 s instead of 3,002

# Conflicts:
#	dm/gfx/nvn/dmsh_chain.h
Kayyluhh added a commit to Kayyluhh/nxnative that referenced this pull request Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation game: 0400486024DEC000 A game nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders tools Tools and scripts (dm/tools, scripts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants