twgl.js 5.5.4 → 6.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- /* @license twgl.js 5.5.4 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
1
+ /* @license twgl.js 6.2.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
2
  Available via the MIT license.
3
3
  see: http://github.com/greggman/twgl.js for details */
4
4
  /*
@@ -5200,6 +5200,74 @@ const RED_INTEGER = 0x8D94;
5200
5200
  const RGB_INTEGER = 0x8D98;
5201
5201
  const RGBA_INTEGER = 0x8D99;
5202
5202
 
5203
+ /* Compressed Texture Formats */
5204
+ // s3tc
5205
+ const COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0;
5206
+ const COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1;
5207
+ const COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2;
5208
+ const COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
5209
+ // s3tc_srgb
5210
+ const COMPRESSED_SRGB_S3TC_DXT1_EXT = 0x8C4C;
5211
+ const COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT = 0x8C4D;
5212
+ const COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT = 0x8C4E;
5213
+ const COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT = 0x8C4F;
5214
+ // etc
5215
+ const COMPRESSED_RGB_ETC1_WEBGL = 0x8D64;
5216
+ const COMPRESSED_R11_EAC = 0x9270;
5217
+ const COMPRESSED_SIGNED_R11_EAC = 0x9271;
5218
+ const COMPRESSED_RG11_EAC = 0x9272;
5219
+ const COMPRESSED_SIGNED_RG11_EAC = 0x9273;
5220
+ const COMPRESSED_RGB8_ETC2 = 0x9274;
5221
+ const COMPRESSED_SRGB8_ETC2 = 0x9275;
5222
+ const COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9276;
5223
+ const COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9277;
5224
+ const COMPRESSED_RGBA8_ETC2_EAC = 0x9278;
5225
+ const COMPRESSED_SRGB8_ALPHA8_ETC2_EAC = 0x9279;
5226
+ // pvrtc
5227
+ const COMPRESSED_RGB_PVRTC_4BPPV1_IMG = 0x8C00;
5228
+ const COMPRESSED_RGB_PVRTC_2BPPV1_IMG = 0x8C01;
5229
+ const COMPRESSED_RGBA_PVRTC_4BPPV1_IMG = 0x8C02;
5230
+ const COMPRESSED_RGBA_PVRTC_2BPPV1_IMG = 0x8C03;
5231
+ // astc
5232
+ const COMPRESSED_RGBA_ASTC_4x4_KHR = 0x93B0;
5233
+ const COMPRESSED_RGBA_ASTC_5x4_KHR = 0x93B1;
5234
+ const COMPRESSED_RGBA_ASTC_5x5_KHR = 0x93B2;
5235
+ const COMPRESSED_RGBA_ASTC_6x5_KHR = 0x93B3;
5236
+ const COMPRESSED_RGBA_ASTC_6x6_KHR = 0x93B4;
5237
+ const COMPRESSED_RGBA_ASTC_8x5_KHR = 0x93B5;
5238
+ const COMPRESSED_RGBA_ASTC_8x6_KHR = 0x93B6;
5239
+ const COMPRESSED_RGBA_ASTC_8x8_KHR = 0x93B7;
5240
+ const COMPRESSED_RGBA_ASTC_10x5_KHR = 0x93B8;
5241
+ const COMPRESSED_RGBA_ASTC_10x6_KHR = 0x93B9;
5242
+ const COMPRESSED_RGBA_ASTC_10x8_KHR = 0x93BA;
5243
+ const COMPRESSED_RGBA_ASTC_10x10_KHR = 0x93BB;
5244
+ const COMPRESSED_RGBA_ASTC_12x10_KHR = 0x93BC;
5245
+ const COMPRESSED_RGBA_ASTC_12x12_KHR = 0x93BD;
5246
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR = 0x93D0;
5247
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR = 0x93D1;
5248
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR = 0x93D2;
5249
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR = 0x93D3;
5250
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR = 0x93D4;
5251
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR = 0x93D5;
5252
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR = 0x93D6;
5253
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR = 0x93D7;
5254
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR = 0x93D8;
5255
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR = 0x93D9;
5256
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR = 0x93DA;
5257
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR = 0x93DB;
5258
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR = 0x93DC;
5259
+ const COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR = 0x93DD;
5260
+ // bptc
5261
+ const COMPRESSED_RGBA_BPTC_UNORM_EXT = 0x8E8C;
5262
+ const COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT = 0x8E8D;
5263
+ const COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT = 0x8E8E;
5264
+ const COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT = 0x8E8F;
5265
+ // rgtc
5266
+ const COMPRESSED_RED_RGTC1_EXT = 0x8DBB;
5267
+ const COMPRESSED_SIGNED_RED_RGTC1_EXT = 0x8DBC;
5268
+ const COMPRESSED_RED_GREEN_RGTC2_EXT = 0x8DBD;
5269
+ const COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT = 0x8DBE;
5270
+
5203
5271
  const formatInfo = {};
5204
5272
  {
5205
5273
  // NOTE: this is named `numColorComponents` vs `numComponents` so we can let Uglify mangle
@@ -5228,7 +5296,8 @@ const formatInfo = {};
5228
5296
  * @property {boolean} colorRenderable true if you can render to this format of texture.
5229
5297
  * @property {boolean} textureFilterable true if you can filter the texture, false if you can ony use `NEAREST`.
5230
5298
  * @property {number[]} type Array of possible types you can pass to texImage2D and similar function
5231
- * @property {Object.<number,number>} bytesPerElementMap A map of types to bytes per element
5299
+ * @property {Object.<number,number>} [bytesPerElementMap] A map of types to bytes per element
5300
+ * @property {boolean} [compressed] true if it's a compressed texture format
5232
5301
  * @private
5233
5302
  */
5234
5303
 
@@ -5310,6 +5379,85 @@ function getTextureInternalFormatInfo(internalFormat) {
5310
5379
  info.bytesPerElementMap[type] = bytesPerElement;
5311
5380
  });
5312
5381
  });
