diff options
author | 2020-12-16 10:03:51 -0600 | |
---|---|---|
committer | 2020-12-16 10:03:51 -0600 | |
commit | 4e391013c8bf8544fb766bee5dbae10cfdbc9d93 (patch) | |
tree | 5272b3d3588a01913854ce4b6e88380909a2a062 /wgpu | |
parent | a42b3c6998274e75fd10f5ff3cecbdb37b9e3895 (diff) | |
download | iced-4e391013c8bf8544fb766bee5dbae10cfdbc9d93.tar.gz iced-4e391013c8bf8544fb766bee5dbae10cfdbc9d93.tar.bz2 iced-4e391013c8bf8544fb766bee5dbae10cfdbc9d93.zip |
don't panic when swapchain frame is outdated
Diffstat (limited to 'wgpu')
-rw-r--r-- | wgpu/src/window/compositor.rs | 123 |
1 files changed, 69 insertions, 54 deletions
diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 492efb42..ad1e609c 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -100,7 +100,7 @@ impl iced_graphics::window::Compositor for Compositor { width: u32, height: u32, ) -> Self::SwapChain { - self.device.create_swap_chain( + let swap_chain = self.device.create_swap_chain( surface, &wgpu::SwapChainDescriptor { usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, @@ -109,7 +109,9 @@ impl iced_graphics::window::Compositor for Compositor { width, height, }, - ) + ); + + swap_chain } fn draw<T: AsRef<str>>( @@ -120,58 +122,71 @@ impl iced_graphics::window::Compositor for Compositor { background_color: Color, output: &<Self::Renderer as iced_native::Renderer>::Output, overlay: &[T], - ) -> mouse::Interaction { - let frame = swap_chain.get_current_frame().expect("Next frame"); - - let mut encoder = self.device.create_command_encoder( - &wgpu::CommandEncoderDescriptor { - label: Some("iced_wgpu encoder"), + ) -> Result<mouse::Interaction, ()> { + match swap_chain.get_current_frame() { + Ok(frame) => { + let mut encoder = self.device.create_command_encoder( + &wgpu::CommandEncoderDescriptor { + label: Some("iced_wgpu encoder"), + }, + ); + + let _ = + encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + color_attachments: &[ + wgpu::RenderPassColorAttachmentDescriptor { + attachment: &frame.output.view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear({ + let [r, g, b, a] = + background_color.into_linear(); + + wgpu::Color { + r: f64::from(r), + g: f64::from(g), + b: f64::from(b), + a: f64::from(a), + } + }), + store: true, + }, + }, + ], + depth_stencil_attachment: None, + }); + + let mouse_interaction = renderer.backend_mut().draw( + &mut self.device, + &mut self.staging_belt, + &mut encoder, + &frame.output.view, + viewport, + output, + overlay, + ); + + // Submit work + self.staging_belt.finish(); + self.queue.submit(Some(encoder.finish())); + + // Recall staging buffers + self.local_pool + .spawner() + .spawn(self.staging_belt.recall()) + .expect("Recall staging belt"); + + self.local_pool.run_until_stalled(); + + Ok(mouse_interaction) + } + Err(error) => match error { + wgpu::SwapChainError::Outdated => { + // Try again next frame. + Err(()) + } + _ => panic!("Swapchain error: {:?}", error), }, - ); - - let _ = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { - attachment: &frame.output.view, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear({ - let [r, g, b, a] = background_color.into_linear(); - - wgpu::Color { - r: f64::from(r), - g: f64::from(g), - b: f64::from(b), - a: f64::from(a), - } - }), - store: true, - }, - }], - depth_stencil_attachment: None, - }); - - let mouse_interaction = renderer.backend_mut().draw( - &mut self.device, - &mut self.staging_belt, - &mut encoder, - &frame.output.view, - viewport, - output, - overlay, - ); - - // Submit work - self.staging_belt.finish(); - self.queue.submit(Some(encoder.finish())); - - // Recall staging buffers - self.local_pool - .spawner() - .spawn(self.staging_belt.recall()) - .expect("Recall staging belt"); - - self.local_pool.run_until_stalled(); - - mouse_interaction + } } } |