diff --git a/examples/webxr_xr_ballshooter.html b/examples/webxr_xr_ballshooter.html index 28e39f2df18181..7a205137f809f1 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( render ); 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 9f338b6b800871..b4bbaa53c70d15 100644 --- a/src/renderers/WebGLRenderer.js +++ b/src/renderers/WebGLRenderer.js @@ -43,6 +43,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'; @@ -76,6 +77,7 @@ class WebGLRenderer { preserveDrawingBuffer = false, powerPreference = 'default', failIfMajorPerformanceCaveat = false, + multiviewStereo = false, } = parameters; this.isWebGLRenderer = true; @@ -283,6 +285,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; @@ -315,6 +318,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 ); @@ -337,7 +341,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; @@ -1184,13 +1188,23 @@ class WebGLRenderer { if ( camera.isArrayCamera ) { - const cameras = camera.cameras; + if ( xr.enabled && xr.isMultiview ) { - for ( let i = 0, l = cameras.length; i < l; i ++ ) { + textures.setDeferTextureUploads( true ); - const camera2 = cameras[ i ]; + renderScene( currentRenderList, scene, camera, camera.cameras[ 0 ].viewport ); - renderScene( currentRenderList, scene, camera2, camera2.viewport ); + } else { + + const cameras = camera.cameras; + + for ( let i = 0, l = cameras.length; i < l; i ++ ) { + + const camera2 = cameras[ i ]; + + renderScene( currentRenderList, scene, camera2, camera2.viewport ); + + } } @@ -1218,6 +1232,8 @@ class WebGLRenderer { if ( scene.isScene === true ) scene.onAfterRender( _this, scene, camera ); + textures.runDeferredUploads(); + if ( xr.enabled && xr.submitFrame ) { xr.submitFrame(); @@ -1701,6 +1717,7 @@ class WebGLRenderer { materialProperties.vertexAlphas = parameters.vertexAlphas; materialProperties.vertexTangents = parameters.vertexTangents; materialProperties.toneMapping = parameters.toneMapping; + materialProperties.numMultiviewViews = parameters.numMultiviewViews; } @@ -1732,6 +1749,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; @@ -1851,6 +1870,10 @@ class WebGLRenderer { needsProgramChange = true; + } else if ( materialProperties.numMultiviewViews !== numMultiviewViews ) { + + needsProgramChange = true; + } } else { @@ -1897,8 +1920,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; @@ -2050,8 +2082,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 @@ -2155,7 +2196,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 03dd387e54db12..f7fc66fb748a08 100644 --- a/src/renderers/webgl/WebGLBackground.js +++ b/src/renderers/webgl/WebGLBackground.js @@ -72,7 +72,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 2f948ff8f2db36..cfd15ace1f99d1 100644 --- a/src/renderers/webgl/WebGLProgram.js +++ b/src/renderers/webgl/WebGLProgram.js @@ -481,6 +481,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 = [ @@ -891,6 +893,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; @@ -1083,6 +1132,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 4b0f07f7028b68..b2c97442112095 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; @@ -196,6 +198,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, @@ -551,6 +554,8 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities _programLayers.enable( 19 ); if ( parameters.alphaToCoverage ) _programLayers.enable( 20 ); + if ( parameters.numMultiviewViews ) + _programLayers.enable( 21 ); array.push( _programLayers.mask ); diff --git a/src/renderers/webgl/WebGLTextures.js b/src/renderers/webgl/WebGLTextures.js index 4f2cbe0d03c7ad..45271916a52c48 100644 --- a/src/renderers/webgl/WebGLTextures.js +++ b/src/renderers/webgl/WebGLTextures.js @@ -7,6 +7,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(); @@ -14,6 +15,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). @@ -453,8 +457,11 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, } else { - uploadTexture( textureProperties, texture, slot ); - return; + if ( uploadTexture( textureProperties, texture, slot ) ) { + + return; + + } } @@ -663,8 +670,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; @@ -1070,6 +1114,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, } textureProperties.__version = texture.version; + return true; } @@ -1325,7 +1370,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 ); @@ -1339,13 +1388,31 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, state.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer ); - if ( useMultisampledRTT( renderTarget ) ) { + const multisampled = useMultisampledRTT( renderTarget ); + + if ( renderTarget.isWebGLMultiviewRenderTarget === true ) { - multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, attachment, textureTarget, properties.get( texture ).__webglTexture, 0, getRenderTargetSamples( renderTarget ) ); + 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 ); + + } } @@ -1359,7 +1426,59 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer ); - if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) { + 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 ) { let glInternalFormat = _gl.DEPTH_COMPONENT24; @@ -1483,37 +1602,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 { - } else if ( renderTarget.depthTexture.format === DepthStencilFormat ) { + multiviewExt.framebufferTextureMultiviewOVR( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, webglDepthTexture, 0, 0, numViews ); - if ( useMultisampledRTT( renderTarget ) ) { + } + + } else if ( renderTarget.depthTexture.format === DepthStencilFormat ) { - multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.TEXTURE_2D, webglDepthTexture, 0, samples ); + if ( useMultisample ) { + + multiviewExt.framebufferTextureMultisampleMultiviewOVR( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, webglDepthTexture, 0, samples, 0, numViews ); + + } else { + + 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' ); + + } } @@ -1771,6 +1938,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 ); @@ -1800,9 +1973,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 ); } @@ -2039,12 +2212,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 e4b20c913ba75a..4708e85e53d647 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,17 +355,31 @@ 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 - } ); + 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 + }; + + 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 ); + + } const renderTargetProperties = renderer.properties.get( newRenderTarget ); renderTargetProperties.__ignoreDepthValues = glProjLayer.ignoreDepthValues;