@luma.gl/webgl 9.1.0-alpha.1 → 9.1.0-alpha.9

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.
Files changed (79) hide show
  1. package/dist/adapter/converters/device-parameters.d.ts.map +1 -1
  2. package/dist/adapter/converters/device-parameters.js +18 -11
  3. package/dist/adapter/converters/texture-formats.d.ts +1 -1
  4. package/dist/adapter/converters/texture-formats.d.ts.map +1 -1
  5. package/dist/adapter/converters/texture-formats.js +9 -16
  6. package/dist/adapter/device-helpers/webgl-device-features.d.ts.map +1 -1
  7. package/dist/adapter/device-helpers/webgl-device-features.js +0 -1
  8. package/dist/adapter/helpers/webgl-texture-utils.d.ts +4 -4
  9. package/dist/adapter/helpers/webgl-texture-utils.d.ts.map +1 -1
  10. package/dist/adapter/helpers/webgl-texture-utils.js +11 -9
  11. package/dist/adapter/resources/webgl-framebuffer.js +1 -1
  12. package/dist/adapter/resources/webgl-render-pass.d.ts.map +1 -1
  13. package/dist/adapter/resources/webgl-render-pass.js +17 -4
  14. package/dist/adapter/resources/webgl-render-pipeline.d.ts.map +1 -1
  15. package/dist/adapter/resources/webgl-render-pipeline.js +1 -1
  16. package/dist/adapter/resources/webgl-shader.js +1 -1
  17. package/dist/adapter/resources/webgl-texture.d.ts +2 -2
  18. package/dist/adapter/resources/webgl-texture.d.ts.map +1 -1
  19. package/dist/adapter/resources/webgl-texture.js +12 -27
  20. package/dist/adapter/resources/webgl-transform-feedback.js +1 -1
  21. package/dist/adapter/resources/webgl-vertex-array.d.ts.map +1 -1
  22. package/dist/adapter/resources/webgl-vertex-array.js +3 -0
  23. package/dist/adapter/webgl-adapter.d.ts +21 -0
  24. package/dist/adapter/webgl-adapter.d.ts.map +1 -0
  25. package/dist/adapter/webgl-adapter.js +91 -0
  26. package/dist/adapter/webgl-device.d.ts +15 -27
  27. package/dist/adapter/webgl-device.d.ts.map +1 -1
  28. package/dist/adapter/webgl-device.js +32 -112
  29. package/dist/classic/copy-and-blit.d.ts +1 -0
  30. package/dist/classic/copy-and-blit.d.ts.map +1 -1
  31. package/dist/classic/copy-and-blit.js +11 -10
  32. package/dist/context/debug/spector-types.d.ts +1108 -0
  33. package/dist/context/debug/spector-types.d.ts.map +1 -0
  34. package/dist/context/debug/spector-types.js +697 -0
  35. package/dist/context/debug/spector.d.ts +12 -8
  36. package/dist/context/debug/spector.d.ts.map +1 -1
  37. package/dist/context/debug/spector.js +23 -17
  38. package/dist/context/polyfills/polyfill-webgl1-extensions.d.ts +9 -0
  39. package/dist/context/polyfills/polyfill-webgl1-extensions.d.ts.map +1 -0
  40. package/dist/context/polyfills/polyfill-webgl1-extensions.js +181 -0
  41. package/dist/context/state-tracker/webgl-state-tracker.d.ts +43 -0
  42. package/dist/context/state-tracker/webgl-state-tracker.d.ts.map +1 -0
  43. package/dist/context/state-tracker/{track-context-state.js → webgl-state-tracker.js} +44 -74
  44. package/dist/context/state-tracker/with-parameters.d.ts.map +1 -1
  45. package/dist/context/state-tracker/with-parameters.js +5 -4
  46. package/dist/dist.dev.js +341 -234
  47. package/dist/dist.min.js +2 -2
  48. package/dist/index.cjs +344 -236
  49. package/dist/index.cjs.map +4 -4
  50. package/dist/index.d.ts +3 -1
  51. package/dist/index.d.ts.map +1 -1
  52. package/dist/index.js +3 -2
  53. package/dist/utils/uid.d.ts +7 -0
  54. package/dist/utils/uid.d.ts.map +1 -0
  55. package/dist/utils/uid.js +14 -0
  56. package/package.json +3 -3
  57. package/src/adapter/converters/device-parameters.ts +18 -12
  58. package/src/adapter/converters/texture-formats.ts +12 -20
  59. package/src/adapter/device-helpers/webgl-device-features.ts +0 -1
  60. package/src/adapter/helpers/webgl-texture-utils.ts +12 -9
  61. package/src/adapter/resources/webgl-framebuffer.ts +1 -1
  62. package/src/adapter/resources/webgl-render-pass.ts +17 -4
  63. package/src/adapter/resources/webgl-render-pipeline.ts +2 -1
  64. package/src/adapter/resources/webgl-shader.ts +1 -1
  65. package/src/adapter/resources/webgl-texture.ts +17 -27
  66. package/src/adapter/resources/webgl-transform-feedback.ts +1 -1
  67. package/src/adapter/resources/webgl-vertex-array.ts +3 -0
  68. package/src/adapter/webgl-adapter.ts +113 -0
  69. package/src/adapter/webgl-device.ts +32 -135
  70. package/src/classic/copy-and-blit.ts +14 -9
  71. package/src/context/debug/spector-types.ts +1154 -0
  72. package/src/context/debug/spector.ts +38 -29
  73. package/src/context/polyfills/polyfill-webgl1-extensions.ts +202 -0
  74. package/src/context/state-tracker/{track-context-state.ts → webgl-state-tracker.ts} +55 -94
  75. package/src/context/state-tracker/with-parameters.ts +5 -4
  76. package/src/index.ts +5 -7
  77. package/src/utils/uid.ts +16 -0
  78. package/dist/context/state-tracker/track-context-state.d.ts +0 -22
  79. package/dist/context/state-tracker/track-context-state.d.ts.map +0 -1
