Skip to content

Commit e8ffaa4

Browse files
fix(core): preview recovers its WebGL scenes and effects after a lost context (#7028)
* fix(core): preview repaints and rebuilds effects after the GPU restores a lost WebGL context * fix(core): a paused scene redraws and a playing one repaints effects after a context restore * test(core): a paused scene is redrawn after a WebGL context restore * test(core): give the restore redraw test a clip * fix(core): shorten the context recovery header comment * fix(core): rebuild effects only after every lost context has restored * test(core): the restore rebuild waits while another context is still lost * test(core): count context requests to show the rebuild waits * test(core): the mock reports the lost flag the test sets
1 parent 4c1c178 commit e8ffaa4

6 files changed

Lines changed: 148 additions & 9 deletions

File tree

‎packages/core/src/runtime/entry.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,32 @@ describe("runtime entry", () => {
389389
expect(visibility(current, later)).toEqual(["hidden", "visible"]);
390390
});
391391

392+
it("redraws a paused scene by stepping its timeline off the current time and back after a WebGL restore", async () => {
393+
const root = mountRoot();
394+
timed(root, "div", "0");
395+
const timeline = window.__timelines.main!;
396+
await evaluateRuntime();
397+
window.__player!.seek(2);
398+
const seen: number[] = [];
399+
const totalTime = timeline.totalTime!;
400+
timeline.totalTime = (t?: number) => {
401+
if (t !== undefined) seen.push(t);
402+
return totalTime(t as number);
403+
};
404+
const frames: FrameRequestCallback[] = [];
405+
vi.spyOn(window, "requestAnimationFrame").mockImplementation((cb) => frames.push(cb));
406+
407+
const scene = root.appendChild(document.createElement("canvas"));
408+
scene.dispatchEvent(new Event("webglcontextlost", { cancelable: true }));
409+
scene.dispatchEvent(new Event("webglcontextrestored"));
410+
seen.length = 0;
411+
frames.splice(0).forEach((cb) => cb(0));
412+
413+
expect(seen[0]).toBeCloseTo(2 - 1e-4, 6);
414+
expect(seen.at(-1)).toBeCloseTo(2, 3);
415+
vi.restoreAllMocks();
416+
});
417+
392418
it("leaves nothing hidden when the runtime is evaluated a second time", async () => {
393419
const root = mountRoot();
394420
const current = timed(root, "div", "0");

‎packages/core/src/runtime/init.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ import { stillStandsIn, unheldPoster, unproxiedSrc } from "./proxySrc";
165165
import { applyVariableBindings, unproxiedMediaSrc } from "./applyVariableBindings";
166166
import { createColorGradingRuntime, type RuntimeColorGradingApi } from "./colorGrading";
167167
import { COLOR_GRADING_AUTHORED_OPACITY_ATTR } from "../colorGrading";
168-
import { initVfx, paintVfx } from "./vfx";
168+
import { initVfx, paintVfx, rebuildLostVfx } from "./vfx";
169+
import { watchSceneWebglContexts } from "./webglContextRecovery";
169170
import {
170171
captureMotionBlurBusy,
171172
initCaptureMotionBlur,
@@ -4778,6 +4779,22 @@ export function initSandboxRuntimeModular(): void {
47784779
// Per-pixel effect chains: compile once here, repaint on every seek below.
47794780
initVfx(document.body, state.canonicalFps);
47804781
initMotionBlur();
4782+
registerRuntimeCleanup(
4783+
watchSceneWebglContexts(() => {
4784+
rebuildLostVfx(document.body);
4785+
// Effects paint on seeks only. A playing transport redraws the scene itself on its next tick.
4786+
if (clock.isPlaying()) {
4787+
paintVfx(state.currentTime);
4788+
return;
4789+
}
4790+
// Seeking to the time a timeline is already at calls no onUpdate, so step off it first.
4791+
const tl = state.capturedTimeline;
4792+
const at = state.currentTime;
4793+
if (tl && typeof tl.totalTime === "function")
4794+
tl.totalTime(at > 1e-3 ? at - 1e-4 : at + 1e-4, false);
4795+
applySeek(at);
4796+
}),
4797+
);
47814798

47824799
const applyPlaybackRate = (nextRate: number) => {
47834800
const parsed = Number(nextRate);

‎packages/core/src/runtime/vfx.test.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22
import type { HfVfxCapture } from "../vfx";
33
import { resetSeekDispatchState, waitForSeekCompletion } from "./adapters/seek-dispatch";
4-
import { initVfx, paintVfx } from "./vfx";
4+
import { initVfx, paintVfx, rebuildLostVfx } from "./vfx";
55

66
/**
77
* No def has `capture !== "none"` until Task 2.2 registers `wave-warp`, so the
@@ -52,6 +52,7 @@ interface MockGl {
5252
usedPrograms: unknown[];
5353
shaderSources: string[];
5454
viewports: number[][];
55+
lost: boolean;
5556
}
5657

5758
/**
@@ -67,6 +68,7 @@ function createMockGl(): WebGL2RenderingContext & MockGl {
6768
usedPrograms: [],
6869
shaderSources: [],
6970
viewports: [],
71+
lost: false,
7072
};
7173
let currentProgram: unknown = null;
7274
const gl = {
@@ -97,6 +99,7 @@ function createMockGl(): WebGL2RenderingContext & MockGl {
9799
getShaderParameter: () => true,
98100
getShaderInfoLog: () => "",
99101
deleteShader: () => {},
102+
isContextLost: () => (gl as unknown as MockGl).lost,
100103
createProgram: () => ({ id: ++state.programCount }),
101104
attachShader: () => {},
102105
linkProgram: () => {},
@@ -407,6 +410,30 @@ describe("vfx runtime", () => {
407410
});
408411
});
409412

413+
it("paints a host again after its lost context is restored and the chains are rebuilt", () => {
414+
const host = makeHost(ONE_NODE);
415+
initVfx(document.body, 30);
416+
const out = host.querySelector("canvas.hf-vfx-out") as HTMLCanvasElement;
417+
out.dispatchEvent(new Event("webglcontextlost", { cancelable: true }));
418+
out.dispatchEvent(new Event("webglcontextrestored"));
419+
paintVfx(0);
420+
expect(gl!.calls).toEqual([]);
421+
422+
// Another context has not come back yet: rebuilding now would drop its host.
423+
const contexts = vi.mocked(HTMLCanvasElement.prototype.getContext);
424+
const asked = contexts.mock.calls.length;
425+
gl!.lost = true;
426+
rebuildLostVfx(document.body);
427+
expect(contexts.mock.calls.length).toBe(asked);
428+
429+
gl!.lost = false;
430+
rebuildLostVfx(document.body);
431+
expect(contexts.mock.calls.length).toBeGreaterThan(asked);
432+
gl!.calls.length = 0;
433+
paintVfx(0);
434+
expect(gl!.calls.length).toBeGreaterThan(0);
435+
});
436+
410437
it("forgets the previous composition's hosts when re-initialised", () => {
411438
makeHost(ONE_NODE);
412439
initVfx(document.body, 30);

‎packages/core/src/runtime/vfx.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,13 +552,8 @@ function resolveCaptureSource(
552552
* Only the OUTPUT canvas is watched: `.hf-vfx-src` is a 2-D context and is
553553
* unaffected by GL context loss.
554554
*
555-
* Restore is deliberately unhandled — the entry stays lost for as long as this
556-
* registry does. `initVfx` already re-scans and re-registers every host when
557-
* the composition mounts and again when a sub-composition arrives, rebuilding
558-
* every program, texture and framebuffer from scratch; a second, narrower
559-
* rebuild path for `webglcontextrestored` alone would duplicate that one and
560-
* be free to drift from it. `preventDefault()` still runs, so the context is
561-
* restorable if a follow-up wants to take it.
555+
* A lost entry stays lost for as long as its registry does; `rebuildLostVfx`
556+
* re-runs `initVfx` once the context is restored, the one rebuild path.
562557
*/
563558
function watchContextLoss(entry: VfxEntry): void {
564559
entry.out.addEventListener("webglcontextlost", (event) => {
@@ -572,6 +567,12 @@ function watchContextLoss(entry: VfxEntry): void {
572567
});
573568
}
574569

570+
/** Rebuilds every chain once none is still waiting on its context: a rebuild on a lost one drops the host for good. */
571+
export function rebuildLostVfx(root: HTMLElement): void {
572+
if (!registry.some((entry) => entry.contextLost)) return;
573+
if (registry.every((entry) => !entry.gl.isContextLost())) initVfx(root, registryFps);
574+
}
575+
575576
/** The host's chain, or `null` once the reason it is unusable has been said. */
576577
function readVfxChain(host: HTMLElement): HfVfxChain | null {
577578
try {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { afterEach, describe, expect, it, vi } from "vitest";
2+
import { watchSceneWebglContexts } from "./webglContextRecovery";
3+
4+
function canvas() {
5+
const el = document.createElement("canvas");
6+
document.body.appendChild(el);
7+
return el;
8+
}
9+
10+
describe("watchSceneWebglContexts", () => {
11+
afterEach(() => {
12+
vi.restoreAllMocks();
13+
document.body.innerHTML = "";
14+
});
15+
16+
it("lets a scene canvas restore, then repaints once after its own restore handler", () => {
17+
const frames: FrameRequestCallback[] = [];
18+
vi.spyOn(window, "requestAnimationFrame").mockImplementation((cb) => frames.push(cb));
19+
const order: string[] = [];
20+
const repaint = vi.fn(() => order.push("repaint"));
21+
const stop = watchSceneWebglContexts(repaint);
22+
const scene = canvas();
23+
scene.addEventListener("webglcontextrestored", () => order.push("scene"));
24+
25+
const lost = new Event("webglcontextlost", { cancelable: true });
26+
scene.dispatchEvent(lost);
27+
expect(lost.defaultPrevented).toBe(true);
28+
29+
scene.dispatchEvent(new Event("webglcontextrestored"));
30+
canvas().dispatchEvent(new Event("webglcontextrestored"));
31+
expect(repaint).not.toHaveBeenCalled();
32+
frames.splice(0).forEach((cb) => cb(0));
33+
expect(order).toEqual(["scene", "repaint"]);
34+
stop();
35+
});
36+
37+
it("does nothing after it is stopped", () => {
38+
const raf = vi.spyOn(window, "requestAnimationFrame");
39+
const repaint = vi.fn();
40+
watchSceneWebglContexts(repaint)();
41+
const lost = new Event("webglcontextlost", { cancelable: true });
42+
canvas().dispatchEvent(lost);
43+
canvas().dispatchEvent(new Event("webglcontextrestored"));
44+
expect(lost.defaultPrevented).toBe(false);
45+
expect(raf).not.toHaveBeenCalled();
46+
});
47+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function watchSceneWebglContexts(onRestored: () => void): () => void {
2+
let frame: number | null = null;
3+
// Without preventDefault the browser never restores the context.
4+
const onLost = (event: Event) => event.preventDefault();
5+
const onRestore = () => {
6+
if (frame !== null) return;
7+
frame = requestAnimationFrame(() => {
8+
frame = null;
9+
onRestored();
10+
});
11+
};
12+
// These events do not bubble, so only a capture listener sees every canvas.
13+
document.addEventListener("webglcontextlost", onLost, true);
14+
document.addEventListener("webglcontextrestored", onRestore, true);
15+
return () => {
16+
document.removeEventListener("webglcontextlost", onLost, true);
17+
document.removeEventListener("webglcontextrestored", onRestore, true);
18+
if (frame !== null) cancelAnimationFrame(frame);
19+
frame = null;
20+
};
21+
}

0 commit comments

Comments
 (0)