1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import { createOffscreenCanvas } from "../canvas-utils";
let gl: WebGLRenderingContext | null = null;
let webglCanvas: OffscreenCanvas | HTMLCanvasElement | null = null;
const programCache = new Map<string, WebGLProgram>();
export function getWebGLContext({
width,
height,
}: {
width: number;
height: number;
}): {
context: WebGLRenderingContext;
programCache: Map<string, WebGLProgram>;
} {
if (!webglCanvas) {
webglCanvas = createOffscreenCanvas({ width, height });
gl = webglCanvas.getContext("webgl", {
premultipliedAlpha: false,
}) as WebGLRenderingContext | null;
if (!gl) throw new Error("WebGL not supported");
}
if (webglCanvas.width !== width || webglCanvas.height !== height) {
webglCanvas.width = width;
webglCanvas.height = height;
}
if (!gl) throw new Error("WebGL context lost");
return { context: gl, programCache };
}
export function readResult({
width,
height,
}: {
width: number;
height: number;
}): OffscreenCanvas | HTMLCanvasElement {
if (!webglCanvas) throw new Error("WebGL canvas not initialized");
const outputCanvas = createOffscreenCanvas({ width, height });
const outputCtx = outputCanvas.getContext("2d") as
| CanvasRenderingContext2D
| OffscreenCanvasRenderingContext2D
| null;
if (outputCtx) {
outputCtx.drawImage(webglCanvas, 0, 0, width, height);
}
return outputCanvas;
}
|