Skip to content

Commit

Permalink
Fix panic when creating a surface while no backend is available (#5166)
Browse files Browse the repository at this point in the history
* Fix panic when creating a surface while no backend is available

* changelog entry
  • Loading branch information
Wumpf authored Jan 31, 2024
1 parent 0888a63 commit 4595708
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand All @@ -483,7 +492,7 @@ impl Global {
display_handle: raw_window_handle::RawDisplayHandle,
window_handle: raw_window_handle::RawWindowHandle,
id_in: Option<SurfaceId>,
) -> Result<SurfaceId, hal::InstanceError> {
) -> Result<SurfaceId, CreateSurfaceError> {
profiling::scope!("Instance::create_surface");

fn init<A: HalApi>(
Expand Down Expand Up @@ -521,8 +530,7 @@ impl Global {
hal_surface = init::<hal::api::Gles>(&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),
Expand Down
6 changes: 3 additions & 3 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -2847,8 +2847,8 @@ impl error::Error for CreateSurfaceError {
}

#[cfg(wgpu_core)]
impl From<hal::InstanceError> for CreateSurfaceError {
fn from(e: hal::InstanceError) -> Self {
impl From<wgc::instance::CreateSurfaceError> for CreateSurfaceError {
fn from(e: wgc::instance::CreateSurfaceError) -> Self {
Self {
inner: CreateSurfaceErrorKind::Hal(e),
}
Expand Down

0 comments on commit 4595708

Please sign in to comment.