Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up WebXR manager by calling setRenderTarget #22558

Merged
merged 14 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/renderers/WebGLMultisampleRenderTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@ import { WebGLRenderTarget } from './WebGLRenderTarget.js';

class WebGLMultisampleRenderTarget extends WebGLRenderTarget {

constructor( width, height, options ) {
constructor( width, height, options = {} ) {

super( width, height, options );

this.samples = 4;

this.ignoreDepthForMultisampleCopy = options.ignoreDepth !== undefined ? options.ignoreDepth : true;
this.useRenderToTexture = ( options.useRenderToTexture !== undefined ) ? options.useRenderToTexture : false;
this.useRenderbuffer = this.useRenderToTexture === false;

}

copy( source ) {

super.copy.call( this, source );

this.samples = source.samples;
this.useRenderToTexture = source.useRenderToTexture;
this.useRenderbuffer = source.useRenderbuffer;

return this;

Expand Down
67 changes: 62 additions & 5 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,8 @@ function WebGLRenderer( parameters = {} ) {
minFilter: LinearMipmapLinearFilter,
magFilter: NearestFilter,
wrapS: ClampToEdgeWrapping,
wrapT: ClampToEdgeWrapping
wrapT: ClampToEdgeWrapping,
useRenderToTexture: extensions.has( 'WEBGL_multisampled_render_to_texture' )
} );

}
Expand Down Expand Up @@ -1761,15 +1762,71 @@ function WebGLRenderer( parameters = {} ) {

};

this.setRenderTargetTextures = function ( renderTarget, colorTexture, depthTexture ) {

properties.get( renderTarget.texture ).__webglTexture = colorTexture;
properties.get( renderTarget.depthTexture ).__webglTexture = depthTexture;

const renderTargetProperties = properties.get( renderTarget );
renderTargetProperties.__hasExternalTextures = true;

if ( renderTargetProperties.__hasExternalTextures ) {

renderTargetProperties.__autoAllocateDepthBuffer = depthTexture === undefined;

if ( ! renderTargetProperties.__autoAllocateDepthBuffer ) {

// The multisample_render_to_texture extension doesn't work properly if there
// are midframe flushes and an external depth buffer. Disable use of the extension.
if ( renderTarget.useRenderToTexture ) {

console.warn( 'render-to-texture extension was disabled because an external texture was provided' );
renderTarget.useRenderToTexture = false;
renderTarget.useRenderbuffer = true;

}

}

}

};

this.setRenderTargetFramebuffer = function ( renderTarget, defaultFramebuffer ) {

const renderTargetProperties = properties.get( renderTarget );
renderTargetProperties.__webglFramebuffer = defaultFramebuffer;
renderTargetProperties.__useDefaultFramebuffer = defaultFramebuffer === undefined;

};

this.setRenderTarget = function ( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) {

_currentRenderTarget = renderTarget;
_currentActiveCubeFace = activeCubeFace;
_currentActiveMipmapLevel = activeMipmapLevel;
let useDefaultFramebuffer = true;

if ( renderTarget ) {

if ( renderTarget && properties.get( renderTarget ).__webglFramebuffer === undefined ) {
const renderTargetProperties = properties.get( renderTarget );

textures.setupRenderTarget( renderTarget );
if ( renderTargetProperties.__useDefaultFramebuffer !== undefined ) {

// We need to make sure to rebind the framebuffer.
state.bindFramebuffer( _gl.FRAMEBUFFER, null );
useDefaultFramebuffer = false;

} else if ( renderTargetProperties.__webglFramebuffer === undefined ) {

textures.setupRenderTarget( renderTarget );

} else if ( renderTargetProperties.__hasExternalTextures ) {

// Color and depth texture must be rebound in order for the swapchain to update.
textures.rebindTextures( renderTarget, properties.get( renderTarget.texture ).__webglTexture, properties.get( renderTarget.depthTexture ).__webglTexture );

}

}

Expand All @@ -1794,7 +1851,7 @@ function WebGLRenderer( parameters = {} ) {
framebuffer = __webglFramebuffer[ activeCubeFace ];
isCube = true;

} else if ( renderTarget.isWebGLMultisampleRenderTarget ) {
} else if ( renderTarget.useRenderbuffer ) {

framebuffer = properties.get( renderTarget ).__webglMultisampledFramebuffer;

Expand All @@ -1818,7 +1875,7 @@ function WebGLRenderer( parameters = {} ) {

const framebufferBound = state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );

if ( framebufferBound && capabilities.drawBuffers ) {
if ( framebufferBound && capabilities.drawBuffers && useDefaultFramebuffer ) {

let needsUpdate = false;

Expand Down
1 change: 1 addition & 0 deletions src/renderers/webgl/WebGLExtensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function WebGLExtensions( gl ) {

getExtension( 'OES_texture_float_linear' );
getExtension( 'EXT_color_buffer_half_float' );
getExtension( 'WEBGL_multisampled_render_to_texture' );

},

Expand Down
17 changes: 0 additions & 17 deletions src/renderers/webgl/WebGLState.js
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ function WebGLState( gl, extensions, capabilities ) {

let enabledCapabilities = {};

let xrFramebuffer = null;
let currentBoundFramebuffers = {};

let currentProgram = null;
Expand Down Expand Up @@ -428,22 +427,8 @@ function WebGLState( gl, extensions, capabilities ) {

}

function bindXRFramebuffer( framebuffer ) {

if ( framebuffer !== xrFramebuffer ) {

gl.bindFramebuffer( gl.FRAMEBUFFER, framebuffer );

xrFramebuffer = framebuffer;

}

}

function bindFramebuffer( target, framebuffer ) {

if ( framebuffer === null && xrFramebuffer !== null ) framebuffer = xrFramebuffer; // use active XR framebuffer if available

if ( currentBoundFramebuffers[ target ] !== framebuffer ) {

gl.bindFramebuffer( target, framebuffer );
Expand Down Expand Up @@ -1019,7 +1004,6 @@ function WebGLState( gl, extensions, capabilities ) {
currentTextureSlot = null;
currentBoundTextures = {};

xrFramebuffer = null;
currentBoundFramebuffers = {};

currentProgram = null;
Expand Down Expand Up @@ -1063,7 +1047,6 @@ function WebGLState( gl, extensions, capabilities ) {
disable: disable,

bindFramebuffer: bindFramebuffer,
bindXRFramebuffer: bindXRFramebuffer,

useProgram: useProgram,

Expand Down
Loading