-
Notifications
You must be signed in to change notification settings - Fork 501
/
WebGL2.js
329 lines (282 loc) · 9.63 KB
/
WebGL2.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import vertexShaderSource from './webgl/vertexShader.glsl'
class WebGL2 {
constructor() {
this.isSupported = false
this.vertexShader = null
if (typeof window !== 'undefined') {
this.canvas = document.createElement('canvas')
this.context = this.canvas.getContext('webgl2')
const gl = this.context
if (gl) {
this.isSupported = true
gl.getExtension('EXT_color_buffer_float')
this.MAX_TEXTURE_SIZE = gl.getParameter(gl.MAX_TEXTURE_SIZE)
this.MAX_TEXTURE_IMAGE_UNITS = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS)
this.init()
} else {
console.log('Unable to initialize WebGL2 -- your browser may not support it.')
}
}
this._refs = { textures: [], buffers: [] }
}
/**
* Intialization after WebGL2 detected.
*/
init() {
this.createCommonVertexShader()
}
/**
* Creates and compiles passthrough vertex shader that we will attach
* to all our programs.
*/
createCommonVertexShader() {
const gl = this.context
const vertexShader = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(vertexShader, vertexShaderSource)
gl.compileShader(vertexShader)
const success = gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)
if (!success) {
console.error(gl.getShaderInfoLog(vertexShader))
gl.deleteShader(vertexShader)
this.isSupported = false
}
this.vertexShader = vertexShader
}
/**
* Compiles fragment shader from source and creates program from it,
* using our passthrough vertex shader.
*
* @param {string} source - fragment shader GLSL source code
* @returns {WebGLProgram}
*/
compileProgram(source) {
const gl = this.context
// create and compile fragment shader
const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER)
gl.shaderSource(fragmentShader, source)
gl.compileShader(fragmentShader)
let success = gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)
if (!success) {
console.error(gl.getShaderInfoLog(fragmentShader))
gl.deleteShader(fragmentShader)
this.isSupported = false
}
// create program and attach compiled shaders
const program = gl.createProgram()
gl.attachShader(program, this.vertexShader)
gl.attachShader(program, fragmentShader)
gl.linkProgram(program)
success = gl.getProgramParameter(program, gl.LINK_STATUS)
if (!success) {
console.error(gl.getProgramInfoLog(program))
this.isSupported = false
}
this.setupVertices(program)
return program
}
/**
* Setup vertices
*
* @param {WebGLProgram} program
*/
setupVertices(program) {
const gl = this.context
const position = gl.getAttribLocation(program, 'position')
const positionVertexObj = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, positionVertexObj)
this.storeRef('buffer', positionVertexObj)
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array([-1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0]),
gl.STATIC_DRAW
)
gl.vertexAttribPointer(position, 3, gl.FLOAT, false, 0, 0)
gl.enableVertexAttribArray(position)
const texcoord = gl.getAttribLocation(program, 'texcoord')
const texcoordVertexObj = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, texcoordVertexObj)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]), gl.STATIC_DRAW)
gl.vertexAttribPointer(texcoord, 2, gl.FLOAT, false, 0, 0)
gl.enableVertexAttribArray(texcoord)
this.storeRef('buffer', texcoordVertexObj)
const indicesVertexObj = gl.createBuffer()
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indicesVertexObj)
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0, 1, 2, 0, 2, 3]), gl.STATIC_DRAW)
this.storeRef('buffer', indicesVertexObj)
}
/**
* Selects linked program as active program
*
* @param {WebGLProgram} program
*/
selectProgram(program) {
const gl = this.context
gl.useProgram(program)
}
/**
* Bind uniforms within program
*
* @param {WebGLProgram} program
* @param {Object[]} uniforms
*/
bindUniforms(program, uniforms) {
const gl = this.context
uniforms.forEach(({ value, type, name }) => {
const loc = gl.getUniformLocation(program, name)
if (type === 'float') {
gl.uniform1f(loc, value)
} else if (type === 'int' || type === 'bool') {
gl.uniform1i(loc, value)
}
})
}
/**
* Bind input textures within program, handling inputs that are fragmented
*
* @param {WebGLProgram} program
* @param {Object[]} inputs
* @param {number} [k]
*/
bindInputTextures(program, inputs, k) {
const gl = this.context
inputs.forEach(({ input, name }, i) => {
gl.activeTexture(gl.TEXTURE0 + i)
if (input.glTextureFragments) {
if (input.glTextureFragmentsAsColStack) {
const { textureTarget } = this.getWebGLTextureOptions(input.glTextureType, input.glTextureFormat)
gl.bindTexture(textureTarget, input.glTextureFragmentsAsColStack)
} else {
const { textureTarget } = this.getWebGLTextureOptions(input.glTextureType, input.glTextureFormat)
gl.bindTexture(textureTarget, input.glTextureFragments[k])
}
} else {
const { textureTarget } = this.getWebGLTextureOptions(input.glTextureType, input.glTextureFormat)
gl.bindTexture(textureTarget, input.glTexture)
}
gl.uniform1i(gl.getUniformLocation(program, name), i)
})
}
/**
* Bind output texture
*
* @param {WebGLTexture} outputTexture
* @param {number[]} shape
*/
bindOutputTexture(outputTexture, shape) {
const gl = this.context
gl.viewport(0, 0, shape[1], shape[0])
this.framebuffer = this.framebuffer || gl.createFramebuffer()
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer)
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, outputTexture, 0)
}
/**
* Runs fragment shader program, after binding output, inputs, and uniforms
*
* @param {WebGLProgram} options.program
* @param {Tensor} options.output
* @param {Object[]} options.inputs
* @param {Object[]} options.uniforms
*/
runProgram({ program, output, inputs, uniforms, supportsTextureFragments = false }) {
if (!program) throw new Error('[WebGL2] missing program')
if (!output) throw new Error('[WebGL2] missing output')
if (!inputs) throw new Error('[WebGL2] missing inputs')
const gl = this.context
this.selectProgram(program)
if (uniforms && Array.isArray(uniforms)) {
this.bindUniforms(program, uniforms)
}
if (output.glTextureFragments) {
if (!supportsTextureFragments) {
throw new Error('[WebGL2] program does not support texture fragments')
}
const inputsWithFragments = inputs.filter(
obj => obj.input.glTextureFragments && !obj.input.glTextureFragmentsAsColStack
)
const numFragments = output.glTextureFragments.length
if (inputsWithFragments.some(obj => obj.input.glTextureFragments.length !== numFragments)) {
throw new Error('[WebGL2] number of texture fragments in inputs and output do not match')
}
for (let k = 0; k < numFragments; k++) {
this.bindOutputTexture(output.glTextureFragments[k], output.glTextureFragmentShape)
this.bindInputTextures(program, inputs, k)
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0)
}
} else {
this.bindOutputTexture(output.glTexture, output.glTextureShape)
this.bindInputTextures(program, inputs)
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0)
}
}
/**
* Reads pixel data from framebuffer
*
* @param {number[]} shape
* @returns {Float32Array}
*/
readData(shape) {
const gl = this.context
const buf = new ArrayBuffer(shape[0] * shape[1] * 4 * 4)
const view = new Float32Array(buf)
gl.readPixels(0, 0, shape[1], shape[0], gl.RGBA, gl.FLOAT, view)
const out = []
for (let i = 0; i < view.length; i += 4) {
out.push(view[i])
}
return new Float32Array(out)
}
/**
* Gets WebGLTexture options constants
*/
getWebGLTextureOptions(type, format) {
const gl = this.context
const targetMap = {
'2d': gl.TEXTURE_2D,
'2d_array': gl.TEXTURE_2D_ARRAY,
'3d': gl.TEXTURE_3D
}
const internalFormatMap = {
float: gl.R32F,
int: gl.R32I
}
const formatMap = {
float: gl.RED,
int: gl.RED_INTEGER
}
const typeMap = {
float: gl.FLOAT,
int: gl.INT
}
const textureTarget = targetMap[type]
const textureInternalFormat = internalFormatMap[format]
const textureFormat = formatMap[format]
const textureType = typeMap[format]
return { textureTarget, textureInternalFormat, textureFormat, textureType }
}
/**
* Store reference to WebGL texture or buffer on class instance, useful for when we want to delete later
*
* @param {string} type
* @param {WebGLTexture|WebGLBuffer} obj
*/
storeRef(type, obj) {
if (type === 'texture') {
this._refs.textures.push(obj)
} else if (type === 'buffer') {
this._refs.buffers.push(obj)
}
}
/**
* Deletes all stored references to WebGL textures and buffers
*/
clearRefs() {
const gl = this.context
this._refs.textures.forEach(texture => gl.deleteTexture(texture))
this._refs.buffers.forEach(buffer => gl.deleteBuffer(buffer))
this._refs = { textures: [], buffers: [] }
}
}
const webgl2 = new WebGL2()
const MAX_TEXTURE_SIZE = webgl2.MAX_TEXTURE_SIZE
const MAX_TEXTURE_IMAGE_UNITS = webgl2.MAX_TEXTURE_IMAGE_UNITS
export { webgl2, MAX_TEXTURE_SIZE, MAX_TEXTURE_IMAGE_UNITS }