From 92a0d7bfc3d86454ab2ab34702e77f2c09d260e7 Mon Sep 17 00:00:00 2001 From: Andy Hovingh <6198728+AndyHovingh@users.noreply.github.com> Date: Sat, 7 Jun 2025 18:29:50 -0500 Subject: [PATCH] webgpu: refactor: move WebGPU render target class to its own source+header files. --- filament/backend/CMakeLists.txt | 4 +- filament/backend/src/webgpu/WebGPUDriver.cpp | 44 ++++++----- filament/backend/src/webgpu/WebGPUDriver.h | 6 +- .../src/webgpu/WebGPUPipelineCreation.cpp | 2 +- ...bGPUHandles.cpp => WebGPURenderTarget.cpp} | 77 ++++++++++--------- .../{WebGPUHandles.h => WebGPURenderTarget.h} | 33 ++++---- 6 files changed, 87 insertions(+), 79 deletions(-) rename filament/backend/src/webgpu/{WebGPUHandles.cpp => WebGPURenderTarget.cpp} (70%) rename filament/backend/src/webgpu/{WebGPUHandles.h => WebGPURenderTarget.h} (72%) diff --git a/filament/backend/CMakeLists.txt b/filament/backend/CMakeLists.txt index 99688836ab..f07e546882 100644 --- a/filament/backend/CMakeLists.txt +++ b/filament/backend/CMakeLists.txt @@ -270,8 +270,8 @@ if (FILAMENT_SUPPORTS_WEBGPU) src/webgpu/WebGPUDriver.h src/webgpu/WebGPUIndexBuffer.cpp src/webgpu/WebGPUIndexBuffer.h - src/webgpu/WebGPUHandles.cpp - src/webgpu/WebGPUHandles.h + src/webgpu/WebGPURenderTarget.cpp + src/webgpu/WebGPURenderTarget.h src/webgpu/WebGPUPipelineCreation.cpp src/webgpu/WebGPUPipelineCreation.h src/webgpu/WebGPUStrings.h diff --git a/filament/backend/src/webgpu/WebGPUDriver.cpp b/filament/backend/src/webgpu/WebGPUDriver.cpp index 3aa71f0b63..c98392d5ab 100644 --- a/filament/backend/src/webgpu/WebGPUDriver.cpp +++ b/filament/backend/src/webgpu/WebGPUDriver.cpp @@ -18,11 +18,11 @@ #include "WebGPUBufferObject.h" #include "WebGPUDescriptorSet.h" #include "WebGPUDescriptorSetLayout.h" -#include "WebGPUHandles.h" #include "WebGPUIndexBuffer.h" #include "WebGPUPipelineCreation.h" #include "WebGPUProgram.h" #include "WebGPURenderPrimitive.h" +#include "WebGPURenderTarget.h" #include "WebGPUSwapChain.h" #include "WebGPUTexture.h" #include "WebGPUVertexBuffer.h" @@ -185,20 +185,20 @@ void WebGPUDriver::destroyProgram(Handle programHandle) { } } -void WebGPUDriver::destroyRenderTarget(Handle rth) { - if (rth) { - WGPURenderTarget* rt = handleCast(rth); - if (rt == mDefaultRenderTarget) { +void WebGPUDriver::destroyRenderTarget(Handle renderTargetHandle) { + if (renderTargetHandle) { + auto renderTarget = handleCast(renderTargetHandle); + if (renderTarget == mDefaultRenderTarget) { mDefaultRenderTarget = nullptr; } - if (rt == mCurrentRenderTarget) { + if (renderTarget == mCurrentRenderTarget) { mCurrentRenderTarget = nullptr; } // WGPURenderTarget destructor is trivial. // The HwTexture handles stored within WGPURenderTarget (via MRT, TargetBufferInfo) // are not owned by WGPURenderTarget, so they are not destroyed here. // They are destroyed via WebGPUDriver::destroyTexture. - destructHandle(rth); + destructHandle(renderTargetHandle); } } @@ -281,7 +281,7 @@ Handle WebGPUDriver::createBufferObjectS() noexcept { } Handle WebGPUDriver::createRenderTargetS() noexcept { - return allocHandle(); + return allocHandle(); } Handle WebGPUDriver::createVertexBufferS() noexcept { @@ -305,7 +305,7 @@ Handle WebGPUDriver::createTextureViewSwizzleS() noexcept { } Handle WebGPUDriver::createDefaultRenderTargetS() noexcept { - return allocHandle(); + return allocHandle(); } Handle WebGPUDriver::createDescriptorSetLayoutS() noexcept { @@ -440,16 +440,19 @@ void WebGPUDriver::createProgramR(Handle programHandle, Program&& pro constructHandle(programHandle, mDevice, program); } -void WebGPUDriver::createDefaultRenderTargetR(Handle rth, int) { +void WebGPUDriver::createDefaultRenderTargetR(Handle renderTargetHandle, + const int /* dummy */) { assert_invariant(!mDefaultRenderTarget); - mDefaultRenderTarget = constructHandle(rth); + mDefaultRenderTarget = constructHandle(renderTargetHandle); assert_invariant(mDefaultRenderTarget); } -void WebGPUDriver::createRenderTargetR(Handle rth, TargetBufferFlags targets, - uint32_t width, uint32_t height, uint8_t samples, uint8_t layerCount, MRT color, - TargetBufferInfo depth, TargetBufferInfo stencil) { - constructHandle(rth, width, height, samples, layerCount, color, depth, stencil); +void WebGPUDriver::createRenderTargetR(Handle renderTargetHandle, + const TargetBufferFlags targets, const uint32_t width, const uint32_t height, + const uint8_t samples, const uint8_t layerCount, const MRT color, + const TargetBufferInfo depth, const TargetBufferInfo stencil) { + constructHandle(renderTargetHandle, width, height, samples, layerCount, + color, depth, stencil); } void WebGPUDriver::createFenceR(Handle fh, int) { @@ -800,7 +803,7 @@ void WebGPUDriver::compilePrograms(CompilerPriorityQueue priority, void WebGPUDriver::beginRenderPass(Handle renderTargetHandle, RenderPassParams const& params) { assert_invariant(mCommandEncoder); - auto renderTarget = handleCast(renderTargetHandle); + auto renderTarget = handleCast(renderTargetHandle); wgpu::TextureView defaultColorView = nullptr; wgpu::TextureView defaultDepthStencilView = nullptr; @@ -966,7 +969,7 @@ void WebGPUDriver::stopCapture(int) { //todo } -void WebGPUDriver::readPixels(Handle src, const uint32_t x, +void WebGPUDriver::readPixels(Handle sourceRenderTargetHandle, const uint32_t x, const uint32_t y, const uint32_t width, const uint32_t height, PixelBufferDescriptor&& pixelBufferDescriptor) { // todo @@ -980,10 +983,9 @@ void WebGPUDriver::readBufferSubData(Handle bufferObjectHandle, } void WebGPUDriver::blitDEPRECATED(TargetBufferFlags buffers, - Handle dst, Viewport dstRect, - Handle src, Viewport srcRect, - SamplerMagFilter filter) { -} + Handle destinationRenderTargetHandle, const Viewport destinationViewport, + Handle sourceRenderTargetHandle, const Viewport sourceViewport, + const SamplerMagFilter filter) {} void WebGPUDriver::resolve(Handle destinationTextureHandle, const uint8_t sourceLevel, const uint8_t sourceLayer, Handle sourceTextureHandle, diff --git a/filament/backend/src/webgpu/WebGPUDriver.h b/filament/backend/src/webgpu/WebGPUDriver.h index 71ed19d23d..c218ba311d 100644 --- a/filament/backend/src/webgpu/WebGPUDriver.h +++ b/filament/backend/src/webgpu/WebGPUDriver.h @@ -17,7 +17,7 @@ #ifndef TNT_FILAMENT_BACKEND_WEBGPUDRIVER_H #define TNT_FILAMENT_BACKEND_WEBGPUDRIVER_H -#include "WebGPUHandles.h" +#include "WebGPURenderTarget.h" #include "webgpu/WebGPUConstants.h" #include @@ -74,8 +74,8 @@ private: wgpu::TextureView mTextureView = nullptr; wgpu::RenderPassEncoder mRenderPassEncoder = nullptr; wgpu::CommandBuffer mCommandBuffer = nullptr; - WGPURenderTarget* mDefaultRenderTarget = nullptr; - WGPURenderTarget* mCurrentRenderTarget = nullptr; + WebGPURenderTarget* mDefaultRenderTarget = nullptr; + WebGPURenderTarget* mCurrentRenderTarget = nullptr; tsl::robin_map mPipelineMap; diff --git a/filament/backend/src/webgpu/WebGPUPipelineCreation.cpp b/filament/backend/src/webgpu/WebGPUPipelineCreation.cpp index 1c4d045a0a..bf57006882 100644 --- a/filament/backend/src/webgpu/WebGPUPipelineCreation.cpp +++ b/filament/backend/src/webgpu/WebGPUPipelineCreation.cpp @@ -16,8 +16,8 @@ #include "WebGPUPipelineCreation.h" -#include "WebGPUHandles.h" #include "WebGPUProgram.h" +#include "WebGPURenderTarget.h" #include "WebGPUVertexBufferInfo.h" #include diff --git a/filament/backend/src/webgpu/WebGPUHandles.cpp b/filament/backend/src/webgpu/WebGPURenderTarget.cpp similarity index 70% rename from filament/backend/src/webgpu/WebGPUHandles.cpp rename to filament/backend/src/webgpu/WebGPURenderTarget.cpp index 7487b09fe8..6582a6861f 100644 --- a/filament/backend/src/webgpu/WebGPUHandles.cpp +++ b/filament/backend/src/webgpu/WebGPURenderTarget.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "WebGPUHandles.h" +#include "WebGPURenderTarget.h" #include #include @@ -30,23 +30,29 @@ namespace filament::backend { -WGPURenderTarget::WGPURenderTarget(uint32_t width, uint32_t height, uint8_t samples, uint8_t layerCount, - const MRT& colorAttachmentsMRT, - const Attachment& depthAttachmentInfo, - const Attachment& stencilAttachmentInfo) - : HwRenderTarget(width, height), - mDefaultRenderTarget(false), - mSamples(samples), - mLayerCount(layerCount), - mColorAttachments(colorAttachmentsMRT), - mDepthAttachment(depthAttachmentInfo), - mStencilAttachment(stencilAttachmentInfo) { - // TODO Make this an array +WebGPURenderTarget::WebGPURenderTarget(const uint32_t width, const uint32_t height, + const uint8_t samples, const uint8_t layerCount, MRT const& colorAttachmentsMRT, + Attachment const& depthAttachmentInfo, Attachment const& stencilAttachmentInfo) + : HwRenderTarget{ width, height }, + mDefaultRenderTarget{ false }, + mSamples{ samples }, + mLayerCount{ layerCount }, + mColorAttachments{ colorAttachmentsMRT }, + mDepthAttachment{ depthAttachmentInfo }, + mStencilAttachment{ stencilAttachmentInfo } { + // TODO consider making this an array mColorAttachmentDescriptors.reserve(MRT::MAX_SUPPORTED_RENDER_TARGET_COUNT); } -wgpu::LoadOp WGPURenderTarget::getLoadOperation(RenderPassParams const& params, - TargetBufferFlags bufferToOperateOn) { +// Default constructor for the default render target +WebGPURenderTarget::WebGPURenderTarget() + : HwRenderTarget{ 0, 0 }, + mDefaultRenderTarget{ true }, + mSamples{ 1 }, + mLayerCount{ 1 } {} + +wgpu::LoadOp WebGPURenderTarget::getLoadOperation(RenderPassParams const& params, + const TargetBufferFlags bufferToOperateOn) { if (any(params.flags.clear & bufferToOperateOn)) { return wgpu::LoadOp::Clear; } @@ -56,21 +62,22 @@ wgpu::LoadOp WGPURenderTarget::getLoadOperation(RenderPassParams const& params, return wgpu::LoadOp::Load; } -wgpu::StoreOp WGPURenderTarget::getStoreOperation(RenderPassParams const& params, - TargetBufferFlags bufferToOperateOn) { +wgpu::StoreOp WebGPURenderTarget::getStoreOperation(RenderPassParams const& params, + const TargetBufferFlags bufferToOperateOn) { if (any(params.flags.discardEnd & bufferToOperateOn)) { return wgpu::StoreOp::Discard; } return wgpu::StoreOp::Store; } -void WGPURenderTarget::setUpRenderPassAttachments(wgpu::RenderPassDescriptor& descriptor, +void WebGPURenderTarget::setUpRenderPassAttachments(wgpu::RenderPassDescriptor& outDescriptor, RenderPassParams const& params, wgpu::TextureView const& defaultColorTextureView, wgpu::TextureView const& defaultDepthStencilTextureView, wgpu::TextureView const* customColorTextureViews, uint32_t customColorTextureViewCount, wgpu::TextureView const& customDepthTextureView, - wgpu::TextureView const& customStencilTextureView, wgpu::TextureFormat customDepthFormat, - wgpu::TextureFormat customStencilFormat) { + wgpu::TextureView const& customStencilTextureView, + const wgpu::TextureFormat customDepthFormat, + const wgpu::TextureFormat customStencilFormat) { mColorAttachmentDescriptors.clear(); mHasDepthStencilAttachment = false; @@ -78,24 +85,24 @@ void WGPURenderTarget::setUpRenderPassAttachments(wgpu::RenderPassDescriptor& de assert_invariant(defaultColorTextureView); mColorAttachmentDescriptors.push_back({ .view = defaultColorTextureView, .resolveTarget = nullptr, - .loadOp = WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::COLOR0), - .storeOp = WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::COLOR0), + .loadOp = WebGPURenderTarget::getLoadOperation(params, TargetBufferFlags::COLOR0), + .storeOp = WebGPURenderTarget::getStoreOperation(params, TargetBufferFlags::COLOR0), .clearValue = { params.clearColor.r, params.clearColor.g, params.clearColor.b, params.clearColor.a } }); if (defaultDepthStencilTextureView) { mDepthStencilAttachmentDescriptor = { .view = defaultDepthStencilTextureView, - .depthLoadOp = WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::DEPTH), + .depthLoadOp = WebGPURenderTarget::getLoadOperation(params, TargetBufferFlags::DEPTH), .depthStoreOp = - WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::DEPTH), + WebGPURenderTarget::getStoreOperation(params, TargetBufferFlags::DEPTH), .depthClearValue = static_cast(params.clearDepth), .depthReadOnly = (params.readOnlyDepthStencil & RenderPassParams::READONLY_DEPTH) > 0, .stencilLoadOp = - WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::STENCIL), + WebGPURenderTarget::getLoadOperation(params, TargetBufferFlags::STENCIL), .stencilStoreOp = - WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::STENCIL), + WebGPURenderTarget::getStoreOperation(params, TargetBufferFlags::STENCIL), .stencilClearValue = params.clearStencil, .stencilReadOnly = (params.readOnlyDepthStencil & RenderPassParams::READONLY_STENCIL) > 0, @@ -107,9 +114,9 @@ void WGPURenderTarget::setUpRenderPassAttachments(wgpu::RenderPassDescriptor& de if (customColorTextureViews[i]) { mColorAttachmentDescriptors.push_back({ .view = customColorTextureViews[i], // .resolveTarget = nullptr; // TODO: MSAA resolve for custom RT - .loadOp = WGPURenderTarget::getLoadOperation(params, getTargetBufferFlagsAt(i)), + .loadOp = WebGPURenderTarget::getLoadOperation(params, getTargetBufferFlagsAt(i)), .storeOp = - WGPURenderTarget::getStoreOperation(params, getTargetBufferFlagsAt(i)), + WebGPURenderTarget::getStoreOperation(params, getTargetBufferFlagsAt(i)), .clearValue = { .r = params.clearColor.r, .g = params.clearColor.g, .b = params.clearColor.b, @@ -140,9 +147,9 @@ void WGPURenderTarget::setUpRenderPassAttachments(wgpu::RenderPassDescriptor& de if (hasDepth) { mDepthStencilAttachmentDescriptor.depthLoadOp = - WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::DEPTH); + WebGPURenderTarget::getLoadOperation(params, TargetBufferFlags::DEPTH); mDepthStencilAttachmentDescriptor.depthStoreOp = - WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::DEPTH); + WebGPURenderTarget::getStoreOperation(params, TargetBufferFlags::DEPTH); mDepthStencilAttachmentDescriptor.depthClearValue = static_cast(params.clearDepth); mDepthStencilAttachmentDescriptor.depthReadOnly = @@ -155,9 +162,9 @@ void WGPURenderTarget::setUpRenderPassAttachments(wgpu::RenderPassDescriptor& de if (hasStencil) { mDepthStencilAttachmentDescriptor.stencilLoadOp = - WGPURenderTarget::getLoadOperation(params, TargetBufferFlags::STENCIL); + WebGPURenderTarget::getLoadOperation(params, TargetBufferFlags::STENCIL); mDepthStencilAttachmentDescriptor.stencilStoreOp = - WGPURenderTarget::getStoreOperation(params, TargetBufferFlags::STENCIL); + WebGPURenderTarget::getStoreOperation(params, TargetBufferFlags::STENCIL); mDepthStencilAttachmentDescriptor.stencilClearValue = params.clearStencil; mDepthStencilAttachmentDescriptor.stencilReadOnly = (params.readOnlyDepthStencil & RenderPassParams::READONLY_STENCIL) > 0; @@ -170,9 +177,9 @@ void WGPURenderTarget::setUpRenderPassAttachments(wgpu::RenderPassDescriptor& de } } - descriptor.colorAttachmentCount = mColorAttachmentDescriptors.size(); - descriptor.colorAttachments = mColorAttachmentDescriptors.data(); - descriptor.depthStencilAttachment = + outDescriptor.colorAttachmentCount = mColorAttachmentDescriptors.size(); + outDescriptor.colorAttachments = mColorAttachmentDescriptors.data(); + outDescriptor.depthStencilAttachment = mHasDepthStencilAttachment ? &mDepthStencilAttachmentDescriptor : nullptr; // descriptor.sampleCount was removed from the core spec. If your webgpu.h still has it, diff --git a/filament/backend/src/webgpu/WebGPUHandles.h b/filament/backend/src/webgpu/WebGPURenderTarget.h similarity index 72% rename from filament/backend/src/webgpu/WebGPUHandles.h rename to filament/backend/src/webgpu/WebGPURenderTarget.h index 3a0677fcbb..f351026ef9 100644 --- a/filament/backend/src/webgpu/WebGPUHandles.h +++ b/filament/backend/src/webgpu/WebGPURenderTarget.h @@ -28,24 +28,20 @@ namespace filament::backend { -class WGPURenderTarget : public HwRenderTarget { +class WebGPURenderTarget : public HwRenderTarget { public: using Attachment = TargetBufferInfo; // Using TargetBufferInfo directly for attachments - WGPURenderTarget(uint32_t width, uint32_t height, uint8_t samples, uint8_t layerCount, - const MRT& colorAttachments, - const Attachment& depthAttachment, - const Attachment& stencilAttachment); + WebGPURenderTarget(uint32_t width, uint32_t height, uint8_t samples, uint8_t layerCount, + MRT const& colorAttachments, Attachment const& depthAttachment, + Attachment const& stencilAttachment); // Default constructor for the default render target - WGPURenderTarget() - : HwRenderTarget(0, 0), - mDefaultRenderTarget(true), - mSamples(1) {} + WebGPURenderTarget(); // Updated signature: takes resolved views for custom RTs, and default views for default RT void setUpRenderPassAttachments( - wgpu::RenderPassDescriptor& descriptor, + wgpu::RenderPassDescriptor& outDescriptor, RenderPassParams const& params, // For default render target: wgpu::TextureView const& defaultColorTextureView, @@ -58,18 +54,20 @@ public: wgpu::TextureFormat customDepthFormat, wgpu::TextureFormat customStencilFormat); - bool isDefaultRenderTarget() const { return mDefaultRenderTarget; } + [[nodiscard]] bool isDefaultRenderTarget() const { return mDefaultRenderTarget; } [[nodiscard]] uint8_t getSamples() const { return mSamples; } [[nodiscard]] uint8_t getLayerCount() const { return mLayerCount; } // Accessors for the driver to get stored attachment info - const MRT& getColorAttachmentInfos() const { return mColorAttachments; } - const Attachment& getDepthAttachmentInfo() const { return mDepthAttachment; } - const Attachment& getStencilAttachmentInfo() const { return mStencilAttachment; } + [[nodiscard]] MRT const& getColorAttachmentInfos() const { return mColorAttachments; } + [[nodiscard]] Attachment const& getDepthAttachmentInfo() const { return mDepthAttachment; } + [[nodiscard]] Attachment const& getStencilAttachmentInfo() const { return mStencilAttachment; } // Static helpers for load/store operations - static wgpu::LoadOp getLoadOperation(const RenderPassParams& params, TargetBufferFlags buffer); - static wgpu::StoreOp getStoreOperation(const RenderPassParams& params, TargetBufferFlags buffer); + [[nodiscard]] static wgpu::LoadOp getLoadOperation(RenderPassParams const& params, + TargetBufferFlags buffer); + [[nodiscard]] static wgpu::StoreOp getStoreOperation(RenderPassParams const& params, + TargetBufferFlags buffer); private: bool mDefaultRenderTarget = false; @@ -77,7 +75,8 @@ private: uint8_t mLayerCount = 1; MRT mColorAttachments{}; - // TODO WebGPU only supports a DepthStencil attachment, should this be just mDepthStencilAttachment? + // TODO WebGPU only supports a DepthStencil attachment, should this be just + // mDepthStencilAttachment? Attachment mDepthAttachment{}; Attachment mStencilAttachment{};