Skip to content

Commit 57f4eff

Browse files
miguel-heygenmiga-heygen
authored andcommitted
fix(engine): skip motion-blur dedup when the shutter window moves
The static-frame dedup reuses the previous frame's buffer when a frame is byte-identical to its predecessor. With motion blur on that test is not enough: the shutter window reads content either side of the frame instant, so a still frame next to a moving one reused a buffer that carries none of that motion. At 30 fps and 16 samples frame 11's window runs 10.75 to 11.25 at a 180 degree shutter and 10.06 to 11.94 at 720, so both reach past the frame instant and the default is affected too. motionBlurWindowIsStatic now owns whether a blurred frame may reuse its predecessor's buffer. It requires every frame the window reads from to be static, and covers the previous frame's window as well, because reuse claims the two blurred outputs are equal. The range is derived from the plan's own sample tick offsets, so it cannot drift from where the samples land. The guard's contract comes from #4013 by Dante-dan, which carried it before we did. One existing test fixture is widened: it asserted reuse with only the frame itself marked static, which the guard now correctly rejects. Co-authored-by: Miguel Ángel <[email protected]>
1 parent b64a7a3 commit 57f4eff

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎packages/engine/src/services/frameCapture-motionBlur.test.ts‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,15 @@ describe("sub-frame accumulation reaches the page with distinct sample times", (
172172
});
173173

174174
describe("accumulation composes with static-frame dedup", () => {
175+
// 720 degrees is the widest shutter After Effects offers, and the AD5 reference export
176+
// uses it, so it is the case a reader is most likely to check the frame ranges against.
177+
const wideShutter = resolveMotionBlurPlan({ shutterAngle: 720, shutterPhase: -360 }) ?? undefined;
178+
175179
it("pays nothing on a static frame instead of capturing K samples of the same instant", async () => {
176180
const anchor = solidPng(42);
177181
const session = makeSession({
178-
staticFrames: new Set([11]),
182+
// The whole window either side of frame 11 is static, which is what reuse claims.
183+
staticFrames: new Set([9, 10, 11, 12]),
179184
lastFrameBuffer: anchor,
180185
lastFrameAbsoluteIndex: 10,
181186
});
@@ -199,6 +204,40 @@ describe("accumulation composes with static-frame dedup", () => {
199204

200205
expect(vi.mocked(pageScreenshotCapture)).toHaveBeenCalledTimes(16);
201206
});
207+
208+
// The defect this locks: dedup asked only whether THIS frame matched its predecessor.
209+
// The shutter window reads either side of the frame instant, so a still frame sitting
210+
// next to a moving one reused a buffer that carries none of that motion. Frame 11 is
211+
// static and 12 is not, and at a 720 degree shutter frame 11's window runs to 11.94, so
212+
// it reaches motion the reused buffer cannot contain. Guard from #4013 by Dante-dan.
213+
it("captures rather than reuses when the shutter window reaches a moving frame", async () => {
214+
const session = makeSession({
215+
motionBlur: wideShutter,
216+
staticFrames: new Set([9, 10, 11]),
217+
lastFrameBuffer: solidPng(42),
218+
lastFrameAbsoluteIndex: 10,
219+
});
220+
221+
await captureFrameToBuffer(session, 11, 11 / 30);
222+
223+
expect(vi.mocked(pageScreenshotCapture)).toHaveBeenCalledTimes(16);
224+
expect(session.staticDedupCount).toBeUndefined();
225+
});
226+
227+
it("still reuses at 720 degrees when the whole window is static", async () => {
228+
const anchor = solidPng(42);
229+
const session = makeSession({
230+
motionBlur: wideShutter,
231+
staticFrames: new Set([9, 10, 11, 12]),
232+
lastFrameBuffer: anchor,
233+
lastFrameAbsoluteIndex: 10,
234+
});
235+
236+
const result = await captureFrameToBuffer(session, 11, 11 / 30);
237+
238+
expect(result.buffer).toBe(anchor);
239+
expect(vi.mocked(pageScreenshotCapture)).not.toHaveBeenCalled();
240+
});
202241
});
203242

204243
describe("every path that marks a session ready also resolves the plan", () => {

‎packages/engine/src/services/frameCapture.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { encodePng } from "../utils/alphaBlit.js";
2424
import {
2525
MotionBlurAccumulator,
2626
motionBlurSampleTimes,
27+
motionBlurWindowIsStatic,
2728
resolveMotionBlurPlan,
2829
type MotionBlurPlan,
2930
} from "./motionBlur.js";
@@ -3832,7 +3833,9 @@ async function captureFrameCore(
38323833
if (
38333834
session.staticFrames?.has(absFrameIndex) &&
38343835
session.lastFrameBuffer &&
3835-
session.lastFrameAbsoluteIndex === absFrameIndex - 1
3836+
session.lastFrameAbsoluteIndex === absFrameIndex - 1 &&
3837+
(!session.motionBlur ||
3838+
motionBlurWindowIsStatic(session.motionBlur, absFrameIndex, session.staticFrames))
38363839
) {
38373840
session.staticDedupCount = (session.staticDedupCount ?? 0) + 1;
38383841
session.lastFrameAbsoluteIndex = absFrameIndex;

‎packages/engine/src/services/motionBlur.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,38 @@ export function motionBlurSampleTimes(
108108
return plan.sampleTickOffsets.map((offset) => Math.max(0, frameTicks + offset) / grid);
109109
}
110110

111+
/**
112+
* Is every frame the shutter window reads from known static, so this frame can reuse its
113+
* predecessor's buffer instead of paying K captures?
114+
*
115+
* The static-frame dedup asks whether a frame is byte-identical to the one before it. With
116+
* blur on that is not enough: the window reads content from either side of the frame
117+
* instant, so a still frame next to a moving one still has to be captured. At a shutter
118+
* angle above 360 the window spans whole neighbouring frames and the reuse would drop a
119+
* frame of motion outright.
120+
*
121+
* The range covers the previous frame's window too, because reuse claims this frame's
122+
* blurred output equals that one's. Contract taken from #4013 by Dante-dan, which carried
123+
* this guard before we did.
124+
*/
125+
export function motionBlurWindowIsStatic(
126+
plan: MotionBlurPlan,
127+
frameIndex: number,
128+
staticFrames: ReadonlySet<number>,
129+
): boolean {
130+
const first = Math.floor(
131+
frameIndex - 1 + Math.min(...plan.sampleTickOffsets) / plan.subFrameDivisions,
132+
);
133+
// A sample landing inside [F, F+1) reads content the frame set only pins down at both
134+
// ends, so the frame after the last one touched has to be static as well.
135+
const last =
136+
Math.floor(frameIndex + Math.max(...plan.sampleTickOffsets) / plan.subFrameDivisions) + 1;
137+
for (let frame = first; frame <= last; frame++) {
138+
if (!staticFrames.has(frame)) return false;
139+
}
140+
return true;
141+
}
142+
111143
const SRGB_TO_LINEAR = (() => {
112144
const lut = new Float64Array(256);
113145
for (let i = 0; i < 256; i++) lut[i] = srgbByteToLinear(i);

0 commit comments

Comments
 (0)