summaryrefslogtreecommitdiffstats
path: root/wgpu/src/image.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-11-24 12:03:12 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-11-24 12:03:12 +0100
commit422e2a619415fdd9ed7fb12db26f6e28603e4590 (patch)
treeb2a416cc7f79c9d4fbfa61061ffca065af51a375 /wgpu/src/image.rs
parent21ec79296e795fa884c10b132ff2957f915f2c02 (diff)
downloadiced-422e2a619415fdd9ed7fb12db26f6e28603e4590.tar.gz
iced-422e2a619415fdd9ed7fb12db26f6e28603e4590.tar.bz2
iced-422e2a619415fdd9ed7fb12db26f6e28603e4590.zip
Avoid panic when image load fails in `iced_wgpu`
Diffstat (limited to 'wgpu/src/image.rs')
-rw-r--r--wgpu/src/image.rs134
1 files changed, 71 insertions, 63 deletions
diff --git a/wgpu/src/image.rs b/wgpu/src/image.rs
index 96c9ab6d..f7b4a172 100644
--- a/wgpu/src/image.rs
+++ b/wgpu/src/image.rs
@@ -203,12 +203,15 @@ impl Pipeline {
fn load(&self, path: &str) {
if !self.cache.borrow().contains_key(path) {
- let image = image::open(path).expect("Load image").to_bgra();
-
- let _ = self
- .cache
- .borrow_mut()
- .insert(path.to_string(), Memory::Host { image });
+ let memory = if let Ok(image) = image::open(path) {
+ Memory::Host {
+ image: image.to_bgra(),
+ }
+ } else {
+ Memory::NotFound
+ };
+
+ let _ = self.cache.borrow_mut().insert(path.to_string(), memory);
}
}
@@ -240,68 +243,70 @@ impl Pipeline {
for image in instances {
self.load(&image.path);
- let texture = self
+ if let Some(texture) = self
.cache
.borrow_mut()
.get_mut(&image.path)
.unwrap()
- .upload(device, encoder, &self.texture_layout);
-
- let instance_buffer = device
- .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
- .fill_from_slice(&[Instance {
- _position: image.position,
- _scale: image.scale,
- }]);
-
- encoder.copy_buffer_to_buffer(
- &instance_buffer,
- 0,
- &self.instances,
- 0,
- mem::size_of::<Image>() as u64,
- );
-
+ .upload(device, encoder, &self.texture_layout)
{
- let mut render_pass =
- encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
- color_attachments: &[
- wgpu::RenderPassColorAttachmentDescriptor {
- attachment: target,
- resolve_target: None,
- load_op: wgpu::LoadOp::Load,
- store_op: wgpu::StoreOp::Store,
- clear_color: wgpu::Color {
- r: 0.0,
- g: 0.0,
- b: 0.0,
- a: 0.0,
- },
- },
- ],
- depth_stencil_attachment: None,
- });
-
- render_pass.set_pipeline(&self.pipeline);
- render_pass.set_bind_group(0, &self.constants, &[]);
- render_pass.set_bind_group(1, &texture, &[]);
- render_pass.set_index_buffer(&self.indices, 0);
- render_pass.set_vertex_buffers(
+ let instance_buffer = device
+ .create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
+ .fill_from_slice(&[Instance {
+ _position: image.position,
+ _scale: image.scale,
+ }]);
+
+ encoder.copy_buffer_to_buffer(
+ &instance_buffer,
0,
- &[(&self.vertices, 0), (&self.instances, 0)],
- );
- render_pass.set_scissor_rect(
- bounds.x,
- bounds.y,
- bounds.width,
- bounds.height,
- );
-
- render_pass.draw_indexed(
- 0..QUAD_INDICES.len() as u32,
+ &self.instances,
0,
- 0..1 as u32,
+ mem::size_of::<Image>() as u64,
);
+
+ {
+ let mut render_pass = encoder.begin_render_pass(
+ &wgpu::RenderPassDescriptor {
+ color_attachments: &[
+ wgpu::RenderPassColorAttachmentDescriptor {
+ attachment: target,
+ resolve_target: None,
+ load_op: wgpu::LoadOp::Load,
+ store_op: wgpu::StoreOp::Store,
+ clear_color: wgpu::Color {
+ r: 0.0,
+ g: 0.0,
+ b: 0.0,
+ a: 0.0,
+ },
+ },
+ ],
+ depth_stencil_attachment: None,
+ },
+ );
+
+ render_pass.set_pipeline(&self.pipeline);
+ render_pass.set_bind_group(0, &self.constants, &[]);
+ render_pass.set_bind_group(1, &texture, &[]);
+ render_pass.set_index_buffer(&self.indices, 0);
+ render_pass.set_vertex_buffers(
+ 0,
+ &[(&self.vertices, 0), (&self.instances, 0)],
+ );
+ render_pass.set_scissor_rect(
+ bounds.x,
+ bounds.y,
+ bounds.width,
+ bounds.height,
+ );
+
+ render_pass.draw_indexed(
+ 0..QUAD_INDICES.len() as u32,
+ 0,
+ 0..1 as u32,
+ );
+ }
}
}
}
@@ -317,6 +322,7 @@ enum Memory {
width: u32,
height: u32,
},
+ NotFound,
}
impl Memory {
@@ -324,6 +330,7 @@ impl Memory {
match self {
Memory::Host { image } => image.dimensions(),
Memory::Device { width, height, .. } => (*width, *height),
+ Memory::NotFound => (1, 1),
}
}
@@ -332,7 +339,7 @@ impl Memory {
device: &wgpu::Device,
encoder: &mut wgpu::CommandEncoder,
texture_layout: &wgpu::BindGroupLayout,
- ) -> Rc<wgpu::BindGroup> {
+ ) -> Option<Rc<wgpu::BindGroup>> {
match self {
Memory::Host { image } => {
let (width, height) = image.dimensions();
@@ -402,9 +409,10 @@ impl Memory {
height,
};
- bind_group
+ Some(bind_group)
}
- Memory::Device { bind_group, .. } => bind_group.clone(),
+ Memory::Device { bind_group, .. } => Some(bind_group.clone()),
+ Memory::NotFound => None,
}
}
}