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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/dist/adapter/converters/device-parameters.d.ts.map +1 -1
  2. package/dist/adapter/converters/device-parameters.js +18 -11
  3. package/dist/adapter/converters/texture-formats.d.ts +1 -1
  4. package/dist/adapter/converters/texture-formats.d.ts.map +1 -1
  5. package/dist/adapter/converters/texture-formats.js +9 -16
  6. package/dist/adapter/device-helpers/webgl-device-features.d.ts.map +1 -1
  7. package/dist/adapter/device-helpers/webgl-device-features.js +0 -1
  8. package/dist/adapter/helpers/webgl-texture-utils.d.ts +4 -4
  9. package/dist/adapter/helpers/webgl-texture-utils.d.ts.map +1 -1
  10. package/dist/adapter/helpers/webgl-texture-utils.js +11 -9
  11. package/dist/adapter/resources/webgl-framebuffer.js +1 -1
  12. package/dist/adapter/resources/webgl-render-pass.d.ts.map +1 -1
  13. package/dist/adapter/resources/webgl-render-pass.js +17 -4
  14. package/dist/adapter/resources/webgl-render-pipeline.d.ts.map +1 -1
  15. package/dist/adapter/resources/webgl-render-pipeline.js +1 -1
  16. package/dist/adapter/resources/webgl-shader.js +1 -1
  17. package/dist/adapter/resources/webgl-texture.d.ts +2 -2
  18. package/dist/adapter/resources/webgl-texture.d.ts.map +1 -1
  19. package/dist/adapter/resources/webgl-texture.js +12 -27
  20. package/dist/adapter/webgl-adapter.d.ts +21 -0
  21. package/dist/adapter/webgl-adapter.d.ts.map +1 -0
  22. package/dist/adapter/webgl-adapter.js +91 -0
  23. package/dist/adapter/webgl-device.d.ts +15 -27
  24. package/dist/adapter/webgl-device.d.ts.map +1 -1
  25. package/dist/adapter/webgl-device.js +32 -112
  26. package/dist/classic/copy-and-blit.d.ts +1 -0
  27. package/dist/classic/copy-and-blit.d.ts.map +1 -1
  28. package/dist/classic/copy-and-blit.js +11 -10
  29. package/dist/context/debug/spector-types.d.ts +1108 -0
  30. package/dist/context/debug/spector-types.d.ts.map +1 -0
  31. package/dist/context/debug/spector-types.js +697 -0
  32. package/dist/context/debug/spector.d.ts +12 -8
  33. package/dist/context/debug/spector.d.ts.map +1 -1
  34. package/dist/context/debug/spector.js +23 -17
  35. package/dist/context/polyfills/polyfill-webgl1-extensions.d.ts +9 -0
  36. package/dist/context/polyfills/polyfill-webgl1-extensions.d.ts.map +1 -0
  37. package/dist/context/polyfills/polyfill-webgl1-extensions.js +181 -0
  38. package/dist/context/state-tracker/webgl-state-tracker.d.ts +43 -0
  39. package/dist/context/state-tracker/webgl-state-tracker.d.ts.map +1 -0
  40. package/dist/context/state-tracker/{track-context-state.js → webgl-state-tracker.js} +44 -74
  41. package/dist/context/state-tracker/with-parameters.d.ts.map +1 -1
  42. package/dist/context/state-tracker/with-parameters.js +5 -4
  43. package/dist/dist.dev.js +339 -233
  44. package/dist/dist.min.js +2 -2
  45. package/dist/index.cjs +342 -235
  46. package/dist/index.cjs.map +4 -4
  47. package/dist/index.d.ts +3 -1
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +3 -2
  50. package/dist/utils/uid.d.ts +7 -0
  51. package/dist/utils/uid.d.ts.map +1 -0
  52. package/dist/utils/uid.js +14 -0
  53. package/package.json +3 -3
  54. package/src/adapter/converters/device-parameters.ts +18 -12
  55. package/src/adapter/converters/texture-formats.ts +12 -20
  56. package/src/adapter/device-helpers/webgl-device-features.ts +0 -1
  57. package/src/adapter/helpers/webgl-texture-utils.ts +12 -9
  58. package/src/adapter/resources/webgl-framebuffer.ts +1 -1
  59. package/src/adapter/resources/webgl-render-pass.ts +17 -4
  60. package/src/adapter/resources/webgl-render-pipeline.ts +2 -1
  61. package/src/adapter/resources/webgl-shader.ts +1 -1
  62. package/src/adapter/resources/webgl-texture.ts +17 -27
  63. package/src/adapter/webgl-adapter.ts +113 -0
  64. package/src/adapter/webgl-device.ts +32 -135
  65. package/src/classic/copy-and-blit.ts +14 -9
  66. package/src/context/debug/spector-types.ts +1154 -0
  67. package/src/context/debug/spector.ts +38 -29
  68. package/src/context/polyfills/polyfill-webgl1-extensions.ts +202 -0
  69. package/src/context/state-tracker/{track-context-state.ts → webgl-state-tracker.ts} +55 -94
  70. package/src/context/state-tracker/with-parameters.ts +5 -4
  71. package/src/index.ts +5 -7
  72. package/src/utils/uid.ts +16 -0
  73. package/dist/context/state-tracker/track-context-state.d.ts +0 -22
  74. package/dist/context/state-tracker/track-context-state.d.ts.map +0 -1
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export type { WebGLDeviceLimits } from "./adapter/device-helpers/webgl-device-limits.js";
2
+ export { webgl2Adapter } from "./adapter/webgl-adapter.js";
3
+ export type { WebGLAdapter } from "./adapter/webgl-adapter.js";
2
4
  export { WebGLDevice } from "./adapter/webgl-device.js";
