From fea71b4d9c61bfc84f1a1b8035c14cc462577b76 Mon Sep 17 00:00:00 2001 From: Felix Z Date: Fri, 20 Oct 2023 11:51:11 -0700 Subject: [PATCH] WebXR: Implement antialiased multiview using OCULUS_multiview (#15) --- examples/webxr_xr_ballshooter.html | 2 +- src/renderers/WebGLMultiviewRenderTarget.js | 35 ++++ src/renderers/WebGLRenderer.js | 59 +++++- src/renderers/webgl/WebGLBackground.js | 2 +- src/renderers/webgl/WebGLMultiview.js | 100 +++++++++ src/renderers/webgl/WebGLProgram.js | 50 +++++ src/renderers/webgl/WebGLPrograms.js | 5 + src/renderers/webgl/WebGLTextures.js | 217 ++++++++++++++++++-- src/renderers/webxr/WebXRManager.js | 50 +++-- 9 files changed, 476 insertions(+), 44 deletions(-) create mode 100644 src/renderers/WebGLMultiviewRenderTarget.js create mode 100644 src/renderers/webgl/WebGLMultiview.js diff --git a/examples/webxr_xr_ballshooter.html b/examples/webxr_xr_ballshooter.html index ff46ada3b4db24..e4d6ca8609c4f2 100644 --- a/examples/webxr_xr_ballshooter.html +++ b/examples/webxr_xr_ballshooter.html @@ -67,7 +67,7 @@ // - renderer = new THREE.WebGLRenderer( { antialias: true } ); + renderer = new THREE.WebGLRenderer( { antialias: true, multiviewStereo: true } ); renderer.setPixelRatio( window.devicePixelRatio ); renderer.setSize( window.innerWidth, window.innerHeight ); renderer.setAnimationLoop( animate ); diff --git a/src/renderers/WebGLMultiviewRenderTarget.js b/src/renderers/WebGLMultiviewRenderTarget.js new file mode 100644 index 00000000000000..016d40ecf09ddd --- /dev/null +++ b/src/renderers/WebGLMultiviewRenderTarget.js @@ -0,0 +1,35 @@ +/** + * @author fernandojsg / http://fernandojsg.com + * @author Takahiro https://github.com/takahirox + */ + +import { WebGLRenderTarget } from './WebGLRenderTarget.js'; + +class WebGLMultiviewRenderTarget extends WebGLRenderTarget { + + constructor( width, height, numViews, options = {} ) { + + super( width, height, options ); + + this.depthBuffer = false; + this.stencilBuffer = false; + + this.numViews = numViews; + + } + + copy( source ) { + + super.copy( source ); + + this.numViews = source.numViews; + + return this; + + } + +} + +WebGLMultiviewRenderTarget.prototype.isWebGLMultiviewRenderTarget = true; + +export { WebGLMultiviewRenderTarget }; diff --git a/src/renderers/WebGLRenderer.js b/src/renderers/WebGLRenderer.js index 8534f4d50a21cf..527def8b19561e 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -40,6 +40,7 @@ import { WebGLGeometries } from './webgl/WebGLGeometries.js'; import { WebGLIndexedBufferRenderer } from './webgl/WebGLIndexedBufferRenderer.js'; import { WebGLInfo } from './webgl/WebGLInfo.js'; import { WebGLMorphtargets } from './webgl/WebGLMorphtargets.js'; +import { WebGLMultiview } from './webgl/WebGLMultiview.js'; import { WebGLObjects } from './webgl/WebGLObjects.js'; import { WebGLPrograms } from './webgl/WebGLPrograms.js'; import { WebGLProperties } from './webgl/WebGLProperties.js'; @@ -73,6 +74,7 @@ class WebGLRenderer { preserveDrawingBuffer = false, powerPreference = 'default', failIfMajorPerformanceCaveat = false, + multiviewStereo = false, } = parameters; this.isWebGLRenderer = true; @@ -276,6 +278,7 @@ class WebGLRenderer { let extensions, capabilities, state, info; let properties, textures, cubemaps, cubeuvmaps, attributes, geometries, objects; let programCache, materials, renderLists, renderStates, clipping, shadowMap; + let multiview; let background, morphtargets, bufferRenderer, indexedBufferRenderer; @@ -308,6 +311,7 @@ class WebGLRenderer { renderLists = new WebGLRenderLists(); renderStates = new WebGLRenderStates( extensions ); background = new WebGLBackground( _this, cubemaps, cubeuvmaps, state, objects, _alpha, premultipliedAlpha ); + multiview = new WebGLMultiview( _this, extensions, _gl ); shadowMap = new WebGLShadowMap( _this, objects, capabilities ); uniformsGroups = new WebGLUniformsGroups( _gl, info, capabilities, state ); @@ -330,7 +334,7 @@ class WebGLRenderer { // xr - const xr = ( typeof navigator !== 'undefined' && 'xr' in navigator ) ? new WebXRManager( _this, _gl ) : new WebVRManager( _this ); + const xr = ( typeof navigator !== 'undefined' && 'xr' in navigator ) ? new WebXRManager( _this, _gl, extensions, multiviewStereo ) : new WebVRManager( _this ); this.xr = xr; @@ -1232,11 +1236,21 @@ class WebGLRenderer { if ( _renderBackground ) background.render( scene ); - for ( let i = 0, l = cameras.length; i < l; i ++ ) { + if ( xr.enabled && xr.isMultiview ) { - const camera2 = cameras[ i ]; + textures.setDeferTextureUploads( true ); - renderScene( currentRenderList, scene, camera2, camera2.viewport ); + renderScene( currentRenderList, scene, camera, camera.cameras[ 0 ].viewport ); + + } else { + + for ( let i = 0, l = cameras.length; i < l; i ++ ) { + + const camera2 = cameras[ i ]; + + renderScene( currentRenderList, scene, camera2, camera2.viewport ); + + } } @@ -1268,6 +1282,8 @@ class WebGLRenderer { if ( scene.isScene === true ) scene.onAfterRender( _this, scene, camera ); + textures.runDeferredUploads(); + if ( xr.enabled && xr.submitFrame ) { xr.submitFrame(); @@ -1765,6 +1781,7 @@ class WebGLRenderer { materialProperties.vertexAlphas = parameters.vertexAlphas; materialProperties.vertexTangents = parameters.vertexTangents; materialProperties.toneMapping = parameters.toneMapping; + materialProperties.numMultiviewViews = parameters.numMultiviewViews; } @@ -1796,6 +1813,8 @@ class WebGLRenderer { } + const numMultiviewViews = _currentRenderTarget && _currentRenderTarget.isWebGLMultiviewRenderTarget ? _currentRenderTarget.numViews : 0; + const morphAttribute = geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color; const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute.length : 0; @@ -1923,6 +1942,10 @@ class WebGLRenderer { needsProgramChange = true; + } else if ( materialProperties.numMultiviewViews !== numMultiviewViews ) { + + needsProgramChange = true; + } } else { @@ -1969,8 +1992,17 @@ class WebGLRenderer { // common camera uniforms - p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix ); - p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse ); + if ( program.numMultiviewViews > 0 ) { + + multiview.updateCameraProjectionMatricesUniform( camera, p_uniforms ); + multiview.updateCameraViewMatricesUniform( camera, p_uniforms ); + + } else { + + p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix ); + p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse ); + + } const uCamPos = p_uniforms.map.cameraPosition; @@ -2132,8 +2164,17 @@ class WebGLRenderer { // common matrices - p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix ); - p_uniforms.setValue( _gl, 'normalMatrix', object.normalMatrix ); + if ( program.numMultiviewViews > 0 ) { + + multiview.updateObjectMatricesUniforms( object, camera, p_uniforms ); + + } else { + + p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix ); + p_uniforms.setValue( _gl, 'normalMatrix', object.normalMatrix ); + + } + p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld ); // UBOs @@ -2237,7 +2278,7 @@ class WebGLRenderer { renderTargetProperties.__autoAllocateDepthBuffer = depthTexture === undefined; - if ( ! renderTargetProperties.__autoAllocateDepthBuffer ) { + if ( ! renderTargetProperties.__autoAllocateDepthBuffer && ! _currentRenderTarget.isWebGLMultiviewRenderTarget ) { // 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. diff --git a/src/renderers/webgl/WebGLBackground.js b/src/renderers/webgl/WebGLBackground.js index b04eee36f73140..5bcb5510378770 100644 --- a/src/renderers/webgl/WebGLBackground.js +++ b/src/renderers/webgl/WebGLBackground.js @@ -92,7 +92,7 @@ function WebGLBackground( renderer, cubemaps, cubeuvmaps, state, objects, alpha, if ( boxMesh === undefined ) { boxMesh = new Mesh( - new BoxGeometry( 1, 1, 1 ), + new BoxGeometry( 10000, 10000, 10000 ), new ShaderMaterial( { name: 'BackgroundCubeMaterial', uniforms: cloneUniforms( ShaderLib.backgroundCube.uniforms ), diff --git a/src/renderers/webgl/WebGLMultiview.js b/src/renderers/webgl/WebGLMultiview.js new file mode 100644 index 00000000000000..4e7d581012e39f --- /dev/null +++ b/src/renderers/webgl/WebGLMultiview.js @@ -0,0 +1,100 @@ +/** + * @author fernandojsg / http://fernandojsg.com + * @author Takahiro https://github.com/takahirox + */ +import { Matrix3 } from '../../math/Matrix3.js'; +import { Matrix4 } from '../../math/Matrix4.js'; + +class WebGLMultiview { + + constructor( renderer, extensions, gl ) { + + this.renderer = renderer; + + this.DEFAULT_NUMVIEWS = 2; + this.maxNumViews = 0; + this.gl = gl; + + this.extensions = extensions; + + this.available = this.extensions.has( 'OCULUS_multiview' ); + + if ( this.available ) { + + const extension = this.extensions.get( 'OCULUS_multiview' ); + + this.maxNumViews = this.gl.getParameter( extension.MAX_VIEWS_OVR ); + + this.mat4 = []; + this.mat3 = []; + this.cameraArray = []; + + for ( var i = 0; i < this.maxNumViews; i ++ ) { + + this.mat4[ i ] = new Matrix4(); + this.mat3[ i ] = new Matrix3(); + + } + + } + + } + + // + getCameraArray( camera ) { + + if ( camera.isArrayCamera ) return camera.cameras; + + this.cameraArray[ 0 ] = camera; + + return this.cameraArray; + + } + + updateCameraProjectionMatricesUniform( camera, uniforms ) { + + var cameras = this.getCameraArray( camera ); + + for ( var i = 0; i < cameras.length; i ++ ) { + + this.mat4[ i ].copy( cameras[ i ].projectionMatrix ); + + } + + uniforms.setValue( this.gl, 'projectionMatrices', this.mat4 ); + + } + + updateCameraViewMatricesUniform( camera, uniforms ) { + + var cameras = this.getCameraArray( camera ); + + for ( var i = 0; i < cameras.length; i ++ ) { + + this.mat4[ i ].copy( cameras[ i ].matrixWorldInverse ); + + } + + uniforms.setValue( this.gl, 'viewMatrices', this.mat4 ); + + } + + updateObjectMatricesUniforms( object, camera, uniforms ) { + + var cameras = this.getCameraArray( camera ); + + for ( var i = 0; i < cameras.length; i ++ ) { + + this.mat4[ i ].multiplyMatrices( cameras[ i ].matrixWorldInverse, object.matrixWorld ); + this.mat3[ i ].getNormalMatrix( this.mat4[ i ] ); + + } + + uniforms.setValue( this.gl, 'modelViewMatrices', this.mat4 ); + uniforms.setValue( this.gl, 'normalMatrices', this.mat3 ); + + } + +} + +export { WebGLMultiview }; diff --git a/src/renderers/webgl/WebGLProgram.js b/src/renderers/webgl/WebGLProgram.js index f08a5cf088a115..767d2c53c00dc9 100644 --- a/src/renderers/webgl/WebGLProgram.js +++ b/src/renderers/webgl/WebGLProgram.js @@ -502,6 +502,8 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) { let prefixVertex, prefixFragment; let versionString = parameters.glslVersion ? '#version ' + parameters.glslVersion + '\n' : ''; + const numMultiviewViews = parameters.numMultiviewViews; + if ( parameters.isRawShaderMaterial ) { prefixVertex = [ @@ -886,6 +888,53 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) { '#define textureCubeGradEXT textureGrad' ].join( '\n' ) + '\n' + prefixFragment; + // Multiview + + if ( numMultiviewViews > 0 ) { + + // TODO: fix light transforms here? + + prefixVertex = [ + '#extension GL_OVR_multiview : require', + 'layout(num_views = ' + numMultiviewViews + ') in;', + '#define VIEW_ID gl_ViewID_OVR' + ].join( '\n' ) + '\n' + prefixVertex; + + prefixVertex = prefixVertex.replace( + [ + 'uniform mat4 modelViewMatrix;', + 'uniform mat4 projectionMatrix;', + 'uniform mat4 viewMatrix;', + 'uniform mat3 normalMatrix;' + ].join( '\n' ), + [ + 'uniform mat4 modelViewMatrices[' + numMultiviewViews + '];', + 'uniform mat4 projectionMatrices[' + numMultiviewViews + '];', + 'uniform mat4 viewMatrices[' + numMultiviewViews + '];', + 'uniform mat3 normalMatrices[' + numMultiviewViews + '];', + + '#define modelViewMatrix modelViewMatrices[VIEW_ID]', + '#define projectionMatrix projectionMatrices[VIEW_ID]', + '#define viewMatrix viewMatrices[VIEW_ID]', + '#define normalMatrix normalMatrices[VIEW_ID]' + ].join( '\n' ) + ); + + prefixFragment = [ + '#extension GL_OVR_multiview : require', + '#define VIEW_ID gl_ViewID_OVR' + ].join( '\n' ) + '\n' + prefixFragment; + + prefixFragment = prefixFragment.replace( + 'uniform mat4 viewMatrix;', + [ + 'uniform mat4 viewMatrices[' + numMultiviewViews + '];', + '#define viewMatrix viewMatrices[VIEW_ID]' + ].join( '\n' ) + ); + + } + } const vertexGlsl = versionString + prefixVertex + vertexShader; @@ -1078,6 +1127,7 @@ function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) { this.program = program; this.vertexShader = glVertexShader; this.fragmentShader = glFragmentShader; + this.numMultiviewViews = numMultiviewViews; return this; diff --git a/src/renderers/webgl/WebGLPrograms.js b/src/renderers/webgl/WebGLPrograms.js index a9a3bf80dd5551..3c014bd9b68ac2 100644 --- a/src/renderers/webgl/WebGLPrograms.js +++ b/src/renderers/webgl/WebGLPrograms.js @@ -109,6 +109,8 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities const currentRenderTarget = renderer.getRenderTarget(); + const numMultiviewViews = currentRenderTarget && currentRenderTarget.isWebGLMultiviewRenderTarget ? currentRenderTarget.numViews : 0; + const IS_INSTANCEDMESH = object.isInstancedMesh === true; const IS_BATCHEDMESH = object.isBatchedMesh === true; @@ -198,6 +200,7 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities instancingMorph: IS_INSTANCEDMESH && object.morphTexture !== null, supportsVertexTextures: SUPPORTS_VERTEX_TEXTURES, + numMultiviewViews: numMultiviewViews, outputColorSpace: ( currentRenderTarget === null ) ? renderer.outputColorSpace : ( currentRenderTarget.isXRRenderTarget === true ? currentRenderTarget.texture.colorSpace : LinearSRGBColorSpace ), alphaToCoverage: !! material.alphaToCoverage, @@ -556,6 +559,8 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities _programLayers.enable( 18 ); if ( parameters.alphaToCoverage ) _programLayers.enable( 19 ); + if ( parameters.numMultiviewViews ) + _programLayers.enable( 20 ); array.push( _programLayers.mask ); diff --git a/src/renderers/webgl/WebGLTextures.js b/src/renderers/webgl/WebGLTextures.js index ea924232005404..9a6b9300fcfdf5 100644 --- a/src/renderers/webgl/WebGLTextures.js +++ b/src/renderers/webgl/WebGLTextures.js @@ -8,6 +8,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, const multisampledRTTExt = extensions.has( 'WEBGL_multisampled_render_to_texture' ) ? extensions.get( 'WEBGL_multisampled_render_to_texture' ) : null; const supportsInvalidateFramebuffer = typeof navigator === 'undefined' ? false : /OculusBrowser/g.test( navigator.userAgent ); + const multiviewExt = extensions.has( 'OCULUS_multiview' ) ? extensions.get( 'OCULUS_multiview' ) : null; const _imageDimensions = new Vector2(); const _videoTextures = new WeakMap(); @@ -15,6 +16,9 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, const _sources = new WeakMap(); // maps WebglTexture objects to instances of Source + let _deferredUploads = []; + let _deferTextureUploads = false; + // cordova iOS (as of 5.0) still uses UIWebView, which provides OffscreenCanvas, // also OffscreenCanvas.getContext("webgl"), but not OffscreenCanvas.getContext("2d")! // Some implementations may only implement OffscreenCanvas partially (e.g. lacking 2d). @@ -496,8 +500,11 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, } else { - uploadTexture( textureProperties, texture, slot ); - return; + if ( uploadTexture( textureProperties, texture, slot ) ) { + + return; + + } } @@ -706,8 +713,45 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, } + function setDeferTextureUploads( deferFlag ) { + + _deferTextureUploads = deferFlag; + + } + + function runDeferredUploads() { + + const previousDeferSetting = _deferTextureUploads; + _deferTextureUploads = false; + + for ( const upload of _deferredUploads ) { + + uploadTexture( upload.textureProperties, upload.texture, upload.slot ); + upload.texture.isPendingDeferredUpload = false; + + } + + _deferredUploads = []; + + _deferTextureUploads = previousDeferSetting; + + } + function uploadTexture( textureProperties, texture, slot ) { + if ( _deferTextureUploads ) { + + if ( ! texture.isPendingDeferredUpload ) { + + texture.isPendingDeferredUpload = true; + _deferredUploads.push( { textureProperties: textureProperties, texture: texture, slot: slot } ); + + } + + return false; + + } + let textureType = _gl.TEXTURE_2D; if ( texture.isDataArrayTexture || texture.isCompressedArrayTexture ) textureType = _gl.TEXTURE_2D_ARRAY; @@ -1137,6 +1181,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, } textureProperties.__version = texture.version; + return true; } @@ -1392,7 +1437,11 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, const width = Math.max( 1, renderTarget.width >> level ); const height = Math.max( 1, renderTarget.height >> level ); - if ( textureTarget === _gl.TEXTURE_3D || textureTarget === _gl.TEXTURE_2D_ARRAY ) { + if ( renderTarget.isWebGLMultiviewRenderTarget === true ) { + + state.texStorage3D( _gl.TEXTURE_2D_ARRAY, 0, glInternalFormat, renderTarget.width, renderTarget.height, renderTarget.numViews ); + + } else if ( textureTarget === _gl.TEXTURE_3D || textureTarget === _gl.TEXTURE_2D_ARRAY ) { state.texImage3D( textureTarget, level, glInternalFormat, width, height, renderTarget.depth, 0, glFormat, glType, null ); @@ -1406,13 +1455,31 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer ); - if ( useMultisampledRTT( renderTarget ) ) { + const multisampled = useMultisampledRTT( renderTarget ); - multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, 0, getRenderTargetSamples( renderTarget ) ); + if ( renderTarget.isWebGLMultiviewRenderTarget === true ) { + + if ( multisampled ) { + + multiviewExt.framebufferTextureMultisampleMultiviewOVR( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, properties.get( texture ).__webglTexture, 0, getRenderTargetSamples( renderTarget ), 0, renderTarget.numViews ); + + } else { + + multiviewExt.framebufferTextureMultiviewOVR( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, properties.get( texture ).__webglTexture, 0, 0, renderTarget.numViews ); + + } } else if ( textureTarget === _gl.TEXTURE_2D || ( textureTarget >= _gl.TEXTURE_CUBE_MAP_POSITIVE_X && textureTarget <= _gl.TEXTURE_CUBE_MAP_NEGATIVE_Z ) ) { // see #24753 - _gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, level ); + if ( multisampled ) { + + multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, 0, getRenderTargetSamples( renderTarget ) ); + + } else { + + _gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, level ); + + } } @@ -1425,7 +1492,59 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer ); - if ( renderTarget.depthBuffer ) { + if ( renderTarget.isWebGLMultiviewRenderTarget === true ) { + + const useMultisample = useMultisampledRTT( renderTarget ); + const numViews = renderTarget.numViews; + + const depthTexture = renderTarget.depthTexture; + let glInternalFormat = _gl.DEPTH_COMPONENT24; + let glDepthAttachment = _gl.DEPTH_ATTACHMENT; + + if ( depthTexture && depthTexture.isDepthTexture ) { + + if ( depthTexture.type === FloatType ) { + + glInternalFormat = _gl.DEPTH_COMPONENT32F; + + } else if ( depthTexture.type === UnsignedInt248Type ) { + + glInternalFormat = _gl.DEPTH24_STENCIL8; + glDepthAttachment = _gl.DEPTH_STENCIL_ATTACHMENT; + + } + + // we're defaulting to _gl.DEPTH_COMPONENT24 so don't assign here + // or else DeepScan will complain + + // else if ( depthTexture.type === UnsignedIntType ) { + + // glInternalFormat = _gl.DEPTH_COMPONENT24; + + // } + + } + + let depthStencilTexture = properties.get( renderTarget.depthTexture ).__webglTexture; + if ( depthStencilTexture === undefined ) { + + depthStencilTexture = _gl.createTexture(); + _gl.bindTexture( _gl.TEXTURE_2D_ARRAY, depthStencilTexture ); + _gl.texStorage3D( _gl.TEXTURE_2D_ARRAY, 1, glInternalFormat, renderTarget.width, renderTarget.height, numViews ); + + } + + if ( useMultisample ) { + + multiviewExt.framebufferTextureMultisampleMultiviewOVR( _gl.FRAMEBUFFER, glDepthAttachment, depthStencilTexture, 0, getRenderTargetSamples( renderTarget ), 0, numViews ); + + } else { + + multiviewExt.framebufferTextureMultiviewOVR( _gl.FRAMEBUFFER, glDepthAttachment, depthStencilTexture, 0, 0, numViews ); + + } + + } else if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) { // retrieve the depth attachment types const depthTexture = renderTarget.depthTexture; @@ -1513,37 +1632,85 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, } setTexture2D( renderTarget.depthTexture, 0 ); + if ( renderTarget.depthTexture.image.depth != 1 ) { + + setTexture2DArray( renderTarget.depthTexture, 0 ); + + } else { + + setTexture2D( renderTarget.depthTexture, 0 ); + + } const webglDepthTexture = properties.get( renderTarget.depthTexture ).__webglTexture; const samples = getRenderTargetSamples( renderTarget ); - if ( renderTarget.depthTexture.format === DepthFormat ) { + if ( renderTarget.isWebGLMultiviewRenderTarget === true ) { - if ( useMultisampledRTT( renderTarget ) ) { + const useMultisample = useMultisampledRTT( renderTarget ); + const numViews = renderTarget.numViews; - multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples ); + if ( renderTarget.depthTexture.format === DepthFormat ) { - } else { + if ( useMultisample ) { - _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 ); + multiviewExt.framebufferTextureMultisampleMultiviewOVR( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, webglDepthTexture, 0, samples, 0, numViews ); - } + } else { + + multiviewExt.framebufferTextureMultiviewOVR( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, webglDepthTexture, 0, 0, numViews ); + + } - } else if ( renderTarget.depthTexture.format === DepthStencilFormat ) { + } else if ( renderTarget.depthTexture.format === DepthStencilFormat ) { - if ( useMultisampledRTT( renderTarget ) ) { + if ( useMultisample ) { + + multiviewExt.framebufferTextureMultisampleMultiviewOVR( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, webglDepthTexture, 0, samples, 0, numViews ); + + } else { - multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples ); + multiviewExt.framebufferTextureMultiviewOVR( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, webglDepthTexture, 0, 0, numViews ); + + } } else { - _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 ); + throw new Error( 'Unknown depthTexture format' ); } } else { - throw new Error( 'Unknown depthTexture format' ); + if ( renderTarget.depthTexture.format === DepthFormat ) { + + if ( useMultisampledRTT( renderTarget ) ) { + + multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples ); + + } else { + + _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 ); + + } + + } else if ( renderTarget.depthTexture.format === DepthStencilFormat ) { + + if ( useMultisampledRTT( renderTarget ) ) { + + multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples ); + + } else { + + _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0 ); + + } + + } else { + + throw new Error( 'Unknown depthTexture format' ); + + } } @@ -1801,6 +1968,12 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, } + if ( renderTarget.isWebGLMultiviewRenderTarget === true ) { + + glTextureType = _gl.TEXTURE_2D_ARRAY; + + } + state.bindTexture( glTextureType, textureProperties.__webglTexture ); setTextureParameters( glTextureType, texture ); @@ -1830,9 +2003,9 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, // Setup depth and stencil buffers - if ( renderTarget.depthBuffer ) { + if ( renderTarget.depthBuffer || renderTarget.isWebGLMultiviewRenderTarget === true ) { - setupDepthRenderbuffer( renderTarget ); + this.setupDepthRenderbuffer( renderTarget ); } @@ -2077,12 +2250,16 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, this.setTexture3D = setTexture3D; this.setTextureCube = setTextureCube; this.rebindTextures = rebindTextures; + this.uploadTexture = uploadTexture; this.setupRenderTarget = setupRenderTarget; this.updateRenderTargetMipmap = updateRenderTargetMipmap; this.updateMultisampleRenderTarget = updateMultisampleRenderTarget; + this.setupDepthTexture = setupDepthTexture; this.setupDepthRenderbuffer = setupDepthRenderbuffer; this.setupFrameBufferTexture = setupFrameBufferTexture; this.useMultisampledRTT = useMultisampledRTT; + this.runDeferredUploads = runDeferredUploads; + this.setDeferTextureUploads = setDeferTextureUploads; } diff --git a/src/renderers/webxr/WebXRManager.js b/src/renderers/webxr/WebXRManager.js index f420d9d2847ee6..c5ccf4dbee0beb 100644 --- a/src/renderers/webxr/WebXRManager.js +++ b/src/renderers/webxr/WebXRManager.js @@ -6,6 +6,7 @@ import { Vector3 } from '../../math/Vector3.js'; import { Vector4 } from '../../math/Vector4.js'; import { RAD2DEG } from '../../math/MathUtils.js'; import { WebGLAnimation } from '../webgl/WebGLAnimation.js'; +import { WebGLMultiviewRenderTarget } from '../WebGLMultiviewRenderTarget.js'; import { WebGLRenderTarget } from '../WebGLRenderTarget.js'; import { WebXRController } from './WebXRController.js'; import { DepthTexture } from '../../textures/DepthTexture.js'; @@ -14,7 +15,7 @@ import { WebXRDepthSensing } from './WebXRDepthSensing.js'; class WebXRManager extends EventDispatcher { - constructor( renderer, gl ) { + constructor( renderer, gl, extensions, useMultiview ) { super(); @@ -76,6 +77,7 @@ class WebXRManager extends EventDispatcher { this.enabled = false; this.isPresenting = false; + this.isMultiview = false; this.getCameraPose = function ( ) { @@ -330,12 +332,20 @@ class WebXRManager extends EventDispatcher { } + scope.isMultiview = useMultiview && extensions.has( 'OCULUS_multiview' ); + const projectionlayerInit = { colorFormat: gl.RGBA8, depthFormat: glDepthFormat, scaleFactor: framebufferScaleFactor }; + if ( scope.isMultiview ) { + + projectionlayerInit.textureType = 'texture-array'; + + } + glBinding = new XRWebGLBinding( session, gl ); glProjLayer = glBinding.createProjectionLayer( projectionlayerInit ); @@ -345,18 +355,32 @@ class WebXRManager extends EventDispatcher { renderer.setPixelRatio( 1 ); renderer.setSize( glProjLayer.textureWidth, glProjLayer.textureHeight, false ); - newRenderTarget = new WebGLRenderTarget( - glProjLayer.textureWidth, - glProjLayer.textureHeight, - { - format: RGBAFormat, - type: UnsignedByteType, - depthTexture: new DepthTexture( glProjLayer.textureWidth, glProjLayer.textureHeight, depthType, undefined, undefined, undefined, undefined, undefined, undefined, depthFormat ), - stencilBuffer: attributes.stencil, - colorSpace: renderer.outputColorSpace, - samples: attributes.antialias ? 4 : 0, - resolveDepthBuffer: ( glProjLayer.ignoreDepthValues === false ) - } ); + const renderTargetOptions = { + format: RGBAFormat, + type: UnsignedByteType, + depthTexture: new DepthTexture( glProjLayer.textureWidth, glProjLayer.textureHeight, depthType, undefined, undefined, undefined, undefined, undefined, undefined, depthFormat ), + stencilBuffer: attributes.stencil, + colorSpace: renderer.outputColorSpace, + samples: attributes.antialias ? 4 : 0, + resolveDepthBuffer: ( glProjLayer.ignoreDepthValues === false ) + }; + + if ( scope.isMultiview ) { + + const extension = extensions.get( 'OCULUS_multiview' ); + + this.maxNumViews = gl.getParameter( extension.MAX_VIEWS_OVR ); + + newRenderTarget = new WebGLMultiviewRenderTarget( glProjLayer.textureWidth, glProjLayer.textureHeight, 2, renderTargetOptions ); + + } else { + + newRenderTarget = new WebGLRenderTarget( + glProjLayer.textureWidth, + glProjLayer.textureHeight, + renderTargetOptions ); + + } }