Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPURenderer: Add Support for Renderer Without Depth and Stencil Buffers #29533

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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