Make Surface::get_default_config return an Option.

This commit is contained in:
Connor Fitzgerald 2022-11-25 22:35:07 -05:00
parent 84cb3e65a9
commit 183ba3f231
3 changed files with 19 additions and 7 deletions

View File

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

View File

@ -267,7 +267,9 @@ fn start<E: Example>(
}: 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...");

View File

@ -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<wgt::SurfaceConfiguration> {
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.