@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
@@ -1,17 +1,21 @@
1
+ import { Spector } from "./spector-types.js";
1
2
  /** Spector debug initialization options */
2
3
  type SpectorProps = {
4
+ /** Whether spector is enabled */
5
+ debugWithSpectorJS?: boolean;
6
+ /** URL to load spector script from. Typically a CDN URL */
7
+ spectorUrl?: string;
3
8
  /** Canvas to monitor */
4
- canvas?: HTMLCanvasElement | OffscreenCanvas;
5
- /** Whether debug is enabled. Auto-detected if ommitted */
6
- debug?: boolean;
7
- /** Whether spector is disabled */
8
- spector?: boolean | string | object;
9
+ gl?: WebGL2RenderingContext;
9
10
  };
10
11
  declare global {
11
- var SPECTOR: any;
12
+ var SPECTOR: Spector;
12
13
  }
14
+ export declare const DEFAULT_SPECTOR_PROPS: Required<SpectorProps>;
13
15
  /** Loads spector from CDN if not already installed */
14
- export declare function loadSpectorJS(props?: SpectorProps): Promise<void>;
15
- export declare function initializeSpectorJS(props?: SpectorProps): any;
16
+ export declare function loadSpectorJS(props: {
17
+ spectorUrl?: string;
18
+ }): Promise<void>;
19
+ export declare function initializeSpectorJS(props: SpectorProps): Spector | null;
16
20
  export {};
17
21
  //# sourceMappingURL=spector.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"spector.d.ts","sourceRoot":"","sources":["../../../src/context/debug/spector.ts"],"names":[],"mappings":"AAOA,2CAA2C;AAC3C,KAAK,YAAY,GAAG;IAClB,wBAAwB;IACxB,MAAM,CAAC,EAAE,iBAAiB,GAAG,eAAe,CAAC;IAC7C,0DAA0D;IAC1D,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,kCAAkC;IAClC,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,CAAC;CACrC,CAAC;AAaF,OAAO,CAAC,MAAM,CAAC;IAEb,IAAI,OAAO,EAAE,GAAG,CAAC;CAClB;AAED,sDAAsD;AACtD,wBAAsB,aAAa,CAAC,KAAK,CAAC,EAAE,YAAY,iBAQvD;AAED,wBAAgB,mBAAmB,CAAC,KAAK,CAAC,EAAE,YAAY,OAsDvD"}
1
+ {"version":3,"file":"spector.d.ts","sourceRoot":"","sources":["../../../src/context/debug/spector.ts"],"names":[],"mappings":"AAOA,OAAO,EAAC,OAAO,EAAC,2BAAwB;AAExC,2CAA2C;AAC3C,KAAK,YAAY,GAAG;IAClB,iCAAiC;IACjC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,2DAA2D;IAC3D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,wBAAwB;IACxB,EAAE,CAAC,EAAE,sBAAsB,CAAC;CAC7B,CAAC;AAOF,OAAO,CAAC,MAAM,CAAC;IAGb,IAAI,OAAO,EAAE,OAAO,CAAC;CACtB;AAED,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,YAAY,CAOxD,CAAC;AAEF,sDAAsD;AACtD,wBAAsB,aAAa,CAAC,KAAK,EAAE;IAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAQ/E;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,GAAG,IAAI,CAyDvE"}
@@ -3,19 +3,22 @@
3
3
  // Copyright (c) vis.gl contributors
4
4
  import { log } from '@luma.gl/core';
5
5
  import { loadScript } from "../../utils/load-script.js";
6
- const DEFAULT_SPECTOR_PROPS = {
7
- spector: log.get('spector') || log.get('inspect')
8
- };
9
- // https://github.com/BabylonJS/Spector.js#basic-usage
10
- const SPECTOR_CDN_URL = 'https://spectorcdn.babylonjs.com/spector.bundle.js';
11
6
  const LOG_LEVEL = 1;
12
7
  let spector = null;
13
8
  let initialized = false;
