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

Add custom shader_version on glow renderer #1993

Merged
merged 20 commits into from
Sep 6, 2022
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
2 changes: 1 addition & 1 deletion crates/eframe/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ NOTE: [`egui-winit`](../egui-winit/CHANGELOG.md), [`egui_glium`](../egui_glium/C
* Added `NativeOptions::event_loop_builder` hook for apps to change platform specific event loop options ([#1952](https://github.com/emilk/egui/pull/1952)).
* Enabled deferred render state initialization to support Android ([#1952](https://github.com/emilk/egui/pull/1952)).
* Allow empty textures with the glow renderer.

* Added `shader_version` to `NativeOptions` for cross compilling support on different target OpenGL | ES versions (on native `glow` renderer only) ([#1993](https://github.com/emilk/egui/pull/1993))..

## 0.19.0 - 2022-08-20
* MSRV (Minimum Supported Rust Version) is now `1.61.0` ([#1846](https://github.com/emilk/egui/pull/1846)).
Expand Down
9 changes: 9 additions & 0 deletions crates/eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,13 @@ pub struct NativeOptions {
///
/// Note: A [`NativeOptions`] clone will not include any `event_loop_builder` hook.
pub event_loop_builder: Option<EventLoopBuilderHook>,

#[cfg(feature = "glow")]
/// Needed for cross compiling for VirtualBox VMSVGA driver with OpenGL ES 2.0 and OpenGL 2.1 which doesn't support SRGB texture.
/// See <https://github.com/emilk/egui/pull/1993>.
///
/// For OpenGL ES 2.0: set this to [`egui_glow::ShaderVersion::Es100`] to solve blank texture problem (by using the "fallback shader").
pub shader_version: Option<egui_glow::ShaderVersion>,
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down Expand Up @@ -350,6 +357,8 @@ impl Default for NativeOptions {
default_theme: Theme::Dark,
run_and_return: true,
event_loop_builder: None,
#[cfg(feature = "glow")]
shader_version: None,
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,9 @@ mod glow_integration {
);
let gl = Arc::new(gl);

let painter = egui_glow::Painter::new(gl.clone(), None, "")
.unwrap_or_else(|error| panic!("some OpenGL error occurred {}\n", error));
let painter =
egui_glow::Painter::new(gl.clone(), None, "", self.native_options.shader_version)
.unwrap_or_else(|error| panic!("some OpenGL error occurred {}\n", error));

let system_theme = self.native_options.system_theme();
let mut integration = epi_integration::EpiIntegration::new(
Expand Down
2 changes: 1 addition & 1 deletion crates/eframe/src/web/web_glow_painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl WebPainter {
let gl = std::sync::Arc::new(gl);

let dimension = [canvas.width() as i32, canvas.height() as i32];
let painter = egui_glow::Painter::new(gl, Some(dimension), shader_prefix)
let painter = egui_glow::Painter::new(gl, Some(dimension), shader_prefix, None)
.map_err(|error| format!("Error starting glow painter: {}", error))?;

Ok(Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_glow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to the `egui_glow` integration will be noted in this file.

## Unreleased
* Allow empty textures.

* Added `shader_version` variable on `EguiGlow::new` for easier cross compilling on different OpenGL | ES targets ([#1993](https://github.com/emilk/egui/pull/1993)).

## 0.19.0 - 2022-08-20
* MSRV (Minimum Supported Rust Version) is now `1.61.0` ([#1846](https://github.com/emilk/egui/pull/1846)).
Expand Down
2 changes: 1 addition & 1 deletion crates/egui_glow/examples/pure_glow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
let (gl_window, gl) = create_display(&event_loop);
let gl = std::sync::Arc::new(gl);

let mut egui_glow = egui_glow::EguiGlow::new(&event_loop, gl.clone());
let mut egui_glow = egui_glow::EguiGlow::new(&event_loop, gl.clone(), None);

event_loop.run(move |event, _, control_flow| {
let mut redraw = || {
Expand Down
16 changes: 8 additions & 8 deletions crates/egui_glow/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,19 @@ impl Painter {
gl: Arc<glow::Context>,
pp_fb_extent: Option<[i32; 2]>,
shader_prefix: &str,
shader_version: Option<ShaderVersion>,
) -> Result<Painter, String> {
crate::profile_function!();
crate::check_for_gl_error_even_in_release!(&gl, "before Painter::new");

let max_texture_side = unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_SIZE) } as usize;

let shader_version = ShaderVersion::get(&gl);
let is_webgl_1 = shader_version == ShaderVersion::Es100;
let header = shader_version.version_declaration();
let shader = shader_version.unwrap_or_else(|| ShaderVersion::get(&gl));
let is_webgl_1 = shader == ShaderVersion::Es100;
let header = shader.version_declaration();
tracing::debug!("Shader header: {:?}.", header);
let srgb_support = gl.supported_extensions().contains("EXT_sRGB");

let (post_process, srgb_support_define) = match (shader_version, srgb_support) {
let (post_process, srgb_support_define) = match (shader, srgb_support) {
// WebGL2 support sRGB default
(ShaderVersion::Es300, _) | (ShaderVersion::Es100, true) => unsafe {
// Add sRGB support marker for fragment shader
Expand Down Expand Up @@ -155,7 +155,7 @@ impl Painter {
"{}\n{}\n{}\n{}",
header,
shader_prefix,
if shader_version.is_new_shader_interface() {
if shader.is_new_shader_interface() {
"#define NEW_SHADER_INTERFACE\n"
} else {
""
Expand All @@ -171,7 +171,7 @@ impl Painter {
header,
shader_prefix,
srgb_support_define,
if shader_version.is_new_shader_interface() {
if shader.is_new_shader_interface() {
"#define NEW_SHADER_INTERFACE\n"
} else {
""
Expand Down Expand Up @@ -233,7 +233,7 @@ impl Painter {
u_screen_size,
u_sampler,
is_webgl_1,
is_embedded: matches!(shader_version, ShaderVersion::Es100 | ShaderVersion::Es300),
is_embedded: matches!(shader, ShaderVersion::Es100 | ShaderVersion::Es300),
vao,
srgb_support,
post_process,
Expand Down
8 changes: 5 additions & 3 deletions crates/egui_glow/src/winit.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::shader_version::ShaderVersion;
pub use egui_winit;
pub use egui_winit::EventResponse;

use egui_winit::winit;
pub use egui_winit::EventResponse;

/// Use [`egui`] from a [`glow`] app based on [`winit`].
pub struct EguiGlow {
Expand All @@ -14,11 +14,13 @@ pub struct EguiGlow {
}

impl EguiGlow {
/// For automatic shader version detection set `shader_version` to `None`.
pub fn new<E>(
event_loop: &winit::event_loop::EventLoopWindowTarget<E>,
gl: std::sync::Arc<glow::Context>,
shader_version: Option<ShaderVersion>,
) -> Self {
let painter = crate::Painter::new(gl, None, "")
let painter = crate::Painter::new(gl, None, "", shader_version)
.map_err(|error| {
tracing::error!("error occurred in initializing painter:\n{}", error);
})
Expand Down
1 change: 1 addition & 0 deletions examples/retained_image/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn main() {
initial_window_size: Some(egui::vec2(500.0, 900.0)),
..Default::default()
};

eframe::run_native(
"Show an image with eframe/egui",
options,
Expand Down