2024-02-15 09:54:49 +11:00
|
|
|
use librashader_common::map::FastHashMap;
|
2024-02-15 11:22:25 +11:00
|
|
|
use librashader_common::{FilterMode, WrapMode};
|
2023-12-15 00:47:33 +11:00
|
|
|
use std::sync::Arc;
|
|
|
|
use wgpu::{Sampler, SamplerBorderColor, SamplerDescriptor};
|
|
|
|
|
|
|
|
pub struct SamplerSet {
|
|
|
|
// todo: may need to deal with differences in mip filter.
|
2024-02-15 09:54:49 +11:00
|
|
|
samplers: FastHashMap<(WrapMode, FilterMode, FilterMode), Arc<Sampler>>,
|
2023-12-15 00:47:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SamplerSet {
|
|
|
|
#[inline(always)]
|
2024-02-02 10:03:21 +11:00
|
|
|
pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> Arc<Sampler> {
|
2023-12-15 00:47:33 +11:00
|
|
|
// eprintln!("{wrap}, {filter}, {mip}");
|
|
|
|
// SAFETY: the sampler set is complete for the matrix
|
|
|
|
// wrap x filter x mipmap
|
|
|
|
unsafe {
|
2024-02-06 16:29:45 +11:00
|
|
|
Arc::clone(
|
|
|
|
&self
|
|
|
|
.samplers
|
|
|
|
.get(&(wrap, filter, mipmap))
|
|
|
|
.unwrap_unchecked(),
|
|
|
|
)
|
2023-12-15 00:47:33 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-16 22:28:41 +11:00
|
|
|
pub fn new(device: &wgpu::Device) -> SamplerSet {
|
2024-02-15 09:54:49 +11:00
|
|
|
let mut samplers = FastHashMap::default();
|
2023-12-15 00:47:33 +11:00
|
|
|
let wrap_modes = &[
|
|
|
|
WrapMode::ClampToBorder,
|
|
|
|
WrapMode::ClampToEdge,
|
|
|
|
WrapMode::Repeat,
|
|
|
|
WrapMode::MirroredRepeat,
|
|
|
|
];
|
|
|
|
for wrap_mode in wrap_modes {
|
|
|
|
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
|
|
|
|
for mipmap_filter in &[FilterMode::Linear, FilterMode::Nearest] {
|
|
|
|
samplers.insert(
|
|
|
|
(*wrap_mode, *filter_mode, *mipmap_filter),
|
2024-02-02 10:03:21 +11:00
|
|
|
Arc::new(device.create_sampler(&SamplerDescriptor {
|
2023-12-15 00:47:33 +11:00
|
|
|
label: None,
|
2023-12-16 22:28:41 +11:00
|
|
|
address_mode_u: (*wrap_mode).into(),
|
|
|
|
address_mode_v: (*wrap_mode).into(),
|
|
|
|
address_mode_w: (*wrap_mode).into(),
|
|
|
|
mag_filter: (*filter_mode).into(),
|
|
|
|
min_filter: (*filter_mode).into(),
|
|
|
|
mipmap_filter: (*mipmap_filter).into(),
|
2023-12-15 00:47:33 +11:00
|
|
|
lod_min_clamp: 0.0,
|
|
|
|
lod_max_clamp: 1000.0,
|
|
|
|
compare: None,
|
|
|
|
anisotropy_clamp: 1,
|
|
|
|
border_color: Some(SamplerBorderColor::TransparentBlack),
|
2024-02-06 16:29:45 +11:00
|
|
|
})),
|
|
|
|
);
|
2023-12-15 00:47:33 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assert all samplers were created.
|
|
|
|
assert_eq!(samplers.len(), wrap_modes.len() * 2 * 2);
|
2023-12-16 22:28:41 +11:00
|
|
|
SamplerSet { samplers }
|
2023-12-15 00:47:33 +11:00
|
|
|
}
|
|
|
|
}
|