diff --git a/examples/scenes/src/images.rs b/examples/scenes/src/images.rs index 2086351..27b8978 100644 --- a/examples/scenes/src/images.rs +++ b/examples/scenes/src/images.rs @@ -16,38 +16,36 @@ impl ImageCache { Self::default() } - pub fn from_file(&mut self, path: impl AsRef) -> Option { + pub fn from_file(&mut self, path: impl AsRef) -> anyhow::Result { let path = path.as_ref(); if let Some(image) = self.files.get(path) { - Some(image.clone()) + Ok(image.clone()) } else { - let data = std::fs::read(path).ok()?; + let data = std::fs::read(path)?; let image = decode_image(&data)?; self.files.insert(path.to_owned(), image.clone()); - Some(image) + Ok(image) } } - pub fn from_bytes(&mut self, key: usize, bytes: &[u8]) -> Option { + pub fn from_bytes(&mut self, key: usize, bytes: &[u8]) -> anyhow::Result { if let Some(image) = self.bytes.get(&key) { - Some(image.clone()) + Ok(image.clone()) } else { let image = decode_image(bytes)?; self.bytes.insert(key, image.clone()); - Some(image) + Ok(image) } } } -fn decode_image(data: &[u8]) -> Option { +fn decode_image(data: &[u8]) -> anyhow::Result { let image = image::io::Reader::new(std::io::Cursor::new(data)) - .with_guessed_format() - .ok()? - .decode() - .ok()?; + .with_guessed_format()? + .decode()?; let width = image.width(); let height = image.height(); let data = Arc::new(image.into_rgba8().into_vec()); let blob = Blob::new(data); - Some(Image::new(blob, Format::Rgba8, width, height)) + Ok(Image::new(blob, Format::Rgba8, width, height)) } diff --git a/examples/scenes/src/test_scenes.rs b/examples/scenes/src/test_scenes.rs index 721b921..41e528b 100644 --- a/examples/scenes/src/test_scenes.rs +++ b/examples/scenes/src/test_scenes.rs @@ -101,6 +101,8 @@ fn cardioid_and_friends(sb: &mut SceneBuilder, _: &mut SceneParams) { } fn animated_text(sb: &mut SceneBuilder, params: &mut SceneParams) { + // Uses the static array address as a cache key for expedience. Real code + // should use a better strategy. let piet_logo = params .images .from_bytes(PIET_LOGO_IMAGE.as_ptr() as usize, PIET_LOGO_IMAGE) diff --git a/src/engine.rs b/src/engine.rs index 805b915..779dc4c 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -299,7 +299,7 @@ impl Engine { base_mip_level: 0, base_array_layer: 0, array_layer_count: None, - format: Some(TextureFormat::Rgba8Unorm), + format: Some(format), }); queue.write_texture( wgpu::ImageCopyTexture { @@ -311,7 +311,9 @@ impl Engine { bytes, wgpu::ImageDataLayout { offset: 0, - bytes_per_row: NonZeroU32::new(image_proxy.width * 4), + bytes_per_row: NonZeroU32::new( + image_proxy.width * format.describe().block_size as u32, + ), rows_per_image: None, }, wgpu::Extent3d { @@ -325,6 +327,7 @@ impl Engine { } Command::WriteImage(proxy, [x, y, width, height], data) => { if let Ok((texture, _)) = self.bind_map.get_or_create_image(*proxy, device) { + let format = proxy.format.to_wgpu(); queue.write_texture( wgpu::ImageCopyTexture { texture: &texture, @@ -335,7 +338,9 @@ impl Engine { &data[..], wgpu::ImageDataLayout { offset: 0, - bytes_per_row: NonZeroU32::new(*width * 4), + bytes_per_row: NonZeroU32::new( + *width * format.describe().block_size as u32, + ), rows_per_image: None, }, wgpu::Extent3d { @@ -778,7 +783,7 @@ impl BindMap { base_mip_level: 0, base_array_layer: 0, array_layer_count: None, - format: Some(TextureFormat::Rgba8Unorm), + format: Some(proxy.format.to_wgpu()), }); Ok(vacant.insert((texture, texture_view))) }