forked from regl-project/regl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture.js
1634 lines (1416 loc) Β· 45.4 KB
/
texture.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
var check = require('./util/check')
var extend = require('./util/extend')
var values = require('./util/values')
var isTypedArray = require('./util/is-typed-array')
var isNDArrayLike = require('./util/is-ndarray')
var pool = require('./util/pool')
var convertToHalfFloat = require('./util/to-half-float')
var isArrayLike = require('./util/is-array-like')
var flattenUtils = require('./util/flatten')
var dtypes = require('./constants/arraytypes.json')
var arrayTypes = require('./constants/arraytypes.json')
var GL_COMPRESSED_TEXTURE_FORMATS = 0x86A3
var GL_TEXTURE_2D = 0x0DE1
var GL_TEXTURE_CUBE_MAP = 0x8513
var GL_TEXTURE_CUBE_MAP_POSITIVE_X = 0x8515
var GL_RGBA = 0x1908
var GL_ALPHA = 0x1906
var GL_RGB = 0x1907
var GL_LUMINANCE = 0x1909
var GL_LUMINANCE_ALPHA = 0x190A
var GL_RGBA4 = 0x8056
var GL_RGB5_A1 = 0x8057
var GL_RGB565 = 0x8D62
var GL_UNSIGNED_SHORT_4_4_4_4 = 0x8033
var GL_UNSIGNED_SHORT_5_5_5_1 = 0x8034
var GL_UNSIGNED_SHORT_5_6_5 = 0x8363
var GL_UNSIGNED_INT_24_8_WEBGL = 0x84FA
var GL_DEPTH_COMPONENT = 0x1902
var GL_DEPTH_STENCIL = 0x84F9
var GL_SRGB_EXT = 0x8C40
var GL_SRGB_ALPHA_EXT = 0x8C42
var GL_HALF_FLOAT_OES = 0x8D61
var GL_COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0
var GL_COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1
var GL_COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2
var GL_COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3
var GL_COMPRESSED_RGB_ATC_WEBGL = 0x8C92
var GL_COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL = 0x8C93
var GL_COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL = 0x87EE
var GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG = 0x8C00
var GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG = 0x8C01
var GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG = 0x8C02
var GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG = 0x8C03
var GL_COMPRESSED_RGB_ETC1_WEBGL = 0x8D64
var GL_UNSIGNED_BYTE = 0x1401
var GL_UNSIGNED_SHORT = 0x1403
var GL_UNSIGNED_INT = 0x1405
var GL_FLOAT = 0x1406
var GL_TEXTURE_WRAP_S = 0x2802
var GL_TEXTURE_WRAP_T = 0x2803
var GL_REPEAT = 0x2901
var GL_CLAMP_TO_EDGE = 0x812F
var GL_MIRRORED_REPEAT = 0x8370
var GL_TEXTURE_MAG_FILTER = 0x2800
var GL_TEXTURE_MIN_FILTER = 0x2801
var GL_NEAREST = 0x2600
var GL_LINEAR = 0x2601
var GL_NEAREST_MIPMAP_NEAREST = 0x2700
var GL_LINEAR_MIPMAP_NEAREST = 0x2701
var GL_NEAREST_MIPMAP_LINEAR = 0x2702
var GL_LINEAR_MIPMAP_LINEAR = 0x2703
var GL_GENERATE_MIPMAP_HINT = 0x8192
var GL_DONT_CARE = 0x1100
var GL_FASTEST = 0x1101
var GL_NICEST = 0x1102
var GL_TEXTURE_MAX_ANISOTROPY_EXT = 0x84FE
var GL_UNPACK_ALIGNMENT = 0x0CF5
var GL_UNPACK_FLIP_Y_WEBGL = 0x9240
var GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL = 0x9241
var GL_UNPACK_COLORSPACE_CONVERSION_WEBGL = 0x9243
var GL_BROWSER_DEFAULT_WEBGL = 0x9244
var GL_TEXTURE0 = 0x84C0
var MIPMAP_FILTERS = [
GL_NEAREST_MIPMAP_NEAREST,
GL_NEAREST_MIPMAP_LINEAR,
GL_LINEAR_MIPMAP_NEAREST,
GL_LINEAR_MIPMAP_LINEAR
]
var CHANNELS_FORMAT = [
0,
GL_LUMINANCE,
GL_LUMINANCE_ALPHA,
GL_RGB,
GL_RGBA
]
var FORMAT_CHANNELS = {}
FORMAT_CHANNELS[GL_LUMINANCE] =
FORMAT_CHANNELS[GL_ALPHA] =
FORMAT_CHANNELS[GL_DEPTH_COMPONENT] = 1
FORMAT_CHANNELS[GL_DEPTH_STENCIL] =
FORMAT_CHANNELS[GL_LUMINANCE_ALPHA] = 2
FORMAT_CHANNELS[GL_RGB] =
FORMAT_CHANNELS[GL_SRGB_EXT] = 3
FORMAT_CHANNELS[GL_RGBA] =
FORMAT_CHANNELS[GL_SRGB_ALPHA_EXT] = 4
var formatTypes = {}
formatTypes[GL_RGBA4] = GL_UNSIGNED_SHORT_4_4_4_4
formatTypes[GL_RGB565] = GL_UNSIGNED_SHORT_5_6_5
formatTypes[GL_RGB5_A1] = GL_UNSIGNED_SHORT_5_5_5_1
formatTypes[GL_DEPTH_COMPONENT] = GL_UNSIGNED_INT
formatTypes[GL_DEPTH_STENCIL] = GL_UNSIGNED_INT_24_8_WEBGL
function objectName (str) {
return '[object ' + str + ']'
}
var CANVAS_CLASS = objectName('HTMLCanvasElement')
var CONTEXT2D_CLASS = objectName('CanvasRenderingContext2D')
var BITMAP_CLASS = objectName('ImageBitmap')
var IMAGE_CLASS = objectName('HTMLImageElement')
var VIDEO_CLASS = objectName('HTMLVideoElement')
var PIXEL_CLASSES = Object.keys(dtypes).concat([
CANVAS_CLASS,
CONTEXT2D_CLASS,
BITMAP_CLASS,
IMAGE_CLASS,
VIDEO_CLASS
])
// for every texture type, store
// the size in bytes.
var TYPE_SIZES = []
TYPE_SIZES[GL_UNSIGNED_BYTE] = 1
TYPE_SIZES[GL_FLOAT] = 4
TYPE_SIZES[GL_HALF_FLOAT_OES] = 2
TYPE_SIZES[GL_UNSIGNED_SHORT] = 2
TYPE_SIZES[GL_UNSIGNED_INT] = 4
var FORMAT_SIZES_SPECIAL = []
FORMAT_SIZES_SPECIAL[GL_RGBA4] = 2
FORMAT_SIZES_SPECIAL[GL_RGB5_A1] = 2
FORMAT_SIZES_SPECIAL[GL_RGB565] = 2
FORMAT_SIZES_SPECIAL[GL_DEPTH_STENCIL] = 4
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGB_S3TC_DXT1_EXT] = 0.5
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGBA_S3TC_DXT1_EXT] = 0.5
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGBA_S3TC_DXT3_EXT] = 1
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGBA_S3TC_DXT5_EXT] = 1
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGB_ATC_WEBGL] = 0.5
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL] = 1
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL] = 1
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG] = 0.5
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG] = 0.25
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG] = 0.5
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG] = 0.25
FORMAT_SIZES_SPECIAL[GL_COMPRESSED_RGB_ETC1_WEBGL] = 0.5
function isNumericArray (arr) {
return (
Array.isArray(arr) &&
(arr.length === 0 ||
typeof arr[0] === 'number'))
}
function isRectArray (arr) {
if (!Array.isArray(arr)) {
return false
}
var width = arr.length
if (width === 0 || !isArrayLike(arr[0])) {
return false
}
return true
}
function classString (x) {
return Object.prototype.toString.call(x)
}
function isCanvasElement (object) {
return classString(object) === CANVAS_CLASS
}
function isContext2D (object) {
return classString(object) === CONTEXT2D_CLASS
}
function isBitmap (object) {
return classString(object) === BITMAP_CLASS
}
function isImageElement (object) {
return classString(object) === IMAGE_CLASS
}
function isVideoElement (object) {
return classString(object) === VIDEO_CLASS
}
function isPixelData (object) {
if (!object) {
return false
}
var className = classString(object)
if (PIXEL_CLASSES.indexOf(className) >= 0) {
return true
}
return (
isNumericArray(object) ||
isRectArray(object) ||
isNDArrayLike(object))
}
function typedArrayCode (data) {
return arrayTypes[Object.prototype.toString.call(data)] | 0
}
function convertData (result, data) {
var n = data.length
switch (result.type) {
case GL_UNSIGNED_BYTE:
case GL_UNSIGNED_SHORT:
case GL_UNSIGNED_INT:
case GL_FLOAT:
var converted = pool.allocType(result.type, n)
converted.set(data)
result.data = converted
break
case GL_HALF_FLOAT_OES:
result.data = convertToHalfFloat(data)
break
default:
check.raise('unsupported texture type, must specify a typed array')
}
}
function preConvert (image, n) {
return pool.allocType(
image.type === GL_HALF_FLOAT_OES
? GL_FLOAT
: image.type, n)
}
function postConvert (image, data) {
if (image.type === GL_HALF_FLOAT_OES) {
image.data = convertToHalfFloat(data)
pool.freeType(data)
} else {
image.data = data
}
}
function transposeData (image, array, strideX, strideY, strideC, offset) {
var w = image.width
var h = image.height
var c = image.channels
var n = w * h * c
var data = preConvert(image, n)
var p = 0
for (var i = 0; i < h; ++i) {
for (var j = 0; j < w; ++j) {
for (var k = 0; k < c; ++k) {
data[p++] = array[strideX * j + strideY * i + strideC * k + offset]
}
}
}
postConvert(image, data)
}
function getTextureSize (format, type, width, height, isMipmap, isCube) {
var s
if (typeof FORMAT_SIZES_SPECIAL[format] !== 'undefined') {
// we have a special array for dealing with weird color formats such as RGB5A1
s = FORMAT_SIZES_SPECIAL[format]
} else {
s = FORMAT_CHANNELS[format] * TYPE_SIZES[type]
}
if (isCube) {
s *= 6
}
if (isMipmap) {
// compute the total size of all the mipmaps.
var total = 0
var w = width
while (w >= 1) {
// we can only use mipmaps on a square image,
// so we can simply use the width and ignore the height:
total += s * w * w
w /= 2
}
return total
} else {
return s * width * height
}
}
module.exports = function createTextureSet (
gl, extensions, limits, reglPoll, contextState, stats, config) {
// -------------------------------------------------------
// Initialize constants and parameter tables here
// -------------------------------------------------------
var mipmapHint = {
"don't care": GL_DONT_CARE,
'dont care': GL_DONT_CARE,
'nice': GL_NICEST,
'fast': GL_FASTEST
}
var wrapModes = {
'repeat': GL_REPEAT,
'clamp': GL_CLAMP_TO_EDGE,
'mirror': GL_MIRRORED_REPEAT
}
var magFilters = {
'nearest': GL_NEAREST,
'linear': GL_LINEAR
}
var minFilters = extend({
'mipmap': GL_LINEAR_MIPMAP_LINEAR,
'nearest mipmap nearest': GL_NEAREST_MIPMAP_NEAREST,
'linear mipmap nearest': GL_LINEAR_MIPMAP_NEAREST,
'nearest mipmap linear': GL_NEAREST_MIPMAP_LINEAR,
'linear mipmap linear': GL_LINEAR_MIPMAP_LINEAR
}, magFilters)
var colorSpace = {
'none': 0,
'browser': GL_BROWSER_DEFAULT_WEBGL
}
var textureTypes = {
'uint8': GL_UNSIGNED_BYTE,
'rgba4': GL_UNSIGNED_SHORT_4_4_4_4,
'rgb565': GL_UNSIGNED_SHORT_5_6_5,
'rgb5 a1': GL_UNSIGNED_SHORT_5_5_5_1
}
var textureFormats = {
'alpha': GL_ALPHA,
'luminance': GL_LUMINANCE,
'luminance alpha': GL_LUMINANCE_ALPHA,
'rgb': GL_RGB,
'rgba': GL_RGBA,
'rgba4': GL_RGBA4,
'rgb5 a1': GL_RGB5_A1,
'rgb565': GL_RGB565
}
var compressedTextureFormats = {}
if (extensions.ext_srgb) {
textureFormats.srgb = GL_SRGB_EXT
textureFormats.srgba = GL_SRGB_ALPHA_EXT
}
if (extensions.oes_texture_float) {
textureTypes.float32 = textureTypes.float = GL_FLOAT
}
if (extensions.oes_texture_half_float) {
textureTypes['float16'] = textureTypes['half float'] = GL_HALF_FLOAT_OES
}
if (extensions.webgl_depth_texture) {
extend(textureFormats, {
'depth': GL_DEPTH_COMPONENT,
'depth stencil': GL_DEPTH_STENCIL
})
extend(textureTypes, {
'uint16': GL_UNSIGNED_SHORT,
'uint32': GL_UNSIGNED_INT,
'depth stencil': GL_UNSIGNED_INT_24_8_WEBGL
})
}
if (extensions.webgl_compressed_texture_s3tc) {
extend(compressedTextureFormats, {
'rgb s3tc dxt1': GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
'rgba s3tc dxt1': GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
'rgba s3tc dxt3': GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
'rgba s3tc dxt5': GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
})
}
if (extensions.webgl_compressed_texture_atc) {
extend(compressedTextureFormats, {
'rgb atc': GL_COMPRESSED_RGB_ATC_WEBGL,
'rgba atc explicit alpha': GL_COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL,
'rgba atc interpolated alpha': GL_COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL
})
}
if (extensions.webgl_compressed_texture_pvrtc) {
extend(compressedTextureFormats, {
'rgb pvrtc 4bppv1': GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG,
'rgb pvrtc 2bppv1': GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG,
'rgba pvrtc 4bppv1': GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG,
'rgba pvrtc 2bppv1': GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG
})
}
if (extensions.webgl_compressed_texture_etc1) {
compressedTextureFormats['rgb etc1'] = GL_COMPRESSED_RGB_ETC1_WEBGL
}
// Copy over all texture formats
var supportedCompressedFormats = Array.prototype.slice.call(
gl.getParameter(GL_COMPRESSED_TEXTURE_FORMATS))
Object.keys(compressedTextureFormats).forEach(function (name) {
var format = compressedTextureFormats[name]
if (supportedCompressedFormats.indexOf(format) >= 0) {
textureFormats[name] = format
}
})
var supportedFormats = Object.keys(textureFormats)
limits.textureFormats = supportedFormats
// associate with every format string its
// corresponding GL-value.
var textureFormatsInvert = []
Object.keys(textureFormats).forEach(function (key) {
var val = textureFormats[key]
textureFormatsInvert[val] = key
})
// associate with every type string its
// corresponding GL-value.
var textureTypesInvert = []
Object.keys(textureTypes).forEach(function (key) {
var val = textureTypes[key]
textureTypesInvert[val] = key
})
var magFiltersInvert = []
Object.keys(magFilters).forEach(function (key) {
var val = magFilters[key]
magFiltersInvert[val] = key
})
var minFiltersInvert = []
Object.keys(minFilters).forEach(function (key) {
var val = minFilters[key]
minFiltersInvert[val] = key
})
var wrapModesInvert = []
Object.keys(wrapModes).forEach(function (key) {
var val = wrapModes[key]
wrapModesInvert[val] = key
})
// colorFormats[] gives the format (channels) associated to an
// internalformat
var colorFormats = supportedFormats.reduce(function (color, key) {
var glenum = textureFormats[key]
if (glenum === GL_LUMINANCE ||
glenum === GL_ALPHA ||
glenum === GL_LUMINANCE ||
glenum === GL_LUMINANCE_ALPHA ||
glenum === GL_DEPTH_COMPONENT ||
glenum === GL_DEPTH_STENCIL) {
color[glenum] = glenum
} else if (glenum === GL_RGB5_A1 || key.indexOf('rgba') >= 0) {
color[glenum] = GL_RGBA
} else {
color[glenum] = GL_RGB
}
return color
}, {})
function TexFlags () {
// format info
this.internalformat = GL_RGBA
this.format = GL_RGBA
this.type = GL_UNSIGNED_BYTE
this.compressed = false
// pixel storage
this.premultiplyAlpha = false
this.flipY = false
this.unpackAlignment = 1
this.colorSpace = GL_BROWSER_DEFAULT_WEBGL
// shape info
this.width = 0
this.height = 0
this.channels = 0
}
function copyFlags (result, other) {
result.internalformat = other.internalformat
result.format = other.format
result.type = other.type
result.compressed = other.compressed
result.premultiplyAlpha = other.premultiplyAlpha
result.flipY = other.flipY
result.unpackAlignment = other.unpackAlignment
result.colorSpace = other.colorSpace
result.width = other.width
result.height = other.height
result.channels = other.channels
}
function parseFlags (flags, options) {
if (typeof options !== 'object' || !options) {
return
}
if ('premultiplyAlpha' in options) {
check.type(options.premultiplyAlpha, 'boolean',
'invalid premultiplyAlpha')
flags.premultiplyAlpha = options.premultiplyAlpha
}
if ('flipY' in options) {
check.type(options.flipY, 'boolean',
'invalid texture flip')
flags.flipY = options.flipY
}
if ('alignment' in options) {
check.oneOf(options.alignment, [1, 2, 4, 8],
'invalid texture unpack alignment')
flags.unpackAlignment = options.alignment
}
if ('colorSpace' in options) {
check.parameter(options.colorSpace, colorSpace,
'invalid colorSpace')
flags.colorSpace = colorSpace[options.colorSpace]
}
if ('type' in options) {
var type = options.type
check(extensions.oes_texture_float ||
!(type === 'float' || type === 'float32'),
'you must enable the OES_texture_float extension in order to use floating point textures.')
check(extensions.oes_texture_half_float ||
!(type === 'half float' || type === 'float16'),
'you must enable the OES_texture_half_float extension in order to use 16-bit floating point textures.')
check(extensions.webgl_depth_texture ||
!(type === 'uint16' || type === 'uint32' || type === 'depth stencil'),
'you must enable the WEBGL_depth_texture extension in order to use depth/stencil textures.')
check.parameter(type, textureTypes,
'invalid texture type')
flags.type = textureTypes[type]
}
var w = flags.width
var h = flags.height
var c = flags.channels
var hasChannels = false
if ('shape' in options) {
check(Array.isArray(options.shape) && options.shape.length >= 2,
'shape must be an array')
w = options.shape[0]
h = options.shape[1]
if (options.shape.length === 3) {
c = options.shape[2]
check(c > 0 && c <= 4, 'invalid number of channels')
hasChannels = true
}
check(w >= 0 && w <= limits.maxTextureSize, 'invalid width')
check(h >= 0 && h <= limits.maxTextureSize, 'invalid height')
} else {
if ('radius' in options) {
w = h = options.radius
check(w >= 0 && w <= limits.maxTextureSize, 'invalid radius')
}
if ('width' in options) {
w = options.width
check(w >= 0 && w <= limits.maxTextureSize, 'invalid width')
}
if ('height' in options) {
h = options.height
check(h >= 0 && h <= limits.maxTextureSize, 'invalid height')
}
if ('channels' in options) {
c = options.channels
check(c > 0 && c <= 4, 'invalid number of channels')
hasChannels = true
}
}
flags.width = w | 0
flags.height = h | 0
flags.channels = c | 0
var hasFormat = false
if ('format' in options) {
var formatStr = options.format
check(extensions.webgl_depth_texture ||
!(formatStr === 'depth' || formatStr === 'depth stencil'),
'you must enable the WEBGL_depth_texture extension in order to use depth/stencil textures.')
check.parameter(formatStr, textureFormats,
'invalid texture format')
var internalformat = flags.internalformat = textureFormats[formatStr]
flags.format = colorFormats[internalformat]
if (formatStr in textureTypes) {
if (!('type' in options)) {
flags.type = textureTypes[formatStr]
}
}
if (formatStr in compressedTextureFormats) {
flags.compressed = true
}
hasFormat = true
}
// Reconcile channels and format
if (!hasChannels && hasFormat) {
flags.channels = FORMAT_CHANNELS[flags.format]
} else if (hasChannels && !hasFormat) {
if (flags.channels !== CHANNELS_FORMAT[flags.format]) {
flags.format = flags.internalformat = CHANNELS_FORMAT[flags.channels]
}
} else if (hasFormat && hasChannels) {
check(
flags.channels === FORMAT_CHANNELS[flags.format],
'number of channels inconsistent with specified format')
}
}
function setFlags (flags) {
gl.pixelStorei(GL_UNPACK_FLIP_Y_WEBGL, flags.flipY)
gl.pixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL, flags.premultiplyAlpha)
gl.pixelStorei(GL_UNPACK_COLORSPACE_CONVERSION_WEBGL, flags.colorSpace)
gl.pixelStorei(GL_UNPACK_ALIGNMENT, flags.unpackAlignment)
}
// -------------------------------------------------------
// Tex image data
// -------------------------------------------------------
function TexImage () {
TexFlags.call(this)
this.xOffset = 0
this.yOffset = 0
// data
this.data = null
this.needsFree = false
// html element
this.element = null
// copyTexImage info
this.needsCopy = false
}
function parseImage (image, options) {
var data = null
if (isPixelData(options)) {
data = options
} else if (options) {
check.type(options, 'object', 'invalid pixel data type')
parseFlags(image, options)
if ('x' in options) {
image.xOffset = options.x | 0
}
if ('y' in options) {
image.yOffset = options.y | 0
}
if (isPixelData(options.data)) {
data = options.data
}
}
check(
!image.compressed ||
data instanceof Uint8Array,
'compressed texture data must be stored in a uint8array')
if (options.copy) {
check(!data, 'can not specify copy and data field for the same texture')
var viewW = contextState.viewportWidth
var viewH = contextState.viewportHeight
image.width = image.width || (viewW - image.xOffset)
image.height = image.height || (viewH - image.yOffset)
image.needsCopy = true
check(image.xOffset >= 0 && image.xOffset < viewW &&
image.yOffset >= 0 && image.yOffset < viewH &&
image.width > 0 && image.width <= viewW &&
image.height > 0 && image.height <= viewH,
'copy texture read out of bounds')
} else if (!data) {
image.width = image.width || 1
image.height = image.height || 1
image.channels = image.channels || 4
} else if (isTypedArray(data)) {
image.channels = image.channels || 4
image.data = data
if (!('type' in options) && image.type === GL_UNSIGNED_BYTE) {
image.type = typedArrayCode(data)
}
} else if (isNumericArray(data)) {
image.channels = image.channels || 4
convertData(image, data)
image.alignment = 1
image.needsFree = true
} else if (isNDArrayLike(data)) {
var array = data.data
if (!Array.isArray(array) && image.type === GL_UNSIGNED_BYTE) {
image.type = typedArrayCode(array)
}
var shape = data.shape
var stride = data.stride
var shapeX, shapeY, shapeC, strideX, strideY, strideC
if (shape.length === 3) {
shapeC = shape[2]
strideC = stride[2]
} else {
check(shape.length === 2, 'invalid ndarray pixel data, must be 2 or 3D')
shapeC = 1
strideC = 1
}
shapeX = shape[0]
shapeY = shape[1]
strideX = stride[0]
strideY = stride[1]
image.alignment = 1
image.width = shapeX
image.height = shapeY
image.channels = shapeC
image.format = image.internalformat = CHANNELS_FORMAT[shapeC]
image.needsFree = true
transposeData(image, array, strideX, strideY, strideC, data.offset)
} else if (isCanvasElement(data) || isContext2D(data)) {
if (isCanvasElement(data)) {
image.element = data
} else {
image.element = data.canvas
}
image.width = image.element.width
image.height = image.element.height
image.channels = 4
} else if (isBitmap(data)) {
image.element = data
image.width = data.width
image.height = data.height
image.channels = 4
} else if (isImageElement(data)) {
image.element = data
image.width = data.naturalWidth
image.height = data.naturalHeight
image.channels = 4
} else if (isVideoElement(data)) {
image.element = data
image.width = data.videoWidth
image.height = data.videoHeight
image.channels = 4
} else if (isRectArray(data)) {
var w = image.width || data[0].length
var h = image.height || data.length
var c = image.channels
if (isArrayLike(data[0][0])) {
c = c || data[0][0].length
} else {
c = c || 1
}
var arrayShape = flattenUtils.shape(data)
var n = 1
for (var dd = 0; dd < arrayShape.length; ++dd) {
n *= arrayShape[dd]
}
var allocData = preConvert(image, n)
flattenUtils.flatten(data, arrayShape, '', allocData)
postConvert(image, allocData)
image.alignment = 1
image.width = w
image.height = h
image.channels = c
image.format = image.internalformat = CHANNELS_FORMAT[c]
image.needsFree = true
}
if (image.type === GL_FLOAT) {
check(limits.extensions.indexOf('oes_texture_float') >= 0,
'oes_texture_float extension not enabled')
} else if (image.type === GL_HALF_FLOAT_OES) {
check(limits.extensions.indexOf('oes_texture_half_float') >= 0,
'oes_texture_half_float extension not enabled')
}
// do compressed texture validation here.
}
function setImage (info, target, miplevel) {
var element = info.element
var data = info.data
var internalformat = info.internalformat
var format = info.format
var type = info.type
var width = info.width
var height = info.height
setFlags(info)
if (element) {
gl.texImage2D(target, miplevel, format, format, type, element)
} else if (info.compressed) {
gl.compressedTexImage2D(target, miplevel, internalformat, width, height, 0, data)
} else if (info.needsCopy) {
reglPoll()
gl.copyTexImage2D(
target, miplevel, format, info.xOffset, info.yOffset, width, height, 0)
} else {
gl.texImage2D(
target, miplevel, format, width, height, 0, format, type, data)
}
}
function setSubImage (info, target, x, y, miplevel) {
var element = info.element
var data = info.data
var internalformat = info.internalformat
var format = info.format
var type = info.type
var width = info.width
var height = info.height
setFlags(info)
if (element) {
gl.texSubImage2D(
target, miplevel, x, y, format, type, element)
} else if (info.compressed) {
gl.compressedTexSubImage2D(
target, miplevel, x, y, internalformat, width, height, data)
} else if (info.needsCopy) {
reglPoll()
gl.copyTexSubImage2D(
target, miplevel, x, y, info.xOffset, info.yOffset, width, height)
} else {
gl.texSubImage2D(
target, miplevel, x, y, width, height, format, type, data)
}
}
// texImage pool
var imagePool = []
function allocImage () {
return imagePool.pop() || new TexImage()
}
function freeImage (image) {
if (image.needsFree) {
pool.freeType(image.data)
}
TexImage.call(image)
imagePool.push(image)
}
// -------------------------------------------------------
// Mip map
// -------------------------------------------------------
function MipMap () {
TexFlags.call(this)
this.genMipmaps = false
this.mipmapHint = GL_DONT_CARE
this.mipmask = 0
this.images = Array(16)
}
function parseMipMapFromShape (mipmap, width, height) {
var img = mipmap.images[0] = allocImage()
mipmap.mipmask = 1
img.width = mipmap.width = width
img.height = mipmap.height = height
img.channels = mipmap.channels = 4
}
function parseMipMapFromObject (mipmap, options) {
var imgData = null
if (isPixelData(options)) {
imgData = mipmap.images[0] = allocImage()
copyFlags(imgData, mipmap)
parseImage(imgData, options)
mipmap.mipmask = 1
} else {
parseFlags(mipmap, options)
if (Array.isArray(options.mipmap)) {
var mipData = options.mipmap
for (var i = 0; i < mipData.length; ++i) {
imgData = mipmap.images[i] = allocImage()
copyFlags(imgData, mipmap)
imgData.width >>= i
imgData.height >>= i
parseImage(imgData, mipData[i])
mipmap.mipmask |= (1 << i)
}
} else {
imgData = mipmap.images[0] = allocImage()
copyFlags(imgData, mipmap)
parseImage(imgData, options)
mipmap.mipmask = 1
}
}
copyFlags(mipmap, mipmap.images[0])
// For textures of the compressed format WEBGL_compressed_texture_s3tc
// we must have that
//
// "When level equals zero width and height must be a multiple of 4.
// When level is greater than 0 width and height must be 0, 1, 2 or a multiple of 4. "
//
// but we do not yet support having multiple mipmap levels for compressed textures,
// so we only test for level zero.
if (mipmap.compressed &&
(mipmap.internalformat === GL_COMPRESSED_RGB_S3TC_DXT1_EXT) ||
(mipmap.internalformat === GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ||
(mipmap.internalformat === GL_COMPRESSED_RGBA_S3TC_DXT3_EXT) ||
(mipmap.internalformat === GL_COMPRESSED_RGBA_S3TC_DXT5_EXT)) {
check(mipmap.width % 4 === 0 &&
mipmap.height % 4 === 0,
'for compressed texture formats, mipmap level 0 must have width and height that are a multiple of 4')
}
}
function setMipMap (mipmap, target) {
var images = mipmap.images
for (var i = 0; i < images.length; ++i) {
if (!images[i]) {
return
}
setImage(images[i], target, i)
}
}
var mipPool = []
function allocMipMap () {
var result = mipPool.pop() || new MipMap()
TexFlags.call(result)
result.mipmask = 0
for (var i = 0; i < 16; ++i) {
result.images[i] = null
}
return result
}
function freeMipMap (mipmap) {
var images = mipmap.images
for (var i = 0; i < images.length; ++i) {
if (images[i]) {
freeImage(images[i])
}
images[i] = null
}
mipPool.push(mipmap)
}
// -------------------------------------------------------
// Tex info
// -------------------------------------------------------
function TexInfo () {
this.minFilter = GL_NEAREST
this.magFilter = GL_NEAREST
this.wrapS = GL_CLAMP_TO_EDGE
this.wrapT = GL_CLAMP_TO_EDGE
this.anisotropic = 1