Skip to content

ShaderCompiler: mark the Metal vertex position invariant - #2070

Merged
bkaradzic-microsoft merged 2 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:fix/metal-invariant-position
Nov 18, 2026
Merged

bkaradzic-microsoft merged 2 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:fix/metal-invariant-position

Conversation

@bkaradzic-microsoft

Copy link
Copy Markdown
Member

Summary

Babylon.js draws the same geometry with different programs across passes (depth prepass, geometry renderer, main pass) and depth-tests them against each other. Metal's fast-math can compile identical position math differently in each pipeline, which causes z-fighting.

The Metal vertex position output is now marked invariant ([[position, invariant]], MSL 2.1). This fixes FrameGraph nrge csm shadows with autoCalcDepthBounds.

Verification

Ran the Playground validation suite on macOS with Metal and JavaScriptCore, with the Metal API validation layer enabled. The full suite passes with no regressions.

@bkaradzic-microsoft bkaradzic-microsoft added bug Something isn't working rendering labels Nov 10, 2026
@bkaradzic-microsoft
bkaradzic-microsoft requested review from bghgary and a balanced review from Copilot November 10, 2026 17:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

1 open issue
What changed in this PR

Marks the Metal vertex position output as invariant to prevent cross-pipeline z-fighting caused by Metal fast-math producing slightly different position results across passes.

Changes:

  • Detects vertex stage and decorates the BuiltInPosition output with Invariant.
  • Forces MSL version 2.1 to ensure [[position, invariant]] emission.
File Description
Plugins/​ShaderCompiler/​Source/​ShaderCompilerMetal.cpp Adds invariant decoration for the Metal vertex position output and bumps MSL version to 2.1 for vertex shaders.

Review effort: Lite

@bghgary bghgary left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Reviewed by Copilot on behalf of @bghgary]

Concerns inline.

@bkaradzic-microsoft
bkaradzic-microsoft force-pushed the fix/metal-invariant-position branch from 8c25775 to 7fb2729 Compare November 18, 2026 00:33
@bkaradzic-microsoft

Copy link
Copy Markdown
Member Author

CI note: run 44208782078 (attempt 1) failed only in Android_Ubuntu_JSC / build. This is infrastructure, not this PR. In "Build Playground JavaScriptCore", Gradle couldn't resolve its classpath because Maven Central returned HTTP 429 Too Many Requests for kotlin-stdlib:2.2.10, httpmime:4.5.6, commons-io:2.16.1 and others. That's a rate limit, likely from several PRs starting CI at the same time. It failed after 3 min, before any BN code compiled. The other 27 jobs passed, including Android_Ubuntu_V8/QuickJS. AndroidTests was skipped because it depends on the failed job.

Job Queue Run
Android_Ubuntu_JSC / build ❌ 3s 3m19s
Ubuntu_Clang_Vulkan / build 61m19s 10m52s
iOSTests / test 55m32s (after iOS_Xcode26 build) 7m00s
MacOS_Installation / test-install 24m58s 6m02s
Win32_x64_D3D11 / build 24m10s 5m31s
Android_Ubuntu_V8 / build 15m42s 7m19s
Win32_x86_D3D11 / build 11m22s 6m08s
Win32_x64_V8_D3D11 / build 8m33s 31m56s
Win32_x64_D3D12 / build 4m44s 3m45s
MacOS / build 3s 11m59s
iOS_Xcode26 / build 4s 10m01s
Ubuntu_Clang_JSC / build 3s 27m51s
Other jobs (UWP ×6, Win32 JSI/QuickJS/Precompiled, Linux/iOS/Win32 Installation, Ubuntu GCC/QuickJS, MacOS Ninja/QuickJS, Android QuickJS) ≤38s 3m33s–10m41s

Total wall-clock: 1h12m38s (00:33:25Z → 01:46:03Z). The critical path was queue time: the Vulkan and iOSTests runners started about an hour late.

ci-complete is the only job still queued, so I'm cancelling the run and rerunning the failed jobs (gh run rerun 44208782078 --failed).

Branimir Karadzic and others added 2 commits November 18, 2026 13:22
Babylon.js draws the same geometry with different programs across passes
(depth prepass, geometry renderer, main pass) and depth tests them
against each other. Metal's fast-math may compile identical position
math differently per pipeline, causing z-fighting. Decorate the position
output Invariant ([[position, invariant]], MSL 2.1). Fixes 'FrameGraph
nrge csm shadows with autoCalcDepthBounds'.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 5a2ca4f4-15a2-4ad1-8a64-9dad061e773d
Invalidate caches produced before Metal vertex positions were marked invariant, and verify the serialized cache header version.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 8bd57bfd-bbb3-4e86-8a17-9a44fb995d0f
@bkaradzic-microsoft
bkaradzic-microsoft force-pushed the fix/metal-invariant-position branch from 7fb2729 to b8a20d3 Compare November 18, 2026 21:25

@bghgary bghgary left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Reviewed by Copilot on behalf of @bghgary]

LGTM.

@bkaradzic-microsoft
bkaradzic-microsoft enabled auto-merge (squash) November 18, 2026 22:04
@bkaradzic-microsoft
bkaradzic-microsoft merged commit 10f734b into BabylonJS:master Nov 18, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working rendering

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants