M13.30: Content-Aware Fill in a job worker on its GPU
contentFill.generate sends its plan (FillPlan) to a browser job worker: the layer's frames render in passes on the worker's WebGPU device, the PNG sequence goes back to browser storage (Services::store_file), and the page adds the fill layer when the plan returns (WorkerReply::Fill). The smoke test checks it; docs updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
91f4a603a9
commit
3921e6c680
@@ -311,7 +311,8 @@ function spawn(base) {
|
||||
}
|
||||
|
||||
/// Run a job in a worker. `files`: [[path, Uint8Array]] the job reads; `onMessage(type, json,
|
||||
/// path, bytes)` receives "reply" (json), "file" (path, bytes) and "error" (json = message).
|
||||
/// path, bytes)` receives "reply" (json), "file" (path, bytes: a rendered file to download),
|
||||
/// "store" (path, bytes: a file to keep in browser storage) and "error" (json = message).
|
||||
export function workerRun(id, json, files, base, onMessage) {
|
||||
const w = idle.pop() ?? spawn(base);
|
||||
running.set(id, w);
|
||||
@@ -326,6 +327,8 @@ export function workerRun(id, json, files, base, onMessage) {
|
||||
}
|
||||
} else if (m.type === "file") {
|
||||
onMessage("file", null, m.path, new Uint8Array(m.bytes));
|
||||
} else if (m.type === "store") {
|
||||
onMessage("store", null, m.path, new Uint8Array(m.bytes));
|
||||
} else if (m.type === "stats") {
|
||||
jobStats.push({ kind: m.kind, gpu: m.gpu, passes: m.passes, readbacks: m.readbacks, ms: m.ms });
|
||||
if (jobStats.length > 16) jobStats.shift();
|
||||
|
||||
@@ -126,6 +126,10 @@ impl Services for WebServices {
|
||||
}
|
||||
fn store_file(&self, path: &str, data: &[u8]) -> std::io::Result<()> {
|
||||
put(path, data.into());
|
||||
// A job worker's files (Content-Aware Fill) go to the page's storage.
|
||||
if crate::is_worker() {
|
||||
crate::worker::post_store(path, data);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,6 +88,12 @@ impl Offload for WorkerOffload {
|
||||
crate::files::add_output(&p, b.to_vec().into());
|
||||
}
|
||||
}
|
||||
"store" => {
|
||||
// A file the job keeps (Content-Aware Fill's sequence): browser storage.
|
||||
if let (Some(p), Ok(b)) = (path.as_string(), bytes.dyn_into::<js_sys::Uint8Array>()) {
|
||||
crate::files::put(&p, b.to_vec().into());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
inbox.push(WorkerReply::Failed { error: json.as_string().unwrap_or_else(|| "worker failed".into()) });
|
||||
inbox.push(WorkerReply::Done);
|
||||
@@ -135,6 +141,13 @@ fn obj(pairs: &[(&str, JsValue)]) -> JsValue {
|
||||
o.into()
|
||||
}
|
||||
|
||||
/// Send a file the job keeps to the page's storage (`Services::store_file` in a job worker).
|
||||
pub(crate) fn post_store(path: &str, data: &[u8]) {
|
||||
let bytes = js_sys::Uint8Array::from(data);
|
||||
let buf = bytes.buffer();
|
||||
post(&obj(&[("type", "store".into()), ("path", path.into()), ("bytes", buf.clone().into())]), Some(&js_sys::Array::of1(&buf)));
|
||||
}
|
||||
|
||||
/// Set up the worker's engine session (called once by `web/worker.js`).
|
||||
#[wasm_bindgen(js_name = workerInit)]
|
||||
pub fn worker_init() {
|
||||
|
||||
@@ -321,6 +321,8 @@ try {
|
||||
report.steps.gpuJobs = await js(`(async () => {
|
||||
let maxGapMs = 0, last = performance.now();
|
||||
const timer = setInterval(() => { const now = performance.now(); maxGapMs = Math.max(maxGapMs, now - last); last = now; }, 10);
|
||||
const gaps = {};
|
||||
const phase = (n) => { gaps[n] = maxGapMs; maxGapMs = 0; };
|
||||
await effectcraft.execute("layer.newSolid", {name: "GPU Job Solid", color: "#3366cc", width: 320, height: 180});
|
||||
await effectcraft.execute("effect.apply", {effect: "Fractal Noise"});
|
||||
await effectcraft.execute("prop.addKey", {path: "effects/#1/transform/offset", time: 0, value: [0.5, 0.5]});
|
||||
@@ -333,6 +335,7 @@ try {
|
||||
const t0 = performance.now();
|
||||
const r = await effectcraft.execute("renderQueue.render", {});
|
||||
const renderMs = performance.now() - t0;
|
||||
phase("render");
|
||||
// Applying Warp Stabilizer starts its analysis in a job worker (as in After Effects).
|
||||
await effectcraft.execute("effect.apply", {effect: "Warp Stabilizer"});
|
||||
const t1 = performance.now();
|
||||
@@ -342,10 +345,30 @@ try {
|
||||
warp = await effectcraft.execute("warp.status", {});
|
||||
} while (!warp.analyzed && performance.now() - t1 < 180000);
|
||||
const warpMs = performance.now() - t1;
|
||||
phase("warp");
|
||||
// Content-Aware Fill (a job worker too): its input is the layer's source, so the noise and
|
||||
// blur go into a precomp (rendered on the worker's GPU); cut a hole, fill half a second.
|
||||
// (Editing on the page — pre-composing, the mask — is not part of the measured jobs.)
|
||||
await effectcraft.execute("effect.remove", {effect: "Warp Stabilizer"});
|
||||
await effectcraft.execute("layer.precompose", {name: "GPU Fill Source", mode: "move", open: false});
|
||||
await effectcraft.execute("layer.addMask", {rect: [140, 70, 40, 40], mode: "subtract"});
|
||||
const wa = await effectcraft.execute("comp.info", {}).then((c) => c.workArea).catch(() => null);
|
||||
await effectcraft.execute("comp.workArea", {start: 0, end: 0.5});
|
||||
phase("prepareFill");
|
||||
const t2 = performance.now();
|
||||
const fill = await effectcraft.execute("contentFill.generate", {method: "edgeBlend", wait: true}).catch((e) => ({error: String(e)}));
|
||||
const fillMs = performance.now() - t2;
|
||||
const fillLayers = await effectcraft.execute("comp.info", {}).then((c) => c.layers.map((l) => l.name).filter((n) => n.startsWith("Fill ")));
|
||||
phase("fill");
|
||||
clearInterval(timer);
|
||||
maxGapMs = Math.max(gaps.render, gaps.warp, gaps.fill);
|
||||
// Back to the project as it was.
|
||||
const c = await effectcraft.execute("comp.info", {});
|
||||
const ids = c.layers.filter((l) => l.name === "GPU Fill Source" || l.name.startsWith("Fill ")).map((l) => l.id);
|
||||
await effectcraft.execute("edit.clear", {layers: ids});
|
||||
if (wa) await effectcraft.execute("comp.workArea", {start: wa[0], end: wa[1]});
|
||||
const workers = effectcraft.info().workers;
|
||||
for (let k = 0; k < 7; k++) await effectcraft.execute("edit.undo", {});
|
||||
return {status: r.items.map((i) => i.status), renderMs, warp, warpMs, workers, maxGapMs};
|
||||
return {status: r.items.map((i) => i.status), renderMs, warp, warpMs, fill, fillMs, fillLayers, workArea: wa, workers, maxGapMs, gaps};
|
||||
})()`);
|
||||
const gj = report.steps.gpuJobs;
|
||||
gj.download = await waitDownload((n) => n.startsWith("gpujob"));
|
||||
@@ -357,7 +380,10 @@ try {
|
||||
check(rj && rj.gpu && rj.readbacks >= 3 && rj.passes >= 6, `Render Queue frames on the job worker's GPU, in passes: ${JSON.stringify(rj)}`);
|
||||
check(gj.warp.analyzed, `Warp Stabilizer analysed in a job worker: ${JSON.stringify(gj.warp)}`);
|
||||
check(wj && wj.gpu && wj.readbacks > 0, `Warp Stabilizer input frames on the job worker's GPU: ${JSON.stringify(wj)}`);
|
||||
check(gj.maxGapMs < 400, `event loop blocked ${gj.maxGapMs} ms during the GPU jobs`);
|
||||
const fj = jobs.filter((j) => j.kind === "ContentFill").pop();
|
||||
check(!gj.fill.error && gj.fillLayers.length > 0, `Content-Aware Fill in a job worker: ${JSON.stringify(gj.fill)} ${JSON.stringify(gj.fillLayers)}`);
|
||||
check(fj && fj.gpu && fj.readbacks > 0, `Content-Aware Fill frames on the job worker's GPU: ${JSON.stringify(fj)}`);
|
||||
check(gj.maxGapMs < 400, `event loop blocked ${gj.maxGapMs} ms during the GPU jobs: ${JSON.stringify(gj.gaps)}`);
|
||||
} else {
|
||||
report.steps.gpuJobs = { note: "no WebGPU in workers here: job workers render on their CPU" };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user