Skip to content

gfx/tools: pass subgroup inputs by value to out-of-line mesh helpers - #3169

Closed
AlexandreBeauduin wants to merge 1 commit into
seedds:mainfrom
AlexandreBeauduin:gfx/subgroup-byval
Closed

AlexandreBeauduin wants to merge 1 commit into
seedds:mainfrom
AlexandreBeauduin:gfx/subgroup-byval

Conversation

@AlexandreBeauduin

Copy link
Copy Markdown
Contributor

Problem

On an M4 Pro, Ocarina of Time hung (kIOGPUCommandBufferCallbackErrorHang), page-faulted and lost GPU access (GPU access revoked) shortly after a save loaded.

The noinline workaround for the mesh SIGBUS (issue #2825) keeps SPIRV-Cross's subgroup helpers out of line, and SPIRV-Cross hands them thread uint& gl_SubgroupInvocationID. When one such helper passes that reference on to another (the game's exclusive scan and its shuffle step), Apple's Metal compiler reads the builtin as one value for the whole SIMD group (-O1 and up, minimums 26.6 and 27.0). Every lane got the same exclusive prefix sum, so mesh groups wrote outside their output ranges.

Fix

mesh_subgroup_noinline now also passes the gl_Subgroup* parameters of final mesh code by value. They are SPIR-V Input builtins and never written, so a copy has the same meaning. The helpers stay noinline, which still avoids the SIGBUS. This works around a defect in Apple's compiler, which remains open; it is documented as such in docs/reference/shaders.md and in the game note.

Validation

  • test_scan_on_gpu runs the game's real scan helpers (mesh a76e46990e6799ba) on the GPU: 62 of 64 lanes wrong in the previous noinline-by-reference form, 0 of 64 inlined or by value.
  • All 160 mesh prewarm descriptors compile offline for g13s, g13d, g14g and g14s at 26.6 and for g18g at 27.0.
  • Full Mac rebuild of Ocarina of Time: 42,847 shaders, 44 mesh libraries changed, none left with a by-reference subgroup parameter.
  • In game on an M4 Pro, from a manual save into the Ice Cavern: 0 hangs, 0 page faults, 0 revocations, 0 out-of-range mesh groups (probe), reaching frame 7,608. Before the change, runs faulted after the load.
  • The affected M1 and M2 Macs were not retested at run time.

These were run on the chain before rebasing onto current main (d51675c8a1eb); the rebase only regenerated dmsh_chain.h, and test_dmsh_raw_noinline.py, test_docs.py and test_nsp_chain.py pass on the rebased branch.

Regression report

The rewrite only changes final native mesh stages whose helpers take gl_Subgroup* parameters. Of the local games, only Ocarina of Time and Mario Kart World ship native mesh shaders; none of Mario Kart World's mesh sources has such a helper (19 of its 26 mesh keys generated and checked). Donkey Kong Bananza, Mario Tennis and Star Fox have no native mesh stages. A full scripts/regress.py check --games was not run: the machine lacked the disk space for its two chains per game.

🤖 Generated with Claude Code

Apple's Metal compiler reads gl_SubgroupInvocationID as one value for the
whole SIMD group when a noinline helper passes it by reference to another
noinline helper (-O1 and up, minimums 26.6 and 27.0). Ocarina of Time's scan
helpers then gave every lane the same prefix sum; its mesh groups wrote
outside their ranges and the GPU hung or faulted after a save loaded.
mesh_subgroup_noinline now passes the gl_Subgroup* parameters by value.
They are SPIR-V Input builtins, never written. The helpers stay noinline to
avoid the SIGBUS of issue seedds#2825.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders game: 0400486024DEC000 A game labels Nov 21, 2026
seedds added a commit that referenced this pull request Nov 22, 2026
Apple's Metal compiler can miscompile nested out-of-line subgroup
helpers when one passes a subgroup input by reference to another.
Ocarina of Time then writes outside its mesh output ranges and can
hang the GPU after a save loads.

Pass read-only subgroup inputs by value in final native mesh sources.
Keep the helpers out of line to avoid the earlier mesh compiler crash.
Cover the rewrite with synthetic helpers and keep the exact GPU
reproducer with the local game files.

Both chains built the 500 native mesh shaders and 1,000 companions.
The 44 changed Metal sources match the rewrite and reflection matches
apart from the chain stamp. The reported pipeline compiles offline
on g13d, g14g and g14s at minimum macOS 26.6. Focused tests pass.
Affected M1 and M2 Macs still need runtime retests.

Refs #3152

Co-authored-by: Alexandre Beauduin <6662875+AlexandreBeauduin@users.noreply.github.com>
Co-authored-by: coderipper <34905663+yripper@users.noreply.github.com>
@seedds

seedds commented Nov 22, 2026

Copy link
Copy Markdown
Owner

Thanks, Alexandre, for tracing the Metal compiler failure and testing the by-value fix. The revised patch landed on main in 6dc2763. It keeps the mesh helpers out of line and passes their read-only subgroup inputs by value. The repository test now uses synthetic shader code, and the exact GPU reproducer is kept with the local game files.

The focused tests passed. Both versions built all 500 Ocarina of Time mesh shaders and their 1,000 companion shaders. The 44 expected shader changes preserved reflection, and the previously failing pipeline compiled offline for the older GPU targets.

The affected M1 and M2 Macs still need runtime retests. Issue #3152 stays open for those results.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants