wgpu/api/
sampler.rs

1use std::{sync::Arc, thread};
2
3use crate::*;
4
5/// Handle to a sampler.
6///
7/// A `Sampler` object defines how a pipeline will sample from a [`TextureView`]. Samplers define
8/// image filters (including anisotropy) and address (wrapping) modes, among other things. See
9/// the documentation for [`SamplerDescriptor`] for more information.
10///
11/// It can be created with [`Device::create_sampler`].
12///
13/// Corresponds to [WebGPU `GPUSampler`](https://gpuweb.github.io/gpuweb/#sampler-interface).
14#[derive(Debug)]
15pub struct Sampler {
16    pub(crate) context: Arc<C>,
17    pub(crate) data: Box<Data>,
18}
19#[cfg(send_sync)]
20static_assertions::assert_impl_all!(Sampler: Send, Sync);
21
22super::impl_partialeq_eq_hash!(Sampler);
23
24impl Drop for Sampler {
25    fn drop(&mut self) {
26        if !thread::panicking() {
27            self.context.sampler_drop(self.data.as_ref());
28        }
29    }
30}
31
32/// Describes a [`Sampler`].
33///
34/// For use with [`Device::create_sampler`].
35///
36/// Corresponds to [WebGPU `GPUSamplerDescriptor`](
37/// https://gpuweb.github.io/gpuweb/#dictdef-gpusamplerdescriptor).
38#[derive(Clone, Debug, PartialEq)]
39pub struct SamplerDescriptor<'a> {
40    /// Debug label of the sampler. This will show up in graphics debuggers for easy identification.
41    pub label: Label<'a>,
42    /// How to deal with out of bounds accesses in the u (i.e. x) direction
43    pub address_mode_u: AddressMode,
44    /// How to deal with out of bounds accesses in the v (i.e. y) direction
45    pub address_mode_v: AddressMode,
46    /// How to deal with out of bounds accesses in the w (i.e. z) direction
47    pub address_mode_w: AddressMode,
48    /// How to filter the texture when it needs to be magnified (made larger)
49    pub mag_filter: FilterMode,
50    /// How to filter the texture when it needs to be minified (made smaller)
51    pub min_filter: FilterMode,
52    /// How to filter between mip map levels
53    pub mipmap_filter: FilterMode,
54    /// Minimum level of detail (i.e. mip level) to use
55    pub lod_min_clamp: f32,
56    /// Maximum level of detail (i.e. mip level) to use
57    pub lod_max_clamp: f32,
58    /// If this is enabled, this is a comparison sampler using the given comparison function.
59    pub compare: Option<CompareFunction>,
60    /// Must be at least 1. If this is not 1, all filter modes must be linear.
61    pub anisotropy_clamp: u16,
62    /// Border color to use when address_mode is [`AddressMode::ClampToBorder`]
63    pub border_color: Option<SamplerBorderColor>,
64}
65static_assertions::assert_impl_all!(SamplerDescriptor<'_>: Send, Sync);
66
67impl Default for SamplerDescriptor<'_> {
68    fn default() -> Self {
69        Self {
70            label: None,
71            address_mode_u: Default::default(),
72            address_mode_v: Default::default(),
73            address_mode_w: Default::default(),
74            mag_filter: Default::default(),
75            min_filter: Default::default(),
76            mipmap_filter: Default::default(),
77            lod_min_clamp: 0.0,
78            lod_max_clamp: 32.0,
79            compare: None,
80            anisotropy_clamp: 1,
81            border_color: None,
82        }
83    }
84}