twgl.js 5.1.0 → 5.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.1.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 5.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
  */
@@ -4551,12 +4551,16 @@ exports.bindTransformFeedbackInfo = bindTransformFeedbackInfo;
4551
4551
  exports.bindUniformBlock = bindUniformBlock;
4552
4552
  exports.createAttributeSetters = createAttributeSetters;
4553
4553
  exports.createProgram = createProgram;
4554
- exports.createProgramAsync = createProgramAsync;
4554
+ exports.createProgramAsync = void 0;
4555
4555
  exports.createProgramFromScripts = createProgramFromScripts;
4556
4556
  exports.createProgramFromSources = createProgramFromSources;
4557
4557
  exports.createProgramInfo = createProgramInfo;
4558
- exports.createProgramInfoAsync = createProgramInfoAsync;
4558
+ exports.createProgramInfoAsync = void 0;
4559
4559
  exports.createProgramInfoFromProgram = createProgramInfoFromProgram;
4560
+ exports.createProgramInfos = createProgramInfos;
4561
+ exports.createProgramInfosAsync = void 0;
4562
+ exports.createPrograms = createPrograms;
4563
+ exports.createProgramsAsync = void 0;
4560
4564
  exports.createTransformFeedback = createTransformFeedback;
4561
4565
  exports.createTransformFeedbackInfo = createTransformFeedbackInfo;
4562
4566
  exports.createUniformBlockInfo = createUniformBlockInfo;
