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,5 +1,5 @@
1
1
  /*!
2
- * @license twgl.js 5.5.4 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 6.2.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
3
3
  * Available via the MIT license.
4
4
  * see: http://github.com/greggman/twgl.js for details
5
5
  */
@@ -2433,7 +2433,7 @@ function deleteProgramAndShaders(gl, program, notThese) {
2433
2433
  try {
2434
2434
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
2435
2435
  var shader = _step.value;
2436
- if (notThese.has(shader)) {
2436
+ if (!notThese.has(shader)) {
2437
2437
  gl.deleteShader(shader);
2438
2438
  }
2439
2439
  }
@@ -2468,8 +2468,8 @@ function createProgramNoCheck(gl, shaders, programOptions) {
2468
2468
  shader = gl.createShader(type);
2469
2469
  gl.shaderSource(shader, prepShaderSource(src).shaderSource);
2470
2470
  gl.compileShader(shader);
2471
- gl.attachShader(program, shader);
2472
2471
  }
2472
+ gl.attachShader(program, shader);
2473
2473
  }
2474
2474
  Object.entries(attribLocations).forEach(function (_ref) {
2475
2475
  var _ref2 = _slicedToArray(_ref, 2),
@@ -3089,6 +3089,7 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
3089
3089
  * @property {ArrayBuffer} array The array buffer that contains the uniform values
3090
3090
  * @property {Float32Array} asFloat A float view on the array buffer. This is useful
3091
3091
  * inspecting the contents of the buffer in the debugger.
3092
+ * @property {Uint8Array} asUint8t A uint8 view on the array buffer.
3092
3093
  * @property {WebGLBuffer} buffer A WebGL buffer that will hold a copy of the uniform values for rendering.
3093
3094
  * @property {number} [offset] offset into buffer
3094
3095
  * @property {Object<string, ArrayBufferView>} uniforms A uniform name to ArrayBufferView map.
@@ -3107,6 +3108,15 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
3107
3108
  * @memberOf module:twgl
3108
3109
  */
3109
3110
 
3111
+ /**
3112
+ * Options to allow createUniformBlockInfo to use an existing buffer and arrayBuffer at an offset
3113
+ * @typedef {Object} UniformBlockInfoOptions
3114
+ * @property {ArrayBuffer} [array] an existing array buffer to use for values
3115
+ * @property {number} [offset] the offset in bytes to use in the array buffer (default = 0)
3116
+ * @property {WebGLBuffer} [buffer] the buffer to use for this uniform block info
3117
+ * @property {number} [bufferOffset] the offset in bytes in the buffer to use (default = use offset above)
3118
+ */
3119
+
3110
3120
  /**
3111
3121
  * Creates a `UniformBlockInfo` for the specified block
3112
3122
  *
@@ -3121,10 +3131,13 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
3121
3131
  * @param {module:twgl.UniformBlockSpec} uniformBlockSpec. A UniformBlockSpec as returned
3122
3132
  * from {@link module:twgl.createUniformBlockSpecFromProgram}.
3123
3133
  * @param {string} blockName The name of the block.
3134
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
3124
3135
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
3125
3136
  * @memberOf module:twgl/programs
3126
3137
  */
3127
3138
  function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockName) {
3139
+ var _options$offset, _options$array, _options$buffer, _options$bufferOffset;
3140
+ var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
3128
3141
  var blockSpecs = uniformBlockSpec.blockSpecs;
3129
3142
  var uniformData = uniformBlockSpec.uniformData;
3130
3143
  var blockSpec = blockSpecs[blockName];
@@ -3135,10 +3148,14 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3135
3148
  uniforms: {}
3136
3149
  };
3137
3150
  }
3138
- var array = new ArrayBuffer(blockSpec.size);
3139
- var buffer = gl.createBuffer();
3151
+ var offset = (_options$offset = options.offset) !== null && _options$offset !== void 0 ? _options$offset : 0;
3152
+ var array = (_options$array = options.array) !== null && _options$array !== void 0 ? _options$array : new ArrayBuffer(blockSpec.size);
3153
+ var buffer = (_options$buffer = options.buffer) !== null && _options$buffer !== void 0 ? _options$buffer : gl.createBuffer();
3140
3154
  var uniformBufferIndex = blockSpec.index;
3141
3155
  gl.bindBuffer(UNIFORM_BUFFER, buffer);
3156
+ if (!options.buffer) {
3157
+ gl.bufferData(UNIFORM_BUFFER, array.byteLength, DYNAMIC_DRAW);
3158
+ }
3142
3159
  gl.uniformBlockBinding(program, blockSpec.index, uniformBufferIndex);
3143
3160
  var prefix = blockName + ".";
3144
3161
  if (arraySuffixRE.test(prefix)) {
@@ -3160,7 +3177,7 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3160
3177
  var typeInfo = typeMap[data.type];
3161
3178
  var Type = typeInfo.Type;
3162
3179
  var byteLength = isArray ? pad(typeInfo.size, 16) * data.size : typeInfo.size * data.size;
3163
- var uniformView = new Type(array, data.offset, byteLength / Type.BYTES_PER_ELEMENT);
3180
+ var uniformView = new Type(array, offset + data.offset, byteLength / Type.BYTES_PER_ELEMENT);
3164
3181
  uniforms[name] = uniformView;
3165
3182
  // Note: I'm not sure what to do here. The original
3166
3183
  // idea was to create TypedArray views into each part
@@ -3196,9 +3213,13 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3196
3213
  array: array,
3197
3214
  asFloat: new Float32Array(array),
3198
3215
  // for debugging
3216
+ asUint8: new Uint8Array(array),
3217
+ // needed for gl.bufferSubData because it doesn't take an array buffer
3199
3218
  buffer: buffer,
3200
3219
  uniforms: uniforms,
3201
- setters: setters
3220
+ setters: setters,
3221
+ offset: (_options$bufferOffset = options.bufferOffset) !== null && _options$bufferOffset !== void 0 ? _options$bufferOffset : offset,
3222
+ size: blockSpec.size
3202
3223
  };
3203
3224
  }
3204
3225
 
@@ -3215,11 +3236,13 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3215
3236
  * @param {module:twgl.ProgramInfo} programInfo a `ProgramInfo`
3216
3237
  * as returned from {@link module:twgl.createProgramInfo}
3217
3238
  * @param {string} blockName The name of the block.
3239
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
3218
3240
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
3219
3241
  * @memberOf module:twgl/programs
3220
3242
  */
3221
3243
  function createUniformBlockInfo(gl, programInfo, blockName) {
3222
- return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName);
3244
+ var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3245
+ return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName, options);
3223
3246
  }
3224
3247
 
3225
3248
  /**
@@ -3244,8 +3267,9 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
3244
3267
  var uniformBlockSpec = programInfo.uniformBlockSpec || programInfo;
3245
3268
  var blockSpec = uniformBlockSpec.blockSpecs[uniformBlockInfo.name];
3246
3269
  if (blockSpec) {
3270
+ var _uniformBlockInfo$siz;
3247
3271
  var bufferBindIndex = blockSpec.index;
3248
- gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, uniformBlockInfo.array.byteLength);
3272
+ gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, (_uniformBlockInfo$siz = uniformBlockInfo.size) !== null && _uniformBlockInfo$siz !== void 0 ? _uniformBlockInfo$siz : uniformBlockInfo.array.byteLength);
3249
3273
  return true;
3250
3274
  }
3251
3275
  return false;
@@ -3268,7 +3292,7 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
3268
3292
  */
3269
3293
  function setUniformBlock(gl, programInfo, uniformBlockInfo) {
3270
3294
  if (bindUniformBlock(gl, programInfo, uniformBlockInfo)) {
3271
- gl.bufferData(UNIFORM_BUFFER, uniformBlockInfo.array, DYNAMIC_DRAW);
3295
+ gl.bufferSubData(UNIFORM_BUFFER, 0, uniformBlockInfo.asUint8, uniformBlockInfo.offset || 0, uniformBlockInfo.size || 0);
3272
3296
  }
3273
3297
  }
