WebGLRenderer: Ensure readback checks work on correct attachment state.#32506
Merged
Conversation
📦 Bundle sizeFull ESM build, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
6015b91 to
6a077c4
Compare
Mugen87
reviewed
Dec 8, 2025
|
|
||
| // when using MRT, select the correct color buffer for the subsequent read command | ||
|
|
||
| if ( renderTarget.textures.length > 1 ) _gl.readBuffer( _gl.COLOR_ATTACHMENT0 + textureIndex ); |
Collaborator
There was a problem hiding this comment.
At least according to my understanding the change is correct. gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_FORMAT ) requires a correct framebuffer/attachment setup which is not true without this PR.
Besides, the bounds check is only required when we actually read pixels so gl.readBuffer() can safely be executed at a earlier point in time.
Mugen87
approved these changes
Dec 27, 2025
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.
Description
This PR fixes a bug when reading pixels from MRT. When using readRenderTargetPixels or readRenderTargetPixelsAsync, the code checks whether the WebGL context can read a pixel from the requested texture:
https://github.com/mrdoob/three.js/blob/1246386f547c5b4e64d3705debfe5a22f18cc91c/src/renderers/WebGLRenderer.js#L2916C1-L2921C7
This is done in textureFormatReadable function by using
gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_FORMAT ). Nonetheless, at this point, we did not define the readBuffer yet, so the result will always return the format from COLOR_ATTACHMENT0, raising some errors when I attempt to read pixels from other color attachments. The fix was basically moving the readBuffer calling function before this check.An example of this bug: https://codepen.io/andredsm/full/jEqXVme. Check the console to see the error. In the code of this example, if you comment the second read, everything works, since we are only reading from the COLOR_ATTACHMENT0.
Finally, I made this pure WebGL2 example showing that the output from
gl.getParameter( gl.IMPLEMENTATION_COLOR_READ_FORMAT )is affected by the state ofgl.readBuffer: https://codepen.io/andredsm/pen/pvyqEqV.