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::ClipDistances,
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::ClipDistances => {
152            enable_extensions.require(ImplementedEnableExtension::ClipDistances, span)?
153        }
154        crate::BuiltIn::PrimitiveIndex => {
155            enable_extensions.require(ImplementedEnableExtension::PrimitiveIndex, span)?
156        }
157        crate::BuiltIn::DrawIndex => {
158            enable_extensions.require(ImplementedEnableExtension::DrawIndex, span)?
159        }
160        crate::BuiltIn::CullPrimitive
161        | crate::BuiltIn::PointIndex
162        | crate::BuiltIn::LineIndices
163        | crate::BuiltIn::TriangleIndices
164        | crate::BuiltIn::VertexCount
165        | crate::BuiltIn::Vertices
166        | crate::BuiltIn::PrimitiveCount
167        | crate::BuiltIn::Primitives => {
168            enable_extensions.require(ImplementedEnableExtension::WgpuMeshShader, span)?
169        }
170        _ => {}
171    }
172    Ok(built_in)
173}
174
175pub fn map_interpolation(
176    enable_extensions: &EnableExtensions,
177    word: &str,
178    span: Span,
179) -> Result<'static, crate::Interpolation> {
180    match word {
181        "linear" => Ok(crate::Interpolation::Linear),
182        "flat" => Ok(crate::Interpolation::Flat),
183        "perspective" => Ok(crate::Interpolation::Perspective),
184        "per_vertex" => {
185            enable_extensions.require(ImplementedEnableExtension::WgpuPerVertex, span)?;
186            Ok(crate::Interpolation::PerVertex)
187        }
188        _ => Err(Box::new(Error::UnknownAttribute(span))),
189    }
190}
191
192pub fn map_sampling(word: &str, span: Span) -> Result<'_, crate::Sampling> {
193    match word {
194        "center" => Ok(crate::Sampling::Center),
195        "centroid" => Ok(crate::Sampling::Centroid),
196        "sample" => Ok(crate::Sampling::Sample),
197        "first" => Ok(crate::Sampling::First),
198        "either" => Ok(crate::Sampling::Either),
199        _ => Err(Box::new(Error::UnknownAttribute(span))),
200    }
201}
202
203pub fn map_storage_format(word: &str, span: Span) -> Result<'_, crate::StorageFormat> {
204    use crate::StorageFormat as Sf;
205    Ok(match word {
206        "r8unorm" => Sf::R8Unorm,
207        "r8snorm" => Sf::R8Snorm,
208        "r8uint" => Sf::R8Uint,
209        "r8sint" => Sf::R8Sint,
210        "r16unorm" => Sf::R16Unorm,
211        "r16snorm" => Sf::R16Snorm,
212        "r16uint" => Sf::R16Uint,
213        "r16sint" => Sf::R16Sint,
214        "r16float" => Sf::R16Float,
215        "rg8unorm" => Sf::Rg8Unorm,
216        "rg8snorm" => Sf::Rg8Snorm,
217        "rg8uint" => Sf::Rg8Uint,
218        "rg8sint" => Sf::Rg8Sint,
219        "r32uint" => Sf::R32Uint,
220        "r32sint" => Sf::R32Sint,
221        "r32float" => Sf::R32Float,
222        "rg16unorm" => Sf::Rg16Unorm,
223        "rg16snorm" => Sf::Rg16Snorm,
224        "rg16uint" => Sf::Rg16Uint,
225        "rg16sint" => Sf::Rg16Sint,
226        "rg16float" => Sf::Rg16Float,
227        "rgba8unorm" => Sf::Rgba8Unorm,
228        "rgba8snorm" => Sf::Rgba8Snorm,
229        "rgba8uint" => Sf::Rgba8Uint,
230        "rgba8sint" => Sf::Rgba8Sint,
231        "rgb10a2uint" => Sf::Rgb10a2Uint,
232        "rgb10a2unorm" => Sf::Rgb10a2Unorm,
233        "rg11b10ufloat" => Sf::Rg11b10Ufloat,
234        "r64uint" => Sf::R64Uint,
235        "rg32uint" => Sf::Rg32Uint,
236        "rg32sint" => Sf::Rg32Sint,
237        "rg32float" => Sf::Rg32Float,
238        "rgba16unorm" => Sf::Rgba16Unorm,
239        "rgba16snorm" => Sf::Rgba16Snorm,
240        "rgba16uint" => Sf::Rgba16Uint,
241        "rgba16sint" => Sf::Rgba16Sint,
242        "rgba16float" => Sf::Rgba16Float,
243        "rgba32uint" => Sf::Rgba32Uint,
244        "rgba32sint" => Sf::Rgba32Sint,
245        "rgba32float" => Sf::Rgba32Float,
246        "bgra8unorm" => Sf::Bgra8Unorm,
247        _ => return Err(Box::new(Error::UnknownStorageFormat(span))),
248    })
249}
250
251pub fn map_derivative(word: &str) -> Option<(crate::DerivativeAxis, crate::DerivativeControl)> {
252    use crate::{DerivativeAxis as Axis, DerivativeControl as Ctrl};
253    match word {
254        "dpdxCoarse" => Some((Axis::X, Ctrl::Coarse)),
255        "dpdyCoarse" => Some((Axis::Y, Ctrl::Coarse)),
256        "fwidthCoarse" => Some((Axis::Width, Ctrl::Coarse)),
257        "dpdxFine" => Some((Axis::X, Ctrl::Fine)),
258        "dpdyFine" => Some((Axis::Y, Ctrl::Fine)),
259        "fwidthFine" => Some((Axis::Width, Ctrl::Fine)),
260        "dpdx" => Some((Axis::X, Ctrl::None)),
261        "dpdy" => Some((Axis::Y, Ctrl::None)),
262        "fwidth" => Some((Axis::Width, Ctrl::None)),
263        _ => None,
264    }
265}
266
267pub fn map_relational_fun(word: &str) -> Option<crate::RelationalFunction> {
268    match word {
269        "any" => Some(crate::RelationalFunction::Any),
270        "all" => Some(crate::RelationalFunction::All),
271        _ => None,
272    }
273}
274
275pub fn map_standard_fun(word: &str) -> Option<crate::MathFunction> {
276    use crate::MathFunction as Mf;
277    Some(match word {
278        // comparison
279        "abs" => Mf::Abs,
280        "min" => Mf::Min,
281        "max" => Mf::Max,
282        "clamp" => Mf::Clamp,
283        "saturate" => Mf::Saturate,
284        // trigonometry
285        "cos" => Mf::Cos,
286        "cosh" => Mf::Cosh,
287        "sin" => Mf::Sin,
288        "sinh" => Mf::Sinh,
289        "tan" => Mf::Tan,
290        "tanh" => Mf::Tanh,
291        "acos" => Mf::Acos,
292        "acosh" => Mf::Acosh,
293        "asin" => Mf::Asin,
294        "asinh" => Mf::Asinh,
295        "atan" => Mf::Atan,
296        "atanh" => Mf::Atanh,
297        "atan2" => Mf::Atan2,
298        "radians" => Mf::Radians,
299        "degrees" => Mf::Degrees,
300        // decomposition
301        "ceil" => Mf::Ceil,
302        "floor" => Mf::Floor,
303        "round" => Mf::Round,
304        "fract" => Mf::Fract,
305        "trunc" => Mf::Trunc,
306        "modf" => Mf::Modf,
307        "frexp" => Mf::Frexp,
308        "ldexp" => Mf::Ldexp,
309        // exponent
310        "exp" => Mf::Exp,
311        "exp2" => Mf::Exp2,
312        "log" => Mf::Log,
313        "log2" => Mf::Log2,
314        "pow" => Mf::Pow,
315        // geometry
316        "dot" => Mf::Dot,
317        "dot4I8Packed" => Mf::Dot4I8Packed,
318        "dot4U8Packed" => Mf::Dot4U8Packed,
319        "cross" => Mf::Cross,
320        "distance" => Mf::Distance,
321        "length" => Mf::Length,
322        "normalize" => Mf::Normalize,
323        "faceForward" => Mf::FaceForward,
324        "reflect" => Mf::Reflect,
325        "refract" => Mf::Refract,
326        // computational
327        "sign" => Mf::Sign,
328        "fma" => Mf::Fma,
329        "mix" => Mf::Mix,
330        "step" => Mf::Step,
331        "smoothstep" => Mf::SmoothStep,
332        "sqrt" => Mf::Sqrt,
333        "inverseSqrt" => Mf::InverseSqrt,
334        "transpose" => Mf::Transpose,
335        "determinant" => Mf::Determinant,
336        "quantizeToF16" => Mf::QuantizeToF16,
337        // bits
338        "countTrailingZeros" => Mf::CountTrailingZeros,
339        "countLeadingZeros" => Mf::CountLeadingZeros,
340        "countOneBits" => Mf::CountOneBits,
341        "reverseBits" => Mf::ReverseBits,
342        "extractBits" => Mf::ExtractBits,
343        "insertBits" => Mf::InsertBits,
344        "firstTrailingBit" => Mf::FirstTrailingBit,
345        "firstLeadingBit" => Mf::FirstLeadingBit,
346        // data packing
347        "pack4x8snorm" => Mf::Pack4x8snorm,
348        "pack4x8unorm" => Mf::Pack4x8unorm,
349        "pack2x16snorm" => Mf::Pack2x16snorm,
350        "pack2x16unorm" => Mf::Pack2x16unorm,
351        "pack2x16float" => Mf::Pack2x16float,
352        "pack4xI8" => Mf::Pack4xI8,
353        "pack4xU8" => Mf::Pack4xU8,
354        "pack4xI8Clamp" => Mf::Pack4xI8Clamp,
355        "pack4xU8Clamp" => Mf::Pack4xU8Clamp,
356        // data unpacking
357        "unpack4x8snorm" => Mf::Unpack4x8snorm,
358        "unpack4x8unorm" => Mf::Unpack4x8unorm,
359        "unpack2x16snorm" => Mf::Unpack2x16snorm,
360        "unpack2x16unorm" => Mf::Unpack2x16unorm,
361        "unpack2x16float" => Mf::Unpack2x16float,
362        "unpack4xI8" => Mf::Unpack4xI8,
363        "unpack4xU8" => Mf::Unpack4xU8,
364        _ => return None,
365    })
366}
367
368pub fn map_conservative_depth(word: &str, span: Span) -> Result<'_, crate::ConservativeDepth> {
369    use crate::ConservativeDepth as Cd;
370    match word {
371        "greater_equal" => Ok(Cd::GreaterEqual),
372        "less_equal" => Ok(Cd::LessEqual),
373        "unchanged" => Ok(Cd::Unchanged),
374        _ => Err(Box::new(Error::UnknownConservativeDepth(span))),
375    }
376}
377
378pub fn map_subgroup_operation(
379    word: &str,
380) -> Option<(crate::SubgroupOperation, crate::CollectiveOperation)> {
381    use crate::CollectiveOperation as co;
382    use crate::SubgroupOperation as sg;
383    Some(match word {
384        "subgroupAll" => (sg::All, co::Reduce),
385        "subgroupAny" => (sg::Any, co::Reduce),
386        "subgroupAdd" => (sg::Add, co::Reduce),
387        "subgroupMul" => (sg::Mul, co::Reduce),
388        "subgroupMin" => (sg::Min, co::Reduce),
389        "subgroupMax" => (sg::Max, co::Reduce),
390        "subgroupAnd" => (sg::And, co::Reduce),
391        "subgroupOr" => (sg::Or, co::Reduce),
392        "subgroupXor" => (sg::Xor, co::Reduce),
393        "subgroupExclusiveAdd" => (sg::Add, co::ExclusiveScan),
394        "subgroupExclusiveMul" => (sg::Mul, co::ExclusiveScan),
395        "subgroupInclusiveAdd" => (sg::Add, co::InclusiveScan),
396        "subgroupInclusiveMul" => (sg::Mul, co::InclusiveScan),
397        _ => return None,
398    })
399}
400
401pub enum TypeGenerator {
402    Vector {
403        size: VectorSize,
404    },
405    Matrix {
406        columns: VectorSize,
407        rows: VectorSize,
408    },
409    Array,
410    Atomic,
411    Pointer,
412    SampledTexture {
413        dim: ImageDimension,
414        arrayed: bool,
415        multi: bool,
416    },
417    StorageTexture {
418        dim: ImageDimension,
419        arrayed: bool,
420    },
421    BindingArray,
422    AccelerationStructure,
423    RayQuery,
424    CooperativeMatrix {
425        columns: crate::CooperativeSize,
426        rows: crate::CooperativeSize,
427    },
428}
429
430pub enum PredeclaredType {
431    TypeInner(TypeInner),
432    RayDesc,
433    RayIntersection,
434    TypeGenerator(TypeGenerator),
435}
436impl From<TypeInner> for PredeclaredType {
437    fn from(value: TypeInner) -> Self {
438        Self::TypeInner(value)
439    }
440}
441impl From<TypeGenerator> for PredeclaredType {
442    fn from(value: TypeGenerator) -> Self {
443        Self::TypeGenerator(value)
444    }
445}
446
447pub fn map_predeclared_type(
448    enable_extensions: &EnableExtensions,
449    span: Span,
450    word: &str,
451) -> Result<'static, Option<PredeclaredType>> {
452    use Scalar as Sc;
453    use TypeInner as Ti;
454    use VectorSize as Vs;
455
456    #[rustfmt::skip]
457    let ty = match word {
458        // predeclared types
459
460        // scalars
461        "bool" => Ti::Scalar(Sc::BOOL).into(),
462        "i32" => Ti::Scalar(Sc::I32).into(),
463        "u32" => Ti::Scalar(Sc::U32).into(),
464        "f32" => Ti::Scalar(Sc::F32).into(),
465        "f16" => Ti::Scalar(Sc::F16).into(),
466        "i64" => Ti::Scalar(Sc::I64).into(),
467        "u64" => Ti::Scalar(Sc::U64).into(),
468        "f64" => Ti::Scalar(Sc::F64).into(),
469        // vector aliases
470        "vec2i" => Ti::Vector { size: Vs::Bi,   scalar: Sc::I32 }.into(),
471        "vec3i" => Ti::Vector { size: Vs::Tri,  scalar: Sc::I32 }.into(),
472        "vec4i" => Ti::Vector { size: Vs::Quad, scalar: Sc::I32 }.into(),
473        "vec2u" => Ti::Vector { size: Vs::Bi,   scalar: Sc::U32 }.into(),
474        "vec3u" => Ti::Vector { size: Vs::Tri,  scalar: Sc::U32 }.into(),
475        "vec4u" => Ti::Vector { size: Vs::Quad, scalar: Sc::U32 }.into(),
476        "vec2f" => Ti::Vector { size: Vs::Bi,   scalar: Sc::F32 }.into(),
477        "vec3f" => Ti::Vector { size: Vs::Tri,  scalar: Sc::F32 }.into(),
478        "vec4f" => Ti::Vector { size: Vs::Quad, scalar: Sc::F32 }.into(),
479        "vec2h" => Ti::Vector { size: Vs::Bi,   scalar: Sc::F16 }.into(),
480        "vec3h" => Ti::Vector { size: Vs::Tri,  scalar: Sc::F16 }.into(),
481        "vec4h" => Ti::Vector { size: Vs::Quad, scalar: Sc::F16 }.into(),
482        // matrix aliases
483        "mat2x2f" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Bi,   scalar: Sc::F32 }.into(),
484        "mat2x3f" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Tri,  scalar: Sc::F32 }.into(),
485        "mat2x4f" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Quad, scalar: Sc::F32 }.into(),
486        "mat3x2f" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Bi,   scalar: Sc::F32 }.into(),
487        "mat3x3f" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Tri,  scalar: Sc::F32 }.into(),
488        "mat3x4f" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Quad, scalar: Sc::F32 }.into(),
489        "mat4x2f" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Bi,   scalar: Sc::F32 }.into(),
490        "mat4x3f" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Tri,  scalar: Sc::F32 }.into(),
491        "mat4x4f" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Quad, scalar: Sc::F32 }.into(),
492        "mat2x2h" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Bi,   scalar: Sc::F16 }.into(),
493        "mat2x3h" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Tri,  scalar: Sc::F16 }.into(),
494        "mat2x4h" => Ti::Matrix { columns: Vs::Bi,   rows: Vs::Quad, scalar: Sc::F16 }.into(),
495        "mat3x2h" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Bi,   scalar: Sc::F16 }.into(),
496        "mat3x3h" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Tri,  scalar: Sc::F16 }.into(),
497        "mat3x4h" => Ti::Matrix { columns: Vs::Tri,  rows: Vs::Quad, scalar: Sc::F16 }.into(),
498        "mat4x2h" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Bi,   scalar: Sc::F16 }.into(),
499        "mat4x3h" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Tri,  scalar: Sc::F16 }.into(),
500        "mat4x4h" => Ti::Matrix { columns: Vs::Quad, rows: Vs::Quad, scalar: Sc::F16 }.into(),
501        // samplers
502        "sampler" =>            Ti::Sampler { comparison: false }.into(),
503        "sampler_comparison" => Ti::Sampler { comparison: true }.into(),
504        // depth textures
505        "texture_depth_2d" =>              Ti::Image { dim: ImageDimension::D2,   arrayed: false, class: ImageClass::Depth { multi: false } }.into(),
506        "texture_depth_2d_array" =>        Ti::Image { dim: ImageDimension::D2,   arrayed: true,  class: ImageClass::Depth { multi: false } }.into(),
507        "texture_depth_cube" =>            Ti::Image { dim: ImageDimension::Cube, arrayed: false, class: ImageClass::Depth { multi: false } }.into(),
508        "texture_depth_cube_array" =>      Ti::Image { dim: ImageDimension::Cube, arrayed: true,  class: ImageClass::Depth { multi: false } }.into(),
509        "texture_depth_multisampled_2d" => Ti::Image { dim: ImageDimension::D2,   arrayed: false, class: ImageClass::Depth { multi: true  } }.into(),
510        // external texture
511        "texture_external" => Ti::Image { dim: ImageDimension::D2, arrayed: false, class: ImageClass::External }.into(),
512        // ray desc
513        "RayDesc" => PredeclaredType::RayDesc,
514        // ray intersection
515        "RayIntersection" => PredeclaredType::RayIntersection,
516
517        // predeclared type generators
518
519        // vector
520        "vec2" => TypeGenerator::Vector { size: Vs::Bi   }.into(),
521        "vec3" => TypeGenerator::Vector { size: Vs::Tri  }.into(),
522        "vec4" => TypeGenerator::Vector { size: Vs::Quad }.into(),
523        // matrix
524        "mat2x2" => TypeGenerator::Matrix { columns: Vs::Bi,   rows: Vs::Bi   }.into(),
525        "mat2x3" => TypeGenerator::Matrix { columns: Vs::Bi,   rows: Vs::Tri  }.into(),
526        "mat2x4" => TypeGenerator::Matrix { columns: Vs::Bi,   rows: Vs::Quad }.into(),
527        "mat3x2" => TypeGenerator::Matrix { columns: Vs::Tri,  rows: Vs::Bi   }.into(),
528        "mat3x3" => TypeGenerator::Matrix { columns: Vs::Tri,  rows: Vs::Tri  }.into(),
529        "mat3x4" => TypeGenerator::Matrix { columns: Vs::Tri,  rows: Vs::Quad }.into(),
530        "mat4x2" => TypeGenerator::Matrix { columns: Vs::Quad, rows: Vs::Bi   }.into(),
531        "mat4x3" => TypeGenerator::Matrix { columns: Vs::Quad, rows: Vs::Tri  }.into(),
532        "mat4x4" => TypeGenerator::Matrix { columns: Vs::Quad, rows: Vs::Quad }.into(),
533        // array
534        "array" => TypeGenerator::Array.into(),
535        // atomic
536        "atomic" => TypeGenerator::Atomic.into(),
537        // pointer
538        "ptr" => TypeGenerator::Pointer.into(),
539        // sampled textures
540        "texture_1d" =>               TypeGenerator::SampledTexture { dim: ImageDimension::D1,   arrayed: false, multi: false }.into(),
541        "texture_2d" =>               TypeGenerator::SampledTexture { dim: ImageDimension::D2,   arrayed: false, multi: false }.into(),
542        "texture_2d_array" =>         TypeGenerator::SampledTexture { dim: ImageDimension::D2,   arrayed: true,  multi: false }.into(),
543        "texture_3d" =>               TypeGenerator::SampledTexture { dim: ImageDimension::D3,   arrayed: false, multi: false }.into(),
544        "texture_cube" =>             TypeGenerator::SampledTexture { dim: ImageDimension::Cube, arrayed: false, multi: false }.into(),
545        "texture_cube_array" =>       TypeGenerator::SampledTexture { dim: ImageDimension::Cube, arrayed: true,  multi: false }.into(),
546        "texture_multisampled_2d" =>  TypeGenerator::SampledTexture { dim: ImageDimension::D2,   arrayed: false, multi: true  }.into(),
547        // storage textures
548        "texture_storage_1d" =>       TypeGenerator::StorageTexture { dim: ImageDimension::D1,   arrayed: false }.into(),
549        "texture_storage_2d" =>       TypeGenerator::StorageTexture { dim: ImageDimension::D2,   arrayed: false }.into(),
550        "texture_storage_2d_array" => TypeGenerator::StorageTexture { dim: ImageDimension::D2,   arrayed: true  }.into(),
551        "texture_storage_3d" =>       TypeGenerator::StorageTexture { dim: ImageDimension::D3,   arrayed: false }.into(),
552        // binding array
553        "binding_array" => TypeGenerator::BindingArray.into(),
554        // acceleration structure
555        "acceleration_structure" => TypeGenerator::AccelerationStructure.into(),
556        // ray query
557        "ray_query" => TypeGenerator::RayQuery.into(),
558        // cooperative matrix
559        "coop_mat8x8" => TypeGenerator::CooperativeMatrix {
560            columns: crate::CooperativeSize::Eight,
561            rows: crate::CooperativeSize::Eight,
562        }.into(),
563        "coop_mat16x16" => TypeGenerator::CooperativeMatrix {
564            columns: crate::CooperativeSize::Sixteen,
565            rows: crate::CooperativeSize::Sixteen,
566        }.into(),
567        _ => return Ok(None),
568    };
569
570    // Check for the enable extension required to use this type, if any.
571    // Slice should be at least len one otherwise extension_needed should be None.
572    let extensions_needed: Option<&[_]> = match ty {
573        PredeclaredType::TypeInner(ref ty) if ty.scalar() == Some(Sc::F16) => {
574            Some(&[ImplementedEnableExtension::F16])
575        }
576        PredeclaredType::RayDesc
577        | PredeclaredType::RayIntersection
578        | PredeclaredType::TypeGenerator(TypeGenerator::AccelerationStructure)
579        | PredeclaredType::TypeGenerator(TypeGenerator::RayQuery) => Some(&[
580            ImplementedEnableExtension::WgpuRayQuery,
581            ImplementedEnableExtension::WgpuRayTracingPipeline,
582        ]),
583        PredeclaredType::TypeGenerator(TypeGenerator::CooperativeMatrix { .. }) => {
584            Some(&[ImplementedEnableExtension::WgpuCooperativeMatrix])
585        }
586        PredeclaredType::TypeGenerator(TypeGenerator::BindingArray) => {
587            Some(&[ImplementedEnableExtension::WgpuBindingArray])
588        }
589        _ => None,
590    };
591    if let Some(extensions_needed) = extensions_needed {
592        let mut any_extension_enabled = false;
593        for extension_needed in extensions_needed {
594            if enable_extensions.contains(*extension_needed) {
595                any_extension_enabled = true;
596            }
597        }
598        if !any_extension_enabled {
599            return Err(Box::new(Error::EnableExtensionNotEnabled {
600                span,
601                kind: extensions_needed[0].into(),
602            }));
603        }
604    }
605
606    Ok(Some(ty))
607}