Code-first video & image, rendered natively on WebGPU.
perf(preview, example): cache drawn preview frames; seek-friendly precomp plate
okanaslea9edc349d3a
diff --git a/.changeset/preview-frame-cache.md b/.changeset/preview-frame-cache.mdnew file mode 100644index 00000000..d8be0a89--- /dev/null+++ b/.changeset/preview-frame-cache.md@@ -0,0 +1,5 @@+---+"framefields": patch+---++Preview: drawn frames are cached, so stepping or scrubbing back to a frame you have already seen is a blit instead of a re-render. The cache is full-resolution and bounded by a byte budget, and is disabled for compositions that use vision (whose frames depend on the one before them).diff --git a/examples/22_framefields_launch/src/precomp.ts b/examples/22_framefields_launch/src/precomp.tsindex 51323815..167b44a1 100644--- a/examples/22_framefields_launch/src/precomp.ts+++ b/examples/22_framefields_launch/src/precomp.ts@@ -67,6 +67,14 @@ async function render(name: string, p: Plate) {"14","-preset","slow",+ // A short GOP keeps the plate cheap to *seek*: the VFX chapter instances+ // it dozens of times at different in-points, and x264's default 250-frame+ // keyframe interval makes each of those seeks re-decode up to 4s before+ // the frame it wants. A keyframe every 15 frames bounds that.+ "-g",+ "15",+ "-keyint_min",+ "15",asset(p.file),]);console.log(diff --git a/packages/framefields/src/preview/player.ts b/packages/framefields/src/preview/player.tsindex f1ca71d2..c043e645 100644--- a/packages/framefields/src/preview/player.ts+++ b/packages/framefields/src/preview/player.ts@@ -174,6 +174,7 @@ async function createPlayer(durationMs,frameCount: Math.max(1, Math.round((durationMs / 1000) * fps)),};+ const hasVision = containsOp(vm as VirtualMediaData, "Vision");const device = await acquireDevice();const canvas =@@ -192,6 +193,69 @@ async function createPlayer(let offscreen: GPUTexture | undefined;+ // Frames drawn on screen are kept so stepping or scrubbing back to one is a+ // blit, not a re-render. Full resolution, bounded by a byte budget; playback+ // still uses the reduced-res ring below. Vision compositions are excluded:+ // their frames depend on the one before, so a hit would skip that work.+ const renderCache = new Map<number, { tex: GPUTexture; used: number }>();+ const renderCacheMax = Math.max(+ 1,+ Math.min(+ 240,+ Math.floor(+ (128 * 1024 * 1024) / Math.max(1, canvas.width * canvas.height * 4),+ ),+ ),+ );+ let renderCacheClock = 0;++ function newRenderCacheTexture(): GPUTexture {+ return device.createTexture({+ size: [canvas.width, canvas.height],+ format: surface.colorFormat,+ usage:+ GPUTextureUsage.RENDER_ATTACHMENT |+ GPUTextureUsage.TEXTURE_BINDING |+ GPUTextureUsage.COPY_DST,+ label: "preview_frame_cache",+ });+ }++ function takeRenderCache(frame: number): GPUTexture | undefined {+ if (hasVision) return undefined;+ const hit = renderCache.get(frame);+ if (!hit) return undefined;+ hit.used = ++renderCacheClock;+ return hit.tex;+ }++ function storeRenderCache(frame: number, tex: GPUTexture): void {+ if (hasVision) {+ tex.destroy();+ return;+ }+ const existing = renderCache.get(frame);+ if (existing) {+ if (existing.tex !== tex) existing.tex.destroy();+ renderCache.delete(frame);+ }+ renderCache.set(frame, { tex, used: ++renderCacheClock });+ while (renderCache.size > renderCacheMax) {+ let oldestKey: number | undefined;+ let oldestUsed = Number.POSITIVE_INFINITY;+ for (const [key, entry] of renderCache) {+ if (entry.used < oldestUsed) {+ oldestUsed = entry.used;+ oldestKey = key;+ }+ }+ if (oldestKey === undefined) break;+ const evicted = renderCache.get(oldestKey);+ renderCache.delete(oldestKey);+ evicted?.tex.destroy();+ }+ }+async function draw(frame: number, toScreen = true): Promise<void> {const renderer = getRenderer2D(device, surface.colorFormat);const ctx: RenderContextValue = { device, renderer, surface };@@ -261,9 +325,18 @@ async function createPlayer(vm as VirtualMediaData,props,);+ let cacheTex: GPUTexture | undefined;+ if (toScreen) {+ cacheTex = newRenderCacheTexture();+ encoder.copyTextureToTexture({ texture: tex }, { texture: cacheTex }, [+ w,+ h,+ ]);+ }device.queue.submit([encoder.finish()]);if (toScreen) surface.present();await device.queue.onSubmittedWorkDone();+ if (cacheTex) storeRenderCache(frame, cacheTex);}// One frame at a time. Requests made while a frame draws collapse to the@@ -276,7 +349,13 @@ async function createPlayer(wanted = null;const started = performance.now();try {- await draw(f);+ const cached = takeRenderCache(f);+ if (cached) {+ blitter.blit(cached, surface.getCurrentTexture());+ surface.present();+ } else {+ await draw(f);+ }} catch (err) {console.error("[framefields] frame", f, err);}@@ -343,6 +422,15 @@ async function createPlayer(if (token !== bufferToken) return;const frame = produced;const slot = frame % capacity;+ const cached = takeRenderCache(frame);+ if (cached) {+ blitter.blit(cached, ringTexture(slot));+ ringFrames[slot] = frame;+ produced = frame + 1;+ // Yield so a run of cached frames can't starve the page.+ await new Promise((r) => setTimeout(r, 0));+ continue;+ }drawing = draw(frame, false).then(() => {if (offscreen) blitter.blit(offscreen, ringTexture(slot));@@ -403,7 +491,7 @@ async function createPlayer(meta,canvas,lastRenderMs: 0,- hasVision: containsOp(vm as VirtualMediaData, "Vision"),+ hasVision,render(frame) {wanted = frame;const done = new Promise<number>((resolve) => waiters.push(resolve));