-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl-2022.js
2182 lines (1981 loc) · 71.7 KB
/
webgl-2022.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
{
const GL = WebGL2RenderingContext; // Easier to type `GL.RGBA`.
// Let's start off strong!
WebGLRenderingContext.create = function(attribs) {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 1;
return canvas.getContext('webgl', attribs);
}
WebGL2RenderingContext.create = function(attribs) {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 1;
return canvas.getContext('webgl2', attribs);
}
// Consider these extensions core:
const WEBGL1_CORE_EXTS = [
'ANGLE_instanced_arrays',
'EXT_blend_minmax',
'OES_element_index_uint',
'OES_standard_derivatives',
'OES_vertex_array_object',
// No need to request early:
//'WEBGL_debug_renderer_info',
//'WEBGL_lose_context',
];
const WEBGL2_CORE_EXTS = [
// No need to request early:
//'WEBGL_debug_renderer_info',
//'WEBGL_lose_context',
];
// -
function* range(n) {
for (let i = 0; i < n; i++) {
yield i;
}
}
function sleepPromise(ms) {
return new Promise(go => {
setTimeout(go, ms);
});
}
// requiredArg = requiredArg || throwv('`requiredArg` is required!');
function throwv(v) {
throw v;
}
/// Prefer `invoke(() => { ... })` to `(() => { ... })()`
/// This way, it's clear up-front that we're calling not just defining.
function invoke(fn) {
return fn();
}
function replacePrefix(str, possiblePrefix, replacement) {
replacement = replacement || '';
if (!str.startsWith(possiblePrefix)) {
return str;
}
return replacement + str.substr(possiblePrefix.length);
}
// -
function kebabToUpperSnake(str) {
return str.toUpperCase().replaceAll('-', '_');
}
function upperSnakeToKebab(str) {
return str.toLowerCase().replaceAll('_', '-');
}
// -
const FULL_BY_ALIAS = {
i8: 'byte',
u8: 'unsigned-byte',
f16: 'half-float',
i16: 'short',
u16: 'unsigned-short',
f32: 'float',
i32: 'int',
u32: 'unsigned-int',
rb: 'renderbuffer',
fb: 'framebuffer',
'draw-fb': 'draw-framebuffer',
'read-fb': 'read-framebuffer',
'2d': 'texture-2d',
'cube-map': 'texture-cube-map',
'3d': 'texture-3d',
'2d-array': 'texture-2d-array',
'cube-map-plus-x': 'texture-cube-map-positive-x',
'cube-map-plus-y': 'texture-cube-map-positive-y',
'cube-map-plus-z': 'texture-cube-map-positive-z',
'cube-map-minus-x': 'texture-cube-map-negative-x',
'cube-map-minus-y': 'texture-cube-map-negative-y',
'cube-map-minus-z': 'texture-cube-map-negative-z',
};
const ALIAS_BY_FULL = Object.fromEntries(Object.entries(FULL_BY_ALIAS).map(([k,v]) => [v,k]));
WebGLRenderingContext.prototype.TEXTURE_CUBE_MAP_FACE0 = GL.TEXTURE_CUBE_MAP_POSITIVE_X;
WebGL2RenderingContext.prototype.TEXTURE_CUBE_MAP_FACE0 = GL.TEXTURE_CUBE_MAP_POSITIVE_X;
// -
const BINDING_BY_TARGET = new Map();
{
const add = (target_name, binding_name) => {
const target_val = WebGL2RenderingContext[target_name];
const binding_val = WebGL2RenderingContext[binding_name];
if (target_val) {
console.assert(binding_val, binding_name);
}
BINDING_BY_TARGET.set(target_val, binding_val);
};
add('RENDERBUFFER', 'RENDERBUFFER_BINDING');
add('TEXTURE_2D', 'TEXTURE_BINDING_2D');
add('TEXTURE_CUBE_MAP', 'TEXTURE_BINDING_CUBE_MAP');
add('TEXTURE_3D', 'TEXTURE_BINDING_3D');
add('TEXTURE_2D_ARRAY', 'TEXTURE_BINDING_2D_ARRAY');
add('FRAMEBUFFER', 'FRAMEBUFFER_BINDING');
add('DRAW_FRAMEBUFFER', 'DRAW_FRAMEBUFFER_BINDING');
add('READ_FRAMEBUFFER', 'READ_FRAMEBUFFER_BINDING');
}
// -
// toEnumVal, fromEnumVal
const TO_ENUM_VAL_CACHE = {};
function toEnumVal(inVal) {
if (typeof(inVal) != 'string') return inVal;
const cached = TO_ENUM_VAL_CACHE[inVal];
if (cached !== undefined) return cached;
let ret = inVal;
ret = FULL_BY_ALIAS[ret] || ret;
ret = kebabToUpperSnake(ret);
ret = GL[ret];
if (ret === undefined) {
console.error(`toEnumVal('${inVal}') undefined!`);
}
TO_ENUM_VAL_CACHE[inVal] = ret;
return ret;
}
const FROM_ENUM_VAL_CACHE = {};
function fromEnumVal(inVal) {
if (!inVal) return inVal;
const cached = FROM_ENUM_VAL_CACHE[inVal];
if (cached !== undefined) return cached;
for (const [k,v] of Object.entries(GL)) {
if (v == inVal) {
let ret = k;
ret = upperSnakeToKebab(ret);
ret = ALIAS_BY_FULL[ret] || ret;
FROM_ENUM_VAL_CACHE[inVal] = ret;
return ret;
}
}
console.error(`fromEnumVal(${code(inVal)}) undefined!`);
return inVal;
}
// -
// `${code(argname)} must be ${code(required)}!`
// w/ argname='foo' =>
// "`foo` must be `-1`!" OR
// "`foo` must be 'framebuffer'!"
function code(str) {
if (typeof(val) == 'string') {
return `'` + val + `'`;
}
return '`' + str + '`';
}
// -
const OBJECT_KINDS = [
'Buffer',
'Framebuffer',
'Program',
'Query',
'Renderbuffer',
'Sampler',
'Shader',
'Sync',
'Texture',
'TransformFeedback',
'VertexArray',
];
function overrideByKey(obj, key, fnNewValFromOld) {
obj[key] = fnNewValFromOld(obj[key]);
}
function overrideWebglByKey(key, fnNewValFromOld) {
overrideByKey(WebGLRenderingContext.prototype,
key, fnNewValFromOld);
overrideByKey(WebGL2RenderingContext.prototype,
key, fnNewValFromOld);
}
// -
overrideByKey(HTMLCanvasElement.prototype, 'getContext', (old) => {
return function() {
const ret = old.call(this, ...arguments);
if (ret) {
switch (arguments[0]) {
case 'webgl':
for (const name of WEBGL1_CORE_EXTS) {
ret.getExtension(name);
}
break;
case 'webgl2':
for (const name of WEBGL2_CORE_EXTS) {
ret.getExtension(name);
}
break;
}
}
return ret;
};
});
// -
// hook createObject etc. for GL_BY_OBJ.
const GL_BY_CHILD = new WeakMap();
{
const makeObserveChildReturned = (prev) => {
if (!prev) return prev; // Ignore unless exists.
return function() {
const ret = prev.call(this, ...arguments);
if (ret) {
GL_BY_CHILD.set(ret, this);
}
return ret;
};
};
for (const Kind of OBJECT_KINDS) {
overrideWebglByKey('create' + Kind, makeObserveChildReturned);
}
overrideWebglByKey('fenceSync', makeObserveChildReturned);
overrideWebglByKey('getExtension', makeObserveChildReturned);
overrideWebglByKey('getUniformLocation', makeObserveChildReturned);
}
// -
// Add obj.close() => gl.deleteObject(obj);
{
const fnNewValFromOld = (old) => {
console.assert(!old, old);
return function() {
const gl = GL_BY_CHILD.get(this);
const ret = gl[funcName](this, ...arguments);
return ret;
};
};
for (const Kind of OBJECT_KINDS) {
let str_WebGLObject = 'WebGL' + Kind;
if (Kind == 'VertexArray') {
str_WebGLObject = 'WebGLVertexArrayObject';
// Is that even standardized? :S
}
const objClass = globalThis[str_WebGLObject];
console.assert(objClass, Kind);
const str_deleteObjectName = 'delete' + Kind;
overrideByKey(objClass.prototype, 'close', (old) => {
return function() {
const gl = GL_BY_CHILD.get(this);
return gl[str_deleteObjectName](this);
};
});
}
}
// -
// Misc
overrideWebglByKey('close', (old) => {
console.assert(!old, old);
return function() {
const ext = this.getExtension('WEBGL_lose_context');
ext.lose_context();
};
});
overrideWebglByKey('getParameter', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
arguments[0] = BINDING_BY_TARGET.get(arguments[0]) || arguments[0];
switch (arguments[0]) {
case GL.RENDERER: {
if (!navigator.userAgent.includes('Firefox/')) {
// Firefox already gives this back for just RENDERER.
const ext = gl.getExtension('WEBGL_debug_renderer_info');
arguments[0] = ext.UNMASKED_RENDERER_WEBGL;
}
break;
}
case GL.VENDOR: {
const ext = gl.getExtension('WEBGL_debug_renderer_info');
arguments[0] = ext.UNMASKED_VENDOR_WEBGL;
break;
}
//case GL.DRAW_FRAMEBUFFER_BINDING: == FRAMEBUFFER_BINDING!
case GL.READ_FRAMEBUFFER_BINDING:
if (!this.READ_FRAMEBUFFER_BINDING) {
arguments[0] = GL.FRAMEBUFFER_BINDING;
}
break;
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getIndexedParameter', (old) => {
if (!old) return old;
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
arguments[0] = BINDING_BY_TARGET.get(arguments[0]) || arguments[0];
return old.call(this, ...arguments);
};
});
// -
overrideWebglByKey('activeTexture', (old) => {
return function() {
if (arguments[0] < 32) {
arguments[0] = GL.TEXTURE0 + arguments[0];
}
arguments[0] = toEnumVal(arguments[0]); // unit
return old.call(this, ...arguments);
};
});
const TARGET_BY_BUFFER = new WeakMap();
overrideWebglByKey('bindBuffer', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
if (arguments[1]) {
TARGET_BY_BUFFER.set(arguments[1], arguments[0]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('bindBufferBase', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
return old.call(this, ...arguments);
};
});
overrideWebglByKey('bindBufferRange', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
return old.call(this, ...arguments);
};
});
overrideWebglByKey('blendEquation', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('blendEquationSeparate', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('blendFunc', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('blendFuncSeparate', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('bufferData', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
arguments[2] = toEnumVal(arguments[2]); // usage
return old.call(this, ...arguments);
};
});
overrideWebglByKey('bufferSubData', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
return old.call(this, ...arguments);
};
});
overrideWebglByKey('clear', (old) => {
return function() {
if (arguments[0] == -1) {
arguments[0] = GL.COLOR_BUFFER_BIT |
GL.DEPTH_BUFFER_BIT |
GL.STENCIL_BUFFER_BIT;
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('copyBufferSubData', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // srcTarget
arguments[1] = toEnumVal(arguments[1]); // dstTarget
return old.call(this, ...arguments);
};
});
const TYPE_BY_SHADER = new WeakMap();
overrideWebglByKey('createShader', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // type
const ret = old.call(this, ...arguments);
if (ret) {
TYPE_BY_SHADER.set(ret, arguments[0]);
}
return ret;
};
});
overrideWebglByKey('cullFace', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // mode
return old.call(this, ...arguments);
};
});
overrideWebglByKey('depthFunc', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // func
return old.call(this, ...arguments);
};
});
overrideWebglByKey('disable', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // cap
return old.call(this, ...arguments);
};
});
overrideWebglByKey('enable', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // cap
return old.call(this, ...arguments);
};
});
overrideWebglByKey('setEnabled', (old) => {
return function(cap, val) {
if (val) {
this.enable(cap);
} else {
this.disable(cap);
}
};
});
overrideWebglByKey('setEnabledVertexAttribArray', (old) => {
return function(index, val) {
if (val) {
this.enableVertexAttribArray(index);
} else {
this.disableVertexAttribArray(index);
}
};
});
overrideWebglByKey('drawArrays', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // mode
return old.call(this, ...arguments);
};
});
overrideWebglByKey('drawArraysInstanced', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // mode
if (old) {
return old.call(this, ...arguments);
} else {
const ext = this.getExtension('ANGLE_instanced_arrays');
return ext.drawArraysInstancedANGLE(...arguments);
}
};
});
overrideWebglByKey('drawElements', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // mode
arguments[2] = toEnumVal(arguments[2]); // type
return old.call(this, ...arguments);
};
});
overrideWebglByKey('drawElementsInstanced', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // mode
arguments[2] = toEnumVal(arguments[2]); // type
if (old) {
return old.call(this, ...arguments);
} else {
const ext = this.getExtension('ANGLE_instanced_arrays');
return ext.drawElementsInstancedANGLE(...arguments);
}
};
});
overrideWebglByKey('fenceSync', (old) => {
if (!old) return old;
return function() {
arguments[0] = arguments[0] || 'sync-gpu-commands-complete';
arguments[0] = toEnumVal(arguments[0]); // condition
arguments[1] = arguments[1] || 0;
return old.call(this, ...arguments);
};
});
overrideWebglByKey('frontFace', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('generateMipmap', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getBufferSubData', (old) => {
if (!old) return old;
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getErrorStr', (old) => {
return function() {
const ret = old.call(this, ...arguments);
if (ret !== GL.NO_ERROR) {
ret = fromEnumVal(ret);
}
return ret;
};
});
overrideWebglByKey('getBufferParameter', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getFramebufferAttachmentParameter', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
arguments[1] = toEnumVal(arguments[1]); // attachment
arguments[2] = toEnumVal(arguments[2]); // pname
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getInternalformatParameter', (old) => {
if (!old) return old;
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
arguments[1] = toEnumVal(arguments[1]); // internalformat
arguments[2] = toEnumVal(arguments[2]); // pname
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getProgramParameter', (old) => {
return function() {
arguments[1] = toEnumVal(arguments[1]); // pname
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getQueryParameter', (old) => {
if (!old) return old;
return function() {
arguments[1] = toEnumVal(arguments[1]); // pname
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getRenderbufferParameter', (old) => {
return function() {
arguments[1] = toEnumVal(arguments[1]); // pname
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getShaderParameter', (old) => {
return function() {
arguments[1] = toEnumVal(arguments[1]); // pname
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getSyncParameter', (old) => {
if (!old) return old;
return function() {
arguments[1] = toEnumVal(arguments[1]); // pname
return old.call(this, ...arguments);
};
});
overrideWebglByKey('getTexParameter', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('hint', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('isEnabled', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
const PENDING_LINK_BY_PROGRAM = new WeakMap();
overrideWebglByKey('linkProgram', (old) => {
return function(prog) {
const ret = old.call(this, ...arguments);
let promise;
if (this.fenceAsync) {
promise = this.fenceAsync();
} else {
promise = new Promise(requestAnimationFrame);
}
PENDING_LINK_BY_PROGRAM.set(prog, promise);
invoke(async () => {
await promise;
PENDING_LINK_BY_PROGRAM.delete(prog);
});
return ret;
};
});
overrideWebglByKey('pixelStorei', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]);
if (arguments[0] == GL.UNPACK_COLORSPACE_CONVERSION) {
arguments[1] = toEnumVal(arguments[1]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('stencilFunc', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // func
return old.call(this, ...arguments);
};
});
overrideWebglByKey('stencilFuncSeparate', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // face
arguments[1] = toEnumVal(arguments[1]); // func
return old.call(this, ...arguments);
};
});
overrideWebglByKey('stencilOp', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('stencilOpSeparate', (old) => {
return function() {
for (let i = 0; i < arguments.length; i++) {
arguments[i] = toEnumVal(arguments[i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('texParameterf', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
arguments[1] = toEnumVal(arguments[1]); // pname
return old.call(this, ...arguments);
};
});
overrideWebglByKey('texParameteri', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
arguments[1] = toEnumVal(arguments[1]); // pname
return old.call(this, ...arguments);
};
});
overrideWebglByKey('vertexAttribDivisor', (old) => {
return function() {
if (old) {
return old.call(this, ...arguments);
} else {
const ext = this.getExtension('ANGLE_instanced_arrays');
return ext.vertexAttribDivisorANGLE(...arguments);
}
};
});
overrideWebglByKey('vertexAttribPointer', (old) => {
return function() {
arguments[2] = toEnumVal(arguments[2]); // type
return old.call(this, ...arguments);
};
});
overrideByKey(WebGL2RenderingContext.prototype, 'vertexAttribIPointer', (old) => {
return function() {
arguments[2] = toEnumVal(arguments[2]); // type
return old.call(this, ...arguments);
};
});
/*
* We can't have this in general, because:
* Some of the enum values are basic (0 unmaps to gl.POINTS)
* And/or multiple keys (POINTS, NONE, NO_ERROR)
* So if we add these they need to be only from a
* greenlit list of keys. (greenlist?)
overrideWebglByKey('getParameterStr', (old) => {
console.assert(!old, old);
return function() {
let ret = this.getParameter(...arguments);
ret = fromEnumVal(ret);
return ret;
};
});
*/
// -
// Framebuffers
overrideWebglByKey('bindFramebuffer', (old) => {
return function() {
if (arguments[0] == 'both') {
arguments[0] = 'framebuffer';
}
arguments[0] = toEnumVal(arguments[0]); // target
if (!this.DRAW_FRAMEBUFFER) {
switch (arguments[0]) {
case GL.DRAW_FRAMEBUFFER:
case GL.READ_FRAMEBUFFER:
arguments[0] = GL.FRAMEBUFFER;
break;
}
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('checkFramebufferStatus', (old) => {
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
return old.call(this, ...arguments);
};
});
overrideWebglByKey('checkFramebufferIncompleteStr', (old) => {
console.assert(!old, old);
return function() {
let ret = this.checkFramebufferStatus(...arguments);
ret = fromEnumVal(ret);
ret = replacePrefix(ret, 'framebuffer-', '');
if (ret == 'complete') {
ret = '';
}
return ret;
}; // E.g. -> 'complete'
});
/* Let's just call getExtension every time unless it's really slow.
const EXT_BY_NAME_BY_GL = {};
overrideWebglByKey('getExtension', (old) => {
return function() {
const ret = old.call(this, ...arguments);
let ext_by_name = EXT_BY_NAME_BY_GL.get(this);
if (!ext_by_name) {
ext_by_name = {};
EXT_BY_NAME_BY_GL.set(this, ext_by_name);
}
ext_by_name[arguments[0]] = ret;
return ret;
}
});
*/
// Ok, so in webgl1, drawBuffers is part of ANGLE_instanced_arrays.
// But we've brought that into core, so we *could* just redirect there.
// However, that breaks duck-typing checks (which I have written!) such
// as `if (!gl.drawBuffers) { // minimal webgl1 path`.
// I think it's not worth it to keep this, though, so just inject
// drawBuffers into webgl1 also, and polyfill.
function sugarDrawBuffersList(list, curFb) {
for (const i in list) {
if (typeof(list[i]) == 'boolean') {
list[i] = (curFb ? 'color-attachment'+i : 'back');
} else {
list[i] = 'none';
}
list[i] = toEnumVal(list[i]);
}
}
overrideWebglByKey('blitFramebuffer', (old) => {
if (!old) return old; // Leave out for webgl1.
return function() {
if (arguments[8] == -1) { // mask
arguments[8] = GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT | GL.STENCIL_BUFFER_BIT;
}
arguments[9] = toEnumVal(arguments[9]); // filter
return old.call(this, ...arguments);
};
});
overrideWebglByKey('invalidateFramebuffer', (old) => {
if (!old) return old; // Leave out for webgl1.
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
for (const i in arguments[1]) { // attachments
arguments[1][i] = toEnumVal(arguments[1][i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('invalidateSubFramebuffer', (old) => {
if (!old) return old; // Leave out for webgl1.
return function() {
arguments[0] = toEnumVal(arguments[0]); // target
for (const i in arguments[1]) { // attachments
arguments[1][i] = toEnumVal(arguments[1][i]);
}
return old.call(this, ...arguments);
};
});
overrideWebglByKey('drawBuffers', (old) => {
return function(list, forFb) {
const curFb = this.getParameter('draw-fb');
if (curFb && !forFb) {
console.warning('`gl.drawBuffers()` is deprecated for use on framebuffers.',
'Please use `fb.drawBuffers()`');
}
sugarDrawBuffersList(list);
if (old) {
return old.call(this, list);
}
const ext = this.getExtension('ANGLE_instanced_arrays');
if (!ext) throw 'ANGLE_instanced_arrays not supported.';
ext.drawBuffersANGLE(list);
};
});
overrideWebglByKey('readBuffer', (old) => {
if (!old) return old; // Leave out for webgl1.
return function(src, forFb) {
const curFb = this.getParameter('read-fb');
if (curFb && !forFb) {
console.warning('`gl.readBuffer()` is deprecated for use on framebuffers.',
'Please use `fb.readBuffer()`');
}
const list = [src];
sugarDrawBuffersList(list);
return old.call(this, list[0]);
};
});
overrideWebglByKey('readPixels', (old) => {
return function() {
const args = [].slice.call(arguments);
let forFb = args[args.length-1];
if (forFb instanceof WebGLFramebuffer) {
args.pop();
} else {
forFb = undefined; // It wasn't!
}
const curFb = this.getParameter('read-fb');
if (curFb && !forFb) {
console.warning('`gl.readPixels()` is deprecated for use on framebuffers.',
'Please use `fb.readPixels()`');
}
args[4] = toEnumVal(args[4]); // pack format
args[5] = toEnumVal(args[5]); // pack type
return old.call(this, ...args);
};
});
// I don't want to do the work to have ext.drawBuffersANGLE also sugared.
overrideWebglByKey('framebufferRenderbuffer', (old) => {
return function() {
if (arguments[1] < 32) {
arguments[1] = 'color-attachment' + arguments[1];
}
arguments[0] = toEnumVal(arguments[0]); // target
arguments[1] = toEnumVal(arguments[1]); // attachment
arguments[2] = toEnumVal(arguments[2]); // rbtarget
return old.call(this, ...arguments);
};
});
overrideWebglByKey('framebufferTexture2D', (old) => {
return function() {
if (arguments[1] < 32) {
arguments[1] = 'color-attachment' + arguments[1];
}
arguments[0] = toEnumVal(arguments[0]); // target
arguments[1] = toEnumVal(arguments[1]); // attachment
arguments[2] = toEnumVal(arguments[2]); // textarget
return old.call(this, ...arguments);
};
});
overrideWebglByKey('framebufferTextureLayer', (old) => {
if (!old) return old;
return function() {
if (arguments[1] < 32) {
arguments[1] = 'color-attachment' + arguments[1];
}
arguments[0] = toEnumVal(arguments[0]); // target
arguments[1] = toEnumVal(arguments[1]); // attachment
return old.call(this, ...arguments);
};
});
// -
// Framebuffer sugar
const ANON_RB_LIST_BY_FB = new WeakMap();
// We can just chain-override deleteFramebuffer again.
overrideWebglByKey('deleteFramebuffer', (old) => {
return function() {
const anonRbList = ANON_RB_LIST_BY_FB.get(arguments[0]);
for (const rb of anonRbList) {
rb.close();
}
return old.call(this, ...arguments);
};
});
// createCompleteFramebuffer({
// 0: [tex], // boring tex2d
// 1: [rb], // rb
// 2: [cube_tex, 3, 2] // miplevel=3, face=POSITIVE_Y
// 'depth-stencil': ['d24s8', 1920, 1080, 0] // DEPTH24_STENCIL8 1920x1080 non-multisampled
// });
overrideWebglByKey('createCompleteFramebuffer', (old) => {
console.assert(!old, old);
return function(argsByAttachId) {
const fbTarget = 'draw-fb';
let fb = this.createFramebuffer();
const was = this.getParameter(fbTarget);
this.bindFramebuffer(fbTarget, fb); // Don't make attach() thrash fb bindings
for (const [attachId,args] of Objects.entries(argsByAttachId)) {
fb.attach(fbTarget, attachId, ...args);
}
const incompleteStr = this.checkFramebufferIncompleteStr(fbTarget);
this.bindFramebuffer(fbTarget, was);
if (incompleteStr) {
console.warning("createCompleteFramebuffer(", argsByAttachId, ") ->", incompleteStr);
fb.close();
return incompleteStr;
}
return fb;
};
});
const TEX_TARGETS_2D = [
WebGLRenderingContext.TEXTURE_2D,
WebGLRenderingContext.TEXTURE_CUBE_MAP,
];
overrideByKey(WebGLFramebuffer.prototype, 'attach', (old) => {
console.assert(!old, old);
return function() {
// (target, attachpoint, rb)
// (target, attachpoint, tex, miplevel, zlayer/face)