mirror of
https://github.com/italicsjenga/vello.git
synced 2025-01-10 12:41:30 +11:00
Address initial review feedback
* Add comment about naughty ptr2int cast * Change Option to anyhow::Result in test scene ImageCache * Replace magic constant with pixel stride from selected format
This commit is contained in:
parent
d12b711fe1
commit
37f05b853c
|
@ -16,38 +16,36 @@ impl ImageCache {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_file(&mut self, path: impl AsRef<Path>) -> Option<Image> {
|
pub fn from_file(&mut self, path: impl AsRef<Path>) -> anyhow::Result<Image> {
|
||||||
let path = path.as_ref();
|
let path = path.as_ref();
|
||||||
if let Some(image) = self.files.get(path) {
|
if let Some(image) = self.files.get(path) {
|
||||||
Some(image.clone())
|
Ok(image.clone())
|
||||||
} else {
|
} else {
|
||||||
let data = std::fs::read(path).ok()?;
|
let data = std::fs::read(path)?;
|
||||||
let image = decode_image(&data)?;
|
let image = decode_image(&data)?;
|
||||||
self.files.insert(path.to_owned(), image.clone());
|
self.files.insert(path.to_owned(), image.clone());
|
||||||
Some(image)
|
Ok(image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_bytes(&mut self, key: usize, bytes: &[u8]) -> Option<Image> {
|
pub fn from_bytes(&mut self, key: usize, bytes: &[u8]) -> anyhow::Result<Image> {
|
||||||
if let Some(image) = self.bytes.get(&key) {
|
if let Some(image) = self.bytes.get(&key) {
|
||||||
Some(image.clone())
|
Ok(image.clone())
|
||||||
} else {
|
} else {
|
||||||
let image = decode_image(bytes)?;
|
let image = decode_image(bytes)?;
|
||||||
self.bytes.insert(key, image.clone());
|
self.bytes.insert(key, image.clone());
|
||||||
Some(image)
|
Ok(image)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decode_image(data: &[u8]) -> Option<Image> {
|
fn decode_image(data: &[u8]) -> anyhow::Result<Image> {
|
||||||
let image = image::io::Reader::new(std::io::Cursor::new(data))
|
let image = image::io::Reader::new(std::io::Cursor::new(data))
|
||||||
.with_guessed_format()
|
.with_guessed_format()?
|
||||||
.ok()?
|
.decode()?;
|
||||||
.decode()
|
|
||||||
.ok()?;
|
|
||||||
let width = image.width();
|
let width = image.width();
|
||||||
let height = image.height();
|
let height = image.height();
|
||||||
let data = Arc::new(image.into_rgba8().into_vec());
|
let data = Arc::new(image.into_rgba8().into_vec());
|
||||||
let blob = Blob::new(data);
|
let blob = Blob::new(data);
|
||||||
Some(Image::new(blob, Format::Rgba8, width, height))
|
Ok(Image::new(blob, Format::Rgba8, width, height))
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,6 +101,8 @@ fn cardioid_and_friends(sb: &mut SceneBuilder, _: &mut SceneParams) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn animated_text(sb: &mut SceneBuilder, params: &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
|
let piet_logo = params
|
||||||
.images
|
.images
|
||||||
.from_bytes(PIET_LOGO_IMAGE.as_ptr() as usize, PIET_LOGO_IMAGE)
|
.from_bytes(PIET_LOGO_IMAGE.as_ptr() as usize, PIET_LOGO_IMAGE)
|
||||||
|
|
|
@ -299,7 +299,7 @@ impl Engine {
|
||||||
base_mip_level: 0,
|
base_mip_level: 0,
|
||||||
base_array_layer: 0,
|
base_array_layer: 0,
|
||||||
array_layer_count: None,
|
array_layer_count: None,
|
||||||
format: Some(TextureFormat::Rgba8Unorm),
|
format: Some(format),
|
||||||
});
|
});
|
||||||
queue.write_texture(
|
queue.write_texture(
|
||||||
wgpu::ImageCopyTexture {
|
wgpu::ImageCopyTexture {
|
||||||
|
@ -311,7 +311,9 @@ impl Engine {
|
||||||
bytes,
|
bytes,
|
||||||
wgpu::ImageDataLayout {
|
wgpu::ImageDataLayout {
|
||||||
offset: 0,
|
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,
|
rows_per_image: None,
|
||||||
},
|
},
|
||||||
wgpu::Extent3d {
|
wgpu::Extent3d {
|
||||||
|
@ -325,6 +327,7 @@ impl Engine {
|
||||||
}
|
}
|
||||||
Command::WriteImage(proxy, [x, y, width, height], data) => {
|
Command::WriteImage(proxy, [x, y, width, height], data) => {
|
||||||
if let Ok((texture, _)) = self.bind_map.get_or_create_image(*proxy, device) {
|
if let Ok((texture, _)) = self.bind_map.get_or_create_image(*proxy, device) {
|
||||||
|
let format = proxy.format.to_wgpu();
|
||||||
queue.write_texture(
|
queue.write_texture(
|
||||||
wgpu::ImageCopyTexture {
|
wgpu::ImageCopyTexture {
|
||||||
texture: &texture,
|
texture: &texture,
|
||||||
|
@ -335,7 +338,9 @@ impl Engine {
|
||||||
&data[..],
|
&data[..],
|
||||||
wgpu::ImageDataLayout {
|
wgpu::ImageDataLayout {
|
||||||
offset: 0,
|
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,
|
rows_per_image: None,
|
||||||
},
|
},
|
||||||
wgpu::Extent3d {
|
wgpu::Extent3d {
|
||||||
|
@ -778,7 +783,7 @@ impl BindMap {
|
||||||
base_mip_level: 0,
|
base_mip_level: 0,
|
||||||
base_array_layer: 0,
|
base_array_layer: 0,
|
||||||
array_layer_count: None,
|
array_layer_count: None,
|
||||||
format: Some(TextureFormat::Rgba8Unorm),
|
format: Some(proxy.format.to_wgpu()),
|
||||||
});
|
});
|
||||||
Ok(vacant.insert((texture, texture_view)))
|
Ok(vacant.insert((texture, texture_view)))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue