spirv_std/image/
sample_with.rs

1/// Helper trait to mimic `Option<T>`, but where the variant are types
2pub trait OptionTy {
3    /// Whether this is a `NoneTy` (when false) or a `SomeTy<T>` (when true)
4    const EXISTS: bool;
5}
6
7impl OptionTy for NoneTy {
8    const EXISTS: bool = false;
9}
10
11impl<T> OptionTy for SomeTy<T> {
12    const EXISTS: bool = true;
13}
14/// Helper struct that denotes that the type doesn't exist, analog to `Option::None`
15pub struct NoneTy;
16
17/// Helper struct that denotes that the type does exist and is of type T, analog to `Option::Some(T)`
18pub struct SomeTy<T>(pub T);
19
20/// Helper struct that allows building image operands.
21///
22/// Start with a global function that returns this struct, and then chain additional calls.
23/// No care is taken to avoid stating multiple operands that, together, make no sense, such as Lod and Grad.
24/// Example: `image.sample_with(coords, sample_with::bias(3.0).sample_index(1))`
25pub struct SampleParams<B: OptionTy, L: OptionTy, G: OptionTy, S: OptionTy> {
26    /// 'Bias' image operand
27    pub bias: B,
28
29    /// 'Lod' image operand
30    pub lod: L,
31
32    /// 'Grad' image operand
33    pub grad: G,
34
35    /// 'Sample' image operandy
36    pub sample_index: S,
37}
38
39/// Sets the 'Bias' image operand
40pub fn bias<B>(bias: B) -> SampleParams<SomeTy<B>, NoneTy, NoneTy, NoneTy> {
41    SampleParams {
42        bias: SomeTy(bias),
43        lod: NoneTy,
44        grad: NoneTy,
45        sample_index: NoneTy,
46    }
47}
48
49/// Sets the 'Lod' image operand
50pub fn lod<L>(lod: L) -> SampleParams<NoneTy, SomeTy<L>, NoneTy, NoneTy> {
51    SampleParams {
52        bias: NoneTy,
53        lod: SomeTy(lod),
54        grad: NoneTy,
55        sample_index: NoneTy,
56    }
57}
58
59/// Sets the 'Grad' image operand
60pub fn grad<T>(grad_x: T, grad_y: T) -> SampleParams<NoneTy, NoneTy, SomeTy<(T, T)>, NoneTy> {
61    SampleParams {
62        bias: NoneTy,
63        lod: NoneTy,
64        grad: SomeTy((grad_x, grad_y)),
65        sample_index: NoneTy,
66    }
67}
68
69/// Sets the 'Sample' image operand
70pub fn sample_index<S>(sample_index: S) -> SampleParams<NoneTy, NoneTy, NoneTy, SomeTy<S>> {
71    SampleParams {
72        bias: NoneTy,
73        lod: NoneTy,
74        grad: NoneTy,
75        sample_index: SomeTy(sample_index),
76    }
77}
78
79impl<L: OptionTy, G: OptionTy, S: OptionTy> SampleParams<NoneTy, L, G, S> {
80    /// Sets the 'Bias' image operand
81    pub fn bias<B>(self, bias: B) -> SampleParams<SomeTy<B>, L, G, S> {
82        SampleParams {
83            bias: SomeTy(bias),
84            lod: self.lod,
85            grad: self.grad,
86            sample_index: self.sample_index,
87        }
88    }
89}
90
91impl<B: OptionTy, G: OptionTy, S: OptionTy> SampleParams<B, NoneTy, G, S> {
92    /// Sets the 'Lod' image operand
93    pub fn lod<L>(self, lod: L) -> SampleParams<B, SomeTy<L>, G, S> {
94        SampleParams {
95            bias: self.bias,
96            lod: SomeTy(lod),
97            grad: self.grad,
98            sample_index: self.sample_index,
99        }
100    }
101}
102
103impl<B: OptionTy, L: OptionTy, S: OptionTy> SampleParams<B, L, NoneTy, S> {
104    /// Sets the 'Lod' image operand
105    pub fn grad<T>(self, grad_x: T, grad_y: T) -> SampleParams<B, L, SomeTy<(T, T)>, S> {
106        SampleParams {
107            bias: self.bias,
108            lod: self.lod,
109            grad: SomeTy((grad_x, grad_y)),
110            sample_index: self.sample_index,
111        }
112    }
113}
114
115impl<B: OptionTy, L: OptionTy, G: OptionTy> SampleParams<B, L, G, NoneTy> {
116    /// Sets the 'Sample' image operand
117    pub fn sample_index<S>(self, sample_index: S) -> SampleParams<B, L, G, SomeTy<S>> {
118        SampleParams {
119            bias: self.bias,
120            lod: self.lod,
121            grad: self.grad,
122            sample_index: SomeTy(sample_index),
123        }
124    }
125}