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}
19
20impl SamplerKind {
21    /// Pure conversion to a wgpu descriptor. You still call
22    /// `device.create_sampler(&kind.descriptor())` yourself, in your own
23    /// `LazyResource::construct` — this only fixes the handful of
24    /// address-mode/filter combinations so you don't re-derive them.
25    pub fn descriptor(&self) -> wgpu::SamplerDescriptor<'static> {
26        match self {
27            SamplerKind::LinearRepeat => wgpu::SamplerDescriptor {
28                address_mode_u: wgpu::AddressMode::Repeat,
29                address_mode_v: wgpu::AddressMode::Repeat,
30                address_mode_w: wgpu::AddressMode::Repeat,
31                mag_filter: wgpu::FilterMode::Linear,
32                min_filter: wgpu::FilterMode::Linear,
33                mipmap_filter: wgpu::MipmapFilterMode::Linear,
34                ..Default::default()
35            },
36            SamplerKind::LinearClamp => wgpu::SamplerDescriptor {
37                address_mode_u: wgpu::AddressMode::ClampToEdge,
38                address_mode_v: wgpu::AddressMode::ClampToEdge,
39                address_mode_w: wgpu::AddressMode::ClampToEdge,
40                mag_filter: wgpu::FilterMode::Linear,
41                min_filter: wgpu::FilterMode::Linear,
42                mipmap_filter: wgpu::MipmapFilterMode::Linear,
43                ..Default::default()
44            },
45            SamplerKind::LinearClampNoMip => 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                lod_min_clamp: 0.0,
53                lod_max_clamp: 0.0,
54                ..Default::default()
55            },
56            SamplerKind::Nearest => wgpu::SamplerDescriptor {
57                mag_filter: wgpu::FilterMode::Nearest,
58                min_filter: wgpu::FilterMode::Nearest,
59                mipmap_filter: wgpu::MipmapFilterMode::Linear,
60                ..Default::default()
61            },
62            SamplerKind::NearestClampBorder => wgpu::SamplerDescriptor {
63                address_mode_u: wgpu::AddressMode::ClampToBorder,
64                address_mode_v: wgpu::AddressMode::ClampToBorder,
65                address_mode_w: wgpu::AddressMode::ClampToBorder,
66                mag_filter: wgpu::FilterMode::Nearest,
67                min_filter: wgpu::FilterMode::Nearest,
68                mipmap_filter: wgpu::MipmapFilterMode::Nearest,
69                border_color: Some(wgpu::SamplerBorderColor::OpaqueWhite),
70                ..Default::default()
71            },
72        }
73    }
74}