-
Notifications
You must be signed in to change notification settings - Fork 0
/
glState.js
1858 lines (1727 loc) · 75.2 KB
/
glState.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
/**
* Everything that is returned as a handle
* @typedef {WebGLBuffer | WebGLTexture | null} HandleObj
*/
/**
* WebGLAbstraction
* @typedef {WebGLRenderingContext | WebGL2RenderingContext} WebGL
*/
/**
* the parsed json version of the Handle class
* @typedef {{handle: number}} JSONHandle
*/
/**
* the parsed version of the NamedParameter class
* @template T
* @typedef {{name: string, value: T}} JSONNamedParameter
*/
/**
* the parsed version of the NamedNumberParameter class
* @typedef {{name: string, value: GLenum | GLint | GLfloat}} JSONNamedNumberParameter
*/
/**
* the parsed version of the NamedStringParameter class
* @typedef {{name: string, value: string}} JSONNamedStringParameter
*/
/**
* the parsed version of the NamedBooleanParameter class
* @typedef {{name: string, value: boolean}} JSONNamedBooleanParameter
*/
/**
* the parsed version of the NamedBooleanArrayParameter class
* @typedef {{name: string, value: Array<boolean>}} JSONNamedBooleanArrayParameter
*/
/**
* the parsed version of the NamedInt32ArrayParameter class
* @typedef {{name: string, value: Int32Array}} JSONNamedInt32ArrayParameter
*/
/**
* the parsed version of the NamedFloat32ArrayParameter class
* @typedef {{name: string, value: Float32Array}} JSONNamedFloat32ArrayParameter
*/
/**
* the parsed version of the NamedHandleParameter class
* @typedef {{name: string, value: JSONHandle}} JSONNamedHandleParameter
*/
/**
* the parsed version of the TypedParameter class
* @typedef {{name: string, type: string, value: GLenum | GLint | GLfloat}} JSONTypedParameter
*/
/**
* the parsed version of the TextureState class
* @typedef {{target: GLenum, texture: JSONHandle, parameters: {[k: string]: TypedParameter}}} JSONTextureState
*/
/**
* the parsed version of the NamedTextureStateParameter class
* @typedef {{name: string, value: JSONTextureState}} JSONNamedTextureStateParameter
*/
/**
* the parsed version of the TextureUnitState class
* @typedef {{textureUnit: GLenum, targets: {[k: string]: JSONNamedTextureStateParameter}}} JSONTextureUnitState
*/
/**
* the parsed version of the NamedTextureUnitStateParameter class
* @typedef {{name: string, value: JSONTextureUnitState}} JSONNamedTextureUnitStateParameter
*/
/**
* the parsed version of the GLState class
* @typedef {{activeTexture: Handle, parameters: Object<string, any>, textureBinds: Object<string, any>, unclonables: Object<string, Handle>}} JSONGLState
*/
/**
* the parsed version of the DeviceDescription class
* @typedef {{supportedExtensions: string[], parameters: {}, shaderPrecisionFormats: {}, extColorBufferHalfFloat: {}, extTextureFilterAnisotropic: {}, webglDebugRendererInfo: {}}} JSONDeviceDescription
*/
/**
* @typedef {{name: string, args:any[], handle: Handle}} Command
*/
/**
* @typedef {{workerId: number, commandBufferId: number, isRendering: boolean, commands: Command[]}} CommandBuffer
*/
/**
* every object we can copy
* @template T
*/
class Clonable {
constructor() {
}
/**
* @abstract
* @returns {T}
*/
clone() {
throw new Error("Calling abstract function");
}
}
/**
* contains an id identiying an internal object that can't be transfered to the client
* @extends Clonable<Handle>
*/
class Handle extends Clonable {
/**
* @param {number} handle
*/
constructor(handle) {
super();
/**
* @type {handle}
*/
this.handle = handle
}
/**
* @override
* @returns {Handle}
*/
clone() {
return new Handle(this.handle);
}
/**
* @returns {JSONHandle}
*/
toJSON() {
return {handle: this.handle};
}
/**
*
* @param {JSONHandle} json
* @returns {Handle}
*/
static fromJSON(json) {
return new Handle(json.handle);
}
valueOf() {
return this.handle;
}
}
/**
* base class of a opengl state parameter with a humn readable name, for easy debugging
* T is the elementary type, while R represents the subclass used when a copy is made
* @template T
* @template R
* @extends Clonable<R>
*/
class BaseNamedParameter extends Clonable {
/**
*
* @param {string} name
* @param {T} value
*/
constructor(name, value) {
super();
/**
* @type {string}
*/
this.name = name;
/**
* @type {T}
*/
this.value = value;
}
}
/**
* parameter class for all number based webgl state parameters
* @extends {BaseNamedParameter<GLenum | GLint | GLfloat, NamedNumberParameter>}
*/
class NamedNumberParameter extends BaseNamedParameter {
/**
*
* @param {string} name
* @param {GLenum | GLint | GLfloat} value
*/
constructor(name, value) {
super(name, value);
}
/**
* @override
* @returns {NamedNumberParameter}
*/
clone() {
return new NamedNumberParameter(this.name, this.value);
}
/**
* @returns {JSONNamedNumberParameter}
*/
toJSON() {
return {name: this.name, value: this.value};
}
/**
* @param {JSONNamedNumberParameter} json
* @returns {NamedNumberParameter}
*/
static fromJSON(json) {
return new NamedNumberParameter(json.name, json.value);
}
}
/**
* class for all boolean based webgl state parameters
* @extends {BaseNamedParameter<boolean, NamedBooleanParameter>}
*/
class NamedBooleanParameter extends BaseNamedParameter {
/**
*
* @param {string} name
* @param {boolean} value
*/
constructor(name, value) {
super(name, value);
}
/**
* @override
* @returns {NamedBooleanParameter}
*/
clone() {
return new NamedBooleanParameter(this.name, this.value);
}
/**
* @returns {JSONNamedBooleanParameter}
*/
toJSON() {
return {name: this.name, value: this.value};
}
/**
* @param {JSONNamedBooleanParameter} json
* @returns {NamedBooleanParameter}
*/
static fromJSON(json) {
return new NamedBooleanParameter(json.name, json.value);
}
}
/**
* class for all string based webgl state parameters
* @extends {BaseNamedParameter<string, NamedStringParameter>}
*/
class NamedStringParameter extends BaseNamedParameter {
/**
*
* @param {string} name
* @param {string} value
*/
constructor(name, value) {
super(name, value);
}
/**
* @override
* @returns {NamedStringParameter}
*/
clone() {
return new NamedStringParameter(this.name, this.value);
}
/**
* @returns {JSONNamedStringParameter}
*/
toJSON() {
return {name: this.name, value: this.value};
}
/**
* @param {JSONNamedStringParameter} json
* @returns {NamedStringParameter}
*/
static fromJSON(json) {
return new NamedStringParameter(json.name, json.value);
}
}
/**
* class for all boolean[] based webgl state parameters
* @extends {BaseNamedParameter<Array<boolean>, NamedBooleanArrayParameter>}
*/
class NamedBooleanArrayParameter extends BaseNamedParameter{
/**
*
* @param {string} name
* @param {Array<boolean>} value
*/
constructor(name, value) {
super(name, value);
}
/**
* @override
* @returns {NamedBooleanArrayParameter}
*/
clone() {
return new NamedBooleanArrayParameter(this.name, [...this.value]);
}
/**
*
* @returns {JSONNamedBooleanArrayParameter}
*/
toJSON() {
return {name: this.name, value: [...this.value]};
}
/**
*
* @param {JSONNamedBooleanArrayParameter} json
* @returns {NamedBooleanArrayParameter}
*/
static fromJSON(json) {
return new NamedBooleanArrayParameter(json.name, [...json.value]);
}
}
/**
* class for all Int32Array based webglstate parameters
* @extends {BaseNamedParameter<Int32Array, NamedInt32ArrayParameter>}
*/
class NamedInt32ArrayParameter extends BaseNamedParameter{
/**
*
* @param {string} name
* @param {Int32Array} value
*/
constructor(name, value) {
super(name, value);
}
/**
* @override
* @returns {NamedInt32ArrayParameter}
*/
clone() {
return new NamedInt32ArrayParameter(this.name, new Int32Array(this.value));
}
/**
*
* @returns {JSONNamedInt32ArrayParameter}
*/
toJSON() {
return {name: this.name, value: new Int32Array(this.value)};
}
/**
*
* @param {JSONNamedInt32ArrayParameter} json
* @returns {NamedInt32ArrayParameter}
*/
static fromJSON(json) {
return new NamedInt32ArrayParameter(json.name, new Int32Array(json.value));
}
}
/**
* class for all Float32[] based webgl state parameters
* @extends {BaseNamedParameter<Float32Array, NamedFloat32ArrayParameter>}
*/
class NamedFloat32ArrayParameter extends BaseNamedParameter{
/**
*
* @param {string} name
* @param {Float32Array} value
*/
constructor(name, value) {
super(name, value);
}
/**
* @override
* @returns {NamedFloat32ArrayParameter}
*/
clone() {
return new NamedFloat32ArrayParameter(this.name, new Float32Array(this.value));
}
/**
*
* @returns {JSONNamedFloat32ArrayParameter}
*/
toJSON() {
return {name: this.name, value: new Float32Array(this.value)};
}
/**
*
* @param {JSONNamedFloat32ArrayParameter} json
* @returns {NamedFloat32ArrayParameter}
*/
static fromJSON(json) {
return new NamedFloat32ArrayParameter(json.name, new Float32Array(json.value));
}
}
/**
* class for all texturestate based webgl state parameters
* @extends {BaseNamedParameter<TextureState, NamedTextureStateParameter>}
*/
class NamedTextureStateParameter extends BaseNamedParameter {
/**
*
* @param {string} name
* @param {TextureState} value
*/
constructor(name, value) {
super(name, value);
}
/**
* @override
* @returns {NamedTextureStateParameter}
*/
clone() {
return new NamedTextureStateParameter(this.name, this.value.clone());
}
/**
*
* @returns {JSONNamedTextureStateParameter}
*/
toJSON() {
return {name: this.name, value: this.value.toJSON()};
}
/**
*
* @param {WebGL} gl
* @param {Map<number, HandleObj>} unclonables
* @param {JSONNamedTextureStateParameter} json
* @returns {NamedTextureStateParameter}
*/
static fromJSON(gl, unclonables, json) {
return new NamedTextureStateParameter(json.name, TextureState.fromJSON(gl, unclonables, json.value));
}
}
/**
* class for all textureunit webgl state based parameters
* @extends {BaseNamedParameter<TextureUnitState, NamedTextureUnitStateParameter>}
*/
class NamedTextureUnitStateParameter extends BaseNamedParameter {
/**
*
* @param {string} name
* @param {TextureUnitState} value
*/
constructor(name, value) {
super(name, value);
}
/**
* @override
* @returns {NamedTextureUnitStateParameter}
*/
clone() {
return new NamedTextureUnitStateParameter(this.name, this.value.clone());
}
/**
*
* @returns {JSONNamedTextureUnitStateParameter}
*/
toJSON() {
return {name: this.name, value: this.value.toJSON()};
}
/**
*
* @param {WebGL} gl
* @param {Map<number, HandleObj>} unclonables
* @param {JSONNamedTextureUnitStateParameter} json
* @returns {NamedTextureUnitStateParameter}
*/
static fromJSON(gl, unclonables, json) {
return new NamedTextureUnitStateParameter(json.name, TextureUnitState.fromJSON(gl, unclonables, json.value));
}
}
/**
* class for all types of internal objects in the webgl state parameter dictionary
* @extends {BaseNamedParameter<Handle, NamedHandleParameter>}
*/
class NamedHandleParameter extends BaseNamedParameter {
/**
*
* @param {string} name
* @param {Handle} value
*/
constructor(name, value) {
super(name, value);
}
/**
* @override
* @returns {NamedHandleParameter}
*/
clone() {
return new NamedHandleParameter(this.name, this.value.clone());
}
/**
*
* @returns {JSONNamedHandleParameter}
*/
toJSON() {
return {name: this.name, value: this.value.toJSON()};
}
/**
* @param {JSONNamedHandleParameter} json
* @returns {NamedHandleParameter}
*/
static fromJSON(json) {
return new NamedHandleParameter(json.name, Handle.fromJSON(json.value));
}
}
/**
* class for all number types webgl state parameters
* @extends BaseNamedParameter<number, TypedParameter>
*/
class TypedParameter extends BaseNamedParameter {
/**
*
* @param {string} name
* @param {string} type
* @param {GLenum | GLint | GLfloat} value
*/
constructor(name, type, value) {
super(name, value)
/**
* @type {string}
*/
this.type = type;
}
/**
*
* @returns {TypedParameter}
*/
clone() {
return new TypedParameter(this.name, this.type, this.value);
}
/**
* @returns {JSONTypedParameter}
*/
toJSON() {
return {name: this.name, type: this.type, value: this.value};
}
/**
* @param {JSONTypedParameter} json
* @returns {TypedParameter}
*/
static fromJSON(json) {
return new TypedParameter(json.name, json.type, json.value);
}
}
/**
* tracks the state of a texture bound to a texture unit
*/
class TextureState {
// recognized webgl parameters
// WebGL 1.0
static TEXTURE_MAG_FILTER = 0x2800;
static TEXTURE_MIN_FILTER = 0x2801;
static TEXTURE_WRAP_S = 0x2802;
static TEXTURE_WRAP_T = 0x2803;
// EXT_texture_filter_anisotropic
static TEXTURE_MAX_ANISOTROPY_EXT = 0x84FE;
// WebGL 2.0
static TEXTURE_BASE_LEVEL = 0x813C;
static TEXTURE_COMPARE_FUNC = 0x884D;
static TEXTURE_COMPARE_MODE = 0x884C;
static TEXTURE_MAX_LEVEL = 0x813D;
static TEXTURE_MAX_LOD = 0x813B;
static TEXTURE_MIN_LOD = 0x813A;
static TEXTURE_WRAP_R = 0x8072;
// values needed for webgl agnostic initialization
static LEQUAL = 0x203;
static LINEAR = 0x2601;
static NEAREST_MIPMAP_LINEAR = 0x2702;
static NONE = 0x0;
static REPEAT = 0x2900;
/**
*
* @param {WebGL} gl
* @param {GLenum} target
* @param {Map<number, HandleObj>} unclonables
*/
constructor(gl, target, unclonables) {
/**
* @type {WebGL}
*/
this.gl = gl;
/**
* @type {Map<number, HandleObj>}
*/
this.unclonables = unclonables;
/**
* @type {GLenum}
*/
this.target = target;
/**
* @type {Handle}
*/
this.texture = TextureState.getTextureHandle(null, unclonables);
/**
* @type {Map<GLenum, TypedParameter>}
*/
this.parameters = new Map();
// WebGL 1.0
this.parameters.set(TextureState.TEXTURE_MAG_FILTER, new TypedParameter("TEXTURE_MAG_FILTER", "i", TextureState.LINEAR));
this.parameters.set(TextureState.TEXTURE_MIN_FILTER, new TypedParameter("TEXTURE_MIN_FILTER", "i", TextureState.NEAREST_MIPMAP_LINEAR));
this.parameters.set(TextureState.TEXTURE_WRAP_S, new TypedParameter("TEXTURE_WRAP_S", "i", TextureState.REPEAT));
this.parameters.set(TextureState.TEXTURE_WRAP_T, new TypedParameter("TEXTURE_WRAP_T", "i", TextureState.REPEAT));
// EXT_texture_filter_anisotropic
this.parameters.set(TextureState.TEXTURE_MAX_ANISOTROPY_EXT, new TypedParameter("MAX_TEXTURE_MAX_ANISOTROPY_EXT", "f", 1.0));
// WebGL 2.0
this.parameters.set(TextureState.TEXTURE_BASE_LEVEL, new TypedParameter("TEXTURE_BASE_LEVEL", "i", 0));
this.parameters.set(TextureState.TEXTURE_COMPARE_FUNC, new TypedParameter("TEXTURE_COMPARE_FUNC", "i", TextureState.LEQUAL));
this.parameters.set(TextureState.TEXTURE_COMPARE_MODE, new TypedParameter("TEXTURE_COMPARE_MODE", "i", TextureState.NONE));
this.parameters.set(TextureState.TEXTURE_MAX_LEVEL, new TypedParameter("TEXTURE_MAX_LEVEL", "i", 1000));
this.parameters.set(TextureState.TEXTURE_MAX_LOD, new TypedParameter("TEXTURE_MAX_LOD", "f", 1000.0));
this.parameters.set(TextureState.TEXTURE_MIN_LOD, new TypedParameter("TEXTURE_MIN_LOD", "f", -1000.0));
this.parameters.set(TextureState.TEXTURE_WRAP_R, new TypedParameter("TEXTURE_WRAP_R", "i", TextureState.REPEAT));
}
/**
*
* @returns {TextureState}
*/
clone() {
let ret = new TextureState(this.gl, this.target, this.unclonables);
ret.texture = this.texture;
this.parameters.forEach((value, key) => ret.parameters.set(key, value.clone()));
return ret;
}
/**
*
* @returns {JSONTextureState}
*/
toJSON() {
return {
target: this.target,
texture: this.texture,
parameters: Object.fromEntries(this.parameters.entries())
};
}
/**
*
* @param {WebGL} gl
* @param {Map<number, HandleObj>} unclonables
* @param {JSONTextureState} json
* @returns {TextureState}
*/
static fromJSON(gl, unclonables, json) {
let ret = new TextureState(gl, json.target, unclonables);
ret.texture = Handle.fromJSON(json.texture);
for (let [key, value] of ret.parameters.entries()) {
value.value = Reflect.get(json.parameters, key).value;
}
return ret;
}
/**
* creates a new texture handle (serverside so negative values)
* it tries to search if a handle already exists for a server side object and reuses it if it finds it
* @param {WebGLTexture | null} texture
* @param {Map<number, HandleObj>} unclonables
* @returns {Handle}
*/
static getTextureHandle(texture, unclonables) {
let ret_handle = 0;
// does the object already have a handle
for (const [handle, obj] of unclonables) {
if (texture === obj) {
ret_handle = handle;
break;
}
}
if (ret_handle === 0) {
// create a new internal handle (always negative)
let minValue = Math.min(...unclonables.keys());
ret_handle = ((!isFinite(minValue)) || (minValue > 0)) ? -1 : minValue - 1;
unclonables.set(ret_handle, texture);
}
return new Handle(ret_handle);
}
/**
* copies the current state from a webgl texturestate
* @param {WebGL} gl the webgl context to copy from
* @param {GLenum} target the target to copy
* @param {Map<number, HandleObj>} unclonables the list of known handles
* @returns {TextureState}
*/
static createFromGLContext(gl, target, unclonables) {
let ret = new TextureState(gl, target, unclonables);
let targetToTargetBinding = new Map();
targetToTargetBinding.set(gl.TEXTURE_2D, gl.TEXTURE_BINDING_2D);
targetToTargetBinding.set(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_BINDING_CUBE_MAP);
// add "|| gl.hasOwnProperty("TEXTURE_3D")" here to support khronos's own debug layer
if (gl instanceof WebGL2RenderingContext || gl.hasOwnProperty("TEXTURE_3D")) {
targetToTargetBinding.set(gl.TEXTURE_3D, gl.TEXTURE_BINDING_3D);
targetToTargetBinding.set(gl.TEXTURE_2D_ARRAY, gl.TEXTURE_BINDING_2D_ARRAY);
}
const texture = gl.getParameter(targetToTargetBinding.get(target));
this.texture = TextureState.getTextureHandle(texture, unclonables);
if (texture !== null) {
ret.parameters.forEach((parameter, key) => parameter.value = gl.getTexParameter(target, key));
}
return ret;
}
/**
* probes a command to see if it affects the state of this texture
* @param {Command} command
*/
processOneCommand(command) {
if (command.name === "bindTexture") {
this.texture = command.args[1];
}
}
/**
* applies this texture state to the current webgl context without checking the current state of the webglcontext
*/
forceApply() {
const texture = this.unclonables.get(this.texture.handle);
if (texture !== undefined) {
this.gl.bindTexture(this.target, texture);
if (this.unclonables.get(this.texture.handle) !== null) {
this.parameters.forEach((parameter, key) => {
if (parameter.type === "i") {
this.gl.texParameteri(this.target, key, parameter.value)
} else if (parameter.type === "f") {
this.gl.texParameterf(this.target, key, parameter.value)
}
});
}
}
}
/**
* make changes based on the given state and the current desired texture state stored in this object
* @param {TextureState} other
*/
apply(other) {
if (other.texture !== this.texture) {
const texture = this.unclonables.get(this.texture.handle);
if (texture !== undefined) {
let ret = this.gl.bindTexture(this.target, texture);
}
}
if (this.unclonables.get(this.texture.handle) !== null) {
this.parameters.forEach((parameter, key) => {
let otherParameter = other.parameters.get(key);
if (otherParameter && otherParameter.value !== parameter.value) {
if (parameter.type === "i") {
this.gl.texParameteri(this.target, key, parameter.value)
} else if (parameter.type === "f") {
this.gl.texParameterf(this.target, key, parameter.value)
}
}
});
}
}
}
/**
* tracks the state of a texture unit in opengl
*/
class TextureUnitState {
// recognized webgl targets
// WebGL 1.0
static TEXTURE_2D = 0x0DE1;
static TEXTURE_CUBE_MAP = 0x8513;
// WebGL 2.0
static TEXTURE_2D_ARRAY = 0x8C1A
static TEXTURE_3D = 0x806F;
/**
*
* @param {WebGL} gl the glcontext
* @param {GLenum} textureUnit the texture unit for which to store state
* @param {Map<number, HandleObj>} unclonables list of internal objects
*/
constructor(gl, textureUnit, unclonables) {
/**
* @type {WebGL}
*/
this.gl = gl;
/**
* @type GLenum
*/
this.textureUnit = textureUnit;
/**
* @type {Map<number, HandleObj>}
*/
this.unclonables = unclonables;
/**
* @type {Map<GLenum, NamedTextureStateParameter>}
*/
this.targets = new Map();
// WebGL 1.0
this.targets.set(TextureUnitState.TEXTURE_2D, new NamedTextureStateParameter("TEXTURE_2D", new TextureState(this.gl, TextureUnitState.TEXTURE_2D, unclonables)));
this.targets.set(TextureUnitState.TEXTURE_CUBE_MAP, new NamedTextureStateParameter("TEXTURE_CUBE_MAP", new TextureState(this.gl, TextureUnitState.TEXTURE_CUBE_MAP, unclonables)));
// WebGL 2.0
this.targets.set(TextureUnitState.TEXTURE_2D_ARRAY, new NamedTextureStateParameter("TEXTURE_2D_ARRAY", new TextureState(this.gl, TextureUnitState.TEXTURE_2D_ARRAY, unclonables)));
this.targets.set(TextureUnitState.TEXTURE_3D, new NamedTextureStateParameter("TEXTURE_3D", new TextureState(this.gl, TextureUnitState.TEXTURE_3D, unclonables)));
}
/**
* Creates a deep copy of the class
* @returns {TextureUnitState} Deep copy
*/
clone() {
let ret = new TextureUnitState(this.gl, this.textureUnit, this.unclonables);
ret.targets.forEach((parameter, key) => {
const thisParameter = this.targets.get(key);
if (thisParameter) {
parameter.value = thisParameter.value.clone();
}
});
return ret;
}
/**
*
* @returns {JSONTextureUnitState}
*/
toJSON() {
return {
textureUnit: this.textureUnit,
targets: Object.fromEntries(Array.from(this.targets.entries()).map(([key, value]) => {
return [key, value.toJSON()];
}))
};
}
/**
*
* @param {WebGL} gl
* @param {Map<number, HandleObj>} unclonables
* @param {JSONTextureUnitState} json
* @returns {TextureUnitState}
*/
static fromJSON(gl, unclonables, json) {
let ret = new TextureUnitState(gl, json.textureUnit, unclonables);
for (let key of ret.targets.keys()) {
ret.targets.set(key, NamedTextureStateParameter.fromJSON(gl, unclonables, Reflect.get(json.targets, key)));
}
return ret;
}
/**
* Copies the current state from the gl context
* @param {WebGL} gl - The context to copy from
* @param {GLenum} activeTexture - The current active texture on the gl context
* @param {Map<number, HandleObj>} unclonables list of internal objects
* @returns A new TextureUnitState instance corresponding to the given gl context
*/
static createFromGLContext(gl, activeTexture, unclonables) {
let ret = new TextureUnitState(gl, activeTexture, unclonables);
ret.targets.forEach((target, key) => target.value = TextureState.createFromGLContext(gl, key, unclonables));
return ret;
}
/**
* probes a command to see if it influences the state of this texture unit
* @param {Command} command
*/
processOneCommand(command) {
if (command.name === "bindTexture") {
let target = this.targets.get(command.args[0]);
if (target !== undefined) {
target.value.processOneCommand(command);
}
}
}
/**
* activate the current texture unit on the gl context
* @returns the current activated texture unit
*/
activate() {
const actTex = this.gl.getParameter(this.gl.ACTIVE_TEXTURE);
if (actTex !== this.textureUnit) {
this.gl.activeTexture(this.textureUnit);
}
return actTex;
}
/**
* deactivate this texture unit on the webgl context
* @param {GLenum} actTex
*/
deactivate(actTex) {
if (actTex !== this.textureUnit) {
this.gl.activeTexture(actTex);
}
}
/**
* Force set all state variables, usefull if you don't know the previous state
*/
forceApply() {
const actTex = this.activate();
this.targets.forEach((target, key) => {
target.value.forceApply()
});
this.deactivate(actTex);
}
/**
* Sets the state by setting only the changed state variables
* @param {TextureUnitState} other - The currently known state of the gl context
* @returns Did the active texture change on the gl context as a result
*/
apply(other) {
let changes = false;
for (let key of this.targets.keys()) {
if (other.targets.get(key) !== this.targets.get(key)) {
changes = true;
break;
}
}
if (changes) {
const actTex = this.activate();
this.targets.forEach((target, key) => {
const otherTarget = other.targets.get(key);
if (otherTarget) {
target.value.apply(otherTarget.value);
}
});
this.deactivate(actTex);
}
}
}
/**
* this class lists information about the precision formats available in the hardware for use in shaders
*/
class ShaderPrecisionFormat {
/**
*
* @param {number} rangeMin
* @param {number} rangeMax
* @param {number} precision
*/
constructor(rangeMin, rangeMax, precision) {
/**
* @type {number}