Skip to content

Commit e7097cc

Browse files
committed
graph-craft: make wgpu-executor properly optional
1 parent 925bd9b commit e7097cc

4 files changed

Lines changed: 26 additions & 11 deletions

File tree

node-graph/graph-craft/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
88
default = ["dealloc_nodes"]
99
serde = ["dep:serde", "graphene-core/serde", "glam/serde", "bezier-rs/serde"]
1010
dealloc_nodes = ["graphene-core/dealloc_nodes"]
11-
wgpu = []
11+
wgpu = ["wgpu-executor"]
1212
tokio = ["dep:tokio"]
1313
wayland = []
1414
loading = ["serde_json", "serde"]
@@ -30,7 +30,7 @@ specta = { workspace = true }
3030
rustc-hash = { workspace = true }
3131
url = { workspace = true }
3232
reqwest = { workspace = true }
33-
wgpu-executor = { workspace = true }
33+
wgpu-executor = { workspace = true, optional = true }
3434

3535
# Optional workspace dependencies
3636
serde = { workspace = true, optional = true }

node-graph/graph-craft/src/wasm_application_io.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use dyn_any::StaticType;
2-
use graphene_core::application_io::SurfaceHandleFrame;
32
use graphene_core::application_io::{ApplicationError, ApplicationIo, ResourceFuture, SurfaceHandle, SurfaceId};
43
#[cfg(target_arch = "wasm32")]
54
use js_sys::{Object, Reflect};
65
use std::collections::HashMap;
76
use std::sync::Arc;
87
#[cfg(target_arch = "wasm32")]
98
use std::sync::atomic::AtomicU64;
9+
use std::sync::atomic::Ordering;
1010
#[cfg(feature = "tokio")]
1111
use tokio::io::AsyncReadExt;
1212
#[cfg(target_arch = "wasm32")]
@@ -17,6 +17,7 @@ use wasm_bindgen::JsValue;
1717
use web_sys::HtmlCanvasElement;
1818
#[cfg(target_arch = "wasm32")]
1919
use web_sys::window;
20+
#[cfg(feature = "wgpu")]
2021
use wgpu_executor::WgpuExecutor;
2122

2223
#[derive(Debug)]
@@ -76,7 +77,7 @@ pub fn wgpu_available() -> Option<bool> {
7677
}
7778
}
7879

79-
match WGPU_AVAILABLE.load(::std::sync::atomic::Ordering::SeqCst) {
80+
match WGPU_AVAILABLE.load(Ordering::SeqCst) {
8081
-1 => None,
8182
0 => Some(false),
8283
_ => Some(true),
@@ -85,7 +86,7 @@ pub fn wgpu_available() -> Option<bool> {
8586

8687
impl WasmApplicationIo {
8788
pub async fn new() -> Self {
88-
#[cfg(target_arch = "wasm32")]
89+
#[cfg(all(feature = "wgpu", target_arch = "wasm32"))]
8990
let executor = if let Some(gpu) = web_sys::window().map(|w| w.navigator().gpu()) {
9091
let request_adapter = || {
9192
let request_adapter = js_sys::Reflect::get(&gpu, &wasm_bindgen::JsValue::from_str("requestAdapter")).ok()?;
@@ -101,9 +102,14 @@ impl WasmApplicationIo {
101102
None
102103
};
103104

104-
#[cfg(not(target_arch = "wasm32"))]
105+
#[cfg(all(feature = "wgpu", not(target_arch = "wasm32")))]
105106
let executor = WgpuExecutor::new().await;
106-
WGPU_AVAILABLE.store(executor.is_some() as i8, ::std::sync::atomic::Ordering::SeqCst);
107+
108+
#[cfg(not(feature = "wgpu"))]
109+
let wgpu_available = false;
110+
#[cfg(feature = "wgpu")]
111+
let wgpu_available = executor.is_some();
112+
WGPU_AVAILABLE.store(wgpu_available as i8, Ordering::SeqCst);
107113

108114
let mut io = Self {
109115
#[cfg(target_arch = "wasm32")]
@@ -121,9 +127,14 @@ impl WasmApplicationIo {
121127
}
122128

123129
pub async fn new_offscreen() -> Self {
130+
#[cfg(feature = "wgpu")]
124131
let executor = WgpuExecutor::new().await;
125132

126-
WGPU_AVAILABLE.store(executor.is_some() as i8, ::std::sync::atomic::Ordering::SeqCst);
133+
#[cfg(not(feature = "wgpu"))]
134+
let wgpu_available = false;
135+
#[cfg(feature = "wgpu")]
136+
let wgpu_available = executor.is_some();
137+
WGPU_AVAILABLE.store(wgpu_available as i8, Ordering::SeqCst);
127138

128139
// Always enable wgpu when running with Tauri
129140
let mut io = Self {
@@ -175,7 +186,7 @@ impl ApplicationIo for WasmApplicationIo {
175186
let document = window().expect("should have a window in this context").document().expect("window should have a document");
176187

177188
let canvas: HtmlCanvasElement = document.create_element("canvas")?.dyn_into::<HtmlCanvasElement>()?;
178-
let id = self.ids.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
189+
let id = self.ids.fetch_add(1, Ordering::SeqCst);
179190
// store the canvas in the global scope so it doesn't get garbage collected
180191
let window = window().expect("should have a window in this context");
181192
let window = Object::from(window);
@@ -299,8 +310,10 @@ impl ApplicationIo for WasmApplicationIo {
299310
}
300311
}
301312

313+
#[cfg(feature = "wgpu")]
302314
pub type WasmSurfaceHandle = SurfaceHandle<wgpu_executor::Window>;
303-
pub type WasmSurfaceHandleFrame = SurfaceHandleFrame<wgpu_executor::Window>;
315+
#[cfg(feature = "wgpu")]
316+
pub type WasmSurfaceHandleFrame = graphene_core::application_io::SurfaceHandleFrame<wgpu_executor::Window>;
304317

305318
#[derive(Clone, Debug, PartialEq, Hash, specta::Type)]
306319
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@@ -330,6 +343,6 @@ impl Default for EditorPreferences {
330343
}
331344
}
332345

333-
unsafe impl dyn_any::StaticType for EditorPreferences {
346+
unsafe impl StaticType for EditorPreferences {
334347
type Static = EditorPreferences;
335348
}

node-graph/gstd/src/wasm_application_io.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use wasm_bindgen::JsCast;
2626
#[cfg(target_arch = "wasm32")]
2727
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement};
2828

29+
#[cfg(feature = "wgpu")]
2930
#[node_macro::node(category("Debug: GPU"))]
3031
async fn create_surface<'a: 'n>(_: impl Ctx, editor: &'a WasmEditorApi) -> Arc<WasmSurfaceHandle> {
3132
Arc::new(editor.application_io.as_ref().unwrap().create_window())

node-graph/interpreted-executor/src/node_registry.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
7373
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => RasterDataTable<Color>]),
7474
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => GraphicGroupTable]),
7575
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => Vec<DVec2>]),
76+
#[cfg(feature = "gpu")]
7677
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => Arc<WasmSurfaceHandle>]),
7778
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => WindowHandle]),
7879
async_node!(graphene_core::memo::MemoNode<_, _>, input: Context, fn_params: [Context => Option<wgpu_executor::WgpuSurface>]),

0 commit comments

Comments
 (0)