librashader/librashader-runtime-gl/src/gl/gl46/lut_load.rs

89 lines
2.7 KiB
Rust
Raw Normal View History

2022-11-30 01:38:05 -05:00
use crate::error::Result;
use crate::framebuffer::GLImage;
use crate::gl::LoadLut;
use crate::texture::Texture;
2022-11-29 23:56:10 -05:00
use gl::types::{GLsizei, GLuint};
use librashader_common::Size;
use librashader_presets::TextureConfig;
2022-12-21 21:39:31 -05:00
use librashader_runtime::image::{Image, UVDirection};
2022-12-21 21:13:35 -05:00
use librashader_runtime::scaling::MipmapSize;
2022-12-21 21:39:31 -05:00
use rustc_hash::FxHashMap;
2022-11-29 23:56:10 -05:00
pub struct Gl46LutLoad;
impl LoadLut for Gl46LutLoad {
fn load_luts(textures: &[TextureConfig]) -> Result<FxHashMap<usize, Texture>> {
let mut luts = FxHashMap::default();
let pixel_unpack = unsafe {
let mut binding = 0;
gl::GetIntegerv(gl::PIXEL_UNPACK_BUFFER_BINDING, &mut binding);
binding
};
unsafe {
gl::BindBuffer(gl::PIXEL_UNPACK_BUFFER, 0);
}
for (index, texture) in textures.iter().enumerate() {
2022-12-21 21:13:35 -05:00
let image: Image = Image::load(&texture.path, UVDirection::BottomLeft)?;
2022-11-29 23:56:10 -05:00
let levels = if texture.mipmap {
2022-12-21 21:13:35 -05:00
image.size.calculate_miplevels()
2022-11-29 23:56:10 -05:00
} else {
1u32
};
let mut handle = 0;
unsafe {
2022-11-30 01:38:05 -05:00
gl::CreateTextures(gl::TEXTURE_2D, 1, &mut handle);
2022-11-29 23:56:10 -05:00
gl::TextureStorage2D(
handle,
levels as GLsizei,
gl::RGBA8,
image.size.width as GLsizei,
image.size.height as GLsizei,
);
gl::PixelStorei(gl::UNPACK_ROW_LENGTH, 0);
gl::PixelStorei(gl::UNPACK_ALIGNMENT, 4);
gl::TextureSubImage2D(
handle,
2022-11-30 01:38:05 -05:00
0,
0,
0,
2022-11-29 23:56:10 -05:00
image.size.width as GLsizei,
image.size.height as GLsizei,
gl::RGBA,
gl::UNSIGNED_BYTE,
image.bytes.as_ptr().cast(),
);
let mipmap = levels > 1;
if mipmap {
gl::GenerateTextureMipmap(handle);
}
}
luts.insert(
index,
Texture {
2022-11-30 00:39:42 -05:00
image: GLImage {
2022-11-29 23:56:10 -05:00
handle,
format: gl::RGBA8,
size: image.size,
padded_size: Size::default(),
},
filter: texture.filter_mode,
mip_filter: texture.filter_mode,
wrap_mode: texture.wrap_mode,
},
);
}
unsafe {
gl::BindBuffer(gl::PIXEL_UNPACK_BUFFER, pixel_unpack as GLuint);
};
Ok(luts)
}
2022-11-30 01:38:05 -05:00
}