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/. */
7 #include "mozilla/dom/WebGPUBinding.h"
8 #include "mozilla/webgpu/ffi/wgpu.h"
9 #include "mozilla/webgpu/WebGPUTypes.h"
11 namespace mozilla::webgpu
{
14 void ConvertToExtent3D(const E
& aExtent
, ffi::WGPUExtent3d
* 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
;
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
);
47 ffi::WGPUExtent3d
ConvertExtent(const dom::OwningGPUExtent3D
& aExtent
) {
48 ffi::WGPUExtent3d extent
= {};
49 ConvertToExtent3D(aExtent
, &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
};
63 case dom::GPUTextureFormat::R8unorm
:
64 result
.tag
= ffi::WGPUTextureFormat_R8Unorm
;
66 case dom::GPUTextureFormat::R8snorm
:
67 result
.tag
= ffi::WGPUTextureFormat_R8Snorm
;
69 case dom::GPUTextureFormat::R8uint
:
70 result
.tag
= ffi::WGPUTextureFormat_R8Uint
;
72 case dom::GPUTextureFormat::R8sint
:
73 result
.tag
= ffi::WGPUTextureFormat_R8Sint
;
75 case dom::GPUTextureFormat::R16uint
:
76 result
.tag
= ffi::WGPUTextureFormat_R16Uint
;
78 case dom::GPUTextureFormat::R16sint
:
79 result
.tag
= ffi::WGPUTextureFormat_R16Sint
;
81 case dom::GPUTextureFormat::R16float
:
82 result
.tag
= ffi::WGPUTextureFormat_R16Float
;
84 case dom::GPUTextureFormat::Rg8unorm
:
85 result
.tag
= ffi::WGPUTextureFormat_Rg8Unorm
;
87 case dom::GPUTextureFormat::Rg8snorm
:
88 result
.tag
= ffi::WGPUTextureFormat_Rg8Snorm
;
90 case dom::GPUTextureFormat::Rg8uint
:
91 result
.tag
= ffi::WGPUTextureFormat_Rg8Uint
;
93 case dom::GPUTextureFormat::Rg8sint
:
94 result
.tag
= ffi::WGPUTextureFormat_Rg8Sint
;
96 case dom::GPUTextureFormat::R32uint
:
97 result
.tag
= ffi::WGPUTextureFormat_R32Uint
;
99 case dom::GPUTextureFormat::R32sint
:
100 result
.tag
= ffi::WGPUTextureFormat_R32Sint
;
102 case dom::GPUTextureFormat::R32float
:
103 result
.tag
= ffi::WGPUTextureFormat_R32Float
;
105 case dom::GPUTextureFormat::Rg16uint
:
106 result
.tag
= ffi::WGPUTextureFormat_Rg16Uint
;
108 case dom::GPUTextureFormat::Rg16sint
:
109 result
.tag
= ffi::WGPUTextureFormat_Rg16Sint
;
111 case dom::GPUTextureFormat::Rg16float
:
112 result
.tag
= ffi::WGPUTextureFormat_Rg16Float
;
114 case dom::GPUTextureFormat::Rgba8unorm
:
115 result
.tag
= ffi::WGPUTextureFormat_Rgba8Unorm
;
117 case dom::GPUTextureFormat::Rgba8unorm_srgb
:
118 result
.tag
= ffi::WGPUTextureFormat_Rgba8UnormSrgb
;
120 case dom::GPUTextureFormat::Rgba8snorm
:
121 result
.tag
= ffi::WGPUTextureFormat_Rgba8Snorm
;
123 case dom::GPUTextureFormat::Rgba8uint
:
124 result
.tag
= ffi::WGPUTextureFormat_Rgba8Uint
;
126 case dom::GPUTextureFormat::Rgba8sint
:
127 result
.tag
= ffi::WGPUTextureFormat_Rgba8Sint
;
129 case dom::GPUTextureFormat::Bgra8unorm
:
130 result
.tag
= ffi::WGPUTextureFormat_Bgra8Unorm
;
132 case dom::GPUTextureFormat::Bgra8unorm_srgb
:
133 result
.tag
= ffi::WGPUTextureFormat_Bgra8UnormSrgb
;
135 case dom::GPUTextureFormat::Rgb9e5ufloat
:
136 result
.tag
= ffi::WGPUTextureFormat_Rgb9e5Ufloat
;
138 case dom::GPUTextureFormat::Rgb10a2unorm
:
139 result
.tag
= ffi::WGPUTextureFormat_Rgb10a2Unorm
;
141 case dom::GPUTextureFormat::Rg11b10ufloat
:
142 result
.tag
= ffi::WGPUTextureFormat_Rg11b10Float
;
144 case dom::GPUTextureFormat::Rg32uint
:
145 result
.tag
= ffi::WGPUTextureFormat_Rg32Uint
;
147 case dom::GPUTextureFormat::Rg32sint
:
148 result
.tag
= ffi::WGPUTextureFormat_Rg32Sint
;
150 case dom::GPUTextureFormat::Rg32float
:
151 result
.tag
= ffi::WGPUTextureFormat_Rg32Float
;
153 case dom::GPUTextureFormat::Rgba16uint
:
154 result
.tag
= ffi::WGPUTextureFormat_Rgba16Uint
;
156 case dom::GPUTextureFormat::Rgba16sint
:
157 result
.tag
= ffi::WGPUTextureFormat_Rgba16Sint
;
159 case dom::GPUTextureFormat::Rgba16float
:
160 result
.tag
= ffi::WGPUTextureFormat_Rgba16Float
;
162 case dom::GPUTextureFormat::Rgba32uint
:
163 result
.tag
= ffi::WGPUTextureFormat_Rgba32Uint
;
165 case dom::GPUTextureFormat::Rgba32sint
:
166 result
.tag
= ffi::WGPUTextureFormat_Rgba32Sint
;
168 case dom::GPUTextureFormat::Rgba32float
:
169 result
.tag
= ffi::WGPUTextureFormat_Rgba32Float
;
171 case dom::GPUTextureFormat::Depth32float
:
172 result
.tag
= ffi::WGPUTextureFormat_Depth32Float
;
174 case dom::GPUTextureFormat::Bc1_rgba_unorm
:
175 result
.tag
= ffi::WGPUTextureFormat_Bc1RgbaUnorm
;
177 case dom::GPUTextureFormat::Bc1_rgba_unorm_srgb
:
178 result
.tag
= ffi::WGPUTextureFormat_Bc1RgbaUnormSrgb
;
180 case dom::GPUTextureFormat::Bc4_r_unorm
:
181 result
.tag
= ffi::WGPUTextureFormat_Bc4RUnorm
;
183 case dom::GPUTextureFormat::Bc4_r_snorm
:
184 result
.tag
= ffi::WGPUTextureFormat_Bc4RSnorm
;
186 case dom::GPUTextureFormat::Bc2_rgba_unorm
:
187 result
.tag
= ffi::WGPUTextureFormat_Bc2RgbaUnorm
;
189 case dom::GPUTextureFormat::Bc2_rgba_unorm_srgb
:
190 result
.tag
= ffi::WGPUTextureFormat_Bc2RgbaUnormSrgb
;
192 case dom::GPUTextureFormat::Bc3_rgba_unorm
:
193 result
.tag
= ffi::WGPUTextureFormat_Bc3RgbaUnorm
;
195 case dom::GPUTextureFormat::Bc3_rgba_unorm_srgb
:
196 result
.tag
= ffi::WGPUTextureFormat_Bc3RgbaUnormSrgb
;
198 case dom::GPUTextureFormat::Bc5_rg_unorm
:
199 result
.tag
= ffi::WGPUTextureFormat_Bc5RgUnorm
;
201 case dom::GPUTextureFormat::Bc5_rg_snorm
:
202 result
.tag
= ffi::WGPUTextureFormat_Bc5RgSnorm
;
204 case dom::GPUTextureFormat::Bc6h_rgb_ufloat
:
205 result
.tag
= ffi::WGPUTextureFormat_Bc6hRgbUfloat
;
207 case dom::GPUTextureFormat::Bc6h_rgb_float
:
208 result
.tag
= ffi::WGPUTextureFormat_Bc6hRgbFloat
;
210 case dom::GPUTextureFormat::Bc7_rgba_unorm
:
211 result
.tag
= ffi::WGPUTextureFormat_Bc7RgbaUnorm
;
213 case dom::GPUTextureFormat::Bc7_rgba_unorm_srgb
:
214 result
.tag
= ffi::WGPUTextureFormat_Bc7RgbaUnormSrgb
;
216 case dom::GPUTextureFormat::Stencil8
:
217 result
.tag
= ffi::WGPUTextureFormat_Stencil8
;
219 case dom::GPUTextureFormat::Depth16unorm
:
220 result
.tag
= ffi::WGPUTextureFormat_Depth16Unorm
;
222 case dom::GPUTextureFormat::Depth24plus
:
223 result
.tag
= ffi::WGPUTextureFormat_Depth24Plus
;
225 case dom::GPUTextureFormat::Depth24plus_stencil8
:
226 result
.tag
= ffi::WGPUTextureFormat_Depth24PlusStencil8
;
228 case dom::GPUTextureFormat::Depth32float_stencil8
:
229 result
.tag
= ffi::WGPUTextureFormat_Depth32FloatStencil8
;
231 case dom::GPUTextureFormat::EndGuard_
:
232 MOZ_ASSERT_UNREACHABLE();
235 // Clang will check for us that the switch above is exhaustive,
236 // but not if we add a 'default' case. So, check this here.
237 MOZ_ASSERT(result
.tag
!= ffi::WGPUTextureFormat_Sentinel
,
238 "unexpected texture format enum");
243 ffi::WGPUMultisampleState
ConvertMultisampleState(
244 const dom::GPUMultisampleState
& aDesc
) {
245 ffi::WGPUMultisampleState desc
= {};
246 desc
.count
= aDesc
.mCount
;
247 desc
.mask
= aDesc
.mMask
;
248 desc
.alpha_to_coverage_enabled
= aDesc
.mAlphaToCoverageEnabled
;
252 ffi::WGPUBlendComponent
ConvertBlendComponent(
253 const dom::GPUBlendComponent
& aDesc
) {
254 ffi::WGPUBlendComponent desc
= {};
255 desc
.src_factor
= ffi::WGPUBlendFactor(aDesc
.mSrcFactor
);
256 desc
.dst_factor
= ffi::WGPUBlendFactor(aDesc
.mDstFactor
);
257 desc
.operation
= ffi::WGPUBlendOperation(aDesc
.mOperation
);
261 ffi::WGPUStencilFaceState
ConvertStencilFaceState(
262 const dom::GPUStencilFaceState
& aDesc
) {
263 ffi::WGPUStencilFaceState desc
= {};
264 desc
.compare
= ConvertCompareFunction(aDesc
.mCompare
);
265 desc
.fail_op
= ffi::WGPUStencilOperation(aDesc
.mFailOp
);
266 desc
.depth_fail_op
= ffi::WGPUStencilOperation(aDesc
.mDepthFailOp
);
267 desc
.pass_op
= ffi::WGPUStencilOperation(aDesc
.mPassOp
);
271 ffi::WGPUDepthStencilState
ConvertDepthStencilState(
272 const dom::GPUDepthStencilState
& aDesc
) {
273 ffi::WGPUDepthStencilState desc
= {};
274 desc
.format
= ConvertTextureFormat(aDesc
.mFormat
);
275 desc
.depth_write_enabled
= aDesc
.mDepthWriteEnabled
;
276 desc
.depth_compare
= ConvertCompareFunction(aDesc
.mDepthCompare
);
277 desc
.stencil
.front
= ConvertStencilFaceState(aDesc
.mStencilFront
);
278 desc
.stencil
.back
= ConvertStencilFaceState(aDesc
.mStencilBack
);
279 desc
.stencil
.read_mask
= aDesc
.mStencilReadMask
;
280 desc
.stencil
.write_mask
= aDesc
.mStencilWriteMask
;
281 desc
.bias
.constant
= aDesc
.mDepthBias
;
282 desc
.bias
.slope_scale
= aDesc
.mDepthBiasSlopeScale
;
283 desc
.bias
.clamp
= aDesc
.mDepthBiasClamp
;
287 } // namespace mozilla::webgpu