Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when creating a surface while no backend is available #5166

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading