From 183ba3f231954d6bc17297ce0ae4a065b652e519 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Fri, 25 Nov 2022 22:35:07 -0500 Subject: [PATCH] Make `Surface::get_default_config` return an Option. --- CHANGELOG.md | 8 ++++++++ wgpu/examples/framework.rs | 4 +++- wgpu/src/lib.rs | 14 ++++++++------ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3712bc778..416bc11a66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,6 +56,13 @@ The various surface capability functions were combined into a single call that g + let alpha_modes = caps.alpha_modes; ``` +Additionally `Surface::get_default_config` now returns an Option and returns None if the surface isn't supported by the adapter. + +```diff +- let config = surface.get_default_config(&adapter); ++ let config = surface.get_default_config(&adapter).expect("Surface unsupported by adapter"); +``` + ### Changes #### General @@ -65,6 +72,7 @@ The various surface capability functions were combined into a single call that g - Improve compute shader validation error message. By @haraldreingruber in [#3139](https://github.com/gfx-rs/wgpu/pull/3139) - New downlevel feature `UNRESTRICTED_INDEX_BUFFER` to indicate support for using `INDEX` together with other non-copy/map usages (unsupported on WebGL). By @Wumpf in [#3157](https://github.com/gfx-rs/wgpu/pull/3157) - Combine `Surface::get_supported_formats`, `Surface::get_supported_present_modes`, and `Surface::get_supported_alpha_modes` into `Surface::get_capabilities` and `SurfaceCapabilities`. By @cwfitzgerald in [#3157](https://github.com/gfx-rs/wgpu/pull/3157) +- Make `Surface::get_default_config` return an Option to prevent panics. By @cwfitzgerald in [#3157](https://github.com/gfx-rs/wgpu/pull/3157) #### WebGPU diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index 5474ff6d5f..76d2c425ee 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -267,7 +267,9 @@ fn start( }: Setup, ) { let spawner = Spawner::new(); - let mut config = surface.get_default_config(&adapter, size.width, size.height); + let mut config = surface + .get_default_config(&adapter, size.width, size.height) + .expect("Surface isn't supported by the adapter."); surface.configure(&device, &config); log::info!("Initializing the example..."); diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 2bf5075268..ff67365ec6 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -3705,28 +3705,30 @@ impl Drop for SurfaceTexture { impl Surface { /// Returns the capabilities of the surface when used with the given adapter. - /// + /// /// Returns specified values (see [`SurfaceCapabilities`]) if surface is incompatible with the adapter. pub fn get_capabilities(&self, adapter: &Adapter) -> SurfaceCapabilities { Context::surface_get_capabilities(&*self.context, &self.id, &adapter.id) } /// Return a default `SurfaceConfiguration` from width and height to use for the [`Surface`] with this adapter. + /// + /// Returns None if the surface isn't supported by this adapter pub fn get_default_config( &self, adapter: &Adapter, width: u32, height: u32, - ) -> wgt::SurfaceConfiguration { + ) -> Option { let caps = self.get_capabilities(adapter); - wgt::SurfaceConfiguration { + Some(wgt::SurfaceConfiguration { usage: wgt::TextureUsages::RENDER_ATTACHMENT, - format: caps.formats[0], + format: *caps.formats.get(0)?, width, height, - present_mode: caps.present_modes[0], + present_mode: *caps.present_modes.get(0)?, alpha_mode: wgt::CompositeAlphaMode::Auto, - } + }) } /// Initializes [`Surface`] for presentation.