librashader/librashader-runtime-gl/src/samplers.rs

64 lines
2.2 KiB
Rust
Raw Normal View History

2022-11-27 08:47:48 +11:00
use gl::types::{GLenum, GLint, GLuint};
use librashader_common::{FilterMode, WrapMode};
2022-11-30 17:38:05 +11:00
use rustc_hash::FxHashMap;
2022-11-27 08:47:48 +11:00
pub struct SamplerSet {
// todo: may need to deal with differences in mip filter.
2022-11-30 17:38:05 +11:00
samplers: FxHashMap<(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 {
*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 {
let mut samplers = FxHashMap::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);
SamplerSet::make_sampler(sampler, *wrap_mode, *filter_mode, *mip_filter);
2022-11-27 08:47:48 +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
}
}