From 7d1cb3ee85bb60aae96c754c758c824d92a3f1ea Mon Sep 17 00:00:00 2001 From: Jeremy Nelson Date: Mon, 16 Jun 2025 21:08:46 -0700 Subject: [PATCH] add requestedDepth --- filament/backend/src/webgpu/WebGPUDriver.cpp | 11 +++---- .../src/webgpu/WebGPUPipelineCreation.cpp | 33 ++++++++++++------- .../src/webgpu/WebGPUPipelineCreation.h | 4 +-- .../backend/src/webgpu/WebGPURenderTarget.cpp | 5 +-- .../backend/src/webgpu/WebGPURenderTarget.h | 4 +-- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/filament/backend/src/webgpu/WebGPUDriver.cpp b/filament/backend/src/webgpu/WebGPUDriver.cpp index 89c20c5f5d..dd41b536e8 100644 --- a/filament/backend/src/webgpu/WebGPUDriver.cpp +++ b/filament/backend/src/webgpu/WebGPUDriver.cpp @@ -927,13 +927,10 @@ void WebGPUDriver::beginRenderPass(Handle renderTargetHandle, params, defaultColorView, defaultDepthStencilView, - defaultDepthStencilFormat, customColorViews.data(), customColorViewCount, customDepthStencilView, - customDepthStencilView, - customDepthStencilFormat, - customDepthStencilFormat); + customDepthStencilView); mRenderPassEncoder = mCommandEncoder.BeginRenderPass(&renderPassDescriptor); @@ -1124,7 +1121,9 @@ void WebGPUDriver::bindPipeline(PipelineState const& pipelineState) { std::vector pipelineColorFormats; wgpu::TextureFormat pipelineDepthStencilFormat = wgpu::TextureFormat::Undefined; uint8_t pipelineSamples = 1; - bool requestedStencil = false; + bool requestedDepth = any(mCurrentRenderTarget->getTargetFlags() & TargetBufferFlags::DEPTH); + bool requestedStencil = any(mCurrentRenderTarget->getTargetFlags() & TargetBufferFlags::STENCIL); + pipelineSamples = mCurrentRenderTarget->getSamples(); if (mCurrentRenderTarget->isDefaultRenderTarget()) { pipelineColorFormats.push_back(mSwapChain->getColorFormat()); @@ -1170,7 +1169,7 @@ void WebGPUDriver::bindPipeline(PipelineState const& pipelineState) { wgpu::RenderPipeline pipeline = createWebGPURenderPipeline(mDevice, *program, *vertexBufferInfo, layout, pipelineState.rasterState, pipelineState.stencilState, pipelineState.polygonOffset, pipelineState.primitiveType, pipelineColorFormats, - pipelineDepthStencilFormat, pipelineSamples, requestedStencil); + pipelineDepthStencilFormat, pipelineSamples, requestedDepth, requestedStencil); assert_invariant(pipeline); mPipelineMap[hash] = pipeline; mRenderPassEncoder.SetPipeline(pipeline); diff --git a/filament/backend/src/webgpu/WebGPUPipelineCreation.cpp b/filament/backend/src/webgpu/WebGPUPipelineCreation.cpp index 90345d5fd8..cfeeb75faf 100644 --- a/filament/backend/src/webgpu/WebGPUPipelineCreation.cpp +++ b/filament/backend/src/webgpu/WebGPUPipelineCreation.cpp @@ -169,20 +169,33 @@ wgpu::RenderPipeline createWebGPURenderPipeline(wgpu::Device const& device, wgpu::PipelineLayout const& layout, RasterState const& rasterState, StencilState const& stencilState, PolygonOffset const& polygonOffset, const PrimitiveType primitiveType, std::vector const& colorFormats, - const wgpu::TextureFormat depthStencilFormat, const uint8_t samplesCount, bool requestedStencil) { + wgpu::TextureFormat const& depthStencilFormat, const uint8_t samplesCount, bool requestedDepth, bool requestedStencil) { assert_invariant(program.vertexShaderModule); wgpu::DepthStencilState depthStencilState{}; + const bool depthOrStencilRequested = (requestedDepth || requestedStencil); - if (depthStencilFormat != wgpu::TextureFormat::Undefined) { + if (depthOrStencilRequested) { + FILAMENT_CHECK_PRECONDITION(depthStencilFormat != wgpu::TextureFormat::Undefined) + << "Depth or Stencil requested for pipeline, but depthStencilFormat is " + "wgpu::TextureFormat::Undefined."; depthStencilState.format = depthStencilFormat; - depthStencilState.depthWriteEnabled = rasterState.depthWrite; - depthStencilState.depthCompare = toWebGPU(rasterState.depthFunc); - if (polygonOffset.constant != 0.0f || polygonOffset.slope != 0.0f) { - depthStencilState.depthBias = static_cast(polygonOffset.constant); - depthStencilState.depthBiasSlopeScale = polygonOffset.slope; - depthStencilState.depthBiasClamp = 0.0f; + if (requestedDepth) { + depthStencilState.depthWriteEnabled = rasterState.depthWrite; + depthStencilState.depthCompare = toWebGPU(rasterState.depthFunc); + + if (polygonOffset.constant != 0.0f || polygonOffset.slope != 0.0f) { + depthStencilState.depthBias = static_cast(polygonOffset.constant); + depthStencilState.depthBiasSlopeScale = polygonOffset.slope; + depthStencilState.depthBiasClamp = 0.0f; + } else { + depthStencilState.depthBias = 0; + depthStencilState.depthBiasSlopeScale = 0.0f; + depthStencilState.depthBiasClamp = 0.0f; + } } else { + depthStencilState.depthWriteEnabled = false; + depthStencilState.depthCompare = wgpu::CompareFunction::Always; depthStencilState.depthBias = 0; depthStencilState.depthBiasSlopeScale = 0.0f; depthStencilState.depthBiasClamp = 0.0f; @@ -208,9 +221,7 @@ wgpu::RenderPipeline createWebGPURenderPipeline(wgpu::Device const& device, depthStencilState.stencilFront.failOp = wgpu::StencilOperation::Keep; depthStencilState.stencilFront.depthFailOp = wgpu::StencilOperation::Keep; depthStencilState.stencilFront.passOp = wgpu::StencilOperation::Keep; - depthStencilState.stencilBack = depthStencilState.stencilFront; - depthStencilState.stencilReadMask = 0; depthStencilState.stencilWriteMask = 0; } @@ -254,7 +265,7 @@ wgpu::RenderPipeline createWebGPURenderPipeline(wgpu::Device const& device, .unclippedDepth = !rasterState.depthClamp && device.HasFeature(wgpu::FeatureName::DepthClipControl) }, - .depthStencil = depthStencilFormat != wgpu::TextureFormat::Undefined ? &depthStencilState: nullptr, + .depthStencil = depthOrStencilRequested ? &depthStencilState: nullptr, .multisample = { .count = samplesCount, .mask = 0xFFFFFFFF, diff --git a/filament/backend/src/webgpu/WebGPUPipelineCreation.h b/filament/backend/src/webgpu/WebGPUPipelineCreation.h index 987c8fb233..8751b8424c 100644 --- a/filament/backend/src/webgpu/WebGPUPipelineCreation.h +++ b/filament/backend/src/webgpu/WebGPUPipelineCreation.h @@ -40,8 +40,8 @@ class WebGPUProgram; [[nodiscard]] wgpu::RenderPipeline createWebGPURenderPipeline(wgpu::Device const&, WebGPUProgram const&, WebGPUVertexBufferInfo const&, wgpu::PipelineLayout const&, RasterState const&, StencilState const&, PolygonOffset const&, PrimitiveType primitiveType, - std::vector const& colorFormats, wgpu::TextureFormat depthFormat, - uint8_t samplesCount, bool requestedStencil); + std::vector const& colorFormats, wgpu::TextureFormat const& depthStencilFormat, + uint8_t samplesCount, bool requestedDepth, bool requestedStencil); }// namespace filament::backend diff --git a/filament/backend/src/webgpu/WebGPURenderTarget.cpp b/filament/backend/src/webgpu/WebGPURenderTarget.cpp index 598bc98e22..c749189706 100644 --- a/filament/backend/src/webgpu/WebGPURenderTarget.cpp +++ b/filament/backend/src/webgpu/WebGPURenderTarget.cpp @@ -76,12 +76,9 @@ wgpu::StoreOp WebGPURenderTarget::getStoreOperation(RenderPassParams const& para void WebGPURenderTarget::setUpRenderPassAttachments(wgpu::RenderPassDescriptor& outDescriptor, RenderPassParams const& params, wgpu::TextureView const& defaultColorTextureView, wgpu::TextureView const& defaultDepthStencilTextureView, - wgpu::TextureFormat const& defaultDepthStencilFormat, wgpu::TextureView const* customColorTextureViews, uint32_t customColorTextureViewCount, wgpu::TextureView const& customDepthTextureView, - wgpu::TextureView const& customStencilTextureView, - const wgpu::TextureFormat customDepthFormat, - const wgpu::TextureFormat customStencilFormat) { + wgpu::TextureView const& customStencilTextureView) { mColorAttachmentDescriptors.clear(); const bool hasDepth = any(mTargetFlags & TargetBufferFlags::DEPTH); diff --git a/filament/backend/src/webgpu/WebGPURenderTarget.h b/filament/backend/src/webgpu/WebGPURenderTarget.h index 8b5aa3f7a9..8fb4b2d39f 100644 --- a/filament/backend/src/webgpu/WebGPURenderTarget.h +++ b/filament/backend/src/webgpu/WebGPURenderTarget.h @@ -45,13 +45,11 @@ public: // For default render target: wgpu::TextureView const& defaultColorTextureView, wgpu::TextureView const& defaultDepthStencilTextureView, - wgpu::TextureFormat const& defaultDepthStencilFormat, // For custom render targets: wgpu::TextureView const* customColorTextureViews, // Array of views uint32_t customColorTextureViewCount, wgpu::TextureView const& customDepthTextureView, - wgpu::TextureView const& customStencilTextureView, - wgpu::TextureFormat customDepthFormat, wgpu::TextureFormat customStencilFormat); + wgpu::TextureView const& customStencilTextureView); [[nodiscard]] bool isDefaultRenderTarget() const { return mDefaultRenderTarget; } [[nodiscard]] uint8_t getSamples() const { return mSamples; }