Avoid panic when image load fails in iced_wgpu
This commit is contained in:
parent
21ec79296e
commit
422e2a6194
1 changed files with 70 additions and 62 deletions
|
|
@ -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,13 +243,13 @@ 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);
|
||||
|
||||
.upload(device, encoder, &self.texture_layout)
|
||||
{
|
||||
let instance_buffer = device
|
||||
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
||||
.fill_from_slice(&[Instance {
|
||||
|
|
@ -263,8 +266,8 @@ impl Pipeline {
|
|||
);
|
||||
|
||||
{
|
||||
let mut render_pass =
|
||||
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
let mut render_pass = encoder.begin_render_pass(
|
||||
&wgpu::RenderPassDescriptor {
|
||||
color_attachments: &[
|
||||
wgpu::RenderPassColorAttachmentDescriptor {
|
||||
attachment: target,
|
||||
|
|
@ -280,7 +283,8 @@ impl Pipeline {
|
|||
},
|
||||
],
|
||||
depth_stencil_attachment: None,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
render_pass.set_pipeline(&self.pipeline);
|
||||
render_pass.set_bind_group(0, &self.constants, &[]);
|
||||
|
|
@ -305,6 +309,7 @@ impl Pipeline {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue