Skip to content

WebGLRenderer: Ensure readback checks work on correct attachment state.#32506

Merged
Mugen87 merged 1 commit into
mrdoob:devfrom
andredsm:fix/mrt-read-pixel
Dec 27, 2025
Merged

WebGLRenderer: Ensure readback checks work on correct attachment state.#32506
Mugen87 merged 1 commit into
mrdoob:devfrom
andredsm:fix/mrt-read-pixel

Conversation

@andredsm

@andredsm andredsm commented Dec 8, 2025

Copy link
Copy Markdown
Contributor

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

github-actions Bot commented Dec 8, 2025

Copy link
Copy Markdown

📦 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
Copy Markdown
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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes this is correct.

@Mugen87 Mugen87 added this to the r182 milestone Dec 8, 2025
@mrdoob mrdoob modified the milestones: r182, r183 Dec 10, 2025
@Mugen87 Mugen87 merged commit ebd9792 into mrdoob:dev Dec 27, 2025
15 of 16 checks passed
@Mugen87 Mugen87 changed the title WebGLRenderer: bugfix when reading pixel in MRT WebGLRenderer: Ensure readback checks work on correct attachment state. Dec 27, 2025
@andredsm andredsm deleted the fix/mrt-read-pixel branch December 27, 2025 10:53
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