@@ -4574,8 +4578,16 @@ var helper = _interopRequireWildcard(__webpack_require__(/*! ./helper.js */ "./s
4574
4578
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
4575
4579
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
4576
4580
  function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
4581
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4582
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4583
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4577
4584
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4578
4585
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
4586
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
4587
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4588
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
4589
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
4590
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
4579
4591
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
4580
4592
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4581
4593
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
@@ -5369,38 +5381,13 @@ function reportError(progOptions, msg) {
5369
5381
  return null;
5370
5382
  }
5371
5383
 
5372
- /**
5373
- * Loads a shader.
5374
- * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5375
- * @param {string} shaderSource The shader source.
5376
- * @param {number} shaderType The type of shader.
5377
- * @param {module:twgl.ProgramOptions} progOptions
5378
- * @return {WebGLShader} The created shader.
5379
- * @private
5380
- */
5381
- function loadShader(gl, shaderSource, shaderType, progOptions) {
5382
- // Create the shader object
5383
- var shader = gl.createShader(shaderType);
5384
-
5385
- // Load the shader source
5386
- gl.shaderSource(shader, prepShaderSource(shaderSource).shaderSource);
5387
-
5388
- // Compile the shader
5389
- gl.compileShader(shader);
5390
- if (!progOptions.callback && !checkShaderStatus(gl, shaderType, shader, progOptions.errorCallback)) {
5391
- gl.deleteShader(shader);
5392
- return null;
5393
- }
5394
- return shader;
5395
- }
5396
-
5397
5384
  /**
5398
5385
  * Check Shader status
5399
5386
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5400
5387
  * @param {number} shaderType The shader type
5401
5388
  * @param {WebGLShader} shader The shader
5402
5389
  * @param {ErrorCallback} [errFn] function to receive error message.
5403
- * @return {bool} true if shader is ok.
5390
+ * @return {string} errors or empty string
5404
5391
  * @private
5405
5392
  */
5406
5393
  function checkShaderStatus(gl, shaderType, shader, errFn) {
@@ -5413,15 +5400,35 @@ function checkShaderStatus(gl, shaderType, shader, errFn) {
5413
5400
  var _prepShaderSource = prepShaderSource(gl.getShaderSource(shader)),
5414
5401
  lineOffset = _prepShaderSource.lineOffset,
5415
5402
  shaderSource = _prepShaderSource.shaderSource;
5416
- errFn("".concat(addLineNumbersWithError(shaderSource, lastError, lineOffset), "\nError compiling ").concat(utils.glEnumToString(gl, shaderType), ": ").concat(lastError));
5403
+ var _error = "".concat(addLineNumbersWithError(shaderSource, lastError, lineOffset), "\nError compiling ").concat(utils.glEnumToString(gl, shaderType), ": ").concat(lastError);
5404
+ errFn(_error);
5405
+ return _error;
5417
5406
  }
5418
- return compiled;
5407
+ return '';
5419
5408
  }
5420
5409
 
5410
+ /**
5411
+ * @typedef {Object} FullProgramSpec
5412
+ * @property {string[]} shaders the shader source or element ids.
5413
+ * @property {function(string)} [errorCallback] callback for errors
5414
+ * @property {Object.<string,number>|string[]} [attribLocations] a attribute name to location map, or array of attribute names where index = location.
5415
+ * @property {(module:twgl.BufferInfo|Object.<string,module:twgl.AttribInfo>|string[])} [transformFeedbackVaryings] If passed
5416
+ * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
5417
+ * you can pass an array of names.
5418
+ * @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
5419
+ * @property {ProgramCallback} [callback] callback for async program compilation.
5420
+ * @memberOf module:twgl
5421
+ */
5422
+
5423
+ /**
5424
+ * @typedef {string[]|module:twgl.FullProgramSpec} ProgramSpec
5425
+ * @memberOf module:twgl
5426
+ */
5427
+
5421
5428
  /**
5422
5429
  * @typedef {Object} ProgramOptions
5423
5430
  * @property {function(string)} [errorCallback] callback for errors
5424
- * @property {Object.<string,number>} [attribLocations] a attribute name to location map
5431
+ * @property {Object.<string,number>|string[]} [attribLocations] a attribute name to location map, or array of attribute names where index = location.
5425
5432
  * @property {(module:twgl.BufferInfo|Object.<string,module:twgl.AttribInfo>|string[])} [transformFeedbackVaryings] If passed
5426
5433
  * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
5427
5434
  * you can pass an array of names.
@@ -5451,11 +5458,6 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
5451
5458
  opt_errorCallback = opt_attribs;
5452
5459
  opt_attribs = undefined;
5453
5460
  } else if (opt_attribs && !Array.isArray(opt_attribs)) {
5454
- // If we have an errorCallback we can just return this object
5455
- // Otherwise we need to construct one with default errorCallback
5456
- if (opt_attribs.errorCallback && opt_attribs.errors) {
5457
- return opt_attribs;
5458
- }
5459
5461
  var opt = opt_attribs;
5460
5462
  opt_errorCallback = opt.errorCallback;
5461
5463
  opt_attribs = opt.attribLocations;
@@ -5478,14 +5480,14 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
5478
5480
  callback: callback,
5479
5481
  errors: errors
5480
5482
  };
5481
- if (opt_attribs) {
5483
+ {
5482
5484
  var attribLocations = {};
5483
5485
  if (Array.isArray(opt_attribs)) {
5484
5486
  opt_attribs.forEach(function (attrib, ndx) {
5485
5487
  attribLocations[attrib] = opt_locations ? opt_locations[ndx] : ndx;
5486
5488
  });
5487
5489
  } else {
5488
- attribLocations = opt_attribs;
5490
+ attribLocations = opt_attribs || {};
5489
5491
  }
5490
5492
  options.attribLocations = attribLocations;
5491
5493
  }
@@ -5500,10 +5502,23 @@ function getShaderTypeFromScriptType(gl, scriptType) {
5500
5502
  }
5501
5503
  return undefined;
5502
5504
  }
5503
- function deleteShaders(gl, shaders) {
5504
- shaders.forEach(function (shader) {
5505
- gl.deleteShader(shader);
5506
- });
5505
+ function deleteProgramAndShaders(gl, program, notThese) {
5506
+ var shaders = gl.getAttachedShaders(program);
5507
+ var _iterator = _createForOfIteratorHelper(shaders),
5508
+ _step;
5509
+ try {
5510
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
5511
+ var shader = _step.value;
5512
+ if (notThese.has(shader)) {
5513
+ gl.deleteShader(shader);
5514
+ }
5515
+ }
5516
+ } catch (err) {
5517
+ _iterator.e(err);
5518
+ } finally {
5519
+ _iterator.f();
5520
+ }
5521
+ gl.deleteProgram(program);
5507
5522
  }
5508
5523
  var wait = function wait() {
5509
5524
  var ms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
@@ -5511,6 +5526,48 @@ var wait = function wait() {
5511
5526
  return setTimeout(resolve, ms);
5512
5527
  });
5513
5528
  };
5529
+ function createProgramNoCheck(gl, shaders, programOptions) {
5530
+ var program = gl.createProgram();
5531
+ var _getProgramOptions = getProgramOptions(programOptions),
5532
+ attribLocations = _getProgramOptions.attribLocations,
5533
+ transformFeedbackVaryings = _getProgramOptions.transformFeedbackVaryings,
5534
+ transformFeedbackMode = _getProgramOptions.transformFeedbackMode;
5535
+ for (var ndx = 0; ndx < shaders.length; ++ndx) {
5536
+ var shader = shaders[ndx];
5537
+ if (typeof shader === 'string') {
5538
+ var elem = getElementById(shader);
5539
+ var src = elem ? elem.text : shader;
5540
+ var type = gl[defaultShaderType[ndx]];
5541
+ if (elem && elem.type) {
5542
+ type = getShaderTypeFromScriptType(gl, elem.type) || type;
5543
+ }
5544
+ shader = gl.createShader(type);
5545
+ gl.shaderSource(shader, prepShaderSource(src).shaderSource);
5546
+ gl.compileShader(shader);
5547
+ gl.attachShader(program, shader);
5548
+ }
5549
+ }
5550
+ Object.entries(attribLocations).forEach(function (_ref) {
5551
+ var _ref2 = _slicedToArray(_ref, 2),
5552
+ attrib = _ref2[0],
5553
+ loc = _ref2[1];
5554
+ return gl.bindAttribLocation(program, loc, attrib);
5555
+ });
5556
+ {
5557
+ var varyings = transformFeedbackVaryings;
5558
+ if (varyings) {
5559
+ if (varyings.attribs) {
5560
+ varyings = varyings.attribs;
5561
+ }
5562
+ if (!Array.isArray(varyings)) {
5563
+ varyings = Object.keys(varyings);
5564
+ }
5565
+ gl.transformFeedbackVaryings(program, varyings, transformFeedbackMode || SEPARATE_ATTRIBS);
5566
+ }
5567
+ }
5568
+ gl.linkProgram(program);
5569
+ return program;
5570
+ }
5514
5571
 
5515
5572
  /**
5516
5573
  * Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
@@ -5536,59 +5593,47 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
5536
5593
  // This code is really convoluted, because it may or may not be async
5537
5594
  // Maybe it would be better to have a separate function
5538
5595
  var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
5539
- var realShaders = [];
5540
- var newShaders = [];
5541
- for (var ndx = 0; ndx < shaders.length; ++ndx) {
5542
- var shader = shaders[ndx];
5543
- if (typeof shader === 'string') {
5544
- var elem = getElementById(shader);
5545
- var src = elem ? elem.text : shader;
5546
- var type = gl[defaultShaderType[ndx]];
5547
- if (elem && elem.type) {
5548
- type = getShaderTypeFromScriptType(gl, elem.type) || type;
5549
- }
5550
- shader = loadShader(gl, src, type, progOptions);
5551
- newShaders.push(shader);
5552
- }
5553
- if (helper.isShader(gl, shader)) {
5554
- realShaders.push(shader);
5596
+ var shaderSet = new Set(shaders);
5597
+ var program = createProgramNoCheck(gl, shaders, progOptions);
5598
+ function hasErrors(gl, program) {
5599
+ var errors = getProgramErrors(gl, program, progOptions.errorCallback);
5600
+ if (errors) {
5601
+ deleteProgramAndShaders(gl, program, shaderSet);
5555
5602
  }
5603
+ return errors;
5556
5604
  }
5557
- if (realShaders.length !== shaders.length) {
5558
- deleteShaders(gl, newShaders);
5559
- return reportError(progOptions, "not enough shaders for program");
5560
- }
5561
- var program = gl.createProgram();
5562
- realShaders.forEach(function (shader) {
5563
- gl.attachShader(program, shader);
5564
- });
5565
- if (progOptions.attribLocations) {
5566
- Object.keys(progOptions.attribLocations).forEach(function (attrib) {
5567
- gl.bindAttribLocation(program, progOptions.attribLocations[attrib], attrib);
5605
+ if (progOptions.callback) {
5606
+ waitForProgramLinkCompletionAsync(gl, program).then(function () {
5607
+ var errors = hasErrors(gl, program);
5608
+ progOptions.callback(errors, errors ? undefined : program);
5568
5609
  });
5610
+ return undefined;
5569
5611
  }
5570
- var varyings = progOptions.transformFeedbackVaryings;
5571
- if (varyings) {
5572
- if (varyings.attribs) {
5573
- varyings = varyings.attribs;
5574
- }
5575
- if (!Array.isArray(varyings)) {
5576
- varyings = Object.keys(varyings);
5577
- }
5578
- gl.transformFeedbackVaryings(program, varyings, progOptions.transformFeedbackMode || SEPARATE_ATTRIBS);
5579
- }
5580
- gl.linkProgram(program);
5581
- if (progOptions.callback) {
5582
- checkForProgramLinkCompletionAsync(gl, program, progOptions);
5583
- return null;
5584
- } else {
5585
- if (!checkProgramStatus(gl, program, progOptions.errorCallback)) {
5586
- gl.deleteProgram(program);
5587
- deleteShaders(gl, newShaders);
5588
- return null;
5612
+ return hasErrors(gl, program) ? undefined : program;
5613
+ }
5614
+
5615
+ /**
5616
+ * This only works because the functions it wraps the first 2 arguments
5617
+ * are gl and any, followed by things that become programOptions
5618
+ * @private
5619
+ */
5620
+ function wrapCallbackFnToAsyncFn(fn) {
5621
+ return function (gl, arg1) {
5622
+ for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
5623
+ args[_key2 - 2] = arguments[_key2];
5589
5624
  }
5590
- return program;
5591
- }
5625
+ return new Promise(function (resolve, reject) {
5626
+ var programOptions = getProgramOptions.apply(void 0, args);
5627
+ programOptions.callback = function (err, program) {
5628
+ if (err) {
5629
+ reject(err);
5630
+ } else {
5631
+ resolve(program);
5632
+ }
5633
+ };
5634
+ fn(gl, arg1, programOptions);
5635
+ });
5636
+ };
5592
5637
  }
