From 627fdf821e663c6519e9f3659aafbfa4404abe52 Mon Sep 17 00:00:00 2001 From: Jinlei Li Date: Mon, 19 Sep 2022 15:22:10 +0800 Subject: [PATCH] Add `from_size()` to get a default `SurfaceConfiguration`, simplify the use of users --- CHANGELOG.md | 1 + wgpu-types/src/lib.rs | 15 +++++++++++++++ wgpu/examples/framework.rs | 9 +-------- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd1493c3158..e438cfa5d95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -110,6 +110,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 `from_size()` to get a default `SurfaceConfiguration`, simplify the use of users. 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-types/src/lib.rs b/wgpu-types/src/lib.rs index 1b698bd9d47..004f5f1c018 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -3122,6 +3122,21 @@ pub struct SurfaceConfiguration { pub alpha_mode: CompositeAlphaMode, } +impl SurfaceConfiguration { + /// Get a default `SurfaceConfiguration` from width and height + #[allow(dead_code)] + pub fn from_size(width: u32, height: u32) -> Self { + Self { + usage: crate::TextureUsages::RENDER_ATTACHMENT, + format: TextureFormat::Bgra8UnormSrgb, + width, + height, + present_mode: PresentMode::AutoVsync, + alpha_mode: CompositeAlphaMode::Auto, + } + } +} + /// Status of the recieved surface image. #[repr(C)] #[derive(Debug)] diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index 795765e6534..e036ed2aa09 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 = wgpu::SurfaceConfiguration::from_size(size.width, size.height); surface.configure(&device, &config); log::info!("Initializing the example...");