diff options
| author | HampusM <hampus@hampusmat.com> | 2026-08-26 18:27:01 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-08-26 18:27:01 +0200 |
| commit | 8c3249a7bc50960e7803f4c6c733256ef126cb39 (patch) | |
| tree | 82423536d827d81f0b26c2747f408bb7427b6469 /engine | |
| parent | 7abe3894137f55aaa7aa0e0c491795342b6124bc (diff) | |
refactor(engine): make UpdateTexture rendering command not take Image
Diffstat (limited to 'engine')
| -rw-r--r-- | engine/src/rendering.rs | 61 | ||||
| -rw-r--r-- | engine/src/rendering/backend/opengl.rs | 76 | ||||
| -rw-r--r-- | engine/src/ui/dear_imgui.rs | 32 |
3 files changed, 122 insertions, 47 deletions
diff --git a/engine/src/rendering.rs b/engine/src/rendering.rs index 572f045..7a46b29 100644 --- a/engine/src/rendering.rs +++ b/engine/src/rendering.rs @@ -19,7 +19,6 @@ use crate::ecs::system::initializable::Initializable; use crate::ecs::system::observer::Observe; use crate::ecs::system::Into; use crate::ecs::{declare_entity, pair, Component, Query, Sole}; -use crate::image::Image; use crate::mesh::Mesh; use crate::rendering::blending::Config as BlendingConfig; use crate::rendering::object::{Id as ObjectId, Store as ObjectStore}; @@ -219,12 +218,9 @@ pub enum Command UpdateTexture { obj_id: ObjectId, - image: Image, + pixels: Box<[u8]>, + pixel_data_format: TexturePixelDataFormat, update: TextureUpdate, - - /// Specifies texel index offsets in the x and y directions within the texture - /// pixels. - offset: Vec2<u32>, }, RemoveTexture(ObjectId), CreateMesh @@ -406,9 +402,22 @@ bitflags! { #[non_exhaustive] pub enum TextureUpdate { - Texture2D, + Texture2D + { + size: Dimens<u32>, + + /// Specifies texel index offsets in the x and y directions within the texture + /// pixels. + offset: Vec2<u32>, + }, CubeMap { + size: Dimens<u32>, + + /// Specifies texel index offsets in the x and y directions within the texture + /// pixels. + offset: Vec2<u32>, + face: CubeMapFace, }, } @@ -546,3 +555,41 @@ struct ActiveDrawProperties { pub draw_properties: DrawProperties, } + +#[derive(Debug, Clone, Copy)] +#[non_exhaustive] +pub enum RgbTextureDataType +{ + UnsignedByte, +} + +#[derive(Debug, Clone, Copy)] +#[non_exhaustive] +pub enum RgbaTextureDataType +{ + UnsignedByte, +} + +#[derive(Debug, Clone, Copy)] +#[non_exhaustive] +pub enum SrgbTextureDataType +{ + UnsignedByte, +} + +#[derive(Debug, Clone, Copy)] +#[non_exhaustive] +pub enum SrgbaTextureDataType +{ + UnsignedByte, +} + +#[derive(Debug, Clone, Copy)] +#[non_exhaustive] +pub enum TexturePixelDataFormat +{ + Rgb(RgbTextureDataType), + Rgba(RgbaTextureDataType), + Srgb(SrgbTextureDataType), + Srgba(SrgbaTextureDataType), +} diff --git a/engine/src/rendering/backend/opengl.rs b/engine/src/rendering/backend/opengl.rs index 629c0ba..f40cb20 100644 --- a/engine/src/rendering/backend/opengl.rs +++ b/engine/src/rendering/backend/opengl.rs @@ -116,6 +116,7 @@ use crate::rendering::{ Surface, SurfaceId, TargetWindow, + TexturePixelDataFormat, TextureUpdate, POST_RENDER_PHASE, RENDER_PHASE, @@ -795,14 +796,19 @@ fn handle_commands( tracing::error!("Failed to create texture object: {err}"); } } - Command::UpdateTexture { obj_id, image, update, offset } => { + Command::UpdateTexture { + obj_id, + pixels, + pixel_data_format, + update, + } => { if let Err(err) = update_texture_object( gl_context, object_store, obj_id, - image, + pixels, + pixel_data_format, update, - offset, ) { tracing::error!("Failed to update texture object: {err}"); } @@ -1362,9 +1368,9 @@ fn update_texture_object( curr_gl_ctx: &MaybeCurrentContextWithFns, object_store: &mut ObjectStore, texture_object_id: ObjectId, - texture_image: Image, + pixels: Box<[u8]>, + pixel_data_format: TexturePixelDataFormat, update: TextureUpdate, - offset: Vec2<u32>, ) -> Result<(), GlTextureError> { let Some(texture_obj) = object_store.get_obj(&texture_object_id) else { @@ -1386,22 +1392,17 @@ fn update_texture_object( let gl_texture = GlTexture::from_raw(texture_obj.as_raw()); match update { - TextureUpdate::Texture2D => { + TextureUpdate::Texture2D { size, offset } => { gl_texture.store_image_2d( curr_gl_ctx, 0, offset.into(), - texture_image.dimensions().into(), - create_pixel_data_format(ImageDataProperties { - color_type: texture_image.color_type(), - is_srgb: texture_image.color_space_is_srgb(), - }), - texture_image.as_bytes(), + size.into(), + tex_pixel_data_format_into_gl(pixel_data_format), + &pixels, )?; } - TextureUpdate::CubeMap { face } => { - let image_dimens = texture_image.dimensions(); - + TextureUpdate::CubeMap { size, offset, face } => { gl_texture.store_image_3d( curr_gl_ctx, 0, @@ -1411,12 +1412,9 @@ fn update_texture_object( z: cube_map_texture_face_to_gl(face) as u32, } .into(), - [image_dimens.width, image_dimens.height, 1], - create_pixel_data_format(ImageDataProperties { - color_type: texture_image.color_type(), - is_srgb: texture_image.color_space_is_srgb(), - }), - texture_image.as_bytes(), + [size.width, size.height, 1], + tex_pixel_data_format_into_gl(pixel_data_format), + &pixels, )?; } } @@ -1462,6 +1460,42 @@ struct ImageDataProperties is_srgb: bool, } +fn tex_pixel_data_format_into_gl( + tex_pixel_data_format: TexturePixelDataFormat, +) -> GlTexturePixelDataFormat +{ + match tex_pixel_data_format { + TexturePixelDataFormat::Rgb(data_type) => { + GlTexturePixelDataFormat::Rgb(match data_type { + crate::rendering::RgbTextureDataType::UnsignedByte => { + opengl_bindings::texture::RgbDataType::UnsignedByte + } + }) + } + TexturePixelDataFormat::Srgb(data_type) => { + GlTexturePixelDataFormat::Srgb(match data_type { + crate::rendering::SrgbTextureDataType::UnsignedByte => { + opengl_bindings::texture::SrgbDataType::UnsignedByte + } + }) + } + TexturePixelDataFormat::Rgba(data_type) => { + GlTexturePixelDataFormat::Rgba(match data_type { + crate::rendering::RgbaTextureDataType::UnsignedByte => { + opengl_bindings::texture::RgbaDataType::UnsignedByte + } + }) + } + TexturePixelDataFormat::Srgba(data_type) => { + GlTexturePixelDataFormat::Srgba(match data_type { + crate::rendering::SrgbaTextureDataType::UnsignedByte => { + opengl_bindings::texture::SrgbaDataType::UnsignedByte + } + }) + } + } +} + fn create_pixel_data_format( image_data_props: ImageDataProperties, ) -> GlTexturePixelDataFormat diff --git a/engine/src/ui/dear_imgui.rs b/engine/src/ui/dear_imgui.rs index fbe4ed8..6bf075b 100644 --- a/engine/src/ui/dear_imgui.rs +++ b/engine/src/ui/dear_imgui.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::hint::cold_path; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::LazyLock; @@ -69,6 +68,7 @@ use crate::rendering::{ MeshUsage, RenderPass, RenderPasses, + RgbaTextureDataType, ScissorBox, Surface, SurfaceId, @@ -610,27 +610,21 @@ fn add_drawing_render_pass( render_pass.commands.reserve(rects.len()); for rect in rects { - let Ok(rect_image) = - Image::from_pixels(ImagePixelBuffer::<Rgba<u8>, _>::new( - rect.data.as_slice(), - Dimens::<u32> { + render_pass.commands.push(RenderingCommand::UpdateTexture { + obj_id: *texture_object_id, + pixels: rect.data.clone().into_boxed_slice(), + pixel_data_format: crate::rendering::TexturePixelDataFormat::Rgba( + RgbaTextureDataType::UnsignedByte, + ), + update: TextureUpdate::Texture2D { + size: Dimens::<u32> { width: rect.rect.w.into(), height: rect.rect.h.into(), }, - )) - else { - cold_path(); - tracing::error!("Texture update rect size is incorrect"); - continue; - }; - - render_pass.commands.push(RenderingCommand::UpdateTexture { - obj_id: *texture_object_id, - image: rect_image, - update: TextureUpdate::Texture2D, - offset: Vec2 { - x: rect.rect.x as u32, - y: rect.rect.y as u32, + offset: Vec2 { + x: rect.rect.x as u32, + y: rect.rect.y as u32, + }, }, }); } |
