Skip to content

Commit

Permalink
Make Surface::get_default_config return an Option.
Browse files Browse the repository at this point in the history
  • Loading branch information
cwfitzgerald committed Nov 27, 2022
1 parent 84cb3e6 commit 183ba3f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion wgpu/examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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...");
Expand Down
14 changes: 8 additions & 6 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 183ba3f

Please sign in to comment.