π₯ Make videos programmatically with React
Improve WebGL context failure errors and add troubleshooting docs
Jonny Burger48f40bf23d02
diff --git a/packages/core/src/effects/canvas-pool.ts b/packages/core/src/effects/canvas-pool.tsindex 4ef37f7891..42c9212fc7 100644--- a/packages/core/src/effects/canvas-pool.ts+++ b/packages/core/src/effects/canvas-pool.ts@@ -1,4 +1,5 @@import type {Backend} from './effect-types.js';+import {createWebGL2ContextError} from './webgl2-context-error.js';// A pair of scratch canvases for ping-ponging within a same-backend run.type CanvasPair = readonly [HTMLCanvasElement, HTMLCanvasElement];@@ -69,7 +70,7 @@ export class CanvasPool {preserveDrawingBuffer: true,});if (!ctx) {- throw new Error('Failed to acquire WebGL2 context for canvas effect');+ throw createWebGL2ContextError('canvas effect');}canvas.addEventListener('webglcontextlost', (e) => {diff --git a/packages/core/src/effects/webgl2-context-error.ts b/packages/core/src/effects/webgl2-context-error.tsnew file mode 100644index 0000000000..1efea70313--- /dev/null+++ b/packages/core/src/effects/webgl2-context-error.ts@@ -0,0 +1,20 @@+const WEBGL_CONTEXT_DOCS_URL =+ 'https://remotion.dev/docs/troubleshooting/webgl2-context';++export const WEBGL2_CONTEXT_ERROR_MARKER = 'Failed to acquire WebGL2 context';++const webGlContextErrorMessage = (+ versionLabel: 'WebGL' | 'WebGL2',+ effectName: string,+): string =>+ `Failed to acquire ${versionLabel} context for ${effectName}. ` ++ 'Headless Chromium often cannot create a WebGL context without the ANGLE renderer. ' ++ 'Pass --gl=angle when using the CLI, set chromiumOptions: { gl: "angle" } when using server-side rendering APIs, ' ++ 'or set "OpenGL render backend" to "angle" in the Advanced section when rendering in the Studio. ' ++ `See ${WEBGL_CONTEXT_DOCS_URL}`;++export const createWebGLContextError = (effectName: string): Error =>+ new Error(webGlContextErrorMessage('WebGL', effectName));++export const createWebGL2ContextError = (effectName: string): Error =>+ new Error(webGlContextErrorMessage('WebGL2', effectName));diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex 8fe1be8e35..1c9d2526cd 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -54,6 +54,10 @@ import {useMemoizedEffectDefinitions,useMemoizedEffects,} from './effects/use-memoized-effects.js';+import {+ createWebGL2ContextError,+ createWebGLContextError,+} from './effects/webgl2-context-error.js';import {addSequenceStackTraces,getComponentsToAddStacksTo,@@ -346,6 +350,8 @@ export const Internals = {useMemoizedEffects,useMemoizedEffectDefinitions,createEffect,+ createWebGLContextError,+ createWebGL2ContextError,computeEffectiveSchemaValuesDotNotation,OverrideIdsToNodePathsGettersContext,OverrideIdsToNodePathsSettersContext,diff --git a/packages/core/src/test/webgl2-context-error.test.ts b/packages/core/src/test/webgl2-context-error.test.tsnew file mode 100644index 0000000000..1710c0d154--- /dev/null+++ b/packages/core/src/test/webgl2-context-error.test.ts@@ -0,0 +1,27 @@+import {describe, expect, test} from 'bun:test';+import {+ createWebGL2ContextError,+ createWebGLContextError,+ WEBGL2_CONTEXT_ERROR_MARKER,+} from '../effects/webgl2-context-error.js';++describe('WebGL context errors', () => {+ test('createWebGL2ContextError includes effect name and docs link', () => {+ const err = createWebGL2ContextError('blur effect');+ expect(err.message).toContain(WEBGL2_CONTEXT_ERROR_MARKER);+ expect(err.message).toContain('blur effect');+ expect(err.message).toContain('--gl=angle');+ expect(err.message).toContain('chromiumOptions: { gl: "angle" }');+ expect(err.message).toContain('OpenGL render backend');+ expect(err.message).toContain(+ 'https://remotion.dev/docs/troubleshooting/webgl2-context',+ );+ });++ test('createWebGLContextError uses WebGL label', () => {+ const err = createWebGLContextError('light leak');+ expect(err.message).toContain(+ 'Failed to acquire WebGL context for light leak',+ );+ });+});diff --git a/packages/docs/docs/troubleshooting/webgl2-context.mdx b/packages/docs/docs/troubleshooting/webgl2-context.mdxnew file mode 100644index 0000000000..0f06d985e4--- /dev/null+++ b/packages/docs/docs/troubleshooting/webgl2-context.mdx@@ -0,0 +1,74 @@+---+image: /generated/articles-docs-troubleshooting-webgl2-context.png+title: Failed to acquire WebGL2 context+sidebar_label: Failed to acquire WebGL2 context+crumb: 'Troubleshooting'+---++You may see an error like this when rendering a composition that uses WebGL-based canvas effects (for example blur, halftone, light leaks, or starburst):++```+Failed to acquire WebGL2 context for blur effect. Headless Chromium often cannot create a WebGL context without the ANGLE renderer. Pass --gl=angle when using the CLI, set chromiumOptions: { gl: "angle" } when using server-side rendering APIs, or set "OpenGL render backend" to "angle" in the Advanced section when rendering in the Studio. See https://remotion.dev/docs/troubleshooting/webgl2-context+```++This means Chromium could not create a WebGL context for the effect. It often happens in headless environments (CLI renders, Lambda, Docker) where the default OpenGL backend does not expose WebGL.++## Fix: use the ANGLE renderer++Enable the ANGLE OpenGL backend so Chromium can create a WebGL context.++### CLI++Pass [`--gl=angle`](/docs/cli/render#--gl) to your render command:++```bash+npx remotion render MyComp out/video.mp4 --gl=angle+```++You can also set it as the default in [`remotion.config.ts`](/docs/config):++```ts twoslash title="remotion.config.ts"+import {Config} from '@remotion/cli/config';+// ---cut---+Config.setChromiumOpenGlRenderer('angle');+```++### Server-side rendering APIs++Pass `gl: "angle"` in [`chromiumOptions`](/docs/renderer/render-media#gl):++```tsx twoslash+import {renderMedia} from '@remotion/renderer';++// ---cut---+await renderMedia({+ composition: 'MyComp',+ serveUrl: bundleLocation,+ codec: 'h264',+ outputLocation: 'out/video.mp4',+ chromiumOptions: {+ gl: 'angle',+ },+});+```++This applies to [`renderMedia()`](/docs/renderer/render-media), [`renderStill()`](/docs/renderer/render-still), [`renderFrames()`](/docs/renderer/render-frames), [`getCompositions()`](/docs/renderer/get-compositions), and the [Lambda](/docs/lambda/rendermediaonlambda) and [Vercel](/docs/vercel/render-media-on-vercel) equivalents.++### Remotion Studio++When rendering from the Studio UI, open the render modal, expand **Advanced**, and set **OpenGL render backend** to `angle`.++## Machines without a GPU++If you are on a machine without a GPU (for example AWS Lambda), use [`--gl=swangle`](/docs/gl-options) instead. This is the default on Lambda.++## Related++- [OpenGL renderer backends](/docs/gl-options)+- [`--gl` Chromium flag](/docs/chromium-flags#--gl)+- [GPU rendering](/docs/gpu)+- [`<HtmlInCanvas>` rendering guide](/docs/html-in-canvas-guide#rendering)++---++If you believe this error was triggered by a bug in Remotion, please [open an issue](/docs/get-help).diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex 0b895b2252..1d9d40a2c1 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -1422,6 +1422,7 @@ const sidebars: SidebarsConfig = {'troubleshooting/broken-fast-refresh','troubleshooting/timed-out-page-function','troubleshooting/delay-render-proxy',+ 'troubleshooting/webgl2-context','troubleshooting/bundling-bundle','troubleshooting/browser-launch','troubleshooting/sigkill',diff --git a/packages/effects/src/blur/blur-runtime.ts b/packages/effects/src/blur/blur-runtime.tsindex 39418dd74e..8bb2ddaf9c 100644--- a/packages/effects/src/blur/blur-runtime.ts+++ b/packages/effects/src/blur/blur-runtime.ts@@ -1,5 +1,8 @@+import {Internals} from 'remotion';import {BLUR_FS_HORIZONTAL, BLUR_FS_VERTICAL, BLUR_VS} from './blur-shaders.js';+const {createWebGL2ContextError} = Internals;+// WebGL2 separable Gaussian blur: horizontal pass renders into an intermediate// texture (FBO), vertical pass samples that texture and draws to the canvas.// Both passes share the same fullscreen quad (VAO/VBO).@@ -112,7 +115,7 @@ export const setupBlur = (target: HTMLCanvasElement): BlurState => {preserveDrawingBuffer: true,});if (!gl) {- throw new Error('Failed to acquire WebGL2 context for blur effect');+ throw createWebGL2ContextError('blur effect');}gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);diff --git a/packages/effects/src/halftone.ts b/packages/effects/src/halftone.tsindex 27a4d65ca3..523b495ca2 100644--- a/packages/effects/src/halftone.ts+++ b/packages/effects/src/halftone.ts@@ -1,7 +1,7 @@import type {SequenceSchema} from 'remotion';import {Internals} from 'remotion';-const {createEffect} = Internals;+const {createEffect, createWebGL2ContextError} = Internals;const SHADE_OUTSIDE_DOT_SCALE = 0.5;@@ -294,7 +294,7 @@ export const halftone = createEffect<HalftoneParams, HalftoneState>({preserveDrawingBuffer: true,});if (!gl) {- throw new Error('Failed to acquire WebGL2 context for halftone effect');+ throw createWebGL2ContextError('halftone effect');}gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);diff --git a/packages/light-leaks/src/LightLeak.tsx b/packages/light-leaks/src/LightLeak.tsxindex 39160d98ef..bd1af975eb 100644--- a/packages/light-leaks/src/LightLeak.tsx+++ b/packages/light-leaks/src/LightLeak.tsx@@ -12,6 +12,8 @@ import {type SequenceProps,} from 'remotion';+const {createWebGLContextError} = Internals;+export type LightLeakProps = Omit<SequenceProps,'children' | 'durationInFrames' | keyof LayoutAndStyle@@ -137,11 +139,7 @@ const LightLeakCanvas: React.FC<{alpha: true,});if (!gl) {- cancelRender(- new Error(- 'Failed to get WebGL context. Try rendering with --gl=angle to enable WebGL.',- ),- );+ cancelRender(createWebGLContextError('light leak'));return null;}diff --git a/packages/light-leaks/src/light-leak-internals.ts b/packages/light-leaks/src/light-leak-internals.tsindex 5d80fe4c5d..3cd47d59fb 100644--- a/packages/light-leaks/src/light-leak-internals.ts+++ b/packages/light-leaks/src/light-leak-internals.ts@@ -1,7 +1,7 @@import type {SequenceSchema} from 'remotion';import {Internals} from 'remotion';-const {createEffect} = Internals;+const {createEffect, createWebGL2ContextError} = Internals;export const lightLeakEffectSchema = {seed: {type: 'number', default: 0, description: 'Seed'},@@ -205,7 +205,7 @@ const lightLeak = createEffect<LightLeakEffectParams, LightLeakGlState>({preserveDrawingBuffer: true,});if (!gl) {- throw new Error('Failed to acquire WebGL2 context for light leak effect');+ throw createWebGL2ContextError('light leak effect');}gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);diff --git a/packages/renderer/src/print-useful-error-message.ts b/packages/renderer/src/print-useful-error-message.tsindex 9a32b21ebe..2d7592f9e8 100644--- a/packages/renderer/src/print-useful-error-message.ts+++ b/packages/renderer/src/print-useful-error-message.ts@@ -105,6 +105,17 @@ export const printUsefulErrorMessage = ();}+ if (+ err.message.includes('Failed to acquire WebGL2 context') ||+ err.message.includes('Failed to acquire WebGL context')+ ) {+ Log.info({indent, logLevel});+ Log.info(+ {indent, logLevel},+ 'π‘ Get help for this issue at https://remotion.dev/docs/troubleshooting/webgl2-context',+ );+ }+if (err.message.includes('Error creating WebGL context')) {Log.info({indent, logLevel});Log.warn(@@ -121,6 +132,10 @@ export const printUsefulErrorMessage = (},"π‘ Check how it's done at https://www.remotion.dev/docs/chromium-flags#--gl",);+ Log.info(+ {indent, logLevel},+ 'π‘ Get help for this issue at https://remotion.dev/docs/troubleshooting/webgl2-context',+ );}if (err.message.includes('The bucket does not allow ACLs')) {diff --git a/packages/starburst/src/starburst-effect.ts b/packages/starburst/src/starburst-effect.tsindex 734d8ab5bf..1571c13728 100644--- a/packages/starburst/src/starburst-effect.ts+++ b/packages/starburst/src/starburst-effect.ts@@ -2,7 +2,7 @@ import type {EffectDescriptor, SequenceSchema} from 'remotion';import {Internals} from 'remotion';import {hexToRgb} from './hex-to-rgb';-const {createEffect} = Internals;+const {createEffect, createWebGL2ContextError} = Internals;export const starburstEffectSchema = {rays: {@@ -306,7 +306,7 @@ const starburstImpl = createEffect<StarburstEffectParams, StarburstGlState>({preserveDrawingBuffer: true,});if (!gl) {- throw new Error('Failed to acquire WebGL2 context for starburst effect');+ throw createWebGL2ContextError('starburst effect');}gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);