2022-11-30 01:38:05 -05:00
|
|
|
use crate::error::Result;
|
|
|
|
use crate::framebuffer::GLImage;
|
|
|
|
use crate::gl::LoadLut;
|
2023-01-15 03:01:23 -05:00
|
|
|
use crate::texture::InputTexture;
|
2022-11-29 23:56:10 -05:00
|
|
|
use gl::types::{GLsizei, GLuint};
|
2024-02-14 19:22:25 -05:00
|
|
|
use librashader_common::map::FastHashMap;
|
2022-11-29 23:56:10 -05:00
|
|
|
use librashader_presets::TextureConfig;
|
2023-02-14 02:56:06 -05:00
|
|
|
use librashader_runtime::image::{Image, ImageError, UVDirection};
|
2022-12-21 21:13:35 -05:00
|
|
|
use librashader_runtime::scaling::MipmapSize;
|
2023-02-14 02:56:06 -05:00
|
|
|
use rayon::prelude::*;
|
2022-11-29 23:56:10 -05:00
|
|
|
|
|
|
|
pub struct Gl46LutLoad;
|
|
|
|
impl LoadLut for Gl46LutLoad {
|
2024-02-14 17:54:49 -05:00
|
|
|
fn load_luts(textures: &[TextureConfig]) -> Result<FastHashMap<usize, InputTexture>> {
|
|
|
|
let mut luts = FastHashMap::default();
|
2022-11-29 23:56:10 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:36:50 -05:00
|
|
|
let images = textures
|
|
|
|
.par_iter()
|
2024-03-03 02:16:45 -05:00
|
|
|
.map(|texture| Image::load(&texture.path, UVDirection::TopLeft))
|
2023-02-14 02:56:06 -05:00
|
|
|
.collect::<std::result::Result<Vec<Image>, ImageError>>()?;
|
|
|
|
|
|
|
|
for (index, (texture, image)) in textures.iter().zip(images).enumerate() {
|
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,
|
2023-01-15 03:01:23 -05:00
|
|
|
InputTexture {
|
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,
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|