3274
3298
 
@@ -3710,6 +3734,8 @@ function setBuffersAndAttributes(gl, programInfo, buffers) {
3710
3734
  /**
3711
3735
  * @typedef {Object} ProgramInfo
3712
3736
  * @property {WebGLProgram} program A shader program
3737
+ * @property {Object<string, WebGLUniformLocation>} uniformLocations The uniform locations of each uniform
3738
+ * @property {Object<string, number>} attribLocations The locations of each attribute
3713
3739
  * @property {Object<string, function>} uniformSetters object of setters as returned from createUniformSetters,
3714
3740
  * @property {Object<string, function>} attribSetters object of setters as returned from createAttribSetters,
3715
3741
  * @property {module:twgl.UniformBlockSpec} [uniformBlockSpec] a uniform block spec for making UniformBlockInfos with createUniformBlockInfo etc..
@@ -3740,7 +3766,19 @@ function createProgramInfoFromProgram(gl, program) {
3740
3766
  var programInfo = {
3741
3767
  program: program,
3742
3768
  uniformSetters: uniformSetters,
3743
- attribSetters: attribSetters
3769
+ attribSetters: attribSetters,
3770
+ uniformLocations: Object.fromEntries(Object.entries(uniformSetters).map(function (_ref3) {
3771
+ var _ref4 = _slicedToArray(_ref3, 2),
3772
+ k = _ref4[0],
3773
+ v = _ref4[1];
3774
+ return [k, v.location];
3775
+ })),
3776
+ attribLocations: Object.fromEntries(Object.entries(attribSetters).map(function (_ref5) {
3777
+ var _ref6 = _slicedToArray(_ref5, 2),
3778
+ k = _ref6[0],
3779
+ v = _ref6[1];
3780
+ return [k, v.location];
3781
+ }))
3744
3782
  };
3745
3783
  if (utils.isWebGL2(gl)) {
3746
3784
  programInfo.uniformBlockSpec = createUniformBlockSpecFromProgram(gl, program);
@@ -3883,10 +3921,10 @@ function createPrograms(gl, programSpecs) {
3883
3921
  var noDeleteShadersSet = new Set();
3884
3922
 
3885
3923
  // compile and link everything
3886
- var programs = Object.fromEntries(Object.entries(programSpecs).map(function (_ref3) {
3887
- var _ref4 = _slicedToArray(_ref3, 2),
3888
- name = _ref4[0],
3889
- spec = _ref4[1];
3924
+ var programs = Object.fromEntries(Object.entries(programSpecs).map(function (_ref7) {
3925
+ var _ref8 = _slicedToArray(_ref7, 2),
3926
+ name = _ref8[0],
3927
+ spec = _ref8[1];
3890
3928
  var options = _objectSpread({}, programOptions);
3891
3929
  var shaders = Array.isArray(spec) ? spec : spec.shaders;
3892
3930
  if (!Array.isArray(spec)) {
@@ -3946,10 +3984,10 @@ function createPrograms(gl, programSpecs) {
3946
3984
  function createProgramInfos(gl, programSpecs, programOptions) {
3947
3985
  programOptions = getProgramOptions(programOptions);
3948
3986
  function createProgramInfosForPrograms(gl, programs) {
3949
- return Object.fromEntries(Object.entries(programs).map(function (_ref5) {
3950
- var _ref6 = _slicedToArray(_ref5, 2),
3951
- name = _ref6[0],
3952
- program = _ref6[1];
3987
+ return Object.fromEntries(Object.entries(programs).map(function (_ref9) {
3988
+ var _ref10 = _slicedToArray(_ref9, 2),
3989
+ name = _ref10[0],
3990
+ program = _ref10[1];
3953
3991
  return [name, createProgramInfoFromProgram(gl, program)];
3954
3992
  }));
3955
3993
  }
@@ -4024,14 +4062,15 @@ var createProgramInfosAsync = exports.createProgramInfosAsync = wrapCallbackFnTo
4024
4062
 
4025
4063
 
4026
4064
 
4027
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4028
4065
  exports.__esModule = true;
4029
4066
  exports.canFilter = canFilter;
4030
4067
  exports.canGenerateMipmap = canGenerateMipmap;
4031
4068
  exports.createSampler = createSampler;
4032
4069
  exports.createSamplers = createSamplers;
4033
4070
  exports.createTexture = createTexture;
4071
+ exports.createTextureAsync = createTextureAsync;
4034
4072
  exports.createTextures = createTextures;
4073
+ exports.createTexturesAsync = createTexturesAsync;
4035
4074
  exports.getBytesPerElementForInternalFormat = getBytesPerElementForInternalFormat;
4036
4075
  exports.getFormatAndTypeForInternalFormat = getFormatAndTypeForInternalFormat;
4037
4076
  exports.getNumComponentsForFormat = getNumComponentsForFormat;
@@ -4050,7 +4089,12 @@ var typedArrays = _interopRequireWildcard(__webpack_require__(/*! ./typedarrays.
4050
4089
  var helper = _interopRequireWildcard(__webpack_require__(/*! ./helper.js */ "./src/helper.js"));
4051
4090
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
4052
4091
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
4053
- /*
4092
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4093
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
4094
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4095
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4096
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
4097
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*
4054
4098
  * Copyright 2019 Gregg Tavares
4055
4099
  *
4056
4100
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -4071,7 +4115,6 @@ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e;
4071
4115
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
4072
4116
  * DEALINGS IN THE SOFTWARE.
4073
4117
  */
