Skip to content

Commit

Permalink
feature gate manual gl error checking in glow render
Browse files Browse the repository at this point in the history
  • Loading branch information
coderedart committed Jan 2, 2024
1 parent dc0d534 commit 9a78500
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
5 changes: 4 additions & 1 deletion crates/egui_render_glow/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
[package]
name = "egui_render_glow"
description = "egui rendering backend using glow"
version = "0.6.1"
version = "0.6.2"
repository.workspace = true
edition.workspace = true
license.workspace = true

[features]
check_gl_error = [] # will slow down your app, as error checking synchronizes gl drivers

[dependencies]
tracing = { workspace = true }
bytemuck = { workspace = true }
Expand Down
12 changes: 7 additions & 5 deletions crates/egui_render_glow/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::Arc;

use crate::{glow_error, WebGlConfig};
use crate::{glow_error, GlowConfig};
use glow::*;
use raw_window_handle::RawWindowHandle;
use tracing::*;
Expand Down Expand Up @@ -39,19 +39,21 @@ pub unsafe fn create_glow_wasm32_unknown(
pub unsafe fn create_glow_context(
mut get_proc_address: impl FnMut(&str) -> *const std::ffi::c_void,
handle: RawWindowHandle,
_webgl_config: WebGlConfig,
config: GlowConfig,
) -> Arc<glow::Context> {
// for wasm32-unknown-unknown, use glow's own constructor.
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
let glow_context = create_glow_wasm32_unknown(window_backend, _webgl_config);
let glow_context = create_glow_wasm32_unknown(window_backend, config.webgl_config);
// for non-web and emscripten platforms, just use loader fn
#[cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))]
let glow_context = glow::Context::from_loader_function(|s| get_proc_address(s));

if config.enable_debug {
enable_debug(&glow_context);
}
tracing::debug!("created glow context");
let glow_context = Arc::new(glow_context);
glow_error!(glow_context);
glow_context
Arc::new(glow_context)
}
pub unsafe fn create_program_from_src(
glow_context: &glow::Context,
Expand Down
32 changes: 24 additions & 8 deletions crates/egui_render_glow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ use raw_window_handle::RawWindowHandle;
use std::sync::Arc;
use tracing::{debug, info, warn};

/// opengl error checking flushes all commands and forces synchronization
/// so, we should make this feature gated eventually and maybe use debug callbacks (on desktop atleast)
/// This is a simple macro tha checks for any opengl errors, and logs them.
/// But this flushes all commands and forces synchronization with driver, which will slow down your program.
/// So, by default, we only check for errors if `check_gl_error` feature is enabled. otherwise, this does nothing.
///
/// OpenGL also supports "debug callbacks" feature, where it will call our callback when it has any logs. see [GlowConfig::enable_debug] for that
#[macro_export]
macro_rules! glow_error {
($glow_context: ident) => {
let error_code = $glow_context.get_error();
if error_code != glow::NO_ERROR {
tracing::error!("glow error: {} at line {}", error_code, line!());
#[cfg(feature = "check_gl_error")]
{
let error_code = $glow_context.get_error();
if error_code != glow::NO_ERROR {
tracing::error!("glow error: {} at line {}", error_code, line!());
}
}
};
}
Expand Down Expand Up @@ -83,12 +89,22 @@ impl Drop for GlowBackend {
}
}

#[derive(Debug, Default)]
/// Configuration for Glow context when you are creating one
#[derive(Debug)]
pub struct GlowConfig {
pub webgl_config: WebGlConfig,
/// This will set the debug callbacks, which will be used by gl drivers to log any gl errors via [tracing].
/// default is true, as it can be helpful to figure out any errors.
pub enable_debug: bool,
}

impl Default for GlowConfig {
fn default() -> Self {
Self {
webgl_config: Default::default(),
enable_debug: true,
}
}
}
impl GlowBackend {
pub fn new(
config: GlowConfig,
Expand All @@ -97,7 +113,7 @@ impl GlowBackend {
framebuffer_size: [u32; 2],
) -> Self {
let glow_context: Arc<glow::Context> =
unsafe { create_glow_context(get_proc_address, handle, config.webgl_config) };
unsafe { create_glow_context(get_proc_address, handle, config) };

if glow_context.supported_extensions().contains("EXT_sRGB")
|| glow_context.supported_extensions().contains("GL_EXT_sRGB")
Expand Down
9 changes: 3 additions & 6 deletions crates/egui_window_glfw_passthrough/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ use glfw::WindowHint;
use std::sync::mpsc::Receiver;
use tracing::info;
/// This is the window backend for egui using [`glfw`]
/// Most of the startup configuration is done inside [`default_glfw_callback()`] and [`default_window_callback()`]
/// These are passed to the `new` function using [`GlfwConfig`].
/// You can configure most of it at startup using [`GlfwConfig`].
///
/// https://www.glfw.org/docs/3.3/intro_guide.html#coordinate_systems
/// <https://www.glfw.org/docs/3.3/intro_guide.html#coordinate_systems>
/// So, there are two different units used when referring to size/position.
/// 1. physical size. This is size in raw physical pixels of the framebuffer.
/// 2. virtual screen coordinates (units). These may or may not be the same size as the pixels.
Expand Down Expand Up @@ -67,13 +66,11 @@ impl Drop for GlfwBackend {
}
}
/// Signature of Glfw callback function inside [`GlfwConfig`]
/// we provide a default callback for common usecases -> [`default_glfw_callback()`]
pub type GlfwCallback = Box<dyn FnOnce(&mut Glfw)>;
/// This is the signature for window callback inside new function of [`GlfwBackend`]
pub type WindowCallback = Box<dyn FnOnce(&mut glfw::Window)>;

/// The configuration struct for Glfw Backend
/// passed in to [`WindowBackend::new()`] of [`GlfwBackend`]
/// The configuration struct for Glfw Backend creation
pub struct GlfwConfig {
/// title of the window
pub window_title: String,
Expand Down

0 comments on commit 9a78500

Please sign in to comment.