Fix Metal/GL instanced-variant attribute location mismatch (particles/instances)#1776
Merged
bkaradzic-microsoft merged 2 commits intoJul 10, 2026
Conversation
…/instances) On Metal and OpenGL, particle systems and instanced meshes with a custom per-instance attribute buffer rendered collapsed geometry (invisible particles, missing instanced cubes). 54 Playground visualization tests were disabled on Metal via excludedGraphicsApis in BabylonJS#1772 as a result. Root cause: NativeEngine compiles a separate instanced shader variant (Program::GetOrCreateInstancedVariant) but binds vertex buffers using the base program's VertexAttributeLocations. The Metal/GL shader-compiler traverser assigned per-vertex attribute locations from a running count driven by its instance/non-instance 2-pass ordering, so a per-vertex attribute (e.g. a particle billboard's `offset`) got a different location in the variant than in the base program (verified: base `offset` -> a_tangent/loc2, variant -> a_position/loc0). The bound buffer therefore no longer matched the shader input, which read zero -> quad corners/instances collapsed to a point. D3D is unaffected because it maps attributes by fixed semantic. Fix: assign each varying's shader location from its stable ordinal position in the name-sorted varying map (new GetStableLocation helper), independent of the 2-pass ordering. This keeps per-vertex attribute locations identical between the base program and its instanced variant, so buffers bound at base locations reach the shader. The i_data* instance-name assignment is unchanged. No bgfx changes. Also re-enables the 54 now-passing tests on Metal (removes their excludedGraphicsApis/crash reason from config.json). The 3 remaining Metal exclusions are separate pixel-comparison issues (Convolution Post Process, two OpenPBR Fuzz tests). Verified on macOS/Metal: all 54 previously-excluded tests pass individually and render correctly (e.g. "Instances with color buffer" shows the expected red + green cubes); the full validation suite passes with them enabled (exit 0, no error diffs) with no regressions to the ~20 existing instancing tests. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a Metal/OpenGL rendering bug where instanced shader variants could receive mismatched vertex attribute locations relative to the base (non-instanced) program, causing particles/instances to render collapsed or invisible. The fix stabilizes attribute location assignment across base and instanced-variant compiles and re-enables previously Metal-excluded Playground visualization tests.
Changes:
- Introduces a “stable location” calculation for varyings in the ShaderCompiler traversers so base/variant compiles align on attribute locations.
- Updates OpenGL and Metal traversers to use the stable location when producing remapped attribute names/locations.
- Removes Metal exclusions (and associated reasons) from numerous Playground visualization tests in
config.json.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| Plugins/ShaderCompiler/Source/ShaderCompilerTraversers.cpp | Changes attribute-location assignment logic to be stable across base and instanced shader variants for Metal/GL paths. |
| Apps/Playground/Scripts/config.json | Re-enables Metal execution for many visualization tests by removing excludedGraphicsApis: ["Metal"]. |
Copilot review feedback: after switching to GetStableLocation(), the "more than 18 vertex attributes" guard still checked the running count while s_attribName[] was indexed by the (different) stable location, risking an out-of-bounds read before the guard fired, and GetStableLocation() was called twice per non-instance attribute. Compute the stable location once at the top of GetVaryingLocationAndNewNameForName (Metal and OpenGL traversers), validate that value against bgfx::Attrib::Count before any indexing, and reuse it in all return paths. Removes the now-dead running-count increment from these two functions (the member is still used by the D3D traverser). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CedricGuillemet
added a commit
to CedricGuillemet/BabylonNative
that referenced
this pull request
Jul 10, 2026
The Metal instanced-attribute crash that motivated disabling the macOS visualization job is fixed by BabylonJS#1776 (merged here), so re-enable the MacOS_VisualizationTests self-hosted job and set upload-visualization-artifact: true on the MacOS build job so the Playground.app artifact is published for it. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
CedricGuillemet
approved these changes
Jul 10, 2026
CedricGuillemet
added a commit
to CedricGuillemet/BabylonNative
that referenced
this pull request
Jul 15, 2026
Rebased onto master; squashes the runVTmacOS development history into a single change on top of the latest upstream. The now-upstream Metal/GL instanced-variant fix (BabylonJS#1776) and macOS test exclusions (BabylonJS#1772) are dropped as duplicates and this layers on upstream's pixel-diff diagnostics (BabylonJS#1775). CI (.github/workflows): - MacOS build publishes a Playground.app artifact; MacOS_VisualizationTests runs the pixel-diff suite on a self-hosted Metal GPU. - iOS_VisualizationTests_Build publishes an iphonesimulator app; iOS_VisualizationTests boots a simulator, verifies the bundle id, runs the suite, and collects crash reports. - Android_VisualizationTests_Build publishes an APK (babylon.js npm deps bundled into assets); Android_VisualizationTests installs, launches via Intent, polls logcat for the run summary, and always uploads logcat. - All standard upstream CI jobs (Win32/UWP/Ubuntu/Android builds, installation checks, QuickJS) are preserved. Runner (validation_native.js): - Optional host hooks: __validationContinueOnFailure (run whole suite, exit non-zero if any failed), __validationMinRenderCount (render-count floor), __validationWarmupFrames (throwaway warm-up frames), plus per-test PASS/FAIL logging. Hosts: - iOS: AppDelegate/ViewController/TestUtils_iOS collect results and exit code; Metal validation disabled at launch on the simulator. - Android: PlaygroundActivity Intent-driven script launch with continue-on-failure; BabylonView setFixedSurfaceSize for correct reference resolution; build.gradle bundles babylon.js + config.json + ReferenceImages into APK assets. - config.json: exclude OpenGL for the scissor-test family and GUI3D SpherePanel/LOD; one pixel-diff exclusion. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Metal (and OpenGL), particle systems and instanced meshes with a custom per-instance attribute buffer rendered collapsed geometry — particles shrank to invisible points, instanced cubes disappeared. 54 Playground visualization tests were disabled on Metal via
excludedGraphicsApisin #1772 because of this (they previously crashed with anMTLVertexDescriptorassertion; that crash was separately addressed in bgfx, after which they rendered blank/collapsed).Reference vs. result before this fix (
Instances with color buffer): two colored cubes → empty frame; particles → tiny dots at the emitter origin.Root cause
NativeEngine compiles a separate instanced shader variant (
Program::GetOrCreateInstancedVariant) but binds vertex buffers using the base program'sVertexAttributeLocations.The Metal/GL shader-compiler traverser assigned per-vertex attribute locations from a running count driven by its instance/non-instance 2-pass ordering. So a per-vertex attribute got a different location in the variant than in the base program. Verified with instrumentation on a particle draw:
The bound buffer no longer matched the shader input, so the per-vertex attribute (the billboard
offset) read zero → all quad corners/instances collapsed to a single point. D3D11 is unaffected because it maps attributes by fixed semantic, keeping base and variant consistent.Fix
Assign each varying's shader location from its stable ordinal position in the name-sorted varying map (new
GetStableLocationhelper), independent of the 2-pass ordering. This keeps per-vertex attribute locations identical between the base program and its instanced variant, so buffers bound at base-program locations reach the shader. Thei_data*instance-name assignment is unchanged.Re-enables 54 tests
Removes the
excludedGraphicsApis: ["Metal"]+ crash reason from the 54 now-passing tests inconfig.json. The 3 remaining Metal exclusions are unrelated pixel-comparison issues (Convolution Post Process; two OpenPBR Fuzz tests).Verification (macOS / Metal, Apple Silicon)
Instances with color buffernow shows the expected red + green cubes; particle billboards expand and are colored.54 crash→ (post bgfx stride fix)37 pass / 17 pixel-fail→54 pass.