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

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 (82) 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 +21 -3
  18. package/dist/adapter/resources/webgl-texture.d.ts.map +1 -1
  19. package/dist/adapter/resources/webgl-texture.js +23 -29
  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 +352 -236
  47. package/dist/dist.min.js +2 -2
  48. package/dist/index.cjs +355 -238
  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/split-uniforms-and-bindings.d.ts +1 -1
  54. package/dist/utils/split-uniforms-and-bindings.d.ts.map +1 -1
  55. package/dist/utils/uid.d.ts +7 -0
  56. package/dist/utils/uid.d.ts.map +1 -0
  57. package/dist/utils/uid.js +14 -0
  58. package/package.json +3 -3
  59. package/src/adapter/converters/device-parameters.ts +18 -12
  60. package/src/adapter/converters/texture-formats.ts +12 -20
  61. package/src/adapter/device-helpers/webgl-device-features.ts +0 -1
  62. package/src/adapter/helpers/webgl-texture-utils.ts +12 -9
  63. package/src/adapter/resources/webgl-framebuffer.ts +1 -1
  64. package/src/adapter/resources/webgl-render-pass.ts +17 -4
  65. package/src/adapter/resources/webgl-render-pipeline.ts +2 -1
  66. package/src/adapter/resources/webgl-shader.ts +1 -1
  67. package/src/adapter/resources/webgl-texture.ts +42 -29
  68. package/src/adapter/resources/webgl-transform-feedback.ts +1 -1
  69. package/src/adapter/resources/webgl-vertex-array.ts +3 -0
  70. package/src/adapter/webgl-adapter.ts +113 -0
  71. package/src/adapter/webgl-device.ts +32 -135
  72. package/src/classic/copy-and-blit.ts +14 -9
  73. package/src/context/debug/spector-types.ts +1154 -0
  74. package/src/context/debug/spector.ts +38 -29
  75. package/src/context/polyfills/polyfill-webgl1-extensions.ts +202 -0
  76. package/src/context/state-tracker/{track-context-state.ts → webgl-state-tracker.ts} +55 -94
  77. package/src/context/state-tracker/with-parameters.ts +5 -4
  78. package/src/index.ts +5 -7
  79. package/src/utils/split-uniforms-and-bindings.ts +3 -3
  80. package/src/utils/uid.ts +16 -0
  81. package/dist/context/state-tracker/track-context-state.d.ts +0 -22
  82. package/dist/context/state-tracker/track-context-state.d.ts.map +0 -1
