forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebGLPipeline.js
More file actions
665 lines (602 loc) · 19.1 KB
/
Copy pathWebGLPipeline.js
File metadata and controls
665 lines (602 loc) · 19.1 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/**
* @author Richard Davey <rich@photonstorm.com>
* @author Felipe Alfonso <@bitnenfer>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var Utils = require('./Utils');
/**
* @classdesc
* WebGLPipeline is a class that describes the way elements will be rendererd
* in WebGL, specially focused on batching vertices (batching is not provided).
* Pipelines are mostly used for describing 2D rendering passes but it's
* flexible enough to be used for any type of rendering including 3D.
* Internally WebGLPipeline will handle things like compiling shaders,
* creating vertex buffers, assigning primitive topology and binding
* vertex attributes.
*
* The config properties are:
* - game: Current game instance.
* - renderer: Current WebGL renderer.
* - gl: Current WebGL context.
* - topology: This indicates how the primitives are rendered. The default value is GL_TRIANGLES.
* Here is the full list of rendering primitives (https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Constants).
* - vertShader: Source for vertex shader as a string.
* - fragShader: Source for fragment shader as a string.
* - vertexCapacity: The amount of vertices that shall be allocated
* - vertexSize: The size of a single vertex in bytes.
* - vertices: An optional buffer of vertices
* - attributes: An array describing the vertex attributes
*
* The vertex attributes properties are:
* - name : String - Name of the attribute in the vertex shader
* - size : integer - How many components describe the attribute. For ex: vec3 = size of 3, float = size of 1
* - type : GLenum - WebGL type (gl.BYTE, gl.SHORT, gl.UNSIGNED_BYTE, gl.UNSIGNED_SHORT, gl.FLOAT)
* - normalized : boolean - Is the attribute normalized
* - offset : integer - The offset in bytes to the current attribute in the vertex. Equivalent to offsetof(vertex, attrib) in C
* Here you can find more information of how to describe an attribute:
* - https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/vertexAttribPointer
*
* @class WebGLPipeline
* @memberOf Phaser.Renderer.WebGL
* @constructor
* @since 3.0.0
*
* @param {object} config - [description]
*/
var WebGLPipeline = new Class({
initialize:
function WebGLPipeline (config)
{
/**
* Name of the Pipeline. Used for identifying
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#name
* @type {string}
* @since 3.0.0
*/
this.name = 'WebGLPipeline';
/**
* [description]
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#game
* @type {Phaser.Game}
* @since 3.0.0
*/
this.game = config.game;
/**
* [description]
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#view
* @type {HTMLCanvasElement}
* @since 3.0.0
*/
this.view = config.game.canvas;
/**
* Used to store the current game resolution
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#resolution
* @type {number}
* @since 3.0.0
*/
this.resolution = config.game.config.resolution;
/**
* Width of the current viewport
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#width
* @type {number}
* @since 3.0.0
*/
this.width = config.game.config.width * this.resolution;
/**
* Height of the current viewport
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#height
* @type {number}
* @since 3.0.0
*/
this.height = config.game.config.height * this.resolution;
/**
* [description]
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#gl
* @type {WebGLRenderingContext}
* @since 3.0.0
*/
this.gl = config.gl;
/**
* How many vertices have been fed to the current pipeline.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexCount
* @type {number}
* @default 0
* @since 3.0.0
*/
this.vertexCount = 0;
/**
* The limit of vertices that the pipeline can hold
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexCapacity
* @type {integer}
* @since 3.0.0
*/
this.vertexCapacity = config.vertexCapacity;
/**
* [description]
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#renderer
* @type {Phaser.Renderer.WebGL.WebGLRenderer}
* @since 3.0.0
*/
this.renderer = config.renderer;
/**
* Raw byte buffer of vertices.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexData
* @type {ArrayBuffer}
* @since 3.0.0
*/
this.vertexData = (config.vertices ? config.vertices : new ArrayBuffer(config.vertexCapacity * config.vertexSize));
/**
* The handle to a WebGL vertex buffer object.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexBuffer
* @type {WebGLBuffer}
* @since 3.0.0
*/
this.vertexBuffer = this.renderer.createVertexBuffer((config.vertices ? config.vertices : this.vertexData.byteLength), this.gl.STREAM_DRAW);
/**
* The handle to a WebGL program
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#program
* @type {WebGLProgram}
* @since 3.0.0
*/
this.program = this.renderer.createProgram(config.vertShader, config.fragShader);
/**
* Array of objects that describe the vertex attributes
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#attributes
* @type {object}
* @since 3.0.0
*/
this.attributes = config.attributes;
/**
* The size in bytes of the vertex
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexSize
* @type {integer}
* @since 3.0.0
*/
this.vertexSize = config.vertexSize;
/**
* The primitive topology which the pipeline will use to submit draw calls
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#topology
* @type {integer}
* @since 3.0.0
*/
this.topology = config.topology;
/**
* Uint8 view to the vertex raw buffer. Used for uploading vertex buffer resources
* to the GPU.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#bytes
* @type {Uint8Array}
* @since 3.0.0
*/
this.bytes = new Uint8Array(this.vertexData);
/**
* This will store the amount of components of 32 bit length
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexComponentCount
* @type {integer}
* @since 3.0.0
*/
this.vertexComponentCount = Utils.getComponentCount(config.attributes, this.gl);
/**
* Indicates if the current pipeline is flushing the contents to the GPU.
* When the variable is set the flush function will be locked.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#flushLocked
* @type {boolean}
* @since 3.1.0
*/
this.flushLocked = false;
/**
* Indicates if the current pipeline is active or not for this frame only.
* Reset in the onRender method.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#active
* @type {boolean}
* @since 3.10.0
*/
this.active = false;
},
/**
* Adds a description of vertex attribute to the pipeline
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#addAttribute
* @since 3.2.0
*
* @param {string} name - Name of the vertex attribute
* @param {integer} size - Vertex component size
* @param {integer} type - Type of the attribute
* @param {boolean} normalized - Is the value normalized to a range
* @param {integer} offset - Byte offset to the beginning of the first element in the vertex
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
addAttribute: function (name, size, type, normalized, offset)
{
this.attributes.push({
name: name,
size: size,
type: this.renderer.glFormats[type],
normalized: normalized,
offset: offset
});
return this;
},
/**
* Check if the current batch of vertices is full.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#shouldFlush
* @since 3.0.0
*
* @return {boolean} [description]
*/
shouldFlush: function ()
{
return (this.vertexCount >= this.vertexCapacity);
},
/**
* Resizes the properties used to describe the viewport
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#resize
* @since 3.0.0
*
* @param {number} width - [description]
* @param {number} height - [description]
* @param {number} resolution - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
resize: function (width, height, resolution)
{
this.width = width * resolution;
this.height = height * resolution;
return this;
},
/**
* Binds the pipeline resources, including programs, vertex buffers and binds attributes
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#bind
* @since 3.0.0
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
bind: function ()
{
var gl = this.gl;
var vertexBuffer = this.vertexBuffer;
var attributes = this.attributes;
var program = this.program;
var renderer = this.renderer;
var vertexSize = this.vertexSize;
renderer.setProgram(program);
renderer.setVertexBuffer(vertexBuffer);
for (var index = 0; index < attributes.length; ++index)
{
var element = attributes[index];
var location = gl.getAttribLocation(program, element.name);
if (location >= 0)
{
gl.enableVertexAttribArray(location);
gl.vertexAttribPointer(location, element.size, element.type, element.normalized, vertexSize, element.offset);
}
else
{
gl.disableVertexAttribArray(location);
}
}
return this;
},
/**
* [description]
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#onBind
* @since 3.0.0
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
onBind: function ()
{
// This is for updating uniform data it's called on each bind attempt.
return this;
},
/**
* [description]
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#onPreRender
* @since 3.0.0
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
onPreRender: function ()
{
// called once every frame
return this;
},
/**
* [description]
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#onRender
* @since 3.0.0
*
* @param {Phaser.Scene} scene - [description]
* @param {Phaser.Cameras.Scene2D.Camera} camera - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
onRender: function ()
{
// called for each camera
return this;
},
/**
* [description]
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#onPostRender
* @since 3.0.0
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
onPostRender: function ()
{
// called once every frame
return this;
},
/**
* Uploads the vertex data and emits a draw call
* for the current batch of vertices.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#flush
* @since 3.0.0
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
flush: function ()
{
if (this.flushLocked) { return this; }
this.flushLocked = true;
var gl = this.gl;
var vertexCount = this.vertexCount;
var topology = this.topology;
var vertexSize = this.vertexSize;
if (vertexCount === 0)
{
this.flushLocked = false;
return;
}
gl.bufferSubData(gl.ARRAY_BUFFER, 0, this.bytes.subarray(0, vertexCount * vertexSize));
gl.drawArrays(topology, 0, vertexCount);
this.vertexCount = 0;
this.flushLocked = false;
return this;
},
/**
* [description]
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#destroy
* @since 3.0.0
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
destroy: function ()
{
var gl = this.gl;
gl.deleteProgram(this.program);
gl.deleteBuffer(this.vertexBuffer);
delete this.program;
delete this.vertexBuffer;
delete this.gl;
return this;
},
/**
* Set a uniform value of the current pipeline program.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setFloat1
* @since 3.2.0
*
* @param {string} name - [description]
* @param {number} x - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setFloat1: function (name, x)
{
this.renderer.setFloat1(this.program, name, x);
return this;
},
/**
* Set a uniform value of the current pipeline program.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setFloat2
* @since 3.2.0
*
* @param {string} name - [description]
* @param {number} x - [description]
* @param {number} y - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setFloat2: function (name, x, y)
{
this.renderer.setFloat2(this.program, name, x, y);
return this;
},
/**
* Set a uniform value of the current pipeline program.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setFloat3
* @since 3.2.0
*
* @param {string} name - [description]
* @param {number} x - [description]
* @param {number} y - [description]
* @param {number} z - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setFloat3: function (name, x, y, z)
{
this.renderer.setFloat3(this.program, name, x, y, z);
return this;
},
/**
* Set a uniform value of the current pipeline program.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setFloat4
* @since 3.2.0
*
* @param {string} name - Name of the uniform
* @param {number} x - X component of the uniform
* @param {number} y - Y component of the uniform
* @param {number} z - Z component of the uniform
* @param {number} w - W component of the uniform
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setFloat4: function (name, x, y, z, w)
{
this.renderer.setFloat4(this.program, name, x, y, z, w);
return this;
},
/**
* Set a uniform value of the current pipeline program.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setInt1
* @since 3.2.0
*
* @param {string} name - [description]
* @param {integer} x - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setInt1: function (name, x)
{
this.renderer.setInt1(this.program, name, x);
return this;
},
/**
* Set a uniform value of the current pipeline program.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setInt2
* @since 3.2.0
*
* @param {string} name - [description]
* @param {integer} x - [description]
* @param {integer} y - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setInt2: function (name, x, y)
{
this.renderer.setInt2(this.program, name, x, y);
return this;
},
/**
* Set a uniform value of the current pipeline program.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setInt3
* @since 3.2.0
*
* @param {string} name - [description]
* @param {integer} x - [description]
* @param {integer} y - [description]
* @param {integer} z - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setInt3: function (name, x, y, z)
{
this.renderer.setInt3(this.program, name, x, y, z);
return this;
},
/**
* Set a uniform value of the current pipeline program.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setInt4
* @since 3.2.0
*
* @param {string} name - Name of the uniform
* @param {integer} x - X component of the uniform
* @param {integer} y - Y component of the uniform
* @param {integer} z - Z component of the uniform
* @param {integer} w - W component of the uniform
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setInt4: function (name, x, y, z, w)
{
this.renderer.setInt4(this.program, name, x, y, z, w);
return this;
},
/**
* Set a uniform value of the current pipeline program.
* [description]
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setMatrix2
* @since 3.2.0
*
* @param {string} name - [description]
* @param {boolean} transpose - [description]
* @param {Float32Array} matrix - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setMatrix2: function (name, transpose, matrix)
{
this.renderer.setMatrix2(this.program, name, transpose, matrix);
return this;
},
/**
* Set a uniform value of the current pipeline program.
* [description]
* [description]
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setMatrix3
* @since 3.2.0
*
* @param {string} name - [description]
* @param {boolean} transpose - [description]
* @param {Float32Array} matrix - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setMatrix3: function (name, transpose, matrix)
{
this.renderer.setMatrix3(this.program, name, transpose, matrix);
return this;
},
/**
* Set a uniform value of the current pipeline program.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setMatrix4
* @since 3.2.0
*
* @param {string} name - Name of the uniform
* @param {boolean} transpose - Should the matrix be transpose
* @param {Float32Array} matrix - Matrix data
*
* @return {Phaser.Renderer.WebGL.WebGLPipeline} [description]
*/
setMatrix4: function (name, transpose, matrix)
{
this.renderer.setMatrix4(this.program, name, transpose, matrix);
return this;
}
});
module.exports = WebGLPipeline;