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

98 lines
3.3 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 {
2022-12-22 14:03:38 +11:00
pub fn get(&self, wrap: WrapMode, filter: FilterMode, mipmap: FilterMode) -> GLuint {
2022-11-27 08:47:48 +11:00
// eprintln!("{wrap}, {filter}, {mip}");
2022-12-22 14:03:38 +11:00
*self.samplers.get(&(wrap, filter, mipmap)).unwrap()
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 {
unsafe {
let mut linear_linear = 0;
let mut linear_nearest = 0;
2022-11-27 09:11:26 +11:00
let mut nearest_nearest = 0;
2022-11-27 08:47:48 +11:00
let mut nearest_linear = 0;
gl::GenSamplers(1, &mut linear_linear);
gl::GenSamplers(1, &mut linear_nearest);
gl::GenSamplers(1, &mut nearest_linear);
gl::GenSamplers(1, &mut nearest_nearest);
2022-11-30 17:38:05 +11:00
SamplerSet::make_sampler(
linear_linear,
*wrap_mode,
FilterMode::Linear,
FilterMode::Linear,
);
SamplerSet::make_sampler(
linear_nearest,
*wrap_mode,
FilterMode::Linear,
FilterMode::Nearest,
);
SamplerSet::make_sampler(
nearest_linear,
*wrap_mode,
FilterMode::Nearest,
FilterMode::Linear,
);
SamplerSet::make_sampler(
nearest_nearest,
*wrap_mode,
FilterMode::Nearest,
FilterMode::Nearest,
);
2022-11-27 09:11:26 +11:00
2022-11-30 17:38:05 +11:00
samplers.insert(
(*wrap_mode, FilterMode::Linear, FilterMode::Linear),
linear_linear,
);
samplers.insert(
(*wrap_mode, FilterMode::Linear, FilterMode::Nearest),
linear_nearest,
);
2022-11-27 08:47:48 +11:00
2022-11-30 17:38:05 +11:00
samplers.insert(
(*wrap_mode, FilterMode::Nearest, FilterMode::Nearest),
nearest_nearest,
);
samplers.insert(
(*wrap_mode, FilterMode::Nearest, FilterMode::Linear),
nearest_linear,
);
2022-11-27 08:47:48 +11:00
}
}
2022-11-30 17:38:05 +11:00
SamplerSet { samplers }
2022-11-27 08:47:48 +11:00
}
}