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) {
|
fn load(&self, path: &str) {
|
||||||
if !self.cache.borrow().contains_key(path) {
|
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
|
let _ = self.cache.borrow_mut().insert(path.to_string(), memory);
|
||||||
.cache
|
|
||||||
.borrow_mut()
|
|
||||||
.insert(path.to_string(), Memory::Host { image });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -240,13 +243,13 @@ impl Pipeline {
|
||||||
for image in instances {
|
for image in instances {
|
||||||
self.load(&image.path);
|
self.load(&image.path);
|
||||||
|
|
||||||
let texture = self
|
if let Some(texture) = self
|
||||||
.cache
|
.cache
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
.get_mut(&image.path)
|
.get_mut(&image.path)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.upload(device, encoder, &self.texture_layout);
|
.upload(device, encoder, &self.texture_layout)
|
||||||
|
{
|
||||||
let instance_buffer = device
|
let instance_buffer = device
|
||||||
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
||||||
.fill_from_slice(&[Instance {
|
.fill_from_slice(&[Instance {
|
||||||
|
|
@ -263,8 +266,8 @@ impl Pipeline {
|
||||||
);
|
);
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut render_pass =
|
let mut render_pass = encoder.begin_render_pass(
|
||||||
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
&wgpu::RenderPassDescriptor {
|
||||||
color_attachments: &[
|
color_attachments: &[
|
||||||
wgpu::RenderPassColorAttachmentDescriptor {
|
wgpu::RenderPassColorAttachmentDescriptor {
|
||||||
attachment: target,
|
attachment: target,
|
||||||
|
|
@ -280,7 +283,8 @@ impl Pipeline {
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
depth_stencil_attachment: None,
|
depth_stencil_attachment: None,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
render_pass.set_pipeline(&self.pipeline);
|
render_pass.set_pipeline(&self.pipeline);
|
||||||
render_pass.set_bind_group(0, &self.constants, &[]);
|
render_pass.set_bind_group(0, &self.constants, &[]);
|
||||||
|
|
@ -306,6 +310,7 @@ impl Pipeline {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Memory {
|
enum Memory {
|
||||||
|
|
@ -317,6 +322,7 @@ enum Memory {
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
},
|
},
|
||||||
|
NotFound,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Memory {
|
impl Memory {
|
||||||
|
|
@ -324,6 +330,7 @@ impl Memory {
|
||||||
match self {
|
match self {
|
||||||
Memory::Host { image } => image.dimensions(),
|
Memory::Host { image } => image.dimensions(),
|
||||||
Memory::Device { width, height, .. } => (*width, *height),
|
Memory::Device { width, height, .. } => (*width, *height),
|
||||||
|
Memory::NotFound => (1, 1),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -332,7 +339,7 @@ impl Memory {
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
texture_layout: &wgpu::BindGroupLayout,
|
texture_layout: &wgpu::BindGroupLayout,
|
||||||
) -> Rc<wgpu::BindGroup> {
|
) -> Option<Rc<wgpu::BindGroup>> {
|
||||||
match self {
|
match self {
|
||||||
Memory::Host { image } => {
|
Memory::Host { image } => {
|
||||||
let (width, height) = image.dimensions();
|
let (width, height) = image.dimensions();
|
||||||
|
|
@ -402,9 +409,10 @@ impl Memory {
|
||||||
height,
|
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