Skip to main content

trueno/backends/gpu/
runtime.rs

1//! Cross-platform async runtime helpers for GPU operations.
2//!
3//! - Native: Uses `pollster::block_on` for sync wrappers
4//! - WASM: Sync wrappers unavailable; use async methods directly
5
6/// Block on async code (native only).
7///
8/// On WASM, this function is not available - use async methods directly
9/// with `wasm_bindgen_futures::spawn_local` or await.
10#[cfg(all(feature = "gpu", not(target_arch = "wasm32")))]
11pub fn block_on<F: std::future::Future>(f: F) -> F::Output {
12    pollster::block_on(f)
13}
14
15/// Check if sync GPU operations are available.
16///
17/// Returns `true` on native platforms, `false` on WASM.
18#[cfg(not(target_arch = "wasm32"))]
19pub const fn sync_available() -> bool {
20    true
21}
22
23#[cfg(target_arch = "wasm32")]
24pub const fn sync_available() -> bool {
25    false
26}
27
28/// Spawn async task for WASM.
29#[cfg(all(feature = "gpu-wasm", target_arch = "wasm32"))]
30pub fn spawn_local<F>(f: F)
31where
32    F: std::future::Future<Output = ()> + 'static,
33{
34    wasm_bindgen_futures::spawn_local(f);
35}
36
37/// Log to console (WASM).
38#[cfg(all(feature = "gpu-wasm", target_arch = "wasm32"))]
39pub fn console_log(s: &str) {
40    web_sys::console::log_1(&s.into());
41}
42
43#[cfg(not(target_arch = "wasm32"))]
44pub fn console_log(s: &str) {
45    eprintln!("{}", s);
46}