Skip to content

Commit 027a61a

Browse files
committed
return Result instead of panic
1 parent b8d26c9 commit 027a61a

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

examples/triangle/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ int main() {
211211

212212
WGPUSwapChainOutput next_texture =
213213
wgpu_swap_chain_get_next_texture(swap_chain);
214+
if (!next_texture.view_id) {
215+
printf("Cannot acquire next swap chain texture");
216+
return 1;
217+
}
214218

215219
WGPUCommandEncoderId cmd_encoder = wgpu_device_create_command_encoder(
216220
device, &(WGPUCommandEncoderDescriptor){.todo = 0});

wgpu-native/src/id.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type Dummy = crate::backend::Empty;
1616
pub struct Id<T>(u64, PhantomData<T>);
1717

1818
impl<T> Id<T> {
19+
pub const ERROR: Self = Self(0, PhantomData);
20+
1921
pub fn backend(&self) -> Backend {
2022
match self.0 >> (64 - BACKEND_BITS) as u8 {
2123
0 => Backend::Empty,

wgpu-native/src/swap_chain.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,16 @@ pub struct SwapChainOutput {
128128
pub view_id: TextureViewId,
129129
}
130130

131+
#[derive(Debug)]
132+
pub enum SwapChainGetNextTextureError {
133+
GpuProcessingTimeout,
134+
}
135+
131136
pub fn swap_chain_get_next_texture<B: GfxBackend>(
132137
global: &Global,
133138
swap_chain_id: SwapChainId,
134139
view_id_in: Input<TextureViewId>,
135-
) -> SwapChainOutput {
140+
) -> Result<SwapChainOutput, SwapChainGetNextTextureError> {
136141
let hub = B::hub(global);
137142
let mut token = Token::root();
138143

@@ -148,7 +153,7 @@ pub fn swap_chain_get_next_texture<B: GfxBackend>(
148153
match unsafe { suf.acquire_image(FRAME_TIMEOUT_MS * 1_000_000) } {
149154
Ok(surface_image) => surface_image,
150155
Err(hal::window::AcquireError::Timeout) => {
151-
panic!("GPU took too much time processing last frames :(");
156+
return Err(SwapChainGetNextTextureError::GpuProcessingTimeout);
152157
}
153158
Err(e) => {
154159
log::warn!("acquire_image() failed ({:?}), reconfiguring swapchain", e);
@@ -197,13 +202,15 @@ pub fn swap_chain_get_next_texture<B: GfxBackend>(
197202
ref_count,
198203
});
199204

200-
SwapChainOutput { view_id }
205+
Ok(SwapChainOutput { view_id })
201206
}
202207

203208
#[cfg(feature = "local")]
204209
#[no_mangle]
205210
pub extern "C" fn wgpu_swap_chain_get_next_texture(swap_chain_id: SwapChainId) -> SwapChainOutput {
206-
gfx_select!(swap_chain_id => swap_chain_get_next_texture(&*GLOBAL, swap_chain_id, PhantomData))
211+
gfx_select!(swap_chain_id => swap_chain_get_next_texture(&*GLOBAL, swap_chain_id, PhantomData)).unwrap_or(SwapChainOutput {
212+
view_id: TextureViewId::ERROR,
213+
})
207214
}
208215

209216
pub fn swap_chain_present<B: GfxBackend>(global: &Global, swap_chain_id: SwapChainId) {

0 commit comments

Comments
 (0)