From 4a1174b5ed0ff136d1a34e4a50277b59bf4b7b7b Mon Sep 17 00:00:00 2001 From: Erich Gubler Date: Tue, 1 Nov 2022 17:21:37 -0600 Subject: [PATCH] refactor: use linear flow instead of broken `loop` for determining fallback --- wgpu-core/src/device/global.rs | 58 ++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index ad10aa6df..d6133f438 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -1916,37 +1916,39 @@ impl Global { } if !caps.present_modes.contains(&config.present_mode) { - let new_mode = 'error: { - // Automatic present mode checks. - // - // The "Automatic" modes are never supported by the backends. - let fallbacks = match config.present_mode { - wgt::PresentMode::AutoVsync => { - &[wgt::PresentMode::FifoRelaxed, wgt::PresentMode::Fifo][..] - } - // Always end in FIFO to make sure it's always supported - wgt::PresentMode::AutoNoVsync => &[ - wgt::PresentMode::Immediate, - wgt::PresentMode::Mailbox, - wgt::PresentMode::Fifo, - ][..], - _ => { - return Err(E::UnsupportedPresentMode { - requested: config.present_mode, - available: caps.present_modes.clone(), - }); - } - }; - - for &fallback in fallbacks { - if caps.present_modes.contains(&fallback) { - break 'error fallback; - } + // Automatic present mode checks. + // + // The "Automatic" modes are never supported by the backends. + let fallbacks = match config.present_mode { + wgt::PresentMode::AutoVsync => { + &[wgt::PresentMode::FifoRelaxed, wgt::PresentMode::Fifo][..] + } + // Always end in FIFO to make sure it's always supported + wgt::PresentMode::AutoNoVsync => &[ + wgt::PresentMode::Immediate, + wgt::PresentMode::Mailbox, + wgt::PresentMode::Fifo, + ][..], + _ => { + return Err(E::UnsupportedPresentMode { + requested: config.present_mode, + available: caps.present_modes.clone(), + }); } - - unreachable!("Fallback system failed to choose present mode. This is a bug. Mode: {:?}, Options: {:?}", config.present_mode, &caps.present_modes); }; + let new_mode = fallbacks + .iter() + .copied() + .find(|fallback| caps.present_modes.contains(fallback)) + .unwrap_or_else(|| { + unreachable!( + "Fallback system failed to choose present mode. \ + This is a bug. Mode: {:?}, Options: {:?}", + config.present_mode, &caps.present_modes + ); + }); + api_log!( "Automatically choosing presentation mode by rule {:?}. Chose {new_mode:?}", config.present_mode