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

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 (109) 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 +1 -3
  8. package/dist/adapter/helpers/format-utils.d.ts.map +1 -0
  9. package/dist/adapter/helpers/get-shader-layout.d.ts.map +1 -1
  10. package/dist/adapter/helpers/get-shader-layout.js +1 -3
  11. package/dist/adapter/helpers/typed-array-utils.d.ts.map +1 -0
  12. package/dist/adapter/helpers/webgl-texture-utils.d.ts +89 -22
  13. package/dist/adapter/helpers/webgl-texture-utils.d.ts.map +1 -1
  14. package/dist/adapter/helpers/webgl-texture-utils.js +220 -26
  15. package/dist/adapter/resources/webgl-framebuffer.js +1 -1
  16. package/dist/adapter/resources/webgl-render-pass.d.ts.map +1 -1
  17. package/dist/adapter/resources/webgl-render-pass.js +17 -4
  18. package/dist/adapter/resources/webgl-render-pipeline.d.ts +1 -3
  19. package/dist/adapter/resources/webgl-render-pipeline.d.ts.map +1 -1
  20. package/dist/adapter/resources/webgl-render-pipeline.js +1 -1
  21. package/dist/adapter/resources/webgl-shader.js +1 -1
  22. package/dist/adapter/resources/webgl-texture.d.ts +21 -3
  23. package/dist/adapter/resources/webgl-texture.d.ts.map +1 -1
  24. package/dist/adapter/resources/webgl-texture.js +49 -30
  25. package/dist/adapter/resources/webgl-transform-feedback.js +1 -1
  26. package/dist/adapter/resources/webgl-vertex-array.d.ts.map +1 -1
  27. package/dist/adapter/resources/webgl-vertex-array.js +3 -0
  28. package/dist/adapter/webgl-adapter.d.ts +21 -0
  29. package/dist/adapter/webgl-adapter.d.ts.map +1 -0
  30. package/dist/adapter/webgl-adapter.js +91 -0
  31. package/dist/adapter/webgl-device.d.ts +16 -29
  32. package/dist/adapter/webgl-device.d.ts.map +1 -1
  33. package/dist/adapter/webgl-device.js +34 -114
  34. package/dist/context/debug/spector-types.d.ts +1108 -0
  35. package/dist/context/debug/spector-types.d.ts.map +1 -0
  36. package/dist/context/debug/spector-types.js +697 -0
  37. package/dist/context/debug/spector.d.ts +12 -8
  38. package/dist/context/debug/spector.d.ts.map +1 -1
  39. package/dist/context/debug/spector.js +23 -17
  40. package/dist/context/polyfills/polyfill-webgl1-extensions.d.ts +9 -0
  41. package/dist/context/polyfills/polyfill-webgl1-extensions.d.ts.map +1 -0
  42. package/dist/context/polyfills/polyfill-webgl1-extensions.js +181 -0
  43. package/dist/context/state-tracker/webgl-state-tracker.d.ts +43 -0
  44. package/dist/context/state-tracker/webgl-state-tracker.d.ts.map +1 -0
  45. package/dist/context/state-tracker/{track-context-state.js → webgl-state-tracker.js} +44 -74
  46. package/dist/context/state-tracker/with-parameters.d.ts.map +1 -1
  47. package/dist/context/state-tracker/with-parameters.js +5 -4
  48. package/dist/deprecated/accessor.d.ts.map +1 -0
  49. package/dist/{classic → deprecated}/accessor.js +36 -1
  50. package/dist/deprecated/clear.d.ts.map +1 -0
  51. package/dist/{classic → deprecated}/clear.js +2 -0
  52. package/dist/dist.dev.js +816 -642
  53. package/dist/dist.min.js +2 -2
  54. package/dist/index.cjs +815 -648
  55. package/dist/index.cjs.map +4 -4
  56. package/dist/index.d.ts +4 -2
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +4 -3
  59. package/dist/utils/fill-array.d.ts +4 -4
  60. package/dist/utils/fill-array.d.ts.map +1 -1
  61. package/dist/utils/split-uniforms-and-bindings.d.ts +1 -1
  62. package/dist/utils/split-uniforms-and-bindings.d.ts.map +1 -1
  63. package/dist/utils/uid.d.ts +7 -0
  64. package/dist/utils/uid.d.ts.map +1 -0
  65. package/dist/utils/uid.js +14 -0
  66. package/package.json +5 -5
  67. package/src/adapter/converters/device-parameters.ts +18 -12
  68. package/src/adapter/converters/texture-formats.ts +12 -20
  69. package/src/adapter/device-helpers/webgl-device-features.ts +5 -3
  70. package/src/adapter/helpers/get-shader-layout.ts +1 -3
  71. package/src/adapter/helpers/webgl-texture-utils.ts +366 -44
  72. package/src/adapter/resources/webgl-framebuffer.ts +1 -1
  73. package/src/adapter/resources/webgl-render-pass.ts +20 -7
  74. package/src/adapter/resources/webgl-render-pipeline.ts +12 -4
  75. package/src/adapter/resources/webgl-shader.ts +1 -1
  76. package/src/adapter/resources/webgl-texture.ts +76 -30
  77. package/src/adapter/resources/webgl-transform-feedback.ts +1 -1
  78. package/src/adapter/resources/webgl-vertex-array.ts +3 -0
  79. package/src/adapter/webgl-adapter.ts +113 -0
  80. package/src/adapter/webgl-device.ts +45 -139
  81. package/src/context/debug/spector-types.ts +1154 -0
  82. package/src/context/debug/spector.ts +38 -29
  83. package/src/context/polyfills/polyfill-webgl1-extensions.ts +202 -0
  84. package/src/context/state-tracker/{track-context-state.ts → webgl-state-tracker.ts} +55 -94
  85. package/src/context/state-tracker/with-parameters.ts +5 -4
  86. package/src/{classic → deprecated}/accessor.ts +44 -3
  87. package/src/{classic → deprecated}/clear.ts +3 -1
  88. package/src/index.ts +6 -8
  89. package/src/utils/fill-array.ts +4 -4
  90. package/src/utils/split-uniforms-and-bindings.ts +3 -3
  91. package/src/utils/uid.ts +16 -0
  92. package/dist/classic/accessor.d.ts.map +0 -1
  93. package/dist/classic/clear.d.ts.map +0 -1
  94. package/dist/classic/copy-and-blit.d.ts +0 -63
  95. package/dist/classic/copy-and-blit.d.ts.map +0 -1
  96. package/dist/classic/copy-and-blit.js +0 -193
  97. package/dist/classic/format-utils.d.ts.map +0 -1
  98. package/dist/classic/typed-array-utils.d.ts.map +0 -1
  99. package/dist/context/state-tracker/track-context-state.d.ts +0 -22
  100. package/dist/context/state-tracker/track-context-state.d.ts.map +0 -1
  101. package/src/classic/copy-and-blit.ts +0 -318
  102. /package/dist/{classic → adapter/helpers}/format-utils.d.ts +0 -0
  103. /package/dist/{classic → adapter/helpers}/format-utils.js +0 -0
  104. /package/dist/{classic → adapter/helpers}/typed-array-utils.d.ts +0 -0
  105. /package/dist/{classic → adapter/helpers}/typed-array-utils.js +0 -0
  106. /package/dist/{classic → deprecated}/accessor.d.ts +0 -0
  107. /package/dist/{classic → deprecated}/clear.d.ts +0 -0
  108. /package/src/{classic → adapter/helpers}/format-utils.ts +0 -0
  109. /package/src/{classic → adapter/helpers}/typed-array-utils.ts +0 -0
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,22 +60,24 @@ 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_core24 = __toESM(require_core(), 1);
78
+
82
79
  // src/adapter/webgl-device.ts
83
- var import_core22 = __toESM(require_core(), 1);
80
+ var import_core23 = __toESM(require_core(), 1);
84
81
 
85
82
  // ../constants/src/webgl-constants.ts
