naga/back/hlsl/
mod.rs

1/*!
2Backend for [HLSL][hlsl] (High-Level Shading Language).
3
4# Supported shader model versions:
5- 5.0
6- 5.1
7- 6.0
8
9# Layout of values in `uniform` buffers
10
11WGSL's ["Internal Layout of Values"][ilov] rules specify how each WGSL
12type should be stored in `uniform` and `storage` buffers. The HLSL we
13generate must access values in that form, even when it is not what
14HLSL would use normally.
15
16Matching the WGSL memory layout is a concern only for `uniform`
17variables. WGSL `storage` buffers are translated as HLSL
18`ByteAddressBuffers`, for which we generate `Load` and `Store` method
19calls with explicit byte offsets. WGSL pipeline inputs must be scalars
20or vectors; they cannot be matrices, which is where the interesting
21problems arise. However, when an affected type appears in a struct
22definition, the transformations described here are applied without
23consideration of where the struct is used.
24
25Access to storage buffers is implemented in `storage.rs`. Access to
26uniform buffers is implemented where applicable in `writer.rs`.
27
28## Row- and column-major ordering for matrices
29
30WGSL specifies that matrices in uniform buffers are stored in
31column-major order. This matches HLSL's default, so one might expect
32things to be straightforward. Unfortunately, WGSL and HLSL disagree on
33what indexing a matrix means: in WGSL, `m[i]` retrieves the `i`'th
34*column* of `m`, whereas in HLSL it retrieves the `i`'th *row*. We
35want to avoid translating `m[i]` into some complicated reassembly of a
36vector from individually fetched components, so this is a problem.
37
38However, with a bit of trickery, it is possible to use HLSL's `m[i]`
39as the translation of WGSL's `m[i]`:
40
41- We declare all matrices in uniform buffers in HLSL with the
42  `row_major` qualifier, and transpose the row and column counts: a
43  WGSL `mat3x4<f32>`, say, becomes an HLSL `row_major float3x4`. (Note
44  that WGSL and HLSL type names put the row and column in reverse
45  order.) Since the HLSL type is the transpose of how WebGPU directs
46  the user to store the data, HLSL will load all matrices transposed.
47
48- Since matrices are transposed, an HLSL indexing expression retrieves
49  the "columns" of the intended WGSL value, as desired.
50
51- For vector-matrix multiplication, since `mul(transpose(m), v)` is
52  equivalent to `mul(v, m)` (note the reversal of the arguments), and
53  `mul(v, transpose(m))` is equivalent to `mul(m, v)`, we can
54  translate WGSL `m * v` and `v * m` to HLSL by simply reversing the
55  arguments to `mul`.
56
57## Padding in two-row matrices
58
59An HLSL `row_major floatKx2` matrix has padding between its rows that
60the WGSL `matKx2<f32>` matrix it represents does not. HLSL stores all
61matrix rows [aligned on 16-byte boundaries][16bb], whereas WGSL says
62that the columns of a `matKx2<f32>` need only be [aligned as required
63for `vec2<f32>`][ilov], which is [eight-byte alignment][8bb].
64
65To compensate for this, any time a `matKx2<f32>` appears in a WGSL
66`uniform` value or as part of a struct/array, we actually emit `K`
67separate `float2` members, and assemble/disassemble the matrix from its
68columns (in WGSL; rows in HLSL) upon load and store.
69
70For example, the following WGSL struct type:
71
72```ignore
73struct Baz {
74        m: mat3x2<f32>,
75}
76```
77
78is rendered as the HLSL struct type:
79
80```ignore
81struct Baz {
82    float2 m_0; float2 m_1; float2 m_2;
83};
84```
85
86The `wrapped_struct_matrix` functions in `help.rs` generate HLSL
87helper functions to access such members, converting between the stored
88form and the HLSL matrix types appropriately. For example, for reading
89the member `m` of the `Baz` struct above, we emit:
90
91```ignore
92float3x2 GetMatmOnBaz(Baz obj) {
93    return float3x2(obj.m_0, obj.m_1, obj.m_2);
94}
95```
96
97We also emit an analogous `Set` function, as well as functions for
98accessing individual columns by dynamic index.
99
100## Sampler Handling
101
102Due to limitations in how sampler heaps work in D3D12, we need to access samplers
103through a layer of indirection. Instead of directly binding samplers, we bind the entire
104sampler heap as both a standard and a comparison sampler heap. We then use a sampler
105index buffer for each bind group. This buffer is accessed in the shader to get the actual
106sampler index within the heap. See the wgpu_hal dx12 backend documentation for more
107information.
108
109# External textures
110
111Support for [`crate::ImageClass::External`] textures is implemented by lowering
112each external texture global variable to 3 `Texture2D<float4>`s, and a `cbuffer`
113of type `NagaExternalTextureParams`. This provides up to 3 planes of texture
114data (for example single planar RGBA, or separate Y, Cb, and Cr planes), and the
115parameters buffer containing information describing how to handle these
116correctly. The bind target to use for each of these globals is specified via
117[`Options::external_texture_binding_map`].
118
119External textures are supported by WGSL's `textureDimensions()`,
120`textureLoad()`, and `textureSampleBaseClampToEdge()` built-in functions. These
121are implemented using helper functions. See the following functions for how
122these are generated:
123 * `Writer::write_wrapped_image_query_function`
124 * `Writer::write_wrapped_image_load_function`
125 * `Writer::write_wrapped_image_sample_function`
126
127Ideally the set of global variables could be wrapped in a single struct that
128could conveniently be passed around. But, alas, HLSL does not allow structs to
129have `Texture2D` members. Fortunately, however, external textures can only be
130used as arguments to either built-in or user-defined functions. We therefore
131expand any external texture function argument to four consecutive arguments (3
132textures and the params struct) when declaring user-defined functions, and
133ensure our built-in function implementations take the same arguments. Then,
134whenever we need to emit an external texture in `Writer::write_expr`, which
135fortunately can only ever be for a global variable or function argument, we
136simply emit the variable name of each of the three textures and the parameters
137struct in a comma-separated list. This won't win any awards for elegance, but
138it works for our purposes.
139
140[hlsl]: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl
141[ilov]: https://gpuweb.github.io/gpuweb/wgsl/#internal-value-layout
142[16bb]: https://github.com/microsoft/DirectXShaderCompiler/wiki/Buffer-Packing#constant-buffer-packing
143[8bb]: https://gpuweb.github.io/gpuweb/wgsl/#alignment-and-size
144*/
145
146mod conv;
147mod help;
148mod keywords;
149mod ray;
150mod storage;
151mod writer;
152
153use alloc::{string::String, vec::Vec};
154use core::fmt::Error as FmtError;
155
156use thiserror::Error;
157
158use crate::{back, ir, proc};
159
160/// Direct3D 12 binding information for a global variable.
161///
162/// This type provides the HLSL-specific information Naga needs to declare and
163/// access an HLSL global variable that cannot be derived from the `Module`
164/// itself.
165///
166/// An HLSL global variable declaration includes details that the Direct3D API
167/// will use to refer to it. For example:
168///
169///    RWByteAddressBuffer s_sasm : register(u0, space2);
170///
171/// This defines a global `s_sasm` that a Direct3D root signature would refer to
172/// as register `0` in register space `2` in a `UAV` descriptor range. Naga can
173/// infer the register's descriptor range type from the variable's address class
174/// (writable [`Storage`] variables are implemented by Direct3D Unordered Access
175/// Views, the `u` register type), but the register number and register space
176/// must be supplied by the user.
177///
178/// The [`back::hlsl::Options`] structure provides `BindTarget`s for various
179/// situations in which Naga may need to generate an HLSL global variable, like
180/// [`binding_map`] for Naga global variables, or [`immediates_target`] for
181/// a module's sole [`Immediate`] variable. See those fields' documentation
182/// for details.
183///
184/// [`Storage`]: crate::ir::AddressSpace::Storage
185/// [`back::hlsl::Options`]: Options
186/// [`binding_map`]: Options::binding_map
187/// [`immediates_target`]: Options::immediates_target
188/// [`Immediate`]: crate::ir::AddressSpace::Immediate
189#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
190#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
191#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
192pub struct BindTarget {
193    pub space: u8,
194    /// For regular bindings this is the register number.
195    ///
196    /// For sampler bindings, this is the index to use into the bind group's sampler index buffer.
197    pub register: u32,
198    /// If the binding is an unsized binding array, this overrides the size.
199    pub binding_array_size: Option<u32>,
200    /// This is the index in the buffer at [`Options::dynamic_storage_buffer_offsets_targets`].
201    pub dynamic_storage_buffer_offsets_index: Option<u32>,
202    /// This is a hint that we need to restrict indexing of vectors, matrices and arrays.
203    ///
204    /// If [`Options::restrict_indexing`] is also `true`, we will restrict indexing.
205    #[cfg_attr(any(feature = "serialize", feature = "deserialize"), serde(default))]
206    pub restrict_indexing: bool,
207}
208
209#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
210#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
211#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
212/// BindTarget for dynamic storage buffer offsets
213pub struct OffsetsBindTarget {
214    pub space: u8,
215    pub register: u32,
216    pub size: u32,
217}
218
219#[cfg(feature = "deserialize")]
220#[derive(serde::Deserialize)]
221struct BindingMapSerialization {
222    resource_binding: crate::ResourceBinding,
223    bind_target: BindTarget,
224}
225
226#[cfg(feature = "deserialize")]
227fn deserialize_binding_map<'de, D>(deserializer: D) -> Result<BindingMap, D::Error>
228where
229    D: serde::Deserializer<'de>,
230{
231    use serde::Deserialize;
232
233    let vec = Vec::<BindingMapSerialization>::deserialize(deserializer)?;
234    let mut map = BindingMap::default();
235    for item in vec {
236        map.insert(item.resource_binding, item.bind_target);
237    }
238    Ok(map)
239}
240
241// Using `BTreeMap` instead of `HashMap` so that we can hash itself.
242pub type BindingMap = alloc::collections::BTreeMap<crate::ResourceBinding, BindTarget>;
243
244/// A HLSL shader model version.
245#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd)]
246#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
247#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
248pub enum ShaderModel {
249    V5_0,
250    V5_1,
251    V6_0,
252    V6_1,
253    V6_2,
254    V6_3,
255    V6_4,
256    V6_5,
257    V6_6,
258    V6_7,
259    V6_8,
260    V6_9,
261}
262
263impl ShaderModel {
264    pub const fn to_str(self) -> &'static str {
265        match self {
266            Self::V5_0 => "5_0",
267            Self::V5_1 => "5_1",
268            Self::V6_0 => "6_0",
269            Self::V6_1 => "6_1",
270            Self::V6_2 => "6_2",
271            Self::V6_3 => "6_3",
272            Self::V6_4 => "6_4",
273            Self::V6_5 => "6_5",
274            Self::V6_6 => "6_6",
275            Self::V6_7 => "6_7",
276            Self::V6_8 => "6_8",
277            Self::V6_9 => "6_9",
278        }
279    }
280}
281
282impl crate::ShaderStage {
283    pub const fn to_hlsl_str(self) -> &'static str {
284        match self {
285            Self::Vertex => "vs",
286            Self::Fragment => "ps",
287            Self::Compute => "cs",
288            Self::Task => "as",
289            Self::Mesh => "ms",
290            Self::RayGeneration | Self::AnyHit | Self::ClosestHit | Self::Miss => "lib",
291        }
292    }
293}
294
295impl crate::ImageDimension {
296    const fn to_hlsl_str(self) -> &'static str {
297        match self {
298            Self::D1 => "1D",
299            Self::D2 => "2D",
300            Self::D3 => "3D",
301            Self::Cube => "Cube",
302        }
303    }
304}
305
306#[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
307#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
308#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
309pub struct SamplerIndexBufferKey {
310    pub group: u32,
311}
312
313#[derive(Clone, Debug, Hash, PartialEq, Eq)]
314#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
315#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
316#[cfg_attr(feature = "deserialize", serde(default))]
317pub struct SamplerHeapBindTargets {
318    pub standard_samplers: BindTarget,
319    pub comparison_samplers: BindTarget,
320}
321
322impl Default for SamplerHeapBindTargets {
323    fn default() -> Self {
324        Self {
325            standard_samplers: BindTarget {
326                space: 0,
327                register: 0,
328                binding_array_size: None,
329                dynamic_storage_buffer_offsets_index: None,
330                restrict_indexing: false,
331            },
332            comparison_samplers: BindTarget {
333                space: 1,
334                register: 0,
335                binding_array_size: None,
336                dynamic_storage_buffer_offsets_index: None,
337                restrict_indexing: false,
338            },
339        }
340    }
341}
342
343#[cfg(feature = "deserialize")]
344#[derive(serde::Deserialize)]
345struct SamplerIndexBufferBindingSerialization {
346    group: u32,
347    bind_target: BindTarget,
348}
349
350#[cfg(feature = "deserialize")]
351fn deserialize_sampler_index_buffer_bindings<'de, D>(
352    deserializer: D,
353) -> Result<SamplerIndexBufferBindingMap, D::Error>
354where
355    D: serde::Deserializer<'de>,
356{
357    use serde::Deserialize;
358
359    let vec = Vec::<SamplerIndexBufferBindingSerialization>::deserialize(deserializer)?;
360    let mut map = SamplerIndexBufferBindingMap::default();
361    for item in vec {
362        map.insert(
363            SamplerIndexBufferKey { group: item.group },
364            item.bind_target,
365        );
366    }
367    Ok(map)
368}
369
370// We use a BTreeMap here so that we can hash it.
371pub type SamplerIndexBufferBindingMap =
372    alloc::collections::BTreeMap<SamplerIndexBufferKey, BindTarget>;
373
374#[cfg(feature = "deserialize")]
375#[derive(serde::Deserialize)]
376struct DynamicStorageBufferOffsetTargetSerialization {
377    index: u32,
378    bind_target: OffsetsBindTarget,
379}
380
381#[cfg(feature = "deserialize")]
382fn deserialize_storage_buffer_offsets<'de, D>(
383    deserializer: D,
384) -> Result<DynamicStorageBufferOffsetsTargets, D::Error>
385where
386    D: serde::Deserializer<'de>,
387{
388    use serde::Deserialize;
389
390    let vec = Vec::<DynamicStorageBufferOffsetTargetSerialization>::deserialize(deserializer)?;
391    let mut map = DynamicStorageBufferOffsetsTargets::default();
392    for item in vec {
393        map.insert(item.index, item.bind_target);
394    }
395    Ok(map)
396}
397
398pub type DynamicStorageBufferOffsetsTargets = alloc::collections::BTreeMap<u32, OffsetsBindTarget>;
399
400/// HLSL binding information for a Naga [`External`] image global variable.
401///
402/// See the module documentation's section on [External textures][mod] for details.
403///
404/// [`External`]: crate::ir::ImageClass::External
405/// [mod]: #external-textures
406#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
407#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
408#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
409pub struct ExternalTextureBindTarget {
410    /// HLSL binding information for the individual plane textures.
411    ///
412    /// Each of these should refer to an HLSL `Texture2D<float4>` holding one
413    /// plane of data for the external texture. The exact meaning of each plane
414    /// varies at runtime depending on where the external texture's data
415    /// originated.
416    pub planes: [BindTarget; 3],
417
418    /// HLSL binding information for a buffer holding the sampling parameters.
419    ///
420    /// This should refer to a cbuffer of type `NagaExternalTextureParams`, that
421    /// the code Naga generates for `textureSampleBaseClampToEdge` consults to
422    /// decide how to combine the data in [`planes`] to get the result required
423    /// by the spec.
424    ///
425    /// [`planes`]: Self::planes
426    pub params: BindTarget,
427}
428
429#[cfg(feature = "deserialize")]
430#[derive(serde::Deserialize)]
431struct ExternalTextureBindingMapSerialization {
432    resource_binding: crate::ResourceBinding,
433    bind_target: ExternalTextureBindTarget,
434}
435
436#[cfg(feature = "deserialize")]
437fn deserialize_external_texture_binding_map<'de, D>(
438    deserializer: D,
439) -> Result<ExternalTextureBindingMap, D::Error>
440where
441    D: serde::Deserializer<'de>,
442{
443    use serde::Deserialize;
444
445    let vec = Vec::<ExternalTextureBindingMapSerialization>::deserialize(deserializer)?;
446    let mut map = ExternalTextureBindingMap::default();
447    for item in vec {
448        map.insert(item.resource_binding, item.bind_target);
449    }
450    Ok(map)
451}
452pub type ExternalTextureBindingMap =
453    alloc::collections::BTreeMap<crate::ResourceBinding, ExternalTextureBindTarget>;
454
455/// Shorthand result used internally by the backend
456type BackendResult = Result<(), Error>;
457
458#[derive(Clone, Debug, PartialEq, thiserror::Error)]
459#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
460#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
461pub enum EntryPointError {
462    #[error("mapping of {0:?} is missing")]
463    MissingBinding(crate::ResourceBinding),
464}
465
466/// Configuration used in the [`Writer`].
467#[derive(Clone, Debug, Hash, PartialEq, Eq)]
468#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
469#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
470#[cfg_attr(feature = "deserialize", serde(default))]
471pub struct Options {
472    /// The hlsl shader model to be used
473    pub shader_model: ShaderModel,
474
475    /// HLSL binding information for each Naga global variable.
476    ///
477    /// This maps Naga [`GlobalVariable`]'s [`ResourceBinding`]s to a
478    /// [`BindTarget`] specifying its register number and space, along with
479    /// other details necessary to generate a full HLSL declaration for it,
480    /// or to access its value.
481    ///
482    /// This must provide a [`BindTarget`] for every [`GlobalVariable`] in the
483    /// [`Module`] that has a [`binding`].
484    ///
485    /// [`GlobalVariable`]: crate::ir::GlobalVariable
486    /// [`ResourceBinding`]: crate::ir::ResourceBinding
487    /// [`Module`]: crate::ir::Module
488    /// [`binding`]: crate::ir::GlobalVariable::binding
489    #[cfg_attr(
490        feature = "deserialize",
491        serde(deserialize_with = "deserialize_binding_map")
492    )]
493    pub binding_map: BindingMap,
494
495    /// Don't panic on missing bindings, instead generate any HLSL.
496    pub fake_missing_bindings: bool,
497    /// Add special constants to `SV_VertexIndex` and `SV_InstanceIndex`,
498    /// to make them work like in Vulkan/Metal, with help of the host.
499    pub special_constants_binding: Option<BindTarget>,
500
501    /// HLSL binding information for the [`Immediate`] global, if present.
502    ///
503    /// If a module contains a global in the [`Immediate`] address space, the
504    /// `dx12` backend stores its value directly in the root signature as a
505    /// series of [`D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS`], whose binding
506    /// information is given here.
507    ///
508    /// [`Immediate`]: crate::ir::AddressSpace::Immediate
509    /// [`D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS`]: https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ne-d3d12-d3d12_root_parameter_type
510    pub immediates_target: Option<BindTarget>,
511
512    /// HLSL binding information for the sampler heap and comparison sampler heap.
513    pub sampler_heap_target: SamplerHeapBindTargets,
514
515    /// Mapping of each bind group's sampler index buffer to a bind target.
516    #[cfg_attr(
517        feature = "deserialize",
518        serde(deserialize_with = "deserialize_sampler_index_buffer_bindings")
519    )]
520    pub sampler_buffer_binding_map: SamplerIndexBufferBindingMap,
521    /// Bind target for dynamic storage buffer offsets
522    #[cfg_attr(
523        feature = "deserialize",
524        serde(deserialize_with = "deserialize_storage_buffer_offsets")
525    )]
526    pub dynamic_storage_buffer_offsets_targets: DynamicStorageBufferOffsetsTargets,
527    #[cfg_attr(
528        feature = "deserialize",
529        serde(deserialize_with = "deserialize_external_texture_binding_map")
530    )]
531
532    /// HLSL binding information for [`External`] image global variables.
533    ///
534    /// See [`ExternalTextureBindTarget`] for details.
535    ///
536    /// [`External`]: crate::ir::ImageClass::External
537    pub external_texture_binding_map: ExternalTextureBindingMap,
538
539    /// Should workgroup variables be zero initialized (by polyfilling)?
540    pub zero_initialize_workgroup_memory: bool,
541    /// Should we restrict indexing of vectors, matrices and arrays?
542    pub restrict_indexing: bool,
543    /// If set, loops will have code injected into them, forcing the compiler
544    /// to think the number of iterations is bounded.
545    pub force_loop_bounding: bool,
546    /// if set, ray queries will get a variable to track their state to prevent
547    /// misuse.
548    pub ray_query_initialization_tracking: bool,
549}
550
551impl Default for Options {
552    fn default() -> Self {
553        Options {
554            shader_model: ShaderModel::V5_1,
555            binding_map: BindingMap::default(),
556            fake_missing_bindings: true,
557            special_constants_binding: None,
558            sampler_heap_target: SamplerHeapBindTargets::default(),
559            sampler_buffer_binding_map: alloc::collections::BTreeMap::default(),
560            immediates_target: None,
561            dynamic_storage_buffer_offsets_targets: alloc::collections::BTreeMap::new(),
562            external_texture_binding_map: ExternalTextureBindingMap::default(),
563            zero_initialize_workgroup_memory: true,
564            restrict_indexing: true,
565            force_loop_bounding: true,
566            ray_query_initialization_tracking: true,
567        }
568    }
569}
570
571impl Options {
572    fn resolve_resource_binding(
573        &self,
574        res_binding: &crate::ResourceBinding,
575    ) -> Result<BindTarget, EntryPointError> {
576        match self.binding_map.get(res_binding) {
577            Some(target) => Ok(*target),
578            None if self.fake_missing_bindings => Ok(BindTarget {
579                space: res_binding.group as u8,
580                register: res_binding.binding,
581                binding_array_size: None,
582                dynamic_storage_buffer_offsets_index: None,
583                restrict_indexing: false,
584            }),
585            None => Err(EntryPointError::MissingBinding(*res_binding)),
586        }
587    }
588
589    fn resolve_external_texture_resource_binding(
590        &self,
591        res_binding: &crate::ResourceBinding,
592    ) -> Result<ExternalTextureBindTarget, EntryPointError> {
593        match self.external_texture_binding_map.get(res_binding) {
594            Some(target) => Ok(*target),
595            None if self.fake_missing_bindings => {
596                let fake = BindTarget {
597                    space: res_binding.group as u8,
598                    register: res_binding.binding,
599                    binding_array_size: None,
600                    dynamic_storage_buffer_offsets_index: None,
601                    restrict_indexing: false,
602                };
603                Ok(ExternalTextureBindTarget {
604                    planes: [fake, fake, fake],
605                    params: fake,
606                })
607            }
608            None => Err(EntryPointError::MissingBinding(*res_binding)),
609        }
610    }
611}
612
613/// Reflection info for entry point names.
614#[derive(Default)]
615pub struct ReflectionInfo {
616    /// Mapping of the entry point names.
617    ///
618    /// Each item in the array corresponds to an entry point index. The real entry point name may be different if one of the
619    /// reserved words are used.
620    ///
621    /// Note: Some entry points may fail translation because of missing bindings.
622    pub entry_point_names: Vec<Result<String, EntryPointError>>,
623}
624
625/// A subset of options that are meant to be changed per pipeline.
626#[derive(Debug, Default, Clone)]
627#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
628#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
629#[cfg_attr(feature = "deserialize", serde(default))]
630pub struct PipelineOptions {
631    /// The entry point to write.
632    ///
633    /// Entry points are identified by a shader stage specification,
634    /// and a name.
635    ///
636    /// If `None`, all entry points will be written. If `Some` and the entry
637    /// point is not found, an error will be thrown while writing.
638    pub entry_point: Option<(ir::ShaderStage, String)>,
639}
640
641#[derive(Error, Debug)]
642pub enum Error {
643    #[error(transparent)]
644    IoError(#[from] FmtError),
645    #[error("A scalar with an unsupported width was requested: {0:?}")]
646    UnsupportedScalar(crate::Scalar),
647    #[error("{0}")]
648    Unimplemented(String), // TODO: Error used only during development
649    #[error("{0}")]
650    Custom(String),
651    #[error("overrides should not be present at this stage")]
652    Override,
653    #[error(transparent)]
654    ResolveArraySizeError(#[from] proc::ResolveArraySizeError),
655    #[error("entry point with stage {0:?} and name '{1}' not found")]
656    EntryPointNotFound(ir::ShaderStage, String),
657    #[error("requires shader model {1:?} for reason: {0}")]
658    ShaderModelTooLow(String, ShaderModel),
659}
660
661#[derive(PartialEq, Eq, Hash)]
662enum WrappedType {
663    ZeroValue(help::WrappedZeroValue),
664    ArrayLength(help::WrappedArrayLength),
665    ImageSample(help::WrappedImageSample),
666    ImageQuery(help::WrappedImageQuery),
667    ImageLoad(help::WrappedImageLoad),
668    ImageLoadScalar(crate::Scalar),
669    Constructor(help::WrappedConstructor),
670    StructMatrixAccess(help::WrappedStructMatrixAccess),
671    MatCx2(help::WrappedMatCx2),
672    Math(help::WrappedMath),
673    UnaryOp(help::WrappedUnaryOp),
674    BinaryOp(help::WrappedBinaryOp),
675    Cast(help::WrappedCast),
676}
677
678#[derive(Default)]
679struct Wrapped {
680    types: crate::FastHashSet<WrappedType>,
681    /// If true, the sampler heaps have been written out.
682    sampler_heaps: bool,
683    // Mapping from SamplerIndexBufferKey to the name the namer returned.
684    sampler_index_buffers: crate::FastHashMap<SamplerIndexBufferKey, String>,
685}
686
687impl Wrapped {
688    fn insert(&mut self, r#type: WrappedType) -> bool {
689        self.types.insert(r#type)
690    }
691
692    fn clear(&mut self) {
693        self.types.clear();
694    }
695}
696
697/// A fragment entry point to be considered when generating HLSL for the output interface of vertex
698/// entry points.
699///
700/// This is provided as an optional parameter to [`Writer::write`].
701///
702/// If this is provided, vertex outputs will be removed if they are not inputs of this fragment
703/// entry point. This is necessary for generating correct HLSL when some of the vertex shader
704/// outputs are not consumed by the fragment shader.
705pub struct FragmentEntryPoint<'a> {
706    module: &'a crate::Module,
707    func: &'a crate::Function,
708}
709
710impl<'a> FragmentEntryPoint<'a> {
711    /// Returns `None` if the entry point with the provided name can't be found or isn't a fragment
712    /// entry point.
713    pub fn new(module: &'a crate::Module, ep_name: &'a str) -> Option<Self> {
714        module
715            .entry_points
716            .iter()
717            .find(|ep| ep.name == ep_name)
718            .filter(|ep| ep.stage == crate::ShaderStage::Fragment)
719            .map(|ep| Self {
720                module,
721                func: &ep.function,
722            })
723    }
724}
725
726pub struct Writer<'a, W> {
727    out: W,
728    names: crate::FastHashMap<proc::NameKey, String>,
729    namer: proc::Namer,
730    /// HLSL backend options
731    options: &'a Options,
732    /// Per-stage backend options
733    pipeline_options: &'a PipelineOptions,
734    /// Information about entry point arguments and result types.
735    entry_point_io: crate::FastHashMap<usize, writer::EntryPointInterface>,
736    /// Set of expressions that have associated temporary variables
737    named_expressions: crate::NamedExpressions,
738    wrapped: Wrapped,
739    written_committed_intersection: bool,
740    written_candidate_intersection: bool,
741    continue_ctx: back::continue_forward::ContinueCtx,
742
743    /// A reference to some part of a global variable, lowered to a series of
744    /// byte offset calculations.
745    ///
746    /// See the [`storage`] module for background on why we need this.
747    ///
748    /// Each [`SubAccess`] in the vector is a lowering of some [`Access`] or
749    /// [`AccessIndex`] expression to the level of byte strides and offsets. See
750    /// [`SubAccess`] for details.
751    ///
752    /// This field is a member of [`Writer`] solely to allow re-use of
753    /// the `Vec`'s dynamic allocation. The value is no longer needed
754    /// once HLSL for the access has been generated.
755    ///
756    /// [`Storage`]: crate::AddressSpace::Storage
757    /// [`SubAccess`]: storage::SubAccess
758    /// [`Access`]: crate::Expression::Access
759    /// [`AccessIndex`]: crate::Expression::AccessIndex
760    temp_access_chain: Vec<storage::SubAccess>,
761    need_bake_expressions: back::NeedBakeExpressions,
762}
763
764pub fn supported_capabilities() -> crate::valid::Capabilities {
765    use crate::valid::Capabilities as Caps;
766    Caps::IMMEDIATES
767        | Caps::FLOAT64 // Unsupported by wgpu but supported by naga
768        | Caps::PRIMITIVE_INDEX
769        | Caps::TEXTURE_AND_SAMPLER_BINDING_ARRAY
770        // No BUFFER_BINDING_ARRAY
771        | Caps::STORAGE_TEXTURE_BINDING_ARRAY
772        // No STORAGE_BUFFER_BINDING_ARRAY
773        // No CLIP_DISTANCE
774        // No CULL_DISTANCE
775        | Caps::STORAGE_TEXTURE_16BIT_NORM_FORMATS
776        | Caps::MULTIVIEW
777        // No EARLY_DEPTH_TEST
778        | Caps::MULTISAMPLED_SHADING
779        | Caps::RAY_QUERY
780        | Caps::DUAL_SOURCE_BLENDING
781        | Caps::CUBE_ARRAY_TEXTURES
782        | Caps::SHADER_INT64
783        | Caps::SUBGROUP
784        // No SUBGROUP_BARRIER
785        // No SUBGROUP_VERTEX_STAGE
786        | Caps::SHADER_INT64_ATOMIC_MIN_MAX
787        | Caps::SHADER_INT64_ATOMIC_ALL_OPS
788        // No SHADER_FLOAT32_ATOMIC
789        | Caps::TEXTURE_ATOMIC
790        | Caps::TEXTURE_INT64_ATOMIC
791        // No RAY_HIT_VERTEX_POSITION
792        | Caps::SHADER_FLOAT16
793        | Caps::TEXTURE_EXTERNAL
794        | Caps::SHADER_FLOAT16_IN_FLOAT32
795        | Caps::SHADER_BARYCENTRICS
796        // No MESH_SHADER
797        // No MESH_SHADER_POINT_TOPOLOGY
798        | Caps::TEXTURE_AND_SAMPLER_BINDING_ARRAY_NON_UNIFORM_INDEXING
799        // No BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING
800        | Caps::STORAGE_TEXTURE_BINDING_ARRAY_NON_UNIFORM_INDEXING
801        | Caps::STORAGE_BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING
802    // No COOPERATIVE_MATRIX
803    // No PER_VERTEX
804    // No RAY_TRACING_PIPELINE
805    // No DRAW_INDEX
806}