4074
-
4075
4118
  /**
4076
4119
  * Low level texture related functions
4077
4120
  *
@@ -4257,6 +4300,74 @@ var RED = 0x1903;
4257
4300
  var RED_INTEGER = 0x8D94;
4258
4301
  var RGB_INTEGER = 0x8D98;
4259
4302
  var RGBA_INTEGER = 0x8D99;
4303
+
4304
+ /* Compressed Texture Formats */
4305
+ // s3tc
4306
+ var COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0;
4307
+ var COMPRESSED_RGBA_S3TC_DXT1_EXT = 0x83F1;
4308
+ var COMPRESSED_RGBA_S3TC_DXT3_EXT = 0x83F2;
4309
+ var COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
4310
+ // s3tc_srgb
4311
+ var COMPRESSED_SRGB_S3TC_DXT1_EXT = 0x8C4C;
4312
+ var COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT = 0x8C4D;
4313
+ var COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT = 0x8C4E;
4314
+ var COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT = 0x8C4F;
4315
+ // etc
4316
+ var COMPRESSED_RGB_ETC1_WEBGL = 0x8D64;
4317
+ var COMPRESSED_R11_EAC = 0x9270;
4318
+ var COMPRESSED_SIGNED_R11_EAC = 0x9271;
4319
+ var COMPRESSED_RG11_EAC = 0x9272;
4320
+ var COMPRESSED_SIGNED_RG11_EAC = 0x9273;
4321
+ var COMPRESSED_RGB8_ETC2 = 0x9274;
4322
+ var COMPRESSED_SRGB8_ETC2 = 0x9275;
4323
+ var COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9276;
4324
+ var COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 = 0x9277;
4325
+ var COMPRESSED_RGBA8_ETC2_EAC = 0x9278;
4326
+ var COMPRESSED_SRGB8_ALPHA8_ETC2_EAC = 0x9279;
4327
+ // pvrtc
4328
+ var COMPRESSED_RGB_PVRTC_4BPPV1_IMG = 0x8C00;
4329
+ var COMPRESSED_RGB_PVRTC_2BPPV1_IMG = 0x8C01;
4330
+ var COMPRESSED_RGBA_PVRTC_4BPPV1_IMG = 0x8C02;
4331
+ var COMPRESSED_RGBA_PVRTC_2BPPV1_IMG = 0x8C03;
4332
+ // astc
4333
+ var COMPRESSED_RGBA_ASTC_4x4_KHR = 0x93B0;
4334
+ var COMPRESSED_RGBA_ASTC_5x4_KHR = 0x93B1;
4335
+ var COMPRESSED_RGBA_ASTC_5x5_KHR = 0x93B2;
4336
+ var COMPRESSED_RGBA_ASTC_6x5_KHR = 0x93B3;
4337
+ var COMPRESSED_RGBA_ASTC_6x6_KHR = 0x93B4;
4338
+ var COMPRESSED_RGBA_ASTC_8x5_KHR = 0x93B5;
4339
+ var COMPRESSED_RGBA_ASTC_8x6_KHR = 0x93B6;
4340
+ var COMPRESSED_RGBA_ASTC_8x8_KHR = 0x93B7;
4341
+ var COMPRESSED_RGBA_ASTC_10x5_KHR = 0x93B8;
4342
+ var COMPRESSED_RGBA_ASTC_10x6_KHR = 0x93B9;
4343
+ var COMPRESSED_RGBA_ASTC_10x8_KHR = 0x93BA;
4344
+ var COMPRESSED_RGBA_ASTC_10x10_KHR = 0x93BB;
4345
+ var COMPRESSED_RGBA_ASTC_12x10_KHR = 0x93BC;
4346
+ var COMPRESSED_RGBA_ASTC_12x12_KHR = 0x93BD;
4347
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR = 0x93D0;
4348
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR = 0x93D1;
4349
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR = 0x93D2;
4350
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR = 0x93D3;
4351
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR = 0x93D4;
4352
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR = 0x93D5;
4353
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR = 0x93D6;
4354
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR = 0x93D7;
4355
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR = 0x93D8;
4356
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR = 0x93D9;
4357
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR = 0x93DA;
4358
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR = 0x93DB;
4359
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR = 0x93DC;
4360
+ var COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR = 0x93DD;
4361
+ // bptc
4362
+ var COMPRESSED_RGBA_BPTC_UNORM_EXT = 0x8E8C;
4363
+ var COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT = 0x8E8D;
4364
+ var COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT = 0x8E8E;
4365
+ var COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT = 0x8E8F;
4366
+ // rgtc
4367
+ var COMPRESSED_RED_RGTC1_EXT = 0x8DBB;
4368
+ var COMPRESSED_SIGNED_RED_RGTC1_EXT = 0x8DBC;
4369
+ var COMPRESSED_RED_GREEN_RGTC2_EXT = 0x8DBD;
4370
+ var COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT = 0x8DBE;
4260
4371
  var formatInfo = {};
4261
4372
  {
4262
4373
  // NOTE: this is named `numColorComponents` vs `numComponents` so we can let Uglify mangle
@@ -4315,7 +4426,8 @@ var formatInfo = {};
4315
4426
  * @property {boolean} colorRenderable true if you can render to this format of texture.
4316
4427
  * @property {boolean} textureFilterable true if you can filter the texture, false if you can ony use `NEAREST`.
4317
4428
  * @property {number[]} type Array of possible types you can pass to texImage2D and similar function
4318
- * @property {Object.<number,number>} bytesPerElementMap A map of types to bytes per element
4429
+ * @property {Object.<number,number>} [bytesPerElementMap] A map of types to bytes per element
4430
+ * @property {boolean} [compressed] true if it's a compressed texture format
4319
4431
  * @private
4320
4432
  */
4321
4433
 
@@ -4756,6 +4868,318 @@ function getTextureInternalFormatInfo(internalFormat) {
4756
4868
  info.bytesPerElementMap[type] = bytesPerElement;
4757
4869
  });
4758
4870
  });
4871
+ var getCompressedFormatInfo = function getCompressedFormatInfo() {
4872
+ var t = {};
4873
+ // compressed texture formats
4874
+ // s3tc:https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_compression_s3tc.txt
4875
+ t[COMPRESSED_RGB_S3TC_DXT1_EXT] = {
4876
+ textureFormat: COMPRESSED_RGB_S3TC_DXT1_EXT,
4877
+ colorRenderable: false,
4878
+ textureFilterable: true
4879
+ };
4880
+ t[COMPRESSED_RGBA_S3TC_DXT1_EXT] = {
4881
+ textureFormat: COMPRESSED_RGBA_S3TC_DXT1_EXT,
4882
+ colorRenderable: false,
4883
+ textureFilterable: true
4884
+ };
4885
+ t[COMPRESSED_RGBA_S3TC_DXT3_EXT] = {
4886
+ textureFormat: COMPRESSED_RGBA_S3TC_DXT3_EXT,
4887
+ colorRenderable: false,
4888
+ textureFilterable: true
4889
+ };
4890
+ t[COMPRESSED_RGBA_S3TC_DXT5_EXT] = {
4891
+ textureFormat: COMPRESSED_RGBA_S3TC_DXT5_EXT,
4892
+ colorRenderable: false,
4893
+ textureFilterable: true
4894
+ };
4895
+ // https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_compression_s3tc_srgb.txt
4896
+ t[COMPRESSED_SRGB_S3TC_DXT1_EXT] = {
4897
+ textureFormat: COMPRESSED_SRGB_S3TC_DXT1_EXT,
4898
+ colorRenderable: false,
4899
+ textureFilterable: true
4900
+ };
4901
+ t[COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT] = {
4902
+ textureFormat: COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT,
4903
+ colorRenderable: false,
4904
+ textureFilterable: true
4905
+ };
4906
+ t[COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT] = {
4907
+ textureFormat: COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT,
4908
+ colorRenderable: false,
4909
+ textureFilterable: true
4910
+ };
4911
+ t[COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT] = {
4912
+ textureFormat: COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT,
4913
+ colorRenderable: false,
4914
+ textureFilterable: true
4915
+ };
4916
+ // https://registry.khronos.org/OpenGL/extensions/OES/OES_compressed_ETC1_RGB8_texture.txt
4917
+ t[COMPRESSED_RGB_ETC1_WEBGL] = {
4918
+ textureFormat: COMPRESSED_RGB_ETC1_WEBGL,
4919
+ colorRenderable: false,
4920
+ textureFilterable: true
4921
+ };
4922
+ t[COMPRESSED_R11_EAC] = {
4923
+ textureFormat: COMPRESSED_R11_EAC,
4924
+ colorRenderable: false,
4925
+ textureFilterable: true
4926
+ };
4927
+ t[COMPRESSED_SIGNED_R11_EAC] = {
4928
+ textureFormat: COMPRESSED_SIGNED_R11_EAC,
4929
+ colorRenderable: false,
4930
+ textureFilterable: true
4931
+ };
4932
+ t[COMPRESSED_RG11_EAC] = {
4933
+ textureFormat: COMPRESSED_RG11_EAC,
4934
+ colorRenderable: false,
4935
+ textureFilterable: true
4936
+ };
4937
+ t[COMPRESSED_SIGNED_RG11_EAC] = {
4938
+ textureFormat: COMPRESSED_SIGNED_RG11_EAC,
4939
+ colorRenderable: false,
4940
+ textureFilterable: true
4941
+ };
4942
+ t[COMPRESSED_RGB8_ETC2] = {
4943
+ textureFormat: COMPRESSED_RGB8_ETC2,
4944
+ colorRenderable: false,
4945
+ textureFilterable: true
4946
+ };
4947
+ t[COMPRESSED_SRGB8_ETC2] = {
4948
+ textureFormat: COMPRESSED_SRGB8_ETC2,
4949
+ colorRenderable: false,
4950
+ textureFilterable: true
4951
+ };
4952
+ t[COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2] = {
4953
+ textureFormat: COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,
4954
+ colorRenderable: false,
4955
+ textureFilterable: true
4956
+ };
4957
+ t[COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2] = {
4958
+ textureFormat: COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2,
4959
+ colorRenderable: false,
4960
+ textureFilterable: true
4961
+ };
4962
+ t[COMPRESSED_RGBA8_ETC2_EAC] = {
4963
+ textureFormat: COMPRESSED_RGBA8_ETC2_EAC,
4964
+ colorRenderable: false,
4965
+ textureFilterable: true
4966
+ };
4967
+ t[COMPRESSED_SRGB8_ALPHA8_ETC2_EAC] = {
4968
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,
4969
+ colorRenderable: false,
4970
+ textureFilterable: true
4971
+ };
4972
+ // https://registry.khronos.org/OpenGL/extensions/IMG/IMG_texture_compression_pvrtc.txt
4973
+ t[COMPRESSED_RGB_PVRTC_4BPPV1_IMG] = {
4974
+ textureFormat: COMPRESSED_RGB_PVRTC_4BPPV1_IMG,
4975
+ colorRenderable: false,
4976
+ textureFilterable: true
4977
+ };
4978
+ t[COMPRESSED_RGB_PVRTC_2BPPV1_IMG] = {
4979
+ textureFormat: COMPRESSED_RGB_PVRTC_2BPPV1_IMG,
4980
+ colorRenderable: false,
4981
+ textureFilterable: true
4982
+ };
4983
+ t[COMPRESSED_RGBA_PVRTC_4BPPV1_IMG] = {
4984
+ textureFormat: COMPRESSED_RGBA_PVRTC_4BPPV1_IMG,
4985
+ colorRenderable: false,
4986
+ textureFilterable: true
4987
+ };
4988
+ t[COMPRESSED_RGBA_PVRTC_2BPPV1_IMG] = {
4989
+ textureFormat: COMPRESSED_RGBA_PVRTC_2BPPV1_IMG,
4990
+ colorRenderable: false,
4991
+ textureFilterable: true
4992
+ };
4993
+ // https://registry.khronos.org/OpenGL/extensions/KHR/KHR_texture_compression_astc_hdr.txt
4994
+ t[COMPRESSED_RGBA_ASTC_4x4_KHR] = {
4995
+ textureFormat: COMPRESSED_RGBA_ASTC_4x4_KHR,
4996
+ colorRenderable: false,
4997
+ textureFilterable: true
4998
+ };
4999
+ t[COMPRESSED_RGBA_ASTC_5x4_KHR] = {
5000
+ textureFormat: COMPRESSED_RGBA_ASTC_5x4_KHR,
5001
+ colorRenderable: false,
5002
+ textureFilterable: true
5003
+ };
5004
+ t[COMPRESSED_RGBA_ASTC_5x5_KHR] = {
5005
+ textureFormat: COMPRESSED_RGBA_ASTC_5x5_KHR,
5006
+ colorRenderable: false,
5007
+ textureFilterable: true
5008
+ };
5009
+ t[COMPRESSED_RGBA_ASTC_6x5_KHR] = {
5010
+ textureFormat: COMPRESSED_RGBA_ASTC_6x5_KHR,
5011
+ colorRenderable: false,
5012
+ textureFilterable: true
5013
+ };
5014
+ t[COMPRESSED_RGBA_ASTC_6x6_KHR] = {
5015
+ textureFormat: COMPRESSED_RGBA_ASTC_6x6_KHR,
5016
+ colorRenderable: false,
5017
+ textureFilterable: true
5018
+ };
5019
+ t[COMPRESSED_RGBA_ASTC_8x5_KHR] = {
5020
+ textureFormat: COMPRESSED_RGBA_ASTC_8x5_KHR,
5021
+ colorRenderable: false,
5022
+ textureFilterable: true
5023
+ };
5024
+ t[COMPRESSED_RGBA_ASTC_8x6_KHR] = {
5025
+ textureFormat: COMPRESSED_RGBA_ASTC_8x6_KHR,
5026
+ colorRenderable: false,
5027
+ textureFilterable: true
5028
+ };
5029
+ t[COMPRESSED_RGBA_ASTC_8x8_KHR] = {
5030
+ textureFormat: COMPRESSED_RGBA_ASTC_8x8_KHR,
5031
+ colorRenderable: false,
5032
+ textureFilterable: true
5033
+ };
5034
+ t[COMPRESSED_RGBA_ASTC_10x5_KHR] = {
5035
+ textureFormat: COMPRESSED_RGBA_ASTC_10x5_KHR,
5036
+ colorRenderable: false,
5037
+ textureFilterable: true
5038
+ };
5039
+ t[COMPRESSED_RGBA_ASTC_10x6_KHR] = {
5040
+ textureFormat: COMPRESSED_RGBA_ASTC_10x6_KHR,
5041
+ colorRenderable: false,
5042
+ textureFilterable: true
5043
+ };
5044
+ t[COMPRESSED_RGBA_ASTC_10x8_KHR] = {
5045
+ textureFormat: COMPRESSED_RGBA_ASTC_10x8_KHR,
5046
+ colorRenderable: false,
5047
+ textureFilterable: true
5048
+ };
5049
+ t[COMPRESSED_RGBA_ASTC_10x10_KHR] = {
5050
+ textureFormat: COMPRESSED_RGBA_ASTC_10x10_KHR,
5051
+ colorRenderable: false,
5052
+ textureFilterable: true
5053
+ };
5054
+ t[COMPRESSED_RGBA_ASTC_12x10_KHR] = {
5055
+ textureFormat: COMPRESSED_RGBA_ASTC_12x10_KHR,
5056
+ colorRenderable: false,
5057
+ textureFilterable: true
5058
+ };
5059
+ t[COMPRESSED_RGBA_ASTC_12x12_KHR] = {
5060
+ textureFormat: COMPRESSED_RGBA_ASTC_12x12_KHR,
5061
+ colorRenderable: false,
5062
+ textureFilterable: true
5063
+ };
5064
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR] = {
5065
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,
5066
+ colorRenderable: false,
5067
+ textureFilterable: true
5068
+ };
5069
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR] = {
5070
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,
5071
+ colorRenderable: false,
5072
+ textureFilterable: true
5073
+ };
5074
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR] = {
5075
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,
5076
+ colorRenderable: false,
5077
+ textureFilterable: true
5078
+ };
5079
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR] = {
5080
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,
5081
+ colorRenderable: false,
5082
+ textureFilterable: true
5083
+ };
5084
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR] = {
5085
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,
5086
+ colorRenderable: false,
5087
+ textureFilterable: true
5088
+ };
5089
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR] = {
5090
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,
5091
+ colorRenderable: false,
5092
+ textureFilterable: true
5093
+ };
5094
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR] = {
5095
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,
5096
+ colorRenderable: false,
5097
+ textureFilterable: true
5098
+ };
5099
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR] = {
5100
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,
5101
+ colorRenderable: false,
5102
+ textureFilterable: true
5103
+ };
5104
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR] = {
5105
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,
5106
+ colorRenderable: false,
5107
+ textureFilterable: true
5108
+ };
5109
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR] = {
5110
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,
5111
+ colorRenderable: false,
5112
+ textureFilterable: true
5113
+ };
5114
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR] = {
5115
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,
5116
+ colorRenderable: false,
5117
+ textureFilterable: true
5118
+ };
5119
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR] = {
5120
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR,
5121
+ colorRenderable: false,
5122
+ textureFilterable: true
5123
+ };
5124
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR] = {
5125
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR,
5126
+ colorRenderable: false,
5127
+ textureFilterable: true
5128
+ };
5129
+ t[COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR] = {
5130
+ textureFormat: COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR,
5131
+ colorRenderable: false,
5132
+ textureFilterable: true
5133
+ };
5134
+ // https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_compression_bptc.txt
5135
+ t[COMPRESSED_RGBA_BPTC_UNORM_EXT] = {
5136
+ textureFormat: COMPRESSED_RGBA_BPTC_UNORM_EXT,
5137
+ colorRenderable: false,
5138
+ textureFilterable: true
5139
+ };
5140
+ t[COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT] = {
5141
+ textureFormat: COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT,
5142
+ colorRenderable: false,
5143
+ textureFilterable: true
5144
+ };
5145
+ t[COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT] = {
5146
+ textureFormat: COMPRESSED_RGB_BPTC_SIGNED_FLOAT_EXT,
5147
+ colorRenderable: false,
5148
+ textureFilterable: true
5149
+ };
5150
+ t[COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT] = {
5151
+ textureFormat: COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_EXT,
5152
+ colorRenderable: false,
5153
+ textureFilterable: true
5154
+ };
5155
+ // https://registry.khronos.org/OpenGL/extensions/EXT/EXT_texture_compression_rgtc.txt
5156
+ t[COMPRESSED_RED_RGTC1_EXT] = {
5157
+ textureFormat: COMPRESSED_RED_RGTC1_EXT,
5158
+ colorRenderable: false,
5159
+ textureFilterable: true
5160
+ };
5161
+ t[COMPRESSED_SIGNED_RED_RGTC1_EXT] = {
5162
+ textureFormat: COMPRESSED_SIGNED_RED_RGTC1_EXT,
5163
+ colorRenderable: false,
5164
+ textureFilterable: true
5165
+ };
5166
+ t[COMPRESSED_RED_GREEN_RGTC2_EXT] = {
5167
+ textureFormat: COMPRESSED_RED_GREEN_RGTC2_EXT,
5168
+ colorRenderable: false,
5169
+ textureFilterable: true
5170
+ };
5171
+ t[COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT] = {
5172
+ textureFormat: COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT,
5173
+ colorRenderable: false,
5174
+ textureFilterable: true
5175
+ };
5176
+ for (var _i = 0, _Object$values = Object.values(t); _i < _Object$values.length; _i++) {
5177
+ var info = _Object$values[_i];
5178
+ info.compressed = true;
5179
+ }
5180
+ return t;
5181
+ };
5182
+ Object.assign(t, getCompressedFormatInfo());
4759
5183
  s_textureInternalFormatInfo = t;
4760
5184
  }
4761
5185
  return s_textureInternalFormatInfo[internalFormat];
@@ -4798,16 +5222,30 @@ function getBytesPerElementForInternalFormat(internalFormat, type) {
4798
5222
  * @memberOf module:twgl/textures
4799
5223
  */
4800
5224
  function getFormatAndTypeForInternalFormat(internalFormat) {
5225
+ var _info$type$, _info$type;
4801
5226
  var info = getTextureInternalFormatInfo(internalFormat);
4802
5227
  if (!info) {
4803
5228
  throw "unknown internal format";
4804
5229
  }
4805
5230
  return {
4806
5231
  format: info.textureFormat,
4807
- type: info.type[0]
5232
+ type: (_info$type$ = (_info$type = info.type) === null || _info$type === void 0 ? void 0 : _info$type[0]) !== null && _info$type$ !== void 0 ? _info$type$ : UNSIGNED_BYTE
4808
5233
  };
4809
5234
  }
4810
5235
 
5236
+ /**
5237
+ * @param {number} internalFormat The internal format
5238
+ * @returns if the internalFormat is a compressed format
5239
+ * @private
5240
+ */
5241
+ function isCompressedInternalFormat(internalFormat) {
5242
+ var info = getTextureInternalFormatInfo(internalFormat);
5243
+ if (!info) {
5244
+ throw "unknown internal format";
5245
+ }
5246
+ return !!info.compressed;
5247
+ }
5248
+
4811
5249
  /**
4812
5250
  * Returns true if value is power of 2
4813
5251
  * @param {number} value number to check.
@@ -5038,21 +5476,62 @@ function setDefaults(newDefaults) {
5038
5476
  */
5039
5477
 
5040
5478
  /**
5041
- * Sets any packing state that will be set based on the options.
5042
- * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
5479
+ * Saves the current packing state, sets the packing state as specified
5480
+ * then calls a function, after which the packing state will be restored.
5043
5481
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5482
+ * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
5483
+ * @param {function():void} [fn] A function to call, after which the packing state will be restored.
5044
5484
  * @private
5045
5485
  */
5046
- function setPackState(gl, options) {
5486
+ function scopedSetPackState(gl, options, fn) {
5487
+ var colorspaceConversion;
5488
+ var premultiplyAlpha;
5489
+ var flipY;
5047
5490
  if (options.colorspaceConversion !== undefined) {
5491
+ colorspaceConversion = gl.getParameter(UNPACK_COLORSPACE_CONVERSION_WEBGL);
5048
5492
  gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, options.colorspaceConversion);
5049
5493
  }
5050
5494
  if (options.premultiplyAlpha !== undefined) {
5495
+ premultiplyAlpha = gl.getParameter(UNPACK_PREMULTIPLY_ALPHA_WEBGL);
5051
5496
  gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, options.premultiplyAlpha);
5052
5497
  }
5053
5498
  if (options.flipY !== undefined) {
5499
+ flipY = gl.getParameter(UNPACK_FLIP_Y_WEBGL);
5054
5500
  gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, options.flipY);
5055
5501
  }
5502
+ fn();
5503
+ if (colorspaceConversion !== undefined) {
5504
+ gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, colorspaceConversion);
5505
+ }
5506
+ if (premultiplyAlpha !== undefined) {
5507
+ gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
5508
+ }
5509
+ if (flipY !== undefined) {
5510
+ gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, flipY);
5511
+ }
5512
+ }
5513
+
5514
+ /**
5515
+ * returns the property if set or the corresponding state if undefined
5516
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5517
+ * @param {module:twgl.TextureOptions} options
5518
+ * @param {string} property the name of the property to copy
5519
+ * @param {number} pname
5520
+ * @return {module:twgl.TextureOptions}
5521
+ */
5522
+ function getPackStateOption(gl, options, property, pname) {
5523
+ var v = options[property];
5524
+ return _defineProperty({}, property, v === undefined ? gl.getParameter(pname) : v);
5525
+ }
5526
+
5527
+ /**
5528
+ * Copy the options object and apply pack state
5529
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5530
+ * @param {module:twgl.TextureOptions} options
5531
+ * @return {module:twgl.TextureOptions}
5532
+ */
5533
+ function copyOptionsAndApplyPackState(gl, options) {
5534
+ return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, options), getPackStateOption(gl, options, 'flipY', UNPACK_FLIP_Y_WEBGL)), getPackStateOption(gl, options, 'premultiplyAlpha', UNPACK_PREMULTIPLY_ALPHA_WEBGL)), getPackStateOption(gl, options, 'colorspaceConversion', UNPACK_COLORSPACE_CONVERSION_WEBGL));
5056
5535
  }
5057
5536
 
