@@ -563,15 +563,40 @@ class WebGLBackend extends Backend {
563
563
}
564
564
565
565
const msaaFrameBuffer = renderTargetContextData . msaaFrameBuffer ;
566
+ const msaaRenderbuffers = renderTargetContextData . msaaRenderbuffers ;
566
567
567
568
const textures = renderContext . textures ;
569
+ const isMRT = textures . length > 1 ;
568
570
569
571
state . bindFramebuffer ( gl . READ_FRAMEBUFFER , msaaFrameBuffer ) ;
570
572
state . bindFramebuffer ( gl . DRAW_FRAMEBUFFER , fb ) ;
571
573
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
+
572
588
for ( let i = 0 ; i < textures . length ; i ++ ) {
573
589
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
+ }
575
600
576
601
if ( renderContext . scissor ) {
577
602
@@ -601,6 +626,21 @@ class WebGLBackend extends Backend {
601
626
602
627
}
603
628
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
+
604
644
} else if ( renderTarget . resolveDepthBuffer === false && renderTargetContextData . framebuffers ) {
605
645
606
646
const fb = renderTargetContextData . framebuffers [ renderContext . getCacheKey ( ) ] ;
@@ -2095,8 +2135,6 @@ class WebGLBackend extends Backend {
2095
2135
2096
2136
}
2097
2137
2098
- state . drawBuffers ( descriptor , fb ) ;
2099
-
2100
2138
}
2101
2139
2102
2140
const depthStyle = stencilBuffer ? gl . DEPTH_STENCIL_ATTACHMENT : gl . DEPTH_ATTACHMENT ;
@@ -2271,6 +2309,8 @@ class WebGLBackend extends Backend {
2271
2309
2272
2310
}
2273
2311
2312
+ gl . bindRenderbuffer ( gl . RENDERBUFFER , null ) ;
2313
+
2274
2314
renderTargetContextData . msaaFrameBuffer = msaaFb ;
2275
2315
renderTargetContextData . msaaRenderbuffers = msaaRenderbuffers ;
2276
2316
@@ -2298,6 +2338,8 @@ class WebGLBackend extends Backend {
2298
2338
2299
2339
}
2300
2340
2341
+ state . drawBuffers ( descriptor , fb ) ;
2342
+
2301
2343
}
2302
2344
2303
2345
state . bindFramebuffer ( gl . FRAMEBUFFER , currentFrameBuffer ) ;
0 commit comments