Skip to content

Commit 192a456

Browse files
committed
WebGPURenderer: Make MSAA with MRT work with WebGL backend.
1 parent 7b31083 commit 192a456

File tree

7 files changed

+47
-10
lines changed

7 files changed

+47
-10
lines changed
17.7 KB
Loading
-1.86 KB
Loading
79 KB
Loading
38 KB
Loading

src/nodes/display/PassNode.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,13 +586,6 @@ class PassNode extends TempNode {
586586

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

589-
// TODO: Disable MSAA for WebGL backend for now
590-
if ( renderer.backend.isWebGLBackend === true ) {
591-
592-
this.renderTarget.samples = 0;
593-
594-
}
595-
596589
this.renderTarget.texture.type = renderer.getColorBufferType();
597590

598591
return this.scope === PassNode.COLOR ? this.getTextureNode() : this.getLinearDepthNode();

src/renderers/webgl-fallback/WebGLBackend.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,40 @@ class WebGLBackend extends Backend {
563563
}
564564

565565
const msaaFrameBuffer = renderTargetContextData.msaaFrameBuffer;
566+
const msaaRenderbuffers = renderTargetContextData.msaaRenderbuffers;
566567

567568
const textures = renderContext.textures;
569+
const isMRT = textures.length > 1;
568570

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

574+
if ( isMRT ) {
575+
576+
// blitFramebuffer() can only copy/resolve the first color attachment of a framebuffer. When using MRT,
577+
// the engine temporarily removes all attachments and then configures each attachment for the resolve.
578+
579+
for ( let i = 0; i < textures.length; i ++ ) {
580+
581+
gl.framebufferRenderbuffer( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.RENDERBUFFER, null );
582+
gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, null, 0 );
583+
584+
}
585+
586+
}
587+
572588
for ( let i = 0; i < textures.length; i ++ ) {
573589

574-
// TODO Add support for MRT
590+
if ( isMRT ) {
591+
592+
// configure attachment for resolve
593+
594+
const { textureGPU } = this.get( textures[ i ] );
595+
596+
gl.framebufferRenderbuffer( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, msaaRenderbuffers[ i ] );
597+
gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, textureGPU, 0 );
598+
599+
}
575600

576601
if ( renderContext.scissor ) {
577602

@@ -601,6 +626,21 @@ class WebGLBackend extends Backend {
601626

602627
}
603628

629+
if ( isMRT ) {
630+
631+
// restore attachments
632+
633+
for ( let i = 0; i < textures.length; i ++ ) {
634+
635+
const { textureGPU } = this.get( textures[ i ] );
636+
637+
gl.framebufferRenderbuffer( gl.READ_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.RENDERBUFFER, msaaRenderbuffers[ i ] );
638+
gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, textureGPU, 0 );
639+
640+
}
641+
642+
}
643+
604644
} else if ( renderTarget.resolveDepthBuffer === false && renderTargetContextData.framebuffers ) {
605645

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

20962136
}
20972137

2098-
state.drawBuffers( descriptor, fb );
2099-
21002138
}
21012139

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

22722310
}
22732311

2312+
gl.bindRenderbuffer( gl.RENDERBUFFER, null );
2313+
22742314
renderTargetContextData.msaaFrameBuffer = msaaFb;
22752315
renderTargetContextData.msaaRenderbuffers = msaaRenderbuffers;
22762316

@@ -2298,6 +2338,8 @@ class WebGLBackend extends Backend {
22982338

22992339
}
23002340

2341+
state.drawBuffers( descriptor, fb );
2342+
23012343
}
23022344

23032345
state.bindFramebuffer( gl.FRAMEBUFFER, currentFrameBuffer );

src/renderers/webgl-fallback/utils/WebGLTextureUtils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,8 @@ class WebGLTextureUtils {
10211021

10221022
}
10231023

1024+
gl.bindRenderbuffer( gl.RENDERBUFFER, null );
1025+
10241026
}
10251027

10261028
/**

0 commit comments

Comments
 (0)