AI media harness for planning, generating, and editing locally
TypeScript
Render GPU effects in preview and export
Bitti8f026d293e4e
diff --git a/docs/video-effects.md b/docs/video-effects.mdnew file mode 100644index 0000000..c7ab61f--- /dev/null+++ b/docs/video-effects.md@@ -0,0 +1,24 @@+# Video effects++Select a video or image clip in the timeline and use **Add Effect** in the inspector.+Each effect can be adjusted or removed independently. Locked tracks cannot be edited.++| Effect | Controls |+| --- | --- |+| Sharpen | Edge enhancement, 0–200%; zero bypasses sharpening. |+| Gaussian Blur | Radius, 0–24 source pixels; zero bypasses blur. |+| Color Correction | Exposure −4 to +4, contrast −100 to +100%, saturation 0–200%. Saturation zero produces monochrome. |+| Vignette | Edge darkening, 0–100%. |+| 3D LUT | Import or replace a `.cube` file and blend it at 0–100% intensity. |++The render order is Chroma Key → Gaussian Blur → Sharpen → Color Correction → LUT → Vignette → Look temperature. Transform, opacity, and transitions composite the result into the timeline. This order is fixed; the inspector is not a reorderable effect stack. Blur radii refer to source resolution, so preview and export use the same radius even at different output sizes.++LUT import accepts 3D `.cube` tables from 2³ through 65³, including `TITLE`, `DOMAIN_MIN`/`DOMAIN_MAX`, and Resolve's `LUT_3D_INPUT_RANGE`. Tables use red-fastest row order and trilinear interpolation. One-dimensional and combined shaper LUTs are rejected with an explanation. Files are limited to 16 MB. LUTs are embedded in the project and need no external file after import. A failed replacement leaves the previous LUT intact.++Color operations work on the decoded, display-referred RGB picture. Choose a LUT appropriate for that input; importing a LUT does not configure log, HDR, or project color management. Exposure multiplies RGB by 2 raised to the selected value; contrast pivots around 0.5. Final output is clamped to 0–1.++These effects require WebGPU. Preview and export share the same shader processor, import decoded video frames as external textures, and keep intermediate pictures in GPU memory. Blur uses two separable passes and filters premultiplied alpha to avoid colored fringes around keyed objects. LUTs and working textures are reused across frames. Playback redraws on video-frame callbacks; paused footage redraws after seeking or changing settings.++If a GPU is unavailable, the monitor reports an error and export refuses to produce a file with these effects omitted. Projects without these effects retain the existing canvas export fallback.++Implementation references: [WebGPU external textures](https://www.w3.org/TR/webgpu/#external-texture), [WGSL](https://www.w3.org/TR/WGSL/), and the [Adobe Cube specification](https://kono.phpage.fr/images/a/a1/Adobe-cube-lut-specification-1.0.pdf).diff --git a/src-tauri/src/project/validation/timeline.rs b/src-tauri/src/project/validation/timeline.rsindex 5a1475a..e7f9a1a 100644--- a/src-tauri/src/project/validation/timeline.rs+++ b/src-tauri/src/project/validation/timeline.rs@@ -61,11 +61,25 @@ pub(super) fn validate(config: &mut ProjectConfig) -> Result<(), String> {return Err(format!("Clip '{}' has an invalid transition.", clip.id));}}- let within = |value: f64, min: f64, max: f64| value.is_finite() && (min..=max).contains(&value);- if clip.sharpen.as_ref().is_some_and(|v| !within(v.amount, 0.0, 200.0))- || clip.blur.as_ref().is_some_and(|v| !within(v.radius, 0.0, 24.0))- || clip.vignette.as_ref().is_some_and(|v| !within(v.amount, 0.0, 100.0))- || clip.color_correction.as_ref().is_some_and(|v| !within(v.exposure, -4.0, 4.0) || !within(v.contrast, -100.0, 100.0) || !within(v.saturation, 0.0, 200.0))+ let within =+ |value: f64, min: f64, max: f64| value.is_finite() && (min..=max).contains(&value);+ if clip+ .sharpen+ .as_ref()+ .is_some_and(|v| !within(v.amount, 0.0, 200.0))+ || clip+ .blur+ .as_ref()+ .is_some_and(|v| !within(v.radius, 0.0, 24.0))+ || clip+ .vignette+ .as_ref()+ .is_some_and(|v| !within(v.amount, 0.0, 100.0))+ || clip.color_correction.as_ref().is_some_and(|v| {+ !within(v.exposure, -4.0, 4.0)+ || !within(v.contrast, -100.0, 100.0)+ || !within(v.saturation, 0.0, 200.0)+ }){return Err(format!("Clip '{}' has invalid video effects.", clip.id));}@@ -75,10 +89,15 @@ pub(super) fn validate(config: &mut ProjectConfig) -> Result<(), String> {}if let Some(table) = &lut.table {if !(2..=65).contains(&table.size)- || table.name.is_empty() || table.name.chars().count() > 256+ || table.name.is_empty()+ || table.name.chars().count() > 256|| table.values.len() != table.size.pow(3) * 3|| table.values.iter().any(|v| !within(*v, -65504.0, 65504.0))- || (0..3).any(|i| !table.domain_min[i].is_finite() || !table.domain_max[i].is_finite() || table.domain_min[i] >= table.domain_max[i])+ || (0..3).any(|i| {+ !within(table.domain_min[i], -65504.0, 65504.0)+ || !within(table.domain_max[i], -65504.0, 65504.0)+ || table.domain_min[i] as f32 >= table.domain_max[i] as f32+ }){return Err(format!("Clip '{}' has an invalid LUT table.", clip.id));}diff --git a/src-tauri/src/tests/persistence.rs b/src-tauri/src/tests/persistence.rsindex c3ef1b8..f59898e 100644--- a/src-tauri/src/tests/persistence.rs+++ b/src-tauri/src/tests/persistence.rs@@ -176,18 +176,36 @@ fn video_effects_survive_save_and_reopen() {"name": "Identity", "size": 2, "domainMin": [0, 0, 0], "domainMax": [1, 1, 1],"values": [0,0,0, 1,0,0, 0,1,0, 1,1,0, 0,0,1, 1,0,1, 0,1,1, 1,1,1]}});- let config = without_derived_project_state(validate_and_normalize_config(serde_json::from_value(json).unwrap()).unwrap());+ let config = without_derived_project_state(+ validate_and_normalize_config(serde_json::from_value(json).unwrap()).unwrap(),+ );let root = tempfile::tempdir().unwrap();let created = create_project_in(root.path(), &config).unwrap();let folder = PathBuf::from(&created.folder_path);write_project(&folder, &config).unwrap();assert_eq!(read_project(&folder).unwrap().config, config);let mut invalid = config.clone();- invalid.timeline.tracks[0].clips[0].lut.as_mut().unwrap().table.as_mut().unwrap().values.pop();- assert!(validate_and_normalize_config(invalid).unwrap_err().contains("LUT table"));+ invalid.timeline.tracks[0].clips[0]+ .lut+ .as_mut()+ .unwrap()+ .table+ .as_mut()+ .unwrap()+ .values+ .pop();+ assert!(validate_and_normalize_config(invalid)+ .unwrap_err()+ .contains("LUT table"));let mut invalid = config.clone();- invalid.timeline.tracks[0].clips[0].blur.as_mut().unwrap().radius = 25.0;- assert!(validate_and_normalize_config(invalid).unwrap_err().contains("video effects"));+ invalid.timeline.tracks[0].clips[0]+ .blur+ .as_mut()+ .unwrap()+ .radius = 25.0;+ assert!(validate_and_normalize_config(invalid)+ .unwrap_err()+ .contains("video effects"));}#[test]diff --git a/src/components/workspace/ClipEffects.test.tsx b/src/components/workspace/ClipEffects.test.tsxindex 902b433..b4b1e65 100644--- a/src/components/workspace/ClipEffects.test.tsx+++ b/src/components/workspace/ClipEffects.test.tsx@@ -1,5 +1,5 @@// @vitest-environment jsdom-import { cleanup, fireEvent, render, screen, within } from "@testing-library/react";+import { cleanup, fireEvent, render, screen, within, waitFor } from "@testing-library/react";import { useState } from "react";import { afterEach, describe, expect, it, vi } from "vitest";import { timelineClipSchema, type TimelineClip } from "../../lib/project";@@ -16,6 +16,50 @@ function Harness() {}describe("clip effects", () => {+ it("adds, edits and removes the new effects independently", () => {+ render(<Harness />);+ for (const name of [/Sharpen Enhance/, /Gaussian Blur Soften/, /Color Correction Adjust/, /Vignette Darken/]) {+ fireEvent.click(screen.getByRole("button", { name: "Add Effect" }));+ fireEvent.click(screen.getByRole("button", { name }));+ }+ fireEvent.change(screen.getByLabelText("Sharpen amount"), { target: { value: "120" } });+ fireEvent.change(screen.getByLabelText("Blur radius"), { target: { value: "6.5" } });+ fireEvent.change(screen.getByLabelText("Exposure"), { target: { value: "-1.2" } });+ fireEvent.change(screen.getByLabelText("Saturation"), { target: { value: "0" } });+ fireEvent.change(screen.getByLabelText("Vignette amount"), { target: { value: "75" } });+ expect(JSON.parse(screen.getByTestId("saved").textContent!)).toMatchObject({+ sharpen: { amount: 120 }, blur: { radius: 6.5 }, colorCorrection: { exposure: -1.2, saturation: 0 }, vignette: { amount: 75 },+ });+ fireEvent.click(screen.getByRole("button", { name: "Remove Gaussian Blur effect" }));+ expect(JSON.parse(screen.getByTestId("saved").textContent!).blur).toBeUndefined();+ expect(screen.getByLabelText("Sharpen amount")).toBeTruthy();+ });++ it("imports a LUT, adjusts its strength, and preserves it after an invalid replacement", async () => {+ render(<Harness />);+ fireEvent.click(screen.getByRole("button", { name: "Add Effect" }));+ fireEvent.click(screen.getByRole("button", { name: /3D LUT Apply/ }));+ const cube = "LUT_3D_SIZE 2\n0 0 0\n1 0 0\n0 1 0\n1 1 0\n0 0 1\n1 0 1\n0 1 1\n1 1 1";+ fireEvent.change(screen.getByLabelText("Import cube LUT"), { target: { files: [{ name: "identity.cube", size: cube.length, text: async () => cube }] } });+ await screen.findByText("identity.cube · 2³");+ fireEvent.change(screen.getByLabelText("LUT intensity"), { target: { value: "45" } });+ fireEvent.change(screen.getByLabelText("Import cube LUT"), { target: { files: [{ name: "bad.cube", size: 4, text: async () => "oops" }] } });+ await screen.findByRole("alert");+ expect(JSON.parse(screen.getByTestId("saved").textContent!).lut).toMatchObject({ intensity: 45, table: { name: "identity.cube", size: 2 } });+ });++ it("does not apply an outstanding LUT import to a different clip", async () => {+ let resolve!: (text: string) => void;+ const pending = new Promise<string>((done) => { resolve = done; });+ const onChange = vi.fn();+ const view = render(<ClipEffects clip={{ ...original, lut: { intensity: 100 } }} disabled={false} onChange={onChange} />);+ fireEvent.change(screen.getByLabelText("Import cube LUT"), { target: { files: [{ name: "identity.cube", size: 50, text: () => pending }] } });+ view.rerender(<ClipEffects clip={{ ...original, id: "other", lut: { intensity: 100 } }} disabled={false} onChange={onChange} />);+ resolve("LUT_3D_SIZE 2\n" + "0 0 0\n".repeat(8));+ await waitFor(() => expect(screen.queryByRole("status")).toBeNull());+ expect(onChange).not.toHaveBeenCalled();+ });+it("adds only chosen effects, persists edits, and removes them without changing other effects", () => {render(<Harness />);expect(screen.queryByRole("heading", { name: "Look" })).toBeNull();diff --git a/src/components/workspace/ClipEffects.tsx b/src/components/workspace/ClipEffects.tsxindex ba3c8f3..b0c3ac5 100644--- a/src/components/workspace/ClipEffects.tsx+++ b/src/components/workspace/ClipEffects.tsx@@ -1,8 +1,9 @@import { Plus, X } from "lucide-react";import { useEffect, useId, useRef, useState, type ReactNode } from "react";import { DEFAULT_CLIP_CHROMA_KEY, DEFAULT_CLIP_LOOK, type TimelineClip } from "../../lib/project";+import { MAX_LUT_FILE_BYTES, parseCube } from "../../lib/effectSettings";-type EffectId = "look" | "transition" | "chromaKey";+type EffectId = "look" | "transition" | "chromaKey" | "sharpen" | "blur" | "colorCorrection" | "vignette" | "lut";type EditorProps = { clip: TimelineClip; update: (patch: Partial<TimelineClip>) => void };type EffectDefinition = {id: EffectId;@@ -12,6 +13,43 @@ type EffectDefinition = {editor: (props: EditorProps) => ReactNode;};+function Slider({ label, value, min = 0, max = 100, step = 1, suffix = "%", onChange }: {+ label: string; value: number; min?: number; max?: number; step?: number; suffix?: string; onChange: (value: number) => void;+}) {+ return <label className="range-field"><span>{label} <b>{value}{suffix}</b></span><input aria-label={label} type="range" min={min} max={max} step={step} value={value} onChange={(event) => onChange(Number(event.target.value))} /></label>;+}++function LutEditor(props: EditorProps) {+ const [error, setError] = useState<string | null>(null);+ const [loading, setLoading] = useState(false);+ const latest = useRef(props);+ latest.current = props;+ const request = useRef(0);+ useEffect(() => () => { request.current++; }, []);+ const lut = props.clip.lut!;+ return <>+ <label><span>{lut.table ? "Replace LUT" : "Import LUT"}</span><input aria-label="Import cube LUT" type="file" accept=".cube" onChange={async (event) => {+ const file = event.target.files?.[0];+ event.target.value = "";+ if (!file) return;+ const token = ++request.current;+ setError(null); setLoading(true);+ try {+ if (!file.name.toLowerCase().endsWith(".cube")) throw new Error("Choose a 3D .cube LUT file.");+ if (file.size > MAX_LUT_FILE_BYTES) throw new Error("LUT files must be smaller than 16 MB.");+ const table = parseCube(await file.text(), file.name);+ if (request.current === token) latest.current.update({ lut: { ...latest.current.clip.lut!, table } });+ } catch (reason) {+ if (request.current === token) setError(reason instanceof Error ? reason.message : String(reason));+ } finally { if (request.current === token) setLoading(false); }+ }} /></label>+ {loading && <p role="status">Reading LUT…</p>}+ {lut.table ? <p>{lut.table.name} · {lut.table.size}³</p> : <p>Import a 3D .cube LUT (2–65 points). The LUT is saved with the project.</p>}+ {error && <p role="alert">{error}</p>}+ <Slider label="LUT intensity" value={lut.intensity} onChange={(intensity) => props.update({ lut: { ...lut, intensity } })} />+ </>;+}+// One catalogue drives the picker, defaults and editors. Missing settings mean// the effect is absent; older projects with Look/Transition keep their edits.const EFFECTS: readonly EffectDefinition[] = [@@ -50,6 +88,38 @@ const EFFECTS: readonly EffectDefinition[] = [</>;},},+ {+ id: "sharpen", name: "Sharpen", description: "Enhance fine edges and detail",+ defaults: { sharpen: { amount: 50 } },+ editor: ({ clip, update }) => <Slider label="Sharpen amount" value={clip.sharpen!.amount} max={200} onChange={(amount) => update({ sharpen: { amount } })} />,+ },+ {+ id: "blur", name: "Gaussian Blur", description: "Soften the picture with a Gaussian blur",+ defaults: { blur: { radius: 4 } },+ editor: ({ clip, update }) => <Slider label="Blur radius" value={clip.blur!.radius} max={24} step={0.5} suffix=" px" onChange={(radius) => update({ blur: { radius } })} />,+ },+ {+ id: "colorCorrection", name: "Color Correction", description: "Adjust exposure, contrast, and saturation",+ defaults: { colorCorrection: { exposure: 0, contrast: 0, saturation: 100 } },+ editor: ({ clip, update }) => {+ const color = clip.colorCorrection!;+ return <>+ <Slider label="Exposure" value={color.exposure} min={-4} max={4} step={0.1} suffix=" stops" onChange={(exposure) => update({ colorCorrection: { ...color, exposure } })} />+ <Slider label="Contrast" value={color.contrast} min={-100} onChange={(contrast) => update({ colorCorrection: { ...color, contrast } })} />+ <Slider label="Saturation" value={color.saturation} max={200} onChange={(saturation) => update({ colorCorrection: { ...color, saturation } })} />+ </>;+ },+ },+ {+ id: "vignette", name: "Vignette", description: "Darken the edges of the picture",+ defaults: { vignette: { amount: 35 } },+ editor: ({ clip, update }) => <Slider label="Vignette amount" value={clip.vignette!.amount} onChange={(amount) => update({ vignette: { amount } })} />,+ },+ {+ id: "lut", name: "3D LUT", description: "Apply a color look from a .cube file",+ defaults: { lut: { intensity: 100 } },+ editor: (props) => <LutEditor key={props.clip.id} {...props} />,+ },];export function ClipEffects({ clip, disabled, onChange }: {diff --git a/src/components/workspace/ProgramLayer.tsx b/src/components/workspace/ProgramLayer.tsxindex 0f1843a..8b88e3c 100644--- a/src/components/workspace/ProgramLayer.tsx+++ b/src/components/workspace/ProgramLayer.tsx@@ -3,13 +3,16 @@ import { clipFrameStyle, clipVisualSettings, type ClipFrameStyle } from "../../limport { PreviewSources } from "../../lib/exportPipeline";import type { ProjectAsset, TimelineClip } from "../../lib/project";import { ChromaKeyPreview } from "./ChromaKeyPreview";+import { VideoEffectsPreview } from "./VideoEffectsPreview";+import { hasVideoEffects } from "../../lib/effectSettings";-export function previewMediaStyle({ transform, look, opacity, revealStart, revealEnd }: ClipFrameStyle): CSSProperties {+export function previewMediaStyle(frame: ClipFrameStyle): CSSProperties {+ const { transform, look, opacity, revealStart, revealEnd } = frame;const warmth = look.temperature / 100;return {transform: `translate(${transform.positionX}%, ${transform.positionY}%) scale(${transform.scale / 100}) rotate(${transform.rotation}deg)`,opacity,- filter: warmth === 0 ? "none" : `sepia(${Math.abs(warmth) * 0.22}) saturate(${1 + Math.abs(warmth) * 0.3}) hue-rotate(${warmth > 0 ? -8 : 172}deg)`,+ filter: warmth === 0 || hasVideoEffects(frame) ? "none" : `sepia(${Math.abs(warmth) * 0.22}) saturate(${1 + Math.abs(warmth) * 0.3}) hue-rotate(${warmth > 0 ? -8 : 172}deg)`,clipPath: `inset(0 ${(1 - revealEnd) * 100}% 0 ${revealStart * 100}%)`,};}@@ -50,12 +53,14 @@ export function ProgramLayer({ clip, asset, sources, playheadMs, playing }: {};useEffect(sync, [playheadMs, playing, clip, url]);const style = previewMediaStyle(clipFrameStyle(clipVisualSettings(clip), playheadMs - clip.startMs));- const sourceStyle: CSSProperties = clip.chromaKey ? { ...style, visibility: "hidden", position: "absolute" } : style;+ const effects = hasVideoEffects(clip);+ const sourceStyle: CSSProperties = clip.chromaKey || effects ? { ...style, visibility: "hidden", position: "absolute" } : style;return <div className="program-layer" aria-label={`Background clip: ${clip.label}`}>{url && (isImage? <img ref={image} src={url} alt={clip.label} style={sourceStyle} onError={() => setError(`Could not decode ${clip.label}.`)} />: <video ref={video} src={url} style={sourceStyle} muted playsInline preload="auto" onLoadedMetadata={sync} onError={() => setError(`Could not decode ${clip.label}.`)} />)}- {url && clip.chromaKey && <ChromaKeyPreview source={isImage ? image : video} sourceUrl={url} effect={clip.chromaKey} playing={playing && !isImage} style={style} onError={setError} />}+ {url && effects && <VideoEffectsPreview source={isImage ? image : video} sourceUrl={url} effects={clip} playing={playing && !isImage} style={style} onError={setError} />}+ {url && !effects && clip.chromaKey && <ChromaKeyPreview source={isImage ? image : video} sourceUrl={url} effect={clip.chromaKey} playing={playing && !isImage} style={style} onError={setError} />}{error && <div className="program-note program-note--error" role="alert">{error}</div>}</div>;}diff --git a/src/components/workspace/ProgramMonitor.tsx b/src/components/workspace/ProgramMonitor.tsxindex c96adb9..2af4de5 100644--- a/src/components/workspace/ProgramMonitor.tsx+++ b/src/components/workspace/ProgramMonitor.tsx@@ -1,4 +1,6 @@import { ChromaKeyPreview } from "./ChromaKeyPreview";+import { VideoEffectsPreview } from "./VideoEffectsPreview";+import { hasVideoEffects } from "../../lib/effectSettings";import { ProgramLayer, previewMediaStyle } from "./ProgramLayer";import { Film, TriangleAlert } from "lucide-react";import { useCallback, useEffect, useMemo, useRef, useState } from "react";@@ -114,7 +116,8 @@ export function ProgramMonitor({ config, folderPath, playheadMs, playing, onSeek[clip, playheadMs],);const mediaStyle = frameStyle ? previewMediaStyle(frameStyle) : undefined;- const sourceStyle: React.CSSProperties | undefined = clip?.chromaKey ? { ...mediaStyle, visibility: "hidden", position: "absolute" } : mediaStyle;+ const effects = clip ? hasVideoEffects(clip) : false;+ const sourceStyle: React.CSSProperties | undefined = clip?.chromaKey || effects ? { ...mediaStyle, visibility: "hidden", position: "absolute" } : mediaStyle;/* Sound that should be audible at this moment: audio-only files and embeddedsound from every active video, minus muted tracks. The visible video playsits own stream, so a second audio element is only needed for the rest. */@@ -435,7 +438,8 @@ export function ProgramMonitor({ config, folderPath, playheadMs, playing, onSeekonError={() => setError("This file could not be decoded.")}/>}{asset && isImage(asset) && url && <img ref={imageRef} src={url} alt={clip?.label ?? ""} style={sourceStyle} />}- {asset && url && clip?.chromaKey && <ChromaKeyPreview source={isImage(asset) ? imageRef : videoRef} sourceUrl={url} effect={clip.chromaKey} playing={playing && !isImage(asset)} style={mediaStyle} onError={setError} />}+ {asset && url && clip && effects && <VideoEffectsPreview source={isImage(asset) ? imageRef : videoRef} sourceUrl={url} effects={clip} playing={playing && !isImage(asset)} style={mediaStyle} onError={setError} />}+ {asset && url && !effects && clip?.chromaKey && <ChromaKeyPreview source={isImage(asset) ? imageRef : videoRef} sourceUrl={url} effect={clip.chromaKey} playing={playing && !isImage(asset)} style={mediaStyle} onError={setError} />}{clip && frameStyle && editingClipId === clip.id && onTransformChange && !transformEditingDisabled && <divclassName="program-transform"style={{ transform: `translate(${frameStyle.transform.positionX}%, ${frameStyle.transform.positionY}%) scale(${frameStyle.transform.scale / 100}) rotate(${frameStyle.transform.rotation}deg)` }}diff --git a/src/components/workspace/VideoEffectsPreview.test.tsx b/src/components/workspace/VideoEffectsPreview.test.tsxnew file mode 100644index 0000000..d004ee9--- /dev/null+++ b/src/components/workspace/VideoEffectsPreview.test.tsx@@ -0,0 +1,57 @@+// @vitest-environment jsdom+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";+import { useRef } from "react";+import { afterEach, describe, expect, it, vi } from "vitest";+import { createVideoEffectsPreview, type GpuEffects } from "../../lib/videoEffectsGpu";+import { VideoEffectsPreview } from "./VideoEffectsPreview";++vi.mock("../../lib/videoEffectsGpu", () => ({ createVideoEffectsPreview: vi.fn() }));+afterEach(() => { cleanup(); vi.resetAllMocks(); vi.unstubAllGlobals(); });++function Harness({ effects, onError = vi.fn() }: { effects: GpuEffects; onError?: (message: string) => void }) {+ const image = useRef<HTMLImageElement>(null);+ return <><img ref={image} src="blob:picture" alt="Source" /><VideoEffectsPreview source={image} sourceUrl="blob:picture" effects={effects} playing={false} onError={onError} /></>;+}++describe("GPU effects preview lifecycle", () => {+ it("redraws paused media after seeking and editing without recreating GPU resources", async () => {+ const renderer = { draw: vi.fn(), dispose: vi.fn() };+ vi.mocked(createVideoEffectsPreview).mockResolvedValue(renderer);+ const view = render(<Harness effects={{ blur: { radius: 4 } }} />);+ await act(async () => {});+ expect(renderer.draw).toHaveBeenCalledWith(screen.getByAltText("Source"), { blur: { radius: 4 } });+ renderer.draw.mockClear();+ fireEvent.seeked(screen.getByAltText("Source"));+ expect(renderer.draw).toHaveBeenCalledTimes(1);+ view.rerender(<Harness effects={{ blur: { radius: 8 } }} />);+ expect(renderer.draw).toHaveBeenLastCalledWith(screen.getByAltText("Source"), { blur: { radius: 8 } });+ expect(createVideoEffectsPreview).toHaveBeenCalledTimes(1);+ view.unmount();+ expect(renderer.dispose).toHaveBeenCalledTimes(1);+ });++ it("disposes a renderer that finishes initializing after the clip is removed", async () => {+ const renderer = { draw: vi.fn(), dispose: vi.fn() };+ let resolve!: (value: typeof renderer) => void;+ vi.mocked(createVideoEffectsPreview).mockImplementation(() => new Promise((done) => { resolve = done; }));+ const view = render(<Harness effects={{ sharpen: { amount: 50 } }} />);+ view.unmount();+ await act(async () => resolve(renderer));+ expect(renderer.draw).not.toHaveBeenCalled();+ expect(renderer.dispose).toHaveBeenCalledTimes(1);+ });++ it("reports GPU startup and draw failures", async () => {+ const onError = vi.fn();+ vi.mocked(createVideoEffectsPreview).mockRejectedValue(new Error("No GPU adapter"));+ const view = render(<Harness effects={{ vignette: { amount: 50 } }} onError={onError} />);+ await act(async () => {});+ expect(onError).toHaveBeenCalledWith("Error: No GPU adapter");+ view.unmount();+ const renderer = { draw: vi.fn(() => { throw new Error("Device lost"); }), dispose: vi.fn() };+ vi.mocked(createVideoEffectsPreview).mockResolvedValue(renderer);+ render(<Harness effects={{ sharpen: { amount: 50 } }} onError={onError} />);+ await act(async () => {});+ expect(onError).toHaveBeenCalledWith("Error: Device lost");+ });+});diff --git a/src/components/workspace/VideoEffectsPreview.tsx b/src/components/workspace/VideoEffectsPreview.tsxnew file mode 100644index 0000000..c26b53c--- /dev/null+++ b/src/components/workspace/VideoEffectsPreview.tsx@@ -0,0 +1,53 @@+import { useEffect, useRef, type CSSProperties, type RefObject } from "react";+import { createVideoEffectsPreview, type GpuEffects } from "../../lib/videoEffectsGpu";++export function VideoEffectsPreview({ source, sourceUrl, effects, playing, style, onError }: {+ source: RefObject<HTMLVideoElement | HTMLImageElement>;+ sourceUrl: string;+ effects: GpuEffects;+ playing: boolean;+ style?: CSSProperties;+ onError: (message: string) => void;+}) {+ const canvas = useRef<HTMLCanvasElement>(null);+ const settings = useRef(effects);+ const errorHandler = useRef(onError);+ const redraw = useRef<(() => void) | null>(null);+ settings.current = effects;+ errorHandler.current = onError;+ useEffect(() => {+ const media = source.current;+ if (!canvas.current || !media) return;+ let live = true, failed = false, animation = 0, videoFrame = 0;+ let renderer: Awaited<ReturnType<typeof createVideoEffectsPreview>> | undefined;+ const video = media instanceof HTMLVideoElement ? media : null;+ const draw = () => {+ if (!live || failed) return;+ try { renderer?.draw(media, settings.current); }+ catch (reason) { failed = true; errorHandler.current(String(reason)); }+ };+ const schedule = () => {+ if (!live || failed || !playing) return;+ if (video?.requestVideoFrameCallback) videoFrame = video.requestVideoFrameCallback(() => { draw(); schedule(); });+ else animation = requestAnimationFrame(() => { draw(); schedule(); });+ };+ redraw.current = draw;+ const events = ["loadeddata", "seeked", "load"];+ events.forEach((event) => media.addEventListener(event, draw));+ void createVideoEffectsPreview(canvas.current).then((created) => {+ if (!live) { created.dispose(); return; }+ renderer = created;+ draw(); schedule();+ }, (reason) => { if (live) errorHandler.current(String(reason)); });+ return () => {+ live = false;+ cancelAnimationFrame(animation);+ if (videoFrame) video?.cancelVideoFrameCallback(videoFrame);+ redraw.current = null;+ events.forEach((event) => media.removeEventListener(event, draw));+ renderer?.dispose();+ };+ }, [source, sourceUrl, playing]);+ useEffect(() => redraw.current?.(), [effects]);+ return <canvas ref={canvas} className="program-keyed-picture" style={style} aria-label="Video effects preview" />;+}diff --git a/src/lib/effectSettings.test.ts b/src/lib/effectSettings.test.tsindex ebc90dc..9862e62 100644--- a/src/lib/effectSettings.test.ts+++ b/src/lib/effectSettings.test.ts@@ -23,11 +23,15 @@ describe("3D cube import", () => {it("accepts Resolve input ranges", () => {expect(parseCube(identityCube.replace("LUT_3D_SIZE 2", "LUT_3D_SIZE 2\nLUT_3D_INPUT_RANGE 0 2"), "test.cube").domainMax).toEqual([2, 2, 2]);});+ it("keeps hash characters inside quoted titles", () => {+ expect(parseCube(identityCube.replace('"Identity"', '"Look #2" # comment'), "test.cube").name).toBe("Look #2");+ });it.each([identityCube.replace("LUT_3D_SIZE 2", "LUT_3D_SIZE 66"),identityCube.replace("LUT_3D_SIZE 2", "LUT_1D_SIZE 2"),identityCube.replace("LUT_3D_SIZE 2", "LUT_3D_SIZE 2\nDOMAIN_MAX 0 1 1"),identityCube.replace("LUT_3D_SIZE 2", "LUT_3D_SIZE 2\nLUT_3D_SIZE 2"),+ identityCube.replace("LUT_3D_SIZE 2", "LUT_3D_SIZE 2\nDOMAIN_MIN 1 0 0\nDOMAIN_MAX 1.000000000001 1 1"),identityCube.replace("1 1 1", "1 NaN 1"),identityCube.replace("1 1 1", ""),identityCube + "\n1 1 1",diff --git a/src/lib/effectSettings.ts b/src/lib/effectSettings.tsindex e26108b..dbfb508 100644--- a/src/lib/effectSettings.ts+++ b/src/lib/effectSettings.ts@@ -1,6 +1,7 @@import { z } from "zod";-const triple = z.tuple([z.number().finite(), z.number().finite(), z.number().finite()]);+const domainValue = z.number().finite().min(-65504).max(65504);+const triple = z.tuple([domainValue, domainValue, domainValue]);export const lutTableSchema = z.object({name: z.string().min(1).max(256),size: z.number().int().min(2).max(65),@@ -9,7 +10,7 @@ export const lutTableSchema = z.object({values: z.array(z.number().finite().min(-65504).max(65504)).max(65 ** 3 * 3),}).superRefine((table, context) => {if (table.values.length !== table.size ** 3 * 3) context.addIssue({ code: "custom", message: "LUT row count does not match its size." });- if (table.domainMin.some((min, i) => min >= table.domainMax[i])) context.addIssue({ code: "custom", message: "LUT domain maximum must exceed minimum." });+ if (table.domainMin.some((min, i) => Math.fround(min) >= Math.fround(table.domainMax[i]))) context.addIssue({ code: "custom", message: "LUT domain maximum must exceed minimum at GPU precision." });});export const effectSchemas = {@@ -41,7 +42,7 @@ export function parseCube(text: string, filename: string): LutTable {const values: number[] = [];const seen = new Set<string>();for (const [index, raw] of text.replace(/^\uFEFF/, "").split(/\r?\n/).entries()) {- const line = raw.replace(/#.*$/, "").trim();+ const line = (raw.match(/^(?:"[^"]*"|[^"#])*/)?.[0] ?? "").trim();if (!line) continue;const [tag, ...args] = line.split(/\s+/);const fail = (message: string): never => { throw new Error(`Line ${index + 1}: ${message}`); };diff --git a/src/lib/exportPipeline.test.ts b/src/lib/exportPipeline.test.tsindex 9101c16..3989905 100644--- a/src/lib/exportPipeline.test.ts+++ b/src/lib/exportPipeline.test.ts@@ -611,6 +611,23 @@ function installWebCodecs(options: HarnessOptions = {}): { restore: () => void;}describe("running an export of external media", () => {+ it("refuses to silently omit GPU effects when only canvas compositing is available", async () => {+ const { restore, recorded } = installWebCodecs({ decodes: true, audio: false });+ try {+ invoked.mockResolvedValue(await sampleFile(5, 30));+ const config = externalProject();+ const video = config.timeline.tracks.find((track) => track.kind === "video" && track.clips.length > 0)!;+ video.clips[0].sharpen = { amount: 50 };+ const settings = defaultExportSettings(config);+ await expect(runExport({+ folderPath: FOLDER, config, settings, plan: buildExportPlan(config, settings),+ bitrate: 5_000_000, onProgress: () => {}, cancelled: () => false,+ })).rejects.toThrow(/effects.*require WebGPU/);+ expect(recorded.videoChunks).toBe(0);+ expect(callLog().some((call) => call.cmd.includes("write"))).toBe(false);+ } finally { restore(); }+ });+it("dispatches the external read, not a project-relative one", async () => {const { restore } = installWebCodecs();try {diff --git a/src/lib/exportPipeline.ts b/src/lib/exportPipeline.tsindex c74cc0f..771c4c1 100644--- a/src/lib/exportPipeline.ts+++ b/src/lib/exportPipeline.ts@@ -35,6 +35,8 @@ import {} from "./export";import type { ProjectAsset, ProjectConfig } from "./project";import { applyChromaKey, keyColor, KEY_FEATHER, RGB_DISTANCE_SCALE } from "./chromaKey";+import { hasVideoEffects } from "./effectSettings";+import { createVideoEffectsProcessor } from "./videoEffectsGpu";/* ---------------------------------------------------------------------------What this machine can do@@ -230,11 +232,11 @@ async function createWebGpuCompositor(width: number, height: number, background:const format = navigator.gpu.getPreferredCanvasFormat();context.configure({ device, format, alphaMode: "opaque" });const shader = device.createShaderModule({ code: COMPOSITOR_SHADER });- const pipeline = device.createRenderPipeline({+ const makePipeline = (module: GPUShaderModule) => device.createRenderPipeline({layout: "auto",- vertex: { module: shader, entryPoint: "vs" },+ vertex: { module, entryPoint: "vs" },fragment: {- module: shader,+ module,entryPoint: "fs",targets: [{format,@@ -246,6 +248,12 @@ async function createWebGpuCompositor(width: number, height: number, background:},primitive: { topology: "triangle-list" },});+ const pipeline = makePipeline(shader);+ const processedPipeline = makePipeline(device.createShaderModule({ code: COMPOSITOR_SHADER+ .replace("var source: texture_external;", "var source: texture_2d<f32>;")+ .replace("let sampled = textureSampleBaseClampToEdge(source, source_sampler, uv);",+ "let premultiplied = textureSampleLevel(source, source_sampler, uv, 0.); let sampled = vec4f(premultiplied.rgb / max(premultiplied.a, .00001), premultiplied.a);") }));+ let effectsProcessor: ReturnType<typeof createVideoEffectsProcessor> | undefined;const uniform = device.createBuffer({ size: 16, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });const styleUniform = device.createBuffer({ size: 48, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST });const sampler = device.createSampler({ magFilter: "linear", minFilter: "linear" });@@ -265,14 +273,14 @@ async function createWebGpuCompositor(width: number, height: number, background:texture as written: the project's own background colour, unconverted. */const clearValue = { r: r / 255, g: g / 255, b: b / 255, a: 1 };- const pass = (view: GPUTextureView, bindGroup: GPUBindGroup | null) => {+ const pass = (view: GPUTextureView, bindGroup: GPUBindGroup | null, renderPipeline = pipeline) => {if (lost.reason) throw new Error(`This computer's GPU stopped responding during the export: ${lost.reason}`);const encoder = device.createCommandEncoder();const renderPass = encoder.beginRenderPass({colorAttachments: [{ view, clearValue, loadOp: bindGroup ? "load" : "clear", storeOp: "store" }],});if (bindGroup) {- renderPass.setPipeline(pipeline);+ renderPass.setPipeline(renderPipeline);renderPass.setBindGroup(0, bindGroup);renderPass.draw(6);}@@ -286,6 +294,8 @@ async function createWebGpuCompositor(width: number, height: number, background:pass(context.getCurrentTexture().createView(), null);},draw(frame, style) {+ const processed = hasVideoEffects(style);+ const renderPipeline = processed ? processedPipeline : pipeline;const fitted = fitRect(frame.displayWidth, frame.displayHeight, width, height);const scale = style.transform.scale / 100;const box = {@@ -302,19 +312,21 @@ async function createWebGpuCompositor(width: number, height: number, background:device.queue.writeBuffer(styleUniform, 0, new Float32Array([(style.transform.rotation * Math.PI) / 180,style.opacity,- style.look.temperature / 100,+ processed ? 0 : style.look.temperature / 100,style.revealStart,style.revealEnd,- style.chromaKey ? 1 : 0,+ style.chromaKey && !processed ? 1 : 0,0,0,...(style.chromaKey ? keyColor(style.chromaKey.color) : [0, 0, 0]),(style.chromaKey?.tolerance ?? 0) / 100,]));// Zero copy: the decoded frame is sampled where it already lives.- const external = device.importExternalTexture({ source: frame });+ const external = processed+ ? (effectsProcessor ??= createVideoEffectsProcessor(device)).render(frame, style).createView()+ : device.importExternalTexture({ source: frame });const bindGroup = device.createBindGroup({- layout: pipeline.getBindGroupLayout(0),+ layout: renderPipeline.getBindGroupLayout(0),entries: [{ binding: 0, resource: { buffer: uniform } },{ binding: 1, resource: { buffer: styleUniform } },@@ -322,12 +334,13 @@ async function createWebGpuCompositor(width: number, height: number, background:{ binding: 3, resource: sampler },],});- pass(context.getCurrentTexture().createView(), bindGroup);+ pass(context.getCurrentTexture().createView(), bindGroup, renderPipeline);},take(timestamp, duration) {return new VideoFrame(canvas, { timestamp, duration, alpha: "discard" });},dispose() {+ effectsProcessor?.dispose();device.destroy();},};@@ -348,6 +361,7 @@ function createCanvasCompositor(width: number, height: number, background: strinclear: paintBackground,draw(frame, style) {let picture: VideoFrame | OffscreenCanvas = frame;+ if (hasVideoEffects(style)) throw new Error("This clip uses video effects that require WebGPU. A GPU is unavailable; effects cannot be included in this export.");if (style.chromaKey) {keyCanvas ??= new OffscreenCanvas(frame.displayWidth, frame.displayHeight);if (keyCanvas.width !== frame.displayWidth) keyCanvas.width = frame.displayWidth;diff --git a/src/lib/videoEffectsGpu.ts b/src/lib/videoEffectsGpu.tsnew file mode 100644index 0000000..a7fcc01--- /dev/null+++ b/src/lib/videoEffectsGpu.ts@@ -0,0 +1,242 @@+/// <reference types="@webgpu/types" />+import { keyColor, KEY_FEATHER, RGB_DISTANCE_SCALE } from "./chromaKey";+import type { VideoEffects, LutTable } from "./effectSettings";+import type { ClipChromaKey } from "./project";++export type GpuEffects = VideoEffects & { chromaKey?: ClipChromaKey | null; look?: { temperature: number } | null };++const VERTEX = /* wgsl */ `+struct VertexOut { @builtin(position) position: vec4f, @location(0) uv: vec2f };+@vertex fn vs(@builtin(vertex_index) index: u32) -> VertexOut {+ let corners = array<vec2f, 3>(vec2f(-1., -1.), vec2f(3., -1.), vec2f(-1., 3.));+ let p = corners[index];+ return VertexOut(vec4f(p, 0., 1.), vec2f(p.x * .5 + .5, .5 - p.y * .5));+}+`;+const PARAMS = /* wgsl */ `+struct Params {+ key: vec4f,+ grade: vec4f,+ detail: vec4f,+ domainMin: vec4f,+ domainMax: vec4f,+ blur: vec4f,+};+@group(0) @binding(0) var<uniform> params: Params;+@group(0) @binding(2) var linearSampler: sampler;+`;+const IMPORT = VERTEX + PARAMS + /* wgsl */ `+@group(0) @binding(1) var picture: texture_external;+@fragment fn fs(@location(0) uv: vec2f) -> @location(0) vec4f {+ let color = textureSampleBaseClampToEdge(picture, linearSampler, uv);+ var alpha = color.a;+ if (params.detail.w > 0.) {+ let difference = distance(color.rgb, params.key.rgb) / ${RGB_DISTANCE_SCALE};+ alpha *= smoothstep(params.key.a, params.key.a + ${KEY_FEATHER}, difference);+ }+ // Premultiply before filtering so removed key colors cannot bleed into edges.+ return vec4f(color.rgb * alpha, alpha);+}+`;+const TEXTURE = "@group(0) @binding(1) var picture: texture_2d<f32>;";+const BLUR = VERTEX + PARAMS + TEXTURE + /* wgsl */ `+@fragment fn fs(@location(0) uv: vec2f) -> @location(0) vec4f {+ let radius = params.blur.x;+ let sigma = max(radius / 3., .333333);+ let step = params.blur.yz / vec2f(textureDimensions(picture));+ var total = vec4f(0.);+ var weights = 0.;+ for (var i = -24; i <= 24; i++) {+ if (abs(f32(i)) > ceil(radius)) { continue; }+ let weight = exp(-f32(i * i) / (2. * sigma * sigma));+ total += textureSampleLevel(picture, linearSampler, uv + f32(i) * step, 0.) * weight;+ weights += weight;+ }+ return total / weights;+}+`;+const FINISH = VERTEX + PARAMS + TEXTURE + /* wgsl */ `+@group(0) @binding(3) var<storage, read> lut: array<f32>;+fn lookup(p: vec3u) -> vec3f {+ let size = u32(params.detail.y);+ let offset = (p.x + size * (p.y + size * p.z)) * 3u;+ return vec3f(lut[offset], lut[offset + 1u], lut[offset + 2u]);+}+fn applyLut(color: vec3f) -> vec3f {+ let size = params.detail.y;+ let position = clamp((color - params.domainMin.xyz) / (params.domainMax.xyz - params.domainMin.xyz), vec3f(0.), vec3f(1.)) * (size - 1.);+ let lo = vec3u(floor(position));+ let hi = min(lo + vec3u(1u), vec3u(u32(size) - 1u));+ let f = fract(position);+ return mix(+ mix(mix(lookup(lo), lookup(vec3u(hi.x, lo.y, lo.z)), f.x),+ mix(lookup(vec3u(lo.x, hi.y, lo.z)), lookup(vec3u(hi.xy, lo.z)), f.x), f.y),+ mix(mix(lookup(vec3u(lo.xy, hi.z)), lookup(vec3u(hi.x, lo.y, hi.z)), f.x),+ mix(lookup(vec3u(lo.x, hi.yz)), lookup(hi), f.x), f.y), f.z);+}+fn straight(color: vec4f) -> vec3f { return color.rgb / max(color.a, .00001); }+@fragment fn fs(@location(0) uv: vec2f) -> @location(0) vec4f {+ let sampled = textureSampleLevel(picture, linearSampler, uv, 0.);+ var rgb = straight(sampled);+ if (params.detail.x > 0.) {+ let pixel = 1. / vec2f(textureDimensions(picture));+ let neighbors = (+ textureSampleLevel(picture, linearSampler, uv + vec2f(pixel.x, 0.), 0.) ++ textureSampleLevel(picture, linearSampler, uv - vec2f(pixel.x, 0.), 0.) ++ textureSampleLevel(picture, linearSampler, uv + vec2f(0., pixel.y), 0.) ++ textureSampleLevel(picture, linearSampler, uv - vec2f(0., pixel.y), 0.)) * .25;+ rgb += (rgb - straight(neighbors)) * params.detail.x;+ }+ rgb *= exp2(params.grade.x);+ rgb = (rgb - vec3f(.5)) * params.grade.y + vec3f(.5);+ let luma = dot(rgb, vec3f(.2126, .7152, .0722));+ rgb = mix(vec3f(luma), rgb, params.grade.z);+ if (params.detail.y >= 2. && params.detail.z > 0.) { rgb = mix(rgb, applyLut(rgb), params.detail.z); }+ let distanceFromCenter = length((uv - vec2f(.5)) * 1.41421356);+ rgb *= 1. - params.grade.w * smoothstep(.3, 1., distanceFromCenter);+ rgb += vec3f(params.domainMin.w * .1, 0., -params.domainMin.w * .1);+ return vec4f(clamp(rgb, vec3f(0.), vec3f(1.)) * sampled.a, sampled.a);+}+`;+const BLIT = VERTEX + /* wgsl */ `+@group(0) @binding(0) var picture: texture_2d<f32>;+@group(0) @binding(1) var linearSampler: sampler;+@fragment fn fs(@location(0) uv: vec2f) -> @location(0) vec4f {+ return textureSampleLevel(picture, linearSampler, uv, 0.);+}+`;++function pipeline(device: GPUDevice, code: string, format: GPUTextureFormat): GPURenderPipeline {+ const module = device.createShaderModule({ code });+ return device.createRenderPipeline({+ layout: "auto", vertex: { module, entryPoint: "vs" },+ fragment: { module, entryPoint: "fs", targets: [{ format }] },+ primitive: { topology: "triangle-list" },+ });+}++/** Shared by monitor and export. All pictures stay in GPU textures; output is premultiplied. */+export function createVideoEffectsProcessor(device: GPUDevice) {+ const importPipeline = pipeline(device, IMPORT, "rgba16float");+ const blurPipeline = pipeline(device, BLUR, "rgba16float");+ const finishPipeline = pipeline(device, FINISH, "rgba16float");+ const sampler = device.createSampler({ minFilter: "linear", magFilter: "linear" });+ const uniforms = Array.from({ length: 3 }, () => device.createBuffer({ size: 96, usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST }));+ const emptyLut = device.createBuffer({ size: 12, usage: GPUBufferUsage.STORAGE });+ const luts: { table: LutTable; buffer: GPUBuffer }[] = [];+ let textures: GPUTexture[] = [];+ let dimensions = "";+ return {+ render(frame: VideoFrame, effects: GpuEffects): GPUTexture {+ const width = frame.displayWidth, height = frame.displayHeight;+ if (dimensions !== `${width}x${height}`) {+ textures.forEach((texture) => texture.destroy());+ textures = Array.from({ length: 3 }, () => device.createTexture({+ size: [width, height], format: "rgba16float", usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,+ }));+ dimensions = `${width}x${height}`;+ }+ const table = effects.lut?.table;+ let lutBuffer = emptyLut;+ if (table) {+ const cached = luts.find((entry) => entry.table === table);+ if (cached) lutBuffer = cached.buffer;+ else {+ lutBuffer = device.createBuffer({ size: table.values.length * 4, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST });+ device.queue.writeBuffer(lutBuffer, 0, new Float32Array(table.values));+ luts.push({ table, buffer: lutBuffer });+ if (luts.length > 8) luts.shift()!.buffer.destroy();+ }+ }+ const grade = effects.colorCorrection;+ const data = new Float32Array([+ ...(effects.chromaKey ? keyColor(effects.chromaKey.color) : [0, 0, 0]), (effects.chromaKey?.tolerance ?? 0) / 100,+ grade?.exposure ?? 0, 1 + (grade?.contrast ?? 0) / 100, (grade?.saturation ?? 100) / 100, (effects.vignette?.amount ?? 0) / 100,+ (effects.sharpen?.amount ?? 0) / 100, table?.size ?? 0, (effects.lut?.intensity ?? 0) / 100, effects.chromaKey ? 1 : 0,+ ...(table?.domainMin ?? [0, 0, 0]), (effects.look?.temperature ?? 0) / 100, ...(table?.domainMax ?? [1, 1, 1]), 0,+ effects.blur?.radius ?? 0, 0, 0, 0,+ ]);+ device.queue.writeBuffer(uniforms[0], 0, data);+ const encoder = device.createCommandEncoder();+ const pass = (renderPipeline: GPURenderPipeline, source: GPUExternalTexture | GPUTextureView, target: GPUTexture, uniform: GPUBuffer, lut?: GPUBuffer) => {+ const entries: GPUBindGroupEntry[] = [+ { binding: 0, resource: { buffer: uniform } }, { binding: 1, resource: source }, { binding: 2, resource: sampler },+ ];+ if (lut) entries.push({ binding: 3, resource: { buffer: lut } });+ const group = device.createBindGroup({ layout: renderPipeline.getBindGroupLayout(0), entries });+ const render = encoder.beginRenderPass({ colorAttachments: [{ view: target.createView(), loadOp: "clear", storeOp: "store", clearValue: [0, 0, 0, 0] }] });+ render.setPipeline(renderPipeline); render.setBindGroup(0, group); render.draw(3); render.end();+ };+ pass(importPipeline, device.importExternalTexture({ source: frame }), textures[0], uniforms[0]);+ if ((effects.blur?.radius ?? 0) > 0) {+ data[21] = 1;+ device.queue.writeBuffer(uniforms[1], 0, data);+ pass(blurPipeline, textures[0].createView(), textures[1], uniforms[1]);+ data[21] = 0; data[22] = 1;+ device.queue.writeBuffer(uniforms[2], 0, data);+ pass(blurPipeline, textures[1].createView(), textures[0], uniforms[2]);+ }+ pass(finishPipeline, textures[0].createView(), textures[2], uniforms[0], lutBuffer);+ device.queue.submit([encoder.finish()]);+ return textures[2];+ },+ dispose() {+ textures.forEach((texture) => texture.destroy());+ uniforms.forEach((buffer) => buffer.destroy());+ luts.forEach(({ buffer }) => buffer.destroy());+ emptyLut.destroy();+ },+ };+}++// Share a device across monitor layers; each renderer owns its textures.+let previewDevice: Promise<GPUDevice> | undefined;+async function getPreviewDevice(): Promise<GPUDevice> {+ previewDevice ??= (async () => {+ if (!navigator.gpu) throw new Error("Video effects require WebGPU on this computer.");+ const adapter = await navigator.gpu.requestAdapter();+ if (!adapter) throw new Error("No GPU is available for video effects.");+ const device = await adapter.requestDevice();+ void device.lost.then(() => { previewDevice = undefined; });+ return device;+ })().catch((reason) => { previewDevice = undefined; throw reason; });+ return previewDevice;+}++export async function createVideoEffectsPreview(canvas: HTMLCanvasElement) {+ const device = await getPreviewDevice();+ const context = canvas.getContext("webgpu");+ if (!context) throw new Error("Could not create the video effects preview.");+ const format = navigator.gpu.getPreferredCanvasFormat();+ context.configure({ device, format, alphaMode: "premultiplied" });+ const processor = createVideoEffectsProcessor(device);+ const blit = pipeline(device, BLIT, format);+ const sampler = device.createSampler({ minFilter: "linear", magFilter: "linear" });+ let failure: string | null = null;+ const onError = (event: Event) => { failure = (event as GPUUncapturedErrorEvent).error.message; };+ device.addEventListener("uncapturederror", onError);+ void device.lost.then((info) => { failure = info.message || "The GPU stopped responding."; });+ return {+ draw(source: HTMLVideoElement | HTMLImageElement, effects: GpuEffects) {+ if (failure) throw new Error(failure);+ const video = source instanceof HTMLVideoElement;+ const width = video ? source.videoWidth : source.naturalWidth;+ const height = video ? source.videoHeight : source.naturalHeight;+ if (!width || !height || (video ? source.readyState < 2 : !source.complete)) return;+ if (canvas.width !== width) canvas.width = width;+ if (canvas.height !== height) canvas.height = height;+ const frame = new VideoFrame(source, { timestamp: 0 });+ try {+ const result = processor.render(frame, effects);+ const group = device.createBindGroup({ layout: blit.getBindGroupLayout(0), entries: [+ { binding: 0, resource: result.createView() }, { binding: 1, resource: sampler },+ ] });+ const encoder = device.createCommandEncoder();+ const render = encoder.beginRenderPass({ colorAttachments: [{ view: context.getCurrentTexture().createView(), loadOp: "clear", storeOp: "store", clearValue: [0, 0, 0, 0] }] });+ render.setPipeline(blit); render.setBindGroup(0, group); render.draw(3); render.end();+ device.queue.submit([encoder.finish()]);+ } finally { frame.close(); }+ },+ dispose() { device.removeEventListener("uncapturederror", onError); processor.dispose(); context.unconfigure(); },+ };+}