From 7ac4f3e1e14335290e505a5799a0b88717474678 Mon Sep 17 00:00:00 2001 From: Sebastian Urban Date: Wed, 18 May 2022 12:12:15 +0200 Subject: [PATCH] Take Glow context using Arc. This allows usage with a Glow context that is passed between threads. --- src/core/context.rs | 5 +++-- src/window/canvas.rs | 3 ++- src/window/glutin_window.rs | 2 +- src/window/headless.rs | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/core/context.rs b/src/core/context.rs index a40aaa8e2..d4ba9f816 100644 --- a/src/core/context.rs +++ b/src/core/context.rs @@ -2,6 +2,7 @@ use super::*; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; +use std::sync::Arc; #[doc(hidden)] pub use crate::context::HasContext; @@ -13,7 +14,7 @@ pub use crate::context::HasContext; /// #[derive(Clone)] pub struct Context { - context: Rc, + context: Arc, pub(super) vao: crate::context::VertexArray, programs: Rc>>, effects: Rc>>, @@ -29,7 +30,7 @@ impl Context { /// Since the content in the [context](crate::context) module is just a re-export of [glow](https://crates.io/crates/glow), /// you can also call this method with a reference counter to a glow context created using glow and not the re-export in [context](crate::context). /// - pub fn from_gl_context(context: Rc) -> ThreeDResult { + pub fn from_gl_context(context: Arc) -> ThreeDResult { #[cfg(not(target_arch = "wasm32"))] unsafe { // Enable seamless cube map textures diff --git a/src/window/canvas.rs b/src/window/canvas.rs index 7a323e104..e2a1bdc95 100644 --- a/src/window/canvas.rs +++ b/src/window/canvas.rs @@ -3,6 +3,7 @@ use crate::window::*; use serde::Serialize; use std::cell::RefCell; use std::rc::Rc; +use std::sync::Arc; use wasm_bindgen::prelude::*; use wasm_bindgen::JsCast; use web_sys::WebGl2RenderingContext; @@ -145,7 +146,7 @@ impl Window { .get_extension("OES_texture_float_linear") .map_err(|e| CanvasError::OESTextureFloatNotSupported(format!(": {:?}", e)))?; - crate::core::Context::from_gl_context(Rc::new( + crate::core::Context::from_gl_context(Arc::new( crate::context::Context::from_webgl2_context(context), )) } diff --git a/src/window/glutin_window.rs b/src/window/glutin_window.rs index bfdf75138..6e5b3b561 100644 --- a/src/window/glutin_window.rs +++ b/src/window/glutin_window.rs @@ -52,7 +52,7 @@ impl Window { Ok(Window { windowed_context, event_loop, - gl: crate::core::Context::from_gl_context(std::rc::Rc::new(context))?, + gl: crate::core::Context::from_gl_context(std::sync::Arc::new(context))?, }) } diff --git a/src/window/headless.rs b/src/window/headless.rs index 6b9d6ca2d..79689cc44 100644 --- a/src/window/headless.rs +++ b/src/window/headless.rs @@ -14,7 +14,7 @@ impl Context { let cb = ContextBuilder::new(); let (headless_context, _el) = build_context(cb).unwrap(); let headless_context = unsafe { headless_context.make_current().unwrap() }; - let mut c = Self::from_gl_context(std::rc::Rc::new(unsafe { + let mut c = Self::from_gl_context(std::sync::Arc::new(unsafe { crate::context::Context::from_loader_function(|s| { headless_context.get_proc_address(s) as *const _ })