3
5
  export { WebGLCanvasContext } from "./adapter/webgl-canvas-context.js";
4
6
  export { WEBGLBuffer } from "./adapter/resources/webgl-buffer.js";
@@ -15,8 +17,8 @@ export { Accessor } from "./classic/accessor.js";
15
17
  export type { AccessorObject } from "./types.js";
16
18
  export { setDeviceParameters, withDeviceParameters } from "./adapter/converters/device-parameters.js";
17
19
  export { getShaderLayout } from "./adapter/helpers/get-shader-layout.js";
20
+ export { WebGLStateTracker } from "./context/state-tracker/webgl-state-tracker.js";
18
21
  export { TEXTURE_FORMATS as _TEXTURE_FORMATS } from "./adapter/converters/texture-formats.js";
19
- export { trackContextState, pushContextState, popContextState } from "./context/state-tracker/track-context-state.js";
20
22
  export { resetGLParameters, setGLParameters, getGLParameters } from "./context/parameters/unified-parameter-api.js";
21
23
  export { withGLParameters } from "./context/state-tracker/with-parameters.js";
22
24
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,YAAY,EAAC,iBAAiB,EAAC,wDAAqD;AAGpF,OAAO,EAAC,WAAW,EAAC,kCAA+B;AACnD,OAAO,EAAC,kBAAkB,EAAC,0CAAuC;AAGlE,OAAO,EAAC,WAAW,EAAC,4CAAyC;AAC7D,OAAO,EAAC,YAAY,EAAC,6CAA0C;AAE/D,OAAO,EAAC,WAAW,EAAC,4CAAyC;AAC7D,OAAO,EAAC,YAAY,EAAC,6CAA0C;AAC/D,OAAO,EAAC,gBAAgB,EAAC,iDAA8C;AAEvE,OAAO,EAAC,mBAAmB,EAAC,qDAAkD;AAE9E,OAAO,EAAC,mBAAmB,EAAC,qDAAkD;AAC9E,OAAO,EAAC,eAAe,EAAC,iDAA8C;AAEtE,OAAO,EAAC,gBAAgB,EAAC,kDAA+C;AAGxE,OAAO,EAAC,sBAAsB,EAAC,wDAAqD;AAGpF,OAAO,EAAC,QAAQ,EAAC,8BAA2B;AAC5C,YAAY,EAAC,cAAc,EAAC,mBAAgB;AAI5C,OAAO,EAAC,mBAAmB,EAAE,oBAAoB,EAAC,kDAA+C;AAGjG,OAAO,EAAC,eAAe,EAAC,+CAA4C;AAGpE,OAAO,EAAC,eAAe,IAAI,gBAAgB,EAAC,gDAA6C;AAIzF,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,eAAe,EAChB,uDAAoD;AAErD,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,eAAe,EAChB,sDAAmD;AAEpD,OAAO,EAAC,gBAAgB,EAAC,mDAAgD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAWA,YAAY,EAAC,iBAAiB,EAAC,wDAAqD;AAGpF,OAAO,EAAC,aAAa,EAAC,mCAAgC;AACtD,YAAY,EAAC,YAAY,EAAC,mCAAgC;AAG1D,OAAO,EAAC,WAAW,EAAC,kCAA+B;AACnD,OAAO,EAAC,kBAAkB,EAAC,0CAAuC;AAGlE,OAAO,EAAC,WAAW,EAAC,4CAAyC;AAC7D,OAAO,EAAC,YAAY,EAAC,6CAA0C;AAE/D,OAAO,EAAC,WAAW,EAAC,4CAAyC;AAC7D,OAAO,EAAC,YAAY,EAAC,6CAA0C;AAC/D,OAAO,EAAC,gBAAgB,EAAC,iDAA8C;AAEvE,OAAO,EAAC,mBAAmB,EAAC,qDAAkD;AAE9E,OAAO,EAAC,mBAAmB,EAAC,qDAAkD;AAC9E,OAAO,EAAC,eAAe,EAAC,iDAA8C;AAEtE,OAAO,EAAC,gBAAgB,EAAC,kDAA+C;AAGxE,OAAO,EAAC,sBAAsB,EAAC,wDAAqD;AAGpF,OAAO,EAAC,QAAQ,EAAC,8BAA2B;AAC5C,YAAY,EAAC,cAAc,EAAC,mBAAgB;AAI5C,OAAO,EAAC,mBAAmB,EAAE,oBAAoB,EAAC,kDAA+C;AAGjG,OAAO,EAAC,eAAe,EAAC,+CAA4C;AACpE,OAAO,EAAC,iBAAiB,EAAC,uDAAoD;AAG9E,OAAO,EAAC,eAAe,IAAI,gBAAgB,EAAC,gDAA6C;AAGzF,OAAO,EACL,iBAAiB,EACjB,eAAe,EACf,eAAe,EAChB,sDAAmD;AAEpD,OAAO,EAAC,gBAAgB,EAAC,mDAAgD"}
package/dist/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
  // WebGL adapter classes
