Avoid panic when image load fails in iced_wgpu

This commit is contained in:
Héctor Ramón Jiménez 2019-11-24 12:03:12 +01:00
parent 21ec79296e
commit 422e2a6194

View file

@ -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 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::Host { image });
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,
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,
);
{
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,
});
],
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.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,
);
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,
}
}
}