5593
5638
 
5594
5639
  /**
@@ -5601,6 +5646,7 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
5601
5646
  * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
5602
5647
  * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
5603
5648
  *
5649
+ * @function
5604
5650
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5605
5651
  * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
5606
5652
  * @param {module:twgl.ProgramOptions|string[]|module:twgl.ErrorCallback} [opt_attribs] Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
@@ -5610,25 +5656,11 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
5610
5656
  * @return {Promise<WebGLProgram>} The created program
5611
5657
  * @memberOf module:twgl/programs
5612
5658
  */
5613
- function createProgramAsync(gl, shaders) {
5614
- for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
5615
- args[_key2 - 2] = arguments[_key2];
5616
- }
5617
- return new Promise(function (resolve, reject) {
5618
- var programOptions = getProgramOptions.apply(void 0, args);
5619
- programOptions.callback = function (err, program) {
5620
- if (err) {
5621
- reject(err);
5622
- } else {
5623
- resolve(program);
5624
- }
5625
- };
5626
- createProgram(gl, shaders, programOptions);
5627
- });
5628
- }
5659
+ var createProgramAsync = wrapCallbackFnToAsyncFn(createProgram);
5629
5660
 
5630
5661
  /**
5631
5662
  * Same as createProgramInfo but returns a promise
5663
+ * @function
5632
5664
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext
5633
5665
  * to use.
5634
5666
  * @param {string[]} shaderSources Array of sources for the
@@ -5641,45 +5673,15 @@ function createProgramAsync(gl, shaders) {
5641
5673
  * @return {Promise<module:twgl.ProgramInfo>} The created ProgramInfo
5642
5674
  * @memberOf module:twgl/programs
5643
5675
  */
