@luma.gl/engine 9.3.6 → 9.4.0-alpha.1

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 (181) hide show
  1. package/dist/animation/animator.d.ts +42 -0
  2. package/dist/animation/animator.d.ts.map +1 -0
  3. package/dist/animation/animator.js +53 -0
  4. package/dist/animation/animator.js.map +1 -0
  5. package/dist/animation-loop/animation-loop.d.ts +14 -2
  6. package/dist/animation-loop/animation-loop.d.ts.map +1 -1
  7. package/dist/animation-loop/animation-loop.js +40 -22
  8. package/dist/animation-loop/animation-loop.js.map +1 -1
  9. package/dist/animation-loop/animation-props.d.ts +2 -0
  10. package/dist/animation-loop/animation-props.d.ts.map +1 -1
  11. package/dist/animation-loop/make-animation-loop.d.ts +1 -0
  12. package/dist/animation-loop/make-animation-loop.d.ts.map +1 -1
  13. package/dist/animation-loop/make-animation-loop.js +3 -0
  14. package/dist/animation-loop/make-animation-loop.js.map +1 -1
  15. package/dist/application-utils/load-file.d.ts +2 -0
  16. package/dist/application-utils/load-file.d.ts.map +1 -1
  17. package/dist/application-utils/load-file.js +31 -5
  18. package/dist/application-utils/load-file.js.map +1 -1
  19. package/dist/compute/buffer-transform.d.ts +5 -1
  20. package/dist/compute/buffer-transform.d.ts.map +1 -1
  21. package/dist/compute/buffer-transform.js +7 -3
  22. package/dist/compute/buffer-transform.js.map +1 -1
  23. package/dist/compute/computation.d.ts +20 -8
  24. package/dist/compute/computation.d.ts.map +1 -1
  25. package/dist/compute/computation.js +34 -13
  26. package/dist/compute/computation.js.map +1 -1
  27. package/dist/compute/swap.d.ts.map +1 -1
  28. package/dist/compute/swap.js +17 -0
  29. package/dist/compute/swap.js.map +1 -1
  30. package/dist/dist.dev.js +4100 -1979
  31. package/dist/dist.min.js +288 -42
  32. package/dist/dynamic-buffer/dynamic-buffer.d.ts +171 -0
  33. package/dist/dynamic-buffer/dynamic-buffer.d.ts.map +1 -0
  34. package/dist/dynamic-buffer/dynamic-buffer.js +316 -0
  35. package/dist/dynamic-buffer/dynamic-buffer.js.map +1 -0
  36. package/dist/dynamic-texture/dynamic-texture.d.ts +14 -1
  37. package/dist/dynamic-texture/dynamic-texture.d.ts.map +1 -1
  38. package/dist/dynamic-texture/dynamic-texture.js +31 -2
  39. package/dist/dynamic-texture/dynamic-texture.js.map +1 -1
  40. package/dist/dynamic-texture/texture-binding-source.d.ts +55 -0
  41. package/dist/dynamic-texture/texture-binding-source.d.ts.map +1 -0
  42. package/dist/dynamic-texture/texture-binding-source.js +55 -0
  43. package/dist/dynamic-texture/texture-binding-source.js.map +1 -0
  44. package/dist/dynamic-texture/texture-data.d.ts +5 -3
  45. package/dist/dynamic-texture/texture-data.d.ts.map +1 -1
  46. package/dist/dynamic-texture/texture-data.js +5 -0
  47. package/dist/dynamic-texture/texture-data.js.map +1 -1
  48. package/dist/dynamic-texture/video-texture.d.ts +101 -0
  49. package/dist/dynamic-texture/video-texture.d.ts.map +1 -0
  50. package/dist/dynamic-texture/video-texture.js +272 -0
  51. package/dist/dynamic-texture/video-texture.js.map +1 -0
  52. package/dist/geometries/cube-geometry.d.ts +1 -0
  53. package/dist/geometries/cube-geometry.d.ts.map +1 -1
  54. package/dist/geometries/cube-geometry.js +22 -2
  55. package/dist/geometries/cube-geometry.js.map +1 -1
  56. package/dist/geometries/plane-geometry.d.ts +10 -0
  57. package/dist/geometries/plane-geometry.d.ts.map +1 -1
  58. package/dist/geometries/plane-geometry.js.map +1 -1
  59. package/dist/geometry/geometry-utils.d.ts +36 -1
  60. package/dist/geometry/geometry-utils.d.ts.map +1 -1
  61. package/dist/geometry/geometry-utils.js +137 -2
  62. package/dist/geometry/geometry-utils.js.map +1 -1
  63. package/dist/geometry/geometry.d.ts +52 -33
  64. package/dist/geometry/geometry.d.ts.map +1 -1
  65. package/dist/geometry/geometry.js +70 -19
  66. package/dist/geometry/geometry.js.map +1 -1
  67. package/dist/geometry/gpu-geometry.d.ts +37 -4
  68. package/dist/geometry/gpu-geometry.d.ts.map +1 -1
  69. package/dist/geometry/gpu-geometry.js +44 -36
  70. package/dist/geometry/gpu-geometry.js.map +1 -1
  71. package/dist/index.cjs +3990 -1961
  72. package/dist/index.cjs.map +4 -4
  73. package/dist/index.d.ts +20 -9
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +8 -5
  76. package/dist/index.js.map +1 -1
  77. package/dist/material/material-factory.d.ts +2 -1
  78. package/dist/material/material-factory.d.ts.map +1 -1
  79. package/dist/material/material-factory.js.map +1 -1
  80. package/dist/material/material.d.ts +29 -10
  81. package/dist/material/material.d.ts.map +1 -1
  82. package/dist/material/material.js +84 -21
  83. package/dist/material/material.js.map +1 -1
  84. package/dist/model/model.d.ts +81 -23
  85. package/dist/model/model.d.ts.map +1 -1
  86. package/dist/model/model.js +312 -78
  87. package/dist/model/model.js.map +1 -1
  88. package/dist/model/split-uniforms-and-bindings.d.ts +5 -2
  89. package/dist/model/split-uniforms-and-bindings.d.ts.map +1 -1
  90. package/dist/model/split-uniforms-and-bindings.js.map +1 -1
  91. package/dist/models/billboard-texture-model.d.ts +5 -2
  92. package/dist/models/billboard-texture-model.d.ts.map +1 -1
  93. package/dist/models/billboard-texture-model.js +28 -10
  94. package/dist/models/billboard-texture-model.js.map +1 -1
  95. package/dist/modules/picking/index-color-picking.d.ts +24 -0
  96. package/dist/modules/picking/index-color-picking.d.ts.map +1 -0
  97. package/dist/modules/picking/index-color-picking.js +205 -0
  98. package/dist/modules/picking/index-color-picking.js.map +1 -0
  99. package/dist/modules/picking/index-picking.d.ts +3 -3
  100. package/dist/modules/picking/index-picking.d.ts.map +1 -1
  101. package/dist/modules/picking/index-picking.js +23 -6
  102. package/dist/modules/picking/index-picking.js.map +1 -1
  103. package/dist/modules/picking/picking-manager.d.ts +21 -0
  104. package/dist/modules/picking/picking-manager.d.ts.map +1 -1
  105. package/dist/modules/picking/picking-manager.js +100 -0
  106. package/dist/modules/picking/picking-manager.js.map +1 -1
  107. package/dist/modules/picking/picking-uniforms.d.ts +4 -4
  108. package/dist/modules/picking/picking-uniforms.d.ts.map +1 -1
  109. package/dist/modules/picking/picking-uniforms.js +3 -0
  110. package/dist/modules/picking/picking-uniforms.js.map +1 -1
  111. package/dist/modules/picking/picking.d.ts +5 -1
  112. package/dist/modules/picking/picking.d.ts.map +1 -1
  113. package/dist/modules/picking/picking.js +6 -0
  114. package/dist/modules/picking/picking.js.map +1 -1
  115. package/dist/passes/get-fragment-shader.js +4 -4
  116. package/dist/passes/shader-pass-renderer.d.ts +92 -25
  117. package/dist/passes/shader-pass-renderer.d.ts.map +1 -1
  118. package/dist/passes/shader-pass-renderer.js +466 -72
  119. package/dist/passes/shader-pass-renderer.js.map +1 -1
  120. package/dist/passes/texture-transform-module.d.ts +10 -0
  121. package/dist/passes/texture-transform-module.d.ts.map +1 -0
  122. package/dist/passes/texture-transform-module.js +50 -0
  123. package/dist/passes/texture-transform-module.js.map +1 -0
  124. package/dist/shader-inputs.d.ts +33 -16
  125. package/dist/shader-inputs.d.ts.map +1 -1
  126. package/dist/shader-inputs.js +42 -12
  127. package/dist/shader-inputs.js.map +1 -1
  128. package/dist/utils/buffer-layout-helper.d.ts +0 -1
  129. package/dist/utils/buffer-layout-helper.d.ts.map +1 -1
  130. package/dist/utils/buffer-layout-helper.js +2 -11
  131. package/dist/utils/buffer-layout-helper.js.map +1 -1
  132. package/dist/utils/buffer-layout-order.d.ts.map +1 -1
  133. package/dist/utils/buffer-layout-order.js +4 -15
  134. package/dist/utils/buffer-layout-order.js.map +1 -1
  135. package/dist/utils/shader-module-utils.d.ts +2 -0
  136. package/dist/utils/shader-module-utils.d.ts.map +1 -1
  137. package/dist/utils/shader-module-utils.js +64 -3
  138. package/dist/utils/shader-module-utils.js.map +1 -1
  139. package/package.json +2 -2
  140. package/src/animation/animator.ts +79 -0
  141. package/src/animation-loop/animation-loop.ts +56 -22
  142. package/src/animation-loop/animation-props.ts +2 -0
  143. package/src/animation-loop/make-animation-loop.ts +4 -0
  144. package/src/application-utils/load-file.ts +38 -5
  145. package/src/compute/buffer-transform.ts +14 -5
  146. package/src/compute/computation.ts +53 -21
  147. package/src/compute/swap.ts +19 -0
  148. package/src/dynamic-buffer/dynamic-buffer.ts +453 -0
  149. package/src/dynamic-texture/dynamic-texture.ts +36 -3
  150. package/src/dynamic-texture/texture-binding-source.ts +94 -0
  151. package/src/dynamic-texture/texture-data.ts +12 -4
  152. package/src/dynamic-texture/video-texture.ts +334 -0
  153. package/src/geometries/cube-geometry.ts +24 -2
  154. package/src/geometries/plane-geometry.ts +10 -0
  155. package/src/geometry/geometry-utils.ts +209 -4
  156. package/src/geometry/geometry.ts +113 -36
  157. package/src/geometry/gpu-geometry.ts +61 -41
  158. package/src/index.ts +50 -14
  159. package/src/material/material-factory.ts +5 -1
  160. package/src/material/material.ts +123 -27
  161. package/src/model/model.ts +441 -102
  162. package/src/model/split-uniforms-and-bindings.ts +7 -3
  163. package/src/models/billboard-texture-model.ts +33 -12
  164. package/src/modules/picking/index-color-picking.ts +212 -0
  165. package/src/modules/picking/index-picking.ts +23 -6
  166. package/src/modules/picking/picking-manager.ts +141 -0
  167. package/src/modules/picking/picking-uniforms.ts +7 -4
  168. package/src/modules/picking/picking.ts +8 -0
  169. package/src/passes/get-fragment-shader.ts +4 -4
  170. package/src/passes/shader-pass-renderer.ts +691 -89
  171. package/src/passes/texture-transform-module.ts +52 -0
  172. package/src/shader-inputs.ts +65 -17
  173. package/src/utils/buffer-layout-helper.ts +2 -14
  174. package/src/utils/buffer-layout-order.ts +14 -24
  175. package/src/utils/shader-module-utils.ts +109 -3
  176. package/dist/geometry/gpu-table.d.ts +0 -1
  177. package/dist/geometry/gpu-table.d.ts.map +0 -1
  178. package/dist/geometry/gpu-table.js +0 -46
  179. package/dist/geometry/gpu-table.js.map +0 -1
  180. package/src/dynamic-texture/texture-data.ts.disabled +0 -296
  181. package/src/geometry/gpu-table.ts +0 -45
