1 // 2 // Copyright 2024 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 7 #ifndef LIBANGLE_RENDERER_WGPU_WGPU_UTILS_H_ 8 #define LIBANGLE_RENDERER_WGPU_WGPU_UTILS_H_ 9 10 #include <dawn/webgpu_cpp.h> 11 #include <stdint.h> 12 13 #include "libANGLE/Caps.h" 14 #include "libANGLE/Error.h" 15 #include "libANGLE/angletypes.h" 16 17 #define ANGLE_WGPU_TRY(context, command) \ 18 do \ 19 { \ 20 auto ANGLE_LOCAL_VAR = command; \ 21 if (ANGLE_UNLIKELY(::rx::webgpu::IsWgpuError(ANGLE_LOCAL_VAR))) \ 22 { \ 23 (context)->handleError(GL_INVALID_OPERATION, "Internal WebGPU error.", __FILE__, \ 24 ANGLE_FUNCTION, __LINE__); \ 25 return angle::Result::Stop; \ 26 } \ 27 } while (0) 28 29 namespace rx 30 { 31 32 class ContextWgpu; 33 class DisplayWgpu; 34 35 namespace webgpu 36 { 37 constexpr size_t kUnpackedDepthIndex = gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; 38 constexpr size_t kUnpackedStencilIndex = gl::IMPLEMENTATION_MAX_DRAW_BUFFERS + 1; 39 constexpr uint32_t kUnpackedColorBuffersMask = 40 angle::BitMask<uint32_t>(gl::IMPLEMENTATION_MAX_DRAW_BUFFERS); 41 // WebGPU image level index. 42 using LevelIndex = gl::LevelIndexWrapper<uint32_t>; 43 44 enum class RenderPassClosureReason 45 { 46 NewRenderPass, 47 48 InvalidEnum, 49 EnumCount = InvalidEnum, 50 }; 51 52 struct ClearValues 53 { 54 wgpu::Color clearColor; 55 uint32_t depthSlice; 56 }; 57 58 class ClearValuesArray final 59 { 60 public: 61 ClearValuesArray(); 62 ~ClearValuesArray(); 63 64 ClearValuesArray(const ClearValuesArray &other); 65 ClearValuesArray &operator=(const ClearValuesArray &rhs); 66 67 void store(uint32_t index, ClearValues clearValues); 68 gl::DrawBufferMask getColorMask() const; reset()69 void reset() 70 { 71 mValues.fill({}); 72 mEnabled.reset(); 73 } reset(size_t index)74 void reset(size_t index) 75 { 76 mValues[index] = {}; 77 mEnabled.reset(index); 78 } 79 const ClearValues &operator[](size_t index) const { return mValues[index]; } 80 empty()81 bool empty() const { return mEnabled.none(); } any()82 bool any() const { return mEnabled.any(); } 83 test(size_t index)84 bool test(size_t index) const { return mEnabled.test(index); } 85 86 private: 87 gl::AttachmentArray<ClearValues> mValues; 88 gl::AttachmentsMask mEnabled; 89 }; 90 91 void EnsureCapsInitialized(const wgpu::Device &device, gl::Caps *nativeCaps); 92 93 ContextWgpu *GetImpl(const gl::Context *context); 94 DisplayWgpu *GetDisplay(const gl::Context *context); 95 wgpu::Device GetDevice(const gl::Context *context); 96 wgpu::Instance GetInstance(const gl::Context *context); 97 wgpu::RenderPassColorAttachment CreateNewClearColorAttachment(wgpu::Color clearValue, 98 uint32_t depthSlice, 99 wgpu::TextureView textureView); 100 101 bool IsWgpuError(wgpu::WaitStatus waitStatus); 102 bool IsWgpuError(WGPUBufferMapAsyncStatus mapBufferStatus); 103 } // namespace webgpu 104 105 namespace wgpu_gl 106 { 107 gl::LevelIndex getLevelIndex(webgpu::LevelIndex levelWgpu, gl::LevelIndex baseLevel); 108 gl::Extents getExtents(wgpu::Extent3D wgpuExtent); 109 } // namespace wgpu_gl 110 111 namespace gl_wgpu 112 { 113 webgpu::LevelIndex getLevelIndex(gl::LevelIndex levelGl, gl::LevelIndex baseLevel); 114 wgpu::TextureDimension getWgpuTextureDimension(gl::TextureType glTextureType); 115 wgpu::Extent3D getExtent3D(const gl::Extents &glExtent); 116 } // namespace gl_wgpu 117 118 } // namespace rx 119 120 #endif // LIBANGLE_RENDERER_WGPU_WGPU_UTILS_H_ 121