Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Textures improvements #207

Merged
merged 4 commits into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ All notable changes to this project will be documented in this file.
- Draw unsupported chars with a font does not panic anymore.
- Added `WindowConfig::window_icon` and `WindowConfig::taskbar_icon` to add icons for windows os.
- Added example `egui_custom_font.rs`.
- Fix images loaded from files can set the texture format other rgba.
- Added `TextureFormat::Rgba32Float`.
- Avoid some allocations when textures are loaded.

## v0.8.0 - 28/11/2022

Expand Down
2 changes: 1 addition & 1 deletion crates/notan_glow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ impl DeviceBackend for GlowBackend {
.bind_texture(glow::TEXTURE_2D, Some(texture.texture));
self.gl.pixel_store_i32(
glow::UNPACK_ALIGNMENT,
opts.format.bytes_per_pixel() as _,
opts.format.bytes_per_pixel().min(8) as _,
);

match source {
Expand Down
5 changes: 4 additions & 1 deletion crates/notan_glow/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub(crate) unsafe fn pre_create_texture<'a>(

let texture = gl.create_texture()?;

let bytes_per_pixel = info.bytes_per_pixel() as _;
let bytes_per_pixel = info.bytes_per_pixel().min(8) as _;
gl.pixel_store_i32(glow::UNPACK_ALIGNMENT, bytes_per_pixel);

gl.bind_texture(glow::TEXTURE_2D, Some(texture));
Expand Down Expand Up @@ -197,6 +197,7 @@ pub(crate) unsafe fn create_texture(
pub(crate) fn texture_type(tf: &TextureFormat) -> u32 {
match tf {
TextureFormat::R32Float => glow::FLOAT,
TextureFormat::Rgba32Float => glow::FLOAT,
TextureFormat::R32Uint => glow::UNSIGNED_INT,
TextureFormat::R16Uint => glow::UNSIGNED_SHORT,
TextureFormat::Depth16 => glow::UNSIGNED_SHORT,
Expand All @@ -207,6 +208,7 @@ pub(crate) fn texture_type(tf: &TextureFormat) -> u32 {
pub(crate) fn texture_format(tf: &TextureFormat) -> u32 {
match tf {
TextureFormat::Rgba32 => glow::RGBA,
TextureFormat::Rgba32Float => RGBA,
TextureFormat::R8 => glow::RED,
TextureFormat::R8Uint => glow::RED_INTEGER,
TextureFormat::R16Uint => glow::RED_INTEGER,
Expand All @@ -225,6 +227,7 @@ pub(crate) fn texture_internal_format(tf: &TextureFormat) -> u32 {
TextureFormat::R32Float => glow::R32F,
TextureFormat::R32Uint => glow::R32UI,
TextureFormat::SRgba8 => glow::SRGB8_ALPHA8,
TextureFormat::Rgba32Float => glow::RGBA32F,
_ => texture_format(tf),
}
}
59 changes: 43 additions & 16 deletions crates/notan_glow/src/texture_source.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::texture::create_texture;
use crate::texture::{create_texture, TextureKey};
use crate::GlowBackend;
use notan_graphics::color::Color;
use notan_graphics::{TextureFormat, TextureInfo};
Expand All @@ -17,25 +17,52 @@ pub(crate) fn add_texture_from_image(
buffer: Vec<u8>,
mut info: TextureInfo,
) -> Result<(u64, TextureInfo), String> {
let data = image::load_from_memory(&buffer)
.map_err(|e| e.to_string())?
.to_rgba8();

let pixels = if info.premultiplied_alpha {
premultiplied_alpha(&data)
} else {
data.to_vec()
};

info.format = TextureFormat::Rgba32;
info.width = data.width() as _;
info.height = data.height() as _;

let tex = unsafe { create_texture(&backend.gl, Some(&pixels), &info)? };
let img = image::load_from_memory(&buffer).map_err(|e| e.to_string())?;
let tex = parse_image(backend, &img, &mut info)?;
let id = backend.add_inner_texture(tex, &info)?;
Ok((id, info))
}

fn parse_image(
backend: &mut GlowBackend,
img: &image::DynamicImage,
info: &mut TextureInfo,
) -> Result<TextureKey, String> {
// TODO process the loading of more texture types directly?
match info.format {
TextureFormat::Rgba32Float => {
let mut data = img.to_rgba32f();
let width = data.width() as _;
let height = data.height() as _;
if info.premultiplied_alpha {
data.pixels_mut().for_each(|rgba| {
rgba.0 = Color::from(rgba.0).to_premultiplied_alpha().into();
});
}
info.width = width;
info.height = height;
let tex =
unsafe { create_texture(&backend.gl, Some(bytemuck::cast_slice(&data)), info)? };
Ok(tex)
}
_ => {
let mut data = img.to_rgba8();
let width = data.width() as _;
let height = data.height() as _;
if info.premultiplied_alpha {
data.pixels_mut().for_each(|rgba| {
rgba.0 = Color::from(rgba.0).to_premultiplied_alpha().into();
});
}
info.width = width;
info.height = height;
let tex =
unsafe { create_texture(&backend.gl, Some(bytemuck::cast_slice(&data)), info)? };
Ok(tex)
}
}
}

pub(crate) fn add_texture_from_bytes(
backend: &mut GlowBackend,
bytes: Vec<u8>,
Expand Down
2 changes: 2 additions & 0 deletions crates/notan_graphics/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl TextureFormat {
R8 => 1,
R8Uint => 1,
R16Uint => 2,
Rgba32Float => 4 * 4,
_ => 4,
}
}
Expand Down Expand Up @@ -250,6 +251,7 @@ pub enum TextureFormat {
R32Float,
R32Uint,
Depth16,
Rgba32Float,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand Down