5644
- function createProgramInfoAsync(gl, shaders) {
5645
- for (var _len3 = arguments.length, args = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
5646
- args[_key3 - 2] = arguments[_key3];
5647
- }
5648
- return new Promise(function (resolve, reject) {
5649
- var programOptions = getProgramOptions.apply(void 0, args);
5650
- programOptions.callback = function (err, programInfo) {
5651
- if (err) {
5652
- reject(err);
5653
- } else {
5654
- resolve(programInfo);
5655
- }
5656
- };
5657
- createProgramInfo(gl, shaders, programOptions);
5658
- });
5676
+ exports.createProgramAsync = createProgramAsync;
5677
+ var createProgramInfoAsync = wrapCallbackFnToAsyncFn(createProgramInfo);
5678
+ exports.createProgramInfoAsync = createProgramInfoAsync;
5679
+ function waitForProgramLinkCompletionAsync(_x, _x2) {
5680
+ return _waitForProgramLinkCompletionAsync.apply(this, arguments);
5659
5681
  }
5660
-
5661
- /**
5662
- * Asynchronously wait for program to link.
5663
- * Note: if 'KHR_parallel_shader_compile' extension does not
5664
- * exist then compilation will not be truly async.
5665
- * @param {WebGLRenderingContext} gl The context
5666
- * @param {WebGLProgram} program The program
5667
- * @param {module:twgl.ProgramOptions} progOptions Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
5668
- * @private
5669
- */
5670
- function checkForProgramLinkCompletionAsync(_x, _x2, _x3) {
5671
- return _checkForProgramLinkCompletionAsync.apply(this, arguments);
5672
- } /**
5673
- * Check a program's link status
5674
- * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5675
- * @param {WebGLProgram} program Program to check
5676
- * @param {ErrorCallback} [errFn] func for errors
5677
- * @return {bool} true if program is ok
5678
- * @private
5679
- */
5680
- function _checkForProgramLinkCompletionAsync() {
5681
- _checkForProgramLinkCompletionAsync = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(gl, program, progOptions) {
5682
- var ext, checkFn, waitTime, success, err, errFn;
5682
+ function _waitForProgramLinkCompletionAsync() {
5683
+ _waitForProgramLinkCompletionAsync = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(gl, program) {
5684
+ var ext, checkFn, waitTime;
5683
5685
  return _regeneratorRuntime().wrap(function _callee$(_context) {
5684
5686
  while (1) {
5685
5687
  switch (_context.prev = _context.next) {
@@ -5703,27 +5705,54 @@ function _checkForProgramLinkCompletionAsync() {
5703
5705
  break;
5704
5706
  }
5705
5707
  case 7:
5706
- success = checkProgramStatus(gl, program, progOptions.errorCallback);
5707
- err = success ? undefined : progOptions.errors.join('\n');
5708
- if (!success) {
5709
- errFn = progOptions.errorCallback || error;
5710
- errFn(err);
5711
- gl.deleteProgram(program);
5712
- // TODO: delete shaders, but only shaders that were created newly for this
5713
- // program
5714
- program = null;
5715
- }
5716
- progOptions.callback(err, program);
5717
- case 11:
5718
5708
  case "end":
5719
5709
  return _context.stop();
5720
5710
  }
5721
5711
  }
5722
5712
  }, _callee);
5723
5713
  }));
5724
- return _checkForProgramLinkCompletionAsync.apply(this, arguments);
5714
+ return _waitForProgramLinkCompletionAsync.apply(this, arguments);
5725
5715
  }
5726
- function checkProgramStatus(gl, program, errFn) {
5716
+ function waitForAllProgramsLinkCompletionAsync(_x3, _x4) {
5717
+ return _waitForAllProgramsLinkCompletionAsync.apply(this, arguments);
5718
+ } /**
5719
+ * Check a program's link status
5720
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5721
+ * @param {WebGLProgram} program Program to check
5722
+ * @param {ErrorCallback} [errFn] func for errors
5723
+ * @return {string?} errors if program is failed, else undefined
5724
+ * @private
5725
+ */
5726
+ function _waitForAllProgramsLinkCompletionAsync() {
5727
+ _waitForAllProgramsLinkCompletionAsync = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(gl, programs) {
5728
+ var _i4, _Object$values2, program;
5729
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
5730
+ while (1) {
5731
+ switch (_context2.prev = _context2.next) {
5732
+ case 0:
5733
+ _i4 = 0, _Object$values2 = Object.values(programs);
5734
+ case 1:
5735
+ if (!(_i4 < _Object$values2.length)) {
5736
+ _context2.next = 8;
5737
+ break;
5738
+ }
5739
+ program = _Object$values2[_i4];
5740
+ _context2.next = 5;
5741
+ return waitForProgramLinkCompletionAsync(gl, program);
5742
+ case 5:
5743
+ _i4++;
5744
+ _context2.next = 1;
5745
+ break;
5746
+ case 8:
5747
+ case "end":
5748
+ return _context2.stop();
5749
+ }
5750
+ }
5751
+ }, _callee2);
5752
+ }));
5753
+ return _waitForAllProgramsLinkCompletionAsync.apply(this, arguments);
5754
+ }
5755
+ function getProgramErrors(gl, program, errFn) {
5727
5756
  errFn = errFn || error;
5728
5757
  // Check the link status
5729
5758
  var linked = gl.getProgramParameter(program, LINK_STATUS);
@@ -5731,32 +5760,16 @@ function checkProgramStatus(gl, program, errFn) {
5731
5760
  // something went wrong with the link
5732
5761
  var lastError = gl.getProgramInfoLog(program);
5733
5762
  errFn("Error in program linking: ".concat(lastError));
5763
+ // print any errors from these shaders
5764
+ var shaders = gl.getAttachedShaders(program);
5765
+ var errors = shaders.map(function (shader) {
5766
+ return checkShaderStatus(gl, gl.getShaderParameter(shader, gl.SHADER_TYPE), shader, errFn);
5767
+ });
5768
+ return "".concat(lastError, "\n").concat(errors.filter(function (_) {
5769
+ return _;
5770
+ }).join('\n'));
5734
5771
  }
5735
- return linked;
5736
- }
5737
-
5738
- /**
5739
- * Loads a shader from a script tag.
5740
- * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5741
- * @param {string} scriptId The id of the script tag.
5742
- * @param {number} [opt_shaderType] The type of shader. If not passed in it will
5743
- * be derived from the type of the script tag.
5744
- * @param {module:twgl.ProgramOptions} [progOptions] callback for errors.
5745
- * @return {WebGLShader?} The created shader or null if error.
5746
- * @private
5747
- */
5748
- function createShaderFromScript(gl, scriptId, opt_shaderType, progOptions) {
5749
- var shaderSource = "";
5750
- var shaderScript = getElementById(scriptId);
5751
- if (!shaderScript) {
5752
- return reportError(progOptions, "unknown script element: ".concat(scriptId));
5753
- }
5754
- shaderSource = shaderScript.text;
5755
- var shaderType = opt_shaderType || getShaderTypeFromScriptType(gl, shaderScript.type);
5756
- if (!shaderType) {
5757
- return reportError(progOptions, 'unknown shader type');
5758
- }
5759
- return loadShader(gl, shaderSource, shaderType, progOptions);
5772
+ return undefined;
5760
5773
  }
