Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RequestAdapterOptions.power_preference optional #3903

Merged
merged 4 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Aaron1011 marked this conversation as resolved.
Show resolved Hide resolved
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 {
Aaron1011 marked this conversation as resolved.
Show resolved Hide resolved
/// 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