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 34eb83bdff0813..5109f762172af2 100644
--- a/src/renderers/WebGLRenderer.js
+++ b/src/renderers/WebGLRenderer.js
@@ -38,6 +38,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';
@@ -71,6 +72,7 @@ class WebGLRenderer {
powerPreference = 'default',
failIfMajorPerformanceCaveat = false,
reverseDepthBuffer = false,
+ multiviewStereo = false,
} = parameters;
this.isWebGLRenderer = true;
@@ -278,6 +280,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;
@@ -316,6 +319,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 );
@@ -1252,11 +1256,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 );
+
+ }
}
@@ -1782,6 +1796,7 @@ class WebGLRenderer {
materialProperties.vertexAlphas = parameters.vertexAlphas;
materialProperties.vertexTangents = parameters.vertexTangents;
materialProperties.toneMapping = parameters.toneMapping;
+ materialProperties.numMultiviewViews = parameters.numMultiviewViews;
}
@@ -1813,6 +1828,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;
@@ -1940,6 +1957,10 @@ class WebGLRenderer {
needsProgramChange = true;
+ } else if ( materialProperties.numMultiviewViews !== numMultiviewViews ) {
+
+ needsProgramChange = true;
+
}
} else {
@@ -1986,24 +2007,33 @@ class WebGLRenderer {
// common camera uniforms
- const reverseDepthBuffer = state.buffers.depth.getReversed();
+ if ( program.numMultiviewViews > 0 ) {
- if ( reverseDepthBuffer ) {
+ multiview.updateCameraProjectionMatricesUniform( camera, p_uniforms );
+ multiview.updateCameraViewMatricesUniform( camera, p_uniforms );
- _currentProjectionMatrix.copy( camera.projectionMatrix );
+ } else {
- toNormalizedProjectionMatrix( _currentProjectionMatrix );
- toReversedProjectionMatrix( _currentProjectionMatrix );
+ const reverseDepthBuffer = state.buffers.depth.getReversed();
- p_uniforms.setValue( _gl, 'projectionMatrix', _currentProjectionMatrix );
+ if ( reverseDepthBuffer ) {
- } else {
+ _currentProjectionMatrix.copy( camera.projectionMatrix );
- p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix );
+ toNormalizedProjectionMatrix( _currentProjectionMatrix );
+ toReversedProjectionMatrix( _currentProjectionMatrix );
- }
+ p_uniforms.setValue( _gl, 'projectionMatrix', _currentProjectionMatrix );
+
+ } else {
+
+ p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix );
- p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
+ }
+
+ p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse );
+
+ }
const uCamPos = p_uniforms.map.cameraPosition;
@@ -2165,8 +2195,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
@@ -2270,7 +2309,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 1344ac8ac4f8db..befed9d6583797 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 f53af2d422d410..59ed5df02ba1f9 100644
--- a/src/renderers/webgl/WebGLProgram.js
+++ b/src/renderers/webgl/WebGLProgram.js
@@ -497,6 +497,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 = [
@@ -884,6 +886,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;
@@ -1076,6 +1125,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 34f575bb174a5b..94f4c1a0d0eca3 100644
--- a/src/renderers/webgl/WebGLPrograms.js
+++ b/src/renderers/webgl/WebGLPrograms.js
@@ -110,6 +110,8 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
const currentRenderTarget = renderer.getRenderTarget();
const reverseDepthBuffer = renderer.state.buffers.depth.getReversed();
+ const numMultiviewViews = currentRenderTarget && currentRenderTarget.isWebGLMultiviewRenderTarget ? currentRenderTarget.numViews : 0;
+
const IS_INSTANCEDMESH = object.isInstancedMesh === true;
const IS_BATCHEDMESH = object.isBatchedMesh === true;
@@ -199,6 +201,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,
@@ -563,6 +566,8 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
_programLayers.enable( 20 );
if ( parameters.alphaToCoverage )
_programLayers.enable( 21 );
+ 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 b9050ccbc3b78d..453ee667eeae28 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).
@@ -529,8 +533,11 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
} else {
- uploadTexture( textureProperties, texture, slot );
- return;
+ if ( uploadTexture( textureProperties, texture, slot ) ) {
+
+ return;
+
+ }
}
@@ -739,8 +746,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;
@@ -1170,6 +1214,7 @@ function WebGLTextures( _gl, extensions, state, properties, capabilities, utils,
}
textureProperties.__version = texture.version;
+ return true;
}
@@ -1428,7 +1473,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 );
@@ -1442,13 +1491,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, textureProperties.__webglTexture, 0, getRenderTargetSamples( renderTarget ) );
+ if ( renderTarget.isWebGLMultiviewRenderTarget === true ) {
+
+ if ( multisampled ) {
+
+ multiviewExt.framebufferTextureMultisampleMultiviewOVR( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureProperties.__webglTexture, 0, getRenderTargetSamples( renderTarget ), 0, renderTarget.numViews );
+
+ } else {
+
+ multiviewExt.framebufferTextureMultiviewOVR( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureProperties.__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, textureProperties.__webglTexture, level );
+ if ( multisampled ) {
+
+ multisampledRTTExt.framebufferTexture2DMultisampleEXT( _gl.FRAMEBUFFER, attachment, textureTarget, textureProperties.__webglTexture, 0, getRenderTargetSamples( renderTarget ) );
+
+ } else {
+
+ _gl.framebufferTexture2D( _gl.FRAMEBUFFER, attachment, textureTarget, textureProperties.__webglTexture, level );
+
+ }
}
@@ -1461,7 +1528,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;
@@ -1552,37 +1671,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 = textureProperties.__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' );
+
+ }
}
@@ -1897,6 +2064,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 );
@@ -1926,9 +2099,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 );
}
@@ -2173,12 +2346,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 b82ea55e9babd3..cdc0df542059c8 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();
@@ -72,6 +73,7 @@ class WebXRManager extends EventDispatcher {
this.enabled = false;
this.isPresenting = false;
+ this.isMultiview = false;
this.getCameraPose = function ( ) {
@@ -328,12 +330,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 );
@@ -343,18 +353,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 );
+
+ }
}