-
Notifications
You must be signed in to change notification settings - Fork 4
/
program.js
63 lines (44 loc) · 1.41 KB
/
program.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
'use strict'
module.exports = setProgram;
function setProgram (gl, vSrc, fSrc) {
if (!gl) throw Error('WebGL context is not provided')
// TODO: delete program by passing null
// TODO: keep track of used programs to avoid this request below, think how
// if just getProgram
if (!vSrc && !fSrc) {
return gl.getParameter(gl.CURRENT_PROGRAM)
}
// if WebGLProgram directly - enable program
if (vSrc && typeof vSrc !== 'string') {
gl.useProgram(vSrc)
if (!vSrc.gl) vSrc.gl = gl
return vSrc
}
// otherwise create and enable program
// TODO: cache shaders
// TODO: provide gl on a program
if (!vSrc || !fSrc) throw Error('Vertex/fragment source is not provided')
var fShader = gl.createShader(gl.FRAGMENT_SHADER)
var vShader = gl.createShader(gl.VERTEX_SHADER)
gl.shaderSource(fShader, fSrc)
gl.shaderSource(vShader, vSrc)
gl.compileShader(fShader)
if (!gl.getShaderParameter(fShader, gl.COMPILE_STATUS)) {
throw Error(gl.getShaderInfoLog(fShader))
}
gl.compileShader(vShader)
if (!gl.getShaderParameter(vShader, gl.COMPILE_STATUS)) {
throw Error(gl.getShaderInfoLog(vShader))
}
var program = gl.createProgram()
gl.attachShader(program, vShader)
gl.attachShader(program, fShader)
gl.linkProgram(program)
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw Error(gl.getProgramInfoLog(program))
}
gl.useProgram(program)
// save gl reference
program.gl = gl
return program;
}