Skip to main content

spirv_webgpu_transform/
correction.rs

1use super::*;
2
3// Q: Hey what happens when you stack corrections?
4// A: I don't want to think about it... I will start thinking after a refactor...
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[repr(u16)]
8pub enum CorrectionType {
9    /// A combined image sampler has been split, a new `sampler` object should be inserted.
10    SplitCombined,
11    /// A mixed depth texture / sampler has been duplicated, insert the same object again with a `Regular` bind type.
12    SplitDrefRegular,
13    /// A mixed depth texture / sampler has been duplicated, insert the same object again with a
14    /// `Comparison` bind type.
15    SplitDrefComparison,
16    /// A storage cube texture has been converted into a storage texture 2D array, change the dimension.
17    ConvertStorageCube,
18    /// A binding array has been split into new variables. Insert the same resource again.
19    /// For an `N` sized array, expect `N-1` entries.
20    SplitBindingArray,
21}
22
23#[derive(Debug, Clone, Default, PartialEq, Eq)]
24pub struct CorrectionBinding {
25    /// In order, what additional bindings have been appended to this one.
26    pub corrections: Vec<CorrectionType>,
27}
28
29#[derive(Debug, Clone, Default, PartialEq, Eq)]
30pub struct CorrectionSet {
31    pub bindings: HashMap<u32, CorrectionBinding>,
32}
33
34#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
35pub enum ImmediatesSetMode {
36    /// Place the immediates binding at certain set.
37    #[default]
38    Absolute,
39    /// Place the immediates binding at the max set or up to a certain set.
40    /// This causes immediates to always mix with existing sets which may have negative performance
41    /// implications.
42    ///
43    /// TODO(davnotdev): Sounds like a great idea but what about mirroring?
44    /// What I mean is that this will cause you issues if you have a vertex shader and fragment
45    /// shader with different max uniform sets.
46    MaxUpTo,
47    /// Place the immediates binding in the set after the max set or up to a certain set.
48    /// Unlike [`ImmediatesSetMode::MaxUpTo`], this attempts to give immediates their own set if
49    /// possible.
50    ///
51    /// TODO(davnotdev): Sounds like a great idea but what about mirroring?
52    /// What I mean is that this will cause you issues if you have a vertex shader and fragment
53    /// shader with different max uniform sets.
54    MaxPlusOneUpTo,
55}
56
57#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
58pub enum ImmediatesBindingMode {
59    /// Always place the immediates binding one binding after the max binding of the set.
60    /// TODO(davnotdev): Once again, sounds like a great idea but what about mirroring?
61    /// If you have a fragment and vertex shader with hidden bindings, you will run into issues
62    #[default]
63    Last,
64    /// Place the immediates binding in a fixed binding index.
65    Absolute,
66}
67
68/// Lookup a set and a binding for a list of [`CorrectionType`].
69/// In order, insert a new variable for each, see [`CorrectionType`] for what type of object should
70/// be inserted for each variant.
71#[derive(Debug, Clone, Default, PartialEq, Eq)]
72pub struct CorrectionMap {
73    pub sets: Option<HashMap<u32, CorrectionSet>>,
74    /// Represents both an input/output where the immediate uniforms should/is written to.
75    /// Control the set immediates should be written to by setting before patching.
76    /// If this is [`None`], this becomes the max set plus one.
77    pub immediates_set: Option<u32>,
78    /// Works as an input setting controlling the behaviour of `immediates_set`.
79    /// This is valuable when dealing with WebGPU's `maxBindGroup` especially if your sets
80    /// previously followed the maximum.
81    pub immediates_set_mode: Option<ImmediatesSetMode>,
82    /// Represents both an input/output representing which binding the immediate uniform is written
83    /// to.
84    /// If [`ImmediatesBindingMode`] is [`ImmediatesBindingMode::Absolute`], set the index of the
85    /// immediates buffer binding.
86    pub immediates_binding: Option<u32>,
87    /// Works as an input setting, controlling how the immediate uniform's binding index should be determined
88    /// within the bind group set.
89    pub immediates_binding_mode: Option<ImmediatesBindingMode>,
90}