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

Take Glow context using Arc. #1640

Merged
merged 2 commits into from
May 22, 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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ opt-level = 2 # fast and small wasm, basically same as `opt-level = 's'`
# opt-level = 3 # unecessarily large wasm for no performance gain

# debug = true # include debug symbols, useful when profiling wasm

[patch.crates-io]
three-d = { git = "https://github.com/asny/three-d.git", rev = "7ac4f3e1e14335290e505a5799a0b88717474678" }
6 changes: 3 additions & 3 deletions eframe/src/epi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct CreationContext<'s> {
/// The [`glow::Context`] allows you to initialize OpenGL resources (e.g. shaders) that
/// you might want to use later from a [`egui::PaintCallback`].
#[cfg(feature = "glow")]
pub gl: Option<std::rc::Rc<glow::Context>>,
pub gl: Option<std::sync::Arc<glow::Context>>,
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -334,7 +334,7 @@ pub struct Frame {
/// A reference to the underlying [`glow`] (OpenGL) context.
#[cfg(feature = "glow")]
#[doc(hidden)]
pub gl: Option<std::rc::Rc<glow::Context>>,
pub gl: Option<std::sync::Arc<glow::Context>>,
}

impl Frame {
Expand Down Expand Up @@ -371,7 +371,7 @@ impl Frame {
/// To get a [`glow`] context you need to compile with the `glow` feature flag,
/// and run eframe with the glow backend.
#[cfg(feature = "glow")]
pub fn gl(&self) -> Option<&std::rc::Rc<glow::Context>> {
pub fn gl(&self) -> Option<&std::sync::Arc<glow::Context>> {
self.gl.as_ref()
}

Expand Down
2 changes: 1 addition & 1 deletion eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl EpiIntegration {
max_texture_side: usize,
window: &winit::window::Window,
storage: Option<Box<dyn epi::Storage>>,
#[cfg(feature = "glow")] gl: Option<std::rc::Rc<glow::Context>>,
#[cfg(feature = "glow")] gl: Option<std::sync::Arc<glow::Context>>,
) -> Self {
let egui_ctx = egui::Context::default();

Expand Down
2 changes: 1 addition & 1 deletion eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn run_glow(
let window_builder =
epi_integration::window_builder(native_options, &window_settings).with_title(app_name);
let (gl_window, gl) = create_display(native_options, window_builder, &event_loop);
let gl = std::rc::Rc::new(gl);
let gl = std::sync::Arc::new(gl);

let mut painter = egui_glow::Painter::new(gl.clone(), None, "")
.unwrap_or_else(|error| panic!("some OpenGL error occurred {}\n", error));
Expand Down
4 changes: 2 additions & 2 deletions eframe/src/web/glow_wrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl WrappedGlowPainter {
let canvas = super::canvas_element_or_die(canvas_id);

let (gl, shader_prefix) = init_glow_context_from_canvas(&canvas)?;
let gl = std::rc::Rc::new(gl);
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)
Expand All @@ -32,7 +32,7 @@ impl WrappedGlowPainter {
}

impl WrappedGlowPainter {
pub fn gl(&self) -> &std::rc::Rc<glow::Context> {
pub fn gl(&self) -> &std::sync::Arc<glow::Context> {
self.painter.gl()
}

Expand Down
2 changes: 1 addition & 1 deletion egui_glow/examples/pure_glow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {

let event_loop = glutin::event_loop::EventLoop::with_user_event();
let (gl_window, gl) = create_display(&event_loop);
let gl = std::rc::Rc::new(gl);
let gl = std::sync::Arc::new(gl);

let mut egui_glow = egui_glow::EguiGlow::new(gl_window.window(), gl.clone());

Expand Down
8 changes: 4 additions & 4 deletions egui_glow/src/painter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(unsafe_code)]

use std::{collections::HashMap, rc::Rc};
use std::{collections::HashMap, sync::Arc};

use egui::{
emath::Rect,
Expand Down Expand Up @@ -49,7 +49,7 @@ impl TextureFilter {
/// This struct must be destroyed with [`Painter::destroy`] before dropping, to ensure OpenGL
/// objects have been properly deleted and are not leaked.
pub struct Painter {
gl: Rc<glow::Context>,
gl: Arc<glow::Context>,

max_texture_side: usize,

Expand Down Expand Up @@ -91,7 +91,7 @@ impl Painter {
/// * failed to create postprocess on webgl with `sRGB` support
/// * failed to create buffer
pub fn new(
gl: Rc<glow::Context>,
gl: Arc<glow::Context>,
pp_fb_extent: Option<[i32; 2]>,
shader_prefix: &str,
) -> Result<Painter, String> {
Expand Down Expand Up @@ -229,7 +229,7 @@ impl Painter {
}

/// Access the shared glow context.
pub fn gl(&self) -> &std::rc::Rc<glow::Context> {
pub fn gl(&self) -> &Arc<glow::Context> {
&self.gl
}

Expand Down
4 changes: 2 additions & 2 deletions egui_glow/src/post_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use glow::HasContext as _;
/// Uses a framebuffer to render everything in linear color space and convert it back to `sRGB`
/// in a separate "post processing" step
pub(crate) struct PostProcess {
gl: std::rc::Rc<glow::Context>,
gl: std::sync::Arc<glow::Context>,
pos_buffer: glow::Buffer,
index_buffer: glow::Buffer,
vao: crate::vao::VertexArrayObject,
Expand All @@ -21,7 +21,7 @@ pub(crate) struct PostProcess {

impl PostProcess {
pub(crate) unsafe fn new(
gl: std::rc::Rc<glow::Context>,
gl: std::sync::Arc<glow::Context>,
shader_prefix: &str,
is_webgl_1: bool,
[width, height]: [i32; 2],
Expand Down
2 changes: 1 addition & 1 deletion egui_glow/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct EguiGlow {
}

impl EguiGlow {
pub fn new(window: &winit::window::Window, gl: std::rc::Rc<glow::Context>) -> Self {
pub fn new(window: &winit::window::Window, gl: std::sync::Arc<glow::Context>) -> Self {
let painter = crate::Painter::new(gl, None, "")
.map_err(|error| {
tracing::error!("error occurred in initializing painter:\n{}", error);
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_3d_three-d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ publish = false
eframe = { path = "../../eframe", features = ["glow"] }
egui_glow = { path = "../../egui_glow" }
glow = "0.11"
three-d = { version = "0.11", default-features = false }
three-d = { version = "0.12", default-features = false }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use git = "https://github.com/asny/three-d.git", rev = "7ac4f3e1e14335290e505a5799a0b88717474678" here to make it easier for others to copy-paste this example without getting surprise errors!

23 changes: 8 additions & 15 deletions examples/custom_3d_three-d/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl MyApp {
/// to the [`egui::PaintCallback`] because [`three_d::Context`] isn't `Send+Sync`, which
/// [`egui::PaintCallback`] is.
fn with_three_d_context<R>(
gl: &std::rc::Rc<glow::Context>,
gl: &std::sync::Arc<glow::Context>,
f: impl FnOnce(&three_d::Context) -> R,
) -> R {
use std::cell::RefCell;
Expand Down Expand Up @@ -111,15 +111,12 @@ fn paint_with_three_d(three_d: &three_d::Context, info: &egui::PaintCallbackInfo

// Respect the egui clip region (e.g. if we are inside an `egui::ScrollArea`).
let clip_rect = info.clip_rect_in_pixels();
let render_states = RenderStates {
clip: Clip::Enabled {
x: clip_rect.left_px.round() as _,
y: clip_rect.from_bottom_px.round() as _,
width: clip_rect.width_px.round() as _,
height: clip_rect.height_px.round() as _,
},
..Default::default()
};
three_d.set_scissor(ScissorBox {
x: clip_rect.left_px.round() as _,
y: clip_rect.from_bottom_px.round() as _,
width: clip_rect.width_px.round() as _,
height: clip_rect.height_px.round() as _,
});

let camera = Camera::new_perspective(
three_d,
Expand Down Expand Up @@ -150,11 +147,7 @@ fn paint_with_three_d(three_d: &three_d::Context, info: &egui::PaintCallbackInfo
..Default::default()
};

let material = ColorMaterial {
render_states,
..Default::default()
};
let mut model = Model::new_with_material(three_d, &cpu_mesh, material).unwrap();
let mut model = Model::new(three_d, &cpu_mesh).unwrap();

// Set the current transformation of the triangle
model.set_transformation(Mat4::from_angle_y(radians(angle)));
Expand Down