-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgl.ts
More file actions
81 lines (60 loc) · 1.94 KB
/
Copy pathwebgl.ts
File metadata and controls
81 lines (60 loc) · 1.94 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as twgl from 'twgl.js'
import { SumResult } from './types'
const canvas = new OffscreenCanvas(1, 1)
const gl = canvas.getContext('webgl2')
if (!gl) {
throw new Error('WebGL 2 is not available')
}
twgl.addExtensionsToContext(gl)
export const setupSumWebGL = (input: Uint32Array) => {
const idealDimension = Math.log2(Math.sqrt(input.length))
const lod = Math.ceil(idealDimension)
const width = 2 ** lod
const height = 2 ** Math.floor(idealDimension)
const vertexShader = /* glsl */ `#version 300 es
in vec2 inPosition;
void main() {
gl_Position = vec4(inPosition, 0, 1);
}
`
const fragmentShader = /* glsl */ `#version 300 es
precision highp float;
uniform sampler2D xTex;
out uint y;
void main() {
y = uint(texelFetch(xTex, ivec2(gl_FragCoord), ${lod}).r * ${input.length}.0);
}
`
const programInfo = twgl.createProgramInfo(gl, [vertexShader, fragmentShader])
const bufferInfo = twgl.createBufferInfoFromArrays(gl, {
inPosition: [-1, -1, 0, 1, -1, 0, -1, 1, 0, -1, 1, 0, 1, -1, 0, 1, 1, 0],
})
const texture = twgl.createTexture(gl, {
internalFormat: gl.R32F,
src: new Float32Array(input),
width,
height,
})
const fbi = twgl.createFramebufferInfo(
gl,
[{ internalFormat: gl.R32UI }],
1,
1,
)
const output = new Uint32Array(4)
const sumWebGL = (): SumResult => {
const start = performance.now()
gl.bindTexture(gl.TEXTURE_2D, texture)
gl.generateMipmap(gl.TEXTURE_2D)
gl.viewport(0, 0, 1, 1)
gl.bindFramebuffer(gl.FRAMEBUFFER, fbi.framebuffer)
gl.useProgram(programInfo.program)
twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo)
twgl.setUniforms(programInfo, { xTex: texture })
twgl.drawBufferInfo(gl, bufferInfo)
gl.readPixels(0, 0, 1, 1, gl.RGBA_INTEGER, gl.UNSIGNED_INT, output)
const time = performance.now() - start
return { result: output[0], time }
}
return sumWebGL
}