86
83
  var GLEnum = /* @__PURE__ */ ((GLEnum2) => {
@@ -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",
@@ -2182,13 +2142,13 @@ var __exports__ = (() => {
2182
2142
  };
2183
2143
 
2184
2144
  // src/adapter/webgl-canvas-context.ts
2185
- var import_core9 = __toESM(require_core(), 1);
2145
+ var import_core10 = __toESM(require_core(), 1);
2186
2146
 
2187
2147
  // src/adapter/resources/webgl-framebuffer.ts
2188
- var import_core8 = __toESM(require_core(), 1);
2148
+ var import_core9 = __toESM(require_core(), 1);
2189
2149
 
2190
2150
  // src/adapter/resources/webgl-texture.ts
2191
- var import_core7 = __toESM(require_core(), 1);
2151
+ var import_core8 = __toESM(require_core(), 1);
2192
2152
 
2193
2153
  // src/context/state-tracker/with-parameters.ts
2194
2154
  function withGLParameters(gl, parameters, func) {
@@ -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"
@@ -2598,6 +2568,94 @@ var __exports__ = (() => {
2598
2568
  }
2599
2569
  };
2600
2570
 
2571
+ // src/adapter/helpers/webgl-texture-utils.ts
2572
+ var import_core7 = __toESM(require_core(), 1);
2573
+
2574
+ // src/adapter/helpers/typed-array-utils.ts
2575
+ var ERR_TYPE_DEDUCTION = "Failed to deduce GL constant from typed array";
2576
+ function getGLTypeFromTypedArray(arrayOrType) {
2577
+ const type = ArrayBuffer.isView(arrayOrType) ? arrayOrType.constructor : arrayOrType;
2578
+ switch (type) {
2579
+ case Float32Array:
2580
+ return 5126 /* FLOAT */;
2581
+ case Uint16Array:
2582
+ return 5123 /* UNSIGNED_SHORT */;
2583
+ case Uint32Array:
2584
+ return 5125 /* UNSIGNED_INT */;
2585
+ case Uint8Array:
2586
+ return 5121 /* UNSIGNED_BYTE */;
2587
+ case Uint8ClampedArray:
2588
+ return 5121 /* UNSIGNED_BYTE */;
2589
+ case Int8Array:
2590
+ return 5120 /* BYTE */;
2591
+ case Int16Array:
2592
+ return 5122 /* SHORT */;
2593
+ case Int32Array:
2594
+ return 5124 /* INT */;
2595
+ default:
2596
+ throw new Error(ERR_TYPE_DEDUCTION);
2597
+ }
2598
+ }
2599
+ function getTypedArrayFromGLType(glType, options) {
2600
+ const { clamped = true } = options || {};
2601
+ switch (glType) {
2602
+ case 5126 /* FLOAT */:
2603
+ return Float32Array;
2604
+ case 5123 /* UNSIGNED_SHORT */:
2605
+ case 33635 /* UNSIGNED_SHORT_5_6_5 */:
2606
+ case 32819 /* UNSIGNED_SHORT_4_4_4_4 */:
2607
+ case 32820 /* UNSIGNED_SHORT_5_5_5_1 */:
2608
+ return Uint16Array;
2609
+ case 5125 /* UNSIGNED_INT */:
2610
+ return Uint32Array;
2611
+ case 5121 /* UNSIGNED_BYTE */:
2612
+ return clamped ? Uint8ClampedArray : Uint8Array;
2613
+ case 5120 /* BYTE */:
2614
+ return Int8Array;
2615
+ case 5122 /* SHORT */:
2616
+ return Int16Array;
2617
+ case 5124 /* INT */:
2618
+ return Int32Array;
2619
+ default:
2620
+ throw new Error("Failed to deduce typed array type from GL constant");
2621
+ }
2622
+ }
2623
+
2624
+ // src/adapter/helpers/format-utils.ts
2625
+ function glFormatToComponents(format) {
2626
+ switch (format) {
2627
+ case 6406 /* ALPHA */:
2628
+ case 33326 /* R32F */:
2629
+ case 6403 /* RED */:
2630
+ return 1;
2631
+ case 33328 /* RG32F */:
2632
+ case 33319 /* RG */:
2633
+ return 2;
2634
+ case 6407 /* RGB */:
2635
+ case 34837 /* RGB32F */:
2636
+ return 3;
2637
+ case 6408 /* RGBA */:
2638
+ case 34836 /* RGBA32F */:
2639
+ return 4;
2640
+ default:
2641
+ return 0;
2642
+ }
2643
+ }
2644
+ function glTypeToBytes(type) {
2645
+ switch (type) {
2646
+ case 5121 /* UNSIGNED_BYTE */:
2647
+ return 1;
2648
+ case 33635 /* UNSIGNED_SHORT_5_6_5 */:
2649
+ case 32819 /* UNSIGNED_SHORT_4_4_4_4 */:
2650
+ case 32820 /* UNSIGNED_SHORT_5_5_5_1 */:
2651
+ return 2;
2652
+ case 5126 /* FLOAT */:
2653
+ return 4;
2654
+ default:
2655
+ return 0;
2656
+ }
2657
+ }
2658
+
2601
2659
  // src/adapter/helpers/webgl-texture-utils.ts
2602
2660
  function initializeTextureStorage(gl, levels, options) {
2603
2661
  const { dimension, width, height, depth = 0 } = options;
@@ -2612,44 +2670,49 @@ var __exports__ = (() => {
2612
2670
  gl.texStorage2D(glTarget, levels, glInternalFormat, width, height);
2613
2671
  }
2614
2672
  }
2615
- function copyCPUImageToMipLevel(gl, image, options) {
2616
- const { dimension, width, height, depth = 0, level = 0 } = options;
2673
+ function copyExternalImageToMipLevel(gl, handle, image, options) {
2674
+ const { width, height } = options;
2675
+ const { dimension, depth = 0, mipLevel = 0 } = options;
2617
2676
  const { x = 0, y = 0, z = 0 } = options;
2618
2677
  const { glFormat, glType } = options;
2619
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
2678
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
2620
2679
  switch (dimension) {
2621
2680
  case "2d-array":
2622
2681
  case "3d":
2623
- gl.texSubImage3D(glTarget, level, x, y, z, width, height, depth, glFormat, glType, image);
2682
+ gl.bindTexture(glTarget, handle);
2683
+ gl.texSubImage3D(glTarget, mipLevel, x, y, z, width, height, depth, glFormat, glType, image);
2684
+ gl.bindTexture(glTarget, null);
2624
2685
  break;
2625
2686
  case "2d":
2626
2687
  case "cube":
2627
- gl.texSubImage2D(glTarget, level, x, y, width, height, glFormat, glType, image);
2688
+ gl.bindTexture(glTarget, handle);
2689
+ gl.texSubImage2D(glTarget, mipLevel, x, y, width, height, glFormat, glType, image);
2690
+ gl.bindTexture(glTarget, null);
2628
2691
  break;
2629
2692
  default:
2630
2693
  throw new Error(dimension);
2631
2694
  }
2632
2695
  }
2633
2696
  function copyCPUDataToMipLevel(gl, typedArray, options) {
2634
- const { dimension, width, height, depth = 0, level = 0, byteOffset = 0 } = options;
2697
+ const { dimension, width, height, depth = 0, mipLevel = 0, byteOffset = 0 } = options;
2635
2698
  const { x = 0, y = 0, z = 0 } = options;
2636
2699
  const { glFormat, glType, compressed } = options;
2637
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
2700
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
2638
2701
  switch (dimension) {
2639
2702
  case "2d-array":
2640
2703
  case "3d":
2641
2704
  if (compressed) {
2642
- gl.compressedTexSubImage3D(glTarget, level, x, y, z, width, height, depth, glFormat, typedArray, byteOffset);
2705
+ gl.compressedTexSubImage3D(glTarget, mipLevel, x, y, z, width, height, depth, glFormat, typedArray, byteOffset);
2643
2706
  } else {
2644
- gl.texSubImage3D(glTarget, level, x, y, z, width, height, depth, glFormat, glType, typedArray, byteOffset);
2707
+ gl.texSubImage3D(glTarget, mipLevel, x, y, z, width, height, depth, glFormat, glType, typedArray, byteOffset);
2645
2708
  }
2646
2709
  break;
2647
2710
  case "2d":
2648
2711
  case "cube":
2649
2712
  if (compressed) {
2650
- gl.compressedTexSubImage2D(glTarget, level, x, y, width, height, glFormat, typedArray, byteOffset);
2713
+ gl.compressedTexSubImage2D(glTarget, mipLevel, x, y, width, height, glFormat, typedArray, byteOffset);
2651
2714
  } else {
2652
- gl.texSubImage2D(glTarget, level, x, y, width, height, glFormat, glType, typedArray, byteOffset);
2715
+ gl.texSubImage2D(glTarget, mipLevel, x, y, width, height, glFormat, glType, typedArray, byteOffset);
2653
2716
  }
2654
2717
  break;
2655
2718
  default:
@@ -2673,9 +2736,105 @@ var __exports__ = (() => {
2673
2736
  }
2674
2737
  throw new Error(dimension);
2675
2738
  }
2676
- function getCubeTargetWebGL(glTarget, dimension, level) {
2739
+ function getWebGLCubeFaceTarget(glTarget, dimension, level) {
2677
2740
  return dimension === "cube" ? 34069 /* TEXTURE_CUBE_MAP_POSITIVE_X */ + level : glTarget;
2678
2741
  }
2742
+ function readPixelsToArray(source, options) {
2743
+ const {
2744
+ sourceX = 0,
2745
+ sourceY = 0,
2746
+ sourceAttachment = 36064 /* COLOR_ATTACHMENT0 */
2747
+ // TODO - support gl.readBuffer
2748
+ } = options || {};
2749
+ let {
2750
+ target = null,
2751
+ // following parameters are auto deduced if not provided
2752
+ sourceWidth,
2753
+ sourceHeight,
2754
+ sourceDepth,
2755
+ sourceFormat,
2756
+ sourceType
2757
+ } = options || {};
2758
+ const { framebuffer, deleteFramebuffer } = getFramebuffer(source);
2759
+ const { gl, handle } = framebuffer;
2760
+ const attachment = sourceAttachment - 36064 /* COLOR_ATTACHMENT0 */;
2761
+ sourceWidth ||= framebuffer.width;
2762
+ sourceHeight ||= framebuffer.height;
2763
+ sourceDepth = framebuffer.colorAttachments[attachment]?.texture?.depth || 1;
2764
+ sourceFormat ||= framebuffer.colorAttachments[attachment]?.texture?.glFormat || 6408 /* RGBA */;
2765
+ sourceType ||= framebuffer.colorAttachments[attachment]?.texture?.glType || 5121 /* UNSIGNED_BYTE */;
2766
+ target = getPixelArray(target, sourceType, sourceFormat, sourceWidth, sourceHeight, sourceDepth);
2767
+ sourceType = sourceType || getGLTypeFromTypedArray(target);
2768
+ const prevHandle = gl.bindFramebuffer(36160 /* FRAMEBUFFER */, handle);
2769
+ gl.readPixels(sourceX, sourceY, sourceWidth, sourceHeight, sourceFormat, sourceType, target);
2770
+ gl.bindFramebuffer(36160 /* FRAMEBUFFER */, prevHandle || null);
2771
+ if (deleteFramebuffer) {
2772
+ framebuffer.destroy();
2773
+ }
2774
+ return target;
2775
+ }
2776
+ function readPixelsToBuffer(source, options) {
2777
+ const {
2778
+ target,
2779
+ sourceX = 0,
2780
+ sourceY = 0,
2781
+ sourceFormat = 6408 /* RGBA */,
2782
+ targetByteOffset = 0
2783
+ } = options || {};
2784
+ let { sourceWidth, sourceHeight, sourceType } = options || {};
2785
+ const { framebuffer, deleteFramebuffer } = getFramebuffer(source);
2786
+ sourceWidth = sourceWidth || framebuffer.width;
2787
+ sourceHeight = sourceHeight || framebuffer.height;
2788
+ const webglFramebuffer = framebuffer;
2789
+ sourceType = sourceType || 5121 /* UNSIGNED_BYTE */;
2790
+ let webglBufferTarget = target;
2791
+ if (!webglBufferTarget) {
2792
+ const components = glFormatToComponents(sourceFormat);
2793
+ const byteCount = glTypeToBytes(sourceType);
2794
+ const byteLength = targetByteOffset + sourceWidth * sourceHeight * components * byteCount;
2795
+ webglBufferTarget = webglFramebuffer.device.createBuffer({ byteLength });
2796
+ }
2797
+ const commandEncoder = source.device.createCommandEncoder();
2798
+ commandEncoder.copyTextureToBuffer({
2799
+ source,
2800
+ width: sourceWidth,
2801
+ height: sourceHeight,
2802
+ origin: [sourceX, sourceY],
2803
+ destination: webglBufferTarget,
2804
+ byteOffset: targetByteOffset
2805
+ });
2806
+ commandEncoder.destroy();
2807
+ if (deleteFramebuffer) {
2808
+ framebuffer.destroy();
2809
+ }
2810
+ return webglBufferTarget;
2811
+ }
2812
+ function getFramebuffer(source) {
2813
+ if (!(source instanceof import_core7.Framebuffer)) {
2814
+ return { framebuffer: toFramebuffer(source), deleteFramebuffer: true };
2815
+ }
2816
+ return { framebuffer: source, deleteFramebuffer: false };
2817
+ }
2818
+ function toFramebuffer(texture, props) {
2819
+ const { device, width, height, id } = texture;
2820
+ const framebuffer = device.createFramebuffer({
2821
+ ...props,
2822
+ id: `framebuffer-for-${id}`,
2823
+ width,
2824
+ height,
2825
+ colorAttachments: [texture]
2826
+ });
2827
+ return framebuffer;
2828
+ }
2829
+ function getPixelArray(pixelArray, type, format, width, height, depth) {
2830
+ if (pixelArray) {
2831
+ return pixelArray;
2832
+ }
2833
+ type = type || 5121 /* UNSIGNED_BYTE */;
2834
+ const ArrayType = getTypedArrayFromGLType(type, { clamped: false });
2835
+ const components = glFormatToComponents(format);
2836
+ return new ArrayType(width * height * components);
2837
+ }
2679
2838
 
2680
2839
  // src/adapter/resources/webgl-texture.ts
2681
2840
  function normalizeTextureData(data, options) {
@@ -2697,7 +2856,7 @@ var __exports__ = (() => {
2697
2856
  }
2698
2857
  return lodArray;
2699
2858
  }
2700
- var WEBGLTexture = class extends import_core7.Texture {
2859
+ var WEBGLTexture = class extends import_core8.Texture {
2701
2860
  MAX_ATTRIBUTES;
2702
2861
  device;
2703
2862
  gl;
@@ -2738,7 +2897,7 @@ var __exports__ = (() => {
2738
2897
  /** For automatically updating video */
2739
2898
  _video = null;
2740
2899
  constructor(device, props) {
2741
- super(device, { ...import_core7.Texture.defaultProps, ...props, data: void 0 });
2900
+ super(device, { ...import_core8.Texture.defaultProps, ...props, data: void 0 });
2742
2901
  this.device = device;
2743
2902
  this.gl = this.device.gl;
2744
2903
  this.glTarget = getWebGLTextureTarget(this.props.dimension);
@@ -2766,7 +2925,7 @@ var __exports__ = (() => {
2766
2925
  const data = props.data;
2767
2926
  let { width, height } = props;
2768
2927
  if (!width || !height) {
2769
- const textureSize = this.getTextureDataSize(data);
2928
+ const textureSize = import_core8.Texture.getTextureDataSize(data);
2770
2929
  width = textureSize?.width || 1;
2771
2930
  height = textureSize?.height || 1;
2772
2931
  }
@@ -2859,7 +3018,7 @@ var __exports__ = (() => {
2859
3018
  }
2860
3019
  /** Update external texture (video frame or canvas) */
2861
3020
  update() {
2862
- import_core7.log.warn("Texture.update() not implemented");
3021
+ import_core8.log.warn("Texture.update() not implemented");
2863
3022
  }
2864
3023
  // Call to regenerate mipmaps after modifying texture(s)
2865
3024
  generateMipmap(params = {}) {
@@ -2874,15 +3033,44 @@ var __exports__ = (() => {
2874
3033
  this.gl.bindTexture(this.glTarget, null);
2875
3034
  }
2876
3035
  // Image Data Setters
3036
+ copyExternalImage(options) {
3037
+ const size = import_core8.Texture.getExternalImageSize(options.image);
3038
+ const opts = { ...import_core8.Texture.defaultCopyExternalImageOptions, ...size, ...options };
3039
+ const { image, depth, mipLevel, x, y, z } = opts;
3040
+ let { width, height } = opts;
3041
+ const { dimension, glTarget, glFormat, glInternalFormat, glType } = this;
3042
+ width = Math.min(width, size.width - x);
3043
+ height = Math.min(height, size.height - y);
3044
+ if (options.sourceX || options.sourceY) {
3045
+ throw new Error(
3046
+ "WebGL does not yet support sourceX/sourceY in copyExternalImage; requires copyTexSubImage2D from a framebuffer"
3047
+ );
3048
+ }
3049
+ copyExternalImageToMipLevel(this.device.gl, this.handle, image, {
3050
+ dimension,
3051
+ mipLevel,
3052
+ x,
3053
+ y,
3054
+ z,
3055
+ width,
3056
+ height,
3057
+ depth,
3058
+ glFormat,
3059
+ glInternalFormat,
3060
+ glType,
3061
+ glTarget
3062
+ });
3063
+ return { width: opts.width, height: opts.height };
3064
+ }
2877
3065
  setTexture1DData(data) {
2878
3066
  throw new Error("setTexture1DData not supported in WebGL.");
2879
3067
  }
2880
3068
  /** Set a simple texture */
2881
- setTexture2DData(lodData, depth = 0, glTarget = this.glTarget) {
3069
+ setTexture2DData(lodData, depth = 0) {
2882
3070
  this.bind();
2883
3071
  const lodArray = normalizeTextureData(lodData, this);
2884
3072
  if (lodArray.length > 1 && this.props.mipmaps !== false) {
2885
- import_core7.log.warn(`Texture ${this.id} mipmap and multiple LODs.`)();
3073
+ import_core8.log.warn(`Texture ${this.id} mipmap and multiple LODs.`)();
2886
3074
  }
2887
3075
  for (let lodLevel = 0; lodLevel < lodArray.length; lodLevel++) {
2888
3076
  const imageData = lodArray[lodLevel];
@@ -2899,7 +3087,9 @@ var __exports__ = (() => {
2899
3087
  throw new Error(this.id);
2900
3088
  }
2901
3089
  if (ArrayBuffer.isView(data)) {
3090
+ this.bind();
2902
3091
  copyCPUDataToMipLevel(this.device.gl, data, this);
3092
+ this.unbind();
2903
3093
  }
2904
3094
  }
2905
3095
  /**
@@ -2912,6 +3102,9 @@ var __exports__ = (() => {
2912
3102
  if (this.props.dimension !== "cube") {
2913
3103
  throw new Error(this.id);
2914
3104
  }
3105
+ for (const face of import_core8.Texture.CubeFaces) {
3106
+ this.setTextureCubeFaceData(data[face], face);
3107
+ }
2915
3108
  }
2916
3109
  /**
2917
3110
  * Sets an entire texture array
@@ -2932,10 +3125,10 @@ var __exports__ = (() => {
2932
3125
  }
2933
3126
  setTextureCubeFaceData(lodData, face, depth = 0) {
2934
3127
  if (Array.isArray(lodData) && lodData.length > 1 && this.props.mipmaps !== false) {
2935
- import_core7.log.warn(`${this.id} has mipmap and multiple LODs.`)();
3128
+ import_core8.log.warn(`${this.id} has mipmap and multiple LODs.`)();
2936
3129
  }
2937
- this.bind();
2938
- this.unbind();
3130
+ const faceDepth = import_core8.Texture.CubeFaces.indexOf(face);
3131
+ this.setTexture2DData(lodData, faceDepth);
2939
3132
  }
2940
3133
  // INTERNAL METHODS
2941
3134
  /** @todo update this method to accept LODs */
@@ -2970,8 +3163,8 @@ var __exports__ = (() => {
2970
3163
  }
2971
3164
  }
2972
3165
  _getImageDataMap(faceData) {
2973
- for (let i = 0; i < import_core7.Texture.CubeFaces.length; ++i) {
2974
- const faceName = import_core7.Texture.CubeFaces[i];
3166
+ for (let i = 0; i < import_core8.Texture.CubeFaces.length; ++i) {
3167
+ const faceName = import_core8.Texture.CubeFaces[i];
2975
3168
  if (faceData[faceName]) {
2976
3169
  faceData[34069 /* TEXTURE_CUBE_MAP_POSITIVE_X */ + i] = faceData[faceName];
2977
3170
  delete faceData[faceName];
@@ -2984,7 +3177,7 @@ var __exports__ = (() => {
2984
3177
  * Sets sampler parameters on texture
2985
3178
  */
2986
3179
  _setSamplerParameters(parameters) {
2987
- import_core7.log.log(1, "texture sampler parameters", parameters)();
3180
+ import_core8.log.log(1, "texture sampler parameters", parameters)();
2988
3181
  this.gl.bindTexture(this.glTarget, this.handle);
2989
3182
  for (const [pname, pvalue] of Object.entries(parameters)) {
2990
3183
  const param = Number(pname);
@@ -3073,16 +3266,22 @@ var __exports__ = (() => {
3073
3266
  * Copy a region of data from a CPU memory buffer into this texture.
3074
3267
  * @todo - GLUnpackParameters parameters
3075
3268
  */
3076
- _setMipLevel(depth, level, textureData, offset = 0) {
3077
- if (import_core7.Texture.isExternalImage(textureData)) {
3078
- copyCPUImageToMipLevel(this.device.gl, textureData, { ...this, depth, level });
3269
+ _setMipLevel(depth, mipLevel, textureData, glTarget = this.glTarget) {
3270
+ if (import_core8.Texture.isExternalImage(textureData)) {
3271
+ copyExternalImageToMipLevel(this.device.gl, this.handle, textureData, {
3272
+ ...this,
3273
+ depth,
3274
+ mipLevel,
3275
+ glTarget
3276
+ });
3079
3277
  return;
3080
3278
  }
3081
- if (this.isTextureLevelData(textureData)) {
3279
+ if (import_core8.Texture.isTextureLevelData(textureData)) {
3082
3280
  copyCPUDataToMipLevel(this.device.gl, textureData.data, {
3083
3281
  ...this,
3084
3282
  depth,
3085
- level
3283
+ mipLevel,
3284
+ glTarget
3086
3285
  });
3087
3286
  return;
3088
3287
  }
@@ -3113,7 +3312,7 @@ var __exports__ = (() => {
3113
3312
  };
3114
3313
 
3115
3314
  // src/adapter/resources/webgl-framebuffer.ts
3116
- var WEBGLFramebuffer = class extends import_core8.Framebuffer {
3315
+ var WEBGLFramebuffer = class extends import_core9.Framebuffer {
3117
3316
  device;
3118
3317
  gl;
3119
3318
  handle;
@@ -3221,7 +3420,7 @@ var __exports__ = (() => {
3221
3420
  switch (texture.glTarget) {
3222
3421
  case 35866 /* TEXTURE_2D_ARRAY */:
3223
3422
  case 32879 /* TEXTURE_3D */:
3224
- gl.framebufferTextureLayer(36160 /* FRAMEBUFFER */, attachment, texture.glTarget, level, layer);
3423
+ gl.framebufferTextureLayer(36160 /* FRAMEBUFFER */, attachment, texture.handle, level, layer);
3225
3424
  break;
3226
3425
  case 34067 /* TEXTURE_CUBE_MAP */:
3227
3426
  const face = mapIndexToCubeMapFace(layer);
@@ -3259,7 +3458,7 @@ var __exports__ = (() => {
3259
3458
  }
3260
3459
 
3261
3460
  // src/adapter/webgl-canvas-context.ts
3262
- var WebGLCanvasContext = class extends import_core9.CanvasContext {
3461
+ var WebGLCanvasContext = class extends import_core10.CanvasContext {
3263
3462
  device;
3264
3463
  format = "rgba8unorm";
3265
3464
  depthStencilFormat = "depth24plus";
@@ -3312,7 +3511,7 @@ var __exports__ = (() => {
3312
3511
  };
3313
3512
 
3314
3513
  // src/context/debug/spector.ts
3315
- var import_core10 = __toESM(require_core(), 1);
3514
+ var import_core11 = __toESM(require_core(), 1);
3316
3515
 
3317
3516
  // src/utils/load-script.ts
3318
3517
  async function loadScript(scriptUrl, scriptId) {
@@ -3334,30 +3533,35 @@ var __exports__ = (() => {
3334
3533
  }
3335
3534
 
3336
3535
  // 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
3536
  var LOG_LEVEL = 1;
3342
3537
  var spector = null;
3343
3538
  var initialized = false;
3539
+ var DEFAULT_SPECTOR_PROPS = {
3540
+ debugWithSpectorJS: import_core11.log.get("spector") || import_core11.log.get("spectorjs"),
3541
+ // https://github.com/BabylonJS/Spector.js#basic-usage
3542
+ // https://forum.babylonjs.com/t/spectorcdn-is-temporarily-off/48241
3543
+ // spectorUrl: 'https://spectorcdn.babylonjs.com/spector.bundle.js';
3544
+ spectorUrl: "https://cdn.jsdelivr.net/npm/spectorjs@0.9.30/dist/spector.bundle.js",
3545
+ gl: void 0
3546
+ };
3344
3547
  async function loadSpectorJS(props) {
3345
3548
  if (!globalThis.SPECTOR) {
3346
3549
  try {
3347
- await loadScript(SPECTOR_CDN_URL);
3550
+ await loadScript(props.spectorUrl || DEFAULT_SPECTOR_PROPS.spectorUrl);
3348
3551
  } catch (error) {
3349
- import_core10.log.warn(String(error));
3552
+ import_core11.log.warn(String(error));
3350
3553
  }
3351
3554
  }
3352
3555
  }
3353
3556
  function initializeSpectorJS(props) {
3354
3557
  props = { ...DEFAULT_SPECTOR_PROPS, ...props };
3355
- if (!props?.spector) {
3558
+ if (!props.debugWithSpectorJS) {
3356
3559
  return null;
3357
3560
  }
3358
- if (!spector && globalThis.SPECTOR) {
3359
- import_core10.log.probe(LOG_LEVEL, "SPECTOR found and initialized")();
3360
- spector = new globalThis.SPECTOR.Spector();
3561
+ if (!spector && globalThis.SPECTOR && !globalThis.luma?.spector) {
3562
+ import_core11.log.probe(LOG_LEVEL, "SPECTOR found and initialized. Start with `luma.spector.displayUI()`")();
3563
+ const { Spector } = globalThis.SPECTOR;
3564
+ spector = new Spector();
3361
3565
  if (globalThis.luma) {
3362
3566
  globalThis.luma.spector = spector;
3363
3567
  }
@@ -3369,22 +3573,22 @@ var __exports__ = (() => {
3369
3573
  initialized = true;
3370
3574
  spector.spyCanvases();
3371
3575
  spector?.onCaptureStarted.add(
3372
- (capture) => import_core10.log.info("Spector capture started:", capture)()
3576
+ (capture) => import_core11.log.info("Spector capture started:", capture)()
3373
3577
  );
3374
3578
  spector?.onCapture.add((capture) => {
3375
- import_core10.log.info("Spector capture complete:", capture)();
3579
+ import_core11.log.info("Spector capture complete:", capture)();
3376
3580
  spector?.getResultUI();
3377
3581
  spector?.resultView.display();
3378
3582
  spector?.resultView.addCapture(capture);
3379
3583
  });
3380
3584
  }
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);
3585
+ if (props.gl) {
3586
+ const gl = props.gl;
3587
+ const device = gl.device;
3588
+ spector?.startCapture(props.gl, 500);
3589
+ gl.device = device;
3386
3590
  new Promise((resolve) => setTimeout(resolve, 2e3)).then((_) => {
3387
- import_core10.log.info("Spector capture stopped after 2 seconds")();
3591
+ import_core11.log.info("Spector capture stopped after 2 seconds")();
3388
3592
  spector?.stopCapture();
3389
3593
  });
3390
3594
  }
@@ -3392,7 +3596,7 @@ var __exports__ = (() => {
3392
3596
  }
3393
3597
 
3394
3598
  // src/context/debug/webgl-developer-tools.ts
3395
- var import_core11 = __toESM(require_core(), 1);
3599
+ var import_core12 = __toESM(require_core(), 1);
3396
3600
 
3397
3601
  // ../../node_modules/@probe.gl/env/dist/lib/globals.js
3398
3602
  var document_ = globalThis.document || {};
@@ -3468,7 +3672,7 @@ var __exports__ = (() => {
3468
3672
  }
3469
3673
  function getDebugContext(gl, props) {
3470
3674
  if (!globalThis.WebGLDebugUtils) {
3471
- import_core11.log.warn("webgl-debug not loaded")();
3675
+ import_core12.log.warn("webgl-debug not loaded")();
3472
3676
  return gl;
3473
3677
  }
3474
3678
  const data = getWebGLContextData(gl);
@@ -3507,7 +3711,7 @@ var __exports__ = (() => {
3507
3711
  const errorMessage = globalThis.WebGLDebugUtils.glEnumToString(err);
3508
3712
  const functionArgs = globalThis.WebGLDebugUtils.glFunctionArgsToString(functionName, args);
3509
3713
  const message2 = `${errorMessage} in gl.${functionName}(${functionArgs})`;
3510
- import_core11.log.error(message2)();
3714
+ import_core12.log.error(message2)();
3511
3715
  debugger;
3512
3716
  if (props.throwOnError) {
3513
3717
  throw new Error(message2);
@@ -3515,9 +3719,9 @@ var __exports__ = (() => {
3515
3719
  }
3516
3720
  function onValidateGLFunc(props, functionName, functionArgs) {
3517
3721
  let functionString = "";
3518
- if (import_core11.log.level >= 1) {
3722
+ if (import_core12.log.level >= 1) {
3519
3723
  functionString = getFunctionString(functionName, functionArgs);
3520
- import_core11.log.log(1, functionString)();
3724
+ import_core12.log.log(1, functionString)();
3521
3725
  }
3522
3726
  if (props.break && props.break.length > 0) {
3523
3727
  functionString = functionString || getFunctionString(functionName, functionArgs);
@@ -3534,16 +3738,24 @@ var __exports__ = (() => {
3534
3738
  if (props.throwOnError) {
3535
3739
  throw new Error(`Undefined argument: ${functionString}`);
3536
3740
  } else {
3537
- import_core11.log.error(`Undefined argument: ${functionString}`)();
3741
+ import_core12.log.error(`Undefined argument: ${functionString}`)();
3538
3742
  debugger;
3539
3743
  }
3540
3744
  }
3541
3745
  }
3542
3746
  }
3543
3747
 
3544
- // src/adapter/resources/webgl-buffer.ts
3545
- var import_core12 = __toESM(require_core(), 1);
3546
- var WEBGLBuffer = class extends import_core12.Buffer {
3748
+ // src/utils/uid.ts
3749
+ var uidCounters = {};
3750
+ function uid(id = "id") {
3751
+ uidCounters[id] = uidCounters[id] || 1;
3752
+ const count = uidCounters[id]++;
3753
+ return `${id}-${count}`;
3754
+ }
3755
+
3756
+ // src/adapter/resources/webgl-buffer.ts
3757
+ var import_core13 = __toESM(require_core(), 1);
3758
+ var WEBGLBuffer = class extends import_core13.Buffer {
3547
3759
  device;
3548
3760
  gl;
3549
3761
  handle;
@@ -3641,32 +3853,32 @@ var __exports__ = (() => {
3641
3853
  }
3642
3854
  };
3643
3855
  function getWebGLTarget(usage) {
3644
- if (usage & import_core12.Buffer.INDEX) {
3856
+ if (usage & import_core13.Buffer.INDEX) {
3645
3857
  return 34963 /* ELEMENT_ARRAY_BUFFER */;
3646
3858
  }
3647
- if (usage & import_core12.Buffer.VERTEX) {
3859
+ if (usage & import_core13.Buffer.VERTEX) {
3648
3860
  return 34962 /* ARRAY_BUFFER */;
3649
3861
  }
3650
- if (usage & import_core12.Buffer.UNIFORM) {
3862
+ if (usage & import_core13.Buffer.UNIFORM) {
3651
3863
  return 35345 /* UNIFORM_BUFFER */;
3652
3864
  }
3653
3865
  return 34962 /* ARRAY_BUFFER */;
3654
3866
  }
3655
3867
  function getWebGLUsage(usage) {
3656
- if (usage & import_core12.Buffer.INDEX) {
3868
+ if (usage & import_core13.Buffer.INDEX) {
3657
3869
  return 35044 /* STATIC_DRAW */;
3658
3870
  }
3659
- if (usage & import_core12.Buffer.VERTEX) {
3871
+ if (usage & import_core13.Buffer.VERTEX) {
3660
3872
  return 35044 /* STATIC_DRAW */;
3661
3873
  }
3662
- if (usage & import_core12.Buffer.UNIFORM) {
3874
+ if (usage & import_core13.Buffer.UNIFORM) {
3663
3875
  return 35048 /* DYNAMIC_DRAW */;
3664
3876
  }
3665
3877
  return 35044 /* STATIC_DRAW */;
3666
3878
  }
3667
3879
 
3668
3880
  // src/adapter/resources/webgl-shader.ts
3669
- var import_core13 = __toESM(require_core(), 1);
3881
+ var import_core14 = __toESM(require_core(), 1);
3670
3882
 
3671
3883
  // src/adapter/helpers/parse-shader-compiler-log.ts
3672
3884
  function parseShaderCompilerLog(errLog) {
@@ -3713,7 +3925,7 @@ var __exports__ = (() => {
3713
3925
  }
3714
3926
 
3715
3927
  // src/adapter/resources/webgl-shader.ts
3716
- var WEBGLShader = class extends import_core13.Shader {
3928
+ var WEBGLShader = class extends import_core14.Shader {
3717
3929
  device;
3718
3930
  handle;
3719
3931
  constructor(device, props) {
@@ -3743,8 +3955,8 @@ var __exports__ = (() => {
3743
3955
  return this.getCompilationInfoSync();
3744
3956
  }
3745
3957
  getCompilationInfoSync() {
3746
- const log9 = this.device.gl.getShaderInfoLog(this.handle);
3747
- return log9 ? parseShaderCompilerLog(log9) : [];
3958
+ const log12 = this.device.gl.getShaderInfoLog(this.handle);
3959
+ return log12 ? parseShaderCompilerLog(log12) : [];
3748
3960
  }
3749
3961
  getTranslatedSource() {
3750
3962
  const extensions = this.device.getExtension("WEBGL_debug_shaders");
@@ -3754,13 +3966,13 @@ var __exports__ = (() => {
3754
3966
  // PRIVATE METHODS
3755
3967
  /** Compile a shader and get compilation status */
3756
3968
  async _compile(source) {
3757
- const addGLSLVersion = (source2) => source2.startsWith("#version ") ? source2 : `#version 100
3969
+ const addGLSLVersion = (source2) => source2.startsWith("#version ") ? source2 : `#version 300 es
3758
3970
  ${source2}`;
3759
3971
  source = addGLSLVersion(source);
3760
3972
  const { gl } = this.device;
3761
3973
  gl.shaderSource(this.handle, source);
3762
3974
  gl.compileShader(this.handle);
3763
- if (import_core13.log.level === 0) {
3975
+ if (import_core14.log.level === 0) {
3764
3976
  this.compilationStatus = "pending";
3765
3977
  return;
3766
3978
  }
@@ -3772,9 +3984,9 @@ ${source2}`;
3772
3984
  }
3773
3985
  return;
3774
3986
  }
3775
- import_core13.log.once(1, "Shader compilation is asynchronous")();
3987
+ import_core14.log.once(1, "Shader compilation is asynchronous")();
3776
3988
  await this._waitForCompilationComplete();
3777
- import_core13.log.info(2, `Shader ${this.id} - async compilation complete: ${this.compilationStatus}`)();
3989
+ import_core14.log.info(2, `Shader ${this.id} - async compilation complete: ${this.compilationStatus}`)();
3778
3990
  this._getCompilationStatus();
3779
3991
  this.debugShader();
3780
3992
  }
@@ -3806,25 +4018,35 @@ ${source2}`;
3806
4018
  };
3807
4019
 
3808
4020
  // src/adapter/resources/webgl-render-pass.ts
3809
- var import_core14 = __toESM(require_core(), 1);
4021
+ var import_core15 = __toESM(require_core(), 1);
3810
4022
  var GL_DEPTH_BUFFER_BIT = 256;
3811
4023
  var GL_STENCIL_BUFFER_BIT = 1024;
3812
4024
  var GL_COLOR_BUFFER_BIT = 16384;
3813
4025
  var GL_COLOR = 6144;
3814
4026
  var COLOR_CHANNELS = [1, 2, 4, 8];
3815
- var WEBGLRenderPass = class extends import_core14.RenderPass {
4027
+ var WEBGLRenderPass = class extends import_core15.RenderPass {
3816
4028
  device;
3817
4029
  /** Parameters that should be applied before each draw call */
3818
4030
  glParameters;
3819
4031
  constructor(device, props) {
3820
4032
  super(device, props);
3821
4033
  this.device = device;
3822
- pushContextState(this.device.gl);
3823
- this.setParameters(this.props.parameters);
4034
+ let viewport;
4035
+ if (!props?.parameters?.viewport) {
4036
+ if (props?.framebuffer) {
4037
+ const { width, height } = props.framebuffer;
4038
+ viewport = [0, 0, width, height];
4039
+ } else {
4040
+ const [width, height] = device.getCanvasContext().getDrawingBufferSize();
4041
+ viewport = [0, 0, width, height];
4042
+ }
4043
+ }
4044
+ this.device.pushState();
4045
+ this.setParameters({ viewport, ...this.props.parameters });
3824
4046
  this.clear();
3825
4047
  }
3826
4048
  end() {
3827
- popContextState(this.device.gl);
4049
+ this.device.popState();
3828
4050
  }
3829
4051
  pushDebugGroup(groupLabel) {
3830
4052
  }
@@ -3946,175 +4168,7 @@ ${source2}`;
3946
4168
  };
3947
4169
 
3948
4170
  // src/adapter/resources/webgl-render-pipeline.ts
3949
- var import_core15 = __toESM(require_core(), 1);
3950
-
3951
- // src/classic/typed-array-utils.ts
3952
- var ERR_TYPE_DEDUCTION = "Failed to deduce GL constant from typed array";
3953
- function getGLTypeFromTypedArray(arrayOrType) {
3954
- const type = ArrayBuffer.isView(arrayOrType) ? arrayOrType.constructor : arrayOrType;
3955
- switch (type) {
3956
- case Float32Array:
3957
- return 5126 /* FLOAT */;
3958
- case Uint16Array:
3959
- return 5123 /* UNSIGNED_SHORT */;
3960
- case Uint32Array:
3961
- return 5125 /* UNSIGNED_INT */;
3962
- case Uint8Array:
3963
- return 5121 /* UNSIGNED_BYTE */;
3964
- case Uint8ClampedArray:
3965
- return 5121 /* UNSIGNED_BYTE */;
3966
- case Int8Array:
3967
- return 5120 /* BYTE */;
3968
- case Int16Array:
3969
- return 5122 /* SHORT */;
3970
- case Int32Array:
3971
- return 5124 /* INT */;
3972
- default:
3973
- throw new Error(ERR_TYPE_DEDUCTION);
3974
- }
3975
- }
3976
- function getTypedArrayFromGLType(glType, options) {
3977
- const { clamped = true } = options || {};
3978
- switch (glType) {
3979
- case 5126 /* FLOAT */:
3980
- return Float32Array;
3981
- case 5123 /* UNSIGNED_SHORT */:
3982
- case 33635 /* UNSIGNED_SHORT_5_6_5 */:
3983
- case 32819 /* UNSIGNED_SHORT_4_4_4_4 */:
3984
- case 32820 /* UNSIGNED_SHORT_5_5_5_1 */:
3985
- return Uint16Array;
3986
- case 5125 /* UNSIGNED_INT */:
3987
- return Uint32Array;
3988
- case 5121 /* UNSIGNED_BYTE */:
3989
- return clamped ? Uint8ClampedArray : Uint8Array;
3990
- case 5120 /* BYTE */:
3991
- return Int8Array;
3992
- case 5122 /* SHORT */:
3993
- return Int16Array;
3994
- case 5124 /* INT */:
3995
- return Int32Array;
3996
- default:
3997
- throw new Error("Failed to deduce typed array type from GL constant");
3998
- }
3999
- }
4000
-
4001
- // src/classic/accessor.ts
4002
- var DEFAULT_ACCESSOR_VALUES = {
4003
- offset: 0,
4004
- stride: 0,
4005
- type: 5126 /* FLOAT */,
4006
- size: 1,
4007
- divisor: 0,
4008
- normalized: false,
4009
- integer: false
4010
- };
4011
- var Accessor = class {
4012
- offset;
4013
- stride;
4014
- type;
4015
- size;
4016
- divisor;
4017
- normalized;
4018
- integer;
4019
- buffer;
4020
- index;
4021
- static getBytesPerElement(accessor) {
4022
- const ArrayType = getTypedArrayFromGLType(accessor.type || 5126 /* FLOAT */);
4023
- return ArrayType.BYTES_PER_ELEMENT;
4024
- }
4025
- static getBytesPerVertex(accessor) {
4026
- const ArrayType = getTypedArrayFromGLType(accessor.type || 5126 /* FLOAT */);
4027
- return ArrayType.BYTES_PER_ELEMENT * accessor.size;
4028
- }
4029
- // Combines (merges) a list of accessors. On top of default values
4030
- // Usually [programAccessor, bufferAccessor, appAccessor]
4031
- // All props will be set in the returned object.
4032
- // TODO check for conflicts between values in the supplied accessors
4033
- static resolve(...accessors) {
4034
- return new Accessor(...[DEFAULT_ACCESSOR_VALUES, ...accessors]);
4035
- }
4036
- constructor(...accessors) {
4037
- accessors.forEach((accessor) => this._assign(accessor));
4038
- Object.freeze(this);
4039
- }
4040
- toString() {
4041
- return JSON.stringify(this);
4042
- }
4043
- // ACCESSORS
4044
- // TODO - remove>
4045
- get BYTES_PER_ELEMENT() {
4046
- return Accessor.getBytesPerElement(this);
4047
- }
4048
- get BYTES_PER_VERTEX() {
4049
- return Accessor.getBytesPerVertex(this);
4050
- }
4051
- // PRIVATE
4052
- // eslint-disable-next-line complexity, max-statements
4053
- _assign(props = {}) {
4054
- if (props.type !== void 0) {
4055
- this.type = props.type;
4056
- if (props.type === 5124 /* INT */ || props.type === 5125 /* UNSIGNED_INT */) {
4057
- this.integer = true;
4058
- }
4059
- }
4060
- if (props.size !== void 0) {
4061
- this.size = props.size;
4062
- }
4063
- if (props.offset !== void 0) {
4064
- this.offset = props.offset;
4065
- }
4066
- if (props.stride !== void 0) {
4067
- this.stride = props.stride;
4068
- }
4069
- if (props.normalize !== void 0) {
4070
- this.normalized = props.normalize;
4071
- }
4072
- if (props.normalized !== void 0) {
4073
- this.normalized = props.normalized;
4074
- }
4075
- if (props.integer !== void 0) {
4076
- this.integer = props.integer;
4077
- }
4078
- if (props.divisor !== void 0) {
4079
- this.divisor = props.divisor;
4080
- }
4081
- if (props.buffer !== void 0) {
4082
- this.buffer = props.buffer;
4083
- }
4084
- if (props.index !== void 0) {
4085
- if (typeof props.index === "boolean") {
4086
- this.index = props.index ? 1 : 0;
4087
- } else {
4088
- this.index = props.index;
4089
- }
4090
- }
4091
- if (props.instanced !== void 0) {
4092
- this.divisor = props.instanced ? 1 : 0;
4093
- }
4094
- if (props.isInstanced !== void 0) {
4095
- this.divisor = props.isInstanced ? 1 : 0;
4096
- }
4097
- if (this.offset === void 0)
4098
- delete this.offset;
4099
- if (this.stride === void 0)
4100
- delete this.stride;
4101
- if (this.type === void 0)
4102
- delete this.type;
4103
- if (this.size === void 0)
4104
- delete this.size;
4105
- if (this.divisor === void 0)
4106
- delete this.divisor;
4107
- if (this.normalized === void 0)
4108
- delete this.normalized;
4109
- if (this.integer === void 0)
4110
- delete this.integer;
4111
- if (this.buffer === void 0)
4112
- delete this.buffer;
4113
- if (this.index === void 0)
4114
- delete this.index;
4115
- return this;
4116
- }
4117
- };
4171
+ var import_core16 = __toESM(require_core(), 1);
4118
4172
 
4119
4173
  // src/adapter/helpers/decode-webgl-types.ts
4120
4174
  function isSamplerUniform(type) {
@@ -4281,8 +4335,7 @@ ${source2}`;
4281
4335
  }
4282
4336
  const { name, type: compositeType, size } = activeInfo;
4283
4337
  const { glType, components } = decodeGLUniformType(compositeType);
4284
- const accessor = new Accessor({ type: glType, size: size * components });
4285
- const varying = { location, name, accessor };
4338
+ const varying = { location, name, type: glType, size: size * components };
4286
4339
  varyings.push(varying);
4287
4340
  }
4288
4341
  varyings.sort((a, b) => a.location - b.location);
@@ -4495,13 +4548,16 @@ ${source2}`;
4495
4548
 
4496
4549
  // ../../node_modules/@math.gl/types/dist/is-array.js
4497
4550
  function isTypedArray(value) {
4498
- return ArrayBuffer.isView(value) && !(value instanceof DataView) ? value : null;
4551
+ return ArrayBuffer.isView(value) && !(value instanceof DataView);
4499
4552
  }
4500
- function isNumericArray(value) {
4553
+ function isNumberArray(value) {
4501
4554
  if (Array.isArray(value)) {
4502
- return value.length === 0 || typeof value[0] === "number" ? value : null;
4555
+ return value.length === 0 || typeof value[0] === "number";
4503
4556
  }
4504
- return isTypedArray(value);
4557
+ return false;
4558
+ }
4559
+ function isNumericArray(value) {
4560
+ return isTypedArray(value) || isNumberArray(value);
4505
4561
  }
4506
4562
 
4507
4563
  // src/utils/split-uniforms-and-bindings.ts
@@ -4557,7 +4613,7 @@ ${source2}`;
4557
4613
 
4558
4614
  // src/adapter/resources/webgl-render-pipeline.ts
4559
4615
  var LOG_PROGRAM_PERF_PRIORITY = 4;
4560
- var WEBGLRenderPipeline = class extends import_core15.RenderPipeline {
4616
+ var WEBGLRenderPipeline = class extends import_core16.RenderPipeline {
4561
4617
  /** The WebGL device that created this render pipeline */
4562
4618
  device;
4563
4619
  /** Handle to underlying WebGL program */
@@ -4590,9 +4646,9 @@ ${source2}`;
4590
4646
  this.device.gl.transformFeedbackVaryings(this.handle, varyings, bufferMode);
4591
4647
  }
4592
4648
  this._linkShaders();
4593
- import_core15.log.time(1, `RenderPipeline ${this.id} - shaderLayout introspection`)();
4649
+ import_core16.log.time(1, `RenderPipeline ${this.id} - shaderLayout introspection`)();
4594
4650
  this.introspectedLayout = getShaderLayout(this.device.gl, this.handle);
4595
- import_core15.log.timeEnd(1, `RenderPipeline ${this.id} - shaderLayout introspection`)();
4651
+ import_core16.log.timeEnd(1, `RenderPipeline ${this.id} - shaderLayout introspection`)();
4596
4652
  this.shaderLayout = mergeShaderLayout(this.introspectedLayout, props.shaderLayout);
4597
4653
  }
4598
4654
  destroy() {
@@ -4611,14 +4667,15 @@ ${source2}`;
4611
4667
  if (!binding) {
4612
4668
  const validBindings = this.shaderLayout.bindings.map((binding2) => `"${binding2.name}"`).join(", ");
4613
4669
  if (!options?.disableWarnings) {
4614
- import_core15.log.warn(
4615
- `Unknown binding "${name}" in render pipeline "${this.id}", expected one of ${validBindings}`
4670
+ import_core16.log.warn(
4671
+ `No binding "${name}" in render pipeline "${this.id}", expected one of ${validBindings}`,
4672
+ value
4616
4673
  )();
4617
4674
  }
4618
4675
  continue;
4619
4676
  }
4620
4677
  if (!value) {
4621
- import_core15.log.warn(`Unsetting binding "${name}" in render pipeline "${this.id}"`)();
4678
+ import_core16.log.warn(`Unsetting binding "${name}" in render pipeline "${this.id}"`)();
4622
4679
  }
4623
4680
  switch (binding.type) {
4624
4681
  case "uniform":
@@ -4632,7 +4689,7 @@ ${source2}`;
4632
4689
  }
4633
4690
  break;
4634
4691
  case "sampler":
4635
- import_core15.log.warn(`Ignoring sampler ${name}`)();
4692
+ import_core16.log.warn(`Ignoring sampler ${name}`)();
4636
4693
  break;
4637
4694
  default:
4638
4695
  throw new Error(binding.type);
@@ -4664,11 +4721,11 @@ ${source2}`;
4664
4721
  const isIndexed = Boolean(vertexArray.indexBuffer);
4665
4722
  const glIndexType = vertexArray.indexBuffer?.glIndexType;
4666
4723
  if (this.linkStatus !== "success") {
4667
- import_core15.log.info(2, `RenderPipeline:${this.id}.draw() aborted - waiting for shader linking`)();
4724
+ import_core16.log.info(2, `RenderPipeline:${this.id}.draw() aborted - waiting for shader linking`)();
4668
4725
  return false;
4669
4726
  }
4670
4727
  if (!this._areTexturesRenderable()) {
4671
- import_core15.log.info(2, `RenderPipeline:${this.id}.draw() aborted - textures not yet loaded`)();
4728
+ import_core16.log.info(2, `RenderPipeline:${this.id}.draw() aborted - textures not yet loaded`)();
4672
4729
  return false;
4673
4730
  }
4674
4731
  this.device.gl.useProgram(this.handle);
@@ -4712,7 +4769,7 @@ ${source2}`;
4712
4769
  setUniformsWebGL(uniforms) {
4713
4770
  const { bindings } = splitUniformsAndBindings(uniforms);
4714
4771
  Object.keys(bindings).forEach((name) => {
4715
- import_core15.log.warn(
4772
+ import_core16.log.warn(
4716
4773
  `Unsupported value "${JSON.stringify(
4717
4774
  bindings[name]
4718
4775
  )}" used in setUniforms() for key ${name}. Use setBindings() instead?`
@@ -4727,19 +4784,19 @@ ${source2}`;
4727
4784
  const { gl } = this.device;
4728
4785
  gl.attachShader(this.handle, this.vs.handle);
4729
4786
  gl.attachShader(this.handle, this.fs.handle);
4730
- import_core15.log.time(LOG_PROGRAM_PERF_PRIORITY, `linkProgram for ${this.id}`)();
4787
+ import_core16.log.time(LOG_PROGRAM_PERF_PRIORITY, `linkProgram for ${this.id}`)();
4731
4788
  gl.linkProgram(this.handle);
4732
- import_core15.log.timeEnd(LOG_PROGRAM_PERF_PRIORITY, `linkProgram for ${this.id}`)();
4733
- if (import_core15.log.level === 0) {
4789
+ import_core16.log.timeEnd(LOG_PROGRAM_PERF_PRIORITY, `linkProgram for ${this.id}`)();
4790
+ if (import_core16.log.level === 0) {
4734
4791
  }
4735
4792
  if (!this.device.features.has("compilation-status-async-webgl")) {
4736
4793
  const status2 = this._getLinkStatus();
4737
4794
  this._reportLinkStatus(status2);
4738
4795
  return;
4739
4796
  }
4740
- import_core15.log.once(1, "RenderPipeline linking is asynchronous")();
4797
+ import_core16.log.once(1, "RenderPipeline linking is asynchronous")();
4741
4798
  await this._waitForLinkComplete();
4742
- import_core15.log.info(2, `RenderPipeline ${this.id} - async linking complete: ${this.linkStatus}`)();
4799
+ import_core16.log.info(2, `RenderPipeline ${this.id} - async linking complete: ${this.linkStatus}`)();
4743
4800
  const status = this._getLinkStatus();
4744
4801
  this._reportLinkStatus(status);
4745
4802
  }
@@ -4807,7 +4864,7 @@ ${source2}`;
4807
4864
  let texturesRenderable = true;
4808
4865
  for (const bindingInfo of this.shaderLayout.bindings) {
4809
4866
  if (!this.bindings[bindingInfo.name] && !this.bindings[bindingInfo.name.replace(/Uniforms$/, "")]) {
4810
- import_core15.log.warn(`Binding ${bindingInfo.name} not found in ${this.id}`)();
4867
+ import_core16.log.warn(`Binding ${bindingInfo.name} not found in ${this.id}`)();
4811
4868
  texturesRenderable = false;
4812
4869
  }
4813
4870
  }
@@ -4866,7 +4923,7 @@ ${source2}`;
4866
4923
  } else if (value instanceof WEBGLTexture) {
4867
4924
  texture = value;
4868
4925
  } else if (value instanceof WEBGLFramebuffer && value.colorAttachments[0] instanceof WEBGLTextureView) {
4869
- import_core15.log.warn(
4926
+ import_core16.log.warn(
4870
4927
  "Passing framebuffer in texture binding may be deprecated. Use fbo.colorAttachments[0] instead"
4871
4928
  )();
4872
4929
  texture = value.colorAttachments[0].texture;
@@ -4907,7 +4964,7 @@ ${source2}`;
4907
4964
  for (const attribute of overrideLayout?.attributes || []) {
4908
4965
  const baseAttribute = mergedLayout.attributes.find((attr) => attr.name === attribute.name);
4909
4966
  if (!baseAttribute) {
4910
- import_core15.log.warn(`shader layout attribute ${attribute.name} not present in shader`);
4967
+ import_core16.log.warn(`shader layout attribute ${attribute.name} not present in shader`);
4911
4968
  } else {
4912
4969
  baseAttribute.type = attribute.type || baseAttribute.type;
4913
4970
  baseAttribute.stepMode = attribute.stepMode || baseAttribute.stepMode;
@@ -4917,11 +4974,11 @@ ${source2}`;
4917
4974
  }
4918
4975
 
4919
4976
  // src/adapter/resources/webgl-command-encoder.ts
4920
- var import_core17 = __toESM(require_core(), 1);
4977
+ var import_core18 = __toESM(require_core(), 1);
4921
4978
 
4922
4979
  // src/adapter/resources/webgl-command-buffer.ts
4923
- var import_core16 = __toESM(require_core(), 1);
4924
- var WEBGLCommandBuffer = class extends import_core16.CommandBuffer {
4980
+ var import_core17 = __toESM(require_core(), 1);
4981
+ var WEBGLCommandBuffer = class extends import_core17.CommandBuffer {
4925
4982
  device;
4926
4983
  commands = [];
4927
4984
  constructor(device) {
@@ -5002,7 +5059,7 @@ ${source2}`;
5002
5059
  if (mipLevel !== 0 || depthOrArrayLayers !== 0 || bytesPerRow || rowsPerImage) {
5003
5060
  throw new Error("not implemented");
5004
5061
  }
5005
- const { framebuffer, destroyFramebuffer } = getFramebuffer(source);
5062
+ const { framebuffer, destroyFramebuffer } = getFramebuffer2(source);
5006
5063
  let prevHandle;
5007
5064
  try {
5008
5065
  const webglBuffer = destination;
@@ -5060,7 +5117,7 @@ ${source2}`;
5060
5117
  height = options.destination.height
5061
5118
  // depthOrArrayLayers = 0
5062
5119
  } = options;
5063
- const { framebuffer, destroyFramebuffer } = getFramebuffer(source);
5120
+ const { framebuffer, destroyFramebuffer } = getFramebuffer2(source);
5064
5121
  const [sourceX, sourceY] = origin;
5065
5122
  const [destinationX, destinationY, destinationZ] = destinationOrigin;
5066
5123
  const prevHandle = device.gl.bindFramebuffer(
@@ -5116,8 +5173,8 @@ ${source2}`;
5116
5173
  framebuffer.destroy();
5117
5174
  }
5118
5175
  }
5119
- function getFramebuffer(source) {
5120
- if (source instanceof import_core16.Texture) {
5176
+ function getFramebuffer2(source) {
5177
+ if (source instanceof import_core17.Texture) {
5121
5178
  const { width, height, id } = source;
5122
5179
  const framebuffer = source.device.createFramebuffer({
5123
5180
  id: `framebuffer-for-${id}`,
@@ -5131,7 +5188,7 @@ ${source2}`;
5131
5188
  }
5132
5189
 
5133
5190
  // src/adapter/resources/webgl-command-encoder.ts
5134
- var WEBGLCommandEncoder = class extends import_core17.CommandEncoder {
5191
+ var WEBGLCommandEncoder = class extends import_core18.CommandEncoder {
5135
5192
  device;
5136
5193
  commandBuffer;
5137
5194
  constructor(device, props) {
@@ -5170,7 +5227,7 @@ ${source2}`;
5170
5227
  };
5171
5228
 
5172
5229
  // src/adapter/resources/webgl-vertex-array.ts
5173
- var import_core18 = __toESM(require_core(), 1);
5230
+ var import_core19 = __toESM(require_core(), 1);
5174
5231
 
5175
5232
  // src/utils/fill-array.ts
5176
5233
  function fillArray(options) {
@@ -5194,7 +5251,7 @@ ${source2}`;
5194
5251
  }
5195
5252
 
5196
5253
  // src/adapter/resources/webgl-vertex-array.ts
5197
- var WEBGLVertexArray = class extends import_core18.VertexArray {
5254
+ var WEBGLVertexArray = class extends import_core19.VertexArray {
5198
5255
  get [Symbol.toStringTag]() {
5199
5256
  return "VertexArray";
5200
5257
  }
@@ -5253,6 +5310,7 @@ ${source2}`;
5253
5310
  } else {
5254
5311
  this.device.gl.vertexAttribPointer(location, size, type, normalized, stride, offset);
5255
5312
  }
5313
+ this.device.gl.bindBuffer(34962 /* ARRAY_BUFFER */, null);
5256
5314
  this.device.gl.enableVertexAttribArray(location);
5257
5315
  this.device.gl.vertexAttribDivisor(location, divisor || 0);
5258
5316
  this.attributes[location] = buffer;
@@ -5361,7 +5419,7 @@ ${source2}`;
5361
5419
  this.buffer = this.buffer || this.device.createBuffer({ byteLength });
5362
5420
  updateNeeded = updateNeeded || !compareConstantArrayValues(constantValue, this.bufferValue);
5363
5421
  if (updateNeeded) {
5364
- const typedArray = (0, import_core18.getScratchArray)(value.constructor, length);
5422
+ const typedArray = (0, import_core19.getScratchArray)(value.constructor, length);
5365
5423
  fillArray({ target: typedArray, source: constantValue, start: 0, count: length });
5366
5424
  this.buffer.write(typedArray);
5367
5425
  this.bufferValue = value;
@@ -5388,8 +5446,8 @@ ${source2}`;
5388
5446
  }
5389
5447
 
5390
5448
  // src/adapter/resources/webgl-transform-feedback.ts
5391
- var import_core19 = __toESM(require_core(), 1);
5392
- var WEBGLTransformFeedback = class extends import_core19.TransformFeedback {
5449
+ var import_core20 = __toESM(require_core(), 1);
5450
+ var WEBGLTransformFeedback = class extends import_core20.TransformFeedback {
5393
5451
  device;
5394
5452
  gl;
5395
5453
  handle;
@@ -5432,7 +5490,7 @@ ${source2}`;
5432
5490
  }
5433
5491
  end() {
5434
5492
  this.gl.endTransformFeedback();
5435
- if (!this.bindOnUse) {
5493
+ if (this.bindOnUse) {
5436
5494
  this._unbindBuffers();
5437
5495
  }
5438
5496
  this.gl.bindTransformFeedback(36386 /* TRANSFORM_FEEDBACK */, null);
@@ -5452,7 +5510,7 @@ ${source2}`;
5452
5510
  const { buffer, byteLength, byteOffset } = this._getBufferRange(bufferOrRange);
5453
5511
  if (location < 0) {
5454
5512
  this.unusedBuffers[locationOrName] = buffer;
5455
- import_core19.log.warn(`${this.id} unusedBuffers varying buffer ${locationOrName}`)();
5513
+ import_core20.log.warn(`${this.id} unusedBuffers varying buffer ${locationOrName}`)();
5456
5514
  return;
5457
5515
  }
5458
5516
  this.buffers[location] = { buffer, byteLength, byteOffset };
@@ -5539,8 +5597,8 @@ ${source2}`;
5539
5597
  }
5540
5598
 
5541
5599
  // src/adapter/resources/webgl-query-set.ts
5542
- var import_core20 = __toESM(require_core(), 1);
5543
- var WEBGLQuerySet = class extends import_core20.QuerySet {
5600
+ var import_core21 = __toESM(require_core(), 1);
5601
+ var WEBGLQuerySet = class extends import_core21.QuerySet {
5544
5602
  device;
5545
5603
  handle;
5546
5604
  target = null;
@@ -5672,155 +5730,24 @@ ${source2}`;
5672
5730
  }
5673
5731
  };
5674
5732
 
5675
- // src/classic/copy-and-blit.ts
5676
- var import_core21 = __toESM(require_core(), 1);
5677
-
5678
- // src/classic/format-utils.ts
5679
- function glFormatToComponents(format) {
5680
- switch (format) {
5681
- case 6406 /* ALPHA */:
5682
- case 33326 /* R32F */:
5683
- case 6403 /* RED */:
5684
- return 1;
5685
- case 33328 /* RG32F */:
5686
- case 33319 /* RG */:
5687
- return 2;
5688
- case 6407 /* RGB */:
5689
- case 34837 /* RGB32F */:
5690
- return 3;
5691
- case 6408 /* RGBA */:
5692
- case 34836 /* RGBA32F */:
5693
- return 4;
5694
- default:
5695
- return 0;
5733
+ // src/deprecated/clear.ts
5734
+ var import_core22 = __toESM(require_core(), 1);
5735
+ var GL_DEPTH_BUFFER_BIT2 = 256;
5736
+ var GL_STENCIL_BUFFER_BIT2 = 1024;
5737
+ var GL_COLOR_BUFFER_BIT2 = 16384;
5738
+ function clear(device, options) {
5739
+ import_core22.log.warn("clear will be removed in next minor release");
5740
+ const { framebuffer = null, color = null, depth = null, stencil = null } = options || {};
5741
+ const parameters = {};
5742
+ if (framebuffer) {
5743
+ parameters.framebuffer = framebuffer;
5696
5744
  }
5697
- }
5698
- function glTypeToBytes(type) {
5699
- switch (type) {
5700
- case 5121 /* UNSIGNED_BYTE */:
5701
- return 1;
5702
- case 33635 /* UNSIGNED_SHORT_5_6_5 */:
5703
- case 32819 /* UNSIGNED_SHORT_4_4_4_4 */:
5704
- case 32820 /* UNSIGNED_SHORT_5_5_5_1 */:
5705
- return 2;
5706
- case 5126 /* FLOAT */:
5707
- return 4;
5708
- default:
5709
- return 0;
5710
- }
5711
- }
5712
-
5713
- // src/classic/copy-and-blit.ts
5714
- function readPixelsToArray(source, options) {
5715
- const {
5716
- sourceX = 0,
5717
- sourceY = 0,
5718
- sourceFormat = 6408 /* RGBA */,
5719
- sourceAttachment = 36064 /* COLOR_ATTACHMENT0 */
5720
- // TODO - support gl.readBuffer
5721
- } = options || {};
5722
- let {
5723
- target = null,
5724
- // following parameters are auto deduced if not provided
5725
- sourceWidth,
5726
- sourceHeight,
5727
- sourceType
5728
- } = options || {};
5729
- const { framebuffer, deleteFramebuffer } = getFramebuffer2(source);
5730
- const { gl, handle } = framebuffer;
5731
- sourceWidth = sourceWidth || framebuffer.width;
5732
- sourceHeight = sourceHeight || framebuffer.height;
5733
- 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);
5736
- sourceType = sourceType || getGLTypeFromTypedArray(target);
5737
- const prevHandle = gl.bindFramebuffer(36160 /* FRAMEBUFFER */, handle);
5738
- gl.readPixels(sourceX, sourceY, sourceWidth, sourceHeight, sourceFormat, sourceType, target);
5739
- gl.bindFramebuffer(36160 /* FRAMEBUFFER */, prevHandle || null);
5740
- if (deleteFramebuffer) {
5741
- framebuffer.destroy();
5742
- }
5743
- return target;
5744
- }
5745
- function readPixelsToBuffer(source, options) {
5746
- const {
5747
- target,
5748
- sourceX = 0,
5749
- sourceY = 0,
5750
- sourceFormat = 6408 /* RGBA */,
5751
- targetByteOffset = 0
5752
- } = options || {};
5753
- let { sourceWidth, sourceHeight, sourceType } = options || {};
5754
- const { framebuffer, deleteFramebuffer } = getFramebuffer2(source);
5755
- sourceWidth = sourceWidth || framebuffer.width;
5756
- sourceHeight = sourceHeight || framebuffer.height;
5757
- const webglFramebuffer = framebuffer;
5758
- sourceType = sourceType || 5121 /* UNSIGNED_BYTE */;
5759
- let webglBufferTarget = target;
5760
- if (!webglBufferTarget) {
5761
- const components = glFormatToComponents(sourceFormat);
5762
- const byteCount = glTypeToBytes(sourceType);
5763
- const byteLength = targetByteOffset + sourceWidth * sourceHeight * components * byteCount;
5764
- webglBufferTarget = webglFramebuffer.device.createBuffer({ byteLength });
5765
- }
5766
- const commandEncoder = source.device.createCommandEncoder();
5767
- commandEncoder.copyTextureToBuffer({
5768
- source,
5769
- width: sourceWidth,
5770
- height: sourceHeight,
5771
- origin: [sourceX, sourceY],
5772
- destination: webglBufferTarget,
5773
- byteOffset: targetByteOffset
5774
- });
5775
- commandEncoder.destroy();
5776
- if (deleteFramebuffer) {
5777
- framebuffer.destroy();
5778
- }
5779
- return webglBufferTarget;
5780
- }
5781
- function getFramebuffer2(source) {
5782
- if (!(source instanceof import_core21.Framebuffer)) {
5783
- return { framebuffer: toFramebuffer(source), deleteFramebuffer: true };
5784
- }
5785
- return { framebuffer: source, deleteFramebuffer: false };
5786
- }
5787
- function toFramebuffer(texture, props) {
5788
- const { device, width, height, id } = texture;
5789
- const framebuffer = device.createFramebuffer({
5790
- ...props,
5791
- id: `framebuffer-for-${id}`,
5792
- width,
5793
- height,
5794
- colorAttachments: [texture]
5795
- });
5796
- return framebuffer;
5797
- }
5798
- function getPixelArray(pixelArray, type, format, width, height) {
5799
- if (pixelArray) {
5800
- return pixelArray;
5801
- }
5802
- type = type || 5121 /* UNSIGNED_BYTE */;
5803
- const ArrayType = getTypedArrayFromGLType(type, { clamped: false });
5804
- const components = glFormatToComponents(format);
5805
- return new ArrayType(width * height * components);
5806
- }
5807
-
5808
- // src/classic/clear.ts
5809
- var GL_DEPTH_BUFFER_BIT2 = 256;
5810
- var GL_STENCIL_BUFFER_BIT2 = 1024;
5811
- var GL_COLOR_BUFFER_BIT2 = 16384;
5812
- function clear(device, options) {
5813
- const { framebuffer = null, color = null, depth = null, stencil = null } = options || {};
5814
- const parameters = {};
5815
- if (framebuffer) {
5816
- parameters.framebuffer = framebuffer;
5817
- }
5818
- let clearFlags = 0;
5819
- if (color) {
5820
- clearFlags |= GL_COLOR_BUFFER_BIT2;
5821
- if (color !== true) {
5822
- parameters.clearColor = color;
5823
- }
5745
+ let clearFlags = 0;
5746
+ if (color) {
5747
+ clearFlags |= GL_COLOR_BUFFER_BIT2;
5748
+ if (color !== true) {
5749
+ parameters.clearColor = color;
5750
+ }
5824
5751
  }
5825
5752
  if (depth) {
5826
5753
  clearFlags |= GL_DEPTH_BUFFER_BIT2;
@@ -5841,8 +5768,10 @@ ${source2}`;
5841
5768
  }
5842
5769
 
5843
5770
  // src/adapter/webgl-device.ts
5844
- var LOG_LEVEL2 = 1;
5845
- var _WebGLDevice = class extends import_core22.Device {
5771
+ var WebGLDevice = class extends import_core23.Device {
5772
+ //
5773
+ // Public `Device` API
5774
+ //
5846
5775
  /** type of this device */
5847
5776
  type = "webgl";
5848
5777
  /** The underlying WebGL context */
@@ -5853,66 +5782,21 @@ ${source2}`;
5853
5782
  canvasContext;
5854
5783
  lost;
5855
5784
  _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
- }
5785
+ /** WebGL2 context. */
5786
+ gl;
5787
+ debug = false;
5788
+ /** State used by luma.gl classes: TODO - move to canvasContext*/
5789
+ _canvasSizeInfo = { clientWidth: 0, clientHeight: 0, devicePixelRatio: 1 };
5790
+ /** State used by luma.gl classes - TODO - not used? */
5791
+ _extensions = {};
5792
+ _polyfilled = false;
5793
+ /** Instance of Spector.js (if initialized) */
5794
+ spectorJS;
5911
5795
  //
5912
5796
  // Public API
5913
5797
  //
5914
5798
  constructor(props) {
5915
- super({ ...props, id: props.id || "webgl-device" });
5799
+ super({ ...props, id: props.id || uid("webgl-device") });
5916
5800
  const device = props.gl?.device;
5917
5801
  if (device) {
5918
5802
  throw new Error(`WebGL context already attached to device ${device.id}`);
@@ -5922,19 +5806,18 @@ ${source2}`;
5922
5806
  this.lost = new Promise((resolve) => {
5923
5807
  this._resolveContextLost = resolve;
5924
5808
  });
5925
- let gl = props.gl || null;
5926
- gl ||= createBrowserContext(this.canvasContext.canvas, {
5809
+ this.handle = createBrowserContext(this.canvasContext.canvas, {
5927
5810
  ...props,
5928
5811
  onContextLost: (event) => this._resolveContextLost?.({
5929
5812
  reason: "destroyed",
5930
5813
  message: "Entered sleep mode, or too many apps or browser tabs are using the GPU."
5931
5814
  })
5932
5815
  });
5933
- if (!gl) {
5816
+ this.gl = this.handle;
5817
+ if (!this.handle) {
5934
5818
  throw new Error("WebGL context creation failed");
5935
5819
  }
5936
- this.handle = gl;
5937
- this.gl = gl;
5820
+ this.spectorJS = initializeSpectorJS({ ...this.props, gl: this.handle });
5938
5821
  this.gl.device = this;
5939
5822
  this.gl._version = 2;
5940
5823
  this.info = getDeviceInfo(this.gl, this._extensions);
@@ -5944,20 +5827,15 @@ ${source2}`;
5944
5827
  this.features.initializeFeatures();
5945
5828
  }
5946
5829
  this.canvasContext.resize();
5947
- const { enable: enable2 = true, copyState = false } = props;
5948
- trackContextState(this.gl, {
5949
- enable: enable2,
5950
- copyState,
5951
- log: (...args) => import_core22.log.log(1, ...args)()
5830
+ const glState = new WebGLStateTracker(this.gl, {
5831
+ log: (...args) => import_core23.log.log(1, ...args)()
5952
5832
  });
5833
+ glState.trackState(this.gl, { copyState: false });
5953
5834
  if (props.debug) {
5954
5835
  this.gl = makeDebugContext(this.gl, { ...props, throwOnError: true });
5955
5836
  this.debug = true;
5956
- import_core22.log.level = Math.max(import_core22.log.level, 1);
5957
- import_core22.log.warn("WebGL debug mode activated. Performance reduced.")();
5958
- }
5959
- if (props.spector) {
5960
- this.spectorJS = initializeSpectorJS({ ...this.props, canvas: this.handle.canvas });
5837
+ import_core23.log.level = Math.max(import_core23.log.level, 1);
5838
+ import_core23.log.warn("WebGL debug mode activated. Performance reduced.")();
5961
5839
  }
5962
5840
  }
5963
5841
  /**
@@ -5969,9 +5847,6 @@ ${source2}`;
5969
5847
  get isLost() {
5970
5848
  return this.gl.isContextLost();
5971
5849
  }
5972
- getSize() {
5973
- return [this.gl.drawingBufferWidth, this.gl.drawingBufferHeight];
5974
- }
5975
5850
  isTextureFormatSupported(format) {
5976
5851
  return isTextureFormatSupported(this.gl, format, this._extensions);
5977
5852
  }
@@ -6063,22 +5938,12 @@ ${source2}`;
6063
5938
  clear(this, options);
6064
5939
  }
6065
5940
  resetWebGL() {
6066
- import_core22.log.warn("WebGLDevice.resetWebGL is deprecated, use only for debugging")();
5941
+ import_core23.log.warn("WebGLDevice.resetWebGL is deprecated, use only for debugging")();
6067
5942
  resetGLParameters(this.gl);
6068
5943
  }
6069
5944
  //
6070
5945
  // WebGL-only API (not part of `Device` API)
6071
5946
  //
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
5947
  /**
6083
5948
  * Triggers device (or WebGL context) loss.
6084
5949
  * @note primarily intended for testing how application reacts to device loss
@@ -6099,11 +5964,13 @@ ${source2}`;
6099
5964
  }
6100
5965
  /** Save current WebGL context state onto an internal stack */
6101
5966
  pushState() {
6102
- pushContextState(this.gl);
5967
+ const webglState = WebGLStateTracker.get(this.gl);
5968
+ webglState.push();
6103
5969
  }
6104
5970
  /** Restores previously saved context state */
6105
5971
  popState() {
6106
- popContextState(this.gl);
5972
+ const webglState = WebGLStateTracker.get(this.gl);
5973
+ webglState.pop();
6107
5974
  }
6108
5975
  /**
6109
5976
  * Storing data on a special field on WebGLObjects makes that data visible in SPECTOR chrome debug extension
@@ -6140,7 +6007,7 @@ ${source2}`;
6140
6007
  this._constants = this._constants || new Array(maxVertexAttributes).fill(null);
6141
6008
  const currentConstant = this._constants[location];
6142
6009
  if (currentConstant && compareConstantArrayValues2(currentConstant, constant)) {
6143
- import_core22.log.info(
6010
+ import_core23.log.info(
6144
6011
  1,
6145
6012
  `setConstantAttributeWebGL(${location}) could have been skipped, value unchanged`
6146
6013
  )();
@@ -6166,18 +6033,6 @@ ${source2}`;
6166
6033
  return this._extensions;
6167
6034
  }
6168
6035
  };
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
6036
  function setConstantFloatArray(device, location, array) {
6182
6037
  switch (array.length) {
6183
6038
  case 1:
@@ -6212,6 +6067,325 @@ ${source2}`;
6212
6067
  }
6213
6068
  return true;
6214
6069
  }
6070
+
6071
+ // src/context/polyfills/polyfill-webgl1-extensions.ts
6072
+ var WEBGL1_STATIC_EXTENSIONS = {
6073
+ WEBGL_depth_texture: {
6074
+ UNSIGNED_INT_24_8_WEBGL: 34042 /* UNSIGNED_INT_24_8 */
6075
+ },
6076
+ OES_element_index_uint: {},
6077
+ OES_texture_float: {},
6078
+ OES_texture_half_float: {
6079
+ // @ts-expect-error different numbers?
6080
+ HALF_FLOAT_OES: 5131 /* HALF_FLOAT */
6081
+ },
6082
+ EXT_color_buffer_float: {},
6083
+ OES_standard_derivatives: {
6084
+ FRAGMENT_SHADER_DERIVATIVE_HINT_OES: 35723 /* FRAGMENT_SHADER_DERIVATIVE_HINT */
6085
+ },
6086
+ EXT_frag_depth: {},
6087
+ EXT_blend_minmax: {
6088
+ MIN_EXT: 32775 /* MIN */,
6089
+ MAX_EXT: 32776 /* MAX */
6090
+ },
6091
+ EXT_shader_texture_lod: {}
6092
+ };
6093
+ var getWEBGL_draw_buffers = (gl) => ({
6094
+ drawBuffersWEBGL(buffers) {
6095
+ return gl.drawBuffers(buffers);
6096
+ },
6097
+ COLOR_ATTACHMENT0_WEBGL: 36064 /* COLOR_ATTACHMENT0 */,
6098
+ COLOR_ATTACHMENT1_WEBGL: 36065 /* COLOR_ATTACHMENT1 */,
6099
+ COLOR_ATTACHMENT2_WEBGL: 36066 /* COLOR_ATTACHMENT2 */,
6100
+ COLOR_ATTACHMENT3_WEBGL: 36067 /* COLOR_ATTACHMENT3 */
6101
+ });
6102
+ var getOES_vertex_array_object = (gl) => ({
6103
+ VERTEX_ARRAY_BINDING_OES: 34229 /* VERTEX_ARRAY_BINDING */,
6104
+ createVertexArrayOES() {
6105
+ return gl.createVertexArray();
6106
+ },
6107
+ deleteVertexArrayOES(vertexArray) {
6108
+ return gl.deleteVertexArray(vertexArray);
6109
+ },
6110
+ isVertexArrayOES(vertexArray) {
6111
+ return gl.isVertexArray(vertexArray);
6112
+ },
6113
+ bindVertexArrayOES(vertexArray) {
6114
+ return gl.bindVertexArray(vertexArray);
6115
+ }
6116
+ });
6117
+ var getANGLE_instanced_arrays = (gl) => ({
6118
+ VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE: 35070,
6119
+ drawArraysInstancedANGLE(...args) {
6120
+ return gl.drawArraysInstanced(...args);
6121
+ },
6122
+ drawElementsInstancedANGLE(...args) {
6123
+ return gl.drawElementsInstanced(...args);
6124
+ },
6125
+ vertexAttribDivisorANGLE(...args) {
6126
+ return gl.vertexAttribDivisor(...args);
6127
+ }
6128
+ });
6129
+ function enforceWebGL2(enforce = true) {
6130
+ const prototype = HTMLCanvasElement.prototype;
6131
+ if (!enforce && prototype.originalGetContext) {
6132
+ prototype.getContext = prototype.originalGetContext;
6133
+ prototype.originalGetContext = void 0;
6134
+ return;
6135
+ }
6136
+ prototype.originalGetContext = prototype.getContext;
6137
+ prototype.getContext = function(contextId, options) {
6138
+ if (contextId === "webgl" || contextId === "experimental-webgl") {
6139
+ const context = this.originalGetContext("webgl2", options);
6140
+ if (context instanceof HTMLElement) {
6141
+ polyfillWebGL1Extensions(context);
6142
+ }
6143
+ return context;
6144
+ }
6145
+ return this.originalGetContext(contextId, options);
6146
+ };
6147
+ }
6148
+ function polyfillWebGL1Extensions(gl) {
6149
+ gl.getExtension("EXT_color_buffer_float");
6150
+ const boundExtensions = {
6151
+ ...WEBGL1_STATIC_EXTENSIONS,
6152
+ WEBGL_disjoint_timer_query: gl.getExtension("EXT_disjoint_timer_query_webgl2"),
6153
+ WEBGL_draw_buffers: getWEBGL_draw_buffers(gl),
6154
+ OES_vertex_array_object: getOES_vertex_array_object(gl),
6155
+ ANGLE_instanced_arrays: getANGLE_instanced_arrays(gl)
6156
+ };
6157
+ const originalGetExtension = gl.getExtension;
6158
+ gl.getExtension = function(extensionName) {
6159
+ const ext = originalGetExtension.call(gl, extensionName);
6160
+ if (ext) {
6161
+ return ext;
6162
+ }
6163
+ if (extensionName in boundExtensions) {
6164
+ return boundExtensions[extensionName];
6165
+ }
6166
+ return null;
6167
+ };
6168
+ const originalGetSupportedExtensions = gl.getSupportedExtensions;
6169
+ gl.getSupportedExtensions = function() {
6170
+ const extensions = originalGetSupportedExtensions.apply(gl) || [];
6171
+ return extensions?.concat(Object.keys(boundExtensions));
6172
+ };
6173
+ }
6174
+
6175
+ // src/adapter/webgl-adapter.ts
6176
+ var LOG_LEVEL2 = 1;
6177
+ var WebGLAdapter = class extends import_core24.Adapter {
6178
+ /** type of device's created by this adapter */
6179
+ type = "webgl";
6180
+ constructor() {
6181
+ super();
6182
+ import_core24.Device.defaultProps = { ...import_core24.Device.defaultProps, ...DEFAULT_SPECTOR_PROPS };
6183
+ WebGLDevice.adapter = this;
6184
+ }
6185
+ /** Check if WebGL 2 is available */
6186
+ isSupported() {
6187
+ return typeof WebGL2RenderingContext !== "undefined";
6188
+ }
6189
+ /** Force any created WebGL contexts to be WebGL2 contexts, polyfilled with WebGL1 extensions */
6190
+ enforceWebGL2(enable2) {
6191
+ enforceWebGL2(enable2);
6192
+ }
6193
+ /**
6194
+ * Get a device instance from a GL context
6195
+ * Creates and instruments the device if not already created
6196
+ * @param gl
6197
+ * @returns
6198
+ */
6199
+ async attach(gl) {
6200
+ if (gl instanceof WebGLDevice) {
6201
+ return gl;
6202
+ }
6203
+ if (gl?.device instanceof import_core24.Device) {
6204
+ return gl.device;
6205
+ }
6206
+ if (!isWebGL(gl)) {
6207
+ throw new Error("Invalid WebGL2RenderingContext");
6208
+ }
6209
+ return new WebGLDevice({ gl });
6210
+ }
6211
+ async create(props = {}) {
6212
+ import_core24.log.groupCollapsed(LOG_LEVEL2, "WebGLDevice created")();
6213
+ const promises = [];
6214
+ if (props.debug) {
6215
+ promises.push(loadWebGLDeveloperTools());
6216
+ }
6217
+ if (props.debugWithSpectorJS) {
6218
+ promises.push(loadSpectorJS(props));
6219
+ }
6220
+ if (typeof props.canvas === "string") {
6221
+ promises.push(import_core24.CanvasContext.pageLoaded);
6222
+ }
6223
+ const results = await Promise.allSettled(promises);
6224
+ for (const result of results) {
6225
+ if (result.status === "rejected") {
6226
+ import_core24.log.error(`Failed to initialize debug libraries ${result.reason}`)();
6227
+ }
6228
+ }
6229
+ import_core24.log.probe(LOG_LEVEL2 + 1, "DOM is loaded")();
6230
+ const device = new WebGLDevice(props);
6231
+ const message2 = `Created ${device.type}${device.debug ? " debug" : ""} context: ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContext.id}`;
6232
+ import_core24.log.probe(LOG_LEVEL2, message2)();
6233
+ import_core24.log.table(LOG_LEVEL2, device.info)();
6234
+ import_core24.log.groupEnd(LOG_LEVEL2)();
6235
+ return device;
6236
+ }
6237
+ };
6238
+ function isWebGL(gl) {
6239
+ if (typeof WebGL2RenderingContext !== "undefined" && gl instanceof WebGL2RenderingContext) {
6240
+ return true;
6241
+ }
6242
+ return Boolean(gl && Number.isFinite(gl._version));
6243
+ }
6244
+ var webgl2Adapter = new WebGLAdapter();
6245
+
6246
+ // src/deprecated/accessor.ts
6247
+ var import_core25 = __toESM(require_core(), 1);
6248
+ var DEFAULT_ACCESSOR_VALUES = {
6249
+ offset: 0,
6250
+ stride: 0,
6251
+ type: 5126 /* FLOAT */,
6252
+ size: 1,
6253
+ divisor: 0,
6254
+ normalized: false,
6255
+ integer: false
6256
+ };
6257
+ var Accessor = class {
6258
+ offset;
6259
+ stride;
6260
+ type;
6261
+ size;
6262
+ divisor;
6263
+ normalized;
6264
+ integer;
6265
+ buffer;
6266
+ index;
6267
+ static getBytesPerElement(accessor) {
6268
+ const ArrayType = getTypedArrayFromGLType2(accessor.type || 5126 /* FLOAT */);
6269
+ return ArrayType.BYTES_PER_ELEMENT;
6270
+ }
6271
+ static getBytesPerVertex(accessor) {
6272
+ const ArrayType = getTypedArrayFromGLType2(accessor.type || 5126 /* FLOAT */);
6273
+ return ArrayType.BYTES_PER_ELEMENT * accessor.size;
6274
+ }
6275
+ // Combines (merges) a list of accessors. On top of default values
6276
+ // Usually [programAccessor, bufferAccessor, appAccessor]
6277
+ // All props will be set in the returned object.
6278
+ // TODO check for conflicts between values in the supplied accessors
6279
+ static resolve(...accessors) {
6280
+ return new Accessor(...[DEFAULT_ACCESSOR_VALUES, ...accessors]);
6281
+ }
6282
+ constructor(...accessors) {
6283
+ import_core25.log.warn("Accessor will be removed in next minor release");
6284
+ accessors.forEach((accessor) => this._assign(accessor));
6285
+ Object.freeze(this);
6286
+ }
6287
+ toString() {
6288
+ return JSON.stringify(this);
6289
+ }
6290
+ // ACCESSORS
6291
+ // TODO - remove>
6292
+ get BYTES_PER_ELEMENT() {
6293
+ return Accessor.getBytesPerElement(this);
6294
+ }
6295
+ get BYTES_PER_VERTEX() {
6296
+ return Accessor.getBytesPerVertex(this);
6297
+ }
6298
+ // PRIVATE
6299
+ // eslint-disable-next-line complexity, max-statements
6300
+ _assign(props = {}) {
6301
+ if (props.type !== void 0) {
6302
+ this.type = props.type;
6303
+ if (props.type === 5124 /* INT */ || props.type === 5125 /* UNSIGNED_INT */) {
6304
+ this.integer = true;
6305
+ }
6306
+ }
6307
+ if (props.size !== void 0) {
6308
+ this.size = props.size;
6309
+ }
6310
+ if (props.offset !== void 0) {
6311
+ this.offset = props.offset;
6312
+ }
6313
+ if (props.stride !== void 0) {
6314
+ this.stride = props.stride;
6315
+ }
6316
+ if (props.normalize !== void 0) {
6317
+ this.normalized = props.normalize;
6318
+ }
6319
+ if (props.normalized !== void 0) {
6320
+ this.normalized = props.normalized;
6321
+ }
6322
+ if (props.integer !== void 0) {
6323
+ this.integer = props.integer;
6324
+ }
6325
+ if (props.divisor !== void 0) {
6326
+ this.divisor = props.divisor;
6327
+ }
6328
+ if (props.buffer !== void 0) {
6329
+ this.buffer = props.buffer;
6330
+ }
6331
+ if (props.index !== void 0) {
6332
+ if (typeof props.index === "boolean") {
6333
+ this.index = props.index ? 1 : 0;
6334
+ } else {
6335
+ this.index = props.index;
6336
+ }
6337
+ }
6338
+ if (props.instanced !== void 0) {
6339
+ this.divisor = props.instanced ? 1 : 0;
6340
+ }
6341
+ if (props.isInstanced !== void 0) {
6342
+ this.divisor = props.isInstanced ? 1 : 0;
6343
+ }
6344
+ if (this.offset === void 0)
6345
+ delete this.offset;
6346
+ if (this.stride === void 0)
6347
+ delete this.stride;
6348
+ if (this.type === void 0)
6349
+ delete this.type;
6350
+ if (this.size === void 0)
6351
+ delete this.size;
6352
+ if (this.divisor === void 0)
6353
+ delete this.divisor;
6354
+ if (this.normalized === void 0)
6355
+ delete this.normalized;
6356
+ if (this.integer === void 0)
6357
+ delete this.integer;
6358
+ if (this.buffer === void 0)
6359
+ delete this.buffer;
6360
+ if (this.index === void 0)
6361
+ delete this.index;
6362
+ return this;
6363
+ }
6364
+ };
6365
+ function getTypedArrayFromGLType2(glType, options) {
6366
+ const { clamped = true } = options || {};
6367
+ switch (glType) {
6368
+ case 5126 /* FLOAT */:
6369
+ return Float32Array;
6370
+ case 5123 /* UNSIGNED_SHORT */:
6371
+ case 33635 /* UNSIGNED_SHORT_5_6_5 */:
6372
+ case 32819 /* UNSIGNED_SHORT_4_4_4_4 */:
6373
+ case 32820 /* UNSIGNED_SHORT_5_5_5_1 */:
6374
+ return Uint16Array;
6375
+ case 5125 /* UNSIGNED_INT */:
6376
+ return Uint32Array;
6377
+ case 5121 /* UNSIGNED_BYTE */:
6378
+ return clamped ? Uint8ClampedArray : Uint8Array;
6379
+ case 5120 /* BYTE */:
6380
+ return Int8Array;
6381
+ case 5122 /* SHORT */:
6382
+ return Int16Array;
6383
+ case 5124 /* INT */:
6384
+ return Int32Array;
6385
+ default:
6386
+ throw new Error("Failed to deduce typed array type from GL constant");
6387
+ }
6388
+ }
6215
6389
  return __toCommonJS(bundle_exports);
6216
6390
  })();
6217
6391
  return __exports__;