Skip to content

Commit

Permalink
Better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 14, 2023
1 parent 999a54f commit f9e6860
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 27 deletions.
5 changes: 5 additions & 0 deletions crates/eframe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ pub enum Error {
#[error("Found no glutin configs matching the template: {0:?}. Error: {1:?}")]
NoGlutinConfigs(glutin::config::ConfigTemplate, Box<dyn std::error::Error>),

/// An error from [`glutin`] when using [`glow`].
#[cfg(feature = "glow")]
#[error("egui_glow: {0}")]
OpenGL(#[from] egui_glow::PainterError),

/// An error from [`wgpu`].
#[cfg(feature = "wgpu")]
#[error("WGPU error: {0}")]
Expand Down
49 changes: 27 additions & 22 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1021,17 +1021,18 @@ mod glow_integration {
.get_mut(&viewport_id)
.expect("viewport doesn't exist");

let window = viewport.window.get_or_insert_with(|| {
let window = if let Some(window) = &mut viewport.window {
window
} else {
log::trace!("Window doesn't exist yet. Creating one now with finalize_window");
Rc::new(
glutin_winit::finalize_window(
viewport
.window
.insert(Rc::new(glutin_winit::finalize_window(
event_loop,
create_winit_window_builder(&viewport.builder),
&self.gl_config,
)
.expect("failed to finalize glutin window"),
)
});
)?))
};

{
// surface attributes
Expand Down Expand Up @@ -1330,8 +1331,7 @@ mod glow_integration {
}))
};

let painter = egui_glow::Painter::new(gl, "", native_options.shader_version)
.unwrap_or_else(|err| panic!("An OpenGL error occurred: {err}\n"));
let painter = egui_glow::Painter::new(gl, "", native_options.shader_version)?;

Ok((glutin_window_context, painter))
}
Expand Down Expand Up @@ -1821,22 +1821,27 @@ mod wgpu_integration {

let viewport_id = self.ids.this;

if let Ok(new_window) = create_winit_window_builder(&self.builder).build(event_loop) {
windows_id.insert(new_window.id(), viewport_id);
match create_winit_window_builder(&self.builder).build(event_loop) {
Ok(new_window) => {
windows_id.insert(new_window.id(), viewport_id);

if let Err(err) =
pollster::block_on(painter.set_window(viewport_id, Some(&new_window)))
{
log::error!("on set_window: viewport_id {viewport_id:?} {err}");
}
if let Err(err) =
pollster::block_on(painter.set_window(viewport_id, Some(&new_window)))
{
log::error!("on set_window: viewport_id {viewport_id:?} {err}");
}

self.egui_winit = Some(egui_winit::State::new(
event_loop,
Some(new_window.scale_factor() as f32),
painter.max_texture_side(),
));
self.egui_winit = Some(egui_winit::State::new(
event_loop,
Some(new_window.scale_factor() as f32),
painter.max_texture_side(),
));

self.window = Some(Rc::new(new_window));
self.window = Some(Rc::new(new_window));
}
Err(err) => {
log::error!("Failed to create window: {err}");
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ mod texture_loader;
use std::borrow::Cow;
use std::fmt::Debug;
use std::ops::Deref;
use std::{error::Error as StdError, fmt::Display, sync::Arc};
use std::{fmt::Display, sync::Arc};

use ahash::HashMap;

Expand Down Expand Up @@ -118,7 +118,7 @@ impl Display for LoadError {
}
}

impl StdError for LoadError {}
impl std::error::Error for LoadError {}

pub type Result<T, E = LoadError> = std::result::Result<T, E>;

Expand Down
2 changes: 1 addition & 1 deletion crates/egui_glow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

pub mod painter;
pub use glow;
pub use painter::{CallbackFn, Painter};
pub use painter::{CallbackFn, Painter, PainterError};
mod misc_util;
mod shader_version;
mod vao;
Expand Down
22 changes: 20 additions & 2 deletions crates/egui_glow/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ impl TextureFilterExt for egui::TextureFilter {
}
}

#[derive(Debug)]
pub struct PainterError(String);

impl std::error::Error for PainterError {}

impl std::fmt::Display for PainterError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "OpenGL: {}", self.0)
}
}

impl From<String> for PainterError {
#[inline]
fn from(value: String) -> Self {
Self(value)
}
}

/// An OpenGL painter using [`glow`].
///
/// This is responsible for painting egui and managing egui textures.
Expand Down Expand Up @@ -103,7 +121,7 @@ impl Painter {
gl: Arc<glow::Context>,
shader_prefix: &str,
shader_version: Option<ShaderVersion>,
) -> Result<Painter, String> {
) -> Result<Painter, PainterError> {
crate::profile_function!();
crate::check_for_gl_error_even_in_release!(&gl, "before Painter::new");

Expand All @@ -121,7 +139,7 @@ impl Painter {
if gl.version().major < 2 {
// this checks on desktop that we are not using opengl 1.1 microsoft sw rendering context.
// ShaderVersion::get fn will segfault due to SHADING_LANGUAGE_VERSION (added in gl2.0)
return Err("egui_glow requires opengl 2.0+. ".to_owned());
return Err(PainterError("egui_glow requires opengl 2.0+. ".to_owned()));
}

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

0 comments on commit f9e6860

Please sign in to comment.