From 20c8ee302c392b9abaaa479603cbf6220c54e36f Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 29 May 2022 20:37:19 +0200 Subject: [PATCH] Allow software rendering OpenGL by default (#1693) Follow-up to https://github.com/emilk/egui/pull/1681 --- CHANGELOG.md | 1 - eframe/CHANGELOG.md | 2 +- eframe/src/epi.rs | 25 ++++++++++++++++++++----- eframe/src/native/run.rs | 11 ++++++++++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca1fc513..43835a02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,6 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui-w ## Unreleased ### Added ⭐ -* Added `NativeOptions::hardware_acceleration` to allow opting out of hardware acceleration for less power consumption ([#1681](https://github.com/emilk/egui/pull/1681)). * Added `*_released` & `*_clicked` methods for `PointerState` ([#1582](https://github.com/emilk/egui/pull/1582)). * Added `egui::hex_color!` to create `Color32`'s from hex strings under the `color-hex` feature ([#1596](https://github.com/emilk/egui/pull/1596)). * Optimized painting of filled circles (e.g. for scatter plots) by 10x or more ([#1616](https://github.com/emilk/egui/pull/1616)). diff --git a/eframe/CHANGELOG.md b/eframe/CHANGELOG.md index f514d4f8..eef287df 100644 --- a/eframe/CHANGELOG.md +++ b/eframe/CHANGELOG.md @@ -10,7 +10,7 @@ NOTE: [`egui-winit`](../egui-winit/CHANGELOG.md), [`egui_glium`](../egui_glium/C * Add features "wgpu" and "glow" * Add `NativeOptions::renderer` to switch between the rendering backends * Fix clipboard on Wayland ([#1613](https://github.com/emilk/egui/pull/1613)). - +* Allow running on native without hardware accelerated rendering. Change with `NativeOptions::hardware_acceleration` ([#1681]([#1693](https://github.com/emilk/egui/pull/1693)). ## 0.18.0 - 2022-04-30 * MSRV (Minimum Supported Rust Version) is now `1.60.0` ([#1467](https://github.com/emilk/egui/pull/1467)). diff --git a/eframe/src/epi.rs b/eframe/src/epi.rs index 0142f734..aecb7891 100644 --- a/eframe/src/epi.rs +++ b/eframe/src/epi.rs @@ -148,6 +148,21 @@ pub trait App { fn post_rendering(&mut self, _window_size_px: [u32; 2], _frame: &Frame) {} } +/// Selects the level of hardware graphics acceleration. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum HardwareAcceleration { + /// Require graphics acceleration. + Required, + + /// Prefer graphics acceleration, but fall back to software. + Preferred, + + /// Do NOT use graphics acceleration. + /// + /// On some platforms (MacOS) this is ignored and treated the same as [`Self::Preferred`]. + Off, +} + /// Options controlling the behavior of a native window. /// /// Only a single native window is currently supported. @@ -221,10 +236,10 @@ pub struct NativeOptions { /// `egui` doesn't need the stencil buffer, so the default value is 0. pub stencil_buffer: u8, - /// Use hardware acceleration if available. On macOS, this will possibly - /// use a dedicated GPU which will lead to higher power consumption. - /// The default value is `Some(true)` - pub hardware_acceleration: Option, + /// Specify wether or not hardware acceleration is preferred, required, or not. + /// + /// Default: [`HardwareAcceleration::Preferred`]. + pub hardware_acceleration: HardwareAcceleration, /// What rendering backend to use. pub renderer: Renderer, @@ -248,7 +263,7 @@ impl Default for NativeOptions { multisampling: 0, depth_buffer: 0, stencil_buffer: 0, - hardware_acceleration: Some(true), + hardware_acceleration: HardwareAcceleration::Preferred, renderer: Renderer::default(), } } diff --git a/eframe/src/native/run.rs b/eframe/src/native/run.rs index 74155254..0b8f8245 100644 --- a/eframe/src/native/run.rs +++ b/eframe/src/native/run.rs @@ -15,9 +15,18 @@ fn create_display( glow::Context, ) { crate::profile_function!(); + + use crate::HardwareAcceleration; + + let hardware_acceleration = match native_options.hardware_acceleration { + HardwareAcceleration::Required => Some(true), + HardwareAcceleration::Preferred => None, + HardwareAcceleration::Off => Some(false), + }; + let gl_window = unsafe { glutin::ContextBuilder::new() - .with_hardware_acceleration(native_options.hardware_acceleration) + .with_hardware_acceleration(hardware_acceleration) .with_depth_buffer(native_options.depth_buffer) .with_multisampling(native_options.multisampling) .with_srgb(true)