5
+ export { webgl2Adapter } from "./adapter/webgl-adapter.js";
6
+ // WebGL Device classes
5
7
  export { WebGLDevice } from "./adapter/webgl-device.js";
6
8
  export { WebGLCanvasContext } from "./adapter/webgl-canvas-context.js";
7
9
  // WebGL Resource classes
@@ -25,10 +27,9 @@ export { Accessor } from "./classic/accessor.js";
25
27
  export { setDeviceParameters, withDeviceParameters } from "./adapter/converters/device-parameters.js";
26
28
  // HELPERS - EXPERIMENTAL
27
29
  export { getShaderLayout } from "./adapter/helpers/get-shader-layout.js";
30
+ export { WebGLStateTracker } from "./context/state-tracker/webgl-state-tracker.js";
28
31
  // TEST EXPORTS
29
32
  export { TEXTURE_FORMATS as _TEXTURE_FORMATS } from "./adapter/converters/texture-formats.js";
30
33
  // DEPRECATED TEST EXPORTS
31
- // State tracking
32
- export { trackContextState, pushContextState, popContextState } from "./context/state-tracker/track-context-state.js";
33
34
  export { resetGLParameters, setGLParameters, getGLParameters } from "./context/parameters/unified-parameter-api.js";
34
35
  export { withGLParameters } from "./context/state-tracker/with-parameters.js";
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns a UID.
3
+ * @param id= - Identifier base name
4
+ * @return uid
5
+ **/
6
+ export declare function uid(id?: string): string;
7
+ //# sourceMappingURL=uid.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uid.d.ts","sourceRoot":"","sources":["../../src/utils/uid.ts"],"names":[],"mappings":"AAMA;;;;IAII;AACJ,wBAAgB,GAAG,CAAC,EAAE,GAAE,MAAa,GAAG,MAAM,CAI7C"}
@@ -0,0 +1,14 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ const uidCounters = {};
5
+ /**
6
+ * Returns a UID.
7
+ * @param id= - Identifier base name
8
+ * @return uid
9
+ **/
10
+ export function uid(id = 'id') {
11
+ uidCounters[id] = uidCounters[id] || 1;
12
+ const count = uidCounters[id]++;
13
+ return `${id}-${count}`;
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luma.gl/webgl",
3
- "version": "9.1.0-alpha.2",
3
+ "version": "9.1.0-alpha.9",
4
4
  "description": "WebGL2 adapter for the luma.gl core API",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -43,9 +43,9 @@
43
43
  "@luma.gl/core": "^9.0.0-beta"
44
44
  },
45
45
  "dependencies": {
46
- "@luma.gl/constants": "9.1.0-alpha.2",
46
+ "@luma.gl/constants": "9.1.0-alpha.9",
47
47
  "@math.gl/types": "^4.0.0",
48
48
  "@probe.gl/env": "^4.0.8"
49
49
  },
50
- "gitHead": "cb258afdefd2d5712d2decca35c746dd9d77a03e"
50
+ "gitHead": "ff05b21269181dbb782ba7c8c1546900288ee6a1"
51
51
  }
@@ -14,7 +14,6 @@ import type {
14
14
  GLProvokingVertex,
15
15
  GLStencilOp
16
16
  } from '@luma.gl/constants';
17
- import {pushContextState, popContextState} from '../../context/state-tracker/track-context-state';
18
17
  import {setGLParameters} from '../../context/parameters/unified-parameter-api';
19
18
  import {WebGLDevice} from '../webgl-device';
20
19
 
@@ -41,13 +40,13 @@ export function withDeviceAndGLParameters<T = unknown>(
41
40
 
42
41
  // Wrap in a try-catch to ensure that parameters are restored on exceptions
43
42
  const webglDevice = device as WebGLDevice;
44
- pushContextState(webglDevice.gl);
43
+ webglDevice.pushState();
45
44
  try {
46
45
  setDeviceParameters(device, parameters);
47
46
  setGLParameters(webglDevice.gl, glParameters);
48
47
  return func(device);
49
48
  } finally {
50
- popContextState(webglDevice.gl);
49
+ webglDevice.popState();
51
50
  }
52
51
  }
53
52
 
@@ -72,12 +71,12 @@ export function withGLParameters<T = unknown>(
72
71
 
73
72
  // Wrap in a try-catch to ensure that parameters are restored on exceptions
74
73
  const webglDevice = device as WebGLDevice;
75
- pushContextState(webglDevice.gl);
74
+ webglDevice.pushState();
76
75
  try {
77
76
  setGLParameters(webglDevice.gl, parameters);
78
77
  return func(device);
79
78
  } finally {
80
- popContextState(webglDevice.gl);
79
+ webglDevice.popState();
81
80
  }
82
81
  }
