1#[cfg(not(target_arch = "wasm32"))]
2use once_cell::sync::OnceCell;
3#[cfg(target_arch = "wasm32")]
4use once_cell::unsync::OnceCell;
5#[cfg(target_arch = "wasm32")]
6use runmat_thread_local::runmat_thread_local;
7#[cfg(target_arch = "wasm32")]
8use std::cell::RefCell;
9use std::sync::Arc;
10
11#[derive(Clone)]
13pub struct SharedWgpuContext {
14 pub instance: Arc<wgpu::Instance>,
15 pub device: Arc<wgpu::Device>,
16 pub queue: Arc<wgpu::Queue>,
17 pub adapter: Arc<wgpu::Adapter>,
18 pub adapter_info: wgpu::AdapterInfo,
19 pub limits: wgpu::Limits,
20 pub features: wgpu::Features,
21}
22
23#[cfg(not(target_arch = "wasm32"))]
24static GLOBAL_CONTEXT: OnceCell<SharedWgpuContext> = OnceCell::new();
25
26#[cfg(target_arch = "wasm32")]
27runmat_thread_local! {
28 static GLOBAL_CONTEXT: RefCell<OnceCell<SharedWgpuContext>> = RefCell::new(OnceCell::new());
29}
30
31pub fn install_shared_wgpu_context(context: SharedWgpuContext) {
33 #[cfg(not(target_arch = "wasm32"))]
34 let _ = GLOBAL_CONTEXT.set(context);
35 #[cfg(target_arch = "wasm32")]
36 GLOBAL_CONTEXT.with(|cell| {
37 let _ = cell.borrow_mut().set(context);
38 });
39}
40
41pub fn shared_wgpu_context() -> Option<SharedWgpuContext> {
43 #[cfg(not(target_arch = "wasm32"))]
44 {
45 GLOBAL_CONTEXT.get().cloned()
46 }
47 #[cfg(target_arch = "wasm32")]
48 {
49 GLOBAL_CONTEXT.with(|cell| cell.borrow().get().cloned())
50 }
51}