Skip to content

Commit

Permalink
Make RequestAdapterOptions.power_preference optional (#3903)
Browse files Browse the repository at this point in the history
Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
  • Loading branch information
Aaron1011 and cwfitzgerald authored Jul 19, 2023
1 parent db87ee8 commit fd5550c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion player/src/bin/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion player/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
11 changes: 11 additions & 0 deletions wgpu-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,17 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
// 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<usize>, b: Option<usize>| {
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);
Expand Down
10 changes: 6 additions & 4 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
11 changes: 8 additions & 3 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit fd5550c

Please sign in to comment.