@luma.gl/webgl 9.0.0-alpha.44 → 9.0.0-alpha.46

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 (56) hide show
  1. package/dist/adapter/device-helpers/device-features.js +1 -0
  2. package/dist/adapter/device-helpers/device-features.js.map +1 -1
  3. package/dist/adapter/helpers/webgl-topology-utils.d.ts +6 -1
  4. package/dist/adapter/helpers/webgl-topology-utils.d.ts.map +1 -1
  5. package/dist/adapter/helpers/webgl-topology-utils.js +40 -0
  6. package/dist/adapter/helpers/webgl-topology-utils.js.map +1 -1
  7. package/dist/adapter/resources/webgl-buffer.d.ts +1 -1
  8. package/dist/adapter/resources/webgl-buffer.d.ts.map +1 -1
  9. package/dist/adapter/resources/webgl-buffer.js +2 -0
  10. package/dist/adapter/resources/webgl-buffer.js.map +1 -1
  11. package/dist/adapter/resources/webgl-command-buffer.d.ts.map +1 -1
  12. package/dist/adapter/resources/webgl-command-buffer.js +6 -4
  13. package/dist/adapter/resources/webgl-command-buffer.js.map +1 -1
  14. package/dist/adapter/resources/webgl-render-pipeline.d.ts +2 -0
  15. package/dist/adapter/resources/webgl-render-pipeline.d.ts.map +1 -1
  16. package/dist/adapter/resources/webgl-render-pipeline.js +4 -44
  17. package/dist/adapter/resources/webgl-render-pipeline.js.map +1 -1
  18. package/dist/adapter/resources/webgl-transform-feedback.d.ts +53 -0
  19. package/dist/adapter/resources/webgl-transform-feedback.d.ts.map +1 -0
  20. package/dist/adapter/resources/webgl-transform-feedback.js +161 -0
  21. package/dist/adapter/resources/webgl-transform-feedback.js.map +1 -0
  22. package/dist/adapter/resources/webgl-vertex-array.d.ts.map +1 -1
  23. package/dist/adapter/resources/webgl-vertex-array.js +5 -3
  24. package/dist/adapter/resources/webgl-vertex-array.js.map +1 -1
  25. package/dist/adapter/webgl-device.d.ts +4 -2
  26. package/dist/adapter/webgl-device.d.ts.map +1 -1
  27. package/dist/adapter/webgl-device.js +7 -3
  28. package/dist/adapter/webgl-device.js.map +1 -1
  29. package/dist/classic/copy-and-blit.d.ts +3 -3
  30. package/dist/classic/copy-and-blit.d.ts.map +1 -1
  31. package/dist/classic/copy-and-blit.js +12 -20
  32. package/dist/classic/copy-and-blit.js.map +1 -1
  33. package/dist/dist.dev.js +2140 -2302
  34. package/dist/index.cjs +645 -806
  35. package/dist/index.d.ts +1 -2
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +1 -1
  38. package/dist/index.js.map +1 -1
  39. package/dist.min.js +23 -22
  40. package/package.json +5 -5
  41. package/src/adapter/device-helpers/device-features.ts +1 -0
  42. package/src/adapter/helpers/webgl-topology-utils.ts +40 -0
  43. package/src/adapter/resources/webgl-buffer.ts +12 -8
  44. package/src/adapter/resources/webgl-command-buffer.ts +7 -13
  45. package/src/adapter/resources/webgl-render-pipeline.ts +11 -47
  46. package/src/adapter/resources/webgl-transform-feedback.ts +205 -0
  47. package/src/adapter/resources/webgl-vertex-array.ts +11 -8
  48. package/src/adapter/webgl-device.ts +10 -5
  49. package/src/classic/copy-and-blit.ts +16 -19
  50. package/src/index.ts +5 -4
  51. package/LICENSE +0 -34
  52. package/dist/classic/buffer-with-accessor.d.ts +0 -82
  53. package/dist/classic/buffer-with-accessor.d.ts.map +0 -1
  54. package/dist/classic/buffer-with-accessor.js +0 -314
  55. package/dist/classic/buffer-with-accessor.js.map +0 -1
  56. package/src/classic/buffer-with-accessor.ts +0 -466
