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,4 +1,4 @@
1
- /* @license twgl.js 5.1.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
1
+ /* @license twgl.js 5.2.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
2
  Available via the MIT license.
3
3
  see: http://github.com/greggman/twgl.js for details */
4
4
  /*
@@ -1977,10 +1977,6 @@ function isRenderbuffer(gl, t) {
1977
1977
  return typeof WebGLRenderbuffer !== 'undefined' && isType(t, 'WebGLRenderbuffer');
1978
1978
  }
1979
1979
 
1980
- function isShader(gl, t) {
1981
- return typeof WebGLShader !== 'undefined' && isType(t, 'WebGLShader');
1982
- }
1983
-
1984
1980
  function isTexture(gl, t) {
1985
1981
  return typeof WebGLTexture !== 'undefined' && isType(t, 'WebGLTexture');
1986
1982
  }
@@ -7363,40 +7359,13 @@ function reportError(progOptions, msg) {
7363
7359
  return null;
7364
7360
  }
7365
7361
 
7366
- /**
7367
- * Loads a shader.
7368
- * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7369
- * @param {string} shaderSource The shader source.
7370
- * @param {number} shaderType The type of shader.
7371
- * @param {module:twgl.ProgramOptions} progOptions
7372
- * @return {WebGLShader} The created shader.
7373
- * @private
7374
- */
7375
- function loadShader(gl, shaderSource, shaderType, progOptions) {
7376
- // Create the shader object
7377
- const shader = gl.createShader(shaderType);
7378
-
7379
- // Load the shader source
7380
- gl.shaderSource(shader, prepShaderSource(shaderSource).shaderSource);
7381
-
7382
- // Compile the shader
7383
- gl.compileShader(shader);
7384
-
7385
- if (!progOptions.callback && !checkShaderStatus(gl, shaderType, shader, progOptions.errorCallback)) {
7386
- gl.deleteShader(shader);
7387
- return null;
7388
- }
7389
-
7390
- return shader;
7391
- }
7392
-
7393
7362
  /**
7394
7363
  * Check Shader status
7395
7364
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7396
7365
  * @param {number} shaderType The shader type
7397
7366
  * @param {WebGLShader} shader The shader
7398
7367
  * @param {ErrorCallback} [errFn] function to receive error message.
7399
- * @return {bool} true if shader is ok.
7368
+ * @return {string} errors or empty string
7400
7369
  * @private
7401
7370
  */
7402
7371
  function checkShaderStatus(gl, shaderType, shader, errFn) {
@@ -7407,15 +7376,35 @@ function checkShaderStatus(gl, shaderType, shader, errFn) {
7407
7376
  // Something went wrong during compilation; get the error
7408
7377
  const lastError = gl.getShaderInfoLog(shader);
7409
7378
  const {lineOffset, shaderSource} = prepShaderSource(gl.getShaderSource(shader));
7410
- errFn(`${addLineNumbersWithError(shaderSource, lastError, lineOffset)}\nError compiling ${glEnumToString(gl, shaderType)}: ${lastError}`);
7379
+ const error = `${addLineNumbersWithError(shaderSource, lastError, lineOffset)}\nError compiling ${glEnumToString(gl, shaderType)}: ${lastError}`;
7380
+ errFn(error);
7381
+ return error;
7411
7382
  }
7412
- return compiled;
7383
+ return '';
7413
7384
  }
7414
7385
 
