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

* Fix panic when creating a surface while no backend is available

* changelog entry
This commit is contained in:
Andreas Reich 2024-01-31 09:48:33 +01:00 committed by GitHub
parent 0888a630a0
commit 45957088ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 6 deletions

View File

@ -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

View File

@ -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<SurfaceId>,
) -> Result<SurfaceId, hal::InstanceError> {
) -> Result<SurfaceId, CreateSurfaceError> {
profiling::scope!("Instance::create_surface");
fn init<A: HalApi>(
@ -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),

View File

@ -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<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),
}