5382
+
5383
+ const getCompressedFormatInfo = () => {
5384
+ const t = {};
5385
+ // compressed texture formats
5386
+ // s3tc:https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_compression_s3tc.txt
5387
+ t[COMPRESSED_RGB_S3TC_DXT1_EXT] = { textureFormat: COMPRESSED_RGB_S3TC_DXT1_EXT, colorRenderable: false, textureFilterable: true };
5388
+ t[COMPRESSED_RGBA_S3TC_DXT1_EXT] = { textureFormat: COMPRESSED_RGBA_S3TC_DXT1_EXT, colorRenderable: false, textureFilterable: true };
5389
+ t[COMPRESSED_RGBA_S3TC_DXT3_EXT] = { textureFormat: COMPRESSED_RGBA_S3TC_DXT3_EXT, colorRenderable: false, textureFilterable: true };
5390
+ t[COMPRESSED_RGBA_S3TC_DXT5_EXT] = { textureFormat: COMPRESSED_RGBA_S3TC_DXT5_EXT, colorRenderable: false, textureFilterable: true };
5391
+ // https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_compression_s3tc_srgb.txt
5392
+ t[COMPRESSED_SRGB_S3TC_DXT1_EXT] = { textureFormat: COMPRESSED_SRGB_S3TC_DXT1_EXT, colorRenderable: false, textureFilterable: true };
5393
+ t[COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT] = { textureFormat: COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, colorRenderable: false, textureFilterable: true };
5394
+ t[COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT] = { textureFormat: COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, colorRenderable: false, textureFilterable: true };
5395
+ t[COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT] = { textureFormat: COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, colorRenderable: false, textureFilterable: true };
5396
+ // https://registry.khronos.org/OpenGL/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt
5397
+ t[COMPRESSED_RGB_ETC1_WEBGL] = { textureFormat: COMPRESSED_RGB_ETC1_WEBGL, colorRenderable: false, textureFilterable: true };
5398
+ t[COMPRESSED_R11_EAC] = { textureFormat: COMPRESSED_R11_EAC, colorRenderable: false, textureFilterable: true };
5399
+ t[COMPRESSED_SIGNED_R11_EAC] = { textureFormat: COMPRESSED_SIGNED_R11_EAC, colorRenderable: false, textureFilterable: true };
5400
+ t[COMPRESSED_RG11_EAC] = { textureFormat: COMPRESSED_RG11_EAC, colorRenderable: false, textureFilterable: true };
5401
+ t[COMPRESSED_SIGNED_RG11_EAC] = { textureFormat: COMPRESSED_SIGNED_RG11_EAC, colorRenderable: false, textureFilterable: true };
5402
+ t[COMPRESSED_RGB8_ETC2] = { textureFormat: COMPRESSED_RGB8_ETC2, colorRenderable: false, textureFilterable: true };
5403
+ t[COMPRESSED_SRGB8_ETC2] = { textureFormat: COMPRESSED_SRGB8_ETC2, colorRenderable: false, textureFilterable: true };
5404
+ t[COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2] = { textureFormat: COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, colorRenderable: false, textureFilterable: true };
5405
+ t[COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2] = { textureFormat: COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, colorRenderable: false, textureFilterable: true };
5406
+ t[COMPRESSED_RGBA8_ETC2_EAC] = { textureFormat: COMPRESSED_RGBA8_ETC2_EAC, colorRenderable: false, textureFilterable: true };
5407
+ t[COMPRESSED_SRGB8_ALPHA8_ETC2_EAC] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, colorRenderable: false, textureFilterable: true };
5408
+ // https://registry.khronos.org/OpenGL/extensions/IMG/IMG_texture_compression_pvrtc.txt
5409
+ t[COMPRESSED_RGB_PVRTC_4BPPV1_IMG] = { textureFormat: COMPRESSED_RGB_PVRTC_4BPPV1_IMG, colorRenderable: false, textureFilterable: true };
5410
+ t[COMPRESSED_RGB_PVRTC_2BPPV1_IMG] = { textureFormat: COMPRESSED_RGB_PVRTC_2BPPV1_IMG, colorRenderable: false, textureFilterable: true };
5411
+ t[COMPRESSED_RGBA_PVRTC_4BPPV1_IMG] = { textureFormat: COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, colorRenderable: false, textureFilterable: true };
5412
+ t[COMPRESSED_RGBA_PVRTC_2BPPV1_IMG] = { textureFormat: COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, colorRenderable: false, textureFilterable: true };
5413
+ // https://registry.khronos.org/OpenGL/extensions/KHR/KHR_texture_compression_astc_hdr.txt
5414
+ t[COMPRESSED_RGBA_ASTC_4x4_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_4x4_KHR, colorRenderable: false, textureFilterable: true };
5415
+ t[COMPRESSED_RGBA_ASTC_5x4_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_5x4_KHR, colorRenderable: false, textureFilterable: true };
5416
+ t[COMPRESSED_RGBA_ASTC_5x5_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_5x5_KHR, colorRenderable: false, textureFilterable: true };
5417
+ t[COMPRESSED_RGBA_ASTC_6x5_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_6x5_KHR, colorRenderable: false, textureFilterable: true };
5418
+ t[COMPRESSED_RGBA_ASTC_6x6_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_6x6_KHR, colorRenderable: false, textureFilterable: true };
5419
+ t[COMPRESSED_RGBA_ASTC_8x5_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_8x5_KHR, colorRenderable: false, textureFilterable: true };
5420
+ t[COMPRESSED_RGBA_ASTC_8x6_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_8x6_KHR, colorRenderable: false, textureFilterable: true };
5421
+ t[COMPRESSED_RGBA_ASTC_8x8_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_8x8_KHR, colorRenderable: false, textureFilterable: true };
5422
+ t[COMPRESSED_RGBA_ASTC_10x5_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_10x5_KHR, colorRenderable: false, textureFilterable: true };
5423
+ t[COMPRESSED_RGBA_ASTC_10x6_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_10x6_KHR, colorRenderable: false, textureFilterable: true };
5424
+ t[COMPRESSED_RGBA_ASTC_10x8_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_10x8_KHR, colorRenderable: false, textureFilterable: true };
5425
+ t[COMPRESSED_RGBA_ASTC_10x10_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_10x10_KHR, colorRenderable: false, textureFilterable: true };
5426
+ t[COMPRESSED_RGBA_ASTC_12x10_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_12x10_KHR, colorRenderable: false, textureFilterable: true };
5427
+ t[COMPRESSED_RGBA_ASTC_12x12_KHR] = { textureFormat: COMPRESSED_RGBA_ASTC_12x12_KHR, colorRenderable: false, textureFilterable: true };
5428
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, colorRenderable: false, textureFilterable: true };
5429
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR, colorRenderable: false, textureFilterable: true };
5430
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, colorRenderable: false, textureFilterable: true };
5431
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR, colorRenderable: false, textureFilterable: true };
5432
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, colorRenderable: false, textureFilterable: true };
5433
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, colorRenderable: false, textureFilterable: true };
5434
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, colorRenderable: false, textureFilterable: true };
5435
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR, colorRenderable: false, textureFilterable: true };
5436
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, colorRenderable: false, textureFilterable: true };
5437
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR, colorRenderable: false, textureFilterable: true };
5438
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR, colorRenderable: false, textureFilterable: true };
5439
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR, colorRenderable: false, textureFilterable: true };
5440
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, colorRenderable: false, textureFilterable: true };
5441
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR] = { textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR, colorRenderable: false, textureFilterable: true };
5442
+ // https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_compression_bptc.txt
5443
+ t[COMPRESSED_RGBA_BPTC_UNORM_EXT] = { textureFormat: COMPRESSED_RGBA_BPTC_UNORM_EXT, colorRenderable: false, textureFilterable: true };
5444
+ t[COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT] = { textureFormat: COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT, colorRenderable: false, textureFilterable: true };
5445
+ t[COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT] = { textureFormat: COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT, colorRenderable: false, textureFilterable: true };
5446
+ t[COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT] = { textureFormat: COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT, colorRenderable: false, textureFilterable: true };
5447
+ // https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_compression_rgtc.txt
5448
+ t[COMPRESSED_RED_RGTC1_EXT] = { textureFormat: COMPRESSED_RED_RGTC1_EXT, colorRenderable: false, textureFilterable: true };
5449
+ t[COMPRESSED_SIGNED_RED_RGTC1_EXT] = { textureFormat: COMPRESSED_SIGNED_RED_RGTC1_EXT, colorRenderable: false, textureFilterable: true };
5450
+ t[COMPRESSED_RED_GREEN_RGTC2_EXT] = { textureFormat: COMPRESSED_RED_GREEN_RGTC2_EXT, colorRenderable: false, textureFilterable: true };
5451
+ t[COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT] = { textureFormat: COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT, colorRenderable: false, textureFilterable: true };
5452
+
5453
+ for (const info of Object.values(t)) {
5454
+ info.compressed = true;
5455
+ }
5456
+
5457
+ return t;
5458
+ };
5459
+ Object.assign(t, getCompressedFormatInfo());
5460
+
5313
5461
  s_textureInternalFormatInfo = t;
5314
5462
  }
5315
5463
  return s_textureInternalFormatInfo[internalFormat];
@@ -5358,10 +5506,23 @@ function getFormatAndTypeForInternalFormat(internalFormat) {
5358
5506
  }
5359
5507
  return {
5360
5508
  format: info.textureFormat,
5361
- type: info.type[0],
5509
+ type: info.type?.[0] ?? UNSIGNED_BYTE$1,
5362
5510
  };
5363
5511
  }
5364
5512
 
5513
+ /**
5514
+ * @param {number} internalFormat The internal format
5515
+ * @returns if the internalFormat is a compressed format
5516
+ * @private
5517
+ */
5518
+ function isCompressedInternalFormat(internalFormat) {
5519
+ const info = getTextureInternalFormatInfo(internalFormat);
5520
+ if (!info) {
5521
+ throw "unknown internal format";
5522
+ }
5523
+ return !!info.compressed;
5524
+ }
5525
+
5365
5526
  /**
5366
5527
  * Returns true if value is power of 2
5367
5528
  * @param {number} value number to check.
@@ -5594,21 +5755,72 @@ function setDefaults$1(newDefaults) {
5594
5755
  */
5595
5756
 
5596
5757
  /**
5597
- * Sets any packing state that will be set based on the options.
5598
- * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
5758
+ * Saves the current packing state, sets the packing state as specified
5759
+ * then calls a function, after which the packing state will be restored.
5599
5760
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5761
+ * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
5762
+ * @param {function():void} [fn] A function to call, after which the packing state will be restored.
5600
5763
  * @private
5601
5764
  */
5602
- function setPackState(gl, options) {
5765
+ function scopedSetPackState(gl, options, fn) {
5766
+ let colorspaceConversion;
5767
+ let premultiplyAlpha;
5768
+ let flipY;
5769
+
5603
5770
  if (options.colorspaceConversion !== undefined) {
5771
+ colorspaceConversion = gl.getParameter(UNPACK_COLORSPACE_CONVERSION_WEBGL);
5604
5772
  gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, options.colorspaceConversion);
5605
5773
  }
5606
5774
  if (options.premultiplyAlpha !== undefined) {
5775
+ premultiplyAlpha = gl.getParameter(UNPACK_PREMULTIPLY_ALPHA_WEBGL);
5607
5776
  gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, options.premultiplyAlpha);
5608
5777
  }
5609
5778
  if (options.flipY !== undefined) {
5779
+ flipY = gl.getParameter(UNPACK_FLIP_Y_WEBGL);
5610
5780
  gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, options.flipY);
5611
5781
  }
5782
+
5783
+ fn();
5784
+
5785
+ if (colorspaceConversion !== undefined) {
5786
+ gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, colorspaceConversion);
5787
+ }
5788
+ if (premultiplyAlpha !== undefined) {
5789
+ gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
5790
+ }
5791
+ if (flipY !== undefined) {
5792
+ gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, flipY);
5793
+ }
5794
+ }
5795
+
5796
+ /**
5797
+ * returns the property if set or the corresponding state if undefined
5798
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5799
+ * @param {module:twgl.TextureOptions} options
5800
+ * @param {string} property the name of the property to copy
5801
+ * @param {number} pname
5802
+ * @return {module:twgl.TextureOptions}
5803
+ */
5804
+ function getPackStateOption(gl, options, property, pname) {
5805
+ const v = options[property];
5806
+ return {
5807
+ [property]: v === undefined ? gl.getParameter(pname) : v,
5808
+ };
5809
+ }
5810
+
5811
+ /**
5812
+ * Copy the options object and apply pack state
5813
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5814
+ * @param {module:twgl.TextureOptions} options
5815
+ * @return {module:twgl.TextureOptions}
5816
+ */
5817
+ function copyOptionsAndApplyPackState(gl, options) {
5818
+ return {
5819
+ ...options,
5820
+ ...getPackStateOption(gl, options, 'flipY', UNPACK_FLIP_Y_WEBGL),
5821
+ ...getPackStateOption(gl, options, 'premultiplyAlpha', UNPACK_PREMULTIPLY_ALPHA_WEBGL),
5822
+ ...getPackStateOption(gl, options, 'colorspaceConversion', UNPACK_COLORSPACE_CONVERSION_WEBGL),
5823
+ };
5612
5824
  }
5613
5825
 
