Something went wrong. Try again.
doll.systems community website
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116import type { PageScope } from './page-scope';type UniformValue = number | Float32List;type Pixels = { width: number; height: number; pixels: ArrayBufferView | null };
// A page owns the GPU resources it creates; shader declarations own uniform names.export function webgl( canvas: HTMLCanvasElement, scope: PageScope, vertex: string, fragment: string,) { const gl = canvas.getContext('webgl', { antialias: false, depth: false, stencil: false, powerPreference: 'low-power', }); if (!gl) return; const program = gl.createProgram()!; for (const [type, source] of [ [gl.VERTEX_SHADER, vertex], [gl.FRAGMENT_SHADER, fragment], ] as const) { const shader = gl.createShader(type)!; gl.shaderSource(shader, source); gl.compileShader(shader); gl.attachShader(program, shader); gl.deleteShader(shader); } gl.linkProgram(program); if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { gl.deleteProgram(program); return; } gl.useProgram(program); const uniformTypes: Record< number, (location: WebGLUniformLocation, value: UniformValue) => void > = { [gl.FLOAT]: (location, value) => gl.uniform1f(location, value as number), [gl.FLOAT_VEC2]: (location, value) => gl.uniform2fv(location, value as Float32List), [gl.FLOAT_VEC4]: (location, value) => gl.uniform4fv(location, value as Float32List), [gl.INT]: (location, value) => gl.uniform1i(location, value as number), [gl.SAMPLER_2D]: (location, value) => gl.uniform1i(location, value as number), }; const uniforms = Object.fromEntries( Array.from({ length: gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS) }, (_, i) => { const { name, type } = gl.getActiveUniform(program, i)!; const location = gl.getUniformLocation(program, name)!; return [name, (value: UniformValue) => uniformTypes[type](location, value)]; }), ); const textures: WebGLTexture[] = []; const buffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buffer); scope.defer(() => { textures.forEach((texture) => gl.deleteTexture(texture)); gl.deleteBuffer(buffer); gl.deleteProgram(program); gl.getExtension('WEBGL_lose_context')?.loseContext(); }); return { gl, set(values: Record<string, UniformValue>) { for (const name in values) uniforms[name]?.(values[name]); }, attribute(name: string, size: number, stride = 0, offset = 0) { const location = gl.getAttribLocation(program, name); gl.enableVertexAttribArray(location); gl.vertexAttribPointer( location, size, gl.FLOAT, false, stride * Float32Array.BYTES_PER_ELEMENT, offset * Float32Array.BYTES_PER_ELEMENT, ); }, texture(name: string, unit = 0, filter: number = gl.NEAREST) { const texture = gl.createTexture()!; textures.push(texture); const bind = () => { gl.activeTexture(gl.TEXTURE0 + unit); gl.bindTexture(gl.TEXTURE_2D, texture); }; bind(); for (const parameter of [gl.TEXTURE_MIN_FILTER, gl.TEXTURE_MAG_FILTER]) gl.texParameteri(gl.TEXTURE_2D, parameter, filter); for (const parameter of [gl.TEXTURE_WRAP_S, gl.TEXTURE_WRAP_T]) gl.texParameteri(gl.TEXTURE_2D, parameter, gl.CLAMP_TO_EDGE); uniforms[name]?.(unit); return (source: TexImageSource | Pixels, { update = false, flip = false } = {}) => { bind(); gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flip); gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1); if ('pixels' in source) { const format = source.pixels ? gl.LUMINANCE : gl.RGBA; gl.texImage2D( gl.TEXTURE_2D, 0, format, source.width, source.height, 0, format, gl.UNSIGNED_BYTE, source.pixels, ); } else if (update) gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, source); else gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source); }; }, };}