5058
5537
  /**
@@ -5336,7 +5815,6 @@ function setTextureFromElement(gl, tex, element, options) {
5336
5815
  var formatType = getFormatAndTypeForInternalFormat(internalFormat);
5337
5816
  var format = options.format || formatType.format;
5338
5817
  var type = options.type || formatType.type;
5339
- setPackState(gl, options);
5340
5818
  gl.bindTexture(target, tex);
5341
5819
  if (target === TEXTURE_CUBE_MAP) {
5342
5820
  // guess the parts
@@ -5369,15 +5847,17 @@ function setTextureFromElement(gl, tex, element, options) {
5369
5847
  ctx.canvas.height = size;
5370
5848
  width = size;
5371
5849
  height = size;
5372
- getCubeFacesWithNdx(gl, options).forEach(function (f) {
5373
- var xOffset = slices[f.ndx * 2 + 0] * size;
5374
- var yOffset = slices[f.ndx * 2 + 1] * size;
5375
- ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
5376
- gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
5850
+ scopedSetPackState(gl, options, function () {
5851
+ getCubeFacesWithNdx(gl, options).forEach(function (f) {
5852
+ var xOffset = slices[f.ndx * 2 + 0] * size;
5853
+ var yOffset = slices[f.ndx * 2 + 1] * size;
5854
+ ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
5855
+ gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
5856
+ });
5857
+ // Free up the canvas memory
5858
+ ctx.canvas.width = 1;
5859
+ ctx.canvas.height = 1;
5377
5860
  });
5378
- // Free up the canvas memory
5379
- ctx.canvas.width = 1;
5380
- ctx.canvas.height = 1;
5381
5861
  } else if (typeof createImageBitmap !== 'undefined') {
5382
5862
  // NOTE: It seems like we should prefer ImageBitmap because unlike canvas it's
5383
5863
  // note lossy? (alpha is not premultiplied? although I'm not sure what
@@ -5396,39 +5876,44 @@ function setTextureFromElement(gl, tex, element, options) {
5396
5876
  premultiplyAlpha: 'none',
5397
5877
  colorSpaceConversion: 'none'
5398
5878
  }).then(function (imageBitmap) {
5399
- setPackState(gl, options);
5400
- gl.bindTexture(target, tex);
5401
- gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
5402
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5403
- setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
5404
- }
5879
+ scopedSetPackState(gl, options, function () {
5880
+ gl.bindTexture(target, tex);
5881
+ gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
5882
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5883
+ setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
5884
+ }
5885
+ });
5405
5886
  });
5406
5887
  });
5407
5888
  }
5408
5889
  } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
5409
- var smallest = Math.min(element.width, element.height);
5410
- var largest = Math.max(element.width, element.height);
5411
- var depth = largest / smallest;
5412
- if (depth % 1 !== 0) {
5413
- throw "can not compute 3D dimensions of element";
5414
- }
5415
- var xMult = element.width === largest ? 1 : 0;
5416
- var yMult = element.height === largest ? 1 : 0;
5417
- gl.pixelStorei(UNPACK_ALIGNMENT, 1);
5418
- gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
5419
- gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
5420
- gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
5421
- gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
5422
- for (var d = 0; d < depth; ++d) {
5423
- var srcX = d * smallest * xMult;
5424
- var srcY = d * smallest * yMult;
5425
- gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
5426
- gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
5427
- gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
5428
- }
5429
- setSkipStateToDefault(gl);
5890
+ scopedSetPackState(gl, options, function () {
5891
+ var smallest = Math.min(element.width, element.height);
5892
+ var largest = Math.max(element.width, element.height);
5893
+ var depth = largest / smallest;
5894
+ if (depth % 1 !== 0) {
5895
+ throw "can not compute 3D dimensions of element";
5896
+ }
5897
+ var xMult = element.width === largest ? 1 : 0;
5898
+ var yMult = element.height === largest ? 1 : 0;
5899
+ gl.pixelStorei(UNPACK_ALIGNMENT, 1);
5900
+ gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
5901
+ gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
5902
+ gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
5903
+ gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
5904
+ for (var d = 0; d < depth; ++d) {
5905
+ var srcX = d * smallest * xMult;
5906
+ var srcY = d * smallest * yMult;
5907
+ gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
5908
+ gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
5909
+ gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
5910
+ }
5911
+ setSkipStateToDefault(gl);
5912
+ });
5430
5913
  } else {
5431
- gl.texImage2D(target, level, internalFormat, format, type, element);
5914
+ scopedSetPackState(gl, options, function () {
5915
+ gl.texImage2D(target, level, internalFormat, format, type, element);
5916
+ });
5432
5917
  }
5433
5918
  if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5434
5919
  setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
@@ -5662,7 +6147,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
5662
6147
  options = options || defaults.textureOptions;
5663
6148
  setTextureTo1PixelColor(gl, tex, options);
5664
6149
  // Because it's async we need to copy the options.
5665
- options = Object.assign({}, options);
6150
+ options = copyOptionsAndApplyPackState(gl, options);
5666
6151
  var img = loadAndUseImage(options.src, options.crossOrigin, function (err, img) {
5667
6152
  if (err) {
5668
6153
  callback(err, tex, img);
@@ -5702,7 +6187,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
5702
6187
  }
5703
6188
  setTextureTo1PixelColor(gl, tex, options);
5704
6189
  // Because it's async we need to copy the options.
5705
- options = Object.assign({}, options);
6190
+ options = copyOptionsAndApplyPackState(gl, options);
5706
6191
  var numToLoad = 6;
5707
6192
  var errors = [];
5708
6193
  var faces = getCubeFaceOrder(gl, options);
@@ -5717,23 +6202,24 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
5717
6202
  if (img.width !== img.height) {
5718
6203
  errors.push("cubemap face img is not a square: " + img.src);
5719
6204
  } else {
5720
- setPackState(gl, options);
5721
- gl.bindTexture(target, tex);
5722
-
5723
- // So assuming this is the first image we now have one face that's img sized
5724
- // and 5 faces that are 1x1 pixel so size the other faces
5725
- if (numToLoad === 5) {
5726
- // use the default order
5727
- getCubeFaceOrder(gl).forEach(function (otherTarget) {
5728
- // Should we re-use the same face or a color?
5729
- gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
5730
- });
5731
- } else {
5732
- gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
5733
- }
5734
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5735
- gl.generateMipmap(target);
5736
- }
6205
+ scopedSetPackState(gl, options, function () {
6206
+ gl.bindTexture(target, tex);
6207
+
6208
+ // So assuming this is the first image we now have one face that's img sized
6209
+ // and 5 faces that are 1x1 pixel so size the other faces
6210
+ if (numToLoad === 5) {
6211
+ // use the default order
6212
+ getCubeFaceOrder(gl).forEach(function (otherTarget) {
6213
+ // Should we re-use the same face or a color?
6214
+ gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
6215
+ });
6216
+ } else {
6217
+ gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
6218
+ }
6219
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6220
+ gl.generateMipmap(target);
6221
+ }
6222
+ });
5737
6223
  }
5738
6224
  }
5739
6225
  if (numToLoad === 0) {
@@ -5779,7 +6265,7 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
5779
6265
  }
5780
6266
  setTextureTo1PixelColor(gl, tex, options);
5781
6267
  // Because it's async we need to copy the options.
5782
- options = Object.assign({}, options);
6268
+ options = copyOptionsAndApplyPackState(gl, options);
5783
6269
  var numToLoad = urls.length;
5784
6270
  var errors = [];
5785
6271
  var imgs; // eslint-disable-line
@@ -5794,40 +6280,41 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
5794
6280
  if (err) {
5795
6281
  errors.push(err);
5796
6282
  } else {
5797
- setPackState(gl, options);
5798
- gl.bindTexture(target, tex);
5799
- if (firstImage) {
5800
- firstImage = false;
5801
- width = options.width || img.width;
5802
- height = options.height || img.height;
5803
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
5804
-
5805
- // put it in every slice otherwise some slices will be 0,0,0,0
5806
- for (var s = 0; s < depth; ++s) {
5807
- gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
5808
- }
5809
- } else {
5810
- var src = img;
5811
- var ctx;
5812
- if (img.width !== width || img.height !== height) {
5813
- // Size the image to fix
5814
- ctx = getShared2DContext();
5815
- src = ctx.canvas;
5816
- ctx.canvas.width = width;
5817
- ctx.canvas.height = height;
5818
- ctx.drawImage(img, 0, 0, width, height);
5819
- }
5820
- gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
6283
+ scopedSetPackState(gl, options, function () {
6284
+ gl.bindTexture(target, tex);
6285
+ if (firstImage) {
6286
+ firstImage = false;
6287
+ width = options.width || img.width;
6288
+ height = options.height || img.height;
6289
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
6290
+
6291
+ // put it in every slice otherwise some slices will be 0,0,0,0
6292
+ for (var s = 0; s < depth; ++s) {
6293
+ gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
6294
+ }
6295
+ } else {
6296
+ var src = img;
6297
+ var ctx;
6298
+ if (img.width !== width || img.height !== height) {
6299
+ // Size the image to fix
6300
+ ctx = getShared2DContext();
6301
+ src = ctx.canvas;
6302
+ ctx.canvas.width = width;
6303
+ ctx.canvas.height = height;
6304
+ ctx.drawImage(img, 0, 0, width, height);
6305
+ }
6306
+ gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
5821
6307
 
5822
- // free the canvas memory
5823
- if (ctx && src === ctx.canvas) {
5824
- ctx.canvas.width = 0;
5825
- ctx.canvas.height = 0;
6308
+ // free the canvas memory
6309
+ if (ctx && src === ctx.canvas) {
6310
+ ctx.canvas.width = 0;
6311
+ ctx.canvas.height = 0;
6312
+ }
5826
6313
  }
5827
- }
5828
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5829
- gl.generateMipmap(target);
5830
- }
6314
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6315
+ gl.generateMipmap(target);
6316
+ }
6317
+ });
5831
6318
  }
5832
6319
  if (numToLoad === 0) {
5833
6320
  callback(errors.length ? errors : undefined, tex, imgs);
@@ -5853,9 +6340,6 @@ function setTextureFromArray(gl, tex, src, options) {
5853
6340
  options = options || defaults.textureOptions;
5854
6341
  var target = options.target || TEXTURE_2D;
5855
6342
  gl.bindTexture(target, tex);
5856
- var width = options.width;
5857
- var height = options.height;
5858
- var depth = options.depth;
5859
6343
  var level = options.level || 0;
5860
6344
  var internalFormat = options.internalFormat || options.format || RGBA;
5861
6345
  var formatType = getFormatAndTypeForInternalFormat(internalFormat);
@@ -5867,60 +6351,90 @@ function setTextureFromArray(gl, tex, src, options) {
5867
6351
  } else if (src instanceof Uint8ClampedArray) {
5868
6352
  src = new Uint8Array(src.buffer);
5869
6353
  }
5870
- var bytesPerElement = getBytesPerElementForInternalFormat(internalFormat, type);
5871
- var numElements = src.byteLength / bytesPerElement; // TODO: check UNPACK_ALIGNMENT?
5872
- if (numElements % 1) {
5873
- throw "length wrong size for format: " + utils.glEnumToString(gl, format);
5874
- }
5875
- var dimensions;
5876
- if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
5877
- if (!width && !height && !depth) {
5878
- var size = Math.cbrt(numElements);
5879
- if (size % 1 !== 0) {
5880
- throw "can't guess cube size of array of numElements: " + numElements;
6354
+ var _getTextureSize = getTextureSize(gl, target, src, options, internalFormat, format, type),
6355
+ width = _getTextureSize.width,
6356
+ height = _getTextureSize.height,
6357
+ depth = _getTextureSize.depth;
6358
+ var compressed = isCompressedInternalFormat(internalFormat);
6359
+ setSkipStateToDefault(gl);
6360
+ gl.pixelStorei(UNPACK_ALIGNMENT, options.unpackAlignment || 1);
6361
+ scopedSetPackState(gl, options, function () {
6362
+ if (target === TEXTURE_CUBE_MAP) {
6363
+ var faceSize = src.length / 6;
6364
+ getCubeFacesWithNdx(gl, options).forEach(function (f) {
6365
+ var offset = faceSize * f.ndx;
6366
+ var data = src.subarray(offset, offset + faceSize);
6367
+ if (compressed) {
6368
+ gl.compressedTexImage2D(f.face, level, internalFormat, width, height, 0, data);
6369
+ } else {
6370
+ gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
6371
+ }
6372
+ });
6373
+ } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
6374
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
6375
+ } else {
6376
+ if (compressed) {
6377
+ gl.compressedTexImage2D(target, level, internalFormat, width, height, 0, src);
6378
+ } else {
6379
+ gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
6380
+ }
6381
+ }
6382
+ });
6383
+ return {
6384
+ width: width,
6385
+ height: height,
6386
+ depth: depth,
6387
+ type: type
6388
+ };
6389
+ }
6390
+ function getTextureSize(gl, target, src, options, internalFormat, format, type) {
6391
+ var width = options.width;
6392
+ var height = options.height;
6393
+ var depth = options.depth;
6394
+ var compressed = isCompressedInternalFormat(internalFormat);
6395
+ if (compressed) {
6396
+ if (!width || !height) {
6397
+ throw "compressed texture needs to set width and height!";
6398
+ }
6399
+ } else {
6400
+ var bytesPerElement = getBytesPerElementForInternalFormat(internalFormat, type);
6401
+ var numElements = src.byteLength / bytesPerElement; // TODO: check UNPACK_ALIGNMENT?
6402
+ if (numElements % 1) {
6403
+ throw "length wrong size for format: " + utils.glEnumToString(gl, format);
6404
+ }
6405
+ var dimensions;
6406
+ if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
6407
+ if (!width && !height && !depth) {
6408
+ var size = Math.cbrt(numElements);
6409
+ if (size % 1 !== 0) {
6410
+ throw "can't guess cube size of array of numElements: " + numElements;
6411
+ }
6412
+ width = size;
6413
+ height = size;
6414
+ depth = size;
6415
+ } else if (width && (!height || !depth)) {
6416
+ dimensions = guessDimensions(gl, target, height, depth, numElements / width);
6417
+ height = dimensions.width;
6418
+ depth = dimensions.height;
6419
+ } else if (height && (!width || !depth)) {
6420
+ dimensions = guessDimensions(gl, target, width, depth, numElements / height);
6421
+ width = dimensions.width;
6422
+ depth = dimensions.height;
6423
+ } else {
6424
+ dimensions = guessDimensions(gl, target, width, height, numElements / depth);
6425
+ width = dimensions.width;
6426
+ height = dimensions.height;
5881
6427
  }
5882
- width = size;
5883
- height = size;
5884
- depth = size;
5885
- } else if (width && (!height || !depth)) {
5886
- dimensions = guessDimensions(gl, target, height, depth, numElements / width);
5887
- height = dimensions.width;
5888
- depth = dimensions.height;
5889
- } else if (height && (!width || !depth)) {
5890
- dimensions = guessDimensions(gl, target, width, depth, numElements / height);
5891
- width = dimensions.width;
5892
- depth = dimensions.height;
5893
6428
  } else {
5894
- dimensions = guessDimensions(gl, target, width, height, numElements / depth);
6429
+ dimensions = guessDimensions(gl, target, width, height, numElements);
5895
6430
  width = dimensions.width;
5896
6431
  height = dimensions.height;
5897
6432
  }
5898
- } else {
5899
- dimensions = guessDimensions(gl, target, width, height, numElements);
5900
- width = dimensions.width;
5901
- height = dimensions.height;
5902
- }
5903
- setSkipStateToDefault(gl);
5904
- gl.pixelStorei(UNPACK_ALIGNMENT, options.unpackAlignment || 1);
5905
- setPackState(gl, options);
5906
- if (target === TEXTURE_CUBE_MAP) {
5907
- var elementsPerElement = bytesPerElement / src.BYTES_PER_ELEMENT;
5908
- var faceSize = numElements / 6 * elementsPerElement;
5909
- getCubeFacesWithNdx(gl, options).forEach(function (f) {
5910
- var offset = faceSize * f.ndx;
5911
- var data = src.subarray(offset, offset + faceSize);
5912
- gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
5913
- });
5914
- } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
5915
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
5916
- } else {
5917
- gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
5918
6433
  }
5919
6434
  return {
5920
6435
  width: width,
5921
6436
  height: height,
5922
- depth: depth,
5923
- type: type
6437
+ depth: depth
5924
6438
  };
5925
6439
  }
5926
6440
 
@@ -5940,24 +6454,46 @@ function setEmptyTexture(gl, tex, options) {
5940
6454
  var formatType = getFormatAndTypeForInternalFormat(internalFormat);
5941
6455
  var format = options.format || formatType.format;
5942
6456
  var type = options.type || formatType.type;
5943
- setPackState(gl, options);
5944
- if (target === TEXTURE_CUBE_MAP) {
5945
- for (var ii = 0; ii < 6; ++ii) {
5946
- gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
6457
+ scopedSetPackState(gl, options, function () {
6458
+ if (target === TEXTURE_CUBE_MAP) {
6459
+ for (var ii = 0; ii < 6; ++ii) {
6460
+ gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
6461
+ }
6462
+ } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
6463
+ gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
6464
+ } else {
6465
+ gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
5947
6466
  }
5948
- } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
5949
- gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
5950
- } else {
5951
- gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
5952
- }
6467
+ });
5953
6468
  }
5954
6469
 
5955
6470
  /**
5956
6471
  * Creates a texture based on the options passed in.
5957
6472
  *
6473
+ * See {@link module:twgl.TextureOptions}
6474
+ *
5958
6475
  * Note: may reset UNPACK_ALIGNMENT, UNPACK_ROW_LENGTH, UNPACK_IMAGE_HEIGHT, UNPACK_SKIP_IMAGES
5959
6476
  * UNPACK_SKIP_PIXELS, and UNPACK_SKIP_ROWS
5960
6477
  *
6478
+ * UNPACK_FLIP_Y_WEBGL, UNPACK_PREMULTIPLY_ALPHA_WEBGL, UNPACK_COLORSPACE_CONVERSION_WEBGL
6479
+ * are left as is though you can pass in options for flipY, premultiplyAlpha, and colorspaceConversion
6480
+ * to override them.
6481
+ *
6482
+ * As for the behavior of these settings
6483
+ *
6484
+ * ```js
6485
+ * gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
6486
+ * t1 = twgl.createTexture({src: someImage }); // flipped
6487
+ * t2 = twgl.createTexture({src: someImage, flipY: true }); // flipped
6488
+ * t3 = twgl.createTexture({src: someImage, flipY: false }); // not flipped
6489
+ * t4 = twgl.createTexture({src: someImage }); // flipped
6490
+ * ```
6491
+ *
6492
+ * * t1 is flipped because UNPACK_FLIP_Y_WEBGL is true
6493
+ * * t2 is flipped because it was requested
6494
+ * * t3 is not flipped because it was requested
6495
+ * * t4 is flipped because UNPACK_FLIP_Y_WEBGL has been restored to true
6496
+ *
5961
6497
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5962
6498
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
5963
6499
  * @param {module:twgl.TextureReadyCallback} [callback] A callback called when an image has been downloaded and uploaded to the texture.
@@ -6011,6 +6547,45 @@ function createTexture(gl, options, callback) {
6011
6547
  return tex;
6012
6548
  }
6013
6549
 
6550
+ /**
6551
+ * Value returned by createTextureAsync
6552
+ *
6553
+ * @typedef {Object} CreateTextureInfo
6554
+ * @param {WebGLTexture} texture the texture.
6555
+ * @param {module:twgl.TextureSrc} source image(s) used to as the src for the texture
6556
+ * @memberOf module:twgl
6557
+ */
6558
+
6559
+ /**
6560
+ * Creates a texture based on the options passed in.
6561
+ *
6562
+ * see {@link module:twgl/textures.createTexture}.
6563
+ * The only difference is this function returns a promise
6564
+ * where as the other returns a texture and takes a callback.
6565
+ *
6566
+ * Note: this is here for completeness. It is probably better to use
6567
+ * the non-async version as it returns a usable texture immediately
6568
+ * where as this one you have to wait for it to load.
6569
+ *
6570
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6571
+ * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
6572
+ * @return {Promise<CreateTextureInfo>} The created texture and source.
6573
+ */
6574
+ function createTextureAsync(gl, options) {
6575
+ return new Promise(function (resolve, reject) {
6576
+ createTexture(gl, options, function (err, texture, source) {
6577
+ if (err) {
6578
+ reject(err);
6579
+ } else {
6580
+ resolve({
6581
+ texture: texture,
6582
+ source: source
6583
+ });
6584
+ }
6585
+ });
6586
+ });
6587
+ }
6588
+
6014
6589
  /**
6015
6590
  * Resizes a texture based on the options passed in.
6016
6591
  *
@@ -6183,6 +6758,45 @@ function createTextures(gl, textureOptions, callback) {
6183
6758
  return textures;
6184
6759
  }
6185
6760
 
6761
+ /**
6762
+ * Value returned by createTextureAsync
6763
+ *
6764
+ * @typedef {Object} CreateTexturesInfo
6765
+ * @param {Object.<string, WebGLTexture>} textures the created textures by name. Same as returned by {@link module:twgl.createTextures}.
6766
+ * @param {Object.<string, module:twgl.TextureSrc>} sources the image(s) used for the texture by name.
6767
+ * @memberOf module:twgl
6768
+ */
6769
+
6770
+ /**
6771
+ * Creates textures based on the options passed in.
6772
+ *
6773
+ * see {@link module:twgl/textures.createTextures}.
6774
+ * The only difference is this function returns a promise
6775
+ * where as the other returns a texture and takes a callback.
6776
+ *
6777
+ * Note: this is here for completeness. It is probably better to use
6778
+ * the non-async version as it returns usable textures immediately
6779
+ * where as this one you have to wait for them to load.
6780
+ *
6781
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6782
+ * @param {Object.<string,module:twgl.TextureOptions>} options A object of TextureOptions one per texture.
6783
+ * @return {Promise<CreateTexturesInfo>} The created textures and sources.
6784
+ */
6785
+ function createTexturesAsync(gl, options) {
6786
+ return new Promise(function (resolve, reject) {
6787
+ createTexture(gl, options, function (err, textures, sources) {
6788
+ if (err) {
6789
+ reject(err);
6790
+ } else {
6791
+ resolve({
6792
+ textures: textures,
6793
+ sources: sources
6794
+ });
6795
+ }
6796
+ });
6797
+ });
6798
+ }
6799
+
6186
6800
  /***/ }),
