twgl.js 4.22.0 → 4.23.1

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.
@@ -89,6 +89,7 @@ export type ProgramOptions = {
89
89
  [key: string]: AttribInfo;
90
90
  } | string[];
91
91
  transformFeedbackMode?: number;
92
+ callback?: ProgramCallback;
92
93
  };
93
94
  export type TransformFeedbackInfo = {
94
95
  index: number;
@@ -256,7 +257,11 @@ export function bindFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo?:
256
257
 
257
258
 
258
259
  export function getBindPointForSamplerType(): void;
260
+ export type ProgramCallback = (err?: string, result?: WebGLProgram | ProgramInfo) => void;
261
+ export function prepShaderSource(shaderSource: string): any;
259
262
  export function createProgram(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
263
+ export function createProgramAsync(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<WebGLProgram>;
264
+ export function createProgramInfoAsync(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<ProgramInfo>;
260
265
  export function createProgramFromScripts(gl: WebGLRenderingContext, shaderScriptIds: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
261
266
  export function createProgramFromSources(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
262
267
  export function createUniformSetters(gl: WebGLRenderingContext, program: WebGLProgram): {
@@ -334,10 +339,10 @@ export function createVAOAndSetAttributes(gl: WebGLRenderingContext, setters: {
334
339
  [key: string]: (...params: any[]) => any;
335
340
  }, attribs: {
336
341
  [key: string]: AttribInfo;
337
- }, indices?: WebGLBuffer): void;
342
+ }, indices?: WebGLBuffer): WebGLVertexArrayObject | null;
338
343
  export function createVAOFromBufferInfo(gl: WebGLRenderingContext, programInfo: {
339
344
  [key: string]: (...params: any[]) => any;
340
- } | ProgramInfo, bufferInfo: BufferInfo, indices?: WebGLBuffer): void;
345
+ } | ProgramInfo, bufferInfo: BufferInfo, indices?: WebGLBuffer): WebGLVertexArrayObject | null;
341
346
 
342
347
  declare module v3 {
343
348
  export type Vec3 = number[] | Float32Array;
@@ -367,6 +372,7 @@ declare module m4 {
367
372
  export type Mat4 = number[] | Float32Array;
368
373
  export function setDefaultType(ctor: Function): Function;
369
374
  export function negate(m: m4.Mat4, dst?: m4.Mat4): m4.Mat4;
375
+ export function create(): m4.Mat4;
370
376
  export function copy(m: m4.Mat4, dst?: m4.Mat4): m4.Mat4;
371
377
  export function identity(dst?: m4.Mat4): m4.Mat4;
372
378
  export function transpose(m: m4.Mat4, dst?: m4.Mat4): m4.Mat4;
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @license twgl.js 4.22.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 4.23.1 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
  */
@@ -1593,6 +1593,7 @@ exports.__esModule = true;
1593
1593
  exports.axisRotate = axisRotate;
1594
1594
  exports.axisRotation = axisRotation;
1595
1595
  exports.copy = copy;
1596
+ exports.create = create;
1596
1597
  exports.frustum = frustum;
1597
1598
  exports.getAxis = getAxis;
1598
1599
  exports.getTranslation = getTranslation;
@@ -1723,6 +1724,16 @@ function negate(m, dst) {
1723
1724
  dst[15] = -m[15];
1724
1725
  return dst;
1725
1726
  }
1727
+ /**
1728
+ * Creates a matrix.
1729
+ * @return {module:twgl/m4.Mat4} A new matrix.
1730
+ * @memberOf module:twgl/m4
1731
+ */
1732
+
1733
+
1734
+ function create() {
1735
+ return new MatType(16).fill(0);
1736
+ }
1726
1737
  /**
1727
1738
  * Copies a matrix.
1728
1739
  * @param {module:twgl/m4.Mat4} m The matrix.
@@ -4670,9 +4681,11 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
4670
4681
  exports.__esModule = true;
4671
4682
  exports.createAttributeSetters = createAttributeSetters;
4672
4683
  exports.createProgram = createProgram;
4684
+ exports.createProgramAsync = createProgramAsync;
4673
4685
  exports.createProgramFromScripts = createProgramFromScripts;
4674
4686
  exports.createProgramFromSources = createProgramFromSources;
4675
4687
  exports.createProgramInfo = createProgramInfo;
4688
+ exports.createProgramInfoAsync = createProgramInfoAsync;
4676
4689
  exports.createProgramInfoFromProgram = createProgramInfoFromProgram;
4677
4690
  exports.createUniformSetters = createUniformSetters;
4678
4691
  exports.createUniformBlockSpecFromProgram = createUniformBlockSpecFromProgram;
@@ -4697,6 +4710,10 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
4697
4710
 
4698
4711
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
4699
4712
 
4713
+ 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); } }
4714
+
4715
+ 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); }); }; }
4716
+
4700
4717
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
4701
4718
 
4702
4719
  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."); }
@@ -5493,56 +5510,117 @@ function addLineNumbersWithError(src) {
5493
5510
  * @memberOf module:twgl
5494
5511
  */
5495
5512
 
5513
+ /**
5514
+ * Program Callback
5515
+ * @callback ProgramCallback
5516
+ * @param {string} [err] error message, falsy if no error
5517
+ * @param {WebGLProgram|module:twgl.ProgramInfo} [result] the program or programInfo
5518
+ */
5519
+
5496
5520
 
5497
5521
  var spaceRE = /^[ \t]*\n/;
5522
+ /**
5523
+ * Remove the first end of line because WebGL 2.0 requires
5524
+ * #version 300 es
5525
+ * as the first line. No whitespace allowed before that line
5526
+ * so
5527
+ *
5528
+ * <script>
5529
+ * #version 300 es
5530
+ * </script>
5531
+ *
5532
+ * Has one line before it which is invalid according to GLSL ES 3.00
5533
+ *
5534
+ * @param {string} shaderSource The source of the shader
5535
+ * @returns {{shaderSource: string, lineOffset: number}}
5536
+ */
5537
+
5538
+ function prepShaderSource(shaderSource) {
5539
+ var lineOffset = 0;
5540
+
5541
+ if (spaceRE.test(shaderSource)) {
5542
+ lineOffset = 1;
5543
+ shaderSource = shaderSource.replace(spaceRE, '');
5544
+ }
5545
+
5546
+ return {
5547
+ lineOffset: lineOffset,
5548
+ shaderSource: shaderSource
5549
+ };
5550
+ }
5551
+ /**
5552
+ * @param {module:twgl.ProgramOptions} progOptions
5553
+ * @param {string} msg
5554
+ * @return null
5555
+ * @private
5556
+ */
5557
+
5558
+
5559
+ function reportError(progOptions, msg) {
5560
+ progOptions.errorCallback(msg);
5561
+
5562
+ if (progOptions.callback) {
5563
+ setTimeout(function () {
5564
+ progOptions.callback("".concat(msg, "\n").concat(progOptions.errors.join('\n')));
5565
+ });
5566
+ }
5567
+
5568
+ return null;
5569
+ }
5498
5570
  /**
5499
5571
  * Loads a shader.
5500
5572
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5501
5573
  * @param {string} shaderSource The shader source.
5502
5574
  * @param {number} shaderType The type of shader.
5503
- * @param {module:twgl.ErrorCallback} opt_errorCallback callback for errors.
5575
+ * @param {module:twgl.ProgramOptions} progOptions
5504
5576
  * @return {WebGLShader} The created shader.
5505
5577
  * @private
5506
5578
  */
5507
5579
 
5508
- function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
5509
- var errFn = opt_errorCallback || error; // Create the shader object
5510
5580
 
5511
- var shader = gl.createShader(shaderType); // Remove the first end of line because WebGL 2.0 requires
5512
- // #version 300 es
5513
- // as the first line. No whitespace allowed before that line
5514
- // so
5515
- //
5516
- // <script>
5517
- // #version 300 es
5518
- // </script>
5519
- //
5520
- // Has one line before it which is invalid according to GLSL ES 3.00
5521
- //
5581
+ function loadShader(gl, shaderSource, shaderType, progOptions) {
5582
+ // Create the shader object
5583
+ var shader = gl.createShader(shaderType); // Load the shader source
5522
5584
 
5523
- var lineOffset = 0;
5585
+ gl.shaderSource(shader, prepShaderSource(shaderSource).shaderSource); // Compile the shader
5524
5586
 
5525
- if (spaceRE.test(shaderSource)) {
5526
- lineOffset = 1;
5527
- shaderSource = shaderSource.replace(spaceRE, '');
5528
- } // Load the shader source
5587
+ gl.compileShader(shader);
5529
5588
 
5589
+ if (!progOptions.callback && !checkShaderStatus(gl, shaderType, shader, progOptions.errorCallback)) {
5590
+ gl.deleteShader(shader);
5591
+ return null;
5592
+ }
5530
5593
 
5531
- gl.shaderSource(shader, shaderSource); // Compile the shader
5594
+ return shader;
5595
+ }
5596
+ /**
5597
+ * Check Shader status
5598
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5599
+ * @param {number} shaderType The shader type
5600
+ * @param {WebGLShader} shader The shader
5601
+ * @param {ErrorCallback} [errFn] function to receive error message.
5602
+ * @return {bool} true if shader is ok.
5603
+ * @private
5604
+ */
5532
5605
 
5533
- gl.compileShader(shader); // Check the compile status
5606
+
5607
+ function checkShaderStatus(gl, shaderType, shader, errFn) {
5608
+ errFn = errFn || error; // Check the compile status
5534
5609
 
5535
5610
  var compiled = gl.getShaderParameter(shader, COMPILE_STATUS);
5536
5611
 
5537
5612
  if (!compiled) {
5538
5613
  // Something went wrong during compilation; get the error
5539
5614
  var lastError = gl.getShaderInfoLog(shader);
5615
+
5616
+ var _prepShaderSource = prepShaderSource(gl.getShaderSource(shader)),
5617
+ lineOffset = _prepShaderSource.lineOffset,
5618
+ shaderSource = _prepShaderSource.shaderSource;
5619
+
5540
5620
  errFn("".concat(addLineNumbersWithError(shaderSource, lastError, lineOffset), "\nError compiling ").concat(utils.glEnumToString(gl, shaderType), ": ").concat(lastError));
5541
- gl.deleteShader(shader);
5542
- return null;
5543
5621
  }
5544
5622
 
5545
- return shader;
5623
+ return compiled;
5546
5624
  }
5547
5625
  /**
5548
5626
  * @typedef {Object} ProgramOptions
@@ -5552,6 +5630,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
5552
5630
  * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
5553
5631
  * you can pass an array of names.
5554
5632
  * @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
5633
+ * @property {ProgramCallback} [callback] callback for async program compilation.
5555
5634
  * @memberOf module:twgl
5556
5635
  */
5557
5636
 
@@ -5569,6 +5648,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
5569
5648
  function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
5570
5649
  var transformFeedbackVaryings;
5571
5650
  var transformFeedbackMode;
5651
+ var callback;
5572
5652
 
5573
5653
  if (typeof opt_locations === 'function') {
5574
5654
  opt_errorCallback = opt_locations;
@@ -5581,7 +5661,7 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
5581
5661
  } else if (opt_attribs && !Array.isArray(opt_attribs)) {
5582
5662
  // If we have an errorCallback we can just return this object
5583
5663
  // Otherwise we need to construct one with default errorCallback
5584
- if (opt_attribs.errorCallback) {
5664
+ if (opt_attribs.errorCallback && opt_attribs.errors) {
5585
5665
  return opt_attribs;
5586
5666
  }
5587
5667
 
@@ -5590,12 +5670,26 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
5590
5670
  opt_attribs = opt.attribLocations;
5591
5671
  transformFeedbackVaryings = opt.transformFeedbackVaryings;
5592
5672
  transformFeedbackMode = opt.transformFeedbackMode;
5673
+ callback = opt.callback;
5593
5674
  }
5594
5675
 
5676
+ var _errorCallback = opt_errorCallback || error;
5677
+
5678
+ var errors = [];
5595
5679
  var options = {
5596
- errorCallback: opt_errorCallback || error,
5680
+ errorCallback: function errorCallback(msg) {
5681
+ errors.push(msg);
5682
+
5683
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
5684
+ args[_key - 1] = arguments[_key];
5685
+ }
5686
+
5687
+ _errorCallback.apply(void 0, [msg].concat(args));
5688
+ },
5597
5689
  transformFeedbackVaryings: transformFeedbackVaryings,
5598
- transformFeedbackMode: transformFeedbackMode
5690
+ transformFeedbackMode: transformFeedbackMode,
5691
+ callback: callback,
5692
+ errors: errors
5599
5693
  };
5600
5694
 
5601
5695
  if (opt_attribs) {
@@ -5632,9 +5726,16 @@ function deleteShaders(gl, shaders) {
5632
5726
  gl.deleteShader(shader);
5633
5727
  });
5634
5728
  }
5729
+
5730
+ var wait = function wait() {
5731
+ var ms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
5732
+ return new Promise(function (resolve) {
5733
+ return setTimeout(resolve, ms);
5734
+ });
5735
+ };
5635
5736
  /**
5636
5737
  * Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
5637
- * program and calls useProgram.
5738
+ * program.
5638
5739
  *
5639
5740
  * NOTE: There are 4 signatures for this function
5640
5741
  *
@@ -5649,12 +5750,14 @@ function deleteShaders(gl, shaders) {
5649
5750
  * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
5650
5751
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
5651
5752
  * on error. If you want something else pass an callback. It's passed an error message.
5652
- * @return {WebGLProgram?} the created program or null if error.
5753
+ * @return {WebGLProgram?} the created program or null if error of a callback was provided.
5653
5754
  * @memberOf module:twgl/programs
5654
5755
  */
5655
5756
 
5656
5757
 
5657
5758
  function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallback) {
5759
+ // This code is really convoluted, because it may or may not be async
5760
+ // Maybe it would be better to have a separate function
5658
5761
  var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
5659
5762
  var realShaders = [];
5660
5763
  var newShaders = [];
@@ -5671,7 +5774,7 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
5671
5774
  type = getShaderTypeFromScriptType(gl, elem.type) || type;
5672
5775
  }
5673
5776
 
5674
- shader = loadShader(gl, src, type, progOptions.errorCallback);
5777
+ shader = loadShader(gl, src, type, progOptions);
5675
5778
  newShaders.push(shader);
5676
5779
  }
5677
5780
 
@@ -5681,9 +5784,8 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
5681
5784
  }
5682
5785
 
5683
5786
  if (realShaders.length !== shaders.length) {
5684
- progOptions.errorCallback("not enough shaders for program");
5685
5787
  deleteShaders(gl, newShaders);
5686
- return null;
5788
+ return reportError(progOptions, "not enough shaders for program");
5687
5789
  }
5688
5790
 
5689
5791
  var program = gl.createProgram();
@@ -5711,24 +5813,186 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
5711
5813
  gl.transformFeedbackVaryings(program, varyings, progOptions.transformFeedbackMode || SEPARATE_ATTRIBS);
5712
5814
  }
