@@ -353,6 +353,14 @@ class WebGLBackend extends Backend {
353
353
354
354
renderTarget . autoAllocateDepthBuffer = false ;
355
355
356
+ // The multisample_render_to_texture extension doesn't work properly if there
357
+ // are midframe flushes and an external depth texture.
358
+ if ( this . extensions . has ( 'WEBGL_multisampled_render_to_texture' ) === true ) {
359
+
360
+ console . warn ( 'THREE.WebGLBackend: Render-to-texture extension was disabled because an external texture was provided' ) ;
361
+
362
+ }
363
+
356
364
}
357
365
358
366
}
@@ -530,7 +538,7 @@ class WebGLBackend extends Backend {
530
538
531
539
const { samples } = renderContext . renderTarget ;
532
540
533
- if ( samples > 0 ) {
541
+ if ( samples > 0 && this . _useMultisampledRTT ( renderContext . renderTarget ) === false ) {
534
542
535
543
const fb = renderTargetContextData . framebuffers [ renderContext . getCacheKey ( ) ] ;
536
544
@@ -1889,6 +1897,8 @@ class WebGLBackend extends Backend {
1889
1897
1890
1898
let msaaFb = renderTargetContextData . msaaFrameBuffer ;
1891
1899
let depthRenderbuffer = renderTargetContextData . depthRenderbuffer ;
1900
+ const multisampledRTTExt = this . extensions . get ( 'WEBGL_multisampled_render_to_texture' ) ;
1901
+ const useMultisampledRTT = this . _useMultisampledRTT ( renderTarget ) ;
1892
1902
1893
1903
const cacheKey = getCacheKey ( descriptor ) ;
1894
1904
@@ -1951,11 +1961,17 @@ class WebGLBackend extends Backend {
1951
1961
1952
1962
} else {
1953
1963
1954
- gl . framebufferTexture2D ( gl . FRAMEBUFFER , attachment , gl . TEXTURE_2D , textureData . textureGPU , 0 ) ;
1964
+ if ( useMultisampledRTT ) {
1955
1965
1956
- }
1966
+ multisampledRTTExt . framebufferTexture2DMultisampleEXT ( gl . FRAMEBUFFER , attachment , gl . TEXTURE_2D , textureData . textureGPU , 0 , samples ) ;
1957
1967
1968
+ } else {
1958
1969
1970
+ gl . framebufferTexture2D ( gl . FRAMEBUFFER , attachment , gl . TEXTURE_2D , textureData . textureGPU , 0 ) ;
1971
+
1972
+ }
1973
+
1974
+ }
1959
1975
1960
1976
}
1961
1977
@@ -1966,7 +1982,7 @@ class WebGLBackend extends Backend {
1966
1982
if ( renderTarget . isXRRenderTarget && renderTarget . autoAllocateDepthBuffer === true ) {
1967
1983
1968
1984
const renderbuffer = gl . createRenderbuffer ( ) ;
1969
- this . textureUtils . setupRenderBufferStorage ( renderbuffer , descriptor , 0 ) ;
1985
+ this . textureUtils . setupRenderBufferStorage ( renderbuffer , descriptor , 0 , useMultisampledRTT ) ;
1970
1986
renderTargetContextData . xrDepthRenderbuffer = renderbuffer ;
1971
1987
1972
1988
} else {
@@ -1978,7 +1994,15 @@ class WebGLBackend extends Backend {
1978
1994
textureData . renderTarget = descriptor . renderTarget ;
1979
1995
textureData . cacheKey = cacheKey ; // required for copyTextureToTexture()
1980
1996
1981
- gl . framebufferTexture2D ( gl . FRAMEBUFFER , depthStyle , gl . TEXTURE_2D , textureData . textureGPU , 0 ) ;
1997
+ if ( useMultisampledRTT ) {
1998
+
1999
+ multisampledRTTExt . framebufferTexture2DMultisampleEXT ( gl . FRAMEBUFFER , depthStyle , gl . TEXTURE_2D , textureData . textureGPU , 0 , samples ) ;
2000
+
2001
+ } else {
2002
+
2003
+ gl . framebufferTexture2D ( gl . FRAMEBUFFER , depthStyle , gl . TEXTURE_2D , textureData . textureGPU , 0 ) ;
2004
+
2005
+ }
1982
2006
1983
2007
}
1984
2008
@@ -1995,7 +2019,16 @@ class WebGLBackend extends Backend {
1995
2019
// rebind color
1996
2020
1997
2021
const textureData = this . get ( descriptor . textures [ 0 ] ) ;
1998
- gl . framebufferTexture2D ( gl . FRAMEBUFFER , gl . COLOR_ATTACHMENT0 , gl . TEXTURE_2D , textureData . textureGPU , 0 ) ;
2022
+
2023
+ if ( useMultisampledRTT ) {
2024
+
2025
+ multisampledRTTExt . framebufferTexture2DMultisampleEXT ( gl . FRAMEBUFFER , gl . COLOR_ATTACHMENT0 , gl . TEXTURE_2D , textureData . textureGPU , 0 , samples ) ;
2026
+
2027
+ } else {
2028
+
2029
+ gl . framebufferTexture2D ( gl . FRAMEBUFFER , gl . COLOR_ATTACHMENT0 , gl . TEXTURE_2D , textureData . textureGPU , 0 ) ;
2030
+
2031
+ }
1999
2032
2000
2033
// rebind depth
2001
2034
@@ -2010,15 +2043,24 @@ class WebGLBackend extends Backend {
2010
2043
} else {
2011
2044
2012
2045
const textureData = this . get ( descriptor . depthTexture ) ;
2013
- gl . framebufferTexture2D ( gl . FRAMEBUFFER , depthStyle , gl . TEXTURE_2D , textureData . textureGPU , 0 ) ;
2046
+
2047
+ if ( useMultisampledRTT ) {
2048
+
2049
+ multisampledRTTExt . framebufferTexture2DMultisampleEXT ( gl . FRAMEBUFFER , depthStyle , gl . TEXTURE_2D , textureData . textureGPU , 0 , samples ) ;
2050
+
2051
+ } else {
2052
+
2053
+ gl . framebufferTexture2D ( gl . FRAMEBUFFER , depthStyle , gl . TEXTURE_2D , textureData . textureGPU , 0 ) ;
2054
+
2055
+ }
2014
2056
2015
2057
}
2016
2058
2017
2059
}
2018
2060
2019
2061
}
2020
2062
2021
- if ( samples > 0 ) {
2063
+ if ( samples > 0 && useMultisampledRTT === false ) {
2022
2064
2023
2065
if ( msaaFb === undefined ) {
2024
2066
@@ -2323,6 +2365,20 @@ class WebGLBackend extends Backend {
2323
2365
2324
2366
}
2325
2367
2368
+ /**
2369
+ * Returns `true` if the `WEBGL_multisampled_render_to_texture` extension
2370
+ * should be used when MSAA is enabled.
2371
+ *
2372
+ * @private
2373
+ * @param {RenderTarget } renderTarget - The render target that should be multisampled.
2374
+ * @return {Boolean } Whether to use the `WEBGL_multisampled_render_to_texture` extension for MSAA or not.
2375
+ */
2376
+ _useMultisampledRTT ( renderTarget ) {
2377
+
2378
+ return renderTarget . samples > 0 && this . extensions . has ( 'WEBGL_multisampled_render_to_texture' ) === true && renderTarget . autoAllocateDepthBuffer !== false ;
2379
+
2380
+ }
2381
+
2326
2382
/**
2327
2383
* Frees internal resources.
2328
2384
*/
0 commit comments