Skip to main content

mew/
sampler.rs

1/// Define a sampler.
2///
3/// The only mandatory thing you _have_ to set is the
4/// [`wgpu::SamplerBindingType`](https://docs.rs/wgpu/latest/wgpu/enum.SamplerBindingType.html)
5/// for its binding. You can also change the
6/// [`wgpu::SamplerDescriptor`](https://docs.rs/wgpu/latest/wgpu/type.SamplerDescriptor.html)
7/// properties if you want, the fields you omit will just be the default...
8/// or just omit the whole section if you really don't care.
9///
10/// ```
11/// sampler! { DontCare as Filtering }
12/// sampler! { DidntAsk as Filtering {
13///     address_mode_u: wgpu::AddressMode::Repeat,
14///     mag_filter: wgpu::FilterMode::Nearest,
15/// } }
16///
17/// let sampler: DidntAsk = context.new_sampler();
18/// ```
19#[macro_export]
20macro_rules! sampler {
21    { $struct:ident as $filtering:ident } => {
22        $crate::sampler! { $struct as $filtering {} }
23    };
24    { $struct:ident as $filtering:ident {
25        $( $field:ident : $value:expr ),* $(,)?
26    } } => {
27        #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
28        pub struct $struct {
29            sampler: $crate::wgpu::Sampler,
30        }
31
32        impl ::std::ops::Deref for $struct {
33            type Target = $crate::wgpu::Sampler;
34            fn deref(&self) -> &Self::Target {
35                &self.sampler
36            }
37        }
38
39        impl $crate::sampler::MewSampler for $struct {
40            fn new(sampler: $crate::wgpu::Sampler) -> Self {
41                Self {
42                    sampler,
43                }
44            }
45
46            fn binding_type() -> $crate::wgpu::BindingType {
47                $crate::wgpu::BindingType::Sampler(
48                    $crate::wgpu::SamplerBindingType::$filtering
49                )
50            }
51
52            fn buffer_desc() -> $crate::wgpu::SamplerDescriptor<'static> {
53                #[allow(unused_imports)]
54                use $crate::wgpu::{
55                    AddressMode::*,
56                    FilterMode::*,
57                    CompareFunction::*,
58                    SamplerBorderColor::*,
59                };
60                $crate::wgpu::SamplerDescriptor {
61                    label: Some(concat!(stringify!($struct), " Sampler")),
62                    $( $field : $value, )*
63                    ..Default::default()
64                }
65            }
66
67            fn as_binding<'a>(&'a self) -> $crate::wgpu::BindingResource<'a> {
68                $crate::wgpu::BindingResource::Sampler(&self.sampler)
69            }
70        }
71    }
72}
73pub use sampler;
74
75
76/// Trait for internal use to work with samplers.
77pub trait MewSampler {
78    /// Build this sampler type from a
79    /// [`wgpu::Sampler`](https://docs.rs/wgpu/latest/wgpu/struct.Sampler.html).
80    fn new(buffer: wgpu::Sampler) -> Self;
81    /// Get a [`wgpu::BindingType`](https://docs.rs/wgpu/latest/wgpu/enum.BindingType.html)
82    /// to use for this type of sampler.
83    fn binding_type() -> wgpu::BindingType;
84    /// Get a [`wgpu::SamplerDescriptor`](https://docs.rs/wgpu/latest/wgpu/type.SamplerDescriptor.html)
85    /// to build this type of sampler.
86    fn buffer_desc() -> wgpu::SamplerDescriptor<'static>;
87    /// Get this sampler's [`wgpu::BindingResource`](https://docs.rs/wgpu/latest/wgpu/enum.BindingResource.html)
88    /// to bind it in a bind group.
89    fn as_binding<'a>(&'a self) -> wgpu::BindingResource<'a>;
90}