Skip to content

πŸŽ₯ Make videos programmatically with React

`@remotion/effects`: Fix upside-down WebGL effects after 2D effects in chain (#7535)

Jonny Burgere4e0802ced36
diff --git a/packages/core/src/effects/effect-types.ts b/packages/core/src/effects/effect-types.ts
index 5391a9f38b..8698a415dc 100644
--- a/packages/core/src/effects/effect-types.ts
+++ b/packages/core/src/effects/effect-types.ts
@@ -26,6 +26,12 @@ export type EffectApplyParams<P, S> = {
readonly width: number;
readonly height: number;
readonly gpuDevice: AnyGpuDevice | null;
+ /**
+ * When `true`, WebGL `texImage2D` uploads use `UNPACK_FLIP_Y_WEBGL` so DOM-style
+ * 2D frame canvases match clip-space UVs. Set by `runEffectChain` β€” `false` for
+ * prior WebGL outputs and `ImageBitmap` bridges from WebGL.
+ */
+ readonly flipSourceY: boolean;
};
export type EffectDefinition<P, S = unknown> = {
diff --git a/packages/core/src/effects/run-effect-chain.ts b/packages/core/src/effects/run-effect-chain.ts
index 61c7072946..233d5f92e4 100644
--- a/packages/core/src/effects/run-effect-chain.ts
+++ b/packages/core/src/effects/run-effect-chain.ts
@@ -117,6 +117,9 @@ export const runEffectChain = async ({
return false;
}
+ // Raw component sources are 2D frame canvases (Gif, WrappedCanvas.canvas, …).
+ let flipWebGLSourceY = true;
+
for (let runIndex = 0; runIndex < runs.length; runIndex++) {
const run = runs[runIndex];
const [a, b] = state.pool.getPair(run.backend);
@@ -134,9 +137,11 @@ export const runEffectChain = async ({
width,
height,
gpuDevice,
+ flipSourceY: run.backend === 'webgl2' ? flipWebGLSourceY : false,
});
if (run.backend === 'webgl2') {
+ flipWebGLSourceY = false;
state.pool.assertContextNotLost(dst);
}
@@ -148,20 +153,29 @@ export const runEffectChain = async ({
const nextRun = runs[runIndex + 1];
if (nextRun && nextRun.backend !== run.backend && lastTarget) {
- // Bridge between backend groups via `createImageBitmap` rather than
- // passing the canvas straight through. A direct `drawImage(webglCanvas)`
- // in the next backend's first effect forces an implicit GPU readback /
- // finish on the consuming context, which empirically blows the per-frame
- // vsync budget and halves the paint rate. `createImageBitmap` performs
- // the same handoff but pipelines the GPU work, so the next effect reads
- // from a ready-to-sample bitmap without stalling.
- const bitmap = await createImageBitmap(lastTarget);
- if (isCancelled()) {
- bitmap.close();
- return false;
+ // 2D β†’ WebGL: pass the 2D canvas directly so `texImage2D` + `UNPACK_FLIP_Y`
+ // matches blur-only on a raw frame canvas. `createImageBitmap` here changes
+ // upload orientation and produced upside-down stacks (wave + blur).
+ if (run.backend === '2d' && nextRun.backend === 'webgl2') {
+ currentImage = lastTarget;
+ flipWebGLSourceY = true;
+ } else {
+ // Other bridges use `createImageBitmap` rather than passing the canvas
+ // straight through. A direct `drawImage(webglCanvas)` in the next
+ // backend's first effect forces an implicit GPU readback / finish on the
+ // consuming context, which empirically blows the per-frame vsync budget and
+ // halves the paint rate. `createImageBitmap` pipelines the GPU work.
+ const bitmap = await createImageBitmap(lastTarget);
+ if (isCancelled()) {
+ bitmap.close();
+ return false;
+ }
+
+ currentImage = bitmap;
+ if (nextRun.backend === 'webgl2') {
+ flipWebGLSourceY = false;
+ }
}
-
- currentImage = bitmap;
}
}
diff --git a/packages/effects/src/blur/blur-runtime.ts b/packages/effects/src/blur/blur-runtime.ts
index 170b3afc08..02e7ac98d1 100644
--- a/packages/effects/src/blur/blur-runtime.ts
+++ b/packages/effects/src/blur/blur-runtime.ts
@@ -119,7 +119,6 @@ export const setupBlur = (target: HTMLCanvasElement): BlurState => {
}
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
const programHorizontal = createProgram(gl, BLUR_VS, BLUR_FS_HORIZONTAL);
const programVertical = createProgram(gl, BLUR_VS, BLUR_FS_VERTICAL);
@@ -258,6 +257,7 @@ type ApplyBlurParams = {
readonly radius: number;
readonly horizontal: boolean;
readonly vertical: boolean;
+ readonly flipSourceY: boolean;
};
const uploadBlurSource = ({
@@ -267,6 +267,7 @@ const uploadBlurSource = ({
textureIntermediate,
width,
height,
+ flipSourceY,
}: {
gl: WebGL2RenderingContext;
textureSource: WebGLTexture;
@@ -274,7 +275,9 @@ const uploadBlurSource = ({
textureIntermediate: WebGLTexture;
width: number;
height: number;
+ flipSourceY: boolean;
}): void => {
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
gl.bindTexture(gl.TEXTURE_2D, textureSource);
gl.texImage2D(
gl.TEXTURE_2D,
@@ -308,6 +311,7 @@ export const applyBlur = ({
radius,
horizontal,
vertical,
+ flipSourceY,
}: ApplyBlurParams): void => {
const {
gl,
@@ -327,6 +331,7 @@ export const applyBlur = ({
textureIntermediate,
width,
height,
+ flipSourceY,
});
gl.clearColor(0, 0, 0, 0);
diff --git a/packages/effects/src/blur/index.ts b/packages/effects/src/blur/index.ts
index f9c70ac992..1334932101 100644
--- a/packages/effects/src/blur/index.ts
+++ b/packages/effects/src/blur/index.ts
@@ -68,7 +68,7 @@ export const blur = createEffect<BlurParams, BlurState>({
return `${r.radius}-${r.horizontal ? 1 : 0}-${r.vertical ? 1 : 0}`;
},
setup: (target) => setupBlur(target),
- apply: ({source, width, height, params, state}) => {
+ apply: ({source, width, height, params, state, flipSourceY}) => {
const r = resolveBlurParams(params);
applyBlur({
state,
@@ -78,6 +78,7 @@ export const blur = createEffect<BlurParams, BlurState>({
radius: r.radius,
horizontal: r.horizontal,
vertical: r.vertical,
+ flipSourceY,
});
},
cleanup: (state) => cleanupBlur(state),
diff --git a/packages/effects/src/halftone.ts b/packages/effects/src/halftone.ts
index 8ba9b8530a..3e8a67ee1c 100644
--- a/packages/effects/src/halftone.ts
+++ b/packages/effects/src/halftone.ts
@@ -298,9 +298,6 @@ export const halftone = createEffect<HalftoneParams, HalftoneState>({
}
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
- // DOM sources (video, canvas) are top-left origin; GL texture space matches
- // our quad UVs (y up in clip space) only when uploads flip vertically.
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
const vs = compileShader(gl, gl.VERTEX_SHADER, HALFTONE_VS);
const fs = compileShader(gl, gl.FRAGMENT_SHADER, HALFTONE_FS);
@@ -374,7 +371,7 @@ export const halftone = createEffect<HalftoneParams, HalftoneState>({
cachedColorRgba: [0, 0, 0, 1] as [number, number, number, number],
};
},
- apply: ({source, width, height, params, state}) => {
+ apply: ({source, width, height, params, state, flipSourceY}) => {
const r = resolve(params);
const {gl, program, vao, texture} = state;
@@ -398,6 +395,7 @@ export const halftone = createEffect<HalftoneParams, HalftoneState>({
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
gl.texImage2D(
gl.TEXTURE_2D,
0,
diff --git a/packages/light-leaks/src/light-leak-internals.ts b/packages/light-leaks/src/light-leak-internals.ts
index abdd7c9d8f..8ce4de3509 100644
--- a/packages/light-leaks/src/light-leak-internals.ts
+++ b/packages/light-leaks/src/light-leak-internals.ts
@@ -209,7 +209,6 @@ const lightLeak = createEffect<LightLeakEffectParams, LightLeakGlState>({
}
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
const vs = compileShader(gl, gl.VERTEX_SHADER, LIGHT_LEAK_VS);
const fs = compileShader(gl, gl.FRAGMENT_SHADER, LIGHT_LEAK_FS);
@@ -270,7 +269,7 @@ const lightLeak = createEffect<LightLeakEffectParams, LightLeakGlState>({
uResolution: gl.getUniformLocation(program, 'resolution'),
};
},
- apply: ({source, width, height, params, state}) => {
+ apply: ({source, width, height, params, state, flipSourceY}) => {
const r = resolve(params);
if (typeof r.seed !== 'number' || !Number.isFinite(r.seed)) {
@@ -320,6 +319,7 @@ const lightLeak = createEffect<LightLeakEffectParams, LightLeakGlState>({
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
gl.texImage2D(
gl.TEXTURE_2D,
0,
diff --git a/packages/starburst/src/starburst-effect.ts b/packages/starburst/src/starburst-effect.ts
index 257fa310d2..7be8c0b164 100644
--- a/packages/starburst/src/starburst-effect.ts
+++ b/packages/starburst/src/starburst-effect.ts
@@ -316,7 +316,6 @@ export const starburst = createEffect<StarburstEffectParams, StarburstGlState>({
}
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
const vs = compileShader(gl, gl.VERTEX_SHADER, STARBURST_VS);
const fs = compileShader(gl, gl.FRAGMENT_SHADER, STARBURST_FS);
@@ -394,7 +393,7 @@ export const starburst = createEffect<StarburstEffectParams, StarburstGlState>({
palettePixelData: new Uint8Array(0),
};
},
- apply: ({source, width, height, params, state}) => {
+ apply: ({source, width, height, params, state, flipSourceY}) => {
const r = resolve(params);
const {
gl,
@@ -445,6 +444,7 @@ export const starburst = createEffect<StarburstEffectParams, StarburstGlState>({
gl.bindTexture(gl.TEXTURE_2D, sourceTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
gl.texImage2D(
gl.TEXTURE_2D,
0,