83
82
 
@@ -99,15 +98,14 @@ export function withDeviceParameters<T = unknown>(
99
98
  return func(device);
100
99
  }
101
100
 
102
- // Wrap in a try-catch to ensure that parameters are restored on exceptions
103
- // @ts-expect-error
104
- pushContextState(device.gl);
101
+ // Wrap in a try-catch to ensure that parameters are restored on exceptions'
102
+ const webglDevice = device as WebGLDevice;
103
+ webglDevice.pushState();
105
104
  try {
106
105
  setDeviceParameters(device, parameters);
107
106
  return func(device);
108
107
  } finally {
109
- // @ts-expect-error
110
- popContextState(device.gl);
108
+ webglDevice.popState();
111
109
  }
112
110
  }
113
111
 
@@ -280,10 +278,18 @@ export function setDeviceParameters(device: Device, parameters: Parameters) {
280
278
  // },
281
279
 
282
280
  // COLOR STATE
281
+ switch (parameters.blend) {
282
+ case true:
283
+ gl.enable(GL.BLEND);
284
+ break;
285
+ case false:
286
+ gl.disable(GL.BLEND);
287
+ break;
288
+ default:
289
+ // leave WebGL blend state unchanged if `parameters.blend` is not set
290
+ }
283
291
 
284
292
  if (parameters.blendColorOperation || parameters.blendAlphaOperation) {
285
- gl.enable(GL.BLEND);
286
-
287
293
  const colorEquation = convertBlendOperationToEquation(
288
294
  'blendColorOperation',
289
295
  parameters.blendColorOperation || 'add'
@@ -141,15 +141,6 @@ type Format = {
141
141
  */
142
142
  // prettier-ignore
143
143
  export const TEXTURE_FORMATS: Record<TextureFormat, Format> = {
144
- // Unsized formats that leave the precision up to the driver. TODO - Fix bpp constants
145
- 'rgb8unorm-unsized': {gl: GL.RGB, b: 4, c: 2, bpp: 4,
146
- dataFormat: GL.RGB, types: [GL.UNSIGNED_BYTE, GL.UNSIGNED_SHORT_5_6_5]},
147
- 'rgba8unorm-unsized': {gl: GL.RGBA, b: 4, c: 2, bpp: 4,
148
- dataFormat: GL.RGBA, types: [GL.UNSIGNED_BYTE, GL.UNSIGNED_SHORT_4_4_4_4, GL.UNSIGNED_SHORT_5_5_5_1]},
149
- // 'r8unorm-unsized': {gl: GL.LUMINANCE, b: 4, c: 2, bpp: 4},
150
- // 'rgb8unorm-srgb-unsized': {gl: GL.SRGB_EXT, b: 4, c: 2, bpp: 4, gl1Ext: SRGB},
151
- // 'rgba8unorm-srgb-unsized': {gl: GL.SRGB_ALPHA_EXT, b: 4, c: 2, bpp: 4, gl1Ext: SRGB},
152
-
153
144
  // 8-bit formats
154
145
  'r8unorm': {gl: GL.R8, b: 1, c: 1, rb: true},
155
146
  'r8snorm': {gl: GL.R8_SNORM, b: 1, c: 1, render: snorm8_renderable},
@@ -239,9 +230,6 @@ export const TEXTURE_FORMATS: Record<TextureFormat, Format> = {
239
230
  // The depth component of the "depth24plus" and "depth24plus-stencil8" formats may be implemented as either a 24-bit depth value or a "depth32float" value.
240
231
  'depth24plus-stencil8': {gl: GL.DEPTH24_STENCIL8, b: 4, c: 2, p: 1, attachment: GL.DEPTH_STENCIL_ATTACHMENT, rb: true, depthTexture: true,
241
232
  dataFormat: GL.DEPTH_STENCIL, types: [GL.UNSIGNED_INT_24_8]},
242
- // "depth24unorm-stencil8" feature
243
- 'depth24unorm-stencil8': {gl: GL.DEPTH24_STENCIL8, b: 4, c: 2, p: 1, attachment: GL.DEPTH_STENCIL_ATTACHMENT,
244
- dataFormat: GL.DEPTH_STENCIL, types: [GL.UNSIGNED_INT_24_8], rb: true},
245
233
  // "depth32float-stencil8" feature - TODO below is render buffer only?
246
234
  'depth32float-stencil8': {gl: GL.DEPTH32F_STENCIL8, b: 5, c: 2, p: 1, attachment: GL.DEPTH_STENCIL_ATTACHMENT,
247
235
  dataFormat: GL.DEPTH_STENCIL, types: [GL.FLOAT_32_UNSIGNED_INT_24_8_REV], rb: true},
@@ -469,6 +457,10 @@ export function isTextureFormatSupported(
469
457
  if (info.gl === undefined) {
470
458
  return false;
471
459
  }
460
+ const feature = info.f;
461
+ if (feature) {
462
+ return checkTextureFeature(gl, feature, extensions);
463
+ }
472
464
  // Check extensions
473
465
  const extension = info.x || info.gl2ext;
474
466
  if (extension) {
@@ -509,7 +501,7 @@ export function getTextureFormatSupport(
509
501
 
510
502
  // Support Check that we have a GL constant
511
503
  let supported = info.gl === undefined;
512
- supported = supported && checkTextureFeature(gl, info.f as DeviceFeature, extensions);
504
+ supported = supported && checkTextureFeature(gl, info.f, extensions);
513
505
 
514
506
  // Filtering
515
507
  // const filterable = info.filter
@@ -521,8 +513,8 @@ export function getTextureFormatSupport(
521
513
 
522
514
  return {
523
515
  supported,
524
- renderable: supported && checkTextureFeature(gl, info.render as DeviceFeature, extensions),
525
- filterable: supported && checkTextureFeature(gl, info.filter as DeviceFeature, extensions),
516
+ renderable: supported && checkTextureFeature(gl, info.render, extensions),
517
+ filterable: supported && checkTextureFeature(gl, info.filter, extensions),
526
518
  blendable: false, // tod,
527
519
  storable: false
528
520
  };
@@ -586,13 +578,12 @@ export function getTextureFormatWebGL(format: TextureFormat): {
586
578
  internalFormat: webglFormat,
587
579
  format:
588
580
  formatData?.dataFormat ||
589
- getWebGLPixelDataFormat(decoded.format, decoded.integer, decoded.normalized, webglFormat),
581
+ getWebGLPixelDataFormat(decoded.channels, decoded.integer, decoded.normalized, webglFormat),
590
582
  // depth formats don't have a type
591
583
  type: decoded.dataType
592
584
  ? getGLFromVertexType(decoded.dataType)
593
585
  : formatData?.types?.[0] || GL.UNSIGNED_BYTE,
594
- // @ts-expect-error
595
- compressed: decoded.compressed
586
+ compressed: decoded.compressed || false
596
587
  };
597
588
  }
598
589
 
@@ -619,7 +610,7 @@ export function getTextureFormatBytesPerPixel(format: TextureFormat): number {
619
610
  // DATA TYPE HELPERS
620
611
 
621
612
  export function getWebGLPixelDataFormat(
622
- dataFormat: string,
613
+ channels: 'r' | 'rg' | 'rgb' | 'rgba' | 'bgra',
623
614
  integer: boolean,
624
615
  normalized: boolean,
625
616
  format: GL
@@ -629,11 +620,12 @@ export function getWebGLPixelDataFormat(
629
620
  return format;
630
621
  }
631
622
  // prettier-ignore
632
- switch (dataFormat) {
623
+ switch (channels) {
633
624
  case 'r': return integer && !normalized ? GL.RED_INTEGER : GL.RED;
634
625
  case 'rg': return integer && !normalized ? GL.RG_INTEGER : GL.RG;
635
626
  case 'rgb': return integer && !normalized ? GL.RGB_INTEGER : GL.RGB;
636
627
  case 'rgba': return integer && !normalized ? GL.RGBA_INTEGER : GL.RGBA;
628
+ case 'bgra': throw new Error('bgra pixels not supported by WebGL');
637
629
  default: return GL.RGBA;
638
630
  }
639
631
  }
@@ -21,7 +21,6 @@ const WEBGL_FEATURES: Partial<Record<DeviceFeature, boolean | string>> = {
21
21
  // 'timestamp-query' // GPUQueryType "timestamp-query"
22
22
  // "indirect-first-instance"
23
23
  // Textures are handled by getTextureFeatures()
24
- // 'depth24unorm-stencil8' // GPUTextureFormat 'depth24unorm-stencil8'
25
24
  // 'depth32float-stencil8' // GPUTextureFormat 'depth32float-stencil8'
26
25
 
27
26
  // optional WebGL features
@@ -97,7 +97,7 @@ export function initializeTextureStorage(
97
97
  ): void {
98
98
  const {dimension, width, height, depth = 0} = options;
99
99
  const {glInternalFormat} = options;
100
- const glTarget = options.glTarget; // getCubeTargetWebGL(options.glTarget, dimension, depth);
100
+ const glTarget = options.glTarget; // getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
101
101
  switch (dimension) {
102
102
  case '2d-array':
103
103
  case '3d':
@@ -120,7 +120,7 @@ export function copyCPUImageToMipLevel(
120
120
  const {dimension, width, height, depth = 0, level = 0} = options;
121
121
  const {x = 0, y = 0, z = 0} = options;
122
122
  const {glFormat, glType} = options;
123
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
123
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
124
124
 
125
125
  // width = size.width,
126
126
  // height = size.height
@@ -154,7 +154,9 @@ export function copyCPUDataToMipLevel(
154
154
  const {dimension, width, height, depth = 0, level = 0, byteOffset = 0} = options;
155
155
  const {x = 0, y = 0, z = 0} = options;
156
156
  const {glFormat, glType, compressed} = options;
157
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
157
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
158
+
159
+ // gl.bindTexture(glTarget, null);
158
160
 
159
161
  switch (dimension) {
160
162
  case '2d-array':
@@ -196,7 +198,7 @@ export function copyGPUBufferToMipLevel(
196
198
  const {dimension, width, height, depth = 0, level = 0, byteOffset = 0} = options;
197
199
  const {x = 0, y = 0, z = 0} = options;
198
200
  const {glFormat, glType, compressed} = options;
199
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
201
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
200
202
 
201
203
  gl.bindBuffer(GL.PIXEL_UNPACK_BUFFER, webglBuffer);
202
204
 
@@ -250,9 +252,10 @@ export function getWebGLTextureTarget(
250
252
 
251
253
  /**
252
254
  * In WebGL, cube maps specify faces by overriding target instead of using the depth parameter.
255
+ * @note We still bind the texture using GL.TEXTURE_CUBE_MAP, but we need to use the face-specific target when setting mip levels.
253
256
  * @returns glTarget unchanged, if dimension !== 'cube'.
254
257
  */
255
- function getCubeTargetWebGL(
258
+ function getWebGLCubeFaceTarget(
256
259
  glTarget: GLTextureTarget,
257
260
  dimension: '1d' | '2d' | '2d-array' | 'cube' | 'cube-array' | '3d',
258
261
  level: number
@@ -269,7 +272,7 @@ function getCubeTargetWebGL(
269
272
  export function clearMipLevel(gl: WebGL2RenderingContext, options: WebGLSetTextureOptions): void {
270
273
  const {dimension, width, height, depth = 0, level = 0} = options;
271
274
  const {glInternalFormat, glFormat, glType, compressed} = options;
272
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
275
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
273
276
 
274
277
  switch (dimension) {
275
278
  case '2d-array':
@@ -312,7 +315,7 @@ export function setMipLevelFromExternalImage(
312
315
  const {dimension, width, height, depth = 0, level = 0} = options;
313
316
  const {glInternalFormat, glType} = options;
314
317
 
315
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
318
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
316
319
 
317
320
  // TODO - we can't change texture width (due to WebGPU limitations) -
318
321
  // and the width/heigh of an external image is implicit, so why do we need to extract it?
@@ -363,7 +366,7 @@ export function setMipLevelFromTypedArray(
363
366
  const {dimension, width, height, depth = 0, level = 0, offset = 0} = options;
364
367
  const {glInternalFormat, glFormat, glType, compressed} = options;
365
368
 
366
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
369
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
367
370
 
368
371
  withGLParameters(gl, parameters, () => {
369
372
  switch (dimension) {
@@ -442,7 +445,7 @@ export function setMipLevelFromGPUBuffer(
442
445
  ): void {
443
446
  const {dimension, width, height, depth = 0, level = 0, byteOffset = 0} = options;
444
447
  const {glInternalFormat, glFormat, glType, compressed} = options;
445
- const glTarget = getCubeTargetWebGL(options.glTarget, dimension, depth);
448
+ const glTarget = getWebGLCubeFaceTarget(options.glTarget, dimension, depth);
446
449
 
447
450
  const webglBuffer = buffer as WEBGLBuffer;
448
451
  const imageSize = buffer.byteLength;
@@ -155,7 +155,7 @@ export class WEBGLFramebuffer extends Framebuffer {
155
155
  switch (texture.glTarget) {
156
156
  case GL.TEXTURE_2D_ARRAY:
157
157
  case GL.TEXTURE_3D:
158
- gl.framebufferTextureLayer(GL.FRAMEBUFFER, attachment, texture.glTarget, level, layer);
158
+ gl.framebufferTextureLayer(GL.FRAMEBUFFER, attachment, texture.handle, level, layer);
159
159
  break;
160
160
 
161
161
  case GL.TEXTURE_CUBE_MAP:
@@ -8,7 +8,6 @@ import {WebGLDevice} from '../webgl-device';
8
8
  import {GL, GLParameters} from '@luma.gl/constants';
9
9
  import {withGLParameters} from '../../context/state-tracker/with-parameters';
10
10
  import {setGLParameters} from '../../context/parameters/unified-parameter-api';
11
- import {pushContextState, popContextState} from '../../context/state-tracker/track-context-state';
12
11
  import {WEBGLQuerySet} from './webgl-query-set';
13
12
 
14
13
  // Should collapse during minification
@@ -29,16 +28,30 @@ export class WEBGLRenderPass extends RenderPass {
29
28
  super(device, props);
30
29
  this.device = device;
31
30
 
31
+ // If no viewport is provided, apply reasonably defaults
32
+ let viewport;
33
+ if (!props?.parameters?.viewport) {
34
+ if (props?.framebuffer) {
35
+ // Set the viewport to the size of the framebuffer
36
+ const {width, height} = props.framebuffer;
37
+ viewport = [0, 0, width, height];
38
+ } else {
39
+ // Instead of using our own book-keeping, we can just read the values from the WebGL context
40
+ const [width, height] = device.getCanvasContext().getDrawingBufferSize();
41
+ viewport = [0, 0, width, height];
42
+ }
43
+ }
44
+
32
45
  // TODO - do parameters (scissorRect) affect the clear operation?
33
- pushContextState(this.device.gl);
34
- this.setParameters(this.props.parameters);
46
+ this.device.pushState();
47
+ this.setParameters({viewport, ...this.props.parameters});
35
48
 
36
49
  // Hack - for now WebGL draws in "immediate mode" (instead of queueing the operations)...
37
50
  this.clear();
38
51
  }
39
52
 
40
53
  end(): void {
41
- popContextState(this.device.gl);
54
+ this.device.popState();
42
55
  // should add commands to CommandEncoder.
43
56
  }
44
57
 
@@ -112,7 +112,8 @@ export class WEBGLRenderPipeline extends RenderPipeline {
112
112
  .join(', ');
113
113
  if (!options?.disableWarnings) {
114
114
  log.warn(
115
- `Unknown binding "${name}" in render pipeline "${this.id}", expected one of ${validBindings}`
115
+ `No binding "${name}" in render pipeline "${this.id}", expected one of ${validBindings}`,
116
+ value
116
117
  )();
117
118
  }
118
119
  continue; // eslint-disable-line no-continue
@@ -60,7 +60,7 @@ export class WEBGLShader extends Shader {
60
60
  /** Compile a shader and get compilation status */
61
61
  protected async _compile(source: string): Promise<void> {
62
62
  const addGLSLVersion = (source: string) =>
63
- source.startsWith('#version ') ? source : `#version 100\n${source}`;
63
+ source.startsWith('#version ') ? source : `#version 300 es\n${source}`;
64
64
  source = addGLSLVersion(source);
65
65
 
66
66
  const {gl} = this.device;
@@ -339,7 +339,7 @@ export class WEBGLTexture extends Texture {
339
339
  }
340
340
 
341
341
  /** Set a simple texture */
342
- setTexture2DData(lodData: Texture2DData, depth = 0, glTarget = this.glTarget): void {
342
+ setTexture2DData(lodData: Texture2DData, depth = 0): void {
343
343
  this.bind();
344
344
 
345
345
  const lodArray = normalizeTextureData(lodData, this);
@@ -367,7 +367,9 @@ export class WEBGLTexture extends Texture {
367
367
  throw new Error(this.id);
368
368
  }
369
369
  if (ArrayBuffer.isView(data)) {
370
+ this.bind();
370
371
  copyCPUDataToMipLevel(this.device.gl, data, this);
372
+ this.unbind();
371
373
  }
372
374
  }
373
375
 
@@ -381,9 +383,9 @@ export class WEBGLTexture extends Texture {
381
383
  if (this.props.dimension !== 'cube') {
382
384
  throw new Error(this.id);
383
385
  }
384
- // for (const face of Texture.CubeFaces) {
385
- // // this.setTextureCubeFaceData(face, data[face]);
386
- // }
386
+ for (const face of Texture.CubeFaces) {
387
+ this.setTextureCubeFaceData(data[face], face);
388
+ }
387
389
  }
388
390
 
389
391
  /**
@@ -414,27 +416,9 @@ export class WEBGLTexture extends Texture {
414
416
  log.warn(`${this.id} has mipmap and multiple LODs.`)();
415
417
  }
416
418
 
417
- // const glFace = GL.TEXTURE_CUBE_MAP_POSITIVE_X + Texture.CubeFaces.indexOf(face);
418
- // const glType = GL.UNSIGNED_BYTE;
419
- // const {width, height, format = GL.RGBA, type = GL.UNSIGNED_BYTE} = this;
420
- // const {width, height, format = GL.RGBA, type = GL.UNSIGNED_BYTE} = this;
419
+ const faceDepth = Texture.CubeFaces.indexOf(face);
421
420
 
422
- this.bind();
423
- // for (let lodLevel = 0; lodLevel < lodData.length; lodLevel++) {
424
- // const imageData = lodData[lodLevel];
425
- // if (imageData instanceof ArrayBuffer) {
426
- // // const imageData = image instanceof ArrayBuffer ? new ImageData(new Uint8ClampedArray(image), this.width) : image;
427
- // this.device.gl.texImage2D?.(
428
- // glFace,
429
- // lodLevel,
430
- // this.glInternalFormat,
431
- // this.glInternalFormat,
432
- // glType,
433
- // imageData
434
- // );
435
- // }
436
- // }
437
- this.unbind();
421
+ this.setTexture2DData(lodData, faceDepth);
438
422
  }
439
423
 
440
424
  // INTERNAL METHODS
@@ -592,14 +576,19 @@ export class WEBGLTexture extends Texture {
592
576
  * Copy a region of data from a CPU memory buffer into this texture.
593
577
  * @todo - GLUnpackParameters parameters
594
578
  */
595
- protected _setMipLevel(depth: number, level: number, textureData: Texture2DData, offset = 0) {
579
+ protected _setMipLevel(
580
+ depth: number,
581
+ level: number,
582
+ textureData: Texture2DData,
583
+ glTarget: GL = this.glTarget
584
+ ) {
596
585
  // if (!textureData) {
597
586
  // clearMipLevel(this.device.gl, {...this, depth, level});
598
587
  // return;
599
588
  // }
600
589
 
601
590
  if (Texture.isExternalImage(textureData)) {
602
- copyCPUImageToMipLevel(this.device.gl, textureData, {...this, depth, level});
591
+ copyCPUImageToMipLevel(this.device.gl, textureData, {...this, depth, level, glTarget});
603
592
  return;
604
593
  }
605
594
 
@@ -608,7 +597,8 @@ export class WEBGLTexture extends Texture {
608
597
  copyCPUDataToMipLevel(this.device.gl, textureData.data, {
609
598
  ...this,
610
599
  depth,
611
- level
600
+ level,
601
+ glTarget
612
602
  });
613
603
  return;
614
604
  }
@@ -0,0 +1,113 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import {Adapter, Device, DeviceProps, CanvasContext, log} from '@luma.gl/core';
6
+ import {WebGLDevice} from './webgl-device';
7
+ import {enforceWebGL2} from '../context/polyfills/polyfill-webgl1-extensions';
8
+ import {loadSpectorJS, DEFAULT_SPECTOR_PROPS} from '../context/debug/spector';
9
+ import {loadWebGLDeveloperTools} from '../context/debug/webgl-developer-tools';
10
+
11
+ const LOG_LEVEL = 1;
12
+
13
+ export class WebGLAdapter extends Adapter {
14
+ /** type of device's created by this adapter */
15
+ readonly type: Device['type'] = 'webgl';
16
+
17
+ constructor() {
18
+ super();
19
+
20
+ // Add spector default props to device default props, so that runtime settings are observed
21
+ Device.defaultProps = {...Device.defaultProps, ...DEFAULT_SPECTOR_PROPS};
22
+
23
+ // @ts-ignore DEPRECATED For backwards compatibility luma.registerDevices
24
+ WebGLDevice.adapter = this;
25
+ }
26
+
27
+ /** Check if WebGL 2 is available */
28
+ isSupported(): boolean {
29
+ return typeof WebGL2RenderingContext !== 'undefined';
30
+ }
31
+
32
+ /** Force any created WebGL contexts to be WebGL2 contexts, polyfilled with WebGL1 extensions */
33
+ enforceWebGL2(enable: boolean): void {
34
+ enforceWebGL2(enable);
35
+ }
36
+
37
+ /**
38
+ * Get a device instance from a GL context
39
+ * Creates and instruments the device if not already created
40
+ * @param gl
41
+ * @returns
42
+ */
43
+ async attach(gl: Device | WebGL2RenderingContext): Promise<WebGLDevice> {
44
+ if (gl instanceof WebGLDevice) {
45
+ return gl;
46
+ }
47
+ // @ts-expect-error
48
+ if (gl?.device instanceof Device) {
49
+ // @ts-expect-error
50
+ return gl.device as WebGLDevice;
51
+ }
52
+ if (!isWebGL(gl)) {
53
+ throw new Error('Invalid WebGL2RenderingContext');
54
+ }
55
+ return new WebGLDevice({gl: gl as WebGL2RenderingContext});
56
+ }
57
+
58
+ async create(props: DeviceProps = {}): Promise<WebGLDevice> {
59
+ log.groupCollapsed(LOG_LEVEL, 'WebGLDevice created')();
60
+
61
+ const promises: Promise<unknown>[] = [];
62
+
63
+ // Load webgl and spector debug scripts from CDN if requested
64
+ if (props.debug) {
65
+ promises.push(loadWebGLDeveloperTools());
66
+ }
67
+
68
+ if (props.debugWithSpectorJS) {
69
+ promises.push(loadSpectorJS(props));
70
+ }
71
+
72
+ // Wait for page to load: if canvas is a string we need to query the DOM for the canvas element.
73
+ // We only wait when props.canvas is string to avoids setting the global page onload callback unless necessary.
74
+ if (typeof props.canvas === 'string') {
75
+ promises.push(CanvasContext.pageLoaded);
76
+ }
77
+
78
+ // Wait for all the loads to settle before creating the context.
79
+ // The Device.create() functions are async, so in contrast to the constructor, we can `await` here.
80
+ const results = await Promise.allSettled(promises);
81
+ for (const result of results) {
82
+ if (result.status === 'rejected') {
83
+ log.error(`Failed to initialize debug libraries ${result.reason}`)();
84
+ }
85
+ }
86
+
87
+ log.probe(LOG_LEVEL + 1, 'DOM is loaded')();
88
+
89
+ const device = new WebGLDevice(props);
90
+
91
+ // Log some debug info about the newly created context
92
+ const message = `\
93
+ Created ${device.type}${device.debug ? ' debug' : ''} context: \
94
+ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContext.id}`;
95
+ log.probe(LOG_LEVEL, message)();
96
+ log.table(LOG_LEVEL, device.info)();
97
+
98
+ log.groupEnd(LOG_LEVEL)();
99
+
100
+ return device;
101
+ }
102
+ }
103
+
104
+ /** Check if supplied parameter is a WebGL2RenderingContext */
105
+ function isWebGL(gl: any): boolean {
106
+ if (typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext) {
107
+ return true;
108
+ }
109
+ // Look for debug contexts, headless gl etc
110
+ return Boolean(gl && Number.isFinite(gl._version));
111
+ }
112
+
113
+ export const webgl2Adapter = new WebGLAdapter();