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. Start with a global function that returns this
21/// struct, and then chain additional calls. No care is taken to avoid stating multiple operands that,
22/// together, make no sense, such as Lod and Grad.
23/// Example: `image.sample_with(coords, sample_with::bias(3.0).sample_index(1))`
24pub struct SampleParams<B: OptionTy, L: OptionTy, G: OptionTy, S: OptionTy> {
25    /// 'Bias' image operand
26    pub bias: B,
27
28    /// 'Lod' image operand
29    pub lod: L,
30
31    /// 'Grad' image operand
32    pub grad: G,
33
34    /// 'Sample' image operandy
35    pub sample_index: S,
36}
37
38/// Sets the 'Bias' image operand
39pub fn bias<B>(bias: B) -> SampleParams<SomeTy<B>, NoneTy, NoneTy, NoneTy> {
40    SampleParams {
41        bias: SomeTy(bias),
42        lod: NoneTy,
43        grad: NoneTy,
44        sample_index: NoneTy,
45    }
46}
47
48/// Sets the 'Lod' image operand
49pub fn lod<L>(lod: L) -> SampleParams<NoneTy, SomeTy<L>, NoneTy, NoneTy> {
50    SampleParams {
51        bias: NoneTy,
52        lod: SomeTy(lod),
53        grad: NoneTy,
54        sample_index: NoneTy,
55    }
56}
57
58/// Sets the 'Grad' image operand
59pub fn grad<T>(grad_x: T, grad_y: T) -> SampleParams<NoneTy, NoneTy, SomeTy<(T, T)>, NoneTy> {
60    SampleParams {
61        bias: NoneTy,
62        lod: NoneTy,
63        grad: SomeTy((grad_x, grad_y)),
64        sample_index: NoneTy,
65    }
66}
67
68/// Sets the 'Sample' image operand
69pub fn sample_index<S>(sample_index: S) -> SampleParams<NoneTy, NoneTy, NoneTy, SomeTy<S>> {
70    SampleParams {
71        bias: NoneTy,
72        lod: NoneTy,
73        grad: NoneTy,
74        sample_index: SomeTy(sample_index),
75    }
76}
77
78impl<L: OptionTy, G: OptionTy, S: OptionTy> SampleParams<NoneTy, L, G, S> {
79    /// Sets the 'Bias' image operand
80    pub fn bias<B>(self, bias: B) -> SampleParams<SomeTy<B>, L, G, S> {
81        SampleParams {
82            bias: SomeTy(bias),
83            lod: self.lod,
84            grad: self.grad,
85            sample_index: self.sample_index,
86        }
87    }
88}
89
90impl<B: OptionTy, G: OptionTy, S: OptionTy> SampleParams<B, NoneTy, G, S> {
91    /// Sets the 'Lod' image operand
92    pub fn lod<L>(self, lod: L) -> SampleParams<B, SomeTy<L>, G, S> {
93        SampleParams {
94            bias: self.bias,
95            lod: SomeTy(lod),
96            grad: self.grad,
97            sample_index: self.sample_index,
98        }
99    }
100}
101
102impl<B: OptionTy, L: OptionTy, S: OptionTy> SampleParams<B, L, NoneTy, S> {
103    /// Sets the 'Lod' image operand
104    pub fn grad<T>(self, grad_x: T, grad_y: T) -> SampleParams<B, L, SomeTy<(T, T)>, S> {
105        SampleParams {
106            bias: self.bias,
107            lod: self.lod,
108            grad: SomeTy((grad_x, grad_y)),
109            sample_index: self.sample_index,
110        }
111    }
112}
113
114impl<B: OptionTy, L: OptionTy, G: OptionTy> SampleParams<B, L, G, NoneTy> {
115    /// Sets the 'Sample' image operand
116    pub fn sample_index<S>(self, sample_index: S) -> SampleParams<B, L, G, SomeTy<S>> {
117        SampleParams {
118            bias: self.bias,
119            lod: self.lod,
120            grad: self.grad,
121            sample_index: SomeTy(sample_index),
122        }
123    }
124}