diff --git a/Cargo.lock b/Cargo.lock index 9e42c6b5..60b9db41 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1601,6 +1601,7 @@ dependencies = [ "effectcraft-engine", "effectcraft-export", "effectcraft-expr", + "effectcraft-gpu", "effectcraft-host", "effectcraft-media", "effectcraft-ui-egui", diff --git a/ROADMAP.md b/ROADMAP.md index a4986fc1..2467a755 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -20,7 +20,7 @@ once. | M11 | Animation tools: audio playback, meters and waveforms, Lottie import and export, presets, Motion Sketch, Wiggler, Smoother | Done | | M12 | Performance: layer cache, parallel and GPU compositing, disk cache, motion tracking (done); GPU versions of the remaining CPU-only effects | Mostly done | | M13 | Puppet tools, paint, Roto Brush, motion tracking, a plug-in API, Timeline depth | Done (Roto Brush and face tracking use classical models; learned models are still to come) | -| M15 | The web app (WebAssembly, WebGPU) | Done: browser storage, Web Audio, renders and analyses in Web Workers, offline install ([docs/web.md](docs/web.md)) | +| M15 | The web app (WebAssembly, WebGPU) | Done: browser storage, Web Audio, renders and analyses in Web Workers, viewer frames and GPU effects in frame workers with their own WebGPU devices, the disk cache in the Origin Private File System, a storage manager, offline install ([docs/web.md](docs/web.md)) | | M14 | Built for agents: MCP server, command-line tool, control channel; Settings, keyboard shortcut editor, auto-save and crash recovery ([docs/preferences.md](docs/preferences.md)) | Done | ## How far from full parity diff --git a/apps/effectcraft-web/Cargo.toml b/apps/effectcraft-web/Cargo.toml index 03663089..6c30f9a5 100644 --- a/apps/effectcraft-web/Cargo.toml +++ b/apps/effectcraft-web/Cargo.toml @@ -21,6 +21,7 @@ serde_json = { workspace = true } [target.'cfg(target_arch = "wasm32")'.dependencies] effectcraft-host = { workspace = true } +effectcraft-gpu = { workspace = true } effectcraft-media = { workspace = true } effectcraft-export = { workspace = true } effectcraft-expr = { workspace = true } diff --git a/apps/effectcraft-web/js/host.js b/apps/effectcraft-web/js/host.js index 1fcdc40b..5435aba0 100644 --- a/apps/effectcraft-web/js/host.js +++ b/apps/effectcraft-web/js/host.js @@ -105,6 +105,14 @@ export async function storeLoadAll() { return backend.loadAll(); } export async function storePut(key, data, modified) { return backend.put(key, data, modified); } export async function storeDelete(key) { return backend.del(key); } +/// Ask the browser to keep the origin's storage under pressure; resolves with whether it is. +export async function storePersist() { + try { + if (navigator.storage && navigator.storage.persist) return await navigator.storage.persist(); + } catch {} + return false; +} + /// {usage, quota, persisted, backend}. export async function storeEstimate() { const out = { backend: backend ? backend.name : null }; @@ -341,8 +349,10 @@ export function workerStats() { const frameWorkers = []; /// Start a frame worker; returns its index. `onMessage(index, json, bytes, error)` receives each -/// `frameReply` (JSON, pixels as a Uint8Array) or, if the worker dies, the error. -export function frameWorkerStart(base, onMessage) { +/// `frameReply` (JSON, pixels as a Uint8Array) or, if the worker dies, the error. `gpu`: the +/// worker opens its own WebGPU device (when the browser has WebGPU in workers) and renders +/// GPU effects on it. +export function frameWorkerStart(base, onMessage, gpu) { const index = frameWorkers.length; const worker = new Worker(new URL("worker.js", base), { type: "module", name: `effectcraft-frames-${index}` }); worker.onmessage = (e) => { @@ -353,11 +363,105 @@ export function frameWorkerStart(base, onMessage) { onMessage(index, undefined, undefined, String(e.message || "frame worker failed")); worker.terminate(); }; - worker.postMessage({ type: "init", module: self.effectcraftModule ?? null, base }); + worker.postMessage({ type: "init", module: self.effectcraftModule ?? null, base, frames: true, gpu: !!gpu }); frameWorkers.push(worker); return index; } +// ------------------------------------------------------------------ disk cache (OPFS) + +// The disk cache's frames (src/diskcache.rs): one file per frame under +// effectcraft-cache/v1/frames/<32 hex>.ecc in the Origin Private File System. Frame workers write +// them with synchronous access handles (a temporary file moved into place, so a reader never +// sees a partial entry; every entry also ends in a checksum); the page lists, reads and deletes. +const CACHE_ROOT = "effectcraft-cache"; +const CACHE_PATH = [CACHE_ROOT, "v1", "frames"]; +let cacheDirHandle = null; + +async function cacheDir() { + if (cacheDirHandle) return cacheDirHandle; + if (!navigator.storage || !navigator.storage.getDirectory) throw new Error("no Origin Private File System"); + let d = await navigator.storage.getDirectory(); + for (const n of CACHE_PATH) d = await d.getDirectoryHandle(n, { create: true }); + cacheDirHandle = d; + return d; +} + +/// Every cached entry: [{name, size, modified}]. Temporaries older than ten minutes (a writer +/// that died) are removed. +export async function cacheList() { + const dir = await cacheDir(); + const out = []; + const stale = []; + for await (const [name, h] of dir.entries()) { + if (h.kind !== "file") continue; + const f = await h.getFile(); + if (name.includes(".tmp")) { + if (Date.now() - f.lastModified > 600000) stale.push(name); + continue; + } + out.push({ name, size: f.size, modified: f.lastModified }); + } + for (const n of stale) { try { await dir.removeEntry(n); } catch {} } + return out; +} + +/// An entry's bytes. +export async function cacheRead(name) { + const dir = await cacheDir(); + const f = await (await dir.getFileHandle(name)).getFile(); + return new Uint8Array(await f.arrayBuffer()); +} + +/// Write an entry (in a worker: a synchronous access handle; elsewhere a writable stream); +/// resolves with its size. +export async function cacheWrite(name, bytes) { + const dir = await cacheDir(); + const canMove = typeof FileSystemHandle !== "undefined" && "move" in FileSystemHandle.prototype; + const tmp = canMove ? `${name}.tmp.${Math.random().toString(36).slice(2)}` : name; + const fh = await dir.getFileHandle(tmp, { create: true }); + if (fh.createSyncAccessHandle) { + const h = await fh.createSyncAccessHandle(); + try { + h.truncate(0); + h.write(bytes, { at: 0 }); + h.flush(); + } finally { + h.close(); + } + } else { + const w = await fh.createWritable(); + await w.write(bytes); + await w.close(); + } + if (canMove) { + try { await fh.move(dir, name); } catch (e) { try { await dir.removeEntry(tmp); } catch {} throw e; } + } + return bytes.length; +} + +/// Delete entries (names); missing ones are ignored. +export async function cacheDelete(names) { + const dir = await cacheDir(); + for (const n of names) { try { await dir.removeEntry(n); } catch {} } +} + +/// Delete the whole cache. +export async function cacheClear() { + cacheDirHandle = null; + try { + const root = await navigator.storage.getDirectory(); + await root.removeEntry(CACHE_ROOT, { recursive: true }); + } catch (e) { + if (e.name !== "NotFoundError") throw e; + } +} + +/// Whether this thread has WebGPU (`navigator.gpu`). +export function hasWebGpu() { + return !!(self.navigator && self.navigator.gpu); +} + /// Post a message ({type: "frame", json} or {type: "file", path, bytes}) to frame worker `index`. export function frameWorkerPost(index, msg, transfer) { const w = frameWorkers[index]; diff --git a/apps/effectcraft-web/src/app.rs b/apps/effectcraft-web/src/app.rs index e6712e3d..bf2c5147 100644 --- a/apps/effectcraft-web/src/app.rs +++ b/apps/effectcraft-web/src/app.rs @@ -6,7 +6,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use effectcraft_engine::Session; -use crate::{api, audio, browse, files, frames, persist, worker}; +use crate::{api, audio, browse, diskcache, files, frames, persist, storage, worker}; use effectcraft_ui_egui::EffectcraftApp; use serde_json::json; use wasm_bindgen::prelude::*; @@ -104,6 +104,10 @@ impl eframe::App for WebApp { } api::set_info("gpu", json!({"compositor": self.app.gpu_adapter(), "viewerOnGpu": self.app.viewer_on_gpu()})); api::set_info("frameWorkers", frames::stats()); + // Settings ▸ Disk ▸ Disk Cache (frames in the Origin Private File System). + let d = &self.app.session.prefs.disk; + diskcache::configure(d.disk_cache_enabled, d.disk_cache_max_gb, storage::quota()); + api::set_info("diskCache", diskcache::stats()); } fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { @@ -139,6 +143,7 @@ pub fn session() -> Session { config: Some(Arc::new(persist::config())), offload: (!query_flag("noworkers")).then(|| Arc::new(worker::WorkerOffload) as Arc), browser: Some(Arc::new(browse::WebBrowser)), + storage: Some(Arc::new(storage::WebStorage)), ..Default::default() } } @@ -186,6 +191,8 @@ pub async fn start(canvas_id: String) -> Result<(), JsValue> { api::set_info("storage", json!({"backend": persist::backend(), "entries": stored, "loadMs": js_sys::Date::now() - t0})); audio::install_unlock(); browse::restore(); + diskcache::init(); + storage::refresh(); let runner = eframe::WebRunner::new(); runner .start( @@ -210,10 +217,11 @@ pub async fn start(canvas_id: String) -> Result<(), JsValue> { } install_hooks(&mut app); // Viewer frames render in frame workers (`?frameworkers=N`, 0 or `?noworkers`: - // on the page's thread). + // on the page's thread), each on its own WebGPU device where the browser has + // WebGPU in workers (`?nogpuworkers`: their CPU). let n = if query_flag("noworkers") { 0 } else { query_value("frameworkers").and_then(|v| v.parse().ok()).unwrap_or(2usize).min(8) }; if n > 0 { - app.frames.set_remote(Some(Arc::new(frames::WebFrames::start(n)))); + app.frames.set_remote(Some(Arc::new(frames::WebFrames::start(n, !query_flag("nogpuworkers"))))); } let (tx, rx) = std::sync::mpsc::channel(); api::set_sender(tx); diff --git a/apps/effectcraft-web/src/diskcache.rs b/apps/effectcraft-web/src/diskcache.rs new file mode 100644 index 00000000..177ce8d8 --- /dev/null +++ b/apps/effectcraft-web/src/diskcache.rs @@ -0,0 +1,228 @@ +//! The disk cache (Settings ▸ Disk ▸ Disk Cache) in the browser: viewer frames kept in the +//! Origin Private File System between visits, with the size limit from the settings and +//! least-recently-used eviction. +//! +//! - **Writes** happen in the frame workers: a render request carries the frame's disk-cache key +//! (`effectcraft_engine::remote::FrameMsg::Render`), the worker posts the frame, then writes +//! the sealed entry (`disk_cache::frame_entry`: LZ4, checksum) with a synchronous access +//! handle into a temporary file moved into place (`js/host.js` `cacheWrite`), and reports +//! `FrameReply::Stored`. +//! - **The index** lives here, on the page ([`DiskIndex`], shared with the desktop's cache): +//! rebuilt at startup from the folder listing (oldest files first), updated by the workers' +//! `Stored` replies and by reads; eviction deletes the least recently used files. +//! - **Reads** are asynchronous: a frame whose key is indexed is read and decoded here instead +//! of rendering it (`crate::frames`); a damaged or missing file is a miss and is forgotten. +//! +//! Only frames are cached on disk in the browser (the desktop also keeps slow layer buffers): +//! a layer lookup happens in the middle of a synchronous render, where the browser's file +//! system can't be read. + +use std::cell::RefCell; + +use effectcraft_engine::render::disk_cache::{self, DiskIndex, Kind}; +use serde_json::{Value, json}; +use wasm_bindgen::prelude::*; +use wasm_bindgen_futures::JsFuture; + +#[wasm_bindgen(module = "/js/host.js")] +extern "C" { + #[wasm_bindgen(js_name = cacheList)] + fn cache_list() -> js_sys::Promise; + #[wasm_bindgen(js_name = cacheRead)] + fn cache_read(name: &str) -> js_sys::Promise; + #[wasm_bindgen(js_name = cacheWrite)] + pub(crate) fn cache_write(name: &str, bytes: js_sys::Uint8Array) -> js_sys::Promise; + #[wasm_bindgen(js_name = cacheDelete)] + fn cache_delete(names: js_sys::Array) -> js_sys::Promise; + #[wasm_bindgen(js_name = cacheClear)] + fn cache_clear() -> js_sys::Promise; +} + +#[derive(Default)] +struct State { + index: DiskIndex, + /// The folder was listed (lookups before then miss). + loaded: bool, + /// The Origin Private File System works here. + available: bool, + /// Settings ▸ Disk ▸ Enable Disk Cache. + enabled: bool, + max_bytes: u64, + hits: u64, + misses: u64, + writes: u64, + evictions: u64, + error: Option, +} + +thread_local! { + static ST: RefCell = RefCell::new(State { enabled: true, max_bytes: 1 << 30, ..Default::default() }); +} + +fn js_err(e: JsValue) -> String { + e.as_string().or_else(|| js_sys::Reflect::get(&e, &"message".into()).ok().and_then(|m| m.as_string())).unwrap_or_else(|| format!("{e:?}")) +} + +/// List the cache folder and build the index (in the background). +pub fn init() { + wasm_bindgen_futures::spawn_local(async { + match JsFuture::from(cache_list()).await { + Ok(list) => { + let mut entries: Vec<(f64, u128, u64)> = js_sys::Array::from(&list) + .iter() + .filter_map(|e| { + let get = |k: &str| js_sys::Reflect::get(&e, &k.into()).ok(); + let key = disk_cache::parse_entry_name(&get("name")?.as_string()?)?; + Some((get("modified")?.as_f64().unwrap_or(0.0), key, get("size")?.as_f64().unwrap_or(0.0) as u64)) + }) + .collect(); + entries.sort_by(|a, b| a.0.total_cmp(&b.0)); + ST.with(|s| { + let mut s = s.borrow_mut(); + for (_, key, bytes) in entries { + s.index.insert(Kind::Frame, key, bytes); + } + s.loaded = true; + s.available = true; + }); + evict(); + } + Err(e) => { + let e = js_err(e); + log::info!("disk cache unavailable: {e}"); + ST.with(|s| s.borrow_mut().error = Some(e)); + } + } + }); +} + +/// Apply the settings: on/off and the limit (`max_gb`, at most half the origin's quota when it +/// is known). +pub fn configure(enabled: bool, max_gb: u32, quota: Option) { + let mut max = (max_gb.max(1) as u64) << 30; + if let Some(q) = quota.filter(|q| *q > 0) { + max = max.min(q / 2); + } + let changed = ST.with(|s| { + let mut s = s.borrow_mut(); + let changed = s.enabled != enabled || s.max_bytes != max; + s.enabled = enabled; + s.max_bytes = max; + changed + }); + if changed { + evict(); + } +} + +/// The cache is usable: listed, available and enabled. +pub fn active() -> bool { + ST.with(|s| { + let s = s.borrow(); + s.loaded && s.available && s.enabled + }) +} + +/// The frame of `key` is cached. +pub fn contains(key: u128) -> bool { + active() && ST.with(|s| s.borrow().index.contains(Kind::Frame, key)) +} + +/// A lookup missed (counted for the statistics). +pub fn note_miss() { + ST.with(|s| s.borrow_mut().misses += 1); +} + +/// Read and decode a cached frame; `None` (forgotten and deleted) when the file is missing or +/// damaged. +pub async fn read(key: u128) -> Option { + let name = disk_cache::entry_name(key); + let bytes = JsFuture::from(cache_read(&name)).await.ok().and_then(|b| b.dyn_into::().ok()).map(|b| b.to_vec()); + let frame = bytes.as_deref().and_then(disk_cache::read_frame_entry); + ST.with(|s| { + let mut s = s.borrow_mut(); + if frame.is_some() { + s.hits += 1; + s.index.touch(Kind::Frame, key); + } else { + s.misses += 1; + s.index.remove(Kind::Frame, key); + } + }); + if frame.is_none() && bytes.is_some() { + delete(vec![name]); + } + frame +} + +/// A worker stored the frame of `key` (`bytes` on disk). +pub fn stored(key: u128, bytes: u64) { + ST.with(|s| { + let mut s = s.borrow_mut(); + s.index.insert(Kind::Frame, key, bytes); + s.writes += 1; + }); + evict(); +} + +fn evict() { + let victims = ST.with(|s| { + let mut s = s.borrow_mut(); + let max = if s.enabled { s.max_bytes } else { u64::MAX }; + let v = s.index.evict(max); + s.evictions += v.len() as u64; + v + }); + if !victims.is_empty() { + delete(victims.into_iter().map(|(_, k)| disk_cache::entry_name(k)).collect()); + } +} + +fn delete(names: Vec) { + let arr = js_sys::Array::new(); + for n in names { + arr.push(&JsValue::from_str(&n)); + } + wasm_bindgen_futures::spawn_local(async move { + let _ = JsFuture::from(cache_delete(arr)).await; + }); +} + +/// Delete every cached frame; returns (entries, bytes) removed. +pub fn clear() -> (u64, u64) { + let r = ST.with(|s| { + let mut s = s.borrow_mut(); + let r = (s.index.len() as u64, s.index.total()); + s.index.clear(); + r + }); + wasm_bindgen_futures::spawn_local(async { + if let Err(e) = JsFuture::from(cache_clear()).await { + log::warn!("disk cache: clearing failed: {}", js_err(e)); + } + }); + r +} + +/// `cache.diskStats` / `storage.info().diskCache`. +pub fn stats() -> Value { + ST.with(|s| { + let s = s.borrow(); + json!({ + "enabled": s.enabled && s.available, + "available": s.available, + "loaded": s.loaded, + "folder": "Origin Private File System: effectcraft-cache/v1/frames", + "entries": s.index.len(), + "frames": s.index.len(), + "layers": 0, + "bytes": s.index.total(), + "maxBytes": s.max_bytes, + "hits": s.hits, + "misses": s.misses, + "writes": s.writes, + "evictions": s.evictions, + "error": s.error, + }) + }) +} diff --git a/apps/effectcraft-web/src/frames.rs b/apps/effectcraft-web/src/frames.rs index abad352f..d22f1ef1 100644 --- a/apps/effectcraft-web/src/frames.rs +++ b/apps/effectcraft-web/src/frames.rs @@ -6,27 +6,39 @@ //! time, then diffs: [`Mirror`]); the frame comes back as premultiplied RGBA8 in a transferred //! buffer. One frame per worker at a time, so the page always decides what renders next (the //! viewer's frame before prefetch). A worker that fails is dropped; with none left, frames render -//! on the page's thread again (`Frames::pump`). +//! on the page's thread again (`Frames::pump`). With the disk cache on, a frame whose key is +//! cached is read from the Origin Private File System instead ([`crate::diskcache`]); the +//! others are stored there by the worker that renders them. //! -//! **Worker side** — [`worker_frame`]: the worker's [`FrameServer`] (footage from the worker's -//! media pool, expressions) handles each message and posts its replies. +//! **Worker side** — [`worker_frame_init`] / [`worker_frame`]: the worker's [`FrameServer`] +//! (footage from the worker's media pool, expressions) handles each message and posts its +//! replies. Where the browser has WebGPU in workers, the worker opens its own device +//! ([`Gpu::request_deferred`]) and renders GPU effects and the GPU compositor on it: a frame +//! takes several passes, awaiting the device's readbacks in between (they can't be waited for +//! synchronously, even in a worker), and Backend Auto picks CPU or GPU per comp from measured +//! frame times. Without WebGPU, frames render on the worker's CPU. use std::cell::RefCell; use std::collections::HashSet; +use std::sync::Arc; use effectcraft_engine::offload::footage_files; use effectcraft_engine::remote::{FrameMsg, FrameReply, FrameServer, Mirror}; +use effectcraft_gpu::Gpu; use effectcraft_ui_egui::frames::{RemoteDone, RemoteFrame, RemoteFrames, RemoteJob}; use serde_json::{Value, json}; use wasm_bindgen::JsCast; use wasm_bindgen::prelude::*; +use wasm_bindgen_futures::JsFuture; #[wasm_bindgen(module = "/js/host.js")] extern "C" { #[wasm_bindgen(js_name = frameWorkerStart)] - fn frame_worker_start(base: &str, on_message: &js_sys::Function) -> u32; + fn frame_worker_start(base: &str, on_message: &js_sys::Function, gpu: bool) -> u32; #[wasm_bindgen(js_name = frameWorkerPost)] fn frame_worker_post(index: u32, msg: &JsValue, transfer: &js_sys::Array); + #[wasm_bindgen(js_name = hasWebGpu)] + fn has_web_gpu() -> bool; } /// One frame worker as the page sees it. @@ -36,6 +48,8 @@ struct Fw { files: HashSet, busy: Option<(u64, RemoteDone)>, alive: bool, + /// Its GPU adapter (after it reported), or why it has none. + gpu: Option>, } #[derive(Default)] @@ -48,6 +62,11 @@ struct State { /// Bytes of project sync messages sent (diffs keep this small). sync_bytes: u64, syncs: u64, + /// Frames rendered through a worker's GPU, and the passes of the last one. + gpu_frames: u64, + last_passes: u32, + /// Frames served from the disk cache. + disk_hits: u64, } type OnMessage = Closure; @@ -57,6 +76,8 @@ thread_local! { static HANDLER: RefCell> = const { RefCell::new(None) }; /// The worker's frame server (worker side). static SERVER: RefCell> = const { RefCell::new(None) }; + /// The worker's GPU device (worker side). + static GPU: RefCell> = const { RefCell::new(None) }; } fn obj(pairs: &[(&str, JsValue)]) -> JsValue { @@ -71,31 +92,22 @@ fn obj(pairs: &[(&str, JsValue)]) -> JsValue { pub struct WebFrames; impl WebFrames { - /// Start `n` frame workers. - pub fn start(n: usize) -> WebFrames { + /// Start `n` frame workers; `gpu`: each opens its own WebGPU device where it can. + pub fn start(n: usize, gpu: bool) -> WebFrames { let on: OnMessage = Closure::new(|index: u32, json: JsValue, bytes: JsValue, error: JsValue| on_message(index, json, bytes, error)); STATE.with(|s| { let mut s = s.borrow_mut(); for _ in 0..n { - let index = frame_worker_start(&crate::page_url(""), on.as_ref().unchecked_ref()); - s.workers.push(Fw { index, mirror: Mirror::default(), files: HashSet::new(), busy: None, alive: true }); + let index = frame_worker_start(&crate::page_url(""), on.as_ref().unchecked_ref(), gpu); + s.workers.push(Fw { index, mirror: Mirror::default(), files: HashSet::new(), busy: None, alive: true, gpu: None }); } }); HANDLER.with(|h| *h.borrow_mut() = Some(on)); WebFrames } -} -impl RemoteFrames for WebFrames { - fn slots(&self) -> usize { - STATE.with(|s| s.borrow().workers.iter().filter(|w| w.alive).count()) - } - - fn purge(&self) { - purge(); - } - - fn start(&self, job: RemoteJob, done: RemoteDone) { + /// Send `job` to an idle worker. + fn render(&self, job: RemoteJob, done: RemoteDone) { let r = STATE.with(|s| { let mut s = s.borrow_mut(); s.next_id += 1; @@ -121,7 +133,8 @@ impl RemoteFrames for WebFrames { sent = t.len() as u64; frame_worker_post(index, &obj(&[("type", "frame".into()), ("json", t.into())]), &js_sys::Array::new()); } - let req = FrameMsg::Render { id, revision: job.revision, comp: job.comp, time: job.t, opts: Box::new(job.opts) }; + let disk = job.disk_key.map(|k| format!("{k:032x}")); + let req = FrameMsg::Render { id, revision: job.revision, comp: job.comp, time: job.t, opts: Box::new(job.opts), disk }; frame_worker_post( index, &obj(&[("type", "frame".into()), ("json", serde_json::to_string(&req).unwrap_or_default().into())]), @@ -140,6 +153,49 @@ impl RemoteFrames for WebFrames { } } +impl RemoteFrames for WebFrames { + fn slots(&self) -> usize { + STATE.with(|s| s.borrow().workers.iter().filter(|w| w.alive).count()) + } + + fn purge(&self) { + purge(); + } + + fn gpu(&self) -> bool { + STATE.with(|s| s.borrow().workers.iter().any(|w| w.alive && matches!(w.gpu, Some(Ok(_))))) + } + + fn disk(&self) -> bool { + crate::diskcache::active() + } + + fn disk_contains(&self, key: u128) -> bool { + crate::diskcache::contains(key) + } + + fn start(&self, job: RemoteJob, done: RemoteDone) { + // A frame in the disk cache is read, not rendered (a failed read renders it). + if let Some(k) = job.disk_key { + if crate::diskcache::contains(k) { + wasm_bindgen_futures::spawn_local(async move { + match crate::diskcache::read(k).await { + Some(f) => { + STATE.with(|s| s.borrow_mut().disk_hits += 1); + done(Ok(RemoteFrame { width: f.width, height: f.height, rgba: f.rgba, ms: 0.0 })); + } + None => WebFrames.render(job, done), + } + crate::repaint(); + }); + return; + } + crate::diskcache::note_miss(); + } + self.render(job, done); + } +} + /// A message from frame worker `index`: a JSON [`FrameReply`] (with the pixels in `bytes`), or /// `error` when the worker died. fn on_message(index: u32, json: JsValue, bytes: JsValue, error: JsValue) { @@ -154,11 +210,15 @@ fn on_message(index: u32, json: JsValue, bytes: JsValue, error: JsValue) { } let reply: FrameReply = serde_json::from_str(&json.as_string()?).ok()?; match reply { - FrameReply::Frame { id, width, height, ms } => { + FrameReply::Frame { id, width, height, ms, passes, gpu } => { let (_, d) = w.busy.take_if(|b| b.0 == id)?; let rgba = bytes.dyn_into::().map(|b| b.to_vec()).unwrap_or_default(); st.rendered += 1; st.last_ms = ms; + if gpu { + st.gpu_frames += 1; + st.last_passes = passes; + } Some((d, Ok(RemoteFrame { width, height, rgba, ms }))) } FrameReply::Failed { id, error, .. } => { @@ -171,6 +231,20 @@ fn on_message(index: u32, json: JsValue, bytes: JsValue, error: JsValue) { w.mirror.reset(); None } + FrameReply::Stored { key, bytes } => { + if let Ok(k) = u128::from_str_radix(&key, 16) { + crate::diskcache::stored(k, bytes); + } + None + } + FrameReply::Info { gpu, error } => { + match (&gpu, &error) { + (Some(g), _) => log::info!("frame worker {index}: GPU {g}"), + (None, e) => log::info!("frame worker {index}: CPU only ({})", e.as_deref().unwrap_or("no GPU")), + } + w.gpu = Some(gpu.ok_or_else(|| error.unwrap_or_default())); + None + } } }); if let Some((d, r)) = done { @@ -207,6 +281,15 @@ pub fn purge() { pub fn stats() -> Value { STATE.with(|s| { let s = s.borrow(); + let gpu: Vec = s + .workers + .iter() + .map(|w| match &w.gpu { + Some(Ok(name)) => json!(name), + Some(Err(e)) => json!({"cpu": e}), + None => Value::Null, + }) + .collect(); json!({ "workers": s.workers.len(), "alive": s.workers.iter().filter(|w| w.alive).count(), @@ -216,16 +299,150 @@ pub fn stats() -> Value { "lastMs": s.last_ms, "syncs": s.syncs, "syncBytes": s.sync_bytes, + "gpu": gpu, + "gpuFrames": s.gpu_frames, + "lastPasses": s.last_passes, + "diskHits": s.disk_hits, }) }) } +thread_local! { + /// `workerFrameCheck` promises waiting for their frame: id → (resolve, reject, local frame). + #[allow(clippy::type_complexity)] + static CHECKS: RefCell), f64)>> = RefCell::new(Default::default()); +} + +/// `effectcraft.workerFrameCheck({time?, scale?, backend?})`: render the active comp's frame in +/// a frame worker (backend `gpu` by default: the worker's WebGPU device) and on the page's CPU +/// with the same render options, and compare them. Resolves with `{width, height, maxDiff, +/// meanDiff, over4, workerMs, gpuFrames}` (differences in 8-bit levels; `over4`: the fraction +/// of channels more than 4 levels apart). Rejects when no worker is idle (try again). +#[wasm_bindgen(js_name = workerFrameCheck)] +pub fn worker_frame_check(params: JsValue) -> js_sys::Promise { + let get = |k: &str| js_sys::Reflect::get(¶ms, &k.into()).ok().filter(|v| !v.is_undefined()); + let time = get("time").and_then(|v| v.as_f64()); + let scale = get("scale").and_then(|v| v.as_f64()).unwrap_or(0.5); + let backend = get("backend").and_then(|v| v.as_string()).unwrap_or_else(|| "gpu".into()); + js_sys::Promise::new(&mut |resolve, reject| { + let backend = backend.clone(); + crate::post(move |app| { + let s = &app.session; + let Some(comp) = s.active_comp_id() else { + let _ = reject.call1(&JsValue::NULL, &"no active composition".into()); + return; + }; + let t = time.map(effectcraft_engine::time::Tick::from_seconds_f64).unwrap_or_else(|| s.time()); + let mut opts = app.frame_opts(comp, scale); + opts.backend = match backend.as_str() { + "cpu" => effectcraft_engine::render::Backend::Cpu, + "auto" => effectcraft_engine::render::Backend::Auto, + _ => effectcraft_engine::render::Backend::Gpu, + }; + let local = { + let mut r = effectcraft_engine::render::Renderer::new( + &s.project, + s.footage.as_ref(), + effectcraft_engine::render::RenderOpts { backend: effectcraft_engine::render::Backend::Cpu, ..opts }, + ); + r.expr = s.expr.as_deref(); + let img = r.comp_frame(comp, t); + (img.width, img.height, effectcraft_engine::remote::rgba8_premultiplied(&img)) + }; + let id = STATE.with(|st| { + let mut st = st.borrow_mut(); + st.next_id += 1; + st.next_id + }); + CHECKS.with(|c| c.borrow_mut().insert(id, (resolve.clone(), reject.clone(), local, js_sys::Date::now()))); + let job = RemoteJob { project: s.project.clone(), revision: s.revision, comp, t, opts, disk_key: None }; + WebFrames.render( + job, + Box::new(move |r: Result| { + let Some((resolve, reject, (w, h, want), t0)) = CHECKS.with(|c| c.borrow_mut().remove(&id)) else { return }; + match r { + Ok(f) if (f.width, f.height) == (w, h) && f.rgba.len() == want.len() => { + let (mut max, mut sum, mut over) = (0u32, 0u64, 0u64); + for (a, b) in f.rgba.iter().zip(&want) { + let d = (*a as i32 - *b as i32).unsigned_abs(); + max = max.max(d); + sum += d as u64; + over += (d > 4) as u64; + } + let n = want.len().max(1) as f64; + let c = ((h / 2 * w + w / 2) * 4) as usize; + let v = json!({"width": w, "height": h, "maxDiff": max, "meanDiff": sum as f64 / n, "over4": over as f64 / n, + "workerMs": f.ms, "wallMs": js_sys::Date::now() - t0, "gpuFrames": stats()["gpuFrames"], + "center": [&f.rgba[c..c + 4], &want[c..c + 4]]}); + let _ = resolve.call1(&JsValue::NULL, &JsValue::from_str(&v.to_string())); + } + Ok(f) => { + let _ = reject.call1(&JsValue::NULL, &format!("size {}x{} vs {w}x{h}", f.width, f.height).into()); + } + Err(e) => { + let _ = reject.call1(&JsValue::NULL, &e.into()); + } + } + }), + ); + }); + }) +} + // ---------------------------------------------------------------- worker side -/// Handle one frame message (a JSON [`FrameMsg`]) in a frame worker; replies are posted with -/// their pixels transferred. +fn post_reply(r: &FrameReply, px: Option<&[u8]>) { + let scope: web_sys::DedicatedWorkerGlobalScope = js_sys::global().unchecked_into(); + let t = serde_json::to_string(r).unwrap_or_default(); + let res = match px { + Some(px) => { + let buf = js_sys::Uint8Array::from(px).buffer(); + scope.post_message_with_transfer( + &obj(&[("type", "frameReply".into()), ("json", t.into()), ("bytes", buf.clone().into())]), + &js_sys::Array::of1(&buf), + ) + } + None => scope.post_message(&obj(&[("type", "frameReply".into()), ("json", t.into())])), + }; + if let Err(e) = res { + log::warn!("frame worker: postMessage: {e:?}"); + } +} + +fn new_server() -> FrameServer { + FrameServer::new(crate::worker::pool(), Some(Arc::new(effectcraft_expr::Expressions))) +} + +/// Set up a frame worker (called once by `web/worker.js` after `workerInit`): its frame +/// server and, with `gpu` and WebGPU in workers, its own GPU device. Reports +/// [`FrameReply::Info`]. +#[wasm_bindgen(js_name = workerFrameInit)] +pub async fn worker_frame_init(gpu: bool) { + let mut server = new_server(); + let info = if !gpu { + FrameReply::Info { gpu: None, error: Some("GPU frame workers are off (?nogpuworkers)".into()) } + } else if !has_web_gpu() { + FrameReply::Info { gpu: None, error: Some("no WebGPU in workers".into()) } + } else { + match Gpu::request_deferred().await { + Ok(g) => { + let name = effectcraft_engine::render::Accelerator::name(&g); + server.set_accel(Some(Arc::new(g.clone()))); + GPU.with(|c| *c.borrow_mut() = Some(g)); + FrameReply::Info { gpu: Some(name), error: None } + } + Err(e) => FrameReply::Info { gpu: None, error: Some(e) }, + } + }; + SERVER.with(|c| *c.borrow_mut() = Some(server)); + post_reply(&info, None); +} + +/// Handle one frame message (a JSON [`FrameMsg`]) in a frame worker: replies are posted with +/// their pixels transferred. A frame rendering in passes awaits the GPU between them; a frame +/// with a disk-cache key is then stored ([`FrameReply::Stored`]). #[wasm_bindgen(js_name = workerFrame)] -pub fn worker_frame(json: String) { +pub async fn worker_frame(json: String) { let msg: FrameMsg = match serde_json::from_str(&json) { Ok(m) => m, Err(e) => { @@ -233,29 +450,25 @@ pub fn worker_frame(json: String) { return; } }; - let replies = SERVER.with(|c| { - let mut c = c.borrow_mut(); - let server = c.get_or_insert_with(|| { - let pool = crate::worker::pool(); - FrameServer::new(pool, Some(std::sync::Arc::new(effectcraft_expr::Expressions))) - }); - server.handle(msg) - }); - let scope: web_sys::DedicatedWorkerGlobalScope = js_sys::global().unchecked_into(); + let disk = match &msg { + FrameMsg::Render { disk, .. } => disk.clone(), + _ => None, + }; + let mut replies = SERVER.with(|c| c.borrow_mut().get_or_insert_with(new_server).handle(msg)); + while SERVER.with(|c| c.borrow().as_ref().is_some_and(|s| s.waiting())) { + let Some(g) = GPU.with(|c| c.borrow().clone()) else { break }; + g.settled().await; + replies = SERVER.with(|c| c.borrow_mut().as_mut().map(|s| s.resume()).unwrap_or_default()); + } for (r, px) in replies { - let t = serde_json::to_string(&r).unwrap_or_default(); - let res = match px { - Some(px) => { - let buf = js_sys::Uint8Array::from(&px[..]).buffer(); - scope.post_message_with_transfer( - &obj(&[("type", "frameReply".into()), ("json", t.into()), ("bytes", buf.clone().into())]), - &js_sys::Array::of1(&buf), - ) + post_reply(&r, px.as_deref()); + if let (FrameReply::Frame { width, height, .. }, Some(px), Some(key)) = (&r, &px, &disk) { + let entry = effectcraft_engine::render::disk_cache::frame_entry(*width, *height, px); + let name = format!("{key}.ecc"); + match JsFuture::from(crate::diskcache::cache_write(&name, js_sys::Uint8Array::from(&entry[..]))).await { + Ok(n) => post_reply(&FrameReply::Stored { key: key.clone(), bytes: n.as_f64().unwrap_or(entry.len() as f64) as u64 }, None), + Err(e) => log::warn!("frame worker: disk cache write failed: {e:?}"), } - None => scope.post_message(&obj(&[("type", "frameReply".into()), ("json", t.into())])), - }; - if let Err(e) = res { - log::warn!("frame worker: postMessage: {e:?}"); } } } diff --git a/apps/effectcraft-web/src/lib.rs b/apps/effectcraft-web/src/lib.rs index 96b17db2..013e0d36 100644 --- a/apps/effectcraft-web/src/lib.rs +++ b/apps/effectcraft-web/src/lib.rs @@ -11,7 +11,9 @@ //! | rfd file dialogs | `` pickers; drop files on the page | //! | Media Browser on the file system | browser storage and folders opened with the File System Access API ([`browse`]) | //! | written files (Save, Render Queue) | browser downloads (several render files as one `.zip`) | -//! | frame render threads | frame workers, each with a project replica fed by diffs ([`frames`]); `Frames::pump` on the UI thread without workers | +//! | frame render threads | frame workers, each with a project replica fed by diffs and its own WebGPU device ([`frames`]); `Frames::pump` on the UI thread without workers | +//! | disk cache folder | frames in the Origin Private File System, written by the frame workers ([`diskcache`]) | +//! | (none) | the storage manager: usage, quota, persistence, clearing ([`storage`]) | //! | Render Queue / analysis threads | Web Workers running their own engine instance ([`worker`]) | //! | cpal audio output | Web Audio: an AudioWorklet fed from the preview mixdown ([`audio`]) | //! | TCP control channel / MCP | `window.effectcraft` JavaScript API ([`api`]) | @@ -29,12 +31,16 @@ pub mod audio; #[cfg(target_arch = "wasm32")] pub mod browse; #[cfg(target_arch = "wasm32")] +pub mod diskcache; +#[cfg(target_arch = "wasm32")] pub mod files; #[cfg(target_arch = "wasm32")] pub mod frames; #[cfg(target_arch = "wasm32")] pub mod persist; #[cfg(target_arch = "wasm32")] +pub mod storage; +#[cfg(target_arch = "wasm32")] pub mod worker; #[cfg(target_arch = "wasm32")] diff --git a/apps/effectcraft-web/src/storage.rs b/apps/effectcraft-web/src/storage.rs new file mode 100644 index 00000000..e286ffc4 --- /dev/null +++ b/apps/effectcraft-web/src/storage.rs @@ -0,0 +1,124 @@ +//! The storage manager ([`StorageHost`], `storage.*` commands, Settings ▸ Disk ▸ Browser +//! Storage): the origin's usage and quota (`navigator.storage.estimate()`), persistent storage +//! (`navigator.storage.persist()`), what the store holds by kind ([`crate::store::FileKind`]) +//! and the disk cache ([`crate::diskcache`]); clearing each. +//! +//! The browser answers asynchronously: [`StorageHost::info`] returns the latest estimate and +//! asks for a new one at most once a second. + +use std::cell::RefCell; + +use effectcraft_engine::storage::StorageHost; +use serde_json::{Value, json}; +use wasm_bindgen::prelude::*; +use wasm_bindgen_futures::JsFuture; + +use crate::store::FileKind; + +#[wasm_bindgen(module = "/js/host.js")] +extern "C" { + #[wasm_bindgen(js_name = storePersist)] + fn store_persist() -> js_sys::Promise; +} + +#[derive(Default)] +struct Estimate { + usage: u64, + quota: u64, + persisted: bool, + /// When the last refresh started (ms). + asked: f64, + busy: bool, +} + +thread_local! { + static EST: RefCell = RefCell::new(Estimate::default()); +} + +/// Refresh the estimate in the background (at most once a second). +pub fn refresh() { + let now = js_sys::Date::now(); + let go = EST.with(|e| { + let mut e = e.borrow_mut(); + if e.busy || now - e.asked < 1000.0 { + return false; + } + e.busy = true; + e.asked = now; + true + }); + if !go { + return; + } + wasm_bindgen_futures::spawn_local(async { + let v = JsFuture::from(crate::persist::store_estimate()).await.ok(); + EST.with(|e| { + let mut e = e.borrow_mut(); + e.busy = false; + if let Some(v) = v { + let get = |k: &str| js_sys::Reflect::get(&v, &k.into()).ok(); + e.usage = get("usage").and_then(|x| x.as_f64()).unwrap_or(0.0) as u64; + e.quota = get("quota").and_then(|x| x.as_f64()).unwrap_or(0.0) as u64; + e.persisted = get("persisted").and_then(|x| x.as_bool()).unwrap_or(false); + } + }); + }); +} + +/// The origin's quota in bytes, when known. +pub fn quota() -> Option { + EST.with(|e| Some(e.borrow().quota).filter(|q| *q > 0)) +} + +/// The web app's [`StorageHost`]. +pub struct WebStorage; + +impl StorageHost for WebStorage { + fn info(&self) -> Value { + refresh(); + let (usage, quota, persisted) = EST.with(|e| { + let e = e.borrow(); + (e.usage, e.quota, e.persisted) + }); + json!({ + "backend": crate::persist::backend(), + "usage": usage, + "quota": quota, + "persisted": persisted, + "files": crate::files::STORE.usage(), + "diskCache": crate::diskcache::stats(), + "error": crate::persist::last_error(), + }) + } + + fn request_persist(&self) -> Value { + wasm_bindgen_futures::spawn_local(async { + let granted = JsFuture::from(store_persist()).await.ok().and_then(|v| v.as_bool()).unwrap_or(false); + EST.with(|e| e.borrow_mut().persisted = granted); + crate::repaint(); + }); + json!({"requested": true, "persisted": EST.with(|e| e.borrow().persisted)}) + } + + fn clear(&self, what: &str) -> Result { + let kinds: Vec = match what { + "diskCache" => vec![], + "all" => vec![FileKind::Media, FileKind::Project, FileKind::AutoSave], + k => vec![FileKind::from_name(k).ok_or_else(|| format!("unknown storage kind {k}"))?], + }; + let (mut entries, mut bytes) = (0u64, 0u64); + if matches!(what, "diskCache" | "all") { + let (n, b) = crate::diskcache::clear(); + entries += n; + bytes += b; + } + for k in kinds { + let (n, b) = crate::files::STORE.clear_kind(k); + entries += n; + bytes += b; + } + // The estimate catches up on the next refresh. + EST.with(|e| e.borrow_mut().asked = 0.0); + Ok(json!({"cleared": what, "entries": entries, "bytes": bytes})) + } +} diff --git a/apps/effectcraft-web/src/store.rs b/apps/effectcraft-web/src/store.rs index 471d96b5..78dc4915 100644 --- a/apps/effectcraft-web/src/store.rs +++ b/apps/effectcraft-web/src/store.rs @@ -182,6 +182,74 @@ impl Store { pub fn files(&self) -> Vec<(String, usize, f64, bool)> { self.lock().list(FILES).into_iter().filter_map(|(k, n, m, p)| Some((key_path(&k)?.to_string(), n, m, p))).collect() } + + /// Stored (persisted) files by kind: `{projects, media, autoSaves}`, each `{count, bytes}` + /// (the storage manager, `storage.info`). + pub fn usage(&self) -> serde_json::Value { + let mut out = serde_json::Map::new(); + for k in [FileKind::Project, FileKind::Media, FileKind::AutoSave] { + let (mut count, mut bytes) = (0u64, 0u64); + for (path, n, _, persisted) in self.files() { + if persisted && FileKind::of(&path) == k { + count += 1; + bytes += n as u64; + } + } + out.insert(k.name().into(), serde_json::json!({"count": count, "bytes": bytes})); + } + out.into() + } + + /// Delete every stored file of a kind; returns (files, bytes) removed. + pub fn clear_kind(&self, kind: FileKind) -> (u64, u64) { + let (mut count, mut bytes) = (0u64, 0u64); + for (path, n, _, persisted) in self.files() { + if persisted && FileKind::of(&path) == kind { + self.lock().remove(&file_key(&path)); + count += 1; + bytes += n as u64; + } + } + if count > 0 { + self.changed(); + } + (count, bytes) + } +} + +/// What a stored file is (Settings ▸ Disk ▸ Browser Storage). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FileKind { + /// A saved project (`.ecproj` outside the auto-save folder). + Project, + /// Imported footage and everything else. + Media, + /// Auto-saves (`/EffectCraft Auto-Save/…`, or `… auto-save N.ecproj`). + AutoSave, +} + +impl FileKind { + pub fn of(path: &str) -> FileKind { + let lower = path.to_ascii_lowercase(); + if !lower.ends_with(".ecproj") { + return FileKind::Media; + } + let name = lower.rsplit('/').next().unwrap_or(&lower); + if lower.contains("/effectcraft auto-save/") || name.contains(" auto-save ") { FileKind::AutoSave } else { FileKind::Project } + } + + /// `storage.clear` / `storage.info` name. + pub fn name(self) -> &'static str { + match self { + FileKind::Project => "projects", + FileKind::Media => "media", + FileKind::AutoSave => "autoSaves", + } + } + + pub fn from_name(n: &str) -> Option { + [FileKind::Project, FileKind::Media, FileKind::AutoSave].into_iter().find(|k| k.name() == n) + } } /// Settings and friends ([`ConfigStore`]) in the store; auto-saves ([`FileOps`]) in its file @@ -289,6 +357,28 @@ mod tests { s } + #[test] + fn usage_by_kind_and_clearing() { + let s = store(); + s.put_file("/a.ecproj", Arc::from(&b"proj"[..]), true); + s.put_file("/clip.mp4", Arc::from(&b"0123456789"[..]), true); + s.put_file("/still.png", Arc::from(&b"px"[..]), true); + s.put_file("/EffectCraft Auto-Save/a auto-save 1.ecproj", Arc::from(&b"auto"[..]), true); + s.put_file("/render.gif", Arc::from(&b"out"[..]), false); + let u = s.usage(); + assert_eq!(u["projects"], serde_json::json!({"count": 1, "bytes": 4})); + assert_eq!(u["media"], serde_json::json!({"count": 2, "bytes": 12}), "render outputs are not stored"); + assert_eq!(u["autoSaves"], serde_json::json!({"count": 1, "bytes": 4})); + assert_eq!(FileKind::from_name("autoSaves"), Some(FileKind::AutoSave)); + assert_eq!(FileKind::of("/x/My auto-save 3.ecproj"), FileKind::AutoSave); + s.lock().take_pending(); + assert_eq!(s.clear_kind(FileKind::Media), (2, 12)); + assert!(s.file("/clip.mp4").is_none() && s.file("/a.ecproj").is_some() && s.file("/render.gif").is_some()); + let deleted: Vec = s.lock().take_pending().into_iter().filter(|p| p.data.is_none()).map(|p| p.key).collect(); + assert_eq!(deleted.len(), 2, "the deletions are flushed to browser storage"); + assert_eq!(s.usage()["media"]["count"], 0); + } + #[test] fn writes_coalesce_per_key_in_order() { let mut m = Mirror::default(); diff --git a/apps/effectcraft-web/tests/smoke.mjs b/apps/effectcraft-web/tests/smoke.mjs index 81bc81cc..47ae22ad 100644 --- a/apps/effectcraft-web/tests/smoke.mjs +++ b/apps/effectcraft-web/tests/smoke.mjs @@ -192,6 +192,71 @@ try { check(fwk.after.syncs >= 2, `project synced as diffs: ${JSON.stringify(fwk.after)}`); check(fwk.maxGapMs < 400, `event loop blocked ${fwk.maxGapMs} ms while scrubbing`); + // GPU effects in the browser (M13.24): each frame worker opens its own WebGPU device; frames + // with effects go to the workers, which render them on the GPU in passes (awaiting the + // device's readbacks), while the page's event loop stays free. + const gw = await js(`(async () => { + const t0 = performance.now(); + while (effectcraft.info().frameWorkers.gpu.some((g) => g === null) && performance.now() - t0 < 30000) await new Promise((r) => setTimeout(r, 200)); + return {gpu: effectcraft.info().frameWorkers.gpu, workerWebGpu: effectcraft.info().webgpu}; + })()`); + report.steps.gpuWorkers = gw; + if (gw.gpu.some((g) => typeof g === "string")) { + report.steps.gpuWorkers.effects = await js(`(async () => { + const before = effectcraft.info().frameWorkers; + let maxGapMs = 0, last = performance.now(); + const timer = setInterval(() => { const now = performance.now(); maxGapMs = Math.max(maxGapMs, now - last); last = now; }, 10); + await effectcraft.execute("layer.newSolid", {name: "GPU Blur Solid", color: "#cc3366", width: 300, height: 200}); + await effectcraft.execute("effect.apply", {effect: "Gaussian Blur"}); + await effectcraft.execute("prop.set", {path: "effects/#1/blurriness", value: 25}); + await effectcraft.execute("effect.apply", {effect: "Glow"}); + for (let k = 0; k < 6; k++) { + await effectcraft.execute("time.set", {time: 0.3 + k / 5}); + await new Promise((r) => setTimeout(r, 200)); + } + const t0 = performance.now(); + while (effectcraft.info().frameWorkers.gpuFrames < before.gpuFrames + 3 && performance.now() - t0 < 120000) await new Promise((r) => setTimeout(r, 100)); + clearInterval(timer); + const after = effectcraft.info().frameWorkers; + // The worker's GPU frame against the page's CPU render of the same frame. + let parity = null; + for (let i = 0; i < 100 && !parity; i++) { + parity = await effectcraft.workerFrameCheck({scale: 0.5}).catch(() => null); + if (!parity) await new Promise((r) => setTimeout(r, 100)); + } + after.parity = parity; + await effectcraft.execute("edit.undo", {}); + await effectcraft.execute("edit.undo", {}); + await effectcraft.execute("edit.undo", {}); + await effectcraft.execute("edit.undo", {}); + return {before, after, maxGapMs}; + })()`); + const ge = report.steps.gpuWorkers.effects; + report.steps.gpuWorkers.screenshot = await shot("01b-gpu-effects"); + check(ge.after.gpuFrames >= ge.before.gpuFrames + 3, `GPU effect frames rendered in workers: ${JSON.stringify(ge)}`); + check(ge.after.lastPasses >= 2, `frames rendered in passes (deferred readbacks): ${ge.after.lastPasses}`); + const pa = ge.after.parity; + check(pa && pa.meanDiff < 0.5 && pa.over4 < 0.005, `GPU worker frame vs the CPU: ${JSON.stringify(pa)}`); + check(ge.maxGapMs < 400, `event loop blocked ${ge.maxGapMs} ms while GPU workers rendered`); + } else { + report.steps.gpuWorkers.note = "no WebGPU in workers here: frame workers render on their CPU"; + } + + // The storage manager (Settings ▸ Disk ▸ Browser Storage) and its commands. + await js(`effectcraft.execute("storage.persist", {})`); + const st = await js(`(async () => { await new Promise((r) => setTimeout(r, 1200)); return effectcraft.execute("storage.info", {}); })()`); + report.steps.storage = st; + check(st.available && st.quota > 0 && st.usage > 0, `storage.info: ${JSON.stringify(st)}`); + check(st.diskCache && st.diskCache.enabled && st.diskCache.writes > 0, `disk cache in OPFS: ${JSON.stringify(st.diskCache)}`); + await js(`effectcraft.execute("prefs.open", {page: "disk"})`); + await sleep(800); + report.steps.storage.screenshot = await shot("01c-storage-manager"); + const ids = await js(`effectcraft.request("ui.elements", {prefix: "settings.storage."}).then((l) => l.map((e) => e.id))`); + report.steps.storage.elements = ids; + check(ids.includes("settings.storage.usage") && ids.includes("settings.storage.clear.diskCache"), `storage manager widgets: ${ids}`); + await js(`effectcraft.request("ui.click", {id: "settings.cancel"})`); + await sleep(300); + // Render Queue: a short, small GIF → download. `renderQueue.render` waits by default; the // render still runs in a worker and the reply comes when it ends (the page keeps running). const rqT = Date.now(); @@ -302,6 +367,25 @@ try { await js(`effectcraft.execute("prefs.set", {key: "general.recentItems", value: 7})`); const saved = await js(`effectcraft.saveToBrowser("/persist-test.ecproj")`); await js(`effectcraft.execute("layer.newSolid", {name: "Unsaved Solid"})`); + // Disk cache: frames the workers render now are stored in the Origin Private File System + // (the CPU renderer sends every frame to the workers). + await js(`effectcraft.execute("render.backend", {backend: "cpu"})`); + const diskTimes = [0.4, 0.8, 1.2]; + const stored = await js(`(async () => { + const w0 = (await effectcraft.execute("cache.diskStats", {})).writes; + for (const t of ${JSON.stringify(diskTimes)}) { + await effectcraft.execute("time.set", {time: t}); + await new Promise((r) => setTimeout(r, 300)); + } + const t0 = performance.now(); + let st; + do { + await new Promise((r) => setTimeout(r, 200)); + st = await effectcraft.execute("cache.diskStats", {}); + } while (st.writes < w0 + ${diskTimes.length} && performance.now() - t0 < 60000); + return {writesBefore: w0, after: st}; + })()`); + check(stored.after.writes >= stored.writesBefore + diskTimes.length, `frames written to the disk cache: ${JSON.stringify(stored)}`); await sleep(1500); // the session snapshot is taken at most every second await js("effectcraft.flush()"); const before = await js("effectcraft.listStored()"); @@ -321,6 +405,31 @@ try { check(paths.includes("/tone.wav") && paths.includes("/persist-test.ecproj"), `stored files: ${paths}`); check(recentItems === 7, `setting after reload: ${recentItems}`); check(rec.recentProjects[0] === "/persist-test.ecproj", `recent projects: ${rec.recentProjects}`); + // The disk cache survived the reload: the same frames come from it, not from a render. + report.steps.diskCache = await js(`(async () => { + const t0 = performance.now(); + let st; + do { + await new Promise((r) => setTimeout(r, 200)); + st = await effectcraft.execute("cache.diskStats", {}); + } while (!st.loaded && performance.now() - t0 < 20000); + const listed = st.entries; + for (const t of ${JSON.stringify(diskTimes)}) { + await effectcraft.execute("time.set", {time: t}); + await new Promise((r) => setTimeout(r, 300)); + } + const t1 = performance.now(); + while (effectcraft.info().frameWorkers.diskHits < ${diskTimes.length} && performance.now() - t1 < 30000) await new Promise((r) => setTimeout(r, 200)); + return {listed, stats: await effectcraft.execute("cache.diskStats", {}), frameWorkers: effectcraft.info().frameWorkers}; + })()`); + const dcr = report.steps.diskCache; + dcr.stored = stored; + check(dcr.listed >= diskTimes.length, `disk cache entries after reload: ${JSON.stringify(dcr)}`); + check(dcr.frameWorkers.diskHits >= diskTimes.length, `cache hits after reload: ${JSON.stringify(dcr.frameWorkers)}`); + const cleared = await js(`effectcraft.execute("storage.clear", {what: "diskCache"})`); + const afterClear = await js(`effectcraft.execute("cache.diskStats", {})`); + dcr.cleared = { cleared, afterClear }; + check(cleared.entries >= diskTimes.length && afterClear.entries === 0, `storage.clear diskCache: ${JSON.stringify(dcr.cleared)}`); await js(`effectcraft.execute("file.openRecent", {index: 0})`); const reopened = await js(`effectcraft.execute("comp.info", {}).then(c => c.layers.map(l => l.name))`); check(reopened.includes("Persisted Solid") && !reopened.includes("Unsaved Solid"), `Open Recent: ${reopened}`); diff --git a/apps/effectcraft-web/web/index.html b/apps/effectcraft-web/web/index.html index 7c6c6c3c..578e6159 100644 --- a/apps/effectcraft-web/web/index.html +++ b/apps/effectcraft-web/web/index.html @@ -62,6 +62,8 @@ listStored: () => ec.listStored(), removeStored: (path) => ec.removeStored(path), flush: () => ec.flush(), + // A frame rendered in a frame worker (its GPU by default) vs the page's CPU: {maxDiff, meanDiff, over4, …}. + workerFrameCheck: (params) => ec.workerFrameCheck(params ?? {}).then(JSON.parse), }; // Offline start and install (PWA): the service worker caches the app's files. if ("serviceWorker" in navigator && !new URLSearchParams(location.search).has("nosw")) { diff --git a/apps/effectcraft-web/web/worker.js b/apps/effectcraft-web/web/worker.js index 6c6853c5..4bd1ab54 100644 --- a/apps/effectcraft-web/web/worker.js +++ b/apps/effectcraft-web/web/worker.js @@ -2,21 +2,35 @@ // crates/engine/src/offload.rs and remote.rs). The page sends the compiled module, then: // - job workers: footage files and jobs (Render Queue, analyses, Roto Brush); the worker replies // with JSON messages and rendered files; -// - frame workers: footage files and frame messages (project syncs as diffs, render requests, -// Roto Brush segmentations); the worker replies with frames (pixels transferred). +// - frame workers (`frames: true`): footage files and frame messages (project syncs as diffs, +// render requests, Roto Brush segmentations); the worker replies with frames (pixels +// transferred). With `gpu: true` it opens its own WebGPU device and renders GPU effects on +// it; a frame then takes several passes, awaiting the device's readbacks in between. +// Messages are handled one at a time, in order: a frame awaiting its readbacks finishes before +// the next message (a project sync, the next request) is looked at. import init, * as ec from "./effectcraft_web.js"; let ready = null; -self.onmessage = async (e) => { - const m = e.data; - if (m.type === "init") { - ready = init({ module_or_path: m.module ?? new URL("effectcraft_web_bg.wasm", m.base) }).then(() => ec.workerInit()); - await ready; - self.postMessage({ type: "ready" }); - return; - } - await ready; +let queue = Promise.resolve(); + +async function handle(m) { if (m.type === "file") ec.workerFile(m.path, m.bytes); else if (m.type === "job") ec.workerJob(m.json); - else if (m.type === "frame") ec.workerFrame(m.json); + else if (m.type === "frame") await ec.workerFrame(m.json); +} + +self.onmessage = (e) => { + const m = e.data; + if (m.type === "init") { + ready = (async () => { + await init({ module_or_path: m.module ?? new URL("effectcraft_web_bg.wasm", m.base) }); + ec.workerInit(); + if (m.frames) await ec.workerFrameInit(!!m.gpu); + self.postMessage({ type: "ready" }); + })(); + return; + } + // A failure (a panic) is re-raised outside the queue so the page's `onerror` sees it, as + // before the queue: the page then drops the worker. + queue = queue.then(() => ready).then(() => handle(m)).catch((err) => setTimeout(() => { throw err; })); }; diff --git a/crates/engine/src/commands/edit.rs b/crates/engine/src/commands/edit.rs index a36b02d4..3ef2b299 100644 --- a/crates/engine/src/commands/edit.rs +++ b/crates/engine/src/commands/edit.rs @@ -549,6 +549,9 @@ fn purge_caches(s: &mut Session, p: &Value) -> Result { if disk && let Some(dc) = &s.disk_cache { disk_entries = dc.stats().entries; dc.clear(); + } else if disk && let Some(h) = &s.storage { + // The browser's disk cache (Origin Private File System). + disk_entries = h.clear("diskCache").ok().and_then(|r| r["entries"].as_u64()).unwrap_or(0) as usize; } if matches!(what.as_str(), "all" | "image" | "memory" | "memoryAndDisk") { effectcraft_effects::roto::purge(); @@ -560,7 +563,11 @@ fn purge_caches(s: &mut Session, p: &Value) -> Result { /// `cache.diskStats`: the disk cache's folder, limit and contents. pub(crate) fn disk_stats(s: &mut Session, _: &Value) -> Result { - let Some(dc) = &s.disk_cache else { return Ok(json!({"enabled": false})) }; + let Some(dc) = &s.disk_cache else { + // The browser's disk cache (Origin Private File System), when the web app has one. + let web = s.storage.as_ref().map(|h| h.info()["diskCache"].clone()).filter(|d| d.is_object()); + return Ok(web.unwrap_or_else(|| json!({"enabled": false}))); + }; let st = dc.stats(); Ok(json!({ "enabled": true, diff --git a/crates/engine/src/commands/mod.rs b/crates/engine/src/commands/mod.rs index 4768ef99..6c70650f 100644 --- a/crates/engine/src/commands/mod.rs +++ b/crates/engine/src/commands/mod.rs @@ -198,6 +198,7 @@ pub fn command_specs() -> &'static [CommandSpec] { v.extend(align::specs()); v.extend(content_fill::specs()); v.extend(stubs::specs()); + v.extend(crate::storage::specs()); v.extend(crate::learn::specs()); v.extend(crate::preview::specs()); v diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 4829395a..5c87e9d5 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -34,6 +34,7 @@ pub mod roto; pub mod scriptui; mod session_settings; pub mod shortcuts; +pub mod storage; pub mod sysinfo; pub mod tracking; pub mod vector; @@ -362,6 +363,8 @@ pub struct Session { pub offloaded: Vec, /// What the Media Browser browses (`None`: the local file system where there is one). pub browser: Option>, + /// The browser's storage (the web app; Settings ▸ Disk ▸ Browser Storage, `storage.*`). + pub storage: Option>, /// Offloaded jobs that ended, newest last (kind, error): how a caller waiting for one /// (`wait: true` over the control channel in the browser) learns its outcome. pub offload_log: Vec<(offload::JobKind, Option)>, @@ -446,6 +449,7 @@ impl Default for Session { offloaded: vec![], offload_log: vec![], browser: None, + storage: None, tasks: vec![], job_log: vec![], next_task_id: 1, diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs index 8e73c0ba..fd0f1fbb 100644 --- a/crates/engine/src/prefs.rs +++ b/crates/engine/src/prefs.rs @@ -816,6 +816,9 @@ pub enum Item { }, /// Read-only information line. Note(&'static str), + /// The browser's storage manager (web app only, `storage.*`): usage and quota, persistent + /// storage, Clear buttons. Not shown where there is no browser storage. + BrowserStorage, } /// One page of the Settings dialog. @@ -1032,6 +1035,7 @@ pub fn pages() -> Vec { s("disk.diskCacheMaxGb", "Maximum Disk Cache Size", Kind::Int(1, 100_000, "GB"), true), s("disk.diskCacheFolder", "Disk Cache Folder", Kind::Path, true), Button { label: "Empty Disk Cache", command: "edit.purge", params: r#"{"what":"disk"}"# }, + BrowserStorage, Section("Media Cache"), s("disk.mediaCacheFolder", "Database and Cache Folder", Kind::Path, true), s("disk.conformedMediaFolder", "Conformed Audio Folder", Kind::Path, true), @@ -1140,6 +1144,7 @@ pub fn pages_json() -> Value { Item::Labels => json!({"labels": "labels.N.name / labels.N.color (N = 0..15)"}), Item::AudioDevices { key } => json!({"type": "device", "key": key, "label": "Default Output"}), Item::Note(t) => json!({"note": t}), + Item::BrowserStorage => json!({"browserStorage": "storage.info / storage.persist / storage.clear (web app)"}), }) .collect(); json!({"id": p.id, "title": p.title, "items": items}) diff --git a/crates/engine/src/remote.rs b/crates/engine/src/remote.rs index ec0640ff..fb3e33e9 100644 --- a/crates/engine/src/remote.rs +++ b/crates/engine/src/remote.rs @@ -13,13 +13,23 @@ //! - [`FrameServer`] (worker side): applies syncs, relayed Roto Brush segmentations and render //! requests, and renders frames as premultiplied RGBA8 ([`rgba8_premultiplied`]). //! +//! - GPU frames: a worker with its own WebGPU device ([`FrameServer::accel`]) renders through +//! the GPU compositor and GPU effects. Its readbacks can't be waited for, so a frame renders +//! in passes ([`effectcraft_render::Accelerator::frame_begin`]): [`FrameServer::handle`] +//! runs the first, and while [`FrameServer::waiting`] the transport awaits the device and +//! calls [`FrameServer::resume`] for the next. Backend Auto picks CPU or GPU per comp from +//! the frames' total times ([`effectcraft_render::AutoPick`]). +//! - Disk cache: a render request may carry the frame's disk-cache key ([`FrameMsg::Render`] +//! `disk`); the transport stores the finished frame under it (the browser: in the Origin +//! Private File System) and reports [`FrameReply::Stored`]. +//! //! The transport (structured-clone messages, transferable pixel buffers) is the web crate's; //! everything here is portable and tested natively. use std::sync::Arc; use effectcraft_project::{ItemId, Project}; -use effectcraft_render::{ExprHost, FootageSource, Image, LayerCache, RenderOpts, Renderer}; +use effectcraft_render::{Accelerator, AutoKey, Backend, ExprHost, FootageSource, Image, LayerCache, RenderOpts, Renderer}; use effectcraft_time::Tick; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -120,8 +130,17 @@ pub enum FrameMsg { Project { revision: u64, project: String }, /// Changes from the worker's revision `from` to `revision`. Patch { from: u64, revision: u64, ops: Vec }, - /// Render a frame of the current project. - Render { id: u64, revision: u64, comp: ItemId, time: Tick, opts: Box }, + /// Render a frame of the current project. `disk`: the frame's disk-cache key (32 hex + /// digits) when the transport should store it. + Render { + id: u64, + revision: u64, + comp: ItemId, + time: Tick, + opts: Box, + #[serde(default, skip_serializing_if = "Option::is_none")] + disk: Option, + }, /// Roto Brush segmentations computed elsewhere (a propagation worker). Segs { segs: Vec }, /// Drop cached layer buffers (Edit ▸ Purge). @@ -133,8 +152,27 @@ pub enum FrameMsg { #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "camelCase")] pub enum FrameReply { - /// Premultiplied RGBA8 pixels of request `id`, rendered in `ms` milliseconds. - Frame { id: u64, width: u32, height: u32, ms: f64 }, + /// Premultiplied RGBA8 pixels of request `id`, rendered in `ms` milliseconds (`passes` + /// render passes, `gpu`: through the GPU accelerator). + Frame { + id: u64, + width: u32, + height: u32, + ms: f64, + #[serde(default)] + passes: u32, + #[serde(default)] + gpu: bool, + }, + /// The frame was stored in the disk cache under `key` (`bytes` on disk). + Stored { key: String, bytes: u64 }, + /// The worker's renderer: its GPU adapter, or why it has none. + Info { + #[serde(default)] + gpu: Option, + #[serde(default)] + error: Option, + }, /// Request `id` was not rendered (`stale`: the worker holds another revision). Failed { id: u64, error: String, stale: bool }, /// A sync could not be applied: the page must send the whole project again. @@ -189,14 +227,45 @@ pub struct FrameServer { pub footage: Arc, pub expr: Option>, pub cache: Arc, + /// The worker's own accelerator (a GPU device; see [`FrameServer::set_accel`]). + accel: Option>, doc: Value, project: Option>, revision: Option, + /// The frame being rendered in passes. + pending: Option, } +/// A frame whose readbacks are in flight. +struct Pending { + id: u64, + project: Arc, + comp: ItemId, + time: Tick, + opts: RenderOpts, + t0: web_time::Instant, + passes: u32, + /// Backend Auto's choice: (timing key, GPU). + auto: Option<(AutoKey, bool)>, +} + +/// Passes before a frame gives up on deferred readbacks and renders on the CPU. +pub const MAX_PASSES: u32 = 24; + impl FrameServer { pub fn new(footage: Arc, expr: Option>) -> FrameServer { - FrameServer { footage, expr, cache: Arc::new(LayerCache::default()), doc: Value::Null, project: None, revision: None } + FrameServer { footage, expr, cache: Arc::new(LayerCache::default()), accel: None, doc: Value::Null, project: None, revision: None, pending: None } + } + + /// Render with `accel` when the request's backend asks for it (the worker's GPU). Its miss + /// gate keeps placeholder buffers of unfinished passes out of the layer cache. + pub fn set_accel(&mut self, accel: Option>) { + self.cache.set_gate(accel.as_ref().and_then(|a| a.miss_gate())); + self.accel = accel; + } + + pub fn accel(&self) -> Option<&Arc> { + self.accel.as_ref() } pub fn revision(&self) -> Option { @@ -207,6 +276,11 @@ impl FrameServer { self.project.as_ref() } + /// A frame is waiting for readbacks: await the device, then [`FrameServer::resume`]. + pub fn waiting(&self) -> bool { + self.pending.is_some() + } + fn load(&mut self, revision: u64) -> Result<(), String> { let p = Project::from_json(&self.doc.to_string()).map_err(|e| e.to_string())?; self.project = Some(Arc::new(p)); @@ -214,7 +288,8 @@ impl FrameServer { Ok(()) } - /// Handle one message; returns the replies with their pixels. + /// Handle one message; returns the replies with their pixels. A render that needs more + /// passes returns nothing yet ([`FrameServer::waiting`]). pub fn handle(&mut self, msg: FrameMsg) -> Vec<(FrameReply, Option>)> { match msg { FrameMsg::Project { revision, project } => { @@ -246,7 +321,7 @@ impl FrameServer { self.cache.clear(); vec![] } - FrameMsg::Render { id, revision, comp, time, opts } => { + FrameMsg::Render { id, revision, comp, time, opts, .. } => { let fail = |error: String, stale: bool| vec![(FrameReply::Failed { id, error, stale }, None)]; if self.revision != Some(revision) { return fail(format!("the replica is at {:?}, not {revision}", self.revision), true); @@ -255,17 +330,60 @@ impl FrameServer { if p.comp(comp).is_none() { return fail(format!("no composition {}", comp.0), false); } - let t0 = web_time::Instant::now(); - let mut r = Renderer::new(&p, self.footage.as_ref(), *opts); - r.expr = self.expr.as_deref(); - r.cache = Some(&self.cache); - let img = r.comp_frame(comp, time); - let ms = t0.elapsed().as_secs_f64() * 1000.0; - vec![(FrameReply::Frame { id, width: img.width, height: img.height, ms }, Some(rgba8_premultiplied(&img)))] + let mut opts = *opts; + let mut auto = None; + if let Some(a) = &self.accel { + // Auto: CPU or GPU for the whole frame (all its passes), timed as a whole. + if opts.backend == Backend::Auto { + if !p.settings.gpu_acceleration { + opts.backend = Backend::Cpu; + } else if opts.roi.is_none() + && let Some(pick) = a.auto_pick() + { + let key = AutoKey::new(comp, opts.scale, false); + let gpu = pick.choose(key); + opts.backend = if gpu { Backend::Gpu } else { Backend::Cpu }; + auto = Some((key, gpu)); + } + } + a.frame_begin(); + } + self.pending = Some(Pending { id, project: p, comp, time, opts, t0: web_time::Instant::now(), passes: 0, auto }); + self.resume() } } } + /// Run the next pass of the frame in progress; its reply once it is done. + pub fn resume(&mut self) -> Vec<(FrameReply, Option>)> { + let Some(mut pr) = self.pending.take() else { return vec![] }; + pr.passes += 1; + let accel = self.accel.clone().filter(|_| pr.opts.backend != Backend::Cpu); + let give_up = pr.passes > MAX_PASSES; + let mut r = Renderer::new(&pr.project, self.footage.as_ref(), pr.opts); + r.expr = self.expr.as_deref(); + r.cache = Some(&self.cache); + if !give_up { + r.accel = accel.as_deref(); + } + if let Some(a) = r.accel { + a.pass_begin(); + } + let img = r.comp_frame(pr.comp, pr.time); + if r.accel.is_some_and(|a| a.pass_missed()) { + self.pending = Some(pr); + return vec![]; + } + let ms = pr.t0.elapsed().as_secs_f64() * 1000.0; + let gpu = r.accel.is_some() && r.active_accel().is_some(); + if let (Some((key, chose)), Some(a)) = (pr.auto, &self.accel) + && let Some(pick) = a.auto_pick() + { + pick.record(key, chose, ms); + } + vec![(FrameReply::Frame { id: pr.id, width: img.width, height: img.height, ms, passes: pr.passes, gpu }, Some(rgba8_premultiplied(&img)))] + } + fn lost(&mut self, error: String) -> Vec<(FrameReply, Option>)> { self.doc = Value::Null; self.project = None; @@ -332,7 +450,8 @@ mod tests { assert!(send(first, &mut w).is_empty()); assert!(m.sync(s.revision, &s.project).is_none(), "nothing new"); let t = Tick::from_seconds_f64(1.0); - let render = |w: &mut FrameServer, rev: u64, id: u64| w.handle(round(&FrameMsg::Render { id, revision: rev, comp, time: t, opts: Box::new(opts) })); + let render = + |w: &mut FrameServer, rev: u64, id: u64| w.handle(round(&FrameMsg::Render { id, revision: rev, comp, time: t, opts: Box::new(opts), disk: None })); let out = render(&mut w, s.revision, 1); let (FrameReply::Frame { id, width, height, .. }, Some(px)) = &out[0] else { panic!("{:?}", out[0].0) }; assert_eq!((*id, *width as usize * *height as usize * 4), (1, px.len())); @@ -367,6 +486,7 @@ mod tests { comp: ItemId(2), time: Tick::from_seconds_f64(0.5), opts: Box::new(RenderOpts { scale: 0.5, draft: true, roi: Some([1.0, 2.0, 3.0, 4.0]), ..Default::default() }), + disk: Some(format!("{:032x}", 0xabcdu128)), }, ] { let a = serde_json::to_string(&m).unwrap(); @@ -374,11 +494,145 @@ mod tests { assert!(a.starts_with("{\"type\":"), "{a}"); } for r in [ - FrameReply::Frame { id: 1, width: 2, height: 3, ms: 4.5 }, + FrameReply::Frame { id: 1, width: 2, height: 3, ms: 4.5, passes: 2, gpu: true }, + FrameReply::Stored { key: "00ff".into(), bytes: 9 }, + FrameReply::Info { gpu: Some("Adapter (BrowserWebGpu)".into()), error: None }, FrameReply::Failed { id: 1, error: "x".into(), stale: true }, FrameReply::Resync { error: "y".into() }, ] { assert_eq!(round(&r), r); } } + + /// An accelerator with deferred readbacks: a GPU effect chain is "read back" one + /// [`Deferred::settle`] after it was first asked for (computed with the CPU effects, so the + /// finished frame equals the CPU render); until then it misses and returns its input. + #[derive(Default)] + struct Deferred { + ready: std::sync::Mutex>, + asked: std::sync::Mutex>, + gate: Arc, + chains: std::sync::atomic::AtomicUsize, + auto: effectcraft_render::AutoPick, + } + + impl Deferred { + fn settle(&self) { + let asked: Vec = self.asked.lock().unwrap().drain().collect(); + self.ready.lock().unwrap().extend(asked); + } + } + + impl Accelerator for Deferred { + fn name(&self) -> String { + "deferred".into() + } + fn comp_frame(&self, _: &Renderer, _: ItemId, _: Tick) -> Option { + None + } + fn supports_effect(&self, id: &str) -> bool { + effectcraft_effects::GPU_EFFECTS.contains(&id) + } + fn effects(&self, chain: &[effectcraft_render::FxStep], buf: &effectcraft_effects::Buf, levels: Option) -> Option { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + for s in chain { + s.spec.id.hash(&mut h); + s.ctx.time.to_bits().hash(&mut h); + } + (buf.img.width, buf.img.height).hash(&mut h); + for p in &buf.img.data { + for c in p { + c.to_bits().hash(&mut h); + } + } + let key = h.finish(); + if !self.ready.lock().unwrap().contains(&key) { + self.asked.lock().unwrap().insert(key); + self.gate.store(true, std::sync::atomic::Ordering::Relaxed); + return Some(buf.clone()); + } + self.chains.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let mut b = buf.clone(); + for s in chain { + b = effectcraft_effects::apply(s.spec, &s.ctx, b); + if let Some(l) = levels { + effectcraft_render::color::quantize(&mut b.img, l); + } + } + Some(b) + } + fn auto_pick(&self) -> Option<&effectcraft_render::AutoPick> { + Some(&self.auto) + } + fn frame_begin(&self) { + self.ready.lock().unwrap().clear(); + } + fn pass_begin(&self) { + self.gate.store(false, std::sync::atomic::Ordering::Relaxed); + } + fn pass_missed(&self) -> bool { + self.gate.load(std::sync::atomic::Ordering::Relaxed) + } + fn miss_gate(&self) -> Option> { + Some(self.gate.clone()) + } + } + + #[test] + fn deferred_readbacks_render_in_passes() { + let mut s = Session::default(); + s.execute("file.openDemoProject", json!({})).unwrap(); + let comp = s.active_comp_id().unwrap(); + s.execute("layer.newSolid", json!({"color": "#3388ff", "name": "Blue", "width": 200, "height": 120})).unwrap(); + s.execute("effect.apply", json!({"effect": "ec.blur.gaussian"})).unwrap(); + s.execute("effect.apply", json!({"effect": "ec.stylize.glow"})).unwrap(); + s.execute("layer.newSolid", json!({"color": "#ffcc00", "name": "Yellow", "width": 80, "height": 80})).unwrap(); + s.execute("effect.apply", json!({"effect": "ec.color.levels"})).unwrap(); + let opts = RenderOpts { scale: 0.25, backend: Backend::Gpu, ..Default::default() }; + let t = Tick::from_seconds_f64(1.0); + let mut w = server(&s); + let acc = Arc::new(Deferred::default()); + w.set_accel(Some(acc.clone())); + let mut m = Mirror::default(); + w.handle(round(&m.sync(s.revision, &s.project).unwrap())); + let req = |id| round(&FrameMsg::Render { id, revision: s.revision, comp, time: t, opts: Box::new(opts), disk: None }); + assert!(w.handle(req(1)).is_empty() && w.waiting(), "the first pass misses"); + let mut out = vec![]; + for _ in 0..10 { + acc.settle(); + out = w.resume(); + if !w.waiting() { + break; + } + } + let (FrameReply::Frame { id: 1, passes, gpu: true, .. }, Some(px)) = &out[0] else { panic!("{:?}", out.first().map(|o| &o.0)) }; + assert!(*passes >= 2, "{passes}"); + assert!(acc.chains.load(std::sync::atomic::Ordering::Relaxed) >= 2); + // The same pixels as the CPU: no placeholder survived, in the frame or the layer cache. + assert_eq!(*px, local(&s, comp, t, RenderOpts { backend: Backend::Cpu, ..opts })); + let out = w.handle(req(2)); + let out = if out.is_empty() { + acc.settle(); + w.resume() + } else { + out + }; + assert_eq!(out[0].1.as_deref(), Some(&px[..])); + // CPU requests never touch the accelerator; Auto times whole frames. + let cpu = RenderOpts { backend: Backend::Cpu, ..opts }; + let out = w.handle(round(&FrameMsg::Render { id: 3, revision: s.revision, comp, time: t, opts: Box::new(cpu), disk: None })); + assert!(matches!(out[0].0, FrameReply::Frame { id: 3, passes: 1, gpu: false, .. })); + for id in 4..12 { + let auto = RenderOpts { backend: Backend::Auto, ..opts }; + let mut out = w.handle(round(&FrameMsg::Render { id, revision: s.revision, comp, time: t, opts: Box::new(auto), disk: None })); + while w.waiting() { + acc.settle(); + out = w.resume(); + } + assert_eq!(out[0].1.as_deref(), Some(&px[..]), "frame {id}"); + } + let st = acc.auto.stats(effectcraft_render::AutoKey::new(comp, opts.scale, false)).unwrap(); + assert!(st.cpu_frames > 0 && st.gpu_frames > 0, "{st:?}"); + } } diff --git a/crates/engine/src/storage.rs b/crates/engine/src/storage.rs new file mode 100644 index 00000000..09c854d5 --- /dev/null +++ b/crates/engine/src/storage.rs @@ -0,0 +1,133 @@ +//! Browser storage management: what the web app keeps in the browser (projects, imported media, +//! auto-saves and settings in the Origin Private File System or IndexedDB; the disk cache's +//! frames in the Origin Private File System), how much of the origin's quota that uses, whether +//! the browser keeps it under storage pressure, and clearing it. +//! +//! The browser's storage APIs are asynchronous and the engine's commands are not, so a +//! [`StorageHost`] answers from its latest snapshot (refreshing it in the background) and +//! starts requests that finish later; the snapshot reports their outcome. +//! +//! Commands (agents, Settings ▸ Disk ▸ Browser Storage): `storage.info`, `storage.persist`, +//! `storage.clear {what}`. Without a host (the desktop) `storage.info` reports +//! `{available: false}` and the others fail. + +use serde_json::{Value, json}; + +use crate::commands::{CommandSpec, str_p}; +use crate::{Result, Session, cmd}; + +/// What can be cleared ([`StorageHost::clear`]). +pub const CLEARABLE: &[&str] = &["diskCache", "media", "projects", "autoSaves", "all"]; + +/// The browser's storage, as the web app manages it. +pub trait StorageHost: Send + Sync { + /// The latest snapshot: `{backend, usage, quota, persisted, files: {count, bytes, media, + /// projects, autoSaves}, diskCache: {enabled, entries, bytes, maxBytes, hits, misses, writes, + /// evictions}}` (`usage` / `quota` from `navigator.storage.estimate()`, in bytes). Asks for + /// a fresh one. + fn info(&self) -> Value; + /// Ask the browser to keep the storage under pressure (`navigator.storage.persist()`); the + /// answer arrives in a later [`StorageHost::info`] (`persisted`). + fn request_persist(&self) -> Value; + /// Delete stored data: `diskCache` (cached frames), `media` (imported footage), `projects` + /// (saved projects), `autoSaves`, or `all` of them (settings stay). Returns what was + /// removed: `{cleared, entries, bytes}`. + fn clear(&self, what: &str) -> std::result::Result; +} + +fn host(s: &Session, cmd: &str) -> Result> { + s.storage.clone().ok_or_else(|| crate::commands::bad(cmd, "browser storage is only managed in the web app")) +} + +fn info(s: &mut Session, _: &Value) -> Result { + Ok(match &s.storage { + Some(h) => { + let mut v = h.info(); + if let Some(o) = v.as_object_mut() { + o.insert("available".into(), json!(true)); + } + v + } + None => json!({"available": false}), + }) +} + +fn persist(s: &mut Session, _: &Value) -> Result { + Ok(host(s, "storage.persist")?.request_persist()) +} + +fn clear(s: &mut Session, p: &Value) -> Result { + let what = str_p(p, "what").unwrap_or("diskCache"); + if !CLEARABLE.contains(&what) { + return Err(crate::commands::bad("storage.clear", format!("what: one of {}", CLEARABLE.join(", ")))); + } + let h = host(s, "storage.clear")?; + let r = h.clear(what).map_err(|e| crate::commands::bad("storage.clear", e))?; + s.toast(format!("Cleared browser storage: {what}")); + Ok(r) +} + +fn has_host(s: &Session) -> std::result::Result<(), String> { + if s.storage.is_some() { Ok(()) } else { Err("browser storage is only managed in the web app".into()) } +} + +pub(crate) fn specs() -> Vec { + vec![ + crate::query!( + "storage.info", + "Browser Storage", + "{} → {available, backend, usage, quota, persisted, files: {count, bytes, media, projects, autoSaves}, diskCache: {enabled, entries, bytes, maxBytes, hits, misses, writes, evictions}}", + info + ), + cmd!("storage.persist", "Request Persistent Storage", [], None, "{} → {requested, persisted}", has_host, persist), + cmd!("storage.clear", "Clear Browser Storage", [], None, "{what: diskCache|media|projects|autoSaves|all} → {cleared, entries, bytes}", has_host, clear), + ] +} + +#[cfg(test)] +mod tests { + use std::sync::{Arc, Mutex}; + + use super::*; + + #[derive(Default)] + struct Mock { + cleared: Mutex>, + persisted: Mutex, + } + + impl StorageHost for Mock { + fn info(&self) -> Value { + json!({"backend": "opfs", "usage": 1000, "quota": 100000, "persisted": *self.persisted.lock().unwrap(), + "diskCache": {"enabled": true, "entries": 3, "bytes": 300, "maxBytes": 1 << 30}}) + } + fn request_persist(&self) -> Value { + *self.persisted.lock().unwrap() = true; + json!({"requested": true}) + } + fn clear(&self, what: &str) -> std::result::Result { + self.cleared.lock().unwrap().push(what.into()); + Ok(json!({"cleared": what, "entries": 3, "bytes": 300})) + } + } + + #[test] + fn storage_commands_go_to_the_host() { + let mut s = Session::default(); + assert_eq!(s.execute_checked("storage.info", json!({})).unwrap(), json!({"available": false})); + assert!(s.execute_checked("storage.clear", json!({"what": "diskCache"})).is_err()); + let m = Arc::new(Mock::default()); + s.storage = Some(m.clone()); + let i = s.execute_checked("storage.info", json!({})).unwrap(); + assert_eq!((i["available"].clone(), i["backend"].clone(), i["persisted"].clone()), (json!(true), json!("opfs"), json!(false))); + assert_eq!(s.execute_checked("storage.persist", json!({})).unwrap()["requested"], json!(true)); + assert_eq!(s.execute_checked("storage.info", json!({})).unwrap()["persisted"], json!(true)); + assert!(s.execute_checked("storage.clear", json!({"what": "everything"})).is_err()); + assert_eq!(s.execute_checked("storage.clear", json!({"what": "media"})).unwrap()["cleared"], json!("media")); + // The disk cache's own commands use the browser's disk cache when there is no folder. + let st = s.execute_checked("cache.diskStats", json!({})).unwrap(); + assert_eq!((st["enabled"].clone(), st["entries"].clone()), (json!(true), json!(3))); + assert_eq!(s.execute_checked("edit.purge", json!({"what": "disk"})).unwrap()["diskEntries"], json!(3)); + assert_eq!(*m.cleared.lock().unwrap(), vec!["media".to_string(), "diskCache".to_string()]); + } +} diff --git a/crates/gpu/src/adv3d.rs b/crates/gpu/src/adv3d.rs index 677db3e5..1b0e8dee 100644 --- a/crates/gpu/src/adv3d.rs +++ b/crates/gpu/src/adv3d.rs @@ -325,7 +325,7 @@ fn raster_into(e: &mut Enc, s: &Scene) -> Option<(wgpu::Texture, wgpu::Texture)> /// Rasterise a scene and read it back ([`effectcraft_render::Accelerator::raster_3d`]). `None` /// when the device can't (no readback, size or buffer limits). pub(crate) fn render(g: &GpuContext, s: &Scene) -> Option { - if !g.can_readback() { + if !g.can_wait() { return None; } let mut e = Enc::new(g); @@ -520,7 +520,7 @@ fn storage_buf(g: &GpuContext, size: u64, label: &str) -> wgpu::Buffer { /// Read a storage buffer back (submits and waits). fn read_buffer(e: &mut Enc, buf: &wgpu::Buffer, size: u64) -> Option> { - if !e.g.can_readback() { + if !e.g.can_wait() { return None; } let rb = e.g.device.create_buffer(&wgpu::BufferDescriptor { @@ -560,7 +560,7 @@ const BIG: f32 = 3.0e38; fn resolve_run(e: &mut Enc, prep: &Prepared) -> Option> { let (w, h) = prep.out; let g = e.g; - if !g.fits(w, h) || (prep.dof.is_some() && !g.can_readback()) { + if !g.fits(w, h) || (prep.dof.is_some() && !g.can_wait()) { return None; } let px = w as u64 * h as u64; @@ -673,7 +673,7 @@ fn finish(e: &mut Enc, r: &Resolved, encode: bool, canvas: Option<&GpuImage>) -> /// [`effectcraft_render::Accelerator::render_3d`]: the whole prepared run on the GPU, read /// back. pub(crate) fn render_prepared(g: &GpuContext, prep: &Prepared) -> Option { - if !g.can_readback() { + if !g.can_wait() { return None; } let mut e = Enc::new(g); diff --git a/crates/gpu/src/context.rs b/crates/gpu/src/context.rs index 3d8d2916..3781b5ee 100644 --- a/crates/gpu/src/context.rs +++ b/crates/gpu/src/context.rs @@ -262,6 +262,8 @@ pub struct GpuContext { /// GPU particle pipeline (built on first use) and simulation checkpoints. pub(crate) particles: crate::particles::PipesCell, pub(crate) particle_states: crate::particles::StatesCell, + /// Deferred readbacks (a browser worker's WebGPU device), see [`crate::deferred`]. + pub(crate) deferred: Option>, } fn tex_entry(binding: u32) -> wgpu::BindGroupLayoutEntry { @@ -442,27 +444,43 @@ impl GpuContext { adv3d: std::sync::OnceLock::new(), particles: Default::default(), particle_states: Default::default(), + deferred: None, }) } - /// A device of its own on the best adapter (CLI, tests, benchmarks). `None` when no - /// adapter qualifies. - #[cfg(not(target_arch = "wasm32"))] - pub fn headless() -> Option { - let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); - let adapter = pollster::block_on( - instance.request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() }), - ) - .ok()?; + /// Switch to deferred readbacks (see [`crate::deferred`]): readbacks are never waited for. + pub fn set_deferred(&mut self, on: bool) { + self.deferred = on.then(Default::default); + } + + pub fn deferred(&self) -> Option<&Arc> { + self.deferred.as_ref() + } + + /// A device of its own on the best adapter, without blocking (a browser worker: WebGPU + /// only; natively any backend). `Err` says why there is none. + pub async fn request() -> Result { + #[allow(unused_mut)] + let mut desc = wgpu::InstanceDescriptor::new_without_display_handle(); + #[cfg(target_arch = "wasm32")] + { + desc.backends = wgpu::Backends::BROWSER_WEBGPU; + } + let instance = wgpu::Instance::new(desc); + let adapter = instance + .request_adapter(&wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() }) + .await + .map_err(|e| format!("no adapter: {e}"))?; let limits = adapter.limits(); let required_limits = wgpu::Limits { max_texture_dimension_2d: limits.max_texture_dimension_2d.min(16384), max_buffer_size: limits.max_buffer_size, ..wgpu::Limits::default() }; - let (device, queue) = - pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor { label: Some("effectcraft gpu"), required_limits, ..Default::default() })) - .ok()?; + let (device, queue) = adapter + .request_device(&wgpu::DeviceDescriptor { label: Some("effectcraft gpu"), required_limits, ..Default::default() }) + .await + .map_err(|e| format!("no device: {e}"))?; device.on_uncaptured_error(Arc::new(|e| { // Under test a validation error (a WGSL typo invalidates every kernel) fails loudly. if cfg!(test) { @@ -470,12 +488,26 @@ impl GpuContext { } log::error!("wgpu: {e}") })); - GpuContext::new(&adapter, device, queue).map_err(|e| log::info!("gpu: {e}")).ok() + GpuContext::new(&adapter, device, queue) } - /// Readback (GPU → CPU) waits for the device; impossible on the browser's main thread. + /// A device of its own on the best adapter (CLI, tests, benchmarks). `None` when no + /// adapter qualifies. + #[cfg(not(target_arch = "wasm32"))] + pub fn headless() -> Option { + pollster::block_on(GpuContext::request()).map_err(|e| log::info!("gpu: {e}")).ok() + } + + /// Readbacks (GPU → CPU) are possible: waited for natively, or deferred (a browser + /// worker, [`crate::deferred`]). Impossible on the browser's main thread. pub fn can_readback(&self) -> bool { - cfg!(not(target_arch = "wasm32")) + self.can_wait() || self.deferred.is_some() + } + + /// Readbacks can be waited for (natively, unless deferred). Steps that read back without a + /// key ([`crate::deferred`]) need this. + pub fn can_wait(&self) -> bool { + cfg!(not(target_arch = "wasm32")) && self.deferred.is_none() } pub(crate) fn fits(&self, w: u32, h: u32) -> bool { @@ -792,7 +824,7 @@ impl<'g> Enc<'g> { /// [`Enc::read_texture`] into `out` (`w × bpp × h` bytes), through a reused staging buffer. fn read_texture_into(&mut self, texture: &wgpu::Texture, w: u32, h: u32, bpp: u32, out: &mut [u8]) -> Option<()> { - if !self.g.can_readback() { + if !self.g.can_wait() { return None; } self.g.transfers.count(false, out.len()); @@ -879,6 +911,25 @@ impl<'g> Enc<'g> { let _ = self.g.device.poll(wgpu::PollType::Poll); } + /// A deferred readback of `img` (RGBA f32) under `key` (see [`crate::deferred`]): the bytes + /// once they arrived (`Some(None)`: the readback failed), else `None` after starting the + /// copy if needed and marking the pass missed. + pub(crate) fn read_keyed(&mut self, img: &GpuImage, key: u128, offset: [f64; 2], scale: f64) -> Option> { + use crate::deferred::Lookup; + let d = self.g.deferred.clone()?; + match d.lookup(key) { + Lookup::Ready(r) => return Some(r), + Lookup::InFlight => {} + Lookup::Absent => { + self.g.transfers.count(false, img.width as usize * img.height as usize * 16); + let done = d.start(key, img.width, img.height, offset, scale); + self.read_texture_async(&img.texture, img.width, img.height, 16, done); + } + } + d.miss(); + None + } + /// A data buffer for kernels that read tables (curves). pub fn data(&self, v: &[f32]) -> wgpu::Buffer { let mut bytes = Vec::with_capacity(v.len().max(4) * 4); diff --git a/crates/gpu/src/deferred.rs b/crates/gpu/src/deferred.rs new file mode 100644 index 00000000..1dab0654 --- /dev/null +++ b/crates/gpu/src/deferred.rs @@ -0,0 +1,293 @@ +//! Deferred readbacks: rendering on a device whose readbacks cannot be waited for (WebGPU in a +//! browser worker: a buffer maps only once the worker returns to its event loop). +//! +//! A frame renders in passes (see [`effectcraft_render::Accelerator::frame_begin`]). Every +//! readback the renderer needs is keyed by what determines its pixels: a GPU effect chain by +//! its input buffer's pixels and the chain's parameters, the top-level frame by comp, time and +//! render options. A key seen for the first time records the GPU work, starts an asynchronous +//! copy (`map_async`) and *misses*: the step returns a placeholder and the pass is marked missed +//! (raising the gate that keeps placeholder-derived buffers out of the layer cache). The caller +//! awaits [`Deferred::settled`] and renders again; keys whose bytes arrived are served from +//! here. A pass without a miss is the finished frame. +//! +//! Correctness: a result is only computed from genuine inputs. Pixels a chain reads are part +//! of its key, and a chain whose kernels read other data through the effect host (other layers, +//! other times) is not started when that data missed while it ran (the miss counter moved). + +use std::collections::{HashMap, HashSet}; +use std::future::Future; +use std::hash::{Hash, Hasher}; +use std::pin::Pin; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; +use std::task::{Context, Poll, Waker}; + +use effectcraft_effects::Buf; +use effectcraft_render::FxStep; + +/// Bytes read back for one key, with the geometry they describe. +#[derive(Clone, Debug)] +pub(crate) struct Readback { + pub bytes: Vec, + pub width: u32, + pub height: u32, + /// Layer buffer offset and scale (chains). + pub offset: [f64; 2], + pub scale: f64, +} + +/// What [`Deferred::lookup`] knows about a key. +pub(crate) enum Lookup { + /// The bytes arrived (`None`: the readback failed; render this step another way). + Ready(Option), + /// Started in this frame, not arrived yet. + InFlight, + /// Never asked for in this frame. + Absent, +} + +#[derive(Default)] +struct State { + /// Frame generation: results of an older frame's readbacks are dropped. + generation: u64, + ready: HashMap>, + waiting: HashSet, + in_flight: usize, + /// Misses so far in this frame (a counter, so a chain sees misses during its own work). + misses: u64, + wakers: Vec, +} + +/// Deferred readback state of a device (see the module docs). +#[derive(Default)] +pub struct Deferred { + st: Mutex, + /// Raised from the pass's first miss to its end. + gate: Arc, + /// Passes and readbacks (diagnostics). + pub(crate) passes: std::sync::atomic::AtomicU64, + pub(crate) readbacks: std::sync::atomic::AtomicU64, +} + +impl Deferred { + pub fn frame_begin(&self) { + if let Ok(mut s) = self.st.lock() { + s.generation += 1; + s.ready.clear(); + s.waiting.clear(); + s.misses = 0; + } + self.gate.store(false, Ordering::Relaxed); + } + + pub fn pass_begin(&self) { + self.gate.store(false, Ordering::Relaxed); + self.passes.fetch_add(1, Ordering::Relaxed); + } + + pub fn missed(&self) -> bool { + self.gate.load(Ordering::Relaxed) + } + + pub fn gate(&self) -> Arc { + self.gate.clone() + } + + /// Misses so far in this frame. + pub(crate) fn misses(&self) -> u64 { + self.st.lock().map(|s| s.misses).unwrap_or(0) + } + + /// Mark the pass as missed. + pub(crate) fn miss(&self) { + if let Ok(mut s) = self.st.lock() { + s.misses += 1; + } + self.gate.store(true, Ordering::Relaxed); + } + + pub(crate) fn lookup(&self, key: u128) -> Lookup { + let Ok(s) = self.st.lock() else { return Lookup::Ready(None) }; + if let Some(r) = s.ready.get(&key) { + return Lookup::Ready(r.clone()); + } + if s.waiting.contains(&key) { Lookup::InFlight } else { Lookup::Absent } + } + + /// A readback for `key` is being started; returns the callback that stores its bytes. + pub(crate) fn start(self: &Arc, key: u128, width: u32, height: u32, offset: [f64; 2], scale: f64) -> impl FnOnce(Option>) + 'static { + let generation = match self.st.lock() { + Ok(mut s) => { + s.waiting.insert(key); + s.in_flight += 1; + s.generation + } + Err(_) => 0, + }; + self.readbacks.fetch_add(1, Ordering::Relaxed); + let me = self.clone(); + move |bytes: Option>| { + let wakers = match me.st.lock() { + Ok(mut s) => { + s.in_flight = s.in_flight.saturating_sub(1); + if s.generation == generation { + s.waiting.remove(&key); + s.ready.insert(key, bytes.map(|bytes| Readback { bytes, width, height, offset, scale })); + } + if s.in_flight == 0 { std::mem::take(&mut s.wakers) } else { vec![] } + } + Err(_) => vec![], + }; + for w in wakers { + w.wake(); + } + } + } + + /// Readbacks still in flight. + pub fn in_flight(&self) -> usize { + self.st.lock().map(|s| s.in_flight).unwrap_or(0) + } + + /// Resolves once no readback is in flight (on the web the browser's event loop delivers + /// them; natively a device poll does). + pub fn settled(self: &Arc) -> Settled { + Settled(self.clone()) + } +} + +/// See [`Deferred::settled`]. +pub struct Settled(Arc); + +impl Future for Settled { + type Output = (); + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { + let Ok(mut s) = self.0.st.lock() else { return Poll::Ready(()) }; + if s.in_flight == 0 { + return Poll::Ready(()); + } + s.wakers.push(cx.waker().clone()); + Poll::Pending + } +} + +/// 128-bit key from two differently seeded std hashers (deterministic within a process). +pub(crate) struct Key(std::collections::hash_map::DefaultHasher, std::collections::hash_map::DefaultHasher); + +impl Key { + pub fn new(tag: u64) -> Key { + let mut a = std::collections::hash_map::DefaultHasher::new(); + let mut b = std::collections::hash_map::DefaultHasher::new(); + tag.hash(&mut a); + (tag ^ 0x9e37_79b9_7f4a_7c15).hash(&mut b); + 0xa5u8.hash(&mut b); + Key(a, b) + } + + pub fn bytes(&mut self, v: &[u8]) { + self.0.write(v); + self.1.write(v); + } + + pub fn u64(&mut self, v: u64) { + self.bytes(&v.to_le_bytes()); + } + + pub fn f64(&mut self, v: f64) { + self.u64(v.to_bits()); + } + + pub fn debug(&mut self, v: &impl std::fmt::Debug) { + self.bytes(format!("{v:?}").as_bytes()); + self.bytes(&[0xff]); + } + + pub fn finish(&self) -> u128 { + ((self.0.finish() as u128) << 64) | self.1.finish() as u128 + } +} + +/// Key of a GPU effect chain on `buf`: the input pixels and geometry, the bit depth, and every +/// step's effect, parameters (sorted) and context. +pub(crate) fn chain_key(chain: &[FxStep], buf: &Buf, levels: Option) -> u128 { + let mut k = Key::new(1); + k.u64(buf.img.width as u64); + k.u64(buf.img.height as u64); + k.bytes(bytemuck::cast_slice(&buf.img.data)); + k.f64(buf.offset[0]); + k.f64(buf.offset[1]); + k.f64(buf.scale); + k.debug(&levels); + for s in chain { + let c = &s.ctx; + k.bytes(s.spec.id.as_bytes()); + let mut names: Vec<&String> = c.params.values.keys().collect(); + names.sort(); + for n in names { + k.bytes(n.as_bytes()); + k.debug(&c.params.values[n]); + } + k.f64(c.time); + k.f64(c.layer_size[0]); + k.f64(c.layer_size[1]); + k.u64(c.seed as u64); + k.u64(c.adjustment as u64); + let e = &c.env; + k.debug(&e.masks); + k.u64(e.host.is_some() as u64); + k.f64(e.comp_time); + k.f64(e.frame_rate); + k.u64(e.effect_index as u64); + k.debug(&e.working_space); + k.u64(e.working_linear as u64); + k.debug(&e.shutter); + } + k.finish() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn keys_are_stable_and_distinct() { + let mut a = Key::new(1); + a.bytes(b"abc"); + let mut b = Key::new(1); + b.bytes(b"abc"); + let mut c = Key::new(2); + c.bytes(b"abc"); + assert_eq!(a.finish(), b.finish()); + assert_ne!(a.finish(), c.finish()); + } + + #[test] + fn readbacks_resolve_and_old_frames_are_dropped() { + let d = Arc::new(Deferred::default()); + d.frame_begin(); + d.pass_begin(); + assert!(matches!(d.lookup(7), Lookup::Absent)); + let done = d.start(7, 2, 1, [1.0, 2.0], 0.5); + d.miss(); + assert!(d.missed() && d.gate().load(Ordering::Relaxed)); + assert!(matches!(d.lookup(7), Lookup::InFlight)); + assert_eq!(d.in_flight(), 1); + // The settle future wakes when the last readback lands. + let mut fut = std::pin::pin!(d.settled()); + let waker = Waker::noop(); + assert!(fut.as_mut().poll(&mut Context::from_waker(waker)).is_pending()); + done(Some(vec![1, 2, 3, 4, 5, 6, 7, 8])); + assert!(fut.as_mut().poll(&mut Context::from_waker(waker)).is_ready()); + let Lookup::Ready(Some(r)) = d.lookup(7) else { panic!("not ready") }; + assert_eq!((r.width, r.height, r.offset, r.scale, r.bytes.len()), (2, 1, [1.0, 2.0], 0.5, 8)); + d.pass_begin(); + assert!(!d.missed()); + // A readback of the previous frame arriving late is dropped. + let late = d.start(9, 1, 1, [0.0; 2], 1.0); + d.frame_begin(); + late(Some(vec![0; 4])); + assert!(matches!(d.lookup(9), Lookup::Absent)); + assert!(matches!(d.lookup(7), Lookup::Absent)); + assert_eq!(d.in_flight(), 0); + } +} diff --git a/crates/gpu/src/effects.rs b/crates/gpu/src/effects.rs index d8ab4746..ef18e17e 100644 --- a/crates/gpu/src/effects.rs +++ b/crates/gpu/src/effects.rs @@ -51,6 +51,20 @@ pub(crate) fn run_chain(e: &mut Enc, chain: &[FxStep], buf: &Buf, levels: Option if !e.g.can_readback() || buf.img.is_empty() { return None; } + // Deferred readbacks (a browser worker): served by key once the bytes arrived. + let deferred = e.g.deferred.clone(); + let key = deferred.as_ref().map(|_| crate::deferred::chain_key(chain, buf, levels)); + if let (Some(d), Some(k)) = (&deferred, key) { + match d.lookup(k) { + crate::deferred::Lookup::Ready(r) => return r.and_then(readback_buf), + crate::deferred::Lookup::InFlight => { + d.miss(); + return Some(buf.clone()); + } + crate::deferred::Lookup::Absent => {} + } + } + let misses = deferred.as_ref().map(|d| d.misses()); let mut b = GBuf { img: e.g.upload_image(&buf.img)?, offset: buf.offset, scale: buf.scale }; for step in chain { b = apply(e, step.spec.id, &step.ctx, b)?; @@ -58,10 +72,30 @@ pub(crate) fn run_chain(e: &mut Enc, chain: &[FxStep], buf: &Buf, levels: Option b.img = ops::quantize(e, &b.img, l); } } + if let (Some(d), Some(k)) = (&deferred, key) { + // Data the kernels fetched through the effect host missed meanwhile: not genuine. + if Some(d.misses()) != misses { + d.miss(); + return Some(buf.clone()); + } + // The placeholder (the input) keeps this pass cheap; the next pass gets the result. + return match e.read_keyed(&b.img, k, b.offset, b.scale) { + Some(r) => r.and_then(readback_buf), + None => Some(buf.clone()), + }; + } let img = e.download(&b.img)?; Some(Buf { img, offset: b.offset, scale: b.scale }) } +/// A layer buffer from deferred readback bytes (RGBA f32). +fn readback_buf(r: crate::deferred::Readback) -> Option { + let mut img = effectcraft_raster::Image::new(r.width, r.height); + let dst: &mut [u8] = bytemuck::cast_slice_mut(&mut img.data); + (dst.len() == r.bytes.len()).then(|| dst.copy_from_slice(&r.bytes))?; + Some(Buf { img, offset: r.offset, scale: r.scale }) +} + /// An effect stack running on a GPU-resident image (adjustment layers): GPU chains stay on /// the device; CPU effects read the image back, run, and upload the result. pub(crate) struct GpuFx<'e, 'g> { diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs index ca9bdbfb..9d687342 100644 --- a/crates/gpu/src/lib.rs +++ b/crates/gpu/src/lib.rs @@ -46,6 +46,7 @@ mod adv3d; mod bokeh; mod classic3d; mod context; +pub mod deferred; mod effects; mod fx_color; mod fx_distort; @@ -105,6 +106,44 @@ impl Gpu { Gpu { ctx: Arc::new(ctx), auto: Default::default() } } + /// A device of its own without blocking, with deferred readbacks (a browser worker, see + /// [`deferred`]); natively the same device works too (tests). `Err` says why there is none. + pub async fn request_deferred() -> Result { + let mut ctx = GpuContext::request().await?; + ctx.set_deferred(true); + Ok(Gpu::from_context(ctx)) + } + + /// [`Gpu::request_deferred`] blocking (tests of the browser worker's path, natively). + #[cfg(not(target_arch = "wasm32"))] + pub fn headless_deferred() -> Option { + pollster::block_on(Gpu::request_deferred()).map_err(|e| log::info!("gpu: {e}")).ok() + } + + /// The deferred readback state, when readbacks are deferred. + pub fn deferred(&self) -> Option<&Arc> { + self.ctx.deferred() + } + + /// Resolves once no deferred readback is in flight (immediately without deferred readbacks). + pub async fn settled(&self) { + if let Some(d) = self.ctx.deferred() { + d.settled().await; + } + } + + /// Deliver finished readbacks without blocking (native; the browser delivers them itself). + pub fn poll(&self) { + #[cfg(not(target_arch = "wasm32"))] + let _ = self.ctx.device.poll(wgpu::PollType::Poll); + } + + /// `{passes, readbacks, inFlight}` of deferred rendering (diagnostics). + pub fn deferred_stats(&self) -> Option<(u64, u64, usize)> { + let d = self.ctx.deferred()?; + Some((d.passes.load(std::sync::atomic::Ordering::Relaxed), d.readbacks.load(std::sync::atomic::Ordering::Relaxed), d.in_flight())) + } + pub fn context(&self) -> &GpuContext { &self.ctx } @@ -139,11 +178,50 @@ impl Gpu { if !self.ctx.can_readback() { return None; } + if let Some(d) = self.ctx.deferred.clone() { + return self.render_deferred(&d, r, comp, t); + } let mut e = Enc::new(&self.ctx); let img = walk::render(&mut e, r, comp, t)?; e.download(&img) } + /// [`Gpu::render`] with deferred readbacks: the frame's readback is keyed by comp, time and + /// render options; until it arrives (or while layer steps still miss) the pass gets a + /// placeholder. + fn render_deferred(&self, d: &Arc, r: &Renderer, comp: ItemId, t: Tick) -> Option { + let mut k = deferred::Key::new(2); + k.u64(comp.0); + k.u64(t.0 as u64); + k.debug(&r.opts); + let key = k.finish(); + let image = |rb: deferred::Readback| -> Option { + let mut img = Image::new(rb.width, rb.height); + let dst: &mut [u8] = bytemuck::cast_slice_mut(&mut img.data); + (dst.len() == rb.bytes.len()).then(|| dst.copy_from_slice(&rb.bytes))?; + Some(img) + }; + match d.lookup(key) { + deferred::Lookup::Ready(rb) => return rb.and_then(image), + deferred::Lookup::InFlight => { + d.miss(); + return Some(Image::new(1, 1)); + } + deferred::Lookup::Absent => {} + } + let misses = d.misses(); + let mut e = Enc::new(&self.ctx); + let img = walk::render(&mut e, r, comp, t)?; + if d.misses() != misses { + // Layer steps are still in flight: this pass's canvas is not the frame. + return Some(Image::new(1, 1)); + } + match e.read_keyed(&img, key, [0.0; 2], 1.0) { + Some(rb) => rb.and_then(image), + None => Some(Image::new(1, 1)), + } + } + /// Wait for all submitted GPU work (benchmarks). pub fn wait(&self) { let _ = self.ctx.device.poll(wgpu::PollType::wait_indefinitely()); @@ -176,12 +254,32 @@ impl Accelerator for Gpu { } fn particles(&self) -> Option<&dyn effectcraft_effects::psim::ParticleSim> { - self.ctx.can_readback().then_some(self as &dyn effectcraft_effects::psim::ParticleSim) + self.ctx.can_wait().then_some(self as &dyn effectcraft_effects::psim::ParticleSim) } fn auto_pick(&self) -> Option<&effectcraft_render::AutoPick> { Some(&self.auto) } + + fn frame_begin(&self) { + if let Some(d) = self.ctx.deferred() { + d.frame_begin(); + } + } + + fn pass_begin(&self) { + if let Some(d) = self.ctx.deferred() { + d.pass_begin(); + } + } + + fn pass_missed(&self) -> bool { + self.ctx.deferred().is_some_and(|d| d.missed()) + } + + fn miss_gate(&self) -> Option> { + self.ctx.deferred().map(|d| d.gate()) + } } impl effectcraft_effects::psim::ParticleSim for Gpu { @@ -199,6 +297,8 @@ mod tests_adjust; #[cfg(test)] mod tests_adv3d; #[cfg(test)] +mod tests_deferred; +#[cfg(test)] mod tests_fx_color; #[cfg(test)] mod tests_fx_distort; diff --git a/crates/gpu/src/particles.rs b/crates/gpu/src/particles.rs index 669a4ba0..487fcfbe 100644 --- a/crates/gpu/src/particles.rs +++ b/crates/gpu/src/particles.rs @@ -109,7 +109,7 @@ fn uniforms(req: &SimRequest, n: u32, s0: u64, prev_n: u32) -> Vec { /// Simulate `req` (see [`effectcraft_effects::psim::ParticleSim`]). pub(crate) fn simulate(g: &GpuContext, req: &SimRequest) -> Option> { - if !g.can_readback() || req.steps > u32::MAX as u64 { + if !g.can_wait() || req.steps > u32::MAX as u64 { return None; } let n = req.births.len() as u32; diff --git a/crates/gpu/src/shaders/fx_noise.wgsl b/crates/gpu/src/shaders/fx_noise.wgsl index d5f9fa9e..3d49ab4d 100644 --- a/crates/gpu/src/shaders/fx_noise.wgsl +++ b/crates/gpu/src/shaders/fx_noise.wgsl @@ -2,7 +2,8 @@ // effectcraft-effects operation for operation. Every name here is prefixed `fxn_` (the // family files share one module). -const FXN_MAX: f32 = 3.40282347e38; +// f32::MAX exactly (a decimal that rounds above it is rejected by browsers' WGSL compilers). +const FXN_MAX: f32 = 3.4028234663852886e38; fn fxn_pixel(gid: vec3) -> vec2 { let dims = out_dims(); diff --git a/crates/gpu/src/tests.rs b/crates/gpu/src/tests.rs index b30658cf..4b848da4 100644 --- a/crates/gpu/src/tests.rs +++ b/crates/gpu/src/tests.rs @@ -562,3 +562,45 @@ fn backend_selection_and_display_frames() { } } } + +/// Browsers' WGSL compilers reject an f32 literal whose exact decimal value lies outside the f32 +/// range (`3.40282347e38` rounds to f32::MAX in Rust but exceeds it), which invalidates the whole +/// module (every kernel); naga accepts it, so native runs can't catch it. Every float literal +/// with an exponent must be within the f32 range exactly. +#[test] +fn wgsl_float_literals_fit_f32() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src/shaders"); + let mut checked = 0; + for entry in std::fs::read_dir(&dir).unwrap() { + let path = entry.unwrap().path(); + let src = std::fs::read_to_string(&path).unwrap(); + for (n, line) in src.lines().enumerate() { + let code = line.split("//").next().unwrap_or(""); + let bytes = code.as_bytes(); + let mut i = 0; + while i < bytes.len() { + let starts = bytes[i].is_ascii_digit() && (i == 0 || !(bytes[i - 1].is_ascii_alphanumeric() || bytes[i - 1] == b'_' || bytes[i - 1] == b'.')); + if !starts { + i += 1; + continue; + } + let mut j = i; + while j < bytes.len() + && (bytes[j].is_ascii_alphanumeric() || bytes[j] == b'.' || ((bytes[j] == b'-' || bytes[j] == b'+') && matches!(bytes[j - 1], b'e' | b'E'))) + { + j += 1; + } + let lit = code[i..j].trim_end_matches('f'); + if !lit.starts_with("0x") + && lit.contains(['e', 'E']) + && let Ok(v) = lit.parse::() + { + checked += 1; + assert!(v.abs() <= f32::MAX as f64, "{}:{}: {lit} is outside the f32 range", path.display(), n + 1); + } + i = j; + } + } + } + assert!(checked > 0); +} diff --git a/crates/gpu/src/tests_deferred.rs b/crates/gpu/src/tests_deferred.rs new file mode 100644 index 00000000..c19153a1 --- /dev/null +++ b/crates/gpu/src/tests_deferred.rs @@ -0,0 +1,140 @@ +//! Deferred readbacks (the browser worker's WebGPU path) vs the CPU: a frame renders in passes +//! until no readback is missing, and the result matches the CPU render. Natively the same +//! device works with readbacks deferred; a device poll delivers them, as the browser's event +//! loop does. + +use std::sync::OnceLock; + +use effectcraft_keyframe::Value; +use effectcraft_project::build; +use effectcraft_project::{BitDepth, Comp, ItemKind, LayerSource}; +use effectcraft_render::{Accelerator, Backend, LayerCache, RenderOpts, Renderer}; +use effectcraft_time::{FrameRate, Tick}; + +use crate::Gpu; +use crate::tests::{Pattern, Scene, diff, n, set, tolerance, v3}; + +/// The device, held exclusively: deferred state is per device and a frame renders at a time +/// (a frame worker renders one frame at a time too). +fn deferred_gpu() -> Option<(&'static Gpu, std::sync::MutexGuard<'static, ()>)> { + static G: OnceLock> = OnceLock::new(); + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + let g = G.get_or_init(|| pollster::block_on(Gpu::request_deferred()).map_err(|e| eprintln!("deferred gpu tests skipped: {e}")).ok()).as_ref()?; + Some((g, LOCK.lock().unwrap_or_else(|e| e.into_inner()))) +} + +/// Footage with GPU effects around a CPU-only one, a solid with Glow, and a precomp whose +/// layer is blurred inside and outside (a chain whose input is another chain's result). +fn scene() -> Scene { + let cpu_only = "ec.blur.ccradial"; + assert!(!effectcraft_effects::GPU_EFFECTS.contains(&cpu_only)); + let mut s = Scene::new(BitDepth::Bpc32); + let mut bg = s.footage(97, 61); + s.effect(&mut bg, "ec.blur.gaussian", &[("blurriness", n(5.0))]); + s.effect(&mut bg, cpu_only, &[]); + s.effect(&mut bg, "ec.color.levels", &[("gamma", n(1.3))]); + s.push(bg); + let mut solid = s.solid([0.9, 0.4, 0.1], 30, 20); + s.effect(&mut solid, "ec.stylize.glow", &[("threshold", n(20.0)), ("radius", n(6.0))]); + set(&mut solid, "transform/position", v3(30.0, 20.0)); + s.push(solid); + // Precomp: a footage layer blurred inside; the precomp layer blurred again outside. + let inner = Comp::new(60, 40, FrameRate::FPS_30, Tick::from_seconds_f64(2.0)); + let inner_id = s.p.add_item("Inner", effectcraft_color::Label::Blue, None, ItemKind::Comp(inner.clone().into())); + { + let mut ins = Scene { p: std::mem::take(&mut s.p), cid: inner_id, comp: inner }; + let mut l = ins.footage(50, 30); + ins.effect(&mut l, "ec.blur.fastbox", &[("radius", n(3.0))]); + ins.push(l); + s.p = ins.p; + } + let mut pre = build::layer(&mut s.p, &s.comp, "Inner", LayerSource::Comp { item: inner_id }, (60, 40), None); + s.effect(&mut pre, "ec.blur.directional", &[("length", n(6.0)), ("direction", Value::Scalar(30.0))]); + set(&mut pre, "transform/position", v3(60.0, 35.0)); + s.push(pre); + s +} + +/// Render in passes with deferred readbacks; returns the frame and the number of passes. +fn render_passes(g: &Gpu, s: &Scene, opts: RenderOpts, cache: &LayerCache, t: Tick) -> (effectcraft_raster::Image, usize) { + cache.set_gate(g.miss_gate()); + let mut r = Renderer::new(&s.p, &Pattern, opts); + r.accel = Some(g); + r.cache = Some(cache); + g.frame_begin(); + for pass in 1..=12 { + g.pass_begin(); + let img = r.comp_frame(s.cid, t); + if !g.pass_missed() { + assert_eq!(g.deferred().unwrap().in_flight(), 0); + return (img, pass); + } + // The browser awaits `Gpu::settled`; natively a blocking poll delivers the readbacks. + g.wait(); + pollster::block_on(g.settled()); + } + panic!("the frame never completed"); +} + +#[test] +fn deferred_passes_match_the_cpu() { + let Some((g, _lock)) = deferred_gpu() else { return }; + assert!(g.context().can_readback() && !g.context().can_wait()); + let s = scene(); + let t = Tick::from_seconds_f64(0.5); + let cpu = Renderer::new(&s.p, &Pattern, RenderOpts { backend: Backend::Cpu, ..Default::default() }).comp_frame_cpu(s.cid, t); + let tol = tolerance(BitDepth::Bpc32) * 2.0; + // The whole frame on the GPU (GPU compositor + chains + the frame's readback). + let cache = LayerCache::default(); + let (img, passes) = render_passes(g, &s, RenderOpts { backend: Backend::Gpu, ..Default::default() }, &cache, t); + let d = diff(&cpu, &img, tol); + assert!(d.over == 0, "whole-frame GPU: {d:?}"); + assert!(passes >= 3, "chains, the nested chain, then the frame readback: {passes} passes"); + // Again (a new frame: results are per frame; the layer cache holds the genuine buffers of + // the cacheable layers): the same pixels. + let (img2, passes2) = render_passes(g, &s, RenderOpts { backend: Backend::Gpu, ..Default::default() }, &cache, t); + assert_eq!(img2.data, img.data); + assert!(passes2 <= passes, "{passes2} <= {passes}"); + // CPU compositing with GPU chains (a region of interest keeps the frame off the GPU + // compositor): every placeholder is gone by the last pass. + let roi = Some([0.0, 0.0, 97.0, 61.0]); + let cpu_roi = Renderer::new(&s.p, &Pattern, RenderOpts { backend: Backend::Cpu, roi, ..Default::default() }).comp_frame_cpu(s.cid, t); + let (img3, passes3) = render_passes(g, &s, RenderOpts { backend: Backend::Gpu, roi, ..Default::default() }, &LayerCache::default(), t); + let d = diff(&cpu_roi, &img3, tol); + assert!(d.over == 0, "CPU composite, GPU chains: {d:?}"); + assert!(passes3 >= 2); + let (p, rb, inflight) = g.deferred_stats().unwrap(); + assert!(p >= (passes + passes2 + passes3) as u64 && rb > 0 && inflight == 0); +} + +#[test] +fn placeholders_never_reach_the_layer_cache() { + let Some((g, _lock)) = deferred_gpu() else { return }; + let s = scene(); + let t = Tick::from_seconds_f64(1.0); + let cache = LayerCache::default(); + cache.set_gate(g.miss_gate()); + let mut r = Renderer::new(&s.p, &Pattern, RenderOpts { backend: Backend::Gpu, roi: Some([0.0, 0.0, 97.0, 61.0]), ..Default::default() }); + r.accel = Some(g); + r.cache = Some(&cache); + g.frame_begin(); + g.pass_begin(); + let _ = r.comp_frame(s.cid, t); + assert!(g.pass_missed()); + // Only buffers computed before the first miss were kept (the first layer's input up to its + // first chain: nothing with an effect result). + let first_pass_entries = cache.stats().entries; + g.wait(); + pollster::block_on(g.settled()); + let (img, _) = render_passes(g, &s, RenderOpts { backend: Backend::Gpu, roi: Some([0.0, 0.0, 97.0, 61.0]), ..Default::default() }, &cache, t); + assert!(cache.stats().entries > first_pass_entries, "{first_pass_entries} → {:?}", cache.stats()); + // A fresh CPU render through the same cache (served from it) agrees with the CPU. + let mut cr = Renderer::new(&s.p, &Pattern, RenderOpts { backend: Backend::Cpu, roi: Some([0.0, 0.0, 97.0, 61.0]), ..Default::default() }); + cr.cache = Some(&cache); + let cached = cr.comp_frame_cpu(s.cid, t); + let fresh = + Renderer::new(&s.p, &Pattern, RenderOpts { backend: Backend::Cpu, roi: Some([0.0, 0.0, 97.0, 61.0]), ..Default::default() }).comp_frame_cpu(s.cid, t); + let tol = tolerance(BitDepth::Bpc32) * 2.0; + assert_eq!(diff(&fresh, &cached, tol).over, 0); + assert_eq!(diff(&fresh, &img, tol).over, 0); +} diff --git a/crates/render/src/cache.rs b/crates/render/src/cache.rs index 734573a3..da2ee829 100644 --- a/crates/render/src/cache.rs +++ b/crates/render/src/cache.rs @@ -32,6 +32,8 @@ use crate::eval::EvalCtx; pub struct LayerCache { inner: Mutex, disk: Mutex, u64)>>, + /// While set, inserts are dropped (see [`LayerCache::set_gate`]). + gate: Mutex>>, } struct Entry { @@ -72,9 +74,24 @@ impl LayerCache { LayerCache { inner: Mutex::new(Inner { map: HashMap::new(), bytes: 0, budget, clock: 0, hits: 0, misses: 0, missed_at: HashMap::new() }), disk: Mutex::new(None), + gate: Mutex::new(None), } } + /// Drop inserts while `gate` is set. A render whose GPU readbacks are still in flight (the + /// browser's deferred readbacks, see [`crate::Accelerator::pass_missed`]) computes some + /// buffers from placeholders; the accelerator raises the gate from its first missing + /// readback to the end of the pass, so none of them is kept (in memory or on disk). + pub fn set_gate(&self, gate: Option>) { + if let Ok(mut g) = self.gate.lock() { + *g = gate; + } + } + + fn gated(&self) -> bool { + self.gate.lock().ok().and_then(|g| g.as_ref().map(|g| g.load(std::sync::atomic::Ordering::Relaxed))).unwrap_or(false) + } + /// Attach (or detach) the disk cache. `salt` separates projects and footage versions (see /// `footage_salt`); keys on disk are `(salt, key)`. pub fn set_disk(&self, disk: Option<(Arc, u64)>) { @@ -130,6 +147,9 @@ impl LayerCache { } pub fn insert(&self, key: u64, buf: Arc) { + if self.gated() { + return; + } let started = self.inner.lock().ok().and_then(|mut g| g.missed_at.remove(&key)); if let Some((dc, dk)) = self.disk_key(key) { let slow = started.is_some_and(|t| t.elapsed().as_secs_f64() * 1e3 >= dc.min_layer_ms() as f64); diff --git a/crates/render/src/disk_cache.rs b/crates/render/src/disk_cache.rs index 50c3d1d7..bd78442a 100644 --- a/crates/render/src/disk_cache.rs +++ b/crates/render/src/disk_cache.rs @@ -40,7 +40,8 @@ pub enum Kind { } impl Kind { - fn dir(self) -> &'static str { + /// The kind's folder name. + pub fn dir(self) -> &'static str { match self { Kind::Frame => "frames", Kind::Layer => "layers", @@ -80,13 +81,98 @@ struct Meta { last_use: u64, } +/// The cache's index: entry sizes and least-recently-used order under a size limit. Portable +/// (no file system): [`DiskCache`] keeps one over its folder, and the browser keeps one over the +/// Origin Private File System (`apps/effectcraft-web`, where the files are written by workers). #[derive(Default)] -struct Index { +pub struct DiskIndex { map: HashMap<(Kind, u128), Meta>, total: u64, clock: u64, } +impl DiskIndex { + /// Record an entry (`bytes` on disk) as the most recently used; replaces an older record. + pub fn insert(&mut self, kind: Kind, key: u128, bytes: u64) { + self.clock += 1; + let c = self.clock; + if let Some(old) = self.map.insert((kind, key), Meta { bytes, last_use: c }) { + self.total -= old.bytes; + } + self.total += bytes; + } + + /// Mark an entry as just used (a read). `false` when it is not indexed. + pub fn touch(&mut self, kind: Kind, key: u128) -> bool { + self.clock += 1; + let c = self.clock; + match self.map.get_mut(&(kind, key)) { + Some(m) => { + m.last_use = c; + true + } + None => false, + } + } + + /// Forget an entry; its size, if it was indexed. + pub fn remove(&mut self, kind: Kind, key: u128) -> Option { + let m = self.map.remove(&(kind, key))?; + self.total -= m.bytes; + Some(m.bytes) + } + + pub fn contains(&self, kind: Kind, key: u128) -> bool { + self.map.contains_key(&(kind, key)) + } + + pub fn len(&self) -> usize { + self.map.len() + } + + pub fn is_empty(&self) -> bool { + self.map.is_empty() + } + + /// Bytes of every indexed entry. + pub fn total(&self) -> u64 { + self.total + } + + /// Keys of one kind (unordered). + pub fn keys(&self, kind: Kind) -> Vec { + self.map.keys().filter(|k| k.0 == kind).map(|k| k.1).collect() + } + + /// Entries from least to most recently used. + pub fn lru_order(&self) -> Vec<(Kind, u128)> { + let mut v: Vec<(u64, (Kind, u128))> = self.map.iter().map(|(k, m)| (m.last_use, *k)).collect(); + v.sort_unstable_by_key(|e| e.0); + v.into_iter().map(|e| e.1).collect() + } + + /// Remove least recently used entries until the total fits `max` bytes; returns them (the + /// caller deletes their files). + pub fn evict(&mut self, max: u64) -> Vec<(Kind, u128)> { + if self.total <= max { + return vec![]; + } + let mut out = vec![]; + for k in self.lru_order() { + if self.total <= max { + break; + } + self.remove(k.0, k.1); + out.push(k); + } + out + } + + pub fn clear(&mut self) { + *self = DiskIndex::default(); + } +} + #[cfg_attr(target_arch = "wasm32", allow(dead_code))] enum Job { Write(Kind, u128, Vec), @@ -99,7 +185,7 @@ struct Queue { pub struct DiskCache { dir: PathBuf, - index: Mutex, + index: Mutex, max_bytes: AtomicU64, hits: AtomicU64, misses: AtomicU64, @@ -172,7 +258,7 @@ impl DiskCache { std::fs::create_dir_all(&dir)?; let dc = Arc::new(DiskCache { dir, - index: Mutex::new(Index::default()), + index: Mutex::new(DiskIndex::default()), max_bytes: AtomicU64::new(max_bytes), hits: AtomicU64::new(0), misses: AtomicU64::new(0), @@ -223,12 +309,9 @@ impl DiskCache { } } found.sort_by_key(|e| e.0); - let mut idx = Index::default(); + let mut idx = DiskIndex::default(); for (_, kind, key, bytes) in found { - idx.clock += 1; - let clock = idx.clock; - idx.total += bytes; - idx.map.insert((kind, key), Meta { bytes, last_use: clock }); + idx.insert(kind, key, bytes); } if let Ok(mut g) = self.index.lock() { *g = idx; @@ -260,16 +343,16 @@ impl DiskCache { } pub fn contains(&self, kind: Kind, key: u128) -> bool { - self.index.lock().is_ok_and(|g| g.map.contains_key(&(kind, key))) + self.index.lock().is_ok_and(|g| g.contains(kind, key)) } /// Keys of one kind (unordered). pub fn keys(&self, kind: Kind) -> Vec { - self.index.lock().map(|g| g.map.keys().filter(|k| k.0 == kind).map(|k| k.1).collect()).unwrap_or_default() + self.index.lock().map(|g| g.keys(kind)).unwrap_or_default() } pub fn stats(&self) -> DiskStats { - let (entries, bytes) = self.index.lock().map(|g| (g.map.len(), g.total)).unwrap_or_default(); + let (entries, bytes) = self.index.lock().map(|g| (g.len(), g.total())).unwrap_or_default(); DiskStats { entries, bytes, @@ -286,7 +369,7 @@ impl DiskCache { pub fn clear(&self) { self.flush(); if let Ok(mut g) = self.index.lock() { - *g = Index::default(); + g.clear(); } for kind in [Kind::Frame, Kind::Layer] { let _ = std::fs::remove_dir_all(self.dir.join(kind.dir())); @@ -322,11 +405,7 @@ impl DiskCache { let payload = payload.to_vec(); self.hits.fetch_add(1, Ordering::Relaxed); if let Ok(mut g) = self.index.lock() { - g.clock += 1; - let c = g.clock; - if let Some(m) = g.map.get_mut(&(kind, key)) { - m.last_use = c; - } + g.touch(kind, key); } // Persist the recency for the next session (best effort). if let Ok(f) = std::fs::File::options().append(true).open(&path) { @@ -347,10 +426,8 @@ impl DiskCache { } fn forget(&self, kind: Kind, key: u128) { - if let Ok(mut g) = self.index.lock() - && let Some(m) = g.map.remove(&(kind, key)) - { - g.total -= m.bytes; + if let Ok(mut g) = self.index.lock() { + g.remove(kind, key); } } @@ -382,13 +459,7 @@ impl DiskCache { if std::fs::create_dir_all(parent).is_err() { return; } - let mut data = Vec::with_capacity(payload.len() + 16); - data.extend_from_slice(MAGIC); - data.push(kind.tag()); - data.extend_from_slice(&[0, 0, 0]); - data.extend_from_slice(payload); - let sum = checksum(&data); - data.extend_from_slice(&sum.to_le_bytes()); + let data = seal(kind, payload); let n = self.tmp_counter.fetch_add(1, Ordering::Relaxed); let tmp = parent.join(format!("{key:032x}.tmp.{}.{n}", std::process::id())); if std::fs::write(&tmp, &data).is_err() { @@ -401,13 +472,7 @@ impl DiskCache { } self.writes.fetch_add(1, Ordering::Relaxed); if let Ok(mut g) = self.index.lock() { - g.clock += 1; - let c = g.clock; - let bytes = data.len() as u64; - if let Some(old) = g.map.insert((kind, key), Meta { bytes, last_use: c }) { - g.total -= old.bytes; - } - g.total += bytes; + g.insert(kind, key, data.len() as u64); } self.evict(); } @@ -415,23 +480,9 @@ impl DiskCache { /// Remove least recently used entries until the cache fits its limit. fn evict(&self) { let max = self.max_bytes(); - let victims: Vec<(Kind, u128)> = { - let Ok(mut g) = self.index.lock() else { return }; - if g.total <= max { - return; - } - let mut by_age: Vec<(u64, (Kind, u128), u64)> = g.map.iter().map(|(k, m)| (m.last_use, *k, m.bytes)).collect(); - by_age.sort_unstable_by_key(|e| e.0); - let mut out = vec![]; - for (_, k, bytes) in by_age { - if g.total <= max { - break; - } - g.map.remove(&k); - g.total -= bytes; - out.push(k); - } - out + let victims = match self.index.lock() { + Ok(mut g) => g.evict(max), + Err(_) => return, }; for (kind, key) in victims { let _ = std::fs::remove_file(self.path(kind, key)); @@ -510,6 +561,40 @@ fn writer(dc: std::sync::Weak, q: Arc<(Mutex, Condvar)>) { } } +/// An entry file's bytes: header (magic, kind), payload, checksum. +pub fn seal(kind: Kind, payload: &[u8]) -> Vec { + let mut data = Vec::with_capacity(payload.len() + 16); + data.extend_from_slice(MAGIC); + data.push(kind.tag()); + data.extend_from_slice(&[0, 0, 0]); + data.extend_from_slice(payload); + let sum = checksum(&data); + data.extend_from_slice(&sum.to_le_bytes()); + data +} + +/// A sealed frame entry ([`seal`] of the encoded frame): what the browser's frame workers +/// write to the Origin Private File System. +pub fn frame_entry(width: u32, height: u32, rgba: &[u8]) -> Vec { + seal(Kind::Frame, &encode_frame(width, height, rgba)) +} + +/// A frame from a sealed entry's bytes (`None` when damaged). +pub fn read_frame_entry(data: &[u8]) -> Option { + decode_frame(verify(data, Kind::Frame)?) +} + +/// An entry's file name in its kind's folder (`<32 hex>.ecc`). +pub fn entry_name(key: u128) -> String { + format!("{key:032x}.ecc") +} + +/// The key of an entry file name (`None` for temporaries and other files). +pub fn parse_entry_name(name: &str) -> Option { + let hex = name.strip_suffix(".ecc")?; + (hex.len() == 32).then(|| u128::from_str_radix(hex, 16).ok()).flatten() +} + /// Check magic, kind and checksum; returns the payload. fn verify(d: &[u8], kind: Kind) -> Option<&[u8]> { if d.len() < 16 || &d[..4] != MAGIC || d[4] != kind.tag() { @@ -707,6 +792,40 @@ mod tests { Buf { img, offset: [1.5, -2.25], scale: 0.5 } } + #[test] + fn index_evicts_least_recently_used() { + let mut ix = DiskIndex::default(); + ix.insert(Kind::Frame, 1, 100); + ix.insert(Kind::Frame, 2, 100); + ix.insert(Kind::Layer, 3, 100); + assert_eq!((ix.len(), ix.total()), (3, 300)); + assert!(ix.touch(Kind::Frame, 1)); + assert!(!ix.touch(Kind::Frame, 3)); + assert_eq!(ix.lru_order(), vec![(Kind::Frame, 2), (Kind::Layer, 3), (Kind::Frame, 1)]); + assert_eq!(ix.evict(250), vec![(Kind::Frame, 2)]); + assert!(ix.evict(250).is_empty()); + ix.insert(Kind::Frame, 1, 50); + assert_eq!(ix.total(), 150); + assert_eq!(ix.remove(Kind::Layer, 3), Some(100)); + assert_eq!(ix.keys(Kind::Frame), vec![1]); + ix.clear(); + assert!(ix.is_empty() && ix.total() == 0); + } + + #[test] + fn frame_entries_round_trip_and_names_parse() { + let rgba: Vec = (0..4 * 6 * 3).map(|i| (i * 7 % 256) as u8).collect(); + let e = frame_entry(6, 3, &rgba); + assert_eq!(read_frame_entry(&e), Some(Frame8 { width: 6, height: 3, rgba: rgba.clone() })); + let mut bad = e.clone(); + bad[20] ^= 1; + assert!(read_frame_entry(&bad).is_none()); + let key = 0x0123_4567_89ab_cdef_0011_2233_4455_6677u128; + assert_eq!(parse_entry_name(&entry_name(key)), Some(key)); + assert_eq!(parse_entry_name("abc.ecc"), None); + assert_eq!(parse_entry_name(&format!("{key:032x}.tmp.1")), None); + } + #[test] fn hit_miss_and_exact_round_trip() { let dir = tmpdir("hit"); diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 390c68c6..461e3e4b 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -296,6 +296,23 @@ pub trait Accelerator: Send + Sync { fn auto_pick(&self) -> Option<&AutoPick> { None } + /// Deferred readbacks: where a GPU readback cannot be waited for (WebGPU in a browser + /// worker), a frame renders in *passes*. A readback the accelerator does not have yet is + /// started, the step returns a cheap placeholder and the pass is marked missed + /// ([`Self::pass_missed`]); the caller waits for the readbacks in flight (asynchronously) + /// and renders the frame again, and the steps whose results arrived are served from them. + /// The frame is done after a pass with no miss. `frame_begin` forgets the previous frame's + /// results, `pass_begin` clears the miss. Synchronous accelerators never miss. + fn frame_begin(&self) {} + fn pass_begin(&self) {} + fn pass_missed(&self) -> bool { + false + } + /// Raised from a pass's first miss to its end (results computed meanwhile may hold + /// placeholders): give it to [`LayerCache::set_gate`] so they are not cached. + fn miss_gate(&self) -> Option> { + None + } } /// Where an effect stack runs (see [`Renderer::run_effects_on`]): a CPU buffer, or an image diff --git a/crates/ui-egui/src/frames.rs b/crates/ui-egui/src/frames.rs index f2662763..19839189 100644 --- a/crates/ui-egui/src/frames.rs +++ b/crates/ui-egui/src/frames.rs @@ -172,6 +172,9 @@ pub struct RemoteJob { pub comp: ItemId, pub t: Tick, pub opts: RenderOpts, + /// The frame's disk-cache key, when the renderer keeps a disk cache + /// ([`RemoteFrames::disk`]): served from it on a hit, stored there after a render. + pub disk_key: Option, } /// Premultiplied RGBA8 pixels from a [`RemoteFrames`] renderer. @@ -195,6 +198,31 @@ pub trait RemoteFrames: Send + Sync { fn start(&self, job: RemoteJob, done: RemoteDone); /// Drop cached intermediate results (Edit ▸ Purge). fn purge(&self) {} + /// It renders GPU effects (its own GPU device): frames with effects go to it rather than + /// the UI thread's GPU compositor, which would run their effects on the CPU. + fn gpu(&self) -> bool { + false + } + /// It keeps a disk cache of frames (the browser's Origin Private File System). + fn disk(&self) -> bool { + false + } + /// The disk cache holds the frame of this key (the blue cache bar). + fn disk_contains(&self, _key: u128) -> bool { + false + } +} + +/// Whether a comp (or a comp it nests) has a layer with enabled effects. +pub fn comp_has_effects(project: &Project, comp: ItemId) -> bool { + fn walk(p: &Project, comp: ItemId, depth: usize) -> bool { + let Some(c) = p.comp(comp) else { return false }; + c.layers.iter().any(|l| { + l.switches.effects && l.effects().is_some_and(|fx| fx.groups().any(|g| g.enabled)) + || depth < 16 && matches!(l.source, effectcraft_engine::project::LayerSource::Comp { item } if walk(p, item, depth + 1)) + }) + } + walk(project, comp, 0) } pub struct Frames { @@ -283,14 +311,22 @@ impl Frames { /// Frames of (revision, comp, scale) in the disk cache but not in RAM — the blue cache bar. /// `opts` are the viewer's render options (part of the disk key). pub fn disk_frames(&self, src: &RenderSource, revision: u64, comp: u64, scale: u32, view: u64, frames: i64, opts: &RenderOpts) -> Vec { - let Some(dc) = &src.disk else { return vec![] }; + let remote = self.remote.as_ref().filter(|r| r.disk()); + let contains = |k: u128| match (&src.disk, remote) { + (Some(dc), _) => dc.contains(disk_cache::Kind::Frame, k), + (None, Some(r)) => r.disk_contains(k), + (None, None) => false, + }; + if src.disk.is_none() && remote.is_none() { + return vec![]; + } let ram: HashSet = self.cached_frames(revision, comp, scale).into_iter().collect(); let oh = opts_hash(opts); (0..frames.clamp(0, 100_000)) .filter(|f| !ram.contains(f)) .filter(|f| { let key = FrameKey { revision, comp, frame: *f, scale, view }; - dc.contains(disk_cache::Kind::Frame, frame_disk_key(&self.content_keys, &src.project, &key, oh)) + contains(frame_disk_key(&self.content_keys, &src.project, &key, oh)) }) .collect() } @@ -396,6 +432,11 @@ impl Frames { self.remote = remote; } + /// The remote renderer keeps a disk cache (the browser's). + pub fn remote_disk(&self) -> bool { + self.remote.as_ref().is_some_and(|r| r.disk()) + } + /// Whether frames go to a usable [`RemoteFrames`]. pub fn remote_active(&self) -> bool { self.remote.as_ref().is_some_and(|r| r.slots() > 0) @@ -435,7 +476,11 @@ impl Frames { } job }; - if job.src.gpu.is_some() && job.src.gpu_display { + // The GPU compositor here keeps the frame on the GPU, but runs layer effects on the + // CPU (this thread can't wait for readbacks): frames with effects go to workers + // that render them on their own GPU. + let gpu_here = job.src.gpu.is_some() && job.src.gpu_display && !(remote.gpu() && comp_has_effects(&job.src.project, job.comp)); + if gpu_here { let t0 = web_time::Instant::now(); if let Some(img) = w.render_gpu(&job) { w.finish(&job, img, t0); @@ -450,7 +495,8 @@ impl Frames { let fw = self.worker(); let busy = self.remote_busy.clone(); let t0 = web_time::Instant::now(); - let rj = RemoteJob { project: job.src.project.clone(), revision: key.revision, comp: job.comp, t: job.t, opts: job.opts }; + let disk_key = remote.disk().then(|| frame_disk_key(&w.content_keys, &job.src.project, &key, opts_hash(&job.opts))); + let rj = RemoteJob { project: job.src.project.clone(), revision: key.revision, comp: job.comp, t: job.t, opts: job.opts, disk_key }; remote.start( rj, Box::new(move |r: Result| { diff --git a/crates/ui-egui/src/panels/settings.rs b/crates/ui-egui/src/panels/settings.rs index 180dae96..d116c7ca 100644 --- a/crates/ui-egui/src/panels/settings.rs +++ b/crates/ui-egui/src/panels/settings.rs @@ -60,6 +60,7 @@ fn page_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, page: &Page, cur: &Value } } Item::Labels => labels_ui(app, ui, cur, acts), + Item::BrowserStorage => browser_storage_ui(app, ui, t, acts), Item::AudioDevices { key } => { let devices: Vec = app.hooks.audio_devices.as_ref().map(|f| f()).unwrap_or_default(); let sel = get(key).as_str().unwrap_or("").to_string(); @@ -193,6 +194,93 @@ fn page_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, page: &Page, cur: &Value } } +/// Bytes as "12.3 MB". +pub fn human_bytes(b: u64) -> String { + let units = ["bytes", "KB", "MB", "GB", "TB"]; + let mut v = b as f64; + let mut u = 0; + while v >= 1000.0 && u + 1 < units.len() { + v /= 1000.0; + u += 1; + } + if u == 0 { format!("{b} bytes") } else { format!("{v:.1} {}", units[u]) } +} + +/// Settings ▸ Disk ▸ Browser Storage (the web app's storage manager, `storage.*`): where the +/// data lives, the origin's usage and quota, persistent storage, and Clear buttons. +fn browser_storage_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, t: &Tokens, acts: &mut Vec) { + let Some(host) = app.session.storage.clone() else { return }; + let info = host.info(); + let u = |v: &Value| v.as_u64().unwrap_or(0); + ui.add_space(6.0); + ui.label(RichText::new("Browser Storage").font(Tokens::semibold(12.5)).color(t.tab_text_active)); + ui.separator(); + let backend = match info["backend"].as_str().unwrap_or("") { + "opfs" => "Origin Private File System", + "indexeddb" => "IndexedDB", + "memory" => "Memory only: nothing is kept after a reload", + "" => "Unknown", + other => other, + }; + let r = ui.label(format!("Stored in: {backend}")); + reg(app, "settings.storage.backend", &r, backend); + let (usage, quota) = (u(&info["usage"]), u(&info["quota"])); + let frac = if quota > 0 { usage as f32 / quota as f32 } else { 0.0 }; + let text = if quota > 0 { format!("{} of {} used ({:.1} %)", human_bytes(usage), human_bytes(quota), frac * 100.0) } else { "Usage unknown".into() }; + let r = ui + .horizontal(|ui| { + ui.add(egui::ProgressBar::new(frac.clamp(0.0, 1.0)).desired_width(220.0).desired_height(8.0)); + ui.label(&text); + }) + .response; + reg(app, "settings.storage.usage", &r, &text); + let f = &info["files"]; + let row = |what: &str, k: &str| format!("{what}: {} ({})", u(&f[k]["count"]), human_bytes(u(&f[k]["bytes"]))); + ui.label( + RichText::new(format!("{} · {} · {}", row("Projects", "projects"), row("Imported media", "media"), row("Auto-saves", "autoSaves"))).color(t.text_dim), + ); + let d = &info["diskCache"]; + let dc = if d["enabled"].as_bool().unwrap_or(false) { + format!( + "Disk cache: {} frames, {} of {} (hits {}, misses {})", + u(&d["entries"]), + human_bytes(u(&d["bytes"])), + human_bytes(u(&d["maxBytes"])), + u(&d["hits"]), + u(&d["misses"]) + ) + } else { + "Disk cache: off".into() + }; + let r = ui.label(RichText::new(&dc).color(t.text_dim)); + reg(app, "settings.storage.diskCache", &r, &dc); + ui.horizontal(|ui| { + let persisted = info["persisted"].as_bool().unwrap_or(false); + let state = if persisted { "Persistent storage: granted" } else { "Persistent storage: not granted (the browser may clear it when space runs low)" }; + ui.label(state); + if !persisted { + let r = ui.button("Request Persistent Storage"); + reg(app, "settings.storage.persist", &r, "Request Persistent Storage"); + if r.clicked() { + acts.push(Act::Run("storage.persist".into(), json!({}))); + } + } + }); + ui.horizontal_wrapped(|ui| { + for (what, label) in + [("diskCache", "Clear Disk Cache"), ("media", "Clear Imported Media"), ("autoSaves", "Clear Auto-Saves"), ("projects", "Clear Saved Projects")] + { + let r = ui.button(label); + reg(app, &format!("settings.storage.clear.{what}"), &r, label); + if r.clicked() { + acts.push(Act::Run("storage.clear".into(), json!({"what": what}))); + } + } + }); + // The numbers refresh in the background. + ui.ctx().request_repaint_after(std::time::Duration::from_secs(1)); +} + fn labels_ui(app: &mut EffectcraftApp, ui: &mut egui::Ui, cur: &Value, acts: &mut Vec) { ui.label("Label Colors and Names:"); ui.add_space(4.0); diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 2fa4e18b..c29e7a49 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -957,7 +957,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { draw_run(a, b); } // Disk-cached frames not in RAM (blue), like After Effects. - if app.session.disk_cache.is_some() { + if app.session.disk_cache.is_some() || app.frames.remote_disk() { let frames = comp.frame_rate.frame_at(comp.duration); let opts = app.frame_opts(cid, scale_key as f64 / 1000.0); let disk = app.frames.disk_frames(&app.render_source(), app.session.revision, cid.0, scale_key, app.view_hash(cid), frames, &opts); diff --git a/crates/ui-egui/tests/ui_web_gpu_storage.rs b/crates/ui-egui/tests/ui_web_gpu_storage.rs new file mode 100644 index 00000000..a25bd4a7 --- /dev/null +++ b/crates/ui-egui/tests/ui_web_gpu_storage.rs @@ -0,0 +1,237 @@ +//! Headless checks for M13.24 (web app depth: GPU frame workers, the browser's disk cache, the +//! storage manager), run natively with stand-ins for the browser: +//! +//! - a frame worker with its own GPU device and deferred readbacks (the browser worker's +//! WebGPU path) renders frames with GPU effects in passes; the page's own GPU compositor +//! keeps frames without effects, and frames with effects go to the worker; +//! - frames the worker rendered are stored under their disk-cache keys (an in-memory stand-in +//! for the Origin Private File System) and served from there after a "reload" (a new frame +//! cache), without rendering; the blue cache bar sees them; +//! - Settings ▸ Disk shows the browser storage manager when the session has a storage host, +//! and its buttons run `storage.*` commands. + +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; + +use effectcraft_engine::Session; +use effectcraft_engine::remote::{FrameMsg, FrameReply, FrameServer, Mirror}; +use effectcraft_engine::render::RenderOpts; +use effectcraft_engine::render::disk_cache; +use effectcraft_engine::storage::StorageHost; +use effectcraft_gpu::Gpu; +use effectcraft_ui_egui::EffectcraftApp; +use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; +use effectcraft_ui_egui::frames::{FrameImage, FrameKey, Frames, RemoteDone, RemoteFrame, RemoteFrames, RemoteJob, RenderSource}; +use egui_kittest::Harness; +use serde_json::{Value, json}; + +fn rt serde::Deserialize<'de>>(v: &T) -> T { + serde_json::from_str(&serde_json::to_string(v).unwrap()).unwrap() +} + +/// A GPU frame worker in this process, with a "disk" shared across reloads. +struct GpuWorker { + gpu: Gpu, + state: Mutex<(Mirror, FrameServer, u64)>, + disk: Arc>>>, + rendered: Mutex>, +} + +impl GpuWorker { + fn new(s: &Session, gpu: Gpu, disk: Arc>>>) -> GpuWorker { + let mut server = FrameServer::new(s.footage.clone(), s.expr.clone()); + server.set_accel(Some(Arc::new(gpu.clone()))); + GpuWorker { gpu, state: Mutex::new((Mirror::default(), server, 0)), disk, rendered: Mutex::default() } + } +} + +impl RemoteFrames for GpuWorker { + fn slots(&self) -> usize { + 1 + } + fn gpu(&self) -> bool { + true + } + fn disk(&self) -> bool { + true + } + fn disk_contains(&self, key: u128) -> bool { + self.disk.lock().unwrap().contains_key(&key) + } + fn start(&self, job: RemoteJob, done: RemoteDone) { + // The page reads a hit from the disk cache instead of asking the worker. + if let Some(k) = job.disk_key + && let Some(f) = self.disk.lock().unwrap().get(&k).and_then(|e| disk_cache::read_frame_entry(e)) + { + return done(Ok(RemoteFrame { width: f.width, height: f.height, rgba: f.rgba, ms: 0.0 })); + } + let mut st = self.state.lock().unwrap(); + let (mirror, server, next) = &mut *st; + if let Some(m) = mirror.sync(job.revision, &job.project) { + assert!(server.handle(rt(&m)).is_empty()); + } + *next += 1; + let disk = job.disk_key.map(|k| format!("{k:032x}")); + let mut out = server.handle(rt(&FrameMsg::Render { + id: *next, + revision: job.revision, + comp: job.comp, + time: job.t, + opts: Box::new(job.opts), + disk: disk.clone(), + })); + // The worker awaits its device between passes (`Gpu::settled`); natively a poll. + while server.waiting() { + self.gpu.wait(); + pollster::block_on(self.gpu.settled()); + out = server.resume(); + } + match out.into_iter().next() { + Some((FrameReply::Frame { width, height, ms, passes, gpu, .. }, Some(rgba))) => { + self.rendered.lock().unwrap().push((passes, gpu)); + if let Some(k) = job.disk_key { + self.disk.lock().unwrap().insert(k, disk_cache::frame_entry(width, height, &rgba)); + } + done(Ok(RemoteFrame { width, height, rgba, ms })) + } + other => done(Err(format!("{:?}", other.map(|o| o.0)))), + } + } +} + +fn source(s: &Session, gpu: Option) -> RenderSource { + RenderSource { + project: s.project.clone(), + footage: s.footage.clone(), + expr: s.expr.clone(), + layer_cache: s.layer_cache.clone(), + gpu, + gpu_display: true, + disk: None, + } +} + +fn cpu_pixels(f: &FrameImage) -> Vec { + match f { + FrameImage::Cpu(c) => c.pixels.clone(), + FrameImage::Gpu(_) => panic!("expected a CPU frame"), + } +} + +#[test] +fn gpu_worker_renders_effect_frames_and_the_disk_cache_survives_a_reload() { + let (Some(page_gpu), Some(worker_gpu)) = (Gpu::headless(), Gpu::headless_deferred()) else { + eprintln!("no GPU adapter: skipped"); + return; + }; + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Fx", "width": 160, "height": 90, "frameRate": 30, "duration": 1})).unwrap(); + let plain = s.active_comp_id().unwrap(); + s.execute("layer.newSolid", json!({"color": "#3080ff", "width": 80, "height": 40})).unwrap(); + s.execute("comp.new", json!({"name": "Blurred", "width": 160, "height": 90, "frameRate": 30, "duration": 1})).unwrap(); + let fx = s.active_comp_id().unwrap(); + s.execute("layer.newSolid", json!({"color": "#ff8030", "width": 80, "height": 40})).unwrap(); + s.execute("effect.apply", json!({"layer": "#1", "effect": "Gaussian Blur"})).unwrap(); + s.execute("prop.set", json!({"layer": "#1", "path": "effects/#1/blurriness", "value": 12})).unwrap(); + let opts = RenderOpts { backend: effectcraft_engine::render::Backend::Gpu, ..Default::default() }; + let t = effectcraft_engine::time::Tick::ZERO; + let key = |comp: effectcraft_engine::project::ItemId| FrameKey { revision: s.revision, comp: comp.0, frame: 0, scale: 1000, view: 0 }; + let disk = Arc::new(Mutex::new(HashMap::new())); + let worker = Arc::new(GpuWorker::new(&s, worker_gpu.clone(), disk.clone())); + let mut frames = Frames::default(); + frames.set_remote(Some(worker.clone())); + // No effects: the page's GPU compositor keeps the frame on the GPU. + frames.request_urgent(&source(&s, Some(page_gpu.clone())), key(plain), plain, t, opts); + frames.dispatch_remote(); + assert!(matches!(frames.get(&key(plain)), Some(FrameImage::Gpu(_)))); + assert!(worker.rendered.lock().unwrap().is_empty()); + // GPU effects: the worker renders the frame on its GPU, in passes. + frames.request_urgent(&source(&s, Some(page_gpu.clone())), key(fx), fx, t, opts); + frames.dispatch_remote(); + let got = cpu_pixels(&frames.get(&key(fx)).expect("rendered by the worker")); + let (passes, on_gpu) = worker.rendered.lock().unwrap()[0]; + assert!(on_gpu && passes >= 2, "passes {passes}"); + let want = effectcraft_ui_egui::frames::to_color_image(&s.render(fx, t, RenderOpts { backend: effectcraft_engine::render::Backend::Cpu, ..opts })); + let worst = got.iter().zip(&want.pixels).map(|(a, b)| (0..4).map(|c| (a[c] as i32 - b[c] as i32).abs()).max().unwrap()).max().unwrap(); + assert!(worst <= 2, "GPU vs CPU: {worst}/255"); + assert_eq!(disk.lock().unwrap().len(), 1, "stored under its disk key"); + // "Reload": a new frame cache and worker; the frame comes from the disk cache. + let worker2 = Arc::new(GpuWorker::new(&s, worker_gpu, disk.clone())); + let mut frames2 = Frames::default(); + frames2.set_remote(Some(worker2.clone())); + assert!(frames2.remote_disk()); + let bar = frames2.disk_frames(&source(&s, None), s.revision, fx.0, 1000, 0, 30, &opts); + assert_eq!(bar, vec![0], "the blue cache bar shows the stored frame"); + frames2.request_urgent(&source(&s, Some(page_gpu)), key(fx), fx, t, opts); + frames2.dispatch_remote(); + assert_eq!(cpu_pixels(&frames2.get(&key(fx)).unwrap()), got); + assert!(worker2.rendered.lock().unwrap().is_empty(), "a hit is not rendered again"); +} + +#[derive(Default)] +struct Storage { + cleared: Mutex>, + persist_asked: Mutex, +} + +impl StorageHost for Storage { + fn info(&self) -> Value { + json!({"backend": "opfs", "usage": 52_000_000u64, "quota": 2_000_000_000u64, "persisted": false, + "files": {"projects": {"count": 2, "bytes": 40000}, "media": {"count": 3, "bytes": 50_000_000u64}, "autoSaves": {"count": 1, "bytes": 20000}}, + "diskCache": {"enabled": true, "entries": 12, "bytes": 1_500_000, "maxBytes": 1_000_000_000u64, "hits": 4, "misses": 12}}) + } + fn request_persist(&self) -> Value { + *self.persist_asked.lock().unwrap() = true; + json!({"requested": true}) + } + fn clear(&self, what: &str) -> Result { + self.cleared.lock().unwrap().push(what.into()); + Ok(json!({"cleared": what, "entries": 1, "bytes": 1})) + } +} + +fn send(h: &mut Harness<'_, EffectcraftApp>, method: &str, params: Value) { + let ctx = h.ctx.clone(); + let (req, _rx) = ControlRequest::new(method, params); + if let Outcome::Done(v) = control::handle(h.state_mut(), &ctx, &req) { + assert!(v["ok"] != false, "{method}: {v}"); + } + // kittest doesn't call `raw_input_hook`: feed the synthetic click in. + for e in h.state_mut().take_synthetic_input() { + h.event(e); + } + h.run_steps(3); +} + +#[test] +fn settings_disk_page_shows_the_browser_storage_manager() { + let mut s = Session::default(); + let host = Arc::new(Storage::default()); + s.storage = Some(host.clone()); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(s)); + h.run_steps(2); + effectcraft_ui_egui::panels::settings::open(h.state_mut(), "disk"); + h.run_steps(3); + for id in [ + "settings.storage.backend", + "settings.storage.usage", + "settings.storage.diskCache", + "settings.storage.persist", + "settings.storage.clear.diskCache", + "settings.storage.clear.media", + ] { + assert!(h.state().auto.find(id).is_some(), "{id} is registered"); + } + let usage = h.state().auto.find("settings.storage.usage").unwrap().label.clone(); + assert!(usage.contains("52.0 MB of 2.0 GB"), "{usage}"); + send(&mut h, "ui.click", json!({"id": "settings.storage.clear.diskCache"})); + send(&mut h, "ui.click", json!({"id": "settings.storage.persist"})); + assert_eq!(*host.cleared.lock().unwrap(), vec!["diskCache".to_string()]); + assert!(*host.persist_asked.lock().unwrap()); + // Without a host (the desktop) the section is not there. + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(Session::default())); + h.run_steps(2); + effectcraft_ui_egui::panels::settings::open(h.state_mut(), "disk"); + h.run_steps(3); + assert!(h.state().auto.find("settings.storage.usage").is_none()); +} diff --git a/crates/ui-egui/tests/ui_web_offload.rs b/crates/ui-egui/tests/ui_web_offload.rs index 54ae1a37..12fe0d5b 100644 --- a/crates/ui-egui/tests/ui_web_offload.rs +++ b/crates/ui-egui/tests/ui_web_offload.rs @@ -69,7 +69,7 @@ impl RemoteFrames for FakeWorker { assert!(server.handle(rt(&m)).is_empty()); } *next += 1; - let out = server.handle(rt(&FrameMsg::Render { id: *next, revision: job.revision, comp: job.comp, time: job.t, opts: Box::new(job.opts) })); + let out = server.handle(rt(&FrameMsg::Render { id: *next, revision: job.revision, comp: job.comp, time: job.t, opts: Box::new(job.opts), disk: None })); match out.into_iter().next() { Some((FrameReply::Frame { width, height, ms, .. }, Some(rgba))) => pending.push((done, RemoteFrame { width, height, rgba, ms })), other => done(Err(format!("{:?}", other.map(|o| o.0)))), diff --git a/docs/architecture.md b/docs/architecture.md index 3a9d5627..ff10181f 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -126,7 +126,17 @@ preview frame are written (LZ4, atomic rename, checksummed) under 128-bit conten comp's content hash (the comp, what it uses, footage file size and time) plus frame, scale and view for frames; the layer key salted with the footage fingerprint for layers — so entries survive restarts and never go stale. LRU eviction keeps it under the size limit; Edit ▸ Purge -empties it, `cache.diskStats` reports it, and the timeline draws disk-only frames in blue. +empties it, `cache.diskStats` reports it, and the timeline draws disk-only frames in blue. In the +browser the frames live in the Origin Private File System: frame workers write them, the page +keeps the index (`DiskIndex`) and reads hits ([web.md](web.md#disk-cache)). + +**Deferred readbacks** (`effectcraft-gpu` `deferred`): where a readback can't be waited for (WebGPU +in a browser worker) a frame renders in passes. `Accelerator::frame_begin` / `pass_begin` / +`pass_missed` / `miss_gate`: a readback not there yet starts asynchronously and the step returns a +placeholder; the pass is missed, the layer cache's gate drops inserts until the pass ends, and the +caller (`remote::FrameServer::resume`) renders again once the device delivered. Readbacks are keyed +by content (a chain's input pixels and parameters; the frame's comp, time and options), so later +passes are served from them; a pass without a miss is the frame. **Time effects** (Echo, Posterize Time, Timewarp…) read the layer at other times through `EffectHost::self_at`: the renderer renders the layer's source and masks (plus, optionally, the diff --git a/docs/parity.md b/docs/parity.md index 8f69f21d..136d0d8d 100644 --- a/docs/parity.md +++ b/docs/parity.md @@ -15,8 +15,10 @@ How close EffectCraft is to After Effects 2026, feature by feature, and how much | Remaining work | ≈ 19–22 agent-hours at the pace measured so far (≈ 58 on the conservative audit scale) | | **Wall-clock estimate** | **≈ 4–5 hours** with five agents in parallel; ≈ 3 hours for 100% of P0 + P1 | -Partial features: GPU coverage (Advanced 3D, more GPU effects), the web app (GPU effects and -the disk cache in the browser; M13.10 moved viewer frames and Roto Brush off the page's thread), Essential Graphics mirrored properties, and the learned-model quality +Partial features: GPU coverage (Advanced 3D, more GPU effects), the web app (GPU effects in +Render Queue workers; M13.10 moved viewer frames and Roto Brush off the page's thread, M13.24 +added GPU effects in the frame workers, the disk cache in the Origin Private File System and the +storage manager), Essential Graphics mirrored properties, and the learned-model quality of Roto Brush and face tracking (both are classical). Since this audit, M9.13 completed all 42 simplified effects, M13.5 added the Learn panel, and M13.8 the Preview panel settings, Align to Selection / Distribute and Advanced 3D iris DOF, collapsed precomps and extruded strokes. M13.7 added ScriptUI resource strings, @@ -134,7 +136,7 @@ the disk cache in the formats wave; the 3D Camera Tracker in M12.6.) | Compositions | ≈ 81% | 3.3 | Mocha-style planar tracks for templates (CMP-7: Essential Graphics mirrored and linked properties landed in M13.11; Font and uniform Scale controls, Composition ▸ Open in Essential Graphics, Save Frame As ▸ Photoshop Layers / ProEXR and the VR Comp Editor landed in M13.7; the marker dialog, Composition Flowchart, Essential Graphics with master properties, `.ectemplate` templates and Responsive Design — Time landed: CMP-6, CMP-7) | | Animation | 70% | 9.0 | puppet depth beyond pins, recording, rigging and follow-through (puppet pin recording with Record Options landed in M13.1; pin selection, rotate/scale handles, nulls for pins and Follow-Through in M13.14–M13.16), Wiggler/Smoother/Motion Sketch (motion-path handles and the graph editor transform box landed in M5.8; keyframe colour labels and Select Keyframe Label Group, Graph Editor snapping to markers / layer ends in M13.5) | | Text | ≈ 96% | 0.3 | no extruded strokes (M13.12: the Variable Font Axes animator re-spaces the text — advances follow the animated axes; M13.6: variable font axes in the character style — `layer.setText variations`, the Character panel's Variable Font Axes fields — shape with HVAR / gvar advances and draw at that design-space position; OpenType features — stylistic sets, discretionary ligatures, contextual / stylistic alternates, swash, titling, ordinals, fractions, figure styles, true small caps / all small caps and superior / inferior glyphs with faux fallback — per character with the Character panel's OpenType popup and `text.fontFeatures` landed in M13.2; vertical Roman / Tate-Chu-Yoko, forced LTR paragraphs, caret on animated and path text, Variable Font Axes and Lottie style runs landed in M13.5; extruded, bevelled text in M7.6; per-character styles, paragraph settings, on-canvas editing and the `sourceText` style API in M9.9–M9.10) | -| Web | 93% | 0.5 | GPU effects in the browser (WebGPU compositing only on the page), no disk cache, no shared-memory threads inside one engine instance (browser storage, Web Audio, Web Worker renders/analyses, WebGPU viewer and offline install landed in M15.2; viewer frames in frame workers fed by project diffs, Roto Brush propagation in a worker, non-blocking `wait: true` jobs and a browser Media Browser (File System Access folders, browser storage) in M13.10) | +| Web | 97% | 0.2 | GPU effects in Render Queue / analysis workers (job workers render on their CPU), layer buffers in the browser's disk cache (frames only), no shared-memory threads inside one engine instance (the decided design is one engine instance per worker; a threaded build needs nightly `build-std`) (browser storage, Web Audio, Web Worker renders/analyses, WebGPU viewer and offline install landed in M15.2; viewer frames in frame workers fed by project diffs, Roto Brush propagation in a worker, non-blocking `wait: true` jobs and a browser Media Browser (File System Access folders, browser storage) in M13.10; WEB-1 in M13.24: GPU effects and the GPU compositor in the frame workers on their own WebGPU devices with deferred readbacks (frames render in passes; Backend Auto per comp), the disk cache in the Origin Private File System (written by the workers with sync access handles, LRU under the settings' limit, served after a reload), the storage manager (Settings ▸ Disk ▸ Browser Storage, `storage.info` / `storage.persist` / `storage.clear`), and a WGSL constant Chrome rejected (it disabled every GPU kernel in Chrome) fixed) | | 3D | 88% | 5.5 | multi-view layouts, the Extended Viewer for Advanced 3D comps (Classic 3D Extended Viewer landed in M13.5 UI completion); collapsed precomps of another size seen through the parent's camera render (fixed in M13.2); stereo rigs, orbit nulls, lights controlled by the camera, cameras/lights from glTF models, environment backgrounds, Advanced 3D motion blur, blend modes and track mattes landed in M7.7; Classic 3D iris-shaped bokeh with highlights, progressive depth of field on tilted layers and the focus-link commands landed in M13.5; Advanced 3D depth of field with the iris and highlight options, collapsed precomps as real Advanced 3D geometry and extruded text/shape strokes landed in M13.8; Advanced 3D (glTF/OBJ models, primitives, extruded text and shapes, PBR, image-based light, shadow maps, GPU rasteriser) in M7.4–M7.6; Advanced 3D end to end on the GPU (motion blur, iris depth of field, compositing) in M13.11 | | Effects | ≈ 85% | 3.0 | GPU versions of the remaining effects (166 run on the GPU since M13.13, EFF-5: Warp, Bezier Warp, Reshape, Smear, CC Bend It, CC Page Turn, Cartoon, Color Emboss, Circle, Ellipse, Iris Wipe, Bevel Alpha / Edges and Gaussian Blur (Legacy) joined the 152 of M13.9; simulations, 3D channel, VR, audio-driven, OCIO, text and the CC light / transition families still render on the CPU) and the missing controls listed as partial in [effects.md](effects.md) (every After Effects effect exists since M9.11, M12.5 and M12.6; parameter names, order, twirl-downs, popups, units and defaults were aligned in M9.12) | | Interface | 74% | 5.0 | more Learn tutorials and pixel-level fidelity of dialogs (Timeline layer reordering by drag, a non-snapping viewer pan, a working rename field and twirl arrows, and the full set of property reveal shortcuts — double presses, Alt+Shift keyframes, Ctrl+` — landed in M13.17–M13.20; the Home ▸ Learn tab with interactive tutorials and a UI fidelity pass landed in M13.5 UI completion; visual editors for Lumetri RGB / hue-saturation curves, Colorama's output cycle wheel, Glow's colour map and Reshape's correspondence points (viewer handles), a scrolling Preview panel and AE-style Composition / Timeline tabs (close, label swatch, viewer lock) landed in M13.10; Timeline outline and Project panel columns scroll horizontally, the Layer Style dialog, ROI resize handles, Pan Behind snapping and 3D Reference Axes landed in M13.5; native macOS menu bar, Timeline columns/search/reveal-add, Home screen with recent projects and all AE workspaces landed; viewer rulers/snapping/channels/snapshots landed in M0.13) | diff --git a/docs/preferences.md b/docs/preferences.md index bd2da7e8..93da1311 100644 --- a/docs/preferences.md +++ b/docs/preferences.md @@ -112,7 +112,10 @@ Commands (CLI, MCP, control channel): - `audio.previewSampleRate`: the rate previews are mixed at (and the device is opened at, when it supports it) - `disk.diskCacheEnabled`, `disk.diskCacheMaxGb`, `disk.diskCacheFolder`: the persistent disk - cache of processed layers + cache of processed layers and frames (in the browser: frames in the Origin Private File System, + at most half the origin's quota; the folder setting does not apply there). The Disk page also + shows **Browser Storage** in the web app: usage and quota, persistent storage, Clear buttons + (`storage.info` / `storage.persist` / `storage.clear`, see [web.md](web.md)) - `disk.mediaCacheFolder`: audio waveform summaries kept between sessions (`Peaks/`) - `disk.conformedMediaFolder`: decoded (conformed) footage audio, written once per file and sample rate and read back instead of decoding again diff --git a/docs/web.md b/docs/web.md index 52d3419c..73ff79f7 100644 --- a/docs/web.md +++ b/docs/web.md @@ -43,13 +43,14 @@ URL flags: `?empty` (a blank project; the session is not restored or recorded), project instead of the last session), `?home` (show the start screen), `?storage=indexeddb` / `?storage=memory` (force a storage backend), `?noworkers` (render, analyse and draw viewer frames on the page's thread), `?frameworkers=N` (frame workers, default 2; 0 = viewer frames on -the page's thread), `?nosw` (don't register the service worker). +the page's thread), `?nogpuworkers` (frame workers render on their CPU, without a WebGPU device of +their own), `?nosw` (don't register the service worker). ## How the desktop pieces map to the browser | Desktop | Web | |---|---| -| frame render thread pool (`ui-egui/src/frames.rs`) | frame workers (below): `Frames::pump` hands queued frames to them, most important first (the viewer's frame, then prefetch); frames the GPU compositor can keep on the GPU render on the page. Without workers, queued frames render on the UI thread after each egui frame within 40 ms (24 ms while playing) | +| frame render thread pool (`ui-egui/src/frames.rs`) | frame workers (below), each with its own WebGPU device: `Frames::pump` hands queued frames to them, most important first (the viewer's frame, then prefetch); frames without effects that the page's GPU compositor can keep on the GPU render on the page. Without workers, queued frames render on the UI thread after each egui frame within 40 ms (24 ms while playing) | | Render Queue on a background thread | a Web Worker running a second engine instance (below); progress streams back, the page never waits | | tracker / mask tracker / Warp Stabilizer / 3D Camera Tracker / Roto Brush Freeze threads | the same workers; the analysed property group comes back as one undo step | | `rayon` parallel loops (raster, effects, export batches) | rayon's global pool falls back to the calling thread when threads are unavailable: same code, serial | @@ -64,7 +65,9 @@ the page's thread), `?nosw` (don't register the service worker). | system fonts | not scanned; the bundled fonts (Inter, Noto Serif, JetBrains Mono) are always there | | TCP control channel / MCP | `window.effectcraft` (below) | | cpal audio output | Web Audio (below) | -| GPU compositor on the desktop's wgpu device | the same compositor on eframe's WebGPU device (below) | +| GPU compositor on the desktop's wgpu device | the same compositor on eframe's WebGPU device, and GPU effects on each frame worker's own WebGPU device (below) | +| disk cache folder (Settings ▸ Disk ▸ Disk Cache) | viewer frames in the Origin Private File System, written by the frame workers (below) | +| (the file system's own tools) | the storage manager: Settings ▸ Disk ▸ Browser Storage and `storage.*` (below) | ### Persistence @@ -144,17 +147,26 @@ of the project: before prefetch); a request for a revision the worker does not hold is refused, a worker that lost its replica asks for the whole project again (`FrameReply::Resync`). -Frames the WebGPU compositor can finish alone still render on the page (they stay on the GPU, no -readback); everything else (CPU-only effects, 3D runs, the Software Only renderer, WebGL2) goes to -the workers. If no worker is usable, frames render on the page's thread as before. -`effectcraft.info().frameWorkers` reports `{workers, alive, busy, rendered, failed, lastMs, -syncs, syncBytes}`. +Frames without effects that the WebGPU compositor can finish alone still render on the page +(they stay on the GPU, no readback); everything else (frames with effects, 3D runs, the Software +Only renderer, WebGL2) goes to the workers. If no worker is usable, frames render on the page's +thread as before. `effectcraft.info().frameWorkers` reports `{workers, alive, busy, rendered, +failed, lastMs, syncs, syncBytes, gpu, gpuFrames, lastPasses, diskHits}` (`gpu`: each worker's +adapter, `{cpu: why}` without one, `null` until it reported). -**Why workers and not wasm threads:** shared-memory threads (SharedArrayBuffer + atomics, a -`wasm-bindgen-rayon`-style pool) need the standard library rebuilt with atomics (`build-std`, -nightly Rust); the repository builds on stable, so each worker runs its own engine instance with -its own memory. The page is still cross-origin isolated (`--serve` and the service worker send -COOP / COEP), so a threaded build can be added later without changing the deployment. +**Threading design: one engine instance per worker (decided, M13.24).** Shared-memory threads +inside one instance (SharedArrayBuffer + atomics, a `wasm-bindgen-rayon`-style pool) need the +standard library rebuilt with atomics (`-Z build-std`, nightly Rust) and every crate's statics +made thread-safe for the browser's rules (no blocking on the page's thread). The repository +builds on stable Rust, so concurrency comes from workers that each run their own engine instance +with their own memory: job workers (renders, analyses), frame workers (viewer frames, each with +its own GPU device) and the page. What crosses between them is explicit: project diffs, footage +bytes, frames, Roto Brush segmentations, cache keys. The cost is memory per worker (a project +replica, the footage it reads, a layer cache) and serial loops inside one frame (rayon's pool +falls back to the calling thread). The page is still cross-origin isolated (`--serve` and the +service worker send COOP / COEP), so a threaded build (`build-std` with `+atomics,+bulk-memory`, +`wasm-bindgen-rayon`) could be added later without changing the deployment; it would parallelise +the loops inside a frame, not replace the workers. ### Media Browser @@ -189,13 +201,94 @@ key press. eframe starts on WebGPU where the browser has it (WebGL2 otherwise), and the GPU compositor (`effectcraft-gpu`) runs on the same device: viewer frames are composited by compute shaders and -drawn straight from their texture, with no readback. Steps that need the CPU mid-render (3D runs, -adjustment layers, CPU-only effects) fall back to the CPU renderer for that frame, because the -page's thread can't wait for a GPU readback. The Info panel's pixel readout and the eyedroppers -read GPU frames back asynchronously (`Gpu::read_display_async`: the pixels arrive a frame -later). On WebGL2, or without a usable adapter, everything renders on the CPU. +drawn straight from their texture, with no readback. The Info panel's pixel readout and the +eyedroppers read GPU frames back asynchronously (`Gpu::read_display_async`: the pixels arrive a +frame later). On WebGL2, or without a usable adapter, everything renders on the CPU. `effectcraft.info().gpu` reports `{compositor, viewerOnGpu}`. +**GPU effects run in the frame workers.** A browser never lets JavaScript (or wasm) wait for a GPU +readback, not even in a worker: a buffer maps only once the thread returns to its event loop. +The CPU renderer, which runs GPU effect chains and reads their results back mid-frame, therefore +can't use the page's device. Instead each frame worker opens its own WebGPU device +(`navigator.gpu` in dedicated workers: Chromium, Safari Technology Preview; `Gpu::request_deferred`) +and renders with **deferred readbacks** (`crates/gpu/src/deferred.rs`): + +1. Every readback the renderer needs is keyed by what determines its pixels: a GPU effect chain by + its input buffer's pixels and the chain's parameters, the top-level frame by comp, time and + render options. +2. A key seen for the first time records the GPU work, starts an asynchronous copy (`map_async`) + and *misses*: the step returns a cheap placeholder (its input), and the pass is marked missed. + From the first miss to the end of the pass, the layer cache drops inserts + (`LayerCache::set_gate`), so no placeholder-derived buffer is kept. +3. The worker awaits the device (`Gpu::settled`) and renders the frame again + (`FrameServer::resume`); keys whose bytes arrived are served from them. A pass without a miss + is the frame. Chains that depend on other chains (a precomp with effects inside, effects that + read other layers) take one more pass per level; independent chains resolve together. A chain + whose kernels read data that missed while they ran (through the effect host) is not started, + so a result is only ever computed from genuine inputs. After 24 passes a frame renders on the + worker's CPU. + +The whole frame renders on the GPU (the compositor walk with GPU effects, then one readback) when +it can; steps that would need a mid-walk readback (an Advanced 3D run the kernels decline, CPU-only +effects on an adjustment layer) make the frame composite on the CPU instead, with its GPU effect +chains still on the GPU. Backend **Auto** works in the workers: it picks CPU or GPU per comp from +the frames' total times across their passes (`AutoPick`), as on the desktop. Without WebGPU in +workers (Firefox, older Safari) or with `?nogpuworkers`, the workers render on their CPU, as +before. A frame with effects goes to a worker when the workers have a GPU, so the page never runs +an effect on its thread. Particles and Advanced 3D scenes (which read back without a key) simulate +and rasterise on the worker's CPU. + +`effectcraft.workerFrameCheck({time?, scale?, backend?})` renders the active comp's frame in a +worker (its GPU by default) and on the page's CPU and reports the differences (`{maxDiff, +meanDiff, over4, workerMs}` in 8-bit levels): the smoke test uses it to check the GPU workers' +pixels. + +### Disk cache + +Settings ▸ Disk ▸ Disk Cache works in the browser, backed by the **Origin Private File System** +(`src/diskcache.rs`): viewer frames are kept under `effectcraft-cache/v1/frames/<32 hex>.ecc` +between visits, in the desktop cache's format (`disk_cache::frame_entry`: 8-bit premultiplied +RGBA, LZ4, a checksum), keyed by the same content hash (the comp and everything it uses, frame, +scale, view, render options). + +- A render request to a frame worker carries the frame's disk key; the worker posts the frame, + then writes the entry with a **synchronous access handle** into a temporary file moved into + place (`cacheWrite` in `js/host.js`; where `move()` is missing, the entry is written in place + and its checksum catches a partial file), and reports `FrameReply::Stored`. +- The page keeps the index (`DiskIndex`, shared with the desktop's cache): rebuilt at startup from + the folder listing (oldest files first, stale temporaries removed), updated by the workers' + writes and by reads. The size limit is Maximum Disk Cache Size, at most half the origin's quota; + the least recently used entries are deleted beyond it. +- A frame whose key is indexed is read and decoded on the page (asynchronously) instead of being + rendered; a damaged or missing file is a miss and is forgotten. The timeline's blue cache bar + shows the frames on disk. +- `cache.diskStats` reports the browser's cache (`{enabled, available, loaded, entries, bytes, + maxBytes, hits, misses, writes, evictions}`); Empty Disk Cache / `edit.purge {what: "disk"}` + clears it. `effectcraft.info().diskCache` has the same. + +Only frames are cached on disk in the browser: the desktop also keeps slow layer buffers, but a +layer lookup happens in the middle of a synchronous render, where the browser's file system can't +be read. + +### Storage manager + +Settings ▸ Disk ▸ **Browser Storage** (shown in the browser only) reports where the data lives +(Origin Private File System, IndexedDB or memory), the origin's usage and quota +(`navigator.storage.estimate()`), what the store holds (projects, imported media, auto-saves: +count and size) and the disk cache, whether the storage is persistent (with **Request Persistent +Storage**, `navigator.storage.persist()`), and **Clear** buttons for the disk cache, imported +media, auto-saves and saved projects. Agents use the same through engine commands +(`crates/engine/src/storage.rs`, the session's `StorageHost`): + +| Command | | +|---|---| +| `storage.info` | `{available, backend, usage, quota, persisted, files: {projects, media, autoSaves: {count, bytes}}, diskCache: {...}}` (`{available: false}` on the desktop) | +| `storage.persist` | ask for persistent storage; `persisted` in a later `storage.info` says whether it was granted | +| `storage.clear {what: diskCache\|media\|projects\|autoSaves\|all}` | delete them (settings stay); `{cleared, entries, bytes}` | + +The widgets have automation ids `settings.storage.backend`, `.usage`, `.diskCache`, `.persist` +and `.clear.`. + ### Offline and install (PWA) `manifest.webmanifest` (name, icons, standalone display) makes the app installable; `sw.js` @@ -222,7 +315,8 @@ with the method's `result`, errors reject with the message. | `effectcraft.listStored()` | `{backend, usage, quota, persisted, pending, files: [{path, size, modified}], config: [name]}` | | `effectcraft.removeStored(path)` | delete a stored file | | `effectcraft.flush()` | resolves once every change is written to browser storage | -| `effectcraft.info()` | graphics backend, `gpu`, `storage`, `audio` (`{state, sampleRate, backend, posted, played, underruns}`), `workers`, `restored`, `webgpu`, `serviceWorker`, version, `crossOriginIsolated`, load timings | +| `effectcraft.info()` | graphics backend, `gpu`, `storage`, `audio` (`{state, sampleRate, backend, posted, played, underruns}`), `workers`, `frameWorkers`, `diskCache`, `restored`, `webgpu`, `serviceWorker`, version, `crossOriginIsolated`, load timings | +| `effectcraft.workerFrameCheck(params)` | a frame rendered in a frame worker (its GPU by default) against the page's CPU render: `{width, height, maxDiff, meanDiff, over4, workerMs}` | `window.effectcraftLoad` holds `{wasmMs, readyMs}` (module fetch + compile, and until the app runs). @@ -251,7 +345,12 @@ node apps/effectcraft-web/tests/smoke.mjs --url http://127.0.0.1:8765/ --out tar It also checks the M13.10 paths: viewer frames rendered in frame workers while scrubbing with the CPU renderer (project synced as diffs, event-loop gaps under 400 ms), a `wait: true` render that -replies when done while the page keeps running, and the Media Browser over browser storage. +replies when done while the page keeps running, and the Media Browser over browser storage; and +the M13.24 ones: GPU effects (Gaussian Blur, Glow) rendered on the frame workers' own WebGPU +devices in passes while the page's event loop stays free, the worker's GPU frame matching the +page's CPU render (`workerFrameCheck`), the storage manager (`storage.info`, `storage.persist`, +the Settings ▸ Disk widgets), and the disk cache: frames written to the Origin Private File +System, served from it after a reload without rendering, and cleared with `storage.clear`. Native unit tests cover the storage model (`apps/effectcraft-web/src/store.rs`: write coalescing, file table, `ConfigStore` / `FileOps`, auto-save and crash recovery through the @@ -261,15 +360,24 @@ propagation through an offload streaming segmentations), the frame-worker protoc (`crates/engine/src/remote.rs`: diff / patch, a replica following edits renders the same pixels; `crates/ui-egui/tests/ui_web_offload.rs`: the frame cache through a remote renderer, `wait: true` deferred until the offloaded job ends) and the Media Browser's virtual tree (`media_browser.rs`, -`tests_panels.rs`). +`tests_panels.rs`). M13.24: deferred readbacks on a real device (`crates/gpu/src/tests_deferred.rs`: +whole-frame GPU and CPU-composited frames in passes match the CPU, placeholders never reach the +layer cache; `deferred.rs`: keys, late readbacks of an older frame dropped), the frame server's +passes and Auto timing (`remote.rs`, a deferred stand-in accelerator), the browser's routing and +disk cache (`crates/ui-egui/tests/ui_web_gpu_storage.rs`: a GPU frame worker renders effect frames, +the page's GPU keeps frames without effects, frames stored under their disk keys come back after a +"reload" without rendering, the blue cache bar sees them; the storage manager's widgets and +commands), the disk index (`disk_cache.rs`), the store's usage by kind and clearing (`store.rs`), +`storage.*` (`storage.rs`), and every WGSL float literal fitting f32 (browsers reject the whole +module otherwise; `tests.rs`). ## Gaps - No shared-memory threads: each worker is a separate engine instance (memory per worker: the - project replica and the footage it reads). -- GPU effects run only in the desktop GPU path: in the browser, effects run on the CPU (inside - workers for renders) because the page's thread can't wait for mid-render readbacks. -- No disk cache (Settings ▸ Media & Disk Cache) in the browser: the layer cache stays in memory. + project replica and the footage it reads); see the threading design above. +- GPU effects run in the frame workers (viewer frames, RAM preview). Render Queue renders and + analyses in job workers still run on their CPU (their loops are synchronous); particles and + Advanced 3D scenes in frame workers too. Browsers without WebGPU in workers render frames on + the workers' CPU. +- The browser's disk cache holds frames, not layer buffers. - Cancelling an analysis drops its partial result (the worker is terminated). -- Stored media count against the origin's storage quota; `effectcraft.listStored()` shows usage, - `removeStored` frees it (there is no storage manager in the UI yet).