diff --git a/src/pob/api/image_handle.rs b/src/pob/api/image_handle.rs index 0562252..21c7b84 100644 --- a/src/pob/api/image_handle.rs +++ b/src/pob/api/image_handle.rs @@ -6,6 +6,7 @@ use std::{ use mlua::{Lua, Result as LuaResult, ffi}; use crate::{ + dpi::PhysicalSize, pob::Context, renderer::textures::{TextureHandle, TextureId, TextureOptions}, }; @@ -33,19 +34,16 @@ impl ImageHandle { #[inline] pub fn is_loading(&self) -> bool { match &self.texture { - Some(texture) => { - let size = texture.size(); - size[0] == 0 - } + Some(texture) => texture.is_loading(), None => true, } } #[inline] - pub fn image_size(&self) -> [usize; 2] { + pub fn image_size(&self) -> PhysicalSize { match &self.texture { - Some(texture) => texture.size(), - None => [0, 0], + Some(texture) => texture.size().unwrap_or(PhysicalSize::zero()), + None => PhysicalSize::zero(), } } } @@ -154,11 +152,11 @@ unsafe extern "C-unwind" fn image_handle_is_loading(state: *mut ffi::lua_State) unsafe extern "C-unwind" fn image_handle_image_size(state: *mut ffi::lua_State) -> c_int { let handle = unsafe { get_image_handle(state, 1) }; - let [width, height] = unsafe { (*handle).image_size() }; + let size = unsafe { (*handle).image_size() }; unsafe { - ffi::lua_pushinteger(state, width as ffi::lua_Integer); - ffi::lua_pushinteger(state, height as ffi::lua_Integer); + ffi::lua_pushinteger(state, size.width as ffi::lua_Integer); + ffi::lua_pushinteger(state, size.height as ffi::lua_Integer); } 2 diff --git a/src/renderer/image.rs b/src/renderer/image.rs index 5af82de..26c9ea1 100644 --- a/src/renderer/image.rs +++ b/src/renderer/image.rs @@ -24,6 +24,22 @@ impl MipStrategy { Self::None } } + + /// Determine mip level count of given `ImageData` after mip strategy has been applied + pub fn resolve_mip_level_count(&self, image: &ImageData) -> u32 { + match self { + Self::None => 1, + Self::Provided => image.mipmap_count.get(), + Self::GenerateOnUpload => { + let size = wgpu::Extent3d { + width: image.width, + height: image.height, + depth_or_array_layers: image.array_layers, + }; + size.max_mips(wgpu::TextureDimension::D2) + } + } + } } #[derive(Copy, Clone, Debug, PartialEq, Eq)] diff --git a/src/renderer/textures.rs b/src/renderer/textures.rs index 1df87c7..1c6e522 100644 --- a/src/renderer/textures.rs +++ b/src/renderer/textures.rs @@ -5,6 +5,7 @@ use anyhow::bail; use crate::{ color::Srgba, + dpi::PhysicalSize, renderer::image::{ImageData, ImageDelta, MipStrategy, load_image_file}, worker_pool::WorkerPool, }; @@ -25,11 +26,30 @@ impl TextureHandle { self.id } - pub fn size(&self) -> [usize; 2] { - self.tex_mngr + pub fn size(&self) -> Option> { + match self + .tex_mngr .borrow() .get_meta_data(self.id) - .map_or([0, 0], |tex| tex.size) + .expect("Texture exists because we hold a handle") + .state + { + TextureState::AsyncLoading => None, + TextureState::Loaded(shape) => Some(shape.size), + } + } + + pub fn is_loading(&self) -> bool { + match self + .tex_mngr + .borrow() + .get_meta_data(self.id) + .expect("Texture exists because we hold a handle") + .state + { + TextureState::AsyncLoading => true, + TextureState::Loaded(_) => false, + } } } @@ -61,14 +81,29 @@ impl TexturesDelta { } } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct TextureShape { + size: PhysicalSize, + format: wgpu::TextureFormat, + array_layers: u32, + mip_level_count: u32, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum TextureState { + /// Texture is being loaded asynchronously + AsyncLoading, + Loaded(TextureShape), +} + /// Metadata about an allocated texture. #[derive(Clone, Debug, PartialEq, Eq)] pub struct TextureMetaData { - pub name: String, - pub size: [usize; 2], + name: String, + options: TextureOptions, + state: TextureState, /// Texture is freed when this reaches zero retain_count: usize, - pub options: TextureOptions, } #[derive(Default)] @@ -96,9 +131,14 @@ impl TextureRegistry { id, TextureMetaData { name, - size: [image.width as usize, image.height as usize], retain_count: 1, options, + state: TextureState::Loaded(TextureShape { + size: PhysicalSize::new(image.width, image.height), + format: image.format, + array_layers: image.array_layers, + mip_level_count: mip_strategy.resolve_mip_level_count(&image), + }), }, ); @@ -118,9 +158,9 @@ impl TextureRegistry { id, TextureMetaData { name, - size: [0, 0], retain_count: 1, options, + state: TextureState::AsyncLoading, }, ); @@ -129,18 +169,45 @@ impl TextureRegistry { /// Assigns a new image to an existing texture. pub fn set(&mut self, id: TextureId, delta: ImageDelta) { - if let Some(meta_data) = self.meta_data.get_mut(&id) { - if let ImageDelta::Full { image, .. } = &delta { - meta_data.size = [image.width as usize, image.height as usize]; - // discard all old enqueued deltas since we're doing a full update - self.delta.update.retain(|(x, _)| x != &id); - } - self.delta.update.push((id, delta)); - } else { + let Some(meta_data) = self.meta_data.get_mut(&id) else { // the handle may have been dropped while the async load was in flight. // just discard the result. log::debug!("Discarding load result for freed texture {id:?}"); + return; + }; + + match &delta { + ImageDelta::Full { + image, + mip_strategy, + .. + } => { + meta_data.state = TextureState::Loaded(TextureShape { + size: PhysicalSize::new(image.width, image.height), + format: image.format, + array_layers: image.array_layers, + mip_level_count: mip_strategy.resolve_mip_level_count(image), + }); + // discard all old enqueued deltas since we're doing a full update + self.delta.update.retain(|(x, _)| x != &id); + } + ImageDelta::Partial { image, .. } => { + let TextureState::Loaded(shape) = &meta_data.state else { + panic!("partial update for texture {id:?} which hasn't finished loading!"); + }; + + assert_eq!( + shape.format, image.format, + "format of partial update must match format of existing texture {id:?}" + ); + + assert_eq!( + shape.mip_level_count, 1, + "partial updates are not supported for textures with mipmaps (texture {id:?})" + ); + } } + self.delta.update.push((id, delta)); } /// Frees an existing texture.