2023-01-15 07:06:43 +11:00
|
|
|
use std::sync::Arc;
|
2023-01-10 14:54:54 +11:00
|
|
|
use crate::error;
|
2022-12-22 14:03:38 +11:00
|
|
|
use ash::vk;
|
|
|
|
use librashader_common::{FilterMode, WrapMode};
|
2023-01-10 14:54:54 +11:00
|
|
|
use rustc_hash::FxHashMap;
|
2022-12-22 14:03:38 +11:00
|
|
|
|
|
|
|
pub struct VulkanSampler {
|
|
|
|
pub handle: vk::Sampler,
|
2023-01-15 07:06:43 +11:00
|
|
|
device: Arc<ash::Device>,
|
2022-12-22 14:03:38 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VulkanSampler {
|
2023-01-10 14:54:54 +11:00
|
|
|
pub fn new(
|
2023-01-15 07:06:43 +11:00
|
|
|
device: &Arc<ash::Device>,
|
2023-01-10 14:54:54 +11:00
|
|
|
wrap: WrapMode,
|
|
|
|
filter: FilterMode,
|
|
|
|
mipmap: FilterMode,
|
|
|
|
) -> error::Result<VulkanSampler> {
|
2022-12-22 14:03:38 +11:00
|
|
|
let create_info = vk::SamplerCreateInfo::builder()
|
|
|
|
.mip_lod_bias(0.0)
|
|
|
|
.max_anisotropy(1.0)
|
|
|
|
.compare_enable(false)
|
|
|
|
.min_lod(0.0)
|
|
|
|
.max_lod(vk::LOD_CLAMP_NONE)
|
|
|
|
.unnormalized_coordinates(false)
|
|
|
|
.border_color(vk::BorderColor::FLOAT_TRANSPARENT_BLACK)
|
|
|
|
.mag_filter(filter.into())
|
|
|
|
.min_filter(filter.into())
|
|
|
|
.mipmap_mode(mipmap.into())
|
|
|
|
.address_mode_u(wrap.into())
|
|
|
|
.address_mode_v(wrap.into())
|
|
|
|
.address_mode_w(wrap.into())
|
|
|
|
.build();
|
|
|
|
|
2023-01-10 14:54:54 +11:00
|
|
|
let sampler = unsafe { device.create_sampler(&create_info, None)? };
|
2022-12-22 14:03:38 +11:00
|
|
|
|
|
|
|
Ok(VulkanSampler {
|
|
|
|
handle: sampler,
|
2023-01-10 14:54:54 +11:00
|
|
|
device: device.clone(),
|
2022-12-22 14:03:38 +11:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for VulkanSampler {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.handle != vk::Sampler::null() {
|
|
|
|
unsafe {
|
|
|
|
self.device.destroy_sampler(self.handle, None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct SamplerSet {
|
|
|
|
// todo: may need to deal with differences in mip filter.
|
|
|
|
samplers: FxHashMap<(WrapMode, FilterMode, FilterMode), VulkanSampler>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SamplerSet {
|
|
|
|
pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> &VulkanSampler {
|
|
|
|
// eprintln!("{wrap}, {filter}, {mip}");
|
|
|
|
self.samplers.get(&(wrap, filter, mipmap)).unwrap()
|
|
|
|
}
|
|
|
|
|
2023-01-15 07:06:43 +11:00
|
|
|
pub fn new(device: &Arc<ash::Device>) -> error::Result<SamplerSet> {
|
2022-12-22 14:03:38 +11:00
|
|
|
let mut samplers = FxHashMap::default();
|
|
|
|
let wrap_modes = &[
|
|
|
|
WrapMode::ClampToBorder,
|
|
|
|
WrapMode::ClampToEdge,
|
|
|
|
WrapMode::Repeat,
|
|
|
|
WrapMode::MirroredRepeat,
|
|
|
|
];
|
|
|
|
for wrap_mode in wrap_modes {
|
|
|
|
samplers.insert(
|
|
|
|
(*wrap_mode, FilterMode::Linear, FilterMode::Linear),
|
|
|
|
VulkanSampler::new(device, *wrap_mode, FilterMode::Linear, FilterMode::Linear)?,
|
|
|
|
);
|
|
|
|
samplers.insert(
|
|
|
|
(*wrap_mode, FilterMode::Linear, FilterMode::Nearest),
|
|
|
|
VulkanSampler::new(device, *wrap_mode, FilterMode::Linear, FilterMode::Nearest)?,
|
|
|
|
);
|
|
|
|
|
|
|
|
samplers.insert(
|
|
|
|
(*wrap_mode, FilterMode::Nearest, FilterMode::Nearest),
|
2023-01-10 14:54:54 +11:00
|
|
|
VulkanSampler::new(device, *wrap_mode, FilterMode::Nearest, FilterMode::Nearest)?,
|
2022-12-22 14:03:38 +11:00
|
|
|
);
|
|
|
|
samplers.insert(
|
|
|
|
(*wrap_mode, FilterMode::Nearest, FilterMode::Linear),
|
2023-01-10 14:54:54 +11:00
|
|
|
VulkanSampler::new(device, *wrap_mode, FilterMode::Nearest, FilterMode::Linear)?,
|
2022-12-22 14:03:38 +11:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(SamplerSet { samplers })
|
|
|
|
}
|
|
|
|
}
|