Repository navigation
WebGPURenderer: Fix shader reuse for instanced meshes. - #34872
Conversation
📦 Bundle sizeFull ESM build including core, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
|
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). |
|
Added the same for 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. |
cac6f42 to
bcddef8
Compare
|
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:
In the same local project (WebGPU, storage matrices):
|
|
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 The fiddle shows it correctly since it is using prod but with this PR, you only see one skinned mesh. No logs or errors. |
|
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. |
|
Before merging, let me just run one more test regarding the declared name conflicts; I think I'll add the commits here. |
|
Merge when you feel the PR is ready! 👍 |
|
The buffers also passed name validation now, it should warning in the |
Related issue: -
Description
I noticed that separate
InstancedMeshobjects 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, sharedMeshBasicMaterial, two meshes with two instances each. Chrome 153, Linux, WebGPU, RX 9070 XT; tested againstdevat455178533.Related: #32609. This reproduction uses equal-count instanced meshes rather than skinned meshes.