// // Copyright 2024 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #ifndef LIBANGLE_RENDERER_WGPU_WGPU_UTILS_H_ #define LIBANGLE_RENDERER_WGPU_WGPU_UTILS_H_ #include #include #include "libANGLE/Caps.h" #include "libANGLE/Error.h" #include "libANGLE/angletypes.h" #define ANGLE_WGPU_TRY(context, command) \ do \ { \ auto ANGLE_LOCAL_VAR = command; \ if (ANGLE_UNLIKELY(::rx::webgpu::IsWgpuError(ANGLE_LOCAL_VAR))) \ { \ (context)->handleError(GL_INVALID_OPERATION, "Internal WebGPU error.", __FILE__, \ ANGLE_FUNCTION, __LINE__); \ return angle::Result::Stop; \ } \ } while (0) namespace rx { class ContextWgpu; class DisplayWgpu; namespace webgpu { constexpr size_t kUnpackedDepthIndex = gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; constexpr size_t kUnpackedStencilIndex = gl::IMPLEMENTATION_MAX_DRAW_BUFFERS + 1; constexpr uint32_t kUnpackedColorBuffersMask = angle::BitMask(gl::IMPLEMENTATION_MAX_DRAW_BUFFERS); // WebGPU image level index. using LevelIndex = gl::LevelIndexWrapper; enum class RenderPassClosureReason { NewRenderPass, InvalidEnum, EnumCount = InvalidEnum, }; struct ClearValues { wgpu::Color clearColor; uint32_t depthSlice; }; class ClearValuesArray final { public: ClearValuesArray(); ~ClearValuesArray(); ClearValuesArray(const ClearValuesArray &other); ClearValuesArray &operator=(const ClearValuesArray &rhs); void store(uint32_t index, ClearValues clearValues); gl::DrawBufferMask getColorMask() const; void reset() { mValues.fill({}); mEnabled.reset(); } void reset(size_t index) { mValues[index] = {}; mEnabled.reset(index); } const ClearValues &operator[](size_t index) const { return mValues[index]; } bool empty() const { return mEnabled.none(); } bool any() const { return mEnabled.any(); } bool test(size_t index) const { return mEnabled.test(index); } private: gl::AttachmentArray mValues; gl::AttachmentsMask mEnabled; }; void EnsureCapsInitialized(const wgpu::Device &device, gl::Caps *nativeCaps); ContextWgpu *GetImpl(const gl::Context *context); DisplayWgpu *GetDisplay(const gl::Context *context); wgpu::Device GetDevice(const gl::Context *context); wgpu::Instance GetInstance(const gl::Context *context); wgpu::RenderPassColorAttachment CreateNewClearColorAttachment(wgpu::Color clearValue, uint32_t depthSlice, wgpu::TextureView textureView); bool IsWgpuError(wgpu::WaitStatus waitStatus); bool IsWgpuError(WGPUBufferMapAsyncStatus mapBufferStatus); } // namespace webgpu namespace wgpu_gl { gl::LevelIndex getLevelIndex(webgpu::LevelIndex levelWgpu, gl::LevelIndex baseLevel); gl::Extents getExtents(wgpu::Extent3D wgpuExtent); } // namespace wgpu_gl namespace gl_wgpu { webgpu::LevelIndex getLevelIndex(gl::LevelIndex levelGl, gl::LevelIndex baseLevel); wgpu::TextureDimension getWgpuTextureDimension(gl::TextureType glTextureType); wgpu::Extent3D getExtent3D(const gl::Extents &glExtent); } // namespace gl_wgpu } // namespace rx #endif // LIBANGLE_RENDERER_WGPU_WGPU_UTILS_H_