@@ -0,0 +1,453 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {BufferMapCallback, BufferProps, Device, Binding as CoreBinding} from '@luma.gl/core';
6
+ import {Buffer} from '@luma.gl/core';
7
+ import {uid} from '../utils/uid';
8
+
9
+ /** Controls whether a {@link DynamicBuffer} keeps a CPU-side debug mirror of recent writes. */
10
+ export type DynamicBufferDebugProps =
11
+ | boolean
12
+ | {
13
+ /** Maximum number of bytes retained in {@link DynamicBuffer.debugData}. */
14
+ maxByteLength?: number;
15
+ };
16
+
17
+ /** Construction props for a {@link DynamicBuffer}. */
18
+ export type DynamicBufferProps = Omit<BufferProps, 'handle' | 'onMapped'> & {
19
+ /** Enables and optionally sizes the CPU-side debug mirror. */
20
+ debugData?: DynamicBufferDebugProps;
21
+ /** Existing immutable buffer to expose through this dynamic wrapper without copying. */
22
+ buffer?: Buffer;
23
+ /** Whether this dynamic wrapper should destroy an adopted `buffer`. Defaults to `true`. */
24
+ ownsBuffer?: boolean;
25
+ };
26
+
27
+ /** Buffer-like source accepted by dynamic buffer range helpers. */
28
+ export type BufferBindingSource = Buffer | DynamicBuffer;
29
+
30
+ /** Binding range that may point at either a {@link Buffer} or a {@link DynamicBuffer}. */
31
+ export type DynamicBufferRange = {
32
+ /** Buffer source for the binding range. */
33
+ buffer: BufferBindingSource;
34
+ /** Byte offset into the current backing buffer. */
35
+ offset?: number;
36
+ /** Byte length of the binding range. */
37
+ size?: number;
38
+ };
39
+
40
+ /** Generic buffer range binding accepted by engine binding resolution helpers. */
41
+ export type BufferRangeBinding = {
42
+ /** Buffer source for the binding range. */
43
+ buffer: BufferBindingSource;
44
+ /** Byte offset into the current backing buffer. */
45
+ offset?: number;
46
+ /** Byte length of the binding range. */
47
+ size?: number;
48
+ };
49
+
50
+ const DEFAULT_MAX_DEBUG_DATA_BYTE_LENGTH = Buffer.DEBUG_DATA_MAX_LENGTH;
51
+
52
+ /**
53
+ * Mutable engine-level wrapper around an immutable core {@link Buffer}.
54
+ *
55
+ * `DynamicBuffer` keeps a stable application object while allowing the backing
56
+ * GPU buffer to be replaced on resize. Engine classes such as {@link Model} and
57
+ * {@link Material} resolve it to the current backing buffer at draw time and
58
+ * invalidate cached bindings when {@link generation} changes.
59
+ */
60
+ export class DynamicBuffer {
61
+ /** Device that owns the backing buffer. */
62
+ readonly device: Device;
63
+ /** Application-provided or generated identifier. */
64
+ readonly id: string;
65
+ /** Ready promise provided for compatibility with other dynamic resources. */
66
+ readonly ready: Promise<Buffer>;
67
+ /** Usage flags applied to every backing buffer created by this wrapper. */
68
+ readonly usage: number;
69
+ /** Normalized buffer props reused when the backing buffer is recreated. */
70
+ readonly props: Readonly<DynamicBufferProps>;
71
+
72
+ /** Dynamic buffers are synchronously ready after construction. */
73
+ isReady = true;
74
+ /** Whether {@link destroy} has been called. */
75
+ destroyed = false;
76
+ /** Monotonic version that increments whenever the backing buffer is replaced. */
77
+ generation = 0;
78
+ /** Last update timestamp for writes, reads that populate debug data, or resize operations. */
79
+ updateTimestamp: number;
80
+ /** Optional CPU-side mirror of recent writes and readbacks for debugging. */
81
+ debugData: ArrayBuffer = new ArrayBuffer(0);
82
+
83
+ private readonly _debugDataEnabled: boolean;
84
+ private readonly _maxDebugDataByteLength: number;
85
+ private _ownsBuffer: boolean;
86
+ private _buffer: Buffer;
87
+
88
+ /** Current immutable core buffer backing this dynamic wrapper. */
89
+ get buffer(): Buffer {
90
+ return this._buffer;
91
+ }
92
+
93
+ /** Current byte length of the backing buffer. */
94
+ get byteLength(): number {
95
+ return this._buffer.byteLength;
96
+ }
97
+
98
+ /** String tag used by `Object.prototype.toString`. */
99
+ get [Symbol.toStringTag](): string {
100
+ return 'DynamicBuffer';
101
+ }
102
+
103
+ /** Human-readable debug string. */
104
+ toString(): string {
105
+ return `DynamicBuffer:"${this.id}":${this.byteLength}B`;
106
+ }
107
+
108
+ /** Compact serialization for assertion diffs and structured debug logs. */
109
+ toJSON(): string {
110
+ return this.toString();
111
+ }
112
+
113
+ /** Creates a dynamic buffer and its initial backing {@link Buffer}. */
114
+ constructor(device: Device, props: DynamicBufferProps) {
115
+ const {
116
+ debugData: debugDataProps = false,
117
+ buffer: adoptedBuffer,
118
+ ownsBuffer = true,
119
+ ...bufferProps
120
+ } = props;
121
+ if (adoptedBuffer && adoptedBuffer.device !== device) {
122
+ throw new Error('DynamicBuffer adopted buffers must belong to the supplied device');
123
+ }
124
+ if (adoptedBuffer && (bufferProps.byteLength !== undefined || bufferProps.data !== undefined)) {
125
+ throw new Error('DynamicBuffer cannot combine an adopted buffer with byteLength or data');
126
+ }
127
+
128
+ const id = props.id || adoptedBuffer?.id || uid('dynamic-buffer');
129
+ const normalizedBufferProps: DynamicBufferProps = {
130
+ ...bufferProps,
131
+ id,
132
+ usage: bufferProps.usage ?? adoptedBuffer?.usage,
133
+ indexType: bufferProps.indexType ?? adoptedBuffer?.indexType
134
+ };
135
+
136
+ if ((normalizedBufferProps.usage || 0) & Buffer.INDEX && !normalizedBufferProps.indexType) {
137
+ if (bufferProps.data instanceof Uint32Array) {
138
+ normalizedBufferProps.indexType = 'uint32';
139
+ } else if (bufferProps.data instanceof Uint16Array) {
140
+ normalizedBufferProps.indexType = 'uint16';
141
+ } else if (bufferProps.data instanceof Uint8Array) {
142
+ normalizedBufferProps.indexType = 'uint8';
143
+ }
144
+ }
145
+
146
+ delete normalizedBufferProps.data;
147
+ delete normalizedBufferProps.byteOffset;
148
+
149
+ this.device = device;
150
+ this.id = id;
151
+ this.props = normalizedBufferProps;
152
+ this.usage = normalizedBufferProps.usage || 0;
153
+ this._debugDataEnabled = Boolean(debugDataProps);
154
+ this._maxDebugDataByteLength =
155
+ typeof debugDataProps === 'object' && debugDataProps.maxByteLength !== undefined
156
+ ? debugDataProps.maxByteLength
157
+ : DEFAULT_MAX_DEBUG_DATA_BYTE_LENGTH;
158
+ this._ownsBuffer = ownsBuffer;
159
+
160
+ this._buffer = adoptedBuffer ?? this.device.createBuffer({...bufferProps, id});
161
+ this.ready = Promise.resolve(this._buffer);
162
+ this.updateTimestamp = this._buffer.updateTimestamp;
163
+
164
+ this._resetDebugData(this._buffer.byteLength);
165
+ if (bufferProps.data) {
166
+ this._writeDebugData(bufferProps.data, bufferProps.byteOffset || 0);
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Writes bytes into the current backing buffer.
172
+ *
173
+ * @param data - Bytes or typed-array view to upload.
174
+ * @param byteOffset - Destination byte offset in the backing buffer.
175
+ */
176
+ write(data: ArrayBuffer | SharedArrayBuffer | ArrayBufferView, byteOffset: number = 0): void {
177
+ this._buffer.write(data, byteOffset);
178
+ this._touch();
179
+ this._writeDebugData(data, byteOffset);
180
+ }
181
+
182
+ /**
183
+ * Maps the current backing buffer for writing and mirrors the written range when debug data is enabled.
184
+ *
185
+ * @param callback - Callback invoked with the mapped range.
186
+ * @param byteOffset - Byte offset of the mapped range.
187
+ * @param byteLength - Byte length of the mapped range.
188
+ */
189
+ async mapAndWriteAsync(
190
+ callback: BufferMapCallback<void | Promise<void>>,
191
+ byteOffset: number = 0,
192
+ byteLength: number = this.byteLength - byteOffset
193
+ ): Promise<void> {
194
+ let copiedBytes: Uint8Array | null = null;
195
+ await this._buffer.mapAndWriteAsync(
196
+ async (arrayBuffer, lifetime) => {
197
+ await callback(arrayBuffer, lifetime);
198
+ copiedBytes = new Uint8Array(arrayBuffer.slice(0, byteLength));
199
+ },
200
+ byteOffset,
201
+ byteLength
202
+ );
203
+ this._touch();
204
+ if (copiedBytes) {
205
+ this._writeDebugData(copiedBytes, byteOffset);
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Reads bytes from the current backing buffer.
211
+ *
212
+ * @param byteOffset - Source byte offset in the backing buffer.
213
+ * @param byteLength - Number of bytes to read.
214
+ */
215
+ async readAsync(
216
+ byteOffset: number = 0,
217
+ byteLength = this.byteLength - byteOffset
218
+ ): Promise<Uint8Array> {
219
+ const data = await this._buffer.readAsync(byteOffset, byteLength);
220
+ if (this._writeDebugData(data, byteOffset)) {
221
+ this._touch();
222
+ }
223
+ return data;
224
+ }
225
+
226
+ /**
227
+ * Maps the current backing buffer for reading.
228
+ *
229
+ * @param callback - Callback invoked with the mapped range.
230
+ * @param byteOffset - Byte offset of the mapped range.
231
+ * @param byteLength - Byte length of the mapped range.
232
+ */
233
+ async mapAndReadAsync<T>(
234
+ callback: BufferMapCallback<T>,
235
+ byteOffset: number = 0,
236
+ byteLength: number = this.byteLength - byteOffset
237
+ ): Promise<T> {
238
+ let copiedBytes: Uint8Array | null = null;
239
+ const result = await this._buffer.mapAndReadAsync(
240
+ async (arrayBuffer, lifetime) => {
241
+ copiedBytes = new Uint8Array(arrayBuffer.slice(0));
242
+ return await callback(arrayBuffer, lifetime);
243
+ },
244
+ byteOffset,
245
+ byteLength
246
+ );
247
+ if (copiedBytes && this._writeDebugData(copiedBytes, byteOffset)) {
248
+ this._touch();
249
+ }
250
+ return result;
251
+ }
252
+
253
+ /**
254
+ * Replaces the backing buffer with a new size.
255
+ *
256
+ * @param options.byteLength - New backing buffer byte length.
257
+ * @param options.preserveData - Copies bytes from the old buffer into the new buffer.
258
+ * @param options.copyByteLength - Maximum number of bytes to copy when preserving data.
259
+ * @returns `true` when a new backing buffer was created.
260
+ */
261
+ resize(options: {byteLength: number; preserveData?: boolean; copyByteLength?: number}): boolean {
262
+ const {byteLength, preserveData = false} = options;
263
+ if (byteLength === this.byteLength) {
264
+ return false;
265
+ }
266
+
267
+ const copyByteLength = Math.min(
268
+ options.copyByteLength ?? Math.min(this.byteLength, byteLength),
269
+ this.byteLength,
270
+ byteLength
271
+ );
272
+
273
+ const previousBuffer = this._buffer;
274
+ const previousDebugData = this.debugData.slice(0);
275
+ const {
276
+ data: _initialData,
277
+ byteOffset: _initialByteOffset,
278
+ ...resizableBufferProps
279
+ } = this.props;
280
+ const nextBuffer = this.device.createBuffer({
281
+ ...resizableBufferProps,
282
+ byteLength
283
+ });
284
+
285
+ if (preserveData && copyByteLength > 0) {
286
+ this._copyBufferContents(previousBuffer, nextBuffer, copyByteLength);
287
+ }
288
+
289
+ this._buffer = nextBuffer;
290
+ this._resetDebugData(byteLength);
291
+ if (preserveData && previousDebugData.byteLength > 0) {
292
+ this._writeDebugData(previousDebugData, 0);
293
+ }
294
+
295
+ if (this._ownsBuffer) {
296
+ previousBuffer.destroy();
297
+ }
298
+ this._ownsBuffer = true;
299
+ this.generation++;
300
+ this._touch();
301
+ return true;
302
+ }
303
+
304
+ /**
305
+ * Grows the backing buffer when the requested size exceeds the current size.
306
+ *
307
+ * @param byteLength - Minimum required byte length.
308
+ * @param options.preserveData - Copies existing bytes when growth is required.
309
+ * @returns `true` when the backing buffer grew.
310
+ */
311
+ ensureSize(byteLength: number, options?: {preserveData?: boolean}): boolean {
312
+ if (byteLength <= this.byteLength) {
313
+ return false;
314
+ }
315
+
316
+ return this.resize({
317
+ byteLength,
318
+ preserveData: options?.preserveData
319
+ });
320
+ }
321
+
322
+ /**
323
+ * Returns the current backing buffer or a range binding over it.
324
+ *
325
+ * @param range - Optional byte range for uniform/storage buffer bindings.
326
+ */
327
+ getBinding(range?: {offset?: number; size?: number}): CoreBinding {
328
+ if (range?.offset === undefined && range?.size === undefined) {
329
+ return this._buffer;
330
+ }
331
+
332
+ return {
333
+ buffer: this._buffer,
334
+ offset: range?.offset,
335
+ size: range?.size
336
+ };
337
+ }
338
+
339
+ /** Destroys the current backing buffer and clears debug data. */
340
+ destroy(): void {
341
+ if (!this.destroyed) {
342
+ if (this._ownsBuffer) {
343
+ this._buffer.destroy();
344
+ }
345
+ this.destroyed = true;
346
+ this.debugData = new ArrayBuffer(0);
347
+ }
348
+ }
349
+
350
+ private _copyBufferContents(
351
+ sourceBuffer: Buffer,
352
+ destinationBuffer: Buffer,
353
+ byteLength: number
354
+ ): void {
355
+ const copyByteLength =
356
+ this.device.type === 'webgpu' ? Math.ceil(byteLength / 4) * 4 : byteLength;
357
+ const commandEncoder = this.device.createCommandEncoder();
358
+ commandEncoder.copyBufferToBuffer({
359
+ sourceBuffer,
360
+ destinationBuffer,
361
+ size: copyByteLength
362
+ });
363
+ this.device.submit(commandEncoder.finish());
364
+ }
365
+
366
+ private _touch(): void {
367
+ this.updateTimestamp = this.device.incrementTimestamp();
368
+ }
369
+
370
+ private _resetDebugData(byteLength: number): void {
371
+ if (!this._debugDataEnabled) {
372
+ this.debugData = new ArrayBuffer(0);
373
+ return;
374
+ }
375
+
376
+ this.debugData = new ArrayBuffer(Math.min(byteLength, this._maxDebugDataByteLength));
377
+ }
378
+
379
+ private _writeDebugData(
380
+ data: ArrayBuffer | SharedArrayBuffer | ArrayBufferView,
381
+ byteOffset: number
382
+ ): boolean {
383
+ if (
384
+ !this._debugDataEnabled ||
385
+ this.debugData.byteLength === 0 ||
386
+ byteOffset >= this.debugData.byteLength
387
+ ) {
388
+ return false;
389
+ }
390
+
391
+ const source = ArrayBuffer.isView(data)
392
+ ? new Uint8Array(data.buffer, data.byteOffset, data.byteLength)
393
+ : new Uint8Array(data);
394
+ const target = new Uint8Array(this.debugData);
395
+ const copyByteLength = Math.min(source.byteLength, target.byteLength - byteOffset);
396
+ target.set(source.subarray(0, copyByteLength), byteOffset);
397
+ return copyByteLength > 0;
398
+ }
399
+ }
400
+
401
+ /** Returns `true` when a value has the structural shape of a buffer range binding. */
402
+ export function isBufferRangeBinding(binding: unknown): binding is BufferRangeBinding {
403
+ return (
404
+ binding !== null &&
405
+ typeof binding === 'object' &&
406
+ 'buffer' in (binding as Record<string, unknown>)
407
+ );
408
+ }
409
+
410
+ /** Returns `true` when a range binding points at a {@link DynamicBuffer}. */
411
+ export function isDynamicBufferRange(binding: unknown): binding is DynamicBufferRange {
412
+ return isBufferRangeBinding(binding) && binding.buffer instanceof DynamicBuffer;
413
+ }
414
+
415
+ /** Extracts a {@link DynamicBuffer} from a direct binding or range binding. */
416
+ export function getDynamicBufferFromBinding(binding: unknown): DynamicBuffer | null {
417
+ if (binding instanceof DynamicBuffer) {
418
+ return binding;
419
+ }
420
+
421
+ if (isBufferRangeBinding(binding) && binding.buffer instanceof DynamicBuffer) {
422
+ return binding.buffer;
423
+ }
424
+
425
+ return null;
426
+ }
427
+
428
+ /** Resolves a static or dynamic buffer source to the current core {@link Buffer}. */
429
+ export function resolveBufferBindingSource(buffer: BufferBindingSource): Buffer {
430
+ return buffer instanceof DynamicBuffer ? buffer.buffer : buffer;
431
+ }
432
+
433
+ /** Resolves a dynamic buffer range to a core buffer range over the current backing buffer. */
434
+ export function resolveDynamicBufferRangeBinding(binding: DynamicBufferRange): {
435
+ buffer: Buffer;
436
+ offset?: number;
437
+ size?: number;
438
+ } {
439
+ return resolveBufferRangeBinding(binding);
440
+ }
441
+
442
+ /** Resolves a buffer range to a core buffer range over the current backing buffer. */
443
+ export function resolveBufferRangeBinding(binding: BufferRangeBinding): {
444
+ buffer: Buffer;
445
+ offset?: number;
446
+ size?: number;
447
+ } {
448
+ return {
449
+ buffer: resolveBufferBindingSource(binding.buffer),
450
+ offset: binding.offset,
451
+ size: binding.size
452
+ };
453
+ }
@@ -40,6 +40,7 @@ import {
40
40
  getTextureArraySubresources,
41
41
  getTextureCubeArraySubresources
42
42
  } from './texture-data';
43
+ import type {TextureBindingLayout, TextureBindingSource} from './texture-binding-source';
43
44
 
44
45
  /**
45
46
  * Properties for a dynamic texture
@@ -75,7 +76,7 @@ export type DynamicTextureProps = Omit<TextureProps, 'data' | 'mipLevels' | 'wid
75
76
  * The `DynamicTexture` class allows luma.gl to provide some support for working with textures
76
77
  * without accumulating excessive complexity in the core Texture class which is designed as an immutable nature of GPU resource.
77
78
  */
78
- export class DynamicTexture {
79
+ export class DynamicTexture implements TextureBindingSource {
79
80
  readonly device: Device;
80
81
  readonly id: string;
81
82
 
@@ -91,6 +92,10 @@ export class DynamicTexture {
91
92
  readonly ready: Promise<Texture>;
92
93
  isReady = false;
93
94
  destroyed = false;
95
+ /** Monotonic version that increments whenever texture, view, or sampler identity changes. */
96
+ generation = 0;
97
+ /** Last update timestamp for texture content, readiness, or identity changes. */
98
+ updateTimestamp: number;
94
99
 
95
100
  private resolveReady: (t: Texture) => void = () => {};
96
101
  private rejectReady: (error: Error) => void = () => {};
@@ -117,6 +122,15 @@ export class DynamicTexture {
117
122
  return `DynamicTexture:"${this.id}":${width}x${height}px:(${this.isReady ? 'ready' : 'loading...'})`;
118
123
  }
119
124
 
125
+ /**
126
+ * Resolves this ready dynamic texture as an ordinary copied texture binding.
127
+ * @param _bindingLayout Reflected texture-like shader slot; DynamicTexture always resolves a copied texture.
128
+ * @returns The copied texture, or `null` while the source is not ready.
129
+ */
130
+ resolveTextureBinding(_bindingLayout: TextureBindingLayout): Texture | null {
131
+ return this.isReady ? this.texture : null;
132
+ }
133
+
120
134
  constructor(device: Device, props: DynamicTextureProps) {
121
135
  this.device = device;
122
136
 
@@ -130,6 +144,7 @@ export class DynamicTexture {
130
144
  this.resolveReady = resolve;
131
145
  this.rejectReady = reject;
132
146
  });
147
+ this.updateTimestamp = this.device.incrementTimestamp();
133
148
 
134
149
  this.initAsync(originalPropsWithAsyncData);
135
150
  }
@@ -224,6 +239,7 @@ export class DynamicTexture {
224
239
  this._texture = this.device.createTexture(finalTextureProps);
225
240
  this._sampler = this.texture.sampler;
226
241
  this._view = this.texture.view;
242
+ this._touchGeneration();
227
243
 
228
244
  // Upload data if provided
229
245
  if (textureData.subresources.length) {
@@ -241,7 +257,7 @@ export class DynamicTexture {
241
257
  this.isReady = true;
242
258
  this.resolveReady(this.texture);
243
259
 
244
- log.info(0, `${this} created`)();
260
+ log.info(1, `${this} created`)();
245
261
  } catch (e) {
246
262
  const err = e instanceof Error ? e : new Error(String(e));
247
263
  this.rejectReady(err);
@@ -255,14 +271,17 @@ export class DynamicTexture {
255
271
  this._sampler = null;
256
272
  this._view = null;
257
273
  }
274
+ this.isReady = false;
258
275
  this.destroyed = true;
259
276
  }
260
277
 
261
278
  generateMipmaps(): void {
262
279
  if (this.device.type === 'webgl') {
263
280
  this.texture.generateMipmapsWebGL();
281
+ this._touch();
264
282
  } else if (this.device.type === 'webgpu') {
265
283
  this.device.generateMipmapsWebGPU(this.texture);
284
+ this._touch();
266
285
  } else {
267
286
  log.warn(`${this} mipmaps not supported on ${this.device.type}`);
268
287
  }
@@ -274,6 +293,7 @@ export class DynamicTexture {
274
293
  const s = sampler instanceof Sampler ? sampler : this.device.createSampler(sampler);
275
294
  this.texture.setSampler(s);
276
295
  this._sampler = s;
296
+ this._touchGeneration();
277
297
  }
278
298
 
279
299
  /**
@@ -326,7 +346,7 @@ export class DynamicTexture {
326
346
  const buffer = await this.readBuffer(options);
327
347
  const data = await buffer.readAsync(0, layout.byteLength);
328
348
  buffer.destroy();
329
- return data.buffer;
349
+ return data.buffer instanceof ArrayBuffer ? data.buffer : data.slice().buffer;
330
350
  }
331
351
 
332
352
  /**
@@ -345,6 +365,7 @@ export class DynamicTexture {
345
365
  this._view = this.texture.view;
346
366
 
347
367
  prev.destroy();
368
+ this._touchGeneration();
348
369
  log.info(`${this} resized`);
349
370
  return true;
350
371
  }
@@ -455,6 +476,9 @@ export class DynamicTexture {
455
476
  throw new Error('Unsupported 2D mip-level payload');
456
477
  }
457
478
  }
479
+ if (subresources.length > 0) {
480
+ this._touch();
481
+ }
458
482
  }
459
483
 
460
484
  // ------------------ helpers ------------------
@@ -478,6 +502,15 @@ export class DynamicTexture {
478
502
  }
479
503
  }
480
504
 
505
+ private _touch(): void {
506
+ this.updateTimestamp = this.device.incrementTimestamp();
507
+ }
508
+
509
+ private _touchGeneration(): void {
510
+ this.generation++;
511
+ this._touch();
512
+ }
513
+
481
514
  static defaultProps: Required<DynamicTextureProps> = {
482
515
  ...Texture.defaultProps,
483
516
  dimension: '2d',
@@ -0,0 +1,94 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ import type {Binding, BindingDeclaration, ComputeShaderLayout, ShaderLayout} from '@luma.gl/core';
6
+ import {getShaderLayoutBinding} from '@luma.gl/core';
7
+
8
+ /** Reflected shader binding declarations that accept texture-like engine sources. */
9
+ export type TextureBindingLayout = Extract<
10
+ BindingDeclaration,
11
+ {type: 'texture' | 'external-texture'}
12
+ >;
13
+
14
+ /**
15
+ * Engine-level source that resolves into a concrete core texture binding for one shader slot.
16
+ */
17
+ export interface TextureBindingSource {
18
+ /** Stable resource identifier used in loading and debug messages. */
19
+ readonly id: string;
20
+ /** Whether the source can resolve a concrete binding for the current draw. */
21
+ readonly isReady: boolean;
22
+ /** Monotonic binding generation advanced when concrete draw bindings may need recreation. */
23
+ readonly generation: number;
24
+ /** Device timestamp of the most recent source content or binding identity update. */
25
+ readonly updateTimestamp: number;
26
+
27
+ /**
28
+ * Resolves this source for one reflected texture binding declaration.
29
+ * @param bindingLayout Reflected `texture` or `external-texture` shader slot.
30
+ * @returns A concrete core binding, or `null` while the source is not ready.
31
+ */
32
+ resolveTextureBinding(bindingLayout: TextureBindingLayout): Binding | null;
33
+ }
34
+
35
+ /**
36
+ * Returns whether a value is an engine source that resolves texture bindings at draw time.
37
+ * @param value Candidate engine binding value.
38
+ * @returns Whether `value` implements {@link TextureBindingSource}.
39
+ */
40
+ export function isTextureBindingSource(value: unknown): value is TextureBindingSource {
41
+ return (
42
+ value !== null &&
43
+ typeof value === 'object' &&
44
+ 'resolveTextureBinding' in value &&
45
+ typeof value.resolveTextureBinding === 'function'
46
+ );
47
+ }
48
+
49
+ /**
50
+ * Returns whether a shader binding declaration accepts a texture binding source.
51
+ * @param bindingLayout Candidate reflected shader binding declaration.
52
+ * @returns Whether `bindingLayout` is a `texture` or `external-texture` declaration.
53
+ */
54
+ export function isTextureBindingLayout(
55
+ bindingLayout: BindingDeclaration | null
56
+ ): bindingLayout is TextureBindingLayout {
57
+ return bindingLayout?.type === 'texture' || bindingLayout?.type === 'external-texture';
58
+ }
59
+
60
+ /**
61
+ * Looks up the reflected texture binding declaration for one engine binding name.
62
+ *
63
+ * @remarks
64
+ * Models and materials can be asked for bindings before a shader layout has been reflected.
65
+ * In that legacy empty-layout path, `fallbackGroup` synthesizes an ordinary `texture` slot so
66
+ * copied `DynamicTexture` and `VideoTexture` bindings keep their pre-reflection behavior.
67
+ * Native WebGPU `texture_external` resolution still requires the real reflected declaration.
68
+ *
69
+ * @param shaderLayout Reflected shader bindings, or an empty binding list before reflection.
70
+ * @param bindingName Engine binding name to resolve against the shader layout.
71
+ * @param options Texture binding lookup behavior.
72
+ * @param options.fallbackGroup Bind group for the legacy empty-layout ordinary texture fallback.
73
+ * @returns The reflected or synthesized texture binding declaration, or `null` for non-texture slots.
74
+ */
75
+ export function getTextureBindingLayout(
76
+ shaderLayout: Pick<ShaderLayout | ComputeShaderLayout, 'bindings'>,
77
+ bindingName: string,
78
+ options?: {fallbackGroup?: number}
79
+ ): TextureBindingLayout | null {
80
+ const bindingLayout = getShaderLayoutBinding(shaderLayout, bindingName, {
81
+ ignoreWarnings: true
82
+ });
83
+ if (isTextureBindingLayout(bindingLayout)) {
84
+ return bindingLayout;
85
+ }
86
+ return shaderLayout.bindings.length === 0 && options?.fallbackGroup !== undefined
87
+ ? {
88
+ type: 'texture',
89
+ name: bindingName,
90
+ group: options.fallbackGroup,
91
+ location: 0
92
+ }
93
+ : null;
94
+ }