From 2e13fa16ef6655c2e3603ad395ed2876dafdcbad Mon Sep 17 00:00:00 2001 From: Jeffrey Alan Scudder Date: Tue, 27 Jan 2026 19:38:03 +0000 Subject: [PATCH] feat: add WebGL composite renderer --- docs/webgl-migration.md | 47 ++++ system/public/aesthetic.computer/bios.mjs | 241 +++++++++++++++--- .../aesthetic.computer/lib/webgl-blit.mjs | 203 +++++++++++++++ 3 files changed, 458 insertions(+), 33 deletions(-) create mode 100644 docs/webgl-migration.md create mode 100644 system/public/aesthetic.computer/lib/webgl-blit.mjs diff --git a/docs/webgl-migration.md b/docs/webgl-migration.md new file mode 100644 index 0000000000..d1e4c1f5f4 --- /dev/null +++ b/docs/webgl-migration.md @@ -0,0 +1,47 @@ +# WebGL Migration (Aesthetic Computer) + +## Goal +Move the rendering pipeline from Canvas2D compositing to a WebGL-based composite path. + +## Status +- [x] Add WebGL blitter module (full-screen quad + texture upload) +- [x] Wire WebGL composite canvas into BIOS render path +- [x] Move overlay compositing off the main canvas +- [x] Preserve recording outputs with WebGL composite +- [x] Preserve screenshot outputs with WebGL composite +- [ ] Verify underlay/tape playback behavior +- [ ] Validate WebGPU path still works (no conflicts) +- [x] Add runtime flag/toggle and telemetry +- [ ] Performance profiling in sandboxed iframes + +## Subtasks + +### 1) WebGL Blitter (Base Frame) +- [x] Create module to upload `ImageData` as a texture and draw full-screen quad +- [x] Add canvas sizing + visibility toggles in `bios.mjs` +- [x] Replace `ctx.putImageData` with WebGL blit when enabled + +### 2) Overlay Compositing +- [x] Add dedicated overlay canvas layer +- [x] Route overlay painters to overlay canvas when WebGL composite enabled +- [x] Ensure overlay canvas clears each frame to avoid accumulation + +### 3) Recording / Screenshot Capture +- [x] Composite WebGL canvas + overlay canvas into offscreen capture canvas +- [x] Preserve existing ordering (HUD before tape progress) +- [x] Ensure clean screenshots exclude overlays + +### 4) Runtime Flags + Fallbacks +- [x] Add `content.webglCompositeEnabled` flag path +- [x] Add `globalThis.acUseWebGLComposite` override +- [x] Fallback to Canvas2D if WebGL init fails + +### 5) Validation +- [ ] objkt sandbox: compare FPS vs Canvas2D path +- [ ] Regular browser: confirm no regressions +- [ ] Recording output: verify overlays + tape progress timing +- [ ] Glaze path: ensure compatibility with composite pipeline + +## Notes +- WebGL composite should coexist with existing WebGPU path; if `content.webgpuEnabled` is true, WebGL composite should stay off. +- Keep UI canvas (cursor/spinner) above overlays to avoid GPU text rendering complexity. diff --git a/system/public/aesthetic.computer/bios.mjs b/system/public/aesthetic.computer/bios.mjs index 5e65b9586f..27dce90e02 100644 --- a/system/public/aesthetic.computer/bios.mjs +++ b/system/public/aesthetic.computer/bios.mjs @@ -42,6 +42,7 @@ import { timestamp, radians } from "./lib/num.mjs"; import * as graph from "./lib/graph.mjs"; import * as WebGPU from "./lib/webgpu.mjs"; import { initGPU, switchBackend } from "./lib/gpu/index.mjs"; // ๐ŸŽจ New backend system (auto-registers backends) +import { createWebGLBlitter } from "./lib/webgl-blit.mjs"; // import * as TwoD from "./lib/2d.mjs"; // ๐Ÿ†• 2D GPU Renderer. const TwoD = undefined; @@ -67,6 +68,11 @@ const isOpaqueOrigin = (() => { // Export to globalThis so pieces can detect sandbox mode globalThis.acIsSandboxed = isOpaqueOrigin; +// Default to WebGL composite unless explicitly disabled +if (globalThis.acUseWebGLComposite === undefined) { + globalThis.acUseWebGLComposite = true; +} + // Log once if we detect sandbox restrictions if (isOpaqueOrigin) { console.warn('โš ๏ธ Running in sandboxed iframe (opaque origin) - some features disabled, performance may be reduced'); @@ -952,6 +958,16 @@ async function boot(parsed, bpm = 60, resolution, debug) { webgpuCanvas.style.pointerEvents = "none"; // Let events pass through to main canvas webgpuCanvas.style.display = "none"; // Hidden by default until WebGPU is enabled + // ๐Ÿ–ฅ๏ธ๐ŸŽจ WebGL Composite Canvas (blitted pixel buffer) + const webglCompositeCanvas = document.createElement("canvas"); + webglCompositeCanvas.dataset.type = "webgl-composite"; + webglCompositeCanvas.style.position = "absolute"; + webglCompositeCanvas.style.top = "0"; + webglCompositeCanvas.style.left = "0"; + webglCompositeCanvas.style.zIndex = "0"; + webglCompositeCanvas.style.pointerEvents = "none"; + webglCompositeCanvas.style.display = "none"; + // ๏ฟฝ DOM-based Stats Overlay (always on top of everything) const statsOverlay = document.createElement("div"); statsOverlay.id = "ac-stats-overlay"; @@ -977,6 +993,10 @@ async function boot(parsed, bpm = 60, resolution, debug) { let statsFrameCount = 0; let statsLastTime = performance.now(); + let webglBlitter = null; + let captureCompositeCanvas = null; + let captureCompositeCtx = null; + function updateStatsOverlay() { if (!statsOverlayEnabled) return; const now = performance.now(); @@ -995,6 +1015,22 @@ async function boot(parsed, bpm = 60, resolution, debug) { statsOverlay.textContent = lines.join("\n"); } + function shouldUseWebGLComposite(content) { + if (!content) return false; + if (content.webgpuEnabled) return false; + return content.webglCompositeEnabled === true || globalThis.acUseWebGLComposite === true; + } + + function ensureCaptureCompositeCanvas(width, height) { + if (!captureCompositeCanvas) { + captureCompositeCanvas = document.createElement("canvas"); + captureCompositeCtx = captureCompositeCanvas.getContext("2d"); + } + if (captureCompositeCanvas.width !== width) captureCompositeCanvas.width = width; + if (captureCompositeCanvas.height !== height) captureCompositeCanvas.height = height; + return captureCompositeCtx; + } + // ๏ฟฝ๐Ÿ–ฅ๏ธ๐Ÿ”Œ Secondary main display surface. (3D GPU Renderer) // TODO: Eventually deprecate the software renderer in favor of this? // (Or even reuse the same tag if pieces swap.) @@ -1046,6 +1082,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { // A buffer for corner label overlays. const overlayCan = document.createElement("canvas"); const octx = overlayCan.getContext("2d"); + overlayCan.style.position = "absolute"; + overlayCan.style.top = "0"; + overlayCan.style.left = "0"; + overlayCan.style.zIndex = "2"; + overlayCan.style.pointerEvents = "none"; + overlayCan.style.display = "none"; // Reusable canvas for dirtyBox updates let dirtyBoxCanvas = document.createElement("canvas"); @@ -1267,6 +1309,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { webgpuCanvas.width = width; webgpuCanvas.height = height; + // Resize WebGL composite and overlay canvases to match + webglCompositeCanvas.width = width; + webglCompositeCanvas.height = height; + overlayCan.width = width; + overlayCan.height = height; + // Restore the clean pixels onto the resized canvas ctx.drawImage(tempCanvas, 0, 0); @@ -1453,8 +1501,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { // Canvases also fill 100% in nogap mode to avoid subpixel rounding gaps canvas.style.width = "100%"; canvas.style.height = "100%"; + webglCompositeCanvas.style.width = "100%"; + webglCompositeCanvas.style.height = "100%"; webgpuCanvas.style.width = "100%"; webgpuCanvas.style.height = "100%"; + overlayCan.style.width = "100%"; + overlayCan.style.height = "100%"; uiCanvas.style.width = "100%"; uiCanvas.style.height = "100%"; } else if (TikTok) { @@ -1465,8 +1517,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { canvas.style.width = projectedWidth + "px"; canvas.style.height = projectedHeight + "px"; + webglCompositeCanvas.style.width = projectedWidth + "px"; + webglCompositeCanvas.style.height = projectedHeight + "px"; webgpuCanvas.style.width = projectedWidth + "px"; webgpuCanvas.style.height = projectedHeight + "px"; + overlayCan.style.width = projectedWidth + "px"; + overlayCan.style.height = projectedHeight + "px"; uiCanvas.style.width = projectedWidth + "px"; uiCanvas.style.height = projectedHeight + "px"; } else { @@ -1477,8 +1533,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { canvas.style.width = projectedWidth + "px"; canvas.style.height = projectedHeight + "px"; + webglCompositeCanvas.style.width = projectedWidth + "px"; + webglCompositeCanvas.style.height = projectedHeight + "px"; webgpuCanvas.style.width = projectedWidth + "px"; webgpuCanvas.style.height = projectedHeight + "px"; + overlayCan.style.width = projectedWidth + "px"; + overlayCan.style.height = projectedHeight + "px"; uiCanvas.style.width = projectedWidth + "px"; uiCanvas.style.height = projectedHeight + "px"; } @@ -1511,12 +1571,14 @@ async function boot(parsed, bpm = 60, resolution, debug) { if (!wrapper.contains(canvas)) { perf.markBoot("canvas-setup-start"); wrapper.append(canvas); + wrapper.append(webglCompositeCanvas); wrapper.append(modal); const bumper = document.createElement("div"); bumper.id = "bumper"; modal.append(bumper); + wrapper.append(overlayCan); wrapper.append(uiCanvas); if (debug) wrapper.append(debugCanvas); wrapper.append(webgpuCanvas); // Add WebGPU canvas (initially hidden) @@ -1531,6 +1593,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { }); } + if (!webglBlitter) { + webglBlitter = createWebGLBlitter(webglCompositeCanvas); + const ok = webglBlitter.init(); + if (!ok) webglBlitter = null; + } + const fonts = [ "berkeley-mono-variable.css", "ywft-processing-regular.css", @@ -1611,9 +1679,6 @@ async function boot(parsed, bpm = 60, resolution, debug) { // }); } - // document.fonts.ready.then(function () {}); - - // Trigger it to re-draw whenever the window resizes. let timeout; let lastWidth = window.innerWidth; let lastHeight = window.innerHeight; @@ -16235,6 +16300,10 @@ async function boot(parsed, bpm = 60, resolution, debug) { pixelsDidChange = content.paintChanged || false; + const useWebGLComposite = shouldUseWebGLComposite(content); + const webglCompositeActive = useWebGLComposite && webglBlitter?.isReady(); + let overlayTargetCtx = webglCompositeActive ? octx : ctx; + // โœจ UI Overlay (Composite) Layer // This currently paints corner labels and tape progress bars only. // (So they can be skipped for recordings?) @@ -16443,7 +16512,7 @@ async function boot(parsed, bpm = 60, resolution, debug) { // Paint overlay to main canvas (ctx is the main canvas context) if (name === "tapeProgressBar") { - ctx.drawImage(canvas, o.x, o.y); + overlayTargetCtx.drawImage(canvas, o.x, o.y); } else { // Add debug logging for durationTimecode if (name === "durationTimecode") { @@ -16455,7 +16524,7 @@ async function boot(parsed, bpm = 60, resolution, debug) { console.log(`๐Ÿ” Drawing qrFullscreenLabel to main canvas at (${o.x}, ${o.y}) with size ${canvas.width}x${canvas.height}`); } - ctx.drawImage(canvas, o.x, o.y); + overlayTargetCtx.drawImage(canvas, o.x, o.y); if (name === "durationTimecode") { console.log(`๐Ÿ• Finished drawing durationTimecode`); @@ -16553,8 +16622,33 @@ async function boot(parsed, bpm = 60, resolution, debug) { const db = content.dirtyBox; if (db) { - ctx.drawImage(dirtyBoxBitmapCan, db.x, db.y); - if (glaze.on) Glaze.update(dirtyBoxBitmapCan, db.x, db.y); + if (webglCompositeActive && imageData) { + if (canvas.style.visibility !== "hidden") { + canvas.style.visibility = "hidden"; + } + if (webglCompositeCanvas.style.display === "none") { + webglCompositeCanvas.style.display = "block"; + } + if (overlayCan.style.display === "none") { + overlayCan.style.display = "block"; + } + if (webgpuCanvas.style.display !== "none") { + webgpuCanvas.style.display = "none"; + } + webglBlitter.render(imageData); + } else { + if (canvas.style.visibility !== "visible") { + canvas.style.visibility = "visible"; + } + if (webglCompositeCanvas.style.display !== "none") { + webglCompositeCanvas.style.display = "none"; + } + if (overlayCan.style.display !== "none") { + overlayCan.style.display = "none"; + } + ctx.drawImage(dirtyBoxBitmapCan, db.x, db.y); + if (glaze.on) Glaze.update(dirtyBoxBitmapCan, db.x, db.y); + } } else if ( pixelsDidChange || needs$creenshot || @@ -16601,7 +16695,28 @@ async function boot(parsed, bpm = 60, resolution, debug) { if (webgpuCanvas.style.display === "none") { webgpuCanvas.style.display = "block"; } + if (webglCompositeCanvas.style.display !== "none") { + webglCompositeCanvas.style.display = "none"; + } + if (overlayCan.style.display !== "none") { + overlayCan.style.display = "none"; + } skipImmediateOverlays = true; // Overlays will be handled separately + } else if (webglCompositeActive) { + if (canvas.style.visibility !== "hidden") { + canvas.style.visibility = "hidden"; + } + if (webglCompositeCanvas.style.display === "none") { + webglCompositeCanvas.style.display = "block"; + } + if (overlayCan.style.display === "none") { + overlayCan.style.display = "block"; + } + if (webgpuCanvas.style.display !== "none") { + webgpuCanvas.style.display = "none"; + } + webglBlitter.render(imageData); + skipImmediateOverlays = false; } else if (underlayFrame) { if (canvas.style.visibility !== "visible") { canvas.style.visibility = "visible"; @@ -16609,6 +16724,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { if (webgpuCanvas.style.display !== "none") { webgpuCanvas.style.display = "none"; } + if (webglCompositeCanvas.style.display !== "none") { + webglCompositeCanvas.style.display = "none"; + } + if (overlayCan.style.display !== "none") { + overlayCan.style.display = "none"; + } // Force sync rendering during tape playback for immediate UI updates ctx.putImageData(imageData, 0, 0); } else if (!forceSynchronousRendering && window.pixelOptimizer && window.pixelOptimizer.asyncRenderingSupported) { @@ -16619,6 +16740,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { if (webgpuCanvas.style.display !== "none") { webgpuCanvas.style.display = "none"; } + if (webglCompositeCanvas.style.display !== "none") { + webglCompositeCanvas.style.display = "none"; + } + if (overlayCan.style.display !== "none") { + overlayCan.style.display = "none"; + } // Non-blocking async rendering window.pixelOptimizer.renderImageDataAsync(imageData, ctx, 0, 0).then(() => { // Paint overlays after async fallback rendering completes @@ -16642,6 +16769,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { if (webgpuCanvas.style.display !== "none") { webgpuCanvas.style.display = "none"; } + if (webglCompositeCanvas.style.display !== "none") { + webglCompositeCanvas.style.display = "none"; + } + if (overlayCan.style.display !== "none") { + overlayCan.style.display = "none"; + } ctx.putImageData(imageData, 0, 0); skipImmediateOverlays = false; } @@ -16652,6 +16785,12 @@ async function boot(parsed, bpm = 60, resolution, debug) { if (webgpuCanvas.style.display !== "none") { webgpuCanvas.style.display = "none"; } + if (webglCompositeCanvas.style.display !== "none") { + webglCompositeCanvas.style.display = "none"; + } + if (overlayCan.style.display !== "none") { + overlayCan.style.display = "none"; + } ctx.putImageData(imageData, 0, 0); skipImmediateOverlays = false; } @@ -16736,15 +16875,33 @@ async function boot(parsed, bpm = 60, resolution, debug) { // ๐Ÿ“ธ Capture clean screenshot data BEFORE overlays are painted (only when needed) let cleanScreenshotData = null; if (needs$creenshot) { - cleanScreenshotData = ctx.getImageData( - 0, - 0, - ctx.canvas.width, - ctx.canvas.height, - ); + if (webglCompositeActive && webglCompositeCanvas) { + const capCtx = ensureCaptureCompositeCanvas( + ctx.canvas.width, + ctx.canvas.height, + ); + capCtx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); + capCtx.drawImage(webglCompositeCanvas, 0, 0); + cleanScreenshotData = capCtx.getImageData( + 0, + 0, + ctx.canvas.width, + ctx.canvas.height, + ); + } else { + cleanScreenshotData = ctx.getImageData( + 0, + 0, + ctx.canvas.width, + ctx.canvas.height, + ); + } } // Paint overlays (but exclude tape progress from recordings) + if (webglCompositeActive) { + octx.clearRect(0, 0, octx.canvas.width, octx.canvas.height); + } // console.log("๐ŸŽจ Available overlay painters:", Object.keys(paintOverlays)); let tapeProgressPainter = paintOverlays["tapeProgressBar"] || null; @@ -16790,29 +16947,29 @@ async function boot(parsed, bpm = 60, resolution, debug) { const utcLine1 = `UTC ${content.merryUTC.time}`; const utcLine2 = `cycle ${content.merryUTC.cycle}s ยท ${content.merryUTC.index}/${content.merryUTC.total} ยท ${content.merryUTC.piece}`; - ctx.save(); - ctx.globalAlpha = 0.95; - ctx.font = `12px YWFTProcessing-Regular, Arial, sans-serif`; - ctx.textBaseline = "top"; + overlayTargetCtx.save(); + overlayTargetCtx.globalAlpha = 0.95; + overlayTargetCtx.font = `12px YWFTProcessing-Regular, Arial, sans-serif`; + overlayTargetCtx.textBaseline = "top"; const pad = 6; const lineH = 14; - const w1 = ctx.measureText(utcLine1).width; - const w2 = ctx.measureText(utcLine2).width; + const w1 = overlayTargetCtx.measureText(utcLine1).width; + const w2 = overlayTargetCtx.measureText(utcLine2).width; const boxW = Math.ceil(Math.max(w1, w2) + pad * 2); const boxH = Math.ceil(lineH * 2 + pad * 2); - const x = Math.max(0, ctx.canvas.width - boxW - 8); + const x = Math.max(0, overlayTargetCtx.canvas.width - boxW - 8); const y = 8; // Backplate - ctx.fillStyle = "rgba(0, 0, 0, 0.55)"; - ctx.fillRect(x, y, boxW, boxH); + overlayTargetCtx.fillStyle = "rgba(0, 0, 0, 0.55)"; + overlayTargetCtx.fillRect(x, y, boxW, boxH); // Text - ctx.fillStyle = "white"; - ctx.fillText(utcLine1, x + pad, y + pad); - ctx.fillText(utcLine2, x + pad, y + pad + lineH); - ctx.restore(); + overlayTargetCtx.fillStyle = "white"; + overlayTargetCtx.fillText(utcLine1, x + pad, y + pad); + overlayTargetCtx.fillText(utcLine2, x + pad, y + pad + lineH); + overlayTargetCtx.restore(); } catch (e) { // Silent fail: debug-only overlay } @@ -16864,12 +17021,29 @@ async function boot(parsed, bpm = 60, resolution, debug) { (Number.isFinite(window.recordingStartTimestamp) ? window.recordingStartTimestamp : Date.now()) + relativeTimestamp; - const frameDataWithHUD = ctx.getImageData( - 0, - 0, - ctx.canvas.width, - ctx.canvas.height, - ); + let frameDataWithHUD; + if (webglCompositeActive && webglCompositeCanvas) { + const capCtx = ensureCaptureCompositeCanvas( + ctx.canvas.width, + ctx.canvas.height, + ); + capCtx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); + capCtx.drawImage(webglCompositeCanvas, 0, 0); + capCtx.drawImage(overlayCan, 0, 0); + frameDataWithHUD = capCtx.getImageData( + 0, + 0, + ctx.canvas.width, + ctx.canvas.height, + ); + } else { + frameDataWithHUD = ctx.getImageData( + 0, + 0, + ctx.canvas.width, + ctx.canvas.height, + ); + } // Capture pen position data for crosshair rendering during export // Only capture pen data if cursor is within canvas bounds @@ -16931,7 +17105,8 @@ async function boot(parsed, bpm = 60, resolution, debug) { if (glaze.on) { ThreeD?.pasteTo(glazeCompositeCtx); - glazeCompositeCtx.drawImage(canvas, 0, 0); + const glazeSource = webglCompositeActive ? webglCompositeCanvas : canvas; + glazeCompositeCtx.drawImage(glazeSource, 0, 0); Glaze.update(glazeComposite); } diff --git a/system/public/aesthetic.computer/lib/webgl-blit.mjs b/system/public/aesthetic.computer/lib/webgl-blit.mjs new file mode 100644 index 0000000000..ecbe059cd6 --- /dev/null +++ b/system/public/aesthetic.computer/lib/webgl-blit.mjs @@ -0,0 +1,203 @@ +// ๐ŸŽจ WebGL Blitter +// Uploads RGBA pixel buffers into a WebGL2 texture and renders a full-screen quad + +import { log } from "./logs.mjs"; + +export function createWebGLBlitter(canvas) { + let gl = null; + let program = null; + let vao = null; + let vbo = null; + let texture = null; + let uResolutionLoc = null; + let uTextureLoc = null; + let currentWidth = 0; + let currentHeight = 0; + let ready = false; + + const vertexSource = `#version 300 es + precision highp float; + in vec2 a_position; + out vec2 v_uv; + void main() { + v_uv = (a_position + 1.0) * 0.5; + gl_Position = vec4(a_position, 0.0, 1.0); + } + `; + + const fragmentSource = `#version 300 es + precision highp float; + in vec2 v_uv; + uniform sampler2D u_texture; + out vec4 outColor; + void main() { + outColor = texture(u_texture, vec2(v_uv.x, 1.0 - v_uv.y)); + } + `; + + function compileShader(source, type) { + const shader = gl.createShader(type); + gl.shaderSource(shader, source); + gl.compileShader(shader); + if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { + const err = gl.getShaderInfoLog(shader); + gl.deleteShader(shader); + throw new Error(err || "Shader compile failed"); + } + return shader; + } + + function createProgram() { + const vs = compileShader(vertexSource, gl.VERTEX_SHADER); + const fs = compileShader(fragmentSource, gl.FRAGMENT_SHADER); + const prog = gl.createProgram(); + gl.attachShader(prog, vs); + gl.attachShader(prog, fs); + gl.linkProgram(prog); + gl.deleteShader(vs); + gl.deleteShader(fs); + if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { + const err = gl.getProgramInfoLog(prog); + gl.deleteProgram(prog); + throw new Error(err || "Program link failed"); + } + return prog; + } + + function ensureTextureSize(width, height) { + if (!texture) { + texture = gl.createTexture(); + gl.bindTexture(gl.TEXTURE_2D, texture); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); + } + + if (width !== currentWidth || height !== currentHeight) { + currentWidth = width; + currentHeight = height; + gl.bindTexture(gl.TEXTURE_2D, texture); + gl.texImage2D( + gl.TEXTURE_2D, + 0, + gl.RGBA, + width, + height, + 0, + gl.RGBA, + gl.UNSIGNED_BYTE, + null, + ); + } + } + + return { + init() { + gl = canvas.getContext("webgl2", { + alpha: false, + antialias: false, + depth: false, + stencil: false, + premultipliedAlpha: false, + preserveDrawingBuffer: false, + powerPreference: "high-performance", + }); + + if (!gl) { + log.gpu.warn?.("WebGL Blitter: WebGL2 not supported"); + return false; + } + + try { + program = createProgram(); + vao = gl.createVertexArray(); + vbo = gl.createBuffer(); + + gl.bindVertexArray(vao); + gl.bindBuffer(gl.ARRAY_BUFFER, vbo); + gl.bufferData( + gl.ARRAY_BUFFER, + new Float32Array([ + -1, -1, + 1, -1, + -1, 1, + -1, 1, + 1, -1, + 1, 1, + ]), + gl.STATIC_DRAW, + ); + + const posLoc = gl.getAttribLocation(program, "a_position"); + gl.enableVertexAttribArray(posLoc); + gl.vertexAttribPointer(posLoc, 2, gl.FLOAT, false, 0, 0); + gl.bindVertexArray(null); + + uTextureLoc = gl.getUniformLocation(program, "u_texture"); + uResolutionLoc = gl.getUniformLocation(program, "u_resolution"); + + gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1); + gl.disable(gl.BLEND); + + ready = true; + log.gpu.success?.("WebGL Blitter initialized"); + return true; + } catch (err) { + log.gpu.error?.("WebGL Blitter init failed:", err); + ready = false; + return false; + } + }, + + isReady() { + return ready; + }, + + render(imageData) { + if (!ready || !imageData || !imageData.data) return false; + + const width = imageData.width; + const height = imageData.height; + if (!width || !height) return false; + + ensureTextureSize(width, height); + + gl.viewport(0, 0, width, height); + gl.useProgram(program); + gl.bindVertexArray(vao); + gl.activeTexture(gl.TEXTURE0); + gl.bindTexture(gl.TEXTURE_2D, texture); + gl.texSubImage2D( + gl.TEXTURE_2D, + 0, + 0, + 0, + width, + height, + gl.RGBA, + gl.UNSIGNED_BYTE, + imageData.data, + ); + gl.uniform1i(uTextureLoc, 0); + if (uResolutionLoc) gl.uniform2f(uResolutionLoc, width, height); + gl.drawArrays(gl.TRIANGLES, 0, 6); + gl.bindVertexArray(null); + return true; + }, + + destroy() { + if (!gl) return; + if (texture) gl.deleteTexture(texture); + if (vbo) gl.deleteBuffer(vbo); + if (vao) gl.deleteVertexArray(vao); + if (program) gl.deleteProgram(program); + gl = null; + texture = null; + vbo = null; + vao = null; + program = null; + ready = false; + }, + }; +} -- knotmirror 0.0.1