Skip to content

Commit 2fe59c2

Browse files
committed
make XR renderingg more efficient
1 parent 722487e commit 2fe59c2

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

src/renderers/common/RenderContext.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ class RenderContext {
9797
*/
9898
this.clearStencilValue = 1;
9999

100+
/**
101+
* Whether the depth and stencil should be skipped during copy operations.
102+
*
103+
* @type {Boolean}
104+
* @default true
105+
*/
106+
this.skipDepthStencilCopy = false;
107+
100108
/**
101109
* By default the viewport encloses the entire framebuffer If a smaller
102110
* viewport is manually defined, this property is to `true` by the renderer.

src/renderers/common/Renderer.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { Matrix4 } from '../../math/Matrix4.js';
2626
import { Vector2 } from '../../math/Vector2.js';
2727
import { Vector4 } from '../../math/Vector4.js';
2828
import { RenderTarget } from '../../core/RenderTarget.js';
29-
import { DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoToneMapping, LinearFilter, LinearSRGBColorSpace, HalfFloatType, RGBAFormat, PCFShadowMap } from '../../constants.js';
29+
import { DoubleSide, BackSide, FrontSide, SRGBColorSpace, NoToneMapping, LinearFilter, LinearSRGBColorSpace, HalfFloatType, RGBAFormat, PCFShadowMap, UnsignedByteType } from '../../constants.js';
3030

3131
/** @module Renderer **/
3232

@@ -1128,7 +1128,7 @@ class Renderer {
11281128
frameBufferTarget = new RenderTarget( width, height, {
11291129
depthBuffer: depth,
11301130
stencilBuffer: stencil,
1131-
type: HalfFloatType, // FloatType
1131+
type: UnsignedByteType,
11321132
format: RGBAFormat,
11331133
colorSpace: LinearSRGBColorSpace,
11341134
generateMipmaps: false,
@@ -1255,6 +1255,8 @@ class Renderer {
12551255

12561256
}
12571257

1258+
if ( outputRenderTarget ) renderContext.skipDepthStencilCopy = outputRenderTarget.needsDepthTexture === false;
1259+
12581260
//
12591261

12601262
let viewport = this._viewport;
@@ -1321,6 +1323,14 @@ class Renderer {
13211323

13221324
const renderTargetData = this._textures.get( renderTarget );
13231325

1326+
// Do not attach a depth buffer if XR doesn't need one and we're using MSAA
1327+
if ( renderContext.skipDepthStencilCopy && ( renderContext.sampleCount > 1 ) ) {
1328+
1329+
renderTarget.depthBuffer = null;
1330+
renderTarget.stencilBuffer = null;
1331+
1332+
}
1333+
13241334
renderContext.textures = renderTargetData.textures;
13251335
renderContext.depthTexture = renderTargetData.depthTexture;
13261336
renderContext.width = renderTargetData.width;

src/renderers/common/XRManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ class XRManager extends EventDispatcher {
636636
} );
637637

638638
this._xrRenderTarget.hasExternalTextures = true;
639+
this._xrRenderTarget.needsDepthTexture = glBinding.usesDepthValues;
639640

640641
} else {
641642

src/renderers/common/XRRenderTarget.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ class XRRenderTarget extends RenderTarget {
3939
*/
4040
this.hasExternalTextures = false;
4141

42+
/**
43+
* Whether the system compositor needs depth textures.
44+
*
45+
* @type {Boolean}
46+
* @default true
47+
*/
48+
this.needsDepthTexture = true;
49+
4250
/**
4351
* Whether a depth buffer should automatically be allocated
4452
* for this XR render target or not.

src/renderers/webgl-fallback/WebGLBackend.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,7 @@ class WebGLBackend extends Backend {
19561956

19571957
}
19581958

1959-
if ( descriptor.depthTexture !== null ) {
1959+
if (( descriptor.depthTexture !== null ) && (!descriptor.skipDepthStencilCopy || ( samples === 0 ))) {
19601960

19611961
const textureData = this.get( descriptor.depthTexture );
19621962
const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
@@ -2044,7 +2044,12 @@ class WebGLBackend extends Backend {
20442044
renderTargetContextData.depthRenderbuffer = depthRenderbuffer;
20452045

20462046
const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
2047-
invalidationArray.push( depthStyle );
2047+
2048+
if ( invalidationArray.indexOf( depthStyle ) === -1 ) {
2049+
2050+
invalidationArray.push( depthStyle );
2051+
2052+
}
20482053

20492054
}
20502055

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class WebGLTextureUtils {
261261

262262
if ( glFormat === gl.DEPTH_COMPONENT ) {
263263

264-
if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.DEPTH24_STENCIL8;
264+
if ( glType === gl.UNSIGNED_INT ) internalFormat = gl.DEPTH_COMPONENT24;
265265
if ( glType === gl.FLOAT ) internalFormat = gl.DEPTH_COMPONENT32F;
266266

267267
}

0 commit comments

Comments
 (0)