Skip to content

Conversation

@andredsm
Copy link
Contributor

@andredsm andredsm commented Dec 8, 2025

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 of gl.readBuffer: https://codepen.io/andredsm/pen/pvyqEqV.

@andredsm andredsm changed the title WebGLRenderer: Ensure correct color buffer selection for MRT read com… WebGLRenderer: bugfix when reading pixel in MRT Dec 8, 2025
@github-actions
Copy link

github-actions bot commented Dec 8, 2025

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 350.9
83.23
350.9
83.23
-2 B
-3 B
WebGPU 616.22
171.03
616.22
171.03
+0 B
+0 B
WebGPU Nodes 614.82
170.77
614.82
170.77
+0 B
+0 B

🌳 Bundle size after tree-shaking

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

Before After Diff
WebGL 483.14
118.07
483.14
118.07
-2 B
-4 B
WebGPU 687.06
186.63
687.06
186.63
+0 B
+0 B
WebGPU Nodes 636.9
173.84
636.9
173.84
+0 B
+0 B


// when using MRT, select the correct color buffer for the subsequent read command

if ( renderTarget.textures.length > 1 ) _gl.readBuffer( _gl.COLOR_ATTACHMENT0 + textureIndex );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@repalash Does this change look good to you?

@Mugen87 Mugen87 added this to the r182 milestone Dec 8, 2025
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.

2 participants