M13.24: web app depth (WEB-1) — GPU effects in frame workers, OPFS disk cache, storage manager
- GPU in the browser: each frame worker opens its own WebGPU device (Gpu::request_deferred) and renders the GPU compositor and GPU effects with deferred readbacks (effectcraft-gpu deferred): readbacks keyed by content start with map_async and miss, the pass returns placeholders, the layer cache's gate drops inserts, and FrameServer::resume renders again once the device delivered. Backend Auto times whole frames per comp in the workers. Frames with effects go to GPU workers instead of the page. CPU fallback without WebGPU in workers (?nogpuworkers). - Fix: a WGSL constant (3.40282347e38) that Chrome rejects invalidated every GPU kernel in Chrome (the page's compositor too); a test checks every float literal fits f32. - Disk cache in the Origin Private File System: workers write sealed frame entries with sync access handles (temp file + move), the page keeps the DiskIndex (LRU under the settings' limit, at most half the quota) and reads hits instead of rendering; survives reloads; blue cache bar. - Storage manager: Settings ▸ Disk ▸ Browser Storage (usage/quota, persistent storage, Clear buttons, automation ids) and storage.info / storage.persist / storage.clear (StorageHost). - Docs: web.md (GPU workers, deferred passes, disk cache, storage manager, threading design: one engine instance per worker), architecture, preferences, parity (WEB-1), roadmap. - Tests: deferred passes vs CPU on a real device, placeholder gating, frame server passes and Auto, GPU worker routing + disk cache across a reload, storage commands and widgets, store usage by kind; smoke test: GPU effect frames in workers with pixel parity, disk hits after a reload, storage manager. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
081ce79de3
commit
1b726349ab
+101
-1
@@ -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<Gpu, String> {
|
||||
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<Gpu> {
|
||||
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<deferred::Deferred>> {
|
||||
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<deferred::Deferred>, r: &Renderer, comp: ItemId, t: Tick) -> Option<Image> {
|
||||
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<Image> {
|
||||
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<Arc<std::sync::atomic::AtomicBool>> {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user