-
-
Notifications
You must be signed in to change notification settings - Fork 642
/
compositor.js
390 lines (342 loc) · 12.5 KB
/
compositor.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* MelonJS Game Engine
* Copyright (C) 2011 - 2014 Olivier Biot, Jason Oster, Aaron McLeod
* http://www.melonjs.org
*/
(function () {
// Handy constants
var VERTEX_SIZE = 2;
var COLOR_SIZE = 4;
var TEXTURE_SIZE = 1;
var REGION_SIZE = 2;
var ELEMENT_SIZE = VERTEX_SIZE + COLOR_SIZE + TEXTURE_SIZE + REGION_SIZE;
var ELEMENT_OFFSET = ELEMENT_SIZE * Float32Array.BYTES_PER_ELEMENT;
var VERTEX_ELEMENT = 0;
var COLOR_ELEMENT = VERTEX_ELEMENT + VERTEX_SIZE;
var TEXTURE_ELEMENT = COLOR_ELEMENT + COLOR_SIZE;
var REGION_ELEMENT = TEXTURE_ELEMENT + TEXTURE_SIZE;
var VERTEX_OFFSET = VERTEX_ELEMENT * Float32Array.BYTES_PER_ELEMENT;
var COLOR_OFFSET = COLOR_ELEMENT * Float32Array.BYTES_PER_ELEMENT;
var TEXTURE_OFFSET = TEXTURE_ELEMENT * Float32Array.BYTES_PER_ELEMENT;
var REGION_OFFSET = REGION_ELEMENT * Float32Array.BYTES_PER_ELEMENT;
var ELEMENTS_PER_QUAD = 4;
var INDICES_PER_QUAD = 6;
var MAX_LENGTH = 16000;
/**
* A WebGL texture Compositor object. This class handles all of the WebGL state<br>
* Pushes texture regions into WebGL buffers, automatically flushes to GPU
* @extends Object
* @namespace me.WebGLRenderer.Compositor
* @memberOf me
* @constructor
* @param {WebGLContext} gl Destination WebGL Context
* @param {me.Matrix2d} matrix Global transformation matrix
* @param {me.Color} color Global color
*/
me.WebGLRenderer.Compositor = Object.extend(
/** @scope me.WebGLRenderer.Compositor.prototype */
{
/**
* @ignore
*/
init : function (gl, matrix, color) {
/**
* The number of quads held in the batch
* @name length
* @memberOf me.WebGLRenderer.Compositor
* @type Number
* @readonly
*/
this.length = 0;
// Hash map of texture units
this.units = [];
this.maxTextures = gl.getParameter(
gl.MAX_TEXTURE_IMAGE_UNITS
);
// Vector pool
this.v = [
new me.Vector2d(),
new me.Vector2d(),
new me.Vector2d(),
new me.Vector2d()
];
// WebGL context
this.gl = gl;
// Global transformation matrix
this.matrix = matrix;
// Global color
this.color = color;
// Uniform projection matrix
this.uMatrix = new me.Matrix2d();
// Load and create shader program
this.shader = me.video.shader.createShader(
this.gl,
(__VERTEX__)(),
(__FRAGMENT__)({
"precision" : (gl.getShaderPrecisionFormat(
gl.FRAGMENT_SHADER,
gl.HIGH_FLOAT
).precision < 16) ? "mediump" : "highp",
"maxTextures" : this.maxTextures
})
);
// Stream buffer
this.sb = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.sb);
gl.bufferData(
gl.ARRAY_BUFFER,
MAX_LENGTH * ELEMENT_OFFSET * ELEMENTS_PER_QUAD,
gl.STREAM_DRAW
);
this.sbSize = 256;
this.sbIndex = 0;
// Quad stream buffer
this.stream = new Float32Array(
this.sbSize * ELEMENT_SIZE * ELEMENTS_PER_QUAD
);
// Index buffer
this.ib = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.ib);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.createIB(), gl.STATIC_DRAW);
// Bind attribute pointers
gl.vertexAttribPointer(
this.shader.attributes.aVertex,
VERTEX_SIZE,
gl.FLOAT,
false,
ELEMENT_OFFSET,
VERTEX_OFFSET
);
gl.vertexAttribPointer(
this.shader.attributes.aColor,
COLOR_SIZE,
gl.FLOAT,
false,
ELEMENT_OFFSET,
COLOR_OFFSET
);
gl.vertexAttribPointer(
this.shader.attributes.aTexture,
TEXTURE_SIZE,
gl.FLOAT,
false,
ELEMENT_OFFSET,
TEXTURE_OFFSET
);
gl.vertexAttribPointer(
this.shader.attributes.aRegion,
REGION_SIZE,
gl.FLOAT,
false,
ELEMENT_OFFSET,
REGION_OFFSET
);
this.reset();
this.setProjection(gl.canvas.width, gl.canvas.height);
// Initialize clear color and blend function
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
},
/**
* Sets the projection matrix with the given size
* @name setProjection
* @memberOf me.WebGLRenderer.Compositor
* @function
* @param {Number} w WebGL Canvas width
* @param {Number} h WebGL Canvas height
*/
setProjection : function (w, h) {
this.gl.viewport(0, 0, w, h);
this.uMatrix.set(
2 / w, 0, 0,
0, -2 / h, 0,
-1, 1, 1
);
this.shader.uniforms.uMatrix = this.uMatrix.val;
},
/**
* @ignore
*/
uploadTexture : function (texture, w, h, b) {
var unit = me.video.renderer.cache.getUnit(texture);
if (!this.units[unit]) {
this.units[unit] = true;
me.video.shader.createTexture(
this.gl,
unit,
texture.texture,
w,
h,
b
);
}
return unit;
},
/**
* Reset compositor internal state
* @ignore
*/
reset : function () {
this.sbIndex = 0;
this.length = 0;
var samplers = [];
for (var i = 0; i < this.maxTextures; i++) {
this.units[i] = false;
samplers[i] = i;
}
this.shader.uniforms.uSampler = samplers;
},
/**
* Create a full index buffer for the element array
* @ignore
*/
createIB : function () {
var indices = [
0, 1, 2,
2, 1, 3
];
// ~384KB index buffer
var data = new Array(MAX_LENGTH * INDICES_PER_QUAD);
for (var i = 0; i < data.length; i++) {
data[i] = indices[i % INDICES_PER_QUAD] +
~~(i / INDICES_PER_QUAD) * ELEMENTS_PER_QUAD;
}
return new Uint16Array(data);
},
/**
* Resize the stream buffer, retaining its original contents
* @ignore
*/
resizeSB : function () {
this.sbSize <<= 1;
var stream = new Float32Array(this.sbSize * ELEMENT_SIZE * ELEMENTS_PER_QUAD);
stream.set(this.stream);
this.stream = stream;
},
/**
* Add a texture region
* @name add
* @memberOf me.WebGLRenderer.Compositor
* @function
* @param {me.video.renderer.Texture} texture Source texture
* @param {String} key Source texture region name
* @param {Number} x Destination x-coordinate
* @param {Number} y Destination y-coordinate
* @param {Number} w Destination width
* @param {Number} h Destination height
*/
add : function (texture, key, x, y, w, h) {
if (this.length >= MAX_LENGTH) {
this.flush();
}
if (this.length >= this.sbSize) {
this.resizeSB();
}
// Transform vertices
var m = this.matrix,
v0 = this.v[0].set(x, y),
v1 = this.v[1].set(x + w, y),
v2 = this.v[2].set(x, y + h),
v3 = this.v[3].set(x + w, y + h);
if (!m.isIdentity()) {
m.vectorMultiply(v0);
m.vectorMultiply(v1);
m.vectorMultiply(v2);
m.vectorMultiply(v3);
}
// Array index computation
var idx0 = this.sbIndex,
idx1 = idx0 + ELEMENT_SIZE,
idx2 = idx1 + ELEMENT_SIZE,
idx3 = idx2 + ELEMENT_SIZE;
// Fill vertex buffer
// FIXME: Pack each vertex vector into single float
this.stream[idx0 + VERTEX_ELEMENT + 0] = v0.x;
this.stream[idx0 + VERTEX_ELEMENT + 1] = v0.y;
this.stream[idx1 + VERTEX_ELEMENT + 0] = v1.x;
this.stream[idx1 + VERTEX_ELEMENT + 1] = v1.y;
this.stream[idx2 + VERTEX_ELEMENT + 0] = v2.x;
this.stream[idx2 + VERTEX_ELEMENT + 1] = v2.y;
this.stream[idx3 + VERTEX_ELEMENT + 0] = v3.x;
this.stream[idx3 + VERTEX_ELEMENT + 1] = v3.y;
// Fill color buffer
// FIXME: Pack color vector into single float
var color = this.color.toGL();
this.stream.set(color, idx0 + COLOR_ELEMENT);
this.stream.set(color, idx1 + COLOR_ELEMENT);
this.stream.set(color, idx2 + COLOR_ELEMENT);
this.stream.set(color, idx3 + COLOR_ELEMENT);
// Fill texture index buffer
// FIXME: Can the texture index be packed into another element?
var unit = this.uploadTexture(texture);
this.stream[idx0 + TEXTURE_ELEMENT] =
this.stream[idx1 + TEXTURE_ELEMENT] =
this.stream[idx2 + TEXTURE_ELEMENT] =
this.stream[idx3 + TEXTURE_ELEMENT] = unit;
// Get the source texture region
var region = texture.getRegion(key);
if (typeof(region) === "undefined") {
// TODO: Require proper atlas regions instead of caching arbitrary region keys
console.warn("Adding texture region", key, "for texture", texture);
var keys = key.split(","),
sx = +keys[0],
sy = +keys[1],
sw = +keys[2],
sh = +keys[3];
region = texture._insertRegion(key, sx, sy, sw, sh);
}
// Fill texture coordinates buffer
// FIXME: Pack each texture coordinate into single floats
var stMap = region.stMap;
this.stream[idx0 + REGION_ELEMENT + 0] = stMap[0];
this.stream[idx0 + REGION_ELEMENT + 1] = stMap[1];
this.stream[idx1 + REGION_ELEMENT + 0] = stMap[2];
this.stream[idx1 + REGION_ELEMENT + 1] = stMap[1];
this.stream[idx2 + REGION_ELEMENT + 0] = stMap[0];
this.stream[idx2 + REGION_ELEMENT + 1] = stMap[3];
this.stream[idx3 + REGION_ELEMENT + 0] = stMap[2];
this.stream[idx3 + REGION_ELEMENT + 1] = stMap[3];
this.sbIndex += ELEMENT_SIZE * ELEMENTS_PER_QUAD;
this.length++;
},
/**
* Flush batched texture operations to the GPU
* @name flush
* @memberOf me.WebGLRenderer.Compositor
* @function
*/
flush : function () {
if (this.length) {
var gl = this.gl;
// Copy data into stream buffer
var len = this.length * ELEMENT_SIZE * ELEMENTS_PER_QUAD;
gl.bufferData(
gl.ARRAY_BUFFER,
this.stream.subarray(0, len),
gl.STREAM_DRAW
);
// Draw the stream buffer
gl.drawElements(
gl.TRIANGLES,
this.length * INDICES_PER_QUAD,
gl.UNSIGNED_SHORT,
0
);
this.sbIndex = 0;
this.length = 0;
}
},
/**
* Clear the frame buffer, flushes the composite operations and calls
* gl.clear()
* @name clear
* @memberOf me.WebGLRenderer.Compositor
* @function
*/
clear : function () {
this.flush();
this.gl.clear(this.gl.COLOR_BUFFER_BIT);
}
});
})();