[WebGPU backend] Add DeviceWGPU and QueueWGPU. Start implementing DeviceWGPU. Most Create*Impl methods are placeholders. Bug: 413053623 Change-Id: I5dd9f5801362856dd45c61ca95dec4afc8ecb196 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/241855 Commit-Queue: Shrek Shao <shrekshao@google.com> Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/src/dawn/native/BUILD.gn b/src/dawn/native/BUILD.gn index 56b8a5f..c8468eb 100644 --- a/src/dawn/native/BUILD.gn +++ b/src/dawn/native/BUILD.gn
@@ -706,9 +706,13 @@ sources += [ "webgpu/BackendWGPU.cpp", "webgpu/BackendWGPU.h", + "webgpu/DeviceWGPU.cpp", + "webgpu/DeviceWGPU.h", "webgpu/Forward.h", "webgpu/PhysicalDeviceWGPU.cpp", "webgpu/PhysicalDeviceWGPU.h", + "webgpu/QueueWGPU.cpp", + "webgpu/QueueWGPU.h", ] }
diff --git a/src/dawn/native/CMakeLists.txt b/src/dawn/native/CMakeLists.txt index ccf82ec..2871791 100644 --- a/src/dawn/native/CMakeLists.txt +++ b/src/dawn/native/CMakeLists.txt
@@ -571,12 +571,16 @@ ) list(APPEND private_headers "webgpu/BackendWGPU.h" + "webgpu/DeviceWGPU.h" "webgpu/Forward.h" "webgpu/PhysicalDeviceWGPU.h" + "webgpu/QueueWGPU.h" ) list(APPEND sources "webgpu/BackendWGPU.cpp" + "webgpu/DeviceWGPU.cpp" "webgpu/PhysicalDeviceWGPU.cpp" + "webgpu/QueueWGPU.cpp" ) endif()
diff --git a/src/dawn/native/webgpu/DeviceWGPU.cpp b/src/dawn/native/webgpu/DeviceWGPU.cpp new file mode 100644 index 0000000..911a272 --- /dev/null +++ b/src/dawn/native/webgpu/DeviceWGPU.cpp
@@ -0,0 +1,237 @@ +// Copyright 2025 The Dawn & Tint Authors +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "dawn/native/webgpu/DeviceWGPU.h" + +#include <string> +#include <utility> + +#include "dawn/common/Constants.h" +#include "dawn/common/StringViewUtils.h" +#include "dawn/native/BackendConnection.h" +#include "dawn/native/BindGroup.h" +#include "dawn/native/BindGroupLayoutInternal.h" +#include "dawn/native/Buffer.h" +#include "dawn/native/ChainUtils.h" +#include "dawn/native/CommandBuffer.h" +#include "dawn/native/CommandEncoder.h" +#include "dawn/native/ErrorData.h" +#include "dawn/native/Instance.h" +#include "dawn/native/PhysicalDevice.h" +#include "dawn/native/PipelineLayout.h" +#include "dawn/native/QuerySet.h" +#include "dawn/native/Queue.h" +#include "dawn/native/RenderPipeline.h" +#include "dawn/native/Sampler.h" +#include "dawn/native/Surface.h" +#include "dawn/native/SwapChain.h" +#include "dawn/native/Texture.h" +#include "dawn/native/webgpu/PhysicalDeviceWGPU.h" +#include "dawn/native/webgpu/QueueWGPU.h" + +#include "tint/tint.h" + +namespace dawn::native::webgpu { + +// static +ResultOrError<Ref<Device>> Device::Create(AdapterBase* adapter, + WGPUAdapter innerAdapter, + const UnpackedPtr<DeviceDescriptor>& descriptor, + const TogglesState& deviceToggles, + Ref<DeviceBase::DeviceLostEvent>&& lostEvent) { + Ref<Device> device = AcquireRef( + new Device(adapter, innerAdapter, descriptor, deviceToggles, std::move(lostEvent))); + DAWN_TRY(device->Initialize(descriptor)); + return device; +} + +Device::Device(AdapterBase* adapter, + WGPUAdapter innerAdapter, + const UnpackedPtr<DeviceDescriptor>& descriptor, + const TogglesState& deviceToggles, + Ref<DeviceBase::DeviceLostEvent>&& lostEvent) + : DeviceBase(adapter, descriptor, deviceToggles, std::move(lostEvent)), + wgpu(ToBackend(adapter->GetPhysicalDevice())->GetFunctions()) { + DAWN_ASSERT(adapter->GetPhysicalDevice()->GetBackendType() == wgpu::BackendType::WebGPU); + + WGPUDeviceDescriptor apiDesc = *(ToAPI(*descriptor)); + std::string label = "Inner Device on " + adapter->GetPhysicalDevice()->GetName(); + apiDesc.label = ToOutputStringView(label); + // Acquire a Ref to the outer webgpu::Device to avoid possible dangling pointer in the callback. + Ref<Device>* outerDeviceRef = new Ref<Device>(this); + apiDesc.deviceLostCallbackInfo = { + nullptr, WGPUCallbackMode_AllowProcessEvents, + [](WGPUDevice const*, WGPUDeviceLostReason reason, WGPUStringView message, void*, + void* outerDeviceRef) { + Ref<Device>* deviceWGPURef = reinterpret_cast<Ref<Device>*>(outerDeviceRef); + Device* deviceWGPU = deviceWGPURef->Get(); + if (reason == WGPUDeviceLostReason_Unknown) { + // Internal crash of the implementation device, call device lost callback of the + // upper DeviceWGPU + if (!deviceWGPU->IsLost()) { + deviceWGPU->APIForceLoss(FromAPI(reason), message); + } + } + delete deviceWGPURef; + }, + nullptr, reinterpret_cast<void*>(outerDeviceRef)}; + // TODO(crbug.com/413053623): revisit for error scope. + apiDesc.uncapturedErrorCallbackInfo = {nullptr, nullptr, nullptr, nullptr}; + + // TODO(crbug.com/413053623): use adapterRequestDevice instead as dawn_wire doesn't support + // adapterCreateDevice. + mInnerDevice = wgpu.adapterCreateDevice(innerAdapter, &apiDesc); +} + +Device::~Device() { + Destroy(); +} + +WGPUDevice Device::GetInnerHandle() const { + return mInnerDevice; +} + +MaybeError Device::Initialize(const UnpackedPtr<DeviceDescriptor>& descriptor) { + Ref<Queue> queue; + DAWN_TRY_ASSIGN(queue, Queue::Create(this, &descriptor->defaultQueue)); + return DeviceBase::Initialize(std::move(queue)); +} + +ResultOrError<Ref<BindGroupBase>> Device::CreateBindGroupImpl( + const BindGroupDescriptor* descriptor) { + return Ref<BindGroupBase>{nullptr}; +} +ResultOrError<Ref<BindGroupLayoutInternalBase>> Device::CreateBindGroupLayoutImpl( + const BindGroupLayoutDescriptor* descriptor) { + // TODO(crbug.com/413053623): Replace with webgpu::BindGroupLayout object. + // This placeholder implementation is needed because device is creating empty BindGroupLayout + // and getting content hash. + return AcquireRef(new BindGroupLayoutInternalBase(this, descriptor)); +} +ResultOrError<Ref<BufferBase>> Device::CreateBufferImpl( + const UnpackedPtr<BufferDescriptor>& descriptor) { + return Ref<BufferBase>{nullptr}; +} +ResultOrError<Ref<CommandBufferBase>> Device::CreateCommandBuffer( + CommandEncoder* encoder, + const CommandBufferDescriptor* descriptor) { + return Ref<CommandBufferBase>{nullptr}; +} +Ref<ComputePipelineBase> Device::CreateUninitializedComputePipelineImpl( + const UnpackedPtr<ComputePipelineDescriptor>& descriptor) { + return Ref<ComputePipelineBase>{nullptr}; +} +ResultOrError<Ref<PipelineLayoutBase>> Device::CreatePipelineLayoutImpl( + const UnpackedPtr<PipelineLayoutDescriptor>& descriptor) { + // TODO(crbug.com/413053623): Replace with webgpu::PipelineLayout object. + // This placeholder implementation is needed because device is creating empty PipelineLayout + // and getting content hash. + return AcquireRef(new PipelineLayoutBase(this, descriptor)); +} +ResultOrError<Ref<QuerySetBase>> Device::CreateQuerySetImpl(const QuerySetDescriptor* descriptor) { + return Ref<QuerySetBase>{nullptr}; +} +Ref<RenderPipelineBase> Device::CreateUninitializedRenderPipelineImpl( + const UnpackedPtr<RenderPipelineDescriptor>& descriptor) { + return Ref<RenderPipelineBase>{nullptr}; +} +ResultOrError<Ref<SamplerBase>> Device::CreateSamplerImpl(const SamplerDescriptor* descriptor) { + return Ref<SamplerBase>{nullptr}; +} +ResultOrError<Ref<ShaderModuleBase>> Device::CreateShaderModuleImpl( + const UnpackedPtr<ShaderModuleDescriptor>& descriptor, + const std::vector<tint::wgsl::Extension>& internalExtensions, + ShaderModuleParseResult* parseResult, + std::unique_ptr<OwnedCompilationMessages>* compilationMessages) { + return Ref<ShaderModuleBase>{nullptr}; +} +ResultOrError<Ref<SwapChainBase>> Device::CreateSwapChainImpl(Surface* surface, + SwapChainBase* previousSwapChain, + const SurfaceConfiguration* config) { + return Ref<SwapChainBase>{nullptr}; +} +ResultOrError<Ref<TextureBase>> Device::CreateTextureImpl( + const UnpackedPtr<TextureDescriptor>& descriptor) { + return Ref<TextureBase>{nullptr}; +} +ResultOrError<Ref<TextureViewBase>> Device::CreateTextureViewImpl( + TextureBase* texture, + const UnpackedPtr<TextureViewDescriptor>& descriptor) { + return Ref<TextureViewBase>{nullptr}; +} + +void Device::DestroyImpl() { + DAWN_ASSERT(GetState() == State::Disconnected); + // TODO(crbug.com/dawn/831): DestroyImpl is called from two places. + // - It may be called if the device is explicitly destroyed with APIDestroy. + // This case is NOT thread-safe and needs proper synchronization with other + // simultaneous uses of the device. + // - It may be called when the last ref to the device is dropped and the device + // is implicitly destroyed. This case is thread-safe because there are no + // other threads using the device since there are no other live refs. + + if (mInnerDevice) { + // webgpu.h guarantees that losing this reference will cause all internal resources to be + // freed and wait on the GPU if needed to do so. + wgpu.deviceRelease(mInnerDevice); + mInnerDevice = nullptr; + } +} + +MaybeError Device::CopyFromStagingToBufferImpl(BufferBase* source, + uint64_t sourceOffset, + BufferBase* destination, + uint64_t destinationOffset, + uint64_t size) { + return DAWN_UNIMPLEMENTED_ERROR("webgpu::Device implementation is incomplete."); +} + +MaybeError Device::CopyFromStagingToTextureImpl(const BufferBase* source, + const TexelCopyBufferLayout& src, + const TextureCopy& dst, + const Extent3D& copySizePixels) { + return DAWN_UNIMPLEMENTED_ERROR("webgpu::Device implementation is incomplete."); +} + +MaybeError Device::TickImpl() { + wgpu.deviceTick(mInnerDevice); + return {}; +} + +uint32_t Device::GetOptimalBytesPerRowAlignment() const { + return kTextureBytesPerRowAlignment; +} + +uint64_t Device::GetOptimalBufferToTextureCopyOffsetAlignment() const { + return 1; +} + +float Device::GetTimestampPeriodInNS() const { + return 1.0f; +} + +} // namespace dawn::native::webgpu
diff --git a/src/dawn/native/webgpu/DeviceWGPU.h b/src/dawn/native/webgpu/DeviceWGPU.h new file mode 100644 index 0000000..16c9ee9 --- /dev/null +++ b/src/dawn/native/webgpu/DeviceWGPU.h
@@ -0,0 +1,121 @@ +// Copyright 2025 The Dawn & Tint Authors +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef SRC_DAWN_NATIVE_WEBGPU_DEVICEWGPU_H_ +#define SRC_DAWN_NATIVE_WEBGPU_DEVICEWGPU_H_ + +#include <memory> +#include <vector> + +#include "dawn/native/Device.h" +#include "dawn/native/ToBackend.h" +#include "dawn/native/webgpu/Forward.h" +#include "partition_alloc/pointers/raw_ptr.h" + +namespace dawn::native::webgpu { + +class Device final : public DeviceBase { + public: + static ResultOrError<Ref<Device>> Create(AdapterBase* adapter, + WGPUAdapter innerAdapter, + const UnpackedPtr<DeviceDescriptor>& descriptor, + const TogglesState& deviceToggles, + Ref<DeviceBase::DeviceLostEvent>&& lostEvent); + ~Device() override; + + MaybeError Initialize(const UnpackedPtr<DeviceDescriptor>& descriptor); + + uint32_t GetOptimalBytesPerRowAlignment() const override; + uint64_t GetOptimalBufferToTextureCopyOffsetAlignment() const override; + + float GetTimestampPeriodInNS() const override; + + WGPUDevice GetInnerHandle() const; + + const DawnProcTable& wgpu; + + private: + Device(AdapterBase* adapter, + WGPUAdapter innerAdapter, + const UnpackedPtr<DeviceDescriptor>& descriptor, + const TogglesState& deviceToggles, + Ref<DeviceBase::DeviceLostEvent>&& lostEvent); + + ResultOrError<Ref<BindGroupBase>> CreateBindGroupImpl( + const BindGroupDescriptor* descriptor) override; + ResultOrError<Ref<BindGroupLayoutInternalBase>> CreateBindGroupLayoutImpl( + const BindGroupLayoutDescriptor* descriptor) override; + ResultOrError<Ref<BufferBase>> CreateBufferImpl( + const UnpackedPtr<BufferDescriptor>& descriptor) override; + ResultOrError<Ref<CommandBufferBase>> CreateCommandBuffer( + CommandEncoder* encoder, + const CommandBufferDescriptor* descriptor) override; + Ref<ComputePipelineBase> CreateUninitializedComputePipelineImpl( + const UnpackedPtr<ComputePipelineDescriptor>& descriptor) override; + ResultOrError<Ref<PipelineLayoutBase>> CreatePipelineLayoutImpl( + const UnpackedPtr<PipelineLayoutDescriptor>& descriptor) override; + ResultOrError<Ref<QuerySetBase>> CreateQuerySetImpl( + const QuerySetDescriptor* descriptor) override; + Ref<RenderPipelineBase> CreateUninitializedRenderPipelineImpl( + const UnpackedPtr<RenderPipelineDescriptor>& descriptor) override; + ResultOrError<Ref<SamplerBase>> CreateSamplerImpl(const SamplerDescriptor* descriptor) override; + ResultOrError<Ref<ShaderModuleBase>> CreateShaderModuleImpl( + const UnpackedPtr<ShaderModuleDescriptor>& descriptor, + const std::vector<tint::wgsl::Extension>& internalExtensions, + ShaderModuleParseResult* parseResult, + std::unique_ptr<OwnedCompilationMessages>* compilationMessages) override; + ResultOrError<Ref<SwapChainBase>> CreateSwapChainImpl( + Surface* surface, + SwapChainBase* previousSwapChain, + const SurfaceConfiguration* config) override; + ResultOrError<Ref<TextureBase>> CreateTextureImpl( + const UnpackedPtr<TextureDescriptor>& descriptor) override; + ResultOrError<Ref<TextureViewBase>> CreateTextureViewImpl( + TextureBase* texture, + const UnpackedPtr<TextureViewDescriptor>& descriptor) override; + + MaybeError TickImpl() override; + + MaybeError CopyFromStagingToBufferImpl(BufferBase* source, + uint64_t sourceOffset, + BufferBase* destination, + uint64_t destinationOffset, + uint64_t size) override; + MaybeError CopyFromStagingToTextureImpl(const BufferBase* source, + const TexelCopyBufferLayout& src, + const TextureCopy& dst, + const Extent3D& copySizePixels) override; + + void DestroyImpl() override; + + // The "lower layer" implementation device that actually manages calls to GPU driver. + WGPUDevice mInnerDevice = nullptr; +}; + +} // namespace dawn::native::webgpu + +#endif // SRC_DAWN_NATIVE_WEBGPU_DEVICEWGPU_H_
diff --git a/src/dawn/native/webgpu/PhysicalDeviceWGPU.cpp b/src/dawn/native/webgpu/PhysicalDeviceWGPU.cpp index fcefe2e..b7302d5 100644 --- a/src/dawn/native/webgpu/PhysicalDeviceWGPU.cpp +++ b/src/dawn/native/webgpu/PhysicalDeviceWGPU.cpp
@@ -28,12 +28,14 @@ #include "dawn/native/webgpu/PhysicalDeviceWGPU.h" #include <string> +#include <utility> #include "dawn/common/StringViewUtils.h" #include "dawn/native/ChainUtils.h" #include "dawn/native/Features_autogen.h" #include "dawn/native/Instance.h" #include "dawn/native/webgpu/BackendWGPU.h" +#include "dawn/native/webgpu/DeviceWGPU.h" namespace dawn::native::webgpu { @@ -132,7 +134,7 @@ const UnpackedPtr<DeviceDescriptor>& descriptor, const TogglesState& deviceToggles, Ref<DeviceBase::DeviceLostEvent>&& lostEvent) { - DAWN_UNREACHABLE(); + return Device::Create(adapter, mInnerAdapter, descriptor, deviceToggles, std::move(lostEvent)); } void PhysicalDevice::PopulateBackendProperties(UnpackedPtr<AdapterInfo>& info) const {
diff --git a/src/dawn/native/webgpu/QueueWGPU.cpp b/src/dawn/native/webgpu/QueueWGPU.cpp new file mode 100644 index 0000000..76bb6b7 --- /dev/null +++ b/src/dawn/native/webgpu/QueueWGPU.cpp
@@ -0,0 +1,82 @@ +// Copyright 2025 The Dawn & Tint Authors +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "dawn/native/webgpu/QueueWGPU.h" + +#include "dawn/native/Queue.h" +#include "dawn/native/webgpu/DeviceWGPU.h" + +namespace dawn::native::webgpu { + +// static +ResultOrError<Ref<Queue>> Queue::Create(Device* device, const QueueDescriptor* descriptor) { + return AcquireRef(new Queue(device, descriptor)); +} + +Queue::Queue(Device* device, const QueueDescriptor* descriptor) : QueueBase(device, descriptor) {} + +Queue::~Queue() = default; + +MaybeError Queue::SubmitImpl(uint32_t commandCount, CommandBufferBase* const* commands) { + // TODO(crbug.com/413053623): finish implementing WebGPU backend. + return {}; +} + +MaybeError Queue::WriteBufferImpl(BufferBase* buffer, + uint64_t bufferOffset, + const void* data, + size_t size) { + // TODO(crbug.com/413053623): finish implementing WebGPU backend. + return {}; +} + +ResultOrError<ExecutionSerial> Queue::CheckAndUpdateCompletedSerials() { + // TODO(crbug.com/413053623): finish implementing WebGPU backend. + return GetLastSubmittedCommandSerial(); +} + +void Queue::ForceEventualFlushOfCommands() { + DAWN_UNREACHABLE(); +} + +bool Queue::HasPendingCommands() const { + return false; +} + +MaybeError Queue::SubmitPendingCommands() { + return {}; +} + +ResultOrError<bool> Queue::WaitForQueueSerial(ExecutionSerial serial, Nanoseconds timeout) { + return DAWN_UNIMPLEMENTED_ERROR("webgpu::Queue implementation is incomplete."); +} + +MaybeError Queue::WaitForIdleForDestruction() { + return {}; +} + +} // namespace dawn::native::webgpu
diff --git a/src/dawn/native/webgpu/QueueWGPU.h b/src/dawn/native/webgpu/QueueWGPU.h new file mode 100644 index 0000000..ae7ec98 --- /dev/null +++ b/src/dawn/native/webgpu/QueueWGPU.h
@@ -0,0 +1,61 @@ +// Copyright 2025 The Dawn & Tint Authors +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef SRC_DAWN_NATIVE_WEBGPU_QUEUEWGPU_H_ +#define SRC_DAWN_NATIVE_WEBGPU_QUEUEWGPU_H_ + +#include "dawn/native/Queue.h" + +#include "dawn/native/webgpu/Forward.h" + +namespace dawn::native::webgpu { + +class Device; + +class Queue final : public QueueBase { + public: + static ResultOrError<Ref<Queue>> Create(Device* device, const QueueDescriptor* descriptor); + + private: + Queue(Device* device, const QueueDescriptor* descriptor); + ~Queue() override; + MaybeError SubmitImpl(uint32_t commandCount, CommandBufferBase* const* commands) override; + MaybeError WriteBufferImpl(BufferBase* buffer, + uint64_t bufferOffset, + const void* data, + size_t size) override; + ResultOrError<ExecutionSerial> CheckAndUpdateCompletedSerials() override; + void ForceEventualFlushOfCommands() override; + bool HasPendingCommands() const override; + MaybeError SubmitPendingCommands() override; + ResultOrError<bool> WaitForQueueSerial(ExecutionSerial serial, Nanoseconds timeout) override; + MaybeError WaitForIdleForDestruction() override; +}; + +} // namespace dawn::native::webgpu + +#endif // SRC_DAWN_NATIVE_WEBGPU_QUEUEWGPU_H_
diff --git a/src/dawn/tests/end2end/DeviceLifetimeTests.cpp b/src/dawn/tests/end2end/DeviceLifetimeTests.cpp index 3732a53..d8087ce 100644 --- a/src/dawn/tests/end2end/DeviceLifetimeTests.cpp +++ b/src/dawn/tests/end2end/DeviceLifetimeTests.cpp
@@ -53,6 +53,9 @@ // Test that the device can be dropped while an onSubmittedWorkDone callback is in flight. TEST_P(DeviceLifetimeTests, DroppedWhileQueueOnSubmittedWorkDone) { + // TODO(crbug.com/413053623): implement webgpu::CommandBuffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + // Submit some work. wgpu::CommandEncoder encoder = device.CreateCommandEncoder(nullptr); wgpu::CommandBuffer commandBuffer = encoder.Finish(); @@ -69,6 +72,9 @@ // Test that the device can be dropped inside an onSubmittedWorkDone callback. TEST_P(DeviceLifetimeTests, DroppedInsideQueueOnSubmittedWorkDone) { + // TODO(crbug.com/413053623): implement webgpu::CommandBuffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + // Submit some work. wgpu::CommandEncoder encoder = device.CreateCommandEncoder(nullptr); wgpu::CommandBuffer commandBuffer = encoder.Finish(); @@ -142,6 +148,9 @@ // Test that the device can be dropped while a buffer created from it is being mapped. TEST_P(DeviceLifetimeTests, DroppedWhileMappingBuffer) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc = {}; desc.size = 4; desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; @@ -159,6 +168,9 @@ // Test that the device can be dropped before a mapped buffer created from it. TEST_P(DeviceLifetimeTests, DroppedBeforeMappedBuffer) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc = {}; desc.size = 4; desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; @@ -171,6 +183,9 @@ // Test that the device can be dropped before a mapped at creation buffer created from it. TEST_P(DeviceLifetimeTests, DroppedBeforeMappedAtCreationBuffer) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc = {}; desc.size = 4; desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; @@ -183,6 +198,9 @@ // Test that the device can be dropped before a buffer created from it, then mapping the buffer // fails. TEST_P(DeviceLifetimeTests, DroppedThenMapBuffer) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc = {}; desc.size = 4; desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; @@ -201,6 +219,9 @@ // Test that the device can be dropped before a buffer created from it, then mapping the buffer // twice (one inside callback) will both fail. TEST_P(DeviceLifetimeTests, Dropped_ThenMapBuffer_ThenMapBufferInCallback) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc = {}; desc.size = 4; desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; @@ -229,6 +250,9 @@ // Test that the device can be dropped inside a buffer map callback. TEST_P(DeviceLifetimeTests, DroppedInsideBufferMapCallback) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc = {}; desc.size = 4; desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; @@ -261,6 +285,9 @@ // Test that the device can be dropped while a write buffer operation is enqueued. TEST_P(DeviceLifetimeTests, DroppedWhileWriteBuffer) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc = {}; desc.size = 4; desc.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst; @@ -275,6 +302,9 @@ // a queue submit occurs. This is slightly different from the former test since it ensures // that pending work is flushed. TEST_P(DeviceLifetimeTests, DroppedWhileWriteBufferAndSubmit) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc = {}; desc.size = 4; desc.usage = wgpu::BufferUsage::CopySrc | wgpu::BufferUsage::CopyDst; @@ -288,6 +318,9 @@ // Test that the device can be dropped while createPipelineAsync is in flight TEST_P(DeviceLifetimeTests, DroppedWhileCreatePipelineAsync) { + // TODO(crbug.com/413053623): implement webgpu::ShaderModule + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ComputePipelineDescriptor desc; desc.compute.module = utils::CreateShaderModule(device, R"( @compute @workgroup_size(1) fn main() { @@ -307,6 +340,9 @@ // Test that the device can be dropped inside a createPipelineAsync callback TEST_P(DeviceLifetimeTests, DroppedInsideCreatePipelineAsync) { + // TODO(crbug.com/413053623): implement webgpu::ShaderModule + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ComputePipelineDescriptor desc; desc.compute.module = utils::CreateShaderModule(device, R"( @compute @workgroup_size(1) fn main() { @@ -330,6 +366,9 @@ // Test that the device can be dropped while createPipelineAsync which will hit the frontend cache // is in flight TEST_P(DeviceLifetimeTests, DroppedWhileCreatePipelineAsyncAlreadyCached) { + // TODO(crbug.com/413053623): implement webgpu::ShaderModule + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ComputePipelineDescriptor desc; desc.compute.module = utils::CreateShaderModule(device, R"( @compute @workgroup_size(1) fn main() { @@ -357,6 +396,9 @@ // Test that the device can be dropped inside a createPipelineAsync callback which will hit the // frontend cache TEST_P(DeviceLifetimeTests, DroppedInsideCreatePipelineAsyncAlreadyCached) { + // TODO(crbug.com/413053623): implement webgpu::ShaderModule + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ComputePipelineDescriptor desc; desc.compute.module = utils::CreateShaderModule(device, R"( @compute @workgroup_size(1) fn main() { @@ -384,6 +426,9 @@ // Test that the device can be dropped while createPipelineAsync which will race with a compilation // to add the same pipeline to the frontend cache TEST_P(DeviceLifetimeTests, DroppedWhileCreatePipelineAsyncRaceCache) { + // TODO(crbug.com/413053623): implement webgpu::ShaderModule + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ComputePipelineDescriptor desc; desc.compute.module = utils::CreateShaderModule(device, R"( @compute @workgroup_size(1) fn main() { @@ -407,6 +452,9 @@ // Test that the device can be dropped inside a createPipelineAsync callback which will race // with a compilation to add the same pipeline to the frontend cache TEST_P(DeviceLifetimeTests, DroppedInsideCreatePipelineAsyncRaceCache) { + // TODO(crbug.com/413053623): implement webgpu::ShaderModule + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ComputePipelineDescriptor desc; desc.compute.module = utils::CreateShaderModule(device, R"( @compute @workgroup_size(1) fn main() { @@ -468,7 +516,8 @@ NullBackend(), OpenGLBackend(), OpenGLESBackend(), - VulkanBackend()); + VulkanBackend(), + WebGPUBackend()); } // anonymous namespace } // namespace dawn
diff --git a/src/dawn/tests/end2end/DeviceLostTests.cpp b/src/dawn/tests/end2end/DeviceLostTests.cpp index 0c5a5a4..d59d67e 100644 --- a/src/dawn/tests/end2end/DeviceLostTests.cpp +++ b/src/dawn/tests/end2end/DeviceLostTests.cpp
@@ -104,6 +104,9 @@ // Test that GetBindGroupLayout fails when device is lost TEST_P(DeviceLostTest, GetBindGroupLayoutFails) { + // TODO(crbug.com/413053623): implement webgpu::Pipeline + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ShaderModule csModule = utils::CreateShaderModule(device, R"( struct UniformBuffer { pos : vec4f @@ -178,6 +181,9 @@ // Tests that CreateComputePipeline fails when device is lost TEST_P(DeviceLostTest, CreateComputePipelineFails) { + // TODO(crbug.com/413053623): implement webgpu::ShaderModule + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ShaderModule shader = utils::CreateShaderModule(device, ""); WaitABit(); @@ -191,6 +197,9 @@ // Tests that CreateRenderPipeline fails when device is lost TEST_P(DeviceLostTest, CreateRenderPipelineFails) { + // TODO(crbug.com/413053623): implement webgpu::ShaderModule + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ShaderModule shader = utils::CreateShaderModule(device, ""); WaitABit(); @@ -250,6 +259,9 @@ // Test that buffer.MapAsync for writing fails after device is lost TEST_P(DeviceLostTest, BufferMapAsyncFailsForWriting) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor bufferDescriptor; bufferDescriptor.size = 4; bufferDescriptor.usage = wgpu::BufferUsage::MapWrite; @@ -267,6 +279,9 @@ // Test that BufferMapAsync for writing calls back with success when device lost after // mapping TEST_P(DeviceLostTest, BufferMapAsyncBeforeLossFailsForWriting) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor bufferDescriptor; bufferDescriptor.size = 4; bufferDescriptor.usage = wgpu::BufferUsage::MapWrite; @@ -282,6 +297,9 @@ // Test that buffer.Unmap after device is lost TEST_P(DeviceLostTest, BufferUnmapAfterDeviceLost) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor bufferDescriptor; bufferDescriptor.size = sizeof(float); bufferDescriptor.usage = wgpu::BufferUsage::MapWrite; @@ -349,6 +367,9 @@ // Test that BufferMapAsync for reading fails after device is lost TEST_P(DeviceLostTest, BufferMapAsyncFailsForReading) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor bufferDescriptor; bufferDescriptor.size = 4; bufferDescriptor.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; @@ -367,6 +388,9 @@ // Test that BufferMapAsync for reading calls back with success when device lost after // mapping TEST_P(DeviceLostTest, BufferMapAsyncBeforeLossFailsForReading) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor bufferDescriptor; bufferDescriptor.size = sizeof(float); bufferDescriptor.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; @@ -396,6 +420,9 @@ // Test it's possible to GetMappedRange on a buffer created mapped after device loss TEST_P(DeviceLostTest, GetMappedRange_CreateBufferMappedAtCreationAfterLoss) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + LoseDeviceForTesting(); wgpu::BufferDescriptor desc; @@ -410,6 +437,9 @@ // Test that device loss doesn't change the result of GetMappedRange, mappedAtCreation version. TEST_P(DeviceLostTest, GetMappedRange_CreateBufferMappedAtCreationBeforeLoss) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc; desc.size = 4; desc.usage = wgpu::BufferUsage::CopySrc; @@ -425,6 +455,9 @@ // Test that device loss doesn't change the result of GetMappedRange, mapping for reading version. TEST_P(DeviceLostTest, GetMappedRange_MapAsyncReading) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc; desc.size = 4; desc.usage = wgpu::BufferUsage::MapRead | wgpu::BufferUsage::CopyDst; @@ -442,6 +475,9 @@ // Test that device loss doesn't change the result of GetMappedRange, mapping for writing version. TEST_P(DeviceLostTest, GetMappedRange_MapAsyncWriting) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::BufferDescriptor desc; desc.size = 4; desc.usage = wgpu::BufferUsage::MapWrite | wgpu::BufferUsage::CopySrc; @@ -508,6 +544,9 @@ // Test that WGPUCreatePipelineAsyncStatus_Success is returned when device is lost // before the callback of Create*PipelineAsync() is called. TEST_P(DeviceLostTest, DeviceLostBeforeCreatePipelineAsyncCallback) { + // TODO(crbug.com/413053623): implement webgpu::ShaderModule + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + wgpu::ShaderModule csModule = utils::CreateShaderModule(device, R"( @compute @workgroup_size(1) fn main() { })"); @@ -530,6 +569,9 @@ // references to bind group layouts such that the cache was non-empty at the end // of shut down. TEST_P(DeviceLostTest, FreeBindGroupAfterDeviceLossWithPendingCommands) { + // TODO(crbug.com/413053623): implement webgpu::BindGroup + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + DAWN_TEST_UNSUPPORTED_IF(GetSupportedLimits().maxStorageBuffersInFragmentStage < 1); wgpu::BindGroupLayout bgl = utils::MakeBindGroupLayout( @@ -567,6 +609,9 @@ // This is a regression test for crbug.com/1365011 where ending a render pass with an indirect draw // in it after the device is lost would cause render commands to be leaked. TEST_P(DeviceLostTest, DeviceLostInRenderPassWithDrawIndirect) { + // TODO(crbug.com/413053623): implement webgpu::Pipeline, etc. + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + utils::BasicRenderPass renderPass = utils::CreateBasicRenderPass(device, 4u, 4u); utils::ComboRenderPipelineDescriptor desc; desc.vertex.module = utils::CreateShaderModule(device, R"( @@ -597,6 +642,9 @@ // Attempting to set an object label after device loss should not cause an error. TEST_P(DeviceLostTest, SetLabelAfterDeviceLoss) { + // TODO(crbug.com/413053623): implement webgpu::Buffer + DAWN_SUPPRESS_TEST_IF(IsWebGPUOnWebGPU()); + std::string label = "test"; wgpu::BufferDescriptor descriptor; descriptor.size = 4; @@ -613,7 +661,8 @@ NullBackend(), OpenGLBackend(), OpenGLESBackend(), - VulkanBackend()); + VulkanBackend(), + WebGPUBackend()); } // anonymous namespace } // namespace dawn