5713
5815
 
5714
- gl.linkProgram(program); // Check the link status
5816
+ gl.linkProgram(program);
5817
+
5818
+ if (progOptions.callback) {
5819
+ checkForProgramLinkCompletionAsync(gl, program, progOptions);
5820
+ return null;
5821
+ } else {
5822
+ if (!checkProgramStatus(gl, program, progOptions.errorCallback)) {
5823
+ gl.deleteProgram(program);
5824
+ deleteShaders(gl, newShaders);
5825
+ return null;
5826
+ }
5827
+
5828
+ return program;
5829
+ }
5830
+ }
5831
+ /**
5832
+ * Same as createProgram but returns a promise
5833
+ *
5834
+ * NOTE: There are 4 signatures for this function
5835
+ *
5836
+ * twgl.createProgramAsync(gl, [vs, fs], options);
5837
+ * twgl.createProgramAsync(gl, [vs, fs], opt_errFunc);
5838
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
5839
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
5840
+ *
5841
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5842
+ * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
5843
+ * @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
5844
+ * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
5845
+ * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
5846
+ * on error. If you want something else pass an callback. It's passed an error message.
5847
+ * @return {Promise<WebGLProgram>} The created program
5848
+ * @memberOf module:twgl/programs
5849
+ */
5850
+
5851
+
5852
+ function createProgramAsync(gl, shaders) {
5853
+ for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
5854
+ args[_key2 - 2] = arguments[_key2];
5855
+ }
5856
+
5857
+ return new Promise(function (resolve, reject) {
5858
+ var programOptions = getProgramOptions.apply(void 0, args);
5859
+
5860
+ programOptions.callback = function (err, program) {
5861
+ if (err) {
5862
+ reject(err);
5863
+ } else {
5864
+ resolve(program);
5865
+ }
5866
+ };
5867
+
5868
+ createProgram(gl, shaders, programOptions);
5869
+ });
5870
+ }
5871
+ /**
5872
+ * Same as createProgramInfo but returns a promise
5873
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
5874
+ * to use.
5875
+ * @param {string[]} shaderSources Array of sources for the
5876
+ * shaders or ids. The first is assumed to be the vertex shader,
5877
+ * the second the fragment shader.
5878
+ * @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
5879
+ * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
5880
+ * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
5881
+ * on error. If you want something else pass an callback. It's passed an error message.
5882
+ * @return {Promise<module:twgl.ProgramInfo>} The created ProgramInfo
5883
+ * @memberOf module:twgl/programs
5884
+ */
5885
+
5886
+
5887
+ function createProgramInfoAsync(gl, shaders) {
5888
+ for (var _len3 = arguments.length, args = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
5889
+ args[_key3 - 2] = arguments[_key3];
5890
+ }
5891
+
5892
+ return new Promise(function (resolve, reject) {
5893
+ var programOptions = getProgramOptions.apply(void 0, args);
5894
+
5895
+ programOptions.callback = function (err, programInfo) {
5896
+ if (err) {
5897
+ reject(err);
5898
+ } else {
5899
+ resolve(programInfo);
5900
+ }
5901
+ };
5902
+
5903
+ createProgramInfo(gl, shaders, programOptions);
5904
+ });
5905
+ }
5906
+ /**
5907
+ * Asynchronously wait for program to link.
5908
+ * Note: if 'KHR_parallel_shader_compile' extension does not
5909
+ * exist then compilation will not be truly async.
5910
+ * @param {WebGLRenderingContext} gl The context
5911
+ * @param {WebGLProgram} program The program
5912
+ * @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
5913
+ * @private
5914
+ */
5915
+
5916
+
5917
+ function checkForProgramLinkCompletionAsync(_x, _x2, _x3) {
5918
+ return _checkForProgramLinkCompletionAsync.apply(this, arguments);
5919
+ }
5920
+ /**
5921
+ * Check a program's link status
5922
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
5923
+ * @param {WebGLProgram} program Program to check
5924
+ * @param {ErrorCallback} [errFn] func for errors
5925
+ * @return {bool} true if program is ok
5926
+ * @private
5927
+ */
5928
+
5929
+
5930
+ function _checkForProgramLinkCompletionAsync() {
5931
+ _checkForProgramLinkCompletionAsync = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(gl, program, progOptions) {
5932
+ var ext, checkFn, waitTime, success, err, errFn;
5933
+ return regeneratorRuntime.wrap(function _callee$(_context) {
5934
+ while (1) {
5935
+ switch (_context.prev = _context.next) {
5936
+ case 0:
5937
+ ext = gl.getExtension('KHR_parallel_shader_compile');
5938
+ checkFn = ext ? function (gl, program) {
5939
+ return gl.getProgramParameter(program, ext.COMPLETION_STATUS_KHR);
5940
+ } : function () {
5941
+ return true;
5942
+ };
5943
+ waitTime = 0;
5944
+
5945
+ case 3:
5946
+ _context.next = 5;
5947
+ return wait(waitTime);
5948
+
5949
+ case 5:
5950
+ // must wait at least once
5951
+ waitTime = 1000 / 60;
5952
+
5953
+ case 6:
5954
+ if (!checkFn(gl, program)) {
5955
+ _context.next = 3;
5956
+ break;
5957
+ }
5958
+
5959
+ case 7:
5960
+ success = checkProgramStatus(gl, program, progOptions.errorCallback);
5961
+ err = success ? undefined : progOptions.errors.join('\n');
5962
+
5963
+ if (!success) {
5964
+ errFn = progOptions.errorCallback || error;
5965
+ errFn(err);
5966
+ gl.deleteProgram(program); // TODO: delete shaders, but only shaders that were created newly for this
5967
+ // program
5968
+
5969
+ program = null;
5970
+ }
5971
+
5972
+ progOptions.callback(err, program);
5973
+
5974
+ case 11:
5975
+ case "end":
5976
+ return _context.stop();
5977
+ }
5978
+ }
5979
+ }, _callee);
5980
+ }));
5981
+ return _checkForProgramLinkCompletionAsync.apply(this, arguments);
5982
+ }
5983
+
5984
+ function checkProgramStatus(gl, program, errFn) {
5985
+ errFn = errFn || error; // Check the link status
5715
5986
 
5716
5987
  var linked = gl.getProgramParameter(program, LINK_STATUS);
5717
5988
 
5718
5989
  if (!linked) {
5719
5990
  // something went wrong with the link
5720
5991
  var lastError = gl.getProgramInfoLog(program);
5721
- progOptions.errorCallback("".concat(realShaders.map(function (shader) {
5722
- var src = addLineNumbersWithError(gl.getShaderSource(shader), '', 0);
5723
- var type = gl.getShaderParameter(shader, gl.SHADER_TYPE);
5724
- return "".concat(utils.glEnumToString(gl, type), "\n").concat(src, "}");
5725
- }).join('\n'), "\nError in program linking: ").concat(lastError));
5726
- gl.deleteProgram(program);
5727
- deleteShaders(gl, newShaders);
5728
- return null;
5992
+ errFn("Error in program linking: ".concat(lastError));
5729
5993
  }
5730
5994
 
5731
- return program;
5995
+ return linked;
5732
5996
  }
