2022-11-27 08:47:48 +11:00
|
|
|
use gl::types::{GLenum, GLint, GLuint};
|
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};
|
2022-11-27 08:47:48 +11:00
|
|
|
|
|
|
|
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), GLuint>,
|
2022-11-27 08:47:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SamplerSet {
|
2023-02-17 10:19:38 +11:00
|
|
|
#[inline(always)]
|
2022-12-22 14:03:38 +11:00
|
|
|
pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> GLuint {
|
2023-02-17 10:19:38 +11:00
|
|
|
// SAFETY: the sampler set is complete for the matrix
|
|
|
|
// wrap x filter x mipmap
|
|
|
|
unsafe {
|
2023-02-17 13:16:42 +11:00
|
|
|
*self
|
|
|
|
.samplers
|
|
|
|
.get(&(wrap, filter, mipmap))
|
|
|
|
.unwrap_unchecked()
|
2023-02-17 10:19:38 +11:00
|
|
|
}
|
2022-11-27 08:47:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_sampler(sampler: GLuint, wrap: WrapMode, filter: FilterMode, mip: FilterMode) {
|
|
|
|
unsafe {
|
2022-11-30 17:38:05 +11:00
|
|
|
gl::SamplerParameteri(sampler, gl::TEXTURE_WRAP_S, GLenum::from(wrap) as GLint);
|
|
|
|
gl::SamplerParameteri(sampler, gl::TEXTURE_WRAP_T, GLenum::from(wrap) as GLint);
|
2022-11-27 08:47:48 +11:00
|
|
|
gl::SamplerParameteri(
|
|
|
|
sampler,
|
|
|
|
gl::TEXTURE_MAG_FILTER,
|
|
|
|
GLenum::from(filter) as GLint,
|
|
|
|
);
|
2022-11-27 09:11:26 +11:00
|
|
|
|
2022-11-30 17:38:05 +11:00
|
|
|
gl::SamplerParameteri(sampler, gl::TEXTURE_MIN_FILTER, filter.gl_mip(mip) as GLint);
|
2022-11-27 08:47:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new() -> SamplerSet {
|
2024-02-15 09:54:49 +11:00
|
|
|
let mut samplers = FastHashMap::default();
|
2022-11-30 17:38:05 +11:00
|
|
|
let wrap_modes = &[
|
|
|
|
WrapMode::ClampToBorder,
|
|
|
|
WrapMode::ClampToEdge,
|
|
|
|
WrapMode::Repeat,
|
|
|
|
WrapMode::MirroredRepeat,
|
|
|
|
];
|
2022-11-27 08:47:48 +11:00
|
|
|
for wrap_mode in wrap_modes {
|
2023-02-17 10:19:38 +11:00
|
|
|
for filter_mode in &[FilterMode::Linear, FilterMode::Nearest] {
|
|
|
|
for mip_filter in &[FilterMode::Linear, FilterMode::Nearest] {
|
|
|
|
let mut sampler = 0;
|
|
|
|
unsafe {
|
|
|
|
gl::GenSamplers(1, &mut sampler);
|
2023-02-17 13:16:42 +11:00
|
|
|
SamplerSet::make_sampler(sampler, *wrap_mode, *filter_mode, *mip_filter);
|
2022-11-27 08:47:48 +11:00
|
|
|
|
2023-02-17 13:16:42 +11:00
|
|
|
samplers.insert((*wrap_mode, *filter_mode, *mip_filter), sampler);
|
2023-02-17 10:19:38 +11:00
|
|
|
}
|
|
|
|
}
|
2022-11-27 08:47:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 10:19:38 +11:00
|
|
|
// assert all samplers were created.
|
|
|
|
assert_eq!(samplers.len(), wrap_modes.len() * 2 * 2);
|
2022-11-30 17:38:05 +11:00
|
|
|
SamplerSet { samplers }
|
2022-11-27 08:47:48 +11:00
|
|
|
}
|
|
|
|
}
|