From fd5550cc89f1d781e4fe0d4645c2cd810c4b44bb Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Wed, 19 Jul 2023 17:04:31 -0400 Subject: [PATCH] Make RequestAdapterOptions.power_preference optional (#3903) Co-authored-by: Connor Fitzgerald --- CHANGELOG.md | 3 ++- player/src/bin/play.rs | 2 +- player/tests/test.rs | 2 +- wgpu-core/src/instance.rs | 11 +++++++++++ wgpu-types/src/lib.rs | 10 ++++++---- wgpu/src/backend/web.rs | 11 ++++++++--- 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 61482fc9b..c9f1797b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,7 +46,8 @@ Bottom level categories: #### Misc Breaking Changes - Change `AdapterInfo::{device,vendor}` to be `u32` instead of `usize`. By @ameknite in [#3760](https://github.com/gfx-rs/wgpu/pull/3760) -- Remove the `backend_bits` parameter in `initialize_adapter_from_env` and `initialize_adapter_from_env_or_default` - use [InstanceDescriptor::backends](https://docs.rs/wgpu/latest/wgpu/struct.InstanceDescriptor.html#structfield.backends) instead. By @fornwall in [#3904](https://github.com/gfx-rs/wgpu/pull/3904) +- Remove the `backend_bits` parameter in `initialize_adapter_from_env` and `initialize_adapter_from_env_or_default` - use [InstanceDescriptor::backends](https://docs.rs/wgpu/latest/wgpu/struct.InstanceDescriptor.html#structfield.backends) instead. By @fornwall in [#3904](https://github.com/gfx-rs/wgpu/pull/3904) +- Make `RequestAdapterOptions.power_preference` optional. By @Aaron1011 in [#3903](https://github.com/gfx-rs/wgpu/pull/3903) - Add a `compatible_surface` parameter to `initialize_adapter_from_env` and use that to make `initialize_adapter_from_env_or_default` always respect its `compatible_surface` parameter. By @fornwall in [#3905](https://github.com/gfx-rs/wgpu/pull/3905) #### Vulkan diff --git a/player/src/bin/play.rs b/player/src/bin/play.rs index cb596b4ac..a9a47ce5f 100644 --- a/player/src/bin/play.rs +++ b/player/src/bin/play.rs @@ -64,7 +64,7 @@ fn main() { let adapter = global .request_adapter( &wgc::instance::RequestAdapterOptions { - power_preference: wgt::PowerPreference::LowPower, + power_preference: wgt::PowerPreference::None, force_fallback_adapter: false, #[cfg(feature = "winit")] compatible_surface: Some(surface), diff --git a/player/tests/test.rs b/player/tests/test.rs index cafb9c0ee..bbaa66cc4 100644 --- a/player/tests/test.rs +++ b/player/tests/test.rs @@ -193,7 +193,7 @@ impl Corpus { } let adapter = match global.request_adapter( &wgc::instance::RequestAdapterOptions { - power_preference: wgt::PowerPreference::LowPower, + power_preference: wgt::PowerPreference::None, force_fallback_adapter: false, compatible_surface: None, }, diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index b291de78c..45f01824b 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -896,6 +896,17 @@ impl Global { // hardware GPU (integrated or discrete). PowerPreference::LowPower => integrated.or(discrete).or(other).or(virt).or(cpu), PowerPreference::HighPerformance => discrete.or(integrated).or(other).or(virt).or(cpu), + PowerPreference::None => { + let option_min = |a: Option, b: Option| { + if let (Some(a), Some(b)) = (a, b) { + Some(a.min(b)) + } else { + a.or(b) + } + }; + // Pick the lowest id of these types + option_min(option_min(discrete, integrated), other) + } }; let mut selected = preferred_gpu.unwrap_or(0); diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 4d8ff0a8b..5532c8a80 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -130,16 +130,18 @@ impl Backend { /// Corresponds to [WebGPU `GPUPowerPreference`]( /// https://gpuweb.github.io/gpuweb/#enumdef-gpupowerpreference). #[repr(C)] -#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)] +#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)] #[cfg_attr(feature = "trace", derive(Serialize))] #[cfg_attr(feature = "replay", derive(Deserialize))] #[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))] pub enum PowerPreference { - /// Adapter that uses the least possible power. This is often an integrated GPU. #[default] - LowPower = 0, + /// Power usage is not considered when choosing an adapter. + None = 0, + /// Adapter that uses the least possible power. This is often an integrated GPU. + LowPower = 1, /// Adapter that has the highest performance. This is often a discrete GPU. - HighPerformance = 1, + HighPerformance = 2, } bitflags::bitflags! { diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index 1dc5c712a..be5f79829 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -986,10 +986,15 @@ impl crate::context::Context for Context { //assert!(backends.contains(wgt::Backends::BROWSER_WEBGPU)); let mut mapped_options = web_sys::GpuRequestAdapterOptions::new(); let mapped_power_preference = match options.power_preference { - wgt::PowerPreference::LowPower => web_sys::GpuPowerPreference::LowPower, - wgt::PowerPreference::HighPerformance => web_sys::GpuPowerPreference::HighPerformance, + wgt::PowerPreference::None => None, + wgt::PowerPreference::LowPower => Some(web_sys::GpuPowerPreference::LowPower), + wgt::PowerPreference::HighPerformance => { + Some(web_sys::GpuPowerPreference::HighPerformance) + } }; - mapped_options.power_preference(mapped_power_preference); + if let Some(mapped_pref) = mapped_power_preference { + mapped_options.power_preference(mapped_pref); + } let adapter_promise = self.0.request_adapter_with_options(&mapped_options); MakeSendFuture::new(