package/dist/index.cjs CHANGED
@@ -2,7 +2,6 @@ var __defProp = Object.defineProperty;
2
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
5
  var __export = (target, all) => {
7
6
  for (var name in all)
8
7
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -16,10 +15,6 @@ var __copyProps = (to, from, except, desc) => {
16
15
  return to;
17
16
  };
18
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var __publicField = (obj, key, value) => {
20
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
- return value;
22
- };
23
18
 
24
19
  // dist/index.js
25
20
  var dist_exports = {};
@@ -37,20 +32,22 @@ __export(dist_exports, {
37
32
  WEBGLVertexArray: () => WEBGLVertexArray,
38
33
  WebGLCanvasContext: () => WebGLCanvasContext,
39
34
  WebGLDevice: () => WebGLDevice,
35
+ WebGLStateTracker: () => WebGLStateTracker,
40
36
  _TEXTURE_FORMATS: () => TEXTURE_FORMATS,
41
37
  getGLParameters: () => getGLParameters,
42
38
  getShaderLayout: () => getShaderLayout,
43
- popContextState: () => popContextState,
44
- pushContextState: () => pushContextState,
45
39
  resetGLParameters: () => resetGLParameters,
46
40
  setDeviceParameters: () => setDeviceParameters,
47
41
  setGLParameters: () => setGLParameters,
48
- trackContextState: () => trackContextState,
42
+ webgl2Adapter: () => webgl2Adapter,
49
43
  withDeviceParameters: () => withDeviceParameters,
50
44
  withGLParameters: () => withGLParameters
51
45
  });
52
46
  module.exports = __toCommonJS(dist_exports);
53
47
 
48
+ // dist/adapter/webgl-adapter.js
49
+ var import_core23 = require("@luma.gl/core");
50
+
54
51
  // dist/adapter/webgl-device.js
55
52
  var import_core22 = require("@luma.gl/core");
56
53
 
@@ -594,24 +591,22 @@ function deepArrayEqual(x, y) {
594
591
  return false;
595
592
  }
596
593
 
597
- // dist/context/state-tracker/track-context-state.js
598
- var GLState = class {
594
+ // dist/context/state-tracker/webgl-state-tracker.js
595
+ var WebGLStateTracker = class {
596
+ static get(gl) {
597
+ return gl.state;
598
+ }
599
599
  gl;
600
600
  program = null;
601
601
  stateStack = [];
602
602
  enable = true;
603
- cache;
603
+ cache = null;
604
604
  log;
605
- constructor(gl, {
606
- copyState = false,
607
- // Copy cache from params (slow) or initialize from WebGL defaults (fast)
608
- log: log9 = () => {
609
- }
610
- // Logging function, called when gl parameter change calls are actually issued
611
- } = {}) {
605
+ initialized = false;
606
+ constructor(gl, props) {
612
607
  this.gl = gl;
613
- this.cache = copyState ? getGLParameters(gl) : Object.assign({}, GL_PARAMETER_DEFAULTS);
614
- this.log = log9;
608
+ this.log = (props == null ? void 0 : props.log) || (() => {
609
+ });
615
610
  this._updateCache = this._updateCache.bind(this);
616
611
  Object.seal(this);
617
612
  }
@@ -623,6 +618,29 @@ var GLState = class {
623
618
  setGLParameters(this.gl, oldValues);
624
619
  this.stateStack.pop();
625
620
  }
621
+ /**
622
+ * Initialize WebGL state caching on a context
623
+ * can be called multiple times to enable/disable
624
+ *
625
+ * @note After calling this function, context state will be cached
626
+ * .push() and .pop() will be available for saving,
627
+ * temporarily modifying, and then restoring state.
628
+ */
629
+ trackState(gl, options) {
630
+ this.cache = options.copyState ? getGLParameters(gl) : Object.assign({}, GL_PARAMETER_DEFAULTS);
631
+ if (this.initialized) {
632
+ throw new Error("WebGLStateTracker");
633
+ }
634
+ this.initialized = true;
635
+ this.gl.state = this;
636
+ installProgramSpy(gl);
637
+ for (const key in GL_HOOKED_SETTERS) {
638
+ const setter = GL_HOOKED_SETTERS[key];
639
+ installSetterSpy(gl, key, setter);
640
+ }
641
+ installGetterOverride(gl, "getParameter");
642
+ installGetterOverride(gl, "isEnabled");
643
+ }
626
644
  /**
627
645
  // interceptor for context set functions - update our cache and our stack
628
646
  // values (Object) - the key values for this setter
@@ -648,44 +666,13 @@ var GLState = class {
648
666
  return { valueChanged, oldValue };
649
667
  }
650
668
  };
651
- function getContextState(gl) {
652
- return gl.state;
653
- }
654
- function trackContextState(gl, options) {
655
- const { enable: enable2 = true, copyState } = options || {};
656
- if (!gl.state) {
657
- gl.state = new GLState(gl, { copyState });
658
- installProgramSpy(gl);
659
- for (const key in GL_HOOKED_SETTERS) {
660
- const setter = GL_HOOKED_SETTERS[key];
661
- installSetterSpy(gl, key, setter);
662
- }
663
- installGetterOverride(gl, "getParameter");
664
- installGetterOverride(gl, "isEnabled");
665
- }
666
- const glState = getContextState(gl);
667
- glState.enable = enable2;
668
- return gl;
669
- }
670
- function pushContextState(gl) {
671
- let glState = getContextState(gl);
672
- if (!glState) {
673
- trackContextState(gl, { copyState: false });
674
- glState = getContextState(gl);
675
- }
676
- glState.push();
677
- }
678
- function popContextState(gl) {
679
- const glState = getContextState(gl);
680
- glState.pop();
681
- }
682
669
  function installGetterOverride(gl, functionName) {
683
670
  const originalGetterFunc = gl[functionName].bind(gl);
684
671
  gl[functionName] = function get(pname) {
685
672
  if (pname === void 0 || NON_CACHE_PARAMETERS.has(pname)) {
686
673
  return originalGetterFunc(pname);
687
674
  }
688
- const glState = getContextState(gl);
675
+ const glState = WebGLStateTracker.get(gl);
689
676
  if (!(pname in glState.cache)) {
690
677
  glState.cache[pname] = originalGetterFunc(pname);
691
678
  }
@@ -708,7 +695,7 @@ function installSetterSpy(gl, functionName, setter) {
708
695
  }
709
696
  const originalSetterFunc = gl[functionName].bind(gl);
710
697
  gl[functionName] = function set(...params) {
711
- const glState = getContextState(gl);
698
+ const glState = WebGLStateTracker.get(gl);
712
699
  const { valueChanged, oldValue } = setter(glState._updateCache, ...params);
713
700
  if (valueChanged) {
714
701
  originalSetterFunc(...params);
@@ -723,7 +710,7 @@ function installSetterSpy(gl, functionName, setter) {
723
710
  function installProgramSpy(gl) {
724
711
  const originalUseProgram = gl.useProgram.bind(gl);
725
712
  gl.useProgram = function useProgramLuma(handle) {
726
- const glState = getContextState(gl);
713
+ const glState = WebGLStateTracker.get(gl);
727
714
  if (glState.program !== handle) {
728
715
  originalUseProgram(handle);
729
716
  glState.program = handle;
@@ -941,26 +928,6 @@ function checkTextureFeature(gl, feature, extensions) {
941
928
  return textureExtensions.every((extension) => getWebGLExtension(gl, extension, extensions));
942
929
  }
943
930
  var TEXTURE_FORMATS = {
944
- // Unsized formats that leave the precision up to the driver. TODO - Fix bpp constants
945
- "rgb8unorm-unsized": {
946
- gl: 6407,
947
- b: 4,
948
- c: 2,
949
- bpp: 4,
950
- dataFormat: 6407,
951
- types: [5121, 33635]
952
- },
953
- "rgba8unorm-unsized": {
954
- gl: 6408,
955
- b: 4,
956
- c: 2,
957
- bpp: 4,
958
- dataFormat: 6408,
959
- types: [5121, 32819, 32820]
960
- },
961
- // 'r8unorm-unsized': {gl: GL.LUMINANCE, b: 4, c: 2, bpp: 4},
962
- // 'rgb8unorm-srgb-unsized': {gl: GL.SRGB_EXT, b: 4, c: 2, bpp: 4, gl1Ext: SRGB},
963
- // 'rgba8unorm-srgb-unsized': {gl: GL.SRGB_ALPHA_EXT, b: 4, c: 2, bpp: 4, gl1Ext: SRGB},
964
931
  // 8-bit formats
965
932
  "r8unorm": { gl: 33321, b: 1, c: 1, rb: true },
966
933
  "r8snorm": { gl: 36756, b: 1, c: 1, render: snorm8_renderable },
@@ -1075,17 +1042,6 @@ var TEXTURE_FORMATS = {
1075
1042
  dataFormat: 34041,
1076
1043
  types: [34042]
1077
1044
  },
1078
- // "depth24unorm-stencil8" feature
1079
- "depth24unorm-stencil8": {
1080
- gl: 35056,
1081
- b: 4,
1082
- c: 2,
1083
- p: 1,
1084
- attachment: 33306,
1085
- dataFormat: 34041,
1086
- types: [34042],
1087
- rb: true
1088
- },
1089
1045
  // "depth32float-stencil8" feature - TODO below is render buffer only?
1090
1046
  "depth32float-stencil8": {
1091
1047
  gl: 36013,
@@ -1175,6 +1131,10 @@ function isTextureFormatSupported(gl, format, extensions) {
1175
1131
  if (info.gl === void 0) {
1176
1132
  return false;
1177
1133
  }
1134
+ const feature = info.f;
1135
+ if (feature) {
1136
+ return checkTextureFeature(gl, feature, extensions);
1137
+ }
1178
1138
  const extension = info.x || info.gl2ext;
1179
1139
  if (extension) {
1180
1140
  return Boolean(getWebGLExtension(gl, extension, extensions));
@@ -1220,11 +1180,10 @@ function getTextureFormatWebGL(format) {
1220
1180
  const decoded = (0, import_core.decodeTextureFormat)(format);
1221
1181
  return {
1222
1182
  internalFormat: webglFormat,
1223
- format: (formatData == null ? void 0 : formatData.dataFormat) || getWebGLPixelDataFormat(decoded.format, decoded.integer, decoded.normalized, webglFormat),
1183
+ format: (formatData == null ? void 0 : formatData.dataFormat) || getWebGLPixelDataFormat(decoded.channels, decoded.integer, decoded.normalized, webglFormat),
1224
1184
  // depth formats don't have a type
1225
1185
  type: decoded.dataType ? getGLFromVertexType(decoded.dataType) : ((_a = formatData == null ? void 0 : formatData.types) == null ? void 0 : _a[0]) || 5121,
1226
- // @ts-expect-error
1227
- compressed: decoded.compressed
1186
+ compressed: decoded.compressed || false
1228
1187
  };
1229
1188
  }
1230
1189
  function getDepthStencilAttachmentWebGL(format) {
@@ -1234,11 +1193,11 @@ function getDepthStencilAttachmentWebGL(format) {
1234
1193
  }
1235
1194
  return info.attachment;
1236
1195
  }
1237
- function getWebGLPixelDataFormat(dataFormat, integer, normalized, format) {
1196
+ function getWebGLPixelDataFormat(channels, integer, normalized, format) {
1238
1197
  if (format === 6408 || format === 6407) {
1239
1198
  return format;
1240
1199
  }
1241
- switch (dataFormat) {
1200
+ switch (channels) {
1242
1201
  case "r":
1243
1202
  return integer && !normalized ? 36244 : 6403;
1244
1203
  case "rg":
@@ -1247,6 +1206,8 @@ function getWebGLPixelDataFormat(dataFormat, integer, normalized, format) {
1247
1206
  return integer && !normalized ? 36248 : 6407;
1248
1207
  case "rgba":
1249
1208
  return integer && !normalized ? 36249 : 6408;
1209
+ case "bgra":
1210
+ throw new Error("bgra pixels not supported by WebGL");
1250
1211
  default:
1251
1212
  return 6408;
1252
1213
  }
@@ -1268,7 +1229,6 @@ var WEBGL_FEATURES = {
1268
1229
  // 'timestamp-query' // GPUQueryType "timestamp-query"
1269
1230
  // "indirect-first-instance"
1270
1231
  // Textures are handled by getTextureFeatures()
1271
- // 'depth24unorm-stencil8' // GPUTextureFormat 'depth24unorm-stencil8'
1272
1232
  // 'depth32float-stencil8' // GPUTextureFormat 'depth32float-stencil8'
1273
1233
  // optional WebGL features
1274
1234
  "timer-query-webgl": "EXT_disjoint_timer_query_webgl2",
@@ -1462,17 +1422,18 @@ function withGLParameters(gl, parameters, func) {
1462
1422
  return func(gl);
1463
1423
  }
1464
1424
  const { nocatch = true } = parameters;
1465
- pushContextState(gl);
1425
+ const webglState = WebGLStateTracker.get(gl);
1426
+ webglState.push();
1466
1427
  setGLParameters(gl, parameters);
1467
1428
  let value;
1468
1429
  if (nocatch) {
1469
1430
  value = func(gl);
1470
- popContextState(gl);
1431
+ webglState.pop();
1471
1432
  } else {
1472
1433
  try {
1473
1434
  value = func(gl);
1474
1435
  } finally {
1475
- popContextState(gl);
1436
+ webglState.pop();
1476
1437
  }
1477
1438
  }
1478
1439
  return value;
@@ -1495,25 +1456,26 @@ function withDeviceAndGLParameters(device, parameters, glParameters, func) {
1495
1456
  return func(device);
1496
1457
  }
1497
1458
  const webglDevice = device;
1498
- pushContextState(webglDevice.gl);
1459
+ webglDevice.pushState();
1499
1460
  try {
1500
1461
  setDeviceParameters(device, parameters);
1501
1462
  setGLParameters(webglDevice.gl, glParameters);
1502
1463
  return func(device);
1503
1464
  } finally {
1504
- popContextState(webglDevice.gl);
1465
+ webglDevice.popState();
1505
1466
  }
1506
1467
  }
1507
1468
  function withDeviceParameters(device, parameters, func) {
1508
1469
  if (isObjectEmpty3(parameters)) {
1509
1470
  return func(device);
1510
1471
  }
1511
- pushContextState(device.gl);
1472
+ const webglDevice = device;
1473
+ webglDevice.pushState();
1512
1474
  try {
1513
1475
  setDeviceParameters(device, parameters);
1514
1476
  return func(device);
1515
1477
  } finally {
1516
- popContextState(device.gl);
1478
+ webglDevice.popState();
1517
1479
  }
1518
1480
  }
1519
1481
  function setDeviceParameters(device, parameters) {
@@ -1632,8 +1594,16 @@ function setDeviceParameters(device, parameters) {
1632
1594
  gl.stencilOpSeparate(1028, sfail, dpfail, dppass);
1633
1595
  gl.stencilOpSeparate(1029, sfail, dpfail, dppass);
1634
1596
  }
1597
+ switch (parameters.blend) {
1598
+ case true:
1599
+ gl.enable(3042);
1600
+ break;
1601
+ case false:
1602
+ gl.disable(3042);
1603
+ break;
1604
+ default:
1605
+ }
1635
1606
  if (parameters.blendColorOperation || parameters.blendAlphaOperation) {
1636
- gl.enable(3042);
1637
1607
  const colorEquation = convertBlendOperationToEquation("blendColorOperation", parameters.blendColorOperation || "add");
1638
1608
  const alphaEquation = convertBlendOperationToEquation("blendAlphaOperation", parameters.blendAlphaOperation || "add");
1639
1609
  gl.blendEquationSeparate(colorEquation, alphaEquation);
@@ -1858,7 +1828,7 @@ function copyCPUImageToMipLevel(gl, image, options) {
1858
1828
  const { dimension, width, height, depth = 0, level = 0 } = options;
1859
1829
  const { x = 0, y = 0, z = 0 } = options;
1860
1830
  const { glFormat, glType } = options;
1861
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
1831
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
1862
1832
  switch (dimension) {
1863
1833
  case "2d-array":
1864
1834
  case "3d":
@@ -1876,7 +1846,7 @@ function copyCPUDataToMipLevel(gl, typedArray, options) {
1876
1846
  const { dimension, width, height, depth = 0, level = 0, byteOffset = 0 } = options;
1877
1847
  const { x = 0, y = 0, z = 0 } = options;
1878
1848
  const { glFormat, glType, compressed } = options;
1879
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
1849
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
1880
1850
  switch (dimension) {
1881
1851
  case "2d-array":
1882
1852
  case "3d":
@@ -1915,7 +1885,7 @@ function getWebGLTextureTarget(dimension) {
1915
1885
  }
1916
1886
  throw new Error(dimension);
1917
1887
  }
1918
- function getCubeTargetWebGL(glTarget, dimension, level) {
1888
+ function getWebGLCubeFaceTarget(glTarget, dimension, level) {
1919
1889
  return dimension === "cube" ? 34069 + level : glTarget;
1920
1890
  }
1921
1891
 
@@ -2120,7 +2090,7 @@ var WEBGLTexture = class extends import_core7.Texture {
2120
2090
  throw new Error("setTexture1DData not supported in WebGL.");
2121
2091
  }
2122
2092
  /** Set a simple texture */
2123
- setTexture2DData(lodData, depth = 0, glTarget = this.glTarget) {
2093
+ setTexture2DData(lodData, depth = 0) {
2124
2094
  this.bind();
2125
2095
  const lodArray = normalizeTextureData(lodData, this);
2126
2096
  if (lodArray.length > 1 && this.props.mipmaps !== false) {
@@ -2141,7 +2111,9 @@ var WEBGLTexture = class extends import_core7.Texture {
2141
2111
  throw new Error(this.id);
2142
2112
  }
2143
2113
  if (ArrayBuffer.isView(data)) {
2114
+ this.bind();
2144
2115
  copyCPUDataToMipLevel(this.device.gl, data, this);
2116
+ this.unbind();
2145
2117
  }
2146
2118
  }
2147
2119
  /**
@@ -2154,6 +2126,9 @@ var WEBGLTexture = class extends import_core7.Texture {
2154
2126
  if (this.props.dimension !== "cube") {
2155
2127
  throw new Error(this.id);
2156
2128
  }
2129
+ for (const face of import_core7.Texture.CubeFaces) {
2130
+ this.setTextureCubeFaceData(data[face], face);
2131
+ }
2157
2132
  }
2158
2133
  /**
2159
2134
  * Sets an entire texture array
@@ -2176,8 +2151,8 @@ var WEBGLTexture = class extends import_core7.Texture {
2176
2151
  if (Array.isArray(lodData) && lodData.length > 1 && this.props.mipmaps !== false) {
2177
2152
  import_core7.log.warn(`${this.id} has mipmap and multiple LODs.`)();
2178
2153
  }
2179
- this.bind();
2180
- this.unbind();
2154
+ const faceDepth = import_core7.Texture.CubeFaces.indexOf(face);
2155
+ this.setTexture2DData(lodData, faceDepth);
2181
2156
  }
2182
2157
  // INTERNAL METHODS
2183
2158
  /** @todo update this method to accept LODs */
@@ -2311,16 +2286,17 @@ var WEBGLTexture = class extends import_core7.Texture {
2311
2286
  * Copy a region of data from a CPU memory buffer into this texture.
2312
2287
  * @todo - GLUnpackParameters parameters
2313
2288
  */
2314
- _setMipLevel(depth, level, textureData, offset = 0) {
2289
+ _setMipLevel(depth, level, textureData, glTarget = this.glTarget) {
2315
2290
  if (import_core7.Texture.isExternalImage(textureData)) {
2316
- copyCPUImageToMipLevel(this.device.gl, textureData, { ...this, depth, level });
2291
+ copyCPUImageToMipLevel(this.device.gl, textureData, { ...this, depth, level, glTarget });
2317
2292
  return;
2318
2293
  }
2319
2294
  if (this.isTextureLevelData(textureData)) {
2320
2295
  copyCPUDataToMipLevel(this.device.gl, textureData.data, {
2321
2296
  ...this,
2322
2297
  depth,
2323
- level
2298
+ level,
2299
+ glTarget
2324
2300
  });
2325
2301
  return;
2326
2302
  }
@@ -2453,7 +2429,7 @@ var WEBGLFramebuffer = class extends import_core8.Framebuffer {
2453
2429
  switch (texture.glTarget) {
2454
2430
  case 35866:
2455
2431
  case 32879:
2456
- gl.framebufferTextureLayer(36160, attachment, texture.glTarget, level, layer);
2432
+ gl.framebufferTextureLayer(36160, attachment, texture.handle, level, layer);
2457
2433
  break;
2458
2434
  case 34067:
2459
2435
  const face = mapIndexToCubeMapFace(layer);
@@ -2566,30 +2542,36 @@ async function loadScript(scriptUrl, scriptId) {
2566
2542
  }
2567
2543
 
2568
2544
  // dist/context/debug/spector.js
2569
- var DEFAULT_SPECTOR_PROPS = {
2570
- spector: import_core10.log.get("spector") || import_core10.log.get("inspect")
2571
- };
2572
- var SPECTOR_CDN_URL = "https://spectorcdn.babylonjs.com/spector.bundle.js";
2573
2545
  var LOG_LEVEL = 1;
2574
2546
  var spector = null;
2575
2547
  var initialized = false;
2548
+ var DEFAULT_SPECTOR_PROPS = {
2549
+ debugWithSpectorJS: import_core10.log.get("spector") || import_core10.log.get("spectorjs"),
2550
+ // https://github.com/BabylonJS/Spector.js#basic-usage
2551
+ // https://forum.babylonjs.com/t/spectorcdn-is-temporarily-off/48241
2552
+ // spectorUrl: 'https://spectorcdn.babylonjs.com/spector.bundle.js';
2553
+ spectorUrl: "https://cdn.jsdelivr.net/npm/spectorjs@0.9.30/dist/spector.bundle.js",
2554
+ gl: void 0
2555
+ };
2576
2556
  async function loadSpectorJS(props) {
2577
2557
  if (!globalThis.SPECTOR) {
2578
2558
  try {
2579
- await loadScript(SPECTOR_CDN_URL);
2559
+ await loadScript(props.spectorUrl || DEFAULT_SPECTOR_PROPS.spectorUrl);
2580
2560
  } catch (error) {
2581
2561
  import_core10.log.warn(String(error));
2582
2562
  }
2583
2563
  }
2584
2564
  }
2585
2565
  function initializeSpectorJS(props) {
2566
+ var _a;
2586
2567
  props = { ...DEFAULT_SPECTOR_PROPS, ...props };
2587
- if (!(props == null ? void 0 : props.spector)) {
2568
+ if (!props.debugWithSpectorJS) {
2588
2569
  return null;
2589
2570
  }
2590
- if (!spector && globalThis.SPECTOR) {
2591
- import_core10.log.probe(LOG_LEVEL, "SPECTOR found and initialized")();
2592
- spector = new globalThis.SPECTOR.Spector();
2571
+ if (!spector && globalThis.SPECTOR && !((_a = globalThis.luma) == null ? void 0 : _a.spector)) {
2572
+ import_core10.log.probe(LOG_LEVEL, "SPECTOR found and initialized. Start with `luma.spector.displayUI()`")();
2573
+ const { Spector } = globalThis.SPECTOR;
2574
+ spector = new Spector();
2593
2575
  if (globalThis.luma) {
2594
2576
  globalThis.luma.spector = spector;
2595
2577
  }
@@ -2608,11 +2590,11 @@ function initializeSpectorJS(props) {
2608
2590
  spector == null ? void 0 : spector.resultView.addCapture(capture);
2609
2591
  });
2610
2592
  }
2611
- if (props == null ? void 0 : props.canvas) {
2612
- if (typeof props.spector === "string" && props.spector !== props.canvas.id) {
2613
- return spector;
2614
- }
2615
- spector == null ? void 0 : spector.startCapture(props == null ? void 0 : props.canvas, 500);
2593
+ if (props.gl) {
2594
+ const gl = props.gl;
2595
+ const device = gl.device;
2596
+ spector == null ? void 0 : spector.startCapture(props.gl, 500);
2597
+ gl.device = device;
2616
2598
  new Promise((resolve) => setTimeout(resolve, 2e3)).then((_) => {
2617
2599
  import_core10.log.info("Spector capture stopped after 2 seconds")();
2618
2600
  spector == null ? void 0 : spector.stopCapture();
@@ -2713,6 +2695,14 @@ function onValidateGLFunc(props, functionName, functionArgs) {
2713
2695
  }
2714
2696
  }
2715
2697
 
2698
+ // dist/utils/uid.js
2699
+ var uidCounters = {};
2700
+ function uid(id = "id") {
2701
+ uidCounters[id] = uidCounters[id] || 1;
2702
+ const count = uidCounters[id]++;
2703
+ return `${id}-${count}`;
2704
+ }
2705
+
2716
2706
  // dist/adapter/resources/webgl-buffer.js
2717
2707
  var import_core12 = require("@luma.gl/core");
2718
2708
  var import_constants13 = require("@luma.gl/constants");
@@ -2917,8 +2907,8 @@ var WEBGLShader = class extends import_core13.Shader {
2917
2907
  return this.getCompilationInfoSync();
2918
2908
  }
2919
2909
  getCompilationInfoSync() {
2920
- const log9 = this.device.gl.getShaderInfoLog(this.handle);
2921
- return log9 ? parseShaderCompilerLog(log9) : [];
2910
+ const log10 = this.device.gl.getShaderInfoLog(this.handle);
2911
+ return log10 ? parseShaderCompilerLog(log10) : [];
2922
2912
  }
2923
2913
  getTranslatedSource() {
2924
2914
  const extensions = this.device.getExtension("WEBGL_debug_shaders");
@@ -2928,7 +2918,7 @@ var WEBGLShader = class extends import_core13.Shader {
2928
2918
  // PRIVATE METHODS
2929
2919
  /** Compile a shader and get compilation status */
2930
2920
  async _compile(source) {
2931
- const addGLSLVersion = (source2) => source2.startsWith("#version ") ? source2 : `#version 100
2921
+ const addGLSLVersion = (source2) => source2.startsWith("#version ") ? source2 : `#version 300 es
2932
2922
  ${source2}`;
2933
2923
  source = addGLSLVersion(source);
2934
2924
  const { gl } = this.device;
@@ -2992,14 +2982,25 @@ var WEBGLRenderPass = class extends import_core14.RenderPass {
2992
2982
  /** Parameters that should be applied before each draw call */
2993
2983
  glParameters;
2994
2984
  constructor(device, props) {
2985
+ var _a;
2995
2986
  super(device, props);
2996
2987
  this.device = device;
2997
- pushContextState(this.device.gl);
2998
- this.setParameters(this.props.parameters);
2988
+ let viewport;
2989
+ if (!((_a = props == null ? void 0 : props.parameters) == null ? void 0 : _a.viewport)) {
2990
+ if (props == null ? void 0 : props.framebuffer) {
2991
+ const { width, height } = props.framebuffer;
2992
+ viewport = [0, 0, width, height];
2993
+ } else {
2994
+ const [width, height] = device.getCanvasContext().getDrawingBufferSize();
2995
+ viewport = [0, 0, width, height];
2996
+ }
2997
+ }
2998
+ this.device.pushState();
2999
+ this.setParameters({ viewport, ...this.props.parameters });
2999
3000
  this.clear();
3000
3001
  }
3001
3002
  end() {
3002
- popContextState(this.device.gl);
3003
+ this.device.popState();
3003
3004
  }
3004
3005
  pushDebugGroup(groupLabel) {
3005
3006
  }
@@ -3767,7 +3768,7 @@ var WEBGLRenderPipeline = class extends import_core15.RenderPipeline {
3767
3768
  if (!binding) {
3768
3769
  const validBindings = this.shaderLayout.bindings.map((binding2) => `"${binding2.name}"`).join(", ");
3769
3770
  if (!(options == null ? void 0 : options.disableWarnings)) {
3770
- import_core15.log.warn(`Unknown binding "${name}" in render pipeline "${this.id}", expected one of ${validBindings}`)();
3771
+ import_core15.log.warn(`No binding "${name}" in render pipeline "${this.id}", expected one of ${validBindings}`, value)();
3771
3772
  }
3772
3773
  continue;
3773
3774
  }
@@ -4364,6 +4365,7 @@ var WEBGLVertexArray = class extends import_core18.VertexArray {
4364
4365
  } else {
4365
4366
  this.device.gl.vertexAttribPointer(location, size, type, normalized, stride, offset);
4366
4367
  }
4368
+ this.device.gl.bindBuffer(34962, null);
4367
4369
  this.device.gl.enableVertexAttribArray(location);
4368
4370
  this.device.gl.vertexAttribDivisor(location, divisor || 0);
4369
4371
  this.attributes[location] = buffer;
@@ -4542,7 +4544,7 @@ var WEBGLTransformFeedback = class extends import_core19.TransformFeedback {
4542
4544
  }
4543
4545
  end() {
4544
4546
  this.gl.endTransformFeedback();
4545
- if (!this.bindOnUse) {
4547
+ if (this.bindOnUse) {
4546
4548
  this._unbindBuffers();
4547
4549
  }
4548
4550
  this.gl.bindTransformFeedback(36386, null);
@@ -4820,11 +4822,10 @@ function glTypeToBytes(type) {
4820
4822
 
4821
4823
  // dist/classic/copy-and-blit.js
4822
4824
  function readPixelsToArray(source, options) {
4823
- var _a, _b;
4825
+ var _a, _b, _c, _d, _e, _f;
4824
4826
  const {
4825
4827
  sourceX = 0,
4826
4828
  sourceY = 0,
4827
- sourceFormat = 6408,
4828
4829
  sourceAttachment = 36064
4829
4830
  // TODO - support gl.readBuffer
4830
4831
  } = options || {};
@@ -4833,15 +4834,19 @@ function readPixelsToArray(source, options) {
4833
4834
  // following parameters are auto deduced if not provided
4834
4835
  sourceWidth,
4835
4836
  sourceHeight,
4837
+ sourceDepth,
4838
+ sourceFormat,
4836
4839
  sourceType
4837
4840
  } = options || {};
4838
4841
  const { framebuffer, deleteFramebuffer } = getFramebuffer2(source);
4839
4842
  const { gl, handle } = framebuffer;
4840
- sourceWidth = sourceWidth || framebuffer.width;
4841
- sourceHeight = sourceHeight || framebuffer.height;
4842
4843
  const attachment = sourceAttachment - 36064;
4843
- sourceType = sourceType || ((_b = (_a = framebuffer.colorAttachments[attachment]) == null ? void 0 : _a.texture) == null ? void 0 : _b.glType) || 5121;
4844
- target = getPixelArray(target, sourceType, sourceFormat, sourceWidth, sourceHeight);
4844
+ sourceWidth ||= framebuffer.width;
4845
+ sourceHeight ||= framebuffer.height;
4846
+ sourceDepth = ((_b = (_a = framebuffer.colorAttachments[attachment]) == null ? void 0 : _a.texture) == null ? void 0 : _b.depth) || 1;
4847
+ sourceFormat ||= ((_d = (_c = framebuffer.colorAttachments[attachment]) == null ? void 0 : _c.texture) == null ? void 0 : _d.glFormat) || 6408;
4848
+ sourceType ||= ((_f = (_e = framebuffer.colorAttachments[attachment]) == null ? void 0 : _e.texture) == null ? void 0 : _f.glType) || 5121;
4849
+ target = getPixelArray(target, sourceType, sourceFormat, sourceWidth, sourceHeight, sourceDepth);
4845
4850
  sourceType = sourceType || getGLTypeFromTypedArray(target);
4846
4851
  const prevHandle = gl.bindFramebuffer(36160, handle);
4847
4852
  gl.readPixels(sourceX, sourceY, sourceWidth, sourceHeight, sourceFormat, sourceType, target);
@@ -4898,7 +4903,7 @@ function toFramebuffer(texture, props) {
4898
4903
  });
4899
4904
  return framebuffer;
4900
4905
  }
4901
- function getPixelArray(pixelArray, type, format, width, height) {
4906
+ function getPixelArray(pixelArray, type, format, width, height, depth) {
4902
4907
  if (pixelArray) {
4903
4908
  return pixelArray;
4904
4909
  }
@@ -4944,8 +4949,10 @@ function clear(device, options) {
4944
4949
  }
4945
4950
 
4946
4951
  // dist/adapter/webgl-device.js
4947
- var LOG_LEVEL2 = 1;
4948
- var _WebGLDevice = class extends import_core22.Device {
4952
+ var WebGLDevice = class extends import_core22.Device {
4953
+ //
4954
+ // Public `Device` API
4955
+ //
4949
4956
  /** type of this device */
4950
4957
  type = "webgl";
4951
4958
  /** The underlying WebGL context */
@@ -4956,68 +4963,22 @@ var _WebGLDevice = class extends import_core22.Device {
4956
4963
  canvasContext;
4957
4964
  lost;
4958
4965
  _resolveContextLost;
4959
- //
4960
- // Static methods, expected to be present by `luma.createDevice()`
4961
- //
4962
- /** Check if WebGL 2 is available */
4963
- static isSupported() {
4964
- return typeof WebGL2RenderingContext !== "undefined";
4965
- }
4966
- /**
4967
- * Get a device instance from a GL context
4968
- * Creates and instruments the device if not already created
4969
- * @param gl
4970
- * @returns
4971
- */
4972
- static attach(gl) {
4973
- if (gl instanceof _WebGLDevice) {
4974
- return gl;
4975
- }
4976
- if ((gl == null ? void 0 : gl.device) instanceof import_core22.Device) {
4977
- return gl.device;
4978
- }
4979
- if (!isWebGL(gl)) {
4980
- throw new Error("Invalid WebGL2RenderingContext");
4981
- }
4982
- return new _WebGLDevice({ gl });
4983
- }
4984
- static async create(props = {}) {
4985
- var _a;
4986
- import_core22.log.groupCollapsed(LOG_LEVEL2, "WebGLDevice created")();
4987
- const promises = [];
4988
- if (props.debug) {
4989
- promises.push(loadWebGLDeveloperTools());
4990
- }
4991
- if (props.spector) {
4992
- promises.push(loadSpectorJS());
4993
- }
4994
- if (typeof props.canvas === "string") {
4995
- promises.push(import_core22.CanvasContext.pageLoaded);
4996
- }
4997
- const results = await Promise.allSettled(promises);
4998
- for (const result of results) {
4999
- if (result.status === "rejected") {
5000
- import_core22.log.error(`Failed to initialize debug libraries ${result.reason}`)();
5001
- }
5002
- }
5003
- import_core22.log.probe(LOG_LEVEL2 + 1, "DOM is loaded")();
5004
- if ((_a = props.gl) == null ? void 0 : _a.device) {
5005
- import_core22.log.warn("reattaching existing device")();
5006
- return _WebGLDevice.attach(props.gl);
5007
- }
5008
- const device = new _WebGLDevice(props);
5009
- const message2 = `Created ${device.type}${device.debug ? " debug" : ""} context: ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContext.id}`;
5010
- import_core22.log.probe(LOG_LEVEL2, message2)();
5011
- import_core22.log.table(LOG_LEVEL2, device.info)();
5012
- import_core22.log.groupEnd(LOG_LEVEL2)();
5013
- return device;
5014
- }
4966
+ /** WebGL2 context. */
4967
+ gl;
4968
+ debug = false;
4969
+ /** State used by luma.gl classes: TODO - move to canvasContext*/
4970
+ _canvasSizeInfo = { clientWidth: 0, clientHeight: 0, devicePixelRatio: 1 };
4971
+ /** State used by luma.gl classes - TODO - not used? */
4972
+ _extensions = {};
4973
+ _polyfilled = false;
4974
+ /** Instance of Spector.js (if initialized) */
4975
+ spectorJS;
5015
4976
  //
5016
4977
  // Public API
5017
4978
  //
5018
4979
  constructor(props) {
5019
4980
  var _a, _b;
5020
- super({ ...props, id: props.id || "webgl-device" });
4981
+ super({ ...props, id: props.id || uid("webgl-device") });
5021
4982
  const device = (_a = props.gl) == null ? void 0 : _a.device;
5022
4983
  if (device) {
5023
4984
  throw new Error(`WebGL context already attached to device ${device.id}`);
@@ -5027,8 +4988,7 @@ var _WebGLDevice = class extends import_core22.Device {
5027
4988
  this.lost = new Promise((resolve) => {
5028
4989
  this._resolveContextLost = resolve;
5029
4990
  });
5030
- let gl = props.gl || null;
5031
- gl ||= createBrowserContext(this.canvasContext.canvas, {
4991
+ this.handle = createBrowserContext(this.canvasContext.canvas, {
5032
4992
  ...props,
5033
4993
  onContextLost: (event) => {
5034
4994
  var _a2;
@@ -5038,11 +4998,11 @@ var _WebGLDevice = class extends import_core22.Device {
5038
4998
  });
5039
4999
  }
5040
5000
  });
5041
- if (!gl) {
5001
+ this.gl = this.handle;
5002
+ if (!this.handle) {
5042
5003
  throw new Error("WebGL context creation failed");
5043
5004
  }
5044
- this.handle = gl;
5045
- this.gl = gl;
5005
+ this.spectorJS = initializeSpectorJS({ ...this.props, gl: this.handle });
5046
5006
  this.gl.device = this;
5047
5007
  this.gl._version = 2;
5048
5008
  this.info = getDeviceInfo(this.gl, this._extensions);
@@ -5052,21 +5012,16 @@ var _WebGLDevice = class extends import_core22.Device {
5052
5012
  this.features.initializeFeatures();
5053
5013
  }
5054
5014
  this.canvasContext.resize();
5055
- const { enable: enable2 = true, copyState = false } = props;
5056
- trackContextState(this.gl, {
5057
- enable: enable2,
5058
- copyState,
5015
+ const glState = new WebGLStateTracker(this.gl, {
5059
5016
  log: (...args) => import_core22.log.log(1, ...args)()
5060
5017
  });
5018
+ glState.trackState(this.gl, { copyState: false });
5061
5019
  if (props.debug) {
5062
5020
  this.gl = makeDebugContext(this.gl, { ...props, throwOnError: true });
5063
5021
  this.debug = true;
5064
5022
  import_core22.log.level = Math.max(import_core22.log.level, 1);
5065
5023
  import_core22.log.warn("WebGL debug mode activated. Performance reduced.")();
5066
5024
  }
5067
- if (props.spector) {
5068
- this.spectorJS = initializeSpectorJS({ ...this.props, canvas: this.handle.canvas });
5069
- }
5070
5025
  }
5071
5026
  /**
5072
5027
  * Destroys the context
@@ -5077,9 +5032,6 @@ var _WebGLDevice = class extends import_core22.Device {
5077
5032
  get isLost() {
5078
5033
  return this.gl.isContextLost();
5079
5034
  }
5080
- getSize() {
5081
- return [this.gl.drawingBufferWidth, this.gl.drawingBufferHeight];
5082
- }
5083
5035
  isTextureFormatSupported(format) {
5084
5036
  return isTextureFormatSupported(this.gl, format, this._extensions);
5085
5037
  }
@@ -5178,16 +5130,6 @@ var _WebGLDevice = class extends import_core22.Device {
5178
5130
  //
5179
5131
  // WebGL-only API (not part of `Device` API)
5180
5132
  //
5181
- /** WebGL2 context. */
5182
- gl;
5183
- debug = false;
5184
- /** State used by luma.gl classes: TODO - move to canvasContext*/
5185
- _canvasSizeInfo = { clientWidth: 0, clientHeight: 0, devicePixelRatio: 1 };
5186
- /** State used by luma.gl classes - TODO - not used? */
5187
- _extensions = {};
5188
- _polyfilled = false;
5189
- /** Instance of Spector.js (if initialized) */
5190
- spectorJS;
5191
5133
  /**
5192
5134
  * Triggers device (or WebGL context) loss.
5193
5135
  * @note primarily intended for testing how application reacts to device loss
@@ -5209,11 +5151,13 @@ var _WebGLDevice = class extends import_core22.Device {
5209
5151
  }
5210
5152
  /** Save current WebGL context state onto an internal stack */
5211
5153
  pushState() {
5212
- pushContextState(this.gl);
5154
+ const webglState = WebGLStateTracker.get(this.gl);
5155
+ webglState.push();
5213
5156
  }
5214
5157
  /** Restores previously saved context state */
5215
5158
  popState() {
5216
- popContextState(this.gl);
5159
+ const webglState = WebGLStateTracker.get(this.gl);
5160
+ webglState.pop();
5217
5161
  }
5218
5162
  /**
5219
5163
  * Storing data on a special field on WebGLObjects makes that data visible in SPECTOR chrome debug extension
@@ -5273,18 +5217,6 @@ var _WebGLDevice = class extends import_core22.Device {
5273
5217
  return this._extensions;
5274
5218
  }
5275
5219
  };
5276
- var WebGLDevice = _WebGLDevice;
5277
- //
5278
- // Public `Device` API
5279
- //
5280
- /** type of this device */
5281
- __publicField(WebGLDevice, "type", "webgl");
5282
- function isWebGL(gl) {
5283
- if (typeof WebGL2RenderingContext !== "undefined" && gl instanceof WebGL2RenderingContext) {
5284
- return true;
5285
- }
5286
- return Boolean(gl && Number.isFinite(gl._version));
5287
- }
5288
5220
  function setConstantFloatArray(device, location, array) {
5289
5221
  switch (array.length) {
5290
5222
  case 1:
@@ -5319,4 +5251,180 @@ function compareConstantArrayValues2(v1, v2) {
5319
5251
  }
5320
5252
  return true;
5321
5253
  }
5254
+
5255
+ // dist/context/polyfills/polyfill-webgl1-extensions.js
5256
+ var import_constants29 = require("@luma.gl/constants");
5257
+ var WEBGL1_STATIC_EXTENSIONS = {
5258
+ WEBGL_depth_texture: {
5259
+ UNSIGNED_INT_24_8_WEBGL: 34042
5260
+ },
5261
+ OES_element_index_uint: {},
5262
+ OES_texture_float: {},
5263
+ OES_texture_half_float: {
5264
+ // @ts-expect-error different numbers?
5265
+ HALF_FLOAT_OES: 5131
5266
+ },
5267
+ EXT_color_buffer_float: {},
5268
+ OES_standard_derivatives: {
5269
+ FRAGMENT_SHADER_DERIVATIVE_HINT_OES: 35723
5270
+ },
5271
+ EXT_frag_depth: {},
5272
+ EXT_blend_minmax: {
5273
+ MIN_EXT: 32775,
5274
+ MAX_EXT: 32776
5275
+ },
5276
+ EXT_shader_texture_lod: {}
5277
+ };
5278
+ var getWEBGL_draw_buffers = (gl) => ({
5279
+ drawBuffersWEBGL(buffers) {
5280
+ return gl.drawBuffers(buffers);
5281
+ },
5282
+ COLOR_ATTACHMENT0_WEBGL: 36064,
5283
+ COLOR_ATTACHMENT1_WEBGL: 36065,
5284
+ COLOR_ATTACHMENT2_WEBGL: 36066,
5285
+ COLOR_ATTACHMENT3_WEBGL: 36067
5286
+ });
5287
+ var getOES_vertex_array_object = (gl) => ({
5288
+ VERTEX_ARRAY_BINDING_OES: 34229,
5289
+ createVertexArrayOES() {
5290
+ return gl.createVertexArray();
5291
+ },
5292
+ deleteVertexArrayOES(vertexArray) {
5293
+ return gl.deleteVertexArray(vertexArray);
5294
+ },
5295
+ isVertexArrayOES(vertexArray) {
5296
+ return gl.isVertexArray(vertexArray);
5297
+ },
5298
+ bindVertexArrayOES(vertexArray) {
5299
+ return gl.bindVertexArray(vertexArray);
5300
+ }
5301
+ });
5302
+ var getANGLE_instanced_arrays = (gl) => ({
5303
+ VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE: 35070,
5304
+ drawArraysInstancedANGLE(...args) {
5305
+ return gl.drawArraysInstanced(...args);
5306
+ },
5307
+ drawElementsInstancedANGLE(...args) {
5308
+ return gl.drawElementsInstanced(...args);
5309
+ },
5310
+ vertexAttribDivisorANGLE(...args) {
5311
+ return gl.vertexAttribDivisor(...args);
5312
+ }
5313
+ });
5314
+ function enforceWebGL2(enforce = true) {
5315
+ const prototype = HTMLCanvasElement.prototype;
5316
+ if (!enforce && prototype.originalGetContext) {
5317
+ prototype.getContext = prototype.originalGetContext;
5318
+ prototype.originalGetContext = void 0;
5319
+ return;
5320
+ }
5321
+ prototype.originalGetContext = prototype.getContext;
5322
+ prototype.getContext = function(contextId, options) {
5323
+ if (contextId === "webgl" || contextId === "experimental-webgl") {
5324
+ const context = this.originalGetContext("webgl2", options);
5325
+ if (context instanceof HTMLElement) {
5326
+ polyfillWebGL1Extensions(context);
5327
+ }
5328
+ return context;
5329
+ }
5330
+ return this.originalGetContext(contextId, options);
5331
+ };
5332
+ }
5333
+ function polyfillWebGL1Extensions(gl) {
5334
+ gl.getExtension("EXT_color_buffer_float");
5335
+ const boundExtensions = {
5336
+ ...WEBGL1_STATIC_EXTENSIONS,
5337
+ WEBGL_disjoint_timer_query: gl.getExtension("EXT_disjoint_timer_query_webgl2"),
5338
+ WEBGL_draw_buffers: getWEBGL_draw_buffers(gl),
5339
+ OES_vertex_array_object: getOES_vertex_array_object(gl),
5340
+ ANGLE_instanced_arrays: getANGLE_instanced_arrays(gl)
5341
+ };
5342
+ const originalGetExtension = gl.getExtension;
5343
+ gl.getExtension = function(extensionName) {
5344
+ const ext = originalGetExtension.call(gl, extensionName);
5345
+ if (ext) {
5346
+ return ext;
5347
+ }
5348
+ if (extensionName in boundExtensions) {
5349
+ return boundExtensions[extensionName];
5350
+ }
5351
+ return null;
5352
+ };
5353
+ const originalGetSupportedExtensions = gl.getSupportedExtensions;
5354
+ gl.getSupportedExtensions = function() {
5355
+ const extensions = originalGetSupportedExtensions.apply(gl) || [];
5356
+ return extensions == null ? void 0 : extensions.concat(Object.keys(boundExtensions));
5357
+ };
5358
+ }
5359
+
5360
+ // dist/adapter/webgl-adapter.js
5361
+ var LOG_LEVEL2 = 1;
5362
+ var WebGLAdapter = class extends import_core23.Adapter {
5363
+ /** type of device's created by this adapter */
5364
+ type = "webgl";
5365
+ constructor() {
5366
+ super();
5367
+ import_core23.Device.defaultProps = { ...import_core23.Device.defaultProps, ...DEFAULT_SPECTOR_PROPS };
5368
+ WebGLDevice.adapter = this;
5369
+ }
5370
+ /** Check if WebGL 2 is available */
5371
+ isSupported() {
5372
+ return typeof WebGL2RenderingContext !== "undefined";
5373
+ }
5374
+ /** Force any created WebGL contexts to be WebGL2 contexts, polyfilled with WebGL1 extensions */
5375
+ enforceWebGL2(enable2) {
5376
+ enforceWebGL2(enable2);
5377
+ }
5378
+ /**
5379
+ * Get a device instance from a GL context
5380
+ * Creates and instruments the device if not already created
5381
+ * @param gl
5382
+ * @returns
5383
+ */
5384
+ async attach(gl) {
5385
+ if (gl instanceof WebGLDevice) {
5386
+ return gl;
5387
+ }
5388
+ if ((gl == null ? void 0 : gl.device) instanceof import_core23.Device) {
5389
+ return gl.device;
5390
+ }
5391
+ if (!isWebGL(gl)) {
5392
+ throw new Error("Invalid WebGL2RenderingContext");
5393
+ }
5394
+ return new WebGLDevice({ gl });
5395
+ }
5396
+ async create(props = {}) {
5397
+ import_core23.log.groupCollapsed(LOG_LEVEL2, "WebGLDevice created")();
5398
+ const promises = [];
5399
+ if (props.debug) {
5400
+ promises.push(loadWebGLDeveloperTools());
5401
+ }
5402
+ if (props.debugWithSpectorJS) {
5403
+ promises.push(loadSpectorJS(props));
5404
+ }
5405
+ if (typeof props.canvas === "string") {
5406
+ promises.push(import_core23.CanvasContext.pageLoaded);
5407
+ }
5408
+ const results = await Promise.allSettled(promises);
5409
+ for (const result of results) {
5410
+ if (result.status === "rejected") {
5411
+ import_core23.log.error(`Failed to initialize debug libraries ${result.reason}`)();
5412
+ }
5413
+ }
5414
+ import_core23.log.probe(LOG_LEVEL2 + 1, "DOM is loaded")();
5415
+ const device = new WebGLDevice(props);
5416
+ const message2 = `Created ${device.type}${device.debug ? " debug" : ""} context: ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContext.id}`;
5417
+ import_core23.log.probe(LOG_LEVEL2, message2)();
5418
+ import_core23.log.table(LOG_LEVEL2, device.info)();
5419
+ import_core23.log.groupEnd(LOG_LEVEL2)();
5420
+ return device;
5421
+ }
5422
+ };
5423
+ function isWebGL(gl) {
5424
+ if (typeof WebGL2RenderingContext !== "undefined" && gl instanceof WebGL2RenderingContext) {
5425
+ return true;
5426
+ }
5427
+ return Boolean(gl && Number.isFinite(gl._version));
5428
+ }
5429
+ var webgl2Adapter = new WebGLAdapter();
5322
5430
  //# sourceMappingURL=index.cjs.map