-
Notifications
You must be signed in to change notification settings - Fork 0
/
glCommandBuffer.js
1942 lines (1838 loc) · 79.4 KB
/
glCommandBuffer.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
import {DeviceDescription, GLState, Handle} from "./glState.js"
import { WorkerMessageInterface, WorkerFactory, WebWorkerFactory, DynamicScriptFactory, SelfMessageInterface, FactoryMessageInterface } from "./WorkerFactory.js";
/**
* @typedef {GLuint} HandleObj
*/
/**
* Creates a gl context that stores all received commands in the active command buffer object.
* This allows us to switch buffers behind the scene and split up the stream of commands over multiple buffers.
*/
class GLCommandBufferContext {
/**
*
* @param {{glState: string, deviceDesc: string, functions: string[]}} message
* @param {WebGLSyncStrategy} webGLSyncStrategy
*/
constructor(message, webGLSyncStrategy) {
/**
* @type {WebGLSyncStrategy}
*/
this.webGLSyncStrategy = webGLSyncStrategy;
/**
* current webGL state of the server will equal a fresh webGL context with a default start state
* @type {JSONGLState}
*/
this.state = JSON.parse(message.glState);
/**
* unchangeable parameters, mostly hardware related based on the server
* @type {JSONDeviceDescription}
*/
this.deviceDesc = JSON.parse(message.deviceDesc);
/**
* handles are 0: invalid, negative: server created (default opengl state of textures and buffers) positive: client created
* first valid handle = 1
* @type {number}
*/
this.nextHandle = 1;
/**
* all created handles
* @type {{}}
*/
this.handles = {};
/**
* enable webgl2
* @type {boolean}
*/
this.enableWebGL2 = true;
this.contextLost = false;
// Create a fake gl context that pushes all called functions on the command buffer
if (this.enableWebGL2) {
/**
* @type {WebGL}
*/
this.gl = Object.create(WebGL2RenderingContext.prototype);
} else {
this.gl = Object.create(WebGLRenderingContext.prototype);
}
// replace all known members on our webGL context so they write to the command buffer instead of executing the commands directly
for (const fnName of message.functions) {
if (fnName === "activeTexture") {
/**
* @type {function(Handle):void}
*/
this.gl.activeTexture = (texture) => {
this.state.activeTexture = texture;
this.addMessage(fnName, [texture]);
};
} else if (fnName === "attachShader") {
/**
* @type {function(Handle, Handle):void}
*/
this.gl.attachShader = (program, shader) => {
if (this.activeBuffer) {
this.webGLSyncStrategy.attachShader(this.activeBuffer, program, shader);
}
};
} else if (fnName === "beginQuery") {
/**
* @type {function(GLenum, Handle):void}
*/
this.gl.beginQuery = (target, query) => {
this.addMessage(fnName, [target, query]);
};
} else if (fnName === "beginTransformFeedback") {
/**
* @type {function(GLenum):void}
*/
this.gl.beginTransformFeedback = (primitiveMode) => {
this.addMessage(fnName, [primitiveMode]);
};
} else if (fnName === "bindAttribLocation") {
/**
* @type {function(Handle, GLuint, string):void}
*/
this.gl.bindAttribLocation = (program, index, name) => {
this.addMessage(fnName, [program, index, name]);
};
} else if (fnName === "bindBuffer") {
/**
* @type {function(GLenum, Handle):void}
*/
this.gl.bindBuffer = (target, buffer) => {
/**
* @type {Object<string, GLenum>}
*/
let bufferTargetToTargetBinding = {}
bufferTargetToTargetBinding[this.gl.ARRAY_BUFFER] = this.gl.ARRAY_BUFFER_BINDING;
bufferTargetToTargetBinding[this.gl.ELEMENT_ARRAY_BUFFER] = this.gl.ELEMENT_ARRAY_BUFFER_BINDING;
// add "|| gl.hasOwnProperty("TEXTURE_3D")" here to support khronos's own debug layer
if (this.gl instanceof WebGL2RenderingContext || this.gl.hasOwnProperty("TEXTURE_3D")) {
bufferTargetToTargetBinding[this.gl.COPY_READ_BUFFER] = this.gl.COPY_READ_BUFFER_BINDING;
bufferTargetToTargetBinding[this.gl.COPY_WRITE_BUFFER] = this.gl.COPY_WRITE_BUFFER_BINDING;
bufferTargetToTargetBinding[this.gl.TRANSFORM_FEEDBACK_BUFFER] = this.gl.TRANSFORM_FEEDBACK_BUFFER_BINDING;
bufferTargetToTargetBinding[this.gl.UNIFORM_BUFFER] = this.gl.UNIFORM_BUFFER_BINDING;
bufferTargetToTargetBinding[this.gl.PIXEL_PACK_BUFFER] = this.gl.PIXEL_PACK_BUFFER_BINDING;
bufferTargetToTargetBinding[this.gl.PIXEL_UNPACK_BUFFER] = this.gl.PIXEL_UNPACK_BUFFER_BINDING;
}
this.state.parameters[bufferTargetToTargetBinding[target]].value = buffer;
this.addMessage(fnName, [target, buffer]);
};
} else if (fnName === "bindBufferBase") {
/**
* @type {function(GLenum, GLuint, Handle):void}
*/
this.gl.bindBufferBase = (target, index, buffer) => {
let bufferTargetToTargetBinding = {}
bufferTargetToTargetBinding[this.gl.TRANSFORM_FEEDBACK_BUFFER] = this.gl.TRANSFORM_FEEDBACK_BUFFER_BINDING;
bufferTargetToTargetBinding[this.gl.UNIFORM_BUFFER] = this.gl.UNIFORM_BUFFER_BINDING
// todo: state tracking for non general binding points (index)
this.addMessage(fnName, [target, index, buffer]);
};
} else if (fnName === "bindBufferRange") {
/**
* @type {function(GLenum, GLuint, Handle, GLintptr, GLsizeiptr):void}
*/
this.gl.bindBufferRange = (target, index, buffer, offset, size) => {
let bufferTargetToTargetBinding = {}
bufferTargetToTargetBinding[this.gl.TRANSFORM_FEEDBACK_BUFFER] = this.gl.TRANSFORM_FEEDBACK_BUFFER_BINDING;
bufferTargetToTargetBinding[this.gl.UNIFORM_BUFFER] = this.gl.UNIFORM_BUFFER_BINDING
// todo: state tracking for non general binding points (index)
this.addMessage(fnName, [target, index, buffer, offset, size]);
};
} else if (fnName === "bindRenderbuffer") {
/**
* @type {function(GLenum, Handle):void}
*/
this.gl.bindRenderBuffer = (target, renderbuffer) => {
this.state.parameters[this.gl.RENDERBUFFER_BINDING].value = renderbuffer;
this.addMessage(fnName, [target, renderbuffer]);
};
} else if (fnName === "bindSampler") {
/**
* @type {function(GLuint, Handle):void}
*/
this.gl.bindSampler = (unit, sampler) => {
// todo: statetracking in the texture unit
this.addMessage(fnName, [unit, sampler]);
};
} else if (fnName === "bindTexture") {
/**
* @type {function(GLenum, Handle):void}
*/
this.gl.bindTexture = (target, texture) => {
this.state.textureBinds[this.state.activeTexture].value.targets[target].value.texture = texture;
this.addMessage(fnName, [target, texture]);
};
} else if (fnName === "bindTransformFeedback") {
/**
* @type {function(GLenum, Handle):void}
*/
this.gl.bindTransformFeedback = (target, transformFeedback) => {
// todo: statetracking
this.addMessage(fnName, [target, transformFeedback]);
};
} else if (fnName === "bindVertexArray") {
/**
* @type {function(Handle):void}
*/
this.gl.bindVertexArray = (vertexArray) => {
this.addMessage(fnName, [vertexArray])
}
} else if (fnName === "blendColor") {
/**
* @type {function(GLclampf, GLclampf, GLclampf, GLclampf):void}
*/
this.gl.blendColor = (red, green, blue, alpha) => {
this.state.parameters[this.gl.BLEND_COLOR].value = new Float32Array([red, green, blue, alpha]);
this.addMessage(fnName, [red, green, blue, alpha]);
}
} else if (fnName === "blendEquation") {
/**
* @type {function(GLenum):void}
*/
this.gl.blendEquation = (mode) => {
this.state.parameters[this.gl.BLEND_EQUATION].value = mode;
this.state.parameters[this.gl.BLEND_EQUATION_RGB].value = mode;
this.state.parameters[this.gl.BLEND_EQUATION_ALPHA].value = mode;
this.addMessage(fnName, [mode]);
}
} else if (fnName === "blendEquationSeparate") {
/**
* @type {function(GLenum, GLenum):void}
*/
this.gl.blendEquationSeparate = (modeRGB, modeAlpha) => {
this.state.parameters[this.gl.BLEND_EQUATION].value = modeRGB === modeAlpha ? modeRGB : this.gl.INVALID_ENUM;
this.state.parameters[this.gl.BLEND_EQUATION_RGB].value = modeRGB;
this.state.parameters[this.gl.BLEND_EQUATION_ALPHA].value = modeAlpha;
this.addMessage(fnName, [modeRGB, modeAlpha]);
}
} else if (fnName === "blendFunc") {
/**
* @type {function(GLenum, GLenum):void}
*/
this.gl.blendFunc = (sfactor, dfactor) => {
this.state.parameters[this.gl.BLEND_SRC_RGB].value = sfactor;
this.state.parameters[this.gl.BLEND_SRC_ALPHA].value = sfactor;
this.state.parameters[this.gl.BLEND_DST_RGB].value = dfactor;
this.state.parameters[this.gl.BLEND_DST_ALPHA].value = dfactor;
this.addMessage(fnName, [sfactor, dfactor])
}
} else if (fnName === "blendFuncSeparate") {
/**
* @type {function(GLenum, GLenum, GLenum, GLenum):void}
*/
this.gl.blendFuncSeparate = (srcRGB, dstRGB, srcAlpha, dstAlpha) => {
this.state.parameters[this.gl.BLEND_SRC_RGB].value = srcRGB;
this.state.parameters[this.gl.BLEND_SRC_ALPHA].value = srcAlpha;
this.state.parameters[this.gl.BLEND_DST_RGB].value = dstRGB;
this.state.parameters[this.gl.BLEND_DST_ALPHA].value = dstAlpha;
this.addMessage(fnName, [srcRGB, dstRGB, srcAlpha, dstAlpha])
}
} else if (fnName === "blitFramebuffer") {
/**
* @type {function(GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLint, GLbitfield, GLenum):void}
*/
this.gl.blitFramebuffer = (srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter) => {
// todo: state tracking
this.addMessage(fnName, [srcX0, srcY0, srcX1, srcY1, dstX0, dstY0, dstX1, dstY1, mask, filter])
}
} else if (fnName === "bufferData") {
/**
* @type {function(GLenum, GLenum):void | function(GLenum, GLsizeiptr, GLenum):void | function(GLenum, BufferSource | null, GLenum):void | function(GLenum, GLenum, GLuint):void | function(GLenum, BufferSource | null, GLenum, GLuint):void | function(GLenum, BufferSource | null, GLenum, GLuint, GLuint):void}
*/
this.gl.bufferData = (...arg) => {
let args = Array.from(arg);
if (args[1] instanceof Float32Array) {
args[1] = {type: "Float32Array", data: new Float32Array(args[1])};
} else if (args[1] instanceof Uint8Array) {
args[1] = {type: "Uint8Array", data: new Uint8Array(args[1])};
} else if (args[1] instanceof Uint16Array) {
args[1] = {type: "Uint16Array", data: new Uint16Array(args[1])};
} else if (args[1] instanceof Array) {
args[1] = Array.from(args[1]);
}
this.addMessage(fnName, args);
};
} else if (fnName === "bufferSubData") {
/**
* @type {function(GLenum, GLenum):void | function(GLenum, GLsizeiptr, GLenum):void | function(GLenum, BufferSource | null, GLenum):void | function(GLenum, GLenum, GLuint):void | function(GLenum, BufferSource | null, GLenum, GLuint):void | function(GLenum, BufferSource | null, GLenum, GLuint, GLuint):void}
*/
this.gl.bufferSubData = (...arg) => {
let args = Array.from(arg);
if (args[2] instanceof Float32Array) {
args[2] = {type: "Float32Array", data: new Float32Array(args[2])};
} else if (args[2] instanceof Uint8Array) {
args[2] = {type: "Uint8Array", data: new Uint8Array(args[2])};
} else if (args[2] instanceof Uint16Array) {
args[2] = {type: "Uint16Array", data: new Uint16Array(args[2])};
} else if (args[2] instanceof Array) {
args[2] = Array.from(args[2]);
}
this.addMessage(fnName, args);
};
} else if (fnName === "checkFramebufferStatus") {
/**
* @type {function(GLenum):void}
*/
this.gl.checkFramebufferStatus = (target) => {
let buffer = new SharedArrayBuffer(4);
this.addMessageAndWait(fnName, [target], buffer);
return new Uint32Array(buffer)[0];
};
} else if (fnName === "clear") {
/**
* @type {function(GLbitfield):void}
*/
this.gl.clear = (mask) => {
this.addMessage(fnName, [mask]);
};
} else if (fnName === "clearColor") {
/**
* @type {function(GLclampf, GLclampf, GLclampf, GLclampf):void}
*/
this.gl.clearColor = (red, green, blue, alpha) => {
this.state.parameters[this.gl.COLOR_CLEAR_VALUE].value = new Float32Array([red, green, blue, alpha]);
this.addMessage(fnName, [red, green, blue, alpha]);
};
} else if (fnName === "clearDepth") {
/**
* @type {function(GLclampf):void}
*/
this.gl.clearDepth = (depth) => {
this.state.parameters[this.gl.DEPTH_CLEAR_VALUE].value = depth;
this.addMessage(fnName, [depth]);
};
} else if (fnName === "clearStencil") {
/**
* @type {function(GLint):void}
*/
this.gl.clearStencil = (s) => {
this.state.parameters[this.gl.STENCIL_CLEAR_VALUE].value = s;
this.addMessage(fnName, [s]);
};
} else if (fnName === "clientWaitSync") {
/**
* @type {function(Handle, GLbitfield, GLint64):GLenum}
*/
this.gl.clientWaitSync = (sync, flags, timeout) => {
let buffer = new SharedArrayBuffer(4);
this.addMessageAndWait(fnName, [sync, flags, timeout], buffer);
return new Uint32Array(buffer)[0];
};
} else if (fnName === "colorMask") {
/**
* @type {function(GLboolean, GLboolean, GLboolean, GLboolean):void}
*/
this.gl.colorMask = (red, green, blue, alpha) => {
this.state.parameters[this.gl.COLOR_WRITEMASK].value = [red, green, blue, alpha];
this.addMessage(fnName, [red, green, blue, alpha]);
};
} else if (fnName === "compileShader") {
/**
* @type {function(Handle):void}
*/
this.gl.compileShader = (shader) => {
if (this.activeBuffer) {
this.webGLSyncStrategy.compileShader(this.activeBuffer, shader);
}
};
} else if (fnName === "compressedTexImage2D") {
this.gl.compressedTexImage2D = (...arg) => {
let args = Array.from(arg);
if (args.length > 6) {
if (args[6] instanceof Float32Array) {
args[6] = {type: "Float32Array", data: new Float32Array(args[6])};
} else if (args[6] instanceof Uint8Array) {
args[6] = {type: "Uint8Array", data: new Uint8Array(args[6])};
} else if (args[6] instanceof Uint16Array) {
args[6] = {type: "Uint16Array", data: new Uint16Array(args[6])};
} else if (args[6] instanceof Array) {
args[6] = Array.from(args[6]);
}
}
this.addMessage(fnName, Array.from(args));
};
} else if (fnName === "compressedTexImage3D") {
this.gl.compressedTexImage3D = (...arg) => {
let args = Array.from(arg);
if (args.length > 7) {
if (args[7] instanceof Float32Array) {
args[7] = {type: "Float32Array", data: new Float32Array(args[7])};
} else if (args[7] instanceof Uint8Array) {
args[7] = {type: "Uint8Array", data: new Uint8Array(args[7])};
} else if (args[7] instanceof Uint16Array) {
args[7] = {type: "Uint16Array", data: new Uint16Array(args[7])};
} else if (args[7] instanceof Array) {
args[7] = Array.from(args[7]);
}
}
this.addMessage(fnName, Array.from(args));
};
} else if (fnName === "compressedTexSubImage2D") {
this.gl.compressedTexSubImage2D = (...arg) => {
let args = Array.from(arg);
if (args.length > 7) {
if (args[7] instanceof Float32Array) {
args[7] = {type: "Float32Array", data: new Float32Array(args[7])};
} else if (args[7] instanceof Uint8Array) {
args[7] = {type: "Uint8Array", data: new Uint8Array(args[7])};
} else if (args[7] instanceof Uint16Array) {
args[7] = {type: "Uint16Array", data: new Uint16Array(args[7])};
} else if (args[7] instanceof Array) {
args[7] = Array.from(args[7]);
}
}
this.addMessage(fnName, Array.from(args));
};
} else if (fnName === "compressedTexSubImage3D") {
this.gl.compressedTexSubImage2D = (...arg) => {
let args = Array.from(arg);
if (args.length > 9) {
if (args[9] instanceof Float32Array) {
args[9] = {type: "Float32Array", data: new Float32Array(args[9])};
} else if (args[9] instanceof Uint8Array) {
args[9] = {type: "Uint8Array", data: new Uint8Array(args[9])};
} else if (args[9] instanceof Uint16Array) {
args[9] = {type: "Uint16Array", data: new Uint16Array(args[9])};
} else if (args[9] instanceof Array) {
args[9] = Array.from(args[9]);
}
}
this.addMessage(fnName, Array.from(args));
};
} else if (fnName === "createBuffer") {
/**
* @type {function():Handle}
*/
this.gl.createBuffer = () => {
let handle = this.nextHandle++;
this.addMessageWithHandle(fnName, [], handle);
return new Handle(handle);
};
} else if (fnName === "createProgram") {
/**
* @type {function():Handle}
*/
this.gl.createProgram = () => {
let handle = new Handle(this.nextHandle++);
if (this.activeBuffer) {
this.webGLSyncStrategy.createProgram(this.activeBuffer, handle);
}
return handle;
};
} else if (fnName === "createQuery") {
/**
* @type {function():Handle}
*/
this.gl.createQuery = () => {
let handle = this.nextHandle++;
this.addMessageWithHandle(fnName, [], handle);
return new Handle(handle);
};
} else if (fnName === "createShader") {
/**
* @type {function():Handle}
*/
this.gl.createShader = (type) => {
let handle = new Handle(this.nextHandle++);
if (this.activeBuffer) {
this.webGLSyncStrategy.createShader(this.activeBuffer, type, handle);
}
return handle;
};
} else if (fnName === "createTexture") {
/**
* @type {function():Handle}
*/
this.gl.createTexture = () => {
let handle = this.nextHandle++;
this.addMessageWithHandle(fnName, [], handle);
return new Handle(handle);
};
} else if (fnName === "createVertexArray") {
/**
* @type {function():Handle}
*/
this.gl.createVertexArray = () => {
let handle = this.nextHandle++;
this.addMessageWithHandle(fnName, [], handle);
return new Handle(handle);
};
} else if (fnName === "cullFace") {
/**
* @type {function(GLenum):void}
*/
this.gl.cullFace = (mode) => {
this.state.parameters[this.gl.CULL_FACE_MODE].value = mode;
this.addMessage(fnName, [mode]);
};
} else if (fnName === "deleteShader") {
/**
* @type {function(Handle):void}
*/
this.gl.deleteShader = (shader) => {
if (shader.handle > 0) {
delete this.state.unclonables[shader.handle];
}
this.webGLSyncStrategy.deleteShader(this.activeBuffer, shader);
};
} else if (fnName === "depthFunc") {
/**
* @type {function(GLenum):void}
*/
this.gl.depthFunc = (func) => {
this.state.parameters[this.gl.DEPTH_FUNC].value = func;
this.addMessage(fnName, [func]);
};
} else if (fnName === "depthMask") {
/**
* @type {function(GLboolean):void}
*/
this.gl.depthMask = (flag) => {
this.state.parameters[this.gl.DEPTH_WRITEMASK].value = flag;
this.addMessage(fnName, [flag]);
};
} else if (fnName === "disable") {
/**
* @type {function(GLenum):void}
*/
this.gl.disable = (cap) => {
this.state.parameters[cap].value = false;
this.addMessage(fnName, [cap]);
};
} else if (fnName === "disableVertexAttribArray") {
/**
* @type {function(GLuint):void}
*/
this.gl.disableVertexAttribArray = (index) => {
// todo state management
this.addMessage(fnName, [index]);
};
} else if (fnName === "drawElements") {
/**
* @type {function(GLenum, GLsizei, GLenum, GLintptr):void}
*/
this.gl.drawElements = (mode, count, type, offset) => {
this.addMessage(fnName, [mode, count, type, offset]);
};
} else if (fnName === "fenceSync") {
/**
* @type {function(GLenum, GLbitfield):Handle}
*/
this.gl.fenceSync = (condition, flags) => {
let handle = this.nextHandle++;
this.addMessage(fnName, [condition, flags], handle);
return new Handle(handle);
};
} else if (fnName === "enable") {
/**
* @type {function(GLenum):void}
*/
this.gl.enable = (cap) => {
this.state.parameters[cap].value = true;
this.addMessage(fnName, [cap]);
};
} else if (fnName === "enableVertexAttribArray") {
/**
* @type {function(GLuint):void}
*/
this.gl.enableVertexAttribArray = (index) => {
this.addMessage(fnName, [index]);
};
} else if (fnName === "frontFace") {
/**
* @type {function(GLenum):void}
*/
this.gl.frontFace = (mode) => {
this.state.parameters[this.gl.FRONT_FACE].value = mode;
this.addMessage(fnName, [mode]);
};
} else if (fnName === "getActiveAttrib") {
/**
* @type {function(Handle, GLuint):{name: string, size: GLsizei, type: GLenum}}}
*/
this.gl.getActiveAttrib = (program, index) => {
if (this.activeBuffer) {
return this.webGLSyncStrategy.getActiveAttrib(this.activeBuffer, program, index);
} else {
return {name: "", size: 0, type: this.gl.INVALID_ENUM};
}
};
} else if (fnName === "getActiveUniform") {
/**
* @type {function(Handle, GLuint):{name: string, size: GLsizei, type: GLenum}}}
*/
this.gl.getActiveUniform = (program, index) => {
if (this.activeBuffer) {
return this.webGLSyncStrategy.getActiveUniform(this.activeBuffer, program, index);
} else {
return {name: "", size: 0, type: this.gl.INVALID_ENUM};
}
};
} else if (fnName === "getAttribLocation") {
/**
* @type {function(Handle, string):GLuint}
*/
this.gl.getAttribLocation = (program, name) => {
if (this.activeBuffer) {
return this.webGLSyncStrategy.getAttribLocation(this.activeBuffer, program, name);
} else {
return -1;
}
};
} else if (fnName === "getContextAttributes") {
/**
* @type {function():void}
*/
this.gl.getContextAttributes = () => {
return this.state.contextAttributes;
};
} else if (fnName === "getExtension") {
/**
* @type {function(string):any}
*/
this.gl.getExtension = (name) => {
// filter based on hardware and webgl version
if (!this.deviceDesc.supportedExtensions.includes(name)) return null;
// handle extension and its state
let ret = null;
if (name === "EXT_color_buffer_float") {
ret = {
RGBA32F_EXT: DeviceDescription.RGBA32F_EXT,
RGB32F_EXT: DeviceDescription.RGB32F_EXT
};
} else if (name === "OES_texture_float_linear") {
ret = {};
} else if (name === "EXT_color_buffer_half_float") {
ret = {
RGBA16F_EXT: DeviceDescription.RGBA16F_EXT,
RGB16F_EXT: DeviceDescription.RGB16F_EXT,
FRAMEBUFFER_ATTACHEMENT_COMPONENT_TYPE_EXT: DeviceDescription.FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE_EXT,
UNSIGNED_NORMALIZED_EXT: DeviceDescription.UNSIGNED_NORMALIZED_EXT
};
} else if ((name === "EXT_texture_filter_anisotropic") || (name === "MOZ_EXT_texture_filter_anisotropic") || (name === "WEBKIT_EXT_texture_filter_anisotropic")) {
ret = {
MAX_TEXTURE_MAX_ANISOTROPY_EXT: DeviceDescription.MAX_TEXTURE_MAX_ANISOTROPY_EXT,
TEXTURE_MAX_ANISOTROPY_EXT: DeviceDescription.TEXTURE_MAX_ANISOTROPY_EXT
};
} else if (name === "WEBGL_debug_renderer_info") {
ret = {
UNMASKED_VENDOR_WEBGL: DeviceDescription.UNMASKED_VENDOR_WEBGL,
UNMASKED_RENDERER_WEBGL: DeviceDescription.UNMASKED_RENDERER_WEBGL
};
}
// send to remote gl to enable extension
this.addMessage(fnName, [name]);
return ret;
};
} else if (fnName === "getParameter") {
/**
* @type {function(GLenum):any}
*/
this.gl.getParameter = (pname) => {
// if the extension is not available we don;t have a vlaue to compare against, so this can't be merged into the switch statement
if ("extTextureFilterAnisotropic" in this.deviceDesc) {
if (pname == this.deviceDesc.extTextureFilterAnisotropic.MAX_TEXTURE_MAX_ANISOTROPY_EXT) {
return this.deviceDesc.extTextureFilterAnisotropic.maxTextureMaxAnisotropyEXT;
}
}
if (this.deviceDesc.parameters.hasOwnProperty(pname)) {
return this.deviceDesc.parameters[pname].value;
} else if (this.state.parameters.hasOwnProperty(pname)) {
return this.state.parameters[pname].value;
} else {
console.log("unknown gl parameter");
}
};
} else if (fnName === "getProgramParameter") {
/**
* @type {function(Handle, GLenum):any}
*/
this.gl.getProgramParameter = (program, pname) => {
if (this.activeBuffer) {
return this.webGLSyncStrategy.getProgramParameter(this.activeBuffer, program, pname);
} else {
return -1;
}
};
} else if (fnName === "getShaderPrecisionFormat") {
/**
* @type {function(GLenum, GLenum):WebGLShaderPrecisionFormat}
*/
this.gl.getShaderPrecisionFormat = (shaderType, precisionType) => {
return this.deviceDesc.shaderPrecisionFormats[shaderType][precisionType];
};
} else if (fnName === "getSupportedExtensions") {
/**
* @type {function():Array<string>}
*/
this.gl.getSupportedExtensions = () => {
return this.deviceDesc.supportedExtensions;
};
} else if (fnName === "getUniformBlockIndex") {
/**
* @type {function(Handle, string):GLuint}
*/
this.gl.getUniformBlockIndex = (program, uniformBlockName) => {
if (this.activeBuffer) {
return this.webGLSyncStrategy.getUniformBlockIndex(this.activeBuffer, program, uniformBlockName);
} else {
return -1;
}
};
} else if (fnName === "getUniformLocation") {
/**
* @type {function(Handle, string):Handle}
*/
this.gl.getUniformLocation = (program, name) => {
let handle = new Handle(this.nextHandle++);
if (this.activeBuffer) {
this.webGLSyncStrategy.getUniformLocation(this.activeBuffer, program, name, handle);
}
return handle;
};
} else if (fnName === "isEnabled") {
/**
* @type {function(GLenum):GLboolean}
*/
this.gl.isEnabled = (cap) => {
return this.state.parameters[cap].value;
};
} else if (fnName === "linkProgram") {
/**
* @type {function(Handle):void}
*/
this.gl.linkProgram = (program) => {
if (this.activeBuffer) {
this.webGLSyncStrategy.linkProgram(this.activeBuffer, program);
}
};
} else if (fnName === "makeXRCompatible") {
/**
* @type {function():void}
*/
this.gl.makeXRCompatible = () => {
this.addMessage(fnName, []);
};
} else if (fnName === "pixelStorei") {
/**
* @type {function(GLenum, GLenum):void}
*/
this.gl.pixelStorei = (pname, param) => {
// todo state management
this.addMessage(fnName, [pname, param]);
};
} else if (fnName === "scissor") {
/**
* @type {function(GLint, GLint, GLsizei, GLsizei):void}
*/
this.gl.scissor = (x, y, width, height) => {
this.state.parameters[this.gl.SCISSOR_BOX].value = new Int32Array([x, y, width, height]);
// send to remote gl
this.addMessage(fnName, [x, y, width, height]);
};
} else if (fnName === "shaderSource") {
/**
* @type {function(Handle, string):void}
*/
this.gl.shaderSource = (shader, source) => {
if (this.activeBuffer) {
this.webGLSyncStrategy.shaderSource(this.activeBuffer, shader, source);
}
};
} else if (fnName === "stencilFunc") {
/**
* @type {function(GLenum, GLint, GLuint):void}
*/
this.gl.stencilFunc = (func, ref, mask) => {
this.addMessage(fnName, [func, ref, mask]);
};
} else if (fnName === "stencilMask") {
/**
* @type {function(GLuint):void}
*/
this.gl.stencilMask = (mask) => {
this.addMessage(fnName, [mask]);
};
} else if (fnName === "stencilOp") {
/**
* @type {function(GLenum, GLenum, GLenum):void}
*/
this.gl.stencilOp = (fail, zfail, zpass) => {
this.addMessage(fnName, [fail, zfail, zpass]);
};
} else if (fnName === "texImage2D") {
/**
* @type {function(GLenum, GLint, GLenum, GLsizei, GLsizei, GLint, GLenum, GLenum, ArrayBufferView):void | function(GLenum, GLint, GLenum, GLenum, GLenum, ArrayBufferView):void}
*/
this.gl.texImage2D = this.makeStub(fnName);
} else if (fnName === "texParameterf") {
/**
* @type {function(GLenum, GLenum, GLfloat):void}
*/
this.gl.texParameterf = (target, pname, param) => {
this.state.textureBinds[this.state.activeTexture].value.targets[target].value.parameters[pname].value = param;
this.addMessage(fnName, [target, pname, param]);
};
} else if (fnName === "texParameteri") {
/**
* @type {function(GLenum, GLenum, GLint):void}
*/
this.gl.texParameteri = (target, pname, param) => {
this.state.textureBinds[this.state.activeTexture].value.targets[target].value.parameters[pname].value = param;
this.addMessage(fnName, [target, pname, param]);
};
} else if (fnName === "useProgram") {
/**
* @type {function(Handle):void}
*/
this.gl.useProgram = (program) => {
// prevent handle decoration for structured cloning
this.addMessage(fnName, [new Handle(program.handle)]);
};
} else if (fnName === "vertexAttribDivisor") {
/**
* @type {function(GLuint, GLuint):void}
*/
this.gl.vertexAttribDivisor = (index, divisor) => {
this.addMessage(fnName, [index, divisor]);
};
} else if (fnName === "vertexAttribPointer") {
/**
* @type {function(GLuint, GLint, GLenum, GLboolean, GLsizei, GLintptr):void}
*/
this.gl.vertexAttribPointer = (index, size, type, normalized, stride, offset) => {
this.addMessage(fnName, [index, size, type, normalized, stride, offset]);
};
} else if (fnName === "viewport") {
/**
* @type {function(GLint, GLint, GLsizei, GLsizei):void}
*/
this.gl.viewport = (x, y, width, height) => {
this.state.parameters[this.gl.VIEWPORT].value = new Int32Array([x, y, width, height]);
// send to remote gl
this.addMessage(fnName, [x, y, width, height]);
};
} else if (fnName === "uniform1i") {
/**
* @type {function(Handle, GLint):void}
*/
this.gl.uniform1i = (location, v0) => {
this.addMessage(fnName, [location, v0]);
};
} else if (fnName === "uniform1f") {
/**
* @type {function(Handle, GLfloat):void}
*/
this.gl.uniform1f = (location, v0) => {
this.addMessage(fnName, [location, v0]);
};
} else if (fnName === "uniform3f") {
/**
* @type {function(Handle, GLfloat, GLfloat, GLfloat):void}
*/
this.gl.uniform3f = (location, v0, v1, v2) => {
this.addMessage(fnName, [location, v0, v1, v2]);
};
} else if (fnName === "uniform3fv") {
/**
* @type {function(Handle, Float32Array, GLsizei=, GLsizei=):void}
*/
this.gl.uniform3fv = (location, data, srcOffset, srcLength) => {
if (srcLength) {
this.addMessage(fnName, [location, data, srcOffset, srcLength]);
} else if (srcOffset) {
this.addMessage(fnName, [location, data, srcOffset]);
} else {
this.addMessage(fnName, [location, data]);
}
};
} else if (fnName === "uniformBlockBinding") {
/**
* @type {function(Handle, Handle, GLuint):void}
*/
this.gl.uniformBlockBinding = (program, uniformBlockIndex, uniformBlockBinding) => {
// todo state management
// prevent handle decoration for structured cloning
this.addMessage(fnName, [new Handle(program.handle), uniformBlockIndex, uniformBlockBinding]);
};
} else if (fnName === "uniformMatrix3fv") {
/**
* @type {function(Handle, GLboolean, Float32Array, GLsizei=, GLsizei=):void}
*/
this.gl.uniformMatrix3fv = (location, transpose, data, srcOffset, srcLength) => {
if (srcLength) {
this.addMessage(fnName, [location, transpose, data, srcOffset, srcLength]);
} else if (srcOffset) {
this.addMessage(fnName, [location, transpose, data, srcOffset]);
} else {
this.addMessage(fnName, [location, transpose, data]);
}
};
} else if (fnName === "uniformMatrix4fv") {
/**
* @type {function(Handle, GLboolean, Float32Array, GLsizei=, GLsizei=):void}
*/
this.gl.uniformMatrix4fv = (location, transpose, data, srcOffset, srcLength) => {
if (srcLength) {
this.addMessage(fnName, [location, transpose, data, srcOffset, srcLength]);
} else if (srcOffset) {
this.addMessage(fnName, [location, transpose, data, srcOffset]);
} else {
this.addMessage(fnName, [location, transpose, data]);
}
};
} else {
// if an unknown function is called, execute it as a default command, not expecting any type of response
this.gl[fnName] = (...args) => {
this.addMessage(fnName, Array.from(args));
}
}
}
}
/**
* old function used for sending texture data
* Makes a stub for a given function which sends a message to the gl
* implementation in the parent.
* @param {string} functionName
* @return {any} a placeholder object from the local hidden gl context (realGl).
*/
makeStub(functionName) {
let localContext = this;
return function() {
let args = Array.from(arguments);
for (let i = 0; i < args.length; i++) {
if (!args[i]) {
continue;
}
if (args[i].hasOwnProperty('__uncloneableId')) {
args[i] = {
fakeClone: true,
index: args[i].__uncloneableId,
};
} else if (typeof args[i] === 'object') {
if (args[i].fakeClone) {
// do nothing
} else if (args[i] instanceof Float32Array) {
args[i] = {type: "Float32Array", data: new Float32Array(args[i])};
} else if (args[i] instanceof Uint8Array) {
args[i] = {type: "Uint8Array", data: new Uint8Array(args[i])};
} else if (args[i] instanceof Uint16Array) {
args[i] = {type: "Uint16Array", data: new Uint16Array(args[i])};
} else if (args[i] instanceof Array) {
args[i] = Array.from(args[i]);
} else {
args[i] = Array.from(Object.values(args[i]));
if (debugGlWorker) console.log('Uncloned arg', args[i]);
}
}
}
if (functionName === 'texImage2D' || functionName === 'texSubImage2D') {
for (let i = 0; i < args.length; i++) {
let elt = args[i];
if (elt.tagName === 'IMG') {
let width = elt.width;
let height = elt.height;
let canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
let gfx = canvas.getContext('2d');
gfx.width = width;
gfx.height = height;
gfx.drawImage(elt, 0, 0, width, height);
let imageData = gfx.getImageData(0, 0, width, height);
args[i] = imageData;
} else if (elt.tagName === 'CANVAS' ||
(typeof OffscreenCanvas !== 'undefined' && elt instanceof OffscreenCanvas)) {
let width = elt.width;
let height = elt.height;
let gfx = elt.getContext('2d');
let imageData = gfx.getImageData(0, 0, width, height);
args[i] = imageData;
}
}
}
// glCommandBuferObject is the copy of the this pointer stored in the closure.
// by requesting the commandbuffer from the object, we can change it between calls.
let messageId = null;
if (localContext.activeBuffer !== null) {
messageId = localContext.addMessage(functionName, args);
} else {
console.error('No active command buffer set for gl context');
}
};
}
/**
* Changes the buffer in which we store open gl commands
* @param {CommandBuffer} commandBuffer - the command buffer to use for storing gl commands
*/
setActiveCommandBuffer(commandBuffer) {
this.activeBuffer = commandBuffer;
if (this.contextLost) {
this.activeBuffer.onContextLost();
} else {
this.activeBuffer.onContextRestored();
}
}
/**
* Adds a message to the active command buffer
* @param {String} name - the function name
* @param {Array<any>} args - the function arguments
*/
addMessage(name, args) {
this.activeBuffer.addMessage(name, args);
}
/**
* Adds a message with the allocated handle to represent the created object
*