Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
[issue-11686] Match film-look GPU defocus and color fringes to SVG (#11711)
[._.]/eef0a764d5df
diff --git a/server/lib/filmLookGpu.browser.test.js b/server/lib/filmLookGpu.browser.test.jsindex ff11f7a2e1..65cd34d531 100644--- a/server/lib/filmLookGpu.browser.test.js+++ b/server/lib/filmLookGpu.browser.test.js@@ -41,6 +41,11 @@ const extreme = normalizeFilmLook(Object.fromEntries(FILM_LOOK_CONTROLS.map((conconst looks = [...FILM_LOOK_PRESETS.filter(({ id }) => id !== 'none').map(({ id }) => [id, filmLookPreset(id)]),['every control at its extreme', extreme],+ // Isolate the regressions: small blurs were skipped/box-approximated, and+ // translated channels invented colored edge pixels when sampled downstream.+ ['soft focus 0.30', normalizeFilmLook({ defocus: 0.3 })],+ ['soft focus 0.35', normalizeFilmLook({ defocus: 0.35 })],+ ['color fringing 0.40', normalizeFilmLook({ chroma: 0.4 })],];const psnr = (a, b) => {@@ -49,10 +54,15 @@ const psnr = (a, b) => {return sum === 0 ? 99 : 10 * Math.log10((255 * 255 * a.length) / sum);};-describe.skipIf(!canRun)('film look on the GPU in Chrome', () => {+// The same Chrome uses different SVG blur kernels with GPU compositing and+// software rasterization. Exercise both without changing the pixel oracle.+describe.skipIf(!canRun).each([+ ['default compositor', []],+ ['software compositor', ['--disable-gpu', '--enable-unsafe-swiftshader']],+])('film look on the GPU in Chrome (%s)', (_backend, args) => {let fixture, svgPage, gpuPage, available;beforeAll(async () => {- fixture = await startOwnedBrowserFixture({ chromium, launchOptions: { executablePath: chrome, headless: true } });+ fixture = await startOwnedBrowserFixture({ chromium, launchOptions: { executablePath: chrome, headless: true, args } });svgPage = await fixture.browser.newPage({ viewport: { width, height } });gpuPage = await fixture.browser.newPage({ viewport: { width, height } });await svgPage.setContent(page(`${picture}<svg id="fx" width="0" height="0" style="position:absolute"></svg>`));@@ -80,7 +90,7 @@ describe.skipIf(!canRun)('film look on the GPU in Chrome', () => {// A video is read the way the page's compositor reads it, scaled to fill the// frame. WebGL's own video upload converted colour differently (about 30 dB// off on the soft-analog look) and copied another size unscaled.- it('draws a video of another size as the page fills the frame', async ({ skip }) => {+ it.for(['soft-analog', 'neon-rain', 'vhs'])('draws %s over a video of another size as the page fills the frame', async (preset, { skip }) => {let clip;try {const dir = mkdtempSync(join(tmpdir(), 'film-look-gpu-video-'));@@ -90,7 +100,7 @@ describe.skipIf(!canRun)('film look on the GPU in Chrome', () => {rmSync(dir, { recursive: true, force: true });} catch { skip(); }if (!available) skip();- const look = filmLookPreset('soft-analog');+ const look = filmLookPreset(preset);const origin = 'http://film-look.test';const body = (extra) => `<video id="v" src="${origin}/clip.webm" muted style="display:block;width:${width}px;height:${height}px;object-fit:fill"></video>${extra}`;const open = async (extra) => {diff --git a/server/lib/filmLookGpu.js b/server/lib/filmLookGpu.jsindex 706f70d9b4..8896fc9b92 100644--- a/server/lib/filmLookGpu.js+++ b/server/lib/filmLookGpu.js@@ -9,11 +9,12 @@* out of it and runs each one as a pass, so a change to the look reaches this* path by construction and an unknown primitive refuses (the caller then keeps* the SVG path). Each pass follows what Chrome's Skia raster backend does, not- * just the spec, as measured against headless Chromium 141: premultiplied- * 8-bit intermediates; the three-box blur at every sigma, reading past the- * frame as transparent even for edgeMode="duplicate"; feOffset snapped to- * whole pixels with antialiased frame edges; feComposite arithmetic without- * clamping color to alpha; Skia's feTurbulence (the spec's lattice and+ * just the spec, as measured against headless Chrome: premultiplied+ * 8-bit intermediates; a measured Gaussian or three-box blur for small sigmas,+ * and a three-box approximation above, reading past the frame as transparent even for+ * edgeMode="duplicate"; feOffset snapped to whole pixels (antialiased frame+ * edges when presented directly); feComposite arithmetic without clamping+ * color to alpha; Skia's feTurbulence (the spec's lattice and* gradients, permuted and stored as 16-bit, sampled half a pixel past the* pixel centre); and the result over the page's white base, where the filter* leaves alpha. `filmLookFinish.js` still compares a frame against the SVG@@ -109,17 +110,17 @@ function turbulencePixel(tables, x, y, { baseFrequency = [0, 0], numOctaves = 1,}/**- * The 1D kernel Chrome's raster blur engine applies for a standard deviation.- * Chrome builds Skia without its small-sigma Gaussian pass, so every sigma- * gets the three-box approximation (the spec's window d = ⌊σ·3·√(2π)/4 + ½⌋;- * for an even d two boxes of d shifted against each other plus one of d + 1),- * and a one-pixel window leaves the axis alone. Past Skia's 135 limit (a- * 255-pixel window) Chrome blurs a downscaled copy; a sampled Gaussian stands+ * The 1D blur kernel used to match Chrome's filter for a standard deviation.+ * Small sigmas use the browser's measured model: accelerated filters use a+ * sampled Gaussian, while Chromium's raster path uses boxes (including an+ * identity for small windows). Larger sigmas use the spec's window+ * d = ⌊σ·3·√(2π)/4 + ½⌋ (for an even d, two boxes of d shifted against each+ * other plus one of d + 1). Past Skia's 135 limit (a 255-pixel window) Chrome blurs a downscaled copy; a sampled Gaussian stands* in for that. `weights[i]` applies to offset `i - origin`; `boxes` and* `divisor` give the same kernel as three integer box sums. Null for no blur.* SELF-CONTAINED (inlined into the page).*/-function blurKernel(sigma) {+function blurKernel(sigma, smallBlur = 'gaussian') {if (!(sigma > 0.03)) return null;const gaussian = () => {const radius = Math.ceil(3 * sigma);@@ -129,8 +130,8 @@ function blurKernel(sigma) {for (let i = 0; i < weights.length; i++) weights[i] /= sum;return { weights, origin: radius };};+ if (sigma < 2 && smallBlur === 'gaussian') return gaussian();const window = Math.max(1, Math.floor(Math.fround(sigma * 3 * Math.sqrt(2 * Math.PI) / 4 + 0.5)));- // A one-pixel window is no blur at all (Chrome skips the axis).if (window === 1) return null;if (window >= 255) return gaussian();const box = (n) => new Float64Array(n).fill(1);@@ -216,11 +217,38 @@ export function filmLookGpuRuntimeSource() {// premultiplied texture in image orientation (row 0 = top), as Skia's raster// filters keep them; the last pass flips into the canvas.function createFilmLookGpuRenderer(canvas, deps) {- const { markup, turbulenceTables, blurKernel, parseFilmLookFilter } = deps;+ const { markup, turbulenceTables, blurKernel: makeBlurKernel, parseFilmLookFilter } = deps;const gl = canvas.getContext('webgl2', { alpha: false, antialias: false, depth: false, stencil: false, premultipliedAlpha: true, preserveDrawingBuffer: true });if (!gl) throw new Error('WebGL2 is unavailable');if (!gl.getExtension('EXT_color_buffer_float')) throw new Error('WebGL2 float render targets are unavailable');+ // Probe once for this renderer, then retain the model across every frame.+ // Canvas2D and DOM filters share Skia's active blur backend; a renderer name+ // or OS cannot tell us whether the compositor uses Gaussian or raster boxes.+ // The opaque impulse avoids alpha/unpremultiplication ambiguity. Refuse an+ // unknown response so the caller preserves SVG and its independent 40 dB gate.+ let smallBlur;+ try {+ const owner = canvas.ownerDocument || document;+ const source = owner.createElement('canvas'), probe = owner.createElement('canvas');+ source.width = probe.width = 31; source.height = probe.height = 31;+ const input = source.getContext('2d'), context = probe.getContext('2d');+ if (!input || !context || !('filter' in context)) throw new Error('filter probe unavailable');+ input.fillStyle = '#000'; input.fillRect(0, 0, 31, 31);+ input.fillStyle = '#fff'; input.fillRect(15, 0, 1, 31);+ context.filter = 'blur(0.7361px)';+ context.drawImage(source, 0, 0);+ const pixels = context.getImageData(12, 15, 7, 1).data;+ const models = { gaussian: [0, 3, 55, 139, 55, 3, 0], box: [0, 0, 0, 255, 0, 0, 0] };+ smallBlur = Object.keys(models).find(model => models[model].every((value, i) =>+ pixels[i * 4 + 3] === 255 && [0, 1, 2].every(c => Math.abs(pixels[i * 4 + c] - value) <= 1)));+ if (!smallBlur) throw new Error('unrecognized filter probe');+ } catch {+ gl.getExtension('WEBGL_lose_context')?.loseContext();+ throw new Error('Chrome blur model is unavailable; keep the SVG film look');+ }+ const blurKernel = sigma => makeBlurKernel(sigma, smallBlur);+const VERTEX = `#version 300 esin vec2 position;void main() { gl_Position = vec4(position, 0.0, 1.0); }`;@@ -247,11 +275,13 @@ bool clipped() { vec2 p = gl_FragCoord.xy; return p.x < clip.x || p.y < clip.y |void main() { color = texelFetch(a, pixel(), 0); }`,flood: `uniform vec4 floodColor;void main() { color = clipped() ? vec4(0.0) : floodColor; }`,- offset: `uniform sampler2D a; uniform vec2 shift;+ offset: `uniform sampler2D a; uniform vec2 shift; uniform int sampled;void main() {if (clipped()) { color = vec4(0.0); return; }- // Chrome samples an feOffset at the nearest whole pixel but antialiases the- // shifted frame's edges: a border pixel holds the edge pixel at its coverage.+ // A downstream filter samples the translated image with transparent+ // out-of-bounds texels; clamping would invent colored edge fringes.+ if (sampled == 1) { color = fetch(a, pixel() - ivec2(floor(shift + 0.5))); return; }+ // A terminal offset is drawn directly with antialiased frame coverage.ivec2 q = clamp(pixel() - ivec2(floor(shift + 0.5)), ivec2(0), ivec2(size) - 1);vec2 lo = max(floor(gl_FragCoord.xy), shift);vec2 hi = min(floor(gl_FragCoord.xy) + 1.0, shift + size);@@ -664,7 +694,7 @@ void main() {run('flood', target, (u) => { clipUniform(u, region); gl.uniform4f(u.floodColor, ...color); });floodOf = { color, rect: pixelRect(region) };} else if (p.type === 'feOffset') {- run('offset', target, (u, bind) => { clipUniform(u, region); bind('a', a); gl.uniform2f(u.shift, num(attrs.dx), num(attrs.dy)); });+ run('offset', target, (u, bind) => { clipUniform(u, region); bind('a', a); gl.uniform2f(u.shift, num(attrs.dx), num(attrs.dy)); gl.uniform1i(u.sampled, i < last - 1 ? 1 : 0); });} else if (p.type === 'feGaussianBlur') {const [sx, sy = sx] = String(attrs.stdDeviation || '0').trim().split(/[\s,]+/).map((value) => num(value));const blurX = Boolean(kernelTexture(sx)), blurY = Boolean(kernelTexture(sy));@@ -687,7 +717,7 @@ void main() {blurAxis(temp.texture, sy, 1, target, region);temp.busy = false;} else if (blurX || blurY) blurAxis(a, blurX ? sx : sy, blurX ? 0 : 1, target, region);- else run('offset', target, (u, bind) => { clipUniform(u, region); bind('a', a); gl.uniform2f(u.shift, 0, 0); });+ else run('offset', target, (u, bind) => { clipUniform(u, region); bind('a', a); gl.uniform2f(u.shift, 0, 0); gl.uniform1i(u.sampled, 0); });} else if (p.type === 'feColorMatrix') {let values;if ((attrs.type || 'matrix') === 'saturate') {diff --git a/server/lib/filmLookGpu.test.js b/server/lib/filmLookGpu.test.jsindex 0291bbd61d..872657b0b6 100644--- a/server/lib/filmLookGpu.test.js+++ b/server/lib/filmLookGpu.test.js@@ -34,15 +34,28 @@ describe('film look on the GPU', () => {}});- it('blurs with Skia\'s three-box kernel, which skips a one-pixel window', () => {- // σ 0.7 rounds to a one-pixel box: Chrome leaves the axis untouched.+ it('matches Chrome small-blur impulse pixels and keeps normalized large-blur box sums', () => {expect(blurKernel(0)).toBeNull();- expect(blurKernel(0.7)).toBeNull();- // σ 1 → d = 2: boxes of 2, 2 and 3 over 12, offset so the whole kernel stays centred.- const small = blurKernel(1);- expect(small.divisor).toBe(12);- expect(Array.from(small.weights, (w) => Math.round(w * 12))).toEqual([1, 3, 4, 3, 1]);- expect(small.origin).toBe(2);+ expect(blurKernel(0.03)).toBeNull();+ // Chrome 155 SVG: a white one-pixel column over black, sampled across its+ // centre after feGaussianBlur stdDeviation="sigma 0", using the hardware+ // compositor. Software compositing below instead uses the raster boxes.+ for (const [sigma, bytes] of [+ [0.7361, [0, 3, 55, 139, 55, 3, 0]],+ [0.9276, [1, 11, 61, 110, 61, 11, 1]],+ ]) {+ const kernel = blurKernel(sigma);+ expect(kernel.origin).toBe(3);+ expect(kernel.weights).toHaveLength(bytes.length);+ kernel.weights.forEach((weight, i) => expect(Math.abs(Math.round(weight * 255) - bytes[i])).toBeLessThanOrEqual(1));+ }+ expect(blurKernel(0.7361, 'box')).toBeNull();+ expect(Array.from(blurKernel(0.9276, 'box').weights, w => Math.round(w * 255))).toEqual([21, 64, 85, 64, 21]);+ // σ 2.24 → d = 4: two boxes of 4 and one of 5, centred over 80.+ const box = blurKernel(2.24);+ expect(box.divisor).toBe(80);+ expect(Array.from(box.weights, (w) => Math.round(w * 80))).toEqual([1, 3, 6, 10, 13, 14, 13, 10, 6, 3, 1]);+ expect(box.origin).toBe(5);for (const sigma of [3.1378, 20.46, 43.648, 110.484]) {const { weights, origin, boxes } = blurKernel(sigma);expect(weights.reduce((sum, w) => sum + w, 0)).toBeCloseTo(1, 5);@@ -68,4 +81,15 @@ describe('film look on the GPU', () => {expect(window.__portosFilmLookGpu.available()).toBe(false);expect(() => window.__portosFilmLookGpu.create({ getContext: () => null })).toThrow(/WebGL2/);});++ it('refuses an unknown blur response before allocating shader resources', () => {+ const window = {};+ let lost = false;+ const context = { filter: 'none', fillRect() {}, drawImage() {}, getImageData: () => ({ data: new Uint8Array(28) }) };+ runInNewContext(filmLookGpuRuntimeSource(), { globalThis: window,+ document: { createElement: () => ({ getContext: () => context }) } });+ const gl = { getExtension: name => name === 'EXT_color_buffer_float' ? {} : { loseContext: () => { lost = true; } } };+ expect(() => window.__portosFilmLookGpu.create({ getContext: () => gl })).toThrow(/keep the SVG film look/);+ expect(lost).toBe(true);+ });});