naga/back/glsl/
writer.rs

1use super::*;
2
3/// Writer responsible for all code generation.
4pub struct Writer<'a, W> {
5    // Inputs
6    /// The module being written.
7    pub(in crate::back::glsl) module: &'a crate::Module,
8    /// The module analysis.
9    pub(in crate::back::glsl) info: &'a valid::ModuleInfo,
10    /// The output writer.
11    out: W,
12    /// User defined configuration to be used.
13    pub(in crate::back::glsl) options: &'a Options,
14    /// The bound checking policies to be used
15    pub(in crate::back::glsl) policies: proc::BoundsCheckPolicies,
16
17    // Internal State
18    /// Features manager used to store all the needed features and write them.
19    pub(in crate::back::glsl) features: FeaturesManager,
20    namer: proc::Namer,
21    /// A map with all the names needed for writing the module
22    /// (generated by a [`Namer`](crate::proc::Namer)).
23    names: crate::FastHashMap<NameKey, String>,
24    /// A map with the names of global variables needed for reflections.
25    reflection_names_globals: crate::FastHashMap<Handle<crate::GlobalVariable>, String>,
26    /// The selected entry point.
27    pub(in crate::back::glsl) entry_point: &'a crate::EntryPoint,
28    /// The index of the selected entry point.
29    pub(in crate::back::glsl) entry_point_idx: proc::EntryPointIndex,
30    /// A generator for unique block numbers.
31    block_id: IdGenerator,
32    /// Set of expressions that have associated temporary variables.
33    named_expressions: crate::NamedExpressions,
34    /// Set of expressions that need to be baked to avoid unnecessary repetition in output
35    need_bake_expressions: back::NeedBakeExpressions,
36    /// Information about nesting of loops and switches.
37    ///
38    /// Used for forwarding continue statements in switches that have been
39    /// transformed to `do {} while(false);` loops.
40    continue_ctx: back::continue_forward::ContinueCtx,
41    /// How many views to render to, if doing multiview rendering.
42    pub(in crate::back::glsl) multiview: Option<core::num::NonZeroU32>,
43    /// Mapping of varying variables to their location. Needed for reflections.
44    varying: crate::FastHashMap<String, VaryingLocation>,
45    /// Number of user-defined clip planes. Only non-zero for vertex shaders.
46    clip_distance_count: u32,
47}
48
49impl<'a, W: Write> Writer<'a, W> {
50    /// Creates a new [`Writer`] instance.
51    ///
52    /// # Errors
53    /// - If the version specified is invalid or supported.
54    /// - If the entry point couldn't be found in the module.
55    /// - If the version specified doesn't support some used features.
56    pub fn new(
57        out: W,
58        module: &'a crate::Module,
59        info: &'a valid::ModuleInfo,
60        options: &'a Options,
61        pipeline_options: &'a PipelineOptions,
62        policies: proc::BoundsCheckPolicies,
63    ) -> Result<Self, Error> {
64        // Check if the requested version is supported
65        if !options.version.is_supported() {
66            log::error!("Version {}", options.version);
67            return Err(Error::VersionNotSupported);
68        }
69
70        // Try to find the entry point and corresponding index
71        let ep_idx = module
72            .entry_points
73            .iter()
74            .position(|ep| {
75                pipeline_options.shader_stage == ep.stage && pipeline_options.entry_point == ep.name
76            })
77            .ok_or(Error::EntryPointNotFound)?;
78
79        // Generate a map with names required to write the module
80        let mut names = crate::FastHashMap::default();
81        let mut namer = proc::Namer::default();
82        namer.reset(
83            module,
84            &keywords::RESERVED_KEYWORD_SET,
85            proc::KeywordSet::empty(),
86            proc::CaseInsensitiveKeywordSet::empty(),
87            &[
88                "gl_",                  // all GL built-in variables
89                "_group",               // all normal bindings
90                "_immediates_binding_", // all immediate data bindings
91            ],
92            &mut names,
93        );
94
95        // Build the instance
96        let mut this = Self {
97            module,
98            info,
99            out,
100            options,
101            policies,
102
103            namer,
104            features: FeaturesManager::new(),
105            names,
106            reflection_names_globals: crate::FastHashMap::default(),
107            entry_point: &module.entry_points[ep_idx],
108            entry_point_idx: ep_idx as u16,
109            multiview: pipeline_options.multiview,
110            block_id: IdGenerator::default(),
111            named_expressions: Default::default(),
112            need_bake_expressions: Default::default(),
113            continue_ctx: back::continue_forward::ContinueCtx::default(),
114            varying: Default::default(),
115            clip_distance_count: 0,
116        };
117
118        // Find all features required to print this module
119        this.collect_required_features()?;
120
121        Ok(this)
122    }
123
124    /// Writes the [`Module`](crate::Module) as glsl to the output
125    ///
126    /// # Notes
127    /// If an error occurs while writing, the output might have been written partially
128    ///
129    /// # Panics
130    /// Might panic if the module is invalid
131    pub fn write(&mut self) -> Result<ReflectionInfo, Error> {
132        // We use `writeln!(self.out)` throughout the write to add newlines
133        // to make the output more readable
134
135        let es = self.options.version.is_es();
136
137        // Write the version (It must be the first thing or it isn't a valid glsl output)
138        writeln!(self.out, "#version {}", self.options.version)?;
139        // Write all the needed extensions
140        //
141        // This used to be the last thing being written as it allowed to search for features while
142        // writing the module saving some loops but some older versions (420 or less) required the
143        // extensions to appear before being used, even though extensions are part of the
144        // preprocessor not the processor ¯\_(ツ)_/¯
145        self.features.write(self.options, &mut self.out)?;
146
147        // glsl es requires a precision to be specified for floats and ints
148        // TODO: Should this be user configurable?
149        if es {
150            writeln!(self.out)?;
151            writeln!(self.out, "precision highp float;")?;
152            writeln!(self.out, "precision highp int;")?;
153            writeln!(self.out)?;
154        }
155
156        if self.entry_point.stage == ShaderStage::Compute {
157            let workgroup_size = self.entry_point.workgroup_size;
158            writeln!(
159                self.out,
160                "layout(local_size_x = {}, local_size_y = {}, local_size_z = {}) in;",
161                workgroup_size[0], workgroup_size[1], workgroup_size[2]
162            )?;
163            writeln!(self.out)?;
164        }
165
166        if self.entry_point.stage == ShaderStage::Vertex
167            && !self
168                .options
169                .writer_flags
170                .contains(WriterFlags::DRAW_PARAMETERS)
171            && self.features.contains(Features::INSTANCE_INDEX)
172        {
173            writeln!(self.out, "uniform uint {FIRST_INSTANCE_BINDING};")?;
174            writeln!(self.out)?;
175        }
176
177        // Enable early depth tests if needed
178        if let Some(early_depth_test) = self.entry_point.early_depth_test {
179            // If early depth test is supported for this version of GLSL
180            if self.options.version.supports_early_depth_test() {
181                match early_depth_test {
182                    crate::EarlyDepthTest::Force => {
183                        writeln!(self.out, "layout(early_fragment_tests) in;")?;
184                    }
185                    crate::EarlyDepthTest::Allow { conservative, .. } => {
186                        use crate::ConservativeDepth as Cd;
187                        let depth = match conservative {
188                            Cd::GreaterEqual => "greater",
189                            Cd::LessEqual => "less",
190                            Cd::Unchanged => "unchanged",
191                        };
192                        writeln!(self.out, "layout (depth_{depth}) out float gl_FragDepth;")?;
193                    }
194                }
195            } else {
196                log::warn!(
197                    "Early depth testing is not supported for this version of GLSL: {}",
198                    self.options.version
199                );
200            }
201        }
202
203        if self.entry_point.stage == ShaderStage::Vertex && self.options.version.is_webgl() {
204            if let Some(multiview) = self.multiview.as_ref() {
205                writeln!(self.out, "layout(num_views = {multiview}) in;")?;
206                writeln!(self.out)?;
207            }
208        }
209
210        // Write struct types.
211        //
212        // This are always ordered because the IR is structured in a way that
213        // you can't make a struct without adding all of its members first.
214        for (handle, ty) in self.module.types.iter() {
215            if let TypeInner::Struct { ref members, .. } = ty.inner {
216                let struct_name = &self.names[&NameKey::Type(handle)];
217
218                // Structures ending with runtime-sized arrays can only be
219                // rendered as shader storage blocks in GLSL, not stand-alone
220                // struct types.
221                if !self.module.types[members.last().unwrap().ty]
222                    .inner
223                    .is_dynamically_sized(&self.module.types)
224                {
225                    write!(self.out, "struct {struct_name} ")?;
226                    self.write_struct_body(handle, members)?;
227                    writeln!(self.out, ";")?;
228                }
229            }
230        }
231
232        // Write functions for special types.
233        for (type_key, struct_ty) in self.module.special_types.predeclared_types.iter() {
234            match type_key {
235                &crate::PredeclaredType::ModfResult { size, scalar }
236                | &crate::PredeclaredType::FrexpResult { size, scalar } => {
237                    let struct_name = &self.names[&NameKey::Type(*struct_ty)];
238                    let arg_type_name_owner;
239                    let arg_type_name = if let Some(size) = size {
240                        arg_type_name_owner = format!(
241                            "{}vec{}",
242                            if scalar.width == 8 { "d" } else { "" },
243                            size as u8
244                        );
245                        &arg_type_name_owner
246                    } else if scalar.width == 8 {
247                        "double"
248                    } else {
249                        "float"
250                    };
251
252                    let other_type_name_owner;
253                    let (defined_func_name, called_func_name, other_type_name) =
254                        if matches!(type_key, &crate::PredeclaredType::ModfResult { .. }) {
255                            (MODF_FUNCTION, "modf", arg_type_name)
256                        } else {
257                            let other_type_name = if let Some(size) = size {
258                                other_type_name_owner = format!("ivec{}", size as u8);
259                                &other_type_name_owner
260                            } else {
261                                "int"
262                            };
263                            (FREXP_FUNCTION, "frexp", other_type_name)
264                        };
265
266                    writeln!(self.out)?;
267                    if !self.options.version.supports_frexp_function()
268                        && matches!(type_key, &crate::PredeclaredType::FrexpResult { .. })
269                    {
270                        writeln!(
271                            self.out,
272                            "{struct_name} {defined_func_name}({arg_type_name} arg) {{
273    {other_type_name} other = arg == {arg_type_name}(0) ? {other_type_name}(0) : {other_type_name}({arg_type_name}(1) + log2(arg));
274    {arg_type_name} fract = arg * exp2({arg_type_name}(-other));
275    return {struct_name}(fract, other);
276}}",
277                        )?;
278                    } else {
279                        writeln!(
280                            self.out,
281                            "{struct_name} {defined_func_name}({arg_type_name} arg) {{
282    {other_type_name} other;
283    {arg_type_name} fract = {called_func_name}(arg, other);
284    return {struct_name}(fract, other);
285}}",
286                        )?;
287                    }
288                }
289                &crate::PredeclaredType::AtomicCompareExchangeWeakResult(_) => {
290                    // Handled by the general struct writing loop earlier.
291                }
292            }
293        }
294
295        // Write all named constants
296        let mut constants = self
297            .module
298            .constants
299            .iter()
300            .filter(|&(_, c)| c.name.is_some())
301            .peekable();
302        while let Some((handle, _)) = constants.next() {
303            self.write_global_constant(handle)?;
304            // Add extra newline for readability on last iteration
305            if constants.peek().is_none() {
306                writeln!(self.out)?;
307            }
308        }
309
310        let ep_info = self.info.get_entry_point(self.entry_point_idx as usize);
311
312        // Write the globals
313        //
314        // Unless explicitly disabled with WriterFlags::INCLUDE_UNUSED_ITEMS,
315        // we filter all globals that aren't used by the selected entry point as they might be
316        // interfere with each other (i.e. two globals with the same location but different with
317        // different classes)
318        let include_unused = self
319            .options
320            .writer_flags
321            .contains(WriterFlags::INCLUDE_UNUSED_ITEMS);
322        for (handle, global) in self.module.global_variables.iter() {
323            let is_unused = ep_info[handle].is_empty();
324            if !include_unused && is_unused {
325                continue;
326            }
327
328            match self.module.types[global.ty].inner {
329                // We treat images separately because they might require
330                // writing the storage format
331                TypeInner::Image {
332                    mut dim,
333                    arrayed,
334                    class,
335                } => {
336                    // Gather the storage format if needed
337                    let storage_format_access = match self.module.types[global.ty].inner {
338                        TypeInner::Image {
339                            class: crate::ImageClass::Storage { format, access },
340                            ..
341                        } => Some((format, access)),
342                        _ => None,
343                    };
344
345                    if dim == crate::ImageDimension::D1 && es {
346                        dim = crate::ImageDimension::D2
347                    }
348
349                    // Gether the location if needed
350                    let layout_binding = if self.options.version.supports_explicit_locations() {
351                        let br = global.binding.as_ref().unwrap();
352                        self.options.binding_map.get(br).cloned()
353                    } else {
354                        None
355                    };
356
357                    // Write all the layout qualifiers
358                    if layout_binding.is_some() || storage_format_access.is_some() {
359                        write!(self.out, "layout(")?;
360                        if let Some(binding) = layout_binding {
361                            write!(self.out, "binding = {binding}")?;
362                        }
363                        if let Some((format, _)) = storage_format_access {
364                            let format_str = glsl_storage_format(format)?;
365                            let separator = match layout_binding {
366                                Some(_) => ",",
367                                None => "",
368                            };
369                            write!(self.out, "{separator}{format_str}")?;
370                        }
371                        write!(self.out, ") ")?;
372                    }
373
374                    if let Some((_, access)) = storage_format_access {
375                        self.write_storage_access(access)?;
376                    }
377
378                    // All images in glsl are `uniform`
379                    // The trailing space is important
380                    write!(self.out, "uniform ")?;
381
382                    // write the type
383                    //
384                    // This is way we need the leading space because `write_image_type` doesn't add
385                    // any spaces at the beginning or end
386                    self.write_image_type(dim, arrayed, class)?;
387
388                    // Finally write the name and end the global with a `;`
389                    // The leading space is important
390                    let global_name = self.get_global_name(handle, global);
391                    writeln!(self.out, " {global_name};")?;
392                    writeln!(self.out)?;
393
394                    self.reflection_names_globals.insert(handle, global_name);
395                }
396                // glsl has no concept of samplers so we just ignore it
397                TypeInner::Sampler { .. } => continue,
398                // All other globals are written by `write_global`
399                _ => {
400                    self.write_global(handle, global)?;
401                    // Add a newline (only for readability)
402                    writeln!(self.out)?;
403                }
404            }
405        }
406
407        for arg in self.entry_point.function.arguments.iter() {
408            self.write_varying(arg.binding.as_ref(), arg.ty, false)?;
409        }
410        if let Some(ref result) = self.entry_point.function.result {
411            self.write_varying(result.binding.as_ref(), result.ty, true)?;
412        }
413        writeln!(self.out)?;
414
415        // Write all regular functions
416        for (handle, function) in self.module.functions.iter() {
417            // Check that the function doesn't use globals that aren't supported
418            // by the current entry point
419            if !include_unused && !ep_info.dominates_global_use(&self.info[handle]) {
420                continue;
421            }
422
423            let fun_info = &self.info[handle];
424
425            // Skip functions that that are not compatible with this entry point's stage.
426            //
427            // When validation is enabled, it rejects modules whose entry points try to call
428            // incompatible functions, so if we got this far, then any functions incompatible
429            // with our selected entry point must not be used.
430            //
431            // When validation is disabled, `fun_info.available_stages` is always just
432            // `ShaderStages::all()`, so this will write all functions in the module, and
433            // the downstream GLSL compiler will catch any problems.
434            if !fun_info.available_stages.contains(ep_info.available_stages) {
435                continue;
436            }
437
438            // Write the function
439            self.write_function(back::FunctionType::Function(handle), function, fun_info)?;
440
441            writeln!(self.out)?;
442        }
443
444        self.write_function(
445            back::FunctionType::EntryPoint(self.entry_point_idx),
446            &self.entry_point.function,
447            ep_info,
448        )?;
449
450        // Add newline at the end of file
451        writeln!(self.out)?;
452
453        // Collect all reflection info and return it to the user
454        self.collect_reflection_info()
455    }
456
457    fn write_array_size(
458        &mut self,
459        base: Handle<crate::Type>,
460        size: crate::ArraySize,
461    ) -> BackendResult {
462        write!(self.out, "[")?;
463
464        // Write the array size
465        // Writes nothing if `IndexableLength::Dynamic`
466        match size.resolve(self.module.to_ctx())? {
467            proc::IndexableLength::Known(size) => {
468                write!(self.out, "{size}")?;
469            }
470            proc::IndexableLength::Dynamic => (),
471        }
472
473        write!(self.out, "]")?;
474
475        if let TypeInner::Array {
476            base: next_base,
477            size: next_size,
478            ..
479        } = self.module.types[base].inner
480        {
481            self.write_array_size(next_base, next_size)?;
482        }
483
484        Ok(())
485    }
486
487    /// Helper method used to write value types
488    ///
489    /// # Notes
490    /// Adds no trailing or leading whitespace
491    fn write_value_type(&mut self, inner: &TypeInner) -> BackendResult {
492        match *inner {
493            // Scalars are simple we just get the full name from `glsl_scalar`
494            TypeInner::Scalar(scalar)
495            | TypeInner::Atomic(scalar)
496            | TypeInner::ValuePointer {
497                size: None,
498                scalar,
499                space: _,
500            } => write!(self.out, "{}", glsl_scalar(scalar)?.full)?,
501            // Vectors are just `gvecN` where `g` is the scalar prefix and `N` is the vector size
502            TypeInner::Vector { size, scalar }
503            | TypeInner::ValuePointer {
504                size: Some(size),
505                scalar,
506                space: _,
507            } => write!(self.out, "{}vec{}", glsl_scalar(scalar)?.prefix, size as u8)?,
508            // Matrices are written with `gmatMxN` where `g` is the scalar prefix (only floats and
509            // doubles are allowed), `M` is the columns count and `N` is the rows count
510            //
511            // glsl supports a matrix shorthand `gmatN` where `N` = `M` but it doesn't justify the
512            // extra branch to write matrices this way
513            TypeInner::Matrix {
514                columns,
515                rows,
516                scalar,
517            } => write!(
518                self.out,
519                "{}mat{}x{}",
520                glsl_scalar(scalar)?.prefix,
521                columns as u8,
522                rows as u8
523            )?,
524            // GLSL arrays are written as `type name[size]`
525            // Here we only write the size of the array i.e. `[size]`
526            // Base `type` and `name` should be written outside
527            TypeInner::Array { base, size, .. } => self.write_array_size(base, size)?,
528            // Write all variants instead of `_` so that if new variants are added a
529            // no exhaustiveness error is thrown
530            TypeInner::Pointer { .. }
531            | TypeInner::Struct { .. }
532            | TypeInner::Image { .. }
533            | TypeInner::Sampler { .. }
534            | TypeInner::AccelerationStructure { .. }
535            | TypeInner::RayQuery { .. }
536            | TypeInner::BindingArray { .. }
537            | TypeInner::CooperativeMatrix { .. } => {
538                return Err(Error::Custom(format!("Unable to write type {inner:?}")))
539            }
540        }
541
542        Ok(())
543    }
544
545    /// Helper method used to write non image/sampler types
546    ///
547    /// # Notes
548    /// Adds no trailing or leading whitespace
549    fn write_type(&mut self, ty: Handle<crate::Type>) -> BackendResult {
550        match self.module.types[ty].inner {
551            // glsl has no pointer types so just write types as normal and loads are skipped
552            TypeInner::Pointer { base, .. } => self.write_type(base),
553            // glsl structs are written as just the struct name
554            TypeInner::Struct { .. } => {
555                // Get the struct name
556                let name = &self.names[&NameKey::Type(ty)];
557                write!(self.out, "{name}")?;
558                Ok(())
559            }
560            // glsl array has the size separated from the base type
561            TypeInner::Array { base, .. } => self.write_type(base),
562            ref other => self.write_value_type(other),
563        }
564    }
565
566    /// Helper method to write a image type
567    ///
568    /// # Notes
569    /// Adds no leading or trailing whitespace
570    fn write_image_type(
571        &mut self,
572        dim: crate::ImageDimension,
573        arrayed: bool,
574        class: crate::ImageClass,
575    ) -> BackendResult {
576        // glsl images consist of four parts the scalar prefix, the image "type", the dimensions
577        // and modifiers
578        //
579        // There exists two image types
580        // - sampler - for sampled images
581        // - image - for storage images
582        //
583        // There are three possible modifiers that can be used together and must be written in
584        // this order to be valid
585        // - MS - used if it's a multisampled image
586        // - Array - used if it's an image array
587        // - Shadow - used if it's a depth image
588        use crate::ImageClass as Ic;
589        use crate::Scalar as S;
590        let float = S {
591            kind: crate::ScalarKind::Float,
592            width: 4,
593        };
594        let (base, scalar, ms, comparison) = match class {
595            Ic::Sampled { kind, multi: true } => ("sampler", S { kind, width: 4 }, "MS", ""),
596            Ic::Sampled { kind, multi: false } => ("sampler", S { kind, width: 4 }, "", ""),
597            Ic::Depth { multi: true } => ("sampler", float, "MS", ""),
598            Ic::Depth { multi: false } => ("sampler", float, "", "Shadow"),
599            Ic::Storage { format, .. } => ("image", format.into(), "", ""),
600            Ic::External => unimplemented!(),
601        };
602
603        let precision = if self.options.version.is_es() {
604            "highp "
605        } else {
606            ""
607        };
608
609        write!(
610            self.out,
611            "{}{}{}{}{}{}{}",
612            precision,
613            glsl_scalar(scalar)?.prefix,
614            base,
615            glsl_dimension(dim),
616            ms,
617            if arrayed { "Array" } else { "" },
618            comparison
619        )?;
620
621        Ok(())
622    }
623
624    /// Helper method used by [Self::write_global] to write just the layout part of
625    /// a non image/sampler global variable, if applicable.
626    ///
627    /// # Notes
628    ///
629    /// Adds trailing whitespace if any layout qualifier is written
630    fn write_global_layout(&mut self, global: &crate::GlobalVariable) -> BackendResult {
631        // Determine which (if any) explicit memory layout to use, and whether we support it
632        let layout = match global.space {
633            crate::AddressSpace::Uniform => {
634                if !self.options.version.supports_std140_layout() {
635                    return Err(Error::Custom(
636                        "Uniform address space requires std140 layout support".to_string(),
637                    ));
638                }
639
640                Some("std140")
641            }
642            crate::AddressSpace::Storage { .. } => {
643                if !self.options.version.supports_std430_layout() {
644                    return Err(Error::Custom(
645                        "Storage address space requires std430 layout support".to_string(),
646                    ));
647                }
648
649                Some("std430")
650            }
651            _ => None,
652        };
653
654        // If our version supports explicit layouts, we can also output the explicit binding
655        // if we have it
656        if self.options.version.supports_explicit_locations() {
657            if let Some(ref br) = global.binding {
658                match self.options.binding_map.get(br) {
659                    Some(binding) => {
660                        write!(self.out, "layout(")?;
661
662                        if let Some(layout) = layout {
663                            write!(self.out, "{layout}, ")?;
664                        }
665
666                        write!(self.out, "binding = {binding}) ")?;
667
668                        return Ok(());
669                    }
670                    None => {
671                        log::debug!("unassigned binding for {:?}", global.name);
672                    }
673                }
674            }
675        }
676
677        // Either no explicit bindings are supported or we didn't have any.
678        // Write just the memory layout.
679        if let Some(layout) = layout {
680            write!(self.out, "layout({layout}) ")?;
681        }
682
683        Ok(())
684    }
685
686    /// Helper method used to write non images/sampler globals
687    ///
688    /// # Notes
689    /// Adds a newline
690    ///
691    /// # Panics
692    /// If the global has type sampler
693    fn write_global(
694        &mut self,
695        handle: Handle<crate::GlobalVariable>,
696        global: &crate::GlobalVariable,
697    ) -> BackendResult {
698        self.write_global_layout(global)?;
699
700        if let crate::AddressSpace::Storage { access } = global.space {
701            self.write_storage_access(access)?;
702            if global
703                .memory_decorations
704                .contains(crate::MemoryDecorations::COHERENT)
705            {
706                write!(self.out, "coherent ")?;
707            }
708            if global
709                .memory_decorations
710                .contains(crate::MemoryDecorations::VOLATILE)
711            {
712                write!(self.out, "volatile ")?;
713            }
714        }
715
716        if let Some(storage_qualifier) = glsl_storage_qualifier(global.space) {
717            write!(self.out, "{storage_qualifier} ")?;
718        }
719
720        match global.space {
721            crate::AddressSpace::Private => {
722                self.write_simple_global(handle, global)?;
723            }
724            crate::AddressSpace::WorkGroup => {
725                self.write_simple_global(handle, global)?;
726            }
727            crate::AddressSpace::Immediate => {
728                self.write_simple_global(handle, global)?;
729            }
730            crate::AddressSpace::Uniform => {
731                self.write_interface_block(handle, global)?;
732            }
733            crate::AddressSpace::Storage { .. } => {
734                self.write_interface_block(handle, global)?;
735            }
736            crate::AddressSpace::TaskPayload => {
737                self.write_interface_block(handle, global)?;
738            }
739            // A global variable in the `Function` address space is a
740            // contradiction in terms.
741            crate::AddressSpace::Function => unreachable!(),
742            // Textures and samplers are handled directly in `Writer::write`.
743            crate::AddressSpace::Handle => unreachable!(),
744            // ray tracing pipelines unsupported
745            crate::AddressSpace::RayPayload | crate::AddressSpace::IncomingRayPayload => {
746                unreachable!()
747            }
748        }
749
750        Ok(())
751    }
752
753    fn write_simple_global(
754        &mut self,
755        handle: Handle<crate::GlobalVariable>,
756        global: &crate::GlobalVariable,
757    ) -> BackendResult {
758        self.write_type(global.ty)?;
759        write!(self.out, " ")?;
760        self.write_global_name(handle, global)?;
761
762        if let TypeInner::Array { base, size, .. } = self.module.types[global.ty].inner {
763            self.write_array_size(base, size)?;
764        }
765
766        if global.space.initializable() && is_value_init_supported(self.module, global.ty) {
767            write!(self.out, " = ")?;
768            if let Some(init) = global.init {
769                self.write_const_expr(init, &self.module.global_expressions)?;
770            } else {
771                self.write_zero_init_value(global.ty)?;
772            }
773        }
774
775        writeln!(self.out, ";")?;
776
777        if let crate::AddressSpace::Immediate = global.space {
778            let global_name = self.get_global_name(handle, global);
779            self.reflection_names_globals.insert(handle, global_name);
780        }
781
782        Ok(())
783    }
784
785    /// Write an interface block for a single Naga global.
786    ///
787    /// Write `block_name { members }`. Since `block_name` must be unique
788    /// between blocks and structs, we add `_block_ID` where `ID` is a
789    /// `IdGenerator` generated number. Write `members` in the same way we write
790    /// a struct's members.
791    fn write_interface_block(
792        &mut self,
793        handle: Handle<crate::GlobalVariable>,
794        global: &crate::GlobalVariable,
795    ) -> BackendResult {
796        // Write the block name, it's just the struct name appended with `_block_ID`
797        let ty_name = &self.names[&NameKey::Type(global.ty)];
798        let block_name = format!(
799            "{}_block_{}{:?}",
800            // avoid double underscores as they are reserved in GLSL
801            ty_name.trim_end_matches('_'),
802            self.block_id.generate(),
803            self.entry_point.stage,
804        );
805        write!(self.out, "{block_name} ")?;
806        self.reflection_names_globals.insert(handle, block_name);
807
808        match self.module.types[global.ty].inner {
809            TypeInner::Struct { ref members, .. }
810                if self.module.types[members.last().unwrap().ty]
811                    .inner
812                    .is_dynamically_sized(&self.module.types) =>
813            {
814                // Structs with dynamically sized arrays must have their
815                // members lifted up as members of the interface block. GLSL
816                // can't write such struct types anyway.
817                self.write_struct_body(global.ty, members)?;
818                write!(self.out, " ")?;
819                self.write_global_name(handle, global)?;
820            }
821            _ => {
822                // A global of any other type is written as the sole member
823                // of the interface block. Since the interface block is
824                // anonymous, this becomes visible in the global scope.
825                write!(self.out, "{{ ")?;
826                self.write_type(global.ty)?;
827                write!(self.out, " ")?;
828                self.write_global_name(handle, global)?;
829                if let TypeInner::Array { base, size, .. } = self.module.types[global.ty].inner {
830                    self.write_array_size(base, size)?;
831                }
832                write!(self.out, "; }}")?;
833            }
834        }
835
836        writeln!(self.out, ";")?;
837
838        Ok(())
839    }
840
841    /// Helper method used to find which expressions of a given function require baking
842    ///
843    /// # Notes
844    /// Clears `need_bake_expressions` set before adding to it
845    fn update_expressions_to_bake(&mut self, func: &crate::Function, info: &valid::FunctionInfo) {
846        use crate::Expression;
847        self.need_bake_expressions.clear();
848        for (fun_handle, expr) in func.expressions.iter() {
849            let expr_info = &info[fun_handle];
850            let min_ref_count = func.expressions[fun_handle].bake_ref_count();
851            if min_ref_count <= expr_info.ref_count {
852                self.need_bake_expressions.insert(fun_handle);
853            }
854
855            let inner = expr_info.ty.inner_with(&self.module.types);
856
857            if let Expression::Math {
858                fun,
859                arg,
860                arg1,
861                arg2,
862                ..
863            } = *expr
864            {
865                match fun {
866                    crate::MathFunction::Dot => {
867                        // if the expression is a Dot product with integer arguments,
868                        // then the args needs baking as well
869                        if let TypeInner::Scalar(crate::Scalar {
870                            kind: crate::ScalarKind::Sint | crate::ScalarKind::Uint,
871                            ..
872                        }) = *inner
873                        {
874                            self.need_bake_expressions.insert(arg);
875                            self.need_bake_expressions.insert(arg1.unwrap());
876                        }
877                    }
878                    crate::MathFunction::Dot4U8Packed | crate::MathFunction::Dot4I8Packed => {
879                        self.need_bake_expressions.insert(arg);
880                        self.need_bake_expressions.insert(arg1.unwrap());
881                    }
882                    crate::MathFunction::Pack4xI8
883                    | crate::MathFunction::Pack4xU8
884                    | crate::MathFunction::Pack4xI8Clamp
885                    | crate::MathFunction::Pack4xU8Clamp
886                    | crate::MathFunction::Unpack4xI8
887                    | crate::MathFunction::Unpack4xU8
888                    | crate::MathFunction::QuantizeToF16 => {
889                        self.need_bake_expressions.insert(arg);
890                    }
891                    /* crate::MathFunction::Pack4x8unorm | */
892                    crate::MathFunction::Unpack4x8snorm
893                        if !self.options.version.supports_pack_unpack_4x8() =>
894                    {
895                        // We have a fallback if the platform doesn't natively support these
896                        self.need_bake_expressions.insert(arg);
897                    }
898                    /* crate::MathFunction::Pack4x8unorm | */
899                    crate::MathFunction::Unpack4x8unorm
900                        if !self.options.version.supports_pack_unpack_4x8() =>
901                    {
902                        self.need_bake_expressions.insert(arg);
903                    }
904                    /* crate::MathFunction::Pack2x16snorm |  */
905                    crate::MathFunction::Unpack2x16snorm
906                        if !self.options.version.supports_pack_unpack_snorm_2x16() =>
907                    {
908                        self.need_bake_expressions.insert(arg);
909                    }
910                    /* crate::MathFunction::Pack2x16unorm | */
911                    crate::MathFunction::Unpack2x16unorm
912                        if !self.options.version.supports_pack_unpack_unorm_2x16() =>
913                    {
914                        self.need_bake_expressions.insert(arg);
915                    }
916                    crate::MathFunction::ExtractBits => {
917                        // Only argument 1 is re-used.
918                        self.need_bake_expressions.insert(arg1.unwrap());
919                    }
920                    crate::MathFunction::InsertBits => {
921                        // Only argument 2 is re-used.
922                        self.need_bake_expressions.insert(arg2.unwrap());
923                    }
924                    crate::MathFunction::CountLeadingZeros => {
925                        if let Some(crate::ScalarKind::Sint) = inner.scalar_kind() {
926                            self.need_bake_expressions.insert(arg);
927                        }
928                    }
929                    _ => {}
930                }
931            }
932        }
933
934        for statement in func.body.iter() {
935            match *statement {
936                crate::Statement::Atomic {
937                    fun: crate::AtomicFunction::Exchange { compare: Some(cmp) },
938                    ..
939                } => {
940                    self.need_bake_expressions.insert(cmp);
941                }
942                _ => {}
943            }
944        }
945    }
946
947    /// Helper method used to get a name for a global
948    ///
949    /// Globals have different naming schemes depending on their binding:
950    /// - Globals without bindings use the name from the [`Namer`](crate::proc::Namer)
951    /// - Globals with resource binding are named `_group_X_binding_Y` where `X`
952    ///   is the group and `Y` is the binding
953    fn get_global_name(
954        &self,
955        handle: Handle<crate::GlobalVariable>,
956        global: &crate::GlobalVariable,
957    ) -> String {
958        match (&global.binding, global.space) {
959            (&Some(ref br), _) => {
960                format!(
961                    "_group_{}_binding_{}_{}",
962                    br.group,
963                    br.binding,
964                    self.entry_point.stage.to_str()
965                )
966            }
967            (&None, crate::AddressSpace::Immediate) => {
968                format!("_immediates_binding_{}", self.entry_point.stage.to_str())
969            }
970            (&None, _) => self.names[&NameKey::GlobalVariable(handle)].clone(),
971        }
972    }
973
974    /// Helper method used to write a name for a global without additional heap allocation
975    fn write_global_name(
976        &mut self,
977        handle: Handle<crate::GlobalVariable>,
978        global: &crate::GlobalVariable,
979    ) -> BackendResult {
980        match (&global.binding, global.space) {
981            (&Some(ref br), _) => write!(
982                self.out,
983                "_group_{}_binding_{}_{}",
984                br.group,
985                br.binding,
986                self.entry_point.stage.to_str()
987            )?,
988            (&None, crate::AddressSpace::Immediate) => write!(
989                self.out,
990                "_immediates_binding_{}",
991                self.entry_point.stage.to_str()
992            )?,
993            (&None, _) => write!(
994                self.out,
995                "{}",
996                &self.names[&NameKey::GlobalVariable(handle)]
997            )?,
998        }
999
1000        Ok(())
1001    }
1002
1003    /// Write a GLSL global that will carry a Naga entry point's argument or return value.
1004    ///
1005    /// A Naga entry point's arguments and return value are rendered in GLSL as
1006    /// variables at global scope with the `in` and `out` storage qualifiers.
1007    /// The code we generate for `main` loads from all the `in` globals into
1008    /// appropriately named locals. Before it returns, `main` assigns the
1009    /// components of its return value into all the `out` globals.
1010    ///
1011    /// This function writes a declaration for one such GLSL global,
1012    /// representing a value passed into or returned from [`self.entry_point`]
1013    /// that has a [`Location`] binding. The global's name is generated based on
1014    /// the location index and the shader stages being connected; see
1015    /// [`VaryingName`]. This means we don't need to know the names of
1016    /// arguments, just their types and bindings.
1017    ///
1018    /// Emit nothing for entry point arguments or return values with [`BuiltIn`]
1019    /// bindings; `main` will read from or assign to the appropriate GLSL
1020    /// special variable; these are pre-declared. As an exception, we do declare
1021    /// `gl_Position` or `gl_FragCoord` with the `invariant` qualifier if
1022    /// needed.
1023    ///
1024    /// Use `output` together with [`self.entry_point.stage`] to determine which
1025    /// shader stages are being connected, and choose the `in` or `out` storage
1026    /// qualifier.
1027    ///
1028    /// [`self.entry_point`]: Writer::entry_point
1029    /// [`self.entry_point.stage`]: crate::EntryPoint::stage
1030    /// [`Location`]: crate::Binding::Location
1031    /// [`BuiltIn`]: crate::Binding::BuiltIn
1032    fn write_varying(
1033        &mut self,
1034        binding: Option<&crate::Binding>,
1035        ty: Handle<crate::Type>,
1036        output: bool,
1037    ) -> Result<(), Error> {
1038        // For a struct, emit a separate global for each member with a binding.
1039        if let TypeInner::Struct { ref members, .. } = self.module.types[ty].inner {
1040            for member in members {
1041                self.write_varying(member.binding.as_ref(), member.ty, output)?;
1042            }
1043            return Ok(());
1044        }
1045
1046        let binding = match binding {
1047            None => return Ok(()),
1048            Some(binding) => binding,
1049        };
1050
1051        let (location, interpolation, sampling, blend_src) = match *binding {
1052            crate::Binding::Location {
1053                location,
1054                interpolation,
1055                sampling,
1056                blend_src,
1057                per_primitive: _,
1058            } => (location, interpolation, sampling, blend_src),
1059            crate::Binding::BuiltIn(built_in) => {
1060                match built_in {
1061                    crate::BuiltIn::Position { invariant: true } => {
1062                        match (self.options.version, self.entry_point.stage) {
1063                            (
1064                                Version::Embedded {
1065                                    version: 300,
1066                                    is_webgl: true,
1067                                },
1068                                ShaderStage::Fragment,
1069                            ) => {
1070                                // `invariant gl_FragCoord` is not allowed in WebGL2 and possibly
1071                                // OpenGL ES in general (waiting on confirmation).
1072                                //
1073                                // See https://github.com/KhronosGroup/WebGL/issues/3518
1074                            }
1075                            _ => {
1076                                writeln!(
1077                                    self.out,
1078                                    "invariant {};",
1079                                    glsl_built_in(
1080                                        built_in,
1081                                        VaryingOptions::from_writer_options(self.options, output)
1082                                    )
1083                                )?;
1084                            }
1085                        }
1086                    }
1087                    crate::BuiltIn::ClipDistances => {
1088                        // Re-declare `gl_ClipDistance` with number of clip planes.
1089                        let TypeInner::Array { size, .. } = self.module.types[ty].inner else {
1090                            unreachable!();
1091                        };
1092                        let proc::IndexableLength::Known(size) =
1093                            size.resolve(self.module.to_ctx())?
1094                        else {
1095                            unreachable!();
1096                        };
1097                        self.clip_distance_count = size;
1098                        writeln!(self.out, "out float gl_ClipDistance[{size}];")?;
1099                    }
1100                    _ => {}
1101                }
1102                return Ok(());
1103            }
1104        };
1105
1106        // Write the interpolation modifier if needed
1107        //
1108        // We ignore all interpolation and auxiliary modifiers that aren't used in fragment
1109        // shaders' input globals or vertex shaders' output globals.
1110        let emit_interpolation_and_auxiliary = match self.entry_point.stage {
1111            ShaderStage::Vertex => output,
1112            ShaderStage::Fragment => !output,
1113            ShaderStage::Compute => false,
1114            ShaderStage::Task
1115            | ShaderStage::Mesh
1116            | ShaderStage::RayGeneration
1117            | ShaderStage::AnyHit
1118            | ShaderStage::ClosestHit
1119            | ShaderStage::Miss => unreachable!(),
1120        };
1121
1122        // Write the I/O locations, if allowed
1123        let io_location = if self.options.version.supports_explicit_locations()
1124            || !emit_interpolation_and_auxiliary
1125        {
1126            if self.options.version.supports_io_locations() {
1127                if let Some(blend_src) = blend_src {
1128                    write!(
1129                        self.out,
1130                        "layout(location = {location}, index = {blend_src}) "
1131                    )?;
1132                } else {
1133                    write!(self.out, "layout(location = {location}) ")?;
1134                }
1135                None
1136            } else {
1137                Some(VaryingLocation {
1138                    location,
1139                    index: blend_src.unwrap_or(0),
1140                })
1141            }
1142        } else {
1143            None
1144        };
1145
1146        // Write the interpolation qualifier.
1147        if let Some(interp) = interpolation {
1148            if emit_interpolation_and_auxiliary {
1149                write!(self.out, "{} ", glsl_interpolation(interp))?;
1150            }
1151        }
1152
1153        // Write the sampling auxiliary qualifier.
1154        //
1155        // Before GLSL 4.2, the `centroid` and `sample` qualifiers were required to appear
1156        // immediately before the `in` / `out` qualifier, so we'll just follow that rule
1157        // here, regardless of the version.
1158        if let Some(sampling) = sampling {
1159            if emit_interpolation_and_auxiliary {
1160                if let Some(qualifier) = glsl_sampling(sampling)? {
1161                    write!(self.out, "{qualifier} ")?;
1162                }
1163            }
1164        }
1165
1166        // Write the input/output qualifier.
1167        write!(self.out, "{} ", if output { "out" } else { "in" })?;
1168
1169        // Write the type
1170        // `write_type` adds no leading or trailing spaces
1171        self.write_type(ty)?;
1172
1173        // Finally write the global name and end the global with a `;` and a newline
1174        // Leading space is important
1175        let vname = VaryingName {
1176            binding: &crate::Binding::Location {
1177                location,
1178                interpolation: None,
1179                sampling: None,
1180                blend_src,
1181                per_primitive: false,
1182            },
1183            stage: self.entry_point.stage,
1184            options: VaryingOptions::from_writer_options(self.options, output),
1185        };
1186        writeln!(self.out, " {vname};")?;
1187
1188        if let Some(location) = io_location {
1189            self.varying.insert(vname.to_string(), location);
1190        }
1191
1192        Ok(())
1193    }
1194
1195    /// Helper method used to write functions (both entry points and regular functions)
1196    ///
1197    /// # Notes
1198    /// Adds a newline
1199    fn write_function(
1200        &mut self,
1201        ty: back::FunctionType,
1202        func: &crate::Function,
1203        info: &valid::FunctionInfo,
1204    ) -> BackendResult {
1205        // Create a function context for the function being written
1206        let ctx = back::FunctionCtx {
1207            ty,
1208            info,
1209            expressions: &func.expressions,
1210            named_expressions: &func.named_expressions,
1211        };
1212
1213        self.named_expressions.clear();
1214        self.update_expressions_to_bake(func, info);
1215
1216        // Write the function header
1217        //
1218        // glsl headers are the same as in c:
1219        // `ret_type name(args)`
1220        // `ret_type` is the return type
1221        // `name` is the function name
1222        // `args` is a comma separated list of `type name`
1223        //  | - `type` is the argument type
1224        //  | - `name` is the argument name
1225
1226        // Start by writing the return type if any otherwise write void
1227        // This is the only place where `void` is a valid type
1228        // (though it's more a keyword than a type)
1229        if let back::FunctionType::EntryPoint(_) = ctx.ty {
1230            write!(self.out, "void")?;
1231        } else if let Some(ref result) = func.result {
1232            self.write_type(result.ty)?;
1233            if let TypeInner::Array { base, size, .. } = self.module.types[result.ty].inner {
1234                self.write_array_size(base, size)?
1235            }
1236        } else {
1237            write!(self.out, "void")?;
1238        }
1239
1240        // Write the function name and open parentheses for the argument list
1241        let function_name = match ctx.ty {
1242            back::FunctionType::Function(handle) => &self.names[&NameKey::Function(handle)],
1243            back::FunctionType::EntryPoint(_) => "main",
1244        };
1245        write!(self.out, " {function_name}(")?;
1246
1247        // Write the comma separated argument list
1248        //
1249        // We need access to `Self` here so we use the reference passed to the closure as an
1250        // argument instead of capturing as that would cause a borrow checker error
1251        let arguments = match ctx.ty {
1252            back::FunctionType::EntryPoint(_) => &[][..],
1253            back::FunctionType::Function(_) => &func.arguments,
1254        };
1255        let arguments: Vec<_> = arguments
1256            .iter()
1257            .enumerate()
1258            .filter(|&(_, arg)| match self.module.types[arg.ty].inner {
1259                TypeInner::Sampler { .. } => false,
1260                _ => true,
1261            })
1262            .collect();
1263        self.write_slice(&arguments, |this, _, &(i, arg)| {
1264            // Write the argument type
1265            match this.module.types[arg.ty].inner {
1266                // We treat images separately because they might require
1267                // writing the storage format
1268                TypeInner::Image {
1269                    dim,
1270                    arrayed,
1271                    class,
1272                } => {
1273                    // Write the storage format if needed
1274                    if let TypeInner::Image {
1275                        class: crate::ImageClass::Storage { format, .. },
1276                        ..
1277                    } = this.module.types[arg.ty].inner
1278                    {
1279                        write!(this.out, "layout({}) ", glsl_storage_format(format)?)?;
1280                    }
1281
1282                    // write the type
1283                    //
1284                    // This is way we need the leading space because `write_image_type` doesn't add
1285                    // any spaces at the beginning or end
1286                    this.write_image_type(dim, arrayed, class)?;
1287                }
1288                TypeInner::Pointer { base, .. } => {
1289                    // write parameter qualifiers
1290                    write!(this.out, "inout ")?;
1291                    this.write_type(base)?;
1292                }
1293                // All other types are written by `write_type`
1294                _ => {
1295                    this.write_type(arg.ty)?;
1296                }
1297            }
1298
1299            // Write the argument name
1300            // The leading space is important
1301            write!(this.out, " {}", &this.names[&ctx.argument_key(i as u32)])?;
1302
1303            // Write array size
1304            match this.module.types[arg.ty].inner {
1305                TypeInner::Array { base, size, .. } => {
1306                    this.write_array_size(base, size)?;
1307                }
1308                TypeInner::Pointer { base, .. } => {
1309                    if let TypeInner::Array { base, size, .. } = this.module.types[base].inner {
1310                        this.write_array_size(base, size)?;
1311                    }
1312                }
1313                _ => {}
1314            }
1315
1316            Ok(())
1317        })?;
1318
1319        // Close the parentheses and open braces to start the function body
1320        writeln!(self.out, ") {{")?;
1321
1322        if self.options.zero_initialize_workgroup_memory
1323            && ctx.ty.is_compute_like_entry_point(self.module)
1324        {
1325            self.write_workgroup_variables_initialization(&ctx)?;
1326        }
1327
1328        // Compose the function arguments from globals, in case of an entry point.
1329        if let back::FunctionType::EntryPoint(ep_index) = ctx.ty {
1330            let stage = self.module.entry_points[ep_index as usize].stage;
1331            for (index, arg) in func.arguments.iter().enumerate() {
1332                write!(self.out, "{}", back::INDENT)?;
1333                self.write_type(arg.ty)?;
1334                let name = &self.names[&NameKey::EntryPointArgument(ep_index, index as u32)];
1335                write!(self.out, " {name}")?;
1336                write!(self.out, " = ")?;
1337                match self.module.types[arg.ty].inner {
1338                    TypeInner::Struct { ref members, .. } => {
1339                        self.write_type(arg.ty)?;
1340                        write!(self.out, "(")?;
1341                        for (index, member) in members.iter().enumerate() {
1342                            let varying_name = VaryingName {
1343                                binding: member.binding.as_ref().unwrap(),
1344                                stage,
1345                                options: VaryingOptions::from_writer_options(self.options, false),
1346                            };
1347                            if index != 0 {
1348                                write!(self.out, ", ")?;
1349                            }
1350                            write!(self.out, "{varying_name}")?;
1351                        }
1352                        writeln!(self.out, ");")?;
1353                    }
1354                    _ => {
1355                        let varying_name = VaryingName {
1356                            binding: arg.binding.as_ref().unwrap(),
1357                            stage,
1358                            options: VaryingOptions::from_writer_options(self.options, false),
1359                        };
1360                        writeln!(self.out, "{varying_name};")?;
1361                    }
1362                }
1363            }
1364        }
1365
1366        // Write all function locals
1367        // Locals are `type name (= init)?;` where the init part (including the =) are optional
1368        //
1369        // Always adds a newline
1370        for (handle, local) in func.local_variables.iter() {
1371            // Write indentation (only for readability) and the type
1372            // `write_type` adds no trailing space
1373            write!(self.out, "{}", back::INDENT)?;
1374            self.write_type(local.ty)?;
1375
1376            // Write the local name
1377            // The leading space is important
1378            write!(self.out, " {}", self.names[&ctx.name_key(handle)])?;
1379            // Write size for array type
1380            if let TypeInner::Array { base, size, .. } = self.module.types[local.ty].inner {
1381                self.write_array_size(base, size)?;
1382            }
1383            // Write the local initializer if needed
1384            if let Some(init) = local.init {
1385                // Put the equal signal only if there's a initializer
1386                // The leading and trailing spaces aren't needed but help with readability
1387                write!(self.out, " = ")?;
1388
1389                // Write the constant
1390                // `write_constant` adds no trailing or leading space/newline
1391                self.write_expr(init, &ctx)?;
1392            } else if is_value_init_supported(self.module, local.ty) {
1393                write!(self.out, " = ")?;
1394                self.write_zero_init_value(local.ty)?;
1395            }
1396
1397            // Finish the local with `;` and add a newline (only for readability)
1398            writeln!(self.out, ";")?
1399        }
1400
1401        // Write the function body (statement list)
1402        for sta in func.body.iter() {
1403            // Write a statement, the indentation should always be 1 when writing the function body
1404            // `write_stmt` adds a newline
1405            self.write_stmt(sta, &ctx, back::Level(1))?;
1406        }
1407
1408        // Close braces and add a newline
1409        writeln!(self.out, "}}")?;
1410
1411        Ok(())
1412    }
1413
1414    fn write_workgroup_variables_initialization(
1415        &mut self,
1416        ctx: &back::FunctionCtx,
1417    ) -> BackendResult {
1418        let mut vars = self
1419            .module
1420            .global_variables
1421            .iter()
1422            .filter(|&(handle, var)| {
1423                !ctx.info[handle].is_empty() && var.space == crate::AddressSpace::WorkGroup
1424            })
1425            .peekable();
1426
1427        if vars.peek().is_some() {
1428            let level = back::Level(1);
1429
1430            writeln!(self.out, "{level}if (gl_LocalInvocationID == uvec3(0u)) {{")?;
1431
1432            for (handle, var) in vars {
1433                let name = &self.names[&NameKey::GlobalVariable(handle)];
1434                write!(self.out, "{}{} = ", level.next(), name)?;
1435                self.write_zero_init_value(var.ty)?;
1436                writeln!(self.out, ";")?;
1437            }
1438
1439            writeln!(self.out, "{level}}}")?;
1440            self.write_control_barrier(crate::Barrier::WORK_GROUP, level)?;
1441        }
1442
1443        Ok(())
1444    }
1445
1446    /// Write a list of comma separated `T` values using a writer function `F`.
1447    ///
1448    /// The writer function `F` receives a mutable reference to `self` that if needed won't cause
1449    /// borrow checker issues (using for example a closure with `self` will cause issues), the
1450    /// second argument is the 0 based index of the element on the list, and the last element is
1451    /// a reference to the element `T` being written
1452    ///
1453    /// # Notes
1454    /// - Adds no newlines or leading/trailing whitespace
1455    /// - The last element won't have a trailing `,`
1456    fn write_slice<T, F: FnMut(&mut Self, u32, &T) -> BackendResult>(
1457        &mut self,
1458        data: &[T],
1459        mut f: F,
1460    ) -> BackendResult {
1461        // Loop through `data` invoking `f` for each element
1462        for (index, item) in data.iter().enumerate() {
1463            if index != 0 {
1464                write!(self.out, ", ")?;
1465            }
1466            f(self, index as u32, item)?;
1467        }
1468
1469        Ok(())
1470    }
1471
1472    /// Helper method used to write global constants
1473    fn write_global_constant(&mut self, handle: Handle<crate::Constant>) -> BackendResult {
1474        write!(self.out, "const ")?;
1475        let constant = &self.module.constants[handle];
1476        self.write_type(constant.ty)?;
1477        let name = &self.names[&NameKey::Constant(handle)];
1478        write!(self.out, " {name}")?;
1479        if let TypeInner::Array { base, size, .. } = self.module.types[constant.ty].inner {
1480            self.write_array_size(base, size)?;
1481        }
1482        write!(self.out, " = ")?;
1483        self.write_const_expr(constant.init, &self.module.global_expressions)?;
1484        writeln!(self.out, ";")?;
1485        Ok(())
1486    }
1487
1488    /// Helper method used to output a dot product as an arithmetic expression
1489    ///
1490    fn write_dot_product(
1491        &mut self,
1492        arg: Handle<crate::Expression>,
1493        arg1: Handle<crate::Expression>,
1494        size: usize,
1495        ctx: &back::FunctionCtx,
1496    ) -> BackendResult {
1497        // Write parentheses around the dot product expression to prevent operators
1498        // with different precedences from applying earlier.
1499        write!(self.out, "(")?;
1500
1501        // Cycle through all the components of the vector
1502        for index in 0..size {
1503            let component = back::COMPONENTS[index];
1504            // Write the addition to the previous product
1505            // This will print an extra '+' at the beginning but that is fine in glsl
1506            write!(self.out, " + ")?;
1507            // Write the first vector expression, this expression is marked to be
1508            // cached so unless it can't be cached (for example, it's a Constant)
1509            // it shouldn't produce large expressions.
1510            self.write_expr(arg, ctx)?;
1511            // Access the current component on the first vector
1512            write!(self.out, ".{component} * ")?;
1513            // Write the second vector expression, this expression is marked to be
1514            // cached so unless it can't be cached (for example, it's a Constant)
1515            // it shouldn't produce large expressions.
1516            self.write_expr(arg1, ctx)?;
1517            // Access the current component on the second vector
1518            write!(self.out, ".{component}")?;
1519        }
1520
1521        write!(self.out, ")")?;
1522        Ok(())
1523    }
1524
1525    /// Helper method used to write structs
1526    ///
1527    /// # Notes
1528    /// Ends in a newline
1529    fn write_struct_body(
1530        &mut self,
1531        handle: Handle<crate::Type>,
1532        members: &[crate::StructMember],
1533    ) -> BackendResult {
1534        // glsl structs are written as in C
1535        // `struct name() { members };`
1536        //  | `struct` is a keyword
1537        //  | `name` is the struct name
1538        //  | `members` is a semicolon separated list of `type name`
1539        //      | `type` is the member type
1540        //      | `name` is the member name
1541        writeln!(self.out, "{{")?;
1542
1543        for (idx, member) in members.iter().enumerate() {
1544            // The indentation is only for readability
1545            write!(self.out, "{}", back::INDENT)?;
1546
1547            match self.module.types[member.ty].inner {
1548                TypeInner::Array {
1549                    base,
1550                    size,
1551                    stride: _,
1552                } => {
1553                    self.write_type(base)?;
1554                    write!(
1555                        self.out,
1556                        " {}",
1557                        &self.names[&NameKey::StructMember(handle, idx as u32)]
1558                    )?;
1559                    // Write [size]
1560                    self.write_array_size(base, size)?;
1561                    // Newline is important
1562                    writeln!(self.out, ";")?;
1563                }
1564                _ => {
1565                    // Write the member type
1566                    // Adds no trailing space
1567                    self.write_type(member.ty)?;
1568
1569                    // Write the member name and put a semicolon
1570                    // The leading space is important
1571                    // All members must have a semicolon even the last one
1572                    writeln!(
1573                        self.out,
1574                        " {};",
1575                        &self.names[&NameKey::StructMember(handle, idx as u32)]
1576                    )?;
1577                }
1578            }
1579        }
1580
1581        write!(self.out, "}}")?;
1582        Ok(())
1583    }
1584
1585    /// Helper method used to write statements
1586    ///
1587    /// # Notes
1588    /// Always adds a newline
1589    fn write_stmt(
1590        &mut self,
1591        sta: &crate::Statement,
1592        ctx: &back::FunctionCtx,
1593        level: back::Level,
1594    ) -> BackendResult {
1595        use crate::Statement;
1596
1597        match *sta {
1598            // This is where we can generate intermediate constants for some expression types.
1599            Statement::Emit(ref range) => {
1600                for handle in range.clone() {
1601                    let ptr_class = ctx.resolve_type(handle, &self.module.types).pointer_space();
1602                    let expr_name = if ptr_class.is_some() {
1603                        // GLSL can't save a pointer-valued expression in a variable,
1604                        // but we shouldn't ever need to: they should never be named expressions,
1605                        // and none of the expression types flagged by bake_ref_count can be pointer-valued.
1606                        None
1607                    } else if let Some(name) = ctx.named_expressions.get(&handle) {
1608                        // Front end provides names for all variables at the start of writing.
1609                        // But we write them to step by step. We need to recache them
1610                        // Otherwise, we could accidentally write variable name instead of full expression.
1611                        // Also, we use sanitized names! It defense backend from generating variable with name from reserved keywords.
1612                        Some(self.namer.call(name))
1613                    } else if self.need_bake_expressions.contains(&handle) {
1614                        Some(Baked(handle).to_string())
1615                    } else {
1616                        None
1617                    };
1618
1619                    // If we are going to write an `ImageLoad` next and the target image
1620                    // is sampled and we are using the `Restrict` policy for bounds
1621                    // checking images we need to write a local holding the clamped lod.
1622                    if let crate::Expression::ImageLoad {
1623                        image,
1624                        level: Some(level_expr),
1625                        ..
1626                    } = ctx.expressions[handle]
1627                    {
1628                        if let TypeInner::Image {
1629                            class: crate::ImageClass::Sampled { .. },
1630                            ..
1631                        } = *ctx.resolve_type(image, &self.module.types)
1632                        {
1633                            if let proc::BoundsCheckPolicy::Restrict = self.policies.image_load {
1634                                write!(self.out, "{level}")?;
1635                                self.write_clamped_lod(ctx, handle, image, level_expr)?
1636                            }
1637                        }
1638                    }
1639
1640                    if let Some(name) = expr_name {
1641                        write!(self.out, "{level}")?;
1642                        self.write_named_expr(handle, name, handle, ctx)?;
1643                    }
1644                }
1645            }
1646            // Blocks are simple we just need to write the block statements between braces
1647            // We could also just print the statements but this is more readable and maps more
1648            // closely to the IR
1649            Statement::Block(ref block) => {
1650                write!(self.out, "{level}")?;
1651                writeln!(self.out, "{{")?;
1652                for sta in block.iter() {
1653                    // Increase the indentation to help with readability
1654                    self.write_stmt(sta, ctx, level.next())?
1655                }
1656                writeln!(self.out, "{level}}}")?
1657            }
1658            // Ifs are written as in C:
1659            // ```
1660            // if(condition) {
1661            //  accept
1662            // } else {
1663            //  reject
1664            // }
1665            // ```
1666            Statement::If {
1667                condition,
1668                ref accept,
1669                ref reject,
1670            } => {
1671                write!(self.out, "{level}")?;
1672                write!(self.out, "if (")?;
1673                self.write_expr(condition, ctx)?;
1674                writeln!(self.out, ") {{")?;
1675
1676                for sta in accept {
1677                    // Increase indentation to help with readability
1678                    self.write_stmt(sta, ctx, level.next())?;
1679                }
1680
1681                // If there are no statements in the reject block we skip writing it
1682                // This is only for readability
1683                if !reject.is_empty() {
1684                    writeln!(self.out, "{level}}} else {{")?;
1685
1686                    for sta in reject {
1687                        // Increase indentation to help with readability
1688                        self.write_stmt(sta, ctx, level.next())?;
1689                    }
1690                }
1691
1692                writeln!(self.out, "{level}}}")?
1693            }
1694            // Switch are written as in C:
1695            // ```
1696            // switch (selector) {
1697            //      // Fallthrough
1698            //      case label:
1699            //          block
1700            //      // Non fallthrough
1701            //      case label:
1702            //          block
1703            //          break;
1704            //      default:
1705            //          block
1706            //  }
1707            //  ```
1708            //  Where the `default` case happens isn't important but we put it last
1709            //  so that we don't need to print a `break` for it
1710            Statement::Switch {
1711                selector,
1712                ref cases,
1713            } => {
1714                let l2 = level.next();
1715                // Some GLSL consumers may not handle switches with a single
1716                // body correctly: See wgpu#4514. Write such switch statements
1717                // as a `do {} while(false);` loop instead.
1718                //
1719                // Since doing so may inadvertently capture `continue`
1720                // statements in the switch body, we must apply continue
1721                // forwarding. See the `naga::back::continue_forward` module
1722                // docs for details.
1723                let one_body = cases
1724                    .iter()
1725                    .rev()
1726                    .skip(1)
1727                    .all(|case| case.fall_through && case.body.is_empty());
1728                if one_body {
1729                    // Unlike HLSL, in GLSL `continue_ctx` only needs to know
1730                    // about [`Switch`] statements that are being rendered as
1731                    // `do-while` loops.
1732                    if let Some(variable) = self.continue_ctx.enter_switch(&mut self.namer) {
1733                        writeln!(self.out, "{level}bool {variable} = false;",)?;
1734                    };
1735                    writeln!(self.out, "{level}do {{")?;
1736                    // Note: Expressions have no side-effects so we don't need to emit selector expression.
1737
1738                    // Body
1739                    if let Some(case) = cases.last() {
1740                        for sta in case.body.iter() {
1741                            self.write_stmt(sta, ctx, l2)?;
1742                        }
1743                    }
1744                    // End do-while
1745                    writeln!(self.out, "{level}}} while(false);")?;
1746
1747                    // Handle any forwarded continue statements.
1748                    use back::continue_forward::ExitControlFlow;
1749                    let op = match self.continue_ctx.exit_switch() {
1750                        ExitControlFlow::None => None,
1751                        ExitControlFlow::Continue { variable } => Some(("continue", variable)),
1752                        ExitControlFlow::Break { variable } => Some(("break", variable)),
1753                    };
1754                    if let Some((control_flow, variable)) = op {
1755                        writeln!(self.out, "{level}if ({variable}) {{")?;
1756                        writeln!(self.out, "{l2}{control_flow};")?;
1757                        writeln!(self.out, "{level}}}")?;
1758                    }
1759                } else {
1760                    // Start the switch
1761                    write!(self.out, "{level}")?;
1762                    write!(self.out, "switch(")?;
1763                    self.write_expr(selector, ctx)?;
1764                    writeln!(self.out, ") {{")?;
1765
1766                    // Write all cases
1767                    for case in cases {
1768                        match case.value {
1769                            crate::SwitchValue::I32(value) => {
1770                                write!(self.out, "{l2}case {value}:")?
1771                            }
1772                            crate::SwitchValue::U32(value) => {
1773                                write!(self.out, "{l2}case {value}u:")?
1774                            }
1775                            crate::SwitchValue::Default => write!(self.out, "{l2}default:")?,
1776                        }
1777
1778                        let write_block_braces = !(case.fall_through && case.body.is_empty());
1779                        if write_block_braces {
1780                            writeln!(self.out, " {{")?;
1781                        } else {
1782                            writeln!(self.out)?;
1783                        }
1784
1785                        for sta in case.body.iter() {
1786                            self.write_stmt(sta, ctx, l2.next())?;
1787                        }
1788
1789                        if !case.fall_through && case.body.last().is_none_or(|s| !s.is_terminator())
1790                        {
1791                            writeln!(self.out, "{}break;", l2.next())?;
1792                        }
1793
1794                        if write_block_braces {
1795                            writeln!(self.out, "{l2}}}")?;
1796                        }
1797                    }
1798
1799                    writeln!(self.out, "{level}}}")?
1800                }
1801            }
1802            // Loops in naga IR are based on wgsl loops, glsl can emulate the behaviour by using a
1803            // while true loop and appending the continuing block to the body resulting on:
1804            // ```
1805            // bool loop_init = true;
1806            // while(true) {
1807            //  if (!loop_init) { <continuing> }
1808            //  loop_init = false;
1809            //  <body>
1810            // }
1811            // ```
1812            Statement::Loop {
1813                ref body,
1814                ref continuing,
1815                break_if,
1816            } => {
1817                self.continue_ctx.enter_loop();
1818                if !continuing.is_empty() || break_if.is_some() {
1819                    let gate_name = self.namer.call("loop_init");
1820                    writeln!(self.out, "{level}bool {gate_name} = true;")?;
1821                    writeln!(self.out, "{level}while(true) {{")?;
1822                    let l2 = level.next();
1823                    let l3 = l2.next();
1824                    writeln!(self.out, "{l2}if (!{gate_name}) {{")?;
1825                    for sta in continuing {
1826                        self.write_stmt(sta, ctx, l3)?;
1827                    }
1828                    if let Some(condition) = break_if {
1829                        write!(self.out, "{l3}if (")?;
1830                        self.write_expr(condition, ctx)?;
1831                        writeln!(self.out, ") {{")?;
1832                        writeln!(self.out, "{}break;", l3.next())?;
1833                        writeln!(self.out, "{l3}}}")?;
1834                    }
1835                    writeln!(self.out, "{l2}}}")?;
1836                    writeln!(self.out, "{}{} = false;", level.next(), gate_name)?;
1837                } else {
1838                    writeln!(self.out, "{level}while(true) {{")?;
1839                }
1840                for sta in body {
1841                    self.write_stmt(sta, ctx, level.next())?;
1842                }
1843                writeln!(self.out, "{level}}}")?;
1844                self.continue_ctx.exit_loop();
1845            }
1846            // Break, continue and return as written as in C
1847            // `break;`
1848            Statement::Break => {
1849                write!(self.out, "{level}")?;
1850                writeln!(self.out, "break;")?
1851            }
1852            // `continue;`
1853            Statement::Continue => {
1854                // Sometimes we must render a `Continue` statement as a `break`.
1855                // See the docs for the `back::continue_forward` module.
1856                if let Some(variable) = self.continue_ctx.continue_encountered() {
1857                    writeln!(self.out, "{level}{variable} = true;",)?;
1858                    writeln!(self.out, "{level}break;")?
1859                } else {
1860                    writeln!(self.out, "{level}continue;")?
1861                }
1862            }
1863            // `return expr;`, `expr` is optional
1864            Statement::Return { value } => {
1865                write!(self.out, "{level}")?;
1866                match ctx.ty {
1867                    back::FunctionType::Function(_) => {
1868                        write!(self.out, "return")?;
1869                        // Write the expression to be returned if needed
1870                        if let Some(expr) = value {
1871                            write!(self.out, " ")?;
1872                            self.write_expr(expr, ctx)?;
1873                        }
1874                        writeln!(self.out, ";")?;
1875                    }
1876                    back::FunctionType::EntryPoint(ep_index) => {
1877                        let mut has_point_size = false;
1878                        let ep = &self.module.entry_points[ep_index as usize];
1879                        if let Some(ref result) = ep.function.result {
1880                            let value = value.unwrap();
1881                            match self.module.types[result.ty].inner {
1882                                TypeInner::Struct { ref members, .. } => {
1883                                    let temp_struct_name = match ctx.expressions[value] {
1884                                        crate::Expression::Compose { .. } => {
1885                                            let return_struct = "_tmp_return";
1886                                            write!(
1887                                                self.out,
1888                                                "{} {} = ",
1889                                                &self.names[&NameKey::Type(result.ty)],
1890                                                return_struct
1891                                            )?;
1892                                            self.write_expr(value, ctx)?;
1893                                            writeln!(self.out, ";")?;
1894                                            write!(self.out, "{level}")?;
1895                                            Some(return_struct)
1896                                        }
1897                                        _ => None,
1898                                    };
1899
1900                                    for (index, member) in members.iter().enumerate() {
1901                                        if let Some(crate::Binding::BuiltIn(
1902                                            crate::BuiltIn::PointSize,
1903                                        )) = member.binding
1904                                        {
1905                                            has_point_size = true;
1906                                        }
1907
1908                                        let varying_name = VaryingName {
1909                                            binding: member.binding.as_ref().unwrap(),
1910                                            stage: ep.stage,
1911                                            options: VaryingOptions::from_writer_options(
1912                                                self.options,
1913                                                true,
1914                                            ),
1915                                        };
1916                                        write!(self.out, "{varying_name} = ")?;
1917
1918                                        if let Some(struct_name) = temp_struct_name {
1919                                            write!(self.out, "{struct_name}")?;
1920                                        } else {
1921                                            self.write_expr(value, ctx)?;
1922                                        }
1923
1924                                        // Write field name
1925                                        writeln!(
1926                                            self.out,
1927                                            ".{};",
1928                                            &self.names
1929                                                [&NameKey::StructMember(result.ty, index as u32)]
1930                                        )?;
1931                                        write!(self.out, "{level}")?;
1932                                    }
1933                                }
1934                                _ => {
1935                                    let name = VaryingName {
1936                                        binding: result.binding.as_ref().unwrap(),
1937                                        stage: ep.stage,
1938                                        options: VaryingOptions::from_writer_options(
1939                                            self.options,
1940                                            true,
1941                                        ),
1942                                    };
1943                                    write!(self.out, "{name} = ")?;
1944                                    self.write_expr(value, ctx)?;
1945                                    writeln!(self.out, ";")?;
1946                                    write!(self.out, "{level}")?;
1947                                }
1948                            }
1949                        }
1950
1951                        let is_vertex_stage = self.module.entry_points[ep_index as usize].stage
1952                            == ShaderStage::Vertex;
1953                        if is_vertex_stage
1954                            && self
1955                                .options
1956                                .writer_flags
1957                                .contains(WriterFlags::ADJUST_COORDINATE_SPACE)
1958                        {
1959                            writeln!(
1960                                self.out,
1961                                "gl_Position.yz = vec2(-gl_Position.y, gl_Position.z * 2.0 - gl_Position.w);",
1962                            )?;
1963                            write!(self.out, "{level}")?;
1964                        }
1965
1966                        if is_vertex_stage
1967                            && self
1968                                .options
1969                                .writer_flags
1970                                .contains(WriterFlags::FORCE_POINT_SIZE)
1971                            && !has_point_size
1972                        {
1973                            writeln!(self.out, "gl_PointSize = 1.0;")?;
1974                            write!(self.out, "{level}")?;
1975                        }
1976                        writeln!(self.out, "return;")?;
1977                    }
1978                }
1979            }
1980            // This is one of the places were glsl adds to the syntax of C in this case the discard
1981            // keyword which ceases all further processing in a fragment shader, it's called OpKill
1982            // in spir-v that's why it's called `Statement::Kill`
1983            Statement::Kill => writeln!(self.out, "{level}discard;")?,
1984            Statement::ControlBarrier(flags) => {
1985                self.write_control_barrier(flags, level)?;
1986            }
1987            Statement::MemoryBarrier(flags) => {
1988                self.write_memory_barrier(flags, level)?;
1989            }
1990            // Stores in glsl are just variable assignments written as `pointer = value;`
1991            Statement::Store { pointer, value } => {
1992                write!(self.out, "{level}")?;
1993                let is_atomic_pointer = ctx
1994                    .resolve_type(pointer, &self.module.types)
1995                    .is_atomic_pointer(&self.module.types);
1996                if is_atomic_pointer {
1997                    write!(self.out, "atomicExchange(")?;
1998                    self.write_expr(pointer, ctx)?;
1999                    write!(self.out, ", ")?;
2000                    self.write_expr(value, ctx)?;
2001                    writeln!(self.out, ");")?
2002                } else {
2003                    self.write_expr(pointer, ctx)?;
2004                    write!(self.out, " = ")?;
2005                    self.write_expr(value, ctx)?;
2006                    writeln!(self.out, ";")?
2007                }
2008            }
2009            Statement::WorkGroupUniformLoad { pointer, result } => {
2010                // GLSL doesn't have pointers, which means that this backend needs to ensure that
2011                // the actual "loading" is happening between the two barriers.
2012                // This is done in `Emit` by never emitting a variable name for pointer variables
2013                self.write_control_barrier(crate::Barrier::WORK_GROUP, level)?;
2014
2015                let result_name = Baked(result).to_string();
2016                write!(self.out, "{level}")?;
2017                // Expressions cannot have side effects, so just writing the expression here is fine.
2018                self.write_named_expr(pointer, result_name, result, ctx)?;
2019
2020                self.write_control_barrier(crate::Barrier::WORK_GROUP, level)?;
2021            }
2022            // Stores a value into an image.
2023            Statement::ImageStore {
2024                image,
2025                coordinate,
2026                array_index,
2027                value,
2028            } => {
2029                write!(self.out, "{level}")?;
2030                self.write_image_store(ctx, image, coordinate, array_index, value)?
2031            }
2032            // A `Call` is written `name(arguments)` where `arguments` is a comma separated expressions list
2033            Statement::Call {
2034                function,
2035                ref arguments,
2036                result,
2037            } => {
2038                write!(self.out, "{level}")?;
2039                if let Some(expr) = result {
2040                    let name = Baked(expr).to_string();
2041                    let result = self.module.functions[function].result.as_ref().unwrap();
2042                    self.write_type(result.ty)?;
2043                    write!(self.out, " {name}")?;
2044                    if let TypeInner::Array { base, size, .. } = self.module.types[result.ty].inner
2045                    {
2046                        self.write_array_size(base, size)?
2047                    }
2048                    write!(self.out, " = ")?;
2049                    self.named_expressions.insert(expr, name);
2050                }
2051                write!(self.out, "{}(", &self.names[&NameKey::Function(function)])?;
2052                let arguments: Vec<_> = arguments
2053                    .iter()
2054                    .enumerate()
2055                    .filter_map(|(i, arg)| {
2056                        let arg_ty = self.module.functions[function].arguments[i].ty;
2057                        match self.module.types[arg_ty].inner {
2058                            TypeInner::Sampler { .. } => None,
2059                            _ => Some(*arg),
2060                        }
2061                    })
2062                    .collect();
2063                self.write_slice(&arguments, |this, _, arg| this.write_expr(*arg, ctx))?;
2064                writeln!(self.out, ");")?
2065            }
2066            Statement::Atomic {
2067                pointer,
2068                ref fun,
2069                value,
2070                result,
2071            } => {
2072                write!(self.out, "{level}")?;
2073
2074                match *fun {
2075                    crate::AtomicFunction::Exchange {
2076                        compare: Some(compare_expr),
2077                    } => {
2078                        let result_handle = result.expect("CompareExchange must have a result");
2079                        let res_name = Baked(result_handle).to_string();
2080                        self.write_type(ctx.info[result_handle].ty.handle().unwrap())?;
2081                        write!(self.out, " {res_name};")?;
2082                        write!(self.out, " {res_name}.old_value = atomicCompSwap(")?;
2083                        self.write_expr(pointer, ctx)?;
2084                        write!(self.out, ", ")?;
2085                        self.write_expr(compare_expr, ctx)?;
2086                        write!(self.out, ", ")?;
2087                        self.write_expr(value, ctx)?;
2088                        writeln!(self.out, ");")?;
2089
2090                        write!(
2091                            self.out,
2092                            "{level}{res_name}.exchanged = ({res_name}.old_value == "
2093                        )?;
2094                        self.write_expr(compare_expr, ctx)?;
2095                        writeln!(self.out, ");")?;
2096                        self.named_expressions.insert(result_handle, res_name);
2097                    }
2098                    _ => {
2099                        if let Some(result) = result {
2100                            let res_name = Baked(result).to_string();
2101                            self.write_type(ctx.info[result].ty.handle().unwrap())?;
2102                            write!(self.out, " {res_name} = ")?;
2103                            self.named_expressions.insert(result, res_name);
2104                        }
2105                        let fun_str = fun.to_glsl();
2106                        write!(self.out, "atomic{fun_str}(")?;
2107                        self.write_expr(pointer, ctx)?;
2108                        write!(self.out, ", ")?;
2109                        if let crate::AtomicFunction::Subtract = *fun {
2110                            // Emulate `atomicSub` with `atomicAdd` by negating the value.
2111                            write!(self.out, "-")?;
2112                        }
2113                        self.write_expr(value, ctx)?;
2114                        writeln!(self.out, ");")?;
2115                    }
2116                }
2117            }
2118            // Stores a value into an image.
2119            Statement::ImageAtomic {
2120                image,
2121                coordinate,
2122                array_index,
2123                fun,
2124                value,
2125            } => {
2126                write!(self.out, "{level}")?;
2127                self.write_image_atomic(ctx, image, coordinate, array_index, fun, value)?
2128            }
2129            Statement::RayQuery { .. } => unreachable!(),
2130            Statement::SubgroupBallot { result, predicate } => {
2131                write!(self.out, "{level}")?;
2132                let res_name = Baked(result).to_string();
2133                let res_ty = ctx.info[result].ty.inner_with(&self.module.types);
2134                self.write_value_type(res_ty)?;
2135                write!(self.out, " {res_name} = ")?;
2136                self.named_expressions.insert(result, res_name);
2137
2138                write!(self.out, "subgroupBallot(")?;
2139                match predicate {
2140                    Some(predicate) => self.write_expr(predicate, ctx)?,
2141                    None => write!(self.out, "true")?,
2142                }
2143                writeln!(self.out, ");")?;
2144            }
2145            Statement::SubgroupCollectiveOperation {
2146                op,
2147                collective_op,
2148                argument,
2149                result,
2150            } => {
2151                write!(self.out, "{level}")?;
2152                let res_name = Baked(result).to_string();
2153                let res_ty = ctx.info[result].ty.inner_with(&self.module.types);
2154                self.write_value_type(res_ty)?;
2155                write!(self.out, " {res_name} = ")?;
2156                self.named_expressions.insert(result, res_name);
2157
2158                match (collective_op, op) {
2159                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::All) => {
2160                        write!(self.out, "subgroupAll(")?
2161                    }
2162                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Any) => {
2163                        write!(self.out, "subgroupAny(")?
2164                    }
2165                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Add) => {
2166                        write!(self.out, "subgroupAdd(")?
2167                    }
2168                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Mul) => {
2169                        write!(self.out, "subgroupMul(")?
2170                    }
2171                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Max) => {
2172                        write!(self.out, "subgroupMax(")?
2173                    }
2174                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Min) => {
2175                        write!(self.out, "subgroupMin(")?
2176                    }
2177                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::And) => {
2178                        write!(self.out, "subgroupAnd(")?
2179                    }
2180                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Or) => {
2181                        write!(self.out, "subgroupOr(")?
2182                    }
2183                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Xor) => {
2184                        write!(self.out, "subgroupXor(")?
2185                    }
2186                    (crate::CollectiveOperation::ExclusiveScan, crate::SubgroupOperation::Add) => {
2187                        write!(self.out, "subgroupExclusiveAdd(")?
2188                    }
2189                    (crate::CollectiveOperation::ExclusiveScan, crate::SubgroupOperation::Mul) => {
2190                        write!(self.out, "subgroupExclusiveMul(")?
2191                    }
2192                    (crate::CollectiveOperation::InclusiveScan, crate::SubgroupOperation::Add) => {
2193                        write!(self.out, "subgroupInclusiveAdd(")?
2194                    }
2195                    (crate::CollectiveOperation::InclusiveScan, crate::SubgroupOperation::Mul) => {
2196                        write!(self.out, "subgroupInclusiveMul(")?
2197                    }
2198                    _ => unimplemented!(),
2199                }
2200                self.write_expr(argument, ctx)?;
2201                writeln!(self.out, ");")?;
2202            }
2203            Statement::SubgroupGather {
2204                mode,
2205                argument,
2206                result,
2207            } => {
2208                write!(self.out, "{level}")?;
2209                let res_name = Baked(result).to_string();
2210                let res_ty = ctx.info[result].ty.inner_with(&self.module.types);
2211                self.write_value_type(res_ty)?;
2212                write!(self.out, " {res_name} = ")?;
2213                self.named_expressions.insert(result, res_name);
2214
2215                match mode {
2216                    crate::GatherMode::BroadcastFirst => {
2217                        write!(self.out, "subgroupBroadcastFirst(")?;
2218                    }
2219                    crate::GatherMode::Broadcast(_) => {
2220                        write!(self.out, "subgroupBroadcast(")?;
2221                    }
2222                    crate::GatherMode::Shuffle(_) => {
2223                        write!(self.out, "subgroupShuffle(")?;
2224                    }
2225                    crate::GatherMode::ShuffleDown(_) => {
2226                        write!(self.out, "subgroupShuffleDown(")?;
2227                    }
2228                    crate::GatherMode::ShuffleUp(_) => {
2229                        write!(self.out, "subgroupShuffleUp(")?;
2230                    }
2231                    crate::GatherMode::ShuffleXor(_) => {
2232                        write!(self.out, "subgroupShuffleXor(")?;
2233                    }
2234                    crate::GatherMode::QuadBroadcast(_) => {
2235                        write!(self.out, "subgroupQuadBroadcast(")?;
2236                    }
2237                    crate::GatherMode::QuadSwap(direction) => match direction {
2238                        crate::Direction::X => {
2239                            write!(self.out, "subgroupQuadSwapHorizontal(")?;
2240                        }
2241                        crate::Direction::Y => {
2242                            write!(self.out, "subgroupQuadSwapVertical(")?;
2243                        }
2244                        crate::Direction::Diagonal => {
2245                            write!(self.out, "subgroupQuadSwapDiagonal(")?;
2246                        }
2247                    },
2248                }
2249                self.write_expr(argument, ctx)?;
2250                match mode {
2251                    crate::GatherMode::BroadcastFirst => {}
2252                    crate::GatherMode::Broadcast(index)
2253                    | crate::GatherMode::Shuffle(index)
2254                    | crate::GatherMode::ShuffleDown(index)
2255                    | crate::GatherMode::ShuffleUp(index)
2256                    | crate::GatherMode::ShuffleXor(index)
2257                    | crate::GatherMode::QuadBroadcast(index) => {
2258                        write!(self.out, ", ")?;
2259                        self.write_expr(index, ctx)?;
2260                    }
2261                    crate::GatherMode::QuadSwap(_) => {}
2262                }
2263                writeln!(self.out, ");")?;
2264            }
2265            Statement::CooperativeStore { .. } => unimplemented!(),
2266            Statement::RayPipelineFunction(_) => unimplemented!(),
2267        }
2268
2269        Ok(())
2270    }
2271
2272    /// Write a const expression.
2273    ///
2274    /// Write `expr`, a handle to an [`Expression`] in the current [`Module`]'s
2275    /// constant expression arena, as GLSL expression.
2276    ///
2277    /// # Notes
2278    /// Adds no newlines or leading/trailing whitespace
2279    ///
2280    /// [`Expression`]: crate::Expression
2281    /// [`Module`]: crate::Module
2282    fn write_const_expr(
2283        &mut self,
2284        expr: Handle<crate::Expression>,
2285        arena: &crate::Arena<crate::Expression>,
2286    ) -> BackendResult {
2287        self.write_possibly_const_expr(
2288            expr,
2289            arena,
2290            |expr| &self.info[expr],
2291            |writer, expr| writer.write_const_expr(expr, arena),
2292        )
2293    }
2294
2295    /// Write [`Expression`] variants that can occur in both runtime and const expressions.
2296    ///
2297    /// Write `expr`, a handle to an [`Expression`] in the arena `expressions`,
2298    /// as as GLSL expression. This must be one of the [`Expression`] variants
2299    /// that is allowed to occur in constant expressions.
2300    ///
2301    /// Use `write_expression` to write subexpressions.
2302    ///
2303    /// This is the common code for `write_expr`, which handles arbitrary
2304    /// runtime expressions, and `write_const_expr`, which only handles
2305    /// const-expressions. Each of those callers passes itself (essentially) as
2306    /// the `write_expression` callback, so that subexpressions are restricted
2307    /// to the appropriate variants.
2308    ///
2309    /// # Notes
2310    /// Adds no newlines or leading/trailing whitespace
2311    ///
2312    /// [`Expression`]: crate::Expression
2313    fn write_possibly_const_expr<'w, I, E>(
2314        &'w mut self,
2315        expr: Handle<crate::Expression>,
2316        expressions: &crate::Arena<crate::Expression>,
2317        info: I,
2318        write_expression: E,
2319    ) -> BackendResult
2320    where
2321        I: Fn(Handle<crate::Expression>) -> &'w proc::TypeResolution,
2322        E: Fn(&mut Self, Handle<crate::Expression>) -> BackendResult,
2323    {
2324        use crate::Expression;
2325
2326        match expressions[expr] {
2327            Expression::Literal(literal) => {
2328                match literal {
2329                    // Floats are written using `Debug` instead of `Display` because it always appends the
2330                    // decimal part even it's zero which is needed for a valid glsl float constant
2331                    crate::Literal::F64(value) => write!(self.out, "{value:?}LF")?,
2332                    crate::Literal::F32(value) => write!(self.out, "{value:?}")?,
2333                    crate::Literal::F16(_) => {
2334                        return Err(Error::Custom("GLSL has no 16-bit float type".into()));
2335                    }
2336                    // Unsigned integers need a `u` at the end
2337                    //
2338                    // While `core` doesn't necessarily need it, it's allowed and since `es` needs it we
2339                    // always write it as the extra branch wouldn't have any benefit in readability
2340                    crate::Literal::U32(value) => write!(self.out, "{value}u")?,
2341                    crate::Literal::I32(value) => write!(self.out, "{value}")?,
2342                    crate::Literal::Bool(value) => write!(self.out, "{value}")?,
2343                    crate::Literal::I64(_) => {
2344                        return Err(Error::Custom("GLSL has no 64-bit integer type".into()));
2345                    }
2346                    crate::Literal::U64(_) => {
2347                        return Err(Error::Custom("GLSL has no 64-bit integer type".into()));
2348                    }
2349                    crate::Literal::AbstractInt(_) | crate::Literal::AbstractFloat(_) => {
2350                        return Err(Error::Custom(
2351                            "Abstract types should not appear in IR presented to backends".into(),
2352                        ));
2353                    }
2354                }
2355            }
2356            Expression::Constant(handle) => {
2357                let constant = &self.module.constants[handle];
2358                if constant.name.is_some() {
2359                    write!(self.out, "{}", self.names[&NameKey::Constant(handle)])?;
2360                } else {
2361                    self.write_const_expr(constant.init, &self.module.global_expressions)?;
2362                }
2363            }
2364            Expression::ZeroValue(ty) => {
2365                self.write_zero_init_value(ty)?;
2366            }
2367            Expression::Compose { ty, ref components } => {
2368                self.write_type(ty)?;
2369
2370                if let TypeInner::Array { base, size, .. } = self.module.types[ty].inner {
2371                    self.write_array_size(base, size)?;
2372                }
2373
2374                write!(self.out, "(")?;
2375                for (index, component) in components.iter().enumerate() {
2376                    if index != 0 {
2377                        write!(self.out, ", ")?;
2378                    }
2379                    write_expression(self, *component)?;
2380                }
2381                write!(self.out, ")")?
2382            }
2383            // `Splat` needs to actually write down a vector, it's not always inferred in GLSL.
2384            Expression::Splat { size: _, value } => {
2385                let resolved = info(expr).inner_with(&self.module.types);
2386                self.write_value_type(resolved)?;
2387                write!(self.out, "(")?;
2388                write_expression(self, value)?;
2389                write!(self.out, ")")?
2390            }
2391            _ => {
2392                return Err(Error::Override);
2393            }
2394        }
2395
2396        Ok(())
2397    }
2398
2399    /// Helper method to write expressions
2400    ///
2401    /// # Notes
2402    /// Doesn't add any newlines or leading/trailing spaces
2403    fn write_expr(
2404        &mut self,
2405        expr: Handle<crate::Expression>,
2406        ctx: &back::FunctionCtx,
2407    ) -> BackendResult {
2408        use crate::Expression;
2409
2410        if let Some(name) = self.named_expressions.get(&expr) {
2411            write!(self.out, "{name}")?;
2412            return Ok(());
2413        }
2414
2415        match ctx.expressions[expr] {
2416            Expression::Literal(_)
2417            | Expression::Constant(_)
2418            | Expression::ZeroValue(_)
2419            | Expression::Compose { .. }
2420            | Expression::Splat { .. } => {
2421                self.write_possibly_const_expr(
2422                    expr,
2423                    ctx.expressions,
2424                    |expr| &ctx.info[expr].ty,
2425                    |writer, expr| writer.write_expr(expr, ctx),
2426                )?;
2427            }
2428            Expression::Override(_) => return Err(Error::Override),
2429            // `Access` is applied to arrays, vectors and matrices and is written as indexing
2430            Expression::Access { base, index } => {
2431                self.write_expr(base, ctx)?;
2432                write!(self.out, "[")?;
2433                self.write_expr(index, ctx)?;
2434                write!(self.out, "]")?
2435            }
2436            // `AccessIndex` is the same as `Access` except that the index is a constant and it can
2437            // be applied to structs, in this case we need to find the name of the field at that
2438            // index and write `base.field_name`
2439            Expression::AccessIndex { base, index } => {
2440                self.write_expr(base, ctx)?;
2441
2442                let base_ty_res = &ctx.info[base].ty;
2443                let mut resolved = base_ty_res.inner_with(&self.module.types);
2444                let base_ty_handle = match *resolved {
2445                    TypeInner::Pointer { base, space: _ } => {
2446                        resolved = &self.module.types[base].inner;
2447                        Some(base)
2448                    }
2449                    _ => base_ty_res.handle(),
2450                };
2451
2452                match *resolved {
2453                    TypeInner::Vector { .. } => {
2454                        // Write vector access as a swizzle
2455                        write!(self.out, ".{}", back::COMPONENTS[index as usize])?
2456                    }
2457                    TypeInner::Matrix { .. }
2458                    | TypeInner::Array { .. }
2459                    | TypeInner::ValuePointer { .. } => write!(self.out, "[{index}]")?,
2460                    TypeInner::Struct { .. } => {
2461                        // This will never panic in case the type is a `Struct`, this is not true
2462                        // for other types so we can only check while inside this match arm
2463                        let ty = base_ty_handle.unwrap();
2464
2465                        write!(
2466                            self.out,
2467                            ".{}",
2468                            &self.names[&NameKey::StructMember(ty, index)]
2469                        )?
2470                    }
2471                    ref other => return Err(Error::Custom(format!("Cannot index {other:?}"))),
2472                }
2473            }
2474            // `Swizzle` adds a few letters behind the dot.
2475            Expression::Swizzle {
2476                size,
2477                vector,
2478                pattern,
2479            } => {
2480                self.write_expr(vector, ctx)?;
2481                write!(self.out, ".")?;
2482                for &sc in pattern[..size as usize].iter() {
2483                    self.out.write_char(back::COMPONENTS[sc as usize])?;
2484                }
2485            }
2486            // Function arguments are written as the argument name
2487            Expression::FunctionArgument(pos) => {
2488                write!(self.out, "{}", &self.names[&ctx.argument_key(pos)])?
2489            }
2490            // Global variables need some special work for their name but
2491            // `get_global_name` does the work for us
2492            Expression::GlobalVariable(handle) => {
2493                let global = &self.module.global_variables[handle];
2494                self.write_global_name(handle, global)?
2495            }
2496            // A local is written as it's name
2497            Expression::LocalVariable(handle) => {
2498                write!(self.out, "{}", self.names[&ctx.name_key(handle)])?
2499            }
2500            // glsl has no pointers so there's no load operation, just write the pointer expression
2501            Expression::Load { pointer } => {
2502                let ty_inner = ctx.resolve_type(pointer, &self.module.types);
2503                if ty_inner.is_atomic_pointer(&self.module.types) {
2504                    let mut suffix = "";
2505                    if let TypeInner::Pointer { base, .. } = *ty_inner {
2506                        if let TypeInner::Atomic(scalar) = self.module.types[base].inner {
2507                            suffix = match (scalar.kind, scalar.width) {
2508                                (crate::ScalarKind::Uint, 8) => "ul",
2509                                (crate::ScalarKind::Sint, 8) => "l",
2510                                (crate::ScalarKind::Uint, _) => "u",
2511                                _ => "",
2512                            };
2513                        }
2514                    }
2515                    write!(self.out, "atomicOr(")?;
2516                    self.write_expr(pointer, ctx)?;
2517                    write!(self.out, ", 0{})", suffix)?
2518                } else {
2519                    self.write_expr(pointer, ctx)?
2520                }
2521            }
2522            // `ImageSample` is a bit complicated compared to the rest of the IR.
2523            //
2524            // First there are three variations depending whether the sample level is explicitly set,
2525            // if it's automatic or it it's bias:
2526            // `texture(image, coordinate)` - Automatic sample level
2527            // `texture(image, coordinate, bias)` - Bias sample level
2528            // `textureLod(image, coordinate, level)` - Zero or Exact sample level
2529            //
2530            // Furthermore if `depth_ref` is some we need to append it to the coordinate vector
2531            Expression::ImageSample {
2532                image,
2533                sampler: _, //TODO?
2534                gather,
2535                coordinate,
2536                array_index,
2537                offset,
2538                level,
2539                depth_ref,
2540                clamp_to_edge: _,
2541            } => {
2542                let (dim, class, arrayed) = match *ctx.resolve_type(image, &self.module.types) {
2543                    TypeInner::Image {
2544                        dim,
2545                        class,
2546                        arrayed,
2547                        ..
2548                    } => (dim, class, arrayed),
2549                    _ => unreachable!(),
2550                };
2551                let mut err = None;
2552                if dim == crate::ImageDimension::Cube {
2553                    if offset.is_some() {
2554                        err = Some("gsamplerCube[Array][Shadow] doesn't support texture sampling with offsets");
2555                    }
2556                    if arrayed
2557                        && matches!(class, crate::ImageClass::Depth { .. })
2558                        && matches!(level, crate::SampleLevel::Gradient { .. })
2559                    {
2560                        err = Some("samplerCubeArrayShadow don't support textureGrad");
2561                    }
2562                }
2563                if gather.is_some() && level != crate::SampleLevel::Zero {
2564                    err = Some("textureGather doesn't support LOD parameters");
2565                }
2566                if let Some(err) = err {
2567                    return Err(Error::Custom(String::from(err)));
2568                }
2569
2570                // `textureLod[Offset]` on `sampler2DArrayShadow` and `samplerCubeShadow` does not exist in GLSL,
2571                // unless `GL_EXT_texture_shadow_lod` is present.
2572                // But if the target LOD is zero, we can emulate that by using `textureGrad[Offset]` with a constant gradient of 0.
2573                let workaround_lod_with_grad = ((dim == crate::ImageDimension::Cube && !arrayed)
2574                    || (dim == crate::ImageDimension::D2 && arrayed))
2575                    && level == crate::SampleLevel::Zero
2576                    && matches!(class, crate::ImageClass::Depth { .. })
2577                    && !self.features.contains(Features::TEXTURE_SHADOW_LOD);
2578
2579                // Write the function to be used depending on the sample level
2580                let fun_name = match level {
2581                    crate::SampleLevel::Zero if gather.is_some() => "textureGather",
2582                    crate::SampleLevel::Zero if workaround_lod_with_grad => "textureGrad",
2583                    crate::SampleLevel::Auto | crate::SampleLevel::Bias(_) => "texture",
2584                    crate::SampleLevel::Zero | crate::SampleLevel::Exact(_) => "textureLod",
2585                    crate::SampleLevel::Gradient { .. } => "textureGrad",
2586                };
2587                let offset_name = match offset {
2588                    Some(_) => "Offset",
2589                    None => "",
2590                };
2591
2592                write!(self.out, "{fun_name}{offset_name}(")?;
2593
2594                // Write the image that will be used
2595                self.write_expr(image, ctx)?;
2596                // The space here isn't required but it helps with readability
2597                write!(self.out, ", ")?;
2598
2599                // TODO: handle clamp_to_edge
2600                // https://github.com/gfx-rs/wgpu/issues/7791
2601
2602                // We need to get the coordinates vector size to later build a vector that's `size + 1`
2603                // if `depth_ref` is some, if it isn't a vector we panic as that's not a valid expression
2604                let mut coord_dim = match *ctx.resolve_type(coordinate, &self.module.types) {
2605                    TypeInner::Vector { size, .. } => size as u8,
2606                    TypeInner::Scalar { .. } => 1,
2607                    _ => unreachable!(),
2608                };
2609
2610                if array_index.is_some() {
2611                    coord_dim += 1;
2612                }
2613                let merge_depth_ref = depth_ref.is_some() && gather.is_none() && coord_dim < 4;
2614                if merge_depth_ref {
2615                    coord_dim += 1;
2616                }
2617
2618                let tex_1d_hack = dim == crate::ImageDimension::D1 && self.options.version.is_es();
2619                let is_vec = tex_1d_hack || coord_dim != 1;
2620                // Compose a new texture coordinates vector
2621                if is_vec {
2622                    write!(self.out, "vec{}(", coord_dim + tex_1d_hack as u8)?;
2623                }
2624                self.write_expr(coordinate, ctx)?;
2625                if tex_1d_hack {
2626                    write!(self.out, ", 0.0")?;
2627                }
2628                if let Some(expr) = array_index {
2629                    write!(self.out, ", ")?;
2630                    self.write_expr(expr, ctx)?;
2631                }
2632                if merge_depth_ref {
2633                    write!(self.out, ", ")?;
2634                    self.write_expr(depth_ref.unwrap(), ctx)?;
2635                }
2636                if is_vec {
2637                    write!(self.out, ")")?;
2638                }
2639
2640                if let (Some(expr), false) = (depth_ref, merge_depth_ref) {
2641                    write!(self.out, ", ")?;
2642                    self.write_expr(expr, ctx)?;
2643                }
2644
2645                match level {
2646                    // Auto needs no more arguments
2647                    crate::SampleLevel::Auto => (),
2648                    // Zero needs level set to 0
2649                    crate::SampleLevel::Zero => {
2650                        if workaround_lod_with_grad {
2651                            let vec_dim = match dim {
2652                                crate::ImageDimension::Cube => 3,
2653                                _ => 2,
2654                            };
2655                            write!(self.out, ", vec{vec_dim}(0.0), vec{vec_dim}(0.0)")?;
2656                        } else if gather.is_none() {
2657                            write!(self.out, ", 0.0")?;
2658                        }
2659                    }
2660                    // Exact and bias require another argument
2661                    crate::SampleLevel::Exact(expr) => {
2662                        write!(self.out, ", ")?;
2663                        self.write_expr(expr, ctx)?;
2664                    }
2665                    crate::SampleLevel::Bias(_) => {
2666                        // This needs to be done after the offset writing
2667                    }
2668                    crate::SampleLevel::Gradient { x, y } => {
2669                        // If we are using sampler2D to replace sampler1D, we also
2670                        // need to make sure to use vec2 gradients
2671                        if tex_1d_hack {
2672                            write!(self.out, ", vec2(")?;
2673                            self.write_expr(x, ctx)?;
2674                            write!(self.out, ", 0.0)")?;
2675                            write!(self.out, ", vec2(")?;
2676                            self.write_expr(y, ctx)?;
2677                            write!(self.out, ", 0.0)")?;
2678                        } else {
2679                            write!(self.out, ", ")?;
2680                            self.write_expr(x, ctx)?;
2681                            write!(self.out, ", ")?;
2682                            self.write_expr(y, ctx)?;
2683                        }
2684                    }
2685                }
2686
2687                if let Some(constant) = offset {
2688                    write!(self.out, ", ")?;
2689                    if tex_1d_hack {
2690                        write!(self.out, "ivec2(")?;
2691                    }
2692                    self.write_const_expr(constant, ctx.expressions)?;
2693                    if tex_1d_hack {
2694                        write!(self.out, ", 0)")?;
2695                    }
2696                }
2697
2698                // Bias is always the last argument
2699                if let crate::SampleLevel::Bias(expr) = level {
2700                    write!(self.out, ", ")?;
2701                    self.write_expr(expr, ctx)?;
2702                }
2703
2704                if let (Some(component), None) = (gather, depth_ref) {
2705                    write!(self.out, ", {}", component as usize)?;
2706                }
2707
2708                // End the function
2709                write!(self.out, ")")?
2710            }
2711            Expression::ImageLoad {
2712                image,
2713                coordinate,
2714                array_index,
2715                sample,
2716                level,
2717            } => self.write_image_load(expr, ctx, image, coordinate, array_index, sample, level)?,
2718            // Query translates into one of the:
2719            // - textureSize/imageSize
2720            // - textureQueryLevels
2721            // - textureSamples/imageSamples
2722            Expression::ImageQuery { image, query } => {
2723                use crate::ImageClass;
2724
2725                // This will only panic if the module is invalid
2726                let (dim, class) = match *ctx.resolve_type(image, &self.module.types) {
2727                    TypeInner::Image {
2728                        dim,
2729                        arrayed: _,
2730                        class,
2731                    } => (dim, class),
2732                    _ => unreachable!(),
2733                };
2734                let components = match dim {
2735                    crate::ImageDimension::D1 => 1,
2736                    crate::ImageDimension::D2 => 2,
2737                    crate::ImageDimension::D3 => 3,
2738                    crate::ImageDimension::Cube => 2,
2739                };
2740
2741                if let crate::ImageQuery::Size { .. } = query {
2742                    match components {
2743                        1 => write!(self.out, "uint(")?,
2744                        _ => write!(self.out, "uvec{components}(")?,
2745                    }
2746                } else {
2747                    write!(self.out, "uint(")?;
2748                }
2749
2750                match query {
2751                    crate::ImageQuery::Size { level } => {
2752                        match class {
2753                            ImageClass::Sampled { multi, .. } | ImageClass::Depth { multi } => {
2754                                write!(self.out, "textureSize(")?;
2755                                self.write_expr(image, ctx)?;
2756                                if let Some(expr) = level {
2757                                    let cast_to_int = matches!(
2758                                        *ctx.resolve_type(expr, &self.module.types),
2759                                        TypeInner::Scalar(crate::Scalar {
2760                                            kind: crate::ScalarKind::Uint,
2761                                            ..
2762                                        })
2763                                    );
2764
2765                                    write!(self.out, ", ")?;
2766
2767                                    if cast_to_int {
2768                                        write!(self.out, "int(")?;
2769                                    }
2770
2771                                    self.write_expr(expr, ctx)?;
2772
2773                                    if cast_to_int {
2774                                        write!(self.out, ")")?;
2775                                    }
2776                                } else if !multi {
2777                                    // All textureSize calls requires an lod argument
2778                                    // except for multisampled samplers
2779                                    write!(self.out, ", 0")?;
2780                                }
2781                            }
2782                            ImageClass::Storage { .. } => {
2783                                write!(self.out, "imageSize(")?;
2784                                self.write_expr(image, ctx)?;
2785                            }
2786                            ImageClass::External => unimplemented!(),
2787                        }
2788                        write!(self.out, ")")?;
2789                        if components != 1 || self.options.version.is_es() {
2790                            write!(self.out, ".{}", &"xyz"[..components])?;
2791                        }
2792                    }
2793                    crate::ImageQuery::NumLevels => {
2794                        write!(self.out, "textureQueryLevels(",)?;
2795                        self.write_expr(image, ctx)?;
2796                        write!(self.out, ")",)?;
2797                    }
2798                    crate::ImageQuery::NumLayers => {
2799                        let fun_name = match class {
2800                            ImageClass::Sampled { .. } | ImageClass::Depth { .. } => "textureSize",
2801                            ImageClass::Storage { .. } => "imageSize",
2802                            ImageClass::External => unimplemented!(),
2803                        };
2804                        write!(self.out, "{fun_name}(")?;
2805                        self.write_expr(image, ctx)?;
2806                        // All textureSize calls requires an lod argument
2807                        // except for multisampled samplers
2808                        if !class.is_multisampled() {
2809                            write!(self.out, ", 0")?;
2810                        }
2811                        write!(self.out, ")")?;
2812                        if components != 1 || self.options.version.is_es() {
2813                            write!(self.out, ".{}", back::COMPONENTS[components])?;
2814                        }
2815                    }
2816                    crate::ImageQuery::NumSamples => {
2817                        let fun_name = match class {
2818                            ImageClass::Sampled { .. } | ImageClass::Depth { .. } => {
2819                                "textureSamples"
2820                            }
2821                            ImageClass::Storage { .. } => "imageSamples",
2822                            ImageClass::External => unimplemented!(),
2823                        };
2824                        write!(self.out, "{fun_name}(")?;
2825                        self.write_expr(image, ctx)?;
2826                        write!(self.out, ")",)?;
2827                    }
2828                }
2829
2830                write!(self.out, ")")?;
2831            }
2832            Expression::Unary { op, expr } => {
2833                let operator_or_fn = match op {
2834                    crate::UnaryOperator::Negate => "-",
2835                    crate::UnaryOperator::LogicalNot => {
2836                        match *ctx.resolve_type(expr, &self.module.types) {
2837                            TypeInner::Vector { .. } => "not",
2838                            _ => "!",
2839                        }
2840                    }
2841                    crate::UnaryOperator::BitwiseNot => "~",
2842                };
2843                write!(self.out, "{operator_or_fn}(")?;
2844
2845                self.write_expr(expr, ctx)?;
2846
2847                write!(self.out, ")")?
2848            }
2849            // `Binary` we just write `left op right`, except when dealing with
2850            // comparison operations on vectors as they are implemented with
2851            // builtin functions.
2852            // Once again we wrap everything in parentheses to avoid precedence issues
2853            Expression::Binary {
2854                mut op,
2855                left,
2856                right,
2857            } => {
2858                // Holds `Some(function_name)` if the binary operation is
2859                // implemented as a function call
2860                use crate::{BinaryOperator as Bo, ScalarKind as Sk, TypeInner as Ti};
2861
2862                let left_inner = ctx.resolve_type(left, &self.module.types);
2863                let right_inner = ctx.resolve_type(right, &self.module.types);
2864
2865                let function = match (left_inner, right_inner) {
2866                    (&Ti::Vector { scalar, .. }, &Ti::Vector { .. }) => match op {
2867                        Bo::Less
2868                        | Bo::LessEqual
2869                        | Bo::Greater
2870                        | Bo::GreaterEqual
2871                        | Bo::Equal
2872                        | Bo::NotEqual => BinaryOperation::VectorCompare,
2873                        Bo::Modulo if scalar.kind == Sk::Float => BinaryOperation::Modulo,
2874                        Bo::And if scalar.kind == Sk::Bool => {
2875                            op = crate::BinaryOperator::LogicalAnd;
2876                            BinaryOperation::VectorComponentWise
2877                        }
2878                        Bo::InclusiveOr if scalar.kind == Sk::Bool => {
2879                            op = crate::BinaryOperator::LogicalOr;
2880                            BinaryOperation::VectorComponentWise
2881                        }
2882                        _ => BinaryOperation::Other,
2883                    },
2884                    _ => match (left_inner.scalar_kind(), right_inner.scalar_kind()) {
2885                        (Some(Sk::Float), _) | (_, Some(Sk::Float)) => match op {
2886                            Bo::Modulo => BinaryOperation::Modulo,
2887                            _ => BinaryOperation::Other,
2888                        },
2889                        (Some(Sk::Bool), Some(Sk::Bool)) => match op {
2890                            Bo::InclusiveOr => {
2891                                op = crate::BinaryOperator::LogicalOr;
2892                                BinaryOperation::Other
2893                            }
2894                            Bo::And => {
2895                                op = crate::BinaryOperator::LogicalAnd;
2896                                BinaryOperation::Other
2897                            }
2898                            _ => BinaryOperation::Other,
2899                        },
2900                        _ => BinaryOperation::Other,
2901                    },
2902                };
2903
2904                match function {
2905                    BinaryOperation::VectorCompare => {
2906                        let op_str = match op {
2907                            Bo::Less => "lessThan(",
2908                            Bo::LessEqual => "lessThanEqual(",
2909                            Bo::Greater => "greaterThan(",
2910                            Bo::GreaterEqual => "greaterThanEqual(",
2911                            Bo::Equal => "equal(",
2912                            Bo::NotEqual => "notEqual(",
2913                            _ => unreachable!(),
2914                        };
2915                        write!(self.out, "{op_str}")?;
2916                        self.write_expr(left, ctx)?;
2917                        write!(self.out, ", ")?;
2918                        self.write_expr(right, ctx)?;
2919                        write!(self.out, ")")?;
2920                    }
2921                    BinaryOperation::VectorComponentWise => {
2922                        self.write_value_type(left_inner)?;
2923                        write!(self.out, "(")?;
2924
2925                        let size = match *left_inner {
2926                            Ti::Vector { size, .. } => size,
2927                            _ => unreachable!(),
2928                        };
2929
2930                        for i in 0..size as usize {
2931                            if i != 0 {
2932                                write!(self.out, ", ")?;
2933                            }
2934
2935                            self.write_expr(left, ctx)?;
2936                            write!(self.out, ".{}", back::COMPONENTS[i])?;
2937
2938                            write!(self.out, " {} ", back::binary_operation_str(op))?;
2939
2940                            self.write_expr(right, ctx)?;
2941                            write!(self.out, ".{}", back::COMPONENTS[i])?;
2942                        }
2943
2944                        write!(self.out, ")")?;
2945                    }
2946                    // TODO: handle undefined behavior of BinaryOperator::Modulo
2947                    //
2948                    // sint:
2949                    // if right == 0 return 0
2950                    // if left == min(type_of(left)) && right == -1 return 0
2951                    // if sign(left) == -1 || sign(right) == -1 return result as defined by WGSL
2952                    //
2953                    // uint:
2954                    // if right == 0 return 0
2955                    //
2956                    // float:
2957                    // if right == 0 return ? see https://github.com/gpuweb/gpuweb/issues/2798
2958                    BinaryOperation::Modulo => {
2959                        write!(self.out, "(")?;
2960
2961                        // write `e1 - e2 * trunc(e1 / e2)`
2962                        self.write_expr(left, ctx)?;
2963                        write!(self.out, " - ")?;
2964                        self.write_expr(right, ctx)?;
2965                        write!(self.out, " * ")?;
2966                        write!(self.out, "trunc(")?;
2967                        self.write_expr(left, ctx)?;
2968                        write!(self.out, " / ")?;
2969                        self.write_expr(right, ctx)?;
2970                        write!(self.out, ")")?;
2971
2972                        write!(self.out, ")")?;
2973                    }
2974                    BinaryOperation::Other => {
2975                        write!(self.out, "(")?;
2976
2977                        self.write_expr(left, ctx)?;
2978                        write!(self.out, " {} ", back::binary_operation_str(op))?;
2979                        self.write_expr(right, ctx)?;
2980
2981                        write!(self.out, ")")?;
2982                    }
2983                }
2984            }
2985            // `Select` is written as `condition ? accept : reject`
2986            // We wrap everything in parentheses to avoid precedence issues
2987            Expression::Select {
2988                condition,
2989                accept,
2990                reject,
2991            } => {
2992                let cond_ty = ctx.resolve_type(condition, &self.module.types);
2993                let vec_select = if let TypeInner::Vector { .. } = *cond_ty {
2994                    true
2995                } else {
2996                    false
2997                };
2998
2999                // TODO: Boolean mix on desktop required GL_EXT_shader_integer_mix
3000                if vec_select {
3001                    // Glsl defines that for mix when the condition is a boolean the first element
3002                    // is picked if condition is false and the second if condition is true
3003                    write!(self.out, "mix(")?;
3004                    self.write_expr(reject, ctx)?;
3005                    write!(self.out, ", ")?;
3006                    self.write_expr(accept, ctx)?;
3007                    write!(self.out, ", ")?;
3008                    self.write_expr(condition, ctx)?;
3009                } else {
3010                    write!(self.out, "(")?;
3011                    self.write_expr(condition, ctx)?;
3012                    write!(self.out, " ? ")?;
3013                    self.write_expr(accept, ctx)?;
3014                    write!(self.out, " : ")?;
3015                    self.write_expr(reject, ctx)?;
3016                }
3017
3018                write!(self.out, ")")?
3019            }
3020            // `Derivative` is a function call to a glsl provided function
3021            Expression::Derivative { axis, ctrl, expr } => {
3022                use crate::{DerivativeAxis as Axis, DerivativeControl as Ctrl};
3023                let fun_name = if self.options.version.supports_derivative_control() {
3024                    match (axis, ctrl) {
3025                        (Axis::X, Ctrl::Coarse) => "dFdxCoarse",
3026                        (Axis::X, Ctrl::Fine) => "dFdxFine",
3027                        (Axis::X, Ctrl::None) => "dFdx",
3028                        (Axis::Y, Ctrl::Coarse) => "dFdyCoarse",
3029                        (Axis::Y, Ctrl::Fine) => "dFdyFine",
3030                        (Axis::Y, Ctrl::None) => "dFdy",
3031                        (Axis::Width, Ctrl::Coarse) => "fwidthCoarse",
3032                        (Axis::Width, Ctrl::Fine) => "fwidthFine",
3033                        (Axis::Width, Ctrl::None) => "fwidth",
3034                    }
3035                } else {
3036                    match axis {
3037                        Axis::X => "dFdx",
3038                        Axis::Y => "dFdy",
3039                        Axis::Width => "fwidth",
3040                    }
3041                };
3042                write!(self.out, "{fun_name}(")?;
3043                self.write_expr(expr, ctx)?;
3044                write!(self.out, ")")?
3045            }
3046            // `Relational` is a normal function call to some glsl provided functions
3047            Expression::Relational { fun, argument } => {
3048                use crate::RelationalFunction as Rf;
3049
3050                let fun_name = match fun {
3051                    Rf::IsInf => "isinf",
3052                    Rf::IsNan => "isnan",
3053                    Rf::All => "all",
3054                    Rf::Any => "any",
3055                };
3056                write!(self.out, "{fun_name}(")?;
3057
3058                self.write_expr(argument, ctx)?;
3059
3060                write!(self.out, ")")?
3061            }
3062            Expression::Math {
3063                fun,
3064                arg,
3065                arg1,
3066                arg2,
3067                arg3,
3068            } => {
3069                use crate::MathFunction as Mf;
3070
3071                let fun_name = match fun {
3072                    // comparison
3073                    Mf::Abs => "abs",
3074                    Mf::Min => "min",
3075                    Mf::Max => "max",
3076                    Mf::Clamp => {
3077                        let scalar_kind = ctx
3078                            .resolve_type(arg, &self.module.types)
3079                            .scalar_kind()
3080                            .unwrap();
3081                        match scalar_kind {
3082                            crate::ScalarKind::Float => "clamp",
3083                            // Clamp is undefined if min > max. In practice this means it can use a median-of-three
3084                            // instruction to determine the value. This is fine according to the WGSL spec for float
3085                            // clamp, but integer clamp _must_ use min-max. As such we write out min/max.
3086                            _ => {
3087                                write!(self.out, "min(max(")?;
3088                                self.write_expr(arg, ctx)?;
3089                                write!(self.out, ", ")?;
3090                                self.write_expr(arg1.unwrap(), ctx)?;
3091                                write!(self.out, "), ")?;
3092                                self.write_expr(arg2.unwrap(), ctx)?;
3093                                write!(self.out, ")")?;
3094
3095                                return Ok(());
3096                            }
3097                        }
3098                    }
3099                    Mf::Saturate => {
3100                        write!(self.out, "clamp(")?;
3101
3102                        self.write_expr(arg, ctx)?;
3103
3104                        match *ctx.resolve_type(arg, &self.module.types) {
3105                            TypeInner::Vector { size, .. } => write!(
3106                                self.out,
3107                                ", vec{}(0.0), vec{0}(1.0)",
3108                                common::vector_size_str(size)
3109                            )?,
3110                            _ => write!(self.out, ", 0.0, 1.0")?,
3111                        }
3112
3113                        write!(self.out, ")")?;
3114
3115                        return Ok(());
3116                    }
3117                    // trigonometry
3118                    Mf::Cos => "cos",
3119                    Mf::Cosh => "cosh",
3120                    Mf::Sin => "sin",
3121                    Mf::Sinh => "sinh",
3122                    Mf::Tan => "tan",
3123                    Mf::Tanh => "tanh",
3124                    Mf::Acos => "acos",
3125                    Mf::Asin => "asin",
3126                    Mf::Atan => "atan",
3127                    Mf::Asinh => "asinh",
3128                    Mf::Acosh => "acosh",
3129                    Mf::Atanh => "atanh",
3130                    Mf::Radians => "radians",
3131                    Mf::Degrees => "degrees",
3132                    // glsl doesn't have atan2 function
3133                    // use two-argument variation of the atan function
3134                    Mf::Atan2 => "atan",
3135                    // decomposition
3136                    Mf::Ceil => "ceil",
3137                    Mf::Floor => "floor",
3138                    Mf::Round => "roundEven",
3139                    Mf::Fract => "fract",
3140                    Mf::Trunc => "trunc",
3141                    Mf::Modf => MODF_FUNCTION,
3142                    Mf::Frexp => FREXP_FUNCTION,
3143                    Mf::Ldexp => "ldexp",
3144                    // exponent
3145                    Mf::Exp => "exp",
3146                    Mf::Exp2 => "exp2",
3147                    Mf::Log => "log",
3148                    Mf::Log2 => "log2",
3149                    Mf::Pow => "pow",
3150                    // geometry
3151                    Mf::Dot => match *ctx.resolve_type(arg, &self.module.types) {
3152                        TypeInner::Vector {
3153                            scalar:
3154                                crate::Scalar {
3155                                    kind: crate::ScalarKind::Float,
3156                                    ..
3157                                },
3158                            ..
3159                        } => "dot",
3160                        TypeInner::Vector { size, .. } => {
3161                            return self.write_dot_product(arg, arg1.unwrap(), size as usize, ctx)
3162                        }
3163                        _ => unreachable!(
3164                            "Correct TypeInner for dot product should be already validated"
3165                        ),
3166                    },
3167                    fun @ (Mf::Dot4I8Packed | Mf::Dot4U8Packed) => {
3168                        let conversion = match fun {
3169                            Mf::Dot4I8Packed => "int",
3170                            Mf::Dot4U8Packed => "",
3171                            _ => unreachable!(),
3172                        };
3173
3174                        let arg1 = arg1.unwrap();
3175
3176                        // Write parentheses around the dot product expression to prevent operators
3177                        // with different precedences from applying earlier.
3178                        write!(self.out, "(")?;
3179                        for i in 0..4 {
3180                            // Since `bitfieldExtract` only sign extends if the value is signed, we
3181                            // need to convert the inputs to `int` in case of `Dot4I8Packed`. For
3182                            // `Dot4U8Packed`, the code below only introduces parenthesis around
3183                            // each factor, which aren't strictly needed because both operands are
3184                            // baked, but which don't hurt either.
3185                            write!(self.out, "bitfieldExtract({conversion}(")?;
3186                            self.write_expr(arg, ctx)?;
3187                            write!(self.out, "), {}, 8)", i * 8)?;
3188
3189                            write!(self.out, " * bitfieldExtract({conversion}(")?;
3190                            self.write_expr(arg1, ctx)?;
3191                            write!(self.out, "), {}, 8)", i * 8)?;
3192
3193                            if i != 3 {
3194                                write!(self.out, " + ")?;
3195                            }
3196                        }
3197                        write!(self.out, ")")?;
3198
3199                        return Ok(());
3200                    }
3201                    Mf::Outer => "outerProduct",
3202                    Mf::Cross => "cross",
3203                    Mf::Distance => "distance",
3204                    Mf::Length => "length",
3205                    Mf::Normalize => "normalize",
3206                    Mf::FaceForward => "faceforward",
3207                    Mf::Reflect => "reflect",
3208                    Mf::Refract => "refract",
3209                    // computational
3210                    Mf::Sign => "sign",
3211                    Mf::Fma => {
3212                        if self.options.version.supports_fma_function() {
3213                            // Use the fma function when available
3214                            "fma"
3215                        } else {
3216                            // No fma support. Transform the function call into an arithmetic expression
3217                            write!(self.out, "(")?;
3218
3219                            self.write_expr(arg, ctx)?;
3220                            write!(self.out, " * ")?;
3221
3222                            let arg1 =
3223                                arg1.ok_or_else(|| Error::Custom("Missing fma arg1".to_owned()))?;
3224                            self.write_expr(arg1, ctx)?;
3225                            write!(self.out, " + ")?;
3226
3227                            let arg2 =
3228                                arg2.ok_or_else(|| Error::Custom("Missing fma arg2".to_owned()))?;
3229                            self.write_expr(arg2, ctx)?;
3230                            write!(self.out, ")")?;
3231
3232                            return Ok(());
3233                        }
3234                    }
3235                    Mf::Mix => "mix",
3236                    Mf::Step => "step",
3237                    Mf::SmoothStep => "smoothstep",
3238                    Mf::Sqrt => "sqrt",
3239                    Mf::InverseSqrt => "inversesqrt",
3240                    Mf::Inverse => "inverse",
3241                    Mf::Transpose => "transpose",
3242                    Mf::Determinant => "determinant",
3243                    Mf::QuantizeToF16 => match *ctx.resolve_type(arg, &self.module.types) {
3244                        TypeInner::Scalar { .. } => {
3245                            write!(self.out, "unpackHalf2x16(packHalf2x16(vec2(")?;
3246                            self.write_expr(arg, ctx)?;
3247                            write!(self.out, "))).x")?;
3248                            return Ok(());
3249                        }
3250                        TypeInner::Vector {
3251                            size: crate::VectorSize::Bi,
3252                            ..
3253                        } => {
3254                            write!(self.out, "unpackHalf2x16(packHalf2x16(")?;
3255                            self.write_expr(arg, ctx)?;
3256                            write!(self.out, "))")?;
3257                            return Ok(());
3258                        }
3259                        TypeInner::Vector {
3260                            size: crate::VectorSize::Tri,
3261                            ..
3262                        } => {
3263                            write!(self.out, "vec3(unpackHalf2x16(packHalf2x16(")?;
3264                            self.write_expr(arg, ctx)?;
3265                            write!(self.out, ".xy)), unpackHalf2x16(packHalf2x16(")?;
3266                            self.write_expr(arg, ctx)?;
3267                            write!(self.out, ".zz)).x)")?;
3268                            return Ok(());
3269                        }
3270                        TypeInner::Vector {
3271                            size: crate::VectorSize::Quad,
3272                            ..
3273                        } => {
3274                            write!(self.out, "vec4(unpackHalf2x16(packHalf2x16(")?;
3275                            self.write_expr(arg, ctx)?;
3276                            write!(self.out, ".xy)), unpackHalf2x16(packHalf2x16(")?;
3277                            self.write_expr(arg, ctx)?;
3278                            write!(self.out, ".zw)))")?;
3279                            return Ok(());
3280                        }
3281                        _ => unreachable!(
3282                            "Correct TypeInner for QuantizeToF16 should be already validated"
3283                        ),
3284                    },
3285                    // bits
3286                    Mf::CountTrailingZeros => {
3287                        match *ctx.resolve_type(arg, &self.module.types) {
3288                            TypeInner::Vector { size, scalar, .. } => {
3289                                let s = common::vector_size_str(size);
3290                                if let crate::ScalarKind::Uint = scalar.kind {
3291                                    write!(self.out, "min(uvec{s}(findLSB(")?;
3292                                    self.write_expr(arg, ctx)?;
3293                                    write!(self.out, ")), uvec{s}(32u))")?;
3294                                } else {
3295                                    write!(self.out, "ivec{s}(min(uvec{s}(findLSB(")?;
3296                                    self.write_expr(arg, ctx)?;
3297                                    write!(self.out, ")), uvec{s}(32u)))")?;
3298                                }
3299                            }
3300                            TypeInner::Scalar(scalar) => {
3301                                if let crate::ScalarKind::Uint = scalar.kind {
3302                                    write!(self.out, "min(uint(findLSB(")?;
3303                                    self.write_expr(arg, ctx)?;
3304                                    write!(self.out, ")), 32u)")?;
3305                                } else {
3306                                    write!(self.out, "int(min(uint(findLSB(")?;
3307                                    self.write_expr(arg, ctx)?;
3308                                    write!(self.out, ")), 32u))")?;
3309                                }
3310                            }
3311                            _ => unreachable!(),
3312                        };
3313                        return Ok(());
3314                    }
3315                    Mf::CountLeadingZeros => {
3316                        if self.options.version.supports_integer_functions() {
3317                            match *ctx.resolve_type(arg, &self.module.types) {
3318                                TypeInner::Vector { size, scalar } => {
3319                                    let s = common::vector_size_str(size);
3320
3321                                    if let crate::ScalarKind::Uint = scalar.kind {
3322                                        write!(self.out, "uvec{s}(ivec{s}(31) - findMSB(")?;
3323                                        self.write_expr(arg, ctx)?;
3324                                        write!(self.out, "))")?;
3325                                    } else {
3326                                        write!(self.out, "mix(ivec{s}(31) - findMSB(")?;
3327                                        self.write_expr(arg, ctx)?;
3328                                        write!(self.out, "), ivec{s}(0), lessThan(")?;
3329                                        self.write_expr(arg, ctx)?;
3330                                        write!(self.out, ", ivec{s}(0)))")?;
3331                                    }
3332                                }
3333                                TypeInner::Scalar(scalar) => {
3334                                    if let crate::ScalarKind::Uint = scalar.kind {
3335                                        write!(self.out, "uint(31 - findMSB(")?;
3336                                    } else {
3337                                        write!(self.out, "(")?;
3338                                        self.write_expr(arg, ctx)?;
3339                                        write!(self.out, " < 0 ? 0 : 31 - findMSB(")?;
3340                                    }
3341
3342                                    self.write_expr(arg, ctx)?;
3343                                    write!(self.out, "))")?;
3344                                }
3345                                _ => unreachable!(),
3346                            };
3347                        } else {
3348                            match *ctx.resolve_type(arg, &self.module.types) {
3349                                TypeInner::Vector { size, scalar } => {
3350                                    let s = common::vector_size_str(size);
3351
3352                                    if let crate::ScalarKind::Uint = scalar.kind {
3353                                        write!(self.out, "uvec{s}(")?;
3354                                        write!(self.out, "vec{s}(31.0) - floor(log2(vec{s}(")?;
3355                                        self.write_expr(arg, ctx)?;
3356                                        write!(self.out, ") + 0.5)))")?;
3357                                    } else {
3358                                        write!(self.out, "ivec{s}(")?;
3359                                        write!(self.out, "mix(vec{s}(31.0) - floor(log2(vec{s}(")?;
3360                                        self.write_expr(arg, ctx)?;
3361                                        write!(self.out, ") + 0.5)), ")?;
3362                                        write!(self.out, "vec{s}(0.0), lessThan(")?;
3363                                        self.write_expr(arg, ctx)?;
3364                                        write!(self.out, ", ivec{s}(0u))))")?;
3365                                    }
3366                                }
3367                                TypeInner::Scalar(scalar) => {
3368                                    if let crate::ScalarKind::Uint = scalar.kind {
3369                                        write!(self.out, "uint(31.0 - floor(log2(float(")?;
3370                                        self.write_expr(arg, ctx)?;
3371                                        write!(self.out, ") + 0.5)))")?;
3372                                    } else {
3373                                        write!(self.out, "(")?;
3374                                        self.write_expr(arg, ctx)?;
3375                                        write!(self.out, " < 0 ? 0 : int(")?;
3376                                        write!(self.out, "31.0 - floor(log2(float(")?;
3377                                        self.write_expr(arg, ctx)?;
3378                                        write!(self.out, ") + 0.5))))")?;
3379                                    }
3380                                }
3381                                _ => unreachable!(),
3382                            };
3383                        }
3384
3385                        return Ok(());
3386                    }
3387                    Mf::CountOneBits => "bitCount",
3388                    Mf::ReverseBits => "bitfieldReverse",
3389                    Mf::ExtractBits => {
3390                        // The behavior of ExtractBits is undefined when offset + count > bit_width. We need
3391                        // to first sanitize the offset and count first. If we don't do this, AMD and Intel chips
3392                        // will return out-of-spec values if the extracted range is not within the bit width.
3393                        //
3394                        // This encodes the exact formula specified by the wgsl spec, without temporary values:
3395                        // https://gpuweb.github.io/gpuweb/wgsl/#extractBits-unsigned-builtin
3396                        //
3397                        // w = sizeof(x) * 8
3398                        // o = min(offset, w)
3399                        // c = min(count, w - o)
3400                        //
3401                        // bitfieldExtract(x, o, c)
3402                        //
3403                        // extract_bits(e, min(offset, w), min(count, w - min(offset, w))))
3404                        let scalar_bits = ctx
3405                            .resolve_type(arg, &self.module.types)
3406                            .scalar_width()
3407                            .unwrap()
3408                            * 8;
3409
3410                        write!(self.out, "bitfieldExtract(")?;
3411                        self.write_expr(arg, ctx)?;
3412                        write!(self.out, ", int(min(")?;
3413                        self.write_expr(arg1.unwrap(), ctx)?;
3414                        write!(self.out, ", {scalar_bits}u)), int(min(",)?;
3415                        self.write_expr(arg2.unwrap(), ctx)?;
3416                        write!(self.out, ", {scalar_bits}u - min(")?;
3417                        self.write_expr(arg1.unwrap(), ctx)?;
3418                        write!(self.out, ", {scalar_bits}u))))")?;
3419
3420                        return Ok(());
3421                    }
3422                    Mf::InsertBits => {
3423                        // InsertBits has the same considerations as ExtractBits above
3424                        let scalar_bits = ctx
3425                            .resolve_type(arg, &self.module.types)
3426                            .scalar_width()
3427                            .unwrap()
3428                            * 8;
3429
3430                        write!(self.out, "bitfieldInsert(")?;
3431                        self.write_expr(arg, ctx)?;
3432                        write!(self.out, ", ")?;
3433                        self.write_expr(arg1.unwrap(), ctx)?;
3434                        write!(self.out, ", int(min(")?;
3435                        self.write_expr(arg2.unwrap(), ctx)?;
3436                        write!(self.out, ", {scalar_bits}u)), int(min(",)?;
3437                        self.write_expr(arg3.unwrap(), ctx)?;
3438                        write!(self.out, ", {scalar_bits}u - min(")?;
3439                        self.write_expr(arg2.unwrap(), ctx)?;
3440                        write!(self.out, ", {scalar_bits}u))))")?;
3441
3442                        return Ok(());
3443                    }
3444                    Mf::FirstTrailingBit => "findLSB",
3445                    Mf::FirstLeadingBit => "findMSB",
3446                    // data packing
3447                    Mf::Pack4x8snorm => {
3448                        if self.options.version.supports_pack_unpack_4x8() {
3449                            "packSnorm4x8"
3450                        } else {
3451                            // polyfill should go here. Needs a corresponding entry in `need_bake_expression`
3452                            return Err(Error::UnsupportedExternal("packSnorm4x8".into()));
3453                        }
3454                    }
3455                    Mf::Pack4x8unorm => {
3456                        if self.options.version.supports_pack_unpack_4x8() {
3457                            "packUnorm4x8"
3458                        } else {
3459                            return Err(Error::UnsupportedExternal("packUnorm4x8".to_owned()));
3460                        }
3461                    }
3462                    Mf::Pack2x16snorm => {
3463                        if self.options.version.supports_pack_unpack_snorm_2x16() {
3464                            "packSnorm2x16"
3465                        } else {
3466                            return Err(Error::UnsupportedExternal("packSnorm2x16".to_owned()));
3467                        }
3468                    }
3469                    Mf::Pack2x16unorm => {
3470                        if self.options.version.supports_pack_unpack_unorm_2x16() {
3471                            "packUnorm2x16"
3472                        } else {
3473                            return Err(Error::UnsupportedExternal("packUnorm2x16".to_owned()));
3474                        }
3475                    }
3476                    Mf::Pack2x16float => {
3477                        if self.options.version.supports_pack_unpack_half_2x16() {
3478                            "packHalf2x16"
3479                        } else {
3480                            return Err(Error::UnsupportedExternal("packHalf2x16".to_owned()));
3481                        }
3482                    }
3483
3484                    fun @ (Mf::Pack4xI8 | Mf::Pack4xU8 | Mf::Pack4xI8Clamp | Mf::Pack4xU8Clamp) => {
3485                        let was_signed = matches!(fun, Mf::Pack4xI8 | Mf::Pack4xI8Clamp);
3486                        let clamp_bounds = match fun {
3487                            Mf::Pack4xI8Clamp => Some(("-128", "127")),
3488                            Mf::Pack4xU8Clamp => Some(("0", "255")),
3489                            _ => None,
3490                        };
3491                        let const_suffix = if was_signed { "" } else { "u" };
3492                        if was_signed {
3493                            write!(self.out, "uint(")?;
3494                        }
3495                        let write_arg = |this: &mut Self| -> BackendResult {
3496                            if let Some((min, max)) = clamp_bounds {
3497                                write!(this.out, "clamp(")?;
3498                                this.write_expr(arg, ctx)?;
3499                                write!(this.out, ", {min}{const_suffix}, {max}{const_suffix})")?;
3500                            } else {
3501                                this.write_expr(arg, ctx)?;
3502                            }
3503                            Ok(())
3504                        };
3505                        write!(self.out, "(")?;
3506                        write_arg(self)?;
3507                        write!(self.out, "[0] & 0xFF{const_suffix}) | ((")?;
3508                        write_arg(self)?;
3509                        write!(self.out, "[1] & 0xFF{const_suffix}) << 8) | ((")?;
3510                        write_arg(self)?;
3511                        write!(self.out, "[2] & 0xFF{const_suffix}) << 16) | ((")?;
3512                        write_arg(self)?;
3513                        write!(self.out, "[3] & 0xFF{const_suffix}) << 24)")?;
3514                        if was_signed {
3515                            write!(self.out, ")")?;
3516                        }
3517
3518                        return Ok(());
3519                    }
3520                    // data unpacking
3521                    Mf::Unpack2x16float => {
3522                        if self.options.version.supports_pack_unpack_half_2x16() {
3523                            "unpackHalf2x16"
3524                        } else {
3525                            return Err(Error::UnsupportedExternal("unpackHalf2x16".into()));
3526                        }
3527                    }
3528                    Mf::Unpack2x16snorm => {
3529                        if self.options.version.supports_pack_unpack_snorm_2x16() {
3530                            "unpackSnorm2x16"
3531                        } else {
3532                            let scale = 32767;
3533
3534                            write!(self.out, "(vec2(ivec2(")?;
3535                            self.write_expr(arg, ctx)?;
3536                            write!(self.out, " << 16, ")?;
3537                            self.write_expr(arg, ctx)?;
3538                            write!(self.out, ") >> 16) / {scale}.0)")?;
3539                            return Ok(());
3540                        }
3541                    }
3542                    Mf::Unpack2x16unorm => {
3543                        if self.options.version.supports_pack_unpack_unorm_2x16() {
3544                            "unpackUnorm2x16"
3545                        } else {
3546                            let scale = 65535;
3547
3548                            write!(self.out, "(vec2(")?;
3549                            self.write_expr(arg, ctx)?;
3550                            write!(self.out, " & 0xFFFFu, ")?;
3551                            self.write_expr(arg, ctx)?;
3552                            write!(self.out, " >> 16) / {scale}.0)")?;
3553                            return Ok(());
3554                        }
3555                    }
3556                    Mf::Unpack4x8snorm => {
3557                        if self.options.version.supports_pack_unpack_4x8() {
3558                            "unpackSnorm4x8"
3559                        } else {
3560                            let scale = 127;
3561
3562                            write!(self.out, "(vec4(ivec4(")?;
3563                            self.write_expr(arg, ctx)?;
3564                            write!(self.out, " << 24, ")?;
3565                            self.write_expr(arg, ctx)?;
3566                            write!(self.out, " << 16, ")?;
3567                            self.write_expr(arg, ctx)?;
3568                            write!(self.out, " << 8, ")?;
3569                            self.write_expr(arg, ctx)?;
3570                            write!(self.out, ") >> 24) / {scale}.0)")?;
3571                            return Ok(());
3572                        }
3573                    }
3574                    Mf::Unpack4x8unorm => {
3575                        if self.options.version.supports_pack_unpack_4x8() {
3576                            "unpackUnorm4x8"
3577                        } else {
3578                            let scale = 255;
3579
3580                            write!(self.out, "(vec4(")?;
3581                            self.write_expr(arg, ctx)?;
3582                            write!(self.out, " & 0xFFu, ")?;
3583                            self.write_expr(arg, ctx)?;
3584                            write!(self.out, " >> 8 & 0xFFu, ")?;
3585                            self.write_expr(arg, ctx)?;
3586                            write!(self.out, " >> 16 & 0xFFu, ")?;
3587                            self.write_expr(arg, ctx)?;
3588                            write!(self.out, " >> 24) / {scale}.0)")?;
3589                            return Ok(());
3590                        }
3591                    }
3592                    fun @ (Mf::Unpack4xI8 | Mf::Unpack4xU8) => {
3593                        let sign_prefix = match fun {
3594                            Mf::Unpack4xI8 => 'i',
3595                            Mf::Unpack4xU8 => 'u',
3596                            _ => unreachable!(),
3597                        };
3598                        write!(self.out, "{sign_prefix}vec4(")?;
3599                        for i in 0..4 {
3600                            write!(self.out, "bitfieldExtract(")?;
3601                            // Since bitfieldExtract only sign extends if the value is signed, this
3602                            // cast is needed
3603                            match fun {
3604                                Mf::Unpack4xI8 => {
3605                                    write!(self.out, "int(")?;
3606                                    self.write_expr(arg, ctx)?;
3607                                    write!(self.out, ")")?;
3608                                }
3609                                Mf::Unpack4xU8 => self.write_expr(arg, ctx)?,
3610                                _ => unreachable!(),
3611                            };
3612                            write!(self.out, ", {}, 8)", i * 8)?;
3613                            if i != 3 {
3614                                write!(self.out, ", ")?;
3615                            }
3616                        }
3617                        write!(self.out, ")")?;
3618
3619                        return Ok(());
3620                    }
3621                };
3622
3623                let extract_bits = fun == Mf::ExtractBits;
3624                let insert_bits = fun == Mf::InsertBits;
3625
3626                // Some GLSL functions always return signed integers (like findMSB),
3627                // so they need to be cast to uint if the argument is also an uint.
3628                let ret_might_need_int_to_uint = matches!(
3629                    fun,
3630                    Mf::FirstTrailingBit | Mf::FirstLeadingBit | Mf::CountOneBits | Mf::Abs
3631                );
3632
3633                // Some GLSL functions only accept signed integers (like abs),
3634                // so they need their argument cast from uint to int.
3635                let arg_might_need_uint_to_int = matches!(fun, Mf::Abs);
3636
3637                // Check if the argument is an unsigned integer and return the vector size
3638                // in case it's a vector
3639                let maybe_uint_size = match *ctx.resolve_type(arg, &self.module.types) {
3640                    TypeInner::Scalar(crate::Scalar {
3641                        kind: crate::ScalarKind::Uint,
3642                        ..
3643                    }) => Some(None),
3644                    TypeInner::Vector {
3645                        scalar:
3646                            crate::Scalar {
3647                                kind: crate::ScalarKind::Uint,
3648                                ..
3649                            },
3650                        size,
3651                    } => Some(Some(size)),
3652                    _ => None,
3653                };
3654
3655                // Cast to uint if the function needs it
3656                if ret_might_need_int_to_uint {
3657                    if let Some(maybe_size) = maybe_uint_size {
3658                        match maybe_size {
3659                            Some(size) => write!(self.out, "uvec{}(", size as u8)?,
3660                            None => write!(self.out, "uint(")?,
3661                        }
3662                    }
3663                }
3664
3665                write!(self.out, "{fun_name}(")?;
3666
3667                // Cast to int if the function needs it
3668                if arg_might_need_uint_to_int {
3669                    if let Some(maybe_size) = maybe_uint_size {
3670                        match maybe_size {
3671                            Some(size) => write!(self.out, "ivec{}(", size as u8)?,
3672                            None => write!(self.out, "int(")?,
3673                        }
3674                    }
3675                }
3676
3677                self.write_expr(arg, ctx)?;
3678
3679                // Close the cast from uint to int
3680                if arg_might_need_uint_to_int && maybe_uint_size.is_some() {
3681                    write!(self.out, ")")?
3682                }
3683
3684                if let Some(arg) = arg1 {
3685                    write!(self.out, ", ")?;
3686                    if extract_bits {
3687                        write!(self.out, "int(")?;
3688                        self.write_expr(arg, ctx)?;
3689                        write!(self.out, ")")?;
3690                    } else {
3691                        self.write_expr(arg, ctx)?;
3692                    }
3693                }
3694                if let Some(arg) = arg2 {
3695                    write!(self.out, ", ")?;
3696                    if extract_bits || insert_bits {
3697                        write!(self.out, "int(")?;
3698                        self.write_expr(arg, ctx)?;
3699                        write!(self.out, ")")?;
3700                    } else {
3701                        self.write_expr(arg, ctx)?;
3702                    }
3703                }
3704                if let Some(arg) = arg3 {
3705                    write!(self.out, ", ")?;
3706                    if insert_bits {
3707                        write!(self.out, "int(")?;
3708                        self.write_expr(arg, ctx)?;
3709                        write!(self.out, ")")?;
3710                    } else {
3711                        self.write_expr(arg, ctx)?;
3712                    }
3713                }
3714                write!(self.out, ")")?;
3715
3716                // Close the cast from int to uint
3717                if ret_might_need_int_to_uint && maybe_uint_size.is_some() {
3718                    write!(self.out, ")")?
3719                }
3720            }
3721            // `As` is always a call.
3722            // If `convert` is true the function name is the type
3723            // Else the function name is one of the glsl provided bitcast functions
3724            Expression::As {
3725                expr,
3726                kind: target_kind,
3727                convert,
3728            } => {
3729                let inner = ctx.resolve_type(expr, &self.module.types);
3730                match convert {
3731                    Some(width) => {
3732                        // this is similar to `write_type`, but with the target kind
3733                        let scalar = glsl_scalar(crate::Scalar {
3734                            kind: target_kind,
3735                            width,
3736                        })?;
3737                        match *inner {
3738                            TypeInner::Matrix { columns, rows, .. } => write!(
3739                                self.out,
3740                                "{}mat{}x{}",
3741                                scalar.prefix, columns as u8, rows as u8
3742                            )?,
3743                            TypeInner::Vector { size, .. } => {
3744                                write!(self.out, "{}vec{}", scalar.prefix, size as u8)?
3745                            }
3746                            _ => write!(self.out, "{}", scalar.full)?,
3747                        }
3748
3749                        write!(self.out, "(")?;
3750                        self.write_expr(expr, ctx)?;
3751                        write!(self.out, ")")?
3752                    }
3753                    None => {
3754                        use crate::ScalarKind as Sk;
3755
3756                        let target_vector_type = match *inner {
3757                            TypeInner::Vector { size, scalar } => Some(TypeInner::Vector {
3758                                size,
3759                                scalar: crate::Scalar {
3760                                    kind: target_kind,
3761                                    width: scalar.width,
3762                                },
3763                            }),
3764                            _ => None,
3765                        };
3766
3767                        let source_kind = inner.scalar_kind().unwrap();
3768
3769                        match (source_kind, target_kind, target_vector_type) {
3770                            // No conversion needed
3771                            (Sk::Sint, Sk::Sint, _)
3772                            | (Sk::Uint, Sk::Uint, _)
3773                            | (Sk::Float, Sk::Float, _)
3774                            | (Sk::Bool, Sk::Bool, _) => {
3775                                self.write_expr(expr, ctx)?;
3776                                return Ok(());
3777                            }
3778
3779                            // Cast to/from floats
3780                            (Sk::Float, Sk::Sint, _) => write!(self.out, "floatBitsToInt")?,
3781                            (Sk::Float, Sk::Uint, _) => write!(self.out, "floatBitsToUint")?,
3782                            (Sk::Sint, Sk::Float, _) => write!(self.out, "intBitsToFloat")?,
3783                            (Sk::Uint, Sk::Float, _) => write!(self.out, "uintBitsToFloat")?,
3784
3785                            // Cast between vector types
3786                            (_, _, Some(vector)) => {
3787                                self.write_value_type(&vector)?;
3788                            }
3789
3790                            // There is no way to bitcast between Uint/Sint in glsl. Use constructor conversion
3791                            (Sk::Uint | Sk::Bool, Sk::Sint, None) => write!(self.out, "int")?,
3792                            (Sk::Sint | Sk::Bool, Sk::Uint, None) => write!(self.out, "uint")?,
3793                            (Sk::Bool, Sk::Float, None) => write!(self.out, "float")?,
3794                            (Sk::Sint | Sk::Uint | Sk::Float, Sk::Bool, None) => {
3795                                write!(self.out, "bool")?
3796                            }
3797
3798                            (Sk::AbstractInt | Sk::AbstractFloat, _, _)
3799                            | (_, Sk::AbstractInt | Sk::AbstractFloat, _) => unreachable!(),
3800                        };
3801
3802                        write!(self.out, "(")?;
3803                        self.write_expr(expr, ctx)?;
3804                        write!(self.out, ")")?;
3805                    }
3806                }
3807            }
3808            // These expressions never show up in `Emit`.
3809            Expression::CallResult(_)
3810            | Expression::AtomicResult { .. }
3811            | Expression::RayQueryProceedResult
3812            | Expression::WorkGroupUniformLoadResult { .. }
3813            | Expression::SubgroupOperationResult { .. }
3814            | Expression::SubgroupBallotResult => unreachable!(),
3815            // `ArrayLength` is written as `expr.length()` and we convert it to a uint
3816            Expression::ArrayLength(expr) => {
3817                write!(self.out, "uint(")?;
3818                self.write_expr(expr, ctx)?;
3819                write!(self.out, ".length())")?
3820            }
3821            // not supported yet
3822            Expression::RayQueryGetIntersection { .. }
3823            | Expression::RayQueryVertexPositions { .. }
3824            | Expression::CooperativeLoad { .. }
3825            | Expression::CooperativeMultiplyAdd { .. } => unreachable!(),
3826        }
3827
3828        Ok(())
3829    }
3830
3831    /// Helper function to write the local holding the clamped lod
3832    fn write_clamped_lod(
3833        &mut self,
3834        ctx: &back::FunctionCtx,
3835        expr: Handle<crate::Expression>,
3836        image: Handle<crate::Expression>,
3837        level_expr: Handle<crate::Expression>,
3838    ) -> Result<(), Error> {
3839        // Define our local and start a call to `clamp`
3840        write!(
3841            self.out,
3842            "int {}{} = clamp(",
3843            Baked(expr),
3844            CLAMPED_LOD_SUFFIX
3845        )?;
3846        // Write the lod that will be clamped
3847        self.write_expr(level_expr, ctx)?;
3848        // Set the min value to 0 and start a call to `textureQueryLevels` to get
3849        // the maximum value
3850        write!(self.out, ", 0, textureQueryLevels(")?;
3851        // Write the target image as an argument to `textureQueryLevels`
3852        self.write_expr(image, ctx)?;
3853        // Close the call to `textureQueryLevels` subtract 1 from it since
3854        // the lod argument is 0 based, close the `clamp` call and end the
3855        // local declaration statement.
3856        writeln!(self.out, ") - 1);")?;
3857
3858        Ok(())
3859    }
3860
3861    // Helper method used to retrieve how many elements a coordinate vector
3862    // for the images operations need.
3863    fn get_coordinate_vector_size(&self, dim: crate::ImageDimension, arrayed: bool) -> u8 {
3864        // openGL es doesn't have 1D images so we need workaround it
3865        let tex_1d_hack = dim == crate::ImageDimension::D1 && self.options.version.is_es();
3866        // Get how many components the coordinate vector needs for the dimensions only
3867        let tex_coord_size = match dim {
3868            crate::ImageDimension::D1 => 1,
3869            crate::ImageDimension::D2 => 2,
3870            crate::ImageDimension::D3 => 3,
3871            crate::ImageDimension::Cube => 2,
3872        };
3873        // Calculate the true size of the coordinate vector by adding 1 for arrayed images
3874        // and another 1 if we need to workaround 1D images by making them 2D
3875        tex_coord_size + tex_1d_hack as u8 + arrayed as u8
3876    }
3877
3878    /// Helper method to write the coordinate vector for image operations
3879    fn write_texture_coord(
3880        &mut self,
3881        ctx: &back::FunctionCtx,
3882        vector_size: u8,
3883        coordinate: Handle<crate::Expression>,
3884        array_index: Option<Handle<crate::Expression>>,
3885        // Emulate 1D images as 2D for profiles that don't support it (glsl es)
3886        tex_1d_hack: bool,
3887    ) -> Result<(), Error> {
3888        match array_index {
3889            // If the image needs an array indice we need to add it to the end of our
3890            // coordinate vector, to do so we will use the `ivec(ivec, scalar)`
3891            // constructor notation (NOTE: the inner `ivec` can also be a scalar, this
3892            // is important for 1D arrayed images).
3893            Some(layer_expr) => {
3894                write!(self.out, "ivec{vector_size}(")?;
3895                self.write_expr(coordinate, ctx)?;
3896                write!(self.out, ", ")?;
3897                // If we are replacing sampler1D with sampler2D we also need
3898                // to add another zero to the coordinates vector for the y component
3899                if tex_1d_hack {
3900                    write!(self.out, "0, ")?;
3901                }
3902                self.write_expr(layer_expr, ctx)?;
3903                write!(self.out, ")")?;
3904            }
3905            // Otherwise write just the expression (and the 1D hack if needed)
3906            None => {
3907                let uvec_size = match *ctx.resolve_type(coordinate, &self.module.types) {
3908                    TypeInner::Scalar(crate::Scalar {
3909                        kind: crate::ScalarKind::Uint,
3910                        ..
3911                    }) => Some(None),
3912                    TypeInner::Vector {
3913                        size,
3914                        scalar:
3915                            crate::Scalar {
3916                                kind: crate::ScalarKind::Uint,
3917                                ..
3918                            },
3919                    } => Some(Some(size as u32)),
3920                    _ => None,
3921                };
3922                if tex_1d_hack {
3923                    write!(self.out, "ivec2(")?;
3924                } else if uvec_size.is_some() {
3925                    match uvec_size {
3926                        Some(None) => write!(self.out, "int(")?,
3927                        Some(Some(size)) => write!(self.out, "ivec{size}(")?,
3928                        _ => {}
3929                    }
3930                }
3931                self.write_expr(coordinate, ctx)?;
3932                if tex_1d_hack {
3933                    write!(self.out, ", 0)")?;
3934                } else if uvec_size.is_some() {
3935                    write!(self.out, ")")?;
3936                }
3937            }
3938        }
3939
3940        Ok(())
3941    }
3942
3943    /// Helper method to write the `ImageStore` statement
3944    fn write_image_store(
3945        &mut self,
3946        ctx: &back::FunctionCtx,
3947        image: Handle<crate::Expression>,
3948        coordinate: Handle<crate::Expression>,
3949        array_index: Option<Handle<crate::Expression>>,
3950        value: Handle<crate::Expression>,
3951    ) -> Result<(), Error> {
3952        use crate::ImageDimension as IDim;
3953
3954        // NOTE: openGL requires that `imageStore`s have no effects when the texel is invalid
3955        // so we don't need to generate bounds checks (OpenGL 4.2 Core §3.9.20)
3956
3957        // This will only panic if the module is invalid
3958        let dim = match *ctx.resolve_type(image, &self.module.types) {
3959            TypeInner::Image { dim, .. } => dim,
3960            _ => unreachable!(),
3961        };
3962
3963        // Begin our call to `imageStore`
3964        write!(self.out, "imageStore(")?;
3965        self.write_expr(image, ctx)?;
3966        // Separate the image argument from the coordinates
3967        write!(self.out, ", ")?;
3968
3969        // openGL es doesn't have 1D images so we need workaround it
3970        let tex_1d_hack = dim == IDim::D1 && self.options.version.is_es();
3971        // Write the coordinate vector
3972        self.write_texture_coord(
3973            ctx,
3974            // Get the size of the coordinate vector
3975            self.get_coordinate_vector_size(dim, array_index.is_some()),
3976            coordinate,
3977            array_index,
3978            tex_1d_hack,
3979        )?;
3980
3981        // Separate the coordinate from the value to write and write the expression
3982        // of the value to write.
3983        write!(self.out, ", ")?;
3984        self.write_expr(value, ctx)?;
3985        // End the call to `imageStore` and the statement.
3986        writeln!(self.out, ");")?;
3987
3988        Ok(())
3989    }
3990
3991    /// Helper method to write the `ImageAtomic` statement
3992    fn write_image_atomic(
3993        &mut self,
3994        ctx: &back::FunctionCtx,
3995        image: Handle<crate::Expression>,
3996        coordinate: Handle<crate::Expression>,
3997        array_index: Option<Handle<crate::Expression>>,
3998        fun: crate::AtomicFunction,
3999        value: Handle<crate::Expression>,
4000    ) -> Result<(), Error> {
4001        use crate::ImageDimension as IDim;
4002
4003        // NOTE: openGL requires that `imageAtomic`s have no effects when the texel is invalid
4004        // so we don't need to generate bounds checks (OpenGL 4.2 Core §3.9.20)
4005
4006        // This will only panic if the module is invalid
4007        let dim = match *ctx.resolve_type(image, &self.module.types) {
4008            TypeInner::Image { dim, .. } => dim,
4009            _ => unreachable!(),
4010        };
4011
4012        // Begin our call to `imageAtomic`
4013        let fun_str = fun.to_glsl();
4014        write!(self.out, "imageAtomic{fun_str}(")?;
4015        self.write_expr(image, ctx)?;
4016        // Separate the image argument from the coordinates
4017        write!(self.out, ", ")?;
4018
4019        // openGL es doesn't have 1D images so we need workaround it
4020        let tex_1d_hack = dim == IDim::D1 && self.options.version.is_es();
4021        // Write the coordinate vector
4022        self.write_texture_coord(
4023            ctx,
4024            // Get the size of the coordinate vector
4025            self.get_coordinate_vector_size(dim, false),
4026            coordinate,
4027            array_index,
4028            tex_1d_hack,
4029        )?;
4030
4031        // Separate the coordinate from the value to write and write the expression
4032        // of the value to write.
4033        write!(self.out, ", ")?;
4034        self.write_expr(value, ctx)?;
4035        // End the call to `imageAtomic` and the statement.
4036        writeln!(self.out, ");")?;
4037
4038        Ok(())
4039    }
4040
4041    /// Helper method for writing an `ImageLoad` expression.
4042    #[allow(clippy::too_many_arguments)]
4043    fn write_image_load(
4044        &mut self,
4045        handle: Handle<crate::Expression>,
4046        ctx: &back::FunctionCtx,
4047        image: Handle<crate::Expression>,
4048        coordinate: Handle<crate::Expression>,
4049        array_index: Option<Handle<crate::Expression>>,
4050        sample: Option<Handle<crate::Expression>>,
4051        level: Option<Handle<crate::Expression>>,
4052    ) -> Result<(), Error> {
4053        use crate::ImageDimension as IDim;
4054
4055        // `ImageLoad` is a bit complicated.
4056        // There are two functions one for sampled
4057        // images another for storage images, the former uses `texelFetch` and the
4058        // latter uses `imageLoad`.
4059        //
4060        // Furthermore we have `level` which is always `Some` for sampled images
4061        // and `None` for storage images, so we end up with two functions:
4062        // - `texelFetch(image, coordinate, level)` for sampled images
4063        // - `imageLoad(image, coordinate)` for storage images
4064        //
4065        // Finally we also have to consider bounds checking, for storage images
4066        // this is easy since openGL requires that invalid texels always return
4067        // 0, for sampled images we need to either verify that all arguments are
4068        // in bounds (`ReadZeroSkipWrite`) or make them a valid texel (`Restrict`).
4069
4070        // This will only panic if the module is invalid
4071        let (dim, class) = match *ctx.resolve_type(image, &self.module.types) {
4072            TypeInner::Image {
4073                dim,
4074                arrayed: _,
4075                class,
4076            } => (dim, class),
4077            _ => unreachable!(),
4078        };
4079
4080        // Get the name of the function to be used for the load operation
4081        // and the policy to be used with it.
4082        let (fun_name, policy) = match class {
4083            // Sampled images inherit the policy from the user passed policies
4084            crate::ImageClass::Sampled { .. } => ("texelFetch", self.policies.image_load),
4085            crate::ImageClass::Storage { .. } => {
4086                // OpenGL ES 3.1 mentions in Chapter "8.22 Texture Image Loads and Stores" that:
4087                // "Invalid image loads will return a vector where the value of R, G, and B components
4088                // is 0 and the value of the A component is undefined."
4089                //
4090                // OpenGL 4.2 Core mentions in Chapter "3.9.20 Texture Image Loads and Stores" that:
4091                // "Invalid image loads will return zero."
4092                //
4093                // So, we only inject bounds checks for ES
4094                let policy = if self.options.version.is_es() {
4095                    self.policies.image_load
4096                } else {
4097                    proc::BoundsCheckPolicy::Unchecked
4098                };
4099                ("imageLoad", policy)
4100            }
4101            // TODO: Is there even a function for this?
4102            crate::ImageClass::Depth { multi: _ } => {
4103                return Err(Error::Custom(
4104                    "WGSL `textureLoad` from depth textures is not supported in GLSL".to_string(),
4105                ))
4106            }
4107            crate::ImageClass::External => unimplemented!(),
4108        };
4109
4110        // openGL es doesn't have 1D images so we need workaround it
4111        let tex_1d_hack = dim == IDim::D1 && self.options.version.is_es();
4112        // Get the size of the coordinate vector
4113        let vector_size = self.get_coordinate_vector_size(dim, array_index.is_some());
4114
4115        if let proc::BoundsCheckPolicy::ReadZeroSkipWrite = policy {
4116            // To write the bounds checks for `ReadZeroSkipWrite` we will use a
4117            // ternary operator since we are in the middle of an expression and
4118            // need to return a value.
4119            //
4120            // NOTE: glsl does short circuit when evaluating logical
4121            // expressions so we can be sure that after we test a
4122            // condition it will be true for the next ones
4123
4124            // Write parentheses around the ternary operator to prevent problems with
4125            // expressions emitted before or after it having more precedence
4126            write!(self.out, "(",)?;
4127
4128            // The lod check needs to precede the size check since we need
4129            // to use the lod to get the size of the image at that level.
4130            if let Some(level_expr) = level {
4131                self.write_expr(level_expr, ctx)?;
4132                write!(self.out, " < textureQueryLevels(",)?;
4133                self.write_expr(image, ctx)?;
4134                // Chain the next check
4135                write!(self.out, ") && ")?;
4136            }
4137
4138            // Check that the sample arguments doesn't exceed the number of samples
4139            if let Some(sample_expr) = sample {
4140                self.write_expr(sample_expr, ctx)?;
4141                write!(self.out, " < textureSamples(",)?;
4142                self.write_expr(image, ctx)?;
4143                // Chain the next check
4144                write!(self.out, ") && ")?;
4145            }
4146
4147            // We now need to write the size checks for the coordinates and array index
4148            // first we write the comparison function in case the image is 1D non arrayed
4149            // (and no 1D to 2D hack was needed) we are comparing scalars so the less than
4150            // operator will suffice, but otherwise we'll be comparing two vectors so we'll
4151            // need to use the `lessThan` function but it returns a vector of booleans (one
4152            // for each comparison) so we need to fold it all in one scalar boolean, since
4153            // we want all comparisons to pass we use the `all` function which will only
4154            // return `true` if all the elements of the boolean vector are also `true`.
4155            //
4156            // So we'll end with one of the following forms
4157            // - `coord < textureSize(image, lod)` for 1D images
4158            // - `all(lessThan(coord, textureSize(image, lod)))` for normal images
4159            // - `all(lessThan(ivec(coord, array_index), textureSize(image, lod)))`
4160            //    for arrayed images
4161            // - `all(lessThan(coord, textureSize(image)))` for multi sampled images
4162
4163            if vector_size != 1 {
4164                write!(self.out, "all(lessThan(")?;
4165            }
4166
4167            // Write the coordinate vector
4168            self.write_texture_coord(ctx, vector_size, coordinate, array_index, tex_1d_hack)?;
4169
4170            if vector_size != 1 {
4171                // If we used the `lessThan` function we need to separate the
4172                // coordinates from the image size.
4173                write!(self.out, ", ")?;
4174            } else {
4175                // If we didn't use it (ie. 1D images) we perform the comparison
4176                // using the less than operator.
4177                write!(self.out, " < ")?;
4178            }
4179
4180            // Call `textureSize` to get our image size
4181            write!(self.out, "textureSize(")?;
4182            self.write_expr(image, ctx)?;
4183            // `textureSize` uses the lod as a second argument for mipmapped images
4184            if let Some(level_expr) = level {
4185                // Separate the image from the lod
4186                write!(self.out, ", ")?;
4187                self.write_expr(level_expr, ctx)?;
4188            }
4189            // Close the `textureSize` call
4190            write!(self.out, ")")?;
4191
4192            if vector_size != 1 {
4193                // Close the `all` and `lessThan` calls
4194                write!(self.out, "))")?;
4195            }
4196
4197            // Finally end the condition part of the ternary operator
4198            write!(self.out, " ? ")?;
4199        }
4200
4201        // Begin the call to the function used to load the texel
4202        write!(self.out, "{fun_name}(")?;
4203        self.write_expr(image, ctx)?;
4204        write!(self.out, ", ")?;
4205
4206        // If we are using `Restrict` bounds checking we need to pass valid texel
4207        // coordinates, to do so we use the `clamp` function to get a value between
4208        // 0 and the image size - 1 (indexing begins at 0)
4209        if let proc::BoundsCheckPolicy::Restrict = policy {
4210            write!(self.out, "clamp(")?;
4211        }
4212
4213        // Write the coordinate vector
4214        self.write_texture_coord(ctx, vector_size, coordinate, array_index, tex_1d_hack)?;
4215
4216        // If we are using `Restrict` bounds checking we need to write the rest of the
4217        // clamp we initiated before writing the coordinates.
4218        if let proc::BoundsCheckPolicy::Restrict = policy {
4219            // Write the min value 0
4220            if vector_size == 1 {
4221                write!(self.out, ", 0")?;
4222            } else {
4223                write!(self.out, ", ivec{vector_size}(0)")?;
4224            }
4225            // Start the `textureSize` call to use as the max value.
4226            write!(self.out, ", textureSize(")?;
4227            self.write_expr(image, ctx)?;
4228            // If the image is mipmapped we need to add the lod argument to the
4229            // `textureSize` call, but this needs to be the clamped lod, this should
4230            // have been generated earlier and put in a local.
4231            if class.is_mipmapped() {
4232                write!(self.out, ", {}{}", Baked(handle), CLAMPED_LOD_SUFFIX)?;
4233            }
4234            // Close the `textureSize` call
4235            write!(self.out, ")")?;
4236
4237            // Subtract 1 from the `textureSize` call since the coordinates are zero based.
4238            if vector_size == 1 {
4239                write!(self.out, " - 1")?;
4240            } else {
4241                write!(self.out, " - ivec{vector_size}(1)")?;
4242            }
4243
4244            // Close the `clamp` call
4245            write!(self.out, ")")?;
4246
4247            // Add the clamped lod (if present) as the second argument to the
4248            // image load function.
4249            if level.is_some() {
4250                write!(self.out, ", {}{}", Baked(handle), CLAMPED_LOD_SUFFIX)?;
4251            }
4252
4253            // If a sample argument is needed we need to clamp it between 0 and
4254            // the number of samples the image has.
4255            if let Some(sample_expr) = sample {
4256                write!(self.out, ", clamp(")?;
4257                self.write_expr(sample_expr, ctx)?;
4258                // Set the min value to 0 and start the call to `textureSamples`
4259                write!(self.out, ", 0, textureSamples(")?;
4260                self.write_expr(image, ctx)?;
4261                // Close the `textureSamples` call, subtract 1 from it since the sample
4262                // argument is zero based, and close the `clamp` call
4263                writeln!(self.out, ") - 1)")?;
4264            }
4265        } else if let Some(sample_or_level) = sample.or(level) {
4266            // GLSL only support SInt on this field while WGSL support also UInt
4267            let cast_to_int = matches!(
4268                *ctx.resolve_type(sample_or_level, &self.module.types),
4269                TypeInner::Scalar(crate::Scalar {
4270                    kind: crate::ScalarKind::Uint,
4271                    ..
4272                })
4273            );
4274
4275            // If no bounds checking is need just add the sample or level argument
4276            // after the coordinates
4277            write!(self.out, ", ")?;
4278
4279            if cast_to_int {
4280                write!(self.out, "int(")?;
4281            }
4282
4283            self.write_expr(sample_or_level, ctx)?;
4284
4285            if cast_to_int {
4286                write!(self.out, ")")?;
4287            }
4288        }
4289
4290        // Close the image load function.
4291        write!(self.out, ")")?;
4292
4293        // If we were using the `ReadZeroSkipWrite` policy we need to end the first branch
4294        // (which is taken if the condition is `true`) with a colon (`:`) and write the
4295        // second branch which is just a 0 value.
4296        if let proc::BoundsCheckPolicy::ReadZeroSkipWrite = policy {
4297            // Get the kind of the output value.
4298            let kind = match class {
4299                // Only sampled images can reach here since storage images
4300                // don't need bounds checks and depth images aren't implemented
4301                crate::ImageClass::Sampled { kind, .. } => kind,
4302                _ => unreachable!(),
4303            };
4304
4305            // End the first branch
4306            write!(self.out, " : ")?;
4307            // Write the 0 value
4308            write!(
4309                self.out,
4310                "{}vec4(",
4311                glsl_scalar(crate::Scalar { kind, width: 4 })?.prefix,
4312            )?;
4313            self.write_zero_init_scalar(kind)?;
4314            // Close the zero value constructor
4315            write!(self.out, ")")?;
4316            // Close the parentheses surrounding our ternary
4317            write!(self.out, ")")?;
4318        }
4319
4320        Ok(())
4321    }
4322
4323    fn write_named_expr(
4324        &mut self,
4325        handle: Handle<crate::Expression>,
4326        name: String,
4327        // The expression which is being named.
4328        // Generally, this is the same as handle, except in WorkGroupUniformLoad
4329        named: Handle<crate::Expression>,
4330        ctx: &back::FunctionCtx,
4331    ) -> BackendResult {
4332        match ctx.info[named].ty {
4333            proc::TypeResolution::Handle(ty_handle) => match self.module.types[ty_handle].inner {
4334                TypeInner::Struct { .. } => {
4335                    let ty_name = &self.names[&NameKey::Type(ty_handle)];
4336                    write!(self.out, "{ty_name}")?;
4337                }
4338                _ => {
4339                    self.write_type(ty_handle)?;
4340                }
4341            },
4342            proc::TypeResolution::Value(ref inner) => {
4343                self.write_value_type(inner)?;
4344            }
4345        }
4346
4347        let resolved = ctx.resolve_type(named, &self.module.types);
4348
4349        write!(self.out, " {name}")?;
4350        if let TypeInner::Array { base, size, .. } = *resolved {
4351            self.write_array_size(base, size)?;
4352        }
4353        write!(self.out, " = ")?;
4354        self.write_expr(handle, ctx)?;
4355        writeln!(self.out, ";")?;
4356        self.named_expressions.insert(named, name);
4357
4358        Ok(())
4359    }
4360
4361    /// Helper function that write string with default zero initialization for supported types
4362    fn write_zero_init_value(&mut self, ty: Handle<crate::Type>) -> BackendResult {
4363        let inner = &self.module.types[ty].inner;
4364        match *inner {
4365            TypeInner::Scalar(scalar) | TypeInner::Atomic(scalar) => {
4366                self.write_zero_init_scalar(scalar.kind)?;
4367            }
4368            TypeInner::Vector { scalar, .. } => {
4369                self.write_value_type(inner)?;
4370                write!(self.out, "(")?;
4371                self.write_zero_init_scalar(scalar.kind)?;
4372                write!(self.out, ")")?;
4373            }
4374            TypeInner::Matrix { .. } => {
4375                self.write_value_type(inner)?;
4376                write!(self.out, "(")?;
4377                self.write_zero_init_scalar(crate::ScalarKind::Float)?;
4378                write!(self.out, ")")?;
4379            }
4380            TypeInner::Array { base, size, .. } => {
4381                let count = match size.resolve(self.module.to_ctx())? {
4382                    proc::IndexableLength::Known(count) => count,
4383                    proc::IndexableLength::Dynamic => return Ok(()),
4384                };
4385                self.write_type(base)?;
4386                self.write_array_size(base, size)?;
4387                write!(self.out, "(")?;
4388                for _ in 1..count {
4389                    self.write_zero_init_value(base)?;
4390                    write!(self.out, ", ")?;
4391                }
4392                // write last parameter without comma and space
4393                self.write_zero_init_value(base)?;
4394                write!(self.out, ")")?;
4395            }
4396            TypeInner::Struct { ref members, .. } => {
4397                let name = &self.names[&NameKey::Type(ty)];
4398                write!(self.out, "{name}(")?;
4399                for (index, member) in members.iter().enumerate() {
4400                    if index != 0 {
4401                        write!(self.out, ", ")?;
4402                    }
4403                    self.write_zero_init_value(member.ty)?;
4404                }
4405                write!(self.out, ")")?;
4406            }
4407            _ => unreachable!(),
4408        }
4409
4410        Ok(())
4411    }
4412
4413    /// Helper function that write string with zero initialization for scalar
4414    fn write_zero_init_scalar(&mut self, kind: crate::ScalarKind) -> BackendResult {
4415        match kind {
4416            crate::ScalarKind::Bool => write!(self.out, "false")?,
4417            crate::ScalarKind::Uint => write!(self.out, "0u")?,
4418            crate::ScalarKind::Float => write!(self.out, "0.0")?,
4419            crate::ScalarKind::Sint => write!(self.out, "0")?,
4420            crate::ScalarKind::AbstractInt | crate::ScalarKind::AbstractFloat => {
4421                return Err(Error::Custom(
4422                    "Abstract types should not appear in IR presented to backends".to_string(),
4423                ))
4424            }
4425        }
4426
4427        Ok(())
4428    }
4429
4430    /// Issue a control barrier.
4431    fn write_control_barrier(
4432        &mut self,
4433        flags: crate::Barrier,
4434        level: back::Level,
4435    ) -> BackendResult {
4436        self.write_memory_barrier(flags, level)?;
4437        writeln!(self.out, "{level}barrier();")?;
4438        Ok(())
4439    }
4440
4441    /// Issue a memory barrier.
4442    fn write_memory_barrier(&mut self, flags: crate::Barrier, level: back::Level) -> BackendResult {
4443        if flags.contains(crate::Barrier::STORAGE) {
4444            writeln!(self.out, "{level}memoryBarrierBuffer();")?;
4445        }
4446        if flags.contains(crate::Barrier::WORK_GROUP) {
4447            writeln!(self.out, "{level}memoryBarrierShared();")?;
4448        }
4449        if flags.contains(crate::Barrier::SUB_GROUP) {
4450            writeln!(self.out, "{level}subgroupMemoryBarrier();")?;
4451        }
4452        if flags.contains(crate::Barrier::TEXTURE) {
4453            writeln!(self.out, "{level}memoryBarrierImage();")?;
4454        }
4455        Ok(())
4456    }
4457
4458    /// Helper function that return the glsl storage access string of [`StorageAccess`](crate::StorageAccess)
4459    ///
4460    /// glsl allows adding both `readonly` and `writeonly` but this means that
4461    /// they can only be used to query information about the resource which isn't what
4462    /// we want here so when storage access is both `LOAD` and `STORE` add no modifiers
4463    fn write_storage_access(&mut self, storage_access: crate::StorageAccess) -> BackendResult {
4464        if storage_access.contains(crate::StorageAccess::ATOMIC) {
4465            return Ok(());
4466        }
4467        if !storage_access.contains(crate::StorageAccess::STORE) {
4468            write!(self.out, "readonly ")?;
4469        }
4470        if !storage_access.contains(crate::StorageAccess::LOAD) {
4471            write!(self.out, "writeonly ")?;
4472        }
4473        Ok(())
4474    }
4475
4476    /// Helper method used to produce the reflection info that's returned to the user
4477    fn collect_reflection_info(&mut self) -> Result<ReflectionInfo, Error> {
4478        let info = self.info.get_entry_point(self.entry_point_idx as usize);
4479        let mut texture_mapping = crate::FastHashMap::default();
4480        let mut uniforms = crate::FastHashMap::default();
4481
4482        for sampling in info.sampling_set.iter() {
4483            let tex_name = self.reflection_names_globals[&sampling.image].clone();
4484
4485            match texture_mapping.entry(tex_name) {
4486                hash_map::Entry::Vacant(v) => {
4487                    v.insert(TextureMapping {
4488                        texture: sampling.image,
4489                        sampler: Some(sampling.sampler),
4490                    });
4491                }
4492                hash_map::Entry::Occupied(e) => {
4493                    if e.get().sampler != Some(sampling.sampler) {
4494                        log::error!("Conflicting samplers for {}", e.key());
4495                        return Err(Error::ImageMultipleSamplers);
4496                    }
4497                }
4498            }
4499        }
4500
4501        let mut immediates_info = None;
4502        for (handle, var) in self.module.global_variables.iter() {
4503            if info[handle].is_empty() {
4504                continue;
4505            }
4506            match self.module.types[var.ty].inner {
4507                TypeInner::Image { .. } => {
4508                    let tex_name = self.reflection_names_globals[&handle].clone();
4509                    match texture_mapping.entry(tex_name) {
4510                        hash_map::Entry::Vacant(v) => {
4511                            v.insert(TextureMapping {
4512                                texture: handle,
4513                                sampler: None,
4514                            });
4515                        }
4516                        hash_map::Entry::Occupied(_) => {
4517                            // already used with a sampler, do nothing
4518                        }
4519                    }
4520                }
4521                _ => match var.space {
4522                    crate::AddressSpace::Uniform | crate::AddressSpace::Storage { .. } => {
4523                        let name = self.reflection_names_globals[&handle].clone();
4524                        uniforms.insert(handle, name);
4525                    }
4526                    crate::AddressSpace::Immediate => {
4527                        let name = self.reflection_names_globals[&handle].clone();
4528                        immediates_info = Some((name, var.ty));
4529                    }
4530                    _ => (),
4531                },
4532            }
4533        }
4534
4535        let mut immediates_segments = Vec::new();
4536        let mut immediates_items = vec![];
4537
4538        if let Some((name, ty)) = immediates_info {
4539            // We don't have a layouter available to us, so we need to create one.
4540            //
4541            // This is potentially a bit wasteful, but the set of types in the program
4542            // shouldn't be too large.
4543            let mut layouter = proc::Layouter::default();
4544            layouter.update(self.module.to_ctx()).unwrap();
4545
4546            // We start with the name of the binding itself.
4547            immediates_segments.push(name);
4548
4549            // We then recursively collect all the uniform fields of the immediate data.
4550            self.collect_immediates_items(
4551                ty,
4552                &mut immediates_segments,
4553                &layouter,
4554                &mut 0,
4555                &mut immediates_items,
4556            );
4557        }
4558
4559        Ok(ReflectionInfo {
4560            texture_mapping,
4561            uniforms,
4562            varying: mem::take(&mut self.varying),
4563            immediates_items,
4564            clip_distance_count: self.clip_distance_count,
4565        })
4566    }
4567
4568    fn collect_immediates_items(
4569        &mut self,
4570        ty: Handle<crate::Type>,
4571        segments: &mut Vec<String>,
4572        layouter: &proc::Layouter,
4573        offset: &mut u32,
4574        items: &mut Vec<ImmediateItem>,
4575    ) {
4576        // At this point in the recursion, `segments` contains the path
4577        // needed to access `ty` from the root.
4578
4579        let layout = &layouter[ty];
4580        *offset = layout.alignment.round_up(*offset);
4581        match self.module.types[ty].inner {
4582            // All these types map directly to GL uniforms.
4583            TypeInner::Scalar { .. } | TypeInner::Vector { .. } | TypeInner::Matrix { .. } => {
4584                // Build the full name, by combining all current segments.
4585                let name: String = segments.iter().map(String::as_str).collect();
4586                items.push(ImmediateItem {
4587                    access_path: name,
4588                    offset: *offset,
4589                    ty,
4590                });
4591                *offset += layout.size;
4592            }
4593            // Arrays are recursed into.
4594            TypeInner::Array { base, size, .. } => {
4595                let crate::ArraySize::Constant(count) = size else {
4596                    unreachable!("Cannot have dynamic arrays in immediates");
4597                };
4598
4599                for i in 0..count.get() {
4600                    // Add the array accessor and recurse.
4601                    segments.push(format!("[{i}]"));
4602                    self.collect_immediates_items(base, segments, layouter, offset, items);
4603                    segments.pop();
4604                }
4605
4606                // Ensure the stride is kept by rounding up to the alignment.
4607                *offset = layout.alignment.round_up(*offset)
4608            }
4609            TypeInner::Struct { ref members, .. } => {
4610                for (index, member) in members.iter().enumerate() {
4611                    // Add struct accessor and recurse.
4612                    segments.push(format!(
4613                        ".{}",
4614                        self.names[&NameKey::StructMember(ty, index as u32)]
4615                    ));
4616                    self.collect_immediates_items(member.ty, segments, layouter, offset, items);
4617                    segments.pop();
4618                }
4619
4620                // Ensure ending padding is kept by rounding up to the alignment.
4621                *offset = layout.alignment.round_up(*offset)
4622            }
4623            _ => unreachable!(),
4624        }
4625    }
4626}