Skip to content

Commit

Permalink
discard depth buffer where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
cabanier committed Jan 19, 2023
1 parent 1b7c37e commit ea1fad2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 53 deletions.
Binary file modified examples/screenshots/webxr_vr_xrlayers.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
126 changes: 73 additions & 53 deletions src/renderers/webgl/WebGLTextures.js
Original file line number Diff line number Diff line change
Expand Up @@ -1784,106 +1784,126 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,

function updateMultisampleRenderTarget( renderTarget ) {

if ( ( isWebGL2 && renderTarget.samples > 0 ) && useMultisampledRTT( renderTarget ) === false ) {
if ( ( isWebGL2 && renderTarget.samples > 0 ) ) {

const textures = renderTarget.isWebGLMultipleRenderTargets ? renderTarget.texture : [ renderTarget.texture ];
const width = renderTarget.width;
const height = renderTarget.height;
let mask = _gl.COLOR_BUFFER_BIT;
const invalidationArray = [];
const depthStyle = renderTarget.stencilBuffer ? _gl.DEPTH_STENCIL_ATTACHMENT : _gl.DEPTH_ATTACHMENT;
const renderTargetProperties = properties.get( renderTarget );
const isMultipleRenderTargets = ( renderTarget.isWebGLMultipleRenderTargets === true );

// If MRT we need to remove FBO attachments
if ( isMultipleRenderTargets ) {
if ( useMultisampledRTT( renderTarget ) ) {

for ( let i = 0; i < textures.length; i ++ ) {
const ignoreDepthValues = ( renderTargetProperties.__ignoreDepthValues !== undefined ) ? renderTargetProperties.__ignoreDepthValues : false;

if ( ignoreDepthValues ) {

state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, null );
const depthStyle = renderTarget.stencilBuffer ? _gl.DEPTH_STENCIL_ATTACHMENT : _gl.DEPTH_ATTACHMENT;

state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
_gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D, null, 0 );
state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
_gl.invalidateFramebuffer( _gl.DRAW_FRAMEBUFFER, [ depthStyle ] );
state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, null );

}

}
} else {

state.bindFramebuffer( _gl.READ_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
const textures = renderTarget.isWebGLMultipleRenderTargets ? renderTarget.texture : [ renderTarget.texture ];
const width = renderTarget.width;
const height = renderTarget.height;
let mask = _gl.COLOR_BUFFER_BIT;
const invalidationArray = [];
const isMultipleRenderTargets = ( renderTarget.isWebGLMultipleRenderTargets === true );

for ( let i = 0; i < textures.length; i ++ ) {
// If MRT we need to remove FBO attachments
if ( isMultipleRenderTargets ) {

invalidationArray.push( _gl.COLOR_ATTACHMENT0 + i );
for ( let i = 0; i < textures.length; i ++ ) {

if ( renderTarget.depthBuffer ) {
state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, null );

invalidationArray.push( depthStyle );
state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
_gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D, null, 0 );

}

}

const ignoreDepthValues = ( renderTargetProperties.__ignoreDepthValues !== undefined ) ? renderTargetProperties.__ignoreDepthValues : false;
state.bindFramebuffer( _gl.READ_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );

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

if ( renderTarget.depthBuffer ) mask |= _gl.DEPTH_BUFFER_BIT;
if ( renderTarget.stencilBuffer ) mask |= _gl.STENCIL_BUFFER_BIT;
invalidationArray.push( _gl.COLOR_ATTACHMENT0 + i );

}
if ( renderTarget.depthBuffer ) {

if ( isMultipleRenderTargets ) {
invalidationArray.push( depthStyle );

_gl.framebufferRenderbuffer( _gl.READ_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] );
}

}
const ignoreDepthValues = ( renderTargetProperties.__ignoreDepthValues !== undefined ) ? renderTargetProperties.__ignoreDepthValues : false;

if ( ignoreDepthValues === true ) {
if ( ignoreDepthValues === false ) {

_gl.invalidateFramebuffer( _gl.READ_FRAMEBUFFER, [ depthStyle ] );
_gl.invalidateFramebuffer( _gl.DRAW_FRAMEBUFFER, [ depthStyle ] );
if ( renderTarget.depthBuffer ) mask |= _gl.DEPTH_BUFFER_BIT;
if ( renderTarget.stencilBuffer ) mask |= _gl.STENCIL_BUFFER_BIT;

}
}

if ( isMultipleRenderTargets ) {
if ( isMultipleRenderTargets ) {

const webglTexture = properties.get( textures[ i ] ).__webglTexture;
_gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, webglTexture, 0 );
_gl.framebufferRenderbuffer( _gl.READ_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] );

}
}

_gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, _gl.NEAREST );
if ( ignoreDepthValues === true ) {

if ( supportsInvalidateFramebuffer ) {
_gl.invalidateFramebuffer( _gl.READ_FRAMEBUFFER, [ depthStyle ] );
_gl.invalidateFramebuffer( _gl.DRAW_FRAMEBUFFER, [ depthStyle ] );

_gl.invalidateFramebuffer( _gl.READ_FRAMEBUFFER, invalidationArray );
}

}
if ( isMultipleRenderTargets ) {

const webglTexture = properties.get( textures[ i ] ).__webglTexture;
_gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, _gl.TEXTURE_2D, webglTexture, 0 );

}
}

state.bindFramebuffer( _gl.READ_FRAMEBUFFER, null );
state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, null );
_gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, _gl.NEAREST );

// If MRT since pre-blit we removed the FBO we need to reconstruct the attachments
if ( isMultipleRenderTargets ) {
if ( supportsInvalidateFramebuffer ) {

for ( let i = 0; i < textures.length; i ++ ) {
_gl.invalidateFramebuffer( _gl.READ_FRAMEBUFFER, invalidationArray );

state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] );
}

const webglTexture = properties.get( textures[ i ] ).__webglTexture;

state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
_gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D, webglTexture, 0 );
}

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

// If MRT since pre-blit we removed the FBO we need to reconstruct the attachments
if ( isMultipleRenderTargets ) {

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

state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );
_gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.RENDERBUFFER, renderTargetProperties.__webglColorRenderbuffer[ i ] );

const webglTexture = properties.get( textures[ i ] ).__webglTexture;

state.bindFramebuffer( _gl.FRAMEBUFFER, renderTargetProperties.__webglFramebuffer );
_gl.framebufferTexture2D( _gl.DRAW_FRAMEBUFFER, _gl.COLOR_ATTACHMENT0 + i, _gl.TEXTURE_2D, webglTexture, 0 );

}

}

state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );

}

state.bindFramebuffer( _gl.DRAW_FRAMEBUFFER, renderTargetProperties.__webglMultisampledFramebuffer );

}

Expand Down
2 changes: 2 additions & 0 deletions src/renderers/webxr/WebXRManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ class WebXRManager extends EventDispatcher {
samples: attributes.antialias ? 4 : 1
} );

renderer.properties.get( renderTarget ).__ignoreDepthValues = true;
const material = new MeshBasicMaterial( { color: 0xffffff, side: FrontSide } );
material.map = renderTarget.texture;
const plane = new Mesh( geometry, material );
Expand Down Expand Up @@ -800,6 +801,7 @@ class WebXRManager extends EventDispatcher {
samples: attributes.antialias ? 4 : 1
} );

renderer.properties.get( renderTarget ).__ignoreDepthValues = true;
const material = new MeshBasicMaterial( { color: 0xffffff, side: BackSide } );
material.map = renderTarget.texture;
const plane = new Mesh( geometry, material );
Expand Down

0 comments on commit ea1fad2

Please sign in to comment.