Skip to content

Shader libraries compile with -fwrapv: Ocarina of Time's mesh-shader models no longer take their ambient light from the wrong probe texels - #1844

Closed
nawangwe wants to merge 4 commits into
seedds:mainfrom
nawangwe:fwrapv-upstream

Conversation

@nawangwe

@nawangwe nawangwe commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Ocarina of Time (0400486024DEC000 v131072): one section of a dark Deku Tree wall is lit brightly (Gohma's room), and a pale patch sits on the ground at the Kokiri Forest exit; both shimmer while the camera moves and settle when it stops, and both are models drawn with task / mesh shaders while the vertex-shader models beside them are lit right.

Before (stock shaders):
oot-gohma-wall-before

The mesh shaders decode packed 16-bit vertex data by hand. Mesh shader 35785cddc971290c (Model.Product.110; task a5288d0a6c4971fd, pixel f4f7e6955d37f40c) extracts the low half of a word for the lightmap / light-probe UV with an IMAD by 65536 and a shift right by 16, translated as as_type<int>(w) * 65536 then uint(...) >> 16. Maxwell and Ampere wrap on overflow, as SPIR-V defines; in MSL signed overflow is undefined, and Metal's front end folds (x * 65536) >> 16 to x, so the whole word becomes the mantissa and the UV's x comes out 65536x too large (in-shader 13184 / 18432 / 31744 where y, from the high half, gives 0.20 / 0.28 / 0.48). The probe texture is sampled at the wrong texels and the pixel shader's spherical-harmonics ambient term reads about 8 instead of 0.001. 64 of the game's 42,848 translated shaders use the idiom.

The change: dmsh_build.py (Mac) and ios_metal.py (iOS) pass -fwrapv to every compile, so signed integer overflow wraps as on the hardware; test_shader_pch.py mirrors the command; the chain version changes. Reference entry: docs/reference/shaders.md, "Signed integer overflow in translated shaders".

After (mesh shader rebuilt with -fwrapv):
oot-gohma-wall-after

Checked

  • The extraction's shl i32 …, 16 is gone from the IR of 35785cddc971290c without -fwrapv (xcrun metal -O1 -fpreserve-invariance -fno-fast-math -S -emit-llvm) and present with it.
  • The stock mesh shader recompiled with -fwrapv and loaded as a loose library beside the shader image renders the wall uniformly dark, everything else unchanged (the pale Kokiri patch is the same term on another prop; not yet revisited).
  • Games run: Ocarina of Time 0400486024DEC000 v131072, the Deku Tree (Gohma's room) and Kokiri Forest, compared by raw reads of the HDR colour target through a diagnostic copy of the pixel shader.
  • Devices and OS: M4 Pro (24 GB), macOS 27.2, Metal 32023.921.
  • Not checked: the regression run (scripts/regress.py check --games); a full chain rebuild with the flag is in progress on the reporting Mac and other games were not rebuilt. vk_shaders.py / gl_shaders.py compile without the flag and may want it too (left out to keep the PR to one chain).

Docs

  • The finding is in its docs/reference/shaders.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • A chain tool changed: dmsh_chain.py --header was run and dm/gfx/nvn/dmsh_chain.h is part of the change
  • No game files, keys or data extracted from a game in the change or the attachments

@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) ios The iPhone and iPad app (dm/ios) tools Tools and scripts (dm/tools, scripts) docs Documentation shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders labels Nov 8, 2026
…models no longer take their ambient light from the wrong probe texels (the bright band on the Deku Tree's walls, the pale patch at the Kokiri Forest exit)

Symptom: in Ocarina of Time (0400486024DEC000 v131072) one section of a dark Deku Tree wall is lit brightly, and a pale patch sits on the ground at the Kokiri Forest exit; both shimmer while the camera moves and settle when it stops, and both are models drawn with task / mesh shaders while the vertex-shader models beside them are lit right.

Cause: the mesh shaders decode packed 16-bit vertex data by hand. Mesh shader 35785cddc971290c (Model.Product.110; task a5288d0a6c4971fd, pixel f4f7e6955d37f40c) extracts the low half of a word for the lightmap / light-probe UV with an IMAD by 65536 and a shift right by 16, translated as as_type<int>(w) * 65536 then uint(...) >> 16. Maxwell and Ampere wrap on overflow, as SPIR-V defines; in MSL signed overflow is undefined, and Metal's front end folded (x * 65536) >> 16 to x, so the whole word was the mantissa and the UV's x came out 65536x too large (in-shader 13184 / 18432 / 31744 where y, taken from the high half, gave 0.20 / 0.28 / 0.48). The probe texture was sampled at the wrong texels and the pixel shader's spherical-harmonics ambient term read about 8 instead of 0.001. 64 of the game's 42,848 translated shaders use the idiom.

Fix: dmsh_build.py (Mac) and ios_metal.py (iOS) pass -fwrapv to every compile, so signed integer overflow wraps as on the hardware. test_shader_pch.py mirrors the command. The chain version changes (dmsh_chain.py --header), so every game's shaders rebuild. docs/reference/shaders.md gets the entry and the symptom index is regenerated.

Checked: the extraction's shl is gone from the IR of 35785cddc971290c without -fwrapv (xcrun metal -O1 -fpreserve-invariance -fno-fast-math -S -emit-llvm) and present with it; the stock mesh shader recompiled with -fwrapv and loaded as a loose library beside the image renders the wall uniformly dark (M4 Pro, macOS 27.2, Metal 32023.921). A full chain rebuild with the flag is in progress on this Mac; the regression run was not done.
@nawangwe

nawangwe commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

The "ruff and pyright (ratchet)" failure is main's own, not this change: the one new finding, Operator "<" not supported for types "int" and "Unknown | None" (337 findings, 336 in the baseline, 1 new), fails identically on main's last lint runs (7de11da, 50aefc1, 8fd4b72; 37f076a passed). The three Python files this PR touches (dmsh_build.py, ios_metal.py, test_shader_pch.py) only change the compile command list and a comment.

@nawangwe

nawangwe commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Either order works for me. This PR's logic is three one-line flag additions (dmsh_build.py, ios_metal.py, test_shader_pch.py) plus the chain header, so if #1846 or either of the others lands first I'll rebase and rerun dmsh_chain.py --header the same day; if this one goes first the rebase on your side is the header alone. One thing worth knowing for #1833: its test reads MAC_METAL out of dmsh_build.py rather than copying the list, so it picks up -fwrapv without a change. I'll leave the order to @seedds.

@nawangwe

nawangwe commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

"Tool tests" fails on two things this PR does not touch: test_env_switches finds docs/env_switches.tsv out of step with the code (DM_NVN_CUT_FIRST, DM_NVN_RT_RECLAIM, DM_NVN_VC_WAYS quoted but read nowhere; DM_NVN_BC_ALIAS, DM_YIELD_CLOCK without a meaning), and test_xcode_workflow errors on a missing RomFS.img in its temporary extract. The PR changes dmsh_build.py, ios_metal.py, test_shader_pch.py, the chain header and two docs pages only; main's last runs of the workflow were cancelled before this job, so I could not point at a green or red run of main itself. Left alone to keep the PR to one change.

…generated with dmsh_chain.py --header (203d485b6616) over both sides' chain changes
…generated with dmsh_chain.py --header (b074a7cccc40) over both sides' chain changes
seedds added a commit that referenced this pull request Nov 9, 2026
…r models no longer take their ambient light from the wrong probe texels (the bright band on a dark wall in Gohma's room)

SPIR-V, GLSL and the Switch's GPUs wrap on signed overflow; spirv-cross writes signed arithmetic as plain MSL int, where overflow is undefined, and Metal's front end folds around it. Ocarina's mesh shader 35785cddc971290c sign-extends a packed 16-bit lightmap / probe coordinate as a = w * 65536, (a >> 31) << 16 | uint(a) >> 16; the compiler took the sign bits from bits 15-30 of w instead of bit 15, so the UV's x came out far too large and the spherical-harmonics ambient term read about 8 instead of 0.001.

Every Metal compile of translated shaders passes -fwrapv: dmsh_build.py (Mac), ios_metal.py (iOS), vk_shaders.py and gl_shaders.py. The flag also drops inbounds from pointer arithmetic (Metal's clang has no -fwrapv-pointer) and keeps loop bounds that may wrap; on 1,000 of Ocarina's shaders 242 compile to otherwise different code, but none of it shows on the GPU: kokiri-deck on an M6, both sides from the same main with their own libraries and warmed archives, three runs a side, GPU p50 / p95 9.94 / 10.51 ms and 59.4 fps without, 9.91 / 10.43 ms and 59.6 fps with. All 39,125 shaders compile, 0 failed.

test_dmsh_wrapv.py runs the sign extension, an overflow test and a doubling and halving on the Mac's GPU over 65,536 inputs with dmsh_build.py's command, checks the iPhone command keeps the shift, and that all four tools pass the flag; without the flag the GPU and iPhone checks fail.

Found and diagnosed by nawangwe (PR #1844).

Regression: Ocarina of Time (the game it was found on; the A/B above).

Co-authored-by: Nawangwe <4419194+nawangwe@users.noreply.github.com>
@seedds

seedds commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Thanks, this was a real miscompile and a very good find. Landed as e4e5ff6, with you as co-author.

What I checked and added:

  • Reproduced the fold on main's chain. Mesh shader 35785cddc971290c sign-extends a packed 16-bit field as a = w * 65536; (a >> 31) << 16 | uint(a) >> 16. Without -fwrapv, Metal's front end takes the sign bits from bits 15-30 of w instead of bit 15. Older chains didn't write that sign extension, which is why it showed up only recently.
  • All four compile paths now pass the flag: dmsh_build.py, ios_metal.py, vk_shaders.py and gl_shaders.py, so Vulkan and GL games get it too.
  • A test, dm/gfx/tests/test_dmsh_wrapv.py. It runs that sign extension, an overflow test and a doubling and halving on the Mac's GPU over 65,536 inputs, checks that the iPhone command keeps the shift, and checks that every tool passes the flag. Without the flag, the GPU check and the iPhone check both fail.
  • Cost. The flag also drops inbounds from pointer arithmetic, and on 1,000 sampled shaders 242 compile to different code, mostly loop bounds. None of it shows on the GPU. I ran kokiri-deck on an M6 with both sides built from the same main, each with its own library and warmed archive, three runs a side. GPU p50 / p95 was 9.94 / 10.51 ms without the flag and 9.91 / 10.43 ms with it; fps was 59.4 vs 59.6.
  • Docs: the reference entry is "Signed integer overflow in translated shaders" in docs/reference/shaders.md, and Ocarina's note links it.

Not rechecked: the pale patch at the Kokiri Forest exit. If you still see it on main, please open an issue.

@seedds seedds closed this Nov 9, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
…r models no longer take their ambient light from the wrong probe texels (the bright band on a dark wall in Gohma's room)

SPIR-V, GLSL and the Switch's GPUs wrap on signed overflow; spirv-cross writes signed arithmetic as plain MSL int, where overflow is undefined, and Metal's front end folds around it. Ocarina's mesh shader 35785cddc971290c sign-extends a packed 16-bit lightmap / probe coordinate as a = w * 65536, (a >> 31) << 16 | uint(a) >> 16; the compiler took the sign bits from bits 15-30 of w instead of bit 15, so the UV's x came out far too large and the spherical-harmonics ambient term read about 8 instead of 0.001.

Every Metal compile of translated shaders passes -fwrapv: dmsh_build.py (Mac), ios_metal.py (iOS), vk_shaders.py and gl_shaders.py. The flag also drops inbounds from pointer arithmetic (Metal's clang has no -fwrapv-pointer) and keeps loop bounds that may wrap; on 1,000 of Ocarina's shaders 242 compile to otherwise different code, but none of it shows on the GPU: kokiri-deck on an M6, both sides from the same main with their own libraries and warmed archives, three runs a side, GPU p50 / p95 9.94 / 10.51 ms and 59.4 fps without, 9.91 / 10.43 ms and 59.6 fps with. All 39,125 shaders compile, 0 failed.

test_dmsh_wrapv.py runs the sign extension, an overflow test and a doubling and halving on the Mac's GPU over 65,536 inputs with dmsh_build.py's command, checks the iPhone command keeps the shift, and that all four tools pass the flag; without the flag the GPU and iPhone checks fail.

Found and diagnosed by nawangwe (PR seedds#1844).

Regression: Ocarina of Time (the game it was found on; the A/B above).

Co-authored-by: Nawangwe <4419194+nawangwe@users.noreply.github.com>
Fasen24-AI added a commit to Fasen24-AI/swordboiMacNative that referenced this pull request Nov 10, 2026
…eedds#1825 (process-local miss cache for base-pipeline lookups), seedds#1368 (native mesh pipelines in the prewarm manifest, 73 M rows), seedds#1842 (DM_NVN_ISOLATE_TASK)

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#1368 by nanotchec (closed): Ocarina of Time: prewarm native mesh pipelines before attacks and enemies appear
- seedds#1825 by socram03 (open): Avoid repeated archive scans for missing pipeline bases
- seedds#1842 by ycarogabriel-lgtm (open): Ocarina of Time: opt-in task isolation (DM_NVN_ISOLATE_TASK), the Hi-Z task a5288 in command buffers of its own, a workaround for its GPU hangs on an M1
- seedds#1844 by nawangwe (closed): Shader libraries compile with -fwrapv: Ocarina of Time's mesh-shader models no longer take their ambient light from the wrong probe texels
Fasen24-AI added a commit to Fasen24-AI/swordboiMacNative that referenced this pull request Nov 10, 2026
…ion by size tier and archive indexes, seedds#1844 merged, 3D texture mirrors, native mesh pipelines in the archive, follower-scan groups)

Hand-resolved: main's compaction and mesh-pipeline archive replace the tail-compaction marker and the mesh-parts draw we carried; the copy mirrors keep our `mir` flag for pkg_copy_absorb; patch_nso.py keeps fixed_resolution beside main's fs_address; the translator keeps the seedds#1585 site argument. Translator rebuilt: chain 1268cc9dae8a.

Co-Authored-By: Claude Fable 5.1 <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#1585 by nafields (open): Raw global memory through a pointer read when the draw is bound: the host fills each fixed-offset pointer's pool range before the draw (chain 1ffcb6b77740)
- seedds#1844 by nawangwe (closed): Shader libraries compile with -fwrapv: Ocarina of Time's mesh-shader models no longer take their ambient light from the wrong probe texels
- seedds#1879 by bettterlabs (closed): Timer flushes wait during any prewarm, so the start after a cold start does not compact (Ocarina of Time 762 s → 3.3 s)
- seedds#1880 by bettterlabs (closed): Pipeline archive compaction by size tier, so the start after play recaptures what play added, not the whole archive (Ocarina of Time 713 s → 24 s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation ios The iPhone and iPad app (dm/ios) 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.

2 participants