446: Prefer discrete GPU for PowerPreference::Default r=kvark a=aloucks



Co-authored-by: Aaron Loucks <aloucks@cofront.net>
This commit is contained in:
bors[bot] 2020-01-12 23:07:45 +00:00 committed by GitHub
commit 9777bbba36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 3 deletions

View File

@ -9,7 +9,7 @@ add_executable(compute main.c ../framework.c)
if(MSVC)
add_definitions(-DWGPU_TARGET=WGPU_TARGET_WINDOWS)
target_compile_options(${TARGET_NAME} PRIVATE /W4)
set(OS_LIBRARIES "userenv" "ws2_32" "Dwmapi" "dbghelp" "d3dcompiler" "D3D12" "D3D11" "DXGI")
set(OS_LIBRARIES "userenv" "ws2_32" "Dwmapi" "dbghelp" "d3dcompiler" "D3D12" "D3D11" "DXGI" "setupapi")
elseif(APPLE)
add_definitions(-DWGPU_TARGET=WGPU_TARGET_MACOS)
set(OS_LIBRARIES "-framework Cocoa" "-framework CoreVideo" "-framework IOKit" "-framework QuartzCore")

View File

@ -9,7 +9,7 @@ add_executable(triangle main.c ../framework.c)
if(MSVC)
add_definitions(-DWGPU_TARGET=WGPU_TARGET_WINDOWS)
target_compile_options(${TARGET_NAME} PRIVATE /W4)
set(OS_LIBRARIES "userenv" "ws2_32" "Dwmapi" "dbghelp" "d3dcompiler" "D3D12" "D3D11" "DXGI")
set(OS_LIBRARIES "userenv" "ws2_32" "Dwmapi" "dbghelp" "d3dcompiler" "D3D12" "D3D11" "DXGI" "setupapi")
elseif(APPLE)
add_definitions(-DWGPU_TARGET=WGPU_TARGET_MACOS)
set(OS_LIBRARIES "-framework Cocoa" "-framework CoreVideo" "-framework IOKit" "-framework QuartzCore")

View File

@ -46,3 +46,6 @@ gfx-backend-vulkan = { version = "0.4", features = ["x11"] }
gfx-backend-dx12 = { version = "0.4.1" }
gfx-backend-dx11 = { version = "0.4" }
gfx-backend-vulkan = { version = "0.4" }
[target.'cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "dragonfly", target_os = "freebsd"))'.dependencies]
battery = "0.7"

View File

@ -8,6 +8,7 @@ use crate::{
device::{Device, BIND_BUFFER_ALIGNMENT},
hub::{GfxBackend, Global, IdentityFilter, Token},
id::{AdapterId, DeviceId},
power,
Backend,
};
@ -271,7 +272,16 @@ impl<F: IdentityFilter<AdapterId>> Global<F> {
}
let preferred_gpu = match desc.power_preference {
PowerPreference::Default => integrated.or(discrete).or(other).or(virt),
PowerPreference::Default => {
match power::is_battery_discharging() {
Ok(false) => discrete.or(integrated).or(other).or(virt),
Ok(true) => integrated.or(discrete).or(other).or(virt),
Err(err) => {
log::debug!("Power info unavailable, preferring integrated gpu ({})", err);
integrated.or(discrete).or(other).or(virt)
}
}
},
PowerPreference::LowPower => integrated.or(other).or(discrete).or(virt),
PowerPreference::HighPerformance => discrete.or(other).or(integrated).or(virt),
};

View File

@ -25,6 +25,7 @@ pub mod hub;
pub mod id;
pub mod instance;
pub mod pipeline;
pub mod power;
pub mod resource;
pub mod swap_chain;
pub mod track;

63
wgpu-core/src/power.rs Normal file
View File

@ -0,0 +1,63 @@
use std::fmt;
#[derive(Debug)]
pub enum Error {
Unsupported,
Error(Box<dyn std::error::Error>),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Unsupported => write!(f, "Battery status is unsupported on this platform"),
Error::Error(err) => write!(f, "Battery status retrieval failed: {}", err),
}
}
}
#[cfg(any(
target_os = "linux",
target_os = "macos",
target_os = "windows",
target_os = "dragonfly",
target_os = "freebsd"
))]
mod platform {
use super::Error;
use battery::{self, Manager, State};
impl From<battery::errors::Error> for Error {
fn from(err: battery::errors::Error) -> Error {
// Box the error so that the battery::errors::Error type does
// not leak out of this module.
Error::Error(Box::new(err))
}
}
pub fn is_battery_discharging() -> Result<bool, Error> {
let manager = Manager::new()?;
for battery in manager.batteries()? {
if battery?.state() == State::Discharging {
return Ok(true);
}
}
Ok(false)
}
}
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "windows",
target_os = "dragonfly",
target_os = "freebsd"
)))]
mod platform {
use super::Error;
pub fn is_battery_discharging() -> Result<bool, Error> {
Err(Error::Unsupported)
}
}
pub use platform::is_battery_discharging;