@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.
- package/dist/adapter/converters/device-parameters.d.ts.map +1 -1
- package/dist/adapter/converters/device-parameters.js +18 -11
- package/dist/adapter/converters/texture-formats.d.ts +1 -1
- package/dist/adapter/converters/texture-formats.d.ts.map +1 -1
- package/dist/adapter/converters/texture-formats.js +9 -16
- package/dist/adapter/device-helpers/webgl-device-features.d.ts.map +1 -1
- package/dist/adapter/device-helpers/webgl-device-features.js +1 -3
- package/dist/adapter/helpers/format-utils.d.ts.map +1 -0
- package/dist/adapter/helpers/get-shader-layout.d.ts.map +1 -1
- package/dist/adapter/helpers/get-shader-layout.js +1 -3
- package/dist/adapter/helpers/typed-array-utils.d.ts.map +1 -0
- package/dist/adapter/helpers/webgl-texture-utils.d.ts +89 -22
- package/dist/adapter/helpers/webgl-texture-utils.d.ts.map +1 -1
- package/dist/adapter/helpers/webgl-texture-utils.js +220 -26
- package/dist/adapter/resources/webgl-framebuffer.js +1 -1
- package/dist/adapter/resources/webgl-render-pass.d.ts.map +1 -1
- package/dist/adapter/resources/webgl-render-pass.js +17 -4
- package/dist/adapter/resources/webgl-render-pipeline.d.ts +1 -3
- package/dist/adapter/resources/webgl-render-pipeline.d.ts.map +1 -1
- package/dist/adapter/resources/webgl-render-pipeline.js +1 -1
- package/dist/adapter/resources/webgl-shader.js +1 -1
- package/dist/adapter/resources/webgl-texture.d.ts +21 -3
- package/dist/adapter/resources/webgl-texture.d.ts.map +1 -1
- package/dist/adapter/resources/webgl-texture.js +49 -30
- package/dist/adapter/resources/webgl-transform-feedback.js +1 -1
- package/dist/adapter/resources/webgl-vertex-array.d.ts.map +1 -1
- package/dist/adapter/resources/webgl-vertex-array.js +3 -0
- package/dist/adapter/webgl-adapter.d.ts +21 -0
- package/dist/adapter/webgl-adapter.d.ts.map +1 -0
- package/dist/adapter/webgl-adapter.js +91 -0
- package/dist/adapter/webgl-device.d.ts +16 -29
- package/dist/adapter/webgl-device.d.ts.map +1 -1
- package/dist/adapter/webgl-device.js +34 -114
- package/dist/context/debug/spector-types.d.ts +1108 -0
- package/dist/context/debug/spector-types.d.ts.map +1 -0
- package/dist/context/debug/spector-types.js +697 -0
- package/dist/context/debug/spector.d.ts +12 -8
- package/dist/context/debug/spector.d.ts.map +1 -1
- package/dist/context/debug/spector.js +23 -17
- package/dist/context/polyfills/polyfill-webgl1-extensions.d.ts +9 -0
- package/dist/context/polyfills/polyfill-webgl1-extensions.d.ts.map +1 -0
- package/dist/context/polyfills/polyfill-webgl1-extensions.js +181 -0
- package/dist/context/state-tracker/webgl-state-tracker.d.ts +43 -0
- package/dist/context/state-tracker/webgl-state-tracker.d.ts.map +1 -0
- package/dist/context/state-tracker/{track-context-state.js → webgl-state-tracker.js} +44 -74
- package/dist/context/state-tracker/with-parameters.d.ts.map +1 -1
- package/dist/context/state-tracker/with-parameters.js +5 -4
- package/dist/deprecated/accessor.d.ts.map +1 -0
- package/dist/{classic → deprecated}/accessor.js +36 -1
- package/dist/deprecated/clear.d.ts.map +1 -0
- package/dist/{classic → deprecated}/clear.js +2 -0
- package/dist/dist.dev.js +816 -642
- package/dist/dist.min.js +2 -2
- package/dist/index.cjs +815 -648
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -3
- package/dist/utils/fill-array.d.ts +4 -4
- package/dist/utils/fill-array.d.ts.map +1 -1
- package/dist/utils/split-uniforms-and-bindings.d.ts +1 -1
- package/dist/utils/split-uniforms-and-bindings.d.ts.map +1 -1
- package/dist/utils/uid.d.ts +7 -0
- package/dist/utils/uid.d.ts.map +1 -0
- package/dist/utils/uid.js +14 -0
- package/package.json +5 -5
- package/src/adapter/converters/device-parameters.ts +18 -12
- package/src/adapter/converters/texture-formats.ts +12 -20
- package/src/adapter/device-helpers/webgl-device-features.ts +5 -3
- package/src/adapter/helpers/get-shader-layout.ts +1 -3
- package/src/adapter/helpers/webgl-texture-utils.ts +366 -44
- package/src/adapter/resources/webgl-framebuffer.ts +1 -1
- package/src/adapter/resources/webgl-render-pass.ts +20 -7
- package/src/adapter/resources/webgl-render-pipeline.ts +12 -4
- package/src/adapter/resources/webgl-shader.ts +1 -1
- package/src/adapter/resources/webgl-texture.ts +76 -30
- package/src/adapter/resources/webgl-transform-feedback.ts +1 -1
- package/src/adapter/resources/webgl-vertex-array.ts +3 -0
- package/src/adapter/webgl-adapter.ts +113 -0
- package/src/adapter/webgl-device.ts +45 -139
- package/src/context/debug/spector-types.ts +1154 -0
- package/src/context/debug/spector.ts +38 -29
- package/src/context/polyfills/polyfill-webgl1-extensions.ts +202 -0
- package/src/context/state-tracker/{track-context-state.ts → webgl-state-tracker.ts} +55 -94
- package/src/context/state-tracker/with-parameters.ts +5 -4
- package/src/{classic → deprecated}/accessor.ts +44 -3
- package/src/{classic → deprecated}/clear.ts +3 -1
- package/src/index.ts +6 -8
- package/src/utils/fill-array.ts +4 -4
- package/src/utils/split-uniforms-and-bindings.ts +3 -3
- package/src/utils/uid.ts +16 -0
- package/dist/classic/accessor.d.ts.map +0 -1
- package/dist/classic/clear.d.ts.map +0 -1
- package/dist/classic/copy-and-blit.d.ts +0 -63
- package/dist/classic/copy-and-blit.d.ts.map +0 -1
- package/dist/classic/copy-and-blit.js +0 -193
- package/dist/classic/format-utils.d.ts.map +0 -1
- package/dist/classic/typed-array-utils.d.ts.map +0 -1
- package/dist/context/state-tracker/track-context-state.d.ts +0 -22
- package/dist/context/state-tracker/track-context-state.d.ts.map +0 -1
- package/src/classic/copy-and-blit.ts +0 -318
- /package/dist/{classic → adapter/helpers}/format-utils.d.ts +0 -0
- /package/dist/{classic → adapter/helpers}/format-utils.js +0 -0
- /package/dist/{classic → adapter/helpers}/typed-array-utils.d.ts +0 -0
- /package/dist/{classic → adapter/helpers}/typed-array-utils.js +0 -0
- /package/dist/{classic → deprecated}/accessor.d.ts +0 -0
- /package/dist/{classic → deprecated}/clear.d.ts +0 -0
- /package/src/{classic → adapter/helpers}/format-utils.ts +0 -0
- /package/src/{classic → adapter/helpers}/typed-array-utils.ts +0 -0
|
@@ -16,7 +16,7 @@ import { WEBGLTextureView } from "./webgl-texture-view.js";
|
|
|
16
16
|
// import type {WebGLSetTextureOptions, WebGLCopyTextureOptions} from '../helpers/webgl-texture-utils';
|
|
17
17
|
import { initializeTextureStorage,
|
|
18
18
|
// clearMipLevel,
|
|
19
|
-
|
|
19
|
+
copyExternalImageToMipLevel, copyCPUDataToMipLevel,
|
|
20
20
|
// copyGPUBufferToMipLevel,
|
|
21
21
|
getWebGLTextureTarget } from "../helpers/webgl-texture-utils.js";
|
|
22
22
|
// PORTABLE HELPERS (Move to methods on Texture?)
|
|
@@ -125,7 +125,7 @@ export class WEBGLTexture extends Texture {
|
|
|
125
125
|
// const {parameters = {} as Record<GL, any>} = props;
|
|
126
126
|
let { width, height } = props;
|
|
127
127
|
if (!width || !height) {
|
|
128
|
-
const textureSize =
|
|
128
|
+
const textureSize = Texture.getTextureDataSize(data);
|
|
129
129
|
width = textureSize?.width || 1;
|
|
130
130
|
height = textureSize?.height || 1;
|
|
131
131
|
}
|
|
@@ -263,11 +263,40 @@ export class WEBGLTexture extends Texture {
|
|
|
263
263
|
this.gl.bindTexture(this.glTarget, null);
|
|
264
264
|
}
|
|
265
265
|
// Image Data Setters
|
|
266
|
+
copyExternalImage(options) {
|
|
267
|
+
const size = Texture.getExternalImageSize(options.image);
|
|
268
|
+
const opts = { ...Texture.defaultCopyExternalImageOptions, ...size, ...options };
|
|
269
|
+
const { image, depth, mipLevel, x, y, z } = opts;
|
|
270
|
+
let { width, height } = opts;
|
|
271
|
+
const { dimension, glTarget, glFormat, glInternalFormat, glType } = this;
|
|
272
|
+
// WebGL will error if we try to copy outside the bounds of the texture
|
|
273
|
+
width = Math.min(width, size.width - x);
|
|
274
|
+
height = Math.min(height, size.height - y);
|
|
275
|
+
// WebGL does not yet support sourceX/sourceY in copyExternalImage; requires copyTexSubImage2D from a framebuffer'
|
|
276
|
+
if (options.sourceX || options.sourceY) {
|
|
277
|
+
throw new Error('WebGL does not yet support sourceX/sourceY in copyExternalImage; requires copyTexSubImage2D from a framebuffer');
|
|
278
|
+
}
|
|
279
|
+
copyExternalImageToMipLevel(this.device.gl, this.handle, image, {
|
|
280
|
+
dimension,
|
|
281
|
+
mipLevel,
|
|
282
|
+
x,
|
|
283
|
+
y,
|
|
284
|
+
z,
|
|
285
|
+
width,
|
|
286
|
+
height,
|
|
287
|
+
depth,
|
|
288
|
+
glFormat,
|
|
289
|
+
glInternalFormat,
|
|
290
|
+
glType,
|
|
291
|
+
glTarget
|
|
292
|
+
});
|
|
293
|
+
return { width: opts.width, height: opts.height };
|
|
294
|
+
}
|
|
266
295
|
setTexture1DData(data) {
|
|
267
296
|
throw new Error('setTexture1DData not supported in WebGL.');
|
|
268
297
|
}
|
|
269
298
|
/** Set a simple texture */
|
|
270
|
-
setTexture2DData(lodData, depth = 0
|
|
299
|
+
setTexture2DData(lodData, depth = 0) {
|
|
271
300
|
this.bind();
|
|
272
301
|
const lodArray = normalizeTextureData(lodData, this);
|
|
273
302
|
// If the user provides multiple LODs, then automatic mipmap
|
|
@@ -290,7 +319,9 @@ export class WEBGLTexture extends Texture {
|
|
|
290
319
|
throw new Error(this.id);
|
|
291
320
|
}
|
|
292
321
|
if (ArrayBuffer.isView(data)) {
|
|
322
|
+
this.bind();
|
|
293
323
|
copyCPUDataToMipLevel(this.device.gl, data, this);
|
|
324
|
+
this.unbind();
|
|
294
325
|
}
|
|
295
326
|
}
|
|
296
327
|
/**
|
|
@@ -303,9 +334,9 @@ export class WEBGLTexture extends Texture {
|
|
|
303
334
|
if (this.props.dimension !== 'cube') {
|
|
304
335
|
throw new Error(this.id);
|
|
305
336
|
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
337
|
+
for (const face of Texture.CubeFaces) {
|
|
338
|
+
this.setTextureCubeFaceData(data[face], face);
|
|
339
|
+
}
|
|
309
340
|
}
|
|
310
341
|
/**
|
|
311
342
|
* Sets an entire texture array
|
|
@@ -331,26 +362,8 @@ export class WEBGLTexture extends Texture {
|
|
|
331
362
|
if (Array.isArray(lodData) && lodData.length > 1 && this.props.mipmaps !== false) {
|
|
332
363
|
log.warn(`${this.id} has mipmap and multiple LODs.`)();
|
|
333
364
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
// const {width, height, format = GL.RGBA, type = GL.UNSIGNED_BYTE} = this;
|
|
337
|
-
// const {width, height, format = GL.RGBA, type = GL.UNSIGNED_BYTE} = this;
|
|
338
|
-
this.bind();
|
|
339
|
-
// for (let lodLevel = 0; lodLevel < lodData.length; lodLevel++) {
|
|
340
|
-
// const imageData = lodData[lodLevel];
|
|
341
|
-
// if (imageData instanceof ArrayBuffer) {
|
|
342
|
-
// // const imageData = image instanceof ArrayBuffer ? new ImageData(new Uint8ClampedArray(image), this.width) : image;
|
|
343
|
-
// this.device.gl.texImage2D?.(
|
|
344
|
-
// glFace,
|
|
345
|
-
// lodLevel,
|
|
346
|
-
// this.glInternalFormat,
|
|
347
|
-
// this.glInternalFormat,
|
|
348
|
-
// glType,
|
|
349
|
-
// imageData
|
|
350
|
-
// );
|
|
351
|
-
// }
|
|
352
|
-
// }
|
|
353
|
-
this.unbind();
|
|
365
|
+
const faceDepth = Texture.CubeFaces.indexOf(face);
|
|
366
|
+
this.setTexture2DData(lodData, faceDepth);
|
|
354
367
|
}
|
|
355
368
|
// INTERNAL METHODS
|
|
356
369
|
/** @todo update this method to accept LODs */
|
|
@@ -482,21 +495,27 @@ export class WEBGLTexture extends Texture {
|
|
|
482
495
|
* Copy a region of data from a CPU memory buffer into this texture.
|
|
483
496
|
* @todo - GLUnpackParameters parameters
|
|
484
497
|
*/
|
|
485
|
-
_setMipLevel(depth,
|
|
498
|
+
_setMipLevel(depth, mipLevel, textureData, glTarget = this.glTarget) {
|
|
486
499
|
// if (!textureData) {
|
|
487
500
|
// clearMipLevel(this.device.gl, {...this, depth, level});
|
|
488
501
|
// return;
|
|
489
502
|
// }
|
|
490
503
|
if (Texture.isExternalImage(textureData)) {
|
|
491
|
-
|
|
504
|
+
copyExternalImageToMipLevel(this.device.gl, this.handle, textureData, {
|
|
505
|
+
...this,
|
|
506
|
+
depth,
|
|
507
|
+
mipLevel,
|
|
508
|
+
glTarget
|
|
509
|
+
});
|
|
492
510
|
return;
|
|
493
511
|
}
|
|
494
512
|
// @ts-expect-error
|
|
495
|
-
if (
|
|
513
|
+
if (Texture.isTextureLevelData(textureData)) {
|
|
496
514
|
copyCPUDataToMipLevel(this.device.gl, textureData.data, {
|
|
497
515
|
...this,
|
|
498
516
|
depth,
|
|
499
|
-
|
|
517
|
+
mipLevel,
|
|
518
|
+
glTarget
|
|
500
519
|
});
|
|
501
520
|
return;
|
|
502
521
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webgl-vertex-array.d.ts","sourceRoot":"","sources":["../../../src/adapter/resources/webgl-vertex-array.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,UAAU,EAAe,MAAM,gBAAgB,CAAC;AAC7D,OAAO,KAAK,EAAC,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAC,MAAM,eAAe,CAAC;AACpE,OAAO,EAAC,WAAW,EAAkB,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAC,EAAE,EAAC,MAAM,oBAAoB,CAAC;AAGtC,OAAO,EAAC,WAAW,EAAC,2BAAwB;AAM5C,gCAAgC;AAChC,qBAAa,gBAAiB,SAAQ,WAAW;IAC/C,IAAa,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,MAAM,CAE1C;IAED,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,sBAAsB,CAAC;IAExC,kCAAkC;IAClC,OAAO,CAAC,MAAM,CAA4B;IAC1C,OAAO,CAAC,WAAW,CA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,gGAAgG;IAChG,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,GAAG,IAAI;IAmC1D,2FAA2F;IAClF,gBAAgB,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,IAAI;IAK3D,gBAAgB,IAAI,IAAI;IAKxB,iBAAiB,IAAI,IAAI;IAOlC;;;;;OAKG;IACH,SAAS,CAAC,wBAAwB,IAAI,IAAI;IAU1C;;;OAGG;IAcH,gCAAgC;IAChC,SAAS,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM;;;;;;;;;IAsBvC;;;;;OAKG;IACH,SAAS,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,UAAO,GAAG,IAAI;IAiBxD;;;;;OAKG;IACH,iBAAiB,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,GAAG,MAAM;CA8BnE"}
|
|
@@ -77,6 +77,9 @@ export class WEBGLVertexArray extends VertexArray {
|
|
|
77
77
|
// Attaches ARRAY_BUFFER with specified buffer format to location
|
|
78
78
|
this.device.gl.vertexAttribPointer(location, size, type, normalized, stride, offset);
|
|
79
79
|
}
|
|
80
|
+
// Clear binding - keeping it may cause [.WebGL-0x12804417100]
|
|
81
|
+
// GL_INVALID_OPERATION: A transform feedback buffer that would be written to is also bound to a non-transform-feedback target
|
|
82
|
+
this.device.gl.bindBuffer(34962, null);
|
|
80
83
|
// Mark as non-constant
|
|
81
84
|
this.device.gl.enableVertexAttribArray(location);
|
|
82
85
|
// Set the step mode 0=vertex, 1=instance
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Adapter, Device, DeviceProps } from '@luma.gl/core';
|
|
2
|
+
import { WebGLDevice } from "./webgl-device.js";
|
|
3
|
+
export declare class WebGLAdapter extends Adapter {
|
|
4
|
+
/** type of device's created by this adapter */
|
|
5
|
+
readonly type: Device['type'];
|
|
6
|
+
constructor();
|
|
7
|
+
/** Check if WebGL 2 is available */
|
|
8
|
+
isSupported(): boolean;
|
|
9
|
+
/** Force any created WebGL contexts to be WebGL2 contexts, polyfilled with WebGL1 extensions */
|
|
10
|
+
enforceWebGL2(enable: boolean): void;
|
|
11
|
+
/**
|
|
12
|
+
* Get a device instance from a GL context
|
|
13
|
+
* Creates and instruments the device if not already created
|
|
14
|
+
* @param gl
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
attach(gl: Device | WebGL2RenderingContext): Promise<WebGLDevice>;
|
|
18
|
+
create(props?: DeviceProps): Promise<WebGLDevice>;
|
|
19
|
+
}
|
|
20
|
+
export declare const webgl2Adapter: WebGLAdapter;
|
|
21
|
+
//# sourceMappingURL=webgl-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webgl-adapter.d.ts","sourceRoot":"","sources":["../../src/adapter/webgl-adapter.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAqB,MAAM,eAAe,CAAC;AAC/E,OAAO,EAAC,WAAW,EAAC,0BAAuB;AAO3C,qBAAa,YAAa,SAAQ,OAAO;IACvC,+CAA+C;IAC/C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,CAAC,CAAW;;IAYxC,oCAAoC;IACpC,WAAW,IAAI,OAAO;IAItB,gGAAgG;IAChG,aAAa,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI;IAIpC;;;;;OAKG;IACG,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,sBAAsB,GAAG,OAAO,CAAC,WAAW,CAAC;IAejE,MAAM,CAAC,KAAK,GAAE,WAAgB,GAAG,OAAO,CAAC,WAAW,CAAC;CA4C5D;AAWD,eAAO,MAAM,aAAa,cAAqB,CAAC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
import { Adapter, Device, CanvasContext, log } from '@luma.gl/core';
|
|
5
|
+
import { WebGLDevice } from "./webgl-device.js";
|
|
6
|
+
import { enforceWebGL2 } from "../context/polyfills/polyfill-webgl1-extensions.js";
|
|
7
|
+
import { loadSpectorJS, DEFAULT_SPECTOR_PROPS } from "../context/debug/spector.js";
|
|
8
|
+
import { loadWebGLDeveloperTools } from "../context/debug/webgl-developer-tools.js";
|
|
9
|
+
const LOG_LEVEL = 1;
|
|
10
|
+
export class WebGLAdapter extends Adapter {
|
|
11
|
+
/** type of device's created by this adapter */
|
|
12
|
+
type = 'webgl';
|
|
13
|
+
constructor() {
|
|
14
|
+
super();
|
|
15
|
+
// Add spector default props to device default props, so that runtime settings are observed
|
|
16
|
+
Device.defaultProps = { ...Device.defaultProps, ...DEFAULT_SPECTOR_PROPS };
|
|
17
|
+
// @ts-ignore DEPRECATED For backwards compatibility luma.registerDevices
|
|
18
|
+
WebGLDevice.adapter = this;
|
|
19
|
+
}
|
|
20
|
+
/** Check if WebGL 2 is available */
|
|
21
|
+
isSupported() {
|
|
22
|
+
return typeof WebGL2RenderingContext !== 'undefined';
|
|
23
|
+
}
|
|
24
|
+
/** Force any created WebGL contexts to be WebGL2 contexts, polyfilled with WebGL1 extensions */
|
|
25
|
+
enforceWebGL2(enable) {
|
|
26
|
+
enforceWebGL2(enable);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get a device instance from a GL context
|
|
30
|
+
* Creates and instruments the device if not already created
|
|
31
|
+
* @param gl
|
|
32
|
+
* @returns
|
|
33
|
+
*/
|
|
34
|
+
async attach(gl) {
|
|
35
|
+
if (gl instanceof WebGLDevice) {
|
|
36
|
+
return gl;
|
|
37
|
+
}
|
|
38
|
+
// @ts-expect-error
|
|
39
|
+
if (gl?.device instanceof Device) {
|
|
40
|
+
// @ts-expect-error
|
|
41
|
+
return gl.device;
|
|
42
|
+
}
|
|
43
|
+
if (!isWebGL(gl)) {
|
|
44
|
+
throw new Error('Invalid WebGL2RenderingContext');
|
|
45
|
+
}
|
|
46
|
+
return new WebGLDevice({ gl: gl });
|
|
47
|
+
}
|
|
48
|
+
async create(props = {}) {
|
|
49
|
+
log.groupCollapsed(LOG_LEVEL, 'WebGLDevice created')();
|
|
50
|
+
const promises = [];
|
|
51
|
+
// Load webgl and spector debug scripts from CDN if requested
|
|
52
|
+
if (props.debug) {
|
|
53
|
+
promises.push(loadWebGLDeveloperTools());
|
|
54
|
+
}
|
|
55
|
+
if (props.debugWithSpectorJS) {
|
|
56
|
+
promises.push(loadSpectorJS(props));
|
|
57
|
+
}
|
|
58
|
+
// Wait for page to load: if canvas is a string we need to query the DOM for the canvas element.
|
|
59
|
+
// We only wait when props.canvas is string to avoids setting the global page onload callback unless necessary.
|
|
60
|
+
if (typeof props.canvas === 'string') {
|
|
61
|
+
promises.push(CanvasContext.pageLoaded);
|
|
62
|
+
}
|
|
63
|
+
// Wait for all the loads to settle before creating the context.
|
|
64
|
+
// The Device.create() functions are async, so in contrast to the constructor, we can `await` here.
|
|
65
|
+
const results = await Promise.allSettled(promises);
|
|
66
|
+
for (const result of results) {
|
|
67
|
+
if (result.status === 'rejected') {
|
|
68
|
+
log.error(`Failed to initialize debug libraries ${result.reason}`)();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
log.probe(LOG_LEVEL + 1, 'DOM is loaded')();
|
|
72
|
+
const device = new WebGLDevice(props);
|
|
73
|
+
// Log some debug info about the newly created context
|
|
74
|
+
const message = `\
|
|
75
|
+
Created ${device.type}${device.debug ? ' debug' : ''} context: \
|
|
76
|
+
${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContext.id}`;
|
|
77
|
+
log.probe(LOG_LEVEL, message)();
|
|
78
|
+
log.table(LOG_LEVEL, device.info)();
|
|
79
|
+
log.groupEnd(LOG_LEVEL)();
|
|
80
|
+
return device;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Check if supplied parameter is a WebGL2RenderingContext */
|
|
84
|
+
function isWebGL(gl) {
|
|
85
|
+
if (typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext) {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
// Look for debug contexts, headless gl etc
|
|
89
|
+
return Boolean(gl && Number.isFinite(gl._version));
|
|
90
|
+
}
|
|
91
|
+
export const webgl2Adapter = new WebGLAdapter();
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { TypedArray } from '@math.gl/types';
|
|
2
|
-
import type { DeviceProps, DeviceInfo, CanvasContextProps, TextureFormat } from '@luma.gl/core';
|
|
3
|
-
import type { Buffer, Texture, Framebuffer, VertexArray, VertexArrayProps } from '@luma.gl/core';
|
|
2
|
+
import type { DeviceProps, DeviceInfo, CanvasContextProps, TextureFormat, Buffer, Texture, Framebuffer, VertexArray, VertexArrayProps } from '@luma.gl/core';
|
|
4
3
|
import { Device, CanvasContext } from '@luma.gl/core';
|
|
5
4
|
import type { GLExtensions } from '@luma.gl/constants';
|
|
6
5
|
import { WebGLDeviceFeatures } from "./device-helpers/webgl-device-features.js";
|
|
7
6
|
import { WebGLDeviceLimits } from "./device-helpers/webgl-device-limits.js";
|
|
8
7
|
import { WebGLCanvasContext } from "./webgl-canvas-context.js";
|
|
8
|
+
import type { Spector } from "../context/debug/spector-types.js";
|
|
9
9
|
import type { BufferProps, ShaderProps, SamplerProps, TextureProps, ExternalTexture, ExternalTextureProps, FramebufferProps, RenderPipelineProps, ComputePipeline, ComputePipelineProps, RenderPassProps, ComputePass, ComputePassProps, CommandEncoderProps, TransformFeedbackProps, QuerySetProps } from '@luma.gl/core';
|
|
10
10
|
import { WEBGLBuffer } from "./resources/webgl-buffer.js";
|
|
11
11
|
import { WEBGLShader } from "./resources/webgl-shader.js";
|
|
@@ -18,8 +18,6 @@ import { WEBGLTransformFeedback } from "./resources/webgl-transform-feedback.js"
|
|
|
18
18
|
import { WEBGLQuerySet } from "./resources/webgl-query-set.js";
|
|
19
19
|
/** WebGPU style Device API for a WebGL context */
|
|
20
20
|
export declare class WebGLDevice extends Device {
|
|
21
|
-
/** type of this device */
|
|
22
|
-
static readonly type: string;
|
|
23
21
|
/** type of this device */
|
|
24
22
|
readonly type = "webgl";
|
|
25
23
|
/** The underlying WebGL context */
|
|
@@ -33,16 +31,20 @@ export declare class WebGLDevice extends Device {
|
|
|
33
31
|
message: string;
|
|
34
32
|
}>;
|
|
35
33
|
private _resolveContextLost?;
|
|
36
|
-
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
/** WebGL2 context. */
|
|
35
|
+
readonly gl: WebGL2RenderingContext;
|
|
36
|
+
readonly debug: boolean;
|
|
37
|
+
/** State used by luma.gl classes: TODO - move to canvasContext*/
|
|
38
|
+
readonly _canvasSizeInfo: {
|
|
39
|
+
clientWidth: number;
|
|
40
|
+
clientHeight: number;
|
|
41
|
+
devicePixelRatio: number;
|
|
42
|
+
};
|
|
43
|
+
/** State used by luma.gl classes - TODO - not used? */
|
|
44
|
+
readonly _extensions: GLExtensions;
|
|
45
|
+
_polyfilled: boolean;
|
|
46
|
+
/** Instance of Spector.js (if initialized) */
|
|
47
|
+
spectorJS: Spector;
|
|
46
48
|
constructor(props: DeviceProps);
|
|
47
49
|
/**
|
|
48
50
|
* Destroys the context
|
|
@@ -50,7 +52,6 @@ export declare class WebGLDevice extends Device {
|
|
|
50
52
|
*/
|
|
51
53
|
destroy(): void;
|
|
52
54
|
get isLost(): boolean;
|
|
53
|
-
getSize(): [number, number];
|
|
54
55
|
isTextureFormatSupported(format: TextureFormat): boolean;
|
|
55
56
|
isTextureFormatFilterable(format: TextureFormat): boolean;
|
|
56
57
|
isTextureFormatRenderable(format: TextureFormat): boolean;
|
|
@@ -108,20 +109,6 @@ export declare class WebGLDevice extends Device {
|
|
|
108
109
|
stencil?: any;
|
|
109
110
|
}): void;
|
|
110
111
|
resetWebGL(): void;
|
|
111
|
-
/** WebGL2 context. */
|
|
112
|
-
readonly gl: WebGL2RenderingContext;
|
|
113
|
-
readonly debug: boolean;
|
|
114
|
-
/** State used by luma.gl classes: TODO - move to canvasContext*/
|
|
115
|
-
readonly _canvasSizeInfo: {
|
|
116
|
-
clientWidth: number;
|
|
117
|
-
clientHeight: number;
|
|
118
|
-
devicePixelRatio: number;
|
|
119
|
-
};
|
|
120
|
-
/** State used by luma.gl classes - TODO - not used? */
|
|
121
|
-
readonly _extensions: GLExtensions;
|
|
122
|
-
_polyfilled: boolean;
|
|
123
|
-
/** Instance of Spector.js (if initialized) */
|
|
124
|
-
spectorJS: unknown;
|
|
125
112
|
/**
|
|
126
113
|
* Triggers device (or WebGL context) loss.
|
|
127
114
|
* @note primarily intended for testing how application reacts to device loss
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"webgl-device.d.ts","sourceRoot":"","sources":["../../src/adapter/webgl-device.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"webgl-device.d.ts","sourceRoot":"","sources":["../../src/adapter/webgl-device.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,EACV,WAAW,EACX,UAAU,EACV,kBAAkB,EAClB,aAAa,EACb,MAAM,EACN,OAAO,EACP,WAAW,EACX,WAAW,EACX,gBAAgB,EACjB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAC,MAAM,EAAE,aAAa,EAAM,MAAM,eAAe,CAAC;AACzD,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,oBAAoB,CAAC;AAIrD,OAAO,EAAC,mBAAmB,EAAC,kDAA+C;AAC3E,OAAO,EAAC,iBAAiB,EAAC,gDAA6C;AACvE,OAAO,EAAC,kBAAkB,EAAC,kCAA+B;AAC1D,OA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GAAG,CAAC;QACZ,KAAK,CAAC,EAAE,GAAG,CAAC;QACZ,OAAO,CAAC,EAAE,GAAG,CAAC;KACf,GAAG,IAAI;IAIC,UAAU,IAAI,IAAI;IAS3B;;;OAGG;IACM,UAAU,IAAI,OAAO;IAgB9B,8DAA8D;IAC9D,SAAS,IAAI,IAAI;IAKjB,8CAA8C;IAC9C,QAAQ,IAAI,IAAI;IAKhB;;;OAGG;IACH,kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAMlE;;;;OAIG;IACH,QAAQ,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CAAC,EAAE,sBAAsB,GAAG,MAAM;IAc7D,sBAAsB;IACtB,UAAU,EAAE,CAAC,UAAU,GAAG,IAAI,CAAC,EAAE,CAAC;IAElC;;;;;OAKG;IACH,yBAAyB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,UAAU,GAAG,IAAI;IA2BvE,gDAAgD;IAChD,YAAY,CAAC,IAAI,EAAE,MAAM,YAAY,GAAG,YAAY;CAIrD"}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
// luma.gl
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
|
-
import { Device,
|
|
5
|
-
import {
|
|
4
|
+
import { Device, log } from '@luma.gl/core';
|
|
5
|
+
import { WebGLStateTracker } from "../context/state-tracker/webgl-state-tracker.js";
|
|
6
6
|
import { createBrowserContext } from "../context/helpers/create-browser-context.js";
|
|
7
7
|
import { getDeviceInfo } from "./device-helpers/webgl-device-info.js";
|
|
8
8
|
import { WebGLDeviceFeatures } from "./device-helpers/webgl-device-features.js";
|
|
9
9
|
import { WebGLDeviceLimits } from "./device-helpers/webgl-device-limits.js";
|
|
10
10
|
import { WebGLCanvasContext } from "./webgl-canvas-context.js";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
11
|
+
import { initializeSpectorJS } from "../context/debug/spector.js";
|
|
12
|
+
import { makeDebugContext } from "../context/debug/webgl-developer-tools.js";
|
|
13
13
|
import { isTextureFormatSupported, isTextureFormatRenderable, isTextureFormatFilterable } from "./converters/texture-formats.js";
|
|
14
|
+
import { uid } from "../utils/uid.js";
|
|
14
15
|
import { WEBGLBuffer } from "./resources/webgl-buffer.js";
|
|
15
16
|
import { WEBGLShader } from "./resources/webgl-shader.js";
|
|
16
17
|
import { WEBGLSampler } from "./resources/webgl-sampler.js";
|
|
@@ -22,20 +23,17 @@ import { WEBGLCommandEncoder } from "./resources/webgl-command-encoder.js";
|
|
|
22
23
|
import { WEBGLVertexArray } from "./resources/webgl-vertex-array.js";
|
|
23
24
|
import { WEBGLTransformFeedback } from "./resources/webgl-transform-feedback.js";
|
|
24
25
|
import { WEBGLQuerySet } from "./resources/webgl-query-set.js";
|
|
25
|
-
import { readPixelsToArray, readPixelsToBuffer } from "
|
|
26
|
+
import { readPixelsToArray, readPixelsToBuffer } from "./helpers/webgl-texture-utils.js";
|
|
26
27
|
import { setGLParameters, getGLParameters, resetGLParameters } from "../context/parameters/unified-parameter-api.js";
|
|
27
28
|
import { withGLParameters } from "../context/state-tracker/with-parameters.js";
|
|
28
|
-
import { clear } from "../
|
|
29
|
+
import { clear } from "../deprecated/clear.js";
|
|
29
30
|
import { getWebGLExtension } from "../context/helpers/webgl-extensions.js";
|
|
30
|
-
const LOG_LEVEL = 1;
|
|
31
31
|
/** WebGPU style Device API for a WebGL context */
|
|
32
32
|
export class WebGLDevice extends Device {
|
|
33
33
|
//
|
|
34
34
|
// Public `Device` API
|
|
35
35
|
//
|
|
36
36
|
/** type of this device */
|
|
37
|
-
static type = 'webgl';
|
|
38
|
-
/** type of this device */
|
|
39
37
|
type = 'webgl';
|
|
40
38
|
/** The underlying WebGL context */
|
|
41
39
|
handle;
|
|
@@ -45,77 +43,21 @@ export class WebGLDevice extends Device {
|
|
|
45
43
|
canvasContext;
|
|
46
44
|
lost;
|
|
47
45
|
_resolveContextLost;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
* @param gl
|
|
59
|
-
* @returns
|
|
60
|
-
*/
|
|
61
|
-
static attach(gl) {
|
|
62
|
-
if (gl instanceof WebGLDevice) {
|
|
63
|
-
return gl;
|
|
64
|
-
}
|
|
65
|
-
// @ts-expect-error
|
|
66
|
-
if (gl?.device instanceof Device) {
|
|
67
|
-
// @ts-expect-error
|
|
68
|
-
return gl.device;
|
|
69
|
-
}
|
|
70
|
-
if (!isWebGL(gl)) {
|
|
71
|
-
throw new Error('Invalid WebGL2RenderingContext');
|
|
72
|
-
}
|
|
73
|
-
return new WebGLDevice({ gl: gl });
|
|
74
|
-
}
|
|
75
|
-
static async create(props = {}) {
|
|
76
|
-
log.groupCollapsed(LOG_LEVEL, 'WebGLDevice created')();
|
|
77
|
-
const promises = [];
|
|
78
|
-
// Load webgl and spector debug scripts from CDN if requested
|
|
79
|
-
if (props.debug) {
|
|
80
|
-
promises.push(loadWebGLDeveloperTools());
|
|
81
|
-
}
|
|
82
|
-
if (props.spector) {
|
|
83
|
-
promises.push(loadSpectorJS());
|
|
84
|
-
}
|
|
85
|
-
// Wait for page to load: if canvas is a string we need to query the DOM for the canvas element.
|
|
86
|
-
// We only wait when props.canvas is string to avoids setting the global page onload callback unless necessary.
|
|
87
|
-
if (typeof props.canvas === 'string') {
|
|
88
|
-
promises.push(CanvasContext.pageLoaded);
|
|
89
|
-
}
|
|
90
|
-
// Wait for all the loads to settle before creating the context.
|
|
91
|
-
// The Device.create() functions are async, so in contrast to the constructor, we can `await` here.
|
|
92
|
-
const results = await Promise.allSettled(promises);
|
|
93
|
-
for (const result of results) {
|
|
94
|
-
if (result.status === 'rejected') {
|
|
95
|
-
log.error(`Failed to initialize debug libraries ${result.reason}`)();
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
log.probe(LOG_LEVEL + 1, 'DOM is loaded')();
|
|
99
|
-
// @ts-expect-error
|
|
100
|
-
if (props.gl?.device) {
|
|
101
|
-
log.warn('reattaching existing device')();
|
|
102
|
-
return WebGLDevice.attach(props.gl);
|
|
103
|
-
}
|
|
104
|
-
const device = new WebGLDevice(props);
|
|
105
|
-
// Log some debug info about the newly created context
|
|
106
|
-
const message = `\
|
|
107
|
-
Created ${device.type}${device.debug ? ' debug' : ''} context: \
|
|
108
|
-
${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContext.id}`;
|
|
109
|
-
log.probe(LOG_LEVEL, message)();
|
|
110
|
-
log.table(LOG_LEVEL, device.info)();
|
|
111
|
-
log.groupEnd(LOG_LEVEL)();
|
|
112
|
-
return device;
|
|
113
|
-
}
|
|
46
|
+
/** WebGL2 context. */
|
|
47
|
+
gl;
|
|
48
|
+
debug = false;
|
|
49
|
+
/** State used by luma.gl classes: TODO - move to canvasContext*/
|
|
50
|
+
_canvasSizeInfo = { clientWidth: 0, clientHeight: 0, devicePixelRatio: 1 };
|
|
51
|
+
/** State used by luma.gl classes - TODO - not used? */
|
|
52
|
+
_extensions = {};
|
|
53
|
+
_polyfilled = false;
|
|
54
|
+
/** Instance of Spector.js (if initialized) */
|
|
55
|
+
spectorJS;
|
|
114
56
|
//
|
|
115
57
|
// Public API
|
|
116
58
|
//
|
|
117
59
|
constructor(props) {
|
|
118
|
-
super({ ...props, id: props.id || 'webgl-device' });
|
|
60
|
+
super({ ...props, id: props.id || uid('webgl-device') });
|
|
119
61
|
// If attaching to an already attached context, return the attached device
|
|
120
62
|
// @ts-expect-error device is attached to context
|
|
121
63
|
const device = props.gl?.device;
|
|
@@ -128,22 +70,25 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex
|
|
|
128
70
|
this.lost = new Promise(resolve => {
|
|
129
71
|
this._resolveContextLost = resolve;
|
|
130
72
|
});
|
|
131
|
-
|
|
132
|
-
gl ||= createBrowserContext(this.canvasContext.canvas, {
|
|
73
|
+
this.handle = createBrowserContext(this.canvasContext.canvas, {
|
|
133
74
|
...props,
|
|
134
75
|
onContextLost: (event) => this._resolveContextLost?.({
|
|
135
76
|
reason: 'destroyed',
|
|
136
77
|
message: 'Entered sleep mode, or too many apps or browser tabs are using the GPU.'
|
|
137
78
|
})
|
|
138
79
|
});
|
|
139
|
-
|
|
80
|
+
this.gl = this.handle;
|
|
81
|
+
if (!this.handle) {
|
|
140
82
|
throw new Error('WebGL context creation failed');
|
|
141
83
|
}
|
|
142
|
-
|
|
143
|
-
|
|
84
|
+
// Add spector debug instrumentation to context
|
|
85
|
+
// We need to trust spector integration to decide if spector should be initialized
|
|
86
|
+
// We also run spector instrumentation first, otherwise spector can clobber luma instrumentation.
|
|
87
|
+
this.spectorJS = initializeSpectorJS({ ...this.props, gl: this.handle });
|
|
88
|
+
// Instrument context
|
|
144
89
|
this.gl.device = this; // Update GL context: Link webgl context back to device
|
|
145
90
|
this.gl._version = 2; // Update GL context: Store WebGL version field on gl context (HACK to identify debug contexts)
|
|
146
|
-
// luma Device fields
|
|
91
|
+
// initialize luma Device fields
|
|
147
92
|
this.info = getDeviceInfo(this.gl, this._extensions);
|
|
148
93
|
this.limits = new WebGLDeviceLimits(this.gl);
|
|
149
94
|
this.features = new WebGLDeviceFeatures(this.gl, this._extensions, this.props.disabledFeatures);
|
|
@@ -152,23 +97,17 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex
|
|
|
152
97
|
}
|
|
153
98
|
this.canvasContext.resize();
|
|
154
99
|
// Install context state tracking
|
|
155
|
-
|
|
156
|
-
const { enable = true, copyState = false } = props;
|
|
157
|
-
trackContextState(this.gl, {
|
|
158
|
-
enable,
|
|
159
|
-
copyState,
|
|
100
|
+
const glState = new WebGLStateTracker(this.gl, {
|
|
160
101
|
log: (...args) => log.log(1, ...args)()
|
|
161
102
|
});
|
|
162
|
-
|
|
103
|
+
glState.trackState(this.gl, { copyState: false });
|
|
104
|
+
// DEBUG contexts: Add luma debug instrumentation to the context, force log level to at least 1
|
|
163
105
|
if (props.debug) {
|
|
164
106
|
this.gl = makeDebugContext(this.gl, { ...props, throwOnError: true });
|
|
165
107
|
this.debug = true;
|
|
166
108
|
log.level = Math.max(log.level, 1);
|
|
167
109
|
log.warn('WebGL debug mode activated. Performance reduced.')();
|
|
168
110
|
}
|
|
169
|
-
if (props.spector) {
|
|
170
|
-
this.spectorJS = initializeSpectorJS({ ...this.props, canvas: this.handle.canvas });
|
|
171
|
-
}
|
|
172
111
|
}
|
|
173
112
|
/**
|
|
174
113
|
* Destroys the context
|
|
@@ -178,9 +117,6 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex
|
|
|
178
117
|
get isLost() {
|
|
179
118
|
return this.gl.isContextLost();
|
|
180
119
|
}
|
|
181
|
-
getSize() {
|
|
182
|
-
return [this.gl.drawingBufferWidth, this.gl.drawingBufferHeight];
|
|
183
|
-
}
|
|
184
120
|
isTextureFormatSupported(format) {
|
|
185
121
|
return isTextureFormatSupported(this.gl, format, this._extensions);
|
|
186
122
|
}
|
|
@@ -279,16 +215,6 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex
|
|
|
279
215
|
//
|
|
280
216
|
// WebGL-only API (not part of `Device` API)
|
|
281
217
|
//
|
|
282
|
-
/** WebGL2 context. */
|
|
283
|
-
gl;
|
|
284
|
-
debug = false;
|
|
285
|
-
/** State used by luma.gl classes: TODO - move to canvasContext*/
|
|
286
|
-
_canvasSizeInfo = { clientWidth: 0, clientHeight: 0, devicePixelRatio: 1 };
|
|
287
|
-
/** State used by luma.gl classes - TODO - not used? */
|
|
288
|
-
_extensions = {};
|
|
289
|
-
_polyfilled = false;
|
|
290
|
-
/** Instance of Spector.js (if initialized) */
|
|
291
|
-
spectorJS;
|
|
292
218
|
/**
|
|
293
219
|
* Triggers device (or WebGL context) loss.
|
|
294
220
|
* @note primarily intended for testing how application reacts to device loss
|
|
@@ -310,11 +236,13 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex
|
|
|
310
236
|
}
|
|
311
237
|
/** Save current WebGL context state onto an internal stack */
|
|
312
238
|
pushState() {
|
|
313
|
-
|
|
239
|
+
const webglState = WebGLStateTracker.get(this.gl);
|
|
240
|
+
webglState.push();
|
|
314
241
|
}
|
|
315
242
|
/** Restores previously saved context state */
|
|
316
243
|
popState() {
|
|
317
|
-
|
|
244
|
+
const webglState = WebGLStateTracker.get(this.gl);
|
|
245
|
+
webglState.pop();
|
|
318
246
|
}
|
|
319
247
|
/**
|
|
320
248
|
* Storing data on a special field on WebGLObjects makes that data visible in SPECTOR chrome debug extension
|
|
@@ -379,14 +307,6 @@ ${device.info.vendor}, ${device.info.renderer} for canvas: ${device.canvasContex
|
|
|
379
307
|
return this._extensions;
|
|
380
308
|
}
|
|
381
309
|
}
|
|
382
|
-
/** Check if supplied parameter is a WebGL2RenderingContext */
|
|
383
|
-
function isWebGL(gl) {
|
|
384
|
-
if (typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext) {
|
|
385
|
-
return true;
|
|
386
|
-
}
|
|
387
|
-
// Look for debug contexts, headless gl etc
|
|
388
|
-
return Boolean(gl && Number.isFinite(gl._version));
|
|
389
|
-
}
|
|
390
310
|
/** Set constant float array attribute */
|
|
391
311
|
function setConstantFloatArray(device, location, array) {
|
|
392
312
|
switch (array.length) {
|