Skip to content

WebGPURenderer: Make MSAA with MRT work with WebGL backend. #31228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/screenshots/webgpu_custom_fog_background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgpu_materials_toon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgpu_postprocessing_ao.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgpu_reflection.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/screenshots/webgpu_skinning_instancing.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 0 additions & 7 deletions src/nodes/display/PassNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,6 @@ class PassNode extends TempNode {

this.renderTarget.samples = this.options.samples === undefined ? renderer.samples : this.options.samples;

// TODO: Disable MSAA for WebGL backend for now
if ( renderer.backend.isWebGLBackend === true ) {

this.renderTarget.samples = 0;

}

this.renderTarget.texture.type = renderer.getColorBufferType();

return this.scope === PassNode.COLOR ? this.getTextureNode() : this.getLinearDepthNode();
Expand Down
48 changes: 45 additions & 3 deletions src/renderers/webgl-fallback/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,40 @@ class WebGLBackend extends Backend {
}

const msaaFrameBuffer = renderTargetContextData.msaaFrameBuffer;
const msaaRenderbuffers = renderTargetContextData.msaaRenderbuffers;

const textures = renderContext.textures;
const isMRT = textures.length > 1;

state.bindFramebuffer( gl.READ_FRAMEBUFFER, msaaFrameBuffer );
state.bindFramebuffer( gl.DRAW_FRAMEBUFFER, fb );

if ( isMRT ) {

// blitFramebuffer() can only copy/resolve the first color attachment of a framebuffer. When using MRT,
// the engine temporarily removes all attachments and then configures each attachment for the resolve.

for ( let i = 0; i < textures.length; i ++ ) {

gl.framebufferRenderbuffer( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.RENDERBUFFER, null );
gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, null, 0 );

}

}

for ( let i = 0; i < textures.length; i ++ ) {

// TODO Add support for MRT
if ( isMRT ) {

// configure attachment for resolve

const { textureGPU } = this.get( textures[ i ] );

gl.framebufferRenderbuffer( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, msaaRenderbuffers[ i ] );
gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textureGPU, 0 );

}

if ( renderContext.scissor ) {

Expand Down Expand Up @@ -601,6 +626,21 @@ class WebGLBackend extends Backend {

}

if ( isMRT ) {

// restore attachments

for ( let i = 0; i < textures.length; i ++ ) {

const { textureGPU } = this.get( textures[ i ] );

Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure but I think you should bind the MSAA FBO and the FBO here @Mugen87:

state.bindFramebuffer( _gl.FRAMEBUFFER, msaaFrameBuffer);

gl.framebufferRenderbuffer( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.RENDERBUFFER, msaaRenderbuffers[ i ] );

state.bindFramebuffer( _gl.FRAMEBUFFER, fb);

gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, textureGPU, 0 );

Copy link
Collaborator

@RenaudRohlinger RenaudRohlinger Jun 5, 2025

Choose a reason for hiding this comment

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

also unbind before the loop:

state.bindFramebuffer( _gl.READ_FRAMEBUFFER, null );
state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, null );

and rebind after:

state.bindFramebuffer( gl.READ_FRAMEBUFFER, msaaFrameBuffer );

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unfortunately, that does not do any difference. In fact, it seems some bindings in WebGLRenderer are redundant. A single bind of the draw and read buffer at the beginning of the blit process should be sufficinet. Removing all these bindings in WebGLRenderer does not break webgl_multiple_rendertargets.

gl.framebufferRenderbuffer( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.RENDERBUFFER, msaaRenderbuffers[ i ] );
gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, textureGPU, 0 );

}

}

} else if ( renderTarget.resolveDepthBuffer === false && renderTargetContextData.framebuffers ) {

const fb = renderTargetContextData.framebuffers[ renderContext.getCacheKey() ];
Expand Down Expand Up @@ -2095,8 +2135,6 @@ class WebGLBackend extends Backend {

}

state.drawBuffers( descriptor, fb );

}

const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
Expand Down Expand Up @@ -2271,6 +2309,8 @@ class WebGLBackend extends Backend {

}

gl.bindRenderbuffer( gl.RENDERBUFFER, null );

renderTargetContextData.msaaFrameBuffer = msaaFb;
renderTargetContextData.msaaRenderbuffers = msaaRenderbuffers;

Expand Down Expand Up @@ -2298,6 +2338,8 @@ class WebGLBackend extends Backend {

}

state.drawBuffers( descriptor, fb );

}

state.bindFramebuffer( gl.FRAMEBUFFER, currentFrameBuffer );
Expand Down
2 changes: 2 additions & 0 deletions src/renderers/webgl-fallback/utils/WebGLTextureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,8 @@ class WebGLTextureUtils {

}

gl.bindRenderbuffer( gl.RENDERBUFFER, null );

}

/**
Expand Down