|
| 1 | +/** |
| 2 | + * Tests for the Dawn/WebGPU shader-blend compositor. |
| 3 | + * |
| 4 | + * We can't depend on a working GPU adapter in CI — the Linux sandbox has |
| 5 | + * no Vulkan driver. So these tests focus on the surface that must work |
| 6 | + * regardless of host: |
| 7 | + * |
| 8 | + * 1. `HF_DAWN_FORCE_FAIL=1` short-circuits init to a clean failure (the |
| 9 | + * env hook the CLI / worker rely on for fallback testability). |
| 10 | + * 2. `initGpuCompositor()` never throws. On a no-GPU host it returns |
| 11 | + * `{ ok: false, reason }` and the caller can fall back without |
| 12 | + * try/catch. |
| 13 | + * 3. When a GPU IS available (Vance's Mac, Linux+GPU), the compositor's |
| 14 | + * crossfade output matches the CPU canonical path within PSNR ≥ 50dB. |
| 15 | + * This branch is skipped when init fails — the test logs the reason |
| 16 | + * instead so a regression on Mac surfaces cleanly without breaking CI |
| 17 | + * elsewhere. |
| 18 | + * |
| 19 | + * Determinism note: we deliberately do NOT pin byte-equality with the CPU |
| 20 | + * shader. The whole point of the new path is f32 GPU math + u16 storage, |
| 21 | + * which differs from f64 CPU math at the LSB. PSNR is the right pin. |
| 22 | + */ |
| 23 | + |
| 24 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 25 | +import { crossfade } from "@hyperframes/engine/shader-transitions"; |
| 26 | +import { initGpuCompositor } from "./shaderTransitionGpu.js"; |
| 27 | + |
| 28 | +const WIDTH = 32; |
| 29 | +const HEIGHT = 16; |
| 30 | +const PX = WIDTH * HEIGHT; |
| 31 | +const BYTES = PX * 6; |
| 32 | + |
| 33 | +function fillGradient(): Buffer { |
| 34 | + const buf = Buffer.alloc(BYTES); |
| 35 | + for (let i = 0; i < PX; i++) { |
| 36 | + const o = i * 6; |
| 37 | + buf.writeUInt16LE((i * 1024) & 0xffff, o); |
| 38 | + buf.writeUInt16LE(((i * 2048) & 0xffff) ^ 0xa5a5, o + 2); |
| 39 | + buf.writeUInt16LE(((i * 4096) & 0xffff) ^ 0x5a5a, o + 4); |
| 40 | + } |
| 41 | + return buf; |
| 42 | +} |
| 43 | + |
| 44 | +function fillSolid(r: number, g: number, b: number): Buffer { |
| 45 | + const buf = Buffer.alloc(BYTES); |
| 46 | + for (let i = 0; i < PX; i++) { |
| 47 | + const o = i * 6; |
| 48 | + buf.writeUInt16LE(r, o); |
| 49 | + buf.writeUInt16LE(g, o + 2); |
| 50 | + buf.writeUInt16LE(b, o + 4); |
| 51 | + } |
| 52 | + return buf; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Peak signal-to-noise ratio in dB between two rgb48le buffers (16-bit |
| 57 | + * channel depth → MAX = 65535). >= 50 dB is the acceptance bar for the |
| 58 | + * GPU path (still visually indistinguishable from f64 canonical; passes |
| 59 | + * the eye / objective metric for transition rendering). |
| 60 | + */ |
| 61 | +function psnrDb(a: Buffer, b: Buffer): number { |
| 62 | + if (a.length !== b.length) throw new Error("buffer length mismatch"); |
| 63 | + const samples = a.length / 2; |
| 64 | + let sse = 0; |
| 65 | + for (let i = 0; i < samples; i++) { |
| 66 | + const av = a.readUInt16LE(i * 2); |
| 67 | + const bv = b.readUInt16LE(i * 2); |
| 68 | + const d = av - bv; |
| 69 | + sse += d * d; |
| 70 | + } |
| 71 | + if (sse === 0) return Infinity; |
| 72 | + const mse = sse / samples; |
| 73 | + const MAX = 65535; |
| 74 | + return 10 * Math.log10((MAX * MAX) / mse); |
| 75 | +} |
| 76 | + |
| 77 | +describe("shaderTransitionGpu", () => { |
| 78 | + const originalForceFail = process.env.HF_DAWN_FORCE_FAIL; |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + // Each test below sets its own value; reset between tests so they don't |
| 82 | + // bleed state. The module caches the loadWebgpu() promise, but each |
| 83 | + // suite-level test runs in a fresh vitest worker file so the cache is |
| 84 | + // only shared within a single `describe` — fine for these tests. |
| 85 | + delete process.env.HF_DAWN_FORCE_FAIL; |
| 86 | + }); |
| 87 | + |
| 88 | + afterEach(() => { |
| 89 | + if (originalForceFail === undefined) { |
| 90 | + delete process.env.HF_DAWN_FORCE_FAIL; |
| 91 | + } else { |
| 92 | + process.env.HF_DAWN_FORCE_FAIL = originalForceFail; |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + it("HF_DAWN_FORCE_FAIL short-circuits to a clean failure", async () => { |
| 97 | + process.env.HF_DAWN_FORCE_FAIL = "1"; |
| 98 | + const result = await initGpuCompositor(); |
| 99 | + expect(result.ok).toBe(false); |
| 100 | + if (!result.ok) { |
| 101 | + expect(result.reason).toMatch(/HF_DAWN_FORCE_FAIL/); |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + it("returns ok:false (never throws) on hosts without a GPU adapter", async () => { |
| 106 | + // No assertion on which branch we hit — we just assert the call never |
| 107 | + // throws and returns a structured result. On Vance's Mac this will |
| 108 | + // typically be `ok: true`; on the Linux sandbox it'll be |
| 109 | + // `{ ok: false, reason: "no GPU adapter..." }` or the |
| 110 | + // module-not-installed branch. Both are correct. |
| 111 | + const result = await initGpuCompositor(); |
| 112 | + expect(typeof result).toBe("object"); |
| 113 | + if (result.ok) { |
| 114 | + expect(typeof result.compositor.supportsShader).toBe("function"); |
| 115 | + expect(result.compositor.supportsShader("crossfade")).toBe(true); |
| 116 | + expect(result.compositor.supportsShader("not-a-real-shader")).toBe(false); |
| 117 | + await result.compositor.dispose(); |
| 118 | + } else { |
| 119 | + expect(typeof result.reason).toBe("string"); |
| 120 | + expect(result.reason.length).toBeGreaterThan(0); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + it("crossfade output matches CPU canonical within PSNR >= 50dB when a GPU is available", async () => { |
| 125 | + const result = await initGpuCompositor(); |
| 126 | + if (!result.ok) { |
| 127 | + // Skipped — host has no GPU. Log so a regression on Mac (where the |
| 128 | + // adapter SHOULD be available) is visible in the test output. |
| 129 | + // eslint-disable-next-line no-console |
| 130 | + console.log(`[shaderTransitionGpu.test] GPU branch skipped: ${result.reason}`); |
| 131 | + return; |
| 132 | + } |
| 133 | + const compositor = result.compositor; |
| 134 | + try { |
| 135 | + const from = fillGradient(); |
| 136 | + const to = fillSolid(40000, 5000, 25000); |
| 137 | + const outGpu = Buffer.alloc(BYTES); |
| 138 | + const outCpu = Buffer.alloc(BYTES); |
| 139 | + await compositor.blend("crossfade", from, to, outGpu, WIDTH, HEIGHT, 0.5); |
| 140 | + crossfade(from, to, outCpu, WIDTH, HEIGHT, 0.5); |
| 141 | + const psnr = psnrDb(outGpu, outCpu); |
| 142 | + // eslint-disable-next-line no-console |
| 143 | + console.log(`[shaderTransitionGpu.test] crossfade PSNR vs CPU: ${psnr.toFixed(2)} dB`); |
| 144 | + expect(psnr).toBeGreaterThanOrEqual(50); |
| 145 | + } finally { |
| 146 | + await compositor.dispose(); |
| 147 | + } |
| 148 | + }); |
| 149 | +}); |
0 commit comments