| // 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 <string> | |
| #include <vector> | |
| #include "dawn/native/WebGPUBackend.h" | |
| #include "partition_alloc/pointers/raw_ptr.h" | |
| #include "src/dawn/native/Device.h" | |
| #include "src/dawn/native/ToBackend.h" | |
| #include "src/dawn/native/webgpu/CaptureContext.h" | |
| #include "src/dawn/native/webgpu/Forward.h" | |
| #include "src/dawn/native/webgpu/ObjectWGPU.h" | |
| #include "src/dawn/native/webgpu/QueueWGPU.h" | |
| namespace dawn::native::webgpu { | |
| class Device final : public DeviceBase, public ObjectWGPU<WGPUDevice> { | |
| 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; | |
| bool CanResolveSubRect() const override; | |
| float GetTimestampPeriodInNS() const override; | |
| bool AreTimestampsQuantized() const override; | |
| bool NeedsIndirectGPUValidation() const override; | |
| WGPUInstance GetInnerInstance() const; | |
| void StartCapture(CaptureStream& commandStream, CaptureStream& contentStream); | |
| void EndCapture(); | |
| template <typename T> | |
| void CaptureSetLabel(T* object, const std::string& label) { | |
| Queue* q = ToBackend(GetQueue()); | |
| if (q->IsCapturing()) { | |
| q->GetCaptureContext()->CaptureSetLabel(object, label); | |
| } | |
| } | |
| const raw_ref<const DawnProcTable> wgpu; | |
| private: | |
| Device(AdapterBase* adapter, | |
| WGPUAdapter innerAdapter, | |
| const UnpackedPtr<DeviceDescriptor>& descriptor, | |
| const TogglesState& deviceToggles, | |
| const TogglesState& innerDeviceToggles, | |
| Ref<DeviceBase::DeviceLostEvent>&& lostEvent); | |
| ResultOrError<Ref<BindGroupBase>> CreateBindGroupImpl( | |
| const UnpackedPtr<BindGroupDescriptor>& descriptor) override; | |
| ResultOrError<Ref<BindGroupLayoutInternalBase>> CreateBindGroupLayoutImpl( | |
| const UnpackedPtr<BindGroupLayoutDescriptor>& descriptor) override; | |
| ResultOrError<Ref<BufferBase>> CreateBufferImpl( | |
| const UnpackedPtr<BufferDescriptor>& descriptor) override; | |
| ResultOrError<Ref<ExternalTextureBase>> CreateExternalTextureImpl( | |
| const ExternalTextureDescriptor* descriptor) override; | |
| ResultOrError<Ref<CommandBufferBase>> CreateCommandBuffer( | |
| CommandEncoder* encoder, | |
| const CommandBufferDescriptor* descriptor) override; | |
| ResultOrError<Ref<RenderBundleBase>> CreateRenderBundle( | |
| RenderBundleEncoderBase* encoder, | |
| const RenderBundleDescriptor* 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; | |
| ResultOrError<Ref<ResourceTableBase>> CreateResourceTableImpl( | |
| const ResourceTableDescriptor* 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) 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; | |
| ResultOrError<Ref<SharedTextureMemoryBase>> ImportSharedTextureMemoryImpl( | |
| const SharedTextureMemoryDescriptor* descriptor) override; | |
| ResultOrError<Ref<SharedFenceBase>> ImportSharedFenceImpl( | |
| const SharedFenceDescriptor* descriptor) override; | |
| MaybeError TickImpl() override; | |
| MaybeError CopyFromStagingToBuffer(BufferBase* source, | |
| uint64_t sourceOffset, | |
| BufferBase* destination, | |
| uint64_t destinationOffset, | |
| uint64_t size) override; | |
| MaybeError CopyFromStagingToTextureImpl(BufferBase* source, | |
| const TexelCopyBufferLayout& src, | |
| const TextureCopy& dst, | |
| const Extent3D& copySizePixels) override; | |
| void DestroyImpl(DestroyReason reason) override; | |
| void SetLabelImpl() override; | |
| }; | |
| } // namespace dawn::native::webgpu | |
| #endif // SRC_DAWN_NATIVE_WEBGPU_DEVICEWGPU_H_ |