7386
+ /**
7387
+ * @typedef {Object} FullProgramSpec
7388
+ * @property {string[]} shaders the shader source or element ids.
7389
+ * @property {function(string)} [errorCallback] callback for errors
7390
+ * @property {Object.<string,number>|string[]} [attribLocations] a attribute name to location map, or array of attribute names where index = location.
7391
+ * @property {(module:twgl.BufferInfo|Object.<string,module:twgl.AttribInfo>|string[])} [transformFeedbackVaryings] If passed
7392
+ * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
7393
+ * you can pass an array of names.
7394
+ * @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
7395
+ * @property {ProgramCallback} [callback] callback for async program compilation.
7396
+ * @memberOf module:twgl
7397
+ */
7398
+
7399
+ /**
7400
+ * @typedef {string[]|module:twgl.FullProgramSpec} ProgramSpec
7401
+ * @memberOf module:twgl
7402
+ */
7403
+
7415
7404
  /**
7416
7405
  * @typedef {Object} ProgramOptions
7417
7406
  * @property {function(string)} [errorCallback] callback for errors
7418
- * @property {Object.<string,number>} [attribLocations] a attribute name to location map
7407
+ * @property {Object.<string,number>|string[]} [attribLocations] a attribute name to location map, or array of attribute names where index = location.
7419
7408
  * @property {(module:twgl.BufferInfo|Object.<string,module:twgl.AttribInfo>|string[])} [transformFeedbackVaryings] If passed
7420
7409
  * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
7421
7410
  * you can pass an array of names.
@@ -7445,11 +7434,6 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
7445
7434
  opt_errorCallback = opt_attribs;
7446
7435
  opt_attribs = undefined;
7447
7436
  } else if (opt_attribs && !Array.isArray(opt_attribs)) {
7448
- // If we have an errorCallback we can just return this object
7449
- // Otherwise we need to construct one with default errorCallback
7450
- if (opt_attribs.errorCallback && opt_attribs.errors) {
7451
- return opt_attribs;
7452
- }
7453
7437
  const opt = opt_attribs;
7454
7438
  opt_errorCallback = opt.errorCallback;
7455
7439
  opt_attribs = opt.attribLocations;
@@ -7471,14 +7455,14 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
7471
7455
  errors,
7472
7456
  };
7473
7457
 
7474
- if (opt_attribs) {
7458
+ {
7475
7459
  let attribLocations = {};
7476
7460
  if (Array.isArray(opt_attribs)) {
7477
7461
  opt_attribs.forEach(function(attrib, ndx) {
7478
7462
  attribLocations[attrib] = opt_locations ? opt_locations[ndx] : ndx;
7479
7463
  });
7480
7464
  } else {
7481
- attribLocations = opt_attribs;
7465
+ attribLocations = opt_attribs || {};
7482
7466
  }
7483
7467
  options.attribLocations = attribLocations;
7484
7468
  }
@@ -7500,14 +7484,61 @@ function getShaderTypeFromScriptType(gl, scriptType) {
7500
7484
  return undefined;
7501
7485
  }
7502
7486
 
7503
- function deleteShaders(gl, shaders) {
7504
- shaders.forEach(function(shader) {
7505
- gl.deleteShader(shader);
7506
- });
7487
+ function deleteProgramAndShaders(gl, program, notThese) {
7488
+ const shaders = gl.getAttachedShaders(program);
7489
+ for (const shader of shaders) {
7490
+ if (notThese.has(shader)) {
7491
+ gl.deleteShader(shader);
7492
+ }
7493
+ }
7494
+ gl.deleteProgram(program);
7507
7495
  }
7508
7496
 
7509
7497
  const wait = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms));
7510
7498
 
7499
+ function createProgramNoCheck(gl, shaders, programOptions) {
7500
+ const program = gl.createProgram();
7501
+ const {
7502
+ attribLocations,
7503
+ transformFeedbackVaryings,
7504
+ transformFeedbackMode,
7505
+ } = getProgramOptions(programOptions);
7506
+
7507
+ for (let ndx = 0; ndx < shaders.length; ++ndx) {
7508
+ let shader = shaders[ndx];
7509
+ if (typeof shader === 'string') {
7510
+ const elem = getElementById(shader);
7511
+ const src = elem ? elem.text : shader;
7512
+ let type = gl[defaultShaderType[ndx]];
7513
+ if (elem && elem.type) {
7514
+ type = getShaderTypeFromScriptType(gl, elem.type) || type;
7515
+ }
7516
+ shader = gl.createShader(type);
7517
+ gl.shaderSource(shader, prepShaderSource(src).shaderSource);
7518
+ gl.compileShader(shader);
7519
+ gl.attachShader(program, shader);
7520
+ }
7521
+ }
7522
+
7523
+ Object.entries(attribLocations).forEach(([attrib, loc]) => gl.bindAttribLocation(program, loc, attrib));
7524
+
7525
+ {
7526
+ let varyings = transformFeedbackVaryings;
7527
+ if (varyings) {
7528
+ if (varyings.attribs) {
7529
+ varyings = varyings.attribs;
7530
+ }
7531
+ if (!Array.isArray(varyings)) {
7532
+ varyings = Object.keys(varyings);
7533
+ }
7534
+ gl.transformFeedbackVaryings(program, varyings, transformFeedbackMode || SEPARATE_ATTRIBS);
7535
+ }
7536
+ }
7537
+
7538
+ gl.linkProgram(program);
7539
+ return program;
7540
+ }
7541
+
7511
7542
  /**
7512
7543
  * Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
7513
7544
  * program.
@@ -7533,62 +7564,47 @@ function createProgram(
7533
7564
  // This code is really convoluted, because it may or may not be async
7534
7565
  // Maybe it would be better to have a separate function
7535
7566
  const progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
7536
- const realShaders = [];
7537
- const newShaders = [];
7538
- for (let ndx = 0; ndx < shaders.length; ++ndx) {
7539
- let shader = shaders[ndx];
7540
- if (typeof (shader) === 'string') {
7541
- const elem = getElementById(shader);
7542
- const src = elem ? elem.text : shader;
7543
- let type = gl[defaultShaderType[ndx]];
7544
- if (elem && elem.type) {
7545
- type = getShaderTypeFromScriptType(gl, elem.type) || type;
7546
- }
7547
- shader = loadShader(gl, src, type, progOptions);
7548
- newShaders.push(shader);
7549
- }
7550
- if (isShader(gl, shader)) {
7551
- realShaders.push(shader);
7552
- }
7553
- }
7554
-
7555
- if (realShaders.length !== shaders.length) {
7556
- deleteShaders(gl, newShaders);
7557
- return reportError(progOptions, "not enough shaders for program");
7558
- }
7567
+ const shaderSet = new Set(shaders);
7568
+ const program = createProgramNoCheck(gl, shaders, progOptions);
7559
7569
 
7560
- const program = gl.createProgram();
7561
- realShaders.forEach(function(shader) {
7562
- gl.attachShader(program, shader);
7563
- });
7564
- if (progOptions.attribLocations) {
7565
- Object.keys(progOptions.attribLocations).forEach(function(attrib) {
7566
- gl.bindAttribLocation(program, progOptions.attribLocations[attrib], attrib);
7567
- });
7568
- }
7569
- let varyings = progOptions.transformFeedbackVaryings;
7570
- if (varyings) {
7571
- if (varyings.attribs) {
7572
- varyings = varyings.attribs;
7570
+ function hasErrors(gl, program) {
7571
+ const errors = getProgramErrors(gl, program, progOptions.errorCallback);
7572
+ if (errors) {
7573
+ deleteProgramAndShaders(gl, program, shaderSet);
7573
7574
  }
7574
- if (!Array.isArray(varyings)) {
7575
- varyings = Object.keys(varyings);
7576
- }
7577
- gl.transformFeedbackVaryings(program, varyings, progOptions.transformFeedbackMode || SEPARATE_ATTRIBS);
7575
+ return errors;
7578
7576
  }
7579
7577
 
7580
- gl.linkProgram(program);
7581
7578
  if (progOptions.callback) {
7582
- checkForProgramLinkCompletionAsync(gl, program, progOptions);
7583
- return null;
7584
- } else {
7585
- if (!checkProgramStatus(gl, program, progOptions.errorCallback)) {
7586
- gl.deleteProgram(program);
7587
- deleteShaders(gl, newShaders);
7588
- return null;
7589
- }
7590
- return program;
7579
+ waitForProgramLinkCompletionAsync(gl, program).then(() => {
7580
+ const errors = hasErrors(gl, program);
7581
+ progOptions.callback(errors, errors ? undefined : program);
7582
+ });
7583
+ return undefined;
7591
7584
  }
7585
+
7586
+ return hasErrors(gl, program) ? undefined : program;
7587
+ }
7588
+
7589
+ /**
7590
+ * This only works because the functions it wraps the first 2 arguments
7591
+ * are gl and any, followed by things that become programOptions
7592
+ * @private
7593
+ */
7594
+ function wrapCallbackFnToAsyncFn(fn) {
7595
+ return function(gl, arg1, ...args) {
7596
+ return new Promise((resolve, reject) => {
7597
+ const programOptions = getProgramOptions(...args);
7598
+ programOptions.callback = (err, program) => {
7599
+ if (err) {
7600
+ reject(err);
7601
+ } else {
7602
+ resolve(program);
7603
+ }
7604
+ };
7605
+ fn(gl, arg1, programOptions);
7606
+ });
7607
+ };
7592
7608
  }
