From dd23e8f015c7ef9d7edbe78431ab4d693ef4e3e6 Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Tue, 5 Nov 2019 14:46:25 -0800 Subject: [PATCH] Handle error case for SwapChain::get_next_texture --- examples/framework.rs | 3 ++- examples/hello-triangle/main.rs | 3 ++- src/lib.rs | 20 +++++++++++++------- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/examples/framework.rs b/examples/framework.rs index efdd3b52c4..1d96cb0960 100644 --- a/examples/framework.rs +++ b/examples/framework.rs @@ -162,7 +162,8 @@ pub fn run(title: &str) { } }, event::Event::EventsCleared => { - let frame = swap_chain.get_next_texture(); + let frame = swap_chain.get_next_texture() + .expect("Timeout when acquiring next swap chain texture"); let command_buf = example.render(&frame, &device); queue.submit(&[command_buf]); } diff --git a/examples/hello-triangle/main.rs b/examples/hello-triangle/main.rs index 45d64ead15..d363d3fb5d 100644 --- a/examples/hello-triangle/main.rs +++ b/examples/hello-triangle/main.rs @@ -133,7 +133,8 @@ fn main() { _ => {} }, event::Event::EventsCleared => { - let frame = swap_chain.get_next_texture(); + let frame = swap_chain.get_next_texture() + .expect("Timeout when acquiring next swap chain texture"); let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { todo: 0 }); { diff --git a/src/lib.rs b/src/lib.rs index a1b8b581ae..e55b17fde5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1351,14 +1351,20 @@ impl SwapChain { /// /// When the [`SwapChainOutput`] returned by this method is dropped, the swapchain will present /// the texture to the associated [`Surface`]. - pub fn get_next_texture(&mut self) -> SwapChainOutput { + /// + /// Returns an `Err` if the GPU timed out when attempting to acquire the next texture. + pub fn get_next_texture(&mut self) -> Result { let output = wgn::wgpu_swap_chain_get_next_texture(self.id); - SwapChainOutput { - view: TextureView { - id: output.view_id, - owned: false, - }, - swap_chain_id: &self.id, + if output.view_id == wgn::Id::ERROR { + Err(()) + } else { + Ok(SwapChainOutput { + view: TextureView { + id: output.view_id, + owned: false, + }, + swap_chain_id: &self.id, + }) } } }