6187
6801
 
6188
6802
  /***/ "./src/twgl-base.js":
@@ -6487,7 +7101,7 @@ function addExtensionToContext(gl, extensionName) {
6487
7101
  * list.
6488
7102
  *
6489
7103
  */
6490
- var supportedExtensions = ['ANGLE_instanced_arrays', 'EXT_blend_minmax', 'EXT_color_buffer_float', 'EXT_color_buffer_half_float', 'EXT_disjoint_timer_query', 'EXT_disjoint_timer_query_webgl2', 'EXT_frag_depth', 'EXT_sRGB', 'EXT_shader_texture_lod', 'EXT_texture_filter_anisotropic', 'OES_element_index_uint', 'OES_standard_derivatives', 'OES_texture_float', 'OES_texture_float_linear', 'OES_texture_half_float', 'OES_texture_half_float_linear', 'OES_vertex_array_object', 'WEBGL_color_buffer_float', 'WEBGL_compressed_texture_atc', 'WEBGL_compressed_texture_etc1', 'WEBGL_compressed_texture_pvrtc', 'WEBGL_compressed_texture_s3tc', 'WEBGL_compressed_texture_s3tc_srgb', 'WEBGL_depth_texture', 'WEBGL_draw_buffers'];
7104
+ var supportedExtensions = ['ANGLE_instanced_arrays', 'EXT_blend_minmax', 'EXT_color_buffer_float', 'EXT_color_buffer_half_float', 'EXT_disjoint_timer_query', 'EXT_disjoint_timer_query_webgl2', 'EXT_frag_depth', 'EXT_sRGB', 'EXT_shader_texture_lod', 'EXT_texture_compression_bptc', 'EXT_texture_compression_rgtc', 'EXT_texture_filter_anisotropic', 'OES_element_index_uint', 'OES_standard_derivatives', 'OES_texture_float', 'OES_texture_float_linear', 'OES_texture_half_float', 'OES_texture_half_float_linear', 'OES_vertex_array_object', 'WEBGL_color_buffer_float', 'WEBGL_compressed_texture_atc', 'WEBGL_compressed_texture_etc1', 'WEBGL_compressed_texture_etc', 'WEBGL_compressed_texture_pvrtc', 'WEBGL_compressed_texture_s3tc', 'WEBGL_compressed_texture_s3tc_srgb', 'WEBGL_depth_texture', 'WEBGL_draw_buffers'];
6491
7105
 
6492
7106
  /**
6493
7107
  * Attempts to enable all of the following extensions