Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Add config option for power preference & default to wgpu::PowerPrefer…
Browse files Browse the repository at this point in the history
…ence::None
  • Loading branch information
darthdeus committed Apr 27, 2024
1 parent 83ba889 commit cfebbb4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
15 changes: 15 additions & 0 deletions comfy-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ pub fn game_config_mut() -> AtomicRefMut<'static, GameConfig> {
.borrow_mut()
}

#[derive(Copy, Clone, Debug)]
pub enum PowerPreference {
None,
LowPower,
HighPerformance,
}

#[derive(Clone, Debug)]
pub struct GameConfig {
pub game_name: String,
Expand All @@ -80,6 +87,12 @@ pub struct GameConfig {
pub resolution: ResolutionConfig,
pub min_resolution: ResolutionConfig,

/// Overrides `wgpu`'s power preference for adapter selection. Should work around
/// a potential issue where when selecting `wgpu::PowerPreference::HighPerformance` is on
/// and the user has a laptop with a dual GPU setup where the dedicated GPU is not enabled,
/// `wgpu` would still choose it and then crash.
pub power_preference: PowerPreference,

/// Overrides the scale factor for the window. This is useful for making
/// egui independent of the system UI scaling.
pub scale_factor_override: Option<f32>,
Expand Down Expand Up @@ -131,6 +144,8 @@ impl Default for GameConfig {
resolution,
min_resolution,

power_preference: PowerPreference::None,

scale_factor_override: None,
fullscreen: false,

Expand Down
5 changes: 3 additions & 2 deletions comfy-wgpu/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ pub async fn create_graphics_context(

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
// TODO: make this configurable
power_preference: wgpu::PowerPreference::HighPerformance,
power_preference: power_preference_to_wgpu(
game_config().power_preference,
),
compatible_surface: Some(&surface),
force_fallback_adapter: false,
})
Expand Down
12 changes: 12 additions & 0 deletions comfy-wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ impl BufferType {
}
}

pub fn power_preference_to_wgpu(
pref: PowerPreference,
) -> wgpu::PowerPreference {
match pref {
PowerPreference::None => wgpu::PowerPreference::None,
PowerPreference::LowPower => wgpu::PowerPreference::LowPower,
PowerPreference::HighPerformance => {
wgpu::PowerPreference::HighPerformance
}
}
}

pub struct UniformBindGroup {
pub bind_group: wgpu::BindGroup,
pub layout: wgpu::BindGroupLayout,
Expand Down

0 comments on commit cfebbb4

Please sign in to comment.