Skip to content

Commit

Permalink
XRManager: Fix foveation. (#30417)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 authored Jan 28, 2025
1 parent 99b02bb commit 089e36d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
13 changes: 2 additions & 11 deletions src/renderers/common/Textures.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,9 @@ class Textures extends DataMap {

const options = { sampleCount };

// when using the WebXR Layers API, the render target uses external textures which
// require no manual updates
// XR render targets require no texture updates

if ( renderTarget.isXRRenderTarget === true && renderTarget.hasExternalTextures === true ) {

if ( depthTexture && renderTarget.autoAllocateDepthBuffer === true ) {

this.updateTexture( depthTexture, options );

}

} else {
if ( renderTarget.isXRRenderTarget !== true ) {

for ( let i = 0; i < textures.length; i ++ ) {

Expand Down
2 changes: 1 addition & 1 deletion src/renderers/common/XRRenderTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class XRRenderTarget extends RenderTarget {
* Allocating a depth buffer is the default behavior of XR render
* targets. However, when using the WebXR Layers API, this flag
* must be set to `false` when the `ignoreDepthValues` property of
* the projection layers evaluates to `true`.
* the projection layers evaluates to `false`.
*
* Reference: {@link https://www.w3.org/TR/webxrlayers-1/#dom-xrprojectionlayer-ignoredepthvalues}.
*
Expand Down
36 changes: 26 additions & 10 deletions src/renderers/webgl-fallback/WebGLBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1963,14 +1963,24 @@ class WebGLBackend extends Backend {

}

if ( descriptor.depthTexture !== null ) {
if ( renderTarget.isXRRenderTarget && renderTarget.autoAllocateDepthBuffer === true ) {

const textureData = this.get( descriptor.depthTexture );
const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
textureData.renderTarget = descriptor.renderTarget;
textureData.cacheKey = cacheKey; // required for copyTextureToTexture()
const renderbuffer = gl.createRenderbuffer();
this.textureUtils.setupRenderBufferStorage( renderbuffer, descriptor, 0 );
renderTargetContextData.xrDepthRenderbuffer = renderbuffer;

} else {

gl.framebufferTexture2D( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0 );
if ( descriptor.depthTexture !== null ) {

const textureData = this.get( descriptor.depthTexture );
const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
textureData.renderTarget = descriptor.renderTarget;
textureData.cacheKey = cacheKey; // required for copyTextureToTexture()

gl.framebufferTexture2D( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0 );

}

}

Expand All @@ -1989,11 +1999,17 @@ class WebGLBackend extends Backend {

// rebind depth

if ( descriptor.depthTexture !== null ) {
const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;

const textureData = this.get( descriptor.depthTexture );
const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
if ( renderTarget.autoAllocateDepthBuffer === true ) {

const renderbuffer = renderTargetContextData.xrDepthRenderbuffer;
gl.bindRenderbuffer( gl.RENDERBUFFER, renderbuffer );
gl.framebufferRenderbuffer( gl.FRAMEBUFFER, depthStyle, gl.RENDERBUFFER, renderbuffer );

} else {

const textureData = this.get( descriptor.depthTexture );
gl.framebufferTexture2D( gl.FRAMEBUFFER, depthStyle, gl.TEXTURE_2D, textureData.textureGPU, 0 );

}
Expand Down Expand Up @@ -2046,7 +2062,7 @@ class WebGLBackend extends Backend {
if ( depthRenderbuffer === undefined ) {

depthRenderbuffer = gl.createRenderbuffer();
this.textureUtils.setupRenderBufferStorage( depthRenderbuffer, descriptor );
this.textureUtils.setupRenderBufferStorage( depthRenderbuffer, descriptor, samples );

renderTargetContextData.depthRenderbuffer = depthRenderbuffer;

Expand Down
5 changes: 3 additions & 2 deletions src/renderers/webgl-fallback/utils/WebGLTextureUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -917,13 +917,14 @@ class WebGLTextureUtils {
*
* @param {WebGLRenderbuffer} renderbuffer - The render buffer.
* @param {RenderContext} renderContext - The render context.
* @param {Number} samples - The MSAA sample count.
*/
setupRenderBufferStorage( renderbuffer, renderContext ) {
setupRenderBufferStorage( renderbuffer, renderContext, samples ) {

const { gl } = this;
const renderTarget = renderContext.renderTarget;

const { samples, depthTexture, depthBuffer, stencilBuffer, width, height } = renderTarget;
const { depthTexture, depthBuffer, stencilBuffer, width, height } = renderTarget;

gl.bindRenderbuffer( gl.RENDERBUFFER, renderbuffer );

Expand Down

0 comments on commit 089e36d

Please sign in to comment.