Skip to content

Fix Metal/GL instanced-variant attribute location mismatch (particles/instances)#1776

Merged
bkaradzic-microsoft merged 2 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:fix/metal-instanced-attribute-locations
Jul 10, 2026
Merged

Fix Metal/GL instanced-variant attribute location mismatch (particles/instances)#1776
bkaradzic-microsoft merged 2 commits into
BabylonJS:masterfrom
bkaradzic-microsoft:fix/metal-instanced-attribute-locations

Conversation

@bkaradzic-microsoft

Copy link
Copy Markdown
Member

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 excludedGraphicsApis in #1772 because of this (they previously crashed with an MTLVertexDescriptor assertion; 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'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 got a different location in the variant than in the base program. Verified with instrumentation on a particle draw:

base    (0 instanced attrs): offset -> a_tangent,  loc=2   <- vertex buffer bound here
variant (4 instanced attrs): offset -> a_position, loc=0   <- shader reads here

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 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-program locations reach the shader. The i_data* instance-name assignment is unchanged.

  • No bgfx changes.
  • Base-program / non-instanced compiles are unchanged (their locations already equalled the stable ordinal).
  • Applied to both the Metal and OpenGL traversers.

Re-enables 54 tests

Removes the excludedGraphicsApis: ["Metal"] + crash reason from the 54 now-passing tests in config.json. The 3 remaining Metal exclusions are unrelated pixel-comparison issues (Convolution Post Process; two OpenPBR Fuzz tests).

Verification (macOS / Metal, Apple Silicon)

  • All 54 previously-excluded tests pass individually and render correctly — e.g. Instances with color buffer now shows the expected red + green cubes; particle billboards expand and are colored.
  • Full validation suite passes with the 54 enabled (exit 0, 0 error diffs), no regressions to the ~20 existing instancing tests (Thin Instances, motion-blur, shadows-with-instances, etc.).
  • Before → after on the 54: 54 crash → (post bgfx stride fix) 37 pass / 17 pixel-fail54 pass.

…/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>
Copilot AI review requested due to automatic review settings July 9, 2026 17:42

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.

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"].

Comment thread Plugins/ShaderCompiler/Source/ShaderCompilerTraversers.cpp Outdated
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>

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

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>
@bkaradzic-microsoft
bkaradzic-microsoft merged commit aa7cf01 into BabylonJS:master Jul 10, 2026
29 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants