/
WebGLRenderer.js
2991 lines (2498 loc) · 93.7 KB
/
WebGLRenderer.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>
* @author Felipe Alfonso <@bitnenfer>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var ArrayRemove = require('../../utils/array/Remove');
var CameraEvents = require('../../cameras/2d/events');
var Class = require('../../utils/Class');
var CONST = require('../../const');
var EventEmitter = require('eventemitter3');
var Events = require('../events');
var GameEvents = require('../../core/events');
var IsSizePowerOfTwo = require('../../math/pow2/IsSizePowerOfTwo');
var Matrix4 = require('../../math/Matrix4');
var NOOP = require('../../utils/NOOP');
var PipelineManager = require('./PipelineManager');
var RenderTarget = require('./RenderTarget');
var ScaleEvents = require('../../scale/events');
var TextureEvents = require('../../textures/events');
var Utils = require('./Utils');
var WebGLSnapshot = require('../snapshot/WebGLSnapshot');
/**
* @callback WebGLContextCallback
*
* @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - The WebGL Renderer which owns the context.
*/
/**
* @classdesc
* WebGLRenderer is a class that contains the needed functionality to keep the
* WebGLRenderingContext state clean. The main idea of the WebGLRenderer is to keep track of
* any context change that happens for WebGL rendering inside of Phaser. This means
* if raw webgl functions are called outside the WebGLRenderer of the Phaser WebGL
* rendering ecosystem they might pollute the current WebGLRenderingContext state producing
* unexpected behavior. It's recommended that WebGL interaction is done through
* WebGLRenderer and/or WebGLPipeline.
*
* @class WebGLRenderer
* @extends Phaser.Events.EventEmitter
* @memberof Phaser.Renderer.WebGL
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - The Game instance which owns this WebGL Renderer.
*/
var WebGLRenderer = new Class({
Extends: EventEmitter,
initialize:
function WebGLRenderer (game)
{
EventEmitter.call(this);
var gameConfig = game.config;
var contextCreationConfig = {
alpha: gameConfig.transparent,
desynchronized: gameConfig.desynchronized,
depth: false,
antialias: gameConfig.antialiasGL,
premultipliedAlpha: gameConfig.premultipliedAlpha,
stencil: true,
failIfMajorPerformanceCaveat: gameConfig.failIfMajorPerformanceCaveat,
powerPreference: gameConfig.powerPreference,
preserveDrawingBuffer: gameConfig.preserveDrawingBuffer
};
/**
* The local configuration settings of this WebGL Renderer.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#config
* @type {object}
* @since 3.0.0
*/
this.config = {
clearBeforeRender: gameConfig.clearBeforeRender,
antialias: gameConfig.antialias,
backgroundColor: gameConfig.backgroundColor,
contextCreation: contextCreationConfig,
roundPixels: gameConfig.roundPixels,
maxTextures: gameConfig.maxTextures,
maxTextureSize: gameConfig.maxTextureSize,
batchSize: gameConfig.batchSize,
maxLights: gameConfig.maxLights,
mipmapFilter: gameConfig.mipmapFilter
};
/**
* The Game instance which owns this WebGL Renderer.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#game
* @type {Phaser.Game}
* @since 3.0.0
*/
this.game = game;
/**
* A constant which allows the renderer to be easily identified as a WebGL Renderer.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#type
* @type {number}
* @since 3.0.0
*/
this.type = CONST.WEBGL;
/**
* An instance of the Pipeline Manager class, that handles all WebGL Pipelines.
*
* Use this to manage all of your interactions with pipelines, such as adding, getting,
* setting and rendering them.
*
* The Pipeline Manager class is created in the `init` method and then populated
* with pipelines during the `boot` method.
*
* Prior to Phaser v3.50.0 this was just a plain JavaScript object, not a class.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#pipelines
* @type {Phaser.Renderer.WebGL.PipelineManager}
* @since 3.50.0
*/
this.pipelines = null;
/**
* The width of the canvas being rendered to.
* This is populated in the onResize event handler.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#width
* @type {number}
* @since 3.0.0
*/
this.width = 0;
/**
* The height of the canvas being rendered to.
* This is populated in the onResize event handler.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#height
* @type {number}
* @since 3.0.0
*/
this.height = 0;
/**
* The canvas which this WebGL Renderer draws to.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#canvas
* @type {HTMLCanvasElement}
* @since 3.0.0
*/
this.canvas = game.canvas;
/**
* An array of blend modes supported by the WebGL Renderer.
*
* This array includes the default blend modes as well as any custom blend modes added through {@link #addBlendMode}.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#blendModes
* @type {array}
* @default []
* @since 3.0.0
*/
this.blendModes = [];
/**
* This property is set to `true` if the WebGL context of the renderer is lost.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#contextLost
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.contextLost = false;
/**
* Details about the currently scheduled snapshot.
*
* If a non-null `callback` is set in this object, a snapshot of the canvas will be taken after the current frame is fully rendered.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#snapshotState
* @type {Phaser.Types.Renderer.Snapshot.SnapshotState}
* @since 3.0.0
*/
this.snapshotState = {
x: 0,
y: 0,
width: 1,
height: 1,
getPixel: false,
callback: null,
type: 'image/png',
encoder: 0.92,
isFramebuffer: false,
bufferWidth: 0,
bufferHeight: 0
};
/**
* Cached value for the last texture unit that was used.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentActiveTexture
* @type {number}
* @since 3.1.0
*/
this.currentActiveTexture = 0;
/**
* Contains the current starting active texture unit.
* This value is constantly updated and should be treated as read-only by your code.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#startActiveTexture
* @type {number}
* @since 3.50.0
*/
this.startActiveTexture = 0;
/**
* The maximum number of textures the GPU can handle. The minimum under the WebGL1 spec is 8.
* This is set via the Game Config `maxTextures` property and should never be changed after boot.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#maxTextures
* @type {number}
* @since 3.50.0
*/
this.maxTextures = 0;
/**
* An array of the available WebGL texture units, used to populate the uSampler uniforms.
*
* This array is populated during the init phase and should never be changed after boot.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#textureIndexes
* @type {array}
* @since 3.50.0
*/
this.textureIndexes;
/**
* An array of default temporary WebGL Textures.
*
* This array is populated during the init phase and should never be changed after boot.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#tempTextures
* @type {array}
* @since 3.50.0
*/
this.tempTextures;
/**
* The currently bound texture at texture unit zero, if any.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#textureZero
* @type {?WebGLTexture}
* @since 3.50.0
*/
this.textureZero;
/**
* The currently bound normal map texture at texture unit one, if any.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#normalTexture
* @type {?WebGLTexture}
* @since 3.50.0
*/
this.normalTexture;
/**
* The currently bound framebuffer in use.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentFramebuffer
* @type {WebGLFramebuffer}
* @default null
* @since 3.0.0
*/
this.currentFramebuffer = null;
/**
* A stack into which the frame buffer objects are pushed and popped.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#fboStack
* @type {WebGLFramebuffer[]}
* @since 3.50.0
*/
this.fboStack = [];
/**
* Current WebGLProgram in use.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentProgram
* @type {WebGLProgram}
* @default null
* @since 3.0.0
*/
this.currentProgram = null;
/**
* Current blend mode in use
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentBlendMode
* @type {number}
* @since 3.0.0
*/
this.currentBlendMode = Infinity;
/**
* Indicates if the the scissor state is enabled in WebGLRenderingContext
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentScissorEnabled
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.currentScissorEnabled = false;
/**
* Stores the current scissor data
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentScissor
* @type {Uint32Array}
* @since 3.0.0
*/
this.currentScissor = null;
/**
* Stack of scissor data
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#scissorStack
* @type {Uint32Array}
* @since 3.0.0
*/
this.scissorStack = [];
/**
* The handler to invoke when the context is lost.
* This should not be changed and is set in the boot method.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#contextLostHandler
* @type {function}
* @since 3.19.0
*/
this.contextLostHandler = NOOP;
/**
* The handler to invoke when the context is restored.
* This should not be changed and is set in the boot method.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#contextRestoredHandler
* @type {function}
* @since 3.19.0
*/
this.contextRestoredHandler = NOOP;
/**
* The underlying WebGL context of the renderer.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#gl
* @type {WebGLRenderingContext}
* @default null
* @since 3.0.0
*/
this.gl = null;
/**
* Array of strings that indicate which WebGL extensions are supported by the browser.
* This is populated in the `boot` method.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#supportedExtensions
* @type {string[]}
* @default null
* @since 3.0.0
*/
this.supportedExtensions = null;
/**
* If the browser supports the `ANGLE_instanced_arrays` extension, this property will hold
* a reference to the glExtension for it.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#instancedArraysExtension
* @type {ANGLE_instanced_arrays}
* @default null
* @since 3.50.0
*/
this.instancedArraysExtension = null;
/**
* If the browser supports the `OES_vertex_array_object` extension, this property will hold
* a reference to the glExtension for it.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#vaoExtension
* @type {OES_vertex_array_object}
* @default null
* @since 3.50.0
*/
this.vaoExtension = null;
/**
* The WebGL Extensions loaded into the current context.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#extensions
* @type {object}
* @default {}
* @since 3.0.0
*/
this.extensions = {};
/**
* Stores the current WebGL component formats for further use.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#glFormats
* @type {array}
* @default []
* @since 3.2.0
*/
this.glFormats = [];
/**
* Stores the supported WebGL texture compression formats.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#compression
* @type {Phaser.Types.Renderer.WebGL.WebGLTextureCompression}
* @since 3.8.0
*/
this.compression = {
ETC1: false,
PVRTC: false,
S3TC: false
};
/**
* Cached drawing buffer height to reduce gl calls.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#drawingBufferHeight
* @type {number}
* @readonly
* @since 3.11.0
*/
this.drawingBufferHeight = 0;
/**
* A blank 32x32 transparent texture, as used by the Graphics system where needed.
* This is set in the `boot` method.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#blankTexture
* @type {WebGLTexture}
* @readonly
* @since 3.12.0
*/
this.blankTexture = null;
/**
* A pure white 4x4 texture, as used by the Graphics system where needed.
* This is set in the `boot` method.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#whiteTexture
* @type {WebGLTexture}
* @readonly
* @since 3.50.0
*/
this.whiteTexture = null;
/**
* The total number of masks currently stacked.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#maskCount
* @type {number}
* @since 3.17.0
*/
this.maskCount = 0;
/**
* The mask stack.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#maskStack
* @type {Phaser.Display.Masks.GeometryMask[]}
* @since 3.17.0
*/
this.maskStack = [];
/**
* Internal property that tracks the currently set mask.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentMask
* @type {any}
* @since 3.17.0
*/
this.currentMask = { mask: null, camera: null };
/**
* Internal property that tracks the currently set camera mask.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentCameraMask
* @type {any}
* @since 3.17.0
*/
this.currentCameraMask = { mask: null, camera: null };
/**
* Internal gl function mapping for uniform look-up.
* https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/uniform
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#glFuncMap
* @type {any}
* @since 3.17.0
*/
this.glFuncMap = null;
/**
* The `type` of the Game Object being currently rendered.
* This can be used by advanced render functions for batching look-ahead.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#currentType
* @type {string}
* @since 3.19.0
*/
this.currentType = '';
/**
* Is the `type` of the Game Object being currently rendered different than the
* type of the object before it in the display list? I.e. it's a 'new' type.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#newType
* @type {boolean}
* @since 3.19.0
*/
this.newType = false;
/**
* Does the `type` of the next Game Object in the display list match that
* of the object being currently rendered?
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#nextTypeMatch
* @type {boolean}
* @since 3.19.0
*/
this.nextTypeMatch = false;
/**
* Is the Game Object being currently rendered the final one in the list?
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#finalType
* @type {boolean}
* @since 3.50.0
*/
this.finalType = false;
/**
* The mipmap magFilter to be used when creating textures.
*
* You can specify this as a string in the game config, i.e.:
*
* `renderer: { mipmapFilter: 'NEAREST_MIPMAP_LINEAR' }`
*
* The 6 options for WebGL1 are, in order from least to most computationally expensive:
*
* NEAREST (for pixel art)
* LINEAR (the default)
* NEAREST_MIPMAP_NEAREST
* LINEAR_MIPMAP_NEAREST
* NEAREST_MIPMAP_LINEAR
* LINEAR_MIPMAP_LINEAR
*
* Mipmaps only work with textures that are fully power-of-two in size.
*
* For more details see https://webglfundamentals.org/webgl/lessons/webgl-3d-textures.html
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#mipmapFilter
* @type {GLenum}
* @since 3.21.0
*/
this.mipmapFilter = null;
/**
* The number of times the renderer had to flush this frame, due to running out of texture units.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#textureFlush
* @type {number}
* @since 3.50.0
*/
this.textureFlush = 0;
/**
* Are the WebGL Textures in their default state?
*
* Used to avoid constant gl binds.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#isTextureClean
* @type {boolean}
* @since 3.51.0
*/
this.isTextureClean = false;
/**
* The default scissor, set during `preRender` and modified during `resize`.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#defaultScissor
* @type {number[]}
* @private
* @since 3.50.0
*/
this.defaultScissor = [ 0, 0, 0, 0 ];
/**
* Has this renderer fully booted yet?
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#isBooted
* @type {boolean}
* @since 3.50.0
*/
this.isBooted = false;
/**
* A Render Target you can use to capture the current state of the Renderer.
*
* A Render Target encapsulates a framebuffer and texture for the WebGL Renderer.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#renderTarget
* @type {Phaser.Renderer.WebGL.RenderTarget}
* @since 3.50.0
*/
this.renderTarget = null;
/**
* The global game Projection matrix, used by shaders as 'uProjectionMatrix' uniform.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#projectionMatrix
* @type {Phaser.Math.Matrix4}
* @since 3.50.0
*/
this.projectionMatrix;
/**
* The cached width of the Projection matrix.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#projectionWidth
* @type {number}
* @since 3.50.0
*/
this.projectionWidth = 0;
/**
* The cached height of the Projection matrix.
*
* @name Phaser.Renderer.WebGL.WebGLRenderer#projectionHeight
* @type {number}
* @since 3.50.0
*/
this.projectionHeight = 0;
this.init(this.config);
},
/**
* Creates a new WebGLRenderingContext and initializes all internal state.
*
* @method Phaser.Renderer.WebGL.WebGLRenderer#init
* @since 3.0.0
*
* @param {object} config - The configuration object for the renderer.
*
* @return {this} This WebGLRenderer instance.
*/
init: function (config)
{
var gl;
var game = this.game;
var canvas = this.canvas;
var clearColor = config.backgroundColor;
// Did they provide their own context?
if (game.config.context)
{
gl = game.config.context;
}
else
{
gl = canvas.getContext('webgl', config.contextCreation) || canvas.getContext('experimental-webgl', config.contextCreation);
}
if (!gl || gl.isContextLost())
{
this.contextLost = true;
throw new Error('WebGL unsupported');
}
this.gl = gl;
var _this = this;
this.contextLostHandler = function (event)
{
_this.contextLost = true;
_this.game.events.emit(GameEvents.CONTEXT_LOST, _this);
event.preventDefault();
};
this.contextRestoredHandler = function ()
{
_this.contextLost = false;
_this.init(_this.config);
_this.game.events.emit(GameEvents.CONTEXT_RESTORED, _this);
};
canvas.addEventListener('webglcontextlost', this.contextLostHandler, false);
canvas.addEventListener('webglcontextrestored', this.contextRestoredHandler, false);
// Set it back into the Game, so developers can access it from there too
game.context = gl;
for (var i = 0; i <= 27; i++)
{
this.blendModes.push({ func: [ gl.ONE, gl.ONE_MINUS_SRC_ALPHA ], equation: gl.FUNC_ADD });
}
// ADD
this.blendModes[1].func = [ gl.ONE, gl.DST_ALPHA ];
// MULTIPLY
this.blendModes[2].func = [ gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA ];
// SCREEN
this.blendModes[3].func = [ gl.ONE, gl.ONE_MINUS_SRC_COLOR ];
// ERASE
this.blendModes[17] = { func: [ gl.ZERO, gl.ONE_MINUS_SRC_ALPHA ], equation: gl.FUNC_REVERSE_SUBTRACT };
this.glFormats[0] = gl.BYTE;
this.glFormats[1] = gl.SHORT;
this.glFormats[2] = gl.UNSIGNED_BYTE;
this.glFormats[3] = gl.UNSIGNED_SHORT;
this.glFormats[4] = gl.FLOAT;
// Set the gl function map
this.glFuncMap = {
mat2: { func: gl.uniformMatrix2fv, length: 1, matrix: true },
mat3: { func: gl.uniformMatrix3fv, length: 1, matrix: true },
mat4: { func: gl.uniformMatrix4fv, length: 1, matrix: true },
'1f': { func: gl.uniform1f, length: 1 },
'1fv': { func: gl.uniform1fv, length: 1 },
'1i': { func: gl.uniform1i, length: 1 },
'1iv': { func: gl.uniform1iv, length: 1 },
'2f': { func: gl.uniform2f, length: 2 },
'2fv': { func: gl.uniform2fv, length: 1 },
'2i': { func: gl.uniform2i, length: 2 },
'2iv': { func: gl.uniform2iv, length: 1 },
'3f': { func: gl.uniform3f, length: 3 },
'3fv': { func: gl.uniform3fv, length: 1 },
'3i': { func: gl.uniform3i, length: 3 },
'3iv': { func: gl.uniform3iv, length: 1 },
'4f': { func: gl.uniform4f, length: 4 },
'4fv': { func: gl.uniform4fv, length: 1 },
'4i': { func: gl.uniform4i, length: 4 },
'4iv': { func: gl.uniform4iv, length: 1 }
};
// Load supported extensions
var exts = gl.getSupportedExtensions();
if (!config.maxTextures || config.maxTextures === -1)
{
config.maxTextures = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
}
if (!config.maxTextureSize)
{
config.maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
}
var extString = 'WEBGL_compressed_texture_';
var wkExtString = 'WEBKIT_' + extString;
this.compression.ETC1 = gl.getExtension(extString + 'etc1') || gl.getExtension(wkExtString + 'etc1');
this.compression.PVRTC = gl.getExtension(extString + 'pvrtc') || gl.getExtension(wkExtString + 'pvrtc');
this.compression.S3TC = gl.getExtension(extString + 's3tc') || gl.getExtension(wkExtString + 's3tc');
this.supportedExtensions = exts;
var angleString = 'ANGLE_instanced_arrays';
this.instancedArraysExtension = (exts.indexOf(angleString) > -1) ? gl.getExtension(angleString) : null;
var vaoString = 'OES_vertex_array_object';
this.vaoExtension = (exts.indexOf(vaoString) > -1) ? gl.getExtension(vaoString) : null;
// Setup initial WebGL state
gl.disable(gl.DEPTH_TEST);
gl.disable(gl.CULL_FACE);
gl.enable(gl.BLEND);
gl.clearColor(clearColor.redGL, clearColor.greenGL, clearColor.blueGL, clearColor.alphaGL);
// Mipmaps
this.mipmapFilter = gl[config.mipmapFilter];
// Check maximum supported textures
this.maxTextures = Utils.checkShaderMax(gl, config.maxTextures);
this.textureIndexes = [];
// Create temporary WebGL textures
var tempTextures = this.tempTextures;
if (Array.isArray(tempTextures))
{
for (var t = 0; i < this.maxTextures; t++)
{
gl.deleteTexture(tempTextures[t]);
}
}
else
{
tempTextures = new Array(this.maxTextures);
}
// Create temp textures to stop WebGL errors on mac os
for (var index = 0; index < this.maxTextures; index++)
{
var tempTexture = gl.createTexture();
gl.activeTexture(gl.TEXTURE0 + index);
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([ 0, 0, 255, 255 ]));
tempTextures[index] = tempTexture;
this.textureIndexes.push(index);
}
this.tempTextures = tempTextures;
// Reset to texture 1 (texture zero is reserved for framebuffers)
this.currentActiveTexture = 1;
this.startActiveTexture++;
gl.activeTexture(gl.TEXTURE1);
this.pipelines = new PipelineManager(this);
this.setBlendMode(CONST.BlendModes.NORMAL);
this.projectionMatrix = new Matrix4().identity();
game.textures.once(TextureEvents.READY, this.boot, this);
return this;
},
/**
* Internal boot handler. Calls 'boot' on each pipeline.
*
* @method Phaser.Renderer.WebGL.WebGLRenderer#boot
* @private
* @since 3.11.0
*/
boot: function ()
{
var game = this.game;
var pipelineManager = this.pipelines;
var baseSize = game.scale.baseSize;
this.width = baseSize.width;
this.height = baseSize.height;
this.isBooted = true;
this.renderTarget = new RenderTarget(this, this.width, this.height, 1, 0, true, true);
// Set-up pipelines
pipelineManager.boot(game.config.pipeline);
// Set-up default textures, fbo and scissor
this.blankTexture = game.textures.getFrame('__DEFAULT');
this.whiteTexture = game.textures.getFrame('__WHITE');
var gl = this.gl;
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
gl.enable(gl.SCISSOR_TEST);
game.scale.on(ScaleEvents.RESIZE, this.onResize, this);
this.resize(baseSize.width, baseSize.height);
},
/**
* The event handler that manages the `resize` event dispatched by the Scale Manager.
*
* @method Phaser.Renderer.WebGL.WebGLRenderer#onResize
* @since 3.16.0
*
* @param {Phaser.Structs.Size} gameSize - The default Game Size object. This is the un-modified game dimensions.
* @param {Phaser.Structs.Size} baseSize - The base Size object. The game dimensions. The canvas width / height values match this.
*/
onResize: function (gameSize, baseSize)
{
// Has the underlying canvas size changed?
if (baseSize.width !== this.width || baseSize.height !== this.height)
{
this.resize(baseSize.width, baseSize.height);
}
},
/**
* Binds the WebGL Renderers Render Target, so all drawn content is now redirected to it.
*
* Make sure to call `endCapture` when you are finished.
*
* @method Phaser.Renderer.WebGL.WebGLRenderer#beginCapture
* @since 3.50.0
*
* @param {number} [width] - Optional new width of the Render Target.
* @param {number} [height] - Optional new height of the Render Target.
*/
beginCapture: function (width, height)
{
if (width === undefined) { width = this.width; }
if (height === undefined) { height = this.height; }
this.renderTarget.bind(true, width, height);
this.setProjectionMatrix(width, height);
this.resetTextures();
},
/**
* Unbinds the WebGL Renderers Render Target and returns it, stopping any further content being drawn to it.
*
* If the viewport or scissors were modified during the capture, you should reset them by calling
* `resetViewport` and `resetScissor` accordingly.
*
* @method Phaser.Renderer.WebGL.WebGLRenderer#endCapture
* @since 3.50.0
*
* @return {Phaser.Renderer.WebGL.RenderTarget} A reference to the WebGL Renderer Render Target.
*/
endCapture: function ()
{
this.renderTarget.unbind(true);
this.resetProjectionMatrix();
return this.renderTarget;
},
/**
* Resizes the drawing buffer to match that required by the Scale Manager.
*
* @method Phaser.Renderer.WebGL.WebGLRenderer#resize
* @fires Phaser.Renderer.Events#RESIZE
* @since 3.0.0
*
* @param {number} [width] - The new width of the renderer.
* @param {number} [height] - The new height of the renderer.
*
* @return {this} This WebGLRenderer instance.
*/
resize: function (width, height)
{
var gl = this.gl;
this.width = width;
this.height = height;
this.setProjectionMatrix(width, height);
gl.viewport(0, 0, width, height);
this.drawingBufferHeight = gl.drawingBufferHeight;
gl.scissor(0, (gl.drawingBufferHeight - height), width, height);
this.defaultScissor[2] = width;
this.defaultScissor[3] = height;
this.emit(Events.RESIZE, width, height);
return this;
},