Skip to main content

naga/front/glsl/
builtins.rs

1use alloc::{vec, vec::Vec};
2
3use super::{
4    ast::{
5        BuiltinVariations, FunctionDeclaration, FunctionKind, Overload, ParameterInfo,
6        ParameterQualifier,
7    },
8    context::Context,
9    Error, ErrorKind, Frontend, Result,
10};
11use crate::{
12    BinaryOperator, DerivativeAxis as Axis, DerivativeControl as Ctrl, Expression, Handle,
13    ImageClass, ImageDimension as Dim, ImageQuery, MathFunction, Module, RelationalFunction,
14    SampleLevel, Scalar, ScalarKind as Sk, Span, Type, TypeInner, UnaryOperator, VectorSize,
15};
16
17impl crate::ScalarKind {
18    const fn dummy_storage_format(&self) -> crate::StorageFormat {
19        match *self {
20            Sk::Sint => crate::StorageFormat::R16Sint,
21            Sk::Uint => crate::StorageFormat::R16Uint,
22            _ => crate::StorageFormat::R16Float,
23        }
24    }
25}
26
27impl Module {
28    /// Helper function, to create a function prototype for a builtin
29    fn add_builtin(&mut self, args: Vec<TypeInner>, builtin: MacroCall) -> Overload {
30        let mut parameters = Vec::with_capacity(args.len());
31        let mut parameters_info = Vec::with_capacity(args.len());
32
33        for arg in args {
34            parameters.push(self.types.insert(
35                Type {
36                    name: None,
37                    inner: arg,
38                },
39                Span::default(),
40            ));
41            parameters_info.push(ParameterInfo {
42                qualifier: ParameterQualifier::In,
43                depth: false,
44            });
45        }
46
47        Overload {
48            parameters,
49            parameters_info,
50            kind: FunctionKind::Macro(builtin),
51            defined: false,
52            internal: true,
53            void: false,
54        }
55    }
56}
57
58const fn make_coords_arg(number_of_components: usize, kind: Sk) -> TypeInner {
59    let scalar = Scalar { kind, width: 4 };
60
61    match number_of_components {
62        1 => TypeInner::Scalar(scalar),
63        _ => TypeInner::Vector {
64            size: match number_of_components {
65                2 => VectorSize::Bi,
66                3 => VectorSize::Tri,
67                _ => VectorSize::Quad,
68            },
69            scalar,
70        },
71    }
72}
73
74/// Inject builtins into the declaration
75///
76/// This is done to not add a large startup cost and not increase memory
77/// usage if it isn't needed.
78pub fn inject_builtin(
79    declaration: &mut FunctionDeclaration,
80    module: &mut Module,
81    name: &str,
82    mut variations: BuiltinVariations,
83) {
84    log::trace!(
85        "{} variations: {:?} {:?}",
86        name,
87        variations,
88        declaration.variations
89    );
90    // Don't regeneate variations
91    variations.remove(declaration.variations);
92    declaration.variations |= variations;
93
94    if variations.contains(BuiltinVariations::STANDARD) {
95        inject_standard_builtins(declaration, module, name)
96    }
97
98    if variations.contains(BuiltinVariations::DOUBLE) {
99        inject_double_builtin(declaration, module, name)
100    }
101
102    match name {
103        "texture"
104        | "textureGrad"
105        | "textureGradOffset"
106        | "textureLod"
107        | "textureLodOffset"
108        | "textureOffset"
109        | "textureProj"
110        | "textureProjGrad"
111        | "textureProjGradOffset"
112        | "textureProjLod"
113        | "textureProjLodOffset"
114        | "textureProjOffset" => {
115            let f = |kind, dim, arrayed, multi, shadow| {
116                for bits in 0..=0b11 {
117                    let variant = bits & 0b1 != 0;
118                    let bias = bits & 0b10 != 0;
119
120                    let (proj, offset, level_type) = match name {
121                        // texture(gsampler, gvec P, [float bias]);
122                        "texture" => (false, false, TextureLevelType::None),
123                        // textureGrad(gsampler, gvec P, gvec dPdx, gvec dPdy);
124                        "textureGrad" => (false, false, TextureLevelType::Grad),
125                        // textureGradOffset(gsampler, gvec P, gvec dPdx, gvec dPdy, ivec offset);
126                        "textureGradOffset" => (false, true, TextureLevelType::Grad),
127                        // textureLod(gsampler, gvec P, float lod);
128                        "textureLod" => (false, false, TextureLevelType::Lod),
129                        // textureLodOffset(gsampler, gvec P, float lod, ivec offset);
130                        "textureLodOffset" => (false, true, TextureLevelType::Lod),
131                        // textureOffset(gsampler, gvec+1 P, ivec offset, [float bias]);
132                        "textureOffset" => (false, true, TextureLevelType::None),
133                        // textureProj(gsampler, gvec+1 P, [float bias]);
134                        "textureProj" => (true, false, TextureLevelType::None),
135                        // textureProjGrad(gsampler, gvec+1 P, gvec dPdx, gvec dPdy);
136                        "textureProjGrad" => (true, false, TextureLevelType::Grad),
137                        // textureProjGradOffset(gsampler, gvec+1 P, gvec dPdx, gvec dPdy, ivec offset);
138                        "textureProjGradOffset" => (true, true, TextureLevelType::Grad),
139                        // textureProjLod(gsampler, gvec+1 P, float lod);
140                        "textureProjLod" => (true, false, TextureLevelType::Lod),
141                        // textureProjLodOffset(gsampler, gvec+1 P, gvec dPdx, gvec dPdy, ivec offset);
142                        "textureProjLodOffset" => (true, true, TextureLevelType::Lod),
143                        // textureProjOffset(gsampler, gvec+1 P, ivec offset, [float bias]);
144                        "textureProjOffset" => (true, true, TextureLevelType::None),
145                        _ => unreachable!(),
146                    };
147
148                    let builtin = MacroCall::Texture {
149                        proj,
150                        offset,
151                        shadow,
152                        level_type,
153                    };
154
155                    // Parse out the variant settings.
156                    let grad = level_type == TextureLevelType::Grad;
157                    let lod = level_type == TextureLevelType::Lod;
158
159                    let supports_variant = proj && !shadow;
160                    if variant && !supports_variant {
161                        continue;
162                    }
163
164                    if bias && !matches!(level_type, TextureLevelType::None) {
165                        continue;
166                    }
167
168                    // Proj doesn't work with arrayed or Cube
169                    if proj && (arrayed || dim == Dim::Cube) {
170                        continue;
171                    }
172
173                    // texture operations with offset are not supported for cube maps
174                    if dim == Dim::Cube && offset {
175                        continue;
176                    }
177
178                    // sampler2DArrayShadow can't be used in textureLod or in texture with bias
179                    if (lod || bias) && arrayed && shadow && dim == Dim::D2 {
180                        continue;
181                    }
182
183                    // TODO: glsl supports using bias with depth samplers but naga doesn't
184                    if bias && shadow {
185                        continue;
186                    }
187
188                    let class = match shadow {
189                        true => ImageClass::Depth { multi },
190                        false => ImageClass::Sampled { kind, multi },
191                    };
192
193                    let image = TypeInner::Image {
194                        dim,
195                        arrayed,
196                        class,
197                    };
198
199                    let num_coords_from_dim = image_dims_to_coords_size(dim).min(3);
200                    let mut num_coords = num_coords_from_dim;
201
202                    if shadow && proj {
203                        num_coords = 4;
204                    } else if dim == Dim::D1 && shadow {
205                        num_coords = 3;
206                    } else if shadow {
207                        num_coords += 1;
208                    } else if proj {
209                        if variant && num_coords == 4 {
210                            // Normal form already has 4 components, no need to have a variant form.
211                            continue;
212                        } else if variant {
213                            num_coords = 4;
214                        } else {
215                            num_coords += 1;
216                        }
217                    }
218
219                    if !(dim == Dim::D1 && shadow) {
220                        num_coords += arrayed as usize;
221                    }
222
223                    // Special case: texture(gsamplerCubeArrayShadow) kicks the shadow compare ref to a separate argument,
224                    // since it would otherwise take five arguments. It also can't take a bias, nor can it be proj/grad/lod/offset
225                    // (presumably because nobody asked for it, and implementation complexity?)
226                    if num_coords >= 5 {
227                        if lod || grad || offset || proj || bias {
228                            continue;
229                        }
230                        debug_assert!(dim == Dim::Cube && shadow && arrayed);
231                    }
232                    debug_assert!(num_coords <= 5);
233
234                    let vector = make_coords_arg(num_coords, Sk::Float);
235                    let mut args = vec![image, vector];
236
237                    if num_coords == 5 {
238                        args.push(TypeInner::Scalar(Scalar::F32));
239                    }
240
241                    match level_type {
242                        TextureLevelType::Lod => {
243                            args.push(TypeInner::Scalar(Scalar::F32));
244                        }
245                        TextureLevelType::Grad => {
246                            args.push(make_coords_arg(num_coords_from_dim, Sk::Float));
247                            args.push(make_coords_arg(num_coords_from_dim, Sk::Float));
248                        }
249                        TextureLevelType::None => {}
250                    };
251
252                    if offset {
253                        args.push(make_coords_arg(num_coords_from_dim, Sk::Sint));
254                    }
255
256                    if bias {
257                        args.push(TypeInner::Scalar(Scalar::F32));
258                    }
259
260                    declaration
261                        .overloads
262                        .push(module.add_builtin(args, builtin));
263                }
264            };
265
266            texture_args_generator(TextureArgsOptions::SHADOW | variations.into(), f)
267        }
268        "textureSize" => {
269            let f = |kind, dim, arrayed, multi, shadow| {
270                let class = match shadow {
271                    true => ImageClass::Depth { multi },
272                    false => ImageClass::Sampled { kind, multi },
273                };
274
275                let image = TypeInner::Image {
276                    dim,
277                    arrayed,
278                    class,
279                };
280
281                let mut args = vec![image];
282
283                if !multi {
284                    args.push(TypeInner::Scalar(Scalar::I32))
285                }
286
287                declaration
288                    .overloads
289                    .push(module.add_builtin(args, MacroCall::TextureSize { arrayed }))
290            };
291
292            texture_args_generator(
293                TextureArgsOptions::SHADOW | TextureArgsOptions::MULTI | variations.into(),
294                f,
295            )
296        }
297        "textureQueryLevels" => {
298            let f = |kind, dim, arrayed, multi, shadow| {
299                let class = match shadow {
300                    true => ImageClass::Depth { multi },
301                    false => ImageClass::Sampled { kind, multi },
302                };
303
304                let image = TypeInner::Image {
305                    dim,
306                    arrayed,
307                    class,
308                };
309
310                declaration
311                    .overloads
312                    .push(module.add_builtin(vec![image], MacroCall::TextureQueryLevels))
313            };
314
315            texture_args_generator(TextureArgsOptions::SHADOW | variations.into(), f)
316        }
317        "texelFetch" | "texelFetchOffset" => {
318            let offset = "texelFetchOffset" == name;
319            let f = |kind, dim, arrayed, multi, _shadow| {
320                // Cube images aren't supported
321                if let Dim::Cube = dim {
322                    return;
323                }
324
325                let image = TypeInner::Image {
326                    dim,
327                    arrayed,
328                    class: ImageClass::Sampled { kind, multi },
329                };
330
331                let dim_value = image_dims_to_coords_size(dim);
332                let coordinates = make_coords_arg(dim_value + arrayed as usize, Sk::Sint);
333
334                let mut args = vec![image, coordinates, TypeInner::Scalar(Scalar::I32)];
335
336                if offset {
337                    args.push(make_coords_arg(dim_value, Sk::Sint));
338                }
339
340                declaration
341                    .overloads
342                    .push(module.add_builtin(args, MacroCall::ImageLoad { multi }))
343            };
344
345            // Don't generate shadow images since they aren't supported
346            texture_args_generator(TextureArgsOptions::MULTI | variations.into(), f)
347        }
348        "imageSize" => {
349            let f = |kind: Sk, dim, arrayed, _, _| {
350                // Naga doesn't support cube images and it's usefulness
351                // is questionable, so they won't be supported for now
352                if dim == Dim::Cube {
353                    return;
354                }
355
356                let image = TypeInner::Image {
357                    dim,
358                    arrayed,
359                    class: ImageClass::Storage {
360                        format: kind.dummy_storage_format(),
361                        access: crate::StorageAccess::empty(),
362                    },
363                };
364
365                declaration
366                    .overloads
367                    .push(module.add_builtin(vec![image], MacroCall::TextureSize { arrayed }))
368            };
369
370            texture_args_generator(variations.into(), f)
371        }
372        "imageLoad" => {
373            let f = |kind: Sk, dim, arrayed, _, _| {
374                // Naga doesn't support cube images and it's usefulness
375                // is questionable, so they won't be supported for now
376                if dim == Dim::Cube {
377                    return;
378                }
379
380                let image = TypeInner::Image {
381                    dim,
382                    arrayed,
383                    class: ImageClass::Storage {
384                        format: kind.dummy_storage_format(),
385                        access: crate::StorageAccess::LOAD,
386                    },
387                };
388
389                let dim_value = image_dims_to_coords_size(dim);
390                let mut coord_size = dim_value + arrayed as usize;
391                // > Every OpenGL API call that operates on cubemap array
392                // > textures takes layer-faces, not array layers
393                //
394                // So this means that imageCubeArray only takes a three component
395                // vector coordinate and the third component is a layer index.
396                if Dim::Cube == dim && arrayed {
397                    coord_size = 3
398                }
399                let coordinates = make_coords_arg(coord_size, Sk::Sint);
400
401                let args = vec![image, coordinates];
402
403                declaration
404                    .overloads
405                    .push(module.add_builtin(args, MacroCall::ImageLoad { multi: false }))
406            };
407
408            // Don't generate shadow nor multisampled images since they aren't supported
409            texture_args_generator(variations.into(), f)
410        }
411        "imageStore" => {
412            let f = |kind: Sk, dim, arrayed, _, _| {
413                // Naga doesn't support cube images and it's usefulness
414                // is questionable, so they won't be supported for now
415                if dim == Dim::Cube {
416                    return;
417                }
418
419                let image = TypeInner::Image {
420                    dim,
421                    arrayed,
422                    class: ImageClass::Storage {
423                        format: kind.dummy_storage_format(),
424                        access: crate::StorageAccess::STORE,
425                    },
426                };
427
428                let dim_value = image_dims_to_coords_size(dim);
429                let mut coord_size = dim_value + arrayed as usize;
430                // > Every OpenGL API call that operates on cubemap array
431                // > textures takes layer-faces, not array layers
432                //
433                // So this means that imageCubeArray only takes a three component
434                // vector coordinate and the third component is a layer index.
435                if Dim::Cube == dim && arrayed {
436                    coord_size = 3
437                }
438                let coordinates = make_coords_arg(coord_size, Sk::Sint);
439
440                let args = vec![
441                    image,
442                    coordinates,
443                    TypeInner::Vector {
444                        size: VectorSize::Quad,
445                        scalar: Scalar { kind, width: 4 },
446                    },
447                ];
448
449                let mut overload = module.add_builtin(args, MacroCall::ImageStore);
450                overload.void = true;
451                declaration.overloads.push(overload)
452            };
453
454            // Don't generate shadow nor multisampled images since they aren't supported
455            texture_args_generator(variations.into(), f)
456        }
457        _ => {}
458    }
459}
460
461/// Injects the builtins into declaration that don't need any special variations
462fn inject_standard_builtins(
463    declaration: &mut FunctionDeclaration,
464    module: &mut Module,
465    name: &str,
466) {
467    // Some samplers (sampler1D, etc...) can be float, int, or uint
468    let anykind_sampler = if name.starts_with("sampler") {
469        Some((name, Sk::Float))
470    } else if name.starts_with("usampler") {
471        Some((&name[1..], Sk::Uint))
472    } else if name.starts_with("isampler") {
473        Some((&name[1..], Sk::Sint))
474    } else {
475        None
476    };
477    if let Some((sampler, kind)) = anykind_sampler {
478        match sampler {
479            "sampler1D" | "sampler1DArray" | "sampler2D" | "sampler2DArray" | "sampler2DMS"
480            | "sampler2DMSArray" | "sampler3D" | "samplerCube" | "samplerCubeArray" => {
481                declaration.overloads.push(module.add_builtin(
482                    vec![
483                        TypeInner::Image {
484                            dim: match sampler {
485                                "sampler1D" | "sampler1DArray" => Dim::D1,
486                                "sampler2D" | "sampler2DArray" | "sampler2DMS"
487                                | "sampler2DMSArray" => Dim::D2,
488                                "sampler3D" => Dim::D3,
489                                _ => Dim::Cube,
490                            },
491                            arrayed: matches!(
492                                sampler,
493                                "sampler1DArray"
494                                    | "sampler2DArray"
495                                    | "sampler2DMSArray"
496                                    | "samplerCubeArray"
497                            ),
498                            class: ImageClass::Sampled {
499                                kind,
500                                multi: matches!(sampler, "sampler2DMS" | "sampler2DMSArray"),
501                            },
502                        },
503                        TypeInner::Sampler { comparison: false },
504                    ],
505                    MacroCall::Sampler,
506                ));
507                return;
508            }
509            _ => (),
510        }
511    }
512
513    match name {
514        // Shadow sampler can only be of kind `Sk::Float`
515        "sampler1DShadow"
516        | "sampler1DArrayShadow"
517        | "sampler2DShadow"
518        | "sampler2DArrayShadow"
519        | "samplerCubeShadow"
520        | "samplerCubeArrayShadow" => {
521            let dim = match name {
522                "sampler1DShadow" | "sampler1DArrayShadow" => Dim::D1,
523                "sampler2DShadow" | "sampler2DArrayShadow" => Dim::D2,
524                _ => Dim::Cube,
525            };
526            let arrayed = matches!(
527                name,
528                "sampler1DArrayShadow" | "sampler2DArrayShadow" | "samplerCubeArrayShadow"
529            );
530
531            for i in 0..2 {
532                let ty = TypeInner::Image {
533                    dim,
534                    arrayed,
535                    class: match i {
536                        0 => ImageClass::Sampled {
537                            kind: Sk::Float,
538                            multi: false,
539                        },
540                        _ => ImageClass::Depth { multi: false },
541                    },
542                };
543
544                declaration.overloads.push(module.add_builtin(
545                    vec![ty, TypeInner::Sampler { comparison: true }],
546                    MacroCall::SamplerShadow,
547                ))
548            }
549        }
550        "sin" | "exp" | "exp2" | "sinh" | "cos" | "cosh" | "tan" | "tanh" | "acos" | "asin"
551        | "log" | "log2" | "radians" | "degrees" | "asinh" | "acosh" | "atanh"
552        | "floatBitsToInt" | "floatBitsToUint" | "dFdx" | "dFdxFine" | "dFdxCoarse" | "dFdy"
553        | "dFdyFine" | "dFdyCoarse" | "fwidth" | "fwidthFine" | "fwidthCoarse" => {
554            // bits layout
555            // bit 0 through 1 - dims
556            for bits in 0..0b100 {
557                let size = match bits {
558                    0b00 => None,
559                    0b01 => Some(VectorSize::Bi),
560                    0b10 => Some(VectorSize::Tri),
561                    _ => Some(VectorSize::Quad),
562                };
563                let scalar = Scalar::F32;
564
565                declaration.overloads.push(module.add_builtin(
566                    vec![match size {
567                        Some(size) => TypeInner::Vector { size, scalar },
568                        None => TypeInner::Scalar(scalar),
569                    }],
570                    match name {
571                        "sin" => MacroCall::MathFunction(MathFunction::Sin),
572                        "exp" => MacroCall::MathFunction(MathFunction::Exp),
573                        "exp2" => MacroCall::MathFunction(MathFunction::Exp2),
574                        "sinh" => MacroCall::MathFunction(MathFunction::Sinh),
575                        "cos" => MacroCall::MathFunction(MathFunction::Cos),
576                        "cosh" => MacroCall::MathFunction(MathFunction::Cosh),
577                        "tan" => MacroCall::MathFunction(MathFunction::Tan),
578                        "tanh" => MacroCall::MathFunction(MathFunction::Tanh),
579                        "acos" => MacroCall::MathFunction(MathFunction::Acos),
580                        "asin" => MacroCall::MathFunction(MathFunction::Asin),
581                        "log" => MacroCall::MathFunction(MathFunction::Log),
582                        "log2" => MacroCall::MathFunction(MathFunction::Log2),
583                        "asinh" => MacroCall::MathFunction(MathFunction::Asinh),
584                        "acosh" => MacroCall::MathFunction(MathFunction::Acosh),
585                        "atanh" => MacroCall::MathFunction(MathFunction::Atanh),
586                        "radians" => MacroCall::MathFunction(MathFunction::Radians),
587                        "degrees" => MacroCall::MathFunction(MathFunction::Degrees),
588                        "floatBitsToInt" => MacroCall::BitCast(Sk::Sint),
589                        "floatBitsToUint" => MacroCall::BitCast(Sk::Uint),
590                        "dFdxCoarse" => MacroCall::Derivate(Axis::X, Ctrl::Coarse),
591                        "dFdyCoarse" => MacroCall::Derivate(Axis::Y, Ctrl::Coarse),
592                        "fwidthCoarse" => MacroCall::Derivate(Axis::Width, Ctrl::Coarse),
593                        "dFdxFine" => MacroCall::Derivate(Axis::X, Ctrl::Fine),
594                        "dFdyFine" => MacroCall::Derivate(Axis::Y, Ctrl::Fine),
595                        "fwidthFine" => MacroCall::Derivate(Axis::Width, Ctrl::Fine),
596                        "dFdx" => MacroCall::Derivate(Axis::X, Ctrl::None),
597                        "dFdy" => MacroCall::Derivate(Axis::Y, Ctrl::None),
598                        "fwidth" => MacroCall::Derivate(Axis::Width, Ctrl::None),
599                        _ => unreachable!(),
600                    },
601                ))
602            }
603        }
604        "intBitsToFloat" | "uintBitsToFloat" => {
605            // bits layout
606            // bit 0 through 1 - dims
607            for bits in 0..0b100 {
608                let size = match bits {
609                    0b00 => None,
610                    0b01 => Some(VectorSize::Bi),
611                    0b10 => Some(VectorSize::Tri),
612                    _ => Some(VectorSize::Quad),
613                };
614                let scalar = match name {
615                    "intBitsToFloat" => Scalar::I32,
616                    _ => Scalar::U32,
617                };
618
619                declaration.overloads.push(module.add_builtin(
620                    vec![match size {
621                        Some(size) => TypeInner::Vector { size, scalar },
622                        None => TypeInner::Scalar(scalar),
623                    }],
624                    MacroCall::BitCast(Sk::Float),
625                ))
626            }
627        }
628        "pow" => {
629            // bits layout
630            // bit 0 through 1 - dims
631            for bits in 0..0b100 {
632                let size = match bits {
633                    0b00 => None,
634                    0b01 => Some(VectorSize::Bi),
635                    0b10 => Some(VectorSize::Tri),
636                    _ => Some(VectorSize::Quad),
637                };
638                let scalar = Scalar::F32;
639                let ty = || match size {
640                    Some(size) => TypeInner::Vector { size, scalar },
641                    None => TypeInner::Scalar(scalar),
642                };
643
644                declaration.overloads.push(
645                    module
646                        .add_builtin(vec![ty(), ty()], MacroCall::MathFunction(MathFunction::Pow)),
647                )
648            }
649        }
650        "abs" | "sign" => {
651            // bits layout
652            // bit 0 through 1 - dims
653            // bit 2 - float/sint
654            for bits in 0..0b1000 {
655                let size = match bits & 0b11 {
656                    0b00 => None,
657                    0b01 => Some(VectorSize::Bi),
658                    0b10 => Some(VectorSize::Tri),
659                    _ => Some(VectorSize::Quad),
660                };
661                let scalar = match bits >> 2 {
662                    0b0 => Scalar::F32,
663                    _ => Scalar::I32,
664                };
665
666                let args = vec![match size {
667                    Some(size) => TypeInner::Vector { size, scalar },
668                    None => TypeInner::Scalar(scalar),
669                }];
670
671                declaration.overloads.push(module.add_builtin(
672                    args,
673                    MacroCall::MathFunction(match name {
674                        "abs" => MathFunction::Abs,
675                        "sign" => MathFunction::Sign,
676                        _ => unreachable!(),
677                    }),
678                ))
679            }
680        }
681        "bitCount" | "bitfieldReverse" | "bitfieldExtract" | "bitfieldInsert" | "findLSB"
682        | "findMSB" => {
683            let fun = match name {
684                "bitCount" => MathFunction::CountOneBits,
685                "bitfieldReverse" => MathFunction::ReverseBits,
686                "bitfieldExtract" => MathFunction::ExtractBits,
687                "bitfieldInsert" => MathFunction::InsertBits,
688                "findLSB" => MathFunction::FirstTrailingBit,
689                "findMSB" => MathFunction::FirstLeadingBit,
690                _ => unreachable!(),
691            };
692
693            let mc = match fun {
694                MathFunction::ExtractBits => MacroCall::BitfieldExtract,
695                MathFunction::InsertBits => MacroCall::BitfieldInsert,
696                _ => MacroCall::MathFunction(fun),
697            };
698
699            // bits layout
700            // bit 0 - int/uint
701            // bit 1 through 2 - dims
702            for bits in 0..0b1000 {
703                let scalar = match bits & 0b1 {
704                    0b0 => Scalar::I32,
705                    _ => Scalar::U32,
706                };
707                let size = match bits >> 1 {
708                    0b00 => None,
709                    0b01 => Some(VectorSize::Bi),
710                    0b10 => Some(VectorSize::Tri),
711                    _ => Some(VectorSize::Quad),
712                };
713
714                let ty = || match size {
715                    Some(size) => TypeInner::Vector { size, scalar },
716                    None => TypeInner::Scalar(scalar),
717                };
718
719                let mut args = vec![ty()];
720
721                match fun {
722                    MathFunction::ExtractBits => {
723                        args.push(TypeInner::Scalar(Scalar::I32));
724                        args.push(TypeInner::Scalar(Scalar::I32));
725                    }
726                    MathFunction::InsertBits => {
727                        args.push(ty());
728                        args.push(TypeInner::Scalar(Scalar::I32));
729                        args.push(TypeInner::Scalar(Scalar::I32));
730                    }
731                    _ => {}
732                }
733
734                // we need to cast the return type of findLsb / findMsb
735                let mc = if scalar.kind == Sk::Uint {
736                    match mc {
737                        MacroCall::MathFunction(MathFunction::FirstTrailingBit) => {
738                            MacroCall::FindLsbUint
739                        }
740                        MacroCall::MathFunction(MathFunction::FirstLeadingBit) => {
741                            MacroCall::FindMsbUint
742                        }
743                        mc => mc,
744                    }
745                } else {
746                    mc
747                };
748
749                declaration.overloads.push(module.add_builtin(args, mc))
750            }
751        }
752        "subgroupBallotFindLSB" | "subgroupBallotFindMSB" => {
753            let mc = match name {
754                "subgroupBallotFindLSB" => MacroCall::SubgroupBallotFindLsb,
755                "subgroupBallotFindMSB" => MacroCall::SubgroupBallotFindMsb,
756                _ => unreachable!(),
757            };
758
759            declaration.overloads.push(module.add_builtin(
760                vec![TypeInner::Vector {
761                    size: VectorSize::Quad,
762                    scalar: Scalar::U32,
763                }],
764                mc,
765            ))
766        }
767        "packSnorm4x8" | "packUnorm4x8" | "packSnorm2x16" | "packUnorm2x16" | "packHalf2x16" => {
768            let fun = match name {
769                "packSnorm4x8" => MathFunction::Pack4x8snorm,
770                "packUnorm4x8" => MathFunction::Pack4x8unorm,
771                "packSnorm2x16" => MathFunction::Pack2x16snorm,
772                "packUnorm2x16" => MathFunction::Pack2x16unorm,
773                "packHalf2x16" => MathFunction::Pack2x16float,
774                _ => unreachable!(),
775            };
776
777            let ty = match fun {
778                MathFunction::Pack4x8snorm | MathFunction::Pack4x8unorm => TypeInner::Vector {
779                    size: VectorSize::Quad,
780                    scalar: Scalar::F32,
781                },
782                MathFunction::Pack2x16unorm
783                | MathFunction::Pack2x16snorm
784                | MathFunction::Pack2x16float => TypeInner::Vector {
785                    size: VectorSize::Bi,
786                    scalar: Scalar::F32,
787                },
788                _ => unreachable!(),
789            };
790
791            let args = vec![ty];
792
793            declaration
794                .overloads
795                .push(module.add_builtin(args, MacroCall::MathFunction(fun)));
796        }
797        "unpackSnorm4x8" | "unpackUnorm4x8" | "unpackSnorm2x16" | "unpackUnorm2x16"
798        | "unpackHalf2x16" => {
799            let fun = match name {
800                "unpackSnorm4x8" => MathFunction::Unpack4x8snorm,
801                "unpackUnorm4x8" => MathFunction::Unpack4x8unorm,
802                "unpackSnorm2x16" => MathFunction::Unpack2x16snorm,
803                "unpackUnorm2x16" => MathFunction::Unpack2x16unorm,
804                "unpackHalf2x16" => MathFunction::Unpack2x16float,
805                _ => unreachable!(),
806            };
807
808            let args = vec![TypeInner::Scalar(Scalar::U32)];
809
810            declaration
811                .overloads
812                .push(module.add_builtin(args, MacroCall::MathFunction(fun)));
813        }
814        "atan" => {
815            // bits layout
816            // bit 0 - atan/atan2
817            // bit 1 through 2 - dims
818            for bits in 0..0b1000 {
819                let fun = match bits & 0b1 {
820                    0b0 => MathFunction::Atan,
821                    _ => MathFunction::Atan2,
822                };
823                let size = match bits >> 1 {
824                    0b00 => None,
825                    0b01 => Some(VectorSize::Bi),
826                    0b10 => Some(VectorSize::Tri),
827                    _ => Some(VectorSize::Quad),
828                };
829                let scalar = Scalar::F32;
830                let ty = || match size {
831                    Some(size) => TypeInner::Vector { size, scalar },
832                    None => TypeInner::Scalar(scalar),
833                };
834
835                let mut args = vec![ty()];
836
837                if fun == MathFunction::Atan2 {
838                    args.push(ty())
839                }
840
841                declaration
842                    .overloads
843                    .push(module.add_builtin(args, MacroCall::MathFunction(fun)))
844            }
845        }
846        "all" | "any" | "not" => {
847            // bits layout
848            // bit 0 through 1 - dims
849            for bits in 0..0b11 {
850                let size = match bits {
851                    0b00 => VectorSize::Bi,
852                    0b01 => VectorSize::Tri,
853                    _ => VectorSize::Quad,
854                };
855
856                let args = vec![TypeInner::Vector {
857                    size,
858                    scalar: Scalar::BOOL,
859                }];
860
861                let fun = match name {
862                    "all" => MacroCall::Relational(RelationalFunction::All),
863                    "any" => MacroCall::Relational(RelationalFunction::Any),
864                    "not" => MacroCall::Unary(UnaryOperator::LogicalNot),
865                    _ => unreachable!(),
866                };
867
868                declaration.overloads.push(module.add_builtin(args, fun))
869            }
870        }
871        "lessThan" | "greaterThan" | "lessThanEqual" | "greaterThanEqual" => {
872            for bits in 0..0b1001 {
873                let (size, scalar) = match bits {
874                    0b0000 => (VectorSize::Bi, Scalar::F32),
875                    0b0001 => (VectorSize::Tri, Scalar::F32),
876                    0b0010 => (VectorSize::Quad, Scalar::F32),
877                    0b0011 => (VectorSize::Bi, Scalar::I32),
878                    0b0100 => (VectorSize::Tri, Scalar::I32),
879                    0b0101 => (VectorSize::Quad, Scalar::I32),
880                    0b0110 => (VectorSize::Bi, Scalar::U32),
881                    0b0111 => (VectorSize::Tri, Scalar::U32),
882                    _ => (VectorSize::Quad, Scalar::U32),
883                };
884
885                let ty = || TypeInner::Vector { size, scalar };
886                let args = vec![ty(), ty()];
887
888                let fun = MacroCall::Binary(match name {
889                    "lessThan" => BinaryOperator::Less,
890                    "greaterThan" => BinaryOperator::Greater,
891                    "lessThanEqual" => BinaryOperator::LessEqual,
892                    "greaterThanEqual" => BinaryOperator::GreaterEqual,
893                    _ => unreachable!(),
894                });
895
896                declaration.overloads.push(module.add_builtin(args, fun))
897            }
898        }
899        "equal" | "notEqual" => {
900            for bits in 0..0b1100 {
901                let (size, scalar) = match bits {
902                    0b0000 => (VectorSize::Bi, Scalar::F32),
903                    0b0001 => (VectorSize::Tri, Scalar::F32),
904                    0b0010 => (VectorSize::Quad, Scalar::F32),
905                    0b0011 => (VectorSize::Bi, Scalar::I32),
906                    0b0100 => (VectorSize::Tri, Scalar::I32),
907                    0b0101 => (VectorSize::Quad, Scalar::I32),
908                    0b0110 => (VectorSize::Bi, Scalar::U32),
909                    0b0111 => (VectorSize::Tri, Scalar::U32),
910                    0b1000 => (VectorSize::Quad, Scalar::U32),
911                    0b1001 => (VectorSize::Bi, Scalar::BOOL),
912                    0b1010 => (VectorSize::Tri, Scalar::BOOL),
913                    _ => (VectorSize::Quad, Scalar::BOOL),
914                };
915
916                let ty = || TypeInner::Vector { size, scalar };
917                let args = vec![ty(), ty()];
918
919                let fun = MacroCall::Binary(match name {
920                    "equal" => BinaryOperator::Equal,
921                    "notEqual" => BinaryOperator::NotEqual,
922                    _ => unreachable!(),
923                });
924
925                declaration.overloads.push(module.add_builtin(args, fun))
926            }
927        }
928        "min" | "max" => {
929            // bits layout
930            // bit 0 through 1 - scalar kind
931            // bit 2 through 4 - dims
932            for bits in 0..0b11100 {
933                let scalar = match bits & 0b11 {
934                    0b00 => Scalar::F32,
935                    0b01 => Scalar::I32,
936                    0b10 => Scalar::U32,
937                    _ => continue,
938                };
939                let (size, second_size) = match bits >> 2 {
940                    0b000 => (None, None),
941                    0b001 => (Some(VectorSize::Bi), None),
942                    0b010 => (Some(VectorSize::Tri), None),
943                    0b011 => (Some(VectorSize::Quad), None),
944                    0b100 => (Some(VectorSize::Bi), Some(VectorSize::Bi)),
945                    0b101 => (Some(VectorSize::Tri), Some(VectorSize::Tri)),
946                    _ => (Some(VectorSize::Quad), Some(VectorSize::Quad)),
947                };
948
949                let args = vec![
950                    match size {
951                        Some(size) => TypeInner::Vector { size, scalar },
952                        None => TypeInner::Scalar(scalar),
953                    },
954                    match second_size {
955                        Some(size) => TypeInner::Vector { size, scalar },
956                        None => TypeInner::Scalar(scalar),
957                    },
958                ];
959
960                let fun = match name {
961                    "max" => MacroCall::Splatted(MathFunction::Max, size, 1),
962                    "min" => MacroCall::Splatted(MathFunction::Min, size, 1),
963                    _ => unreachable!(),
964                };
965
966                declaration.overloads.push(module.add_builtin(args, fun))
967            }
968        }
969        "mix" => {
970            // bits layout
971            // bit 0 through 1 - dims
972            // bit 2 through 4 - types
973            //
974            // 0b10011 is the last element since splatted single elements
975            // were already added
976            for bits in 0..0b10011 {
977                let size = match bits & 0b11 {
978                    0b00 => Some(VectorSize::Bi),
979                    0b01 => Some(VectorSize::Tri),
980                    0b10 => Some(VectorSize::Quad),
981                    _ => None,
982                };
983                let (scalar, splatted, boolean) = match bits >> 2 {
984                    0b000 => (Scalar::I32, false, true),
985                    0b001 => (Scalar::U32, false, true),
986                    0b010 => (Scalar::F32, false, true),
987                    0b011 => (Scalar::F32, false, false),
988                    _ => (Scalar::F32, true, false),
989                };
990
991                let ty = |scalar| match size {
992                    Some(size) => TypeInner::Vector { size, scalar },
993                    None => TypeInner::Scalar(scalar),
994                };
995                let args = vec![
996                    ty(scalar),
997                    ty(scalar),
998                    match (boolean, splatted) {
999                        (true, _) => ty(Scalar::BOOL),
1000                        (_, false) => TypeInner::Scalar(scalar),
1001                        _ => ty(scalar),
1002                    },
1003                ];
1004
1005                declaration.overloads.push(module.add_builtin(
1006                    args,
1007                    match boolean {
1008                        true => MacroCall::MixBoolean,
1009                        false => MacroCall::Splatted(MathFunction::Mix, size, 2),
1010                    },
1011                ))
1012            }
1013        }
1014        "clamp" => {
1015            // bits layout
1016            // bit 0 through 1 - float/int/uint
1017            // bit 2 through 3 - dims
1018            // bit 4 - splatted
1019            //
1020            // 0b11010 is the last element since splatted single elements
1021            // were already added
1022            for bits in 0..0b11011 {
1023                let scalar = match bits & 0b11 {
1024                    0b00 => Scalar::F32,
1025                    0b01 => Scalar::I32,
1026                    0b10 => Scalar::U32,
1027                    _ => continue,
1028                };
1029                let size = match (bits >> 2) & 0b11 {
1030                    0b00 => Some(VectorSize::Bi),
1031                    0b01 => Some(VectorSize::Tri),
1032                    0b10 => Some(VectorSize::Quad),
1033                    _ => None,
1034                };
1035                let splatted = bits & 0b10000 == 0b10000;
1036
1037                let base_ty = || match size {
1038                    Some(size) => TypeInner::Vector { size, scalar },
1039                    None => TypeInner::Scalar(scalar),
1040                };
1041                let limit_ty = || match splatted {
1042                    true => TypeInner::Scalar(scalar),
1043                    false => base_ty(),
1044                };
1045
1046                let args = vec![base_ty(), limit_ty(), limit_ty()];
1047
1048                declaration
1049                    .overloads
1050                    .push(module.add_builtin(args, MacroCall::Clamp(size)))
1051            }
1052        }
1053        "barrier" => declaration
1054            .overloads
1055            .push(module.add_builtin(Vec::new(), MacroCall::Barrier)),
1056        // Add common builtins with floats
1057        _ => inject_common_builtin(declaration, module, name, 4),
1058    }
1059}
1060
1061/// Injects the builtins into declaration that need doubles
1062fn inject_double_builtin(declaration: &mut FunctionDeclaration, module: &mut Module, name: &str) {
1063    match name {
1064        "abs" | "sign" => {
1065            // bits layout
1066            // bit 0 through 1 - dims
1067            for bits in 0..0b100 {
1068                let size = match bits {
1069                    0b00 => None,
1070                    0b01 => Some(VectorSize::Bi),
1071                    0b10 => Some(VectorSize::Tri),
1072                    _ => Some(VectorSize::Quad),
1073                };
1074                let scalar = Scalar::F64;
1075
1076                let args = vec![match size {
1077                    Some(size) => TypeInner::Vector { size, scalar },
1078                    None => TypeInner::Scalar(scalar),
1079                }];
1080
1081                declaration.overloads.push(module.add_builtin(
1082                    args,
1083                    MacroCall::MathFunction(match name {
1084                        "abs" => MathFunction::Abs,
1085                        "sign" => MathFunction::Sign,
1086                        _ => unreachable!(),
1087                    }),
1088                ))
1089            }
1090        }
1091        "min" | "max" => {
1092            // bits layout
1093            // bit 0 through 2 - dims
1094            for bits in 0..0b111 {
1095                let (size, second_size) = match bits {
1096                    0b000 => (None, None),
1097                    0b001 => (Some(VectorSize::Bi), None),
1098                    0b010 => (Some(VectorSize::Tri), None),
1099                    0b011 => (Some(VectorSize::Quad), None),
1100                    0b100 => (Some(VectorSize::Bi), Some(VectorSize::Bi)),
1101                    0b101 => (Some(VectorSize::Tri), Some(VectorSize::Tri)),
1102                    _ => (Some(VectorSize::Quad), Some(VectorSize::Quad)),
1103                };
1104                let scalar = Scalar::F64;
1105
1106                let args = vec![
1107                    match size {
1108                        Some(size) => TypeInner::Vector { size, scalar },
1109                        None => TypeInner::Scalar(scalar),
1110                    },
1111                    match second_size {
1112                        Some(size) => TypeInner::Vector { size, scalar },
1113                        None => TypeInner::Scalar(scalar),
1114                    },
1115                ];
1116
1117                let fun = match name {
1118                    "max" => MacroCall::Splatted(MathFunction::Max, size, 1),
1119                    "min" => MacroCall::Splatted(MathFunction::Min, size, 1),
1120                    _ => unreachable!(),
1121                };
1122
1123                declaration.overloads.push(module.add_builtin(args, fun))
1124            }
1125        }
1126        "mix" => {
1127            // bits layout
1128            // bit 0 through 1 - dims
1129            // bit 2 through 3 - splatted/boolean
1130            //
1131            // 0b1010 is the last element since splatted with single elements
1132            // is equal to normal single elements
1133            for bits in 0..0b1011 {
1134                let size = match bits & 0b11 {
1135                    0b00 => Some(VectorSize::Quad),
1136                    0b01 => Some(VectorSize::Bi),
1137                    0b10 => Some(VectorSize::Tri),
1138                    _ => None,
1139                };
1140                let scalar = Scalar::F64;
1141                let (splatted, boolean) = match bits >> 2 {
1142                    0b00 => (false, false),
1143                    0b01 => (false, true),
1144                    _ => (true, false),
1145                };
1146
1147                let ty = |scalar| match size {
1148                    Some(size) => TypeInner::Vector { size, scalar },
1149                    None => TypeInner::Scalar(scalar),
1150                };
1151                let args = vec![
1152                    ty(scalar),
1153                    ty(scalar),
1154                    match (boolean, splatted) {
1155                        (true, _) => ty(Scalar::BOOL),
1156                        (_, false) => TypeInner::Scalar(scalar),
1157                        _ => ty(scalar),
1158                    },
1159                ];
1160
1161                declaration.overloads.push(module.add_builtin(
1162                    args,
1163                    match boolean {
1164                        true => MacroCall::MixBoolean,
1165                        false => MacroCall::Splatted(MathFunction::Mix, size, 2),
1166                    },
1167                ))
1168            }
1169        }
1170        "clamp" => {
1171            // bits layout
1172            // bit 0 through 1 - dims
1173            // bit 2 - splatted
1174            //
1175            // 0b110 is the last element since splatted with single elements
1176            // is equal to normal single elements
1177            for bits in 0..0b111 {
1178                let scalar = Scalar::F64;
1179                let size = match bits & 0b11 {
1180                    0b00 => Some(VectorSize::Bi),
1181                    0b01 => Some(VectorSize::Tri),
1182                    0b10 => Some(VectorSize::Quad),
1183                    _ => None,
1184                };
1185                let splatted = bits & 0b100 == 0b100;
1186
1187                let base_ty = || match size {
1188                    Some(size) => TypeInner::Vector { size, scalar },
1189                    None => TypeInner::Scalar(scalar),
1190                };
1191                let limit_ty = || match splatted {
1192                    true => TypeInner::Scalar(scalar),
1193                    false => base_ty(),
1194                };
1195
1196                let args = vec![base_ty(), limit_ty(), limit_ty()];
1197
1198                declaration
1199                    .overloads
1200                    .push(module.add_builtin(args, MacroCall::Clamp(size)))
1201            }
1202        }
1203        "lessThan" | "greaterThan" | "lessThanEqual" | "greaterThanEqual" | "equal"
1204        | "notEqual" => {
1205            let scalar = Scalar::F64;
1206            for bits in 0..0b11 {
1207                let size = match bits {
1208                    0b00 => VectorSize::Bi,
1209                    0b01 => VectorSize::Tri,
1210                    _ => VectorSize::Quad,
1211                };
1212
1213                let ty = || TypeInner::Vector { size, scalar };
1214                let args = vec![ty(), ty()];
1215
1216                let fun = MacroCall::Binary(match name {
1217                    "lessThan" => BinaryOperator::Less,
1218                    "greaterThan" => BinaryOperator::Greater,
1219                    "lessThanEqual" => BinaryOperator::LessEqual,
1220                    "greaterThanEqual" => BinaryOperator::GreaterEqual,
1221                    "equal" => BinaryOperator::Equal,
1222                    "notEqual" => BinaryOperator::NotEqual,
1223                    _ => unreachable!(),
1224                });
1225
1226                declaration.overloads.push(module.add_builtin(args, fun))
1227            }
1228        }
1229        // Add common builtins with doubles
1230        _ => inject_common_builtin(declaration, module, name, 8),
1231    }
1232}
1233
1234/// Injects the builtins into declaration that can used either float or doubles
1235fn inject_common_builtin(
1236    declaration: &mut FunctionDeclaration,
1237    module: &mut Module,
1238    name: &str,
1239    float_width: crate::Bytes,
1240) {
1241    let float_scalar = Scalar {
1242        kind: Sk::Float,
1243        width: float_width,
1244    };
1245    match name {
1246        "ceil" | "round" | "roundEven" | "floor" | "fract" | "trunc" | "sqrt" | "inversesqrt"
1247        | "normalize" | "length" | "isinf" | "isnan" => {
1248            // bits layout
1249            // bit 0 through 1 - dims
1250            for bits in 0..0b100 {
1251                let size = match bits {
1252                    0b00 => None,
1253                    0b01 => Some(VectorSize::Bi),
1254                    0b10 => Some(VectorSize::Tri),
1255                    _ => Some(VectorSize::Quad),
1256                };
1257
1258                let args = vec![match size {
1259                    Some(size) => TypeInner::Vector {
1260                        size,
1261                        scalar: float_scalar,
1262                    },
1263                    None => TypeInner::Scalar(float_scalar),
1264                }];
1265
1266                let fun = match name {
1267                    "ceil" => MacroCall::MathFunction(MathFunction::Ceil),
1268                    "round" | "roundEven" => MacroCall::MathFunction(MathFunction::Round),
1269                    "floor" => MacroCall::MathFunction(MathFunction::Floor),
1270                    "fract" => MacroCall::MathFunction(MathFunction::Fract),
1271                    "trunc" => MacroCall::MathFunction(MathFunction::Trunc),
1272                    "sqrt" => MacroCall::MathFunction(MathFunction::Sqrt),
1273                    "inversesqrt" => MacroCall::MathFunction(MathFunction::InverseSqrt),
1274                    "normalize" => MacroCall::MathFunction(MathFunction::Normalize),
1275                    "length" => MacroCall::MathFunction(MathFunction::Length),
1276                    "isinf" => MacroCall::Relational(RelationalFunction::IsInf),
1277                    "isnan" => MacroCall::Relational(RelationalFunction::IsNan),
1278                    _ => unreachable!(),
1279                };
1280
1281                declaration.overloads.push(module.add_builtin(args, fun))
1282            }
1283        }
1284        "dot" | "reflect" | "distance" | "ldexp" => {
1285            // bits layout
1286            // bit 0 through 1 - dims
1287            for bits in 0..0b100 {
1288                let size = match bits {
1289                    0b00 => None,
1290                    0b01 => Some(VectorSize::Bi),
1291                    0b10 => Some(VectorSize::Tri),
1292                    _ => Some(VectorSize::Quad),
1293                };
1294                let ty = |scalar| match size {
1295                    Some(size) => TypeInner::Vector { size, scalar },
1296                    None => TypeInner::Scalar(scalar),
1297                };
1298
1299                let fun = match name {
1300                    "dot" => MacroCall::MathFunction(MathFunction::Dot),
1301                    "reflect" => MacroCall::MathFunction(MathFunction::Reflect),
1302                    "distance" => MacroCall::MathFunction(MathFunction::Distance),
1303                    "ldexp" => MacroCall::MathFunction(MathFunction::Ldexp),
1304                    _ => unreachable!(),
1305                };
1306
1307                let second_scalar = match fun {
1308                    MacroCall::MathFunction(MathFunction::Ldexp) => Scalar::I32,
1309                    _ => float_scalar,
1310                };
1311
1312                declaration
1313                    .overloads
1314                    .push(module.add_builtin(vec![ty(float_scalar), ty(second_scalar)], fun))
1315            }
1316        }
1317        "transpose" => {
1318            // bits layout
1319            // bit 0 through 3 - dims
1320            for bits in 0..0b1001 {
1321                let (rows, columns) = match bits {
1322                    0b0000 => (VectorSize::Bi, VectorSize::Bi),
1323                    0b0001 => (VectorSize::Bi, VectorSize::Tri),
1324                    0b0010 => (VectorSize::Bi, VectorSize::Quad),
1325                    0b0011 => (VectorSize::Tri, VectorSize::Bi),
1326                    0b0100 => (VectorSize::Tri, VectorSize::Tri),
1327                    0b0101 => (VectorSize::Tri, VectorSize::Quad),
1328                    0b0110 => (VectorSize::Quad, VectorSize::Bi),
1329                    0b0111 => (VectorSize::Quad, VectorSize::Tri),
1330                    _ => (VectorSize::Quad, VectorSize::Quad),
1331                };
1332
1333                declaration.overloads.push(module.add_builtin(
1334                    vec![TypeInner::Matrix {
1335                        columns,
1336                        rows,
1337                        scalar: float_scalar,
1338                    }],
1339                    MacroCall::MathFunction(MathFunction::Transpose),
1340                ))
1341            }
1342        }
1343        "inverse" | "determinant" => {
1344            // bits layout
1345            // bit 0 through 1 - dims
1346            for bits in 0..0b11 {
1347                let (rows, columns) = match bits {
1348                    0b00 => (VectorSize::Bi, VectorSize::Bi),
1349                    0b01 => (VectorSize::Tri, VectorSize::Tri),
1350                    _ => (VectorSize::Quad, VectorSize::Quad),
1351                };
1352
1353                let args = vec![TypeInner::Matrix {
1354                    columns,
1355                    rows,
1356                    scalar: float_scalar,
1357                }];
1358
1359                declaration.overloads.push(module.add_builtin(
1360                    args,
1361                    MacroCall::MathFunction(match name {
1362                        "inverse" => MathFunction::Inverse,
1363                        "determinant" => MathFunction::Determinant,
1364                        _ => unreachable!(),
1365                    }),
1366                ))
1367            }
1368        }
1369        "mod" | "step" => {
1370            // bits layout
1371            // bit 0 through 2 - dims
1372            for bits in 0..0b111 {
1373                let (size, second_size) = match bits {
1374                    0b000 => (None, None),
1375                    0b001 => (Some(VectorSize::Bi), None),
1376                    0b010 => (Some(VectorSize::Tri), None),
1377                    0b011 => (Some(VectorSize::Quad), None),
1378                    0b100 => (Some(VectorSize::Bi), Some(VectorSize::Bi)),
1379                    0b101 => (Some(VectorSize::Tri), Some(VectorSize::Tri)),
1380                    _ => (Some(VectorSize::Quad), Some(VectorSize::Quad)),
1381                };
1382
1383                let mut args = Vec::with_capacity(2);
1384                let step = name == "step";
1385
1386                for i in 0..2 {
1387                    let maybe_size = match i == step as u32 {
1388                        true => size,
1389                        false => second_size,
1390                    };
1391
1392                    args.push(match maybe_size {
1393                        Some(size) => TypeInner::Vector {
1394                            size,
1395                            scalar: float_scalar,
1396                        },
1397                        None => TypeInner::Scalar(float_scalar),
1398                    })
1399                }
1400
1401                let fun = match name {
1402                    "mod" => MacroCall::Mod(size),
1403                    "step" => MacroCall::Splatted(MathFunction::Step, size, 0),
1404                    _ => unreachable!(),
1405                };
1406
1407                declaration.overloads.push(module.add_builtin(args, fun))
1408            }
1409        }
1410        // TODO: https://github.com/gfx-rs/naga/issues/2526
1411        // "modf" | "frexp" => { ... }
1412        "cross" => {
1413            let args = vec![
1414                TypeInner::Vector {
1415                    size: VectorSize::Tri,
1416                    scalar: float_scalar,
1417                },
1418                TypeInner::Vector {
1419                    size: VectorSize::Tri,
1420                    scalar: float_scalar,
1421                },
1422            ];
1423
1424            declaration
1425                .overloads
1426                .push(module.add_builtin(args, MacroCall::MathFunction(MathFunction::Cross)))
1427        }
1428        "outerProduct" => {
1429            // bits layout
1430            // bit 0 through 3 - dims
1431            for bits in 0..0b1001 {
1432                let (size1, size2) = match bits {
1433                    0b0000 => (VectorSize::Bi, VectorSize::Bi),
1434                    0b0001 => (VectorSize::Bi, VectorSize::Tri),
1435                    0b0010 => (VectorSize::Bi, VectorSize::Quad),
1436                    0b0011 => (VectorSize::Tri, VectorSize::Bi),
1437                    0b0100 => (VectorSize::Tri, VectorSize::Tri),
1438                    0b0101 => (VectorSize::Tri, VectorSize::Quad),
1439                    0b0110 => (VectorSize::Quad, VectorSize::Bi),
1440                    0b0111 => (VectorSize::Quad, VectorSize::Tri),
1441                    _ => (VectorSize::Quad, VectorSize::Quad),
1442                };
1443
1444                let args = vec![
1445                    TypeInner::Vector {
1446                        size: size1,
1447                        scalar: float_scalar,
1448                    },
1449                    TypeInner::Vector {
1450                        size: size2,
1451                        scalar: float_scalar,
1452                    },
1453                ];
1454
1455                declaration
1456                    .overloads
1457                    .push(module.add_builtin(args, MacroCall::MathFunction(MathFunction::Outer)))
1458            }
1459        }
1460        "faceforward" | "fma" => {
1461            // bits layout
1462            // bit 0 through 1 - dims
1463            for bits in 0..0b100 {
1464                let size = match bits {
1465                    0b00 => None,
1466                    0b01 => Some(VectorSize::Bi),
1467                    0b10 => Some(VectorSize::Tri),
1468                    _ => Some(VectorSize::Quad),
1469                };
1470
1471                let ty = || match size {
1472                    Some(size) => TypeInner::Vector {
1473                        size,
1474                        scalar: float_scalar,
1475                    },
1476                    None => TypeInner::Scalar(float_scalar),
1477                };
1478                let args = vec![ty(), ty(), ty()];
1479
1480                let fun = match name {
1481                    "faceforward" => MacroCall::MathFunction(MathFunction::FaceForward),
1482                    "fma" => MacroCall::MathFunction(MathFunction::Fma),
1483                    _ => unreachable!(),
1484                };
1485
1486                declaration.overloads.push(module.add_builtin(args, fun))
1487            }
1488        }
1489        "refract" => {
1490            // bits layout
1491            // bit 0 through 1 - dims
1492            for bits in 0..0b100 {
1493                let size = match bits {
1494                    0b00 => None,
1495                    0b01 => Some(VectorSize::Bi),
1496                    0b10 => Some(VectorSize::Tri),
1497                    _ => Some(VectorSize::Quad),
1498                };
1499
1500                let ty = || match size {
1501                    Some(size) => TypeInner::Vector {
1502                        size,
1503                        scalar: float_scalar,
1504                    },
1505                    None => TypeInner::Scalar(float_scalar),
1506                };
1507                let args = vec![ty(), ty(), TypeInner::Scalar(Scalar::F32)];
1508                declaration
1509                    .overloads
1510                    .push(module.add_builtin(args, MacroCall::MathFunction(MathFunction::Refract)))
1511            }
1512        }
1513        "smoothstep" => {
1514            // bit 0 - splatted
1515            // bit 1 through 2 - dims
1516            for bits in 0..0b1000 {
1517                let splatted = bits & 0b1 == 0b1;
1518                let size = match bits >> 1 {
1519                    0b00 => None,
1520                    0b01 => Some(VectorSize::Bi),
1521                    0b10 => Some(VectorSize::Tri),
1522                    _ => Some(VectorSize::Quad),
1523                };
1524
1525                if splatted && size.is_none() {
1526                    continue;
1527                }
1528
1529                let base_ty = || match size {
1530                    Some(size) => TypeInner::Vector {
1531                        size,
1532                        scalar: float_scalar,
1533                    },
1534                    None => TypeInner::Scalar(float_scalar),
1535                };
1536                let ty = || match splatted {
1537                    true => TypeInner::Scalar(float_scalar),
1538                    false => base_ty(),
1539                };
1540                declaration.overloads.push(module.add_builtin(
1541                    vec![ty(), ty(), base_ty()],
1542                    MacroCall::SmoothStep { splatted: size },
1543                ))
1544            }
1545        }
1546        // The function isn't a builtin or we don't yet support it
1547        _ => {}
1548    }
1549}
1550
1551#[derive(Clone, Copy, PartialEq, Debug)]
1552pub enum TextureLevelType {
1553    None,
1554    Lod,
1555    Grad,
1556}
1557
1558/// A compiler defined builtin function
1559#[derive(Clone, Copy, PartialEq, Debug)]
1560pub enum MacroCall {
1561    Sampler,
1562    SamplerShadow,
1563    Texture {
1564        proj: bool,
1565        offset: bool,
1566        shadow: bool,
1567        level_type: TextureLevelType,
1568    },
1569    TextureSize {
1570        arrayed: bool,
1571    },
1572    TextureQueryLevels,
1573    ImageLoad {
1574        multi: bool,
1575    },
1576    ImageStore,
1577    MathFunction(MathFunction),
1578    FindLsbUint,
1579    FindMsbUint,
1580    SubgroupBallotFindLsb,
1581    SubgroupBallotFindMsb,
1582    BitfieldExtract,
1583    BitfieldInsert,
1584    Relational(RelationalFunction),
1585    Unary(UnaryOperator),
1586    Binary(BinaryOperator),
1587    Mod(Option<VectorSize>),
1588    Splatted(MathFunction, Option<VectorSize>, usize),
1589    MixBoolean,
1590    Clamp(Option<VectorSize>),
1591    BitCast(Sk),
1592    Derivate(Axis, Ctrl),
1593    Barrier,
1594    /// SmoothStep needs a separate variant because it might need it's inputs
1595    /// to be splatted depending on the overload
1596    SmoothStep {
1597        /// The size of the splat operation if some
1598        splatted: Option<VectorSize>,
1599    },
1600}
1601
1602impl MacroCall {
1603    /// Adds the necessary expressions and statements to the passed body and
1604    /// finally returns the final expression with the correct result
1605    pub fn call(
1606        &self,
1607        frontend: &mut Frontend,
1608        ctx: &mut Context,
1609        args: &mut [Handle<Expression>],
1610        meta: Span,
1611    ) -> Result<Option<Handle<Expression>>> {
1612        Ok(Some(match *self {
1613            MacroCall::Sampler => {
1614                ctx.samplers.insert(args[0], args[1]);
1615                args[0]
1616            }
1617            MacroCall::SamplerShadow => {
1618                sampled_to_depth(ctx, args[0], meta, &mut frontend.errors);
1619                ctx.invalidate_expression(args[0], meta)?;
1620                ctx.samplers.insert(args[0], args[1]);
1621                args[0]
1622            }
1623            MacroCall::Texture {
1624                proj,
1625                offset,
1626                shadow,
1627                level_type,
1628            } => {
1629                let mut coords = args[1];
1630
1631                if proj {
1632                    let size = match *ctx.resolve_type(coords, meta)? {
1633                        TypeInner::Vector { size, .. } => size,
1634                        _ => unreachable!(),
1635                    };
1636                    let mut right = ctx.add_expression(
1637                        Expression::AccessIndex {
1638                            base: coords,
1639                            index: size as u32 - 1,
1640                        },
1641                        Span::default(),
1642                    )?;
1643                    let left = if let VectorSize::Bi = size {
1644                        ctx.add_expression(
1645                            Expression::AccessIndex {
1646                                base: coords,
1647                                index: 0,
1648                            },
1649                            Span::default(),
1650                        )?
1651                    } else {
1652                        let size = match size {
1653                            VectorSize::Tri => VectorSize::Bi,
1654                            _ => VectorSize::Tri,
1655                        };
1656                        right = ctx.add_expression(
1657                            Expression::Splat { size, value: right },
1658                            Span::default(),
1659                        )?;
1660                        ctx.vector_resize(size, coords, Span::default())?
1661                    };
1662                    coords = ctx.add_expression(
1663                        Expression::Binary {
1664                            op: BinaryOperator::Divide,
1665                            left,
1666                            right,
1667                        },
1668                        Span::default(),
1669                    )?;
1670                }
1671
1672                let extra = args.get(2).copied();
1673                let comps = frontend.coordinate_components(ctx, args[0], coords, extra, meta)?;
1674
1675                let mut num_args = 2;
1676
1677                if comps.used_extra {
1678                    num_args += 1;
1679                };
1680
1681                // Parse out explicit texture level.
1682                let mut level = match level_type {
1683                    TextureLevelType::None => SampleLevel::Auto,
1684
1685                    TextureLevelType::Lod => {
1686                        num_args += 1;
1687
1688                        if shadow {
1689                            log::debug!("Assuming LOD {:?} is zero", args[2],);
1690
1691                            SampleLevel::Zero
1692                        } else {
1693                            SampleLevel::Exact(args[2])
1694                        }
1695                    }
1696
1697                    TextureLevelType::Grad => {
1698                        num_args += 2;
1699
1700                        if shadow {
1701                            log::debug!(
1702                                "Assuming gradients {:?} and {:?} are not greater than 1",
1703                                args[2],
1704                                args[3],
1705                            );
1706                            SampleLevel::Zero
1707                        } else {
1708                            SampleLevel::Gradient {
1709                                x: args[2],
1710                                y: args[3],
1711                            }
1712                        }
1713                    }
1714                };
1715
1716                let texture_offset = match offset {
1717                    true => {
1718                        let offset_arg = args[num_args];
1719                        num_args += 1;
1720                        Some(offset_arg)
1721                    }
1722                    false => None,
1723                };
1724
1725                // Now go back and look for optional bias arg (if available)
1726                if let TextureLevelType::None = level_type {
1727                    level = args
1728                        .get(num_args)
1729                        .copied()
1730                        .map_or(SampleLevel::Auto, SampleLevel::Bias);
1731                }
1732
1733                texture_call(ctx, args[0], level, comps, texture_offset, meta)?
1734            }
1735
1736            MacroCall::TextureSize { arrayed } => {
1737                let mut expr = ctx.add_expression(
1738                    Expression::ImageQuery {
1739                        image: args[0],
1740                        query: ImageQuery::Size {
1741                            level: args.get(1).copied(),
1742                        },
1743                    },
1744                    Span::default(),
1745                )?;
1746
1747                if arrayed {
1748                    let mut components = Vec::with_capacity(4);
1749
1750                    let size = match *ctx.resolve_type(expr, meta)? {
1751                        TypeInner::Vector { size: ori_size, .. } => {
1752                            for index in 0..(ori_size as u32) {
1753                                components.push(ctx.add_expression(
1754                                    Expression::AccessIndex { base: expr, index },
1755                                    Span::default(),
1756                                )?)
1757                            }
1758
1759                            match ori_size {
1760                                VectorSize::Bi => VectorSize::Tri,
1761                                _ => VectorSize::Quad,
1762                            }
1763                        }
1764                        _ => {
1765                            components.push(expr);
1766                            VectorSize::Bi
1767                        }
1768                    };
1769
1770                    components.push(ctx.add_expression(
1771                        Expression::ImageQuery {
1772                            image: args[0],
1773                            query: ImageQuery::NumLayers,
1774                        },
1775                        Span::default(),
1776                    )?);
1777
1778                    let ty = ctx.module.types.insert(
1779                        Type {
1780                            name: None,
1781                            inner: TypeInner::Vector {
1782                                size,
1783                                scalar: Scalar::U32,
1784                            },
1785                        },
1786                        Span::default(),
1787                    );
1788
1789                    expr = ctx.add_expression(Expression::Compose { components, ty }, meta)?
1790                }
1791
1792                ctx.add_expression(
1793                    Expression::As {
1794                        expr,
1795                        target: Scalar::I32,
1796                    },
1797                    Span::default(),
1798                )?
1799            }
1800            MacroCall::TextureQueryLevels => {
1801                let expr = ctx.add_expression(
1802                    Expression::ImageQuery {
1803                        image: args[0],
1804                        query: ImageQuery::NumLevels,
1805                    },
1806                    Span::default(),
1807                )?;
1808
1809                ctx.add_expression(
1810                    Expression::As {
1811                        expr,
1812                        target: Scalar::I32,
1813                    },
1814                    Span::default(),
1815                )?
1816            }
1817            MacroCall::ImageLoad { multi } => {
1818                let comps = frontend.coordinate_components(ctx, args[0], args[1], None, meta)?;
1819                let (sample, level) = match (multi, args.get(2)) {
1820                    (_, None) => (None, None),
1821                    (true, Some(&arg)) => (Some(arg), None),
1822                    (false, Some(&arg)) => (None, Some(arg)),
1823                };
1824                ctx.add_expression(
1825                    Expression::ImageLoad {
1826                        image: args[0],
1827                        coordinate: comps.coordinate,
1828                        array_index: comps.array_index,
1829                        sample,
1830                        level,
1831                    },
1832                    Span::default(),
1833                )?
1834            }
1835            MacroCall::ImageStore => {
1836                let comps = frontend.coordinate_components(ctx, args[0], args[1], None, meta)?;
1837                ctx.emit_restart();
1838                ctx.body.push(
1839                    crate::Statement::ImageStore {
1840                        image: args[0],
1841                        coordinate: comps.coordinate,
1842                        array_index: comps.array_index,
1843                        value: args[2],
1844                    },
1845                    meta,
1846                );
1847                return Ok(None);
1848            }
1849            MacroCall::MathFunction(fun) => ctx.add_expression(
1850                Expression::Math {
1851                    fun,
1852                    arg: args[0],
1853                    arg1: args.get(1).copied(),
1854                    arg2: args.get(2).copied(),
1855                    arg3: args.get(3).copied(),
1856                },
1857                Span::default(),
1858            )?,
1859            mc @ (MacroCall::FindLsbUint | MacroCall::FindMsbUint) => {
1860                let fun = match mc {
1861                    MacroCall::FindLsbUint => MathFunction::FirstTrailingBit,
1862                    MacroCall::FindMsbUint => MathFunction::FirstLeadingBit,
1863                    _ => unreachable!(),
1864                };
1865                let res = ctx.add_expression(
1866                    Expression::Math {
1867                        fun,
1868                        arg: args[0],
1869                        arg1: None,
1870                        arg2: None,
1871                        arg3: None,
1872                    },
1873                    Span::default(),
1874                )?;
1875                ctx.add_expression(
1876                    Expression::As {
1877                        expr: res,
1878                        target: Scalar::I32,
1879                    },
1880                    Span::default(),
1881                )?
1882            }
1883            mc @ (MacroCall::SubgroupBallotFindLsb | MacroCall::SubgroupBallotFindMsb) => {
1884                let order = match mc {
1885                    MacroCall::SubgroupBallotFindLsb => crate::BallotFindBitOrder::Lsb,
1886                    MacroCall::SubgroupBallotFindMsb => crate::BallotFindBitOrder::Msb,
1887                    _ => unreachable!(),
1888                };
1889                // `SubgroupOperationResult` must not be part of an `Emit` range, so
1890                // the pending range has to be flushed before creating it and
1891                // restarted afterwards.
1892                ctx.emit_end();
1893                let ty = ctx.module.types.insert(
1894                    Type {
1895                        name: None,
1896                        inner: TypeInner::Scalar(Scalar::U32),
1897                    },
1898                    Span::default(),
1899                );
1900                let result =
1901                    ctx.add_expression(Expression::SubgroupOperationResult { ty }, meta)?;
1902                ctx.body.push(
1903                    crate::Statement::SubgroupBallotFindBit {
1904                        order,
1905                        argument: args[0],
1906                        result,
1907                    },
1908                    meta,
1909                );
1910                ctx.emit_start();
1911                result
1912            }
1913            MacroCall::BitfieldInsert => {
1914                let conv_arg_2 = ctx.add_expression(
1915                    Expression::As {
1916                        expr: args[2],
1917                        target: Scalar::U32,
1918                    },
1919                    Span::default(),
1920                )?;
1921                let conv_arg_3 = ctx.add_expression(
1922                    Expression::As {
1923                        expr: args[3],
1924                        target: Scalar::U32,
1925                    },
1926                    Span::default(),
1927                )?;
1928                ctx.add_expression(
1929                    Expression::Math {
1930                        fun: MathFunction::InsertBits,
1931                        arg: args[0],
1932                        arg1: Some(args[1]),
1933                        arg2: Some(conv_arg_2),
1934                        arg3: Some(conv_arg_3),
1935                    },
1936                    Span::default(),
1937                )?
1938            }
1939            MacroCall::BitfieldExtract => {
1940                let conv_arg_1 = ctx.add_expression(
1941                    Expression::As {
1942                        expr: args[1],
1943                        target: Scalar::U32,
1944                    },
1945                    Span::default(),
1946                )?;
1947                let conv_arg_2 = ctx.add_expression(
1948                    Expression::As {
1949                        expr: args[2],
1950                        target: Scalar::U32,
1951                    },
1952                    Span::default(),
1953                )?;
1954                ctx.add_expression(
1955                    Expression::Math {
1956                        fun: MathFunction::ExtractBits,
1957                        arg: args[0],
1958                        arg1: Some(conv_arg_1),
1959                        arg2: Some(conv_arg_2),
1960                        arg3: None,
1961                    },
1962                    Span::default(),
1963                )?
1964            }
1965            MacroCall::Relational(fun) => ctx.add_expression(
1966                Expression::Relational {
1967                    fun,
1968                    argument: args[0],
1969                },
1970                Span::default(),
1971            )?,
1972            MacroCall::Unary(op) => {
1973                ctx.add_expression(Expression::Unary { op, expr: args[0] }, Span::default())?
1974            }
1975            MacroCall::Binary(op) => ctx.add_expression(
1976                Expression::Binary {
1977                    op,
1978                    left: args[0],
1979                    right: args[1],
1980                },
1981                Span::default(),
1982            )?,
1983            MacroCall::Mod(size) => {
1984                ctx.implicit_splat(&mut args[1], meta, size)?;
1985
1986                // x - y * floor(x / y)
1987
1988                let div = ctx.add_expression(
1989                    Expression::Binary {
1990                        op: BinaryOperator::Divide,
1991                        left: args[0],
1992                        right: args[1],
1993                    },
1994                    Span::default(),
1995                )?;
1996                let floor = ctx.add_expression(
1997                    Expression::Math {
1998                        fun: MathFunction::Floor,
1999                        arg: div,
2000                        arg1: None,
2001                        arg2: None,
2002                        arg3: None,
2003                    },
2004                    Span::default(),
2005                )?;
2006                let mult = ctx.add_expression(
2007                    Expression::Binary {
2008                        op: BinaryOperator::Multiply,
2009                        left: floor,
2010                        right: args[1],
2011                    },
2012                    Span::default(),
2013                )?;
2014                ctx.add_expression(
2015                    Expression::Binary {
2016                        op: BinaryOperator::Subtract,
2017                        left: args[0],
2018                        right: mult,
2019                    },
2020                    Span::default(),
2021                )?
2022            }
2023            MacroCall::Splatted(fun, size, i) => {
2024                ctx.implicit_splat(&mut args[i], meta, size)?;
2025
2026                ctx.add_expression(
2027                    Expression::Math {
2028                        fun,
2029                        arg: args[0],
2030                        arg1: args.get(1).copied(),
2031                        arg2: args.get(2).copied(),
2032                        arg3: args.get(3).copied(),
2033                    },
2034                    Span::default(),
2035                )?
2036            }
2037            MacroCall::MixBoolean => ctx.add_expression(
2038                Expression::Select {
2039                    condition: args[2],
2040                    accept: args[1],
2041                    reject: args[0],
2042                },
2043                Span::default(),
2044            )?,
2045            MacroCall::Clamp(size) => {
2046                ctx.implicit_splat(&mut args[1], meta, size)?;
2047                ctx.implicit_splat(&mut args[2], meta, size)?;
2048
2049                ctx.add_expression(
2050                    Expression::Math {
2051                        fun: MathFunction::Clamp,
2052                        arg: args[0],
2053                        arg1: args.get(1).copied(),
2054                        arg2: args.get(2).copied(),
2055                        arg3: args.get(3).copied(),
2056                    },
2057                    Span::default(),
2058                )?
2059            }
2060            MacroCall::BitCast(kind) => {
2061                let inner = match *ctx.resolve_type(args[0], meta)? {
2062                    TypeInner::Scalar(_) => TypeInner::Scalar(Scalar { kind, width: 4 }),
2063                    TypeInner::Vector { size, .. } => TypeInner::Vector {
2064                        size,
2065                        scalar: Scalar { kind, width: 4 },
2066                    },
2067                    _ => unreachable!(),
2068                };
2069                let ty = ctx
2070                    .module
2071                    .types
2072                    .insert(Type { name: None, inner }, Span::default());
2073                ctx.add_expression(Expression::Bitcast { expr: args[0], ty }, meta)?
2074            }
2075            MacroCall::Derivate(axis, ctrl) => ctx.add_expression(
2076                Expression::Derivative {
2077                    axis,
2078                    ctrl,
2079                    expr: args[0],
2080                },
2081                Span::default(),
2082            )?,
2083            MacroCall::Barrier => {
2084                ctx.emit_restart();
2085                ctx.body.push(
2086                    crate::Statement::ControlBarrier(crate::Barrier::all()),
2087                    meta,
2088                );
2089                return Ok(None);
2090            }
2091            MacroCall::SmoothStep { splatted } => {
2092                ctx.implicit_splat(&mut args[0], meta, splatted)?;
2093                ctx.implicit_splat(&mut args[1], meta, splatted)?;
2094
2095                ctx.add_expression(
2096                    Expression::Math {
2097                        fun: MathFunction::SmoothStep,
2098                        arg: args[0],
2099                        arg1: args.get(1).copied(),
2100                        arg2: args.get(2).copied(),
2101                        arg3: None,
2102                    },
2103                    Span::default(),
2104                )?
2105            }
2106        }))
2107    }
2108}
2109
2110fn texture_call(
2111    ctx: &mut Context,
2112    image: Handle<Expression>,
2113    level: SampleLevel,
2114    comps: CoordComponents,
2115    offset: Option<Handle<Expression>>,
2116    meta: Span,
2117) -> Result<Handle<Expression>> {
2118    if let Some(sampler) = ctx.samplers.get(&image).copied() {
2119        let mut array_index = comps.array_index;
2120
2121        if let Some(ref mut array_index_expr) = array_index {
2122            ctx.conversion(array_index_expr, meta, Scalar::I32)?;
2123        }
2124
2125        Ok(ctx.add_expression(
2126            Expression::ImageSample {
2127                image,
2128                sampler,
2129                gather: None, //TODO
2130                coordinate: comps.coordinate,
2131                array_index,
2132                offset,
2133                level,
2134                depth_ref: comps.depth_ref,
2135                clamp_to_edge: false,
2136            },
2137            meta,
2138        )?)
2139    } else {
2140        Err(Error {
2141            kind: ErrorKind::SemanticError("Bad call".into()),
2142            meta,
2143        })
2144    }
2145}
2146
2147/// Helper struct for texture calls with the separate components from the vector argument
2148///
2149/// Obtained by calling [`coordinate_components`](Frontend::coordinate_components)
2150#[derive(Debug)]
2151struct CoordComponents {
2152    coordinate: Handle<Expression>,
2153    depth_ref: Option<Handle<Expression>>,
2154    array_index: Option<Handle<Expression>>,
2155    used_extra: bool,
2156}
2157
2158impl Frontend {
2159    /// Helper function for texture calls, splits the vector argument into it's components
2160    fn coordinate_components(
2161        &mut self,
2162        ctx: &mut Context,
2163        image: Handle<Expression>,
2164        coord: Handle<Expression>,
2165        extra: Option<Handle<Expression>>,
2166        meta: Span,
2167    ) -> Result<CoordComponents> {
2168        if let TypeInner::Image {
2169            dim,
2170            arrayed,
2171            class,
2172        } = *ctx.resolve_type(image, meta)?
2173        {
2174            let image_size = match dim {
2175                Dim::D1 => None,
2176                Dim::D2 => Some(VectorSize::Bi),
2177                Dim::D3 => Some(VectorSize::Tri),
2178                Dim::Cube => Some(VectorSize::Tri),
2179            };
2180            let coord_size = match *ctx.resolve_type(coord, meta)? {
2181                TypeInner::Vector { size, .. } => Some(size),
2182                _ => None,
2183            };
2184            let (shadow, storage) = match class {
2185                ImageClass::Depth { .. } => (true, false),
2186                ImageClass::Storage { .. } => (false, true),
2187                ImageClass::Sampled { .. } => (false, false),
2188                ImageClass::External => unreachable!(),
2189            };
2190
2191            let coordinate = match (image_size, coord_size) {
2192                (Some(size), Some(coord_s)) if size != coord_s => {
2193                    ctx.vector_resize(size, coord, Span::default())?
2194                }
2195                (None, Some(_)) => ctx.add_expression(
2196                    Expression::AccessIndex {
2197                        base: coord,
2198                        index: 0,
2199                    },
2200                    Span::default(),
2201                )?,
2202                _ => coord,
2203            };
2204
2205            let mut coord_index = image_size.map_or(1, |s| s as u32);
2206
2207            let array_index = if arrayed && !(storage && dim == Dim::Cube) {
2208                let index = coord_index;
2209                coord_index += 1;
2210
2211                Some(ctx.add_expression(
2212                    Expression::AccessIndex { base: coord, index },
2213                    Span::default(),
2214                )?)
2215            } else {
2216                None
2217            };
2218            let mut used_extra = false;
2219            let depth_ref = match shadow {
2220                true => {
2221                    let index = coord_index;
2222
2223                    if index == 4 {
2224                        used_extra = true;
2225                        extra
2226                    } else {
2227                        Some(ctx.add_expression(
2228                            Expression::AccessIndex { base: coord, index },
2229                            Span::default(),
2230                        )?)
2231                    }
2232                }
2233                false => None,
2234            };
2235
2236            Ok(CoordComponents {
2237                coordinate,
2238                depth_ref,
2239                array_index,
2240                used_extra,
2241            })
2242        } else {
2243            self.errors.push(Error {
2244                kind: ErrorKind::SemanticError("Type is not an image".into()),
2245                meta,
2246            });
2247
2248            Ok(CoordComponents {
2249                coordinate: coord,
2250                depth_ref: None,
2251                array_index: None,
2252                used_extra: false,
2253            })
2254        }
2255    }
2256}
2257
2258/// Helper function to cast a expression holding a sampled image to a
2259/// depth image.
2260pub fn sampled_to_depth(
2261    ctx: &mut Context,
2262    image: Handle<Expression>,
2263    meta: Span,
2264    errors: &mut Vec<Error>,
2265) {
2266    // Get the a mutable type handle of the underlying image storage
2267    let ty = match ctx[image] {
2268        Expression::GlobalVariable(handle) => &mut ctx.module.global_variables.get_mut(handle).ty,
2269        Expression::FunctionArgument(i) => {
2270            // Mark the function argument as carrying a depth texture
2271            ctx.parameters_info[i as usize].depth = true;
2272            // NOTE: We need to later also change the parameter type
2273            &mut ctx.arguments[i as usize].ty
2274        }
2275        _ => {
2276            // Only globals and function arguments are allowed to carry an image
2277            return errors.push(Error {
2278                kind: ErrorKind::SemanticError("Not a valid texture expression".into()),
2279                meta,
2280            });
2281        }
2282    };
2283
2284    match ctx.module.types[*ty].inner {
2285        // Update the image class to depth in case it already isn't
2286        TypeInner::Image {
2287            class,
2288            dim,
2289            arrayed,
2290        } => match class {
2291            ImageClass::Sampled { multi, .. } => {
2292                *ty = ctx.module.types.insert(
2293                    Type {
2294                        name: None,
2295                        inner: TypeInner::Image {
2296                            dim,
2297                            arrayed,
2298                            class: ImageClass::Depth { multi },
2299                        },
2300                    },
2301                    Span::default(),
2302                )
2303            }
2304            ImageClass::Depth { .. } => {}
2305            // Other image classes aren't allowed to be transformed to depth
2306            ImageClass::Storage { .. } => errors.push(Error {
2307                kind: ErrorKind::SemanticError("Not a texture".into()),
2308                meta,
2309            }),
2310            ImageClass::External => unreachable!(),
2311        },
2312        _ => errors.push(Error {
2313            kind: ErrorKind::SemanticError("Not a texture".into()),
2314            meta,
2315        }),
2316    };
2317
2318    // Copy the handle to allow borrowing the `ctx` again
2319    let ty = *ty;
2320
2321    // If the image was passed through a function argument we also need to change
2322    // the corresponding parameter
2323    if let Expression::FunctionArgument(i) = ctx[image] {
2324        ctx.parameters[i as usize] = ty;
2325    }
2326}
2327
2328bitflags::bitflags! {
2329    /// Influences the operation [`texture_args_generator`]
2330    struct TextureArgsOptions: u32 {
2331        /// Generates multisampled variants of images
2332        const MULTI = 1 << 0;
2333        /// Generates shadow variants of images
2334        const SHADOW = 1 << 1;
2335        /// Generates standard images
2336        const STANDARD = 1 << 2;
2337        /// Generates cube arrayed images
2338        const CUBE_ARRAY = 1 << 3;
2339        /// Generates cube arrayed images
2340        const D2_MULTI_ARRAY = 1 << 4;
2341    }
2342}
2343
2344impl From<BuiltinVariations> for TextureArgsOptions {
2345    fn from(variations: BuiltinVariations) -> Self {
2346        let mut options = TextureArgsOptions::empty();
2347        if variations.contains(BuiltinVariations::STANDARD) {
2348            options |= TextureArgsOptions::STANDARD
2349        }
2350        if variations.contains(BuiltinVariations::CUBE_TEXTURES_ARRAY) {
2351            options |= TextureArgsOptions::CUBE_ARRAY
2352        }
2353        if variations.contains(BuiltinVariations::D2_MULTI_TEXTURES_ARRAY) {
2354            options |= TextureArgsOptions::D2_MULTI_ARRAY
2355        }
2356        options
2357    }
2358}
2359
2360/// Helper function to generate the image components for texture/image builtins
2361///
2362/// Calls the passed function `f` with:
2363/// ```text
2364/// f(ScalarKind, ImageDimension, arrayed, multi, shadow)
2365/// ```
2366///
2367/// `options` controls extra image variants generation like multisampling and depth,
2368/// see the struct documentation
2369fn texture_args_generator(
2370    options: TextureArgsOptions,
2371    mut f: impl FnMut(crate::ScalarKind, Dim, bool, bool, bool),
2372) {
2373    for kind in [Sk::Float, Sk::Uint, Sk::Sint].iter().copied() {
2374        for dim in [Dim::D1, Dim::D2, Dim::D3, Dim::Cube].iter().copied() {
2375            for arrayed in [false, true].iter().copied() {
2376                if dim == Dim::Cube && arrayed {
2377                    if !options.contains(TextureArgsOptions::CUBE_ARRAY) {
2378                        continue;
2379                    }
2380                } else if Dim::D2 == dim
2381                    && options.contains(TextureArgsOptions::MULTI)
2382                    && arrayed
2383                    && options.contains(TextureArgsOptions::D2_MULTI_ARRAY)
2384                {
2385                    // multisampling for sampler2DMSArray
2386                    f(kind, dim, arrayed, true, false);
2387                } else if !options.contains(TextureArgsOptions::STANDARD) {
2388                    continue;
2389                }
2390
2391                f(kind, dim, arrayed, false, false);
2392
2393                // 3D images can't be neither arrayed nor shadow
2394                // so we break out early, this way arrayed will always
2395                // be false and we won't hit the shadow branch
2396                if let Dim::D3 = dim {
2397                    break;
2398                }
2399
2400                if Dim::D2 == dim && options.contains(TextureArgsOptions::MULTI) && !arrayed {
2401                    // multisampling
2402                    f(kind, dim, arrayed, true, false);
2403                }
2404
2405                if Sk::Float == kind && options.contains(TextureArgsOptions::SHADOW) {
2406                    // shadow
2407                    f(kind, dim, arrayed, false, true);
2408                }
2409            }
2410        }
2411    }
2412}
2413
2414/// Helper functions used to convert from a image dimension into a integer representing the
2415/// number of components needed for the coordinates vector (1 means scalar instead of vector)
2416const fn image_dims_to_coords_size(dim: Dim) -> usize {
2417    match dim {
2418        Dim::D1 => 1,
2419        Dim::D2 => 2,
2420        _ => 3,
2421    }
2422}