package/dist/dist.dev.js CHANGED
@@ -11,7 +11,6 @@ var __exports__ = (() => {
11
11
  var __getOwnPropNames = Object.getOwnPropertyNames;
12
12
  var __getProtoOf = Object.getPrototypeOf;
13
13
  var __hasOwnProp = Object.prototype.hasOwnProperty;
14
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
15
14
  var __commonJS = (cb, mod) => function __require() {
16
15
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
17
16
  };
@@ -37,10 +36,6 @@ var __exports__ = (() => {
37
36
  mod
38
37
  ));
39
38
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
40
- var __publicField = (obj, key, value) => {
41
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
42
- return value;
43
- };
44
39
 
45
40
  // external-global-plugin:@luma.gl/core
46
41
  var require_core = __commonJS({
@@ -65,20 +60,22 @@ var __exports__ = (() => {
65
60
  WEBGLVertexArray: () => WEBGLVertexArray,
66
61
  WebGLCanvasContext: () => WebGLCanvasContext,
67
62
  WebGLDevice: () => WebGLDevice,
63
+ WebGLStateTracker: () => WebGLStateTracker,
68
64
  _TEXTURE_FORMATS: () => TEXTURE_FORMATS,
69
65
  getGLParameters: () => getGLParameters,
70
66
  getShaderLayout: () => getShaderLayout,
71
- popContextState: () => popContextState,
72
- pushContextState: () => pushContextState,
73
67
  resetGLParameters: () => resetGLParameters,
74
68
  setDeviceParameters: () => setDeviceParameters,
75
69
  setGLParameters: () => setGLParameters,
76
- trackContextState: () => trackContextState,
70
+ webgl2Adapter: () => webgl2Adapter,
77
71
  withDeviceParameters: () => withDeviceParameters,
78
72
  withGLParameters: () => withGLParameters
79
73
  });
80
74
  __reExport(bundle_exports, __toESM(require_core(), 1));
81
75
 
76
+ // src/adapter/webgl-adapter.ts
77
+ var import_core23 = __toESM(require_core(), 1);
78
+
82
79
  // src/adapter/webgl-device.ts
83
80
  var import_core22 = __toESM(require_core(), 1);
84
81
 
@@ -1309,24 +1306,22 @@ var __exports__ = (() => {
1309
1306
  return false;
1310
1307
  }
1311
1308
 
1312
- // src/context/state-tracker/track-context-state.ts
1313
- var GLState = class {
1309
+ // src/context/state-tracker/webgl-state-tracker.ts
1310
+ var WebGLStateTracker = class {
1311
+ static get(gl) {
1312
+ return gl.state;
1313
+ }
1314
1314
  gl;
1315
1315
  program = null;
1316
1316
  stateStack = [];
1317
1317
  enable = true;
1318
- cache;
1318
+ cache = null;
1319
1319
  log;
1320
- constructor(gl, {
1321
- copyState = false,
1322
- // Copy cache from params (slow) or initialize from WebGL defaults (fast)
1323
- log: log9 = () => {
1324
- }
1325
- // Logging function, called when gl parameter change calls are actually issued
1326
- } = {}) {
1320
+ initialized = false;
1321
+ constructor(gl, props) {
1327
1322
  this.gl = gl;
1328
- this.cache = copyState ? getGLParameters(gl) : Object.assign({}, GL_PARAMETER_DEFAULTS);
1329
- this.log = log9;
1323
+ this.log = props?.log || (() => {
1324
+ });
1330
1325
  this._updateCache = this._updateCache.bind(this);
1331
1326
  Object.seal(this);
1332
1327
  }
@@ -1338,6 +1333,29 @@ var __exports__ = (() => {
1338
1333
  setGLParameters(this.gl, oldValues);
1339
1334
  this.stateStack.pop();
1340
1335
  }
1336
+ /**
1337
+ * Initialize WebGL state caching on a context
1338
+ * can be called multiple times to enable/disable
1339
+ *
1340
+ * @note After calling this function, context state will be cached
1341
+ * .push() and .pop() will be available for saving,
1342
+ * temporarily modifying, and then restoring state.
1343
+ */
1344
+ trackState(gl, options) {
1345
+ this.cache = options.copyState ? getGLParameters(gl) : Object.assign({}, GL_PARAMETER_DEFAULTS);
1346
+ if (this.initialized) {
1347
+ throw new Error("WebGLStateTracker");
1348
+ }
1349
+ this.initialized = true;
1350
+ this.gl.state = this;
1351
+ installProgramSpy(gl);
1352
+ for (const key in GL_HOOKED_SETTERS) {
1353
+ const setter = GL_HOOKED_SETTERS[key];
1354
+ installSetterSpy(gl, key, setter);
1355
+ }
1356
+ installGetterOverride(gl, "getParameter");
1357
+ installGetterOverride(gl, "isEnabled");
1358
+ }
1341
1359
  /**
1342
1360
  // interceptor for context set functions - update our cache and our stack
1343
1361
  // values (Object) - the key values for this setter
@@ -1363,44 +1381,13 @@ var __exports__ = (() => {
1363
1381
  return { valueChanged, oldValue };
1364
1382
  }
1365
1383
  };
1366
- function getContextState(gl) {
1367
- return gl.state;
1368
- }
1369
- function trackContextState(gl, options) {
1370
- const { enable: enable2 = true, copyState } = options || {};
1371
- if (!gl.state) {
1372
- gl.state = new GLState(gl, { copyState });
1373
- installProgramSpy(gl);
1374
- for (const key in GL_HOOKED_SETTERS) {
1375
- const setter = GL_HOOKED_SETTERS[key];
1376
- installSetterSpy(gl, key, setter);
1377
- }
1378
- installGetterOverride(gl, "getParameter");
1379
- installGetterOverride(gl, "isEnabled");
1380
- }
1381
- const glState = getContextState(gl);
1382
- glState.enable = enable2;
1383
- return gl;
1384
- }
1385
- function pushContextState(gl) {
1386
- let glState = getContextState(gl);
1387
- if (!glState) {
1388
- trackContextState(gl, { copyState: false });
1389
- glState = getContextState(gl);
1390
- }
1391
- glState.push();
1392
- }
1393
- function popContextState(gl) {
1394
- const glState = getContextState(gl);
1395
- glState.pop();
1396
- }
1397
1384
  function installGetterOverride(gl, functionName) {
1398
1385
  const originalGetterFunc = gl[functionName].bind(gl);
1399
1386
  gl[functionName] = function get(pname) {
1400
1387
  if (pname === void 0 || NON_CACHE_PARAMETERS.has(pname)) {
1401
1388
  return originalGetterFunc(pname);
1402
1389
  }
1403
- const glState = getContextState(gl);
1390
+ const glState = WebGLStateTracker.get(gl);
1404
1391
  if (!(pname in glState.cache)) {
1405
1392
  glState.cache[pname] = originalGetterFunc(pname);
1406
1393
  }
@@ -1423,7 +1410,7 @@ var __exports__ = (() => {
1423
1410
  }
1424
1411
  const originalSetterFunc = gl[functionName].bind(gl);
1425
1412
  gl[functionName] = function set(...params) {
1426
- const glState = getContextState(gl);
1413
+ const glState = WebGLStateTracker.get(gl);
1427
1414
  const { valueChanged, oldValue } = setter(glState._updateCache, ...params);
1428
1415
  if (valueChanged) {
1429
1416
  originalSetterFunc(...params);
@@ -1438,7 +1425,7 @@ var __exports__ = (() => {
1438
1425
  function installProgramSpy(gl) {
1439
1426
  const originalUseProgram = gl.useProgram.bind(gl);
1440
1427
  gl.useProgram = function useProgramLuma(handle) {
1441
- const glState = getContextState(gl);
1428
+ const glState = WebGLStateTracker.get(gl);
1442
1429
  if (glState.program !== handle) {
1443
1430
  originalUseProgram(handle);
1444
1431
  glState.program = handle;
@@ -1655,26 +1642,6 @@ var __exports__ = (() => {
1655
1642
  return textureExtensions.every((extension) => getWebGLExtension(gl, extension, extensions));
1656
1643
  }
1657
1644
  var TEXTURE_FORMATS = {
1658
- // Unsized formats that leave the precision up to the driver. TODO - Fix bpp constants
1659
- "rgb8unorm-unsized": {
1660
- gl: 6407 /* RGB */,
1661
- b: 4,
1662
- c: 2,
1663
- bpp: 4,
1664
- dataFormat: 6407 /* RGB */,
1665
- types: [5121 /* UNSIGNED_BYTE */, 33635 /* UNSIGNED_SHORT_5_6_5 */]
1666
- },
1667
- "rgba8unorm-unsized": {
1668
- gl: 6408 /* RGBA */,
1669
- b: 4,
1670
- c: 2,
1671
- bpp: 4,
1672
- dataFormat: 6408 /* RGBA */,
1673
- types: [5121 /* UNSIGNED_BYTE */, 32819 /* UNSIGNED_SHORT_4_4_4_4 */, 32820 /* UNSIGNED_SHORT_5_5_5_1 */]
1674
- },
1675
- // 'r8unorm-unsized': {gl: GL.LUMINANCE, b: 4, c: 2, bpp: 4},
1676
- // 'rgb8unorm-srgb-unsized': {gl: GL.SRGB_EXT, b: 4, c: 2, bpp: 4, gl1Ext: SRGB},
1677
- // 'rgba8unorm-srgb-unsized': {gl: GL.SRGB_ALPHA_EXT, b: 4, c: 2, bpp: 4, gl1Ext: SRGB},
1678
1645
  // 8-bit formats
1679
1646
  "r8unorm": { gl: 33321 /* R8 */, b: 1, c: 1, rb: true },
1680
1647
  "r8snorm": { gl: 36756 /* R8_SNORM */, b: 1, c: 1, render: snorm8_renderable },
@@ -1789,17 +1756,6 @@ var __exports__ = (() => {
1789
1756
  dataFormat: 34041 /* DEPTH_STENCIL */,
1790
1757
  types: [34042 /* UNSIGNED_INT_24_8 */]
1791
1758
  },
1792
- // "depth24unorm-stencil8" feature
1793
- "depth24unorm-stencil8": {
1794
- gl: 35056 /* DEPTH24_STENCIL8 */,
1795
- b: 4,
1796
- c: 2,
1797
- p: 1,
1798
- attachment: 33306 /* DEPTH_STENCIL_ATTACHMENT */,
1799
- dataFormat: 34041 /* DEPTH_STENCIL */,
1800
- types: [34042 /* UNSIGNED_INT_24_8 */],
1801
- rb: true
1802
- },
1803
1759
  // "depth32float-stencil8" feature - TODO below is render buffer only?
1804
1760
  "depth32float-stencil8": {
1805
1761
  gl: 36013 /* DEPTH32F_STENCIL8 */,
@@ -1914,6 +1870,10 @@ var __exports__ = (() => {
1914
1870
  if (info.gl === void 0) {
1915
1871
  return false;
1916
1872
  }
1873
+ const feature = info.f;
1874
+ if (feature) {
1875
+ return checkTextureFeature(gl, feature, extensions);
1876
+ }
1917
1877
  const extension = info.x || info.gl2ext;
1918
1878
  if (extension) {
1919
1879
  return Boolean(getWebGLExtension(gl, extension, extensions));
@@ -1958,11 +1918,10 @@ var __exports__ = (() => {
1958
1918
  const decoded = (0, import_core.decodeTextureFormat)(format);
1959
1919
  return {
1960
1920
  internalFormat: webglFormat,
1961
- format: formatData?.dataFormat || getWebGLPixelDataFormat(decoded.format, decoded.integer, decoded.normalized, webglFormat),
1921
+ format: formatData?.dataFormat || getWebGLPixelDataFormat(decoded.channels, decoded.integer, decoded.normalized, webglFormat),
1962
1922
  // depth formats don't have a type
1963
1923
  type: decoded.dataType ? getGLFromVertexType(decoded.dataType) : formatData?.types?.[0] || 5121 /* UNSIGNED_BYTE */,
1964
- // @ts-expect-error
1965
- compressed: decoded.compressed
1924
+ compressed: decoded.compressed || false
1966
1925
  };
1967
1926
  }
1968
1927
  function getDepthStencilAttachmentWebGL(format) {
@@ -1972,11 +1931,11 @@ var __exports__ = (() => {
1972
1931
  }
1973
1932
  return info.attachment;
1974
1933
  }
1975
- function getWebGLPixelDataFormat(dataFormat, integer, normalized, format) {
1934
+ function getWebGLPixelDataFormat(channels, integer, normalized, format) {
1976
1935
  if (format === 6408 /* RGBA */ || format === 6407 /* RGB */) {
1977
1936
  return format;
1978
1937
  }
1979
- switch (dataFormat) {
1938
+ switch (channels) {
1980
1939
  case "r":
1981
1940
  return integer && !normalized ? 36244 /* RED_INTEGER */ : 6403 /* RED */;
1982
1941
  case "rg":
@@ -1985,6 +1944,8 @@ var __exports__ = (() => {
1985
1944
  return integer && !normalized ? 36248 /* RGB_INTEGER */ : 6407 /* RGB */;
1986
1945
  case "rgba":
1987
1946
  return integer && !normalized ? 36249 /* RGBA_INTEGER */ : 6408 /* RGBA */;
1947
+ case "bgra":
1948
+ throw new Error("bgra pixels not supported by WebGL");
1988
1949
  default:
1989
1950
  return 6408 /* RGBA */;
1990
1951
  }
@@ -2006,7 +1967,6 @@ var __exports__ = (() => {
2006
1967
  // 'timestamp-query' // GPUQueryType "timestamp-query"
2007
1968
  // "indirect-first-instance"
2008
1969
  // Textures are handled by getTextureFeatures()
2009
- // 'depth24unorm-stencil8' // GPUTextureFormat 'depth24unorm-stencil8'
2010
1970
  // 'depth32float-stencil8' // GPUTextureFormat 'depth32float-stencil8'
2011
1971
  // optional WebGL features
2012
1972
  "timer-query-webgl": "EXT_disjoint_timer_query_webgl2",
@@ -2196,17 +2156,18 @@ var __exports__ = (() => {
2196
2156
  return func(gl);
2197
2157
  }
2198
2158
  const { nocatch = true } = parameters;
2199
- pushContextState(gl);
2159
+ const webglState = WebGLStateTracker.get(gl);
2160
+ webglState.push();
2200
2161
  setGLParameters(gl, parameters);
2201
2162
  let value;
2202
2163
  if (nocatch) {
2203
2164
  value = func(gl);
2204
- popContextState(gl);
2165
+ webglState.pop();
2205
2166
  } else {
2206
2167
  try {
2207
2168
  value = func(gl);
2208
2169
  } finally {
2209
- popContextState(gl);
2170
+ webglState.pop();
2210
2171
  }
2211
2172
  }
2212
2173
  return value;
@@ -2225,25 +2186,26 @@ var __exports__ = (() => {
2225
2186
  return func(device);
2226
2187
  }
2227
2188
  const webglDevice = device;
2228
- pushContextState(webglDevice.gl);
2189
+ webglDevice.pushState();
2229
2190
  try {
2230
2191
  setDeviceParameters(device, parameters);
2231
2192
  setGLParameters(webglDevice.gl, glParameters);
2232
2193
  return func(device);
2233
2194
  } finally {
2234
- popContextState(webglDevice.gl);
2195
+ webglDevice.popState();
2235
2196
  }
2236
2197
  }
2237
2198
  function withDeviceParameters(device, parameters, func) {
2238
2199
  if (isObjectEmpty3(parameters)) {
2239
2200
  return func(device);
2240
2201
  }
2241
- pushContextState(device.gl);
2202
+ const webglDevice = device;
2203
+ webglDevice.pushState();
2242
2204
  try {
2243
2205
  setDeviceParameters(device, parameters);
2244
2206
  return func(device);
2245
2207
  } finally {
2246
- popContextState(device.gl);
2208
+ webglDevice.popState();
2247
2209
  }
2248
2210
  }
2249
2211
  function setDeviceParameters(device, parameters) {
@@ -2371,8 +2333,16 @@ var __exports__ = (() => {
2371
2333
  gl.stencilOpSeparate(1028 /* FRONT */, sfail, dpfail, dppass);
2372
2334
  gl.stencilOpSeparate(1029 /* BACK */, sfail, dpfail, dppass);
2373
2335
  }
2336
+ switch (parameters.blend) {
2337
+ case true:
2338
+ gl.enable(3042 /* BLEND */);
2339
+ break;
2340
+ case false:
2341
+ gl.disable(3042 /* BLEND */);
2342
+ break;
2343
+ default:
2344
+ }
2374
2345
  if (parameters.blendColorOperation || parameters.blendAlphaOperation) {
2375
- gl.enable(3042 /* BLEND */);
2376
2346
  const colorEquation = convertBlendOperationToEquation(
2377
2347
  "blendColorOperation",
2378
2348
  parameters.blendColorOperation || "add"
@@ -2616,7 +2586,7 @@ var __exports__ = (() => {
2616
2586
  const { dimension, width, height, depth = 0, level = 0 } = options;
2617
2587
  const { x = 0, y = 0, z = 0 } = options;
2618
2588
  const { glFormat, glType } = options;
2619
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
2589
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
2620
2590
  switch (dimension) {
2621
2591
  case "2d-array":
2622
2592
  case "3d":
@@ -2634,7 +2604,7 @@ var __exports__ = (() => {
2634
2604
  const { dimension, width, height, depth = 0, level = 0, byteOffset = 0 } = options;
2635
2605
  const { x = 0, y = 0, z = 0 } = options;
2636
2606
  const { glFormat, glType, compressed } = options;
2637
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
2607
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
2638
2608
  switch (dimension) {
2639
2609
  case "2d-array":
2640
2610
  case "3d":
@@ -2673,7 +2643,7 @@ var __exports__ = (() => {
2673
2643
  }
2674
2644
  throw new Error(dimension);
2675
2645
  }
2676
- function getCubeTargetWebGL(glTarget, dimension, level) {
2646
+ function getWebGLCubeFaceTarget(glTarget, dimension, level) {
2677
2647
  return dimension === "cube" ? 34069 /* TEXTURE_CUBE_MAP_POSITIVE_X */ + level : glTarget;
2678
2648
  }
2679
2649
 
@@ -2766,7 +2736,7 @@ var __exports__ = (() => {
2766
2736
  const data = props.data;
2767
2737
  let { width, height } = props;
2768
2738
  if (!width || !height) {
2769
- const textureSize = this.getTextureDataSize(data);
2739
+ const textureSize = import_core7.Texture.getTextureDataSize(data);
2770
2740
  width = textureSize?.width || 1;
2771
2741
  height = textureSize?.height || 1;
2772
2742
  }
@@ -2874,11 +2844,20 @@ var __exports__ = (() => {
2874
2844
  this.gl.bindTexture(this.glTarget, null);
2875
2845
  }
2876
2846
  // Image Data Setters
2847
+ copyExternalImage(options) {
2848
+ const size = import_core7.Texture.getExternalImageSize(options.image);
2849
+ const opts = { ...import_core7.Texture.defaultCopyExternalImageOptions, ...size, ...options };
2850
+ const { depth, mipLevel: lodLevel, image } = opts;
2851
+ this.bind();
2852
+ this._setMipLevel(depth, lodLevel, image);
2853
+ this.unbind();
2854
+ return { width: opts.width, height: opts.height };
2855
+ }
2877
2856
  setTexture1DData(data) {
2878
2857
  throw new Error("setTexture1DData not supported in WebGL.");
2879
2858
  }
2880
2859
  /** Set a simple texture */
2881
- setTexture2DData(lodData, depth = 0, glTarget = this.glTarget) {
2860
+ setTexture2DData(lodData, depth = 0) {
2882
2861
  this.bind();
2883
2862
  const lodArray = normalizeTextureData(lodData, this);
2884
2863
  if (lodArray.length > 1 && this.props.mipmaps !== false) {
@@ -2899,7 +2878,9 @@ var __exports__ = (() => {
2899
2878
  throw new Error(this.id);
2900
2879
  }
2901
2880
  if (ArrayBuffer.isView(data)) {
2881
+ this.bind();
2902
2882
  copyCPUDataToMipLevel(this.device.gl, data, this);
2883
+ this.unbind();
2903
2884
  }
2904
2885
  }
2905
2886
  /**
@@ -2912,6 +2893,9 @@ var __exports__ = (() => {
2912
2893
  if (this.props.dimension !== "cube") {
2913
2894
  throw new Error(this.id);
2914
2895
  }
2896
+ for (const face of import_core7.Texture.CubeFaces) {
2897
+ this.setTextureCubeFaceData(data[face], face);
2898
+ }
2915
2899
  }
2916
2900
  /**
2917
2901
  * Sets an entire texture array
@@ -2934,8 +2918,8 @@ var __exports__ = (() => {
2934
2918
  if (Array.isArray(lodData) && lodData.length > 1 && this.props.mipmaps !== false) {
2935
2919
  import_core7.log.warn(`${this.id} has mipmap and multiple LODs.`)();
2936
2920
  }
2937
- this.bind();
2938
- this.unbind();
2921
+ const faceDepth = import_core7.Texture.CubeFaces.indexOf(face);
2922
+ this.setTexture2DData(lodData, faceDepth);
2939
2923
  }
2940
2924
  // INTERNAL METHODS
2941
2925
  /** @todo update this method to accept LODs */
@@ -3073,16 +3057,17 @@ var __exports__ = (() => {
3073
3057
  * Copy a region of data from a CPU memory buffer into this texture.
3074
3058
  * @todo - GLUnpackParameters parameters
3075
3059
  */
3076
- _setMipLevel(depth, level, textureData, offset = 0) {
3060
+ _setMipLevel(depth, level, textureData, glTarget = this.glTarget) {
3077
3061
  if (import_core7.Texture.isExternalImage(textureData)) {
3078
- copyCPUImageToMipLevel(this.device.gl, textureData, { ...this, depth, level });
3062
+ copyCPUImageToMipLevel(this.device.gl, textureData, { ...this, depth, level, glTarget });
3079
3063
  return;
3080
3064
  }
3081
- if (this.isTextureLevelData(textureData)) {
3065
+ if (import_core7.Texture.isTextureLevelData(textureData)) {
3082
3066
  copyCPUDataToMipLevel(this.device.gl, textureData.data, {
3083
3067
  ...this,
3084
3068
  depth,
3085
- level
3069
+ level,
3070
+ glTarget
3086
3071
  });
3087
3072
  return;
3088
3073
  }
@@ -3221,7 +3206,7 @@ var __exports__ = (() => {
3221
3206
  switch (texture.glTarget) {
3222
3207
  case 35866 /* TEXTURE_2D_ARRAY */:
3223
3208
  case 32879 /* TEXTURE_3D */:
3224
- gl.framebufferTextureLayer(36160 /* FRAMEBUFFER */, attachment, texture.glTarget, level, layer);
3209
+ gl.framebufferTextureLayer(36160 /* FRAMEBUFFER */, attachment, texture.handle, level, layer);
3225
3210
  break;
3226
3211
  case 34067 /* TEXTURE_CUBE_MAP */:
3227
3212
  const face = mapIndexToCubeMapFace(layer);
@@ -3334,17 +3319,21 @@ var __exports__ = (() => {
3334
3319
  }
3335
3320
 
3336
3321
  // src/context/debug/spector.ts
3337
- var DEFAULT_SPECTOR_PROPS = {
3338
- spector: import_core10.log.get("spector") || import_core10.log.get("inspect")
3339
- };
3340
- var SPECTOR_CDN_URL = "https://spectorcdn.babylonjs.com/spector.bundle.js";
3341
3322
  var LOG_LEVEL = 1;
3342
3323
  var spector = null;
3343
3324
  var initialized = false;
3325
+ var DEFAULT_SPECTOR_PROPS = {
3326
+ debugWithSpectorJS: import_core10.log.get("spector") || import_core10.log.get("spectorjs"),
3327
+ // https://github.com/BabylonJS/Spector.js#basic-usage
3328
+ // https://forum.babylonjs.com/t/spectorcdn-is-temporarily-off/48241
3329
+ // spectorUrl: 'https://spectorcdn.babylonjs.com/spector.bundle.js';
3330
+ spectorUrl: "https://cdn.jsdelivr.net/npm/spectorjs@0.9.30/dist/spector.bundle.js",
3331
+ gl: void 0
3332
+ };
3344
3333
  async function loadSpectorJS(props) {
3345
3334
  if (!globalThis.SPECTOR) {
3346
3335
  try {
3347
- await loadScript(SPECTOR_CDN_URL);
3336
+ await loadScript(props.spectorUrl || DEFAULT_SPECTOR_PROPS.spectorUrl);
3348
3337
  } catch (error) {
3349
3338
  import_core10.log.warn(String(error));
3350
3339
  }
@@ -3352,12 +3341,13 @@ var __exports__ = (() => {
3352
3341
  }
3353
3342
  function initializeSpectorJS(props) {
3354
3343
  props = { ...DEFAULT_SPECTOR_PROPS, ...props };
3355
- if (!props?.spector) {
3344
+ if (!props.debugWithSpectorJS) {
3356
3345
  return null;
3357
3346
  }
3358
- if (!spector && globalThis.SPECTOR) {
3359
- import_core10.log.probe(LOG_LEVEL, "SPECTOR found and initialized")();
3360
- spector = new globalThis.SPECTOR.Spector();
3347
+ if (!spector && globalThis.SPECTOR && !globalThis.luma?.spector) {
3348
+ import_core10.log.probe(LOG_LEVEL, "SPECTOR found and initialized. Start with `luma.spector.displayUI()`")();
3349
+ const { Spector } = globalThis.SPECTOR;
3350
+ spector = new Spector();
3361
3351
  if (globalThis.luma) {
3362
3352
  globalThis.luma.spector = spector;
3363
3353
  }
@@ -3378,11 +3368,11 @@ var __exports__ = (() => {
3378
3368
  spector?.resultView.addCapture(capture);
3379
3369
  });
3380
3370
  }
3381
- if (props?.canvas) {
3382
- if (typeof props.spector === "string" && props.spector !== props.canvas.id) {
3383
- return spector;
3384
- }
3385
- spector?.startCapture(props?.canvas, 500);
3371
+ if (props.gl) {
3372
+ const gl = props.gl;
3373
+ const device = gl.device;
3374
+ spector?.startCapture(props.gl, 500);
3375
+ gl.device = device;
3386
3376
  new Promise((resolve) => setTimeout(resolve, 2e3)).then((_) => {
3387
3377
  import_core10.log.info("Spector capture stopped after 2 seconds")();
3388
3378
  spector?.stopCapture();
@@ -3541,6 +3531,14 @@ var __exports__ = (() => {
3541
3531
  }
3542
3532
  }
3543
3533
 
3534
+ // src/utils/uid.ts
3535
+ var uidCounters = {};
3536
+ function uid(id = "id") {
3537
+ uidCounters[id] = uidCounters[id] || 1;
3538
+ const count = uidCounters[id]++;
3539
+ return `${id}-${count}`;
3540
+ }
3541
+
3544
3542
  // src/adapter/resources/webgl-buffer.ts
3545
3543
  var import_core12 = __toESM(require_core(), 1);
3546
3544
  var WEBGLBuffer = class extends import_core12.Buffer {
@@ -3743,8 +3741,8 @@ var __exports__ = (() => {
3743
3741
  return this.getCompilationInfoSync();
3744
3742
  }
3745
3743
  getCompilationInfoSync() {
3746
- const log9 = this.device.gl.getShaderInfoLog(this.handle);
3747
- return log9 ? parseShaderCompilerLog(log9) : [];
3744
+ const log10 = this.device.gl.getShaderInfoLog(this.handle);
3745
+ return log10 ? parseShaderCompilerLog(log10) : [];
3748
3746
  }
3749
3747
  getTranslatedSource() {
3750
3748
  const extensions = this.device.getExtension("WEBGL_debug_shaders");
@@ -3754,7 +3752,7 @@ var __exports__ = (() => {
3754
3752
  // PRIVATE METHODS
3755
3753
  /** Compile a shader and get compilation status */
3756
3754
  async _compile(source) {
3757
- const addGLSLVersion = (source2) => source2.startsWith("#version ") ? source2 : `#version 100
3755
+ const addGLSLVersion = (source2) => source2.startsWith("#version ") ? source2 : `#version 300 es
3758
3756
  ${source2}`;
3759
3757
  source = addGLSLVersion(source);
3760
3758
  const { gl } = this.device;
@@ -3819,12 +3817,22 @@ ${source2}`;
3819
3817
  constructor(device, props) {
3820
3818
  super(device, props);
3821
3819
  this.device = device;
3822
- pushContextState(this.device.gl);
3823
- this.setParameters(this.props.parameters);
3820
+ let viewport;
3821
+ if (!props?.parameters?.viewport) {
3822
+ if (props?.framebuffer) {
3823
+ const { width, height } = props.framebuffer;
3824
+ viewport = [0, 0, width, height];
3825
+ } else {
3826
+ const [width, height] = device.getCanvasContext().getDrawingBufferSize();
3827
+ viewport = [0, 0, width, height];
3828
+ }
3829
+ }
3830
+ this.device.pushState();
3831
+ this.setParameters({ viewport, ...this.props.parameters });
3824
3832
  this.clear();
3825
3833
  }
3826
3834
  end() {
3827
- popContextState(this.device.gl);
3835
+ this.device.popState();
3828
3836
  }
3829
3837
  pushDebugGroup(groupLabel) {
3830
3838
  }
@@ -4612,7 +4620,8 @@ ${source2}`;
4612
4620
  const validBindings = this.shaderLayout.bindings.map((binding2) => `"${binding2.name}"`).join(", ");
4613
4621
  if (!options?.disableWarnings) {
4614
4622
  import_core15.log.warn(
4615
- `Unknown binding "${name}" in render pipeline "${this.id}", expected one of ${validBindings}`
4623
+ `No binding "${name}" in render pipeline "${this.id}", expected one of ${validBindings}`,
4624
+ value
4616
4625
  )();
4617
4626
  }
4618
4627
  continue;
@@ -5253,6 +5262,7 @@ ${source2}`;
5253
5262
  } else {
5254
5263
  this.device.gl.vertexAttribPointer(location, size, type, normalized, stride, offset);
5255
5264
  }
5265
+ this.device.gl.bindBuffer(34962 /* ARRAY_BUFFER */, null);
5256
5266
  this.device.gl.enableVertexAttribArray(location);
5257
5267
  this.device.gl.vertexAttribDivisor(location, divisor || 0);
5258
5268
  this.attributes[location] = buffer;
@@ -5432,7 +5442,7 @@ ${source2}`;
5432
5442
  }
5433
5443
  end() {
5434
5444
  this.gl.endTransformFeedback();
5435
- if (!this.bindOnUse) {
5445
+ if (this.bindOnUse) {
5436
5446
  this._unbindBuffers();
5437
5447
  }
5438
5448
  this.gl.bindTransformFeedback(36386 /* TRANSFORM_FEEDBACK */, null);
@@ -5715,7 +5725,6 @@ ${source2}`;
5715
5725
  const {
5716
5726
  sourceX = 0,
5717
5727
  sourceY = 0,
5718
- sourceFormat = 6408 /* RGBA */,
5719
5728
  sourceAttachment = 36064 /* COLOR_ATTACHMENT0 */
5720
5729
  // TODO - support gl.readBuffer
5721
5730
  } = options || {};
@@ -5724,15 +5733,19 @@ ${source2}`;
5724
5733
  // following parameters are auto deduced if not provided
5725
5734
  sourceWidth,
5726
5735
  sourceHeight,
5736
+ sourceDepth,
5737
+ sourceFormat,
5727
5738
  sourceType
5728
5739
  } = options || {};
5729
5740
  const { framebuffer, deleteFramebuffer } = getFramebuffer2(source);
5730
5741
  const { gl, handle } = framebuffer;
5731
- sourceWidth = sourceWidth || framebuffer.width;
5732
- sourceHeight = sourceHeight || framebuffer.height;
5733
5742
  const attachment = sourceAttachment - 36064 /* COLOR_ATTACHMENT0 */;
5734
- sourceType = sourceType || framebuffer.colorAttachments[attachment]?.texture?.glType || 5121 /* UNSIGNED_BYTE */;
5735
- target = getPixelArray(target, sourceType, sourceFormat, sourceWidth, sourceHeight);
5743
+ sourceWidth ||= framebuffer.width;
5744
+ sourceHeight ||= framebuffer.height;
5745
+ sourceDepth = framebuffer.colorAttachments[attachment]?.texture?.depth || 1;
5746
+ sourceFormat ||= framebuffer.colorAttachments[attachment]?.texture?.glFormat || 6408 /* RGBA */;
5747
+ sourceType ||= framebuffer.colorAttachments[attachment]?.texture?.glType || 5121 /* UNSIGNED_BYTE */;
5748
+ target = getPixelArray(target, sourceType, sourceFormat, sourceWidth, sourceHeight, sourceDepth);
5736
5749
  sourceType = sourceType || getGLTypeFromTypedArray(target);
5737
5750
  const prevHandle = gl.bindFramebuffer(36160 /* FRAMEBUFFER */, handle);
5738
5751
  gl.readPixels(sourceX, sourceY, sourceWidth, sourceHeight, sourceFormat, sourceType, target);
@@ -5795,7 +5808,7 @@ ${source2}`;
5795
5808
  });
5796
5809
  return framebuffer;
5797
5810
  }
5798
- function getPixelArray(pixelArray, type, format, width, height) {
5811
+ function getPixelArray(pixelArray, type, format, width, height, depth) {
5799
5812
  if (pixelArray) {
5800
5813
  return pixelArray;
5801
5814
  }
@@ -5841,8 +5854,10 @@ ${source2}`;
5841
5854
  }
5842
5855
 
5843
5856
  // src/adapter/webgl-device.ts
5844
- var LOG_LEVEL2 = 1;
5845
- var _WebGLDevice = class extends import_core22.Device {
5857
+ var WebGLDevice = class extends import_core22.Device {
5858
+ //
5859
+ // Public `Device` API
5860
+ //
5846
5861
  /** type of this device */
5847
5862
  type = "webgl";
5848
5863
  /** The underlying WebGL context */
@@ -5853,66 +5868,21 @@ ${source2}`;
5853
5868
  canvasContext;
5854
5869
  lost;
5855
5870
  _resolveContextLost;
5856
- //
5857
- // Static methods, expected to be present by `luma.createDevice()`
5858
- //
5859
- /** Check if WebGL 2 is available */
5860
- static isSupported() {
5861
- return typeof WebGL2RenderingContext !== "undefined";
5862
- }
5863
- /**
5864
- * Get a device instance from a GL context
5865
- * Creates and instruments the device if not already created
5866
- * @param gl
5867
- * @returns
5868
- */
5869
- static attach(gl) {
5870
- if (gl instanceof _WebGLDevice) {
5871
- return gl;
5872
- }
5873
- if (gl?.device instanceof import_core22.Device) {
5874
- return gl.device;
5875
- }
5876
- if (!isWebGL(gl)) {
5877
- throw new Error("Invalid WebGL2RenderingContext");
5878
- }
5879
- return new _WebGLDevice({ gl });
5880
- }
5881
- static async create(props = {}) {
5882
- import_core22.log.groupCollapsed(LOG_LEVEL2, "WebGLDevice created")();
5883
- const promises = [];
5884
- if (props.debug) {
5885
- promises.push(loadWebGLDeveloperTools());
5886
- }
5887
- if (props.spector) {
5888
- promises.push(loadSpectorJS());
5889
- }
5890
- if (typeof props.canvas === "string") {
5891
- promises.push(import_core22.CanvasContext.pageLoaded);
5892
- }
5893
- const results = await Promise.allSettled(promises);
5894
- for (const result of results) {
5895
- if (result.status === "rejected") {
5896
- import_core22.log.error(`Failed to initialize debug libraries ${result.reason}`)();
5897
- }
5898
- }
5899
- import_core22.log.probe(LOG_LEVEL2 + 1, "DOM is loaded")();
5900
- if (props.gl?.device) {
5901
- import_core22.log.warn("reattaching existing device")();
5902
- return _WebGLDevice.attach(props.gl);
5903
- }
5904
- const device = new _WebGLDevice(props);
5905
- const message2 = `Created ${device.type}${device.debug ? " debug" : ""} context: ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContext.id}`;
5906
- import_core22.log.probe(LOG_LEVEL2, message2)();
5907
- import_core22.log.table(LOG_LEVEL2, device.info)();
5908
- import_core22.log.groupEnd(LOG_LEVEL2)();
5909
- return device;
5910
- }
5871
+ /** WebGL2 context. */
5872
+ gl;
5873
+ debug = false;
5874
+ /** State used by luma.gl classes: TODO - move to canvasContext*/
5875
+ _canvasSizeInfo = { clientWidth: 0, clientHeight: 0, devicePixelRatio: 1 };
5876
+ /** State used by luma.gl classes - TODO - not used? */
5877
+ _extensions = {};
5878
+ _polyfilled = false;
5879
+ /** Instance of Spector.js (if initialized) */
5880
+ spectorJS;
5911
5881
  //
5912
5882
  // Public API
5913
5883
  //
5914
5884
  constructor(props) {
5915
- super({ ...props, id: props.id || "webgl-device" });
5885
+ super({ ...props, id: props.id || uid("webgl-device") });
5916
5886
  const device = props.gl?.device;
5917
5887
  if (device) {
5918
5888
  throw new Error(`WebGL context already attached to device ${device.id}`);
@@ -5922,19 +5892,18 @@ ${source2}`;
5922
5892
  this.lost = new Promise((resolve) => {
5923
5893
  this._resolveContextLost = resolve;
5924
5894
  });
5925
- let gl = props.gl || null;
5926
- gl ||= createBrowserContext(this.canvasContext.canvas, {
5895
+ this.handle = createBrowserContext(this.canvasContext.canvas, {
5927
5896
  ...props,
5928
5897
  onContextLost: (event) => this._resolveContextLost?.({
5929
5898
  reason: "destroyed",
5930
5899
  message: "Entered sleep mode, or too many apps or browser tabs are using the GPU."
5931
5900
  })
5932
5901
  });
5933
- if (!gl) {
5902
+ this.gl = this.handle;
5903
+ if (!this.handle) {
5934
5904
  throw new Error("WebGL context creation failed");
5935
5905
  }
5936
- this.handle = gl;
5937
- this.gl = gl;
5906
+ this.spectorJS = initializeSpectorJS({ ...this.props, gl: this.handle });
5938
5907
  this.gl.device = this;
5939
5908
  this.gl._version = 2;
5940
5909
  this.info = getDeviceInfo(this.gl, this._extensions);
@@ -5944,21 +5913,16 @@ ${source2}`;
5944
5913
  this.features.initializeFeatures();
5945
5914
  }
5946
5915
  this.canvasContext.resize();
5947
- const { enable: enable2 = true, copyState = false } = props;
5948
- trackContextState(this.gl, {
5949
- enable: enable2,
5950
- copyState,
5916
+ const glState = new WebGLStateTracker(this.gl, {
5951
5917
  log: (...args) => import_core22.log.log(1, ...args)()
5952
5918
  });
5919
+ glState.trackState(this.gl, { copyState: false });
5953
5920
  if (props.debug) {
5954
5921
  this.gl = makeDebugContext(this.gl, { ...props, throwOnError: true });
5955
5922
  this.debug = true;
5956
5923
  import_core22.log.level = Math.max(import_core22.log.level, 1);
5957
5924
  import_core22.log.warn("WebGL debug mode activated. Performance reduced.")();
5958
5925
  }
5959
- if (props.spector) {
5960
- this.spectorJS = initializeSpectorJS({ ...this.props, canvas: this.handle.canvas });
5961
- }
5962
5926
  }
5963
5927
  /**
5964
5928
  * Destroys the context
@@ -5969,9 +5933,6 @@ ${source2}`;
5969
5933
  get isLost() {
5970
5934
  return this.gl.isContextLost();
5971
5935
  }
5972
- getSize() {
5973
- return [this.gl.drawingBufferWidth, this.gl.drawingBufferHeight];
5974
- }
5975
5936
  isTextureFormatSupported(format) {
5976
5937
  return isTextureFormatSupported(this.gl, format, this._extensions);
5977
5938
  }
@@ -6069,16 +6030,6 @@ ${source2}`;
6069
6030
  //
6070
6031
  // WebGL-only API (not part of `Device` API)
6071
6032
  //
6072
- /** WebGL2 context. */
6073
- gl;
6074
- debug = false;
6075
- /** State used by luma.gl classes: TODO - move to canvasContext*/
6076
- _canvasSizeInfo = { clientWidth: 0, clientHeight: 0, devicePixelRatio: 1 };
6077
- /** State used by luma.gl classes - TODO - not used? */
6078
- _extensions = {};
6079
- _polyfilled = false;
6080
- /** Instance of Spector.js (if initialized) */
6081
- spectorJS;
6082
6033
  /**
6083
6034
  * Triggers device (or WebGL context) loss.
6084
6035
  * @note primarily intended for testing how application reacts to device loss
@@ -6099,11 +6050,13 @@ ${source2}`;
6099
6050
  }
6100
6051
  /** Save current WebGL context state onto an internal stack */
6101
6052
  pushState() {
6102
- pushContextState(this.gl);
6053
+ const webglState = WebGLStateTracker.get(this.gl);
6054
+ webglState.push();
6103
6055
  }
6104
6056
  /** Restores previously saved context state */
6105
6057
  popState() {
6106
- popContextState(this.gl);
6058
+ const webglState = WebGLStateTracker.get(this.gl);
6059
+ webglState.pop();
6107
6060
  }
6108
6061
  /**
6109
6062
  * Storing data on a special field on WebGLObjects makes that data visible in SPECTOR chrome debug extension
@@ -6166,18 +6119,6 @@ ${source2}`;
6166
6119
  return this._extensions;
6167
6120
  }
6168
6121
  };
6169
- var WebGLDevice = _WebGLDevice;
6170
- //
6171
- // Public `Device` API
6172
- //
6173
- /** type of this device */
6174
- __publicField(WebGLDevice, "type", "webgl");
6175
- function isWebGL(gl) {
6176
- if (typeof WebGL2RenderingContext !== "undefined" && gl instanceof WebGL2RenderingContext) {
6177
- return true;
6178
- }
6179
- return Boolean(gl && Number.isFinite(gl._version));
6180
- }
6181
6122
  function setConstantFloatArray(device, location, array) {
6182
6123
  switch (array.length) {
6183
6124
  case 1:
@@ -6212,6 +6153,181 @@ ${source2}`;
6212
6153
  }
6213
6154
  return true;
6214
6155
  }
6156
+
6157
+ // src/context/polyfills/polyfill-webgl1-extensions.ts
6158
+ var WEBGL1_STATIC_EXTENSIONS = {
6159
+ WEBGL_depth_texture: {
6160
+ UNSIGNED_INT_24_8_WEBGL: 34042 /* UNSIGNED_INT_24_8 */
6161
+ },
6162
+ OES_element_index_uint: {},
6163
+ OES_texture_float: {},
6164
+ OES_texture_half_float: {
6165
+ // @ts-expect-error different numbers?
6166
+ HALF_FLOAT_OES: 5131 /* HALF_FLOAT */
6167
+ },
6168
+ EXT_color_buffer_float: {},
6169
+ OES_standard_derivatives: {
6170
+ FRAGMENT_SHADER_DERIVATIVE_HINT_OES: 35723 /* FRAGMENT_SHADER_DERIVATIVE_HINT */
6171
+ },
6172
+ EXT_frag_depth: {},
6173
+ EXT_blend_minmax: {
6174
+ MIN_EXT: 32775 /* MIN */,
6175
+ MAX_EXT: 32776 /* MAX */
6176
+ },
6177
+ EXT_shader_texture_lod: {}
6178
+ };
6179
+ var getWEBGL_draw_buffers = (gl) => ({
6180
+ drawBuffersWEBGL(buffers) {
6181
+ return gl.drawBuffers(buffers);
6182
+ },
6183
+ COLOR_ATTACHMENT0_WEBGL: 36064 /* COLOR_ATTACHMENT0 */,
6184
+ COLOR_ATTACHMENT1_WEBGL: 36065 /* COLOR_ATTACHMENT1 */,
6185
+ COLOR_ATTACHMENT2_WEBGL: 36066 /* COLOR_ATTACHMENT2 */,
6186
+ COLOR_ATTACHMENT3_WEBGL: 36067 /* COLOR_ATTACHMENT3 */
6187
+ });
6188
+ var getOES_vertex_array_object = (gl) => ({
6189
+ VERTEX_ARRAY_BINDING_OES: 34229 /* VERTEX_ARRAY_BINDING */,
6190
+ createVertexArrayOES() {
6191
+ return gl.createVertexArray();
6192
+ },
6193
+ deleteVertexArrayOES(vertexArray) {
6194
+ return gl.deleteVertexArray(vertexArray);
6195
+ },
6196
+ isVertexArrayOES(vertexArray) {
6197
+ return gl.isVertexArray(vertexArray);
6198
+ },
6199
+ bindVertexArrayOES(vertexArray) {
6200
+ return gl.bindVertexArray(vertexArray);
6201
+ }
6202
+ });
6203
+ var getANGLE_instanced_arrays = (gl) => ({
6204
+ VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE: 35070,
6205
+ drawArraysInstancedANGLE(...args) {
6206
+ return gl.drawArraysInstanced(...args);
6207
+ },
6208
+ drawElementsInstancedANGLE(...args) {
6209
+ return gl.drawElementsInstanced(...args);
6210
+ },
6211
+ vertexAttribDivisorANGLE(...args) {
6212
+ return gl.vertexAttribDivisor(...args);
6213
+ }
6214
+ });
6215
+ function enforceWebGL2(enforce = true) {
6216
+ const prototype = HTMLCanvasElement.prototype;
6217
+ if (!enforce && prototype.originalGetContext) {
6218
+ prototype.getContext = prototype.originalGetContext;
6219
+ prototype.originalGetContext = void 0;
6220
+ return;
6221
+ }
6222
+ prototype.originalGetContext = prototype.getContext;
6223
+ prototype.getContext = function(contextId, options) {
6224
+ if (contextId === "webgl" || contextId === "experimental-webgl") {
6225
+ const context = this.originalGetContext("webgl2", options);
6226
+ if (context instanceof HTMLElement) {
6227
+ polyfillWebGL1Extensions(context);
6228
+ }
6229
+ return context;
6230
+ }
6231
+ return this.originalGetContext(contextId, options);
6232
+ };
6233
+ }
6234
+ function polyfillWebGL1Extensions(gl) {
6235
+ gl.getExtension("EXT_color_buffer_float");
6236
+ const boundExtensions = {
6237
+ ...WEBGL1_STATIC_EXTENSIONS,
6238
+ WEBGL_disjoint_timer_query: gl.getExtension("EXT_disjoint_timer_query_webgl2"),
6239
+ WEBGL_draw_buffers: getWEBGL_draw_buffers(gl),
6240
+ OES_vertex_array_object: getOES_vertex_array_object(gl),
6241
+ ANGLE_instanced_arrays: getANGLE_instanced_arrays(gl)
6242
+ };
6243
+ const originalGetExtension = gl.getExtension;
6244
+ gl.getExtension = function(extensionName) {
6245
+ const ext = originalGetExtension.call(gl, extensionName);
6246
+ if (ext) {
6247
+ return ext;
6248
+ }
6249
+ if (extensionName in boundExtensions) {
6250
+ return boundExtensions[extensionName];
6251
+ }
6252
+ return null;
6253
+ };
6254
+ const originalGetSupportedExtensions = gl.getSupportedExtensions;
6255
+ gl.getSupportedExtensions = function() {
6256
+ const extensions = originalGetSupportedExtensions.apply(gl) || [];
6257
+ return extensions?.concat(Object.keys(boundExtensions));
6258
+ };
6259
+ }
6260
+
6261
+ // src/adapter/webgl-adapter.ts
6262
+ var LOG_LEVEL2 = 1;
6263
+ var WebGLAdapter = class extends import_core23.Adapter {
6264
+ /** type of device's created by this adapter */
6265
+ type = "webgl";
6266
+ constructor() {
6267
+ super();
6268
+ import_core23.Device.defaultProps = { ...import_core23.Device.defaultProps, ...DEFAULT_SPECTOR_PROPS };
6269
+ WebGLDevice.adapter = this;
6270
+ }
6271
+ /** Check if WebGL 2 is available */
6272
+ isSupported() {
6273
+ return typeof WebGL2RenderingContext !== "undefined";
6274
+ }
6275
+ /** Force any created WebGL contexts to be WebGL2 contexts, polyfilled with WebGL1 extensions */
6276
+ enforceWebGL2(enable2) {
6277
+ enforceWebGL2(enable2);
6278
+ }
6279
+ /**
6280
+ * Get a device instance from a GL context
6281
+ * Creates and instruments the device if not already created
6282
+ * @param gl
6283
+ * @returns
6284
+ */
6285
+ async attach(gl) {
6286
+ if (gl instanceof WebGLDevice) {
6287
+ return gl;
6288
+ }
6289
+ if (gl?.device instanceof import_core23.Device) {
6290
+ return gl.device;
6291
+ }
6292
+ if (!isWebGL(gl)) {
6293
+ throw new Error("Invalid WebGL2RenderingContext");
6294
+ }
6295
+ return new WebGLDevice({ gl });
6296
+ }
6297
+ async create(props = {}) {
6298
+ import_core23.log.groupCollapsed(LOG_LEVEL2, "WebGLDevice created")();
6299
+ const promises = [];
6300
+ if (props.debug) {
6301
+ promises.push(loadWebGLDeveloperTools());
6302
+ }
6303
+ if (props.debugWithSpectorJS) {
6304
+ promises.push(loadSpectorJS(props));
6305
+ }
6306
+ if (typeof props.canvas === "string") {
6307
+ promises.push(import_core23.CanvasContext.pageLoaded);
6308
+ }
6309
+ const results = await Promise.allSettled(promises);
6310
+ for (const result of results) {
6311
+ if (result.status === "rejected") {
6312
+ import_core23.log.error(`Failed to initialize debug libraries ${result.reason}`)();
6313
+ }
6314
+ }
6315
+ import_core23.log.probe(LOG_LEVEL2 + 1, "DOM is loaded")();
6316
+ const device = new WebGLDevice(props);
6317
+ const message2 = `Created ${device.type}${device.debug ? " debug" : ""} context: ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContext.id}`;
6318
+ import_core23.log.probe(LOG_LEVEL2, message2)();
6319
+ import_core23.log.table(LOG_LEVEL2, device.info)();
6320
+ import_core23.log.groupEnd(LOG_LEVEL2)();
6321
+ return device;
6322
+ }
6323
+ };
6324
+ function isWebGL(gl) {
6325
+ if (typeof WebGL2RenderingContext !== "undefined" && gl instanceof WebGL2RenderingContext) {
6326
+ return true;
6327
+ }
6328
+ return Boolean(gl && Number.isFinite(gl._version));
6329
+ }
6330
+ var webgl2Adapter = new WebGLAdapter();
6215
6331
  return __toCommonJS(bundle_exports);
6216
6332
  })();
6217
6333
  return __exports__;