7593
7609
 
7594
7610
  /**
@@ -7601,6 +7617,7 @@ function createProgram(
7601
7617
  * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
7602
7618
  * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
7603
7619
  *
7620
+ * @function
7604
7621
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7605
7622
  * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
7606
7623
  * @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
@@ -7610,22 +7627,11 @@ function createProgram(
7610
7627
  * @return {Promise<WebGLProgram>} The created program
7611
7628
  * @memberOf module:twgl/programs
7612
7629
  */
7613
- function createProgramAsync(gl, shaders, ...args) {
7614
- return new Promise((resolve, reject) => {
7615
- const programOptions = getProgramOptions(...args);
7616
- programOptions.callback = (err, program) => {
7617
- if (err) {
7618
- reject(err);
7619
- } else {
7620
- resolve(program);
7621
- }
7622
- };
7623
- createProgram(gl, shaders, programOptions);
7624
- });
7625
- }
7630
+ const createProgramAsync = wrapCallbackFnToAsyncFn(createProgram);
7626
7631
 
7627
7632
  /**
7628
7633
  * Same as createProgramInfo but returns a promise
7634
+ * @function
7629
7635
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext
7630
7636
  * to use.
7631
7637
  * @param {string[]} shaderSources Array of sources for the
@@ -7638,31 +7644,9 @@ function createProgramAsync(gl, shaders, ...args) {
7638
7644
  * @return {Promise<module:twgl.ProgramInfo>} The created ProgramInfo
7639
7645
  * @memberOf module:twgl/programs
7640
7646
  */