9
+ export const DEFAULT_SPECTOR_PROPS = {
10
+ debugWithSpectorJS: log.get('spector') || log.get('spectorjs'),
11
+ // https://github.com/BabylonJS/Spector.js#basic-usage
12
+ // https://forum.babylonjs.com/t/spectorcdn-is-temporarily-off/48241
13
+ // spectorUrl: 'https://spectorcdn.babylonjs.com/spector.bundle.js';
14
+ spectorUrl: 'https://cdn.jsdelivr.net/npm/spectorjs@0.9.30/dist/spector.bundle.js',
15
+ gl: undefined
16
+ };
14
17
  /** Loads spector from CDN if not already installed */
15
18
  export async function loadSpectorJS(props) {
16
19
  if (!globalThis.SPECTOR) {
17
20
  try {
18
- await loadScript(SPECTOR_CDN_URL);
21
+ await loadScript(props.spectorUrl || DEFAULT_SPECTOR_PROPS.spectorUrl);
19
22
  }
20
23
  catch (error) {
21
24
  log.warn(String(error));
@@ -24,12 +27,13 @@ export async function loadSpectorJS(props) {
24
27
  }
25
28
  export function initializeSpectorJS(props) {
26
29
  props = { ...DEFAULT_SPECTOR_PROPS, ...props };
27
- if (!props?.spector) {
30
+ if (!props.debugWithSpectorJS) {
28
31
  return null;
29
32
  }
30
- if (!spector && globalThis.SPECTOR) {
31
- log.probe(LOG_LEVEL, 'SPECTOR found and initialized')();
32
- spector = new globalThis.SPECTOR.Spector();
33
+ if (!spector && globalThis.SPECTOR && !globalThis.luma?.spector) {
34
+ log.probe(LOG_LEVEL, 'SPECTOR found and initialized. Start with `luma.spector.displayUI()`')();
35
+ const { Spector } = globalThis.SPECTOR;
36
+ spector = new Spector();
33
37
  if (globalThis.luma) {
34
38
  globalThis.luma.spector = spector;
35
39
  }
@@ -48,18 +52,20 @@ export function initializeSpectorJS(props) {
48
52
  // Use undocumented Spector API to open the UI with our capture
49
53
  // See https://github.com/BabylonJS/Spector.js/blob/767ad1195a25b85a85c381f400eb50a979239eca/src/spector.ts#L124
50
54
  spector?.getResultUI();
55
+ // @ts-expect-error private
51
56
  spector?.resultView.display();
57
+ // @ts-expect-error private
52
58
  spector?.resultView.addCapture(capture);
53
59
  });
54
60
  }
55
- if (props?.canvas) {
56
- // @ts-expect-error If spector is specified as a canvas id, only monitor that canvas
57
- if (typeof props.spector === 'string' && props.spector !== props.canvas.id) {
58
- return spector;
59
- }
61
+ if (props.gl) {
60
62
  // capture startup
61
- // spector?.captureCanvas(props?.canvas);
62
- spector?.startCapture(props?.canvas, 500); // 500 commands
63
+ const gl = props.gl;
64
+ // @ts-expect-error
65
+ const device = gl.device;
66
+ spector?.startCapture(props.gl, 500); // 500 commands
67
+ // @ts-expect-error
68
+ gl.device = device;
63
69
  new Promise(resolve => setTimeout(resolve, 2000)).then(_ => {
64
70
  log.info('Spector capture stopped after 2 seconds')();
65
71
  spector?.stopCapture();
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Make browser return WebGL2 contexts even if WebGL1 contexts are requested
3
+ * @param enforce
4
+ * @returns
5
+ */
6
+ export declare function enforceWebGL2(enforce?: boolean): void;
7
+ /** Install WebGL1-only extensions on WebGL2 contexts */
8
+ export declare function polyfillWebGL1Extensions(gl: WebGL2RenderingContext): void;
9
+ //# sourceMappingURL=polyfill-webgl1-extensions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"polyfill-webgl1-extensions.d.ts","sourceRoot":"","sources":["../../../src/context/polyfills/polyfill-webgl1-extensions.ts"],"names":[],"mappings":"AA4EA;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,GAAE,OAAc,GAAG,IAAI,CA0B3D;AAED,wDAAwD;AACxD,wBAAgB,wBAAwB,CAAC,EAAE,EAAE,sBAAsB,GAAG,IAAI,CAqCzE"}
@@ -0,0 +1,181 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Goal is to make WebGL2 contexts look like WebGL1
5
+ // @note Partly inspired by with some older code from the `regl` library
6
+ /* eslint-disable camelcase */
7
+ import { GL } from '@luma.gl/constants';
8
+ // webgl1 extensions natively supported by webgl2
9
+ const WEBGL1_STATIC_EXTENSIONS = {
10
+ WEBGL_depth_texture: {
11
+ UNSIGNED_INT_24_8_WEBGL: 34042
12
+ },
13
+ OES_element_index_uint: {},
14
+ OES_texture_float: {},
15
+ OES_texture_half_float: {
16
+ // @ts-expect-error different numbers?
17
+ HALF_FLOAT_OES: 5131
18
+ },
19
+ EXT_color_buffer_float: {},
20
+ OES_standard_derivatives: {
21
+ FRAGMENT_SHADER_DERIVATIVE_HINT_OES: 35723
22
+ },
23
+ EXT_frag_depth: {},
24
+ EXT_blend_minmax: {
25
+ MIN_EXT: 32775,
26
+ MAX_EXT: 32776
27
+ },
28
+ EXT_shader_texture_lod: {}
29
+ };
30
+ const getWEBGL_draw_buffers = (gl) => ({
31
+ drawBuffersWEBGL(buffers) {
32
+ return gl.drawBuffers(buffers);
33
+ },
34
+ COLOR_ATTACHMENT0_WEBGL: 36064,
35
+ COLOR_ATTACHMENT1_WEBGL: 36065,
36
+ COLOR_ATTACHMENT2_WEBGL: 36066,
37
+ COLOR_ATTACHMENT3_WEBGL: 36067
38
+ }); // - too many fields
39
+ const getOES_vertex_array_object = (gl) => ({
40
+ VERTEX_ARRAY_BINDING_OES: 34229,
41
+ createVertexArrayOES() {
42
+ return gl.createVertexArray();
43
+ },
44
+ deleteVertexArrayOES(vertexArray) {
45
+ return gl.deleteVertexArray(vertexArray);
46
+ },
47
+ isVertexArrayOES(vertexArray) {
48
+ return gl.isVertexArray(vertexArray);
49
+ },
50
+ bindVertexArrayOES(vertexArray) {
51
+ return gl.bindVertexArray(vertexArray);
52
+ }
53
+ });
54
+ const getANGLE_instanced_arrays = (gl) => ({
55
+ VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE: 0x88fe,
56
+ drawArraysInstancedANGLE(...args) {
57
+ return gl.drawArraysInstanced(...args);
58
+ },
59
+ drawElementsInstancedANGLE(...args) {
60
+ return gl.drawElementsInstanced(...args);
61
+ },
62
+ vertexAttribDivisorANGLE(...args) {
63
+ return gl.vertexAttribDivisor(...args);
64
+ }
65
+ });
66
+ /**
67
+ * Make browser return WebGL2 contexts even if WebGL1 contexts are requested
68
+ * @param enforce
69
+ * @returns
70
+ */
71
+ export function enforceWebGL2(enforce = true) {
72
+ const prototype = HTMLCanvasElement.prototype;
73
+ if (!enforce && prototype.originalGetContext) {
74
+ // Reset the original getContext function
75
+ prototype.getContext = prototype.originalGetContext;
76
+ prototype.originalGetContext = undefined;
77
+ return;
78
+ }
79
+ // Store the original getContext function
80
+ prototype.originalGetContext = prototype.getContext;
81
+ // Override the getContext function
82
+ prototype.getContext = function (contextId, options) {
83
+ // Attempt to force WebGL2 for all WebGL1 contexts
84
+ if (contextId === 'webgl' || contextId === 'experimental-webgl') {
85
+ const context = this.originalGetContext('webgl2', options);
86
+ // Work around test mocking
87
+ if (context instanceof HTMLElement) {
88
+ polyfillWebGL1Extensions(context);
89
+ }
90
+ return context;
91
+ }
92
+ // For any other type, return the original context
93
+ return this.originalGetContext(contextId, options);
94
+ };
95
+ }
96
+ /** Install WebGL1-only extensions on WebGL2 contexts */
97
+ export function polyfillWebGL1Extensions(gl) {
98
+ // Enable, to support float and half-float textures
99
+ gl.getExtension('EXT_color_buffer_float');
100
+ // WebGL1 extensions implemented using WebGL2 APIs
101
+ const boundExtensions = {
102
+ ...WEBGL1_STATIC_EXTENSIONS,
103
+ WEBGL_disjoint_timer_query: gl.getExtension('EXT_disjoint_timer_query_webgl2'),
104
+ WEBGL_draw_buffers: getWEBGL_draw_buffers(gl),
105
+ OES_vertex_array_object: getOES_vertex_array_object(gl),
106
+ ANGLE_instanced_arrays: getANGLE_instanced_arrays(gl)
107
+ };
108
+ // Override gl.getExtension
109
+ // eslint-disable-next-line @typescript-eslint/unbound-method
110
+ const originalGetExtension = gl.getExtension;
111
+ gl.getExtension = function (extensionName) {
112
+ const ext = originalGetExtension.call(gl, extensionName);
113
+ if (ext) {
114
+ return ext;
115
+ }
116
+ // Injected extensions
117
+ if (extensionName in boundExtensions) {
118
+ return boundExtensions[extensionName];
119
+ }
120
+ return null;
121
+ };
122
+ // Override gl.getSupportedExtensions
123
+ // eslint-disable-next-line @typescript-eslint/unbound-method
124
+ const originalGetSupportedExtensions = gl.getSupportedExtensions;
125
+ gl.getSupportedExtensions = function () {
126
+ const extensions = originalGetSupportedExtensions.apply(gl) || [];
127
+ return extensions?.concat(Object.keys(boundExtensions));
128
+ };
129
+ }
130
+ // Update unsized WebGL1 formats to sized WebGL2 formats
131
+ // todo move to texture format file
132
+ // export function getInternalFormat(gl: WebGL2RenderingContext, format: GL, type: GL): GL {
133
+ // // webgl2 texture formats
134
+ // // https://webgl2fundamentals.org/webgl/lessons/webgl-data-textures.html
135
+ // switch (format) {
136
+ // case GL.DEPTH_COMPONENT:
137
+ // return GL.DEPTH_COMPONENT24;
138
+ // case GL.DEPTH_STENCIL:
139
+ // return GL.DEPTH24_STENCIL8;
140
+ // case GL.RGBA:
141
+ // return type === GL.HALF_FLOAT ? GL.RGBA16F : GL.RGBA32F;
142
+ // case GL.RGB:
143
+ // return type === GL.HALF_FLOAT ? GL.RGB16F : GL.RGB32F;
144
+ // default:
145
+ // return format;
146
+ // }
147
+ // }
148
+ /*
149
+ // texture type to update on the fly
150
+ export function getTextureType(gl: WebGL2RenderingContext, type: GL): GL {
151
+ if (type === HALF_FLOAT_OES) {
152
+ return GL.HALF_FLOAT;
153
+ }
154
+ return type;
155
+ }
156
+
157
+ // And texImage2D to convert the internalFormat to webgl2.
158
+ const webgl2 = this;
159
+ const origTexImage = gl.texImage2D;
160
+ gl.texImage2D = function (target, miplevel, iformat, a, typeFor6, c, d, typeFor9, f) {
161
+ if (arguments.length == 6) {
162
+ var ifmt = webgl2.getInternalFormat(gl, iformat, typeFor6);
163
+ origTexImage.apply(gl, [target, miplevel, ifmt, a, webgl.getTextureType(gl, typeFor6), c]);
164
+ } else {
165
+ // arguments.length == 9
166
+ var ifmt = webgl2.getInternalFormat(gl, iformat, typeFor9);
167
+ origTexImage.apply(gl, [
168
+ target,
169
+ miplevel,
170
+ ifmt,
171
+ a,
172
+ typeFor6,
173
+ c,
174
+ d,
175
+ webgl2.getTextureType(gl, typeFor9),
176
+ f
177
+ ]);
178
+ }
179
+ };
180
+ };
181
+ */
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Support for listening to context state changes and intercepting state queries
3
+ * NOTE: this system does not handle buffer bindings
4
+ */
5
+ export declare class WebGLStateTracker {
6
+ static get(gl: WebGL2RenderingContext): WebGLStateTracker;
7
+ gl: WebGL2RenderingContext;
8
+ program: unknown;
9
+ stateStack: object[];
10
+ enable: boolean;
11
+ cache: Record<string, any>;
12
+ log: any;
13
+ protected initialized: boolean;
14
+ constructor(gl: WebGL2RenderingContext, props?: {
15
+ log: any;
16
+ });
17
+ push(values?: {}): void;
18
+ pop(): void;
19
+ /**
20
+ * Initialize WebGL state caching on a context
21
+ * can be called multiple times to enable/disable
22
+ *
23
+ * @note After calling this function, context state will be cached
24
+ * .push() and .pop() will be available for saving,
25
+ * temporarily modifying, and then restoring state.
26
+ */
27
+ trackState(gl: WebGL2RenderingContext, options?: {
28
+ copyState?: boolean;
29
+ }): void;
30
+ /**
31
+ // interceptor for context set functions - update our cache and our stack
32
+ // values (Object) - the key values for this setter
33
+ * @param values
34
+ * @returns
35
+ */
36
+ _updateCache(values: {
37
+ [key: number | string]: any;
38
+ }): {
39
+ valueChanged: boolean;
40
+ oldValue: any;
41
+ };
42
+ }
43
+ //# sourceMappingURL=webgl-state-tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webgl-state-tracker.d.ts","sourceRoot":"","sources":["../../../src/context/state-tracker/webgl-state-tracker.ts"],"names":[],"mappings":"AAcA;;;GAGG;AACH,qBAAa,iBAAiB;IAC5B,MAAM,CAAC,GAAG,CAAC,EAAE,EAAE,sBAAsB,GAAG,iBAAiB;IAKzD,EAAE,EAAE,sBAAsB,CAAC;IAC3B,OAAO,EAAE,OAAO,CAAQ;IACxB,UAAU,EAAE,MAAM,EAAE,CAAM;IAC1B,MAAM,UAAQ;IACd,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAS;IACnC,GAAG,MAAC;IAEJ,SAAS,CAAC,WAAW,UAAS;gBAG5B,EAAE,EAAE,sBAAsB,EAC1B,KAAK,CAAC,EAAE;QACN,GAAG,MAAC;KACL;IASH,IAAI,CAAC,MAAM,KAAK;IAIhB,GAAG;IASH;;;;;;;OAOG;IACH,UAAU,CAAC,EAAE,EAAE,sBAAsB,EAAE,OAAO,CAAC,EAAE;QAAC,SAAS,CAAC,EAAE,OAAO,CAAA;KAAC,GAAG,IAAI;IAwB7E;;;;;OAKG;IACH,YAAY,CAAC,MAAM,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,GAAG,CAAA;KAAC;;;;CA8BnD"}
@@ -1,26 +1,29 @@
1
1
  // luma.gl
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
- // Support for listening to context state changes and intercepting state queries
5
- // NOTE: this system does not handle buffer bindings
6
- import { GL_PARAMETER_DEFAULTS, GL_HOOKED_SETTERS, NON_CACHE_PARAMETERS } from "../parameters/webgl-parameter-tables.js";
7
4
  import { setGLParameters, getGLParameters } from "../parameters/unified-parameter-api.js";
8
5
  import { deepArrayEqual } from "./deep-array-equal.js";
9
- // HELPER CLASS - GLState
10
- /* eslint-disable no-shadow */
11
- class GLState {
6
+ import { GL_PARAMETER_DEFAULTS, GL_HOOKED_SETTERS, NON_CACHE_PARAMETERS } from "../parameters/webgl-parameter-tables.js";
7
+ // HELPER CLASS - WebGLStateTracker
8
+ /**
9
+ * Support for listening to context state changes and intercepting state queries
10
+ * NOTE: this system does not handle buffer bindings
11
+ */
12
+ export class WebGLStateTracker {
13
+ static get(gl) {
14
+ // @ts-expect-error
15
+ return gl.state;
16
+ }
12
17
  gl;
13
18
  program = null;
14
19
  stateStack = [];
15
20
  enable = true;
16
- cache;
21
+ cache = null;
17
22
  log;
18
- constructor(gl, { copyState = false, // Copy cache from params (slow) or initialize from WebGL defaults (fast)
19
- log = () => { } // Logging function, called when gl parameter change calls are actually issued
20
- } = {}) {
23
+ initialized = false;
24
+ constructor(gl, props) {
21
25
  this.gl = gl;
22
- this.cache = copyState ? getGLParameters(gl) : Object.assign({}, GL_PARAMETER_DEFAULTS);
23
- this.log = log;
26
+ this.log = props?.log || (() => { });
24
27
  this._updateCache = this._updateCache.bind(this);
25
28
  Object.seal(this);
26
29
  }
@@ -35,6 +38,32 @@ class GLState {
35
38
  // Don't pop until we have reset parameters (to make sure other "stack frames" are not affected)
36
39
  this.stateStack.pop();
37
40
  }
41
+ /**
42
+ * Initialize WebGL state caching on a context
43
+ * can be called multiple times to enable/disable
44
+ *
45
+ * @note After calling this function, context state will be cached
46
+ * .push() and .pop() will be available for saving,
47
+ * temporarily modifying, and then restoring state.
48
+ */
49
+ trackState(gl, options) {
50
+ this.cache = options.copyState ? getGLParameters(gl) : Object.assign({}, GL_PARAMETER_DEFAULTS);
51
+ if (this.initialized) {
52
+ throw new Error('WebGLStateTracker');
53
+ }
54
+ this.initialized = true;
55
+ // @ts-expect-error
56
+ this.gl.state = this;
57
+ installProgramSpy(gl);
58
+ // intercept all setter functions in the table
59
+ for (const key in GL_HOOKED_SETTERS) {
60
+ const setter = GL_HOOKED_SETTERS[key];
61
+ installSetterSpy(gl, key, setter);
62
+ }
63
+ // intercept all getter functions in the table
64
+ installGetterOverride(gl, 'getParameter');
65
+ installGetterOverride(gl, 'isEnabled');
66
+ }
38
67
  /**
39
68
  // interceptor for context set functions - update our cache and our stack
40
69
  // values (Object) - the key values for this setter
@@ -66,65 +95,6 @@ class GLState {
66
95
  return { valueChanged, oldValue };
67
96
  }
68
97
  }
69
- function getContextState(gl) {
70
- // @ts-expect-error
71
- return gl.state;
72
- }
73
- // PUBLIC API
74
- /**
75
- * Initialize WebGL state caching on a context
76
- * can be called multiple times to enable/disable
77
- *
78
- * @note After calling this function, context state will be cached
79
- * gl.state.push() and gl.state.pop() will be available for saving,
80
- * temporarily modifying, and then restoring state.
81
- */
82
- export function trackContextState(gl, options) {
83
- const { enable = true, copyState } = options || {};
84
- // assert(copyState !== undefined);
85
- // @ts-expect-error
86
- if (!gl.state) {
87
- // @ts-ignore
88
- // const {polyfillContext} = global_;
89
- // if (polyfillContext) {
90
- // polyfillContext(gl);
91
- // }
92
- // Create a state cache
93
- // @ts-expect-error
94
- gl.state = new GLState(gl, { copyState });
95
- installProgramSpy(gl);
96
- // intercept all setter functions in the table
97
- for (const key in GL_HOOKED_SETTERS) {
98
- const setter = GL_HOOKED_SETTERS[key];
99
- installSetterSpy(gl, key, setter);
100
- }
101
- // intercept all getter functions in the table
102
- installGetterOverride(gl, 'getParameter');
103
- installGetterOverride(gl, 'isEnabled');
104
- }
105
- const glState = getContextState(gl);
106
- glState.enable = enable;
107
- return gl;
108
- }
109
- /**
110
- * Saves current WebGL context state onto an internal per-context stack
111
- */
112
- export function pushContextState(gl) {
113
- let glState = getContextState(gl);
114
- if (!glState) {
115
- trackContextState(gl, { copyState: false });
116
- glState = getContextState(gl);
117
- }
118
- glState.push();
119
- }
120
- /**
121
- * Restores previously saved WebGL context state
122
- */
123
- export function popContextState(gl) {
124
- const glState = getContextState(gl);
125
- // assert(glState);
126
- glState.pop();
127
- }
128
98
  // HELPER FUNCTIONS - INSTALL GET/SET INTERCEPTORS (SPYS) ON THE CONTEXT
129
99
  /**
130
100
  // Overrides a WebGL2RenderingContext state "getter" function
@@ -141,7 +111,7 @@ function installGetterOverride(gl, functionName) {
141
111
  // Invalid or blacklisted parameter, do not cache
142
112
  return originalGetterFunc(pname);
143
113
  }
144
- const glState = getContextState(gl);
114
+ const glState = WebGLStateTracker.get(gl);
145
115
  if (!(pname in glState.cache)) {
146
116
  // WebGL limits are not prepopulated in the cache, call the original getter when first queried.
147
117
  glState.cache[pname] = originalGetterFunc(pname);
@@ -180,7 +150,7 @@ function installSetterSpy(gl, functionName, setter) {
180
150
  gl[functionName] = function set(...params) {
181
151
  // Update the value
182
152
  // Call the setter with the state cache and the params so that it can store the parameters
183
- const glState = getContextState(gl);
153
+ const glState = WebGLStateTracker.get(gl);
184
154
  // eslint-disable-next-line @typescript-eslint/unbound-method
185
155
  const { valueChanged, oldValue } = setter(glState._updateCache, ...params);
186
156
  // Call the original WebGL2RenderingContext func to make sure the context actually gets updated
@@ -202,7 +172,7 @@ function installSetterSpy(gl, functionName, setter) {
202
172
  function installProgramSpy(gl) {
203
173
  const originalUseProgram = gl.useProgram.bind(gl);
204
174
  gl.useProgram = function useProgramLuma(handle) {
205
- const glState = getContextState(gl);
175
+ const glState = WebGLStateTracker.get(gl);
206
176
  if (glState.program !== handle) {
207
177
  originalUseProgram(handle);
208
178
  glState.program = handle;
@@ -1 +1 @@
1
- {"version":3,"file":"with-parameters.d.ts","sourceRoot":"","sources":["../../../src/context/state-tracker/with-parameters.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,YAAY,EAAkB,+CAA4C;AAGlF;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,EAAE,EAAE,sBAAsB,EAC1B,UAAU,EAAE,YAAY,GAAG;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAC,EAC9C,IAAI,EAAE,GAAG,GACR,GAAG,CA4BL"}
1
+ {"version":3,"file":"with-parameters.d.ts","sourceRoot":"","sources":["../../../src/context/state-tracker/with-parameters.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,YAAY,EAAkB,+CAA4C;AAGlF;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAC9B,EAAE,EAAE,sBAAsB,EAC1B,UAAU,EAAE,YAAY,GAAG;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAC,EAC9C,IAAI,EAAE,GAAG,GACR,GAAG,CA6BL"}
@@ -2,7 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright (c) vis.gl contributors
4
4
  import { setGLParameters } from "../parameters/unified-parameter-api.js";
5
- import { pushContextState, popContextState } from "./track-context-state.js";
5
+ import { WebGLStateTracker } from "./webgl-state-tracker.js";
6
6
  /**
7
7
  * Execute a function with a set of temporary WebGL parameter overrides
8
8
  * - Saves current "global" WebGL context settings
@@ -17,14 +17,15 @@ export function withGLParameters(gl, parameters, func) {
17
17
  return func(gl);
18
18
  }
19
19
  const { nocatch = true } = parameters;
20
- pushContextState(gl);
20
+ const webglState = WebGLStateTracker.get(gl);
21
+ webglState.push();
21
22
  setGLParameters(gl, parameters);
22
23
  // Setup is done, call the function
23
24
  let value;
24
25
  if (nocatch) {
25
26
  // Avoid try catch to minimize stack size impact for safe execution paths
26
27
  value = func(gl);
27
- popContextState(gl);
28
+ webglState.pop();
28
29
  }
29
30
  else {
30
31
  // Wrap in a try-catch to ensure that parameters are restored on exceptions
@@ -32,7 +33,7 @@ export function withGLParameters(gl, parameters, func) {
32
33
  value = func(gl);
33
34
  }
34
35
  finally {
35
- popContextState(gl);
36
+ webglState.pop();
36
37
  }
37
38
  }
38
39
  return value;