diff --git a/CHANGELOG.md b/CHANGELOG.md index 99bbcbc48..db05ec360 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,6 +103,7 @@ Bottom level categories: - Fix `serde` feature not compiling for `wgpu-types`. By @KirmesBude in [#5149](https://github.com/gfx-rs/wgpu/pull/5149) - Fix the validation of vertex and index ranges. By @nical in [#5144](https://github.com/gfx-rs/wgpu/pull/5144) and [#5156](https://github.com/gfx-rs/wgpu/pull/5156) - Device lost callbacks are invoked when replaced and when global is dropped. By @bradwerth in [#5168](https://github.com/gfx-rs/wgpu/pull/5168) +- Fix panic when creating a surface while no backend is available. By @wumpf [#5166](https://github.com/gfx-rs/wgpu/pull/5166) #### WGL diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 4c3134237..5ef090def 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -471,6 +471,15 @@ pub enum RequestAdapterError { InvalidSurface(SurfaceId), } +#[derive(Clone, Debug, Error)] +#[non_exhaustive] +pub enum CreateSurfaceError { + #[error("No backend is available")] + NoSupportedBackend, + #[error(transparent)] + InstanceError(#[from] hal::InstanceError), +} + impl Global { /// # Safety /// @@ -483,7 +492,7 @@ impl Global { display_handle: raw_window_handle::RawDisplayHandle, window_handle: raw_window_handle::RawWindowHandle, id_in: Option, - ) -> Result { + ) -> Result { profiling::scope!("Instance::create_surface"); fn init( @@ -521,8 +530,7 @@ impl Global { hal_surface = init::(&self.instance.gl, display_handle, window_handle); } - // This is only None if there's no instance at all. - let hal_surface = hal_surface.unwrap()?; + let hal_surface = hal_surface.ok_or(CreateSurfaceError::NoSupportedBackend)??; let surface = Surface { presentation: Mutex::new(None), diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 6becc7cde..1e52369a3 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -2812,7 +2812,7 @@ pub struct CreateSurfaceError { enum CreateSurfaceErrorKind { /// Error from [`wgpu_hal`]. #[cfg(wgpu_core)] - Hal(hal::InstanceError), + Hal(wgc::instance::CreateSurfaceError), /// Error from WebGPU surface creation. #[allow(dead_code)] // may be unused depending on target and features @@ -2847,8 +2847,8 @@ impl error::Error for CreateSurfaceError { } #[cfg(wgpu_core)] -impl From for CreateSurfaceError { - fn from(e: hal::InstanceError) -> Self { +impl From for CreateSurfaceError { + fn from(e: wgc::instance::CreateSurfaceError) -> Self { Self { inner: CreateSurfaceErrorKind::Hal(e), }