From 027a61a48dc1df2be8bf3430844489171204765f Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Thu, 31 Oct 2019 22:08:26 -0700 Subject: [PATCH] return Result instead of panic --- examples/triangle/main.c | 4 ++++ wgpu-native/src/id.rs | 2 ++ wgpu-native/src/swap_chain.rs | 15 +++++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/examples/triangle/main.c b/examples/triangle/main.c index db711c62a6..711508ada5 100644 --- a/examples/triangle/main.c +++ b/examples/triangle/main.c @@ -211,6 +211,10 @@ int main() { WGPUSwapChainOutput next_texture = wgpu_swap_chain_get_next_texture(swap_chain); + if (!next_texture.view_id) { + printf("Cannot acquire next swap chain texture"); + return 1; + } WGPUCommandEncoderId cmd_encoder = wgpu_device_create_command_encoder( device, &(WGPUCommandEncoderDescriptor){.todo = 0}); diff --git a/wgpu-native/src/id.rs b/wgpu-native/src/id.rs index 839c8521a7..403dd23863 100644 --- a/wgpu-native/src/id.rs +++ b/wgpu-native/src/id.rs @@ -16,6 +16,8 @@ type Dummy = crate::backend::Empty; pub struct Id(u64, PhantomData); impl Id { + pub const ERROR: Self = Self(0, PhantomData); + pub fn backend(&self) -> Backend { match self.0 >> (64 - BACKEND_BITS) as u8 { 0 => Backend::Empty, diff --git a/wgpu-native/src/swap_chain.rs b/wgpu-native/src/swap_chain.rs index d085b3e228..de84ab678a 100644 --- a/wgpu-native/src/swap_chain.rs +++ b/wgpu-native/src/swap_chain.rs @@ -128,11 +128,16 @@ pub struct SwapChainOutput { pub view_id: TextureViewId, } +#[derive(Debug)] +pub enum SwapChainGetNextTextureError { + GpuProcessingTimeout, +} + pub fn swap_chain_get_next_texture( global: &Global, swap_chain_id: SwapChainId, view_id_in: Input, -) -> SwapChainOutput { +) -> Result { let hub = B::hub(global); let mut token = Token::root(); @@ -148,7 +153,7 @@ pub fn swap_chain_get_next_texture( match unsafe { suf.acquire_image(FRAME_TIMEOUT_MS * 1_000_000) } { Ok(surface_image) => surface_image, Err(hal::window::AcquireError::Timeout) => { - panic!("GPU took too much time processing last frames :("); + return Err(SwapChainGetNextTextureError::GpuProcessingTimeout); } Err(e) => { log::warn!("acquire_image() failed ({:?}), reconfiguring swapchain", e); @@ -197,13 +202,15 @@ pub fn swap_chain_get_next_texture( ref_count, }); - SwapChainOutput { view_id } + Ok(SwapChainOutput { view_id }) } #[cfg(feature = "local")] #[no_mangle] pub extern "C" fn wgpu_swap_chain_get_next_texture(swap_chain_id: SwapChainId) -> SwapChainOutput { - gfx_select!(swap_chain_id => swap_chain_get_next_texture(&*GLOBAL, swap_chain_id, PhantomData)) + gfx_select!(swap_chain_id => swap_chain_get_next_texture(&*GLOBAL, swap_chain_id, PhantomData)).unwrap_or(SwapChainOutput { + view_id: TextureViewId::ERROR, + }) } pub fn swap_chain_present(global: &Global, swap_chain_id: SwapChainId) {