diff --git a/src/renderers/common/Textures.js b/src/renderers/common/Textures.js index 79487279ba7fab..58c64b07b52331 100644 --- a/src/renderers/common/Textures.js +++ b/src/renderers/common/Textures.js @@ -127,18 +127,9 @@ class Textures extends DataMap { const options = { sampleCount }; - // when using the WebXR Layers API, the render target uses external textures which - // require no manual updates + // XR render targets require no texture updates - if ( renderTarget.isXRRenderTarget === true && renderTarget.hasExternalTextures === true ) { - - if ( depthTexture && renderTarget.autoAllocateDepthBuffer === true ) { - - this.updateTexture( depthTexture, options ); - - } - - } else { + if ( renderTarget.isXRRenderTarget !== true ) { for ( let i = 0; i < textures.length; i ++ ) { diff --git a/src/renderers/common/XRRenderTarget.js b/src/renderers/common/XRRenderTarget.js index d038cfa32f0c6f..a0bc0afe7bf02f 100644 --- a/src/renderers/common/XRRenderTarget.js +++ b/src/renderers/common/XRRenderTarget.js @@ -46,7 +46,7 @@ class XRRenderTarget extends RenderTarget { * Allocating a depth buffer is the default behavior of XR render * targets. However, when using the WebXR Layers API, this flag * must be set to `false` when the `ignoreDepthValues` property of - * the projection layers evaluates to `true`. + * the projection layers evaluates to `false`. * * Reference: {@link https://www.w3.org/TR/webxrlayers-1/#dom-xrprojectionlayer-ignoredepthvalues}. * diff --git a/src/renderers/webgl-fallback/WebGLBackend.js b/src/renderers/webgl-fallback/WebGLBackend.js index c147dcff95c57c..6451809a98f9ad 100644 --- a/src/renderers/webgl-fallback/WebGLBackend.js +++ b/src/renderers/webgl-fallback/WebGLBackend.js @@ -1963,14 +1963,24 @@ class WebGLBackend extends Backend { } - if ( descriptor.depthTexture !== null ) { + if ( renderTarget.isXRRenderTarget && renderTarget.autoAllocateDepthBuffer === true ) { - const textureData = this.get( descriptor.depthTexture ); - const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; - textureData.renderTarget = descriptor.renderTarget; - textureData.cacheKey = cacheKey; // required for copyTextureToTexture() + const renderbuffer = gl.createRenderbuffer(); + this.textureUtils.setupRenderBufferStorage( renderbuffer, descriptor, 0 ); + renderTargetContextData.xrDepthRenderbuffer = renderbuffer; + + } else { - gl.framebufferTexture2D( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0 ); + if ( descriptor.depthTexture !== null ) { + + const textureData = this.get( descriptor.depthTexture ); + const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; + textureData.renderTarget = descriptor.renderTarget; + textureData.cacheKey = cacheKey; // required for copyTextureToTexture() + + gl.framebufferTexture2D( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0 ); + + } } @@ -1989,11 +1999,17 @@ class WebGLBackend extends Backend { // rebind depth - if ( descriptor.depthTexture !== null ) { + const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; - const textureData = this.get( descriptor.depthTexture ); - const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT; + if ( renderTarget.autoAllocateDepthBuffer === true ) { + + const renderbuffer = renderTargetContextData.xrDepthRenderbuffer; + gl.bindRenderbuffer( gl.RENDERBUFFER, renderbuffer ); + gl.framebufferRenderbuffer( gl.FRAMEBUFFER, depthStyle, gl.RENDERBUFFER, renderbuffer ); + } else { + + const textureData = this.get( descriptor.depthTexture ); gl.framebufferTexture2D( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0 ); } @@ -2046,7 +2062,7 @@ class WebGLBackend extends Backend { if ( depthRenderbuffer === undefined ) { depthRenderbuffer = gl.createRenderbuffer(); - this.textureUtils.setupRenderBufferStorage( depthRenderbuffer, descriptor ); + this.textureUtils.setupRenderBufferStorage( depthRenderbuffer, descriptor, samples ); renderTargetContextData.depthRenderbuffer = depthRenderbuffer; diff --git a/src/renderers/webgl-fallback/utils/WebGLTextureUtils.js b/src/renderers/webgl-fallback/utils/WebGLTextureUtils.js index 85c8f75921722e..95eeb1b8538e0a 100644 --- a/src/renderers/webgl-fallback/utils/WebGLTextureUtils.js +++ b/src/renderers/webgl-fallback/utils/WebGLTextureUtils.js @@ -917,13 +917,14 @@ class WebGLTextureUtils { * * @param {WebGLRenderbuffer} renderbuffer - The render buffer. * @param {RenderContext} renderContext - The render context. + * @param {Number} samples - The MSAA sample count. */ - setupRenderBufferStorage( renderbuffer, renderContext ) { + setupRenderBufferStorage( renderbuffer, renderContext, samples ) { const { gl } = this; const renderTarget = renderContext.renderTarget; - const { samples, depthTexture, depthBuffer, stencilBuffer, width, height } = renderTarget; + const { depthTexture, depthBuffer, stencilBuffer, width, height } = renderTarget; gl.bindRenderbuffer( gl.RENDERBUFFER, renderbuffer );