use std::time::Duration;
#[cfg(any(target_arch = "wasm32", test))]
mod frame;
#[cfg(not(target_arch = "wasm32"))]
mod thread;
#[cfg(target_arch = "wasm32")]
pub use frame::Driver;
#[cfg(not(target_arch = "wasm32"))]
pub use thread::Driver;
pub const CAN_SPAWN_THREADS: bool = cfg!(not(target_arch = "wasm32"));
use crate::snapshot::Snapshot;
use std::sync::{Arc, Mutex};
pub const PUMP_BUDGET_MS: f64 = 6.0;
pub const RUN_TO_PUBLISH_INTERVAL: Duration = Duration::from_millis(100);
#[cfg(target_arch = "wasm32")]
pub const MAX_VIEW_BUDGET_MS: f32 = PUMP_BUDGET_MS as f32;
#[cfg(not(target_arch = "wasm32"))]
pub const MAX_VIEW_BUDGET_MS: f32 = f32::INFINITY;
#[derive(Debug)]
pub enum Pace {
Idle,
Now,
After(Duration),
}
pub trait SimLoop {
type Command;
fn handle_command(&mut self, cmd: Self::Command) -> bool;
fn pump(&mut self) -> Pace;
fn start(&mut self) {}
}
#[derive(Debug, Default)]
pub struct SnapshotSlot {
fresh: Option<Snapshot>,
spare: Option<Snapshot>,
}
pub type SharedSlot = Arc<Mutex<SnapshotSlot>>;
impl SnapshotSlot {
#[cfg_attr(
all(target_arch = "wasm32", target_feature = "atomics"),
expect(
clippy::arc_with_non_send_sync,
reason = "a snapshot holds wgpu handles, which atomics leave unsendable"
)
)]
pub fn empty() -> SharedSlot {
Arc::new(Mutex::new(Self::default()))
}
#[cfg_attr(
all(target_arch = "wasm32", target_feature = "atomics"),
expect(
clippy::arc_with_non_send_sync,
reason = "a snapshot holds wgpu handles, which atomics leave unsendable"
)
)]
pub fn with_initial(snapshot: Snapshot) -> SharedSlot {
Arc::new(Mutex::new(Self {
fresh: Some(snapshot),
spare: None,
}))
}
}
pub fn take_snapshot(slot: &SharedSlot) -> Option<Snapshot> {
slot.lock().ok()?.fresh.take()
}
pub fn recycle(slot: &SharedSlot, snapshot: Snapshot) {
if let Ok(mut slot) = slot.lock() {
slot.spare = Some(snapshot);
}
}
pub fn claim_spare(slot: &SharedSlot) -> Option<Snapshot> {
slot.lock().ok()?.spare.take()
}
pub fn publish(slot: &SharedSlot, snapshot: Snapshot) {
if let Ok(mut slot) = slot.lock() {
slot.spare = slot.fresh.replace(snapshot);
}
}