Skip to main content

pebble/wgpu/
samplers.rs

1#[derive(Clone, Copy, PartialEq, Eq, Hash)]
2pub enum SamplerKind {
3    LinearRepeat,
4    LinearClamp,
5    /// Like `LinearClamp`, but clamped to mip 0 only (`lod_min_clamp`/
6    /// `lod_max_clamp` both 0.0). Use this when you need to force sampling
7    /// the base level regardless of how many mips the texture actually
8    /// has — e.g. an environment-map capture pass where blending across
9    /// mips would introduce blur you don't want in the baked result.
10    LinearClampNoMip,
11    Nearest,
12    /// Nearest-filtered (min/mag), clamped to an opaque white border rather
13    /// than the edge texel — matches the classic GL texture-array setup:
14    /// `GL_NEAREST` + `GL_CLAMP_TO_BORDER` + a `{1,1,1,1}` border color.
15    /// Useful for a `texture_2d_array` where sampling past a layer's edge
16    /// should read as "nothing here" (white) instead of edge bleed.
17    NearestClampBorder,
18    /// A *comparison* sampler (`sampler_comparison` in WGSL, sampled via
19    /// `textureSampleCompare`) using `CompareFunction::Less` — the
20    /// standard shadow-map test ("is this fragment's depth less than the
21    /// depth stored at this texel"). Linear-filtered, clamped to edge.
22    /// Must be paired with [`BindingKind::ComparisonSampler`](crate::wgpu::BindingKind::ComparisonSampler)/
23    /// [`BindingEntry::comparison_sampler`](crate::wgpu::BindingEntry::comparison_sampler)
24    /// in the layout — a regular `Sampler` binding won't accept a
25    /// comparison sampler, and vice versa.
26    CompareLess,
27}
28
29impl SamplerKind {
30    /// Pure conversion to a wgpu descriptor. You still call
31    /// `device.create_sampler(&kind.descriptor())` yourself, in your own
32    /// `LazyResource::construct` — this only fixes the handful of
33    /// address-mode/filter combinations so you don't re-derive them.
34    pub fn descriptor(&self) -> wgpu::SamplerDescriptor<'static> {
35        match self {
36            SamplerKind::LinearRepeat => wgpu::SamplerDescriptor {
37                address_mode_u: wgpu::AddressMode::Repeat,
38                address_mode_v: wgpu::AddressMode::Repeat,
39                address_mode_w: wgpu::AddressMode::Repeat,
40                mag_filter: wgpu::FilterMode::Linear,
41                min_filter: wgpu::FilterMode::Linear,
42                mipmap_filter: wgpu::MipmapFilterMode::Linear,
43                ..Default::default()
44            },
45            SamplerKind::LinearClamp => wgpu::SamplerDescriptor {
46                address_mode_u: wgpu::AddressMode::ClampToEdge,
47                address_mode_v: wgpu::AddressMode::ClampToEdge,
48                address_mode_w: wgpu::AddressMode::ClampToEdge,
49                mag_filter: wgpu::FilterMode::Linear,
50                min_filter: wgpu::FilterMode::Linear,
51                mipmap_filter: wgpu::MipmapFilterMode::Linear,
52                ..Default::default()
53            },
54            SamplerKind::LinearClampNoMip => wgpu::SamplerDescriptor {
55                address_mode_u: wgpu::AddressMode::ClampToEdge,
56                address_mode_v: wgpu::AddressMode::ClampToEdge,
57                address_mode_w: wgpu::AddressMode::ClampToEdge,
58                mag_filter: wgpu::FilterMode::Linear,
59                min_filter: wgpu::FilterMode::Linear,
60                mipmap_filter: wgpu::MipmapFilterMode::Linear,
61                lod_min_clamp: 0.0,
62                lod_max_clamp: 0.0,
63                ..Default::default()
64            },
65            SamplerKind::Nearest => wgpu::SamplerDescriptor {
66                mag_filter: wgpu::FilterMode::Nearest,
67                min_filter: wgpu::FilterMode::Nearest,
68                mipmap_filter: wgpu::MipmapFilterMode::Linear,
69                ..Default::default()
70            },
71            SamplerKind::NearestClampBorder => wgpu::SamplerDescriptor {
72                address_mode_u: wgpu::AddressMode::ClampToBorder,
73                address_mode_v: wgpu::AddressMode::ClampToBorder,
74                address_mode_w: wgpu::AddressMode::ClampToBorder,
75                mag_filter: wgpu::FilterMode::Nearest,
76                min_filter: wgpu::FilterMode::Nearest,
77                mipmap_filter: wgpu::MipmapFilterMode::Nearest,
78                border_color: Some(wgpu::SamplerBorderColor::OpaqueWhite),
79                ..Default::default()
80            },
81            SamplerKind::CompareLess => wgpu::SamplerDescriptor {
82                address_mode_u: wgpu::AddressMode::ClampToEdge,
83                address_mode_v: wgpu::AddressMode::ClampToEdge,
84                address_mode_w: wgpu::AddressMode::ClampToEdge,
85                mag_filter: wgpu::FilterMode::Linear,
86                min_filter: wgpu::FilterMode::Linear,
87                mipmap_filter: wgpu::MipmapFilterMode::Linear,
88                compare: Some(wgpu::CompareFunction::Less),
89                ..Default::default()
90            },
91        }
92    }
93}