1use alloc::format;
4use alloc::string::{String, ToString};
5
6pub trait ToWgsl: Sized {
19 fn to_wgsl(self) -> &'static str;
21}
22
23pub trait TryToWgsl: Sized {
37 fn try_to_wgsl(self) -> Option<&'static str>;
42
43 const DESCRIPTION: &'static str;
45
46 fn to_wgsl_for_diagnostics(self) -> String
57 where
58 Self: core::fmt::Debug + Copy,
59 {
60 match self.try_to_wgsl() {
61 Some(static_string) => static_string.to_string(),
62 None => format!("{{non-WGSL {} {self:?}}}", Self::DESCRIPTION),
63 }
64 }
65}
66
67impl TryToWgsl for crate::MathFunction {
68 const DESCRIPTION: &'static str = "math function";
69
70 fn try_to_wgsl(self) -> Option<&'static str> {
71 use crate::MathFunction as Mf;
72
73 Some(match self {
74 Mf::Abs => "abs",
75 Mf::Min => "min",
76 Mf::Max => "max",
77 Mf::Clamp => "clamp",
78 Mf::Saturate => "saturate",
79 Mf::Cos => "cos",
80 Mf::Cosh => "cosh",
81 Mf::Sin => "sin",
82 Mf::Sinh => "sinh",
83 Mf::Tan => "tan",
84 Mf::Tanh => "tanh",
85 Mf::Acos => "acos",
86 Mf::Asin => "asin",
87 Mf::Atan => "atan",
88 Mf::Atan2 => "atan2",
89 Mf::Asinh => "asinh",
90 Mf::Acosh => "acosh",
91 Mf::Atanh => "atanh",
92 Mf::Radians => "radians",
93 Mf::Degrees => "degrees",
94 Mf::Ceil => "ceil",
95 Mf::Floor => "floor",
96 Mf::Round => "round",
97 Mf::Fract => "fract",
98 Mf::Trunc => "trunc",
99 Mf::Modf => "modf",
100 Mf::Frexp => "frexp",
101 Mf::Ldexp => "ldexp",
102 Mf::Exp => "exp",
103 Mf::Exp2 => "exp2",
104 Mf::Log => "log",
105 Mf::Log2 => "log2",
106 Mf::Pow => "pow",
107 Mf::Dot => "dot",
108 Mf::Dot4I8Packed => "dot4I8Packed",
109 Mf::Dot4U8Packed => "dot4U8Packed",
110 Mf::Cross => "cross",
111 Mf::Distance => "distance",
112 Mf::Length => "length",
113 Mf::Normalize => "normalize",
114 Mf::FaceForward => "faceForward",
115 Mf::Reflect => "reflect",
116 Mf::Refract => "refract",
117 Mf::Sign => "sign",
118 Mf::Fma => "fma",
119 Mf::Mix => "mix",
120 Mf::Step => "step",
121 Mf::SmoothStep => "smoothstep",
122 Mf::Sqrt => "sqrt",
123 Mf::InverseSqrt => "inverseSqrt",
124 Mf::Transpose => "transpose",
125 Mf::Determinant => "determinant",
126 Mf::QuantizeToF16 => "quantizeToF16",
127 Mf::CountTrailingZeros => "countTrailingZeros",
128 Mf::CountLeadingZeros => "countLeadingZeros",
129 Mf::CountOneBits => "countOneBits",
130 Mf::ReverseBits => "reverseBits",
131 Mf::ExtractBits => "extractBits",
132 Mf::InsertBits => "insertBits",
133 Mf::FirstTrailingBit => "firstTrailingBit",
134 Mf::FirstLeadingBit => "firstLeadingBit",
135 Mf::Pack4x8snorm => "pack4x8snorm",
136 Mf::Pack4x8unorm => "pack4x8unorm",
137 Mf::Pack2x16snorm => "pack2x16snorm",
138 Mf::Pack2x16unorm => "pack2x16unorm",
139 Mf::Pack2x16float => "pack2x16float",
140 Mf::Pack4xI8 => "pack4xI8",
141 Mf::Pack4xU8 => "pack4xU8",
142 Mf::Pack4xI8Clamp => "pack4xI8Clamp",
143 Mf::Pack4xU8Clamp => "pack4xU8Clamp",
144 Mf::Unpack4x8snorm => "unpack4x8snorm",
145 Mf::Unpack4x8unorm => "unpack4x8unorm",
146 Mf::Unpack2x16snorm => "unpack2x16snorm",
147 Mf::Unpack2x16unorm => "unpack2x16unorm",
148 Mf::Unpack2x16float => "unpack2x16float",
149 Mf::Unpack4xI8 => "unpack4xI8",
150 Mf::Unpack4xU8 => "unpack4xU8",
151
152 Mf::Inverse | Mf::Outer => return None,
154 })
155 }
156}
157
158impl TryToWgsl for crate::BuiltIn {
159 const DESCRIPTION: &'static str = "builtin value";
160
161 fn try_to_wgsl(self) -> Option<&'static str> {
162 use crate::BuiltIn as Bi;
163 Some(match self {
164 Bi::Position { .. } => "position",
165 Bi::ViewIndex => "view_index",
166 Bi::InstanceIndex => "instance_index",
167 Bi::VertexIndex => "vertex_index",
168 Bi::ClipDistance => "clip_distances",
169 Bi::FragDepth => "frag_depth",
170 Bi::FrontFacing => "front_facing",
171 Bi::PrimitiveIndex => "primitive_index",
172 Bi::Barycentric => "barycentric",
173 Bi::SampleIndex => "sample_index",
174 Bi::SampleMask => "sample_mask",
175 Bi::GlobalInvocationId => "global_invocation_id",
176 Bi::LocalInvocationId => "local_invocation_id",
177 Bi::LocalInvocationIndex => "local_invocation_index",
178 Bi::WorkGroupId => "workgroup_id",
179 Bi::NumWorkGroups => "num_workgroups",
180 Bi::NumSubgroups => "num_subgroups",
181 Bi::SubgroupId => "subgroup_id",
182 Bi::SubgroupSize => "subgroup_size",
183 Bi::SubgroupInvocationId => "subgroup_invocation_id",
184
185 Bi::BaseInstance
187 | Bi::BaseVertex
188 | Bi::CullDistance
189 | Bi::PointSize
190 | Bi::DrawID
191 | Bi::PointCoord
192 | Bi::WorkGroupSize
193 | Bi::CullPrimitive
194 | Bi::TriangleIndices
195 | Bi::LineIndices
196 | Bi::MeshTaskSize
197 | Bi::PointIndex
198 | Bi::VertexCount
199 | Bi::PrimitiveCount
200 | Bi::Vertices
201 | Bi::Primitives => return None,
202 })
203 }
204}
205
206impl ToWgsl for crate::Interpolation {
207 fn to_wgsl(self) -> &'static str {
208 match self {
209 crate::Interpolation::Perspective => "perspective",
210 crate::Interpolation::Linear => "linear",
211 crate::Interpolation::Flat => "flat",
212 }
213 }
214}
215
216impl ToWgsl for crate::Sampling {
217 fn to_wgsl(self) -> &'static str {
218 match self {
219 crate::Sampling::Center => "center",
220 crate::Sampling::Centroid => "centroid",
221 crate::Sampling::Sample => "sample",
222 crate::Sampling::First => "first",
223 crate::Sampling::Either => "either",
224 }
225 }
226}
227
228impl ToWgsl for crate::StorageFormat {
229 fn to_wgsl(self) -> &'static str {
230 use crate::StorageFormat as Sf;
231
232 match self {
233 Sf::R8Unorm => "r8unorm",
234 Sf::R8Snorm => "r8snorm",
235 Sf::R8Uint => "r8uint",
236 Sf::R8Sint => "r8sint",
237 Sf::R16Uint => "r16uint",
238 Sf::R16Sint => "r16sint",
239 Sf::R16Float => "r16float",
240 Sf::Rg8Unorm => "rg8unorm",
241 Sf::Rg8Snorm => "rg8snorm",
242 Sf::Rg8Uint => "rg8uint",
243 Sf::Rg8Sint => "rg8sint",
244 Sf::R32Uint => "r32uint",
245 Sf::R32Sint => "r32sint",
246 Sf::R32Float => "r32float",
247 Sf::Rg16Uint => "rg16uint",
248 Sf::Rg16Sint => "rg16sint",
249 Sf::Rg16Float => "rg16float",
250 Sf::Rgba8Unorm => "rgba8unorm",
251 Sf::Rgba8Snorm => "rgba8snorm",
252 Sf::Rgba8Uint => "rgba8uint",
253 Sf::Rgba8Sint => "rgba8sint",
254 Sf::Bgra8Unorm => "bgra8unorm",
255 Sf::Rgb10a2Uint => "rgb10a2uint",
256 Sf::Rgb10a2Unorm => "rgb10a2unorm",
257 Sf::Rg11b10Ufloat => "rg11b10ufloat",
258 Sf::R64Uint => "r64uint",
259 Sf::Rg32Uint => "rg32uint",
260 Sf::Rg32Sint => "rg32sint",
261 Sf::Rg32Float => "rg32float",
262 Sf::Rgba16Uint => "rgba16uint",
263 Sf::Rgba16Sint => "rgba16sint",
264 Sf::Rgba16Float => "rgba16float",
265 Sf::Rgba32Uint => "rgba32uint",
266 Sf::Rgba32Sint => "rgba32sint",
267 Sf::Rgba32Float => "rgba32float",
268 Sf::R16Unorm => "r16unorm",
269 Sf::R16Snorm => "r16snorm",
270 Sf::Rg16Unorm => "rg16unorm",
271 Sf::Rg16Snorm => "rg16snorm",
272 Sf::Rgba16Unorm => "rgba16unorm",
273 Sf::Rgba16Snorm => "rgba16snorm",
274 }
275 }
276}
277
278impl TryToWgsl for crate::Scalar {
279 const DESCRIPTION: &'static str = "scalar type";
280
281 fn try_to_wgsl(self) -> Option<&'static str> {
282 use crate::Scalar;
283
284 Some(match self {
285 Scalar::F16 => "f16",
286 Scalar::F32 => "f32",
287 Scalar::F64 => "f64",
288 Scalar::I32 => "i32",
289 Scalar::U32 => "u32",
290 Scalar::I64 => "i64",
291 Scalar::U64 => "u64",
292 Scalar::BOOL => "bool",
293 _ => return None,
294 })
295 }
296
297 fn to_wgsl_for_diagnostics(self) -> String {
298 match self.try_to_wgsl() {
299 Some(static_string) => static_string.to_string(),
300 None => match self.kind {
301 crate::ScalarKind::Sint
302 | crate::ScalarKind::Uint
303 | crate::ScalarKind::Float
304 | crate::ScalarKind::Bool => format!("{{non-WGSL scalar {self:?}}}"),
305 crate::ScalarKind::AbstractInt => "{AbstractInt}".to_string(),
306 crate::ScalarKind::AbstractFloat => "{AbstractFloat}".to_string(),
307 },
308 }
309 }
310}
311
312impl ToWgsl for crate::ImageDimension {
313 fn to_wgsl(self) -> &'static str {
314 use crate::ImageDimension as IDim;
315
316 match self {
317 IDim::D1 => "1d",
318 IDim::D2 => "2d",
319 IDim::D3 => "3d",
320 IDim::Cube => "cube",
321 }
322 }
323}
324
325pub const fn address_space_str(
344 space: crate::AddressSpace,
345) -> (Option<&'static str>, Option<&'static str>) {
346 use crate::AddressSpace as As;
347
348 (
349 Some(match space {
350 As::Private => "private",
351 As::Uniform => "uniform",
352 As::Storage { access } => {
353 if access.contains(crate::StorageAccess::ATOMIC) {
354 return (Some("storage"), Some("atomic"));
355 } else if access.contains(crate::StorageAccess::STORE) {
356 return (Some("storage"), Some("read_write"));
357 } else {
358 "storage"
359 }
360 }
361 As::PushConstant => "push_constant",
362 As::WorkGroup => "workgroup",
363 As::Handle => return (None, None),
364 As::Function => "function",
365 As::TaskPayload => return (None, None),
366 }),
367 None,
368 )
369}