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,68 +243,70 @@ 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
|
|
||||||
.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 =
|
let instance_buffer = device
|
||||||
encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
.create_buffer_mapped(1, wgpu::BufferUsage::COPY_SRC)
|
||||||
color_attachments: &[
|
.fill_from_slice(&[Instance {
|
||||||
wgpu::RenderPassColorAttachmentDescriptor {
|
_position: image.position,
|
||||||
attachment: target,
|
_scale: image.scale,
|
||||||
resolve_target: None,
|
}]);
|
||||||
load_op: wgpu::LoadOp::Load,
|
|
||||||
store_op: wgpu::StoreOp::Store,
|
encoder.copy_buffer_to_buffer(
|
||||||
clear_color: wgpu::Color {
|
&instance_buffer,
|
||||||
r: 0.0,
|
0,
|
||||||
g: 0.0,
|
&self.instances,
|
||||||
b: 0.0,
|
0,
|
||||||
a: 0.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_pipeline(&self.pipeline);
|
||||||
render_pass.set_bind_group(0, &self.constants, &[]);
|
render_pass.set_bind_group(0, &self.constants, &[]);
|
||||||
render_pass.set_bind_group(1, &texture, &[]);
|
render_pass.set_bind_group(1, &texture, &[]);
|
||||||
render_pass.set_index_buffer(&self.indices, 0);
|
render_pass.set_index_buffer(&self.indices, 0);
|
||||||
render_pass.set_vertex_buffers(
|
render_pass.set_vertex_buffers(
|
||||||
0,
|
0,
|
||||||
&[(&self.vertices, 0), (&self.instances, 0)],
|
&[(&self.vertices, 0), (&self.instances, 0)],
|
||||||
);
|
);
|
||||||
render_pass.set_scissor_rect(
|
render_pass.set_scissor_rect(
|
||||||
bounds.x,
|
bounds.x,
|
||||||
bounds.y,
|
bounds.y,
|
||||||
bounds.width,
|
bounds.width,
|
||||||
bounds.height,
|
bounds.height,
|
||||||
);
|
);
|
||||||
|
|
||||||
render_pass.draw_indexed(
|
render_pass.draw_indexed(
|
||||||
0..QUAD_INDICES.len() as u32,
|
0..QUAD_INDICES.len() as u32,
|
||||||
0,
|
0,
|
||||||
0..1 as u32,
|
0..1 as u32,
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -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