The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.
Render: WebGL on the GPU on Windows, Vulkan first with a fallback to the default (#73)
HEOJUNFO90189757998c
diff --git a/.changeset/windows-angle-vulkan.md b/.changeset/windows-angle-vulkan.mdnew file mode 100644index 0000000..1ce2513--- /dev/null+++ b/.changeset/windows-angle-vulkan.md@@ -0,0 +1,5 @@+---+'openfilm': patch+---++On Windows, render, look and Studio draw WebGL on the GPU. Headless Chromium picked no graphics backend there and fell back to SwiftShader, which draws on the CPU; it now starts with Vulkan, reads the renderer once, and keeps Chromium's default when Vulkan doesn't start or is software too (a virtual machine, a CI runner, a computer without a Vulkan driver).diff --git a/packages/openfilm/src/host.d.mts b/packages/openfilm/src/host.d.mtsindex e7db23e..3b1c5b1 100644--- a/packages/openfilm/src/host.d.mts+++ b/packages/openfilm/src/host.d.mts@@ -6,6 +6,10 @@ export declare function injectFirst(html: string, tag: string): string;export declare function serve(root: string, opts?: { port?: number }): Promise<{ url: string; close(): Promise<void> }>;export interface LaunchOptions { headed?: boolean; channel?: string | null; args?: string[]; env?: Record<string, string | undefined> }export declare function launch(opts?: LaunchOptions): Promise<Browser>;+/** A renderer string that is not a GPU: WebGL drawn on the CPU. */+export declare const SOFTWARE_GL: RegExp;+/** Windows: open with Vulkan once, and keep it only when its renderer is a GPU; otherwise Chromium's default. */+export declare function vulkanFirst(renderer?: (browser: Browser) => Promise<string>): (open: (angle: string[]) => Promise<Browser>) => Promise<Browser>;/** Close every browser launch() opened that is still open: what waits on one fails at once instead of never answering. */export declare function closeBrowsers(): Promise<void>;/** With `offline`: the origins still open to a page, and optionally who answers them (a cache). */diff --git a/packages/openfilm/src/host.mjs b/packages/openfilm/src/host.mjsindex e358b45..74f9732 100644--- a/packages/openfilm/src/host.mjs+++ b/packages/openfilm/src/host.mjs@@ -222,13 +222,63 @@ export function injectFirst(html, tag) {export async function launch({ headed = false, channel = headed ? 'chromium' : null, args: extra = [], env } = {}) {/* CanvasDrawElement: HTML-in-Canvas (drawElementImage, texElementImage2D), so a page can draw its own DOM into 2D or WebGL */const args = ['--ignore-gpu-blocklist', '--enable-gpu-rasterization', '--autoplay-policy=no-user-gesture-required', '--hide-scrollbars', '--enable-blink-features=CanvasDrawElement', ...extra];- if (process.platform === 'darwin') args.push('--use-angle=metal');- const browser = await chromium.launch({ ...(channel ? { channel } : {}), headless: !headed, args, ...(env ? { env } : {}) });+ const open = (/** @type {string[]} */ angle) => chromium.launch({ ...(channel ? { channel } : {}), headless: !headed, args: [...args, ...angle], ...(env ? { env } : {}) });+ const browser = extra.some((a) => a.startsWith('--use-angle')) ? await open([])+ : process.platform === 'darwin' ? await open(['--use-angle=metal'])+ : process.platform === 'win32' ? await onWindows(open)+ : await open([]);LIVE.add(browser);browser.on('disconnected', () => LIVE.delete(browser));return browser;}+/** A renderer string that is not a GPU: WebGL drawn on the CPU. */+export const SOFTWARE_GL = /swiftshader|llvmpipe|software/i;++/** The page's WebGL renderer as the driver names it; 'none' without WebGL. */+const readRenderer = () => {+ const gl = document.createElement('canvas').getContext('webgl2') || document.createElement('canvas').getContext('webgl');+ if (!gl) return 'none';+ const ext = gl.getExtension('WEBGL_debug_renderer_info');+ return ext ? gl.getParameter(ext.UNMASKED_RENDERER_WEBGL) : gl.getParameter(gl.RENDERER);+};++/** @param {import('playwright-core').Browser} browser */+async function rendererOf(browser) {+ const page = await browser.newPage();+ try { return await page.evaluate(readRenderer); } finally { await page.close(); }+}++/**+ * Windows: headless Chromium picks no ANGLE backend by itself and draws WebGL with SwiftShader, on the CPU. Vulkan+ * reaches the GPU (on a laptop with two, the discrete one: d3d11 and gl take the integrated one). A virtual machine, a+ * CI runner or a computer without a Vulkan driver has none, so the first browser reads its renderer once: when Vulkan+ * did not start, or is software too, it and every later browser take Chromium's default.+ * @param {(browser: import('playwright-core').Browser) => Promise<string>} [renderer]+ */+export function vulkanFirst(renderer = rendererOf) {+ /** @type {Promise<string[]> | null} what every browser after the first is opened with */+ let chosen = null;+ /** @param {(angle: string[]) => Promise<import('playwright-core').Browser>} open */+ return async (open) => {+ if (chosen) return open(await chosen);+ /** @type {(angle: string[]) => void} */+ let choose = () => {};+ chosen = new Promise((r) => { choose = r; });+ const vulkan = ['--use-angle=vulkan'];+ const browser = await open(vulkan).catch(() => null);+ const gpu = browser ? await inTime(renderer(browser), 30_000, 'no renderer').catch(() => 'none') : 'none';+ if (browser && gpu !== 'none' && !SOFTWARE_GL.test(gpu)) {+ choose(vulkan);+ return browser;+ }+ await browser?.close().catch(() => {});+ choose([]);+ return open([]);+ };+}+const onWindows = vulkanFirst();+/** Every browser launch() opened and that is still open. */const LIVE = new Set();/** Close them all: what waits on one (a render, a poster) fails at once instead of never answering. */@@ -420,12 +470,7 @@ export class FilmPage {}async gpu() {- return this.page.evaluate(() => {- const gl = document.createElement('canvas').getContext('webgl2') || document.createElement('canvas').getContext('webgl');- if (!gl) return 'none';- const ext = gl.getExtension('WEBGL_debug_renderer_info');- return ext ? gl.getParameter(ext.UNMASKED_RENDERER_WEBGL) : gl.getParameter(gl.RENDERER);- });+ return this.page.evaluate(readRenderer);}async close() { await this.context.close(); }diff --git a/packages/openfilm/src/host.test.mjs b/packages/openfilm/src/host.test.mjsindex 4338553..2b40a27 100644--- a/packages/openfilm/src/host.test.mjs+++ b/packages/openfilm/src/host.test.mjs@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';import { mkdirSync, mkdtempSync, realpathSync, symlinkSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';import { join } from 'node:path';-import { serve } from './host.mjs';+import { serve, vulkanFirst } from './host.mjs';import { locate } from './shared.mjs';test('the served folder is all a page reads: no way out through .., a link or a malformed address', { skip: process.platform === 'win32' && 'links need Developer Mode on Windows' }, async () => {@@ -55,3 +55,45 @@ test('look and render serve the film\'s project, never the folders around it', (process.chdir(was);}});++test('Windows: Vulkan when its renderer is a GPU, read once; otherwise the default, for every later browser too', async () => {+ /** A launcher that records what each browser was opened with; `fails` makes a Vulkan launch throw. */+ const launcher = (fails = false) => {+ const opened = /** @type {string[][]} */ ([]), closed = /** @type {string[][]} */ ([]);+ const open = async (/** @type {string[]} */ angle) => {+ opened.push(angle);+ if (fails && angle.length) throw new Error('no Vulkan driver');+ return /** @type {any} */ ({ angle, close: async () => { closed.push(angle); } });+ };+ return { open, opened, closed };+ };+ const reads = (/** @type {() => Promise<string>} */ said) => {+ let count = 0;+ return { renderer: async () => { count++; return said(); }, get count() { return count; } };+ };+ const vulkan = ['--use-angle=vulkan'];++ const gpu = reads(async () => 'ANGLE (NVIDIA, Vulkan 1.4.329 (NVIDIA GeForce RTX 5070 Laptop GPU (0x00002D58)), NVIDIA)');+ let l = launcher(), pick = vulkanFirst(gpu.renderer);+ const browsers = await Promise.all([pick(l.open), pick(l.open), pick(l.open)]);+ assert.deepEqual(browsers.map((b) => b.angle), [vulkan, vulkan, vulkan]);+ assert.equal(gpu.count, 1);+ assert.deepEqual(l.closed, []);++ for (const said of [async () => 'ANGLE (Google, Vulkan 1.3.0 (SwiftShader Device (Subzero) (0x0000C0DE)), SwiftShader driver)', async () => 'none', async () => { throw new Error('page crashed'); }]) {+ const software = reads(said);+ l = launcher(); pick = vulkanFirst(software.renderer);+ const [first, second] = await Promise.all([pick(l.open), pick(l.open)]);+ assert.deepEqual([first.angle, second.angle], [[], []]);+ assert.deepEqual(l.opened, [vulkan, [], []]);+ assert.deepEqual(l.closed, [vulkan]);+ assert.equal(software.count, 1);+ }++ const never = reads(async () => 'unused');+ l = launcher(true); pick = vulkanFirst(never.renderer);+ assert.deepEqual((await pick(l.open)).angle, []);+ assert.deepEqual((await pick(l.open)).angle, []);+ assert.deepEqual(l.opened, [vulkan, [], []]);+ assert.equal(never.count, 0);+});diff --git a/packages/openfilm/src/look.mjs b/packages/openfilm/src/look.mjsindex 0a138ef..593df1e 100644--- a/packages/openfilm/src/look.mjs+++ b/packages/openfilm/src/look.mjs@@ -3,7 +3,7 @@import { spawnSync } from 'node:child_process';import { existsSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';import { basename, dirname, extname, join, resolve } from 'node:path';-import { FrameError, launch } from './host.mjs';+import { FrameError, launch, SOFTWARE_GL } from './host.mjs';import { blockDiff, checkSound, cutText, fileOf, ffmpeg, filmSound, fmt, hash, homeFor, mix, open, outDir, parseTarget, SAME_LIMIT, shown, shuffled, spread, UsageError } from './shared.mjs';const MEDIA = /\.(mp4|mov|m4v|webm|mkv|avi|png|jpe?g|webp|gif|mp3|wav|m4a|aac|ogg|flac)$/i;@@ -34,7 +34,7 @@ export async function look(args, opts = {}) {/* a page plays no sound of its own: what it still lists in `audio` is heard nowhere until it is a clip */for (const page of await pagesWithAudio()) warnings.push(`${page}: window.film.audio is not played: a sound is a clip in film.html`);const gpu = await film.gpu();- if (/swiftshader|llvmpipe|software/i.test(gpu)) warnings.push(`WebGL runs in software (${gpu}): slow, and pictures may differ from a GPU`);+ if (SOFTWARE_GL.test(gpu)) warnings.push(`WebGL runs in software (${gpu}): slow, and pictures may differ from a GPU`);if (errors.length) return finish();try { await draw(); } catch (e) {