-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.js
More file actions
283 lines (251 loc) · 8.05 KB
/
program.js
File metadata and controls
283 lines (251 loc) · 8.05 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
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
// Copyright 2012 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Defines the Program class which is the base class for all
* embeded shader definitions.
*
*/
goog.provide('embedsl.Program');
goog.require('embedsl.Ast');
goog.require('embedsl.Definition');
goog.require('embedsl.DefinitionKind');
goog.require('embedsl.Expression');
goog.require('embedsl.GLSLGenerator');
goog.require('embedsl.Inferrer');
goog.require('embedsl.Kind');
goog.require('embedsl.Localizer');
goog.require('embedsl.Mapper');
goog.require('embedsl.Plumber');
goog.require('embedsl.Purger');
/**
* Base class for an embedded shader program definition. The class defines all
* built-in global variables available to a shader program. Derived classes can
* add application specific uniform and attribute variables.
*
* Provides access to GLSL 1.0 built-in inputs and constants.
*
* @constructor
*/
embedsl.Program = function() {
/**
* Output variables used by this shader.
*
* @type {!Object.<string,!embedsl.Definition>}
*/
this.outputs = {};
/** @type {embedsl.Ast} */
this.ast = null;
/** @type {WebGLProgram} */
this.glProgram = null;
/** @type {Object.<string,{location:WebGLUniformLocation,version:number}>}
* @private
*/
this.uniformLocations_ = {};
/** @type {Object.<string,number>}
* @private
*/
this.attributeLocations_ = {};
};
/**
* Define the value of the gl_Position variable.
*
* @param {!embedsl.Expression} def Defining expression.
*/
embedsl.Program.prototype.gl_Position = function(def) {
this.outputs['gl_Position'] =
new embedsl.Definition(embedsl.DefinitionKind.BUILTIN,
'vertex', embedsl.Type.Vec4, 'gl_Position', def);
};
/**
* Define the value of the gl_FragColor variable.
*
* @param {!embedsl.Expression} def Defining expression.
*/
embedsl.Program.prototype.gl_FragColor = function(def) {
this.outputs['gl_FragColor'] =
new embedsl.Definition(embedsl.DefinitionKind.BUILTIN,
'fragment', embedsl.Type.Vec4, 'gl_FragColor', def);
};
/**
* Add a mixin to a progam instance. Applies a constructor function to
* the existing shader definition.
*
* @param {function(?boolean)} mixin Override all definitions found in the
* mixin.
* @return {embedsl.Program} Extended shader program.
*/
embedsl.Program.prototype.mixin = function(mixin) {
mixin.call(this, true);
return this;
};
/**
* Inheritance with automatic conditional invocation of the base class
* constructor function. This is compatible with closure library's
* base.inherits() method and thus with static type checking.
*
* @param {function()} ext Constructor function of the extending shader
* definiton.
* @param {function()} base Constructor function of the extended shader
* definiton.
* @return {function(?boolean)} Resulting constructor function.
*/
embedsl.Program.extend = function(ext, base) {
// A fresh constructor function that handles calling of the base
// constructor functions.
var ctor = function(mixin) {
// Don't call the base constructor if this is just mixed in.
if (!mixin) base.call(this);
ext.call(this);
};
/**
* Normal proto chain construction.
*
* @constructor
*/
function tempCtor() {};
tempCtor.prototype = base.prototype;
ctor.superClass_ = base.prototype;
ctor.prototype = new tempCtor();
ctor.prototype.constructor = ctor;
return ctor;
};
/**
* Compile the program in-place to GLSL source code. Allows set() functions on
* uniform properties to be type checked.
*
* @param {Object=} options Compile options.
* @return {!embedsl.Program} The program.
*/
embedsl.Program.prototype.compile = function(options) {
var opts = {'rename': false};
goog.object.extend(opts, options || {});
var passes = [
new embedsl.Inferrer(),
new embedsl.Localizer(),
// TODO(tramberend) Find strange bug in purger. Until then, no optimization.
// new embedsl.Purger(),
new embedsl.Mapper(),
new embedsl.Plumber(),
new embedsl.GLSLGenerator(opts['rename'])
];
this.ast = new embedsl.Ast(this.outputs, this);
for (var i in passes)
this.ast = passes[i].transformAst(this.ast);
return this;
};
/**
* Inject a hand crafted ast into the compilation chain. Strictly for debugging.
*
* @param {!embedsl.Ast} ast Hand crafted ast.
* @return {!embedsl.Program} The program.
*/
embedsl.Program.prototype.inject = function(ast) {
this.ast = ast;
return this;
};
/**
* Returns the uniform location object for the named uniform.
*
* @param {string} name Uniform name.
* @return {WebGLUniformLocation} The uniform location.
*/
embedsl.Program.prototype.uniformLocationByName = function(name) {
var l = this.uniformLocations_[name];
return l ? l.location : null;
};
/**
* Returns the attribute index for the named attribute.
*
* @param {string} name Attribute name.
* @return {number} The attribute index.
*/
embedsl.Program.prototype.attributeLocationByName = function(name) {
var i = this.attributeLocations_[name];
return i == undefined ? -1 : i;
};
/**
* Use the program in a WebGL context. Compiles the GLSL source code on the
* first call.
*
* @param {WebGLRenderingContext} gl Rendering context.
*/
embedsl.Program.prototype.use = function(gl) {
if (!this.ast)
throw 'Compile embedsl program before use.';
if (!this.glProgram)
this.compileAndLinkProgram_(gl);
gl.useProgram(this.glProgram);
// Pull the current values from the uniforms.
for (var name in this.ast.uniforms) {
var uniform = this.ast.uniforms[name];
var location = this.uniformLocations_[name];
// But only if neccessary.
if (location.version < uniform.version)
uniform.apply(gl, location.location);
location.version = uniform.version;
}
};
// TODO(tramberend) Remove all dependencies on WebGL from expression definition
// and compilation to enable AOT compilation with V8 at build time.
/**
* Compile GLSL shader.
*
* @param {WebGLRenderingContext} gl Rendering context.
* @param {number} type Shader type.
* @param {string} source GLSL source code.
* @return {WebGLShader} Compiled shader.
* @private
*/
embedsl.Program.prototype.compileShader_ = function(gl, type, source) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
// TODO(tramberend) Build more useful error handling.
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
throw gl.getShaderInfoLog(shader) + '\n' + source;
return shader;
};
/**
* Compile and link GLSL shader definiton.
*
* @param {WebGLRenderingContext} gl Rendering context.
* @private
*/
embedsl.Program.prototype.compileAndLinkProgram_ = function(gl) {
this.glProgram = gl.createProgram();
gl.attachShader(
this.glProgram,
this.compileShader_(
gl, gl.VERTEX_SHADER, this.ast.vertexShader));
gl.attachShader(
this.glProgram,
this.compileShader_(
gl, gl.FRAGMENT_SHADER, this.ast.fragmentShader));
// Bind attribute locations, if specified.
var attribs = this.ast.attributes;
for (var a in attribs)
attribs[a].bindLocation(gl, this.glProgram);
gl.linkProgram(this.glProgram);
if (!gl.getProgramParameter(this.glProgram, gl.LINK_STATUS))
throw gl.getProgramInfoLog(this.glProgram);
// Get uniform locations.
for (var name in this.ast.uniforms)
this.uniformLocations_[name] = {
location: gl.getUniformLocation(this.glProgram, name),
version: 0
};
// Get attribute locations.
for (var name in this.ast.attributes)
this.attributeLocations_[name] = gl.getAttribLocation(this.glProgram, name);
};