-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebGLUtils.js
More file actions
270 lines (253 loc) · 8.29 KB
/
WebGLUtils.js
File metadata and controls
270 lines (253 loc) · 8.29 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
(function(global) {
/*
* Receives a canvas and a list of context keys, then tries them
* one by one to create a context with a specified set of options.
*
* Receives:
* - HTMLCanvasElement
* - Array<String> (context keys, e.g. 'webgl', 'experimental-webgl')
* - String (option name) -> * (option value)
*
* Returns:
* - RenderingContext
*/
function getContext(canvas, keys, options) {
var context;
for (var i = 0; i < keys.length; i++) {
try {
context = canvas.getContext(keys[i], options);
} catch (e) {
}
if (context) {
return context;
}
}
throw new Error('Cannot create WebGL context');
}
/*
* Receives a shader source string and returns a compiled
* shader object.
*
* Receives:
* - WebGLRenderingContext
* - String (shader source)
* - Number (shader type)
*
* Returns:
* - WebGLShader
*/
function createShader(gl, source, type) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
var status = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
if (!status) {
var log = gl.getShaderInfoLog(shader);
throw new Error('Cannot compile shader\nInfo log:\n' + log);
}
return shader;
}
/*
* Receives a list of shader objects and returns the linked program
* with two maps of its active attribute and uniform locations.
*
* Receives:
* - WebGLRenderingContext
* - Array<WebGLShader>
*
* Returns:
* - program: WebGLProgram
* - attributes: String (attribute name) -> WebGLAttributeLocation
* - uniforms: String (uniform name) -> WebGLUniformLocation
*/
function createProgram(gl, shaders) {
var program = gl.createProgram();
for (var i = 0; i < shaders.length; i++) {
gl.attachShader(program, shaders[i]);
}
gl.linkProgram(program);
var status = gl.getProgramParameter(program, gl.LINK_STATUS);
if (!status) {
var log = gl.getProgramInfoLog(program);
throw new Error('Cannot link program\nInfo log:\n' + log);
}
for (var i = 0; i < shaders.length; i++) {
gl.deleteShader(shaders[i]);
}
var attributes = {};
var activeAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
for (var i = 0; i < activeAttributes; i++) {
var info = gl.getActiveAttrib(program, i);
attributes[info.name] = gl.getAttribLocation(program, info.name);
}
var uniforms = {};
var activeUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (var i = 0; i < activeUniforms; i++) {
var info = gl.getActiveUniform(program, i);
uniforms[info.name] = gl.getUniformLocation(program, info.name);
}
return {
program: program,
attributes: attributes,
uniforms: uniforms
};
}
/*
* Receives a map of program sources and mixins and returns a map
* of linked WebGLProgram objects. This implementation assumes a
* vertex and a fragment shader for each program.
* Wherever a '@name' is encountered in a shader, it is replaced with
* a mixin with the same 'name'.
*
* Receives:
* - WebGLRenderingContext
* - String (program name) -> String (vertex|fragment) -> String (shader source)
* - String (mixin name) -> String (mixin source)
*
* Returns:
* - String (program name) -> WebGLProgram
*/
function compileShaders(gl, shaders, mixins) {
var cooked = {};
Object.keys(shaders).forEach(function(name) {
cooked[name] = {};
var types = shaders[name];
Object.keys(types).forEach(function(type) {
cooked[name][type] = types[type].replace(/@([a-zA-Z0-9]+)/g, function(_, mixin) {
return mixins[mixin];
});
});
});
var programs = {};
Object.keys(cooked).forEach(function(name) {
try {
programs[name] = createProgram(gl, [
createShader(gl, cooked[name].vertex, gl.VERTEX_SHADER),
createShader(gl, cooked[name].fragment, gl.FRAGMENT_SHADER)
]);
} catch (e) {
throw new Error('Error compiling ' + name + '\n' + e);
}
});
return programs;
}
/*
* Receives a set of options and (optionally) some data and creates
* a texture in the TEXTURE_2D target of the active texture unit.
* An image can be supplied or a typed array, or data may be missing to
* just allocate with the given dimensions.
*
* Receives:
* - WebGLRenderingContext
* - String (option name) -> * (option value)
* - internalFormat (internal texel format)
* - format (texel format)
* - type (texel datatype)
* - image or data (image, video, canvas, typed array etc.)
* - width (only when data is present or both image and data are missing)
* - height (only when data is present or both image and data are missing)
* - wrapS (texture wrapping mode for the S coordinate)
* - wrapT (texture wrapping mode for the T coordinate)
* - min (minification filter)
* - mag (magnification filter)
*
* Returns:
* - WebGLTexture
*/
function createTexture(gl, options) {
var target = options.target || gl.TEXTURE_2D;
var internalFormat = options.internalFormat || gl.RGBA;
var format = options.format || gl.RGBA;
var type = options.type || gl.UNSIGNED_BYTE;
var texture = gl.createTexture();
gl.bindTexture(target, texture);
if (options.image) {
gl.texImage2D(target, 0, internalFormat, format, type, options.image);
} else { // if options.data == null, just allocate
gl.texImage2D(target, 0, internalFormat, options.width, options.height, 0, format, type, options.data);
}
if (options.wrapS) { gl.texParameteri(target, gl.TEXTURE_WRAP_S, options.wrapS); }
if (options.wrapT) { gl.texParameteri(target, gl.TEXTURE_WRAP_T, options.wrapT); }
if (options.min) { gl.texParameteri(target, gl.TEXTURE_MIN_FILTER, options.min); }
if (options.mag) { gl.texParameteri(target, gl.TEXTURE_MAG_FILTER, options.mag); }
gl.bindTexture(target, null);
return texture;
}
/*
* Receives a set of options and dimensions and creates a framebuffer with
* just a color attachment (without depth and stencil attachments).
* Useful for e.g. simulating compute shaders.
*
* Receives:
* - WebGLRenderingContext
* - String (option name) -> * (option value)
* - texture creation options (see createTexture)
* - width
* - height
*
* Returns:
* - framebuffer: WebGLFramebuffer
* - texture: WebGLTexture (color attachment)
* - width: Number
* - height: Number
*/
function createSimpleRenderTarget(gl, options) {
var texture = createTexture(gl, options);
var framebuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
return {
framebuffer: framebuffer,
texture: texture,
width: options.width,
height: options.height
};
}
/*
* Creates a unit quad [(0,0), (1,0), (1,1), (0,1)] with a STATIC_DRAW hint
* that can be drawn with gl.drawArrays(gl.TRIANGLE_FAN, 0, 4)
*
* Receives:
* - WebGLRenderingContext
*
* Returns:
* - WebGLBuffer
*/
function createUnitQuad(gl) {
var buffer = gl.createBuffer();
var data = new Float32Array([0, 0, 1, 0, 1, 1, 0, 1]);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return buffer;
}
/*
* Creates a clip quad [(-1,-1), (1,-1), (1,1), (-1,1)] with a STATIC_DRAW hint
* that can be drawn with gl.drawArrays(gl.TRIANGLE_FAN, 0, 4)
*
* Receives:
* - WebGLRenderingContext
*
* Returns:
* - WebGLBuffer
*/
function createClipQuad(gl) {
var buffer = gl.createBuffer();
var data = new Float32Array([-1, -1, 1, -1, 1, 1, -1, 1]);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return buffer;
}
var WebGLUtils = global.WebGLUtils = {
getContext: getContext,
createShader: createShader,
createProgram: createProgram,
compileShaders: compileShaders,
createTexture: createTexture,
createSimpleRenderTarget: createSimpleRenderTarget,
createUnitQuad: createUnitQuad,
createClipQuad: createClipQuad
};
})(this);