Repository navigation
feat(render): real WebGPU backend + render==preview pipeline - #12
Merged
Merged
Conversation
WebGPU is now the primary renderer in a strict WebGPU → WebGL2 → Canvas2D fallback ladder, applied identically to live preview AND export. Two long-standing render≠preview bugs both close with this: - WebGPU was a stub (init always threw); active preview was WebGL2. - The export Compositor was hardcoded to Canvas2DBackend, so the final MP4 used the abgespeckte Canvas2D-Path (e.g. WEAR's TV-static, sat wobble, dropouts were WebGL2-only). Both now flow through one createBackend() factory. Whatever the platform picks for preview is what the export uses; visual parity between the two is no longer accidental. Implementation: - WebGPUBackend: real init/resize/dispose, render-target+snapshot textures, layer-blit pipeline (WGSL), per-FX snapshot via copyTextureToTexture, present-blit into the canvas. - 7 WGSL FX shaders ported from GLSL (vignette, rgb, zoom, uv, wear, echo, tape) with a WebGPUDrawContext that mirrors WebGL2DrawContext — catalog draw methods are structurally identical across backends. - Pipeline-cache with std140 uniform layout, two pipelines per FX (over/replace blend variants). - Compositor (export) now uses createBackend() and a two-canvas pattern: backend renders Layer+FX into an internal OffscreenCanvas (GPU- or 2D-context), then blits into a final 2D OffscreenCanvas for visualizers + ASS overlays + VideoFrame wrapping. - edit.worker.ts probes capabilities once on start and passes them to editRender/editRenderMulti, so the worker picks the same tier the main thread uses. - capabilities.ts: detectCapabilities() stays sync; new probeWebGPU() actually calls requestAdapter; initCapabilities() merges them into a module singleton; getCapabilities() is the sync read for renderers. - Factory: EXPERIMENTAL_WEBGPU flag and try/catch around WebGPU removed. caps.webgpu === true is treated as a hard guarantee. - Browser test matrix: 25 WebGPU pixel-parity tests (rotation, flip, letterbox, vignette overlay, RGB source-sampling, WEAR+TAPE serial composition). Vitest browser config gets WebGPU launch flags so headless Chromium exposes navigator.gpu. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…adback The user reported "frames hin und her springen" in WebGPU exports. PSNR analysis confirmed the smoking gun: PSNR(N → N+2) HIGHER than PSNR(N → N+1), which is physically only possible if frame N+1 is content from a different time than frames N and N+2. Forcing WebGL2 in the same pipeline produced clean monotonic PSNR — confirming the bug is WebGPU-specific. Root cause: the WebGPU canvas's implicit swap-chain (`context.getCurrentTexture()`) cycles between 2+ slots. In the export loop the VideoDecoder fires synchronously without VSync anchor, so `backendCanvas.transferToImageBitmap()` could read the wrong swap slot — alternating between slot A (frame N) and slot B (frame N-1). Live preview is unaffected because RAF + display present give a VSync anchor that flushes the swap chain in order. Fix: skip the canvas swap-chain entirely on the WebGPU export path. Add `WebGPUBackend.readbackToImageBitmap()` which does `copyTextureToBuffer(renderTarget → buffer)` → mapAsync → `createImageBitmap(ImageData)`. One GPU→CPU readback per frame; on 4K that's ~33 MB bandwidth — entirely fine for export. Knock-on changes: - `Compositor.compositeImage()` is now async (createImageBitmap from ImageData is async). Other backends keep using sync `transferToImageBitmap()` via the regular branch. - `editRender` decoder.output callback now serializes per-frame work through a Promise chain so concurrent frames don't interleave into the encoder. `editRenderMulti` just adds `await`. - New `compositor-isolation.browser.test.ts` (10 cases across 3 backends) verifies that two consecutive composite calls produce independent VideoFrame content — the regression target for this bug and any future canvas-snapshot mistake. - Settings page: explicit "Render backend" tile + WebGL2/WebGPU rows in the capability list so the active GPU backend is visible. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The user reported "Punch-In-Effekte fehlen" after the stutter fix.
Verified by exporting a 2-cam job with 7 FX in segment 12.41–26.22s:
the output rendered cleanly (no stutter) but every FX was absent.
Root cause: `Compositor.compositeImage()` used `timestampUs / 1e6` as
the time both for the output VideoFrame's timestamp AND for the FX
lookup `activeFxAt(opts.fx, t)`. But:
- VideoFrame timestamp must be **segment-relative** (starts at 0
each segment) so the encoder produces consecutive timestamps.
- FX live on the **master timeline** (e.g. fx.inS = 13.43 s).
So when `editSpec.segments = [{in: 12.41, out: 26.22}]`, output time
runs 0…13.8 s but FX in/out times run 13.43…24.83 s — `activeFxAt`
with the output time finds zero overlap and the FX list is empty
every frame. Whole-clip exports (no segments) coincidentally worked
because output time == master time there.
Pre-existing bug; surfaced now because the user's repro is a segment
export. Old Canvas2D path had the same code so the same FX would have
been missing in the prior commit's render — but the focus there was
the stutter so the FX absence wasn't called out.
Fix: `compositeImage(...)` takes an optional `tMasterS` parameter.
Both call sites in `edit.ts` pass it: `editRender` uses the source
frame's timestamp `tS` (single-cam = master time), `editRenderMulti`
passes `tMaster` directly. Falls back to `timestampUs / 1e6` when
omitted (whole-clip path stays correct).
Verified end-to-end: re-exported the same 13.8 s job; vignette,
wear, echo, rgb, tape, zoom and uv all visible in the output frames
at their expected timestamps.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The app must stay playable as an instrument with multi-cam 4K + FX loops. Two optimizations land here: **Export side — WebGPU readback hot path.** Three wins, in order of impact at 4K (1536×2048 RGBA): - Reuse the GPU readback buffer + RGBA staging array across frames (was allocate-and-destroy per frame, ~33 MB churn at 4K). - BGRA → RGBA via Uint32-view swap instead of per-byte loop. 4× fewer loop iterations; lets the JIT vectorize the per-pixel work into a handful of bitwise ops. - Compositor uses `putImageData(imageData)` instead of `createImageBitmap → drawImage`. Skips an async hop and a GPU bitmap allocation per frame. Steady-state benchmark (1080p, headless software-Vulkan): p50 ≈ 19 ms, p95 ≈ 21 ms isolated. Before: per-frame buffer allocate + per-byte swap easily landed in the 150–200 ms range. Native macOS GPU sees p95 < 15 ms post-fix. New `webgpu-readback.bench.browser.test.ts` locks a regression budget so we don't drift back. **Preview side — adaptive resolution scale.** New `AdaptiveScaler` measures `backend.drawFrame()` latency every RAF tick, sliding window p95. When p95 > 22 ms (≈45 fps): scale backbuffer down by ×0.75 (down to 0.25 floor). When p95 < 12 ms (≈80 fps): scale up by ×1.33 (capped at 1.0). Hysteresis: 30-tick cooldown between adjustments so the loop doesn't oscillate. Visual cost: minor softness when downscaled (browser upsamples at canvas-blit time on the GPU — basically free). Functional gain: the instrument stays responsive under load instead of dropping every nth frame. Manual `setScale()` still works and overrides the auto loop until the user releases. 9 unit tests covering the scaler logic (cooldown, bounds, both directions of adjustment, override).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
createBackend()factory as the live preview, eliminating the long-standing "WEAR/TAPE look different in render vs preview" bug. Same backend tier, same pixels in both paths.What's in here
5 commits on the branch:
b22f869feat(render): real WebGPU backend, render==preview via shared factory4e98211fix(render): WebGPU export stutter — bypass canvas swap-chain via GPU readback (the "frames hin und her springen" bug)7023d0afix(export): FX missed in segment exports — look up FX at master time, not output-relative timeecde9b6perf(render): cached readback buffer + adaptive preview scale6a0766eMergeorigin/main(mobile FX panel fixes fix(fx): make envelope screen usable on mobile #10/fix(fx): make ADSR knots reliably grabbable on mobile #11) — clean merge, no conflictsSettings page now exposes the active Render backend (WebGPU / WebGL2 / Canvas2D) and a real
probeWebGPU()(callsrequestAdapter()) socaps.webgpuis reliable.Test plan
adaptive-scaler.test.ts× 9, expandedwebgpu-backend.test.ts)webgpu-backend.browser.test.ts(25 — rotation/flip/letterbox/vignette/rgb/wear+tape serial),compositor-isolation.browser.test.ts(10 — VideoFrame isolation across 3 backends),webgpu-readback.bench.browser.test.ts(steady-state throughput)webgpu=falsein the worker drops cleanly to WebGL2 and produces equivalent outputKnown soft spots
stress.browser.test.tsandwebgpu-readback.bench.browser.test.tsare flaky in the full browser suite — isolated runs are green, but cross-test GPU driver state pollution makes timing budgets noisy. Pre-existing for stress; readback bench has a generous 300 ms p95 budget specifically because of this. Documented inline.Settings.test.tsx) was not updated for the new "Render backend" tile — passes the existing 5 cases, but doesn't cover the new tile. Low risk.🤖 Generated with Claude Code