5733
5997
  /**
5734
5998
  * Loads a shader from a script tag.
@@ -5736,28 +6000,28 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
5736
6000
  * @param {string} scriptId The id of the script tag.
5737
6001
  * @param {number} [opt_shaderType] The type of shader. If not passed in it will
5738
6002
  * be derived from the type of the script tag.
5739
- * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors.
6003
+ * @param {module:twgl.ProgramOptions} [progOptions] callback for errors.
5740
6004
  * @return {WebGLShader?} The created shader or null if error.
5741
6005
  * @private
5742
6006
  */
5743
6007
 
5744
6008
 
5745
- function createShaderFromScript(gl, scriptId, opt_shaderType, opt_errorCallback) {
6009
+ function createShaderFromScript(gl, scriptId, opt_shaderType, progOptions) {
5746
6010
  var shaderSource = "";
5747
6011
  var shaderScript = getElementById(scriptId);
5748
6012
 
5749
6013
  if (!shaderScript) {
5750
- throw new Error("unknown script element: ".concat(scriptId));
6014
+ return reportError(progOptions, "unknown script element: ".concat(scriptId));
5751
6015
  }
5752
6016
 
5753
6017
  shaderSource = shaderScript.text;
5754
6018
  var shaderType = opt_shaderType || getShaderTypeFromScriptType(gl, shaderScript.type);
5755
6019
 
5756
6020
  if (!shaderType) {
5757
- throw new Error('unknown shader type');
6021
+ return reportError(progOptions, 'unknown shader type');
5758
6022
  }
5759
6023
 
5760
- return loadShader(gl, shaderSource, shaderType, opt_errorCallback);
6024
+ return loadShader(gl, shaderSource, shaderType, progOptions);
5761
6025
  }
5762
6026
  /**
5763
6027
  * Creates a program from 2 script tags.
@@ -5778,7 +6042,7 @@ function createShaderFromScript(gl, scriptId, opt_shaderType, opt_errorCallback)
5778
6042
  * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
5779
6043
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
5780
6044
  * on error. If you want something else pass an callback. It's passed an error message.
5781
- * @return {WebGLProgram?} the created program or null if error.
6045
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
5782
6046
  * @memberOf module:twgl/programs
5783
6047
  */
5784
6048
 
@@ -5788,7 +6052,7 @@ function createProgramFromScripts(gl, shaderScriptIds, opt_attribs, opt_location
5788
6052
  var shaders = [];
5789
6053
 
5790
6054
  for (var ii = 0; ii < shaderScriptIds.length; ++ii) {
5791
- var shader = createShaderFromScript(gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions.errorCallback);
6055
+ var shader = createShaderFromScript(gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions);
5792
6056
 
5793
6057
  if (!shader) {
5794
6058
  return null;
@@ -5818,7 +6082,7 @@ function createProgramFromScripts(gl, shaderScriptIds, opt_attribs, opt_location
5818
6082
  * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
5819
6083
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
5820
6084
  * on error. If you want something else pass an callback. It's passed an error message.
5821
- * @return {WebGLProgram?} the created program or null if error.
6085
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
5822
6086
  * @memberOf module:twgl/programs
5823
6087
  */
5824
6088
 
@@ -5828,9 +6092,9 @@ function createProgramFromSources(gl, shaderSources, opt_attribs, opt_locations,
5828
6092
  var shaders = [];
5829
6093
 
5830
6094
  for (var ii = 0; ii < shaderSources.length; ++ii) {
5831
- var shader = loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions.errorCallback);
6095
+ var shader = loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions);
5832
6096
 
5833
- if (!shader) {
6097
+ if (!progOptions.callback && !shader) {
5834
6098
  return null;
5835
6099
  }
5836
6100
 
@@ -6905,6 +7169,8 @@ function createProgramInfoFromProgram(gl, program) {
6905
7169
 
6906
7170
  return programInfo;
6907
7171
  }
7172
+
7173
+ var notIdRE = /\s|{|}|;/;
6908
7174
  /**
6909
7175
  * Creates a ProgramInfo from 2 sources.
6910
7176
  *
@@ -6936,18 +7202,18 @@ function createProgramInfoFromProgram(gl, program) {
6936
7202
  * @memberOf module:twgl/programs
6937
7203
  */
6938
7204
 
6939
-
6940
7205
  function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
6941
7206
  var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
6942
- var good = true;
7207
+ var errors = [];
6943
7208
  shaderSources = shaderSources.map(function (source) {
6944
7209
  // Lets assume if there is no \n it's an id
6945
- if (source.indexOf("\n") < 0) {
7210
+ if (!notIdRE.test(source)) {
6946
7211
  var script = getElementById(source);
6947
7212
 
6948
7213
  if (!script) {
6949
- progOptions.errorCallback("no element with id: " + source);
6950
- good = false;
7214
+ var err = "no element with id: ".concat(source);
7215
+ progOptions.errorCallback(err);
7216
+ errors.push(err);
6951
7217
  } else {
6952
7218
  source = script.text;
6953
7219
  }
@@ -6956,8 +7222,22 @@ function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_er
6956
7222
  return source;
6957
7223
  });
6958
7224
 
6959
- if (!good) {
6960
- return null;
7225
+ if (errors.length) {
7226
+ return reportError(progOptions, '');
7227
+ }
7228
+
7229
+ var origCallback = progOptions.callback;
7230
+
7231
+ if (origCallback) {
7232
+ progOptions.callback = function (err, program) {
7233
+ var programInfo;
7234
+
7235
+ if (!err) {
7236
+ programInfo = createProgramInfoFromProgram(gl, program);
7237
+ }
7238
+
7239
+ origCallback(err, programInfo);
7240
+ };
6961
7241
  }
6962
7242
 
6963
7243
  var program = createProgramFromSources(gl, shaderSources, progOptions);
@@ -10785,6 +11065,9 @@ function createVertexArrayInfo(gl, programInfos, bufferInfo) {
10785
11065
  * @param {Object.<string, function>} setters Attribute setters as returned from createAttributeSetters
10786
11066
  * @param {Object.<string, module:twgl.AttribInfo>} attribs AttribInfos mapped by attribute name.
10787
11067
  * @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
11068
+ *
11069
+ * @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
11070
+ *
10788
11071
  * @memberOf module:twgl/vertexArrays
10789
11072
  */
10790
11073
 
@@ -10812,6 +11095,9 @@ function createVAOAndSetAttributes(gl, setters, attribs, indices) {
10812
11095
  * @param {Object.<string, function>| module:twgl.ProgramInfo} programInfo as returned from createProgramInfo or Attribute setters as returned from createAttributeSetters
10813
11096
  * @param {module:twgl.BufferInfo} bufferInfo BufferInfo as returned from createBufferInfoFromArrays etc...
10814
11097
  * @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
11098
+ *
11099
+ * @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
11100
+ *
10815
11101
  * @memberOf module:twgl/vertexArrays
10816
11102
  */
10817
11103