5761
5774
 
5762
5775
  /**
@@ -5784,12 +5797,21 @@ function createShaderFromScript(gl, scriptId, opt_shaderType, progOptions) {
5784
5797
  function createProgramFromScripts(gl, shaderScriptIds, opt_attribs, opt_locations, opt_errorCallback) {
5785
5798
  var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
5786
5799
  var shaders = [];
5787
- for (var ii = 0; ii < shaderScriptIds.length; ++ii) {
5788
- var shader = createShaderFromScript(gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions);
5789
- if (!shader) {
5790
- return null;
5800
+ var _iterator2 = _createForOfIteratorHelper(shaderScriptIds),
5801
+ _step2;
5802
+ try {
5803
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
5804
+ var scriptId = _step2.value;
5805
+ var shaderScript = getElementById(scriptId);
5806
+ if (!shaderScript) {
5807
+ return reportError(progOptions, "unknown script element: ".concat(scriptId));
5808
+ }
5809
+ shaders.push(shaderScript.text);
5791
5810
  }
5792
- shaders.push(shader);
5811
+ } catch (err) {
5812
+ _iterator2.e(err);
5813
+ } finally {
5814
+ _iterator2.f();
5793
5815
  }
5794
5816
  return createProgram(gl, shaders, progOptions);
5795
5817
  }
@@ -5817,16 +5839,7 @@ function createProgramFromScripts(gl, shaderScriptIds, opt_attribs, opt_location
5817
5839
  * @memberOf module:twgl/programs
5818
5840
  */
5819
5841
  function createProgramFromSources(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
5820
- var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
5821
- var shaders = [];
5822
- for (var ii = 0; ii < shaderSources.length; ++ii) {
5823
- var shader = loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions);
5824
- if (!progOptions.callback && !shader) {
5825
- return null;
5826
- }
5827
- shaders.push(shader);
5828
- }
5829
- return createProgram(gl, shaders, progOptions);
5842
+ return createProgram(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback);
5830
5843
  }
5831
5844
 
5832
5845
  /**
@@ -6874,11 +6887,7 @@ function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_er
6874
6887
  var origCallback = progOptions.callback;
6875
6888
  if (origCallback) {
6876
6889
  progOptions.callback = function (err, program) {
6877
- var programInfo;
6878
- if (!err) {
6879
- programInfo = createProgramInfoFromProgram(gl, program);
6880
- }
6881
- origCallback(err, programInfo);
6890
+ origCallback(err, err ? undefined : createProgramInfoFromProgram(gl, program));
6882
6891
  };
6883
6892
  }
6884
6893
  var program = createProgramFromSources(gl, shaderSources, progOptions);
@@ -6887,6 +6896,209 @@ function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_er
6887
6896
  }
6888
6897
  return createProgramInfoFromProgram(gl, program);
6889
6898
  }
6899
+ function checkAllPrograms(gl, programs, programSpecs, noDeleteShadersSet, programOptions) {
6900
+ // check errors for everything.
6901
+ for (var _i2 = 0, _Object$entries = Object.entries(programs); _i2 < _Object$entries.length; _i2++) {
6902
+ var _Object$entries$_i = _slicedToArray(_Object$entries[_i2], 2),
6903
+ name = _Object$entries$_i[0],
6904
+ program = _Object$entries$_i[1];
6905
+ var options = _objectSpread({}, programOptions);
6906
+ var spec = programSpecs[name];
6907
+ if (!Array.isArray(spec)) {
6908
+ Object.assign(options, spec);
6909
+ }
6910
+ var errors = getProgramErrors(gl, program, options.errorCallback);
6911
+ if (errors) {
6912
+ // delete everything we created
6913
+ for (var _i3 = 0, _Object$values = Object.values(programs); _i3 < _Object$values.length; _i3++) {
6914
+ var _program = _Object$values[_i3];
6915
+ var shaders = gl.getAttachedShaders(_program);
6916
+ gl.deleteProgram(_program);
6917
+ var _iterator3 = _createForOfIteratorHelper(shaders),
6918
+ _step3;
6919
+ try {
6920
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
6921
+ var shader = _step3.value;
6922
+ // Don't delete it if we didn't create it.
6923
+ if (!noDeleteShadersSet.has(shader)) {
6924
+ gl.deleteShader(shader);
6925
+ }
6926
+ }
6927
+ } catch (err) {
6928
+ _iterator3.e(err);
6929
+ } finally {
6930
+ _iterator3.f();
6931
+ }
6932
+ }
6933
+ return errors;
6934
+ }
6935
+ }
6936
+ return undefined;
6937
+ }
6938
+
6939
+ /**
6940
+ * Creates multiple programs
6941
+ *
6942
+ * Note: the reason this function exists is because the fastest way to create multiple
6943
+ * programs in WebGL is to create and compile all shaders and link all programs and only
6944
+ * afterwards check if they succeeded. In that way, giving all your shaders
6945
+ *
6946
+ * @see {@link module:twgl.createProgram}
6947
+ *
6948
+ * Example:
6949
+ *
6950
+ * const programs = twgl.createPrograms(gl, {
6951
+ * lambert: [lambertVS, lambertFS],
6952
+ * phong: [phongVS, phoneFS],
6953
+ * particles: {
6954
+ * shaders: [particlesVS, particlesFS],
6955
+ * transformFeedbackVaryings: ['position', 'velocity'],
6956
+ * },
6957
+ * });
6958
+ *
6959
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6960
+ * @param {Object.<string, module:twgl.ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
6961
+ * @param {module:twgl.ProgramOptions} [programOptions] options to apply to all programs
6962
+ * @return {Object.<string, WebGLProgram>?} the created programInfos by name
6963
+ */
6964
+ function createPrograms(gl, programSpecs) {
6965
+ var programOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
6966
+ // Remember existing shaders so that if there is an error we don't delete them
6967
+ var noDeleteShadersSet = new Set();
6968
+
6969
+ // compile and link everything
6970
+ var programs = Object.fromEntries(Object.entries(programSpecs).map(function (_ref3) {
6971
+ var _ref4 = _slicedToArray(_ref3, 2),
6972
+ name = _ref4[0],
6973
+ spec = _ref4[1];
6974
+ var options = _objectSpread({}, programOptions);
6975
+ var shaders = Array.isArray(spec) ? spec : spec.shaders;
6976
+ if (!Array.isArray(spec)) {
6977
+ Object.assign(options, spec);
6978
+ }
6979
+ shaders.forEach(noDeleteShadersSet.add, noDeleteShadersSet);
6980
+ return [name, createProgramNoCheck(gl, shaders, options)];
6981
+ }));
6982
+ if (programOptions.callback) {
6983
+ waitForAllProgramsLinkCompletionAsync(gl, programs).then(function () {
6984
+ var errors = checkAllPrograms(gl, programs, programSpecs, noDeleteShadersSet, programOptions);
6985
+ programOptions.callback(errors, errors ? undefined : programs);
6986
+ });
6987
+ return undefined;
6988
+ }
6989
+ var errors = checkAllPrograms(gl, programs, programSpecs, noDeleteShadersSet, programOptions);
6990
+ return errors ? undefined : programs;
6991
+ }
6992
+
6993
+ /**
6994
+ * Creates multiple programInfos
6995
+ *
6996
+ * Note: the reason this function exists is because the fastest way to create multiple
6997
+ * programs in WebGL is to create and compile all shaders and link all programs and only
6998
+ * afterwards check if they succeeded. In that way, giving all your shaders
6999
+ *
7000
+ * @see {@link module:twgl.createProgramInfo}
7001
+ *
7002
+ * Examples:
7003
+ *
7004
+ * const programInfos = twgl.createProgramInfos(gl, {
7005
+ * lambert: [lambertVS, lambertFS],
7006
+ * phong: [phongVS, phoneFS],
7007
+ * particles: {
7008
+ * shaders: [particlesVS, particlesFS],
7009
+ * transformFeedbackVaryings: ['position', 'velocity'],
7010
+ * },
7011
+ * });
7012
+ *
7013
+ * or
7014
+ *
7015
+ * const {lambert, phong, particles} = twgl.createProgramInfos(gl, {
7016
+ * lambert: [lambertVS, lambertFS],
7017
+ * phong: [phongVS, phoneFS],
7018
+ * particles: {
7019
+ * shaders: [particlesVS, particlesFS],
7020
+ * transformFeedbackVaryings: ['position', 'velocity'],
7021
+ * },
7022
+ * });
7023
+ *
7024
+ *
7025
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
7026
+ * @param {Object.<string, module:twgl.ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
7027
+ * @param {module:twgl.ProgramOptions} [programOptions] options to apply to all programs
7028
+ * @return {Object.<string, module:twgl.ProgramInfo>?} the created programInfos by name
7029
+ */
7030
+ function createProgramInfos(gl, programSpecs, programOptions) {
7031
+ programOptions = getProgramOptions(programOptions);
7032
+ function createProgramInfosForPrograms(gl, programs) {
7033
+ return Object.fromEntries(Object.entries(programs).map(function (_ref5) {
7034
+ var _ref6 = _slicedToArray(_ref5, 2),
7035
+ name = _ref6[0],
7036
+ program = _ref6[1];
7037
+ return [name, createProgramInfoFromProgram(gl, program)];
7038
+ }));
7039
+ }
7040
+ var origCallback = programOptions.callback;
7041
+ if (origCallback) {
7042
+ programOptions.callback = function (err, programs) {
7043
+ origCallback(err, err ? undefined : createProgramInfosForPrograms(gl, programs));
7044
+ };
7045
+ }
7046
+ var programs = createPrograms(gl, programSpecs, programOptions);
7047
+ if (origCallback || !programs) {
7048
+ return undefined;
7049
+ }
7050
+ return createProgramInfosForPrograms(gl, programs);
7051
+ }
7052
+
7053
+ /**
7054
+ * Creates multiple programs asynchronously
7055
+ *
7056
+ * @see {@link module:twgl.createProgramAsync}
7057
+ *
7058
+ * Example:
7059
+ *
7060
+ * const programs = await twgl.createProgramsAsync(gl, {
7061
+ * lambert: [lambertVS, lambertFS],
7062
+ * phong: [phongVS, phoneFS],
7063
+ * particles: {
7064
+ * shaders: [particlesVS, particlesFS],
7065
+ * transformFeedbackVaryings: ['position', 'velocity'],
7066
+ * },
7067
+ * });
7068
+ *
7069
+ * @function
7070
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
7071
+ * @param {Object.<string, module:twgl.ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
7072
+ * @param {module:twgl.ProgramOptions} [programOptions] options to apply to all programs
7073
+ * @return {Object.<string, WebGLProgram>?} the created programInfos by name
7074
+ */
7075
+ var createProgramsAsync = wrapCallbackFnToAsyncFn(createPrograms);
7076
+
7077
+ /**
7078
+ * Creates multiple programInfos asynchronously
7079
+ *
7080
+ * @see {@link module:twgl.createProgramInfoAsync}
7081
+ *
7082
+ * Example:
7083
+ *
7084
+ * const programInfos = await twgl.createProgramInfosAsync(gl, {
7085
+ * lambert: [lambertVS, lambertFS],
7086
+ * phong: [phongVS, phoneFS],
7087
+ * particles: {
7088
+ * shaders: [particlesVS, particlesFS],
7089
+ * transformFeedbackVaryings: ['position', 'velocity'],
7090
+ * },
7091
+ * });
7092
+ *
7093
+ * @function
7094
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
7095
+ * @param {Object.<string, module:twgl.ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
7096
+ * @param {module:twgl.ProgramOptions} [programOptions] options to apply to all programs
7097
+ * @return {Promise<Object.<string, module:twgl.ProgramInfo>>} the created programInfos by name
7098
+ */
7099
+ exports.createProgramsAsync = createProgramsAsync;
7100
+ var createProgramInfosAsync = wrapCallbackFnToAsyncFn(createProgramInfos);
7101
+ exports.createProgramInfosAsync = createProgramInfosAsync;
6890
7102
 
6891
7103
  /***/ }),
6892
7104