diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cb917054d9..15d46c6a492 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -115,6 +115,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` 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-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 1f2363e21a9..a11bdc1dd78 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3206,6 +3206,21 @@ impl Global { ) } + pub fn surface_get_default_configure( + &self, + surface_id: id::SurfaceId, + adapter_id: id::AdapterId, + width: u32, + height: u32, + ) -> Result { + profiling::scope!("Surface::get_default_configure"); + self.fetch_adapter_and_surface::( + surface_id, + adapter_id, + |adapter, surface| surface.get_default_configure(adapter, width, height), + ) + } + fn fetch_adapter_and_surface< A: HalApi, F: FnOnce(&Adapter, &Surface) -> Result, diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 6fd32e9f470..100fc55c8b5 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -195,6 +195,26 @@ impl Surface { Ok(caps) } + + pub fn get_default_configure( + &self, + adapter: &Adapter, + width: u32, + height: u32, + ) -> Result { + let formats = self.get_supported_formats(adapter)?; + let present_modes = self.get_supported_present_modes(adapter)?; + let alpha_modes = self.get_supported_alpha_modes(adapter)?; + + Ok(wgt::SurfaceConfiguration { + usage: wgt::TextureUsages::RENDER_ATTACHMENT, + format: formats[0], + width, + height, + present_mode: present_modes[0], + alpha_mode: alpha_modes[0], + }) + } } pub struct Adapter { 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/backend/direct.rs b/wgpu/src/backend/direct.rs index 8ec8bc6f66d..153e23693a7 100644 --- a/wgpu/src/backend/direct.rs +++ b/wgpu/src/backend/direct.rs @@ -1004,6 +1004,21 @@ impl crate::Context for Context { } } + fn surface_get_default_configure( + &self, + surface: &Self::SurfaceId, + adapter: &Self::AdapterId, + width: u32, + height: u32, + ) -> wgt::SurfaceConfiguration { + let global = &self.0; + match wgc::gfx_select!(adapter => global.surface_get_default_configure(surface.id, *adapter, width, height)) + { + Ok(config) => config, + Err(err) => self.handle_error_fatal(err, "Surface::get_default_configure"), + } + } + fn surface_configure( &self, surface: &Self::SurfaceId, diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index f6cba40b1cc..3d89a3e1ed8 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -1229,6 +1229,23 @@ impl crate::Context for Context { vec![wgt::CompositeAlphaMode::Opaque] } + fn surface_get_default_configure( + &self, + _surface: &Self::SurfaceId, + _adapter: &Self::AdapterId, + width: u32, + height: u32, + ) -> wgt::SurfaceConfiguration { + wgt::SurfaceConfiguration { + usage: wgt::TextureUsages::RENDER_ATTACHMENT, + format: wgt::TextureFormat::Bgra8Unorm, + width, + height, + present_mode: wgt::PresentMode::Fifo, + alpha_mode: wgt::CompositeAlphaMode::Opaque, + } + } + fn surface_configure( &self, surface: &Self::SurfaceId, diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 9bdcd9d32e0..11afabc8bc8 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -242,6 +242,13 @@ trait Context: Debug + Send + Sized + Sync { surface: &Self::SurfaceId, adapter: &Self::AdapterId, ) -> Vec; + fn surface_get_default_configure( + &self, + surface: &Self::SurfaceId, + adapter: &Self::AdapterId, + width: u32, + height: u32, + ) -> wgt::SurfaceConfiguration; fn surface_configure( &self, surface: &Self::SurfaceId, @@ -3690,6 +3697,18 @@ 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. + /// + /// Panic if the surface is incompatible with the adapter. + pub fn get_default_configure( + &self, + adapter: &Adapter, + width: u32, + height: u32, + ) -> wgt::SurfaceConfiguration { + Context::surface_get_default_configure(&*self.context, &self.id, &adapter.id, width, height) + } + /// Initializes [`Surface`] for presentation. /// /// # Panics