WebGLRenderer: Save/restore textureUnits on render state stack#33217
WebGLRenderer: Save/restore textureUnits on render state stack#33217Mugen87 merged 2 commits intomrdoob:devfrom
Conversation
Related issue: mrdoob#33207 Fixes nested render calls (e.g. PMREMGenerator inside setProgram) corrupting the shared textureUnits counter, which caused GL_INVALID_OPERATION errors on shadow map sampler uniforms.
📦 Bundle sizeFull ESM build, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
travisbreaks
left a comment
There was a problem hiding this comment.
Clean fix for a real bug. Nested render calls (PMREMGenerator, render-to-texture, shadow maps) corrupting the shared textureUnits counter is exactly the kind of issue that manifests as intermittent GL_INVALID_OPERATION errors that are hard to track down.
The save/restore pattern on the render state stack is the correct approach.
One observation:
Fallback behavior
textures.setTextureUnits( currentRenderState.state.textureUnits || 0 );The `|| 0` fallback silently defaults to 0 if `textureUnits` is undefined (i.e., the render state was created before this change, or through a code path that skips the save). This is safe at runtime, but it masks a potential bug: if a render state somehow lacks `textureUnits`, silently resetting to 0 could cause incorrect texture bindings in the restored context.
Consider whether this should be initialized explicitly in the render state constructor/init instead:
currentRenderState.init( camera );
// textureUnits would already be defined as 0 from initThat way the `|| 0` becomes truly unnecessary rather than a silent safety net.
Minor point, not a blocker. The fix itself is correct and well-scoped.
Address review feedback: explicitly initialize textureUnits in WebGLRenderState and remove the || 0 fallback to avoid silently masking potential bugs.
Fixed. |
Fixed #33207
Description
Fixes nested render calls (e.g. PMREMGenerator inside setProgram)
corrupting the shared textureUnits counter, which caused
GL_INVALID_OPERATION errors on shadow map sampler uniforms.
This contribution is funded by Wearitar