Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(render): waiting for a frame is bounded by the device, not by a poll count

yhh0a9d845a9cf6
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 97da73b35..9444fbdd5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -214,6 +214,22 @@ published separately; it ships inside the editor.
### Fixed
+- **Waiting for a frame the GPU has not finished is bounded by the device, not by a
+ poll count.** The readback poll gave up after 240 turns of the event loop — about a
+ second — and reported the readback as FAILED. That is the right answer for a device
+ that died and the wrong one for a queue that has not drained, and the two are
+ indistinguishable from the outside: what the caller gets either way is no pixels.
+
+ Measured on the second backend, where the copy resolves on a later event-loop turn
+ rather than at request time: `point-shadow` stepped 32 frames hands its frame over
+ after 1.4 seconds, and 64 frames after 2.7 — a CPU rasterizer with six cube faces per
+ frame behind it. Both were reported as failures. The bound is wall-clock now (15s) and
+ is a backstop rather than a schedule: the copy is already submitted, so what keeps it
+ from landing is a device that died, which the poll reports by itself.
+
+ Gate `point-shadow-stepped`, both backends: the same scene at 64 steps. Verified
+ against the defect — with the bound back at 1.1s it fails, by name.
+
- **A readback the engine refuses is a failure, not a blank frame.** The pixel harness
asked the engine for its own pixels and, when that came back null, read the composited
PAGE instead. On the hidden window every verifier runs in, that page is blank — so a
diff --git a/desktop/scripts/headless-verify.mjs b/desktop/scripts/headless-verify.mjs
index eb5007eaf..d52480756 100644
--- a/desktop/scripts/headless-verify.mjs
+++ b/desktop/scripts/headless-verify.mjs
@@ -469,7 +469,7 @@ app.whenReady().then(async () => {
let c = null;
if (api.captureViewportPixels) {
c = await api.captureViewportPixels();
- if (!c) throw new Error('captureViewportPixels: the engine refused its own readback');
+ if (!c) throw new Error('captureViewportPixels: no pixels — the engine refused the readback, or it was still pending when the wait gave up');
}
c = c ?? api.captureViewport();
const px = c.rgba, w = c.width, h = c.height;
diff --git a/sdk/src/render/readback.ts b/sdk/src/render/readback.ts
index 65f6a1bbc..977595f77 100644
--- a/sdk/src/render/readback.ts
+++ b/sdk/src/render/readback.ts
@@ -18,17 +18,22 @@ export const READBACK_FAILED = 2;
/**
* Awaits an engine readback poll until it reports ready (1) or failed (2),
- * yielding a macrotask between pending polls. Returns the terminal status;
- * a readback still pending after @p maxPolls resolves as failed.
+ * yielding a macrotask between pending polls. Returns the terminal status.
+ *
+ * @param timeoutMs A backstop, not a schedule: the copy is already submitted, so
+ * what keeps it from landing is a device that died — which the poll reports as
+ * failed on its own — or a queue that has not drained. A CPU rasterizer with a
+ * few dozen shadow-casting frames behind it takes seconds to hand one over.
*/
export async function awaitReadback(
poll: () => number,
- maxPolls = 240,
+ timeoutMs = 15_000,
): Promise<number> {
- for (let i = 0; i < maxPolls; i++) {
+ const deadline = Date.now() + timeoutMs;
+ for (;;) {
const status = poll();
if (status !== 0) return status;
+ if (Date.now() >= deadline) return READBACK_FAILED;
await new Promise<void>((resolve) => setTimeout(resolve, 0));
}
- return READBACK_FAILED;
}
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 3ba3c5099..8d5779634 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -92,6 +92,10 @@ export const SCENES = [
// it did and the edge sharpens by the ratio of the two distances. A fixed blur would
// read the same in both gates, and one that ignores the source's size neither.
{ id: "soft-shadow-contact", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/soft-shadow-contact.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.745,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.759,\"y\":0.5,\"rgb\":[85,85,85],\"tol\":55},{\"x\":0.77,\"y\":0.5,\"rgb\":[180,180,180],\"tol\":25},{\"x\":0.833,\"y\":0.5,\"rgb\":[170,170,170],\"tol\":25}]" } },
+ // The same scene stepped far enough to leave a deep queue behind it: on the second
+ // backend the copy of the frame lands about 2.7 seconds after it is asked for, so this
+ // is the gate that says waiting for one is bounded by the device, not by a poll count.
+ { id: "point-shadow-stepped", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/point-shadow.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "64", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.167,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.5,\"y\":0.833,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[242,242,242],\"tol\":20}]" } },
{ id: "mesh-shadow-cascade", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow-cascade.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.657,\"y\":0.452,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.6,\"y\":0.452,\"rgb\":[252,252,252],\"tol\":20},{\"x\":0.72,\"y\":0.452,\"rgb\":[252,252,252],\"tol\":20}]" } },
{ id: "mesh-shadow-perspective", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh-shadow-perspective.esscene", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.86,\"y\":0.5,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.10,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20},{\"x\":0.5,\"y\":0.5,\"rgb\":[228,228,228],\"tol\":20}]" } },
// A sun aimed OUT of the plane: 0.25 in x and 0.25 in depth throws the blocker's