-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.js
More file actions
133 lines (129 loc) · 4.99 KB
/
Program.js
File metadata and controls
133 lines (129 loc) · 4.99 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
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
function createShader(ctx, type, src) {
const s = ctx.createShader(type);
ctx.shaderSource(s, src);
ctx.compileShader(s);
if (!ctx.getShaderParameter(s, ctx.COMPILE_STATUS))
throw "error compile " + (type === ctx.VERTEX_SHADER? "vertex": "fragment") + " shader: "
+ ctx.getShaderInfoLog(s);
return s;
}
function createProgram(ctx, vertShader, fragShader) {
const p = ctx.createProgram();
ctx.attachShader(p, vertShader);
ctx.attachShader(p, fragShader);
ctx.linkProgram(p);
if (!ctx.getProgramParameter(p, ctx.LINK_STATUS))
throw "error link program: " + ctx.getProgramInfoLog(p);
return p;
}
class Program {
constructor(ctx, vertSrc, fragSrc) {
this.ctx = this.gl = ctx;
let vs = createShader(ctx, ctx.VERTEX_SHADER, vertSrc),
fs = createShader(ctx, ctx.FRAGMENT_SHADER, fragSrc);
this.shaders = { vs, fs };
let prog = this.prog = this.program = createProgram(ctx, vs, fs);
const getCurrentVars = (varsType, aou = varsType === ctx.ACTIVE_ATTRIBUTES? "Attrib": "Uniform") =>
[...Array(ctx.getProgramParameter(prog, varsType))]
.map((_, i) => {
const {size, type, name} = ctx["getActive" + aou](prog, i),
loc = ctx[`get${aou}Location`](prog, name);
return {size, type, name: name.split("[")[0], loc};
})
.reduce((ac, {name, size, type, loc}) => {
ac[name] = {name, size, type, loc};
return ac;
}, {});
this.vars = {
atts: getCurrentVars(ctx.ACTIVE_ATTRIBUTES),
unis: getCurrentVars(ctx.ACTIVE_UNIFORMS)
};
};
use() { this.ctx.useProgram(this.prog); return this; };
getAtt(name) { return this.vars.atts[name].loc; };
getUni(name) { return this.vars.unis[name].loc; };
setAtt(name, bufferData, size = undefined, attDataType = this.ctx.FLOAT, normalized = false, stride = 0, offset = 0) {
const ctx = this.ctx, att = this.vars.atts[name];
if (!att) throw "Cannot get attribute " + name;
if (size === undefined) {
switch (att.type) {
case ctx.FLOAT:
size = 1; break;
case ctx.FLOAT_VEC2: case ctx.FLOAT_MAT2:
size = 2; break;
case ctx.FLOAT_VEC3: case ctx.FLOAT_MAT3:
size = 3; break;
case ctx.FLOAT_VEC4: case ctx.FLOAT_MAT4:
size = 4; break;
default:
console.error("Don't know gl type", att.type, "for attribute", att.name);
throw "Don't know attribute type";
}
}
let bufferType = bufferData.type || ctx.ARRAY_BUFFER;
ctx.bindBuffer(bufferType, bufferData);
ctx.enableVertexAttribArray(att.loc);
ctx.vertexAttribPointer(att.loc, size, attDataType, normalized, stride, offset);
ctx.bindBuffer(bufferType, null);
return this;
};
setUni(name, value) {
const ctx = this.ctx, uni = this.vars.unis[name];
switch (uni.type) {
case ctx.FLOAT_MAT4:
ctx.uniformMatrix4fv(uni.loc, false, value);
break;
case ctx.FLOAT_MAT3:
ctx.uniformMatrix3fv(uni.loc, false, value);
break;
case ctx.FLOAT_MAT2:
ctx.uniformMatrix2fv(uni.loc, false, value);
break;
case ctx.FLOAT:
ctx.uniform1f(uni.loc, value);
break;
case ctx.INT: case ctx.SAMPLER_CUBE: case ctx.SAMPLER_2D:
case ctx.SAMPLER_2D_ARRAY:
ctx.uniform1i(uni.loc, value);
break;
case ctx.FLOAT_VEC2:
ctx.uniform2fv(uni.loc, value);
break;
case ctx.FLOAT_VEC3:
ctx.uniform3fv(uni.loc, value);
break;
case ctx.FLOAT_VEC4:
ctx.uniform4fv(uni.loc, value);
break;
case ctx.INT_VEC2:
ctx.uniform2iv(uni.loc, value);
break;
case ctx.INT_VEC3:
ctx.uniform3iv(uni.loc, value);
break;
case ctx.INT_VEC4:
ctx.uniform4iv(uni.loc, value);
break;
default:
console.warn("Don't know gl type", uni.type, "for uniform", uni.name);
throw "Don't know uniform type";
}
return this;
};
bindTex(uniName, tex, texType = tex.type || ctx.TEXTURE_2D, unit = 0) {
const ctx = this.ctx;
ctx.activeTexture(ctx.TEXTURE0 + unit);
ctx.bindTexture(texType, tex);
return this.setUni(uniName, unit);
};
dispose() {
const {ctx} = this;
ctx.deleteShader(this.shaders.vs);
ctx.deleteShader(this.shaders.fs);
ctx.deleteProgram(this.program);
};
};
export {
Program,
Program as default
};