Skip to content

Commit b251067

Browse files
authored
Take Glow context using Arc. (#1640)
This allows usage with a Glow context that is passed between threads.
1 parent a291542 commit b251067

File tree

11 files changed

+27
-31
lines changed

11 files changed

+27
-31
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ opt-level = 2 # fast and small wasm, basically same as `opt-level = 's'`
3737
# opt-level = 3 # unecessarily large wasm for no performance gain
3838

3939
# debug = true # include debug symbols, useful when profiling wasm
40+
41+
[patch.crates-io]
42+
three-d = { git = "https://github.com/asny/three-d.git", rev = "7ac4f3e1e14335290e505a5799a0b88717474678" }

eframe/src/epi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct CreationContext<'s> {
2828
/// The [`glow::Context`] allows you to initialize OpenGL resources (e.g. shaders) that
2929
/// you might want to use later from a [`egui::PaintCallback`].
3030
#[cfg(feature = "glow")]
31-
pub gl: Option<std::rc::Rc<glow::Context>>,
31+
pub gl: Option<std::sync::Arc<glow::Context>>,
3232
}
3333

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

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

eframe/src/native/epi_integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl EpiIntegration {
185185
max_texture_side: usize,
186186
window: &winit::window::Window,
187187
storage: Option<Box<dyn epi::Storage>>,
188-
#[cfg(feature = "glow")] gl: Option<std::rc::Rc<glow::Context>>,
188+
#[cfg(feature = "glow")] gl: Option<std::sync::Arc<glow::Context>>,
189189
) -> Self {
190190
let egui_ctx = egui::Context::default();
191191

eframe/src/native/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn run_glow(
5151
let window_builder =
5252
epi_integration::window_builder(native_options, &window_settings).with_title(app_name);
5353
let (gl_window, gl) = create_display(native_options, window_builder, &event_loop);
54-
let gl = std::rc::Rc::new(gl);
54+
let gl = std::sync::Arc::new(gl);
5555

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

eframe/src/web/glow_wrapping.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl WrappedGlowPainter {
1717
let canvas = super::canvas_element_or_die(canvas_id);
1818

1919
let (gl, shader_prefix) = init_glow_context_from_canvas(&canvas)?;
20-
let gl = std::rc::Rc::new(gl);
20+
let gl = std::sync::Arc::new(gl);
2121

2222
let dimension = [canvas.width() as i32, canvas.height() as i32];
2323
let painter = egui_glow::Painter::new(gl, Some(dimension), shader_prefix)
@@ -32,7 +32,7 @@ impl WrappedGlowPainter {
3232
}
3333

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

egui_glow/examples/pure_glow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88

99
let event_loop = glutin::event_loop::EventLoop::with_user_event();
1010
let (gl_window, gl) = create_display(&event_loop);
11-
let gl = std::rc::Rc::new(gl);
11+
let gl = std::sync::Arc::new(gl);
1212

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

egui_glow/src/painter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(unsafe_code)]
22

3-
use std::{collections::HashMap, rc::Rc};
3+
use std::{collections::HashMap, sync::Arc};
44

55
use egui::{
66
emath::Rect,
@@ -42,7 +42,7 @@ impl TextureFilterExt for TextureFilter {
4242
/// This struct must be destroyed with [`Painter::destroy`] before dropping, to ensure OpenGL
4343
/// objects have been properly deleted and are not leaked.
4444
pub struct Painter {
45-
gl: Rc<glow::Context>,
45+
gl: Arc<glow::Context>,
4646

4747
max_texture_side: usize,
4848

@@ -82,7 +82,7 @@ impl Painter {
8282
/// * failed to create postprocess on webgl with `sRGB` support
8383
/// * failed to create buffer
8484
pub fn new(
85-
gl: Rc<glow::Context>,
85+
gl: Arc<glow::Context>,
8686
pp_fb_extent: Option<[i32; 2]>,
8787
shader_prefix: &str,
8888
) -> Result<Painter, String> {
@@ -219,7 +219,7 @@ impl Painter {
219219
}
220220

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

egui_glow/src/post_process.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use glow::HasContext as _;
77
/// Uses a framebuffer to render everything in linear color space and convert it back to `sRGB`
88
/// in a separate "post processing" step
99
pub(crate) struct PostProcess {
10-
gl: std::rc::Rc<glow::Context>,
10+
gl: std::sync::Arc<glow::Context>,
1111
pos_buffer: glow::Buffer,
1212
index_buffer: glow::Buffer,
1313
vao: crate::vao::VertexArrayObject,
@@ -21,7 +21,7 @@ pub(crate) struct PostProcess {
2121

2222
impl PostProcess {
2323
pub(crate) unsafe fn new(
24-
gl: std::rc::Rc<glow::Context>,
24+
gl: std::sync::Arc<glow::Context>,
2525
shader_prefix: &str,
2626
is_webgl_1: bool,
2727
[width, height]: [i32; 2],

egui_glow/src/winit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct EguiGlow {
1212
}
1313

1414
impl EguiGlow {
15-
pub fn new(window: &winit::window::Window, gl: std::rc::Rc<glow::Context>) -> Self {
15+
pub fn new(window: &winit::window::Window, gl: std::sync::Arc<glow::Context>) -> Self {
1616
let painter = crate::Painter::new(gl, None, "")
1717
.map_err(|error| {
1818
tracing::error!("error occurred in initializing painter:\n{}", error);

examples/custom_3d_three-d/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ publish = false
1212
eframe = { path = "../../eframe", features = ["glow"] }
1313
egui_glow = { path = "../../egui_glow" }
1414
glow = "0.11"
15-
three-d = { version = "0.11", default-features = false }
15+
three-d = { version = "0.12", default-features = false }

0 commit comments

Comments
 (0)