Skip to main content

wgpu/api/
sampler.rs

1use crate::*;
2
3/// Handle to a sampler.
4///
5/// A `Sampler` object defines how a pipeline will sample from a [`TextureView`]. Samplers define
6/// image filters (including anisotropy) and address (wrapping) modes, among other things. See
7/// the documentation for [`SamplerDescriptor`] for more information.
8///
9/// It can be created with [`Device::create_sampler`].
10///
11/// Corresponds to [WebGPU `GPUSampler`](https://gpuweb.github.io/gpuweb/#sampler-interface).
12#[derive(Debug, Clone)]
13pub struct Sampler {
14    pub(crate) inner: dispatch::DispatchSampler,
15}
16#[cfg(send_sync)]
17static_assertions::assert_impl_all!(Sampler: Send, Sync);
18
19crate::cmp::impl_eq_ord_hash_proxy!(Sampler => .inner);
20
21impl Sampler {
22    #[cfg(custom)]
23    /// Returns custom implementation of Sampler (if custom backend and is internally T)
24    pub fn as_custom<T: custom::SamplerInterface>(&self) -> Option<&T> {
25        self.inner.as_custom()
26    }
27}
28
29#[cfg(wgpu_core)]
30impl Sampler {
31    /// Create a new sampler of wgpu from a wgpu-core sampler.
32    ///
33    /// # Arguments
34    ///
35    /// - `core_sampler` - wgpu-core sampler.
36    pub fn from_core(core_sampler: alloc::sync::Arc<wgc::resource::Sampler>) -> Self {
37        Self {
38            inner: crate::backend::wgpu_core::CoreSampler::from_core(core_sampler).into(),
39        }
40    }
41
42    /// Returns the underlying wgpu-core sampler if this `Sampler` is on the wgpu-core backend, otherwise `None`.
43    pub fn as_core(&self) -> Option<alloc::sync::Arc<wgc::resource::Sampler>> {
44        self.inner.as_core_opt().map(|s| s.as_core())
45    }
46}
47
48/// Describes a [`Sampler`].
49///
50/// For use with [`Device::create_sampler`].
51///
52/// Corresponds to [WebGPU `GPUSamplerDescriptor`](
53/// https://gpuweb.github.io/gpuweb/#dictdef-gpusamplerdescriptor).
54pub type SamplerDescriptor<'a> = wgt::SamplerDescriptor<Label<'a>>;
55static_assertions::assert_impl_all!(SamplerDescriptor<'_>: Send, Sync);