Skip to content

Commit

Permalink
WebGPURenderer: Add Support for Renderer Without Depth and Stencil Bu…
Browse files Browse the repository at this point in the history
…ffers (#29533)

* WebGPURenderer: Support Renderer without Depth and Stencil buffers

* cleanup
  • Loading branch information
RenaudRohlinger authored Sep 30, 2024
1 parent 1841e38 commit d4ae99c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
35 changes: 27 additions & 8 deletions src/renderers/common/Textures.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ class Textures extends DataMap {
const mipHeight = size.height >> activeMipmapLevel;

let depthTexture = renderTarget.depthTexture || depthTextureMips[ activeMipmapLevel ];
const useDepthTexture = renderTarget.depthBuffer === true || renderTarget.stencilBuffer === true;

let textureNeedsUpdate = false;

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

depthTexture = new DepthTexture();
depthTexture.format = renderTarget.stencilBuffer ? DepthStencilFormat : DepthFormat;
Expand All @@ -50,25 +52,34 @@ class Textures extends DataMap {
if ( renderTargetData.width !== size.width || size.height !== renderTargetData.height ) {

textureNeedsUpdate = true;
depthTexture.needsUpdate = true;

depthTexture.image.width = mipWidth;
depthTexture.image.height = mipHeight;
if ( depthTexture ) {

depthTexture.needsUpdate = true;
depthTexture.image.width = mipWidth;
depthTexture.image.height = mipHeight;

}

}

renderTargetData.width = size.width;
renderTargetData.height = size.height;
renderTargetData.textures = textures;
renderTargetData.depthTexture = depthTexture;
renderTargetData.depthTexture = depthTexture || null;
renderTargetData.depth = renderTarget.depthBuffer;
renderTargetData.stencil = renderTarget.stencilBuffer;
renderTargetData.renderTarget = renderTarget;

if ( renderTargetData.sampleCount !== sampleCount ) {

textureNeedsUpdate = true;
depthTexture.needsUpdate = true;

if ( depthTexture ) {

depthTexture.needsUpdate = true;

}

renderTargetData.sampleCount = sampleCount;

Expand All @@ -88,7 +99,11 @@ class Textures extends DataMap {

}

this.updateTexture( depthTexture, options );
if ( depthTexture ) {

this.updateTexture( depthTexture, options );

}

// dispose handler

Expand All @@ -108,7 +123,11 @@ class Textures extends DataMap {

}

this._destroyTexture( depthTexture );
if ( depthTexture ) {

this._destroyTexture( depthTexture );

}

this.delete( renderTarget );

Expand Down
28 changes: 19 additions & 9 deletions src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,16 @@ class WebGPUBackend extends Backend {
colorAttachments: [ {
view: null
} ],
depthStencilAttachment: {
view: this.textureUtils.getDepthBuffer( renderer.depth, renderer.stencil ).createView()
}
};

if ( this.renderer.depth === true || this.renderer.stencil === true ) {

descriptor.depthStencilAttachment = {
view: this.textureUtils.getDepthBuffer( renderer.depth, renderer.stencil ).createView()
};

}

const colorAttachment = descriptor.colorAttachments[ 0 ];

if ( this.renderer.samples > 0 ) {
Expand Down Expand Up @@ -263,17 +268,22 @@ class WebGPUBackend extends Backend {

}

const depthTextureData = this.get( renderContext.depthTexture );

const depthStencilAttachment = {
view: depthTextureData.texture.createView()
};

descriptor = {
colorAttachments,
depthStencilAttachment
};

if ( renderContext.depth ) {

const depthTextureData = this.get( renderContext.depthTexture );

const depthStencilAttachment = {
view: depthTextureData.texture.createView()
};
descriptor.depthStencilAttachment = depthStencilAttachment;

}

descriptors[ cacheKey ] = descriptor;

renderTargetData.width = renderTarget.width;
Expand Down
38 changes: 29 additions & 9 deletions src/renderers/webgpu/utils/WebGPUPipelineUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,6 @@ class WebGPUPipelineUtils {
vertex: Object.assign( {}, vertexModule, { buffers: vertexBuffers } ),
fragment: Object.assign( {}, fragmentModule, { targets } ),
primitive: primitiveState,
depthStencil: {
format: depthStencilFormat,
depthWriteEnabled: material.depthWrite,
depthCompare: depthCompare,
stencilFront: stencilFront,
stencilBack: {}, // three.js does not provide an API to configure the back function (gl.stencilFuncSeparate() was never used)
stencilReadMask: material.stencilFuncMask,
stencilWriteMask: material.stencilWriteMask
},
multisample: {
count: sampleCount,
alphaToCoverageEnabled: material.alphaToCoverage && sampleCount > 1
Expand All @@ -145,6 +136,35 @@ class WebGPUPipelineUtils {
} )
};


const depthStencil = {};
const renderDepth = renderObject.context.depth;
const renderStencil = renderObject.context.stencil;

if ( renderDepth === true || renderStencil === true ) {

if ( renderDepth === true ) {

depthStencil.format = depthStencilFormat;
depthStencil.depthWriteEnabled = material.depthWrite;
depthStencil.depthCompare = depthCompare;

}

if ( renderStencil === true ) {

depthStencil.stencilFront = stencilFront;
depthStencil.stencilBack = {}; // three.js does not provide an API to configure the back function (gl.stencilFuncSeparate() was never used)
depthStencil.stencilReadMask = material.stencilFuncMask;
depthStencil.stencilWriteMask = material.stencilWriteMask;

}

pipelineDescriptor.depthStencil = depthStencil;

}


if ( promises === null ) {

pipelineData.pipeline = device.createRenderPipeline( pipelineDescriptor );
Expand Down

0 comments on commit d4ae99c

Please sign in to comment.