7641
- function createProgramInfoAsync(gl, shaders, ...args) {
7642
- return new Promise((resolve, reject) => {
7643
- const programOptions = getProgramOptions(...args);
7644
- programOptions.callback = (err, programInfo) => {
7645
- if (err) {
7646
- reject(err);
7647
- } else {
7648
- resolve(programInfo);
7649
- }
7650
- };
7651
- createProgramInfo(gl, shaders, programOptions);
7652
- });
7653
- }
7647
+ const createProgramInfoAsync = wrapCallbackFnToAsyncFn(createProgramInfo);
7654
7648
 
7655
-
7656
- /**
7657
- * Asynchronously wait for program to link.
7658
- * Note: if 'KHR_parallel_shader_compile' extension does not
7659
- * exist then compilation will not be truly async.
7660
- * @param {WebGLRenderingContext} gl The context
7661
- * @param {WebGLProgram} program The program
7662
- * @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
7663
- * @private
7664
- */
7665
- async function checkForProgramLinkCompletionAsync(gl, program, progOptions) {
7649
+ async function waitForProgramLinkCompletionAsync(gl, program) {
7666
7650
  const ext = gl.getExtension('KHR_parallel_shader_compile');
7667
7651
  const checkFn = ext
7668
7652
  ? (gl, program) => gl.getProgramParameter(program, ext.COMPLETION_STATUS_KHR)
@@ -7673,18 +7657,12 @@ async function checkForProgramLinkCompletionAsync(gl, program, progOptions) {
7673
7657
  await wait(waitTime); // must wait at least once
7674
7658
  waitTime = 1000 / 60;
7675
7659
  } while (!checkFn(gl, program));
7660
+ }
7676
7661
 
7677
- const success = checkProgramStatus(gl, program, progOptions.errorCallback);
7678
- const err = success ? undefined : progOptions.errors.join('\n');
7679
- if (!success) {
7680
- const errFn = progOptions.errorCallback || error;
7681
- errFn(err);
7682
- gl.deleteProgram(program);
7683
- // TODO: delete shaders, but only shaders that were created newly for this
7684
- // program
7685
- program = null;
7662
+ async function waitForAllProgramsLinkCompletionAsync(gl, programs) {
7663
+ for (const program of Object.values(programs)) {
7664
+ await waitForProgramLinkCompletionAsync(gl, program);
7686
7665
  }
7687
- progOptions.callback(err, program);
7688
7666
  }
7689
7667
 
7690
7668
  /**
@@ -7692,10 +7670,10 @@ async function checkForProgramLinkCompletionAsync(gl, program, progOptions) {
7692
7670
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7693
7671
  * @param {WebGLProgram} program Program to check
7694
7672
  * @param {ErrorCallback} [errFn] func for errors
7695
- * @return {bool} true if program is ok
7673
+ * @return {string?} errors if program is failed, else undefined
7696
7674
  * @private
7697
7675
  */
7698
- function checkProgramStatus(gl, program, errFn) {
7676
+ function getProgramErrors(gl, program, errFn) {
7699
7677
  errFn = errFn || error;
7700
7678
  // Check the link status
7701
7679
  const linked = gl.getProgramParameter(program, LINK_STATUS);
@@ -7703,35 +7681,12 @@ function checkProgramStatus(gl, program, errFn) {
7703
7681
  // something went wrong with the link
7704
7682
  const lastError = gl.getProgramInfoLog(program);
7705
7683
  errFn(`Error in program linking: ${lastError}`);
7684
+ // print any errors from these shaders
7685
+ const shaders = gl.getAttachedShaders(program);
7686
+ const errors = shaders.map(shader => checkShaderStatus(gl, gl.getShaderParameter(shader, gl.SHADER_TYPE), shader, errFn));
7687
+ return `${lastError}\n${errors.filter(_ => _).join('\n')}`;
7706
7688
  }
7707
- return linked;
7708
- }
7709
-
7710
- /**
7711
- * Loads a shader from a script tag.
7712
- * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7713
- * @param {string} scriptId The id of the script tag.
7714
- * @param {number} [opt_shaderType] The type of shader. If not passed in it will
7715
- * be derived from the type of the script tag.
7716
- * @param {module:twgl.ProgramOptions} [progOptions] callback for errors.
7717
- * @return {WebGLShader?} The created shader or null if error.
7718
- * @private
7719
- */
7720
- function createShaderFromScript(
7721
- gl, scriptId, opt_shaderType, progOptions) {
7722
- let shaderSource = "";
7723
- const shaderScript = getElementById(scriptId);
7724
- if (!shaderScript) {
7725
- return reportError(progOptions, `unknown script element: ${scriptId}`);
7726
- }
7727
- shaderSource = shaderScript.text;
7728
-
7729
- const shaderType = opt_shaderType || getShaderTypeFromScriptType(gl, shaderScript.type);
7730
- if (!shaderType) {
7731
- return reportError(progOptions, 'unknown shader type');
7732
- }
7733
-
7734
- return loadShader(gl, shaderSource, shaderType, progOptions);
7689
+ return undefined;
7735
7690
  }
7736
7691
 
7737
7692
  /**
@@ -7760,13 +7715,12 @@ function createProgramFromScripts(
7760
7715
  gl, shaderScriptIds, opt_attribs, opt_locations, opt_errorCallback) {
7761
7716
  const progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
7762
7717
  const shaders = [];
7763
- for (let ii = 0; ii < shaderScriptIds.length; ++ii) {
7764
- const shader = createShaderFromScript(
7765
- gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions);
7766
- if (!shader) {
7767
- return null;
7718
+ for (const scriptId of shaderScriptIds) {
7719
+ const shaderScript = getElementById(scriptId);
7720
+ if (!shaderScript) {
7721
+ return reportError(progOptions, `unknown script element: ${scriptId}`);
7768
7722
  }
7769
- shaders.push(shader);
7723
+ shaders.push(shaderScript.text);
7770
7724
  }
7771
7725
  return createProgram(gl, shaders, progOptions);
7772
7726
  }
@@ -7795,16 +7749,7 @@ function createProgramFromScripts(
7795
7749
  */
7796
7750
  function createProgramFromSources(
7797
7751
  gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
7798
- const progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
7799
- const shaders = [];
7800
- for (let ii = 0; ii < shaderSources.length; ++ii) {
7801
- const shader = loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions);
7802
- if (!progOptions.callback && !shader) {
7803
- return null;
7804
- }
7805
- shaders.push(shader);
7806
- }
7807
- return createProgram(gl, shaders, progOptions);
7752
+ return createProgram(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback);
7808
7753
  }
7809
7754
 
7810
7755
  /**
@@ -8863,35 +8808,228 @@ function createProgramInfo(
8863
8808
  }
8864
8809
  return source;
8865
8810
  });
8811
+
8866
8812
  if (errors.length) {
8867
8813
  return reportError(progOptions, '');
8868
8814
  }
8815
+
8869
8816
  const origCallback = progOptions.callback;
8870
8817
  if (origCallback) {
8871
8818
  progOptions.callback = (err, program) => {
8872
- let programInfo;
8873
- if (!err) {
8874
- programInfo = createProgramInfoFromProgram(gl, program);
8875
- }
8876
- origCallback(err, programInfo);
8819
+ origCallback(err, err ? undefined : createProgramInfoFromProgram(gl, program));
8877
8820
  };
8878
8821
  }
8822
+
8879
8823
  const program = createProgramFromSources(gl, shaderSources, progOptions);
8880
8824
  if (!program) {
8881
8825
  return null;
8882
8826
  }
8827
+
8883
8828
  return createProgramInfoFromProgram(gl, program);
8884
8829
  }
8885
8830
 
8831
+ function checkAllPrograms(gl, programs, programSpecs, noDeleteShadersSet, programOptions) {
8832
+ // check errors for everything.
8833
+ for (const [name, program] of Object.entries(programs)) {
8834
+ const options = {...programOptions};
8835
+ const spec = programSpecs[name];
8836
+ if (!Array.isArray(spec)) {
8837
+ Object.assign(options, spec);
8838
+ }
8839
+ const errors = getProgramErrors(gl, program, options.errorCallback);
8840
+ if (errors) {
8841
+ // delete everything we created
8842
+ for (const program of Object.values(programs)) {
8843
+ const shaders = gl.getAttachedShaders(program);
8844
+ gl.deleteProgram(program);
8845
+ for (const shader of shaders) {
8846
+ // Don't delete it if we didn't create it.
8847
+ if (!noDeleteShadersSet.has(shader)) {
8848
+ gl.deleteShader(shader);
8849
+ }
8850
+ }
8851
+ }
8852
+ return errors;
8853
+ }
8854
+ }
8855
+
8856
+ return undefined;
8857
+ }
8858
+
8859
+ /**
8860
+ * Creates multiple programs
8861
+ *
8862
+ * Note: the reason this function exists is because the fastest way to create multiple
8863
+ * programs in WebGL is to create and compile all shaders and link all programs and only
8864
+ * afterwards check if they succeeded. In that way, giving all your shaders
8865
+ *
8866
+ * @see {@link module:twgl.createProgram}
8867
+ *
8868
+ * Example:
8869
+ *
8870
+ * const programs = twgl.createPrograms(gl, {
8871
+ * lambert: [lambertVS, lambertFS],
8872
+ * phong: [phongVS, phoneFS],
8873
+ * particles: {
8874
+ * shaders: [particlesVS, particlesFS],
8875
+ * transformFeedbackVaryings: ['position', 'velocity'],
8876
+ * },
8877
+ * });
8878
+ *
8879
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
8880
+ * @param {Object.<string, module:twgl.ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
8881
+ * @param {module:twgl.ProgramOptions} [programOptions] options to apply to all programs
8882
+ * @return {Object.<string, WebGLProgram>?} the created programInfos by name
8883
+ */
8884
+ function createPrograms(gl, programSpecs, programOptions = {}) {
8885
+ // Remember existing shaders so that if there is an error we don't delete them
8886
+ const noDeleteShadersSet = new Set();
8887
+
8888
+ // compile and link everything
8889
+ const programs = Object.fromEntries(Object.entries(programSpecs).map(([name, spec]) => {
8890
+ const options = {...programOptions};
8891
+ const shaders = Array.isArray(spec) ? spec : spec.shaders;
8892
+ if (!Array.isArray(spec)) {
8893
+ Object.assign(options, spec);
8894
+ }
8895
+ shaders.forEach(noDeleteShadersSet.add, noDeleteShadersSet);
8896
+ return [name, createProgramNoCheck(gl, shaders, options)];
8897
+ }));
8898
+
8899
+ if (programOptions.callback) {
8900
+ waitForAllProgramsLinkCompletionAsync(gl, programs).then(() => {
8901
+ const errors = checkAllPrograms(gl, programs, programSpecs, noDeleteShadersSet, programOptions);
8902
+ programOptions.callback(errors, errors ? undefined : programs);
8903
+ });
8904
+ return undefined;
8905
+ }
8906
+
8907
+ const errors = checkAllPrograms(gl, programs, programSpecs, noDeleteShadersSet, programOptions);
8908
+ return errors ? undefined : programs;
8909
+ }
8910
+
8911
+ /**
8912
+ * Creates multiple programInfos
8913
+ *
8914
+ * Note: the reason this function exists is because the fastest way to create multiple
8915
+ * programs in WebGL is to create and compile all shaders and link all programs and only
8916
+ * afterwards check if they succeeded. In that way, giving all your shaders
8917
+ *
8918
+ * @see {@link module:twgl.createProgramInfo}
8919
+ *
8920
+ * Examples:
8921
+ *
8922
+ * const programInfos = twgl.createProgramInfos(gl, {
8923
+ * lambert: [lambertVS, lambertFS],
8924
+ * phong: [phongVS, phoneFS],
8925
+ * particles: {
8926
+ * shaders: [particlesVS, particlesFS],
8927
+ * transformFeedbackVaryings: ['position', 'velocity'],
8928
+ * },
8929
+ * });
8930
+ *
8931
+ * or
8932
+ *
8933
+ * const {lambert, phong, particles} = twgl.createProgramInfos(gl, {
8934
+ * lambert: [lambertVS, lambertFS],
8935
+ * phong: [phongVS, phoneFS],
8936
+ * particles: {
8937
+ * shaders: [particlesVS, particlesFS],
8938
+ * transformFeedbackVaryings: ['position', 'velocity'],
8939
+ * },
8940
+ * });
8941
+ *
8942
+ *
8943
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
8944
+ * @param {Object.<string, module:twgl.ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
8945
+ * @param {module:twgl.ProgramOptions} [programOptions] options to apply to all programs
8946
+ * @return {Object.<string, module:twgl.ProgramInfo>?} the created programInfos by name
8947
+ */
8948
+ function createProgramInfos(gl, programSpecs, programOptions) {
8949
+ programOptions = getProgramOptions(programOptions);
8950
+
8951
+ function createProgramInfosForPrograms(gl, programs) {
8952
+ return Object.fromEntries(Object.entries(programs).map(([name, program]) =>
8953
+ [name, createProgramInfoFromProgram(gl, program)]
8954
+ ));
8955
+ }
8956
+
8957
+ const origCallback = programOptions.callback;
8958
+ if (origCallback) {
8959
+ programOptions.callback = (err, programs) => {
8960
+ origCallback(err, err ? undefined : createProgramInfosForPrograms(gl, programs));
8961
+ };
8962
+ }
8963
+
8964
+ const programs = createPrograms(gl, programSpecs, programOptions);
8965
+ if (origCallback || !programs) {
8966
+ return undefined;
8967
+ }
8968
+
8969
+ return createProgramInfosForPrograms(gl, programs);
8970
+ }
8971
+
8972
+ /**
8973
+ * Creates multiple programs asynchronously
8974
+ *
8975
+ * @see {@link module:twgl.createProgramAsync}
8976
+ *
8977
+ * Example:
8978
+ *
8979
+ * const programs = await twgl.createProgramsAsync(gl, {
8980
+ * lambert: [lambertVS, lambertFS],
8981
+ * phong: [phongVS, phoneFS],
8982
+ * particles: {
8983
+ * shaders: [particlesVS, particlesFS],
8984
+ * transformFeedbackVaryings: ['position', 'velocity'],
8985
+ * },
8986
+ * });
8987
+ *
8988
+ * @function
8989
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
8990
+ * @param {Object.<string, module:twgl.ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
8991
+ * @param {module:twgl.ProgramOptions} [programOptions] options to apply to all programs
8992
+ * @return {Object.<string, WebGLProgram>?} the created programInfos by name
8993
+ */
8994
+ const createProgramsAsync = wrapCallbackFnToAsyncFn(createPrograms);
8995
+
8996
+ /**
8997
+ * Creates multiple programInfos asynchronously
8998
+ *
8999
+ * @see {@link module:twgl.createProgramInfoAsync}
9000
+ *
9001
+ * Example:
9002
+ *
9003
+ * const programInfos = await twgl.createProgramInfosAsync(gl, {
9004
+ * lambert: [lambertVS, lambertFS],
9005
+ * phong: [phongVS, phoneFS],
9006
+ * particles: {
9007
+ * shaders: [particlesVS, particlesFS],
9008
+ * transformFeedbackVaryings: ['position', 'velocity'],
9009
+ * },
9010
+ * });
9011
+ *
9012
+ * @function
9013
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
9014
+ * @param {Object.<string, module:twgl.ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
9015
+ * @param {module:twgl.ProgramOptions} [programOptions] options to apply to all programs
9016
+ * @return {Promise<Object.<string, module:twgl.ProgramInfo>>} the created programInfos by name
9017
+ */
9018
+ const createProgramInfosAsync = wrapCallbackFnToAsyncFn(createProgramInfos);
9019
+
8886
9020
  var programs = /*#__PURE__*/Object.freeze({
8887
9021
  __proto__: null,
8888
9022
  createAttributeSetters: createAttributeSetters,
8889
9023
  createProgram: createProgram,
8890
9024
  createProgramAsync: createProgramAsync,
9025
+ createPrograms: createPrograms,
9026
+ createProgramsAsync: createProgramsAsync,
8891
9027
  createProgramFromScripts: createProgramFromScripts,
8892
9028
  createProgramFromSources: createProgramFromSources,
8893
9029
  createProgramInfo: createProgramInfo,
8894
9030
  createProgramInfoAsync: createProgramInfoAsync,
9031
+ createProgramInfos: createProgramInfos,
9032
+ createProgramInfosAsync: createProgramInfosAsync,
8895
9033
  createProgramInfoFromProgram: createProgramInfoFromProgram,
8896
9034
  createUniformSetters: createUniformSetters,
8897
9035
  createUniformBlockSpecFromProgram: createUniformBlockSpecFromProgram,
@@ -9934,4 +10072,4 @@ function resizeCanvasToDisplaySize(canvas, multiplier) {
9934
10072
  return false;
9935
10073
  }
9936
10074
 
9937
- export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramAsync, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoAsync, createProgramInfoFromProgram, createSampler, createSamplers, createTexture, createTextures, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray$1 as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents$1 as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer$1 as isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults$2 as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };
10075
+ export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramAsync, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoAsync, createProgramInfoFromProgram, createProgramInfos, createProgramInfosAsync, createPrograms, createProgramsAsync, createSampler, createSamplers, createTexture, createTextures, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray$1 as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents$1 as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer$1 as isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults$2 as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };