Skip to content

WebGPURenderer: Fix shader reuse for instanced meshes. - #34872

Merged
sunag merged 6 commits into
mrdoob:devfrom
EdoardoLuciani:fix/wgsl-buffer-names-upstream
Oct 14, 2026
Merged

sunag merged 6 commits into
mrdoob:devfrom
EdoardoLuciani:fix/wgsl-buffer-names-upstream

Conversation

@EdoardoLuciani

@EdoardoLuciani EdoardoLuciani commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Related issue: -

Description

I noticed that separate InstancedMesh objects can generate duplicate WebGPU shaders even when they share geometry, material and instance count.

Root cause: Unnamed buffer identifiers contain global node IDs, so otherwise equivalent WGSL misses the exact-source shader cache.

Fix: Use the builder-allocated uniform name instead. Explicit names, per-object bindings and node-builder cache keys stay unchanged.

Pipeline reuse

Stock WebGPURenderer, shared MeshBasicMaterial, two meshes with two instances each. Chrome 153, Linux, WebGPU, RX 9070 XT; tested against dev at 455178533.

Metric Before After
Shader builders 2 2
Cached vertex programs 2 1
Cached render pipelines 2 1

Related: #32609. This reproduction uses equal-count instanced meshes rather than skinned meshes.

@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

📦 Bundle size

Full ESM build including core, minified and gzipped.

Before After Diff
Core 390.84
102.52
390.84
102.52
+0 B
+0 B
WebGL 777.27
194.10
777.27
194.10
+0 B
+0 B
WebGPU 1097.46
295.55
1097.71
295.57
+251 B
+25 B
WebGPU Nodes 1095.40
295.20
1095.66
295.22
+251 B
+23 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 518.46
124.59
518.46
124.59
+0 B
+0 B
WebGPU 775.74
207.70
775.99
207.74
+251 B
+39 B
WebGPU Nodes 722.86
194.46
723.11
194.50
+251 B
+41 B

@EdoardoLuciani
EdoardoLuciani marked this pull request as ready for review October 7, 2026 19:45
@mrdoob mrdoob added this to the r187 milestone Oct 8, 2026
@mrdoob

mrdoob commented Oct 8, 2026

Copy link
Copy Markdown
Owner

Hey Claude, how does this PR relate to #34819?


Bottom line: #34819 makes #34872 unnecessary for standard InstancedMeshes (and is faster since it skips rebuilding nodes and works across different instance counts), but #34872 is still worth merging alongside it because it fixes WGSL shader reuse for all the cases #34819 doesn't share (instanceColor, storage buffers, morph targets, velocity, or custom buffer()/storage() nodes).

@mrdoob
mrdoob requested a review from sunag October 9, 2026 02:22
@mrdoob

mrdoob commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Added the same for GLSLNodeBuilder in bcddef8, and merged dev since #34857 changed the same lines. GLSLNodeBuilder no longer sets node.name, so WebGLNodesHandler names the buffer groups after the uniform.

In a local project, pipelines go from 485 to 155 with WebGPU and from 342 to 310 with the WebGL fallback. The WebGL gain is smaller because its uniform buffers bake the instance count into the shader.

@mrdoob
mrdoob force-pushed the fix/wgsl-buffer-names-upstream branch from cac6f42 to bcddef8 Compare October 14, 2026 11:36
@mrdoob
mrdoob requested review from Mugen87 and removed request for sunag October 14, 2026 11:40
@mrdoob

mrdoob commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

This PR is useful regardless of #35007. #35007 shares node builds between instanced meshes, while this PR lets separate builds share shaders when they only differ in buffer names. That still happens with:

  • Buffers created per material, like uniformArray(), buffer() or storage() nodes. For example, instanced meshes with storage matrices and different materials.
  • Builds that remain per object: objects with count > 1, instanced morph targets, and instanced meshes that fall back to uniform buffers.
  • Compute shaders with their own storage buffers.

In the same local project (WebGPU, storage matrices):

Node builds Pipelines
dev 512 485
This PR 512 155
#35007 215 203
This PR + #35007 215 155

@mrdoob mrdoob changed the title WGSLNodeBuilder: Fix shader reuse for instanced meshes WebGPURenderer: Fix shader reuse for instanced meshes. Oct 14, 2026
@Mugen87

Mugen87 commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

I did a review on the PR and found no regression.

The only thing that came up is an existing issue that is now more visible with this PR: When two kernels produce identical GLSL in the WebGL backend, the second one reads and writes the first one's storage buffers. This was hidden in the past because the ID of UBOs kept the shaders apart. But with this PR, this side effect is gone.

This can be reproduced with a modified version of webgpu_skinning_points. I've just added a second skinned mesh: https://jsfiddle.net/ntxc7ypu/1/

The fiddle shows it correctly since it is using prod but with this PR, you only see one skinned mesh. No logs or errors.

@Mugen87

Mugen87 commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

BTW: The fix for GLSL issue is to build the WebGL compute pipeline from each kernel's own transforms and attributes, not the shared program. I can file a PR for this once this is merged. In this way, I can easier verify the fix.

@sunag

sunag commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Before merging, let me just run one more test regarding the declared name conflicts; I think I'll add the commits here.

@Mugen87

Mugen87 commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

Merge when you feel the PR is ready! 👍

@sunag

sunag commented Oct 14, 2026

Copy link
Copy Markdown
Collaborator

The buffers also passed name validation now, it should warning in the NodeBuilder instead of failing during WGSL compilation.

// 3. duplicate names -> expected: colors, colors_1
const a = uniformArray( [ new THREE.Color( 1, 0, 0 ) ], 'color' ).setName( 'colors' );
const b = uniformArray( [ new THREE.Color( 0, 1, 0 ) ], 'color' ).setName( 'colors' );
material.colorNode = a.element( 0 ).add( b.element( 0 ) );

// 4. buffer vs var -> expected: tint (var), tint_1 (buffer)
const c = uniformArray( [ new THREE.Color( 1, 0, 0 ) ], 'color' ).setName( 'tint' );
material.colorNode = vec3( 0, 1, 0 ).toVar( 'tint' ).add( c.element( 0 ) );
3. duplicate names
var<uniform> colors : colorsStruct;
var<uniform> colors : colorsStruct;     ← error: redeclaration of 'colors'

4. name vs var
var<uniform> tint : tintStruct;
var<private> tint : vec3<f32>;          ← error: redeclaration of 'tint'

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.

4 participants