From 9cba89f059e32db588e461db57cc980323b61008 Mon Sep 17 00:00:00 2001 From: Jinlei Li Date: Thu, 22 Sep 2022 14:18:50 +0800 Subject: [PATCH] Add `get_default_configure` for `Surface` to simplify user creation of `SurfaceConfiguration` --- CHANGELOG.md | 1 + wgpu/examples/framework.rs | 9 +-------- wgpu/src/lib.rs | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fd4e5348e5..60a0bd50aa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -117,6 +117,7 @@ SurfaceConfiguration { - Address Clippy 0.1.63 complaints. By @jimblandy in [#2977](https://github.com/gfx-rs/wgpu/pull/2977) - Don't use `PhantomData` for `IdentityManager`'s `Input` type. By @jimblandy in [#2972](https://github.com/gfx-rs/wgpu/pull/2972) - Changed Naga variant in ShaderSource to `Cow<'static, Module>`, to allow loading global variables by @daxpedda in [#2903](https://github.com/gfx-rs/wgpu/pull/2903) +- Add `get_default_configure` for `Surface` to simplify user creation of `SurfaceConfiguration`. By @jinleili in [#3034](https://github.com/gfx-rs/wgpu/pull/3034) #### Metal - Extract the generic code into `get_metal_layer` by @jinleili in [#2826](https://github.com/gfx-rs/wgpu/pull/2826) diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index 795765e6534..3b6e5794f0e 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -267,14 +267,7 @@ fn start( }: Setup, ) { let spawner = Spawner::new(); - let mut config = wgpu::SurfaceConfiguration { - usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: surface.get_supported_formats(&adapter)[0], - width: size.width, - height: size.height, - present_mode: wgpu::PresentMode::Fifo, - alpha_mode: surface.get_supported_alpha_modes(&adapter)[0], - }; + let mut config = surface.get_default_configure(&adapter, size.width, size.height); surface.configure(&device, &config); log::info!("Initializing the example..."); diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 9bdcd9d32e0..2cf33116aa6 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -3690,6 +3690,23 @@ impl Surface { Context::surface_get_supported_alpha_modes(&*self.context, &self.id, &adapter.id) } + /// Return a default `SurfaceConfiguration` from width and height to use for the [`Surface`] with this adapter. + pub fn get_default_configure( + &self, + adapter: &Adapter, + width: u32, + height: u32, + ) -> wgt::SurfaceConfiguration { + wgt::SurfaceConfiguration { + usage: wgt::TextureUsages::RENDER_ATTACHMENT, + format: self.get_supported_formats(adapter)[0], + width, + height, + present_mode: self.get_supported_present_modes(adapter)[0], + alpha_mode: wgt::CompositeAlphaMode::Auto, + } + } + /// Initializes [`Surface`] for presentation. /// /// # Panics