Skip to main content

naga/back/hlsl/
writer.rs

1use alloc::{
2    format,
3    string::{String, ToString},
4    vec::Vec,
5};
6use core::{
7    fmt::{self, Write as _},
8    mem,
9};
10
11use super::{
12    help,
13    help::{
14        WrappedArrayLength, WrappedConstructor, WrappedImageQuery, WrappedStructMatrixAccess,
15        WrappedZeroValue,
16    },
17    storage::StoreValue,
18    BackendResult, Error, FragmentEntryPoint, Options, PipelineOptions, ShaderModel,
19};
20use crate::{
21    back::{self, get_entry_points, Baked},
22    common,
23    proc::{self, index, ExternalTextureNameKey, NameKey},
24    valid, Handle, Module, RayQueryFunction, Scalar, ScalarKind, ShaderStage, TypeInner,
25};
26
27const LOCATION_SEMANTIC: &str = "LOC";
28const SPECIAL_CBUF_TYPE: &str = "NagaConstants";
29const SPECIAL_CBUF_VAR: &str = "_NagaConstants";
30const SPECIAL_FIRST_VERTEX: &str = "first_vertex";
31const SPECIAL_FIRST_INSTANCE: &str = "first_instance";
32const SPECIAL_OTHER: &str = "other";
33
34pub(crate) const MODF_FUNCTION: &str = "naga_modf";
35pub(crate) const FREXP_FUNCTION: &str = "naga_frexp";
36pub(crate) const EXTRACT_BITS_FUNCTION: &str = "naga_extractBits";
37pub(crate) const INSERT_BITS_FUNCTION: &str = "naga_insertBits";
38pub(crate) const SAMPLER_HEAP_VAR: &str = "nagaSamplerHeap";
39pub(crate) const COMPARISON_SAMPLER_HEAP_VAR: &str = "nagaComparisonSamplerHeap";
40pub(crate) const SAMPLE_EXTERNAL_TEXTURE_FUNCTION: &str = "nagaSampleExternalTexture";
41pub(crate) const ABS_FUNCTION: &str = "naga_abs";
42pub(crate) const DIV_FUNCTION: &str = "naga_div";
43pub(crate) const MOD_FUNCTION: &str = "naga_mod";
44pub(crate) const NEG_FUNCTION: &str = "naga_neg";
45pub(crate) const F2I32_FUNCTION: &str = "naga_f2i32";
46pub(crate) const F2U32_FUNCTION: &str = "naga_f2u32";
47pub(crate) const F2I64_FUNCTION: &str = "naga_f2i64";
48pub(crate) const F2U64_FUNCTION: &str = "naga_f2u64";
49pub(crate) const IMAGE_SAMPLE_BASE_CLAMP_TO_EDGE_FUNCTION: &str =
50    "nagaTextureSampleBaseClampToEdge";
51pub(crate) const IMAGE_LOAD_EXTERNAL_FUNCTION: &str = "nagaTextureLoadExternal";
52pub(crate) const RAY_QUERY_TRACKER_VARIABLE_PREFIX: &str = "naga_query_init_tracker_for_";
53/// Prefix for variables in a naga statement
54pub(crate) const INTERNAL_PREFIX: &str = "naga_";
55
56enum Index {
57    Expression(Handle<crate::Expression>),
58    Static(u32),
59}
60
61pub(super) struct EpStructMember {
62    pub(super) name: String,
63    pub(super) ty: Handle<crate::Type>,
64    // technically, this should always be `Some`
65    // (we `debug_assert!` this in `write_interface_struct`)
66    pub(super) binding: Option<crate::Binding>,
67    pub(super) index: u32,
68}
69
70/// Structure contains information required for generating
71/// wrapped structure of all entry points arguments
72pub(super) struct EntryPointBinding {
73    /// Name of the fake EP argument that contains the struct
74    /// with all the flattened input data.
75    pub(super) arg_name: String,
76    /// Generated structure name
77    pub(super) ty_name: String,
78    /// Members of generated structure
79    pub(super) members: Vec<EpStructMember>,
80    pub(super) local_invocation_index_name: Option<String>,
81}
82
83pub(super) struct EntryPointInterface {
84    /// If `Some`, the input of an entry point is gathered in a special
85    /// struct with members sorted by binding.
86    /// The `EntryPointBinding::members` array is sorted by index,
87    /// so that we can walk it in `write_ep_arguments_initialization`.
88    pub(crate) input: Option<EntryPointBinding>,
89    /// If `Some`, the output of an entry point is flattened.
90    /// The `EntryPointBinding::members` array is sorted by binding,
91    /// So that we can walk it in `Statement::Return` handler.
92    pub(crate) output: Option<EntryPointBinding>,
93    pub(crate) mesh_vertices: Option<EntryPointBinding>,
94    pub(crate) mesh_primitives: Option<EntryPointBinding>,
95    pub(crate) mesh_indices: Option<EntryPointBinding>,
96}
97
98#[derive(Clone, Eq, PartialEq, PartialOrd, Ord)]
99enum InterfaceKey {
100    Location(u32),
101    BuiltIn(crate::BuiltIn),
102    Other,
103}
104
105impl InterfaceKey {
106    const fn new(binding: Option<&crate::Binding>) -> Self {
107        match binding {
108            Some(&crate::Binding::Location { location, .. }) => Self::Location(location),
109            Some(&crate::Binding::BuiltIn(built_in)) => Self::BuiltIn(built_in),
110            None => Self::Other,
111        }
112    }
113}
114
115#[derive(Copy, Clone, PartialEq)]
116pub(super) enum Io {
117    Input,
118    Output,
119    MeshVertices,
120    MeshPrimitives,
121}
122
123/// Argument list for nested entry points
124pub(super) struct NestedEntryPointArgs {
125    /// Arguments literally declared by the user
126    pub user_args: Vec<String>,
127    pub task_payload: Option<String>,
128    pub local_invocation_index: String,
129}
130
131const fn is_subgroup_builtin_binding(binding: &Option<crate::Binding>) -> bool {
132    let &Some(crate::Binding::BuiltIn(builtin)) = binding else {
133        return false;
134    };
135    matches!(
136        builtin,
137        crate::BuiltIn::SubgroupSize
138            | crate::BuiltIn::SubgroupInvocationId
139            | crate::BuiltIn::NumSubgroups
140            | crate::BuiltIn::SubgroupId
141    )
142}
143
144/// The invocations that share the zero initialization of workgroup variables.
145#[derive(Clone, Copy)]
146struct WorkgroupInitInvocations<'a> {
147    /// Name of the local invocation index.
148    local_index: &'a str,
149    /// Number of invocations in the workgroup.
150    workgroup_size: u32,
151}
152
153/// Information for how to generate a `binding_array<sampler>` access.
154struct BindingArraySamplerInfo {
155    /// Variable name of the sampler heap
156    sampler_heap_name: &'static str,
157    /// Variable name of the sampler index buffer
158    sampler_index_buffer_name: String,
159    /// Variable name of the base index _into_ the sampler index buffer
160    binding_array_base_index_name: String,
161}
162
163impl<'a, W: fmt::Write> super::Writer<'a, W> {
164    pub fn new(out: W, options: &'a Options, pipeline_options: &'a PipelineOptions) -> Self {
165        Self {
166            out,
167            names: crate::FastHashMap::default(),
168            namer: proc::Namer::default(),
169            options,
170            pipeline_options,
171            entry_point_io: crate::FastHashMap::default(),
172            early_depth_test: None,
173            named_expressions: crate::NamedExpressions::default(),
174            wrapped: super::Wrapped::default(),
175            written_committed_intersection: false,
176            written_candidate_intersection: false,
177            continue_ctx: back::continue_forward::ContinueCtx::default(),
178            temp_access_chain: Vec::new(),
179            need_bake_expressions: Default::default(),
180            function_task_payload_var: Default::default(),
181            immediate_raw_names: crate::FastHashMap::default(),
182        }
183    }
184
185    fn reset(&mut self, module: &Module) {
186        self.names.clear();
187        self.namer.reset(
188            module,
189            &super::keywords::RESERVED_SET,
190            proc::KeywordSet::empty(),
191            &super::keywords::RESERVED_CASE_INSENSITIVE_SET,
192            super::keywords::RESERVED_PREFIXES,
193            &mut self.names,
194        );
195        self.entry_point_io.clear();
196        self.named_expressions.clear();
197        self.wrapped.clear();
198        self.written_committed_intersection = false;
199        self.written_candidate_intersection = false;
200        self.continue_ctx.clear();
201        self.need_bake_expressions.clear();
202        self.function_task_payload_var.clear();
203        self.immediate_raw_names.clear();
204    }
205
206    /// Generates statements to be inserted immediately before and at the very
207    /// start of the body of each loop, to defeat infinite loop reasoning.
208    /// The 0th item of the returned tuple should be inserted immediately prior
209    /// to the loop and the 1st item should be inserted at the very start of
210    /// the loop body.
211    ///
212    /// See [`back::msl::Writer::gen_force_bounded_loop_statements`] for details.
213    fn gen_force_bounded_loop_statements(
214        &mut self,
215        level: back::Level,
216    ) -> Option<(String, String)> {
217        if !self.options.force_loop_bounding {
218            return None;
219        }
220
221        let loop_bound_name = self.namer.call("loop_bound");
222        let max = u32::MAX;
223        // Count down from u32::MAX rather than up from 0 to avoid hang on
224        // certain Intel drivers. See <https://github.com/gfx-rs/wgpu/issues/7319>.
225        let decl = format!("{level}uint2 {loop_bound_name} = uint2({max}u, {max}u);");
226        let level = level.next();
227        let break_and_inc = format!(
228            "{level}if (all({loop_bound_name} == uint2(0u, 0u))) {{ break; }}
229{level}{loop_bound_name} -= uint2({loop_bound_name}.y == 0u, 1u);"
230        );
231
232        Some((decl, break_and_inc))
233    }
234
235    /// Helper method used to find which expressions of a given function require baking
236    ///
237    /// # Notes
238    /// Clears `need_bake_expressions` set before adding to it
239    fn update_expressions_to_bake(
240        &mut self,
241        module: &Module,
242        func: &crate::Function,
243        info: &valid::FunctionInfo,
244    ) {
245        use crate::Expression;
246        self.need_bake_expressions.clear();
247        for (exp_handle, expr) in func.expressions.iter() {
248            let expr_info = &info[exp_handle];
249            let min_ref_count = func.expressions[exp_handle].bake_ref_count();
250            if min_ref_count <= expr_info.ref_count {
251                self.need_bake_expressions.insert(exp_handle);
252            }
253            if let Expression::Load { pointer } = *expr {
254                if info[pointer]
255                    .ty
256                    .inner_with(&module.types)
257                    .is_atomic_pointer(&module.types)
258                {
259                    self.need_bake_expressions.insert(exp_handle);
260                }
261            }
262
263            if let Expression::Math { fun, arg, arg1, .. } = *expr {
264                match fun {
265                    crate::MathFunction::Asinh
266                    | crate::MathFunction::Acosh
267                    | crate::MathFunction::Atanh
268                    | crate::MathFunction::Unpack2x16float
269                    | crate::MathFunction::Unpack2x16snorm
270                    | crate::MathFunction::Unpack2x16unorm
271                    | crate::MathFunction::Unpack4x8snorm
272                    | crate::MathFunction::Unpack4x8unorm
273                    | crate::MathFunction::Unpack4xI8
274                    | crate::MathFunction::Unpack4xU8
275                    | crate::MathFunction::Pack2x16float
276                    | crate::MathFunction::Pack2x16snorm
277                    | crate::MathFunction::Pack2x16unorm
278                    | crate::MathFunction::Pack4x8snorm
279                    | crate::MathFunction::Pack4x8unorm
280                    | crate::MathFunction::Pack4xI8
281                    | crate::MathFunction::Pack4xU8
282                    | crate::MathFunction::Pack4xI8Clamp
283                    | crate::MathFunction::Pack4xU8Clamp => {
284                        self.need_bake_expressions.insert(arg);
285                    }
286                    crate::MathFunction::CountLeadingZeros => {
287                        let inner = info[exp_handle].ty.inner_with(&module.types);
288                        if let Some(ScalarKind::Sint) = inner.scalar_kind() {
289                            self.need_bake_expressions.insert(arg);
290                        }
291                    }
292                    crate::MathFunction::Dot4U8Packed | crate::MathFunction::Dot4I8Packed => {
293                        self.need_bake_expressions.insert(arg);
294                        self.need_bake_expressions.insert(arg1.unwrap());
295                    }
296                    _ => {}
297                }
298            }
299
300            if let Expression::Derivative { axis, ctrl, expr } = *expr {
301                use crate::{DerivativeAxis as Axis, DerivativeControl as Ctrl};
302                if axis == Axis::Width && (ctrl == Ctrl::Coarse || ctrl == Ctrl::Fine) {
303                    self.need_bake_expressions.insert(expr);
304                }
305            }
306
307            if let Expression::GlobalVariable(_) = *expr {
308                let inner = info[exp_handle].ty.inner_with(&module.types);
309
310                if let TypeInner::Sampler { .. } = *inner {
311                    self.need_bake_expressions.insert(exp_handle);
312                }
313            }
314        }
315        for statement in func.body.iter() {
316            match *statement {
317                crate::Statement::SubgroupCollectiveOperation {
318                    op: _,
319                    collective_op: crate::CollectiveOperation::InclusiveScan,
320                    argument,
321                    result: _,
322                } => {
323                    self.need_bake_expressions.insert(argument);
324                }
325                crate::Statement::Atomic {
326                    fun: crate::AtomicFunction::Exchange { compare: Some(cmp) },
327                    ..
328                } => {
329                    self.need_bake_expressions.insert(cmp);
330                }
331                _ => {}
332            }
333        }
334    }
335
336    pub fn write(
337        &mut self,
338        module: &Module,
339        module_info: &valid::ModuleInfo,
340        fragment_entry_point: Option<&FragmentEntryPoint<'_>>,
341    ) -> Result<super::ReflectionInfo, Error> {
342        self.reset(module);
343
344        if module.uses_mesh_shaders() && self.options.shader_model < ShaderModel::V6_5 {
345            return Err(Error::ShaderModelTooLow(
346                "mesh shaders".to_string(),
347                ShaderModel::V6_5,
348            ));
349        }
350
351        // Write special constants, if needed
352        if let Some(ref bt) = self.options.special_constants_binding {
353            writeln!(self.out, "struct {SPECIAL_CBUF_TYPE} {{")?;
354            writeln!(self.out, "{}int {};", back::INDENT, SPECIAL_FIRST_VERTEX)?;
355            writeln!(self.out, "{}int {};", back::INDENT, SPECIAL_FIRST_INSTANCE)?;
356            writeln!(self.out, "{}uint {};", back::INDENT, SPECIAL_OTHER)?;
357            writeln!(self.out, "}};")?;
358            write!(
359                self.out,
360                "ConstantBuffer<{}> {}: register(b{}",
361                SPECIAL_CBUF_TYPE, SPECIAL_CBUF_VAR, bt.register
362            )?;
363            if bt.space != 0 {
364                write!(self.out, ", space{}", bt.space)?;
365            }
366            writeln!(self.out, ");")?;
367
368            // Extra newline for readability
369            writeln!(self.out)?;
370        }
371
372        for (group, bt) in self.options.dynamic_storage_buffer_offsets_targets.iter() {
373            writeln!(self.out, "struct __dynamic_buffer_offsetsTy{group} {{")?;
374            for i in 0..bt.size {
375                writeln!(self.out, "{}uint _{};", back::INDENT, i)?;
376            }
377            writeln!(self.out, "}};")?;
378            writeln!(
379                self.out,
380                "ConstantBuffer<__dynamic_buffer_offsetsTy{}> __dynamic_buffer_offsets{}: register(b{}, space{});",
381                group, group, bt.register, bt.space
382            )?;
383
384            // Extra newline for readability
385            writeln!(self.out)?;
386        }
387
388        let ep_range = get_entry_points(module, self.pipeline_options.entry_point.as_ref())
389            .map_err(|(stage, name)| Error::EntryPointNotFound(stage, name))?;
390
391        // Save all entry point output types
392        let ep_results = module
393            .entry_points
394            .iter()
395            .map(|ep| (ep.stage, ep.function.result.clone()))
396            .collect::<Vec<(ShaderStage, Option<crate::FunctionResult>)>>();
397
398        self.write_all_mat_cx2_typedefs_and_functions(module)?;
399
400        // Write all structs
401        for (handle, ty) in module.types.iter() {
402            if let TypeInner::Struct { ref members, span } = ty.inner {
403                if module.types[members.last().unwrap().ty]
404                    .inner
405                    .is_dynamically_sized(&module.types)
406                {
407                    // unsized arrays can only be in storage buffers,
408                    // for which we use `ByteAddressBuffer` anyway.
409                    continue;
410                }
411
412                let ep_result = ep_results.iter().find(|e| {
413                    if let Some(ref result) = e.1 {
414                        result.ty == handle
415                    } else {
416                        false
417                    }
418                });
419
420                let early_depth_test = shared_early_depth_test(module, ep_range.clone(), handle);
421                self.with_early_depth_test(early_depth_test, |this| {
422                    this.write_struct(
423                        module,
424                        handle,
425                        members,
426                        span,
427                        ep_result.map(|r| (r.0, Io::Output)),
428                    )
429                })?;
430                writeln!(self.out)?;
431            }
432        }
433
434        self.write_special_functions(module)?;
435
436        self.write_wrapped_expression_functions(module, &module.global_expressions, None)?;
437        self.write_wrapped_zero_value_functions(module, &module.global_expressions)?;
438
439        // Write all named constants
440        let mut constants = module
441            .constants
442            .iter()
443            .filter(|&(_, c)| c.name.is_some())
444            .peekable();
445        while let Some((handle, _)) = constants.next() {
446            self.write_global_constant(module, handle)?;
447            // Add extra newline for readability on last iteration
448            if constants.peek().is_none() {
449                writeln!(self.out)?;
450            }
451        }
452
453        // Write all globals
454        for (global, _) in module.global_variables.iter() {
455            self.write_global(module, global)?;
456        }
457
458        // Write the functions that assemble the values of immediates
459        self.write_immediate_constructors(module)?;
460
461        if !module.global_variables.is_empty() {
462            // Add extra newline for readability
463            writeln!(self.out)?;
464        }
465
466        // Write all entry points wrapped structs
467        for index in ep_range.clone() {
468            let ep = &module.entry_points[index];
469            let ep_name = self.names[&NameKey::EntryPoint(index as u16)].clone();
470            let ep_io = self.write_ep_interface(module, ep, &ep_name, fragment_entry_point)?;
471            self.entry_point_io.insert(index, ep_io);
472        }
473
474        // Write all regular functions
475        for (handle, function) in module.functions.iter() {
476            let info = &module_info[handle];
477
478            // Check if all of the globals are accessible
479            if !self.options.fake_missing_bindings {
480                if let Some((var_handle, _)) =
481                    module
482                        .global_variables
483                        .iter()
484                        .find(|&(var_handle, var)| match var.binding {
485                            Some(ref binding) if !info[var_handle].is_empty() => {
486                                self.options.resolve_resource_binding(binding).is_err()
487                                    && self
488                                        .options
489                                        .resolve_external_texture_resource_binding(binding)
490                                        .is_err()
491                            }
492                            _ => false,
493                        })
494                {
495                    log::debug!(
496                        "Skipping function {:?} (name {:?}) because global {:?} is inaccessible",
497                        handle,
498                        function.name,
499                        var_handle
500                    );
501                    continue;
502                }
503            }
504
505            let ctx = back::FunctionCtx {
506                ty: back::FunctionType::Function(handle),
507                info,
508                expressions: &function.expressions,
509                named_expressions: &function.named_expressions,
510            };
511            let name = self.names[&NameKey::Function(handle)].clone();
512
513            self.write_wrapped_functions(module, &ctx)?;
514
515            self.write_function(module, name.as_str(), function, &ctx, info, String::new())?;
516
517            writeln!(self.out)?;
518        }
519
520        let mut translated_ep_names = Vec::with_capacity(ep_range.len());
521
522        // Write all entry points
523        for index in ep_range {
524            let ep = &module.entry_points[index];
525            let info = module_info.get_entry_point(index);
526
527            if !self.options.fake_missing_bindings {
528                let mut ep_error = None;
529                for (var_handle, var) in module.global_variables.iter() {
530                    match var.binding {
531                        Some(ref binding) if !info[var_handle].is_empty() => {
532                            if let Err(err) = self.options.resolve_resource_binding(binding) {
533                                if self
534                                    .options
535                                    .resolve_external_texture_resource_binding(binding)
536                                    .is_err()
537                                {
538                                    ep_error = Some(err);
539                                    break;
540                                }
541                            }
542                        }
543                        _ => {}
544                    }
545                }
546                if let Some(err) = ep_error {
547                    translated_ep_names.push(Err(err));
548                    continue;
549                }
550            }
551
552            let ctx = back::FunctionCtx {
553                ty: back::FunctionType::EntryPoint(index as u16),
554                info,
555                expressions: &ep.function.expressions,
556                named_expressions: &ep.function.named_expressions,
557            };
558
559            self.write_wrapped_functions(module, &ctx)?;
560
561            // Mesh/task shaders have a wrapper entry point which is declared after the "main"
562            // user-written function. We therefore cannot always just document the next function.
563            let mut attribute_string = String::new();
564            if ep.stage.compute_like() {
565                // HLSL is calling workgroup size "num threads"
566                let num_threads = ep.workgroup_size;
567                writeln!(
568                    attribute_string,
569                    "[numthreads({}, {}, {})]",
570                    num_threads[0], num_threads[1], num_threads[2]
571                )?;
572            }
573            if ep.stage == ShaderStage::Fragment
574                && ep.early_depth_test == Some(crate::EarlyDepthTest::Force)
575            {
576                writeln!(attribute_string, "[earlydepthstencil]")?;
577            }
578            if let Some(ref info) = ep.mesh_info {
579                let topology_str = match info.topology {
580                    crate::MeshOutputTopology::Points => unreachable!(),
581                    crate::MeshOutputTopology::Lines => "line",
582                    crate::MeshOutputTopology::Triangles => "triangle",
583                };
584                writeln!(attribute_string, "[outputtopology(\"{topology_str}\")]")?;
585            }
586
587            let name = self.names[&NameKey::EntryPoint(index as u16)].clone();
588            self.write_function(module, &name, &ep.function, &ctx, info, attribute_string)?;
589
590            if index < module.entry_points.len() - 1 {
591                writeln!(self.out)?;
592            }
593
594            translated_ep_names.push(Ok(name));
595        }
596
597        Ok(super::ReflectionInfo {
598            entry_point_names: translated_ep_names,
599        })
600    }
601
602    fn write_modifier(&mut self, binding: &crate::Binding) -> BackendResult {
603        match *binding {
604            crate::Binding::BuiltIn(crate::BuiltIn::Position { invariant }) => {
605                if invariant {
606                    write!(self.out, "precise ")?;
607                }
608                if self.conservative_depth().is_some() {
609                    // This is only valid for shaders that are not multisampled, which
610                    // `conservative_depth` ensures.
611                    write!(self.out, "noperspective centroid ")?;
612                }
613            }
614            crate::Binding::BuiltIn(crate::BuiltIn::Barycentric { perspective: false }) => {
615                write!(self.out, "noperspective ")?;
616            }
617            crate::Binding::Location {
618                interpolation,
619                sampling,
620                ..
621            } => {
622                if let Some(interpolation) = interpolation {
623                    if let Some(string) = interpolation.to_hlsl_str() {
624                        write!(self.out, "{string} ")?
625                    }
626                }
627
628                if let Some(sampling) = sampling {
629                    if let Some(string) = sampling.to_hlsl_str() {
630                        write!(self.out, "{string} ")?
631                    }
632                }
633            }
634            crate::Binding::BuiltIn(_) => {}
635        }
636
637        Ok(())
638    }
639
640    /// Gets the conservative depth that the fragment shader being written promises, if it is used.
641    ///
642    /// Fragment shaders expect the position input to be at the center of the pixel. DXC only
643    /// accepts `SV_DepthGreaterEqual` and `SV_DepthLessEqual` when the `SV_Position` input is
644    /// declared `noperspective centroid`. That is the pixel center when there is one sample per
645    /// pixel, but under MSAA it can differ, which would change the position that the shader sees.
646    /// Conservative depth is therefore only used for pipelines that are not multisampled.
647    /// Multisampled pipelines use an unrestricted depth output and leave the position input alone.
648    const fn conservative_depth(&self) -> Option<crate::ConservativeDepth> {
649        match self.early_depth_test {
650            Some(crate::EarlyDepthTest::Allow {
651                conservative:
652                    conservative @ (crate::ConservativeDepth::GreaterEqual
653                    | crate::ConservativeDepth::LessEqual),
654            }) if !self.pipeline_options.multisampled => Some(conservative),
655            _ => None,
656        }
657    }
658
659    /// Runs `write` with [`Self::early_depth_test`] set to `early_depth_test`.
660    fn with_early_depth_test<T>(
661        &mut self,
662        early_depth_test: Option<crate::EarlyDepthTest>,
663        write: impl FnOnce(&mut Self) -> T,
664    ) -> T {
665        self.early_depth_test = early_depth_test;
666        let result = write(self);
667        self.early_depth_test = None;
668        result
669    }
670
671    //TODO: we could force fragment outputs to always go through `entry_point_io.output` path
672    // if they are struct, so that the `stage` argument here could be omitted.
673    pub(super) fn write_semantic(
674        &mut self,
675        binding: &Option<crate::Binding>,
676        stage: Option<(ShaderStage, Io)>,
677    ) -> BackendResult {
678        let is_per_primitive = match *binding {
679            Some(crate::Binding::BuiltIn(builtin)) if !is_subgroup_builtin_binding(binding) => {
680                if builtin == crate::BuiltIn::ViewIndex
681                    && self.options.shader_model < ShaderModel::V6_1
682                {
683                    return Err(Error::ShaderModelTooLow(
684                        "used @builtin(view_index) or SV_ViewID".to_string(),
685                        ShaderModel::V6_1,
686                    ));
687                }
688                if let Some(builtin_str) = builtin.to_hlsl_str()? {
689                    let builtin_str = match (builtin, self.conservative_depth()) {
690                        (
691                            crate::BuiltIn::FragDepth,
692                            Some(crate::ConservativeDepth::GreaterEqual),
693                        ) => "SV_DepthGreaterEqual",
694                        (crate::BuiltIn::FragDepth, Some(crate::ConservativeDepth::LessEqual)) => {
695                            "SV_DepthLessEqual"
696                        }
697                        _ => builtin_str,
698                    };
699                    write!(self.out, " : {builtin_str}")?;
700                }
701                false
702            }
703            Some(crate::Binding::Location {
704                blend_src: Some(1),
705                per_primitive,
706                ..
707            }) => {
708                write!(self.out, " : SV_Target1")?;
709                per_primitive
710            }
711            Some(crate::Binding::Location {
712                location,
713                per_primitive,
714                ..
715            }) => {
716                if stage == Some((ShaderStage::Fragment, Io::Output)) {
717                    write!(self.out, " : SV_Target{location}")?;
718                } else {
719                    write!(self.out, " : {LOCATION_SEMANTIC}{location}")?;
720                }
721                per_primitive
722            }
723            _ => false,
724        };
725        if is_per_primitive {
726            write!(self.out, " : primitive")?;
727        }
728
729        Ok(())
730    }
731
732    pub(super) fn write_interface_struct(
733        &mut self,
734        module: &Module,
735        shader_stage: (ShaderStage, Io),
736        struct_name: String,
737        var_name: Option<&str>,
738        mut members: Vec<EpStructMember>,
739    ) -> Result<EntryPointBinding, Error> {
740        let struct_name = self.namer.call(&struct_name);
741        // Sort the members so that first come the user-defined varyings
742        // in ascending locations, and then built-ins. This allows VS and FS
743        // interfaces to match with regards to order.
744        members.sort_by_key(|m| InterfaceKey::new(m.binding.as_ref()));
745
746        write!(self.out, "struct {struct_name}")?;
747        writeln!(self.out, " {{")?;
748        let mut local_invocation_index_name = None;
749        let mut subgroup_id_used = false;
750        for m in members.iter() {
751            // Sanity check that each IO member is a built-in or is assigned a
752            // location. Also see note about nesting in `write_ep_input_struct`.
753            debug_assert!(m.binding.is_some());
754
755            match m.binding {
756                Some(crate::Binding::BuiltIn(crate::BuiltIn::SubgroupId)) => {
757                    subgroup_id_used = true;
758                }
759                Some(crate::Binding::BuiltIn(crate::BuiltIn::LocalInvocationIndex)) => {
760                    local_invocation_index_name = Some(m.name.clone());
761                }
762                _ => (),
763            }
764
765            if is_subgroup_builtin_binding(&m.binding) {
766                continue;
767            }
768            write!(self.out, "{}", back::INDENT)?;
769            if let Some(ref binding) = m.binding {
770                self.write_modifier(binding)?;
771            }
772            self.write_type(module, m.ty)?;
773            write!(self.out, " {}", m.name)?;
774            self.write_semantic(&m.binding, Some(shader_stage))?;
775            writeln!(self.out, ";")?;
776        }
777        if subgroup_id_used && local_invocation_index_name.is_none() {
778            let name = self.namer.call("local_invocation_index");
779            writeln!(self.out, "{}uint {name} : SV_GroupIndex;", back::INDENT)?;
780            local_invocation_index_name = Some(name);
781        }
782        writeln!(self.out, "}};")?;
783        writeln!(self.out)?;
784
785        // See ordering notes on EntryPointInterface fields
786        match shader_stage.1 {
787            Io::Input => {
788                // bring back the original order
789                members.sort_by_key(|m| m.index);
790            }
791            Io::Output | Io::MeshVertices | Io::MeshPrimitives => {
792                // keep it sorted by binding
793            }
794        }
795
796        Ok(EntryPointBinding {
797            arg_name: self
798                .namer
799                .call(var_name.unwrap_or(struct_name.to_lowercase().as_str())),
800            ty_name: struct_name,
801            members,
802            local_invocation_index_name,
803        })
804    }
805
806    /// Flatten all entry point arguments into a single struct.
807    /// This is needed since we need to re-order them: first placing user locations,
808    /// then built-ins.
809    fn write_ep_input_struct(
810        &mut self,
811        module: &Module,
812        func: &crate::Function,
813        stage: ShaderStage,
814        entry_point_name: &str,
815    ) -> Result<EntryPointBinding, Error> {
816        let struct_name = format!("{stage:?}Input_{entry_point_name}");
817
818        let mut fake_members = Vec::new();
819        for arg in func.arguments.iter() {
820            // NOTE: We don't need to handle nesting structs. All members must
821            // be either built-ins or assigned a location. I.E. `binding` is
822            // `Some`. This is checked in `VaryingContext::validate`. See:
823            // https://gpuweb.github.io/gpuweb/wgsl/#input-output-locations
824            match module.types[arg.ty].inner {
825                TypeInner::Struct { ref members, .. } => {
826                    for member in members.iter() {
827                        let name = self.namer.call_or(&member.name, "member");
828                        let index = fake_members.len() as u32;
829                        fake_members.push(EpStructMember {
830                            name,
831                            ty: member.ty,
832                            binding: member.binding.clone(),
833                            index,
834                        });
835                    }
836                }
837                _ => {
838                    let member_name = self.namer.call_or(&arg.name, "member");
839                    let index = fake_members.len() as u32;
840                    fake_members.push(EpStructMember {
841                        name: member_name,
842                        ty: arg.ty,
843                        binding: arg.binding.clone(),
844                        index,
845                    });
846                }
847            }
848        }
849
850        self.write_interface_struct(module, (stage, Io::Input), struct_name, None, fake_members)
851    }
852
853    /// Flatten all entry point results into a single struct.
854    /// This is needed since we need to re-order them: first placing user locations,
855    /// then built-ins.
856    fn write_ep_output_struct(
857        &mut self,
858        module: &Module,
859        entry_point: &crate::EntryPoint,
860        result: &crate::FunctionResult,
861        entry_point_name: &str,
862        frag_ep: Option<&FragmentEntryPoint<'_>>,
863    ) -> Result<EntryPointBinding, Error> {
864        let stage = entry_point.stage;
865        let struct_name = format!("{stage:?}Output_{entry_point_name}");
866
867        let empty = [];
868        let members = match module.types[result.ty].inner {
869            TypeInner::Struct { ref members, .. } => members,
870            ref other => {
871                log::error!("Unexpected {other:?} output type without a binding");
872                &empty[..]
873            }
874        };
875
876        // Gather list of fragment input locations. We use this below to remove user-defined
877        // varyings from VS outputs that aren't in the FS inputs. This makes the VS interface match
878        // as long as the FS inputs are a subset of the VS outputs. This is only applied if the
879        // writer is supplied with information about the fragment entry point.
880        let fs_input_locs = if let (Some(frag_ep), ShaderStage::Vertex) = (frag_ep, stage) {
881            let mut fs_input_locs = Vec::new();
882            for arg in frag_ep.func.arguments.iter() {
883                let mut push_if_location = |binding: &Option<crate::Binding>| match *binding {
884                    Some(crate::Binding::Location { location, .. }) => fs_input_locs.push(location),
885                    Some(crate::Binding::BuiltIn(_)) | None => {}
886                };
887
888                // NOTE: We don't need to handle struct nesting. See note in
889                // `write_ep_input_struct`.
890                match frag_ep.module.types[arg.ty].inner {
891                    TypeInner::Struct { ref members, .. } => {
892                        for member in members.iter() {
893                            push_if_location(&member.binding);
894                        }
895                    }
896                    _ => push_if_location(&arg.binding),
897                }
898            }
899            fs_input_locs.sort();
900            Some(fs_input_locs)
901        } else {
902            None
903        };
904
905        let mut fake_members = Vec::new();
906        for (index, member) in members.iter().enumerate() {
907            if let Some(ref fs_input_locs) = fs_input_locs {
908                match member.binding {
909                    Some(crate::Binding::Location { location, .. }) => {
910                        if fs_input_locs.binary_search(&location).is_err() {
911                            continue;
912                        }
913                    }
914                    Some(crate::Binding::BuiltIn(_)) | None => {}
915                }
916            }
917
918            if discards_frag_depth(entry_point) && is_frag_depth(member.binding.as_ref()) {
919                continue;
920            }
921
922            let member_name = self.namer.call_or(&member.name, "member");
923            fake_members.push(EpStructMember {
924                name: member_name,
925                ty: member.ty,
926                binding: member.binding.clone(),
927                index: index as u32,
928            });
929        }
930
931        self.write_interface_struct(module, (stage, Io::Output), struct_name, None, fake_members)
932    }
933
934    /// Writes special interface structures for an entry point. The special structures have
935    /// all the fields flattened into them and sorted by binding. They are needed to emulate
936    /// subgroup built-ins and to make the interfaces between VS outputs and FS inputs match.
937    fn write_ep_interface(
938        &mut self,
939        module: &Module,
940        ep: &crate::EntryPoint,
941        ep_name: &str,
942        frag_ep: Option<&FragmentEntryPoint<'_>>,
943    ) -> Result<EntryPointInterface, Error> {
944        let func = &ep.function;
945        let stage = ep.stage;
946        Ok(EntryPointInterface {
947            input: if !func.arguments.is_empty()
948                && (stage == ShaderStage::Fragment
949                    || func
950                        .arguments
951                        .iter()
952                        .any(|arg| is_subgroup_builtin_binding(&arg.binding)))
953            {
954                Some(self.with_early_depth_test(ep.early_depth_test, |this| {
955                    this.write_ep_input_struct(module, func, stage, ep_name)
956                })?)
957            } else {
958                None
959            },
960            output: match func.result {
961                Some(ref fr)
962                    if fr.binding.is_none()
963                        && (stage == ShaderStage::Vertex
964                            || (discards_frag_depth(ep)
965                                && !only_writes_discarded_depth(module, ep))) =>
966                {
967                    Some(self.write_ep_output_struct(module, ep, fr, ep_name, frag_ep)?)
968                }
969                _ => None,
970            },
971            mesh_vertices: if let Some(ref info) = ep.mesh_info {
972                Some(self.write_ep_mesh_output_struct(module, ep_name, false, info)?)
973            } else {
974                None
975            },
976            mesh_primitives: if let Some(ref info) = ep.mesh_info {
977                Some(self.write_ep_mesh_output_struct(module, ep_name, true, info)?)
978            } else {
979                None
980            },
981            mesh_indices: if let Some(ref info) = ep.mesh_info {
982                Some(self.write_ep_mesh_output_indices(info.topology)?)
983            } else {
984                None
985            },
986        })
987    }
988
989    fn write_ep_argument_initialization(
990        &mut self,
991        ep: &crate::EntryPoint,
992        ep_input: &EntryPointBinding,
993        fake_member: &EpStructMember,
994    ) -> BackendResult {
995        match fake_member.binding {
996            Some(crate::Binding::BuiltIn(crate::BuiltIn::SubgroupSize)) => {
997                write!(self.out, "WaveGetLaneCount()")?
998            }
999            Some(crate::Binding::BuiltIn(crate::BuiltIn::SubgroupInvocationId)) => {
1000                write!(self.out, "WaveGetLaneIndex()")?
1001            }
1002            Some(crate::Binding::BuiltIn(crate::BuiltIn::NumSubgroups)) => write!(
1003                self.out,
1004                "({}u + WaveGetLaneCount() - 1u) / WaveGetLaneCount()",
1005                ep.workgroup_size[0] * ep.workgroup_size[1] * ep.workgroup_size[2]
1006            )?,
1007            Some(crate::Binding::BuiltIn(crate::BuiltIn::SubgroupId)) => {
1008                write!(
1009                    self.out,
1010                    "{}.{} / WaveGetLaneCount()",
1011                    ep_input.arg_name,
1012                    // When writing SubgroupId, we always guarantee that local_invocation_index_name is written
1013                    ep_input.local_invocation_index_name.as_ref().unwrap()
1014                )?;
1015            }
1016            // WGSL's fragment `position.w` is 1/w of the clip position, D3D's `SV_Position.w` is w.
1017            Some(crate::Binding::BuiltIn(crate::BuiltIn::Position { .. }))
1018                if ep.stage == ShaderStage::Fragment =>
1019            {
1020                write!(
1021                    self.out,
1022                    "float4({0}.{1}.xyz, 1.0 / {0}.{1}.w)",
1023                    ep_input.arg_name, fake_member.name,
1024                )?;
1025            }
1026            Some(crate::Binding::Location {
1027                interpolation: Some(crate::Interpolation::PerVertex),
1028                ..
1029            }) => {
1030                if self.options.shader_model < ShaderModel::V6_1 {
1031                    return Err(Error::ShaderModelTooLow(
1032                        "per_vertex fragment inputs".to_string(),
1033                        ShaderModel::V6_1,
1034                    ));
1035                }
1036                write!(
1037                    self.out,
1038                    "{{ GetAttributeAtVertex({0}.{1}, 0), GetAttributeAtVertex({0}.{1}, 1), GetAttributeAtVertex({0}.{1}, 2) }}",
1039                    ep_input.arg_name,
1040                    fake_member.name,
1041                )?;
1042            }
1043            _ => {
1044                write!(self.out, "{}.{}", ep_input.arg_name, fake_member.name)?;
1045            }
1046        }
1047        Ok(())
1048    }
1049
1050    /// Write an entry point preface that initializes the arguments as specified in IR.
1051    fn write_ep_arguments_initialization(
1052        &mut self,
1053        module: &Module,
1054        func: &crate::Function,
1055        ep_index: u16,
1056    ) -> BackendResult {
1057        let ep = &module.entry_points[ep_index as usize];
1058        let ep_input = match self
1059            .entry_point_io
1060            .get_mut(&(ep_index as usize))
1061            .unwrap()
1062            .input
1063            .take()
1064        {
1065            Some(ep_input) => ep_input,
1066            None => return Ok(()),
1067        };
1068        let mut fake_iter = ep_input.members.iter();
1069        for (arg_index, arg) in func.arguments.iter().enumerate() {
1070            write!(self.out, "{}", back::INDENT)?;
1071            self.write_type(module, arg.ty)?;
1072            let arg_name = &self.names[&NameKey::EntryPointArgument(ep_index, arg_index as u32)];
1073            write!(self.out, " {arg_name}")?;
1074            match module.types[arg.ty].inner {
1075                TypeInner::Array { base, size, .. } => {
1076                    self.write_array_size(module, base, size)?;
1077                    write!(self.out, " = ")?;
1078                    self.write_ep_argument_initialization(
1079                        ep,
1080                        &ep_input,
1081                        fake_iter.next().unwrap(),
1082                    )?;
1083                    writeln!(self.out, ";")?;
1084                }
1085                TypeInner::Struct { ref members, .. } => {
1086                    write!(self.out, " = {{ ")?;
1087                    for index in 0..members.len() {
1088                        if index != 0 {
1089                            write!(self.out, ", ")?;
1090                        }
1091                        self.write_ep_argument_initialization(
1092                            ep,
1093                            &ep_input,
1094                            fake_iter.next().unwrap(),
1095                        )?;
1096                    }
1097                    writeln!(self.out, " }};")?;
1098                }
1099                _ => {
1100                    write!(self.out, " = ")?;
1101                    self.write_ep_argument_initialization(
1102                        ep,
1103                        &ep_input,
1104                        fake_iter.next().unwrap(),
1105                    )?;
1106                    writeln!(self.out, ";")?;
1107                }
1108            }
1109        }
1110        assert!(fake_iter.next().is_none());
1111        Ok(())
1112    }
1113
1114    /// Helper method used to write global variables
1115    /// # Notes
1116    /// Always adds a newline
1117    fn write_global(
1118        &mut self,
1119        module: &Module,
1120        handle: Handle<crate::GlobalVariable>,
1121    ) -> BackendResult {
1122        let global = &module.global_variables[handle];
1123        let inner = &module.types[global.ty].inner;
1124
1125        let handle_ty = match *inner {
1126            TypeInner::BindingArray { ref base, .. } => &module.types[*base].inner,
1127            _ => inner,
1128        };
1129
1130        // External textures are handled entirely differently, so defer entirely to that method.
1131        // We do so prior to calling resolve_resource_binding() below, as we even need to resolve
1132        // their bindings separately.
1133        if handle_ty.is_external_image() {
1134            return self.write_global_external_texture(module, handle, global);
1135        }
1136
1137        if let Some(ref binding) = global.binding {
1138            if let Err(err) = self.options.resolve_resource_binding(binding) {
1139                log::debug!(
1140                    "Skipping global {:?} (name {:?}) for being inaccessible: {}",
1141                    handle,
1142                    global.name,
1143                    err,
1144                );
1145                return Ok(());
1146            }
1147        }
1148
1149        // Samplers are handled entirely differently, so defer entirely to that method.
1150        let is_sampler = matches!(*handle_ty, TypeInner::Sampler { .. });
1151
1152        if is_sampler {
1153            return self.write_global_sampler(module, handle, global);
1154        }
1155
1156        // Immediates are handled entirely differently, so defer entirely to that method.
1157        if global.space == crate::AddressSpace::Immediate {
1158            return self.write_global_immediate(module, handle, global);
1159        }
1160
1161        // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-variable-register
1162        let register_ty = match global.space {
1163            crate::AddressSpace::Function => unreachable!("Function address space"),
1164            crate::AddressSpace::Private => {
1165                write!(self.out, "static ")?;
1166                self.write_type(module, global.ty)?;
1167                ""
1168            }
1169            crate::AddressSpace::WorkGroup | crate::AddressSpace::TaskPayload => {
1170                write!(self.out, "groupshared ")?;
1171                self.write_type(module, global.ty)?;
1172                ""
1173            }
1174            crate::AddressSpace::Uniform => {
1175                // constant buffer declarations are expected to be inlined, e.g.
1176                // `cbuffer foo: register(b0) { field1: type1; }`
1177                write!(self.out, "cbuffer")?;
1178                "b"
1179            }
1180            crate::AddressSpace::Storage { access } => {
1181                if global
1182                    .memory_decorations
1183                    .contains(crate::MemoryDecorations::COHERENT)
1184                {
1185                    write!(self.out, "globallycoherent ")?;
1186                }
1187                let (prefix, register) = if access.contains(crate::StorageAccess::STORE) {
1188                    ("RW", "u")
1189                } else {
1190                    ("", "t")
1191                };
1192                write!(self.out, "{prefix}ByteAddressBuffer")?;
1193                register
1194            }
1195            crate::AddressSpace::Handle => {
1196                let register = match *handle_ty {
1197                    // all storage textures are UAV, unconditionally
1198                    TypeInner::Image {
1199                        class: crate::ImageClass::Storage { .. },
1200                        ..
1201                    } => "u",
1202                    _ => "t",
1203                };
1204                self.write_type(module, global.ty)?;
1205                register
1206            }
1207            crate::AddressSpace::Immediate => {
1208                unreachable!("Immediates are handled by `write_global_immediate`")
1209            }
1210            crate::AddressSpace::RayPayload | crate::AddressSpace::IncomingRayPayload => {
1211                unimplemented!()
1212            }
1213        };
1214
1215        let name = &self.names[&NameKey::GlobalVariable(handle)];
1216        write!(self.out, " {name}")?;
1217
1218        if let Some(ref binding) = global.binding {
1219            // this was already resolved earlier when we started evaluating an entry point.
1220            let bt = self.options.resolve_resource_binding(binding).unwrap();
1221
1222            // need to write the binding array size if the type was emitted with `write_type`
1223            if let TypeInner::BindingArray { base, size, .. } = module.types[global.ty].inner {
1224                if let Some(overridden_size) = bt.binding_array_size {
1225                    write!(self.out, "[{overridden_size}]")?;
1226                } else if matches!(size, crate::ArraySize::Dynamic) {
1227                    // Unbounded descriptor array; legal since SM 5.1.
1228                    write!(self.out, "[]")?;
1229                } else {
1230                    self.write_array_size(module, base, size)?;
1231                }
1232            }
1233
1234            write!(self.out, " : register({}{}", register_ty, bt.register)?;
1235            if bt.space != 0 {
1236                write!(self.out, ", space{}", bt.space)?;
1237            }
1238            write!(self.out, ")")?;
1239        } else {
1240            // need to write the array size if the type was emitted with `write_type`
1241            if let TypeInner::Array { base, size, .. } = module.types[global.ty].inner {
1242                self.write_array_size(module, base, size)?;
1243            }
1244            if global.space == crate::AddressSpace::Private {
1245                write!(self.out, " = ")?;
1246                if let Some(init) = global.init {
1247                    self.write_const_expression(module, init, &module.global_expressions)?;
1248                } else {
1249                    self.write_default_init(module, global.ty)?;
1250                }
1251            }
1252        }
1253
1254        if global.space == crate::AddressSpace::Uniform {
1255            write!(self.out, " {{ ")?;
1256
1257            self.write_global_type(module, global.ty)?;
1258
1259            write!(
1260                self.out,
1261                " {}",
1262                self.names[&NameKey::GlobalVariable(handle)]
1263            )?;
1264
1265            // need to write the array size if the type was emitted with `write_type`
1266            if let TypeInner::Array { base, size, .. } = module.types[global.ty].inner {
1267                self.write_array_size(module, base, size)?;
1268            }
1269
1270            writeln!(self.out, "; }}")?;
1271        } else {
1272            writeln!(self.out, ";")?;
1273        }
1274
1275        Ok(())
1276    }
1277
1278    fn write_global_sampler(
1279        &mut self,
1280        module: &Module,
1281        handle: Handle<crate::GlobalVariable>,
1282        global: &crate::GlobalVariable,
1283    ) -> BackendResult {
1284        let binding = *global.binding.as_ref().unwrap();
1285
1286        let key = super::SamplerIndexBufferKey {
1287            group: binding.group,
1288        };
1289        self.write_wrapped_sampler_buffer(key)?;
1290
1291        // This was already validated, so we can confidently unwrap it.
1292        let bt = self.options.resolve_resource_binding(&binding).unwrap();
1293
1294        match module.types[global.ty].inner {
1295            TypeInner::Sampler { comparison } => {
1296                // If we are generating a static access, we create a variable for the sampler.
1297                //
1298                // This prevents the DXIL from containing multiple lookups for the sampler, which
1299                // the backend compiler will then have to eliminate. AMD does seem to be able to
1300                // eliminate these, but better safe than sorry.
1301
1302                write!(self.out, "static const ")?;
1303                self.write_type(module, global.ty)?;
1304
1305                let heap_var = if comparison {
1306                    COMPARISON_SAMPLER_HEAP_VAR
1307                } else {
1308                    SAMPLER_HEAP_VAR
1309                };
1310
1311                let index_buffer_name = &self.wrapped.sampler_index_buffers[&key];
1312                let name = &self.names[&NameKey::GlobalVariable(handle)];
1313                writeln!(
1314                    self.out,
1315                    " {name} = {heap_var}[{index_buffer_name}[{register}]];",
1316                    register = bt.register
1317                )?;
1318            }
1319            TypeInner::BindingArray { .. } => {
1320                // If we are generating a binding array, we cannot directly access the sampler as the index
1321                // into the sampler index buffer is unknown at compile time. Instead we generate a constant
1322                // that represents the "base" index into the sampler index buffer. This constant is added
1323                // to the user provided index to get the final index into the sampler index buffer.
1324
1325                let name = &self.names[&NameKey::GlobalVariable(handle)];
1326                writeln!(
1327                    self.out,
1328                    "static const uint {name} = {register};",
1329                    register = bt.register
1330                )?;
1331            }
1332            _ => unreachable!(),
1333        };
1334
1335        Ok(())
1336    }
1337
1338    /// Write the declarations for an external texture global variable.
1339    /// These are emitted as multiple global variables: Three `Texture2D`s
1340    /// (one for each plane) and a parameters cbuffer.
1341    fn write_global_external_texture(
1342        &mut self,
1343        module: &Module,
1344        handle: Handle<crate::GlobalVariable>,
1345        global: &crate::GlobalVariable,
1346    ) -> BackendResult {
1347        let res_binding = global
1348            .binding
1349            .as_ref()
1350            .expect("External texture global variables must have a resource binding");
1351        let ext_tex_bindings = match self
1352            .options
1353            .resolve_external_texture_resource_binding(res_binding)
1354        {
1355            Ok(bindings) => bindings,
1356            Err(err) => {
1357                log::debug!(
1358                    "Skipping global {:?} (name {:?}) for being inaccessible: {}",
1359                    handle,
1360                    global.name,
1361                    err,
1362                );
1363                return Ok(());
1364            }
1365        };
1366
1367        let mut write_plane = |bt: &super::BindTarget, name| -> BackendResult {
1368            write!(
1369                self.out,
1370                "Texture2D<float4> {}: register(t{}",
1371                name, bt.register
1372            )?;
1373            if bt.space != 0 {
1374                write!(self.out, ", space{}", bt.space)?;
1375            }
1376            writeln!(self.out, ");")?;
1377            Ok(())
1378        };
1379        for (i, bt) in ext_tex_bindings.planes.iter().enumerate() {
1380            let plane_name = &self.names
1381                [&NameKey::ExternalTextureGlobalVariable(handle, ExternalTextureNameKey::Plane(i))];
1382            write_plane(bt, plane_name)?;
1383        }
1384
1385        let params_name = &self.names
1386            [&NameKey::ExternalTextureGlobalVariable(handle, ExternalTextureNameKey::Params)];
1387        let params_ty_name =
1388            &self.names[&NameKey::Type(module.special_types.external_texture_params.unwrap())];
1389        write!(
1390            self.out,
1391            "cbuffer {}: register(b{}",
1392            params_name, ext_tex_bindings.params.register
1393        )?;
1394        if ext_tex_bindings.params.space != 0 {
1395            write!(self.out, ", space{}", ext_tex_bindings.params.space)?;
1396        }
1397        writeln!(self.out, ") {{ {params_ty_name} {params_name}; }};")?;
1398
1399        Ok(())
1400    }
1401
1402    /// Helper method used to write global constants
1403    ///
1404    /// # Notes
1405    /// Ends in a newline
1406    fn write_global_constant(
1407        &mut self,
1408        module: &Module,
1409        handle: Handle<crate::Constant>,
1410    ) -> BackendResult {
1411        write!(self.out, "static const ")?;
1412        let constant = &module.constants[handle];
1413        self.write_type(module, constant.ty)?;
1414        let name = &self.names[&NameKey::Constant(handle)];
1415        write!(self.out, " {name}")?;
1416        // Write size for array type
1417        if let TypeInner::Array { base, size, .. } = module.types[constant.ty].inner {
1418            self.write_array_size(module, base, size)?;
1419        }
1420        write!(self.out, " = ")?;
1421        self.write_const_expression(module, constant.init, &module.global_expressions)?;
1422        writeln!(self.out, ";")?;
1423        Ok(())
1424    }
1425
1426    pub(super) fn write_array_size(
1427        &mut self,
1428        module: &Module,
1429        base: Handle<crate::Type>,
1430        size: crate::ArraySize,
1431    ) -> BackendResult {
1432        write!(self.out, "[")?;
1433
1434        match size.resolve(module.to_ctx())? {
1435            proc::IndexableLength::Known(size) => {
1436                write!(self.out, "{size}")?;
1437            }
1438            proc::IndexableLength::Dynamic => unreachable!(),
1439        }
1440
1441        write!(self.out, "]")?;
1442
1443        if let TypeInner::Array {
1444            base: next_base,
1445            size: next_size,
1446            ..
1447        } = module.types[base].inner
1448        {
1449            self.write_array_size(module, next_base, next_size)?;
1450        }
1451
1452        Ok(())
1453    }
1454
1455    /// Helper method used to write structs
1456    ///
1457    /// # Notes
1458    /// Ends in a newline
1459    fn write_struct(
1460        &mut self,
1461        module: &Module,
1462        handle: Handle<crate::Type>,
1463        members: &[crate::StructMember],
1464        span: u32,
1465        shader_stage: Option<(ShaderStage, Io)>,
1466    ) -> BackendResult {
1467        // Write struct name
1468        let struct_name = &self.names[&NameKey::Type(handle)];
1469        writeln!(self.out, "struct {struct_name} {{")?;
1470
1471        let mut last_offset = 0;
1472        for (index, member) in members.iter().enumerate() {
1473            if member.binding.is_none() && member.offset > last_offset {
1474                // using int as padding should work as long as the backend
1475                // doesn't support a type that's less than 4 bytes in size
1476                // (Error::UnsupportedScalar catches this)
1477                let padding = (member.offset - last_offset) / 4;
1478                for i in 0..padding {
1479                    writeln!(self.out, "{}int _pad{}_{};", back::INDENT, index, i)?;
1480                }
1481            }
1482            let ty_inner = &module.types[member.ty].inner;
1483            last_offset = member.offset + ty_inner.size_hlsl(module.to_ctx())?;
1484
1485            // The indentation is only for readability
1486            write!(self.out, "{}", back::INDENT)?;
1487
1488            match module.types[member.ty].inner {
1489                TypeInner::Array { base, size, .. } => {
1490                    // HLSL arrays are written as `type name[size]`
1491
1492                    self.write_global_type(module, member.ty)?;
1493
1494                    // Write `name`
1495                    write!(
1496                        self.out,
1497                        " {}",
1498                        self.names[&NameKey::StructMember(handle, index as u32)]
1499                    )?;
1500                    // Write [size]
1501                    self.write_array_size(module, base, size)?;
1502                }
1503                // We treat matrices of the form `matCx2` as a sequence of C `vec2`s.
1504                // See the module-level block comment in mod.rs for details.
1505                TypeInner::Matrix {
1506                    rows,
1507                    columns,
1508                    scalar,
1509                } if member.binding.is_none() && rows == crate::VectorSize::Bi => {
1510                    let vec_ty = TypeInner::Vector { size: rows, scalar };
1511                    let field_name_key = NameKey::StructMember(handle, index as u32);
1512
1513                    for i in 0..columns as u8 {
1514                        if i != 0 {
1515                            write!(self.out, "; ")?;
1516                        }
1517                        self.write_value_type(module, &vec_ty)?;
1518                        write!(self.out, " {}_{}", self.names[&field_name_key], i)?;
1519                    }
1520                }
1521                _ => {
1522                    // Write modifier before type
1523                    if let Some(ref binding) = member.binding {
1524                        self.write_modifier(binding)?;
1525                    }
1526
1527                    // Even though Naga IR matrices are column-major, we must describe
1528                    // matrices passed from the CPU as being in row-major order.
1529                    // See the module-level block comment in mod.rs for details.
1530                    if let TypeInner::Matrix { .. } = module.types[member.ty].inner {
1531                        write!(self.out, "row_major ")?;
1532                    }
1533
1534                    // Write the member type and name
1535                    self.write_type(module, member.ty)?;
1536                    write!(
1537                        self.out,
1538                        " {}",
1539                        self.names[&NameKey::StructMember(handle, index as u32)]
1540                    )?;
1541                }
1542            }
1543
1544            self.write_semantic(&member.binding, shader_stage)?;
1545            writeln!(self.out, ";")?;
1546        }
1547
1548        // add padding at the end since sizes of types don't get rounded up to their alignment in HLSL
1549        if members.last().unwrap().binding.is_none() && span > last_offset {
1550            let padding = (span - last_offset) / 4;
1551            for i in 0..padding {
1552                writeln!(self.out, "{}int _end_pad_{};", back::INDENT, i)?;
1553            }
1554        }
1555
1556        writeln!(self.out, "}};")?;
1557        Ok(())
1558    }
1559
1560    /// Helper method used to write global/structs non image/sampler types
1561    ///
1562    /// # Notes
1563    /// Adds no trailing or leading whitespace
1564    pub(super) fn write_global_type(
1565        &mut self,
1566        module: &Module,
1567        ty: Handle<crate::Type>,
1568    ) -> BackendResult {
1569        let matrix_data = get_inner_matrix_data(module, ty);
1570
1571        // We treat matrices of the form `matCx2` as a sequence of C `vec2`s.
1572        // See the module-level block comment in mod.rs for details.
1573        if let Some(MatrixType {
1574            columns,
1575            rows: crate::VectorSize::Bi,
1576            width,
1577        }) = matrix_data
1578        {
1579            write!(self.out, "__mat{}x2_f{}", columns as u8, width * 8)?;
1580        } else {
1581            // Even though Naga IR matrices are column-major, we must describe
1582            // matrices passed from the CPU as being in row-major order.
1583            // See the module-level block comment in mod.rs for details.
1584            if matrix_data.is_some() {
1585                write!(self.out, "row_major ")?;
1586            }
1587
1588            self.write_type(module, ty)?;
1589        }
1590
1591        Ok(())
1592    }
1593
1594    /// Helper method used to write non image/sampler types
1595    ///
1596    /// # Notes
1597    /// Adds no trailing or leading whitespace
1598    pub(super) fn write_type(&mut self, module: &Module, ty: Handle<crate::Type>) -> BackendResult {
1599        let inner = &module.types[ty].inner;
1600        match *inner {
1601            TypeInner::Struct { .. } => write!(self.out, "{}", self.names[&NameKey::Type(ty)])?,
1602            // hlsl array has the size separated from the base type
1603            TypeInner::Array { base, .. } | TypeInner::BindingArray { base, .. } => {
1604                self.write_type(module, base)?
1605            }
1606            ref other => self.write_value_type(module, other)?,
1607        }
1608
1609        Ok(())
1610    }
1611
1612    /// Helper method used to write value types
1613    ///
1614    /// # Notes
1615    /// Adds no trailing or leading whitespace
1616    pub(super) fn write_value_type(&mut self, module: &Module, inner: &TypeInner) -> BackendResult {
1617        match *inner {
1618            TypeInner::Scalar(scalar) | TypeInner::Atomic(scalar) => {
1619                write!(self.out, "{}", scalar.to_hlsl_str()?)?;
1620            }
1621            TypeInner::Vector { size, scalar } => {
1622                write!(
1623                    self.out,
1624                    "{}{}",
1625                    scalar.to_hlsl_str()?,
1626                    common::vector_size_str(size)
1627                )?;
1628            }
1629            TypeInner::Matrix {
1630                columns,
1631                rows,
1632                scalar,
1633            } => {
1634                // The IR supports only float matrix
1635                // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-matrix
1636
1637                // Because of the implicit transpose all matrices have in HLSL, we need to transpose the size as well.
1638                write!(
1639                    self.out,
1640                    "{}{}x{}",
1641                    scalar.to_hlsl_str()?,
1642                    common::vector_size_str(columns),
1643                    common::vector_size_str(rows),
1644                )?;
1645            }
1646            TypeInner::Image {
1647                dim,
1648                arrayed,
1649                class,
1650            } => {
1651                self.write_image_type(dim, arrayed, class)?;
1652            }
1653            TypeInner::Sampler { comparison } => {
1654                let sampler = if comparison {
1655                    "SamplerComparisonState"
1656                } else {
1657                    "SamplerState"
1658                };
1659                write!(self.out, "{sampler}")?;
1660            }
1661            // HLSL arrays are written as `type name[size]`
1662            // Current code is written arrays only as `[size]`
1663            // Base `type` and `name` should be written outside
1664            TypeInner::Array { base, size, .. } | TypeInner::BindingArray { base, size } => {
1665                self.write_array_size(module, base, size)?;
1666            }
1667            TypeInner::AccelerationStructure { .. } => {
1668                write!(self.out, "RaytracingAccelerationStructure")?;
1669            }
1670            TypeInner::RayQuery { .. } => {
1671                // these are constant flags, there are dynamic flags also but constant flags are not supported by naga
1672                write!(self.out, "RayQuery<RAY_FLAG_NONE>")?;
1673            }
1674            _ => return Err(Error::Unimplemented(format!("write_value_type {inner:?}"))),
1675        }
1676
1677        Ok(())
1678    }
1679
1680    /// Helper method used to write functions
1681    /// # Notes
1682    /// Ends in a newline
1683    fn write_function(
1684        &mut self,
1685        module: &Module,
1686        name: &str,
1687        func: &crate::Function,
1688        func_ctx: &back::FunctionCtx<'_>,
1689        info: &valid::FunctionInfo,
1690        header: String,
1691    ) -> BackendResult {
1692        // Function Declaration Syntax - https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-function-syntax
1693
1694        self.update_expressions_to_bake(module, func, info);
1695        let ep = match func_ctx.ty {
1696            back::FunctionType::EntryPoint(idx) => Some(&module.entry_points[idx as usize]),
1697            back::FunctionType::Function(_) => None,
1698        };
1699
1700        let nested = matches!(
1701            ep,
1702            Some(crate::EntryPoint {
1703                stage: ShaderStage::Task | ShaderStage::Mesh,
1704                ..
1705            })
1706        );
1707        if !nested {
1708            write!(self.out, "{header}")?;
1709        }
1710
1711        let result = func
1712            .result
1713            .as_ref()
1714            .filter(|_| !ep.is_some_and(|ep| only_writes_discarded_depth(module, ep)));
1715
1716        if let Some(result) = result {
1717            // Write typedef if return type is an array
1718            let array_return_type = match module.types[result.ty].inner {
1719                TypeInner::Array { base, size, .. } => {
1720                    let array_return_type = self.namer.call(&format!("ret_{name}"));
1721                    write!(self.out, "typedef ")?;
1722                    self.write_type(module, result.ty)?;
1723                    write!(self.out, " {array_return_type}")?;
1724                    self.write_array_size(module, base, size)?;
1725                    writeln!(self.out, ";")?;
1726                    Some(array_return_type)
1727                }
1728                _ => None,
1729            };
1730
1731            // Write modifier
1732            if let Some(
1733                ref binding @ crate::Binding::BuiltIn(crate::BuiltIn::Position { invariant: true }),
1734            ) = result.binding
1735            {
1736                self.write_modifier(binding)?;
1737            }
1738
1739            // Write return type
1740            match func_ctx.ty {
1741                back::FunctionType::Function(_) => {
1742                    if let Some(array_return_type) = array_return_type {
1743                        write!(self.out, "{array_return_type}")?;
1744                    } else {
1745                        self.write_type(module, result.ty)?;
1746                    }
1747                }
1748                back::FunctionType::EntryPoint(index) => {
1749                    if let Some(ref ep_output) =
1750                        self.entry_point_io.get(&(index as usize)).unwrap().output
1751                    {
1752                        write!(self.out, "{}", ep_output.ty_name)?;
1753                    } else {
1754                        self.write_type(module, result.ty)?;
1755                    }
1756                }
1757            }
1758        } else {
1759            write!(self.out, "void")?;
1760        }
1761
1762        let nested_name = if nested {
1763            self.namer.call(&format!("_{name}"))
1764        } else {
1765            name.to_string()
1766        };
1767
1768        // Write function name
1769        write!(self.out, " {nested_name}(")?;
1770
1771        let need_workgroup_variables_initialization =
1772            self.need_workgroup_variables_initialization(func_ctx, module);
1773
1774        let mut any_args_written = false;
1775        let mut separator = || {
1776            if any_args_written {
1777                ", "
1778            } else {
1779                any_args_written = true;
1780                ""
1781            }
1782        };
1783
1784        let needs_local_invocation_index_name = need_workgroup_variables_initialization || nested;
1785        let mut local_invocation_index_name = None;
1786        // For nested entry points, collect arg names as we write them so that
1787        // write_nested_function_outer can pass the exact same names to the call site.
1788        let mut nested_wgsl_args: Vec<String> = Vec::new();
1789        let mut nested_task_payload_name: Option<String> = None;
1790        // Write function arguments for non entry point functions
1791        match func_ctx.ty {
1792            back::FunctionType::Function(handle) => {
1793                for (index, arg) in func.arguments.iter().enumerate() {
1794                    write!(self.out, "{}", separator())?;
1795                    self.write_function_argument(module, handle, arg, index)?;
1796                }
1797                // If this reads a task payload variable the variable needs to be passed as an `in` argument
1798                for (var_handle, var) in module.global_variables.iter() {
1799                    let uses = info[var_handle];
1800                    if uses.contains(valid::GlobalUse::READ)
1801                        && !uses.contains(valid::GlobalUse::WRITE)
1802                        && var.space == crate::AddressSpace::TaskPayload
1803                    {
1804                        self.function_task_payload_var.insert(handle, var_handle);
1805                        write!(self.out, "{}in ", separator())?;
1806
1807                        self.write_type(module, var.ty)?;
1808                        let name = &self.names[&NameKey::GlobalVariable(var_handle)];
1809                        write!(self.out, " {name}")?;
1810                        break;
1811                    }
1812                }
1813            }
1814            back::FunctionType::EntryPoint(ep_index) => {
1815                let ep = &module.entry_points[ep_index as usize];
1816                if let Some(ref ep_input) =
1817                    self.entry_point_io.get(&(ep_index as usize)).unwrap().input
1818                {
1819                    write!(self.out, "{} {}", ep_input.ty_name, ep_input.arg_name)?;
1820                    separator();
1821                    nested_wgsl_args.push(ep_input.arg_name.clone());
1822                } else {
1823                    let stage = ep.stage;
1824                    for (index, arg) in func.arguments.iter().enumerate() {
1825                        write!(self.out, "{}", separator())?;
1826                        self.write_type(module, arg.ty)?;
1827
1828                        let argument_name =
1829                            &self.names[&NameKey::EntryPointArgument(ep_index, index as u32)];
1830
1831                        if arg.binding
1832                            == Some(crate::Binding::BuiltIn(
1833                                crate::BuiltIn::LocalInvocationIndex,
1834                            ))
1835                        {
1836                            local_invocation_index_name = Some(argument_name.clone());
1837                        }
1838
1839                        nested_wgsl_args.push(argument_name.clone());
1840                        write!(self.out, " {argument_name}")?;
1841                        if let TypeInner::Array { base, size, .. } = module.types[arg.ty].inner {
1842                            self.write_array_size(module, base, size)?;
1843                        }
1844
1845                        self.write_semantic(&arg.binding, Some((stage, Io::Input)))?;
1846                    }
1847                }
1848                if ep.stage == ShaderStage::Mesh {
1849                    if let Some(var_handle) = ep.task_payload {
1850                        let var = &module.global_variables[var_handle];
1851                        write!(self.out, "{}in ", separator())?;
1852                        self.write_type(module, var.ty)?;
1853                        let arg_name = &self.names[&NameKey::GlobalVariable(var_handle)];
1854                        write!(self.out, " {arg_name}")?;
1855                        nested_task_payload_name = Some(arg_name.clone());
1856                        if let TypeInner::Array { base, size, .. } = module.types[var.ty].inner {
1857                            self.write_array_size(module, base, size)?;
1858                        }
1859                    }
1860                }
1861                if needs_local_invocation_index_name && local_invocation_index_name.is_none() {
1862                    let name = self.namer.call("local_invocation_index");
1863                    write!(self.out, "{}uint {name}", separator())?;
1864                    write!(self.out, " : SV_GroupIndex")?;
1865                    local_invocation_index_name = Some(name);
1866                }
1867            }
1868        }
1869        // Ends of arguments
1870        write!(self.out, ")")?;
1871
1872        // Write semantic if it present
1873        if let back::FunctionType::EntryPoint(index) = func_ctx.ty {
1874            let entry_point = &module.entry_points[index as usize];
1875            if let Some(result) = result {
1876                self.with_early_depth_test(entry_point.early_depth_test, |this| {
1877                    this.write_semantic(&result.binding, Some((entry_point.stage, Io::Output)))
1878                })?;
1879            }
1880        }
1881
1882        // Function body start
1883        writeln!(self.out)?;
1884        writeln!(self.out, "{{")?;
1885
1886        if need_workgroup_variables_initialization && !nested {
1887            // need_workgroup_variables_initialization forces this to be written
1888            // if the user doesn't specify it (so this must be Some())
1889            self.write_workgroup_variables_initialization(
1890                func_ctx,
1891                module,
1892                local_invocation_index_name.as_ref().unwrap(),
1893            )?;
1894        }
1895
1896        if let back::FunctionType::EntryPoint(index) = func_ctx.ty {
1897            self.write_ep_arguments_initialization(module, func, index)?;
1898        }
1899
1900        if let back::FunctionType::EntryPoint(_) = func_ctx.ty {
1901            self.write_immediates_initialization(module, info)?;
1902        }
1903
1904        // Write function local variables
1905        for (handle, local) in func.local_variables.iter() {
1906            // Write indentation (only for readability)
1907            write!(self.out, "{}", back::INDENT)?;
1908
1909            // Write the local name
1910            // The leading space is important
1911            self.write_type(module, local.ty)?;
1912            write!(self.out, " {}", self.names[&func_ctx.name_key(handle)])?;
1913            // Write size for array type
1914            if let TypeInner::Array { base, size, .. } = module.types[local.ty].inner {
1915                self.write_array_size(module, base, size)?;
1916            }
1917
1918            let is_ray_query = match module.types[local.ty].inner {
1919                // from https://microsoft.github.io/DirectX-Specs/d3d/Raytracing.html#tracerayinline-example-1 it seems that ray queries shouldn't be zeroed
1920                TypeInner::RayQuery { .. } => true,
1921                _ => {
1922                    write!(self.out, " = ")?;
1923                    // Write the local initializer if needed
1924                    if let Some(init) = local.init {
1925                        self.write_expr(module, init, func_ctx)?;
1926                    } else {
1927                        // Zero initialize local variables
1928                        self.write_default_init(module, local.ty)?;
1929                    }
1930                    false
1931                }
1932            };
1933            // Finish the local with `;` and add a newline (only for readability)
1934            writeln!(self.out, ";")?;
1935            // If it's a ray query, we also want a tracker variable
1936            if is_ray_query {
1937                write!(self.out, "{}", back::INDENT)?;
1938                self.write_value_type(module, &TypeInner::Scalar(Scalar::U32))?;
1939                writeln!(
1940                    self.out,
1941                    " {RAY_QUERY_TRACKER_VARIABLE_PREFIX}{} = 0;",
1942                    self.names[&func_ctx.name_key(handle)]
1943                )?;
1944            }
1945        }
1946
1947        if !func.local_variables.is_empty() {
1948            writeln!(self.out)?;
1949        }
1950
1951        // Write the function body (statement list)
1952        for sta in func.body.iter() {
1953            // The indentation should always be 1 when writing the function body
1954            self.write_stmt(module, sta, func_ctx, back::Level(1))?;
1955        }
1956
1957        writeln!(self.out, "}}")?;
1958
1959        if nested {
1960            self.write_nested_function_outer(
1961                module,
1962                func_ctx,
1963                &header,
1964                name,
1965                need_workgroup_variables_initialization,
1966                &nested_name,
1967                ep.unwrap(),
1968                NestedEntryPointArgs {
1969                    user_args: nested_wgsl_args,
1970                    task_payload: nested_task_payload_name,
1971                    // guaranteed to be set for nested functions (task/mesh shaders)
1972                    local_invocation_index: local_invocation_index_name.unwrap(),
1973                },
1974            )?;
1975        }
1976
1977        self.named_expressions.clear();
1978
1979        Ok(())
1980    }
1981
1982    fn write_function_argument(
1983        &mut self,
1984        module: &Module,
1985        handle: Handle<crate::Function>,
1986        arg: &crate::FunctionArgument,
1987        index: usize,
1988    ) -> BackendResult {
1989        // External texture arguments must be expanded into separate
1990        // arguments for each plane and the params buffer.
1991        if module.types[arg.ty].inner.is_external_image() {
1992            return self.write_function_external_texture_argument(module, handle, index);
1993        }
1994
1995        // Write argument type
1996        let arg_ty = match module.types[arg.ty].inner {
1997            // pointers in function arguments are expected and resolve to `inout`
1998            TypeInner::Pointer { base, .. } => {
1999                //TODO: can we narrow this down to just `in` when possible?
2000                write!(self.out, "inout ")?;
2001                base
2002            }
2003            _ => arg.ty,
2004        };
2005        self.write_type(module, arg_ty)?;
2006
2007        let argument_name = &self.names[&NameKey::FunctionArgument(handle, index as u32)];
2008
2009        // Write argument name. Space is important.
2010        write!(self.out, " {argument_name}")?;
2011        if let TypeInner::Array { base, size, .. } = module.types[arg_ty].inner {
2012            self.write_array_size(module, base, size)?;
2013        }
2014
2015        Ok(())
2016    }
2017
2018    fn write_function_external_texture_argument(
2019        &mut self,
2020        module: &Module,
2021        handle: Handle<crate::Function>,
2022        index: usize,
2023    ) -> BackendResult {
2024        let plane_names = [0, 1, 2].map(|i| {
2025            &self.names[&NameKey::ExternalTextureFunctionArgument(
2026                handle,
2027                index as u32,
2028                ExternalTextureNameKey::Plane(i),
2029            )]
2030        });
2031        let params_name = &self.names[&NameKey::ExternalTextureFunctionArgument(
2032            handle,
2033            index as u32,
2034            ExternalTextureNameKey::Params,
2035        )];
2036        let params_ty_name =
2037            &self.names[&NameKey::Type(module.special_types.external_texture_params.unwrap())];
2038        write!(
2039            self.out,
2040            "Texture2D<float4> {}, Texture2D<float4> {}, Texture2D<float4> {}, {params_ty_name} {params_name}",
2041            plane_names[0], plane_names[1], plane_names[2],
2042        )?;
2043        Ok(())
2044    }
2045
2046    fn need_workgroup_variables_initialization(
2047        &mut self,
2048        func_ctx: &back::FunctionCtx,
2049        module: &Module,
2050    ) -> bool {
2051        self.options.zero_initialize_workgroup_memory
2052            && func_ctx.ty.is_compute_like_entry_point(module)
2053            && module.global_variables.iter().any(|(handle, var)| {
2054                !func_ctx.info[handle].is_empty() && var.space.is_workgroup_like()
2055            })
2056    }
2057
2058    /// Writes code that zeroes `access`, of type `ty`, with the work spread over the workgroup.
2059    ///
2060    /// `struct_array_element` is true if `access` is an element of an array that is a struct member.
2061    fn write_compute_workgroup_init(
2062        &mut self,
2063        module: &Module,
2064        ty: proc::TypeResolution,
2065        access: &str,
2066        struct_array_element: bool,
2067        dimensions: &mut Vec<(String, u32)>,
2068        invocations: WorkgroupInitInvocations,
2069    ) -> BackendResult {
2070        match *ty.inner_with(&module.types) {
2071            TypeInner::Array { base, size, .. } => {
2072                let proc::IndexableLength::Known(count) = size.resolve(module.to_ctx())? else {
2073                    unreachable!();
2074                };
2075                let index = self.namer.call("zero_index");
2076                let element = format!("{access}[{index}]");
2077                dimensions.push((index, count));
2078                self.write_compute_workgroup_init(
2079                    module,
2080                    proc::TypeResolution::Handle(base),
2081                    &element,
2082                    struct_array_element,
2083                    dimensions,
2084                    invocations,
2085                )?;
2086                dimensions.pop();
2087            }
2088            TypeInner::Struct { ref members, .. } => {
2089                for (index, member) in members.iter().enumerate() {
2090                    let name =
2091                        &self.names[&NameKey::StructMember(ty.handle().unwrap(), index as u32)];
2092                    let field = format!("{access}.{name}");
2093                    match module.types[member.ty].inner {
2094                        TypeInner::Matrix {
2095                            columns,
2096                            rows: crate::VectorSize::Bi,
2097                            scalar,
2098                        } if member.binding.is_none() => {
2099                            for column in 0..columns as u8 {
2100                                self.write_compute_workgroup_init(
2101                                    module,
2102                                    proc::TypeResolution::Value(TypeInner::Vector {
2103                                        size: crate::VectorSize::Bi,
2104                                        scalar,
2105                                    }),
2106                                    &format!("{field}_{column}"),
2107                                    false,
2108                                    dimensions,
2109                                    invocations,
2110                                )?;
2111                            }
2112                        }
2113                        _ => {
2114                            self.write_compute_workgroup_init(
2115                                module,
2116                                proc::TypeResolution::Handle(member.ty),
2117                                &field,
2118                                matches!(module.types[member.ty].inner, TypeInner::Array { .. }),
2119                                dimensions,
2120                                invocations,
2121                            )?;
2122                        }
2123                    }
2124                }
2125            }
2126            ref inner => {
2127                let level = back::Level(1);
2128                let body = level.next();
2129                let WorkgroupInitInvocations {
2130                    local_index,
2131                    workgroup_size,
2132                } = invocations;
2133                if dimensions.is_empty() {
2134                    writeln!(self.out, "{level}if ({local_index} == 0) {{")?;
2135                } else {
2136                    let count = dimensions
2137                        .iter()
2138                        .try_fold(1u32, |total, &(_, size)| total.checked_mul(size))
2139                        .ok_or_else(|| {
2140                            Error::Custom("Workgroup array element count overflow".into())
2141                        })?;
2142                    let index = self.namer.call("zero_flat_index");
2143                    // Keep DXC from expanding large aggregate initializers during optimization.
2144                    writeln!(self.out, "{level}[loop]")?;
2145                    writeln!(self.out, "{level}for (uint {index} = {local_index}; {index} < {count}u; {index} += {workgroup_size}u) {{")?;
2146                    let mut stride = count;
2147                    for (i, &(ref dimension, size)) in dimensions.iter().enumerate() {
2148                        stride /= size;
2149                        let mut value = index.clone();
2150                        if stride != 1 {
2151                            value = format!("{value} / {stride}u");
2152                        }
2153                        // The loop bound keeps the outermost index in range.
2154                        if i != 0 {
2155                            if stride != 1 {
2156                                value = format!("({value})");
2157                            }
2158                            value = format!("{value} % {size}u");
2159                        }
2160                        writeln!(self.out, "{body}uint {dimension} = {value};")?;
2161                    }
2162                }
2163                write!(self.out, "{body}{access} = (")?;
2164                if struct_array_element
2165                    && matches!(
2166                        inner,
2167                        TypeInner::Matrix {
2168                            rows: crate::VectorSize::Bi,
2169                            ..
2170                        }
2171                    )
2172                {
2173                    self.write_global_type(module, ty.handle().unwrap())?;
2174                } else {
2175                    self.write_value_type(module, inner)?;
2176                }
2177                writeln!(self.out, ")0;")?;
2178                writeln!(self.out, "{level}}}")?;
2179            }
2180        }
2181        Ok(())
2182    }
2183
2184    /// Writes the zero initialization of the workgroup variables that the entry point uses,
2185    /// followed by a workgroup barrier.
2186    pub(super) fn write_workgroup_variables_initialization(
2187        &mut self,
2188        func_ctx: &back::FunctionCtx,
2189        module: &Module,
2190        local_invocation_index: &str,
2191    ) -> BackendResult {
2192        let back::FunctionType::EntryPoint(ep_index) = func_ctx.ty else {
2193            unreachable!();
2194        };
2195        let entry_point = &module.entry_points[ep_index as usize];
2196        let workgroup_size = entry_point
2197            .workgroup_size
2198            .iter()
2199            .try_fold(1u32, |total, &size| total.checked_mul(size))
2200            .ok_or_else(|| Error::Custom("Workgroup invocation count overflow".into()))?;
2201        for (handle, var) in module.global_variables.iter() {
2202            // The task payload is read-only in mesh shaders.
2203            let task_needs_zero = var.space == crate::AddressSpace::TaskPayload
2204                && entry_point.stage == ShaderStage::Task;
2205            if !func_ctx.info[handle].is_empty()
2206                && (var.space == crate::AddressSpace::WorkGroup || task_needs_zero)
2207            {
2208                let name = self.names[&NameKey::GlobalVariable(handle)].clone();
2209                self.write_compute_workgroup_init(
2210                    module,
2211                    proc::TypeResolution::Handle(var.ty),
2212                    &name,
2213                    false,
2214                    &mut Vec::new(),
2215                    WorkgroupInitInvocations {
2216                        local_index: local_invocation_index,
2217                        workgroup_size,
2218                    },
2219                )?;
2220            }
2221        }
2222        self.write_control_barrier(crate::Barrier::WORK_GROUP, back::Level(1))
2223    }
2224
2225    /// Helper method used to write switches
2226    fn write_switch(
2227        &mut self,
2228        module: &Module,
2229        func_ctx: &back::FunctionCtx<'_>,
2230        level: back::Level,
2231        selector: Handle<crate::Expression>,
2232        cases: &[crate::SwitchCase],
2233    ) -> BackendResult {
2234        // Write all cases
2235        let indent_level_1 = level.next();
2236        let indent_level_2 = indent_level_1.next();
2237
2238        // See docs of `back::continue_forward` module.
2239        if let Some(variable) = self.continue_ctx.enter_switch(&mut self.namer) {
2240            writeln!(self.out, "{level}bool {variable} = false;",)?;
2241        };
2242
2243        // Check if there is only one body, by seeing if all except the last case are fall through
2244        // with empty bodies. FXC doesn't handle these switches correctly, so
2245        // we generate a `do {} while(false);` loop instead. There must be a default case, so there
2246        // is no need to check if one of the cases would have matched.
2247        let one_body = cases
2248            .iter()
2249            .rev()
2250            .skip(1)
2251            .all(|case| case.fall_through && case.body.is_empty());
2252        if one_body {
2253            // Start the do-while
2254            writeln!(self.out, "{level}do {{")?;
2255            // Note: Expressions have no side-effects so we don't need to emit selector expression.
2256
2257            // Body
2258            if let Some(case) = cases.last() {
2259                for sta in case.body.iter() {
2260                    self.write_stmt(module, sta, func_ctx, indent_level_1)?;
2261                }
2262            }
2263            // End do-while
2264            writeln!(self.out, "{level}}} while(false);")?;
2265        } else {
2266            // Start the switch
2267            write!(self.out, "{level}")?;
2268            write!(self.out, "switch(")?;
2269            self.write_expr(module, selector, func_ctx)?;
2270            writeln!(self.out, ") {{")?;
2271
2272            for (i, case) in cases.iter().enumerate() {
2273                match case.value {
2274                    crate::SwitchValue::I32(value) => {
2275                        write!(self.out, "{indent_level_1}case {value}:")?
2276                    }
2277                    crate::SwitchValue::U32(value) => {
2278                        write!(self.out, "{indent_level_1}case {value}u:")?
2279                    }
2280                    crate::SwitchValue::Default => write!(self.out, "{indent_level_1}default:")?,
2281                }
2282
2283                // The new block is not only stylistic, it plays a role here:
2284                // We might end up having to write the same case body
2285                // multiple times due to FXC not supporting fallthrough.
2286                // Therefore, some `Expression`s written by `Statement::Emit`
2287                // will end up having the same name (`_expr<handle_index>`).
2288                // So we need to put each case in its own scope.
2289                let write_block_braces = !(case.fall_through && case.body.is_empty());
2290                if write_block_braces {
2291                    writeln!(self.out, " {{")?;
2292                } else {
2293                    writeln!(self.out)?;
2294                }
2295
2296                // Although FXC does support a series of case clauses before
2297                // a block[^yes], it does not support fallthrough from a
2298                // non-empty case block to the next[^no]. If this case has a
2299                // non-empty body with a fallthrough, emulate that by
2300                // duplicating the bodies of all the cases it would fall
2301                // into as extensions of this case's own body. This makes
2302                // the HLSL output potentially quadratic in the size of the
2303                // Naga IR.
2304                //
2305                // [^yes]: ```hlsl
2306                // case 1:
2307                // case 2: do_stuff()
2308                // ```
2309                // [^no]: ```hlsl
2310                // case 1: do_this();
2311                // case 2: do_that();
2312                // ```
2313                if case.fall_through && !case.body.is_empty() {
2314                    let curr_len = i + 1;
2315                    let end_case_idx = curr_len
2316                        + cases
2317                            .iter()
2318                            .skip(curr_len)
2319                            .position(|case| !case.fall_through)
2320                            .unwrap();
2321                    let indent_level_3 = indent_level_2.next();
2322                    for case in &cases[i..=end_case_idx] {
2323                        writeln!(self.out, "{indent_level_2}{{")?;
2324                        let prev_len = self.named_expressions.len();
2325                        for sta in case.body.iter() {
2326                            self.write_stmt(module, sta, func_ctx, indent_level_3)?;
2327                        }
2328                        // Clear all named expressions that were previously inserted by the statements in the block
2329                        self.named_expressions.truncate(prev_len);
2330                        writeln!(self.out, "{indent_level_2}}}")?;
2331                    }
2332
2333                    let last_case = &cases[end_case_idx];
2334                    if last_case.body.last().is_none_or(|s| !s.is_terminator()) {
2335                        writeln!(self.out, "{indent_level_2}break;")?;
2336                    }
2337                } else {
2338                    for sta in case.body.iter() {
2339                        self.write_stmt(module, sta, func_ctx, indent_level_2)?;
2340                    }
2341                    if !case.fall_through && case.body.last().is_none_or(|s| !s.is_terminator()) {
2342                        writeln!(self.out, "{indent_level_2}break;")?;
2343                    }
2344                }
2345
2346                if write_block_braces {
2347                    writeln!(self.out, "{indent_level_1}}}")?;
2348                }
2349            }
2350
2351            writeln!(self.out, "{level}}}")?;
2352        }
2353
2354        // Handle any forwarded continue statements.
2355        use back::continue_forward::ExitControlFlow;
2356        let op = match self.continue_ctx.exit_switch() {
2357            ExitControlFlow::None => None,
2358            ExitControlFlow::Continue { variable } => Some(("continue", variable)),
2359            ExitControlFlow::Break { variable } => Some(("break", variable)),
2360        };
2361        if let Some((control_flow, variable)) = op {
2362            writeln!(self.out, "{level}if ({variable}) {{")?;
2363            writeln!(self.out, "{indent_level_1}{control_flow};")?;
2364            writeln!(self.out, "{level}}}")?;
2365        }
2366
2367        Ok(())
2368    }
2369
2370    fn write_index(
2371        &mut self,
2372        module: &Module,
2373        index: Index,
2374        func_ctx: &back::FunctionCtx<'_>,
2375    ) -> BackendResult {
2376        match index {
2377            Index::Static(index) => {
2378                write!(self.out, "{index}")?;
2379            }
2380            Index::Expression(index) => {
2381                self.write_expr(module, index, func_ctx)?;
2382            }
2383        }
2384        Ok(())
2385    }
2386
2387    /// Helper method used to write statements
2388    ///
2389    /// # Notes
2390    /// Always adds a newline
2391    fn write_stmt(
2392        &mut self,
2393        module: &Module,
2394        stmt: &crate::Statement,
2395        func_ctx: &back::FunctionCtx<'_>,
2396        level: back::Level,
2397    ) -> BackendResult {
2398        use crate::Statement;
2399
2400        match *stmt {
2401            Statement::Emit(ref range) => {
2402                for handle in range.clone() {
2403                    let ptr_class = func_ctx.resolve_type(handle, &module.types).pointer_space();
2404                    let expr_name = if ptr_class.is_some() {
2405                        // HLSL can't save a pointer-valued expression in a variable,
2406                        // but we shouldn't ever need to: they should never be named expressions,
2407                        // and none of the expression types flagged by bake_ref_count can be pointer-valued.
2408                        None
2409                    } else if let Some(name) = func_ctx.named_expressions.get(&handle) {
2410                        // Front end provides names for all variables at the start of writing.
2411                        // But we write them to step by step. We need to recache them
2412                        // Otherwise, we could accidentally write variable name instead of full expression.
2413                        // Also, we use sanitized names! It defense backend from generating variable with name from reserved keywords.
2414                        Some(self.namer.call(name))
2415                    } else if self.need_bake_expressions.contains(&handle) {
2416                        Some(Baked(handle).to_string())
2417                    } else {
2418                        None
2419                    };
2420
2421                    if let Some(name) = expr_name {
2422                        write!(self.out, "{level}")?;
2423                        self.write_named_expr(module, handle, name, handle, func_ctx)?;
2424                    }
2425                }
2426            }
2427            // TODO: copy-paste from glsl-out
2428            Statement::Block(ref block) => {
2429                write!(self.out, "{level}")?;
2430                writeln!(self.out, "{{")?;
2431                for sta in block.iter() {
2432                    // Increase the indentation to help with readability
2433                    self.write_stmt(module, sta, func_ctx, level.next())?
2434                }
2435                writeln!(self.out, "{level}}}")?
2436            }
2437            // TODO: copy-paste from glsl-out
2438            Statement::If {
2439                condition,
2440                ref accept,
2441                ref reject,
2442            } => {
2443                write!(self.out, "{level}")?;
2444                write!(self.out, "if (")?;
2445                self.write_expr(module, condition, func_ctx)?;
2446                writeln!(self.out, ") {{")?;
2447
2448                let l2 = level.next();
2449                for sta in accept {
2450                    // Increase indentation to help with readability
2451                    self.write_stmt(module, sta, func_ctx, l2)?;
2452                }
2453
2454                // If there are no statements in the reject block we skip writing it
2455                // This is only for readability
2456                if !reject.is_empty() {
2457                    writeln!(self.out, "{level}}} else {{")?;
2458
2459                    for sta in reject {
2460                        // Increase indentation to help with readability
2461                        self.write_stmt(module, sta, func_ctx, l2)?;
2462                    }
2463                }
2464
2465                writeln!(self.out, "{level}}}")?
2466            }
2467            // TODO: copy-paste from glsl-out
2468            Statement::Kill => writeln!(self.out, "{level}discard;")?,
2469            Statement::Return { value: None } => {
2470                writeln!(self.out, "{level}return;")?;
2471            }
2472            Statement::Return { value: Some(_) } if returns_nothing(module, func_ctx) => {
2473                writeln!(self.out, "{level}return;")?;
2474            }
2475            Statement::Return { value: Some(expr) } => {
2476                let base_ty_res = &func_ctx.info[expr].ty;
2477                let mut resolved = base_ty_res.inner_with(&module.types);
2478                if let TypeInner::Pointer { base, space: _ } = *resolved {
2479                    resolved = &module.types[base].inner;
2480                }
2481
2482                if let TypeInner::Struct { .. } = *resolved {
2483                    // We can safely unwrap here, since we now we working with struct
2484                    let ty = base_ty_res.handle().unwrap();
2485                    let struct_name = &self.names[&NameKey::Type(ty)];
2486                    let variable_name = self.namer.call(&struct_name.to_lowercase());
2487                    write!(self.out, "{level}const {struct_name} {variable_name} = ",)?;
2488                    self.write_expr(module, expr, func_ctx)?;
2489                    writeln!(self.out, ";")?;
2490
2491                    // for entry point returns, we may need to reshuffle the outputs into a different struct
2492                    let ep_output = match func_ctx.ty {
2493                        back::FunctionType::Function(_) => None,
2494                        back::FunctionType::EntryPoint(index) => self
2495                            .entry_point_io
2496                            .get(&(index as usize))
2497                            .unwrap()
2498                            .output
2499                            .as_ref(),
2500                    };
2501                    let final_name = match ep_output {
2502                        Some(ep_output) => {
2503                            let final_name = self.namer.call(&variable_name);
2504                            write!(
2505                                self.out,
2506                                "{}const {} {} = {{ ",
2507                                level, ep_output.ty_name, final_name,
2508                            )?;
2509                            for (index, m) in ep_output.members.iter().enumerate() {
2510                                if index != 0 {
2511                                    write!(self.out, ", ")?;
2512                                }
2513                                let member_name = &self.names[&NameKey::StructMember(ty, m.index)];
2514                                write!(self.out, "{variable_name}.{member_name}")?;
2515                            }
2516                            writeln!(self.out, " }};")?;
2517                            final_name
2518                        }
2519                        None => variable_name,
2520                    };
2521                    writeln!(self.out, "{level}return {final_name};")?;
2522                } else {
2523                    write!(self.out, "{level}return ")?;
2524                    self.write_expr(module, expr, func_ctx)?;
2525                    writeln!(self.out, ";")?
2526                }
2527            }
2528            Statement::Store { pointer, value } => {
2529                let ty_inner = func_ctx.resolve_type(pointer, &module.types);
2530                if ty_inner.is_atomic_pointer(&module.types) {
2531                    let pointer_space = ty_inner.pointer_space().unwrap();
2532                    let dummy = self.namer.call("dummy");
2533                    write!(self.out, "{level}{{ ")?;
2534                    if let TypeInner::Pointer { base, .. } = *ty_inner {
2535                        self.write_value_type(module, &module.types[base].inner)?;
2536                    }
2537                    write!(self.out, " {dummy} = 0; ")?;
2538                    match pointer_space {
2539                        crate::AddressSpace::WorkGroup => {
2540                            write!(self.out, "InterlockedExchange(")?;
2541                            self.write_expr(module, pointer, func_ctx)?;
2542                        }
2543                        crate::AddressSpace::Storage { .. } => {
2544                            let var_handle = self.fill_access_chain(module, pointer, func_ctx)?;
2545                            let var_name = &self.names[&NameKey::GlobalVariable(var_handle)];
2546                            write!(self.out, "{var_name}.InterlockedExchange(")?;
2547                            let chain = mem::take(&mut self.temp_access_chain);
2548                            self.write_storage_address(module, &chain, func_ctx)?;
2549                            self.temp_access_chain = chain;
2550                        }
2551                        _ => unreachable!(),
2552                    }
2553                    write!(self.out, ", ")?;
2554                    self.write_expr(module, value, func_ctx)?;
2555                    writeln!(self.out, ", {dummy}); }}")?;
2556                } else if let Some(crate::AddressSpace::Storage { .. }) = ty_inner.pointer_space() {
2557                    let var_handle = self.fill_access_chain(module, pointer, func_ctx)?;
2558                    self.write_storage_store(
2559                        module,
2560                        var_handle,
2561                        StoreValue::Expression(value),
2562                        func_ctx,
2563                        level,
2564                        None,
2565                    )?;
2566                } else {
2567                    // We treat matrices of the form `matCx2` as a sequence of C `vec2`s.
2568                    // See the module-level block comment in mod.rs for details.
2569                    //
2570                    // We handle matrix Stores here directly (including sub accesses for Vectors and Scalars).
2571                    // Loads are handled by `Expression::AccessIndex` (since sub accesses work fine for Loads).
2572                    enum MatrixAccess {
2573                        Direct {
2574                            base: Handle<crate::Expression>,
2575                            index: u32,
2576                        },
2577                        Struct {
2578                            columns: crate::VectorSize,
2579                            width: u8,
2580                            base: Handle<crate::Expression>,
2581                        },
2582                    }
2583
2584                    let get_members = |expr: Handle<crate::Expression>| {
2585                        let resolved = func_ctx.resolve_type(expr, &module.types);
2586                        match *resolved {
2587                            TypeInner::Pointer { base, .. } => match module.types[base].inner {
2588                                TypeInner::Struct { ref members, .. } => Some(members),
2589                                _ => None,
2590                            },
2591                            _ => None,
2592                        }
2593                    };
2594
2595                    write!(self.out, "{level}")?;
2596
2597                    let matrix_access_on_lhs =
2598                        find_matrix_in_access_chain(module, pointer, func_ctx).and_then(
2599                            |(matrix_expr, vector, scalar)| match (
2600                                func_ctx.resolve_type(matrix_expr, &module.types),
2601                                &func_ctx.expressions[matrix_expr],
2602                            ) {
2603                                (
2604                                    &TypeInner::Pointer { base: ty, .. },
2605                                    &crate::Expression::AccessIndex { base, index },
2606                                ) if matches!(
2607                                    module.types[ty].inner,
2608                                    TypeInner::Matrix {
2609                                        rows: crate::VectorSize::Bi,
2610                                        ..
2611                                    }
2612                                ) && get_members(base)
2613                                    .map(|members| members[index as usize].binding.is_none())
2614                                    == Some(true) =>
2615                                {
2616                                    Some((MatrixAccess::Direct { base, index }, vector, scalar))
2617                                }
2618                                _ => {
2619                                    if let Some(MatrixType {
2620                                        columns,
2621                                        rows: crate::VectorSize::Bi,
2622                                        width,
2623                                    }) = get_inner_matrix_of_struct_array_member(
2624                                        module,
2625                                        matrix_expr,
2626                                        func_ctx,
2627                                        true,
2628                                    ) {
2629                                        Some((
2630                                            MatrixAccess::Struct {
2631                                                columns,
2632                                                width,
2633                                                base: matrix_expr,
2634                                            },
2635                                            vector,
2636                                            scalar,
2637                                        ))
2638                                    } else {
2639                                        None
2640                                    }
2641                                }
2642                            },
2643                        );
2644
2645                    match matrix_access_on_lhs {
2646                        Some((MatrixAccess::Direct { index, base }, vector, scalar)) => {
2647                            let base_ty_res = &func_ctx.info[base].ty;
2648                            let resolved = base_ty_res.inner_with(&module.types);
2649                            let ty = match *resolved {
2650                                TypeInner::Pointer { base, .. } => base,
2651                                _ => base_ty_res.handle().unwrap(),
2652                            };
2653
2654                            if let Some(Index::Static(vec_index)) = vector {
2655                                self.write_expr(module, base, func_ctx)?;
2656                                write!(
2657                                    self.out,
2658                                    ".{}_{}",
2659                                    self.names[&NameKey::StructMember(ty, index)],
2660                                    vec_index
2661                                )?;
2662
2663                                if let Some(scalar_index) = scalar {
2664                                    write!(self.out, "[")?;
2665                                    self.write_index(module, scalar_index, func_ctx)?;
2666                                    write!(self.out, "]")?;
2667                                }
2668
2669                                write!(self.out, " = ")?;
2670                                self.write_expr(module, value, func_ctx)?;
2671                                writeln!(self.out, ";")?;
2672                            } else {
2673                                let access = WrappedStructMatrixAccess { ty, index };
2674                                match (&vector, &scalar) {
2675                                    (&Some(_), &Some(_)) => {
2676                                        self.write_wrapped_struct_matrix_set_scalar_function_name(
2677                                            access,
2678                                        )?;
2679                                    }
2680                                    (&Some(_), &None) => {
2681                                        self.write_wrapped_struct_matrix_set_vec_function_name(
2682                                            access,
2683                                        )?;
2684                                    }
2685                                    (&None, _) => {
2686                                        self.write_wrapped_struct_matrix_set_function_name(access)?;
2687                                    }
2688                                }
2689
2690                                write!(self.out, "(")?;
2691                                self.write_expr(module, base, func_ctx)?;
2692                                write!(self.out, ", ")?;
2693                                self.write_expr(module, value, func_ctx)?;
2694
2695                                if let Some(Index::Expression(vec_index)) = vector {
2696                                    write!(self.out, ", ")?;
2697                                    self.write_expr(module, vec_index, func_ctx)?;
2698
2699                                    if let Some(scalar_index) = scalar {
2700                                        write!(self.out, ", ")?;
2701                                        self.write_index(module, scalar_index, func_ctx)?;
2702                                    }
2703                                }
2704                                writeln!(self.out, ");")?;
2705                            }
2706                        }
2707                        Some((
2708                            MatrixAccess::Struct {
2709                                columns,
2710                                width,
2711                                base,
2712                            },
2713                            Some(Index::Expression(vec_index)),
2714                            scalar,
2715                        )) => {
2716                            // We handle `Store`s to __matCx2 column vectors and scalar elements via
2717                            // the previously injected functions __set_col_of_matCx2 / __set_el_of_matCx2.
2718
2719                            if scalar.is_some() {
2720                                write!(
2721                                    self.out,
2722                                    "__set_el_of_mat{}x2_f{}",
2723                                    columns as u8,
2724                                    width * 8
2725                                )?;
2726                            } else {
2727                                write!(
2728                                    self.out,
2729                                    "__set_col_of_mat{}x2_f{}",
2730                                    columns as u8,
2731                                    width * 8
2732                                )?;
2733                            }
2734                            write!(self.out, "(")?;
2735                            self.write_expr(module, base, func_ctx)?;
2736                            write!(self.out, ", ")?;
2737                            self.write_expr(module, vec_index, func_ctx)?;
2738
2739                            if let Some(scalar_index) = scalar {
2740                                write!(self.out, ", ")?;
2741                                self.write_index(module, scalar_index, func_ctx)?;
2742                            }
2743
2744                            write!(self.out, ", ")?;
2745                            self.write_expr(module, value, func_ctx)?;
2746
2747                            writeln!(self.out, ");")?;
2748                        }
2749                        Some((MatrixAccess::Struct { .. }, Some(Index::Static(_)), _))
2750                        | Some((MatrixAccess::Struct { .. }, None, _))
2751                        | None => {
2752                            self.write_expr(module, pointer, func_ctx)?;
2753                            write!(self.out, " = ")?;
2754
2755                            // We cast the RHS of this store in cases where the LHS
2756                            // is a struct member with type:
2757                            //  - matCx2 or
2758                            //  - a (possibly nested) array of matCx2's
2759                            if let Some(MatrixType {
2760                                columns,
2761                                rows: crate::VectorSize::Bi,
2762                                width,
2763                            }) = get_inner_matrix_of_struct_array_member(
2764                                module, pointer, func_ctx, false,
2765                            ) {
2766                                let mut resolved = func_ctx.resolve_type(pointer, &module.types);
2767                                if let TypeInner::Pointer { base, .. } = *resolved {
2768                                    resolved = &module.types[base].inner;
2769                                }
2770
2771                                write!(self.out, "(__mat{}x2_f{}", columns as u8, width * 8)?;
2772                                if let TypeInner::Array { base, size, .. } = *resolved {
2773                                    self.write_array_size(module, base, size)?;
2774                                }
2775                                write!(self.out, ")")?;
2776                            }
2777
2778                            self.write_expr(module, value, func_ctx)?;
2779                            writeln!(self.out, ";")?
2780                        }
2781                    }
2782                }
2783            }
2784            Statement::Loop {
2785                ref body,
2786                ref continuing,
2787                break_if,
2788            } => {
2789                let force_loop_bound_statements = self.gen_force_bounded_loop_statements(level);
2790                let gate_name = (!continuing.is_empty() || break_if.is_some())
2791                    .then(|| self.namer.call("loop_init"));
2792
2793                if let Some((ref decl, _)) = force_loop_bound_statements {
2794                    writeln!(self.out, "{decl}")?;
2795                }
2796                if let Some(ref gate_name) = gate_name {
2797                    writeln!(self.out, "{level}bool {gate_name} = true;")?;
2798                }
2799
2800                self.continue_ctx.enter_loop();
2801                writeln!(self.out, "{level}while(true) {{")?;
2802                if let Some((_, ref break_and_inc)) = force_loop_bound_statements {
2803                    writeln!(self.out, "{break_and_inc}")?;
2804                }
2805                let l2 = level.next();
2806                if let Some(gate_name) = gate_name {
2807                    writeln!(self.out, "{l2}if (!{gate_name}) {{")?;
2808                    let l3 = l2.next();
2809                    for sta in continuing.iter() {
2810                        self.write_stmt(module, sta, func_ctx, l3)?;
2811                    }
2812                    if let Some(condition) = break_if {
2813                        write!(self.out, "{l3}if (")?;
2814                        self.write_expr(module, condition, func_ctx)?;
2815                        writeln!(self.out, ") {{")?;
2816                        writeln!(self.out, "{}break;", l3.next())?;
2817                        writeln!(self.out, "{l3}}}")?;
2818                    }
2819                    writeln!(self.out, "{l2}}}")?;
2820                    writeln!(self.out, "{l2}{gate_name} = false;")?;
2821                }
2822
2823                for sta in body.iter() {
2824                    self.write_stmt(module, sta, func_ctx, l2)?;
2825                }
2826
2827                writeln!(self.out, "{level}}}")?;
2828                self.continue_ctx.exit_loop();
2829            }
2830            Statement::Break => writeln!(self.out, "{level}break;")?,
2831            Statement::Continue => {
2832                if let Some(variable) = self.continue_ctx.continue_encountered() {
2833                    writeln!(self.out, "{level}{variable} = true;")?;
2834                    writeln!(self.out, "{level}break;")?
2835                } else {
2836                    writeln!(self.out, "{level}continue;")?
2837                }
2838            }
2839            Statement::ControlBarrier(barrier) => {
2840                self.write_control_barrier(barrier, level)?;
2841            }
2842            Statement::MemoryBarrier(barrier) => {
2843                self.write_memory_barrier(barrier, level)?;
2844            }
2845            Statement::ImageStore {
2846                image,
2847                coordinate,
2848                array_index,
2849                value,
2850            } => {
2851                write!(self.out, "{level}")?;
2852                self.write_expr(module, image, func_ctx)?;
2853
2854                write!(self.out, "[")?;
2855                if let Some(index) = array_index {
2856                    // Array index accepted only for texture_storage_2d_array, so we can safety use int3(coordinate, array_index) here
2857                    write!(self.out, "int3(")?;
2858                    self.write_expr(module, coordinate, func_ctx)?;
2859                    write!(self.out, ", ")?;
2860                    self.write_expr(module, index, func_ctx)?;
2861                    write!(self.out, ")")?;
2862                } else {
2863                    self.write_expr(module, coordinate, func_ctx)?;
2864                }
2865                write!(self.out, "]")?;
2866
2867                write!(self.out, " = ")?;
2868                self.write_expr(module, value, func_ctx)?;
2869                writeln!(self.out, ";")?;
2870            }
2871            Statement::Call {
2872                function,
2873                ref arguments,
2874                result,
2875            } => {
2876                write!(self.out, "{level}")?;
2877
2878                if let Some(expr) = result {
2879                    write!(self.out, "const ")?;
2880                    let name = Baked(expr).to_string();
2881                    let expr_ty = &func_ctx.info[expr].ty;
2882                    let ty_inner = match *expr_ty {
2883                        proc::TypeResolution::Handle(handle) => {
2884                            self.write_type(module, handle)?;
2885                            &module.types[handle].inner
2886                        }
2887                        proc::TypeResolution::Value(ref value) => {
2888                            self.write_value_type(module, value)?;
2889                            value
2890                        }
2891                    };
2892                    write!(self.out, " {name}")?;
2893                    if let TypeInner::Array { base, size, .. } = *ty_inner {
2894                        self.write_array_size(module, base, size)?;
2895                    }
2896                    write!(self.out, " = ")?;
2897                    self.named_expressions.insert(expr, name);
2898                }
2899                let func_name = &self.names[&NameKey::Function(function)];
2900                write!(self.out, "{func_name}(")?;
2901                let mut any_args_written = false;
2902                let mut separator = || {
2903                    if any_args_written {
2904                        ", "
2905                    } else {
2906                        any_args_written = true;
2907                        ""
2908                    }
2909                };
2910                for argument in arguments {
2911                    write!(self.out, "{}", separator())?;
2912                    self.write_expr(module, *argument, func_ctx)?;
2913                }
2914                if let Some(&var) = self.function_task_payload_var.get(&function) {
2915                    let name = &self.names[&NameKey::GlobalVariable(var)];
2916                    // Pass it through directly, whether its an in variable to this function or the global variable
2917                    write!(self.out, "{}{name}", separator())?;
2918                }
2919                writeln!(self.out, ");")?;
2920            }
2921            Statement::Atomic {
2922                pointer,
2923                ref fun,
2924                value,
2925                result,
2926            } => {
2927                write!(self.out, "{level}")?;
2928                let res_var_info = if let Some(res_handle) = result {
2929                    let name = Baked(res_handle).to_string();
2930                    match func_ctx.info[res_handle].ty {
2931                        proc::TypeResolution::Handle(handle) => self.write_type(module, handle)?,
2932                        proc::TypeResolution::Value(ref value) => {
2933                            self.write_value_type(module, value)?
2934                        }
2935                    };
2936                    write!(self.out, " {name}; ")?;
2937                    self.named_expressions.insert(res_handle, name.clone());
2938                    Some((res_handle, name))
2939                } else {
2940                    None
2941                };
2942                let pointer_space = func_ctx
2943                    .resolve_type(pointer, &module.types)
2944                    .pointer_space()
2945                    .unwrap();
2946                let fun_str = fun.to_hlsl_suffix();
2947                let compare_expr = match *fun {
2948                    crate::AtomicFunction::Exchange { compare: Some(cmp) } => Some(cmp),
2949                    _ => None,
2950                };
2951                match pointer_space {
2952                    crate::AddressSpace::WorkGroup => {
2953                        write!(self.out, "Interlocked{fun_str}(")?;
2954                        self.write_expr(module, pointer, func_ctx)?;
2955                        self.emit_hlsl_atomic_tail(
2956                            module,
2957                            func_ctx,
2958                            fun,
2959                            compare_expr,
2960                            value,
2961                            &res_var_info,
2962                        )?;
2963                    }
2964                    crate::AddressSpace::Storage { .. } => {
2965                        let var_handle = self.fill_access_chain(module, pointer, func_ctx)?;
2966                        let var_name = &self.names[&NameKey::GlobalVariable(var_handle)];
2967                        let width = match func_ctx.resolve_type(value, &module.types) {
2968                            &TypeInner::Scalar(Scalar { width: 8, .. }) => "64",
2969                            _ => "",
2970                        };
2971                        write!(self.out, "{var_name}.Interlocked{fun_str}{width}(")?;
2972                        let chain = mem::take(&mut self.temp_access_chain);
2973                        self.write_storage_address(module, &chain, func_ctx)?;
2974                        self.temp_access_chain = chain;
2975                        self.emit_hlsl_atomic_tail(
2976                            module,
2977                            func_ctx,
2978                            fun,
2979                            compare_expr,
2980                            value,
2981                            &res_var_info,
2982                        )?;
2983                    }
2984                    ref other => {
2985                        return Err(Error::Custom(format!(
2986                            "invalid address space {other:?} for atomic statement"
2987                        )))
2988                    }
2989                }
2990                if let Some(cmp) = compare_expr {
2991                    if let Some(&(_res_handle, ref res_name)) = res_var_info.as_ref() {
2992                        write!(
2993                            self.out,
2994                            "{level}{res_name}.exchanged = ({res_name}.old_value == "
2995                        )?;
2996                        self.write_expr(module, cmp, func_ctx)?;
2997                        writeln!(self.out, ");")?;
2998                    }
2999                }
3000            }
3001            Statement::ImageAtomic {
3002                image,
3003                coordinate,
3004                array_index,
3005                fun,
3006                value,
3007            } => {
3008                write!(self.out, "{level}")?;
3009
3010                let fun_str = fun.to_hlsl_suffix();
3011                write!(self.out, "Interlocked{fun_str}(")?;
3012                self.write_expr(module, image, func_ctx)?;
3013                write!(self.out, "[")?;
3014                self.write_texture_coordinates(
3015                    "int",
3016                    coordinate,
3017                    array_index,
3018                    None,
3019                    module,
3020                    func_ctx,
3021                )?;
3022                write!(self.out, "],")?;
3023
3024                self.write_expr(module, value, func_ctx)?;
3025                writeln!(self.out, ");")?;
3026            }
3027            Statement::WorkGroupUniformLoad { pointer, result } => {
3028                self.write_control_barrier(crate::Barrier::WORK_GROUP, level)?;
3029                write!(self.out, "{level}")?;
3030                let name = Baked(result).to_string();
3031                self.write_named_expr(module, pointer, name, result, func_ctx)?;
3032
3033                self.write_control_barrier(crate::Barrier::WORK_GROUP, level)?;
3034            }
3035            Statement::Switch {
3036                selector,
3037                ref cases,
3038            } => {
3039                self.write_switch(module, func_ctx, level, selector, cases)?;
3040            }
3041            Statement::RayQuery { query, ref fun } => {
3042                // There are three possibilities for a ptr to be:
3043                // 1. A variable
3044                // 2. A function argument
3045                // 3. part of a struct
3046                //
3047                // 2 and 3 are not possible, a ray query (in naga IR)
3048                // is not allowed to be passed into a function, and
3049                // all languages disallow it in a struct (you get fun results if
3050                // you try it :) ).
3051                //
3052                // Therefore, the ray query expression must be a variable.
3053                let crate::Expression::LocalVariable(query_var) = func_ctx.expressions[query]
3054                else {
3055                    unreachable!()
3056                };
3057
3058                let tracker_expr_name = format!(
3059                    "{RAY_QUERY_TRACKER_VARIABLE_PREFIX}{}",
3060                    self.names[&func_ctx.name_key(query_var)]
3061                );
3062
3063                match *fun {
3064                    RayQueryFunction::Initialize {
3065                        acceleration_structure,
3066                        descriptor,
3067                    } => {
3068                        self.write_initialize_function(
3069                            module,
3070                            level,
3071                            query,
3072                            acceleration_structure,
3073                            descriptor,
3074                            &tracker_expr_name,
3075                            func_ctx,
3076                        )?;
3077                    }
3078                    RayQueryFunction::Proceed { result } => {
3079                        self.write_proceed(
3080                            module,
3081                            level,
3082                            query,
3083                            result,
3084                            &tracker_expr_name,
3085                            func_ctx,
3086                        )?;
3087                    }
3088                    RayQueryFunction::GenerateIntersection { hit_t } => {
3089                        self.write_generate_intersection(
3090                            module,
3091                            level,
3092                            query,
3093                            hit_t,
3094                            &tracker_expr_name,
3095                            func_ctx,
3096                        )?;
3097                    }
3098                    RayQueryFunction::ConfirmIntersection => {
3099                        self.write_confirm_intersection(
3100                            module,
3101                            level,
3102                            query,
3103                            &tracker_expr_name,
3104                            func_ctx,
3105                        )?;
3106                    }
3107                    RayQueryFunction::Terminate => {
3108                        self.write_terminate(module, level, query, &tracker_expr_name, func_ctx)?;
3109                    }
3110                    RayQueryFunction::Begin => {
3111                        if self.options.ray_query_initialization_tracking {
3112                            writeln!(self.out, "{level}{tracker_expr_name} = 0u;")?;
3113                        }
3114                    }
3115                }
3116            }
3117            Statement::SubgroupBallot { result, predicate } => {
3118                write!(self.out, "{level}")?;
3119                let name = Baked(result).to_string();
3120                write!(self.out, "const uint4 {name} = ")?;
3121                self.named_expressions.insert(result, name);
3122
3123                write!(self.out, "WaveActiveBallot(")?;
3124                match predicate {
3125                    Some(predicate) => self.write_expr(module, predicate, func_ctx)?,
3126                    None => write!(self.out, "true")?,
3127                }
3128                writeln!(self.out, ");")?;
3129            }
3130            Statement::SubgroupCollectiveOperation {
3131                op,
3132                collective_op,
3133                argument,
3134                result,
3135            } => {
3136                write!(self.out, "{level}")?;
3137                write!(self.out, "const ")?;
3138                let name = Baked(result).to_string();
3139                match func_ctx.info[result].ty {
3140                    proc::TypeResolution::Handle(handle) => self.write_type(module, handle)?,
3141                    proc::TypeResolution::Value(ref value) => {
3142                        self.write_value_type(module, value)?
3143                    }
3144                };
3145                write!(self.out, " {name} = ")?;
3146                self.named_expressions.insert(result, name);
3147
3148                match (collective_op, op) {
3149                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::All) => {
3150                        write!(self.out, "WaveActiveAllTrue(")?
3151                    }
3152                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Any) => {
3153                        write!(self.out, "WaveActiveAnyTrue(")?
3154                    }
3155                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Add) => {
3156                        write!(self.out, "WaveActiveSum(")?
3157                    }
3158                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Mul) => {
3159                        write!(self.out, "WaveActiveProduct(")?
3160                    }
3161                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Max) => {
3162                        write!(self.out, "WaveActiveMax(")?
3163                    }
3164                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Min) => {
3165                        write!(self.out, "WaveActiveMin(")?
3166                    }
3167                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::And) => {
3168                        write!(self.out, "WaveActiveBitAnd(")?
3169                    }
3170                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Or) => {
3171                        write!(self.out, "WaveActiveBitOr(")?
3172                    }
3173                    (crate::CollectiveOperation::Reduce, crate::SubgroupOperation::Xor) => {
3174                        write!(self.out, "WaveActiveBitXor(")?
3175                    }
3176                    (crate::CollectiveOperation::ExclusiveScan, crate::SubgroupOperation::Add) => {
3177                        write!(self.out, "WavePrefixSum(")?
3178                    }
3179                    (crate::CollectiveOperation::ExclusiveScan, crate::SubgroupOperation::Mul) => {
3180                        write!(self.out, "WavePrefixProduct(")?
3181                    }
3182                    (crate::CollectiveOperation::InclusiveScan, crate::SubgroupOperation::Add) => {
3183                        self.write_expr(module, argument, func_ctx)?;
3184                        write!(self.out, " + WavePrefixSum(")?;
3185                    }
3186                    (crate::CollectiveOperation::InclusiveScan, crate::SubgroupOperation::Mul) => {
3187                        self.write_expr(module, argument, func_ctx)?;
3188                        write!(self.out, " * WavePrefixProduct(")?;
3189                    }
3190                    _ => unimplemented!(),
3191                }
3192                self.write_expr(module, argument, func_ctx)?;
3193                writeln!(self.out, ");")?;
3194            }
3195            Statement::SubgroupGather {
3196                mode,
3197                argument,
3198                result,
3199            } => {
3200                write!(self.out, "{level}")?;
3201                write!(self.out, "const ")?;
3202                let name = Baked(result).to_string();
3203                match func_ctx.info[result].ty {
3204                    proc::TypeResolution::Handle(handle) => self.write_type(module, handle)?,
3205                    proc::TypeResolution::Value(ref value) => {
3206                        self.write_value_type(module, value)?
3207                    }
3208                };
3209                write!(self.out, " {name} = ")?;
3210                self.named_expressions.insert(result, name);
3211                match mode {
3212                    crate::GatherMode::BroadcastFirst => {
3213                        write!(self.out, "WaveReadLaneFirst(")?;
3214                        self.write_expr(module, argument, func_ctx)?;
3215                    }
3216                    crate::GatherMode::QuadBroadcast(index) => {
3217                        write!(self.out, "QuadReadLaneAt(")?;
3218                        self.write_expr(module, argument, func_ctx)?;
3219                        write!(self.out, ", ")?;
3220                        self.write_expr(module, index, func_ctx)?;
3221                    }
3222                    crate::GatherMode::QuadSwap(direction) => {
3223                        match direction {
3224                            crate::Direction::X => {
3225                                write!(self.out, "QuadReadAcrossX(")?;
3226                            }
3227                            crate::Direction::Y => {
3228                                write!(self.out, "QuadReadAcrossY(")?;
3229                            }
3230                            crate::Direction::Diagonal => {
3231                                write!(self.out, "QuadReadAcrossDiagonal(")?;
3232                            }
3233                        }
3234                        self.write_expr(module, argument, func_ctx)?;
3235                    }
3236                    _ => {
3237                        write!(self.out, "WaveReadLaneAt(")?;
3238                        self.write_expr(module, argument, func_ctx)?;
3239                        write!(self.out, ", ")?;
3240                        match mode {
3241                            crate::GatherMode::BroadcastFirst => unreachable!(),
3242                            crate::GatherMode::Broadcast(index)
3243                            | crate::GatherMode::Shuffle(index) => {
3244                                self.write_expr(module, index, func_ctx)?;
3245                            }
3246                            crate::GatherMode::ShuffleDown(index) => {
3247                                write!(self.out, "WaveGetLaneIndex() + ")?;
3248                                self.write_expr(module, index, func_ctx)?;
3249                            }
3250                            crate::GatherMode::ShuffleUp(index) => {
3251                                write!(self.out, "WaveGetLaneIndex() - ")?;
3252                                self.write_expr(module, index, func_ctx)?;
3253                            }
3254                            crate::GatherMode::ShuffleXor(index) => {
3255                                write!(self.out, "WaveGetLaneIndex() ^ ")?;
3256                                self.write_expr(module, index, func_ctx)?;
3257                            }
3258                            crate::GatherMode::QuadBroadcast(_) => unreachable!(),
3259                            crate::GatherMode::QuadSwap(_) => unreachable!(),
3260                        }
3261                    }
3262                }
3263                writeln!(self.out, ");")?;
3264            }
3265            Statement::SubgroupBallotFindBit {
3266                order,
3267                argument,
3268                result,
3269            } => {
3270                // Evaluate each ballot word once into a local, rather than
3271                // repeatedly writing `argument.x`/`.y`/`.z`/`.w` in the
3272                // ternary chain below.
3273                const COMPONENTS: [char; 4] = ['x', 'y', 'z', 'w'];
3274                let mut words: [String; 4] = core::array::from_fn(|_| String::new());
3275                for (word, &component) in words.iter_mut().zip(&COMPONENTS) {
3276                    write!(self.out, "{level}const uint ")?;
3277                    let name = self.namer.call("");
3278                    write!(self.out, "{name} = ")?;
3279                    self.write_expr(module, argument, func_ctx)?;
3280                    writeln!(self.out, ".{component};")?;
3281                    *word = name;
3282                }
3283
3284                write!(self.out, "{level}")?;
3285                write!(self.out, "const ")?;
3286                let name = Baked(result).to_string();
3287                match func_ctx.info[result].ty {
3288                    proc::TypeResolution::Handle(handle) => self.write_type(module, handle)?,
3289                    proc::TypeResolution::Value(ref value) => {
3290                        self.write_value_type(module, value)?
3291                    }
3292                };
3293                write!(self.out, " {name} = ")?;
3294                self.named_expressions.insert(result, name);
3295
3296                self.write_ballot_find_bit(&words, order, 3)?;
3297                writeln!(self.out, ";")?;
3298            }
3299            Statement::CooperativeStore { .. } => unimplemented!(),
3300            Statement::RayPipelineFunction(_) | Statement::HitObject { .. } => unreachable!(),
3301            Statement::DebugPrintf { .. } => unimplemented!(),
3302        }
3303
3304        Ok(())
3305    }
3306
3307    #[allow(clippy::too_many_arguments)]
3308    fn write_math_expression(
3309        &mut self,
3310        module: &Module,
3311        fun: crate::MathFunction,
3312        arg: Handle<crate::Expression>,
3313        arg1: Option<Handle<crate::Expression>>,
3314        arg2: Option<Handle<crate::Expression>>,
3315        arg3: Option<Handle<crate::Expression>>,
3316        func_ctx: &back::FunctionCtx<'_>,
3317    ) -> BackendResult {
3318        use crate::MathFunction as Mf;
3319
3320        enum Function {
3321            Asincosh { is_sin: bool },
3322            Atanh,
3323            Pack2x16float,
3324            Pack2x16snorm,
3325            Pack2x16unorm,
3326            Pack4x8snorm,
3327            Pack4x8unorm,
3328            Pack4xI8,
3329            Pack4xU8,
3330            Pack4xI8Clamp,
3331            Pack4xU8Clamp,
3332            Unpack2x16float,
3333            Unpack2x16snorm,
3334            Unpack2x16unorm,
3335            Unpack4x8snorm,
3336            Unpack4x8unorm,
3337            Unpack4xI8,
3338            Unpack4xU8,
3339            Dot4I8Packed,
3340            Dot4U8Packed,
3341            QuantizeToF16,
3342            Sign,
3343            Regular(&'static str),
3344            MissingIntOverload(&'static str),
3345            MissingIntReturnType(&'static str),
3346            CountTrailingZeros,
3347            CountLeadingZeros,
3348        }
3349
3350        let fun = match fun {
3351            // comparison
3352            Mf::Abs => match func_ctx.resolve_type(arg, &module.types).scalar() {
3353                Some(Scalar::I32) => Function::Regular(ABS_FUNCTION),
3354                _ => Function::Regular("abs"),
3355            },
3356            Mf::Min => Function::Regular("min"),
3357            Mf::Max => Function::Regular("max"),
3358            Mf::Clamp => Function::Regular("clamp"),
3359            Mf::Saturate => Function::Regular("saturate"),
3360            // trigonometry
3361            Mf::Cos => Function::Regular("cos"),
3362            Mf::Cosh => Function::Regular("cosh"),
3363            Mf::Sin => Function::Regular("sin"),
3364            Mf::Sinh => Function::Regular("sinh"),
3365            Mf::Tan => Function::Regular("tan"),
3366            Mf::Tanh => Function::Regular("tanh"),
3367            Mf::Acos => Function::Regular("acos"),
3368            Mf::Asin => Function::Regular("asin"),
3369            Mf::Atan => Function::Regular("atan"),
3370            Mf::Atan2 => Function::Regular("atan2"),
3371            Mf::Asinh => Function::Asincosh { is_sin: true },
3372            Mf::Acosh => Function::Asincosh { is_sin: false },
3373            Mf::Atanh => Function::Atanh,
3374            Mf::Radians => Function::Regular("radians"),
3375            Mf::Degrees => Function::Regular("degrees"),
3376            // decomposition
3377            Mf::Ceil => Function::Regular("ceil"),
3378            Mf::Floor => Function::Regular("floor"),
3379            Mf::Round => Function::Regular("round"),
3380            Mf::Fract => Function::Regular("frac"),
3381            Mf::Trunc => Function::Regular("trunc"),
3382            Mf::Modf => Function::Regular(MODF_FUNCTION),
3383            Mf::Frexp => Function::Regular(FREXP_FUNCTION),
3384            Mf::Ldexp => Function::Regular("ldexp"),
3385            // exponent
3386            Mf::Exp => Function::Regular("exp"),
3387            Mf::Exp2 => Function::Regular("exp2"),
3388            Mf::Log => Function::Regular("log"),
3389            Mf::Log2 => Function::Regular("log2"),
3390            Mf::Pow => Function::Regular("pow"),
3391            // geometry
3392            Mf::Dot => Function::Regular("dot"),
3393            Mf::Dot4I8Packed => Function::Dot4I8Packed,
3394            Mf::Dot4U8Packed => Function::Dot4U8Packed,
3395            //Mf::Outer => ,
3396            Mf::Cross => Function::Regular("cross"),
3397            Mf::Distance => Function::Regular("distance"),
3398            Mf::Length => Function::Regular("length"),
3399            Mf::Normalize => Function::Regular("normalize"),
3400            Mf::FaceForward => Function::Regular("faceforward"),
3401            Mf::Reflect => Function::Regular("reflect"),
3402            Mf::Refract => Function::Regular("refract"),
3403            // computational
3404            Mf::Sign => Function::Sign,
3405            Mf::Fma => Function::Regular("mad"),
3406            Mf::Mix => Function::Regular("lerp"),
3407            Mf::Step => Function::Regular("step"),
3408            Mf::SmoothStep => Function::Regular("smoothstep"),
3409            Mf::Sqrt => Function::Regular("sqrt"),
3410            Mf::InverseSqrt => Function::Regular("rsqrt"),
3411            //Mf::Inverse =>,
3412            Mf::Transpose => Function::Regular("transpose"),
3413            Mf::Determinant => Function::Regular("determinant"),
3414            Mf::QuantizeToF16 => Function::QuantizeToF16,
3415            // bits
3416            Mf::CountTrailingZeros => Function::CountTrailingZeros,
3417            Mf::CountLeadingZeros => Function::CountLeadingZeros,
3418            Mf::CountOneBits => Function::MissingIntOverload("countbits"),
3419            Mf::ReverseBits => Function::MissingIntOverload("reversebits"),
3420            Mf::FirstTrailingBit => Function::MissingIntReturnType("firstbitlow"),
3421            Mf::FirstLeadingBit => Function::MissingIntReturnType("firstbithigh"),
3422            Mf::ExtractBits => Function::Regular(EXTRACT_BITS_FUNCTION),
3423            Mf::InsertBits => Function::Regular(INSERT_BITS_FUNCTION),
3424            // Data Packing
3425            Mf::Pack2x16float => Function::Pack2x16float,
3426            Mf::Pack2x16snorm => Function::Pack2x16snorm,
3427            Mf::Pack2x16unorm => Function::Pack2x16unorm,
3428            Mf::Pack4x8snorm => Function::Pack4x8snorm,
3429            Mf::Pack4x8unorm => Function::Pack4x8unorm,
3430            Mf::Pack4xI8 => Function::Pack4xI8,
3431            Mf::Pack4xU8 => Function::Pack4xU8,
3432            Mf::Pack4xI8Clamp => Function::Pack4xI8Clamp,
3433            Mf::Pack4xU8Clamp => Function::Pack4xU8Clamp,
3434            // Data Unpacking
3435            Mf::Unpack2x16float => Function::Unpack2x16float,
3436            Mf::Unpack2x16snorm => Function::Unpack2x16snorm,
3437            Mf::Unpack2x16unorm => Function::Unpack2x16unorm,
3438            Mf::Unpack4x8snorm => Function::Unpack4x8snorm,
3439            Mf::Unpack4x8unorm => Function::Unpack4x8unorm,
3440            Mf::Unpack4xI8 => Function::Unpack4xI8,
3441            Mf::Unpack4xU8 => Function::Unpack4xU8,
3442            _ => return Err(Error::Unimplemented(format!("write_expr_math {fun:?}"))),
3443        };
3444
3445        match fun {
3446            Function::Asincosh { is_sin } => {
3447                write!(self.out, "log(")?;
3448                self.write_expr(module, arg, func_ctx)?;
3449                write!(self.out, " + sqrt(")?;
3450                self.write_expr(module, arg, func_ctx)?;
3451                write!(self.out, " * ")?;
3452                self.write_expr(module, arg, func_ctx)?;
3453                match is_sin {
3454                    true => write!(self.out, " + 1.0))")?,
3455                    false => write!(self.out, " - 1.0))")?,
3456                }
3457            }
3458            Function::Atanh => {
3459                write!(self.out, "0.5 * log((1.0 + ")?;
3460                self.write_expr(module, arg, func_ctx)?;
3461                write!(self.out, ") / (1.0 - ")?;
3462                self.write_expr(module, arg, func_ctx)?;
3463                write!(self.out, "))")?;
3464            }
3465            Function::Pack2x16float => {
3466                write!(self.out, "(f32tof16(")?;
3467                self.write_expr(module, arg, func_ctx)?;
3468                write!(self.out, "[0]) | f32tof16(")?;
3469                self.write_expr(module, arg, func_ctx)?;
3470                write!(self.out, "[1]) << 16)")?;
3471            }
3472            Function::Pack2x16snorm => {
3473                let scale = 32767;
3474
3475                write!(self.out, "uint((int(round(clamp(")?;
3476                self.write_expr(module, arg, func_ctx)?;
3477                write!(
3478                    self.out,
3479                    "[0], -1.0, 1.0) * {scale}.0)) & 0xFFFF) | ((int(round(clamp("
3480                )?;
3481                self.write_expr(module, arg, func_ctx)?;
3482                write!(self.out, "[1], -1.0, 1.0) * {scale}.0)) & 0xFFFF) << 16))",)?;
3483            }
3484            Function::Pack2x16unorm => {
3485                let scale = 65535;
3486
3487                write!(self.out, "(uint(round(clamp(")?;
3488                self.write_expr(module, arg, func_ctx)?;
3489                write!(self.out, "[0], 0.0, 1.0) * {scale}.0)) | uint(round(clamp(")?;
3490                self.write_expr(module, arg, func_ctx)?;
3491                write!(self.out, "[1], 0.0, 1.0) * {scale}.0)) << 16)")?;
3492            }
3493            Function::Pack4x8snorm => {
3494                let scale = 127;
3495
3496                write!(self.out, "uint((int(round(clamp(")?;
3497                self.write_expr(module, arg, func_ctx)?;
3498                write!(
3499                    self.out,
3500                    "[0], -1.0, 1.0) * {scale}.0)) & 0xFF) | ((int(round(clamp("
3501                )?;
3502                self.write_expr(module, arg, func_ctx)?;
3503                write!(
3504                    self.out,
3505                    "[1], -1.0, 1.0) * {scale}.0)) & 0xFF) << 8) | ((int(round(clamp("
3506                )?;
3507                self.write_expr(module, arg, func_ctx)?;
3508                write!(
3509                    self.out,
3510                    "[2], -1.0, 1.0) * {scale}.0)) & 0xFF) << 16) | ((int(round(clamp("
3511                )?;
3512                self.write_expr(module, arg, func_ctx)?;
3513                write!(self.out, "[3], -1.0, 1.0) * {scale}.0)) & 0xFF) << 24))",)?;
3514            }
3515            Function::Pack4x8unorm => {
3516                let scale = 255;
3517
3518                write!(self.out, "(uint(round(clamp(")?;
3519                self.write_expr(module, arg, func_ctx)?;
3520                write!(self.out, "[0], 0.0, 1.0) * {scale}.0)) | uint(round(clamp(")?;
3521                self.write_expr(module, arg, func_ctx)?;
3522                write!(
3523                    self.out,
3524                    "[1], 0.0, 1.0) * {scale}.0)) << 8 | uint(round(clamp("
3525                )?;
3526                self.write_expr(module, arg, func_ctx)?;
3527                write!(
3528                    self.out,
3529                    "[2], 0.0, 1.0) * {scale}.0)) << 16 | uint(round(clamp("
3530                )?;
3531                self.write_expr(module, arg, func_ctx)?;
3532                write!(self.out, "[3], 0.0, 1.0) * {scale}.0)) << 24)")?;
3533            }
3534            fun @ (Function::Pack4xI8
3535            | Function::Pack4xU8
3536            | Function::Pack4xI8Clamp
3537            | Function::Pack4xU8Clamp) => {
3538                let was_signed = matches!(fun, Function::Pack4xI8 | Function::Pack4xI8Clamp);
3539                let clamp_bounds = match fun {
3540                    Function::Pack4xI8Clamp => Some(("-128", "127")),
3541                    Function::Pack4xU8Clamp => Some(("0", "255")),
3542                    _ => None,
3543                };
3544                if was_signed {
3545                    write!(self.out, "uint(")?;
3546                }
3547                let write_arg = |this: &mut Self| -> BackendResult {
3548                    if let Some((min, max)) = clamp_bounds {
3549                        write!(this.out, "clamp(")?;
3550                        this.write_expr(module, arg, func_ctx)?;
3551                        write!(this.out, ", {min}, {max})")?;
3552                    } else {
3553                        this.write_expr(module, arg, func_ctx)?;
3554                    }
3555                    Ok(())
3556                };
3557                write!(self.out, "(")?;
3558                write_arg(self)?;
3559                write!(self.out, "[0] & 0xFF) | ((")?;
3560                write_arg(self)?;
3561                write!(self.out, "[1] & 0xFF) << 8) | ((")?;
3562                write_arg(self)?;
3563                write!(self.out, "[2] & 0xFF) << 16) | ((")?;
3564                write_arg(self)?;
3565                write!(self.out, "[3] & 0xFF) << 24)")?;
3566                if was_signed {
3567                    write!(self.out, ")")?;
3568                }
3569            }
3570
3571            Function::Unpack2x16float => {
3572                write!(self.out, "float2(f16tof32(")?;
3573                self.write_expr(module, arg, func_ctx)?;
3574                write!(self.out, "), f16tof32((")?;
3575                self.write_expr(module, arg, func_ctx)?;
3576                write!(self.out, ") >> 16))")?;
3577            }
3578            Function::Unpack2x16snorm => {
3579                let scale = 32767;
3580
3581                write!(self.out, "(float2(int2(")?;
3582                self.write_expr(module, arg, func_ctx)?;
3583                write!(self.out, " << 16, ")?;
3584                self.write_expr(module, arg, func_ctx)?;
3585                write!(self.out, ") >> 16) / {scale}.0)")?;
3586            }
3587            Function::Unpack2x16unorm => {
3588                let scale = 65535;
3589
3590                write!(self.out, "(float2(")?;
3591                self.write_expr(module, arg, func_ctx)?;
3592                write!(self.out, " & 0xFFFF, ")?;
3593                self.write_expr(module, arg, func_ctx)?;
3594                write!(self.out, " >> 16) / {scale}.0)")?;
3595            }
3596            Function::Unpack4x8snorm => {
3597                let scale = 127;
3598
3599                write!(self.out, "(float4(int4(")?;
3600                self.write_expr(module, arg, func_ctx)?;
3601                write!(self.out, " << 24, ")?;
3602                self.write_expr(module, arg, func_ctx)?;
3603                write!(self.out, " << 16, ")?;
3604                self.write_expr(module, arg, func_ctx)?;
3605                write!(self.out, " << 8, ")?;
3606                self.write_expr(module, arg, func_ctx)?;
3607                write!(self.out, ") >> 24) / {scale}.0)")?;
3608            }
3609            Function::Unpack4x8unorm => {
3610                let scale = 255;
3611
3612                write!(self.out, "(float4(")?;
3613                self.write_expr(module, arg, func_ctx)?;
3614                write!(self.out, " & 0xFF, ")?;
3615                self.write_expr(module, arg, func_ctx)?;
3616                write!(self.out, " >> 8 & 0xFF, ")?;
3617                self.write_expr(module, arg, func_ctx)?;
3618                write!(self.out, " >> 16 & 0xFF, ")?;
3619                self.write_expr(module, arg, func_ctx)?;
3620                write!(self.out, " >> 24) / {scale}.0)")?;
3621            }
3622            fun @ (Function::Unpack4xI8 | Function::Unpack4xU8) => {
3623                write!(self.out, "(")?;
3624                if matches!(fun, Function::Unpack4xU8) {
3625                    write!(self.out, "u")?;
3626                }
3627                write!(self.out, "int4(")?;
3628                self.write_expr(module, arg, func_ctx)?;
3629                write!(self.out, ", ")?;
3630                self.write_expr(module, arg, func_ctx)?;
3631                write!(self.out, " >> 8, ")?;
3632                self.write_expr(module, arg, func_ctx)?;
3633                write!(self.out, " >> 16, ")?;
3634                self.write_expr(module, arg, func_ctx)?;
3635                write!(self.out, " >> 24) << 24 >> 24)")?;
3636            }
3637            fun @ (Function::Dot4I8Packed | Function::Dot4U8Packed) => {
3638                let arg1 = arg1.unwrap();
3639
3640                if self.options.shader_model >= ShaderModel::V6_4 {
3641                    // Intrinsics `dot4add_{i, u}8packed` are available in SM 6.4 and later.
3642                    let function_name = match fun {
3643                        Function::Dot4I8Packed => "dot4add_i8packed",
3644                        Function::Dot4U8Packed => "dot4add_u8packed",
3645                        _ => unreachable!(),
3646                    };
3647                    write!(self.out, "{function_name}(")?;
3648                    self.write_expr(module, arg, func_ctx)?;
3649                    write!(self.out, ", ")?;
3650                    self.write_expr(module, arg1, func_ctx)?;
3651                    write!(self.out, ", 0)")?;
3652                } else {
3653                    // Fall back to a polyfill as `dot4add_u8packed` is not available.
3654                    write!(self.out, "dot(")?;
3655
3656                    if matches!(fun, Function::Dot4U8Packed) {
3657                        write!(self.out, "u")?;
3658                    }
3659                    write!(self.out, "int4(")?;
3660                    self.write_expr(module, arg, func_ctx)?;
3661                    write!(self.out, ", ")?;
3662                    self.write_expr(module, arg, func_ctx)?;
3663                    write!(self.out, " >> 8, ")?;
3664                    self.write_expr(module, arg, func_ctx)?;
3665                    write!(self.out, " >> 16, ")?;
3666                    self.write_expr(module, arg, func_ctx)?;
3667                    write!(self.out, " >> 24) << 24 >> 24, ")?;
3668
3669                    if matches!(fun, Function::Dot4U8Packed) {
3670                        write!(self.out, "u")?;
3671                    }
3672                    write!(self.out, "int4(")?;
3673                    self.write_expr(module, arg1, func_ctx)?;
3674                    write!(self.out, ", ")?;
3675                    self.write_expr(module, arg1, func_ctx)?;
3676                    write!(self.out, " >> 8, ")?;
3677                    self.write_expr(module, arg1, func_ctx)?;
3678                    write!(self.out, " >> 16, ")?;
3679                    self.write_expr(module, arg1, func_ctx)?;
3680                    write!(self.out, " >> 24) << 24 >> 24)")?;
3681                }
3682            }
3683            Function::QuantizeToF16 => {
3684                write!(self.out, "f16tof32(f32tof16(")?;
3685                self.write_expr(module, arg, func_ctx)?;
3686                write!(self.out, "))")?;
3687            }
3688            // HLSL's `sign` always returns a signed integer, even for floating-point
3689            // arguments, so cast the result back to the WGSL result type.
3690            Function::Sign => {
3691                let inner = func_ctx.resolve_type(arg, &module.types);
3692                let is_float = inner.scalar_kind() == Some(ScalarKind::Float);
3693                if is_float {
3694                    self.write_value_type(module, inner)?;
3695                    write!(self.out, "(")?;
3696                }
3697                write!(self.out, "sign(")?;
3698                self.write_expr(module, arg, func_ctx)?;
3699                write!(self.out, ")")?;
3700                if is_float {
3701                    write!(self.out, ")")?;
3702                }
3703            }
3704            Function::Regular(fun_name) => {
3705                write!(self.out, "{fun_name}(")?;
3706                self.write_expr(module, arg, func_ctx)?;
3707                if let Some(arg) = arg1 {
3708                    write!(self.out, ", ")?;
3709                    self.write_expr(module, arg, func_ctx)?;
3710                }
3711                if let Some(arg) = arg2 {
3712                    write!(self.out, ", ")?;
3713                    self.write_expr(module, arg, func_ctx)?;
3714                }
3715                if let Some(arg) = arg3 {
3716                    write!(self.out, ", ")?;
3717                    self.write_expr(module, arg, func_ctx)?;
3718                }
3719                write!(self.out, ")")?
3720            }
3721            // These overloads are only missing on FXC, so this is only needed for 32bit types,
3722            // as non-32bit types are DXC only.
3723            Function::MissingIntOverload(fun_name) => {
3724                let scalar_kind = func_ctx.resolve_type(arg, &module.types).scalar();
3725                if let Some(Scalar::I32) = scalar_kind {
3726                    write!(self.out, "asint({fun_name}(asuint(")?;
3727                    self.write_expr(module, arg, func_ctx)?;
3728                    write!(self.out, ")))")?;
3729                } else {
3730                    write!(self.out, "{fun_name}(")?;
3731                    self.write_expr(module, arg, func_ctx)?;
3732                    write!(self.out, ")")?;
3733                }
3734            }
3735            // These overloads are only missing on FXC, so this is only needed for 32bit types,
3736            // as non-32bit types are DXC only.
3737            Function::MissingIntReturnType(fun_name) => {
3738                let scalar_kind = func_ctx.resolve_type(arg, &module.types).scalar();
3739                if let Some(Scalar::I32) = scalar_kind {
3740                    write!(self.out, "asint({fun_name}(")?;
3741                    self.write_expr(module, arg, func_ctx)?;
3742                    write!(self.out, "))")?;
3743                } else {
3744                    write!(self.out, "{fun_name}(")?;
3745                    self.write_expr(module, arg, func_ctx)?;
3746                    write!(self.out, ")")?;
3747                }
3748            }
3749            Function::CountTrailingZeros => {
3750                match *func_ctx.resolve_type(arg, &module.types) {
3751                    TypeInner::Vector { size, scalar } => {
3752                        let s = match size {
3753                            crate::VectorSize::Bi => ".xx",
3754                            crate::VectorSize::Tri => ".xxx",
3755                            crate::VectorSize::Quad => ".xxxx",
3756                        };
3757
3758                        let scalar_width_bits = scalar.width * 8;
3759
3760                        if scalar.kind == ScalarKind::Uint || scalar.width != 4 {
3761                            write!(self.out, "min(({scalar_width_bits}u){s}, firstbitlow(")?;
3762                            self.write_expr(module, arg, func_ctx)?;
3763                            write!(self.out, "))")?;
3764                        } else {
3765                            // This is only needed for the FXC path, on 32bit signed integers.
3766                            write!(
3767                                self.out,
3768                                "asint(min(({scalar_width_bits}u){s}, firstbitlow("
3769                            )?;
3770                            self.write_expr(module, arg, func_ctx)?;
3771                            write!(self.out, ")))")?;
3772                        }
3773                    }
3774                    TypeInner::Scalar(scalar) => {
3775                        let scalar_width_bits = scalar.width * 8;
3776
3777                        if scalar.kind == ScalarKind::Uint || scalar.width != 4 {
3778                            write!(self.out, "min({scalar_width_bits}u, firstbitlow(")?;
3779                            self.write_expr(module, arg, func_ctx)?;
3780                            write!(self.out, "))")?;
3781                        } else {
3782                            // This is only needed for the FXC path, on 32bit signed integers.
3783                            write!(self.out, "asint(min({scalar_width_bits}u, firstbitlow(")?;
3784                            self.write_expr(module, arg, func_ctx)?;
3785                            write!(self.out, ")))")?;
3786                        }
3787                    }
3788                    _ => unreachable!(),
3789                }
3790
3791                return Ok(());
3792            }
3793            Function::CountLeadingZeros => {
3794                match *func_ctx.resolve_type(arg, &module.types) {
3795                    TypeInner::Vector { size, scalar } => {
3796                        let s = match size {
3797                            crate::VectorSize::Bi => ".xx",
3798                            crate::VectorSize::Tri => ".xxx",
3799                            crate::VectorSize::Quad => ".xxxx",
3800                        };
3801
3802                        // scalar width - 1
3803                        let constant = scalar.width * 8 - 1;
3804
3805                        if scalar.kind == ScalarKind::Uint {
3806                            write!(self.out, "(({constant}u){s} - firstbithigh(")?;
3807                            self.write_expr(module, arg, func_ctx)?;
3808                            write!(self.out, "))")?;
3809                        } else {
3810                            let conversion_func = match scalar.width {
3811                                4 => "asint",
3812                                _ => "",
3813                            };
3814                            write!(self.out, "(")?;
3815                            self.write_expr(module, arg, func_ctx)?;
3816                            write!(
3817                                self.out,
3818                                " < (0){s} ? (0){s} : ({constant}){s} - {conversion_func}(firstbithigh("
3819                            )?;
3820                            self.write_expr(module, arg, func_ctx)?;
3821                            write!(self.out, ")))")?;
3822                        }
3823                    }
3824                    TypeInner::Scalar(scalar) => {
3825                        // scalar width - 1
3826                        let constant = scalar.width * 8 - 1;
3827
3828                        if let ScalarKind::Uint = scalar.kind {
3829                            write!(self.out, "({constant}u - firstbithigh(")?;
3830                            self.write_expr(module, arg, func_ctx)?;
3831                            write!(self.out, "))")?;
3832                        } else {
3833                            let conversion_func = match scalar.width {
3834                                4 => "asint",
3835                                _ => "",
3836                            };
3837                            write!(self.out, "(")?;
3838                            self.write_expr(module, arg, func_ctx)?;
3839                            write!(
3840                                self.out,
3841                                " < 0 ? 0 : {constant} - {conversion_func}(firstbithigh("
3842                            )?;
3843                            self.write_expr(module, arg, func_ctx)?;
3844                            write!(self.out, ")))")?;
3845                        }
3846                    }
3847                    _ => unreachable!(),
3848                }
3849            }
3850        }
3851
3852        Ok(())
3853    }
3854
3855    fn write_const_expression(
3856        &mut self,
3857        module: &Module,
3858        expr: Handle<crate::Expression>,
3859        arena: &crate::Arena<crate::Expression>,
3860    ) -> BackendResult {
3861        self.write_possibly_const_expression(module, expr, arena, |writer, expr| {
3862            writer.write_const_expression(module, expr, arena)
3863        })
3864    }
3865
3866    pub(super) fn write_literal(&mut self, literal: crate::Literal) -> BackendResult {
3867        match literal {
3868            crate::Literal::F64(value) => write!(self.out, "{value:?}L")?,
3869            crate::Literal::F32(value) => write!(self.out, "{value:?}")?,
3870            crate::Literal::F16(value) => write!(self.out, "{value:?}h")?,
3871            crate::Literal::U16(value) => write!(self.out, "uint16_t({value})")?,
3872            crate::Literal::I16(value) => write!(self.out, "int16_t({value})")?,
3873            crate::Literal::U32(value) => write!(self.out, "{value}u")?,
3874            // `-2147483648` is parsed by some compilers as unary negation of
3875            // positive 2147483648, which is too large for an int, causing
3876            // issues for some compilers. Neither DXC nor FXC appear to have
3877            // this problem, but this is not specified and could change. We
3878            // therefore use `-2147483647 - 1` as a precaution.
3879            crate::Literal::I32(value) if value == i32::MIN => {
3880                write!(self.out, "int({} - 1)", value + 1)?
3881            }
3882            // HLSL has no suffix for explicit i32 literals, but not using any suffix
3883            // makes the type ambiguous which prevents overload resolution from
3884            // working. So we explicitly use the int() constructor syntax.
3885            crate::Literal::I32(value) => write!(self.out, "int({value})")?,
3886            crate::Literal::U64(value) => write!(self.out, "{value}ULL")?,
3887            // I64 version of the minimum I32 value issue described above.
3888            crate::Literal::I64(value) if value == i64::MIN => {
3889                write!(self.out, "({}LL - 1LL)", value + 1)?;
3890            }
3891            crate::Literal::I64(value) => write!(self.out, "{value}LL")?,
3892            crate::Literal::Bool(value) => write!(self.out, "{value}")?,
3893            crate::Literal::AbstractInt(_) | crate::Literal::AbstractFloat(_) => {
3894                return Err(Error::Custom(
3895                    "Abstract types should not appear in IR presented to backends".into(),
3896                ));
3897            }
3898        }
3899        Ok(())
3900    }
3901
3902    fn write_possibly_const_expression<E>(
3903        &mut self,
3904        module: &Module,
3905        expr: Handle<crate::Expression>,
3906        expressions: &crate::Arena<crate::Expression>,
3907        write_expression: E,
3908    ) -> BackendResult
3909    where
3910        E: Fn(&mut Self, Handle<crate::Expression>) -> BackendResult,
3911    {
3912        use crate::Expression;
3913
3914        match expressions[expr] {
3915            Expression::Literal(literal) => {
3916                self.write_literal(literal)?;
3917            }
3918            Expression::Constant(handle) => {
3919                let constant = &module.constants[handle];
3920                if constant.name.is_some() {
3921                    write!(self.out, "{}", self.names[&NameKey::Constant(handle)])?;
3922                } else {
3923                    self.write_const_expression(module, constant.init, &module.global_expressions)?;
3924                }
3925            }
3926            Expression::ZeroValue(ty) => {
3927                self.write_wrapped_zero_value_function_name(module, WrappedZeroValue { ty })?;
3928                write!(self.out, "()")?;
3929            }
3930            Expression::Compose { ty, ref components } => {
3931                match module.types[ty].inner {
3932                    TypeInner::Struct { .. } | TypeInner::Array { .. } => {
3933                        self.write_wrapped_constructor_function_name(
3934                            module,
3935                            WrappedConstructor { ty },
3936                        )?;
3937                    }
3938                    _ => {
3939                        self.write_type(module, ty)?;
3940                    }
3941                };
3942                write!(self.out, "(")?;
3943                for (index, component) in components.iter().enumerate() {
3944                    if index != 0 {
3945                        write!(self.out, ", ")?;
3946                    }
3947                    write_expression(self, *component)?;
3948                }
3949                write!(self.out, ")")?;
3950            }
3951            Expression::Splat { size, value } => {
3952                // hlsl is not supported one value constructor
3953                // if we write, for example, int4(0), dxc returns error:
3954                // error: too few elements in vector initialization (expected 4 elements, have 1)
3955                let number_of_components = match size {
3956                    crate::VectorSize::Bi => "xx",
3957                    crate::VectorSize::Tri => "xxx",
3958                    crate::VectorSize::Quad => "xxxx",
3959                };
3960                write!(self.out, "(")?;
3961                write_expression(self, value)?;
3962                write!(self.out, ").{number_of_components}")?
3963            }
3964            _ => {
3965                return Err(Error::Override);
3966            }
3967        }
3968
3969        Ok(())
3970    }
3971
3972    /// Write a polyfill for [`Statement::SubgroupBallotFindBit`], since HLSL has no
3973    /// built-in for it: a chain of ternaries over the already-evaluated ballot
3974    /// `words` (`words[0..4]` is `x, y, z, w`). `priority` counts up from 0 (the
3975    /// lowest-priority word, used as the fallback base case) to 3 (the
3976    /// highest-priority word, checked first: `x` for LSB, `w` for MSB), each
3977    /// overriding the lower-priority result when that word has any bit set.
3978    ///
3979    /// [`Statement::SubgroupBallotFindBit`]: crate::Statement::SubgroupBallotFindBit
3980    fn write_ballot_find_bit(
3981        &mut self,
3982        words: &[String; 4],
3983        order: crate::BallotFindBitOrder,
3984        priority: u32,
3985    ) -> BackendResult {
3986        let (word_index, base) = match (order, priority) {
3987            (crate::BallotFindBitOrder::Lsb, 0) => (3, 96),
3988            (crate::BallotFindBitOrder::Lsb, 1) => (2, 64),
3989            (crate::BallotFindBitOrder::Lsb, 2) => (1, 32),
3990            (crate::BallotFindBitOrder::Lsb, 3) => (0, 0),
3991            (crate::BallotFindBitOrder::Msb, 0) => (0, 0),
3992            (crate::BallotFindBitOrder::Msb, 1) => (1, 32),
3993            (crate::BallotFindBitOrder::Msb, 2) => (2, 64),
3994            (crate::BallotFindBitOrder::Msb, 3) => (3, 96),
3995            (_, _) => unreachable!(),
3996        };
3997        let word = &words[word_index];
3998
3999        if priority == 0 {
4000            self.write_ballot_find_bit_value(order, word, base)?;
4001        } else {
4002            write!(self.out, "({word} != 0u ? ")?;
4003            self.write_ballot_find_bit_value(order, word, base)?;
4004            write!(self.out, " : ")?;
4005            self.write_ballot_find_bit(words, order, priority - 1)?;
4006            write!(self.out, ")")?;
4007        }
4008        Ok(())
4009    }
4010
4011    /// Write `firstbitlow`/`firstbithigh` applied to an already-evaluated ballot
4012    /// word, offset by that word's base invocation index. Used by
4013    /// [`Self::write_ballot_find_bit`].
4014    fn write_ballot_find_bit_value(
4015        &mut self,
4016        order: crate::BallotFindBitOrder,
4017        word: &str,
4018        base: u32,
4019    ) -> BackendResult {
4020        let math_fn = match order {
4021            crate::BallotFindBitOrder::Lsb => "firstbitlow",
4022            crate::BallotFindBitOrder::Msb => "firstbithigh",
4023        };
4024        write!(self.out, "{math_fn}({word})")?;
4025        if base != 0 {
4026            write!(self.out, " + {base}u")?;
4027        }
4028        Ok(())
4029    }
4030
4031    /// Helper method to write expressions
4032    ///
4033    /// # Notes
4034    /// Doesn't add any newlines or leading/trailing spaces
4035    pub(super) fn write_expr(
4036        &mut self,
4037        module: &Module,
4038        expr: Handle<crate::Expression>,
4039        func_ctx: &back::FunctionCtx<'_>,
4040    ) -> BackendResult {
4041        use crate::Expression;
4042
4043        // Handle the special semantics of vertex_index/instance_index
4044        let ff_input = if self.options.special_constants_binding.is_some() {
4045            func_ctx.is_fixed_function_input(expr, module)
4046        } else {
4047            None
4048        };
4049        let closing_bracket = match ff_input {
4050            Some(crate::BuiltIn::VertexIndex) => {
4051                write!(self.out, "({SPECIAL_CBUF_VAR}.{SPECIAL_FIRST_VERTEX} + ")?;
4052                ")"
4053            }
4054            Some(crate::BuiltIn::InstanceIndex) => {
4055                write!(self.out, "({SPECIAL_CBUF_VAR}.{SPECIAL_FIRST_INSTANCE} + ",)?;
4056                ")"
4057            }
4058            Some(crate::BuiltIn::NumWorkGroups) => {
4059                // Note: despite their names (`FIRST_VERTEX` and `FIRST_INSTANCE`),
4060                // in compute shaders the special constants contain the number
4061                // of workgroups, which we are using here.
4062                write!(
4063                    self.out,
4064                    "uint3({SPECIAL_CBUF_VAR}.{SPECIAL_FIRST_VERTEX}, {SPECIAL_CBUF_VAR}.{SPECIAL_FIRST_INSTANCE}, {SPECIAL_CBUF_VAR}.{SPECIAL_OTHER})",
4065                )?;
4066                return Ok(());
4067            }
4068            _ => "",
4069        };
4070
4071        if let Some(name) = self.named_expressions.get(&expr) {
4072            write!(self.out, "{name}{closing_bracket}")?;
4073            return Ok(());
4074        }
4075
4076        let expression = &func_ctx.expressions[expr];
4077
4078        match *expression {
4079            Expression::Literal(_)
4080            | Expression::Constant(_)
4081            | Expression::ZeroValue(_)
4082            | Expression::Compose { .. }
4083            | Expression::Splat { .. } => {
4084                self.write_possibly_const_expression(
4085                    module,
4086                    expr,
4087                    func_ctx.expressions,
4088                    |writer, expr| writer.write_expr(module, expr, func_ctx),
4089                )?;
4090            }
4091            Expression::Override(_) => return Err(Error::Override),
4092            // Avoid undefined behaviour for addition, subtraction, and
4093            // multiplication of signed integers by casting operands to
4094            // unsigned, performing the operation, then casting the result back
4095            // to signed.
4096            // TODO(#7109): This relies on the asint()/asuint() functions which only work
4097            // for 32-bit types, so we must find another solution for different bit widths.
4098            Expression::Binary {
4099                op:
4100                    op @ crate::BinaryOperator::Add
4101                    | op @ crate::BinaryOperator::Subtract
4102                    | op @ crate::BinaryOperator::Multiply,
4103                left,
4104                right,
4105            } if matches!(
4106                func_ctx.resolve_type(expr, &module.types).scalar(),
4107                Some(Scalar::I32)
4108            ) =>
4109            {
4110                write!(self.out, "asint(asuint(",)?;
4111                self.write_expr(module, left, func_ctx)?;
4112                write!(self.out, ") {} asuint(", back::binary_operation_str(op))?;
4113                self.write_expr(module, right, func_ctx)?;
4114                write!(self.out, "))")?;
4115            }
4116            // All of the multiplication can be expressed as `mul`,
4117            // except vector * vector, which needs to use the "*" operator.
4118            Expression::Binary {
4119                op: crate::BinaryOperator::Multiply,
4120                left,
4121                right,
4122            } if func_ctx.resolve_type(left, &module.types).is_matrix()
4123                || func_ctx.resolve_type(right, &module.types).is_matrix() =>
4124            {
4125                // We intentionally flip the order of multiplication as our matrices are implicitly transposed.
4126                write!(self.out, "mul(")?;
4127                self.write_expr(module, right, func_ctx)?;
4128                write!(self.out, ", ")?;
4129                self.write_expr(module, left, func_ctx)?;
4130                write!(self.out, ")")?;
4131            }
4132
4133            // WGSL says that floating-point division by zero should return
4134            // infinity. Microsoft's Direct3D 11 functional specification
4135            // (https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm)
4136            // says:
4137            //
4138            //     Divide by 0 produces +/- INF, except 0/0 which results in NaN.
4139            //
4140            // which is what we want. The DXIL specification for the FDiv
4141            // instruction corroborates this:
4142            //
4143            // https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#fdiv
4144            Expression::Binary {
4145                op: crate::BinaryOperator::Divide,
4146                left,
4147                right,
4148            } if matches!(
4149                func_ctx.resolve_type(expr, &module.types).scalar_kind(),
4150                Some(ScalarKind::Sint | ScalarKind::Uint)
4151            ) =>
4152            {
4153                write!(self.out, "{DIV_FUNCTION}(")?;
4154                self.write_expr(module, left, func_ctx)?;
4155                write!(self.out, ", ")?;
4156                self.write_expr(module, right, func_ctx)?;
4157                write!(self.out, ")")?;
4158            }
4159
4160            Expression::Binary {
4161                op: crate::BinaryOperator::Modulo,
4162                left,
4163                right,
4164            } if matches!(
4165                func_ctx.resolve_type(expr, &module.types).scalar_kind(),
4166                Some(ScalarKind::Sint | ScalarKind::Uint | ScalarKind::Float)
4167            ) =>
4168            {
4169                write!(self.out, "{MOD_FUNCTION}(")?;
4170                self.write_expr(module, left, func_ctx)?;
4171                write!(self.out, ", ")?;
4172                self.write_expr(module, right, func_ctx)?;
4173                write!(self.out, ")")?;
4174            }
4175
4176            Expression::Binary { op, left, right } => {
4177                write!(self.out, "(")?;
4178                self.write_expr(module, left, func_ctx)?;
4179                write!(self.out, " {} ", back::binary_operation_str(op))?;
4180                self.write_expr(module, right, func_ctx)?;
4181                write!(self.out, ")")?;
4182            }
4183            Expression::Access { base, index } => {
4184                if let Some(crate::AddressSpace::Storage { .. }) =
4185                    func_ctx.resolve_type(expr, &module.types).pointer_space()
4186                {
4187                    // do nothing, the chain is written on `Load`/`Store`
4188                } else {
4189                    // We use the function __get_col_of_matCx2 here in cases
4190                    // where `base`s type resolves to a matCx2 and is part of a
4191                    // struct member with type of (possibly nested) array of matCx2's.
4192                    //
4193                    // Note that this only works for `Load`s and we handle
4194                    // `Store`s differently in `Statement::Store`.
4195                    if let Some(MatrixType {
4196                        columns,
4197                        rows: crate::VectorSize::Bi,
4198                        width,
4199                    }) = get_inner_matrix_of_struct_array_member(module, base, func_ctx, true)
4200                        .or_else(|| {
4201                            get_inner_matrix_of_global_uniform(module, base, func_ctx, true)
4202                        })
4203                    {
4204                        write!(
4205                            self.out,
4206                            "__get_col_of_mat{}x2_f{}(",
4207                            columns as u8,
4208                            width * 8
4209                        )?;
4210                        self.write_expr(module, base, func_ctx)?;
4211                        write!(self.out, ", ")?;
4212                        self.write_expr(module, index, func_ctx)?;
4213                        write!(self.out, ")")?;
4214                        return Ok(());
4215                    }
4216
4217                    let resolved = func_ctx.resolve_type(base, &module.types);
4218
4219                    let (indexing_binding_array, non_uniform_qualifier) = match *resolved {
4220                        TypeInner::BindingArray { .. } => {
4221                            let uniformity = &func_ctx.info[index].uniformity;
4222
4223                            (true, uniformity.non_uniform_result.is_some())
4224                        }
4225                        _ => (false, false),
4226                    };
4227
4228                    self.write_expr(module, base, func_ctx)?;
4229
4230                    let array_sampler_info = self.sampler_binding_array_info_from_expression(
4231                        module, func_ctx, base, resolved,
4232                    );
4233
4234                    if let Some(ref info) = array_sampler_info {
4235                        write!(self.out, "{}[", info.sampler_heap_name)?;
4236                    } else {
4237                        write!(self.out, "[")?;
4238                    }
4239
4240                    let needs_bound_check = self.options.restrict_indexing
4241                        && !indexing_binding_array
4242                        && match resolved.pointer_space() {
4243                            Some(
4244                                crate::AddressSpace::Function
4245                                | crate::AddressSpace::Private
4246                                | crate::AddressSpace::WorkGroup
4247                                | crate::AddressSpace::Immediate
4248                                | crate::AddressSpace::TaskPayload
4249                                | crate::AddressSpace::RayPayload
4250                                | crate::AddressSpace::IncomingRayPayload,
4251                            )
4252                            | None => true,
4253                            Some(crate::AddressSpace::Uniform) => {
4254                                // check if BindTarget.restrict_indexing is set, this is used for dynamic buffers
4255                                let var_handle = self.fill_access_chain(module, base, func_ctx)?;
4256                                let bind_target = self
4257                                    .options
4258                                    .resolve_resource_binding(
4259                                        module.global_variables[var_handle]
4260                                            .binding
4261                                            .as_ref()
4262                                            .unwrap(),
4263                                    )
4264                                    .unwrap();
4265                                bind_target.restrict_indexing
4266                            }
4267                            Some(
4268                                crate::AddressSpace::Handle | crate::AddressSpace::Storage { .. },
4269                            ) => unreachable!(),
4270                        };
4271                    // Decide whether this index needs to be clamped to fall within range.
4272                    let restriction_needed = if needs_bound_check {
4273                        index::access_needs_check(
4274                            base,
4275                            index::GuardedIndex::Expression(index),
4276                            module,
4277                            func_ctx.expressions,
4278                            func_ctx.info,
4279                        )
4280                    } else {
4281                        None
4282                    };
4283                    if let Some(limit) = restriction_needed {
4284                        write!(self.out, "min(uint(")?;
4285                        self.write_expr(module, index, func_ctx)?;
4286                        write!(self.out, "), ")?;
4287                        match limit {
4288                            index::IndexableLength::Known(limit) => {
4289                                write!(self.out, "{}u", limit - 1)?;
4290                            }
4291                            index::IndexableLength::Dynamic => unreachable!(),
4292                        }
4293                        write!(self.out, ")")?;
4294                    } else {
4295                        if non_uniform_qualifier {
4296                            write!(self.out, "NonUniformResourceIndex(")?;
4297                        }
4298                        if let Some(ref info) = array_sampler_info {
4299                            write!(
4300                                self.out,
4301                                "{}[{} + ",
4302                                info.sampler_index_buffer_name, info.binding_array_base_index_name,
4303                            )?;
4304                        }
4305                        self.write_expr(module, index, func_ctx)?;
4306                        if array_sampler_info.is_some() {
4307                            write!(self.out, "]")?;
4308                        }
4309                        if non_uniform_qualifier {
4310                            write!(self.out, ")")?;
4311                        }
4312                    }
4313
4314                    write!(self.out, "]")?;
4315                }
4316            }
4317            Expression::AccessIndex { base, index } => {
4318                if let Some(crate::AddressSpace::Storage { .. }) =
4319                    func_ctx.resolve_type(expr, &module.types).pointer_space()
4320                {
4321                    // do nothing, the chain is written on `Load`/`Store`
4322                } else {
4323                    // See if we need to write the matrix column access in a
4324                    // special way since the type of `base` is our special
4325                    // __matCx2 struct.
4326                    if let Some(MatrixType {
4327                        rows: crate::VectorSize::Bi,
4328                        ..
4329                    }) = get_inner_matrix_of_struct_array_member(module, base, func_ctx, true)
4330                        .or_else(|| {
4331                            get_inner_matrix_of_global_uniform(module, base, func_ctx, true)
4332                        })
4333                    {
4334                        self.write_expr(module, base, func_ctx)?;
4335                        write!(self.out, "._{index}")?;
4336                        return Ok(());
4337                    }
4338
4339                    let base_ty_res = &func_ctx.info[base].ty;
4340                    let mut resolved = base_ty_res.inner_with(&module.types);
4341                    let base_ty_handle = match *resolved {
4342                        TypeInner::Pointer { base, .. } => {
4343                            resolved = &module.types[base].inner;
4344                            Some(base)
4345                        }
4346                        _ => base_ty_res.handle(),
4347                    };
4348
4349                    // We treat matrices of the form `matCx2` as a sequence of C `vec2`s.
4350                    // See the module-level block comment in mod.rs for details.
4351                    //
4352                    // We handle matrix reconstruction here for Loads.
4353                    // Stores are handled directly by `Statement::Store`.
4354                    if let TypeInner::Struct { ref members, .. } = *resolved {
4355                        let member = &members[index as usize];
4356
4357                        match module.types[member.ty].inner {
4358                            TypeInner::Matrix {
4359                                rows: crate::VectorSize::Bi,
4360                                ..
4361                            } if member.binding.is_none() => {
4362                                let ty = base_ty_handle.unwrap();
4363                                self.write_wrapped_struct_matrix_get_function_name(
4364                                    WrappedStructMatrixAccess { ty, index },
4365                                )?;
4366                                write!(self.out, "(")?;
4367                                self.write_expr(module, base, func_ctx)?;
4368                                write!(self.out, ")")?;
4369                                return Ok(());
4370                            }
4371                            _ => {}
4372                        }
4373                    }
4374
4375                    let array_sampler_info = self.sampler_binding_array_info_from_expression(
4376                        module, func_ctx, base, resolved,
4377                    );
4378
4379                    if let Some(ref info) = array_sampler_info {
4380                        write!(
4381                            self.out,
4382                            "{}[{}",
4383                            info.sampler_heap_name, info.sampler_index_buffer_name
4384                        )?;
4385                    }
4386
4387                    self.write_expr(module, base, func_ctx)?;
4388
4389                    match *resolved {
4390                        // We specifically lift the ValuePointer to this case. While `[0]` is valid
4391                        // HLSL for any vector behind a value pointer, FXC completely miscompiles
4392                        // it and generates completely nonsensical DXBC.
4393                        //
4394                        // See https://github.com/gfx-rs/naga/issues/2095 for more details.
4395                        TypeInner::Vector { .. } | TypeInner::ValuePointer { .. } => {
4396                            // Write vector access as a swizzle
4397                            write!(self.out, ".{}", back::COMPONENTS[index as usize])?
4398                        }
4399                        TypeInner::Matrix { .. }
4400                        | TypeInner::Array { .. }
4401                        | TypeInner::BindingArray { .. } => {
4402                            if let Some(ref info) = array_sampler_info {
4403                                write!(
4404                                    self.out,
4405                                    "[{} + {index}]",
4406                                    info.binding_array_base_index_name
4407                                )?;
4408                            } else {
4409                                write!(self.out, "[{index}]")?;
4410                            }
4411                        }
4412                        TypeInner::Struct { .. } => {
4413                            // This will never panic in case the type is a `Struct`, this is not true
4414                            // for other types so we can only check while inside this match arm
4415                            let ty = base_ty_handle.unwrap();
4416
4417                            write!(
4418                                self.out,
4419                                ".{}",
4420                                self.names[&NameKey::StructMember(ty, index)]
4421                            )?
4422                        }
4423                        ref other => return Err(Error::Custom(format!("Cannot index {other:?}"))),
4424                    }
4425
4426                    if array_sampler_info.is_some() {
4427                        write!(self.out, "]")?;
4428                    }
4429                }
4430            }
4431            Expression::FunctionArgument(pos) => {
4432                let ty = func_ctx.resolve_type(expr, &module.types);
4433
4434                // We know that any external texture function argument has been expanded into
4435                // separate consecutive arguments for each plane and the parameters buffer. And we
4436                // also know that external textures can only ever be used as an argument to another
4437                // function. Therefore we can simply emit each of the expanded arguments in a
4438                // consecutive comma-separated list.
4439                if ty.is_external_image() {
4440                    let plane_names = [0, 1, 2].map(|i| {
4441                        &self.names[&func_ctx
4442                            .external_texture_argument_key(pos, ExternalTextureNameKey::Plane(i))]
4443                    });
4444                    let params_name = &self.names[&func_ctx
4445                        .external_texture_argument_key(pos, ExternalTextureNameKey::Params)];
4446                    write!(
4447                        self.out,
4448                        "{}, {}, {}, {}",
4449                        plane_names[0], plane_names[1], plane_names[2], params_name
4450                    )?;
4451                } else {
4452                    let key = func_ctx.argument_key(pos);
4453                    let name = &self.names[&key];
4454                    write!(self.out, "{name}")?;
4455                }
4456            }
4457            Expression::ImageSample {
4458                coordinate,
4459                image,
4460                sampler,
4461                clamp_to_edge: true,
4462                gather: None,
4463                array_index: None,
4464                offset: None,
4465                level: crate::SampleLevel::Zero,
4466                depth_ref: None,
4467            } => {
4468                write!(self.out, "{IMAGE_SAMPLE_BASE_CLAMP_TO_EDGE_FUNCTION}(")?;
4469                self.write_expr(module, image, func_ctx)?;
4470                write!(self.out, ", ")?;
4471                self.write_expr(module, sampler, func_ctx)?;
4472                write!(self.out, ", ")?;
4473                self.write_expr(module, coordinate, func_ctx)?;
4474                write!(self.out, ")")?;
4475            }
4476            Expression::ImageSample {
4477                image,
4478                sampler,
4479                gather,
4480                coordinate,
4481                array_index,
4482                offset,
4483                level,
4484                depth_ref,
4485                clamp_to_edge,
4486            } => {
4487                if clamp_to_edge {
4488                    return Err(Error::Custom(
4489                        "ImageSample::clamp_to_edge should have been validated out".to_string(),
4490                    ));
4491                }
4492
4493                use crate::SampleLevel as Sl;
4494                const COMPONENTS: [&str; 4] = ["", "Green", "Blue", "Alpha"];
4495
4496                let (base_str, component_str) = match gather {
4497                    Some(component) => ("Gather", COMPONENTS[component as usize]),
4498                    None => ("Sample", ""),
4499                };
4500                let cmp_str = match depth_ref {
4501                    Some(_) => "Cmp",
4502                    None => "",
4503                };
4504                let level_str = match level {
4505                    Sl::Zero if gather.is_none() => "LevelZero",
4506                    Sl::Auto | Sl::Zero => "",
4507                    Sl::Exact(_) => "Level",
4508                    Sl::Bias(_) => "Bias",
4509                    Sl::Gradient { .. } => "Grad",
4510                };
4511
4512                self.write_expr(module, image, func_ctx)?;
4513                write!(self.out, ".{base_str}{cmp_str}{component_str}{level_str}(")?;
4514                self.write_expr(module, sampler, func_ctx)?;
4515                write!(self.out, ", ")?;
4516                self.write_texture_coordinates(
4517                    "float",
4518                    coordinate,
4519                    array_index,
4520                    None,
4521                    module,
4522                    func_ctx,
4523                )?;
4524
4525                if let Some(depth_ref) = depth_ref {
4526                    write!(self.out, ", ")?;
4527                    self.write_expr(module, depth_ref, func_ctx)?;
4528                }
4529
4530                match level {
4531                    Sl::Auto | Sl::Zero => {}
4532                    Sl::Exact(expr) => {
4533                        write!(self.out, ", ")?;
4534                        self.write_expr(module, expr, func_ctx)?;
4535                    }
4536                    Sl::Bias(expr) => {
4537                        write!(self.out, ", ")?;
4538                        self.write_expr(module, expr, func_ctx)?;
4539                    }
4540                    Sl::Gradient { x, y } => {
4541                        write!(self.out, ", ")?;
4542                        self.write_expr(module, x, func_ctx)?;
4543                        write!(self.out, ", ")?;
4544                        self.write_expr(module, y, func_ctx)?;
4545                    }
4546                }
4547
4548                if let Some(offset) = offset {
4549                    write!(self.out, ", ")?;
4550                    // Work around https://github.com/microsoft/DirectXShaderCompiler/issues/5082#issuecomment-1540147807
4551                    let (size, scalar) = func_ctx
4552                        .resolve_type(offset, &module.types)
4553                        .vector_size_and_scalar()
4554                        .unwrap();
4555                    assert_eq!(scalar.kind, ScalarKind::Sint);
4556                    write!(self.out, "{}", scalar.to_hlsl_str()?)?;
4557                    if let Some(size) = size {
4558                        write!(self.out, "{}", common::vector_size_str(size))?;
4559                    }
4560                    write!(self.out, "(")?;
4561                    self.write_const_expression(module, offset, func_ctx.expressions)?;
4562                    write!(self.out, ")")?;
4563                }
4564
4565                write!(self.out, ")")?;
4566            }
4567            Expression::ImageQuery { image, query } => {
4568                // use wrapped image query function
4569                if let TypeInner::Image {
4570                    dim,
4571                    arrayed,
4572                    class,
4573                } = *func_ctx.resolve_type(image, &module.types)
4574                {
4575                    let wrapped_image_query = WrappedImageQuery {
4576                        dim,
4577                        arrayed,
4578                        class,
4579                        query: query.into(),
4580                    };
4581
4582                    self.write_wrapped_image_query_function_name(wrapped_image_query)?;
4583                    write!(self.out, "(")?;
4584                    // Image always first param
4585                    self.write_expr(module, image, func_ctx)?;
4586                    if let crate::ImageQuery::Size { level: Some(level) } = query {
4587                        write!(self.out, ", ")?;
4588                        self.write_expr(module, level, func_ctx)?;
4589                    }
4590                    write!(self.out, ")")?;
4591                }
4592            }
4593            Expression::ImageLoad {
4594                image,
4595                coordinate,
4596                array_index,
4597                sample,
4598                level,
4599            } => self.write_image_load(
4600                &module,
4601                expr,
4602                func_ctx,
4603                image,
4604                coordinate,
4605                array_index,
4606                sample,
4607                level,
4608            )?,
4609            Expression::GlobalVariable(handle) => {
4610                let global_variable = &module.global_variables[handle];
4611                let ty = &module.types[global_variable.ty].inner;
4612
4613                // In the case of binding arrays of samplers, we need to not write anything
4614                // as the we are in the wrong position to fully write the expression.
4615                //
4616                // The entire writing is done by AccessIndex.
4617                let is_binding_array_of_samplers = match *ty {
4618                    TypeInner::BindingArray { base, .. } => {
4619                        let base_ty = &module.types[base].inner;
4620                        matches!(*base_ty, TypeInner::Sampler { .. })
4621                    }
4622                    _ => false,
4623                };
4624
4625                let is_storage_space =
4626                    matches!(global_variable.space, crate::AddressSpace::Storage { .. });
4627
4628                // Our external texture global variable has been expanded into multiple
4629                // global variables, one for each plane and the parameters buffer.
4630                // External textures can only ever be used as arguments to a function
4631                // call, and we know that an external texture argument to any function
4632                // will have been expanded to separate consecutive arguments for each
4633                // plane and the parameters buffer. Therefore we can simply emit each of
4634                // the expanded global variables in a consecutive comma-separated list.
4635                if ty.is_external_image() {
4636                    let plane_names = [0, 1, 2].map(|i| {
4637                        &self.names[&NameKey::ExternalTextureGlobalVariable(
4638                            handle,
4639                            ExternalTextureNameKey::Plane(i),
4640                        )]
4641                    });
4642                    let params_name = &self.names[&NameKey::ExternalTextureGlobalVariable(
4643                        handle,
4644                        ExternalTextureNameKey::Params,
4645                    )];
4646                    write!(
4647                        self.out,
4648                        "{}, {}, {}, {}",
4649                        plane_names[0], plane_names[1], plane_names[2], params_name
4650                    )?;
4651                } else if !is_binding_array_of_samplers && !is_storage_space {
4652                    let name = &self.names[&NameKey::GlobalVariable(handle)];
4653                    write!(self.out, "{name}")?;
4654                }
4655            }
4656            Expression::LocalVariable(handle) => {
4657                write!(self.out, "{}", self.names[&func_ctx.name_key(handle)])?
4658            }
4659            Expression::Load { pointer } => {
4660                match func_ctx
4661                    .resolve_type(pointer, &module.types)
4662                    .pointer_space()
4663                {
4664                    Some(crate::AddressSpace::Storage { .. }) => {
4665                        let var_handle = self.fill_access_chain(module, pointer, func_ctx)?;
4666                        let result_ty = func_ctx.info[expr].ty.clone();
4667                        self.write_storage_load(module, var_handle, result_ty, func_ctx)?;
4668                    }
4669                    _ => {
4670                        let mut close_paren = false;
4671
4672                        // We cast the value loaded to a native HLSL floatCx2
4673                        // in cases where it is of type:
4674                        //  - __matCx2 or
4675                        //  - a (possibly nested) array of __matCx2's
4676                        if let Some(MatrixType {
4677                            rows: crate::VectorSize::Bi,
4678                            ..
4679                        }) = get_inner_matrix_of_struct_array_member(
4680                            module, pointer, func_ctx, false,
4681                        )
4682                        .or_else(|| {
4683                            get_inner_matrix_of_global_uniform(module, pointer, func_ctx, false)
4684                        }) {
4685                            let mut resolved = func_ctx.resolve_type(pointer, &module.types);
4686                            let ptr_tr = resolved.pointer_base_type();
4687                            if let Some(ptr_ty) =
4688                                ptr_tr.as_ref().map(|tr| tr.inner_with(&module.types))
4689                            {
4690                                resolved = ptr_ty;
4691                            }
4692
4693                            write!(self.out, "((")?;
4694                            if let TypeInner::Array { base, size, .. } = *resolved {
4695                                self.write_type(module, base)?;
4696                                self.write_array_size(module, base, size)?;
4697                            } else {
4698                                self.write_value_type(module, resolved)?;
4699                            }
4700                            write!(self.out, ")")?;
4701                            close_paren = true;
4702                        }
4703
4704                        self.write_expr(module, pointer, func_ctx)?;
4705
4706                        if close_paren {
4707                            write!(self.out, ")")?;
4708                        }
4709                    }
4710                }
4711            }
4712            Expression::Unary { op, expr } => {
4713                // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-operators#unary-operators
4714                let op_str = match op {
4715                    crate::UnaryOperator::Negate => {
4716                        match func_ctx.resolve_type(expr, &module.types).scalar() {
4717                            Some(Scalar::I32) => NEG_FUNCTION,
4718                            _ => "-",
4719                        }
4720                    }
4721                    crate::UnaryOperator::LogicalNot => "!",
4722                    crate::UnaryOperator::BitwiseNot => "~",
4723                };
4724                write!(self.out, "{op_str}(")?;
4725                self.write_expr(module, expr, func_ctx)?;
4726                write!(self.out, ")")?;
4727            }
4728            Expression::As { expr, target } => {
4729                let inner = func_ctx.resolve_type(expr, &module.types);
4730                if inner.scalar_kind() == Some(ScalarKind::Float)
4731                    && (target.kind == ScalarKind::Sint || target.kind == ScalarKind::Uint)
4732                    && matches!(target.width, 4 | 8)
4733                {
4734                    // Use helper functions for float to int casts in order to
4735                    // avoid undefined behaviour when value is out of range for
4736                    // the target type.
4737                    let fun_name = match (target.kind, target.width) {
4738                        (ScalarKind::Sint, 4) => F2I32_FUNCTION,
4739                        (ScalarKind::Uint, 4) => F2U32_FUNCTION,
4740                        (ScalarKind::Sint, 8) => F2I64_FUNCTION,
4741                        (ScalarKind::Uint, 8) => F2U64_FUNCTION,
4742                        _ => unreachable!(),
4743                    };
4744                    write!(self.out, "{fun_name}(")?;
4745                    self.write_expr(module, expr, func_ctx)?;
4746                    write!(self.out, ")")?;
4747                } else {
4748                    match *inner {
4749                        TypeInner::Vector { size, .. } => {
4750                            write!(
4751                                self.out,
4752                                "{}{}(",
4753                                target.to_hlsl_str()?,
4754                                common::vector_size_str(size)
4755                            )?;
4756                        }
4757                        TypeInner::Scalar(_) => {
4758                            write!(self.out, "{}(", target.to_hlsl_str()?,)?;
4759                        }
4760                        TypeInner::Matrix { columns, rows, .. } => {
4761                            write!(
4762                                self.out,
4763                                "{}{}x{}(",
4764                                target.to_hlsl_str()?,
4765                                common::vector_size_str(columns),
4766                                common::vector_size_str(rows)
4767                            )?;
4768                        }
4769                        _ => {
4770                            return Err(Error::Unimplemented(format!(
4771                                "write_expr expression::as {inner:?}"
4772                            )));
4773                        }
4774                    };
4775                    self.write_expr(module, expr, func_ctx)?;
4776                    write!(self.out, ")")?;
4777                }
4778            }
4779            Expression::Bitcast { expr, ty } => {
4780                let src_ty = func_ctx.resolve_type(expr, &module.types);
4781                let dst_ty = &module.types[ty].inner;
4782                let unsupported =
4783                    || Error::Unimplemented(format!("bitcast from {src_ty:?} to {dst_ty:?}"));
4784                let (Some((src_size, src)), Some((dst_size, dst))) = (
4785                    src_ty.vector_size_and_scalar(),
4786                    dst_ty.vector_size_and_scalar(),
4787                ) else {
4788                    return Err(unsupported());
4789                };
4790
4791                if let Some(wrapped) = help::WrappedBitcast::new(src_ty, dst_ty) {
4792                    write!(self.out, "{}(", wrapped.function_name()?)?;
4793                    self.write_expr(module, expr, func_ctx)?;
4794                    write!(self.out, ")")?;
4795                } else if src == dst {
4796                    self.write_expr(module, expr, func_ctx)?;
4797                } else {
4798                    debug_assert_eq!(src_size, dst_size);
4799                    let is_int =
4800                        |scalar: Scalar| matches!(scalar.kind, ScalarKind::Sint | ScalarKind::Uint);
4801                    // HLSL's `asint()`, `asuint()` and `asfloat()` only work on
4802                    // 32-bit types, and 16-bit types have `asint16()` and so
4803                    // on. For other widths of integers, use a constructor
4804                    // instead.
4805                    match (src.width, is_int(src) && is_int(dst)) {
4806                        (2 | 8, true) => {
4807                            write!(self.out, "{}", dst.to_hlsl_str()?)?;
4808                            if let Some(size) = dst_size {
4809                                write!(self.out, "{}", common::vector_size_str(size))?;
4810                            }
4811                            write!(self.out, "(")?;
4812                        }
4813                        (2, false) => write!(self.out, "{}(", dst.kind.to_hlsl_cast_16bit())?,
4814                        (4, _) => write!(self.out, "{}(", dst.kind.to_hlsl_cast())?,
4815                        _ => return Err(unsupported()),
4816                    };
4817                    self.write_expr(module, expr, func_ctx)?;
4818                    write!(self.out, ")")?;
4819                }
4820            }
4821            Expression::Math {
4822                fun,
4823                arg,
4824                arg1,
4825                arg2,
4826                arg3,
4827            } => {
4828                return self.write_math_expression(module, fun, arg, arg1, arg2, arg3, func_ctx);
4829            }
4830            Expression::Swizzle {
4831                size,
4832                vector,
4833                pattern,
4834            } => {
4835                self.write_expr(module, vector, func_ctx)?;
4836                write!(self.out, ".")?;
4837                for &sc in pattern[..size as usize].iter() {
4838                    self.out.write_char(back::COMPONENTS[sc as usize])?;
4839                }
4840            }
4841            Expression::ArrayLength(expr) => {
4842                let var_handle = match func_ctx.expressions[expr] {
4843                    Expression::AccessIndex { base, index: _ } => {
4844                        match func_ctx.expressions[base] {
4845                            Expression::GlobalVariable(handle) => handle,
4846                            _ => unreachable!(),
4847                        }
4848                    }
4849                    Expression::GlobalVariable(handle) => handle,
4850                    _ => unreachable!(),
4851                };
4852
4853                let var = &module.global_variables[var_handle];
4854                let (offset, stride) = match module.types[var.ty].inner {
4855                    TypeInner::Array { stride, .. } => (0, stride),
4856                    TypeInner::Struct { ref members, .. } => {
4857                        let last = members.last().unwrap();
4858                        let stride = match module.types[last.ty].inner {
4859                            TypeInner::Array { stride, .. } => stride,
4860                            _ => unreachable!(),
4861                        };
4862                        (last.offset, stride)
4863                    }
4864                    _ => unreachable!(),
4865                };
4866
4867                let storage_access = match var.space {
4868                    crate::AddressSpace::Storage { access } => access,
4869                    _ => crate::StorageAccess::default(),
4870                };
4871                let wrapped_array_length = WrappedArrayLength {
4872                    writable: storage_access.contains(crate::StorageAccess::STORE),
4873                };
4874
4875                write!(self.out, "((")?;
4876                self.write_wrapped_array_length_function_name(wrapped_array_length)?;
4877                let var_name = &self.names[&NameKey::GlobalVariable(var_handle)];
4878                write!(self.out, "({var_name}) - {offset}) / {stride})")?
4879            }
4880            Expression::Derivative { axis, ctrl, expr } => {
4881                use crate::{DerivativeAxis as Axis, DerivativeControl as Ctrl};
4882                if axis == Axis::Width && (ctrl == Ctrl::Coarse || ctrl == Ctrl::Fine) {
4883                    let tail = match ctrl {
4884                        Ctrl::Coarse => "coarse",
4885                        Ctrl::Fine => "fine",
4886                        Ctrl::None => unreachable!(),
4887                    };
4888                    write!(self.out, "abs(ddx_{tail}(")?;
4889                    self.write_expr(module, expr, func_ctx)?;
4890                    write!(self.out, ")) + abs(ddy_{tail}(")?;
4891                    self.write_expr(module, expr, func_ctx)?;
4892                    write!(self.out, "))")?
4893                } else {
4894                    let fun_str = match (axis, ctrl) {
4895                        (Axis::X, Ctrl::Coarse) => "ddx_coarse",
4896                        (Axis::X, Ctrl::Fine) => "ddx_fine",
4897                        (Axis::X, Ctrl::None) => "ddx",
4898                        (Axis::Y, Ctrl::Coarse) => "ddy_coarse",
4899                        (Axis::Y, Ctrl::Fine) => "ddy_fine",
4900                        (Axis::Y, Ctrl::None) => "ddy",
4901                        (Axis::Width, Ctrl::Coarse | Ctrl::Fine) => unreachable!(),
4902                        (Axis::Width, Ctrl::None) => "fwidth",
4903                    };
4904                    write!(self.out, "{fun_str}(")?;
4905                    self.write_expr(module, expr, func_ctx)?;
4906                    write!(self.out, ")")?
4907                }
4908            }
4909            Expression::Relational { fun, argument } => {
4910                use crate::RelationalFunction as Rf;
4911
4912                let fun_str = match fun {
4913                    Rf::All => "all",
4914                    Rf::Any => "any",
4915                    Rf::IsNan => "isnan",
4916                    Rf::IsInf => "isinf",
4917                };
4918                write!(self.out, "{fun_str}(")?;
4919                self.write_expr(module, argument, func_ctx)?;
4920                write!(self.out, ")")?
4921            }
4922            Expression::Select {
4923                condition,
4924                accept,
4925                reject,
4926            } => {
4927                write!(self.out, "(")?;
4928                self.write_expr(module, condition, func_ctx)?;
4929                write!(self.out, " ? ")?;
4930                self.write_expr(module, accept, func_ctx)?;
4931                write!(self.out, " : ")?;
4932                self.write_expr(module, reject, func_ctx)?;
4933                write!(self.out, ")")?
4934            }
4935            Expression::RayQueryGetIntersection { query, committed } => {
4936                // For reasoning, see write_stmt
4937                let Expression::LocalVariable(query_var) = func_ctx.expressions[query] else {
4938                    unreachable!()
4939                };
4940
4941                let tracker_expr_name = format!(
4942                    "{RAY_QUERY_TRACKER_VARIABLE_PREFIX}{}",
4943                    self.names[&func_ctx.name_key(query_var)]
4944                );
4945
4946                if committed {
4947                    write!(self.out, "GetCommittedIntersection(")?;
4948                    self.write_expr(module, query, func_ctx)?;
4949                    write!(self.out, ", {tracker_expr_name})")?;
4950                } else {
4951                    write!(self.out, "GetCandidateIntersection(")?;
4952                    self.write_expr(module, query, func_ctx)?;
4953                    write!(self.out, ", {tracker_expr_name})")?;
4954                }
4955            }
4956            // Not supported yet
4957            Expression::RayQueryVertexPositions { .. }
4958            | Expression::HitObjectQuery { .. }
4959            | Expression::CooperativeLoad { .. }
4960            | Expression::CooperativeMultiplyAdd { .. } => {
4961                unreachable!()
4962            }
4963            // Nothing to do here, since call expression already cached
4964            Expression::CallResult(_)
4965            | Expression::AtomicResult { .. }
4966            | Expression::WorkGroupUniformLoadResult { .. }
4967            | Expression::RayQueryProceedResult
4968            | Expression::SubgroupBallotResult
4969            | Expression::SubgroupOperationResult { .. } => {}
4970        }
4971
4972        if !closing_bracket.is_empty() {
4973            write!(self.out, "{closing_bracket}")?;
4974        }
4975        Ok(())
4976    }
4977
4978    #[allow(clippy::too_many_arguments)]
4979    fn write_image_load(
4980        &mut self,
4981        module: &&Module,
4982        expr: Handle<crate::Expression>,
4983        func_ctx: &back::FunctionCtx,
4984        image: Handle<crate::Expression>,
4985        coordinate: Handle<crate::Expression>,
4986        array_index: Option<Handle<crate::Expression>>,
4987        sample: Option<Handle<crate::Expression>>,
4988        level: Option<Handle<crate::Expression>>,
4989    ) -> Result<(), Error> {
4990        let mut wrapping_type = None;
4991        match *func_ctx.resolve_type(image, &module.types) {
4992            TypeInner::Image {
4993                class: crate::ImageClass::External,
4994                ..
4995            } => {
4996                write!(self.out, "{IMAGE_LOAD_EXTERNAL_FUNCTION}(")?;
4997                self.write_expr(module, image, func_ctx)?;
4998                write!(self.out, ", ")?;
4999                self.write_expr(module, coordinate, func_ctx)?;
5000                write!(self.out, ")")?;
5001                return Ok(());
5002            }
5003            TypeInner::Image {
5004                class: crate::ImageClass::Storage { format, .. },
5005                ..
5006            } if format.single_component() => {
5007                wrapping_type = Some(Scalar::from(format));
5008            }
5009            _ => {}
5010        }
5011        if let Some(scalar) = wrapping_type {
5012            write!(
5013                self.out,
5014                "{}{}(",
5015                help::IMAGE_STORAGE_LOAD_SCALAR_WRAPPER,
5016                scalar.to_hlsl_str()?
5017            )?;
5018        }
5019        // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-load
5020        self.write_expr(module, image, func_ctx)?;
5021        write!(self.out, ".Load(")?;
5022
5023        self.write_texture_coordinates("int", coordinate, array_index, level, module, func_ctx)?;
5024
5025        if let Some(sample) = sample {
5026            write!(self.out, ", ")?;
5027            self.write_expr(module, sample, func_ctx)?;
5028        }
5029
5030        // close bracket for Load function
5031        write!(self.out, ")")?;
5032
5033        if wrapping_type.is_some() {
5034            write!(self.out, ")")?;
5035        }
5036
5037        // return x component if return type is scalar
5038        if let TypeInner::Scalar(_) = *func_ctx.resolve_type(expr, &module.types) {
5039            write!(self.out, ".x")?;
5040        }
5041        Ok(())
5042    }
5043
5044    /// Find the [`BindingArraySamplerInfo`] from an expression so that such an access
5045    /// can be generated later.
5046    fn sampler_binding_array_info_from_expression(
5047        &mut self,
5048        module: &Module,
5049        func_ctx: &back::FunctionCtx<'_>,
5050        base: Handle<crate::Expression>,
5051        resolved: &TypeInner,
5052    ) -> Option<BindingArraySamplerInfo> {
5053        if let TypeInner::BindingArray {
5054            base: base_ty_handle,
5055            ..
5056        } = *resolved
5057        {
5058            let base_ty = &module.types[base_ty_handle].inner;
5059            if let TypeInner::Sampler { comparison, .. } = *base_ty {
5060                let base = &func_ctx.expressions[base];
5061
5062                if let crate::Expression::GlobalVariable(handle) = *base {
5063                    let variable = &module.global_variables[handle];
5064
5065                    let sampler_heap_name = match comparison {
5066                        true => COMPARISON_SAMPLER_HEAP_VAR,
5067                        false => SAMPLER_HEAP_VAR,
5068                    };
5069
5070                    return Some(BindingArraySamplerInfo {
5071                        sampler_heap_name,
5072                        sampler_index_buffer_name: self
5073                            .wrapped
5074                            .sampler_index_buffers
5075                            .get(&super::SamplerIndexBufferKey {
5076                                group: variable.binding.unwrap().group,
5077                            })
5078                            .unwrap()
5079                            .clone(),
5080                        binding_array_base_index_name: self.names[&NameKey::GlobalVariable(handle)]
5081                            .clone(),
5082                    });
5083                }
5084            }
5085        }
5086
5087        None
5088    }
5089
5090    fn write_named_expr(
5091        &mut self,
5092        module: &Module,
5093        handle: Handle<crate::Expression>,
5094        name: String,
5095        // The expression which is being named.
5096        // Generally, this is the same as handle, except in WorkGroupUniformLoad
5097        expr: Handle<crate::Expression>,
5098        func_ctx: &back::FunctionCtx,
5099    ) -> BackendResult {
5100        if let crate::Expression::Load { pointer } = func_ctx.expressions[expr] {
5101            let ty_inner = func_ctx.resolve_type(pointer, &module.types);
5102            if ty_inner.is_atomic_pointer(&module.types) {
5103                let pointer_space = ty_inner.pointer_space().unwrap();
5104                self.write_value_type(module, func_ctx.info[handle].ty.inner_with(&module.types))?;
5105                write!(self.out, " {name}; ")?;
5106                match pointer_space {
5107                    crate::AddressSpace::WorkGroup => {
5108                        write!(self.out, "InterlockedOr(")?;
5109                        self.write_expr(module, pointer, func_ctx)?;
5110                    }
5111                    crate::AddressSpace::Storage { .. } => {
5112                        let var_handle = self.fill_access_chain(module, pointer, func_ctx)?;
5113                        let var_name = &self.names[&NameKey::GlobalVariable(var_handle)];
5114                        write!(self.out, "{var_name}.InterlockedOr(")?;
5115                        let chain = mem::take(&mut self.temp_access_chain);
5116                        self.write_storage_address(module, &chain, func_ctx)?;
5117                        self.temp_access_chain = chain;
5118                    }
5119                    _ => unreachable!(),
5120                }
5121                writeln!(self.out, ", 0, {name});")?;
5122                self.named_expressions.insert(expr, name);
5123                return Ok(());
5124            }
5125        }
5126        match func_ctx.info[expr].ty {
5127            proc::TypeResolution::Handle(ty_handle) => match module.types[ty_handle].inner {
5128                TypeInner::Struct { .. } => {
5129                    let ty_name = &self.names[&NameKey::Type(ty_handle)];
5130                    write!(self.out, "{ty_name}")?;
5131                }
5132                _ => {
5133                    self.write_type(module, ty_handle)?;
5134                }
5135            },
5136            proc::TypeResolution::Value(ref inner) => {
5137                self.write_value_type(module, inner)?;
5138            }
5139        }
5140
5141        let resolved = func_ctx.resolve_type(expr, &module.types);
5142
5143        write!(self.out, " {name}")?;
5144        // If rhs is a array type, we should write array size
5145        if let TypeInner::Array { base, size, .. } = *resolved {
5146            self.write_array_size(module, base, size)?;
5147        }
5148        write!(self.out, " = ")?;
5149        self.write_expr(module, handle, func_ctx)?;
5150        writeln!(self.out, ";")?;
5151        self.named_expressions.insert(expr, name);
5152
5153        Ok(())
5154    }
5155
5156    /// Helper function that write default zero initialization
5157    pub(super) fn write_default_init(
5158        &mut self,
5159        module: &Module,
5160        ty: Handle<crate::Type>,
5161    ) -> BackendResult {
5162        write!(self.out, "(")?;
5163        self.write_type(module, ty)?;
5164        if let TypeInner::Array { base, size, .. } = module.types[ty].inner {
5165            self.write_array_size(module, base, size)?;
5166        }
5167        write!(self.out, ")0")?;
5168        Ok(())
5169    }
5170
5171    pub(super) fn write_control_barrier(
5172        &mut self,
5173        barrier: crate::Barrier,
5174        level: back::Level,
5175    ) -> BackendResult {
5176        if barrier.contains(crate::Barrier::STORAGE) {
5177            writeln!(self.out, "{level}DeviceMemoryBarrierWithGroupSync();")?;
5178        }
5179        if barrier.contains(crate::Barrier::WORK_GROUP) {
5180            writeln!(self.out, "{level}GroupMemoryBarrierWithGroupSync();")?;
5181        }
5182        if barrier.contains(crate::Barrier::SUB_GROUP) {
5183            // Does not exist in DirectX
5184        }
5185        if barrier.contains(crate::Barrier::TEXTURE) {
5186            writeln!(self.out, "{level}DeviceMemoryBarrierWithGroupSync();")?;
5187        }
5188        Ok(())
5189    }
5190
5191    fn write_memory_barrier(
5192        &mut self,
5193        barrier: crate::Barrier,
5194        level: back::Level,
5195    ) -> BackendResult {
5196        if barrier.contains(crate::Barrier::STORAGE) {
5197            writeln!(self.out, "{level}DeviceMemoryBarrier();")?;
5198        }
5199        if barrier.contains(crate::Barrier::WORK_GROUP) {
5200            writeln!(self.out, "{level}GroupMemoryBarrier();")?;
5201        }
5202        if barrier.contains(crate::Barrier::SUB_GROUP) {
5203            // Does not exist in DirectX
5204        }
5205        if barrier.contains(crate::Barrier::TEXTURE) {
5206            writeln!(self.out, "{level}DeviceMemoryBarrier();")?;
5207        }
5208        Ok(())
5209    }
5210
5211    /// Helper to emit the shared tail of an HLSL atomic call (arguments, value, result)
5212    fn emit_hlsl_atomic_tail(
5213        &mut self,
5214        module: &Module,
5215        func_ctx: &back::FunctionCtx<'_>,
5216        fun: &crate::AtomicFunction,
5217        compare_expr: Option<Handle<crate::Expression>>,
5218        value: Handle<crate::Expression>,
5219        res_var_info: &Option<(Handle<crate::Expression>, String)>,
5220    ) -> BackendResult {
5221        if let Some(cmp) = compare_expr {
5222            write!(self.out, ", ")?;
5223            self.write_expr(module, cmp, func_ctx)?;
5224        }
5225        write!(self.out, ", ")?;
5226        if let crate::AtomicFunction::Subtract = *fun {
5227            // we just wrote `InterlockedAdd`, so negate the argument
5228            write!(self.out, "-")?;
5229        }
5230        self.write_expr(module, value, func_ctx)?;
5231        if let Some(&(_res_handle, ref res_name)) = res_var_info.as_ref() {
5232            write!(self.out, ", ")?;
5233            if compare_expr.is_some() {
5234                write!(self.out, "{res_name}.old_value")?;
5235            } else {
5236                write!(self.out, "{res_name}")?;
5237            }
5238        }
5239        writeln!(self.out, ");")?;
5240        Ok(())
5241    }
5242}
5243
5244pub(super) struct MatrixType {
5245    pub(super) columns: crate::VectorSize,
5246    pub(super) rows: crate::VectorSize,
5247    pub(super) width: crate::Bytes,
5248}
5249
5250pub(super) fn get_inner_matrix_data(
5251    module: &Module,
5252    handle: Handle<crate::Type>,
5253) -> Option<MatrixType> {
5254    match module.types[handle].inner {
5255        TypeInner::Matrix {
5256            columns,
5257            rows,
5258            scalar,
5259        } => Some(MatrixType {
5260            columns,
5261            rows,
5262            width: scalar.width,
5263        }),
5264        TypeInner::Array { base, .. } => get_inner_matrix_data(module, base),
5265        _ => None,
5266    }
5267}
5268
5269/// Returns `true` if `binding` is the depth that a fragment shader writes.
5270const fn is_frag_depth(binding: Option<&crate::Binding>) -> bool {
5271    matches!(
5272        binding,
5273        Some(crate::Binding::BuiltIn(crate::BuiltIn::FragDepth))
5274    )
5275}
5276
5277/// Returns `true` if the depth that `entry_point` writes must be discarded.
5278///
5279/// HLSL has no way to specify that depth is unchanged. Discarding the depth has the same effect,
5280/// and keeps early depth tests enabled.
5281fn discards_frag_depth(entry_point: &crate::EntryPoint) -> bool {
5282    entry_point.stage == ShaderStage::Fragment
5283        && entry_point.early_depth_test
5284            == Some(crate::EarlyDepthTest::Allow {
5285                conservative: crate::ConservativeDepth::Unchanged,
5286            })
5287}
5288
5289/// Returns `true` if `entry_point` has nothing to return once its depth is discarded.
5290fn only_writes_discarded_depth(module: &Module, entry_point: &crate::EntryPoint) -> bool {
5291    discards_frag_depth(entry_point)
5292        && entry_point.function.result.as_ref().is_some_and(|result| {
5293            match module.types[result.ty].inner {
5294                TypeInner::Struct { ref members, .. } if result.binding.is_none() => members
5295                    .iter()
5296                    .all(|member| is_frag_depth(member.binding.as_ref())),
5297                _ => is_frag_depth(result.binding.as_ref()),
5298            }
5299        })
5300}
5301
5302/// Returns `true` if the function described by `func_ctx` is an entry point that returns nothing,
5303/// because the only thing it would return is a depth that is discarded.
5304fn returns_nothing(module: &Module, func_ctx: &back::FunctionCtx<'_>) -> bool {
5305    match func_ctx.ty {
5306        back::FunctionType::EntryPoint(index) => {
5307            only_writes_discarded_depth(module, &module.entry_points[index as usize])
5308        }
5309        back::FunctionType::Function(_) => false,
5310    }
5311}
5312
5313/// Gets the early depth test of the fragment entry points in `entry_points` that return `ty`.
5314///
5315/// Returns `None` if there are no such entry points or if they disagree. The semantic of a
5316/// depth output is part of the definition of the struct that contains it, so a struct that is
5317/// shared by entry points with different settings cannot use a conservative semantic.
5318fn shared_early_depth_test(
5319    module: &Module,
5320    entry_points: core::ops::Range<usize>,
5321    ty: Handle<crate::Type>,
5322) -> Option<crate::EarlyDepthTest> {
5323    let mut users = entry_points
5324        .map(|index| &module.entry_points[index])
5325        .filter(|ep| {
5326            ep.stage == ShaderStage::Fragment
5327                && ep.function.result.as_ref().is_some_and(|r| r.ty == ty)
5328        });
5329    let first = users.next()?.early_depth_test;
5330    if users.all(|ep| ep.early_depth_test == first) {
5331        first
5332    } else {
5333        None
5334    }
5335}
5336
5337/// If `base` is an access chain of the form `mat`, `mat[col]`, or `mat[col][row]`,
5338/// returns a tuple of the matrix, the column (vector) index (if present), and
5339/// the row (scalar) index (if present).
5340fn find_matrix_in_access_chain(
5341    module: &Module,
5342    base: Handle<crate::Expression>,
5343    func_ctx: &back::FunctionCtx<'_>,
5344) -> Option<(Handle<crate::Expression>, Option<Index>, Option<Index>)> {
5345    let mut current_base = base;
5346    let mut vector = None;
5347    let mut scalar = None;
5348    loop {
5349        let resolved_tr = func_ctx
5350            .resolve_type(current_base, &module.types)
5351            .pointer_base_type();
5352        let resolved = resolved_tr.as_ref()?.inner_with(&module.types);
5353
5354        match *resolved {
5355            TypeInner::Matrix { .. } => return Some((current_base, vector, scalar)),
5356            TypeInner::Scalar(_) | TypeInner::Vector { .. } => {}
5357            _ => return None,
5358        }
5359
5360        let index;
5361        (current_base, index) = match func_ctx.expressions[current_base] {
5362            crate::Expression::Access { base, index } => (base, Index::Expression(index)),
5363            crate::Expression::AccessIndex { base, index } => (base, Index::Static(index)),
5364            _ => return None,
5365        };
5366
5367        match *resolved {
5368            TypeInner::Scalar(_) => scalar = Some(index),
5369            TypeInner::Vector { .. } => vector = Some(index),
5370            _ => unreachable!(),
5371        }
5372    }
5373}
5374
5375/// Returns the matrix data if the access chain starting at `base`:
5376/// - starts with an expression with resolved type of [`TypeInner::Matrix`] if `direct = true`
5377/// - contains one or more expressions with resolved type of [`TypeInner::Array`] of [`TypeInner::Matrix`]
5378/// - ends at an expression with resolved type of [`TypeInner::Struct`]
5379pub(super) fn get_inner_matrix_of_struct_array_member(
5380    module: &Module,
5381    base: Handle<crate::Expression>,
5382    func_ctx: &back::FunctionCtx<'_>,
5383    direct: bool,
5384) -> Option<MatrixType> {
5385    let mut mat_data = None;
5386    let mut array_base = None;
5387
5388    let mut current_base = base;
5389    loop {
5390        let mut resolved = func_ctx.resolve_type(current_base, &module.types);
5391        if let TypeInner::Pointer { base, .. } = *resolved {
5392            resolved = &module.types[base].inner;
5393        };
5394
5395        match *resolved {
5396            TypeInner::Matrix {
5397                columns,
5398                rows,
5399                scalar,
5400            } => {
5401                mat_data = Some(MatrixType {
5402                    columns,
5403                    rows,
5404                    width: scalar.width,
5405                })
5406            }
5407            TypeInner::Array { base, .. } => {
5408                array_base = Some(base);
5409            }
5410            TypeInner::Struct { .. } => {
5411                if let Some(array_base) = array_base {
5412                    if direct {
5413                        return mat_data;
5414                    } else {
5415                        return get_inner_matrix_data(module, array_base);
5416                    }
5417                }
5418
5419                break;
5420            }
5421            _ => break,
5422        }
5423
5424        current_base = match func_ctx.expressions[current_base] {
5425            crate::Expression::Access { base, .. } => base,
5426            crate::Expression::AccessIndex { base, .. } => base,
5427            _ => break,
5428        };
5429    }
5430    None
5431}
5432
5433/// Returns the matrix data if the access chain starting at `base`:
5434/// - starts with an expression with resolved type of [`TypeInner::Matrix`], or
5435/// - contains zero or more expressions with resolved type of [`TypeInner::Array`] of [`TypeInner::Matrix`] if `direct = false`
5436/// - and ends with an [`Expression::GlobalVariable`](crate::Expression::GlobalVariable) in [`AddressSpace::Uniform`](crate::AddressSpace::Uniform)
5437fn get_inner_matrix_of_global_uniform(
5438    module: &Module,
5439    base: Handle<crate::Expression>,
5440    func_ctx: &back::FunctionCtx<'_>,
5441    direct: bool,
5442) -> Option<MatrixType> {
5443    let mut mat_data = None;
5444    let mut array_base = None;
5445
5446    let mut current_base = base;
5447    loop {
5448        let mut resolved = func_ctx.resolve_type(current_base, &module.types);
5449        if let TypeInner::Pointer { base, .. } = *resolved {
5450            resolved = &module.types[base].inner;
5451        };
5452
5453        match *resolved {
5454            TypeInner::Matrix {
5455                columns,
5456                rows,
5457                scalar,
5458            } => {
5459                mat_data = Some(MatrixType {
5460                    columns,
5461                    rows,
5462                    width: scalar.width,
5463                })
5464            }
5465            TypeInner::Array { base, .. } => {
5466                if !direct {
5467                    array_base = Some(base);
5468                }
5469            }
5470            _ => break,
5471        }
5472
5473        current_base = match func_ctx.expressions[current_base] {
5474            crate::Expression::Access { base, .. } => base,
5475            crate::Expression::AccessIndex { base, .. } => base,
5476            crate::Expression::GlobalVariable(handle)
5477                if module.global_variables[handle].space == crate::AddressSpace::Uniform =>
5478            {
5479                return mat_data.or_else(|| {
5480                    array_base.and_then(|array_base| get_inner_matrix_data(module, array_base))
5481                })
5482            }
5483            _ => break,
5484        };
5485    }
5486    None
5487}