/
WebGLPipeline.js
2138 lines (1861 loc) · 73.1 KB
/
WebGLPipeline.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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
var DeepCopy = require('../../utils/object/DeepCopy');
var EventEmitter = require('eventemitter3');
var Events = require('./pipelines/events');
var GetFastValue = require('../../utils/object/GetFastValue');
var Matrix4 = require('../../math/Matrix4');
var RendererEvents = require('../events');
var RenderTarget = require('./RenderTarget');
var Utils = require('./Utils');
var WebGLShader = require('./WebGLShader');
/**
* @classdesc
* The `WebGLPipeline` is a base class used by all of the core Phaser pipelines.
*
* It describes the way elements will be rendered in WebGL. Internally, it handles
* compiling the shaders, creating vertex buffers, assigning primitive topology and
* binding vertex attributes, all based on the given configuration data.
*
* The pipeline is configured by passing in a `WebGLPipelineConfig` object. Please
* see the documentation for this type to fully understand the configuration options
* available to you.
*
* Usually, you would not extend from this class directly, but would instead extend
* from one of the core pipelines, such as the Multi Pipeline.
*
* The pipeline flow per render-step is as follows:
*
* 1) onPreRender - called once at the start of the render step
* 2) onRender - call for each Scene Camera that needs to render (so can be multiple times per render step)
* 3) Internal flow:
* 3a) bind (only called if a Game Object is using this pipeline and it's not currently active)
* 3b) onBind (called for every Game Object that uses this pipeline)
* 3c) flush (can be called by a Game Object, internal method or from outside by changing pipeline)
* 4) onPostRender - called once at the end of the render step
*
* @class WebGLPipeline
* @extends Phaser.Events.EventEmitter
* @memberof Phaser.Renderer.WebGL
* @constructor
* @since 3.0.0
*
* @param {Phaser.Types.Renderer.WebGL.WebGLPipelineConfig} config - The configuration object for this WebGL Pipeline.
*/
var WebGLPipeline = new Class({
Extends: EventEmitter,
initialize:
function WebGLPipeline (config)
{
EventEmitter.call(this);
var game = config.game;
var renderer = game.renderer;
var gl = renderer.gl;
/**
* Name of the pipeline. Used for identification and setting from Game Objects.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#name
* @type {string}
* @since 3.0.0
*/
this.name = GetFastValue(config, 'name', 'WebGLPipeline');
/**
* The Phaser Game instance to which this pipeline is bound.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#game
* @type {Phaser.Game}
* @since 3.0.0
*/
this.game = game;
/**
* The WebGL Renderer instance to which this pipeline is bound.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#renderer
* @type {Phaser.Renderer.WebGL.WebGLRenderer}
* @since 3.0.0
*/
this.renderer = renderer;
/**
* A reference to the WebGL Pipeline Manager.
*
* This is initially undefined and only set when this pipeline is added
* to the manager.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#manager
* @type {?Phaser.Renderer.WebGL.PipelineManager}
* @since 3.50.0
*/
this.manager;
/**
* The WebGL context this WebGL Pipeline uses.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#gl
* @type {WebGLRenderingContext}
* @since 3.0.0
*/
this.gl = gl;
/**
* The canvas which this WebGL Pipeline renders to.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#view
* @type {HTMLCanvasElement}
* @since 3.0.0
*/
this.view = game.canvas;
/**
* Width of the current viewport.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#width
* @type {number}
* @since 3.0.0
*/
this.width = 0;
/**
* Height of the current viewport.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#height
* @type {number}
* @since 3.0.0
*/
this.height = 0;
/**
* The current number of vertices that have been added to the pipeline batch.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexCount
* @type {number}
* @default 0
* @since 3.0.0
*/
this.vertexCount = 0;
/**
* The total number of vertices that this pipeline batch can hold before it will flush.
*
* This defaults to `renderer batchSize * 6`, where `batchSize` is defined in the Renderer Game Config.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexCapacity
* @type {number}
* @since 3.0.0
*/
this.vertexCapacity = 0;
/**
* Raw byte buffer of vertices.
*
* Either set via the config object `vertices` property, or generates a new Array Buffer of
* size `vertexCapacity * vertexSize`.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexData
* @type {ArrayBuffer}
* @readonly
* @since 3.0.0
*/
this.vertexData;
/**
* The WebGLBuffer that holds the vertex data.
*
* Created from the `vertexData` ArrayBuffer. If `vertices` are set in the config, a `STATIC_DRAW` buffer
* is created. If not, a `DYNAMIC_DRAW` buffer is created.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexBuffer
* @type {WebGLBuffer}
* @readonly
* @since 3.0.0
*/
this.vertexBuffer;
/**
* The primitive topology which the pipeline will use to submit draw calls.
*
* Defaults to GL_TRIANGLES if not otherwise set in the config.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#topology
* @type {GLenum}
* @since 3.0.0
*/
this.topology = GetFastValue(config, 'topology', gl.TRIANGLES);
/**
* Uint8 view to the `vertexData` ArrayBuffer. Used for uploading vertex buffer resources to the GPU.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#bytes
* @type {Uint8Array}
* @since 3.0.0
*/
this.bytes;
/**
* Float32 view of the array buffer containing the pipeline's vertices.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexViewF32
* @type {Float32Array}
* @since 3.0.0
*/
this.vertexViewF32;
/**
* Uint32 view of the array buffer containing the pipeline's vertices.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#vertexViewU32
* @type {Uint32Array}
* @since 3.0.0
*/
this.vertexViewU32;
/**
* Indicates if the current pipeline is active, or not.
*
* Toggle this property to enable or disable a pipeline from rendering anything.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#active
* @type {boolean}
* @since 3.10.0
*/
this.active = true;
/**
* Holds the most recently assigned texture unit.
*
* Treat this value as read-only.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#currentUnit
* @type {number}
* @since 3.50.0
*/
this.currentUnit = 0;
/**
* Some pipelines require the forced use of texture zero (like the light pipeline).
*
* This property should be set when that is the case.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#forceZero
* @type {boolean}
* @since 3.50.0
*/
this.forceZero = GetFastValue(config, 'forceZero', false);
/**
* Indicates if this pipeline has booted or not.
*
* A pipeline boots only when the Game instance itself, and all associated systems, is
* fully ready.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#hasBooted
* @type {boolean}
* @readonly
* @since 3.50.0
*/
this.hasBooted = false;
/**
* Indicates if this is a Post FX Pipeline, or not.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#isPostFX
* @type {boolean}
* @readonly
* @since 3.50.0
*/
this.isPostFX = false;
/**
* An array of RenderTarget instances that belong to this pipeline.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#renderTargets
* @type {Phaser.Renderer.WebGL.RenderTarget[]}
* @since 3.50.0
*/
this.renderTargets = [];
/**
* A reference to the currently bound Render Target instance from the `WebGLPipeline.renderTargets` array.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#currentRenderTarget
* @type {Phaser.Renderer.WebGL.RenderTarget}
* @since 3.50.0
*/
this.currentRenderTarget;
/**
* An array of all the WebGLShader instances that belong to this pipeline.
*
* Shaders manage their own attributes and uniforms, but share the same vertex data buffer,
* which belongs to this pipeline.
*
* Shaders are set in a call to the `setShadersFromConfig` method, which happens automatically,
* but can also be called at any point in your game. See the method documentation for details.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#shaders
* @type {Phaser.Renderer.WebGL.WebGLShader[]}
* @since 3.50.0
*/
this.shaders = [];
/**
* A reference to the currently bound WebGLShader instance from the `WebGLPipeline.shaders` array.
*
* For lots of pipelines, this is the only shader, so it is a quick way to reference it without
* an array look-up.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#currentShader
* @type {Phaser.Renderer.WebGL.WebGLShader}
* @since 3.50.0
*/
this.currentShader;
/**
* The Projection matrix, used by shaders as 'uProjectionMatrix' uniform.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#projectionMatrix
* @type {Phaser.Math.Matrix4}
* @since 3.50.0
*/
this.projectionMatrix;
/**
* The cached width of the Projection matrix.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#projectionWidth
* @type {number}
* @since 3.50.0
*/
this.projectionWidth = 0;
/**
* The cached height of the Projection matrix.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#projectionHeight
* @type {number}
* @since 3.50.0
*/
this.projectionHeight = 0;
/**
* The configuration object that was used to create this pipeline.
*
* Treat this object as 'read only', because changing it post-creation will not
* impact this pipeline in any way. However, it is used internally for cloning
* and post-boot set-up.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#config
* @type {Phaser.Types.Renderer.WebGL.WebGLPipelineConfig}
* @since 3.50.0
*/
this.config = config;
/**
* Has the GL Context been reset to the Phaser defaults since the last time
* this pipeline was bound? This is set automatically when the Pipeline Manager
* resets itself, usually after handing off to a 3rd party renderer like Spine.
*
* You should treat this property as read-only.
*
* @name Phaser.Renderer.WebGL.WebGLPipeline#glReset
* @type {boolean}
* @since 3.53.0
*/
this.glReset = false;
},
/**
* Called when the Game has fully booted and the Renderer has finished setting up.
*
* By this stage all Game level systems are now in place. You can perform any final tasks that the
* pipeline may need, that relies on game systems such as the Texture Manager being ready.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#boot
* @fires Phaser.Renderer.WebGL.Pipelines.Events#BOOT
* @since 3.11.0
*/
boot: function ()
{
var i;
var gl = this.gl;
var config = this.config;
var renderer = this.renderer;
if (!this.isPostFX)
{
this.projectionMatrix = new Matrix4().identity();
}
// Create the Render Targets
var renderTargets = this.renderTargets;
var targets = GetFastValue(config, 'renderTarget', false);
// If boolean, set to number = 1
if (typeof(targets) === 'boolean' && targets)
{
targets = 1;
}
var width = renderer.width;
var height = renderer.height;
if (typeof(targets) === 'number')
{
// Create this many default RTs
for (i = 0; i < targets; i++)
{
renderTargets.push(new RenderTarget(renderer, width, height, 1, 0, true));
}
}
else if (Array.isArray(targets))
{
for (i = 0; i < targets.length; i++)
{
var scale = GetFastValue(targets[i], 'scale', 1);
var minFilter = GetFastValue(targets[i], 'minFilter', 0);
var autoClear = GetFastValue(targets[i], 'autoClear', 1);
renderTargets.push(new RenderTarget(renderer, width, height, scale, minFilter, autoClear));
}
}
if (renderTargets.length)
{
// Default to the first one in the array
this.currentRenderTarget = renderTargets[0];
}
// Create the Shaders
this.setShadersFromConfig(config);
// Which shader has the largest vertex size?
var shaders = this.shaders;
var vertexSize = 0;
for (i = 0; i < shaders.length; i++)
{
if (shaders[i].vertexSize > vertexSize)
{
vertexSize = shaders[i].vertexSize;
}
}
var batchSize = GetFastValue(config, 'batchSize', renderer.config.batchSize);
// * 6 because there are 6 vertices in a quad and 'batchSize' represents the quantity of quads in the batch
this.vertexCapacity = batchSize * 6;
var data = new ArrayBuffer(this.vertexCapacity * vertexSize);
this.vertexData = data;
this.bytes = new Uint8Array(data);
this.vertexViewF32 = new Float32Array(data);
this.vertexViewU32 = new Uint32Array(data);
var configVerts = GetFastValue(config, 'vertices', null);
if (configVerts)
{
this.vertexViewF32.set(configVerts);
this.vertexBuffer = renderer.createVertexBuffer(data, gl.STATIC_DRAW);
}
else
{
this.vertexBuffer = renderer.createVertexBuffer(data.byteLength, gl.DYNAMIC_DRAW);
}
// Set-up shaders
this.setVertexBuffer();
for (i = shaders.length - 1; i >= 0; i--)
{
shaders[i].rebind();
}
this.hasBooted = true;
renderer.on(RendererEvents.RESIZE, this.resize, this);
renderer.on(RendererEvents.PRE_RENDER, this.onPreRender, this);
renderer.on(RendererEvents.RENDER, this.onRender, this);
renderer.on(RendererEvents.POST_RENDER, this.onPostRender, this);
this.emit(Events.BOOT, this);
this.onBoot();
},
/**
* This method is called once when this pipeline has finished being set-up
* at the end of the boot process. By the time this method is called, all
* of the shaders are ready and configured.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#onBoot
* @since 3.50.0
*/
onBoot: function ()
{
},
/**
* This method is called once when this pipeline has finished being set-up
* at the end of the boot process. By the time this method is called, all
* of the shaders are ready and configured. It's also called if the renderer
* changes size.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#onResize
* @since 3.50.0
*
* @param {number} width - The new width of this WebGL Pipeline.
* @param {number} height - The new height of this WebGL Pipeline.
*/
onResize: function ()
{
},
/**
* Sets the currently active shader within this pipeline.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setShader
* @since 3.50.0
*
* @param {Phaser.Renderer.WebGL.WebGLShader} shader - The shader to set as being current.
* @param {boolean} [setAttributes=false] - Should the vertex attribute pointers be set?
*
* @return {this} This WebGLPipeline instance.
*/
setShader: function (shader, setAttributes)
{
var renderer = this.renderer;
if (shader !== this.currentShader || renderer.currentProgram !== this.currentShader.program)
{
this.flush();
renderer.resetTextures();
var wasBound = this.setVertexBuffer();
if (wasBound && !setAttributes)
{
setAttributes = true;
}
shader.bind(setAttributes, false);
this.currentShader = shader;
}
return this;
},
/**
* Searches all shaders in this pipeline for one matching the given name, then returns it.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#getShaderByName
* @since 3.50.0
*
* @param {string} name - The index of the shader to set.
*
* @return {Phaser.Renderer.WebGL.WebGLShader} The WebGLShader instance, if found.
*/
getShaderByName: function (name)
{
var shaders = this.shaders;
for (var i = 0; i < shaders.length; i++)
{
if (shaders[i].name === name)
{
return shaders[i];
}
}
},
/**
* Destroys all shaders currently set in the `WebGLPipeline.shaders` array and then parses the given
* `config` object, extracting the shaders from it, creating `WebGLShader` instances and finally
* setting them into the `shaders` array of this pipeline.
*
* This is a destructive process. Be very careful when you call it, should you need to.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setShadersFromConfig
* @since 3.50.0
*
* @param {Phaser.Types.Renderer.WebGL.WebGLPipelineConfig} config - The configuration object for this WebGL Pipeline.
*
* @return {this} This WebGLPipeline instance.
*/
setShadersFromConfig: function (config)
{
var i;
var shaders = this.shaders;
var renderer = this.renderer;
for (i = 0; i < shaders.length; i++)
{
shaders[i].destroy();
}
var vName = 'vertShader';
var fName = 'fragShader';
var aName = 'attributes';
var defaultVertShader = GetFastValue(config, vName, null);
var defaultFragShader = Utils.parseFragmentShaderMaxTextures(GetFastValue(config, fName, null), renderer.maxTextures);
var defaultAttribs = GetFastValue(config, aName, null);
var configShaders = GetFastValue(config, 'shaders', []);
var len = configShaders.length;
if (len === 0)
{
if (defaultVertShader && defaultFragShader)
{
this.shaders = [ new WebGLShader(this, 'default', defaultVertShader, defaultFragShader, DeepCopy(defaultAttribs)) ];
}
}
else
{
var newShaders = [];
for (i = 0; i < len; i++)
{
var shaderEntry = configShaders[i];
var name = GetFastValue(shaderEntry, 'name', 'default');
var vertShader = GetFastValue(shaderEntry, vName, defaultVertShader);
var fragShader = Utils.parseFragmentShaderMaxTextures(GetFastValue(shaderEntry, fName, defaultFragShader), renderer.maxTextures);
var attributes = GetFastValue(shaderEntry, aName, defaultAttribs);
if (vertShader && fragShader)
{
newShaders.push(new WebGLShader(this, name, vertShader, fragShader, DeepCopy(attributes)));
}
}
this.shaders = newShaders;
}
if (this.shaders.length === 0)
{
console.warn('Pipeline: ' + this.name + ' - Invalid shader config');
}
else
{
this.currentShader = this.shaders[0];
}
return this;
},
/**
* Custom pipelines can use this method in order to perform any required pre-batch tasks
* for the given Game Object. It must return the texture unit the Game Object was assigned.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setGameObject
* @since 3.50.0
*
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object being rendered or added to the batch.
* @param {Phaser.Textures.Frame} [frame] - Optional frame to use. Can override that of the Game Object.
*
* @return {number} The texture unit the Game Object has been assigned.
*/
setGameObject: function (gameObject, frame)
{
if (frame === undefined) { frame = gameObject.frame; }
this.currentUnit = this.renderer.setTextureSource(frame.source);
return this.currentUnit;
},
/**
* Check if the current batch of vertices is full.
*
* You can optionally provide an `amount` parameter. If given, it will check if the batch
* needs to flush _if_ the `amount` is added to it. This allows you to test if you should
* flush before populating the batch.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#shouldFlush
* @since 3.0.0
*
* @param {number} [amount=0] - Will the batch need to flush if this many vertices are added to it?
*
* @return {boolean} `true` if the current batch should be flushed, otherwise `false`.
*/
shouldFlush: function (amount)
{
if (amount === undefined) { amount = 0; }
return (this.vertexCount + amount > this.vertexCapacity);
},
/**
* Resizes the properties used to describe the viewport.
*
* This method is called automatically by the renderer during its resize handler.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#resize
* @fires Phaser.Renderer.WebGL.Pipelines.Events#RESIZE
* @since 3.0.0
*
* @param {number} width - The new width of this WebGL Pipeline.
* @param {number} height - The new height of this WebGL Pipeline.
*
* @return {this} This WebGLPipeline instance.
*/
resize: function (width, height)
{
if (width !== this.width || height !== this.height)
{
this.flush();
}
this.width = width;
this.height = height;
var targets = this.renderTargets;
for (var i = 0; i < targets.length; i++)
{
targets[i].resize(width, height);
}
this.setProjectionMatrix(width, height);
this.emit(Events.RESIZE, width, height, this);
this.onResize(width, height);
return this;
},
/**
* Adjusts this pipelines ortho Projection Matrix to use the given dimensions
* and resets the `uProjectionMatrix` uniform on all bound shaders.
*
* This method is called automatically by the renderer during its resize handler.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setProjectionMatrix
* @since 3.50.0
*
* @param {number} width - The new width of this WebGL Pipeline.
* @param {number} height - The new height of this WebGL Pipeline.
*
* @return {this} This WebGLPipeline instance.
*/
setProjectionMatrix: function (width, height)
{
var projectionMatrix = this.projectionMatrix;
// Because not all pipelines have them
if (!projectionMatrix)
{
return this;
}
this.projectionWidth = width;
this.projectionHeight = height;
projectionMatrix.ortho(0, width, height, 0, -1000, 1000);
var shaders = this.shaders;
var name = 'uProjectionMatrix';
for (var i = 0; i < shaders.length; i++)
{
var shader = shaders[i];
if (shader.hasUniform(name))
{
shader.resetUniform(name);
shader.setMatrix4fv(name, false, projectionMatrix.val, shader);
}
}
return this;
},
/**
* Adjusts this pipelines ortho Projection Matrix to match that of the global
* WebGL Renderer Projection Matrix.
*
* This method is called automatically by the Pipeline Manager when this
* pipeline is set.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#updateProjectionMatrix
* @since 3.50.0
*/
updateProjectionMatrix: function ()
{
if (this.projectionMatrix)
{
var globalWidth = this.renderer.projectionWidth;
var globalHeight = this.renderer.projectionHeight;
if (this.projectionWidth !== globalWidth || this.projectionHeight !== globalHeight)
{
this.setProjectionMatrix(globalWidth, globalHeight);
}
}
},
/**
* This method is called every time the Pipeline Manager makes this pipeline the currently active one.
*
* It binds the resources and shader needed for this pipeline, including setting the vertex buffer
* and attribute pointers.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#bind
* @fires Phaser.Renderer.WebGL.Pipelines.Events#BIND
* @since 3.0.0
*
* @param {Phaser.Renderer.WebGL.WebGLShader} [currentShader] - The shader to set as being current.
*
* @return {this} This WebGLPipeline instance.
*/
bind: function (currentShader)
{
if (currentShader === undefined) { currentShader = this.currentShader; }
if (this.glReset)
{
return this.rebind(currentShader);
}
var wasBound = this.setVertexBuffer();
currentShader.bind(wasBound);
this.currentShader = currentShader;
this.emit(Events.BIND, this, currentShader);
this.onActive(currentShader);
return this;
},
/**
* This method is called every time the Pipeline Manager rebinds this pipeline.
*
* It resets all shaders this pipeline uses, setting their attributes again.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#rebind
* @fires Phaser.Renderer.WebGL.Pipelines.Events#REBIND
* @since 3.0.0
*
* @param {Phaser.Renderer.WebGL.WebGLShader} [currentShader] - The shader to set as being current.
*
* @return {this} This WebGLPipeline instance.
*/
rebind: function (currentShader)
{
this.setVertexBuffer();
var shaders = this.shaders;
// Loop in reverse, so the first shader in the array is left as being bound
for (var i = shaders.length - 1; i >= 0; i--)
{
var shader = shaders[i].rebind();
if (!currentShader || shader === currentShader)
{
this.currentShader = shader;
}
}
this.emit(Events.REBIND, this.currentShader);
this.onActive(this.currentShader);
this.onRebind();
this.glReset = false;
return this;
},
/**
* Binds the vertex buffer to be the active ARRAY_BUFFER on the WebGL context.
*
* It first checks to see if it's already set as the active buffer and only
* binds itself if not.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#setVertexBuffer
* @since 3.50.0
*
* @return {boolean} `true` if the vertex buffer was bound, or `false` if it was already bound.
*/
setVertexBuffer: function ()
{
var gl = this.gl;
var buffer = this.vertexBuffer;
if (gl.getParameter(gl.ARRAY_BUFFER_BINDING) !== buffer)
{
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
return true;
}
return false;
},
/**
* This method is called as a result of the `WebGLPipeline.batchQuad` method, right before a quad
* belonging to a Game Object is about to be added to the batch. When this is called, the
* renderer has just performed a flush. It will bind the current render target, if any are set
* and finally call the `onPreBatch` hook.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#preBatch
* @since 3.50.0
*
* @param {(Phaser.GameObjects.GameObject|Phaser.Cameras.Scene2D.Camera)} [gameObject] - The Game Object or Camera that invoked this pipeline, if any.
*
* @return {this} This WebGLPipeline instance.
*/
preBatch: function (gameObject)
{
if (this.currentRenderTarget)
{
this.currentRenderTarget.bind();
}
this.onPreBatch(gameObject);
return this;
},
/**
* This method is called as a result of the `WebGLPipeline.batchQuad` method, right after a quad
* belonging to a Game Object has been added to the batch. When this is called, the
* renderer has just performed a flush.
*
* It calls the `onDraw` hook followed by the `onPostBatch` hook, which can be used to perform
* additional Post FX Pipeline processing.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#postBatch
* @since 3.50.0
*
* @param {(Phaser.GameObjects.GameObject|Phaser.Cameras.Scene2D.Camera)} [gameObject] - The Game Object or Camera that invoked this pipeline, if any.
*
* @return {this} This WebGLPipeline instance.
*/
postBatch: function (gameObject)
{
this.onDraw(this.currentRenderTarget);
this.onPostBatch(gameObject);
return this;
},
/**
* This method is only used by Post FX Pipelines and those that extend from them.
*
* This method is called every time the `postBatch` method is called and is passed a
* reference to the current render target.
*
* At the very least a Post FX Pipeline should call `this.bindAndDraw(renderTarget)`,
* however, you can do as much additional processing as you like in this method if
* you override it from within your own pipelines.
*
* @method Phaser.Renderer.WebGL.WebGLPipeline#onDraw
* @since 3.50.0
*
* @param {Phaser.Renderer.WebGL.RenderTarget} renderTarget - The Render Target.
*/
onDraw: function ()
{
},
/**
* This method is called every time the Pipeline Manager deactivates this pipeline, swapping from
* it to another one. This happens after a call to `flush` and before the new pipeline is bound.
*