Bug 1857841 - pt 3. Add a new page kind named "fresh" r=glandium
[gecko.git] / dom / webgpu / Utility.cpp
blob111bac44165b2087b03bfd1be9eb6012e7a34489
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "Utility.h"
7 #include "mozilla/dom/WebGPUBinding.h"
8 #include "mozilla/webgpu/ffi/wgpu.h"
9 #include "mozilla/webgpu/WebGPUTypes.h"
11 namespace mozilla::webgpu {
13 template <typename E>
14 void ConvertToExtent3D(const E& aExtent, ffi::WGPUExtent3d* aExtentFFI) {
15 *aExtentFFI = {};
16 if (aExtent.IsRangeEnforcedUnsignedLongSequence()) {
17 const auto& seq = aExtent.GetAsRangeEnforcedUnsignedLongSequence();
18 aExtentFFI->width = seq.Length() > 0 ? seq[0] : 0;
19 aExtentFFI->height = seq.Length() > 1 ? seq[1] : 1;
20 aExtentFFI->depth_or_array_layers = seq.Length() > 2 ? seq[2] : 1;
21 } else if (aExtent.IsGPUExtent3DDict()) {
22 const auto& dict = aExtent.GetAsGPUExtent3DDict();
23 aExtentFFI->width = dict.mWidth;
24 aExtentFFI->height = dict.mHeight;
25 aExtentFFI->depth_or_array_layers = dict.mDepthOrArrayLayers;
26 } else {
27 MOZ_CRASH("Unexpected extent type");
31 void ConvertExtent3DToFFI(const dom::GPUExtent3D& aExtent,
32 ffi::WGPUExtent3d* aExtentFFI) {
33 ConvertToExtent3D(aExtent, aExtentFFI);
36 void ConvertExtent3DToFFI(const dom::OwningGPUExtent3D& aExtent,
37 ffi::WGPUExtent3d* aExtentFFI) {
38 ConvertToExtent3D(aExtent, aExtentFFI);
41 ffi::WGPUExtent3d ConvertExtent(const dom::GPUExtent3D& aExtent) {
42 ffi::WGPUExtent3d extent = {};
43 ConvertToExtent3D(aExtent, &extent);
44 return extent;
47 ffi::WGPUExtent3d ConvertExtent(const dom::OwningGPUExtent3D& aExtent) {
48 ffi::WGPUExtent3d extent = {};
49 ConvertToExtent3D(aExtent, &extent);
50 return extent;
53 ffi::WGPUCompareFunction ConvertCompareFunction(
54 const dom::GPUCompareFunction& aCompare) {
55 // Value of 0 = Undefined is reserved on the C side for "null" semantics.
56 return ffi::WGPUCompareFunction(UnderlyingValue(aCompare) + 1);
59 ffi::WGPUTextureFormat ConvertTextureFormat(
60 const dom::GPUTextureFormat& aFormat) {
61 ffi::WGPUTextureFormat result = {ffi::WGPUTextureFormat_Sentinel};
62 switch (aFormat) {
63 case dom::GPUTextureFormat::R8unorm:
64 result.tag = ffi::WGPUTextureFormat_R8Unorm;
65 break;
66 case dom::GPUTextureFormat::R8snorm:
67 result.tag = ffi::WGPUTextureFormat_R8Snorm;
68 break;
69 case dom::GPUTextureFormat::R8uint:
70 result.tag = ffi::WGPUTextureFormat_R8Uint;
71 break;
72 case dom::GPUTextureFormat::R8sint:
73 result.tag = ffi::WGPUTextureFormat_R8Sint;
74 break;
75 case dom::GPUTextureFormat::R16uint:
76 result.tag = ffi::WGPUTextureFormat_R16Uint;
77 break;
78 case dom::GPUTextureFormat::R16sint:
79 result.tag = ffi::WGPUTextureFormat_R16Sint;
80 break;
81 case dom::GPUTextureFormat::R16float:
82 result.tag = ffi::WGPUTextureFormat_R16Float;
83 break;
84 case dom::GPUTextureFormat::Rg8unorm:
85 result.tag = ffi::WGPUTextureFormat_Rg8Unorm;
86 break;
87 case dom::GPUTextureFormat::Rg8snorm:
88 result.tag = ffi::WGPUTextureFormat_Rg8Snorm;
89 break;
90 case dom::GPUTextureFormat::Rg8uint:
91 result.tag = ffi::WGPUTextureFormat_Rg8Uint;
92 break;
93 case dom::GPUTextureFormat::Rg8sint:
94 result.tag = ffi::WGPUTextureFormat_Rg8Sint;
95 break;
96 case dom::GPUTextureFormat::R32uint:
97 result.tag = ffi::WGPUTextureFormat_R32Uint;
98 break;
99 case dom::GPUTextureFormat::R32sint:
100 result.tag = ffi::WGPUTextureFormat_R32Sint;
101 break;
102 case dom::GPUTextureFormat::R32float:
103 result.tag = ffi::WGPUTextureFormat_R32Float;
104 break;
105 case dom::GPUTextureFormat::Rg16uint:
106 result.tag = ffi::WGPUTextureFormat_Rg16Uint;
107 break;
108 case dom::GPUTextureFormat::Rg16sint:
109 result.tag = ffi::WGPUTextureFormat_Rg16Sint;
110 break;
111 case dom::GPUTextureFormat::Rg16float:
112 result.tag = ffi::WGPUTextureFormat_Rg16Float;
113 break;
114 case dom::GPUTextureFormat::Rgba8unorm:
115 result.tag = ffi::WGPUTextureFormat_Rgba8Unorm;
116 break;
117 case dom::GPUTextureFormat::Rgba8unorm_srgb:
118 result.tag = ffi::WGPUTextureFormat_Rgba8UnormSrgb;
119 break;
120 case dom::GPUTextureFormat::Rgba8snorm:
121 result.tag = ffi::WGPUTextureFormat_Rgba8Snorm;
122 break;
123 case dom::GPUTextureFormat::Rgba8uint:
124 result.tag = ffi::WGPUTextureFormat_Rgba8Uint;
125 break;
126 case dom::GPUTextureFormat::Rgba8sint:
127 result.tag = ffi::WGPUTextureFormat_Rgba8Sint;
128 break;
129 case dom::GPUTextureFormat::Bgra8unorm:
130 result.tag = ffi::WGPUTextureFormat_Bgra8Unorm;
131 break;
132 case dom::GPUTextureFormat::Bgra8unorm_srgb:
133 result.tag = ffi::WGPUTextureFormat_Bgra8UnormSrgb;
134 break;
135 case dom::GPUTextureFormat::Rgb9e5ufloat:
136 result.tag = ffi::WGPUTextureFormat_Rgb9e5Ufloat;
137 break;
138 case dom::GPUTextureFormat::Rgb10a2unorm:
139 result.tag = ffi::WGPUTextureFormat_Rgb10a2Unorm;
140 break;
141 case dom::GPUTextureFormat::Rg11b10ufloat:
142 result.tag = ffi::WGPUTextureFormat_Rg11b10Float;
143 break;
144 case dom::GPUTextureFormat::Rg32uint:
145 result.tag = ffi::WGPUTextureFormat_Rg32Uint;
146 break;
147 case dom::GPUTextureFormat::Rg32sint:
148 result.tag = ffi::WGPUTextureFormat_Rg32Sint;
149 break;
150 case dom::GPUTextureFormat::Rg32float:
151 result.tag = ffi::WGPUTextureFormat_Rg32Float;
152 break;
153 case dom::GPUTextureFormat::Rgba16uint:
154 result.tag = ffi::WGPUTextureFormat_Rgba16Uint;
155 break;
156 case dom::GPUTextureFormat::Rgba16sint:
157 result.tag = ffi::WGPUTextureFormat_Rgba16Sint;
158 break;
159 case dom::GPUTextureFormat::Rgba16float:
160 result.tag = ffi::WGPUTextureFormat_Rgba16Float;
161 break;
162 case dom::GPUTextureFormat::Rgba32uint:
163 result.tag = ffi::WGPUTextureFormat_Rgba32Uint;
164 break;
165 case dom::GPUTextureFormat::Rgba32sint:
166 result.tag = ffi::WGPUTextureFormat_Rgba32Sint;
167 break;
168 case dom::GPUTextureFormat::Rgba32float:
169 result.tag = ffi::WGPUTextureFormat_Rgba32Float;
170 break;
171 case dom::GPUTextureFormat::Depth32float:
172 result.tag = ffi::WGPUTextureFormat_Depth32Float;
173 break;
174 case dom::GPUTextureFormat::Bc1_rgba_unorm:
175 result.tag = ffi::WGPUTextureFormat_Bc1RgbaUnorm;
176 break;
177 case dom::GPUTextureFormat::Bc1_rgba_unorm_srgb:
178 result.tag = ffi::WGPUTextureFormat_Bc1RgbaUnormSrgb;
179 break;
180 case dom::GPUTextureFormat::Bc4_r_unorm:
181 result.tag = ffi::WGPUTextureFormat_Bc4RUnorm;
182 break;
183 case dom::GPUTextureFormat::Bc4_r_snorm:
184 result.tag = ffi::WGPUTextureFormat_Bc4RSnorm;
185 break;
186 case dom::GPUTextureFormat::Bc2_rgba_unorm:
187 result.tag = ffi::WGPUTextureFormat_Bc2RgbaUnorm;
188 break;
189 case dom::GPUTextureFormat::Bc2_rgba_unorm_srgb:
190 result.tag = ffi::WGPUTextureFormat_Bc2RgbaUnormSrgb;
191 break;
192 case dom::GPUTextureFormat::Bc3_rgba_unorm:
193 result.tag = ffi::WGPUTextureFormat_Bc3RgbaUnorm;
194 break;
195 case dom::GPUTextureFormat::Bc3_rgba_unorm_srgb:
196 result.tag = ffi::WGPUTextureFormat_Bc3RgbaUnormSrgb;
197 break;
198 case dom::GPUTextureFormat::Bc5_rg_unorm:
199 result.tag = ffi::WGPUTextureFormat_Bc5RgUnorm;
200 break;
201 case dom::GPUTextureFormat::Bc5_rg_snorm:
202 result.tag = ffi::WGPUTextureFormat_Bc5RgSnorm;
203 break;
204 case dom::GPUTextureFormat::Bc6h_rgb_ufloat:
205 result.tag = ffi::WGPUTextureFormat_Bc6hRgbUfloat;
206 break;
207 case dom::GPUTextureFormat::Bc6h_rgb_float:
208 result.tag = ffi::WGPUTextureFormat_Bc6hRgbFloat;
209 break;
210 case dom::GPUTextureFormat::Bc7_rgba_unorm:
211 result.tag = ffi::WGPUTextureFormat_Bc7RgbaUnorm;
212 break;
213 case dom::GPUTextureFormat::Bc7_rgba_unorm_srgb:
214 result.tag = ffi::WGPUTextureFormat_Bc7RgbaUnormSrgb;
215 break;
216 case dom::GPUTextureFormat::Stencil8:
217 result.tag = ffi::WGPUTextureFormat_Stencil8;
218 break;
219 case dom::GPUTextureFormat::Depth16unorm:
220 result.tag = ffi::WGPUTextureFormat_Depth16Unorm;
221 break;
222 case dom::GPUTextureFormat::Depth24plus:
223 result.tag = ffi::WGPUTextureFormat_Depth24Plus;
224 break;
225 case dom::GPUTextureFormat::Depth24plus_stencil8:
226 result.tag = ffi::WGPUTextureFormat_Depth24PlusStencil8;
227 break;
228 case dom::GPUTextureFormat::Depth32float_stencil8:
229 result.tag = ffi::WGPUTextureFormat_Depth32FloatStencil8;
230 break;
233 // Clang will check for us that the switch above is exhaustive,
234 // but not if we add a 'default' case. So, check this here.
235 MOZ_ASSERT(result.tag != ffi::WGPUTextureFormat_Sentinel,
236 "unexpected texture format enum");
238 return result;
241 ffi::WGPUMultisampleState ConvertMultisampleState(
242 const dom::GPUMultisampleState& aDesc) {
243 ffi::WGPUMultisampleState desc = {};
244 desc.count = aDesc.mCount;
245 desc.mask = aDesc.mMask;
246 desc.alpha_to_coverage_enabled = aDesc.mAlphaToCoverageEnabled;
247 return desc;
250 ffi::WGPUBlendComponent ConvertBlendComponent(
251 const dom::GPUBlendComponent& aDesc) {
252 ffi::WGPUBlendComponent desc = {};
253 desc.src_factor = ffi::WGPUBlendFactor(aDesc.mSrcFactor);
254 desc.dst_factor = ffi::WGPUBlendFactor(aDesc.mDstFactor);
255 desc.operation = ffi::WGPUBlendOperation(aDesc.mOperation);
256 return desc;
259 ffi::WGPUStencilFaceState ConvertStencilFaceState(
260 const dom::GPUStencilFaceState& aDesc) {
261 ffi::WGPUStencilFaceState desc = {};
262 desc.compare = ConvertCompareFunction(aDesc.mCompare);
263 desc.fail_op = ffi::WGPUStencilOperation(aDesc.mFailOp);
264 desc.depth_fail_op = ffi::WGPUStencilOperation(aDesc.mDepthFailOp);
265 desc.pass_op = ffi::WGPUStencilOperation(aDesc.mPassOp);
266 return desc;
269 ffi::WGPUDepthStencilState ConvertDepthStencilState(
270 const dom::GPUDepthStencilState& aDesc) {
271 ffi::WGPUDepthStencilState desc = {};
272 desc.format = ConvertTextureFormat(aDesc.mFormat);
273 desc.depth_write_enabled = aDesc.mDepthWriteEnabled;
274 desc.depth_compare = ConvertCompareFunction(aDesc.mDepthCompare);
275 desc.stencil.front = ConvertStencilFaceState(aDesc.mStencilFront);
276 desc.stencil.back = ConvertStencilFaceState(aDesc.mStencilBack);
277 desc.stencil.read_mask = aDesc.mStencilReadMask;
278 desc.stencil.write_mask = aDesc.mStencilWriteMask;
279 desc.bias.constant = aDesc.mDepthBias;
280 desc.bias.slope_scale = aDesc.mDepthBiasSlopeScale;
281 desc.bias.clamp = aDesc.mDepthBiasClamp;
282 return desc;
285 } // namespace mozilla::webgpu