-
Notifications
You must be signed in to change notification settings - Fork 37
/
library_gl.js
2507 lines (2274 loc) · 104 KB
/
library_gl.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
/*
* GL support. See https://github.com/kripken/emscripten/wiki/OpenGL-support
* for current status.
*/
var LibraryGL = {
$GL__postset: 'GL.init()',
$GL: {
#if GL_DEBUG
debug: true,
#endif
counter: 1, // 0 is reserved as 'null' in gl
buffers: [],
programs: [],
framebuffers: [],
renderbuffers: [],
textures: [],
uniforms: [],
shaders: [],
uniformTable: {}, // name => uniform ID. the uID must be identical until relinking, cannot create a new uID each call to glGetUniformLocation
packAlignment: 4, // default alignment is 4 bytes
unpackAlignment: 4, // default alignment is 4 bytes
init: function() {
Browser.moduleContextCreatedCallbacks.push(GL.initExtensions);
},
// Get a new ID for a texture/buffer/etc., while keeping the table dense and fast. Creation is farely rare so it is worth optimizing lookups later.
getNewId: function(table) {
var ret = GL.counter++;
for (var i = table.length; i < ret; i++) {
table[i] = null;
}
return ret;
},
// Linear lookup in one of the tables (buffers, programs, etc.). TODO: consider using a weakmap to make this faster, if it matters
scan: function(table, object) {
for (var item in table) {
if (table[item] == object) return item;
}
return 0;
},
// Find a token in a shader source string
findToken: function(source, token) {
function isIdentChar(ch) {
if (ch >= 48 && ch <= 57) // 0-9
return true;
if (ch >= 65 && ch <= 90) // A-Z
return true;
if (ch >= 97 && ch <= 122) // a-z
return true;
return false;
}
var i = -1;
do {
i = source.indexOf(token, i + 1);
if (i < 0) {
break;
}
if (i > 0 && isIdentChar(source[i - 1])) {
continue;
}
i += token.length;
if (i < source.length - 1 && isIdentChar(source[i + 1])) {
continue;
}
return true;
} while (true);
return false;
},
getSource: function(shader, count, string, length) {
var source = '';
for (var i = 0; i < count; ++i) {
var frag;
if (length) {
var len = {{{ makeGetValue('length', 'i*4', 'i32') }}};
if (len < 0) {
frag = Pointer_stringify({{{ makeGetValue('string', 'i*4', 'i32') }}});
} else {
frag = Pointer_stringify({{{ makeGetValue('string', 'i*4', 'i32') }}}, len);
}
} else {
frag = Pointer_stringify({{{ makeGetValue('string', 'i*4', 'i32') }}});
}
source += frag;
}
// Let's see if we need to enable the standard derivatives extension
type = Module.ctx.getShaderParameter(GL.shaders[shader], 0x8B4F /* GL_SHADER_TYPE */);
if (type == 0x8B30 /* GL_FRAGMENT_SHADER */) {
if (GL.findToken(source, "dFdx") ||
GL.findToken(source, "dFdy") ||
GL.findToken(source, "fwidth")) {
source = "#extension GL_OES_standard_derivatives : enable\n" + source;
var extension = Module.ctx.getExtension("OES_standard_derivatives");
#if GL_DEBUG
if (!extension) {
Module.printErr("Shader attempts to use the standard derivatives extension which is not available.");
}
#endif
}
}
return source;
},
computeImageSize: function(width, height, sizePerPixel, alignment) {
function roundedToNextMultipleOf(x, y) {
return Math.floor((x + y - 1) / y) * y
}
var plainRowSize = width * sizePerPixel;
var alignedRowSize = roundedToNextMultipleOf(plainRowSize, alignment);
return (height <= 0) ? 0 :
((height - 1) * alignedRowSize + plainRowSize);
},
getTexPixelData: function(type, format, width, height, pixels, internalFormat) {
var sizePerPixel;
switch (type) {
case 0x1401 /* GL_UNSIGNED_BYTE */:
switch (format) {
case 0x1906 /* GL_ALPHA */:
case 0x1909 /* GL_LUMINANCE */:
sizePerPixel = 1;
break;
case 0x1907 /* GL_RGB */:
sizePerPixel = 3;
break;
case 0x1908 /* GL_RGBA */:
sizePerPixel = 4;
break;
case 0x190A /* GL_LUMINANCE_ALPHA */:
sizePerPixel = 2;
break;
default:
throw 'Invalid format (' + format + ')';
}
break;
case 0x8363 /* GL_UNSIGNED_SHORT_5_6_5 */:
case 0x8033 /* GL_UNSIGNED_SHORT_4_4_4_4 */:
case 0x8034 /* GL_UNSIGNED_SHORT_5_5_5_1 */:
sizePerPixel = 2;
break;
case 0x1406 /* GL_FLOAT */:
assert(GL.floatExt, 'Must have OES_texture_float to use float textures');
switch (format) {
case 0x1907 /* GL_RGB */:
sizePerPixel = 3*4;
break;
case 0x1908 /* GL_RGBA */:
sizePerPixel = 4*4;
break;
default:
throw 'Invalid format (' + format + ')';
}
internalFormat = Module.ctx.RGBA;
break;
default:
throw 'Invalid type (' + type + ')';
}
var bytes = GL.computeImageSize(width, height, sizePerPixel, GL.unpackAlignment);
if (type == 0x1401 /* GL_UNSIGNED_BYTE */) {
pixels = {{{ makeHEAPView('U8', 'pixels', 'pixels+bytes') }}};
} else if (type == 0x1406 /* GL_FLOAT */) {
pixels = {{{ makeHEAPView('F32', 'pixels', 'pixels+bytes') }}};
} else {
pixels = {{{ makeHEAPView('U16', 'pixels', 'pixels+bytes') }}};
}
return {
pixels: pixels,
internalFormat: internalFormat
}
},
initExtensions: function() {
if (GL.initExtensions.done) return;
GL.initExtensions.done = true;
if (!Module.useWebGL) return; // an app might link both gl and 2d backends
GL.compressionExt = Module.ctx.getExtension('WEBGL_compressed_texture_s3tc') ||
Module.ctx.getExtension('MOZ_WEBGL_compressed_texture_s3tc') ||
Module.ctx.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc');
GL.anisotropicExt = Module.ctx.getExtension('EXT_texture_filter_anisotropic') ||
Module.ctx.getExtension('MOZ_EXT_texture_filter_anisotropic') ||
Module.ctx.getExtension('WEBKIT_EXT_texture_filter_anisotropic');
GL.floatExt = Module.ctx.getExtension('OES_texture_float');
}
},
glPixelStorei: function(pname, param) {
if (pname == 0x0D05 /* GL_PACK_ALIGNMENT */) {
GL.packAlignment = param;
} else if (pname == 0x0cf5 /* GL_UNPACK_ALIGNMENT */) {
GL.unpackAlignment = param;
}
Module.ctx.pixelStorei(pname, param);
},
glGetString: function(name_) {
switch(name_) {
case 0x1F00 /* GL_VENDOR */:
case 0x1F01 /* GL_RENDERER */:
case 0x1F02 /* GL_VERSION */:
return allocate(intArrayFromString(Module.ctx.getParameter(name_)), 'i8', ALLOC_NORMAL);
case 0x1F03 /* GL_EXTENSIONS */:
return allocate(intArrayFromString(Module.ctx.getSupportedExtensions().join(' ')), 'i8', ALLOC_NORMAL);
case 0x8B8C /* GL_SHADING_LANGUAGE_VERSION */:
return allocate(intArrayFromString('OpenGL ES GLSL 1.00 (WebGL)'), 'i8', ALLOC_NORMAL);
default:
throw 'Failure: Invalid glGetString value: ' + name_;
}
},
glGetIntegerv: function(name_, p) {
switch(name_) { // Handle a few trivial GLES values
case 0x8DFA: // GL_SHADER_COMPILER
{{{ makeSetValue('p', '0', '1', 'i32') }}};
return;
case 0x8DF9: // GL_NUM_SHADER_BINARY_FORMATS
{{{ makeSetValue('p', '0', '0', 'i32') }}};
return;
}
var result = Module.ctx.getParameter(name_);
switch (typeof(result)) {
case "number":
{{{ makeSetValue('p', '0', 'result', 'i32') }}};
break;
case "boolean":
{{{ makeSetValue('p', '0', 'result ? 1 : 0', 'i8') }}};
break;
case "string":
throw 'Native code calling glGetIntegerv(' + name_ + ') on a name which returns a string!';
case "object":
if (result === null) {
{{{ makeSetValue('p', '0', '0', 'i32') }}};
} else if (result instanceof Float32Array ||
result instanceof Uint32Array ||
result instanceof Int32Array ||
result instanceof Array) {
for (var i = 0; i < result.length; ++i) {
{{{ makeSetValue('p', 'i*4', 'result[i]', 'i32') }}};
}
} else if (result instanceof WebGLBuffer) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.buffers, result)', 'i32') }}};
} else if (result instanceof WebGLProgram) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.programs, result)', 'i32') }}};
} else if (result instanceof WebGLFramebuffer) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.framebuffers, result)', 'i32') }}};
} else if (result instanceof WebGLRenderbuffer) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.renderbuffers, result)', 'i32') }}};
} else if (result instanceof WebGLTexture) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.textures, result)', 'i32') }}};
} else {
throw 'Unknown object returned from WebGL getParameter';
}
break;
case "undefined":
throw 'Native code calling glGetIntegerv(' + name_ + ') and it returns undefined';
default:
throw 'Why did we hit the default case?';
}
},
glGetFloatv: function(name_, p) {
var result = Module.ctx.getParameter(name_);
switch (typeof(result)) {
case "number":
{{{ makeSetValue('p', '0', 'result', 'float') }}};
break;
case "boolean":
{{{ makeSetValue('p', '0', 'result ? 1.0 : 0.0', 'float') }}};
break;
case "string":
{{{ makeSetValue('p', '0', '0', 'float') }}};
case "object":
if (result === null) {
throw 'Native code calling glGetFloatv(' + name_ + ') and it returns null';
} else if (result instanceof Float32Array ||
result instanceof Uint32Array ||
result instanceof Int32Array ||
result instanceof Array) {
for (var i = 0; i < result.length; ++i) {
{{{ makeSetValue('p', 'i*4', 'result[i]', 'float') }}};
}
} else if (result instanceof WebGLBuffer) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.buffers, result)', 'float') }}};
} else if (result instanceof WebGLProgram) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.programs, result)', 'float') }}};
} else if (result instanceof WebGLFramebuffer) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.framebuffers, result)', 'float') }}};
} else if (result instanceof WebGLRenderbuffer) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.renderbuffers, result)', 'float') }}};
} else if (result instanceof WebGLTexture) {
{{{ makeSetValue('p', '0', 'GL.scan(GL.textures, result)', 'float') }}};
} else {
throw 'Unknown object returned from WebGL getParameter';
}
break;
case "undefined":
throw 'Native code calling glGetFloatv(' + name_ + ') and it returns undefined';
default:
throw 'Why did we hit the default case?';
}
},
glGetBooleanv: function(name_, p) {
var result = Module.ctx.getParameter(name_);
switch (typeof(result)) {
case "number":
{{{ makeSetValue('p', '0', 'result != 0', 'i8') }}};
break;
case "boolean":
{{{ makeSetValue('p', '0', 'result != 0', 'i8') }}};
break;
case "string":
throw 'Native code calling glGetBooleanv(' + name_ + ') on a name which returns a string!';
case "object":
if (result === null) {
{{{ makeSetValue('p', '0', '0', 'i8') }}};
} else if (result instanceof Float32Array ||
result instanceof Uint32Array ||
result instanceof Int32Array ||
result instanceof Array) {
for (var i = 0; i < result.length; ++i) {
{{{ makeSetValue('p', 'i', 'result[i] != 0', 'i8') }}};
}
} else if (result instanceof WebGLBuffer ||
result instanceof WebGLProgram ||
result instanceof WebGLFramebuffer ||
result instanceof WebGLRenderbuffer ||
result instanceof WebGLTexture) {
{{{ makeSetValue('p', '0', '1', 'i8') }}}; // non-zero ID is always 1!
} else {
throw 'Unknown object returned from WebGL getParameter';
}
break;
case "undefined":
throw 'Unknown object returned from WebGL getParameter';
default:
throw 'Why did we hit the default case?';
}
},
glGenTextures: function(n, textures) {
for (var i = 0; i < n; i++) {
var id = GL.getNewId(GL.textures);
GL.textures[id] = Module.ctx.createTexture();
{{{ makeSetValue('textures', 'i*4', 'id', 'i32') }}};
}
},
glDeleteTextures: function(n, textures) {
for (var i = 0; i < n; i++) {
var id = {{{ makeGetValue('textures', 'i*4', 'i32') }}};
Module.ctx.deleteTexture(GL.textures[id]);
GL.textures[id] = null;
}
},
glCompressedTexImage2D: function(target, level, internalFormat, width, height, border, imageSize, data) {
assert(GL.compressionExt);
if (data) {
data = {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}};
} else {
data = null;
}
Module.ctx['compressedTexImage2D'](target, level, internalFormat, width, height, border, data);
},
glCompressedTexSubImage2D: function(target, level, xoffset, yoffset, width, height, format, imageSize, data) {
assert(GL.compressionExt);
if (data) {
data = {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}};
} else {
data = null;
}
Module.ctx['compressedTexSubImage2D'](target, level, xoffset, yoffset, width, height, data);
},
glTexImage2D: function(target, level, internalFormat, width, height, border, format, type, pixels) {
if (pixels) {
var data = GL.getTexPixelData(type, format, width, height, pixels, internalFormat);
pixels = data.pixels;
internalFormat = data.internalFormat;
} else {
pixels = null;
}
Module.ctx.texImage2D(target, level, internalFormat, width, height, border, format, type, pixels);
},
glTexSubImage2D: function(target, level, xoffset, yoffset, width, height, format, type, pixels) {
if (pixels) {
var data = GL.getTexPixelData(type, format, width, height, pixels, -1);
pixels = data.pixels;
} else {
pixels = null;
}
Module.ctx.texSubImage2D(target, level, xoffset, yoffset, width, height, format, type, pixels);
},
glReadPixels: function(x, y, width, height, format, type, pixels) {
Module.ctx.readPixels(x, y, width, height, format, type, HEAPU8.subarray(pixels));
},
glBindTexture: function(target, texture) {
Module.ctx.bindTexture(target, texture ? GL.textures[texture] : null);
},
glGetTexParameterfv: function(target, pname, params) {
{{{ makeSetValue('params', '0', 'Module.getTexParameter(target, pname)', 'float') }}};
},
glGetTexParameteriv: function(target, pname, params) {
{{{ makeSetValue('params', '0', 'Module.getTexParameter(target, pname)', 'i32') }}};
},
glIsTexture: function(texture) {
var fb = GL.textures[texture];
if (typeof(fb) == 'undefined') {
return 0;
}
return Module.ctx.isTexture(fb);
},
glGenBuffers: function(n, buffers) {
for (var i = 0; i < n; i++) {
var id = GL.getNewId(GL.buffers);
GL.buffers[id] = Module.ctx.createBuffer();
{{{ makeSetValue('buffers', 'i*4', 'id', 'i32') }}};
}
},
glDeleteBuffers: function(n, buffers) {
for (var i = 0; i < n; i++) {
var id = {{{ makeGetValue('buffers', 'i*4', 'i32') }}};
Module.ctx.deleteBuffer(GL.buffers[id]);
GL.buffers[id] = null;
}
},
glGetBufferParameteriv: function(target, value, data) {
{{{ makeSetValue('data', '0', 'Module.ctx.getBufferParameter(target, value)', 'i32') }}};
},
glBufferData: function(target, size, data, usage) {
Module.ctx.bufferData(target, HEAPU8.subarray(data, data+size), usage);
},
glBufferSubData: function(target, offset, size, data) {
Module.ctx.bufferSubData(target, offset, HEAPU8.subarray(data, data+size));
},
glIsBuffer: function(buffer) {
var fb = GL.buffers[buffer];
if (typeof(fb) == 'undefined') {
return 0;
}
return Module.ctx.isBuffer(fb);
},
glGenRenderbuffers: function(n, renderbuffers) {
for (var i = 0; i < n; i++) {
var id = GL.getNewId(GL.renderbuffers);
GL.renderbuffers[id] = Module.ctx.createRenderbuffer();
{{{ makeSetValue('renderbuffers', 'i*4', 'id', 'i32') }}};
}
},
glDeleteRenderbuffers: function(n, renderbuffers) {
for (var i = 0; i < n; i++) {
var id = {{{ makeGetValue('renderbuffers', 'i*4', 'i32') }}};
Module.ctx.deleteRenderbuffer(GL.renderbuffers[id]);
GL.renderbuffers[id];
}
},
glBindRenderbuffer: function(target, renderbuffer) {
Module.ctx.bindRenderbuffer(target, renderbuffer ? GL.renderbuffers[renderbuffer] : null);
},
glGetRenderbufferParameteriv: function(target, pname, params) {
{{{ makeSetValue('params', '0', 'Module.ctx.getRenderbufferParameter(target, pname)', 'i32') }}};
},
glIsRenderbuffer: function(renderbuffer) {
var fb = GL.renderbuffers[renderbuffer];
if (typeof(fb) == 'undefined') {
return 0;
}
return Module.ctx.isRenderbuffer(fb);
},
glGetUniformfv: function(program, location, params) {
var data = Module.ctx.getUniform(GL.programs[program], GL.uniforms[location]);
if (typeof data == 'number') {
{{{ makeSetValue('params', '0', 'data', 'float') }}};
} else {
for (var i = 0; i < data.length; i++) {
{{{ makeSetValue('params', 'i', 'data[i]', 'float') }}};
}
}
},
glGetUniformiv: function(program, location, params) {
var data = Module.ctx.getUniform(GL.programs[program], GL.uniforms[location]);
if (typeof data == 'number' || typeof data == 'boolean') {
{{{ makeSetValue('params', '0', 'data', 'i32') }}};
} else {
for (var i = 0; i < data.length; i++) {
{{{ makeSetValue('params', 'i', 'data[i]', 'i32') }}};
}
}
},
glGetUniformLocation: function(program, name) {
name = Pointer_stringify(name);
var ptable = GL.uniformTable[program];
if (!ptable) ptable = GL.uniformTable[program] = {};
var id = ptable[name];
if (id) return id;
var loc = Module.ctx.getUniformLocation(GL.programs[program], name);
if (!loc) return -1;
id = GL.getNewId(GL.uniforms);
GL.uniforms[id] = loc;
ptable[name] = id;
return id;
},
glGetVertexAttribfv: function(index, pname, params) {
var data = Module.ctx.getVertexAttrib(index, pname);
if (typeof data == 'number') {
{{{ makeSetValue('params', '0', 'data', 'float') }}};
} else {
for (var i = 0; i < data.length; i++) {
{{{ makeSetValue('params', 'i', 'data[i]', 'float') }}};
}
}
},
glGetVertexAttribiv: function(index, pname, params) {
var data = Module.ctx.getVertexAttrib(index, pname);
if (typeof data == 'number' || typeof data == 'boolean') {
{{{ makeSetValue('params', '0', 'data', 'i32') }}};
} else {
for (var i = 0; i < data.length; i++) {
{{{ makeSetValue('params', 'i', 'data[i]', 'i32') }}};
}
}
},
glGetVertexAttribPointerv: function(index, pname, pointer) {
{{{ makeSetValue('pointer', '0', 'Module.ctx.getVertexAttribOffset(index, pname)', 'i32') }}};
},
glGetActiveUniform: function(program, index, bufSize, length, size, type, name) {
program = GL.programs[program];
var info = Module.ctx.getActiveUniform(program, index);
var infoname = info.name.slice(0, bufSize - 1);
writeStringToMemory(infoname, name);
if (length) {
{{{ makeSetValue('length', '0', 'infoname.length', 'i32') }}};
}
if (size) {
{{{ makeSetValue('size', '0', 'info.size', 'i32') }}};
}
if (type) {
{{{ makeSetValue('type', '0', 'info.type', 'i32') }}};
}
},
glUniform1f: function(location, v0) {
location = GL.uniforms[location];
Module.ctx.uniform1f(location, v0);
},
glUniform2f: function(location, v0, v1) {
location = GL.uniforms[location];
Module.ctx.uniform2f(location, v0, v1);
},
glUniform3f: function(location, v0, v1, v2) {
location = GL.uniforms[location];
Module.ctx.uniform3f(location, v0, v1, v2);
},
glUniform4f: function(location, v0, v1, v2, v3) {
location = GL.uniforms[location];
Module.ctx.uniform4f(location, v0, v1, v2, v3);
},
glUniform1i: function(location, v0) {
location = GL.uniforms[location];
Module.ctx.uniform1i(location, v0);
},
glUniform2i: function(location, v0, v1) {
location = GL.uniforms[location];
Module.ctx.uniform2i(location, v0, v1);
},
glUniform3i: function(location, v0, v1, v2) {
location = GL.uniforms[location];
Module.ctx.uniform3i(location, v0, v1, v2);
},
glUniform4i: function(location, v0, v1, v2, v3) {
location = GL.uniforms[location];
Module.ctx.uniform4i(location, v0, v1, v2, v3);
},
glUniform1iv: function(location, count, value) {
location = GL.uniforms[location];
value = {{{ makeHEAPView('32', 'value', 'value+count*4') }}};
Module.ctx.uniform1iv(location, value);
},
glUniform2iv: function(location, count, value) {
location = GL.uniforms[location];
count *= 2;
value = {{{ makeHEAPView('32', 'value', 'value+count*4') }}};
Module.ctx.uniform2iv(location, value);
},
glUniform3iv: function(location, count, value) {
location = GL.uniforms[location];
count *= 3;
value = {{{ makeHEAPView('32', 'value', 'value+count*4') }}};
Module.ctx.uniform3iv(location, value);
},
glUniform4iv: function(location, count, value) {
location = GL.uniforms[location];
count *= 4;
value = {{{ makeHEAPView('32', 'value', 'value+count*4') }}};
Module.ctx.uniform4iv(location, value);
},
glUniform1fv: function(location, count, value) {
location = GL.uniforms[location];
value = {{{ makeHEAPView('F32', 'value', 'value+count*4') }}};
Module.ctx.uniform1fv(location, value);
},
glUniform2fv: function(location, count, value) {
location = GL.uniforms[location];
count *= 2;
value = {{{ makeHEAPView('F32', 'value', 'value+count*4') }}};
Module.ctx.uniform2fv(location, value);
},
glUniform3fv: function(location, count, value) {
location = GL.uniforms[location];
count *= 3;
value = {{{ makeHEAPView('F32', 'value', 'value+count*4') }}};
Module.ctx.uniform3fv(location, value);
},
glUniform4fv: function(location, count, value) {
location = GL.uniforms[location];
count *= 4;
value = {{{ makeHEAPView('F32', 'value', 'value+count*4') }}};
Module.ctx.uniform4fv(location, value);
},
glUniformMatrix2fv: function(location, count, transpose, value) {
location = GL.uniforms[location];
count *= 4;
value = {{{ makeHEAPView('F32', 'value', 'value+count*4') }}};
Module.ctx.uniformMatrix2fv(location, transpose, value);
},
glUniformMatrix3fv: function(location, count, transpose, value) {
location = GL.uniforms[location];
count *= 9;
value = {{{ makeHEAPView('F32', 'value', 'value+count*4') }}};
Module.ctx.uniformMatrix3fv(location, transpose, value);
},
glUniformMatrix4fv: function(location, count, transpose, value) {
location = GL.uniforms[location];
count *= 16;
value = {{{ makeHEAPView('F32', 'value', 'value+count*4') }}};
Module.ctx.uniformMatrix4fv(location, transpose, value);
},
glBindBuffer: function(target, buffer) {
Module.ctx.bindBuffer(target, buffer ? GL.buffers[buffer] : null);
},
glVertexAttrib1fv: function(index, v) {
v = {{{ makeHEAPView('F32', 'v', 'v+1*4') }}};
Module.ctx.vertexAttrib1fv(index, v);
},
glVertexAttrib2fv: function(index, v) {
v = {{{ makeHEAPView('F32', 'v', 'v+2*4') }}};
Module.ctx.vertexAttrib2fv(index, v);
},
glVertexAttrib3fv: function(index, v) {
v = {{{ makeHEAPView('F32', 'v', 'v+3*4') }}};
Module.ctx.vertexAttrib3fv(index, v);
},
glVertexAttrib4fv: function(index, v) {
v = {{{ makeHEAPView('F32', 'v', 'v+4*4') }}};
Module.ctx.vertexAttrib4fv(index, v);
},
glGetAttribLocation: function(program, name) {
program = GL.programs[program];
name = Pointer_stringify(name);
return Module.ctx.getAttribLocation(program, name);
},
glGetActiveAttrib: function(program, index, bufSize, length, size, type, name) {
program = GL.programs[program];
var info = Module.ctx.getActiveAttrib(program, index);
var infoname = info.name.slice(0, bufSize - 1);
writeStringToMemory(infoname, name);
if (length) {
{{{ makeSetValue('length', '0', 'infoname.length', 'i32') }}};
}
if (size) {
{{{ makeSetValue('size', '0', 'info.size', 'i32') }}};
}
if (type) {
{{{ makeSetValue('type', '0', 'info.type', 'i32') }}};
}
},
glCreateShader: function(shaderType) {
var id = GL.getNewId(GL.shaders);
GL.shaders[id] = Module.ctx.createShader(shaderType);
return id;
},
glDeleteShader: function(shader) {
Module.ctx.deleteShader(GL.shaders[shader]);
GL.shaders[shader] = null;
},
glDetachShader: function(program, shader) {
Module.ctx.detachShader(GL.programs[program],
GL.shaders[shader]);
},
glGetAttachedShaders: function(program, maxCount, count, shaders) {
var result = Module.ctx.getAttachedShaders(GL.programs[program]);
var len = result.length;
if (len > maxCount) {
len = maxCount;
}
{{{ makeSetValue('count', '0', 'len', 'i32') }}};
for (var i = 0; i < len; ++i) {
{{{ makeSetValue('shaders', 'i*4', 'GL.shaders[result[i]]', 'i32') }}};
}
},
glShaderSource: function(shader, count, string, length) {
var source = GL.getSource(shader, count, string, length);
Module.ctx.shaderSource(GL.shaders[shader], source);
},
glGetShaderSource: function(shader, bufSize, length, source) {
var result = Module.ctx.getShaderSource(GL.shaders[shader]);
result.slice(0, bufSize - 1);
writeStringToMemory(result, source);
if (length) {
{{{ makeSetValue('length', '0', 'result.length', 'i32') }}};
}
},
glCompileShader: function(shader) {
Module.ctx.compileShader(GL.shaders[shader]);
},
glGetShaderInfoLog: function(shader, maxLength, length, infoLog) {
var log = Module.ctx.getShaderInfoLog(GL.shaders[shader]);
// Work around a bug in Chromium which causes getShaderInfoLog to return null
if (!log) {
log = "";
}
log = log.substr(0, maxLength - 1);
writeStringToMemory(log, infoLog);
if (length) {
{{{ makeSetValue('length', '0', 'log.length', 'i32') }}}
}
},
glGetShaderiv : function(shader, pname, p) {
if (pname == 0x8B84) { // GL_INFO_LOG_LENGTH
{{{ makeSetValue('p', '0', 'Module.ctx.getShaderInfoLog(GL.shaders[shader]).length + 1', 'i32') }}};
} else {
{{{ makeSetValue('p', '0', 'Module.ctx.getShaderParameter(GL.shaders[shader], pname)', 'i32') }}};
}
},
glGetProgramiv : function(program, pname, p) {
if (pname == 0x8B84) { // GL_INFO_LOG_LENGTH
{{{ makeSetValue('p', '0', 'Module.ctx.getProgramInfoLog(GL.programs[program]).length + 1', 'i32') }}};
} else {
{{{ makeSetValue('p', '0', 'Module.ctx.getProgramParameter(GL.programs[program], pname)', 'i32') }}};
}
},
glIsShader: function(shader) {
var fb = GL.shaders[shader];
if (typeof(fb) == 'undefined') {
return 0;
}
return Module.ctx.isShader(fb);
},
glCreateProgram: function() {
var id = GL.getNewId(GL.programs);
GL.programs[id] = Module.ctx.createProgram();
return id;
},
glDeleteProgram: function(program) {
Module.ctx.deleteProgram(GL.programs[program]);
GL.programs[program] = null;
GL.uniformTable[program] = null;
},
glAttachShader: function(program, shader) {
Module.ctx.attachShader(GL.programs[program],
GL.shaders[shader]);
},
glGetShaderPrecisionFormat: function(shaderType, precisionType, range, precision) {
var result = Module.ctx.getShaderPrecisionFormat(shaderType, precisionType);
{{{ makeSetValue('range', '0', 'result.rangeMin', 'i32') }}};
{{{ makeSetValue('range', '4', 'result.rangeMax', 'i32') }}};
{{{ makeSetValue('precision', '0', 'result.precision', 'i32') }}};
},
glLinkProgram: function(program) {
Module.ctx.linkProgram(GL.programs[program]);
GL.uniformTable[program] = {}; // uniforms no longer keep the same names after linking
},
glGetProgramInfoLog: function(program, maxLength, length, infoLog) {
var log = Module.ctx.getProgramInfoLog(GL.programs[program]);
// Work around a bug in Chromium which causes getProgramInfoLog to return null
if (!log) {
log = "";
}
log = log.substr(0, maxLength - 1);
writeStringToMemory(log, infoLog);
if (length) {
{{{ makeSetValue('length', '0', 'log.length', 'i32') }}}
}
},
glUseProgram: function(program) {
Module.ctx.useProgram(program ? GL.programs[program] : null);
},
glValidateProgram: function(program) {
Module.ctx.validateProgram(GL.programs[program]);
},
glIsProgram: function(program) {
var fb = GL.programs[program];
if (typeof(fb) == 'undefined') {
return 0;
}
return Module.ctx.isProgram(fb);
},
glBindAttribLocation: function(program, index, name) {
name = Pointer_stringify(name);
Module.ctx.bindAttribLocation(GL.programs[program], index, name);
},
glBindFramebuffer: function(target, framebuffer) {
Module.ctx.bindFramebuffer(target, framebuffer ? GL.framebuffers[framebuffer] : null);
},
glGenFramebuffers: function(n, ids) {
for (var i = 0; i < n; ++i) {
var id = GL.getNewId(GL.framebuffers);
GL.framebuffers[id] = Module.ctx.createFramebuffer();
{{{ makeSetValue('ids', 'i*4', 'id', 'i32') }}};
}
},
glDeleteFramebuffers: function(n, framebuffers) {
for (var i = 0; i < n; ++i) {
var id = {{{ makeGetValue('framebuffers', 'i*4', 'i32') }}};
Module.ctx.deleteFramebuffer(GL.framebuffers[id]);
GL.framebuffers[id] = null;
}
},
glFramebufferRenderbuffer: function(target, attachment, renderbuffertarget, renderbuffer) {
Module.ctx.framebufferRenderbuffer(target, attachment, renderbuffertarget,
GL.renderbuffers[renderbuffer]);
},
glFramebufferTexture2D: function(target, attachment, textarget, texture, level) {
Module.ctx.framebufferTexture2D(target, attachment, textarget,
GL.textures[texture], level);
},
glGetFramebufferAttachmentParameteriv: function(target, attachment, pname, params) {
var result = Module.ctx.getFramebufferAttachmentParameter(target, attachment, pname);
{{{ makeSetValue('params', '0', 'params', 'i32') }}};
},
glIsFramebuffer: function(framebuffer) {
var fb = GL.framebuffers[framebuffer];
if (typeof(fb) == 'undefined') {
return 0;
}
return Module.ctx.isFramebuffer(fb);
},
// GL emulation: provides misc. functionality not present in OpenGL ES 2.0 or WebGL
$GLEmulation__postset: 'GLEmulation.init();',
$GLEmulation: {
// Fog support. Partial, we assume shaders are used that implement fog. We just pass them uniforms
fogStart: 0,
fogEnd: 1,
fogDensity: 1.0,
fogColor: null,
fogMode: 0x0800, // GL_EXP
fogEnabled: false,
init: function() {
GLEmulation.fogColor = new Float32Array(4);
// Add some emulation workarounds
Module.printErr('WARNING: using emscripten GL emulation. This is a collection of limited workarounds, do not expect it to work');
// XXX some of the capabilities we don't support may lead to incorrect rendering, if we do not emulate them in shaders
var validCapabilities = {
0x0B44: 1, // GL_CULL_FACE
0x0BE2: 1, // GL_BLEND
0x0BD0: 1, // GL_DITHER,
0x0B90: 1, // GL_STENCIL_TEST
0x0B71: 1, // GL_DEPTH_TEST
0x0C11: 1, // GL_SCISSOR_TEST
0x8037: 1, // GL_POLYGON_OFFSET_FILL
0x809E: 1, // GL_SAMPLE_ALPHA_TO_COVERAGE
0x80A0: 1 // GL_SAMPLE_COVERAGE
};
_glEnable = function(cap) {
// Clean up the renderer on any change to the rendering state. The optimization of
// skipping renderer setup is aimed at the case of multiple glDraw* right after each other
if (GL.immediate.lastRenderer) GL.immediate.lastRenderer.cleanup();
if (cap == 0x0B60 /* GL_FOG */) {
GLEmulation.fogEnabled = true;
return;
} else if (!(cap in validCapabilities)) {
return;
}
Module.ctx.enable(cap);
};
_glDisable = function(cap) {
if (GL.immediate.lastRenderer) GL.immediate.lastRenderer.cleanup();
if (cap == 0x0B60 /* GL_FOG */) {
GLEmulation.fogEnabled = false;
return;
} else if (!(cap in validCapabilities)) {
return;
}
Module.ctx.disable(cap);
};
_glIsEnabled = function(cap) {
if (cap == 0x0B60 /* GL_FOG */) {
return GLEmulation.fogEnabled ? 1 : 0;
} else if (!(cap in validCapabilities)) {
return 0;
}
return Module.ctx.isEnabled(cap);
};
var glGetIntegerv = _glGetIntegerv;
_glGetIntegerv = function(pname, params) {
switch (pname) {
case 0x84E2: pname = Module.ctx.MAX_TEXTURE_IMAGE_UNITS /* fake it */; break; // GL_MAX_TEXTURE_UNITS
case 0x8B4A: { // GL_MAX_VERTEX_UNIFORM_COMPONENTS_ARB
var result = Module.ctx.getParameter(Module.ctx.MAX_VERTEX_UNIFORM_VECTORS);
{{{ makeSetValue('params', '0', 'result*4', 'i32') }}}; // GLES gives num of 4-element vectors, GL wants individual components, so multiply
return;