naga/front/wgsl/parse/
conv.rs

1use crate::front::wgsl::parse::directive::enable_extension::{
2    EnableExtensions, ImplementedEnableExtension,
3};
4use crate::front::wgsl::{Error, Result, Scalar};
5use crate::{ImageClass, ImageDimension, Span, TypeInner, VectorSize};
6
7use alloc::boxed::Box;
8
9pub fn map_address_space<'a>(
10    word: &str,
11    span: Span,
12    enable_extensions: &EnableExtensions,
13) -> Result<'a, crate::AddressSpace> {
14    match word {
15        "private" => Ok(crate::AddressSpace::Private),
16        "workgroup" => Ok(crate::AddressSpace::WorkGroup),
17        "uniform" => Ok(crate::AddressSpace::Uniform),
18        "storage" => Ok(crate::AddressSpace::Storage {
19            access: crate::StorageAccess::default(),
20        }),
21        "immediate" => Ok(crate::AddressSpace::Immediate),
22        "function" => Ok(crate::AddressSpace::Function),
23        "task_payload" => {
24            enable_extensions.require(ImplementedEnableExtension::WgpuMeshShader, span)?;
25            Ok(crate::AddressSpace::TaskPayload)
26        }
27        "ray_payload" => {
28            if enable_extensions.contains(ImplementedEnableExtension::WgpuRayTracingPipeline) {
29                Ok(crate::AddressSpace::RayPayload)
30            } else {
31                Err(Box::new(Error::EnableExtensionNotEnabled {
32                    span,
33                    kind: ImplementedEnableExtension::WgpuRayTracingPipeline.into(),
34                }))
35            }
36        }
37        "incoming_ray_payload" => {
38            if enable_extensions.contains(ImplementedEnableExtension::WgpuRayTracingPipeline) {
39                Ok(crate::AddressSpace::IncomingRayPayload)
40            } else {
41                Err(Box::new(Error::EnableExtensionNotEnabled {
42                    span,
43                    kind: ImplementedEnableExtension::WgpuRayTracingPipeline.into(),
44                }))
45            }
46        }
47        _ => Err(Box::new(Error::UnknownAddressSpace(span))),
48    }
49}
50
51pub fn map_access_mode(word: &str, span: Span) -> Result<'_, crate::StorageAccess> {
52    match word {
53        "read" => Ok(crate::StorageAccess::LOAD),
54        "write" => Ok(crate::StorageAccess::STORE),
55        "read_write" => Ok(crate::StorageAccess::LOAD | crate::StorageAccess::STORE),
56        "atomic" => Ok(crate::StorageAccess::ATOMIC
57            | crate::StorageAccess::LOAD
58            | crate::StorageAccess::STORE),
59        _ => Err(Box::new(Error::UnknownAccess(span))),
60    }
61}
62
63pub fn map_ray_flag(
64    enable_extensions: &EnableExtensions,
65    word: &str,
66    span: Span,
67) -> Result<'static, ()> {
68    match word {
69        "vertex_return" => {
70            if !enable_extensions.contains(ImplementedEnableExtension::WgpuRayQueryVertexReturn) {
71                return Err(Box::new(Error::EnableExtensionNotEnabled {
72                    span,
73                    kind: ImplementedEnableExtension::WgpuRayQueryVertexReturn.into(),
74                }));
75            }
76            Ok(())
77        }
78        _ => Err(Box::new(Error::UnknownRayFlag(span))),
79    }
80}
81
82pub fn map_cooperative_role(word: &str, span: Span) -> Result<'_, crate::CooperativeRole> {
83    match word {
84        "A" => Ok(crate::CooperativeRole::A),
85        "B" => Ok(crate::CooperativeRole::B),
86        "C" => Ok(crate::CooperativeRole::C),
87        _ => Err(Box::new(Error::UnknownAccess(span))),
88    }
89}
90
91pub fn map_built_in(
92    enable_extensions: &EnableExtensions,
93    word: &str,
94    span: Span,
95) -> Result<'static, crate::BuiltIn> {
96    let built_in = match word {
97        "position" => crate::BuiltIn::Position { invariant: false },
98        // vertex
99        "vertex_index" => crate::BuiltIn::VertexIndex,
100        "instance_index" => crate::BuiltIn::InstanceIndex,
101        "view_index" => crate::BuiltIn::ViewIndex,
102        "clip_distances" => crate::BuiltIn::ClipDistance,
103        // fragment
104        "front_facing" => crate::BuiltIn::FrontFacing,
105        "frag_depth" => crate::BuiltIn::FragDepth,
106        "primitive_index" => crate::BuiltIn::PrimitiveIndex,
107        "draw_index" => crate::BuiltIn::DrawIndex,
108        "barycentric" => crate::BuiltIn::Barycentric { perspective: true },
109        "barycentric_no_perspective" => crate::BuiltIn::Barycentric { perspective: false },
110        "sample_index" => crate::BuiltIn::SampleIndex,
111        "sample_mask" => crate::BuiltIn::SampleMask,
112        // compute
113        "global_invocation_id" => crate::BuiltIn::GlobalInvocationId,
114        "local_invocation_id" => crate::BuiltIn::LocalInvocationId,
115        "local_invocation_index" => crate::BuiltIn::LocalInvocationIndex,
116        "workgroup_id" => crate::BuiltIn::WorkGroupId,
117        "num_workgroups" => crate::BuiltIn::NumWorkGroups,
118        // subgroup
119        "num_subgroups" => crate::BuiltIn::NumSubgroups,
120        "subgroup_id" => crate::BuiltIn::SubgroupId,
121        "subgroup_size" => crate::BuiltIn::SubgroupSize,
122        "subgroup_invocation_id" => crate::BuiltIn::SubgroupInvocationId,
123        // mesh
124        "cull_primitive" => crate::BuiltIn::CullPrimitive,
125        "point_index" => crate::BuiltIn::PointIndex,
126        "line_indices" => crate::BuiltIn::LineIndices,
127        "triangle_indices" => crate::BuiltIn::TriangleIndices,
128        "mesh_task_size" => crate::BuiltIn::MeshTaskSize,
129        // mesh global variable
130        "vertex_count" => crate::BuiltIn::VertexCount,
131        "vertices" => crate::BuiltIn::Vertices,
132        "primitive_count" => crate::BuiltIn::PrimitiveCount,
133        "primitives" => crate::BuiltIn::Primitives,
134        // ray tracing pipeline
135        "ray_invocation_id" => crate::BuiltIn::RayInvocationId,
136        "num_ray_invocations" => crate::BuiltIn::NumRayInvocations,
137        "instance_custom_data" => crate::BuiltIn::InstanceCustomData,
138        "geometry_index" => crate::BuiltIn::GeometryIndex,
139        "world_ray_origin" => crate::BuiltIn::WorldRayOrigin,
140        "world_ray_direction" => crate::BuiltIn::WorldRayDirection,
141        "object_ray_origin" => crate::BuiltIn::ObjectRayOrigin,
142        "object_ray_direction" => crate::BuiltIn::ObjectRayDirection,
143        "ray_t_min" => crate::BuiltIn::RayTmin,
144        "ray_t_current_max" => crate::BuiltIn::RayTCurrentMax,
145        "object_to_world" => crate::BuiltIn::ObjectToWorld,
146        "world_to_object" => crate::BuiltIn::WorldToObject,
147        "hit_kind" => crate::BuiltIn::HitKind,
148        _ => return Err(Box::new(Error::UnknownBuiltin(span))),
149    };
150    match built_in {
151        crate::BuiltIn::ClipDistance => {
152            enable_extensions.require(ImplementedEnableExtension::ClipDistances, span)?
153        }
154
155        crate::BuiltIn::PrimitiveIndex => {
156            enable_extensions.require(ImplementedEnableExtension::PrimitiveIndex, span)?
157        }
158        crate::BuiltIn::DrawIndex => {
159            if !enable_extensions.contains(ImplementedEnableExtension::DrawIndex) {
160                return Err(Box::new(Error::EnableExtensionNotEnabled {
161                    span,
162                    kind: ImplementedEnableExtension::DrawIndex.into(),
163                }));
164            }
165        }
166        crate::BuiltIn::CullPrimitive
167        | crate::BuiltIn::PointIndex
168        | crate::BuiltIn::LineIndices
169        | crate::BuiltIn::TriangleIndices
170        | crate::BuiltIn::VertexCount
171        | crate::BuiltIn::Vertices
172        | crate::BuiltIn::PrimitiveCount
173        | crate::BuiltIn::Primitives => {
174            enable_extensions.require(ImplementedEnableExtension::WgpuMeshShader, span)?
175        }
176        _ => {}
177    }
178    Ok(built_in)
179}
180
181pub fn map_interpolation(word: &str, span: Span) -> Result<'_, crate::Interpolation> {
182    match word {
183        "linear" => Ok(crate::Interpolation::Linear),
184        "flat" => Ok(crate::Interpolation::Flat),
185        "perspective" => Ok(crate::Interpolation::Perspective),
186        "per_vertex" => Ok(crate::Interpolation::PerVertex),
187        _ => Err(Box::new(Error::UnknownAttribute(span))),
188    }
189}
190
191pub fn map_sampling(word: &str, span: Span) -> Result<'_, crate::Sampling> {
192    match word {
193        "center" => Ok(crate::Sampling::Center),
194        "centroid" => Ok(crate::Sampling::Centroid),
195        "sample" => Ok(crate::Sampling::Sample),
196        "first" => Ok(crate::Sampling::First),
197        "either" => Ok(crate::Sampling::Either),
198        _ => Err(Box::new(Error::UnknownAttribute(span))),
199    }
200}
201
202pub fn map_storage_format(word: &str, span: Span) -> Result<'_, crate::StorageFormat> {
203    use crate::StorageFormat as Sf;
204    Ok(match word {
205        "r8unorm" => Sf::R8Unorm,
206        "r8snorm" => Sf::R8Snorm,
207        "r8uint" => Sf::R8Uint,
208        "r8sint" => Sf::R8Sint,
209        "r16unorm" => Sf::R16Unorm,
210        "r16snorm" => Sf::R16Snorm,
211        "r16uint" => Sf::R16Uint,
212        "r16sint" => Sf::R16Sint,
213        "r16float" => Sf::R16Float,
214        "rg8unorm" => Sf::Rg8Unorm,
215        "rg8snorm" => Sf::Rg8Snorm,
216        "rg8uint" => Sf::Rg8Uint,
217        "rg8sint" => Sf::Rg8Sint,
218        "r32uint" => Sf::R32Uint,
219        "r32sint" => Sf::R32Sint,
220        "r32float" => Sf::R32Float,
221        "rg16unorm" => Sf::Rg16Unorm,
222        "rg16snorm" => Sf::Rg16Snorm,
223        "rg16uint" => Sf::Rg16Uint,
224        "rg16sint" => Sf::Rg16Sint,
225        "rg16float" => Sf::Rg16Float,
226        "rgba8unorm" => Sf::Rgba8Unorm,
227        "rgba8snorm" => Sf::Rgba8Snorm,
228        "rgba8uint" => Sf::Rgba8Uint,
229        "rgba8sint" => Sf::Rgba8Sint,
230        "rgb10a2uint" => Sf::Rgb10a2Uint,
231        "rgb10a2unorm" => Sf::Rgb10a2Unorm,
232        "rg11b10ufloat" => Sf::Rg11b10Ufloat,
233        "r64uint" => Sf::R64Uint,
234        "rg32uint" => Sf::Rg32Uint,
235        "rg32sint" => Sf::Rg32Sint,
236        "rg32float" => Sf::Rg32Float,
237        "rgba16unorm" => Sf::Rgba16Unorm,
238        "rgba16snorm" => Sf::Rgba16Snorm,
239        "rgba16uint" => Sf::Rgba16Uint,
240        "rgba16sint" => Sf::Rgba16Sint,
241        "rgba16float" => Sf::Rgba16Float,
242        "rgba32uint" => Sf::Rgba32Uint,
243        "rgba32sint" => Sf::Rgba32Sint,
244        "rgba32float" => Sf::Rgba32Float,
245        "bgra8unorm" => Sf::Bgra8Unorm,
246        _ => return Err(Box::new(Error::UnknownStorageFormat(span))),
247    })
248}
249
250pub fn map_derivative(word: &str) -> Option<(crate::DerivativeAxis, crate::DerivativeControl)> {
251    use crate::{DerivativeAxis as Axis, DerivativeControl as Ctrl};
252    match word {
253        "dpdxCoarse" => Some((Axis::X, Ctrl::Coarse)),
254        "dpdyCoarse" => Some((Axis::Y, Ctrl::Coarse)),
255        "fwidthCoarse" => Some((Axis::Width, Ctrl::Coarse)),
256        "dpdxFine" => Some((Axis::X, Ctrl::Fine)),
257        "dpdyFine" => Some((Axis::Y, Ctrl::Fine)),
258        "fwidthFine" => Some((Axis::Width, Ctrl::Fine)),
259        "dpdx" => Some((Axis::X, Ctrl::None)),
260        "dpdy" => Some((Axis::Y, Ctrl::None)),
261        "fwidth" => Some((Axis::Width, Ctrl::None)),
262        _ => None,
263    }
264}
265
266pub fn map_relational_fun(word: &str) -> Option<crate::RelationalFunction> {
267    match word {
268        "any" => Some(crate::RelationalFunction::Any),
269        "all" => Some(crate::RelationalFunction::All),
270        _ => None,
271    }
272}
273
274pub fn map_standard_fun(word: &str) -> Option<crate::MathFunction> {
275    use crate::MathFunction as Mf;
276    Some(match word {
277        // comparison
278        "abs" => Mf::Abs,
279        "min" => Mf::Min,
280        "max" => Mf::Max,
281        "clamp" => Mf::Clamp,
282        "saturate" => Mf::Saturate,
283        // trigonometry
284        "cos" => Mf::Cos,
285        "cosh" => Mf::Cosh,
286        "sin" => Mf::Sin,
287        "sinh" => Mf::Sinh,
288        "tan" => Mf::Tan,
289        "tanh" => Mf::Tanh,
290        "acos" => Mf::Acos,
291        "acosh" => Mf::Acosh,
292        "asin" => Mf::Asin,
293        "asinh" => Mf::Asinh,
294        "atan" => Mf::Atan,
295        "atanh" => Mf::Atanh,
296        "atan2" => Mf::Atan2,
297        "radians" => Mf::Radians,
298        "degrees" => Mf::Degrees,
299        // decomposition
300        "ceil" => Mf::Ceil,
301        "floor" => Mf::Floor,
302        "round" => Mf::Round,
303        "fract" => Mf::Fract,
304        "trunc" => Mf::Trunc,
305        "modf" => Mf::Modf,
306        "frexp" => Mf::Frexp,
307        "ldexp" => Mf::Ldexp,
308        // exponent
309        "exp" => Mf::Exp,
310        "exp2" => Mf::Exp2,
311        "log" => Mf::Log,
312        "log2" => Mf::Log2,
313        "pow" => Mf::Pow,
314        // geometry
315        "dot" => Mf::Dot,
316        "dot4I8Packed" => Mf::Dot4I8Packed,
317        "dot4U8Packed" => Mf::Dot4U8Packed,
318        "cross" => Mf::Cross,
319        "distance" => Mf::Distance,
320        "length" => Mf::Length,
321        "normalize" => Mf::Normalize,
322        "faceForward" => Mf::FaceForward,
323        "reflect" => Mf::Reflect,
324        "refract" => Mf::Refract,
325        // computational
326        "sign" => Mf::Sign,
327        "fma" => Mf::Fma,
328        "mix" => Mf::Mix,
329        "step" => Mf::Step,
330        "smoothstep" => Mf::SmoothStep,
331        "sqrt" => Mf::Sqrt,
332        "inverseSqrt" => Mf::InverseSqrt,
333        "transpose" => Mf::Transpose,
334        "determinant" => Mf::Determinant,
335        "quantizeToF16" => Mf::QuantizeToF16,
336        // bits
337        "countTrailingZeros" => Mf::CountTrailingZeros,
338        "countLeadingZeros" => Mf::CountLeadingZeros,
339        "countOneBits" => Mf::CountOneBits,
340        "reverseBits" => Mf::ReverseBits,
341        "extractBits" => Mf::ExtractBits,
342        "insertBits" => Mf::InsertBits,
343        "firstTrailingBit" => Mf::FirstTrailingBit,
344        "firstLeadingBit" => Mf::FirstLeadingBit,
345        // data packing
346        "pack4x8snorm" => Mf::Pack4x8snorm,
347        "pack4x8unorm" => Mf::Pack4x8unorm,
348        "pack2x16snorm" => Mf::Pack2x16snorm,
349        "pack2x16unorm" => Mf::Pack2x16unorm,
350        "pack2x16float" => Mf::Pack2x16float,
351        "pack4xI8" => Mf::Pack4xI8,
352        "pack4xU8" => Mf::Pack4xU8,
353        "pack4xI8Clamp" => Mf::Pack4xI8Clamp,
354        "pack4xU8Clamp" => Mf::Pack4xU8Clamp,
355        // data unpacking
356        "unpack4x8snorm" => Mf::Unpack4x8snorm,
357        "unpack4x8unorm" => Mf::Unpack4x8unorm,
358        "unpack2x16snorm" => Mf::Unpack2x16snorm,
359        "unpack2x16unorm" => Mf::Unpack2x16unorm,
360        "unpack2x16float" => Mf::Unpack2x16float,
361        "unpack4xI8" => Mf::Unpack4xI8,
362        "unpack4xU8" => Mf::Unpack4xU8,
363        _ => return None,
364    })
365}
366
367pub fn map_conservative_depth(word: &str, span: Span) -> Result<'_, crate::ConservativeDepth> {
368    use crate::ConservativeDepth as Cd;
369    match word {
370        "greater_equal" => Ok(Cd::GreaterEqual),
371        "less_equal" => Ok(Cd::LessEqual),
372        "unchanged" => Ok(Cd::Unchanged),
373        _ => Err(Box::new(Error::UnknownConservativeDepth(span))),
374    }
375}
376
377pub fn map_subgroup_operation(
378    word: &str,
379) -> Option<(crate::SubgroupOperation, crate::CollectiveOperation)> {
380    use crate::CollectiveOperation as co;
381    use crate::SubgroupOperation as sg;
382    Some(match word {
383        "subgroupAll" => (sg::All, co::Reduce),
384        "subgroupAny" => (sg::Any, co::Reduce),
385        "subgroupAdd" => (sg::Add, co::Reduce),
386        "subgroupMul" => (sg::Mul, co::Reduce),
387        "subgroupMin" => (sg::Min, co::Reduce),
388        "subgroupMax" => (sg::Max, co::Reduce),
389        "subgroupAnd" => (sg::And, co::Reduce),
390        "subgroupOr" => (sg::Or, co::Reduce),
391        "subgroupXor" => (sg::Xor, co::Reduce),
392        "subgroupExclusiveAdd" => (sg::Add, co::ExclusiveScan),
393        "subgroupExclusiveMul" => (sg::Mul, co::ExclusiveScan),
394        "subgroupInclusiveAdd" => (sg::Add, co::InclusiveScan),
395        "subgroupInclusiveMul" => (sg::Mul, co::InclusiveScan),
396        _ => return None,
397    })
398}
399
400pub enum TypeGenerator {
401    Vector {
402        size: VectorSize,
403    },
404    Matrix {
405        columns: VectorSize,
406        rows: VectorSize,
407    },
408    Array,
409    Atomic,
410    Pointer,
411    SampledTexture {
412        dim: ImageDimension,
413        arrayed: bool,
414        multi: bool,
415    },
416    StorageTexture {
417        dim: ImageDimension,
418        arrayed: bool,
419    },
420    BindingArray,
421    AccelerationStructure,
422    RayQuery,
423    CooperativeMatrix {
424        columns: crate::CooperativeSize,
425        rows: crate::CooperativeSize,
426    },
427}
428
429pub enum PredeclaredType {
430    TypeInner(TypeInner),
431    RayDesc,
432    RayIntersection,
433    TypeGenerator(TypeGenerator),
434}
435impl From<TypeInner> for PredeclaredType {
436    fn from(value: TypeInner) -> Self {
437        Self::TypeInner(value)
438    }
439}
440impl From<TypeGenerator> for PredeclaredType {
441    fn from(value: TypeGenerator) -> Self {
442        Self::TypeGenerator(value)
443    }
444}
445
446pub fn map_predeclared_type(
447    enable_extensions: &EnableExtensions,
448    span: Span,
449    word: &str,
450) -> Result<'static, Option<PredeclaredType>> {
451    use Scalar as Sc;
452    use TypeInner as Ti;
453    use VectorSize as Vs;
454
455    #[rustfmt::skip]
456    let ty = match word {
457        // predeclared types
458
459        // scalars
460        "bool" => Ti::Scalar(Sc::BOOL).into(),
461        "i32" => Ti::Scalar(Sc::I32).into(),
462        "u32" => Ti::Scalar(Sc::U32).into(),
463        "f32" => Ti::Scalar(Sc::F32).into(),
464        "f16" => Ti::Scalar(Sc::F16).into(),
465        "i64" => Ti::Scalar(Sc::I64).into(),
466        "u64" => Ti::Scalar(Sc::U64).into(),
467        "f64" => Ti::Scalar(Sc::F64).into(),
468        // vector aliases
469        "vec2i" => Ti::Vector { size: Vs::Bi,   scalar: Sc::I32 }.into(),
470        "vec3i" => Ti::Vector { size: Vs::Tri,  scalar: Sc::I32 }.into(),
471        "vec4i" => Ti::Vector { size: Vs::Quad, scalar: Sc::I32 }.into(),
472        "vec2u" => Ti::Vector { size: Vs::Bi,   scalar: Sc::U32 }.into(),
473        "vec3u" => Ti::Vector { size: Vs::Tri,  scalar: Sc::U32 }.into(),
474        "vec4u" => Ti::Vector { size: Vs::Quad, scalar: Sc::U32 }.into(),
475        "vec2f" => Ti::Vector { size: Vs::Bi,   scalar: Sc::F32 }.into(),
476        "vec3f" => Ti::Vector { size: Vs::Tri,  scalar: Sc::F32 }.into(),
477        "vec4f" => Ti::Vector { size: Vs::Quad, scalar: Sc::F32 }.into(),
478        "vec2h" => Ti::Vector { size: Vs::Bi,   scalar: Sc::F16 }.into(),
479        "vec3h" => Ti::Vector { size: Vs::Tri,  scalar: Sc::F16 }.into(),
480        "vec4h" => Ti::Vector { size: Vs::Quad, scalar: Sc::F16 }.into(),
481        // matrix aliases
482        "mat2x2f" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Bi,   scalar: Sc::F32 }.into(),
483        "mat2x3f" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Tri,  scalar: Sc::F32 }.into(),
484        "mat2x4f" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Quad, scalar: Sc::F32 }.into(),
485        "mat3x2f" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Bi,   scalar: Sc::F32 }.into(),
486        "mat3x3f" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Tri,  scalar: Sc::F32 }.into(),
487        "mat3x4f" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Quad, scalar: Sc::F32 }.into(),
488        "mat4x2f" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Bi,   scalar: Sc::F32 }.into(),
489        "mat4x3f" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Tri,  scalar: Sc::F32 }.into(),
490        "mat4x4f" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Quad, scalar: Sc::F32 }.into(),
491        "mat2x2h" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Bi,   scalar: Sc::F16 }.into(),
492        "mat2x3h" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Tri,  scalar: Sc::F16 }.into(),
493        "mat2x4h" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Quad, scalar: Sc::F16 }.into(),
494        "mat3x2h" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Bi,   scalar: Sc::F16 }.into(),
495        "mat3x3h" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Tri,  scalar: Sc::F16 }.into(),
496        "mat3x4h" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Quad, scalar: Sc::F16 }.into(),
497        "mat4x2h" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Bi,   scalar: Sc::F16 }.into(),
498        "mat4x3h" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Tri,  scalar: Sc::F16 }.into(),
499        "mat4x4h" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Quad, scalar: Sc::F16 }.into(),
500        // samplers
501        "sampler" =>            Ti::Sampler { comparison: false }.into(),
502        "sampler_comparison" => Ti::Sampler { comparison: true }.into(),
503        // depth textures
504        "texture_depth_2d" =>              Ti::Image { dim: ImageDimension::D2,   arrayed: false, class: ImageClass::Depth { multi: false } }.into(),
505        "texture_depth_2d_array" =>        Ti::Image { dim: ImageDimension::D2,   arrayed: true,  class: ImageClass::Depth { multi: false } }.into(),
506        "texture_depth_cube" =>            Ti::Image { dim: ImageDimension::Cube, arrayed: false, class: ImageClass::Depth { multi: false } }.into(),
507        "texture_depth_cube_array" =>      Ti::Image { dim: ImageDimension::Cube, arrayed: true,  class: ImageClass::Depth { multi: false } }.into(),
508        "texture_depth_multisampled_2d" => Ti::Image { dim: ImageDimension::D2,   arrayed: false, class: ImageClass::Depth { multi: true  } }.into(),
509        // external texture
510        "texture_external" => Ti::Image { dim: ImageDimension::D2, arrayed: false, class: ImageClass::External }.into(),
511        // ray desc
512        "RayDesc" => PredeclaredType::RayDesc,
513        // ray intersection
514        "RayIntersection" => PredeclaredType::RayIntersection,
515
516        // predeclared type generators
517
518        // vector
519        "vec2" => TypeGenerator::Vector { size: Vs::Bi   }.into(),
520        "vec3" => TypeGenerator::Vector { size: Vs::Tri  }.into(),
521        "vec4" => TypeGenerator::Vector { size: Vs::Quad }.into(),
522        // matrix
523        "mat2x2" => TypeGenerator::Matrix { columns: Vs::Bi,   rows: Vs::Bi   }.into(),
524        "mat2x3" => TypeGenerator::Matrix { columns: Vs::Bi,   rows: Vs::Tri  }.into(),
525        "mat2x4" => TypeGenerator::Matrix { columns: Vs::Bi,   rows: Vs::Quad }.into(),
526        "mat3x2" => TypeGenerator::Matrix { columns: Vs::Tri,  rows: Vs::Bi   }.into(),
527        "mat3x3" => TypeGenerator::Matrix { columns: Vs::Tri,  rows: Vs::Tri  }.into(),
528        "mat3x4" => TypeGenerator::Matrix { columns: Vs::Tri,  rows: Vs::Quad }.into(),
529        "mat4x2" => TypeGenerator::Matrix { columns: Vs::Quad, rows: Vs::Bi   }.into(),
530        "mat4x3" => TypeGenerator::Matrix { columns: Vs::Quad, rows: Vs::Tri  }.into(),
531        "mat4x4" => TypeGenerator::Matrix { columns: Vs::Quad, rows: Vs::Quad }.into(),
532        // array
533        "array" => TypeGenerator::Array.into(),
534        // atomic
535        "atomic" => TypeGenerator::Atomic.into(),
536        // pointer
537        "ptr" => TypeGenerator::Pointer.into(),
538        // sampled textures
539        "texture_1d" =>               TypeGenerator::SampledTexture { dim: ImageDimension::D1,   arrayed: false, multi: false }.into(),
540        "texture_2d" =>               TypeGenerator::SampledTexture { dim: ImageDimension::D2,   arrayed: false, multi: false }.into(),
541        "texture_2d_array" =>         TypeGenerator::SampledTexture { dim: ImageDimension::D2,   arrayed: true,  multi: false }.into(),
542        "texture_3d" =>               TypeGenerator::SampledTexture { dim: ImageDimension::D3,   arrayed: false, multi: false }.into(),
543        "texture_cube" =>             TypeGenerator::SampledTexture { dim: ImageDimension::Cube, arrayed: false, multi: false }.into(),
544        "texture_cube_array" =>       TypeGenerator::SampledTexture { dim: ImageDimension::Cube, arrayed: true,  multi: false }.into(),
545        "texture_multisampled_2d" =>  TypeGenerator::SampledTexture { dim: ImageDimension::D2,   arrayed: false, multi: true  }.into(),
546        // storage textures
547        "texture_storage_1d" =>       TypeGenerator::StorageTexture { dim: ImageDimension::D1,   arrayed: false }.into(),
548        "texture_storage_2d" =>       TypeGenerator::StorageTexture { dim: ImageDimension::D2,   arrayed: false }.into(),
549        "texture_storage_2d_array" => TypeGenerator::StorageTexture { dim: ImageDimension::D2,   arrayed: true  }.into(),
550        "texture_storage_3d" =>       TypeGenerator::StorageTexture { dim: ImageDimension::D3,   arrayed: false }.into(),
551        // binding array
552        "binding_array" => TypeGenerator::BindingArray.into(),
553        // acceleration structure
554        "acceleration_structure" => TypeGenerator::AccelerationStructure.into(),
555        // ray query
556        "ray_query" => TypeGenerator::RayQuery.into(),
557        // cooperative matrix
558        "coop_mat8x8" => TypeGenerator::CooperativeMatrix {
559            columns: crate::CooperativeSize::Eight,
560            rows: crate::CooperativeSize::Eight,
561        }.into(),
562        "coop_mat16x16" => TypeGenerator::CooperativeMatrix {
563            columns: crate::CooperativeSize::Sixteen,
564            rows: crate::CooperativeSize::Sixteen,
565        }.into(),
566        _ => return Ok(None),
567    };
568
569    // Check for the enable extension required to use this type, if any.
570    // Slice should be at least len one otherwise extension_needed should be None.
571    let extensions_needed: Option<&[_]> = match ty {
572        PredeclaredType::TypeInner(ref ty) if ty.scalar() == Some(Sc::F16) => {
573            Some(&[ImplementedEnableExtension::F16])
574        }
575        PredeclaredType::RayDesc
576        | PredeclaredType::RayIntersection
577        | PredeclaredType::TypeGenerator(TypeGenerator::AccelerationStructure)
578        | PredeclaredType::TypeGenerator(TypeGenerator::RayQuery) => Some(&[
579            ImplementedEnableExtension::WgpuRayQuery,
580            ImplementedEnableExtension::WgpuRayTracingPipeline,
581        ]),
582        PredeclaredType::TypeGenerator(TypeGenerator::CooperativeMatrix { .. }) => {
583            Some(&[ImplementedEnableExtension::WgpuCooperativeMatrix])
584        }
585        _ => None,
586    };
587    if let Some(extensions_needed) = extensions_needed {
588        let mut any_extension_enabled = false;
589        for extension_needed in extensions_needed {
590            if enable_extensions.contains(*extension_needed) {
591                any_extension_enabled = true;
592            }
593        }
594        if !any_extension_enabled {
595            return Err(Box::new(Error::EnableExtensionNotEnabled {
596                span,
597                kind: extensions_needed[0].into(),
598            }));
599        }
600    }
601
602    Ok(Some(ty))
603}