@@ -1,466 +0,0 @@
1
- // luma.gl, MIT license
2
- // Copyright (c) vis.gl contributors
3
-
4
- import type {Device, BufferProps, TypedArray} from '@luma.gl/core';
5
- import {assert, checkProps} from '@luma.gl/core';
6
- import {GL} from '@luma.gl/constants';
7
- import {assertWebGL2Context} from '../context/context/webgl-checks';
8
- import {AccessorObject} from '../types';
9
- import {Accessor} from './accessor';
10
- import {getGLTypeFromTypedArray, getTypedArrayFromGLType} from './typed-array-utils';
11
-
12
- import {WebGLDevice} from '../adapter/webgl-device';
13
- import {WEBGLBuffer} from '../adapter/resources/webgl-buffer';
14
-
15
- const DEBUG_DATA_LENGTH = 10;
16
-
17
- // Shared prop checks for constructor and setProps
18
- const DEPRECATED_PROPS = {
19
- offset: 'accessor.offset',
20
- stride: 'accessor.stride',
21
- type: 'accessor.type',
22
- size: 'accessor.size',
23
- divisor: 'accessor.divisor',
24
- normalized: 'accessor.normalized',
25
- integer: 'accessor.integer',
26
- instanced: 'accessor.divisor',
27
- isInstanced: 'accessor.divisor'
28
- };
29
-
30
- // Prop checks for constructor
31
- const PROP_CHECKS_INITIALIZE = {
32
- removedProps: {},
33
- replacedProps: {
34
- bytes: 'byteLength'
35
- },
36
- // new Buffer() with individual accessor props is still used in apps, emit warnings
37
- deprecatedProps: DEPRECATED_PROPS
38
- };
39
-
40
- // Prop checks for setProps
41
- const PROP_CHECKS_SET_PROPS = {
42
- // Buffer.setProps() with individual accessor props is rare => emit errors
43
- removedProps: DEPRECATED_PROPS
44
- };
45
-
46
- function getWEBGLBufferProps(props: BufferWithAccessorProps | ArrayBufferView | number): BufferProps {
47
- // Signature `new Buffer(gl, new Float32Array(...)`
48
- if (ArrayBuffer.isView(props)) {
49
- return {data: props};
50
- }
51
-
52
- // Signature: `new Buffer(gl, 100)`
53
- else if (typeof props === 'number') {
54
- return {byteLength: props };
55
- }
56
-
57
- props = checkProps('Buffer', props, PROP_CHECKS_INITIALIZE);
58
- const bufferProps = {...props };
59
- // if (bufferProps.offset) {
60
- // bufferProps.byteOffset = bufferProps.offset;
61
- // }
62
- return bufferProps;
63
- }
64
-
65
- /** WebGL Buffer interface */
66
- export type BufferWithAccessorProps = BufferProps & {
67
- handle?: WebGLBuffer;
68
-
69
- accessor?: AccessorObject;
70
-
71
- // target?: number;
72
- // glUsage?: number;
73
- // /** @deprecated */
74
- // index?: number;
75
- // /** @deprecated */
76
- // offset?: number;
77
- // /** @deprecated */
78
- // size?: number;
79
- // /** @deprecated */
80
- // type?: number
81
- }
82
-
83
- /** WebGL Buffer interface */
84
- export class BufferWithAccessor extends WEBGLBuffer {
85
- accessor: Accessor;
86
-
87
- constructor(device: Device | WebGLRenderingContext, props?: BufferWithAccessorProps);
88
- constructor(device: Device | WebGLRenderingContext, data: ArrayBufferView | number[]);
89
- constructor(device: Device | WebGLRenderingContext, byteLength: number);
90
-
91
- constructor(device: Device | WebGLRenderingContext, props = {}) {
92
- super(WebGLDevice.attach(device), getWEBGLBufferProps(props));
93
-
94
- // Base class initializes
95
- // this.initialize(props);
96
-
97
- // Deprecated: Merge main props and accessor
98
- this.setAccessor(Object.assign({}, props, (props as BufferWithAccessorProps).accessor));
99
-
100
- // infer GL type from supplied typed array
101
- if (this.props.data) {
102
- const type = getGLTypeFromTypedArray(this.props.data as TypedArray);
103
- assert(type);
104
- this.setAccessor(new Accessor(this.accessor, {type}));
105
- }
106
-
107
- Object.seal(this);
108
- }
109
-
110
- // override write(data: TypedArray, byteOffset: number = 0): void {
111
- // this.subData({data, offset: byteOffset});
112
- // }
113
-
114
- // returns number of elements in the buffer (assuming that the full buffer is used)
115
- getElementCount(accessor: AccessorObject = this.accessor): number {
116
- return Math.round(this.byteLength / Accessor.getBytesPerElement(accessor));
117
- }
118
-
119
- // returns number of vertices in the buffer (assuming that the full buffer is used)
120
- getVertexCount(accessor: AccessorObject = this.accessor): number {
121
- return Math.round(this.byteLength / Accessor.getBytesPerVertex(accessor));
122
- }
123
-
124
- // Creates and initializes the buffer object's data store.
125
- // Signature: `new Buffer(gl, {data: new Float32Array(...)})`
126
- // Signature: `new Buffer(gl, new Float32Array(...))`
127
- // Signature: `new Buffer(gl, 100)`
128
- initialize(props: BufferWithAccessorProps = {}): this {
129
- // Signature `new Buffer(gl, new Float32Array(...)`
130
- if (ArrayBuffer.isView(props)) {
131
- props = {data: props};
132
- }
133
-
134
- // Signature: `new Buffer(gl, 100)`
135
- if (Number.isFinite(props)) {
136
- // @ts-expect-error
137
- props = {byteLength: props};
138
- }
139
-
140
- props = checkProps('Buffer', props, PROP_CHECKS_INITIALIZE);
141
-
142
- // Initialize member fields
143
- // @ts-expect-error readonly field
144
- this.glUsage = props.glUsage || GL.STATIC_DRAW;
145
- this.debugData = null;
146
-
147
- // Deprecated: Merge main props and accessor
148
- this.setAccessor(Object.assign({}, props, props.accessor));
149
-
150
- // Set data: (re)initializes the buffer
151
- if (props.data) {
152
- this._setData(props.data, props.byteOffset, props.byteLength);
153
- } else {
154
- this._setByteLength(props.byteLength || 0);
155
- }
156
-
157
- return this;
158
- }
159
-
160
- setProps(props: BufferWithAccessorProps): this {
161
- props = checkProps('Buffer', props, PROP_CHECKS_SET_PROPS);
162
-
163
- if ('accessor' in props) {
164
- this.setAccessor(props.accessor);
165
- }
166
-
167
- return this;
168
- }
169
-
170
- // Optionally stores an accessor with the buffer, makes it easier to use it as an attribute later
171
- // {type, size = 1, offset = 0, stride = 0, normalized = false, integer = false, divisor = 0}
172
- setAccessor(accessor: AccessorObject | Accessor): this {
173
- // NOTE: From luma.gl v7.0, Accessors have an optional `buffer `field
174
- // (mainly to support "interleaving")
175
- // To avoid confusion, ensure `buffer.accessor` does not have a `buffer.accessor.buffer` field:
176
- accessor = Object.assign({}, accessor);
177
- // @ts-expect-error
178
- delete accessor.buffer;
179
-
180
- // This new statement ensures that an "accessor object" is re-packaged as an Accessor instance
181
- this.accessor = new Accessor(accessor);
182
- return this;
183
- }
184
-
185
- // Allocate a bigger GPU buffer (if the current buffer is not big enough).
186
- // If a reallocation is triggered it clears the buffer
187
- // Returns:
188
- // `true`: buffer was reallocated, data was cleared
189
- // `false`: buffer was big enough, data is intact
190
- reallocate(byteLength: number): boolean {
191
- if (byteLength > this.byteLength) {
192
- this._setByteLength(byteLength);
193
- return true;
194
- }
195
- this.bytesUsed = byteLength;
196
- return false;
197
- }
198
-
199
- // Update with new data. Reinitializes the buffer
200
- setData(props: BufferWithAccessorProps) {
201
- return this.initialize(props);
202
- }
203
-
204
- // Updates a subset of a buffer object's data store.
205
- // Data (Typed Array or ArrayBuffer), length is inferred unless provided
206
- // Offset into buffer
207
- // WebGL2 only: Offset into srcData
208
- // WebGL2 only: Number of bytes to be copied
209
- subData(options: TypedArray | {data: TypedArray, offset?: number; srcOffset?: number; byteLength?: number, length?: number}) {
210
- // Signature: buffer.subData(new Float32Array([...]))
211
- if (ArrayBuffer.isView(options)) {
212
- options = {data: options};
213
- }
214
-
215
- const {data, offset = 0, srcOffset = 0} = options;
216
- const byteLength = options.byteLength || options.length;
217
-
218
- assert(data);
219
-
220
- // Create the buffer - binding it here for the first time locks the type
221
- // In WebGL2, use GL.COPY_WRITE_BUFFER to avoid locking the type
222
- // @ts-expect-error
223
- const glTarget = this.gl.webgl2 ? GL.COPY_WRITE_BUFFER : this.glTarget;
224
- this.gl.bindBuffer(glTarget, this.handle);
225
- // WebGL2: subData supports additional srcOffset and length parameters
226
- if (srcOffset !== 0 || byteLength !== undefined) {
227
- assertWebGL2Context(this.gl);
228
- // @ts-expect-error
229
- this.gl.bufferSubData(this.glTarget, offset, data, srcOffset, byteLength);
230
- } else {
231
- this.gl.bufferSubData(glTarget, offset, data);
232
- }
233
- this.gl.bindBuffer(glTarget, null);
234
-
235
- // TODO - update local `data` if offsets are right
236
- this.debugData = null;
237
-
238
- this._inferType(data);
239
-
240
- return this;
241
- }
242
-
243
- /**
244
- * Copies part of the data of another buffer into this buffer
245
- * @note WEBGL2 ONLY
246
- */
247
- copyData(options: {
248
- sourceBuffer: any;
249
- readOffset?: number;
250
- writeOffset?: number;
251
- size: any;
252
- }): this {
253
- const {sourceBuffer, readOffset = 0, writeOffset = 0, size} = options;
254
- const {gl, gl2} = this;
255
- assertWebGL2Context(gl);
256
-
257
- // Use GL.COPY_READ_BUFFER+GL.COPY_WRITE_BUFFER avoid disturbing other targets and locking type
258
- gl.bindBuffer(GL.COPY_READ_BUFFER, sourceBuffer.handle);
259
- gl.bindBuffer(GL.COPY_WRITE_BUFFER, this.handle);
260
- gl2?.copyBufferSubData(GL.COPY_READ_BUFFER, GL.COPY_WRITE_BUFFER, readOffset, writeOffset, size);
261
- gl.bindBuffer(GL.COPY_READ_BUFFER, null);
262
- gl.bindBuffer(GL.COPY_WRITE_BUFFER, null);
263
-
264
- // TODO - update local `data` if offsets are 0
265
- this.debugData = null;
266
-
267
- return this;
268
- }
269
-
270
- /**
271
- * Reads data from buffer into an ArrayBufferView or SharedArrayBuffer.
272
- * @note WEBGL2 ONLY
273
- */
274
- override getData(options?: {
275
- dstData?: any;
276
- srcByteOffset?: number;
277
- dstOffset?: number;
278
- length?: number;
279
- }): any {
280
- let {dstData = null, length = 0} = options || {};
281
- const {srcByteOffset = 0, dstOffset = 0} = options || {};
282
- assertWebGL2Context(this.gl);
283
-
284
- const ArrayType = getTypedArrayFromGLType(this.accessor.type || GL.FLOAT, {clamped: false});
285
- const sourceAvailableElementCount = this._getAvailableElementCount(srcByteOffset);
286
-
287
- const dstElementOffset = dstOffset;
288
-
289
- let dstAvailableElementCount;
290
- let dstElementCount;
291
- if (dstData) {
292
- dstElementCount = dstData.length;
293
- dstAvailableElementCount = dstElementCount - dstElementOffset;
294
- } else {
295
- // Allocate ArrayBufferView with enough size to copy all eligible data.
296
- dstAvailableElementCount = Math.min(
297
- sourceAvailableElementCount,
298
- length || sourceAvailableElementCount
299
- );
300
- dstElementCount = dstElementOffset + dstAvailableElementCount;
301
- }
302
-
303
- const copyElementCount = Math.min(sourceAvailableElementCount, dstAvailableElementCount);
304
- length = length || copyElementCount;
305
- assert(length <= copyElementCount);
306
- dstData = dstData || new ArrayType(dstElementCount);
307
-
308
- // Use GL.COPY_READ_BUFFER to avoid disturbing other targets and locking type
309
- this.gl.bindBuffer(GL.COPY_READ_BUFFER, this.handle);
310
- this.gl2?.getBufferSubData(GL.COPY_READ_BUFFER, srcByteOffset, dstData, dstOffset, length);
311
- this.gl.bindBuffer(GL.COPY_READ_BUFFER, null);
312
-
313
- // TODO - update local `data` if offsets are 0
314
- return dstData;
315
- }
316
-
317
- /**
318
- * Binds a buffer to a given binding point (target).
319
- * GL.TRANSFORM_FEEDBACK_BUFFER and GL.UNIFORM_BUFFER take an index, and optionally a range.
320
- * - GL.TRANSFORM_FEEDBACK_BUFFER and GL.UNIFORM_BUFFER need an index to affect state
321
- * - GL.UNIFORM_BUFFER: `offset` must be aligned to GL.UNIFORM_BUFFER_OFFSET_ALIGNMENT.
322
- * - GL.UNIFORM_BUFFER: `size` must be a minimum of GL.UNIFORM_BLOCK_SIZE_DATA.
323
- */
324
- bind(options?: {glTarget?: number; index?: any; offset?: number; size?: any}): this {
325
- const {
326
- glTarget = this.glTarget, // target for the bind operation
327
- index = this.accessor && this.accessor.index, // index = index of target (indexed bind point)
328
- offset = 0,
329
- size
330
- } = options || {};
331
- // NOTE: While GL.TRANSFORM_FEEDBACK_BUFFER and GL.UNIFORM_BUFFER could
332
- // be used as direct binding points, they will not affect transform feedback or
333
- // uniform buffer state. Instead indexed bindings need to be made.
334
- if (glTarget === GL.UNIFORM_BUFFER || glTarget === GL.TRANSFORM_FEEDBACK_BUFFER) {
335
- if (size !== undefined) {
336
- this.gl2?.bindBufferRange(glTarget, index, this.handle, offset, size);
337
- } else {
338
- assert(offset === 0); // Make sure offset wasn't supplied
339
- this.gl2?.bindBufferBase(glTarget, index, this.handle);
340
- }
341
- } else {
342
- this.gl.bindBuffer(glTarget, this.handle);
343
- }
344
-
345
- return this;
346
- }
347
-
348
- unbind(options?: {glTarget?: any; index?: any}): this {
349
- const {glTarget = this.glTarget, index = this.accessor && this.accessor.index} = options || {};
350
- const isIndexedBuffer = glTarget === GL.UNIFORM_BUFFER || glTarget === GL.TRANSFORM_FEEDBACK_BUFFER;
351
- if (isIndexedBuffer) {
352
- this.gl2?.bindBufferBase(glTarget, index, null);
353
- } else {
354
- this.gl.bindBuffer(glTarget, null);
355
- }
356
- return this;
357
- }
358
-
359
- // PROTECTED METHODS (INTENDED FOR USE BY OTHER FRAMEWORK CODE ONLY)
360
-
361
- // Returns a short initial data array
362
- getDebugData(): {
363
- data: any;
364
- changed: boolean;
365
- } {
366
- if (!this.debugData) {
367
- this.debugData = this.getData({length: Math.min(DEBUG_DATA_LENGTH, this.byteLength)});
368
- return {data: this.debugData, changed: true};
369
- }
370
- return {data: this.debugData, changed: false};
371
- }
372
-
373
- invalidateDebugData() {
374
- this.debugData = null;
375
- }
376
-
377
- // PRIVATE METHODS
378
-
379
- // Allocate a new buffer and initialize to contents of typed array
380
- _setData(data, offset: number = 0, byteLength: number = data.byteLength + offset): this {
381
- assert(ArrayBuffer.isView(data));
382
-
383
- this.trackDeallocatedMemory();
384
-
385
- const target = this._getTarget();
386
- this.gl.bindBuffer(target, this.handle);
387
- this.gl.bufferData(target, byteLength, this.glUsage);
388
- this.gl.bufferSubData(target, offset, data);
389
- this.gl.bindBuffer(target, null);
390
-
391
- this.debugData = data.slice(0, DEBUG_DATA_LENGTH);
392
- this.bytesUsed = byteLength;
393
- this.byteLength = byteLength;
394
- this.trackAllocatedMemory(byteLength);
395
-
396
- // infer GL type from supplied typed array
397
- const type = getGLTypeFromTypedArray(data);
398
- assert(type);
399
- this.setAccessor(new Accessor(this.accessor, {type}));
400
- return this;
401
- }
402
-
403
- // Allocate a GPU buffer of specified size.
404
- _setByteLength(byteLength: number): this {
405
- assert(byteLength >= 0);
406
-
407
- this.trackDeallocatedMemory();
408
-
409
- // Workaround needed for Safari (#291):
410
- // gl.bufferData with size equal to 0 crashes. Instead create zero sized array.
411
- let data = byteLength;
412
- if (byteLength === 0) {
413
- // @ts-expect-error
414
- data = new Float32Array(0);
415
- }
416
-
417
- const glTarget = this._getTarget();
418
- this.gl.bindBuffer(glTarget, this.handle);
419
- this.gl.bufferData(glTarget, data, this.glUsage);
420
- this.gl.bindBuffer(glTarget, null);
421
-
422
- this.debugData = null;
423
- this.bytesUsed = byteLength;
424
- this.byteLength = byteLength;
425
-
426
- this.trackAllocatedMemory(byteLength);
427
-
428
- return this;
429
- }
430
-
431
- // Binding a buffer for the first time locks the type
432
- // In WebGL2, use GL.COPY_WRITE_BUFFER to avoid locking the type
433
- _getTarget() {
434
- // @ts-expect-error
435
- return this.gl.webgl2 ? GL.COPY_WRITE_BUFFER : this.glTarget;
436
- }
437
-
438
- _getAvailableElementCount(srcByteOffset: number) {
439
- const ArrayType = getTypedArrayFromGLType(this.accessor.type || GL.FLOAT, {clamped: false});
440
- const sourceElementOffset = srcByteOffset / ArrayType.BYTES_PER_ELEMENT;
441
- return this.getElementCount() - sourceElementOffset;
442
- }
443
-
444
- // Automatically infers type from typed array passed to setData
445
- // Note: No longer that useful, since type is now autodeduced from the compiled shaders
446
- _inferType(data) {
447
- if (!this.accessor.type) {
448
- this.setAccessor(new Accessor(this.accessor, {type: getGLTypeFromTypedArray(data)}));
449
- }
450
- }
451
-
452
- // RESOURCE METHODS
453
-
454
- getParameter(pname: GL): any {
455
- this.gl.bindBuffer(this.glTarget, this.handle);
456
- const value = this.gl.getBufferParameter(this.glTarget, pname);
457
- this.gl.bindBuffer(this.glTarget, null);
458
- return value;
459
- }
460
-
461
- // DEPRECATIONS - v7.0
462
- /** @deprecated Use Buffer.accessor.type */
463
- get type() {
464
- return this.accessor.type;
465
- }
466
- }