-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprogram.js
More file actions
89 lines (73 loc) · 2.18 KB
/
program.js
File metadata and controls
89 lines (73 loc) · 2.18 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
// Copyright 2014 Robert Scott Dionne. All rights reserved.
/**
* @constructor
*/
bouncingball.Program = function(var_args) {
this.name = Array.prototype.map.call(arguments, function(shader) {
return shader.name
}, this).join(':');
/**
* @type {Array.<bouncingball.Shader>}
*/
this.shaders_ = Array.prototype.slice.call(arguments);
};
bouncingball.Program.EXTRACT_ARRAY = /(.*)\[.*/;
bouncingball.Program.prototype.getSafeName = function(name) {
var match = name.match(bouncingball.Program.EXTRACT_ARRAY);
if (match) {
return match[1];
} else {
return name;
}
};
bouncingball.Program.prototype.defineUniforms = function(gl) {
var uniforms = gl.getProgramParameter(this.handle, gl.ACTIVE_UNIFORMS);
for (var i = 0; i < uniforms; ++i) {
var name = this.getSafeName(gl.getActiveUniform(this.handle, i).name);
this[name] = gl.getUniformLocation(this.handle, name);
}
};
bouncingball.Program.prototype.defineAttributes = function(gl) {
var attribs = gl.getProgramParameter(this.handle, gl.ACTIVE_ATTRIBUTES);
for (var i = 0; i < attribs; ++i) {
var name = this.getSafeName(gl.getActiveAttrib(this.handle, i).name);
this[name] = gl.getAttribLocation(this.handle, name);
}
};
/**
* @param {WebGLRenderingContext} gl
*/
bouncingball.Program.prototype.create = function(gl) {
this.shaders_.forEach(function(shader) {
shader.create(gl);
}, this);
this.handle = gl.createProgram();
};
/**
* @param {WebGLRenderingContext} gl
*/
bouncingball.Program.prototype.dispose = function(gl) {
this.shaders_.forEach(function(shader) {
gl.detachShader(this.handle, shader.handle);
shader.dispose(gl);
}, this);
gl.deleteProgram(this.handle);
this.handle = null;
};
/**
* @param {WebGLRenderingContext} gl
*/
bouncingball.Program.prototype.link = function(gl) {
this.shaders_.forEach(function(shader) {
shader.compile(gl);
gl.attachShader(this.handle, shader.handle);
}, this);
gl.linkProgram(this.handle);
if (!gl.getProgramParameter(this.handle, gl.LINK_STATUS)) {
var log = gl.getProgramInfoLog(this.handle);
this.dispose(gl);
throw new Error(log);
}
this.defineUniforms(gl);
this.defineAttributes(gl);
};