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 #include "libANGLE/renderer/wgpu/wgpu_utils.h"
8
9 #include "libANGLE/renderer/renderer_utils.h"
10 #include "libANGLE/renderer/wgpu/ContextWgpu.h"
11 #include "libANGLE/renderer/wgpu/DisplayWgpu.h"
12
13 namespace rx
14 {
15
16 namespace webgpu
17 {
18
GetImpl(const gl::Context * context)19 ContextWgpu *GetImpl(const gl::Context *context)
20 {
21 return GetImplAs<ContextWgpu>(context);
22 }
23
GetDisplay(const gl::Context * context)24 DisplayWgpu *GetDisplay(const gl::Context *context)
25 {
26 ContextWgpu *contextWgpu = GetImpl(context);
27 return contextWgpu->getDisplay();
28 }
29
GetDevice(const gl::Context * context)30 wgpu::Device GetDevice(const gl::Context *context)
31 {
32 DisplayWgpu *display = GetDisplay(context);
33 return display->getDevice();
34 }
35
GetInstance(const gl::Context * context)36 wgpu::Instance GetInstance(const gl::Context *context)
37 {
38 DisplayWgpu *display = GetDisplay(context);
39 return display->getInstance();
40 }
41
CreateNewClearColorAttachment(wgpu::Color clearValue,uint32_t depthSlice,wgpu::TextureView textureView)42 wgpu::RenderPassColorAttachment CreateNewClearColorAttachment(wgpu::Color clearValue,
43 uint32_t depthSlice,
44 wgpu::TextureView textureView)
45 {
46 wgpu::RenderPassColorAttachment colorAttachment;
47 colorAttachment.view = textureView;
48 colorAttachment.depthSlice = depthSlice;
49 colorAttachment.loadOp = wgpu::LoadOp::Clear;
50 colorAttachment.storeOp = wgpu::StoreOp::Store;
51 colorAttachment.clearValue = clearValue;
52
53 return colorAttachment;
54 }
55
IsWgpuError(wgpu::WaitStatus waitStatus)56 bool IsWgpuError(wgpu::WaitStatus waitStatus)
57 {
58 return waitStatus != wgpu::WaitStatus::Success;
59 }
60
IsWgpuError(WGPUBufferMapAsyncStatus mapBufferStatus)61 bool IsWgpuError(WGPUBufferMapAsyncStatus mapBufferStatus)
62 {
63 return mapBufferStatus != WGPUBufferMapAsyncStatus_Success;
64 }
65
ClearValuesArray()66 ClearValuesArray::ClearValuesArray() : mValues{}, mEnabled{} {}
67 ClearValuesArray::~ClearValuesArray() = default;
68
69 ClearValuesArray::ClearValuesArray(const ClearValuesArray &other) = default;
70 ClearValuesArray &ClearValuesArray::operator=(const ClearValuesArray &rhs) = default;
71
store(uint32_t index,ClearValues clearValues)72 void ClearValuesArray::store(uint32_t index, ClearValues clearValues)
73 {
74 mValues[index] = clearValues;
75 mEnabled.set(index);
76 }
77
getColorMask() const78 gl::DrawBufferMask ClearValuesArray::getColorMask() const
79 {
80 return gl::DrawBufferMask(mEnabled.bits() & kUnpackedColorBuffersMask);
81 }
82
EnsureCapsInitialized(const wgpu::Device & device,gl::Caps * nativeCaps)83 void EnsureCapsInitialized(const wgpu::Device &device, gl::Caps *nativeCaps)
84 {
85 wgpu::SupportedLimits limitsWgpu = {};
86 device.GetLimits(&limitsWgpu);
87
88 nativeCaps->maxElementIndex = std::numeric_limits<GLuint>::max() - 1;
89 nativeCaps->max3DTextureSize = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension3D);
90 nativeCaps->max2DTextureSize = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
91 nativeCaps->maxArrayTextureLayers = rx::LimitToInt(limitsWgpu.limits.maxTextureArrayLayers);
92 nativeCaps->maxCubeMapTextureSize = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
93 nativeCaps->maxRenderbufferSize = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
94
95 nativeCaps->maxDrawBuffers = rx::LimitToInt(limitsWgpu.limits.maxColorAttachments);
96 nativeCaps->maxFramebufferWidth = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
97 nativeCaps->maxFramebufferHeight = rx::LimitToInt(limitsWgpu.limits.maxTextureDimension2D);
98 nativeCaps->maxColorAttachments = rx::LimitToInt(limitsWgpu.limits.maxColorAttachments);
99
100 nativeCaps->maxVertexAttribStride =
101 rx::LimitToInt(limitsWgpu.limits.maxVertexBufferArrayStride);
102
103 nativeCaps->maxVertexAttributes = rx::LimitToInt(limitsWgpu.limits.maxVertexAttributes);
104
105 nativeCaps->maxTextureBufferSize = rx::LimitToInt(limitsWgpu.limits.maxBufferSize);
106 }
107 } // namespace webgpu
108
109 namespace wgpu_gl
110 {
getLevelIndex(webgpu::LevelIndex levelWgpu,gl::LevelIndex baseLevel)111 gl::LevelIndex getLevelIndex(webgpu::LevelIndex levelWgpu, gl::LevelIndex baseLevel)
112 {
113 return gl::LevelIndex(levelWgpu.get() + baseLevel.get());
114 }
115
getExtents(wgpu::Extent3D wgpuExtent)116 gl::Extents getExtents(wgpu::Extent3D wgpuExtent)
117 {
118 gl::Extents glExtent;
119 glExtent.width = wgpuExtent.width;
120 glExtent.height = wgpuExtent.height;
121 glExtent.depth = wgpuExtent.depthOrArrayLayers;
122 return glExtent;
123 }
124 } // namespace wgpu_gl
125
126 namespace gl_wgpu
127 {
getLevelIndex(gl::LevelIndex levelGl,gl::LevelIndex baseLevel)128 webgpu::LevelIndex getLevelIndex(gl::LevelIndex levelGl, gl::LevelIndex baseLevel)
129 {
130 ASSERT(baseLevel <= levelGl);
131 return webgpu::LevelIndex(levelGl.get() - baseLevel.get());
132 }
133
getExtent3D(const gl::Extents & glExtent)134 wgpu::Extent3D getExtent3D(const gl::Extents &glExtent)
135 {
136 wgpu::Extent3D wgpuExtent;
137 wgpuExtent.width = glExtent.width;
138 wgpuExtent.height = glExtent.height;
139 wgpuExtent.depthOrArrayLayers = glExtent.depth;
140 return wgpuExtent;
141 }
142
getWgpuTextureDimension(gl::TextureType glTextureType)143 wgpu::TextureDimension getWgpuTextureDimension(gl::TextureType glTextureType)
144 {
145 wgpu::TextureDimension dimension = {};
146 switch (glTextureType)
147 {
148 case gl::TextureType::_2D:
149 case gl::TextureType::_2DMultisample:
150 case gl::TextureType::Rectangle:
151 case gl::TextureType::External:
152 case gl::TextureType::Buffer:
153 dimension = wgpu::TextureDimension::e2D;
154 break;
155 case gl::TextureType::_2DArray:
156 case gl::TextureType::_2DMultisampleArray:
157 case gl::TextureType::_3D:
158 case gl::TextureType::CubeMap:
159 case gl::TextureType::CubeMapArray:
160 case gl::TextureType::VideoImage:
161 dimension = wgpu::TextureDimension::e3D;
162 break;
163 default:
164 break;
165 }
166 return dimension;
167 }
168 } // namespace gl_wgpu
169 } // namespace rx
170