Repository navigation
Conversation
…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.
cb225dd to
377a044
Compare
|
The "ruff and pyright (ratchet)" failure is main's own, not this change: the one new finding, |
|
Either order works for me. This PR's logic is three one-line flag additions ( |
|
"Tool tests" fails on two things this PR does not touch: |
…generated with dmsh_chain.py --header (203d485b6616) over both sides' chain changes
…generated with dmsh_chain.py --header (b074a7cccc40) over both sides' chain changes
…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>
|
Thanks, this was a real miscompile and a very good find. Landed as e4e5ff6, with you as co-author. What I checked and added:
Not rechecked: the pale patch at the Kokiri Forest exit. If you still see it on main, please open an issue. |
…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>
…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
…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)
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):

The mesh shaders decode packed 16-bit vertex data by hand. Mesh shader
35785cddc971290c(Model.Product.110; taska5288d0a6c4971fd, pixelf4f7e6955d37f40c) 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 asas_type<int>(w) * 65536thenuint(...) >> 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) >> 16tox, 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) andios_metal.py(iOS) pass-fwrapvto every compile, so signed integer overflow wraps as on the hardware;test_shader_pch.pymirrors the command; the chain version changes. Reference entry: docs/reference/shaders.md, "Signed integer overflow in translated shaders".After (mesh shader rebuilt with -fwrapv):

Checked
shl i32 …, 16is gone from the IR of35785cddc971290cwithout-fwrapv(xcrun metal -O1 -fpreserve-invariance -fno-fast-math -S -emit-llvm) and present with it.-fwrapvand 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).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.pycompile without the flag and may want it too (left out to keep the PR to one chain).Docs
docs/reference/shaders.mdentry, andpython3 dm/tools/docs_symptoms.pywas run (the symptom index)dmsh_chain.py --headerwas run anddm/gfx/nvn/dmsh_chain.his part of the change