5614
5826
  /**
@@ -5897,7 +6109,6 @@ function setTextureFromElement(gl, tex, element, options) {
5897
6109
  const formatType = getFormatAndTypeForInternalFormat(internalFormat);
5898
6110
  const format = options.format || formatType.format;
5899
6111
  const type = options.type || formatType.type;
5900
- setPackState(gl, options);
5901
6112
  gl.bindTexture(target, tex);
5902
6113
  if (target === TEXTURE_CUBE_MAP$1) {
5903
6114
  // guess the parts
@@ -5930,15 +6141,17 @@ function setTextureFromElement(gl, tex, element, options) {
5930
6141
  ctx.canvas.height = size;
5931
6142
  width = size;
5932
6143
  height = size;
5933
- getCubeFacesWithNdx(gl, options).forEach(function(f) {
5934
- const xOffset = slices[f.ndx * 2 + 0] * size;
5935
- const yOffset = slices[f.ndx * 2 + 1] * size;
5936
- ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
5937
- gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
6144
+ scopedSetPackState(gl, options, () => {
6145
+ getCubeFacesWithNdx(gl, options).forEach(function(f) {
6146
+ const xOffset = slices[f.ndx * 2 + 0] * size;
6147
+ const yOffset = slices[f.ndx * 2 + 1] * size;
6148
+ ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
6149
+ gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
6150
+ });
6151
+ // Free up the canvas memory
6152
+ ctx.canvas.width = 1;
6153
+ ctx.canvas.height = 1;
5938
6154
  });
5939
- // Free up the canvas memory
5940
- ctx.canvas.width = 1;
5941
- ctx.canvas.height = 1;
5942
6155
  } else if (typeof createImageBitmap !== 'undefined') {
5943
6156
  // NOTE: It seems like we should prefer ImageBitmap because unlike canvas it's
5944
6157
  // note lossy? (alpha is not premultiplied? although I'm not sure what
@@ -5958,39 +6171,44 @@ function setTextureFromElement(gl, tex, element, options) {
5958
6171
  colorSpaceConversion: 'none',
5959
6172
  })
5960
6173
  .then(function(imageBitmap) {
5961
- setPackState(gl, options);
5962
- gl.bindTexture(target, tex);
5963
- gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
5964
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5965
- setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
5966
- }
6174
+ scopedSetPackState(gl, options, () => {
6175
+ gl.bindTexture(target, tex);
6176
+ gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
6177
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6178
+ setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
6179
+ }
6180
+ });
5967
6181
  });
5968
6182
  });
5969
6183
  }
5970
6184
  } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
5971
- const smallest = Math.min(element.width, element.height);
5972
- const largest = Math.max(element.width, element.height);
5973
- const depth = largest / smallest;
5974
- if (depth % 1 !== 0) {
5975
- throw "can not compute 3D dimensions of element";
5976
- }
5977
- const xMult = element.width === largest ? 1 : 0;
5978
- const yMult = element.height === largest ? 1 : 0;
5979
- gl.pixelStorei(UNPACK_ALIGNMENT, 1);
5980
- gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
5981
- gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
5982
- gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
5983
- gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
5984
- for (let d = 0; d < depth; ++d) {
5985
- const srcX = d * smallest * xMult;
5986
- const srcY = d * smallest * yMult;
5987
- gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
5988
- gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
5989
- gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
5990
- }
5991
- setSkipStateToDefault(gl);
6185
+ scopedSetPackState(gl, options, () => {
6186
+ const smallest = Math.min(element.width, element.height);
6187
+ const largest = Math.max(element.width, element.height);
6188
+ const depth = largest / smallest;
6189
+ if (depth % 1 !== 0) {
6190
+ throw "can not compute 3D dimensions of element";
6191
+ }
6192
+ const xMult = element.width === largest ? 1 : 0;
6193
+ const yMult = element.height === largest ? 1 : 0;
6194
+ gl.pixelStorei(UNPACK_ALIGNMENT, 1);
6195
+ gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
6196
+ gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
6197
+ gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
6198
+ gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
6199
+ for (let d = 0; d < depth; ++d) {
6200
+ const srcX = d * smallest * xMult;
6201
+ const srcY = d * smallest * yMult;
6202
+ gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
6203
+ gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
6204
+ gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
6205
+ }
6206
+ setSkipStateToDefault(gl);
6207
+ });
5992
6208
  } else {
5993
- gl.texImage2D(target, level, internalFormat, format, type, element);
6209
+ scopedSetPackState(gl, options, () => {
6210
+ gl.texImage2D(target, level, internalFormat, format, type, element);
6211
+ });
5994
6212
  }
5995
6213
  if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5996
6214
  setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
@@ -6239,7 +6457,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
6239
6457
  options = options || defaults$1.textureOptions;
6240
6458
  setTextureTo1PixelColor(gl, tex, options);
6241
6459
  // Because it's async we need to copy the options.
6242
- options = Object.assign({}, options);
6460
+ options = copyOptionsAndApplyPackState(gl, options);
6243
6461
  const img = loadAndUseImage(options.src, options.crossOrigin, function(err, img) {
6244
6462
  if (err) {
6245
6463
  callback(err, tex, img);
@@ -6279,7 +6497,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
6279
6497
  }
6280
6498
  setTextureTo1PixelColor(gl, tex, options);
6281
6499
  // Because it's async we need to copy the options.
6282
- options = Object.assign({}, options);
6500
+ options = copyOptionsAndApplyPackState(gl, options);
6283
6501
  let numToLoad = 6;
6284
6502
  const errors = [];
6285
6503
  const faces = getCubeFaceOrder(gl, options);
@@ -6294,24 +6512,25 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
6294
6512
  if (img.width !== img.height) {
6295
6513
  errors.push("cubemap face img is not a square: " + img.src);
6296
6514
  } else {
6297
- setPackState(gl, options);
6298
- gl.bindTexture(target, tex);
6299
-
6300
- // So assuming this is the first image we now have one face that's img sized
6301
- // and 5 faces that are 1x1 pixel so size the other faces
6302
- if (numToLoad === 5) {
6303
- // use the default order
6304
- getCubeFaceOrder().forEach(function(otherTarget) {
6305
- // Should we re-use the same face or a color?
6306
- gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
6307
- });
6308
- } else {
6309
- gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
6310
- }
6311
-
6312
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6313
- gl.generateMipmap(target);
6314
- }
6515
+ scopedSetPackState(gl, options, () => {
6516
+ gl.bindTexture(target, tex);
6517
+
6518
+ // So assuming this is the first image we now have one face that's img sized
6519
+ // and 5 faces that are 1x1 pixel so size the other faces
6520
+ if (numToLoad === 5) {
6521
+ // use the default order
6522
+ getCubeFaceOrder().forEach(function(otherTarget) {
6523
+ // Should we re-use the same face or a color?
6524
+ gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
6525
+ });
6526
+ } else {
6527
+ gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
6528
+ }
6529
+
6530
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6531
+ gl.generateMipmap(target);
6532
+ }
6533
+ });
6315
6534
  }
6316
6535
  }
6317
6536
 
@@ -6359,7 +6578,7 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
6359
6578
  }
6360
6579
  setTextureTo1PixelColor(gl, tex, options);
6361
6580
  // Because it's async we need to copy the options.
6362
- options = Object.assign({}, options);
6581
+ options = copyOptionsAndApplyPackState(gl, options);
6363
6582
  let numToLoad = urls.length;
6364
6583
  const errors = [];
6365
6584
  let imgs; // eslint-disable-line
@@ -6375,43 +6594,44 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
6375
6594
  if (err) {
6376
6595
  errors.push(err);
6377
6596
  } else {
6378
- setPackState(gl, options);
6379
- gl.bindTexture(target, tex);
6380
-
6381
- if (firstImage) {
6382
- firstImage = false;
6383
- width = options.width || img.width;
6384
- height = options.height || img.height;
6385
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
6386
-
6387
- // put it in every slice otherwise some slices will be 0,0,0,0
6388
- for (let s = 0; s < depth; ++s) {
6389
- gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
6390
- }
6391
- } else {
6392
- let src = img;
6393
- let ctx;
6394
- if (img.width !== width || img.height !== height) {
6395
- // Size the image to fix
6396
- ctx = getShared2DContext();
6397
- src = ctx.canvas;
6398
- ctx.canvas.width = width;
6399
- ctx.canvas.height = height;
6400
- ctx.drawImage(img, 0, 0, width, height);
6401
- }
6597
+ scopedSetPackState(gl, options, () => {
6598
+ gl.bindTexture(target, tex);
6402
6599
 
6403
- gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
6600
+ if (firstImage) {
6601
+ firstImage = false;
6602
+ width = options.width || img.width;
6603
+ height = options.height || img.height;
6604
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
6404
6605
 
6405
- // free the canvas memory
6406
- if (ctx && src === ctx.canvas) {
6407
- ctx.canvas.width = 0;
6408
- ctx.canvas.height = 0;
6606
+ // put it in every slice otherwise some slices will be 0,0,0,0
6607
+ for (let s = 0; s < depth; ++s) {
6608
+ gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
6609
+ }
6610
+ } else {
6611
+ let src = img;
6612
+ let ctx;
6613
+ if (img.width !== width || img.height !== height) {
6614
+ // Size the image to fix
6615
+ ctx = getShared2DContext();
6616
+ src = ctx.canvas;
6617
+ ctx.canvas.width = width;
6618
+ ctx.canvas.height = height;
6619
+ ctx.drawImage(img, 0, 0, width, height);
6620
+ }
6621
+
6622
+ gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
6623
+
6624
+ // free the canvas memory
6625
+ if (ctx && src === ctx.canvas) {
6626
+ ctx.canvas.width = 0;
6627
+ ctx.canvas.height = 0;
6628
+ }
6409
6629
  }
6410
- }
6411
6630
 
6412
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6413
- gl.generateMipmap(target);
6414
- }
6631
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6632
+ gl.generateMipmap(target);
6633
+ }
6634
+ });
6415
6635
  }
6416
6636
 
6417
6637
  if (numToLoad === 0) {
@@ -6439,9 +6659,6 @@ function setTextureFromArray(gl, tex, src, options) {
6439
6659
  options = options || defaults$1.textureOptions;
6440
6660
  const target = options.target || TEXTURE_2D$2;
6441
6661
  gl.bindTexture(target, tex);
6442
- let width = options.width;
6443
- let height = options.height;
6444
- let depth = options.depth;
6445
6662
  const level = options.level || 0;
6446
6663
  const internalFormat = options.internalFormat || options.format || RGBA$1;
6447
6664
  const formatType = getFormatAndTypeForInternalFormat(internalFormat);
@@ -6454,61 +6671,91 @@ function setTextureFromArray(gl, tex, src, options) {
6454
6671
  src = new Uint8Array(src.buffer);
6455
6672
  }
6456
6673
 
6457
- const bytesPerElement = getBytesPerElementForInternalFormat(internalFormat, type);
6458
- const numElements = src.byteLength / bytesPerElement; // TODO: check UNPACK_ALIGNMENT?
6459
- if (numElements % 1) {
6460
- throw "length wrong size for format: " + glEnumToString(gl, format);
6461
- }
6462
- let dimensions;
6463
- if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6464
- if (!width && !height && !depth) {
6465
- const size = Math.cbrt(numElements);
6466
- if (size % 1 !== 0) {
6467
- throw "can't guess cube size of array of numElements: " + numElements;
6674
+ const { width, height, depth } = getTextureSize(gl, target, src, options, internalFormat, format, type);
6675
+ const compressed = isCompressedInternalFormat(internalFormat);
6676
+
6677
+ setSkipStateToDefault(gl);
6678
+ gl.pixelStorei(UNPACK_ALIGNMENT, options.unpackAlignment || 1);
6679
+ scopedSetPackState(gl, options, () => {
6680
+ if (target === TEXTURE_CUBE_MAP$1) {
6681
+ const faceSize = src.length / 6;
6682
+ getCubeFacesWithNdx(gl, options).forEach(f => {
6683
+ const offset = faceSize * f.ndx;
6684
+ const data = src.subarray(offset, offset + faceSize);
6685
+ if (compressed) {
6686
+ gl.compressedTexImage2D(f.face, level, internalFormat, width, height, 0, data);
6687
+ } else {
6688
+ gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
6689
+ }
6690
+ });
6691
+ } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6692
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
6693
+ } else {
6694
+ if (compressed) {
6695
+ gl.compressedTexImage2D(target, level, internalFormat, width, height, 0, src);
6696
+ } else {
6697
+ gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
6698
+ }
6699
+ }
6700
+ });
6701
+ return {
6702
+ width: width,
6703
+ height: height,
6704
+ depth: depth,
6705
+ type: type,
6706
+ };
6707
+ }
6708
+
6709
+ function getTextureSize(gl, target, src, options, internalFormat, format, type) {
6710
+ let width = options.width;
6711
+ let height = options.height;
6712
+ let depth = options.depth;
6713
+
6714
+ const compressed = isCompressedInternalFormat(internalFormat);
6715
+ if (compressed) {
6716
+ if (!width || !height) {
6717
+ throw "compressed texture needs to set width and height!";
6718
+ }
6719
+ } else {
6720
+ const bytesPerElement = getBytesPerElementForInternalFormat(internalFormat, type);
6721
+ const numElements = src.byteLength / bytesPerElement; // TODO: check UNPACK_ALIGNMENT?
6722
+ if (numElements % 1) {
6723
+ throw "length wrong size for format: " + glEnumToString(gl, format);
6724
+ }
6725
+ let dimensions;
6726
+ if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6727
+ if (!width && !height && !depth) {
6728
+ const size = Math.cbrt(numElements);
6729
+ if (size % 1 !== 0) {
6730
+ throw "can't guess cube size of array of numElements: " + numElements;
6731
+ }
6732
+ width = size;
6733
+ height = size;
6734
+ depth = size;
6735
+ } else if (width && (!height || !depth)) {
6736
+ dimensions = guessDimensions(gl, target, height, depth, numElements / width);
6737
+ height = dimensions.width;
6738
+ depth = dimensions.height;
6739
+ } else if (height && (!width || !depth)) {
6740
+ dimensions = guessDimensions(gl, target, width, depth, numElements / height);
6741
+ width = dimensions.width;
6742
+ depth = dimensions.height;
6743
+ } else {
6744
+ dimensions = guessDimensions(gl, target, width, height, numElements / depth);
6745
+ width = dimensions.width;
6746
+ height = dimensions.height;
6468
6747
  }
6469
- width = size;
6470
- height = size;
6471
- depth = size;
6472
- } else if (width && (!height || !depth)) {
6473
- dimensions = guessDimensions(gl, target, height, depth, numElements / width);
6474
- height = dimensions.width;
6475
- depth = dimensions.height;
6476
- } else if (height && (!width || !depth)) {
6477
- dimensions = guessDimensions(gl, target, width, depth, numElements / height);
6478
- width = dimensions.width;
6479
- depth = dimensions.height;
6480
6748
  } else {
6481
- dimensions = guessDimensions(gl, target, width, height, numElements / depth);
6749
+ dimensions = guessDimensions(gl, target, width, height, numElements);
6482
6750
  width = dimensions.width;
6483
6751
  height = dimensions.height;
6484
6752
  }
6485
- } else {
6486
- dimensions = guessDimensions(gl, target, width, height, numElements);
6487
- width = dimensions.width;
6488
- height = dimensions.height;
6489
6753
  }
6490
- setSkipStateToDefault(gl);
6491
- gl.pixelStorei(UNPACK_ALIGNMENT, options.unpackAlignment || 1);
6492
- setPackState(gl, options);
6493
- if (target === TEXTURE_CUBE_MAP$1) {
6494
- const elementsPerElement = bytesPerElement / src.BYTES_PER_ELEMENT;
6495
- const faceSize = numElements / 6 * elementsPerElement;
6496
6754
 
6497
- getCubeFacesWithNdx(gl, options).forEach(f => {
6498
- const offset = faceSize * f.ndx;
6499
- const data = src.subarray(offset, offset + faceSize);
6500
- gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
6501
- });
6502
- } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6503
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
6504
- } else {
6505
- gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
6506
- }
6507
6755
  return {
6508
- width: width,
6509
- height: height,
6510
- depth: depth,
6511
- type: type,
6756
+ width,
6757
+ height,
6758
+ depth,
6512
6759
  };
6513
6760
  }
6514
6761
 
@@ -6528,24 +6775,46 @@ function setEmptyTexture(gl, tex, options) {
6528
6775
  const formatType = getFormatAndTypeForInternalFormat(internalFormat);
6529
6776
  const format = options.format || formatType.format;
6530
6777
  const type = options.type || formatType.type;
6531
- setPackState(gl, options);
6532
- if (target === TEXTURE_CUBE_MAP$1) {
6533
- for (let ii = 0; ii < 6; ++ii) {
6534
- gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
6778
+ scopedSetPackState(gl, options, () => {
6779
+ if (target === TEXTURE_CUBE_MAP$1) {
6780
+ for (let ii = 0; ii < 6; ++ii) {
6781
+ gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
6782
+ }
6783
+ } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6784
+ gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
6785
+ } else {
6786
+ gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
6535
6787
  }
6536
- } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6537
- gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
6538
- } else {
6539
- gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
6540
- }
6788
+ });
6541
6789
  }
6542
6790
 
6543
6791
  /**
6544
6792
  * Creates a texture based on the options passed in.
6545
6793
  *
6794
+ * See {@link module:twgl.TextureOptions}
6795
+ *
6546
6796
  * Note: may reset UNPACK_ALIGNMENT, UNPACK_ROW_LENGTH, UNPACK_IMAGE_HEIGHT, UNPACK_SKIP_IMAGES
6547
6797
  * UNPACK_SKIP_PIXELS, and UNPACK_SKIP_ROWS
6548
6798
  *
6799
+ * UNPACK_FLIP_Y_WEBGL, UNPACK_PREMULTIPLY_ALPHA_WEBGL, UNPACK_COLORSPACE_CONVERSION_WEBGL
6800
+ * are left as is though you can pass in options for flipY, premultiplyAlpha, and colorspaceConversion
6801
+ * to override them.
6802
+ *
6803
+ * As for the behavior of these settings
6804
+ *
6805
+ * ```js
6806
+ * gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
6807
+ * t1 = twgl.createTexture({src: someImage }); // flipped
6808
+ * t2 = twgl.createTexture({src: someImage, flipY: true }); // flipped
6809
+ * t3 = twgl.createTexture({src: someImage, flipY: false }); // not flipped
6810
+ * t4 = twgl.createTexture({src: someImage }); // flipped
6811
+ * ```
6812
+ *
6813
+ * * t1 is flipped because UNPACK_FLIP_Y_WEBGL is true
6814
+ * * t2 is flipped because it was requested
6815
+ * * t3 is not flipped because it was requested
6816
+ * * t4 is flipped because UNPACK_FLIP_Y_WEBGL has been restored to true
6817
+ *
6549
6818
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6550
6819
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
6551
6820
  * @param {module:twgl.TextureReadyCallback} [callback] A callback called when an image has been downloaded and uploaded to the texture.
@@ -6604,6 +6873,42 @@ function createTexture(gl, options, callback) {
6604
6873
  return tex;
6605
6874
  }
6606
6875
 
6876
+ /**
6877
+ * Value returned by createTextureAsync
6878
+ *
6879
+ * @typedef {Object} CreateTextureInfo
6880
+ * @param {WebGLTexture} texture the texture.
6881
+ * @param {module:twgl.TextureSrc} source image(s) used to as the src for the texture
6882
+ * @memberOf module:twgl
6883
+ */
6884
+
6885
+ /**
6886
+ * Creates a texture based on the options passed in.
6887
+ *
6888
+ * see {@link module:twgl/textures.createTexture}.
6889
+ * The only difference is this function returns a promise
6890
+ * where as the other returns a texture and takes a callback.
6891
+ *
6892
+ * Note: this is here for completeness. It is probably better to use
6893
+ * the non-async version as it returns a usable texture immediately
6894
+ * where as this one you have to wait for it to load.
6895
+ *
6896
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6897
+ * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
6898
+ * @return {Promise<CreateTextureInfo>} The created texture and source.
6899
+ */
6900
+ function createTextureAsync(gl, options) {
6901
+ return new Promise((resolve, reject) => {
6902
+ createTexture(gl, options, (err, texture, source) => {
6903
+ if (err) {
6904
+ reject(err);
6905
+ } else {
6906
+ resolve({ texture, source });
6907
+ }
6908
+ });
6909
+ });
6910
+ }
6911
+
6607
6912
  /**
6608
6913
  * Resizes a texture based on the options passed in.
6609
6914
  *
@@ -6780,6 +7085,42 @@ function createTextures(gl, textureOptions, callback) {
6780
7085
  return textures;
6781
7086
  }
6782
7087
 
7088
+ /**
7089
+ * Value returned by createTextureAsync
7090
+ *
7091
+ * @typedef {Object} CreateTexturesInfo
7092
+ * @param {Object.<string, WebGLTexture>} textures the created textures by name. Same as returned by {@link module:twgl.createTextures}.
7093
+ * @param {Object.<string, module:twgl.TextureSrc>} sources the image(s) used for the texture by name.
7094
+ * @memberOf module:twgl
7095
+ */
7096
+
7097
+ /**
7098
+ * Creates textures based on the options passed in.
7099
+ *
7100
+ * see {@link module:twgl/textures.createTextures}.
7101
+ * The only difference is this function returns a promise
7102
+ * where as the other returns a texture and takes a callback.
7103
+ *
7104
+ * Note: this is here for completeness. It is probably better to use
7105
+ * the non-async version as it returns usable textures immediately
7106
+ * where as this one you have to wait for them to load.
7107
+ *
7108
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
7109
+ * @param {Object.<string,module:twgl.TextureOptions>} options A object of TextureOptions one per texture.
7110
+ * @return {Promise<CreateTexturesInfo>} The created textures and sources.
7111
+ */
7112
+ function createTexturesAsync(gl, options) {
7113
+ return new Promise((resolve, reject) => {
7114
+ createTexture(gl, options, (err, textures, sources) => {
7115
+ if (err) {
7116
+ reject(err);
7117
+ } else {
7118
+ resolve({ textures, sources });
7119
+ }
7120
+ });
7121
+ });
7122
+ }
7123
+
6783
7124
  var textures = /*#__PURE__*/Object.freeze({
6784
7125
  __proto__: null,
6785
7126
  setTextureDefaults_: setDefaults$1,
@@ -6787,6 +7128,7 @@ var textures = /*#__PURE__*/Object.freeze({
6787
7128
  createSamplers: createSamplers,
6788
7129
  setSamplerParameters: setSamplerParameters,
6789
7130
  createTexture: createTexture,
7131
+ createTextureAsync: createTextureAsync,
6790
7132
  setEmptyTexture: setEmptyTexture,
6791
7133
  setTextureFromArray: setTextureFromArray,
6792
7134
  loadTextureFromUrl: loadTextureFromUrl,
@@ -6795,6 +7137,7 @@ var textures = /*#__PURE__*/Object.freeze({
6795
7137
  setTextureParameters: setTextureParameters,
6796
7138
  setDefaultTextureColor: setDefaultTextureColor,
6797
7139
  createTextures: createTextures,
7140
+ createTexturesAsync: createTexturesAsync,
6798
7141
  resizeTexture: resizeTexture,
6799
7142
  canGenerateMipmap: canGenerateMipmap,
6800
7143
  canFilter: canFilter,
@@ -7497,7 +7840,7 @@ function getShaderTypeFromScriptType(gl, scriptType) {
7497
7840
  function deleteProgramAndShaders(gl, program, notThese) {
7498
7841
  const shaders = gl.getAttachedShaders(program);
7499
7842
  for (const shader of shaders) {
7500
- if (notThese.has(shader)) {
7843
+ if (!notThese.has(shader)) {
7501
7844
  gl.deleteShader(shader);
7502
7845
  }
7503
7846
  }
@@ -7526,8 +7869,8 @@ function createProgramNoCheck(gl, shaders, programOptions) {
7526
7869
  shader = gl.createShader(type);
7527
7870
  gl.shaderSource(shader, prepShaderSource(src).shaderSource);
7528
7871
  gl.compileShader(shader);
7529
- gl.attachShader(program, shader);
7530
7872
  }
7873
+ gl.attachShader(program, shader);
7531
7874
  }
7532
7875
 
7533
7876
  Object.entries(attribLocations).forEach(([attrib, loc]) => gl.bindAttribLocation(program, loc, attrib));
@@ -8101,6 +8444,7 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
8101
8444
  * @property {ArrayBuffer} array The array buffer that contains the uniform values
8102
8445
  * @property {Float32Array} asFloat A float view on the array buffer. This is useful
8103
8446
  * inspecting the contents of the buffer in the debugger.
8447
+ * @property {Uint8Array} asUint8t A uint8 view on the array buffer.
8104
8448
  * @property {WebGLBuffer} buffer A WebGL buffer that will hold a copy of the uniform values for rendering.
8105
8449
  * @property {number} [offset] offset into buffer
8106
8450
  * @property {Object<string, ArrayBufferView>} uniforms A uniform name to ArrayBufferView map.
@@ -8119,6 +8463,15 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
8119
8463
  * @memberOf module:twgl
8120
8464
  */
8121
8465
 
8466
+ /**
8467
+ * Options to allow createUniformBlockInfo to use an existing buffer and arrayBuffer at an offset
8468
+ * @typedef {Object} UniformBlockInfoOptions
8469
+ * @property {ArrayBuffer} [array] an existing array buffer to use for values
8470
+ * @property {number} [offset] the offset in bytes to use in the array buffer (default = 0)
8471
+ * @property {WebGLBuffer} [buffer] the buffer to use for this uniform block info
8472
+ * @property {number} [bufferOffset] the offset in bytes in the buffer to use (default = use offset above)
8473
+ */
8474
+
8122
8475
  /**
8123
8476
  * Creates a `UniformBlockInfo` for the specified block
8124
8477
  *
@@ -8133,10 +8486,11 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
8133
8486
  * @param {module:twgl.UniformBlockSpec} uniformBlockSpec. A UniformBlockSpec as returned
8134
8487
  * from {@link module:twgl.createUniformBlockSpecFromProgram}.
8135
8488
  * @param {string} blockName The name of the block.
8489
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
8136
8490
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
8137
8491
  * @memberOf module:twgl/programs
8138
8492
  */
8139
- function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockName) {
8493
+ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockName, options = {}) {
8140
8494
  const blockSpecs = uniformBlockSpec.blockSpecs;
8141
8495
  const uniformData = uniformBlockSpec.uniformData;
8142
8496
  const blockSpec = blockSpecs[blockName];
@@ -8147,10 +8501,14 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
8147
8501
  uniforms: {},
8148
8502
  };
8149
8503
  }
8150
- const array = new ArrayBuffer(blockSpec.size);
8151
- const buffer = gl.createBuffer();
8504
+ const offset = options.offset ?? 0;
8505
+ const array = options.array ?? new ArrayBuffer(blockSpec.size);
8506
+ const buffer = options.buffer ?? gl.createBuffer();
8152
8507
  const uniformBufferIndex = blockSpec.index;
8153
8508
  gl.bindBuffer(UNIFORM_BUFFER, buffer);
8509
+ if (!options.buffer) {
8510
+ gl.bufferData(UNIFORM_BUFFER, array.byteLength, DYNAMIC_DRAW);
8511
+ }
8154
8512
  gl.uniformBlockBinding(program, blockSpec.index, uniformBufferIndex);
8155
8513
 
8156
8514
  let prefix = blockName + ".";
@@ -8175,7 +8533,7 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
8175
8533
  const byteLength = isArray
8176
8534
  ? pad(typeInfo.size, 16) * data.size
8177
8535
  : typeInfo.size * data.size;
8178
- const uniformView = new Type(array, data.offset, byteLength / Type.BYTES_PER_ELEMENT);
8536
+ const uniformView = new Type(array, offset + data.offset, byteLength / Type.BYTES_PER_ELEMENT);
8179
8537
  uniforms[name] = uniformView;
8180
8538
  // Note: I'm not sure what to do here. The original
8181
8539
  // idea was to create TypedArray views into each part
@@ -8210,9 +8568,12 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
8210
8568
  name: blockName,
8211
8569
  array,
8212
8570
  asFloat: new Float32Array(array), // for debugging
8571
+ asUint8: new Uint8Array(array), // needed for gl.bufferSubData because it doesn't take an array buffer
8213
8572
  buffer,
8214
8573
  uniforms,
8215
8574
  setters,
8575
+ offset: options.bufferOffset ?? offset,
8576
+ size: blockSpec.size,
8216
8577
  };
8217
8578
  }
8218
8579
 
@@ -8229,11 +8590,12 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
8229
8590
  * @param {module:twgl.ProgramInfo} programInfo a `ProgramInfo`
8230
8591
  * as returned from {@link module:twgl.createProgramInfo}
8231
8592
  * @param {string} blockName The name of the block.
8593
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
8232
8594
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
8233
8595
  * @memberOf module:twgl/programs
8234
8596
  */
8235
- function createUniformBlockInfo(gl, programInfo, blockName) {
8236
- return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName);
8597
+ function createUniformBlockInfo(gl, programInfo, blockName, options = {}) {
8598
+ return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName, options);
8237
8599
  }
8238
8600
 
8239
8601
  /**
@@ -8259,7 +8621,7 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
8259
8621
  const blockSpec = uniformBlockSpec.blockSpecs[uniformBlockInfo.name];
8260
8622
  if (blockSpec) {
8261
8623
  const bufferBindIndex = blockSpec.index;
8262
- gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, uniformBlockInfo.array.byteLength);
8624
+ gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, uniformBlockInfo.size ?? uniformBlockInfo.array.byteLength);
8263
8625
  return true;
8264
8626
  }
8265
8627
  return false;
@@ -8282,7 +8644,7 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
8282
8644
  */
8283
8645
  function setUniformBlock(gl, programInfo, uniformBlockInfo) {
8284
8646
  if (bindUniformBlock(gl, programInfo, uniformBlockInfo)) {
8285
- gl.bufferData(UNIFORM_BUFFER, uniformBlockInfo.array, DYNAMIC_DRAW);
8647
+ gl.bufferSubData(UNIFORM_BUFFER, 0, uniformBlockInfo.asUint8, uniformBlockInfo.offset || 0, uniformBlockInfo.size || 0);
8286
8648
  }
8287
8649
  }
8288
8650
 
@@ -8727,6 +9089,8 @@ function setBuffersAndAttributes(gl, programInfo, buffers) {
8727
9089
  /**
8728
9090
  * @typedef {Object} ProgramInfo
8729
9091
  * @property {WebGLProgram} program A shader program
9092
+ * @property {Object<string, WebGLUniformLocation>} uniformLocations The uniform locations of each uniform
9093
+ * @property {Object<string, number>} attribLocations The locations of each attribute
8730
9094
  * @property {Object<string, function>} uniformSetters object of setters as returned from createUniformSetters,
8731
9095
  * @property {Object<string, function>} attribSetters object of setters as returned from createAttribSetters,
8732
9096
  * @property {module:twgl.UniformBlockSpec} [uniformBlockSpec] a uniform block spec for making UniformBlockInfos with createUniformBlockInfo etc..
@@ -8758,6 +9122,8 @@ function createProgramInfoFromProgram(gl, program) {
8758
9122
  program,
8759
9123
  uniformSetters,
8760
9124
  attribSetters,
9125
+ uniformLocations: Object.fromEntries(Object.entries(uniformSetters).map(([k, v]) => [k, v.location])),
9126
+ attribLocations: Object.fromEntries(Object.entries(attribSetters).map(([k, v]) => [k, v.location])),
8761
9127
  };
8762
9128
 
8763
9129
  if (isWebGL2(gl)) {
@@ -9896,6 +10262,8 @@ const supportedExtensions = [
9896
10262
  'EXT_frag_depth',
9897
10263
  'EXT_sRGB',
9898
10264
  'EXT_shader_texture_lod',
10265
+ 'EXT_texture_compression_bptc',
10266
+ 'EXT_texture_compression_rgtc',
9899
10267
  'EXT_texture_filter_anisotropic',
9900
10268
  'OES_element_index_uint',
9901
10269
  'OES_standard_derivatives',
@@ -9907,6 +10275,7 @@ const supportedExtensions = [
9907
10275
  'WEBGL_color_buffer_float',
9908
10276
  'WEBGL_compressed_texture_atc',
9909
10277
  'WEBGL_compressed_texture_etc1',
10278
+ 'WEBGL_compressed_texture_etc',
9910
10279
  'WEBGL_compressed_texture_pvrtc',
9911
10280
  'WEBGL_compressed_texture_s3tc',
9912
10281
  'WEBGL_compressed_texture_s3tc_srgb',
@@ -10082,4 +10451,4 @@ function resizeCanvasToDisplaySize(canvas, multiplier) {
10082
10451
  return false;
10083
10452
  }
10084
10453
 
10085
- export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramAsync, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoAsync, createProgramInfoFromProgram, createProgramInfos, createProgramInfosAsync, createPrograms, createProgramsAsync, createSampler, createSamplers, createTexture, createTextures, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray$1 as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents$1 as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer$1 as isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults$2 as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };
10454
+ export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramAsync, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoAsync, createProgramInfoFromProgram, createProgramInfos, createProgramInfosAsync, createPrograms, createProgramsAsync, createSampler, createSamplers, createTexture, createTextureAsync, createTextures, createTexturesAsync, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray$1 as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents$1 as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer$1 as isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults$2 as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };