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