Skip to content

Commit 9656d5e

Browse files
committed
support multi-pass with webxr multiview
1 parent 8d5eafc commit 9656d5e

File tree

11 files changed

+66
-56
lines changed

11 files changed

+66
-56
lines changed

src/core/RenderTarget.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class RenderTarget extends EventDispatcher {
7979
* @type {number}
8080
* @default 1
8181
*/
82-
this.depth = 1;
82+
this.depth = options.depth ? options.depth : 1;
8383

8484
/**
8585
* A rectangular area inside the render target's viewport. Fragments that are
@@ -107,7 +107,7 @@ class RenderTarget extends EventDispatcher {
107107
*/
108108
this.viewport = new Vector4( 0, 0, width, height );
109109

110-
const image = { width: width, height: height, depth: 1 };
110+
const image = { width: width, height: height, depth: this.depth };
111111

112112
options = Object.assign( {
113113
generateMipmaps: false,

src/renderers/common/Renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ class Renderer {
11921192

11931193
frameBufferTarget.depthBuffer = depth;
11941194
frameBufferTarget.stencilBuffer = stencil;
1195-
frameBufferTarget.setSize( width, height );
1195+
frameBufferTarget.setSize( width, height, this.getOutputRenderTarget() ? this.getOutputRenderTarget().depth : 1 );
11961196
frameBufferTarget.viewport.copy( this._viewport );
11971197
frameBufferTarget.scissor.copy( this._scissor );
11981198
frameBufferTarget.viewport.multiplyScalar( this._pixelRatio );

src/renderers/common/Textures.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import DataMap from './DataMap.js';
22

33
import { Vector3 } from '../../math/Vector3.js';
44
import { DepthTexture } from '../../textures/DepthTexture.js';
5+
import { DepthArrayTexture } from '../../textures/DepthArrayTexture.js';
56
import { DepthStencilFormat, DepthFormat, UnsignedIntType, UnsignedInt248Type, UnsignedByteType } from '../../constants.js';
67

78
const _size = /*@__PURE__*/ new Vector3();
@@ -76,11 +77,21 @@ class Textures extends DataMap {
7677

7778
if ( depthTexture === undefined && useDepthTexture ) {
7879

79-
depthTexture = new DepthTexture();
80+
if ( size.depth > 1 ) {
81+
82+
depthTexture = new DepthArrayTexture();
83+
84+
} else {
85+
86+
depthTexture = new DepthTexture();
87+
88+
}
89+
8090
depthTexture.format = renderTarget.stencilBuffer ? DepthStencilFormat : DepthFormat;
8191
depthTexture.type = renderTarget.stencilBuffer ? UnsignedInt248Type : UnsignedIntType; // FloatType
8292
depthTexture.image.width = mipWidth;
8393
depthTexture.image.height = mipHeight;
94+
depthTexture.image.depth = size.depth;
8495

8596
depthTextureMips[ activeMipmapLevel ] = depthTexture;
8697

@@ -136,6 +147,7 @@ class Textures extends DataMap {
136147

137148
const texture = textures[ i ];
138149

150+
texture.isTextureArray = size.depth > 1;
139151
if ( textureNeedsUpdate ) texture.needsUpdate = true;
140152

141153
this.updateTexture( texture, options );

src/renderers/common/XRManager.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class XRManager extends EventDispatcher {
3333
* Constructs a new XR manager.
3434
*
3535
* @param {Renderer} renderer - The renderer.
36+
* @param {boolean} enablemultiviewifpossible - enable multiview if device supports it.
3637
*/
3738
constructor( renderer, enablemultiviewifpossible = false ) {
3839

@@ -371,7 +372,7 @@ class XRManager extends EventDispatcher {
371372
* @type {boolean}
372373
* @readonly
373374
*/
374-
this._usesMultiview = false;
375+
this._usesMultiview = false;
375376

376377
}
377378

@@ -848,16 +849,16 @@ class XRManager extends EventDispatcher {
848849

849850
}
850851

851-
let projectionlayerInit = {
852+
const projectionlayerInit = {
852853
colorFormat: gl.RGBA8,
853854
depthFormat: glDepthFormat,
854855
scaleFactor: this._framebufferScaleFactor
855856
};
856857

857-
if ( this._useMultiviewIfPossible && renderer.hasFeature( 'OVR_multiview2') ) {
858+
if ( this._useMultiviewIfPossible && renderer.hasFeature( 'OVR_multiview2' ) ) {
858859

859860
projectionlayerInit.textureType = 'texture-array';
860-
this._usesMultiview = true;
861+
this._usesMultiview = true;
861862

862863
}
863864

@@ -897,10 +898,12 @@ class XRManager extends EventDispatcher {
897898
samples: attributes.antialias ? 4 : 0,
898899
resolveDepthBuffer: ( glProjLayer.ignoreDepthValues === false ),
899900
resolveStencilBuffer: ( glProjLayer.ignoreDepthValues === false ),
901+
depth: this._usesMultiview ? 2 : 1
900902
} );
901903

902904
this._xrRenderTarget.hasExternalTextures = true;
903905
this._xrRenderTarget.usesMultiview = this._usesMultiview;
906+
this._xrRenderTarget.depth = this._usesMultiview ? 2 : 1;
904907

905908
this._referenceSpace = await session.requestReferenceSpace( this.getReferenceSpaceType() );
906909

@@ -1313,6 +1316,7 @@ function onSessionEnd() {
13131316
//
13141317

13151318
this.isPresenting = false;
1319+
this._usesMultiview = false;
13161320

13171321
renderer._animation.stop();
13181322

@@ -1483,7 +1487,7 @@ function onAnimationFrame( time, frame ) {
14831487
backend.setXRRenderTargetTextures(
14841488
this._xrRenderTarget,
14851489
glSubImage.colorTexture,
1486-
( this._glProjLayer.ignoreDepthValues && !this._usesMultiview ) ? undefined : glSubImage.depthStencilTexture
1490+
( this._glProjLayer.ignoreDepthValues && ! this._usesMultiview ) ? undefined : glSubImage.depthStencilTexture
14871491
);
14881492

14891493
}

src/renderers/common/nodes/Nodes.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import ChainMap from '../ChainMap.js';
33
import NodeBuilderState from './NodeBuilderState.js';
44
import { cubeMapNode } from '../../../nodes/utils/CubeMapNode.js';
55
import { NodeFrame } from '../../../nodes/Nodes.js';
6-
import { objectGroup, renderGroup, frameGroup, cubeTexture, texture, fog, rangeFogFactor, densityFogFactor, reference, pmremTexture, screenUV } from '../../../nodes/TSL.js';
6+
import { objectGroup, renderGroup, frameGroup, cubeTexture, texture, texture3D, fog, rangeFogFactor, densityFogFactor, reference, pmremTexture, screenUV } from '../../../nodes/TSL.js';
7+
import { builtin } from '../../../nodes/accessors/BuiltinNode.js';
78

89
import { CubeUVReflectionMapping, EquirectangularReflectionMapping, EquirectangularRefractionMapping } from '../../../constants.js';
910
import { hashArray } from '../../../nodes/core/NodeUtils.js';
@@ -201,9 +202,12 @@ class Nodes extends DataMap {
201202
nodeBuilder.environmentNode = this.getEnvironmentNode( renderObject.scene );
202203
nodeBuilder.fogNode = this.getFogNode( renderObject.scene );
203204
nodeBuilder.clippingContext = renderObject.clippingContext;
204-
if (this.renderer.xr.usesMultiview()) {
205+
if ( this.renderer.xr.usesMultiview() ) {
206+
205207
nodeBuilder.enableMultiview();
208+
206209
}
210+
207211
nodeBuilder.build();
208212

209213
nodeBuilderState = this._createNodeBuilderState( nodeBuilder );
@@ -661,7 +665,7 @@ class Nodes extends DataMap {
661665

662666
const renderer = this.renderer;
663667

664-
return renderer.toneMapping + ',' + renderer.currentColorSpace;
668+
return renderer.toneMapping + ',' + renderer.currentColorSpace + ',' + renderer.xr.isPresenting;
665669

666670
}
667671

@@ -692,7 +696,11 @@ class Nodes extends DataMap {
692696
const renderer = this.renderer;
693697
const cacheKey = this.getOutputCacheKey();
694698

695-
const output = texture( outputTarget, screenUV ).renderOutput( renderer.toneMapping, renderer.currentColorSpace );
699+
//builtin( 'gl_ViewID_OVR' )??
700+
701+
const output = outputTarget.isTextureArray ?
702+
texture3D( outputTarget, screenUV ).renderOutput( renderer.toneMapping, renderer.currentColorSpace ) :
703+
texture( outputTarget, screenUV ).renderOutput( renderer.toneMapping, renderer.currentColorSpace );
696704

697705
_outputNodeMap.set( outputTarget, cacheKey );
698706

src/renderers/webgl-fallback/WebGLBackend.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class WebGLBackend extends Backend {
361361

362362
// The multisample_render_to_texture extension doesn't work properly if there
363363
// are midframe flushes and an external depth texture.
364-
if ( ( this.extensions.has( 'WEBGL_multisampled_render_to_texture' ) === true ) && renderTarget.autoAllocateDepthBuffer && !renderTarget.usesMultiview ) {
364+
if ( ( this.extensions.has( 'WEBGL_multisampled_render_to_texture' ) === true ) && renderTarget.autoAllocateDepthBuffer && ! renderTarget.usesMultiview ) {
365365

366366
console.warn( 'THREE.WebGLBackend: Render-to-texture extension was disabled because an external texture was provided' );
367367

@@ -1143,7 +1143,7 @@ class WebGLBackend extends Backend {
11431143

11441144
};
11451145

1146-
if ( renderObject.camera.isArrayCamera && renderObject.camera.cameras.length > 0 && !renderObject.camera.isMultiViewCamera) {
1146+
if ( renderObject.camera.isArrayCamera && renderObject.camera.cameras.length > 0 && ! renderObject.camera.isMultiViewCamera ) {
11471147

11481148
const cameraData = this.get( renderObject.camera );
11491149
const cameras = renderObject.camera.cameras;
@@ -2063,7 +2063,7 @@ class WebGLBackend extends Backend {
20632063

20642064
} else {
20652065

2066-
if ( hasExternalTextures && this.renderer.xr.usesMultiview() ) {
2066+
if ( this.renderer.xr.usesMultiview() ) {
20672067

20682068
multiviewExt.framebufferTextureMultisampleMultiviewOVR( gl.FRAMEBUFFER, attachment, textureData.textureGPU, 0, samples, 0, 2 );
20692069

@@ -2100,10 +2100,10 @@ class WebGLBackend extends Backend {
21002100
textureData.renderTarget = descriptor.renderTarget;
21012101
textureData.cacheKey = cacheKey; // required for copyTextureToTexture()
21022102

2103-
if ( hasExternalTextures && this.renderer.xr.usesMultiview() ) {
2103+
if ( this.renderer.xr.usesMultiview() ) {
21042104

21052105
multiviewExt.framebufferTextureMultisampleMultiviewOVR( gl.FRAMEBUFFER, depthStyle, textureData.textureGPU, 0, samples, 0, 2 );
2106-
2106+
21072107
} else if ( hasExternalTextures && useMultisampledRTT ) {
21082108

21092109
multisampledRTTExt.framebufferTexture2DMultisampleEXT( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0, samples );
@@ -2116,7 +2116,6 @@ class WebGLBackend extends Backend {
21162116

21172117
gl.framebufferTextureLayer( gl.FRAMEBUFFER, depthStyle, textureData.textureGPU, 0, layer );
21182118

2119-
21202119
} else {
21212120

21222121
gl.framebufferTexture2D( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0 );
@@ -2153,7 +2152,7 @@ class WebGLBackend extends Backend {
21532152

21542153
// rebind external XR textures
21552154

2156-
if ( isXRRenderTarget && hasExternalTextures ) {
2155+
if ( ( isXRRenderTarget && hasExternalTextures ) || this.renderer.xr.usesMultiview() ) {
21572156

21582157
state.bindFramebuffer( gl.FRAMEBUFFER, fb );
21592158

@@ -2192,7 +2191,7 @@ class WebGLBackend extends Backend {
21922191
if ( this.renderer.xr.usesMultiview() ) {
21932192

21942193
multiviewExt.framebufferTextureMultisampleMultiviewOVR( gl.FRAMEBUFFER, depthStyle, textureData.textureGPU, 0, samples, 0, 2 );
2195-
2194+
21962195
} else if ( useMultisampledRTT ) {
21972196

21982197
multisampledRTTExt.framebufferTexture2DMultisampleEXT( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0, samples );
@@ -2209,7 +2208,7 @@ class WebGLBackend extends Backend {
22092208

22102209
}
22112210

2212-
if ( samples > 0 && useMultisampledRTT === false && !this.renderer.xr.usesMultiview ) {
2211+
if ( samples > 0 && useMultisampledRTT === false && ! this.renderer.xr.usesMultiview ) {
22132212

22142213
if ( msaaFb === undefined ) {
22152214

src/renderers/webgl-fallback/nodes/GLSLNodeBuilder.js

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,7 @@ ${ flowData.code }
557557

558558
}
559559

560-
if ( uniform.type === 'texture3D' ) {
561-
562-
snippet = `${typePrefix}sampler3D ${ uniform.name };`;
563-
564-
} else if ( texture.compareFunction ) {
560+
if ( texture.compareFunction ) {
565561

566562
if ( texture.isDepthArrayTexture === true ) {
567563

@@ -573,10 +569,14 @@ ${ flowData.code }
573569

574570
}
575571

576-
} else if ( texture.isDataArrayTexture === true || texture.isCompressedArrayTexture === true ) {
572+
} else if ( texture.isDataArrayTexture === true || texture.isCompressedArrayTexture === true || texture.isTextureArray === true ) {
577573

578574
snippet = `${typePrefix}sampler2DArray ${ uniform.name };`;
579575

576+
} else if ( uniform.type === 'texture3D' ) {
577+
578+
snippet = `${typePrefix}sampler3D ${ uniform.name };`;
579+
580580
} else {
581581

582582
snippet = `${typePrefix}sampler2D ${ uniform.name };`;
@@ -946,17 +946,6 @@ ${ flowData.code }
946946

947947
}
948948

949-
/**
950-
* Returns the view id builtin for multiview.
951-
*
952-
* @return {string} The ViewID builtin.
953-
*/
954-
getViewID() {
955-
956-
return 'gl_ViewID_OVR';
957-
958-
}
959-
960949
/**
961950
* Enables the given extension.
962951
*
@@ -1112,7 +1101,7 @@ ${ flowData.code }
11121101
this.enableExtension( 'GL_OVR_multiview2', 'require', 'fragment' );
11131102
this.enableExtension( 'GL_OVR_multiview2', 'require', 'vertex' );
11141103

1115-
this.builtins[ 'vertex' ].push( `layout(num_views = 2) in` );
1104+
this.builtins[ 'vertex' ].push( 'layout(num_views = 2) in' );
11161105

11171106
}
11181107

src/renderers/webgl-fallback/utils/WebGLTextureUtils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class WebGLTextureUtils {
112112

113113
glTextureType = gl.TEXTURE_CUBE_MAP;
114114

115-
} else if ( texture.isDepthArrayTexture === true || texture.isDataArrayTexture === true || texture.isCompressedArrayTexture === true ) {
115+
} else if ( texture.isDepthArrayTexture === true || texture.isDataArrayTexture === true || texture.isCompressedArrayTexture === true || texture.isTextureArray === true ) {
116116

117117
glTextureType = gl.TEXTURE_2D_ARRAY;
118118

@@ -307,7 +307,7 @@ class WebGLTextureUtils {
307307
if ( textureType === gl.TEXTURE_3D || textureType === gl.TEXTURE_2D_ARRAY ) {
308308

309309
// WebGL 2 does not support wrapping for depth 2D array textures
310-
if ( ! texture.isDepthArrayTexture ) {
310+
if ( ! texture.isDepthArrayTexture && ! texture.isTextureArray ) {
311311

312312
gl.texParameteri( textureType, gl.TEXTURE_WRAP_R, wrappingToGL[ texture.wrapR ] );
313313

@@ -409,7 +409,7 @@ class WebGLTextureUtils {
409409

410410
this.setTextureParameters( glTextureType, texture );
411411

412-
if ( texture.isDepthArrayTexture || texture.isDataArrayTexture || texture.isCompressedArrayTexture ) {
412+
if ( texture.isDepthArrayTexture || texture.isDataArrayTexture || texture.isCompressedArrayTexture || texture.isTextureArray ) {
413413

414414
gl.texStorage3D( gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, width, height, depth );
415415

src/renderers/webgl/WebGLPrograms.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,6 @@ function WebGLPrograms( renderer, cubemaps, cubeuvmaps, extensions, capabilities
355355

356356
extensionClipCullDistance: HAS_EXTENSIONS && material.extensions.clipCullDistance === true && extensions.has( 'WEBGL_clip_cull_distance' ),
357357
extensionMultiDraw: ( HAS_EXTENSIONS && material.extensions.multiDraw === true || IS_BATCHEDMESH ) && extensions.has( 'WEBGL_multi_draw' ),
358-
// extensionMultiview: renderer.xr && renderer.xr.usesMultiview(),
359358

360359
rendererExtensionParallelShaderCompile: extensions.has( 'KHR_parallel_shader_compile' ),
361360

src/renderers/webgpu/nodes/WGSLNodeBuilder.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,17 +1188,6 @@ ${ flowData.code }
11881188

11891189
}
11901190

1191-
/**
1192-
* Overwritten as a NOP since this method is intended for the WebGL 2 backend.
1193-
*
1194-
* @return {null} Null.
1195-
*/
1196-
getViewID() {
1197-
1198-
return null;
1199-
1200-
}
1201-
12021191
/**
12031192
* Whether to flip texture data along its vertical axis or not.
12041193
*
@@ -1683,7 +1672,7 @@ ${ flowData.code }
16831672

16841673
textureType = 'texture_cube<f32>';
16851674

1686-
} else if ( texture.isDataArrayTexture === true || texture.isCompressedArrayTexture === true ) {
1675+
} else if ( texture.isDataArrayTexture === true || texture.isCompressedArrayTexture === true || texture.isTextureArray === true ) {
16871676

16881677
textureType = 'texture_2d_array<f32>';
16891678

0 commit comments

Comments
 (0)