Skip to content

Commit

Permalink
Add get_default_configure to simplify user creation of `SurfaceConf…
Browse files Browse the repository at this point in the history
…iguration`
  • Loading branch information
jinleili committed Sep 20, 2022
1 parent ffd7337 commit fa44e57
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,21 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
)
}

pub fn surface_get_default_configure<A: HalApi>(
&self,
surface_id: id::SurfaceId,
adapter_id: id::AdapterId,
width: u32,
height: u32,
) -> Result<wgt::SurfaceConfiguration, instance::GetSurfaceSupportError> {
profiling::scope!("Surface::get_default_configure");
self.fetch_adapter_and_surface::<A, _, wgt::SurfaceConfiguration>(
surface_id,
adapter_id,
|adapter, surface| surface.get_default_configure(adapter, width, height),
)
}

fn fetch_adapter_and_surface<
A: HalApi,
F: FnOnce(&Adapter<A>, &Surface) -> Result<B, instance::GetSurfaceSupportError>,
Expand Down
20 changes: 20 additions & 0 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@ impl Surface {

Ok(caps)
}

pub fn get_default_configure<A: HalApi>(
&self,
adapter: &Adapter<A>,
width: u32,
height: u32,
) -> Result<wgt::SurfaceConfiguration, GetSurfaceSupportError> {
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<A: hal::Api> {
Expand Down
9 changes: 1 addition & 8 deletions wgpu/examples/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,7 @@ fn start<E: Example>(
}: 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...");
Expand Down
15 changes: 15 additions & 0 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ trait Context: Debug + Send + Sized + Sync {
surface: &Self::SurfaceId,
adapter: &Self::AdapterId,
) -> Vec<CompositeAlphaMode>;
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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fa44e57

Please sign in to comment.