forked from stackgl/headless-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl.js
4314 lines (3839 loc) Β· 108 KB
/
webgl.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
'use strict'
var bits = require('bit-twiddle')
var nativeGL = require('bindings')('webgl')
var tokenize = require('glsl-tokenizer/string')
var HEADLESS_VERSION = require('./package.json').version
// These are defined by the WebGL spec
var MAX_UNIFORM_LENGTH = 256
var MAX_ATTRIBUTE_LENGTH = 256
// We need to wrap some of the native WebGL functions to handle certain error codes and check input values
var gl = nativeGL.WebGLRenderingContext.prototype
gl.VERSION = 0x1F02
gl.IMPLEMENTATION_COLOR_READ_TYPE = 0x8B9A
gl.IMPLEMENTATION_COLOR_READ_FORMAT = 0x8B9B
var ATTACHMENTS = [
gl.COLOR_ATTACHMENT0,
gl.DEPTH_ATTACHMENT,
gl.STENCIL_ATTACHMENT,
gl.DEPTH_STENCIL_ATTACHMENT
]
// Hook clean up
process.on('exit', nativeGL.cleanup)
// Export type boxes for WebGL
exports.WebGLRenderingContext = nativeGL.WebGLRenderingContext
function WebGLProgram (_, ctx) {
this._ = _
this._ctx = ctx
this._linkCount = 0
this._pendingDelete = false
this._linkStatus = false
this._linkInfoLog = 'not linked'
this._references = []
this._refCount = 0
this._attributes = []
this._uniforms = []
}
exports.WebGLProgram = WebGLProgram
function WebGLShader (_, ctx, type) {
this._ = _
this._type = type
this._ctx = ctx
this._pendingDelete = false
this._references = []
this._refCount = 0
this._source = ''
this._compileStatus = false
this._compileInfo = ''
}
exports.WebGLShader = WebGLShader
function WebGLBuffer (_, ctx) {
this._ = _
this._ctx = ctx
this._binding = 0
this._size = 0
this._pendingDelete = false
this._references = []
this._refCount = 0
this._elements = new Uint8Array(0)
}
exports.WebGLBuffer = WebGLBuffer
function WebGLFramebuffer (_, ctx) {
this._ = _
this._ctx = ctx
this._binding = 0
this._pendingDelete = false
this._references = []
this._refCount = 0
this._width = 0
this._height = 0
this._attachments = {}
this._attachments[gl.COLOR_ATTACHMENT0] = null
this._attachments[gl.DEPTH_ATTACHMENT] = null
this._attachments[gl.STENCIL_ATTACHMENT] = null
this._attachments[gl.DEPTH_STENCIL_ATTACHMENT] = null
this._attachmentLevel = {}
this._attachmentLevel[gl.COLOR_ATTACHMENT0] = 0
this._attachmentLevel[gl.DEPTH_ATTACHMENT] = 0
this._attachmentLevel[gl.STENCIL_ATTACHMENT] = 0
this._attachmentLevel[gl.DEPTH_STENCIL_ATTACHMENT] = 0
this._attachmentFace = {}
this._attachmentFace[gl.COLOR_ATTACHMENT0] = 0
this._attachmentFace[gl.DEPTH_ATTACHMENT] = 0
this._attachmentFace[gl.STENCIL_ATTACHMENT] = 0
this._attachmentFace[gl.DEPTH_STENCIL_ATTACHMENT] = 0
}
exports.WebGLFramebuffer = WebGLFramebuffer
function WebGLRenderbuffer (_, ctx) {
this._ = _
this._ctx = ctx
this._binding = 0
this._pendingDelete = false
this._references = []
this._refCount = 0
this._width = 0
this._height = 0
this._format = 0
}
exports.WebGLRenderbuffer = WebGLRenderbuffer
function WebGLTexture (_, ctx) {
this._ = _
this._ctx = ctx
this._binding = 0
this._pendingDelete = false
this._references = []
this._refCount = 0
this._levelWidth = new Int32Array(32)
this._levelHeight = new Int32Array(32)
this._format = 0
this._type = 0
this._complete = true
}
exports.WebGLTexture = WebGLTexture
function WebGLActiveInfo (_) {
this.size = _.size
this.type = _.type
this.name = _.name
}
exports.WebGLActiveInfo = WebGLActiveInfo
function WebGLShaderPrecisionFormat (_) {
this.rangeMin = _.rangeMin
this.rangeMax = _.rangeMax
this.precision = _.precision
}
exports.WebGLShaderPrecisionFormat = WebGLShaderPrecisionFormat
function WebGLUniformLocation (_, program, info) {
this._ = _
this._program = program
this._linkCount = program._linkCount
this._activeInfo = info
this._array = null
}
exports.WebGLUniformLocation = WebGLUniformLocation
function WebGLContextAttributes (
alpha,
depth,
stencil,
antialias,
premultipliedAlpha,
preserveDrawingBuffer,
preferLowPowerToHighPerformance,
failIfMajorPerformanceCaveat) {
this.alpha = alpha
this.depth = depth
this.stencil = stencil
this.antialias = antialias
this.premultipliedAlpha = premultipliedAlpha
this.preserveDrawingBuffer = preserveDrawingBuffer
this.preferLowPowerToHighPerformance = preferLowPowerToHighPerformance
this.failIfMajorPerformanceCaveat = failIfMajorPerformanceCaveat
}
exports.WebGLContextAttributes = WebGLContextAttributes
function WebGLVertexAttribute (ctx, idx) {
this._ctx = ctx
this._idx = idx
this._isPointer = false
this._pointerBuffer = null
this._pointerOffset = 0
this._pointerSize = 0
this._pointerStride = 0
this._pointerType = gl.FLOAT
this._pointerNormal = false
this._divisor = 0
this._inputSize = 4
this._inputStride = 0
this._data = new Float32Array([0, 0, 0, 1])
}
exports.WebGLVertexAttribute = WebGLVertexAttribute
function WebGLTextureUnit (ctx, idx) {
this._ctx = ctx
this._idx = idx
this._mode = 0
this._bind2D = null
this._bindCube = null
}
exports.WebGLTextureUnit = WebGLTextureUnit
function WebGLDrawingBufferWrapper (
framebuffer,
color,
depthStencil) {
this._framebuffer = framebuffer
this._color = color
this._depthStencil = depthStencil
}
exports.WebGLDrawingBufferWrapper = WebGLDrawingBufferWrapper
/* eslint-disable camelcase */
function ANGLE_instanced_arrays () {
}
function STACKGL_resize_drawingbuffer () {
}
function STACKGL_destroy_context () {
}
function OESElementIndexUint () {
}
function OES_texture_float () {
}
/* eslint-enable camelcase */
function unpackTypedArray (array) {
return (new Uint8Array(array.buffer)).subarray(
array.byteOffset,
array.byteLength + array.byteOffset)
}
// Don't allow: ", $, `, @, \, ', \0
function isValidString (str) {
// Remove comments first
var c = str.replace(/(?:\/\*(?:[\s\S]*?)\*\/)|(?:([\s;])+\/\/(?:.*)$)/gm, '')
return !(/["$`@\\'\0]/.test(c))
}
function isTypedArray (data) {
return data instanceof Uint8Array ||
data instanceof Uint8ClampedArray ||
data instanceof Int8Array ||
data instanceof Uint16Array ||
data instanceof Int16Array ||
data instanceof Uint32Array ||
data instanceof Int32Array ||
data instanceof Float32Array ||
data instanceof Float64Array
}
function activeTextureUnit (context) {
return context._textureUnits[context._activeTextureUnit]
}
function activeTexture (context, target) {
var activeUnit = activeTextureUnit(context)
if (target === gl.TEXTURE_2D) {
return activeUnit._bind2D
} else if (target === gl.TEXTURE_CUBE_MAP) {
return activeUnit._bindCube
}
return null
}
function validCubeTarget (target) {
return target === gl.TEXTURE_CUBE_MAP_POSITIVE_X ||
target === gl.TEXTURE_CUBE_MAP_NEGATIVE_X ||
target === gl.TEXTURE_CUBE_MAP_POSITIVE_Y ||
target === gl.TEXTURE_CUBE_MAP_NEGATIVE_Y ||
target === gl.TEXTURE_CUBE_MAP_POSITIVE_Z ||
target === gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
}
function precheckFramebufferStatus (framebuffer) {
var attachments = framebuffer._attachments
var width = []
var height = []
var colorAttachment = attachments[gl.COLOR_ATTACHMENT0]
var depthAttachment = attachments[gl.DEPTH_ATTACHMENT]
var depthStencilAttachment = attachments[gl.DEPTH_STENCIL_ATTACHMENT]
var stencilAttachment = attachments[gl.STENCIL_ATTACHMENT]
if ((depthStencilAttachment && (stencilAttachment || depthAttachment)) ||
(stencilAttachment && depthAttachment)) {
return gl.FRAMEBUFFER_UNSUPPORTED
}
if (!colorAttachment) {
return gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
}
if (depthStencilAttachment instanceof WebGLTexture) {
return gl.FRAMEBUFFER_UNSUPPORTED
} else if (depthStencilAttachment instanceof WebGLRenderbuffer) {
if (depthStencilAttachment._format !== gl.DEPTH_STENCIL) {
return gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT
}
width.push(depthStencilAttachment._width)
height.push(depthStencilAttachment._height)
}
if (depthAttachment instanceof WebGLTexture) {
return gl.FRAMEBUFFER_UNSUPPORTED
} else if (depthAttachment instanceof WebGLRenderbuffer) {
if (depthAttachment._format !== gl.DEPTH_COMPONENT16) {
return gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT
}
width.push(depthAttachment._width)
height.push(depthAttachment._height)
}
if (stencilAttachment instanceof WebGLTexture) {
return gl.FRAMEBUFFER_UNSUPPORTED
} else if (stencilAttachment instanceof WebGLRenderbuffer) {
if (stencilAttachment._format !== gl.STENCIL_INDEX8) {
return gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT
}
width.push(stencilAttachment._width)
height.push(stencilAttachment._height)
}
if (colorAttachment instanceof WebGLTexture) {
if (colorAttachment._format !== gl.RGBA ||
!(colorAttachment._type === gl.UNSIGNED_BYTE || colorAttachment._type === gl.FLOAT)) {
return gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT
}
var level = framebuffer._attachmentLevel[gl.COLOR_ATTACHMENT0]
width.push(colorAttachment._levelWidth[level])
height.push(colorAttachment._levelHeight[level])
} else if (colorAttachment instanceof WebGLRenderbuffer) {
var format = colorAttachment._format
if (format !== gl.RGBA4 &&
format !== gl.RGB565 &&
format !== gl.RGB5_A1) {
return gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT
}
width.push(colorAttachment._width)
height.push(colorAttachment._height)
}
if (!colorAttachment &&
!stencilAttachment &&
!depthAttachment &&
!depthStencilAttachment) {
return gl.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
}
if (width.length <= 0 || height.length <= 0) {
return gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT
}
for (var i = 1; i < width.length; ++i) {
if (width[i - 1] !== width[i] ||
height[i - 1] !== height[i]) {
return gl.FRAMEBUFFER_INCOMPLETE_DIMENSIONS
}
}
if (width[0] === 0 || height[0] === 0) {
return gl.FRAMEBUFFER_INCOMPLETE_ATTACHMENT
}
framebuffer._width = width[0]
framebuffer._height = height[0]
return gl.FRAMEBUFFER_COMPLETE
}
function framebufferOk (context) {
var framebuffer = context._activeFramebuffer
if (framebuffer &&
precheckFramebufferStatus(framebuffer) !== gl.FRAMEBUFFER_COMPLETE) {
setError(context, gl.INVALID_FRAMEBUFFER_OPERATION)
return false
}
return true
}
function getTexImage (context, target) {
var unit = activeTextureUnit(context)
if (target === gl.TEXTURE_2D) {
return unit._bind2D
} else if (validCubeTarget(target)) {
return unit._bindCube
}
setError(context, gl.INVALID_ENUM)
return null
}
function checkObject (object) {
return typeof object === 'object' ||
(object === void 0)
}
function validFramebufferAttachment (attachment) {
return attachment === gl.COLOR_ATTACHMENT0 ||
attachment === gl.DEPTH_ATTACHMENT ||
attachment === gl.STENCIL_ATTACHMENT ||
attachment === gl.DEPTH_STENCIL_ATTACHMENT
}
function validTextureTarget (target) {
return target === gl.TEXTURE_2D ||
target === gl.TEXTURE_CUBE_MAP
}
function checkTextureTarget (context, target) {
var unit = activeTextureUnit(context)
var tex = null
if (target === gl.TEXTURE_2D) {
tex = unit._bind2D
} else if (target === gl.TEXTURE_CUBE_MAP) {
tex = unit._bindCube
} else {
setError(context, gl.INVALID_ENUM)
return false
}
if (!tex) {
setError(context, gl.INVALID_OPERATION)
return false
}
return true
}
function typeSize (type) {
switch (type) {
case gl.UNSIGNED_BYTE:
case gl.BYTE:
return 1
case gl.UNSIGNED_SHORT:
case gl.SHORT:
return 2
case gl.UNSIGNED_INT:
case gl.INT:
case gl.FLOAT:
return 4
}
return 0
}
function formatSize (internalformat) {
switch (internalformat) {
case gl.ALPHA:
case gl.LUMINANCE:
return 1
case gl.LUMINANCE_ALPHA:
return 2
case gl.RGB:
return 3
case gl.RGBA:
return 4
}
return 0
}
function vertexCount (primitive, count) {
switch (primitive) {
case gl.TRIANGLES:
return count - (count % 3)
case gl.LINES:
return count - (count % 2)
case gl.LINE_LOOP:
case gl.POINTS:
return count
case gl.TRIANGLE_FAN:
case gl.LINE_STRIP:
if (count < 2) {
return 0
}
return count
case gl.TRIANGLE_STRIP:
if (count < 3) {
return 0
}
return count
default:
return -1
}
}
function link (a, b) {
a._references.push(b)
b._refCount += 1
return true
}
function unlink (a, b) {
var idx = a._references.indexOf(b)
if (idx < 0) {
return false
}
while (idx >= 0) {
a._references[idx] = a._references[a._references.length - 1]
a._references.pop()
b._refCount -= 1
checkDelete(b)
idx = a._references.indexOf(b)
}
return true
}
function linked (a, b) {
return a._references.indexOf(b) >= 0
}
function checkDelete (obj) {
if (obj._refCount <= 0 &&
obj._pendingDelete &&
obj._ !== 0) {
while (obj._references.length > 0) {
unlink(obj, obj._references[0])
}
obj._performDelete()
obj._ = 0
}
}
function setError (context, error) {
nativeGL.setError.call(context, error | 0)
}
function checkValid (object, type) {
return object instanceof type && object._ !== 0
}
function checkOwns (context, object) {
return typeof object === 'object' &&
object._ctx === context
}
function checkUniform (program, location) {
return location instanceof WebGLUniformLocation &&
location._program === program &&
location._linkCount === program._linkCount
}
function checkLocation (context, location) {
if (!(location instanceof WebGLUniformLocation)) {
setError(context, gl.INVALID_VALUE)
return false
} else if (location._program._ctx !== context ||
location._linkCount !== location._program._linkCount) {
setError(context, gl.INVALID_OPERATION)
return false
}
return true
}
function checkLocationActive (context, location) {
if (!location) {
return false
} else if (!checkLocation(context, location)) {
return false
} else if (location._program !== context._activeProgram) {
setError(context, gl.INVALID_OPERATION)
return false
}
return true
}
function checkWrapper (context, object, wrapper) {
if (!checkValid(object, wrapper)) {
setError(context, gl.INVALID_VALUE)
return false
} else if (!checkOwns(context, object)) {
setError(context, gl.INVALID_OPERATION)
return false
}
return true
}
function saveError (context) {
context._errorStack.push(context.getError())
}
function restoreError (context, lastError) {
var topError = context._errorStack.pop()
if (topError === gl.NO_ERROR) {
setError(context, lastError)
} else {
setError(context, topError)
}
}
function getActiveBuffer (context, target) {
if (target === gl.ARRAY_BUFFER) {
return context._activeArrayBuffer
} else if (target === gl.ELEMENT_ARRAY_BUFFER) {
return context._activeElementArrayBuffer
}
return null
}
function checkVertexAttribState (context, maxIndex) {
var program = context._activeProgram
if (!program) {
setError(context, gl.INVALID_OPERATION)
return false
}
var attribs = context._vertexAttribs
for (var i = 0; i < attribs.length; ++i) {
var attrib = attribs[i]
if (attrib._isPointer) {
var buffer = attrib._pointerBuffer
if (!buffer) {
setError(context, gl.INVALID_OPERATION)
return false
}
if (program._attributes.indexOf(i) >= 0) {
var maxByte = 0
if (attrib._divisor) {
maxByte = attrib._pointerSize +
attrib._pointerOffset
} else {
maxByte = attrib._pointerStride * maxIndex +
attrib._pointerSize +
attrib._pointerOffset
}
if (maxByte > buffer._size) {
setError(context, gl.INVALID_OPERATION)
return false
}
}
}
}
return true
}
function clearFramebufferAttachment (framebuffer, attachment) {
var object = framebuffer._attachments[attachment]
if (!object) {
return
}
framebuffer._attachments[attachment] = null
unlink(framebuffer, object)
}
function setFramebufferAttachment (framebuffer, object, attachment) {
var prevObject = framebuffer._attachments[attachment]
if (prevObject === object) {
return
}
clearFramebufferAttachment(framebuffer, attachment)
if (!object) {
return
}
framebuffer._attachments[attachment] = object
link(framebuffer, object)
}
gl.resize = function (width, height) {
width = width | 0
height = height | 0
if (!(width > 0 && height > 0)) {
throw new Error('Invalid surface dimensions')
} else if (width !== this.drawingBufferWidth ||
height !== this.drawingBufferHeight) {
resizeDrawingBuffer(this, width, height)
this.drawingBufferWidth = width
this.drawingBufferHeight = height
}
}
var _destroy = gl.destroy
gl.destroy = function () {
_destroy.call(this)
}
gl.getContextAttributes = function () {
return this._contextattributes
}
var _getSupportedExtensions = gl.getSupportedExtensions
gl.getSupportedExtensions = function getSupportedExtensions () {
var exts = [
'ANGLE_instanced_arrays',
'STACKGL_resize_drawingbuffer',
'STACKGL_destroy_context'
]
var supportedExts = _getSupportedExtensions.call(this)
if (supportedExts.indexOf('GL_OES_element_index_uint') >= 0) {
exts.push('OES_element_index_uint')
}
if (supportedExts.indexOf('GL_OES_texture_float') >= 0) {
exts.push('OES_texture_float')
}
return exts
}
function createANGLEInstancedArrays (context) {
function checkInstancedVertexAttribState (context, maxIndex, primCount) {
var program = context._activeProgram
if (!program) {
setError(context, gl.INVALID_OPERATION)
return false
}
var attribs = context._vertexAttribs
var hasZero = false
for (var i = 0; i < attribs.length; ++i) {
var attrib = attribs[i]
if (attrib._isPointer) {
var buffer = attrib._pointerBuffer
if (program._attributes.indexOf(i) >= 0) {
if (!buffer) {
setError(context, gl.INVALID_OPERATION)
return false
}
var maxByte = 0
if (attrib._divisor === 0) {
hasZero = true
maxByte = attrib._pointerStride * maxIndex +
attrib._pointerSize +
attrib._pointerOffset
} else {
maxByte = attrib._pointerStride * (Math.ceil(primCount / attrib._divisor) - 1) +
attrib._pointerSize +
attrib._pointerOffset
}
if (maxByte > buffer._size) {
setError(context, gl.INVALID_OPERATION)
return false
}
}
}
}
if (!hasZero) {
setError(context, gl.INVALID_OPERATION)
return false
}
return true
}
var result = new ANGLE_instanced_arrays()
result.VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE = 0x88fe
result.drawArraysInstancedANGLE = function (mode, first, count, primCount) {
mode |= 0
first |= 0
count |= 0
primCount |= 0
if (first < 0 || count < 0 || primCount < 0) {
setError(context, gl.INVALID_VALUE)
return
}
if (!checkStencilState(context)) {
return
}
var reducedCount = vertexCount(mode, count)
if (reducedCount < 0) {
setError(context, gl.INVALID_ENUM)
return
}
if (!framebufferOk(context)) {
return
}
if (count === 0 || primCount === 0) {
return
}
var maxIndex = first
if (count > 0) {
maxIndex = (count + first - 1) >>> 0
}
if (checkInstancedVertexAttribState(context, maxIndex, primCount)) {
return _drawArraysInstanced.call(
context, mode, first, reducedCount, primCount)
}
}
result.drawElementsInstancedANGLE = function (
mode, count, type, ioffset, primCount) {
mode |= 0
count |= 0
type |= 0
ioffset |= 0
primCount |= 0
if (count < 0 || ioffset < 0 || primCount < 0) {
setError(context, gl.INVALID_VALUE)
return
}
if (!checkStencilState(context)) {
return
}
var elementBuffer = context._activeElementArrayBuffer
if (!elementBuffer) {
setError(context, gl.INVALID_OPERATION)
return
}
// Unpack element data
var elementData = null
var offset = ioffset
if (type === gl.UNSIGNED_SHORT) {
if (offset % 2) {
setError(context, gl.INVALID_OPERATION)
return
}
offset >>= 1
elementData = new Uint16Array(elementBuffer._elements.buffer)
} else if (context._extensions.oes_element_index_uint && type === gl.UNSIGNED_INT) {
if (offset % 4) {
setError(context, gl.INVALID_OPERATION)
return
}
offset >>= 2
elementData = new Uint32Array(elementBuffer._elements.buffer)
} else if (type === gl.UNSIGNED_BYTE) {
elementData = elementBuffer._elements
} else {
setError(context, gl.INVALID_ENUM)
return
}
var reducedCount = count
switch (mode) {
case gl.TRIANGLES:
if (count % 3) {
reducedCount -= (count % 3)
}
break
case gl.LINES:
if (count % 2) {
reducedCount -= (count % 2)
}
break
case gl.POINTS:
break
case gl.LINE_LOOP:
case gl.LINE_STRIP:
if (count < 2) {
setError(context, gl.INVALID_OPERATION)
return
}
break
case gl.TRIANGLE_FAN:
case gl.TRIANGLE_STRIP:
if (count < 3) {
setError(context, gl.INVALID_OPERATION)
return
}
break
default:
setError(context, gl.INVALID_ENUM)
return
}
if (!framebufferOk(context)) {
return
}
if (count === 0 || primCount === 0) {
checkInstancedVertexAttribState(context, 0, 0)
return
}
if ((count + offset) >>> 0 > elementData.length) {
setError(context, gl.INVALID_OPERATION)
return
}
// Compute max index
var maxIndex = -1
for (var i = offset; i < offset + count; ++i) {
maxIndex = Math.max(maxIndex, elementData[i])
}
if (maxIndex < 0) {
checkInstancedVertexAttribState(context, 0, 0)
return
}
if (checkInstancedVertexAttribState(context, maxIndex, primCount)) {
if (reducedCount > 0) {
_drawElementsInstanced.call(context, mode, reducedCount, type, ioffset, primCount)
}
}
}
result.vertexAttribDivisorANGLE = function (index, divisor) {
index |= 0
divisor |= 0
if (divisor < 0 ||
index < 0 || index >= context._vertexAttribs.length) {
setError(context, gl.INVALID_VALUE)
return
}
var attrib = context._vertexAttribs[index]
attrib._divisor = divisor
_vertexAttribDivisor.call(context, index, divisor)
}
return result
}
function getOESElementIndexUint (context) {
var result = null
var exts = context.getSupportedExtensions()
if (exts && exts.indexOf('OES_element_index_uint') >= 0) {
result = new OESElementIndexUint()
}
return result
}
function getOESTextureFloat (context) {
var result = null
var exts = context.getSupportedExtensions()
if (exts && exts.indexOf('OES_texture_float') >= 0) {
result = new OES_texture_float()
}
return result
}
gl.getExtension = function getExtension (name) {
var str = name.toLowerCase()
if (str in this._extensions) {
return this._extensions[str]
}
var ext = null
switch (str) {
case 'angle_instanced_arrays':
ext = createANGLEInstancedArrays(this)
break
case 'stackgl_destroy_context':
ext = new STACKGL_destroy_context()
ext.destroy = this.destroy.bind(this)
break
case 'stackgl_resize_drawingbuffer':
ext = new STACKGL_resize_drawingbuffer()
ext.resize = this.resize.bind(this)
break
case 'oes_element_index_uint':
ext = getOESElementIndexUint(this)
break
case 'oes_texture_float':
ext = getOESTextureFloat(this)
break
}
if (ext) {
this._extensions[str] = ext
}
return ext
}
var _activeTexture = gl.activeTexture
gl.activeTexture = function activeTexture (texture) {
texture |= 0
var texNum = texture - gl.TEXTURE0
if (texNum >= 0 && texNum < this._textureUnits.length) {
this._activeTextureUnit = texNum
return _activeTexture.call(this, texture)
}
setError(this, gl.INVALID_ENUM)
}
var _attachShader = gl.attachShader
gl.attachShader = function attachShader (program, shader) {
if (!checkObject(program) ||
!checkObject(shader)) {
throw new TypeError('attachShader(WebGLProgram, WebGLShader)')
}
if (!program || !shader) {
setError(this, gl.INVALID_VALUE)
return
} else if (program instanceof WebGLProgram &&
shader instanceof WebGLShader &&
checkOwns(this, program) &&
checkOwns(this, shader)) {
if (!linked(program, shader)) {
saveError(this)
_attachShader.call(
this,
program._ | 0,
shader._ | 0)
var error = this.getError()
restoreError(this, error)
if (error === gl.NO_ERROR) {
link(program, shader)
}
return
}
}
setError(this, gl.INVALID_OPERATION)
}
var _bindAttribLocation = gl.bindAttribLocation
gl.bindAttribLocation = function bindAttribLocation (program, index, name) {
if (!checkObject(program) ||