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
40#[inline]
41pub fn bias<B>(bias: B) -> SampleParams<SomeTy<B>, NoneTy, NoneTy, NoneTy> {
42    SampleParams {
43        bias: SomeTy(bias),
44        lod: NoneTy,
45        grad: NoneTy,
46        sample_index: NoneTy,
47    }
48}
49
50/// Sets the 'Lod' image operand
51#[inline]
52pub fn lod<L>(lod: L) -> SampleParams<NoneTy, SomeTy<L>, NoneTy, NoneTy> {
53    SampleParams {
54        bias: NoneTy,
55        lod: SomeTy(lod),
56        grad: NoneTy,
57        sample_index: NoneTy,
58    }
59}
60
61/// Sets the 'Grad' image operand
62#[inline]
63pub fn grad<T>(grad_x: T, grad_y: T) -> SampleParams<NoneTy, NoneTy, SomeTy<(T, T)>, NoneTy> {
64    SampleParams {
65        bias: NoneTy,
66        lod: NoneTy,
67        grad: SomeTy((grad_x, grad_y)),
68        sample_index: NoneTy,
69    }
70}
71
72/// Sets the 'Sample' image operand
73#[inline]
74pub fn sample_index<S>(sample_index: S) -> SampleParams<NoneTy, NoneTy, NoneTy, SomeTy<S>> {
75    SampleParams {
76        bias: NoneTy,
77        lod: NoneTy,
78        grad: NoneTy,
79        sample_index: SomeTy(sample_index),
80    }
81}
82
83impl<L: OptionTy, G: OptionTy, S: OptionTy> SampleParams<NoneTy, L, G, S> {
84    /// Sets the 'Bias' image operand
85    #[inline]
86    pub fn bias<B>(self, bias: B) -> SampleParams<SomeTy<B>, L, G, S> {
87        SampleParams {
88            bias: SomeTy(bias),
89            lod: self.lod,
90            grad: self.grad,
91            sample_index: self.sample_index,
92        }
93    }
94}
95
96impl<B: OptionTy, G: OptionTy, S: OptionTy> SampleParams<B, NoneTy, G, S> {
97    /// Sets the 'Lod' image operand
98    #[inline]
99    pub fn lod<L>(self, lod: L) -> SampleParams<B, SomeTy<L>, G, S> {
100        SampleParams {
101            bias: self.bias,
102            lod: SomeTy(lod),
103            grad: self.grad,
104            sample_index: self.sample_index,
105        }
106    }
107}
108
109impl<B: OptionTy, L: OptionTy, S: OptionTy> SampleParams<B, L, NoneTy, S> {
110    /// Sets the 'Lod' image operand
111    #[inline]
112    pub fn grad<T>(self, grad_x: T, grad_y: T) -> SampleParams<B, L, SomeTy<(T, T)>, S> {
113        SampleParams {
114            bias: self.bias,
115            lod: self.lod,
116            grad: SomeTy((grad_x, grad_y)),
117            sample_index: self.sample_index,
118        }
119    }
120}
121
122impl<B: OptionTy, L: OptionTy, G: OptionTy> SampleParams<B, L, G, NoneTy> {
123    /// Sets the 'Sample' image operand
124    #[inline]
125    pub fn sample_index<S>(self, sample_index: S) -> SampleParams<B, L, G, SomeTy<S>> {
126        SampleParams {
127            bias: self.bias,
128            lod: self.lod,
129            grad: self.grad,
130            sample_index: SomeTy(sample_index),
131        }
132    }
133}