@luma.gl/graph 10.0.0-alpha.7 → 10.0.0-alpha.8

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 (177) hide show
  1. package/dist/dist.dev.js +5096 -7269
  2. package/dist/dist.min.js +270 -83
  3. package/dist/experimental/index.cjs.map +2 -2
  4. package/dist/experimental/simulation/index.cjs.map +2 -2
  5. package/dist/gpu-core/compiled-gpu-graph.cjs +99 -14
  6. package/dist/gpu-core/compiled-gpu-graph.d.ts +26 -4
  7. package/dist/gpu-core/compiled-gpu-graph.d.ts.map +1 -1
  8. package/dist/gpu-core/compiled-gpu-graph.js +101 -13
  9. package/dist/gpu-core/compiled-gpu-graph.js.map +1 -1
  10. package/dist/gpu-core/gpu-command-graph-compiler.cjs +35 -3
  11. package/dist/gpu-core/gpu-command-graph-compiler.js +40 -4
  12. package/dist/gpu-core/gpu-command-graph-compiler.js.map +1 -1
  13. package/dist/gpu-core/gpu-command-graph-types.d.ts +2 -0
  14. package/dist/gpu-core/gpu-command-graph-types.d.ts.map +1 -1
  15. package/dist/gpu-core/gpu-compaction-operation.cjs +56 -0
  16. package/dist/gpu-core/gpu-compaction-operation.d.ts +20 -0
  17. package/dist/gpu-core/gpu-compaction-operation.d.ts.map +1 -0
  18. package/dist/gpu-core/gpu-compaction-operation.js +36 -0
  19. package/dist/gpu-core/gpu-compaction-operation.js.map +1 -0
  20. package/dist/gpu-core/gpu-compaction.cjs +35 -3
  21. package/dist/gpu-core/gpu-compaction.d.ts.map +1 -1
  22. package/dist/gpu-core/gpu-compaction.js +38 -3
  23. package/dist/gpu-core/gpu-compaction.js.map +1 -1
  24. package/dist/gpu-core/gpu-expression-operation.cjs +18 -2
  25. package/dist/gpu-core/gpu-expression-operation.d.ts +2 -2
  26. package/dist/gpu-core/gpu-expression-operation.d.ts.map +1 -1
  27. package/dist/gpu-core/gpu-expression-operation.js +20 -1
  28. package/dist/gpu-core/gpu-expression-operation.js.map +1 -1
  29. package/dist/gpu-core/gpu-expression-plan.cjs +140 -29
  30. package/dist/gpu-core/gpu-expression-plan.d.ts +26 -5
  31. package/dist/gpu-core/gpu-expression-plan.d.ts.map +1 -1
  32. package/dist/gpu-core/gpu-expression-plan.js +168 -34
  33. package/dist/gpu-core/gpu-expression-plan.js.map +1 -1
  34. package/dist/gpu-core/gpu-graph-compiler.cjs +100 -11
  35. package/dist/gpu-core/gpu-graph-compiler.d.ts +7 -3
  36. package/dist/gpu-core/gpu-graph-compiler.d.ts.map +1 -1
  37. package/dist/gpu-core/gpu-graph-compiler.js +109 -9
  38. package/dist/gpu-core/gpu-graph-compiler.js.map +1 -1
  39. package/dist/gpu-core/gpu-graph-declarations.cjs +120 -0
  40. package/dist/gpu-core/gpu-graph-declarations.d.ts +4 -0
  41. package/dist/gpu-core/gpu-graph-declarations.d.ts.map +1 -0
  42. package/dist/gpu-core/gpu-graph-declarations.js +110 -0
  43. package/dist/gpu-core/gpu-graph-declarations.js.map +1 -0
  44. package/dist/gpu-core/gpu-graph-validation.cjs +33 -2
  45. package/dist/gpu-core/gpu-graph-validation.d.ts.map +1 -1
  46. package/dist/gpu-core/gpu-graph-validation.js +32 -0
  47. package/dist/gpu-core/gpu-graph-validation.js.map +1 -1
  48. package/dist/gpu-core/gpu-graph-value.cjs +38 -3
  49. package/dist/gpu-core/gpu-graph-value.d.ts +50 -4
  50. package/dist/gpu-core/gpu-graph-value.d.ts.map +1 -1
  51. package/dist/gpu-core/gpu-graph-value.js +39 -1
  52. package/dist/gpu-core/gpu-graph-value.js.map +1 -1
  53. package/dist/gpu-core/gpu-graph.cjs +65 -0
  54. package/dist/gpu-core/gpu-graph.d.ts +15 -2
  55. package/dist/gpu-core/gpu-graph.d.ts.map +1 -1
  56. package/dist/gpu-core/gpu-graph.js +70 -0
  57. package/dist/gpu-core/gpu-graph.js.map +1 -1
  58. package/dist/gpu-core/gpu-operation-contract.cjs +5 -0
  59. package/dist/gpu-core/gpu-operation-contract.d.ts +10 -0
  60. package/dist/gpu-core/gpu-operation-contract.d.ts.map +1 -1
  61. package/dist/gpu-core/gpu-operation-contract.js +6 -1
  62. package/dist/gpu-core/gpu-operation-contract.js.map +1 -1
  63. package/dist/gpu-core/gpu-scan.cjs +11 -2
  64. package/dist/gpu-core/gpu-scan.d.ts.map +1 -1
  65. package/dist/gpu-core/gpu-scan.js +11 -2
  66. package/dist/gpu-core/gpu-scan.js.map +1 -1
  67. package/dist/gpu-core/gpu-texture-copy.cjs +53 -0
  68. package/dist/gpu-core/gpu-texture-copy.d.ts +7 -0
  69. package/dist/gpu-core/gpu-texture-copy.d.ts.map +1 -0
  70. package/dist/gpu-core/gpu-texture-copy.js +39 -0
  71. package/dist/gpu-core/gpu-texture-copy.js.map +1 -0
  72. package/dist/gpu-core/gpu-texture-plan.cjs +181 -0
  73. package/dist/gpu-core/gpu-texture-plan.d.ts +32 -0
  74. package/dist/gpu-core/gpu-texture-plan.d.ts.map +1 -0
  75. package/dist/gpu-core/gpu-texture-plan.js +215 -0
  76. package/dist/gpu-core/gpu-texture-plan.js.map +1 -0
  77. package/dist/gpu-core/gpu-transfer-lowering.cjs +109 -0
  78. package/dist/gpu-core/gpu-transfer-lowering.d.ts +9 -0
  79. package/dist/gpu-core/gpu-transfer-lowering.d.ts.map +1 -0
  80. package/dist/gpu-core/gpu-transfer-lowering.js +88 -0
  81. package/dist/gpu-core/gpu-transfer-lowering.js.map +1 -0
  82. package/dist/gpu-core/gpu-transfer-operation.cjs +100 -0
  83. package/dist/gpu-core/gpu-transfer-operation.d.ts +38 -0
  84. package/dist/gpu-core/gpu-transfer-operation.d.ts.map +1 -0
  85. package/dist/gpu-core/gpu-transfer-operation.js +84 -0
  86. package/dist/gpu-core/gpu-transfer-operation.js.map +1 -0
  87. package/dist/gpu-core/index.cjs +13 -0
  88. package/dist/gpu-core/index.cjs.map +3 -3
  89. package/dist/gpu-core/index.d.ts +11 -1
  90. package/dist/gpu-core/index.d.ts.map +1 -1
  91. package/dist/gpu-core/index.js +4 -1
  92. package/dist/gpu-core/index.js.map +1 -1
  93. package/dist/gpu-dggs/benchmarks.cjs.map +2 -2
  94. package/dist/gpu-graph/index.cjs +8 -0
  95. package/dist/gpu-graph/index.cjs.map +3 -3
  96. package/dist/gpu-graph/index.d.ts +5 -0
  97. package/dist/gpu-graph/index.d.ts.map +1 -1
  98. package/dist/gpu-graph/index.js +2 -0
  99. package/dist/gpu-graph/index.js.map +1 -1
  100. package/dist/gpu-graph-algorithms/benchmarks.cjs.map +3 -3
  101. package/dist/gpu-graph-algorithms/index.cjs.map +2 -2
  102. package/dist/gpu-physics/gpu-rigid-body-world.cjs +56 -17
  103. package/dist/gpu-physics/gpu-rigid-body-world.d.ts +1 -1
  104. package/dist/gpu-physics/gpu-rigid-body-world.d.ts.map +1 -1
  105. package/dist/gpu-physics/gpu-rigid-body-world.js +42 -2
  106. package/dist/gpu-physics/gpu-rigid-body-world.js.map +1 -1
  107. package/dist/gpu-physics/index.cjs.map +2 -2
  108. package/dist/gpu-physics/rigid-body-joints.cjs +6 -1
  109. package/dist/gpu-physics/rigid-body-joints.d.ts.map +1 -1
  110. package/dist/gpu-physics/rigid-body-joints.js +9 -1
  111. package/dist/gpu-physics/rigid-body-joints.js.map +1 -1
  112. package/dist/gpu-physics/rigid-body-parallel-shaders.cjs +4 -2
  113. package/dist/gpu-physics/rigid-body-parallel-shaders.d.ts.map +1 -1
  114. package/dist/gpu-physics/rigid-body-parallel-shaders.js +4 -2
  115. package/dist/gpu-physics/rigid-body-parallel-shaders.js.map +1 -1
  116. package/dist/gpu-physics/rigid-body-shaders.cjs +1 -0
  117. package/dist/gpu-physics/rigid-body-shaders.d.ts.map +1 -1
  118. package/dist/gpu-physics/rigid-body-shaders.js +1 -0
  119. package/dist/gpu-physics/rigid-body-shaders.js.map +1 -1
  120. package/dist/gpu-pointcloud/index.cjs.map +1 -1
  121. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.cjs +54 -7
  122. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.d.ts +11 -4
  123. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.d.ts.map +1 -1
  124. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.js +56 -4
  125. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.js.map +1 -1
  126. package/dist/gpu-simple-graph/gpu-simple-graph.cjs +16 -0
  127. package/dist/gpu-simple-graph/gpu-simple-graph.d.ts +11 -4
  128. package/dist/gpu-simple-graph/gpu-simple-graph.d.ts.map +1 -1
  129. package/dist/gpu-simple-graph/gpu-simple-graph.js +18 -0
  130. package/dist/gpu-simple-graph/gpu-simple-graph.js.map +1 -1
  131. package/dist/gpu-simple-graph/gpu-transfer-execution.cjs +82 -0
  132. package/dist/gpu-simple-graph/gpu-transfer-execution.d.ts +7 -0
  133. package/dist/gpu-simple-graph/gpu-transfer-execution.d.ts.map +1 -0
  134. package/dist/gpu-simple-graph/gpu-transfer-execution.js +64 -0
  135. package/dist/gpu-simple-graph/gpu-transfer-execution.js.map +1 -0
  136. package/dist/gpu-simple-graph/index.cjs +3 -0
  137. package/dist/gpu-simple-graph/index.cjs.map +3 -3
  138. package/dist/gpu-simple-graph/index.d.ts +3 -0
  139. package/dist/gpu-simple-graph/index.d.ts.map +1 -1
  140. package/dist/gpu-simple-graph/index.js +1 -0
  141. package/dist/gpu-simple-graph/index.js.map +1 -1
  142. package/dist/index.cjs +13 -0
  143. package/dist/index.cjs.map +3 -3
  144. package/dist/index.d.ts +11 -1
  145. package/dist/index.d.ts.map +1 -1
  146. package/dist/index.js +4 -1
  147. package/dist/index.js.map +1 -1
  148. package/package.json +6 -7
  149. package/src/gpu-core/compiled-gpu-graph.ts +118 -15
  150. package/src/gpu-core/gpu-command-graph-compiler.ts +38 -6
  151. package/src/gpu-core/gpu-command-graph-types.ts +2 -0
  152. package/src/gpu-core/gpu-compaction-operation.ts +49 -0
  153. package/src/gpu-core/gpu-compaction.ts +47 -3
  154. package/src/gpu-core/gpu-expression-operation.ts +23 -2
  155. package/src/gpu-core/gpu-expression-plan.ts +247 -44
  156. package/src/gpu-core/gpu-graph-compiler.ts +130 -11
  157. package/src/gpu-core/gpu-graph-declarations.ts +137 -0
  158. package/src/gpu-core/gpu-graph-validation.ts +32 -0
  159. package/src/gpu-core/gpu-graph-value.ts +88 -4
  160. package/src/gpu-core/gpu-graph.ts +104 -2
  161. package/src/gpu-core/gpu-operation-contract.ts +18 -0
  162. package/src/gpu-core/gpu-scan.ts +15 -2
  163. package/src/gpu-core/gpu-texture-copy.ts +51 -0
  164. package/src/gpu-core/gpu-texture-plan.ts +248 -0
  165. package/src/gpu-core/gpu-transfer-lowering.ts +102 -0
  166. package/src/gpu-core/gpu-transfer-operation.ts +110 -0
  167. package/src/gpu-core/index.ts +22 -1
  168. package/src/gpu-graph/index.ts +15 -0
  169. package/src/gpu-physics/gpu-rigid-body-world.ts +44 -3
  170. package/src/gpu-physics/rigid-body-joints.ts +11 -2
  171. package/src/gpu-physics/rigid-body-parallel-shaders.ts +4 -2
  172. package/src/gpu-physics/rigid-body-shaders.ts +1 -0
  173. package/src/gpu-simple-graph/compiled-gpu-expression-graph.ts +90 -8
  174. package/src/gpu-simple-graph/gpu-simple-graph.ts +56 -3
  175. package/src/gpu-simple-graph/gpu-transfer-execution.ts +73 -0
  176. package/src/gpu-simple-graph/index.ts +9 -0
  177. package/src/index.ts +29 -1
@@ -2,11 +2,21 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
+ import {lowerGPUUpload, lowerGPUCopy, lowerGPUTextureCopy} from './gpu-transfer-lowering';
6
+ import {GPUUploadOperation, GPUCopyOperation} from './gpu-transfer-operation';
7
+ import {GPUCompactionOperation} from './gpu-compaction-operation';
8
+ import {GPUCompaction} from './gpu-compaction';
9
+
5
10
  import type {TextureFormat} from '@luma.gl/core';
6
- import type {GraphTextureHandle, GraphImportedTexture} from './gpu-command-graph';
11
+ import {getTextureBindingInfo, getBoundTextureViewProps} from './gpu-texture-plan';
12
+ import type {
13
+ GraphBufferHandle,
14
+ GraphTextureHandle,
15
+ GraphImportedTexture
16
+ } from './gpu-command-graph';
7
17
  import {GPUGraphTextureView, type GPUGraphTexture} from './gpu-graph-value';
8
18
 
9
- import {Buffer, type Device} from '@luma.gl/core';
19
+ import {assert, Buffer, TextureView, type Device} from '@luma.gl/core';
10
20
  import {getGPUVectorFormatInfo, type GPUVectorInput} from '@luma.gl/graph/gpu-data';
11
21
  import type {GPUGraphVectorFormat} from './gpu-graph-value';
12
22
  import {type GPUCommandGraph, GraphVectorView} from './gpu-command-graph';
@@ -50,13 +60,14 @@ import {
50
60
  type GPUOperationLoweringDecision,
51
61
  type GPUGraphBackendCapabilities
52
62
  } from './gpu-graph-lowering';
63
+ import {resolveGPUGraphDeclarations} from './gpu-graph-declarations';
53
64
  import {validateGPUGraph, type GPUGraphValidationReport} from './gpu-graph-validation';
54
65
  import type {GPUGraphScalar, GPUGraphVector} from './gpu-graph-value';
55
66
  import type {GPUGraph} from './gpu-graph';
56
67
  export type GPUGraphBindings = {
57
68
  scalars?: Readonly<Record<string, number>>;
58
69
  vectors?: Readonly<Record<string, GPUVectorInput>>;
59
- textures?: Readonly<Record<string, GraphImportedTexture>>;
70
+ textures?: Readonly<Record<string, GraphImportedTexture | TextureView>>;
60
71
  };
61
72
  export type GPUGraphLoweredNode = {
62
73
  nodeId: string;
@@ -86,13 +97,17 @@ type LoweringState = {
86
97
  vectors: Map<string, GraphVectorView>;
87
98
  textures: Map<string, GraphTextureHandle>;
88
99
  textureValues: readonly GPUGraphTexture[];
100
+ textureViews: ReadonlyMap<string, TextureView>;
89
101
  predicateConjunctions: Map<string, GPUScalar<'uint32'>>;
90
102
  commandNodeIds: Set<string>;
103
+ transientResultNames: ReadonlySet<string>;
104
+ retainedBuffers: readonly GraphBufferHandle[];
91
105
  };
92
106
  export class GPUGraphCompiler<Parameters = void> {
93
107
  readonly device: Device;
94
108
  readonly capabilities: GPUGraphBackendCapabilities;
95
109
  readonly lowerings: readonly GPUOperationLowering<Parameters>[];
110
+ private compactionIndex = 0;
96
111
  constructor(device: Device, lowerings: readonly GPUOperationLowering<Parameters>[] = []) {
97
112
  this.device = device;
98
113
  this.capabilities = Object.freeze({
@@ -115,6 +130,35 @@ export class GPUGraphCompiler<Parameters = void> {
115
130
  );
116
131
  }
117
132
  compile(program: GPUGraph, bindings: GPUGraphBindings = {}): GPUGraphCompilation<Parameters> {
133
+ this.compactionIndex = 0;
134
+ // Bound external partitions must be available when resolving shape-inheriting results.
135
+ program = program.clone();
136
+ for (const vector of program.vectors) {
137
+ const binding = bindings.vectors?.[vector.id];
138
+ if (!vector.external || !binding || vector.chunkLengths) continue;
139
+ const data = 'data' in binding ? binding.data : 'buffer' in binding ? [binding] : binding;
140
+ program.constrainResult(vector, {
141
+ type: 'vector',
142
+ format: vector.format,
143
+ chunkLengths: data.map(chunk => chunk.length)
144
+ });
145
+ }
146
+ program = resolveGPUGraphDeclarations(program);
147
+ const transferUsages = new Map<string, number>();
148
+ const addTransferUsage = (name: string, usage: number): void => {
149
+ transferUsages.set(name, (transferUsages.get(name) ?? 0) | usage);
150
+ };
151
+ const collectTransferUsages = (operation: GPUGraphOperation): void => {
152
+ if (operation instanceof GPUCompositeOperation)
153
+ operation.operations.forEach(collectTransferUsages);
154
+ else if (operation instanceof GPUUploadOperation)
155
+ addTransferUsage(operation.output, Buffer.COPY_DST);
156
+ else if (operation instanceof GPUCopyOperation) {
157
+ addTransferUsage(operation.input, Buffer.COPY_SRC);
158
+ addTransferUsage(operation.output, Buffer.COPY_DST);
159
+ }
160
+ };
161
+ program.operations.forEach(collectTransferUsages);
118
162
  const validation = validateGPUGraph(program, bindings);
119
163
  if (!validation.valid)
120
164
  throw new Error(
@@ -198,7 +242,10 @@ export class GPUGraphCompiler<Parameters = void> {
198
242
  graph,
199
243
  chunkLengths.length === 1 ? vector.id : `${vector.id}-chunk-${index}`,
200
244
  vector.format,
201
- length
245
+ length,
246
+ vector.role === 'value'
247
+ ? Buffer.STORAGE | Buffer.VERTEX | Buffer.COPY_SRC | Buffer.COPY_DST
248
+ : Buffer.STORAGE | (transferUsages.get(vector.id) ?? 0)
202
249
  )
203
250
  );
204
251
  const formatInfo = getGPUVectorFormatInfo(vector.format);
@@ -217,10 +264,34 @@ export class GPUGraphCompiler<Parameters = void> {
217
264
  vectors.set(vector.id, resolved);
218
265
  }
219
266
  const textures = new Map<string, GraphTextureHandle>();
267
+ const textureViews = new Map<string, TextureView>();
220
268
  for (const texture of program.textures) {
269
+ // Reusable declarations must have execution-resolved dimensions before low-level lowering.
270
+ assert(texture.width !== undefined && texture.height !== undefined);
271
+ let binding = bindings.textures?.[texture.id];
272
+ let descriptor = {...texture, width: texture.width, height: texture.height};
273
+ if (binding instanceof TextureView) {
274
+ const info = getTextureBindingInfo(binding);
275
+ assert(
276
+ info.format === texture.format &&
277
+ info.width === texture.width &&
278
+ info.height === texture.height
279
+ );
280
+ textureViews.set(texture.id, binding);
281
+ binding = info.texture;
282
+ descriptor = {
283
+ ...descriptor,
284
+ dimension: binding.dimension,
285
+ width: binding.width,
286
+ height: binding.height,
287
+ depth: binding.depth,
288
+ mipLevels: binding.mipLevels,
289
+ samples: binding.samples
290
+ };
291
+ }
221
292
  const handle = texture.external
222
- ? graph.importTexture(texture, bindings.textures?.[texture.id])
223
- : graph.createTransientTexture(texture);
293
+ ? graph.importTexture(descriptor, binding)
294
+ : graph.createTransientTexture(descriptor);
224
295
  textures.set(texture.id, handle);
225
296
  }
226
297
  const state: LoweringState = {
@@ -232,8 +303,15 @@ export class GPUGraphCompiler<Parameters = void> {
232
303
  vectors,
233
304
  textures,
234
305
  textureValues: program.textures,
306
+ textureViews,
235
307
  predicateConjunctions: new Map(),
236
- commandNodeIds: new Set()
308
+ commandNodeIds: new Set(),
309
+ transientResultNames: new Set(
310
+ program.vectors
311
+ .filter(vector => vector.role === 'value' && !vector.external)
312
+ .map(vector => vector.id)
313
+ ),
314
+ retainedBuffers: []
237
315
  };
238
316
  for (const operation of program.operations) this.lower(graph, operation, state);
239
317
  return Object.freeze({
@@ -256,6 +334,24 @@ export class GPUGraphCompiler<Parameters = void> {
256
334
  }
257
335
  protected createCoreLowerings(): GPUOperationLowering<Parameters>[] {
258
336
  return [
337
+ {type: 'vector-upload', lower: lowerGPUUpload},
338
+ {type: 'vector-copy', lower: lowerGPUCopy},
339
+ {
340
+ type: 'compaction',
341
+ lower: (operation: GPUCompactionOperation, context) => {
342
+ const {input, flags, output, count} = operation.props;
343
+ context.graph.add(
344
+ new GPUCompaction({
345
+ // Unrolled iterations need distinct scratch resources as well as distinct nodes.
346
+ id: `${operation.id}-${this.compactionIndex++}`,
347
+ input: context.resolveVector<'uint32'>(input),
348
+ flags: context.resolveVector<'uint32'>(flags),
349
+ output: context.resolveVector<'uint32'>(output),
350
+ count: context.resolveScalar<'uint32'>(count).view
351
+ })
352
+ );
353
+ }
354
+ },
259
355
  {
260
356
  type: 'scalar-literal',
261
357
  lower: (operation: GPUGraphScalarLiteral, context) => {
@@ -411,6 +507,16 @@ export class GPUGraphCompiler<Parameters = void> {
411
507
  ? operation.id
412
508
  : (operation.constructor?.name ?? 'command-node-producer');
413
509
  state.path.push(id);
510
+ const previousRetainedBuffers = state.retainedBuffers;
511
+ const retainedBuffers = [...previousRetainedBuffers];
512
+ if (isGPUOperation(operation)) {
513
+ for (const use of operation.contract?.values ?? []) {
514
+ const name = typeof use.value === 'string' ? use.value : use.value.id;
515
+ if (state.transientResultNames.has(name))
516
+ for (const chunk of state.vectors.get(name)!.data) retainedBuffers.push(chunk.buffer);
517
+ }
518
+ }
519
+ state.retainedBuffers = retainedBuffers;
414
520
  try {
415
521
  if (operation instanceof GPUCompositeOperation && operation.type === 'composite') {
416
522
  state.decisions.push({
@@ -428,7 +534,10 @@ export class GPUGraphCompiler<Parameters = void> {
428
534
  const lowerer = this.lowerings.find(lowering => lowering.type === operation.type)?.lower;
429
535
  if (lowerer) {
430
536
  const invoke = () =>
431
- lowerer(operation, {
537
+ (operation instanceof GPUCopyOperation &&
538
+ state.textureValues.some(value => value.id === operation.input)
539
+ ? (lowerGPUTextureCopy as typeof lowerer)
540
+ : lowerer)(operation, {
432
541
  graph,
433
542
  capabilities: this.capabilities,
434
543
  lower: child => this.lower(graph, child, state),
@@ -446,7 +555,13 @@ export class GPUGraphCompiler<Parameters = void> {
446
555
  resolveTextureView: value => {
447
556
  const texture = value instanceof GPUGraphTextureView ? value.texture : value;
448
557
  const props = value instanceof GPUGraphTextureView ? value.props : {};
449
- return graph.createTextureView(this.resolveTexture(texture, state), props);
558
+ const binding = state.textureViews.get(
559
+ typeof texture === 'string' ? texture : texture.id
560
+ );
561
+ return graph.createTextureView(
562
+ this.resolveTexture(texture, state),
563
+ binding ? getBoundTextureViewProps(binding, props) : props
564
+ );
450
565
  },
451
566
  recordDecision: d => state.decisions.push(d)
452
567
  });
@@ -468,6 +583,7 @@ export class GPUGraphCompiler<Parameters = void> {
468
583
  }
469
584
  throw new Error(`GPUGraph operation "${id}" has no WebGPU lowering`);
470
585
  } finally {
586
+ state.retainedBuffers = previousRetainedBuffers;
471
587
  state.path.pop();
472
588
  }
473
589
  }
@@ -515,7 +631,7 @@ export class GPUGraphCompiler<Parameters = void> {
515
631
  nodeType: decorated.type,
516
632
  operationPath: [...state.path]
517
633
  });
518
- return decorated;
634
+ return {...decorated, retainedBuffers: state.retainedBuffers};
519
635
  }, callback)
520
636
  );
521
637
  }
@@ -524,7 +640,10 @@ export class GPUGraphCompiler<Parameters = void> {
524
640
  node: any,
525
641
  state: LoweringState
526
642
  ): any {
527
- node = this.makeCommandNodeIdUnique(node, state);
643
+ node = {
644
+ ...this.makeCommandNodeIdUnique(node, state),
645
+ retainedBuffers: state.retainedBuffers
646
+ };
528
647
  if (state.path.length)
529
648
  state.nodes.push({nodeId: node.id, nodeType: 'compute', operationPath: [...state.path]});
530
649
  if (!state.predicates.length) return node;
@@ -0,0 +1,137 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import type {GPUGraph} from './gpu-graph';
6
+ import {
7
+ GPUGraphScalar,
8
+ GPUGraphTexture,
9
+ GPUGraphVector,
10
+ gpuGraph,
11
+ type GPUGraphValueDescriptor
12
+ } from './gpu-graph-value';
13
+ import {GPUCompositeOperation, isGPUOperation, type GPUGraphOperation} from './gpu-operation';
14
+ import {getGPUOperationValueName, validateGPUOperationContract} from './gpu-operation-contract';
15
+
16
+ /** Resolve operation-owned logical results without modifying the authored graph. @internal */
17
+ export function resolveGPUGraphDeclarations(source: GPUGraph): GPUGraph {
18
+ const program = source.clone();
19
+ const values = new Map(
20
+ [...program.scalars, ...program.vectors, ...program.textures].map(value => [value.id, value])
21
+ );
22
+ const written = new Set<string>();
23
+ const visit = (operation: GPUGraphOperation, conditional = false): void => {
24
+ if (operation instanceof GPUCompositeOperation) {
25
+ for (const child of operation.operations)
26
+ visit(child, conditional || operation.type !== 'composite');
27
+ return;
28
+ }
29
+ if (!isGPUOperation(operation)) return;
30
+ try {
31
+ const contract = operation.contract;
32
+ if (!contract) return;
33
+ const outputs =
34
+ typeof contract.outputs === 'function'
35
+ ? contract.outputs({
36
+ getValue(name): GPUGraphValueDescriptor {
37
+ const value = values.get(name);
38
+ if (!value) throw new Error(`Undeclared operation value ${name}`);
39
+ if (value instanceof GPUGraphTexture)
40
+ return (
41
+ value.descriptor ??
42
+ gpuGraph.texture({
43
+ format: value.format,
44
+ dimension: value.dimension,
45
+ extent: {width: value.width!, height: value.height!, depth: value.depth},
46
+ mipLevels: value.mipLevels,
47
+ samples: value.samples
48
+ })
49
+ );
50
+ return value instanceof GPUGraphScalar
51
+ ? gpuGraph.scalar(value.format)
52
+ : gpuGraph.vector(value.format, {
53
+ length: value.length,
54
+ chunkLengths: value.chunkLengths
55
+ });
56
+ }
57
+ })
58
+ : contract.outputs;
59
+ for (const [name, descriptor] of Object.entries(outputs ?? {})) {
60
+ if (
61
+ !contract.values.some(
62
+ use => use.access === 'write' && getGPUOperationValueName(use.value) === name
63
+ )
64
+ )
65
+ throw new Error(`Operation result ${name} requires a write`);
66
+ if (descriptor.type === 'texture' && descriptor.extent && 'like' in descriptor.extent) {
67
+ const sourceName = descriptor.extent.like;
68
+ if (
69
+ !(values.get(sourceName) instanceof GPUGraphTexture) ||
70
+ !contract.values.some(
71
+ use => use.access !== 'write' && getGPUOperationValueName(use.value) === sourceName
72
+ )
73
+ )
74
+ throw new Error(`Texture extent source ${sourceName} must be a declared texture read`);
75
+ }
76
+ const existing = values.get(name);
77
+ if (existing?.role === 'input' || existing?.role === 'state')
78
+ throw new Error(`Operation result conflicts with ${existing.role} ${name}`);
79
+ if (!existing) {
80
+ if (conditional)
81
+ throw new Error('Operation results inside control flow require explicit declarations');
82
+ const shape = contract.values.find(
83
+ use => getGPUOperationValueName(use.value) === name && use.access === 'write'
84
+ )?.shape;
85
+ let resolvedDescriptor = descriptor;
86
+ if (descriptor.type === 'vector' && descriptor.length === undefined && shape) {
87
+ const source =
88
+ 'like' in shape ? values.get(getGPUOperationValueName(shape.like)) : undefined;
89
+ const dimensions =
90
+ 'length' in shape ? shape : source instanceof GPUGraphVector ? source : undefined;
91
+ if (dimensions?.length !== undefined)
92
+ resolvedDescriptor = {
93
+ ...descriptor,
94
+ length: dimensions.length,
95
+ chunkLengths: descriptor.chunkLengths ?? dimensions.chunkLengths
96
+ };
97
+ }
98
+ const declaration = program.addDeclaration(name, resolvedDescriptor, 'value');
99
+ values.set(name, declaration);
100
+ } else if (
101
+ existing.format !== descriptor.format ||
102
+ existing instanceof GPUGraphVector !== (descriptor.type === 'vector') ||
103
+ existing instanceof GPUGraphTexture !== (descriptor.type === 'texture')
104
+ )
105
+ throw new Error(`Incompatible operation result ${name}`);
106
+ else if (existing instanceof GPUGraphVector && descriptor.type === 'vector')
107
+ values.set(name, program.constrainResult(existing, descriptor));
108
+ }
109
+ validateGPUOperationContract(program, contract);
110
+ for (const use of contract.values) {
111
+ const name = getGPUOperationValueName(use.value);
112
+ const declaration = values.get(name);
113
+ if (use.access !== 'read' && declaration?.role === 'input')
114
+ throw new Error(`Cannot write graph input ${name}`);
115
+ if (use.access !== 'write' && declaration?.role === 'value' && !written.has(name))
116
+ throw new Error(`Reads ${name} before it is produced`);
117
+ }
118
+ const writes = new Set(
119
+ contract.values
120
+ .filter(use => use.access !== 'read')
121
+ .map(use => getGPUOperationValueName(use.value))
122
+ );
123
+ for (const name of writes) {
124
+ if (values.get(name)?.role === 'value' && written.has(name))
125
+ throw new Error(`Multiple writers for graph result ${name}`);
126
+ written.add(name);
127
+ }
128
+ } catch (error) {
129
+ throw new Error(
130
+ `${operation.id}: ${error instanceof Error ? error.message : String(error)}`,
131
+ {cause: error}
132
+ );
133
+ }
134
+ };
135
+ program.operations.forEach(operation => visit(operation));
136
+ return program;
137
+ }
@@ -3,6 +3,7 @@
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
5
  import type {GPUGraph} from './gpu-graph';
6
+ import {resolveGPUGraphDeclarations} from './gpu-graph-declarations';
6
7
  import {validateGPUOperationContract} from './gpu-operation-contract';
7
8
  import {GPUCompositeOperation, isGPUOperation, type GPUGraphOperation} from './gpu-operation';
8
9
  import {GPUConditionalOperation, GPULoopOperation} from './gpu-control-flow-operation';
@@ -24,6 +25,37 @@ export function validateGPUGraph(
24
25
  bindings: GPUGraphBindings = {}
25
26
  ): GPUGraphValidationReport {
26
27
  const issues: GPUGraphValidationIssue[] = [];
28
+ try {
29
+ program = resolveGPUGraphDeclarations(program);
30
+ } catch (error) {
31
+ return {
32
+ valid: false,
33
+ issues: [
34
+ {
35
+ level: 'error',
36
+ code: 'invalid-operation-contract',
37
+ message: error instanceof Error ? error.message : String(error)
38
+ }
39
+ ]
40
+ };
41
+ }
42
+ for (const scalar of program.scalars) {
43
+ if (scalar.role === 'input' && bindings.scalars?.[scalar.id] === undefined)
44
+ issues.push({
45
+ level: 'error',
46
+ code: 'missing-input',
47
+ valueId: scalar.id,
48
+ message: `Missing input binding ${scalar.id}`
49
+ });
50
+ if (scalar.role === 'value' && bindings.scalars?.[scalar.id] !== undefined)
51
+ issues.push({
52
+ level: 'error',
53
+ code: 'result-binding',
54
+ valueId: scalar.id,
55
+ message: `Cannot initialize graph result ${scalar.id}`
56
+ });
57
+ }
58
+
27
59
  const scalars = new Set(program.scalars.map(value => value.id));
28
60
  for (const vector of program.vectors)
29
61
  if (vector.external && !bindings.vectors?.[vector.id])
@@ -13,11 +13,80 @@ import type {
13
13
  export type GPUGraphScalarFormat = 'float32' | 'uint32' | 'sint32';
14
14
  export type GPUGraphVectorFormat = GPUGraphScalarFormat | `${GPUGraphScalarFormat}x${2 | 3 | 4}`;
15
15
 
16
+ export type GPUGraphValueRole = 'input' | 'state' | 'value';
17
+ export type GPUGraphScalarDescriptor<T extends GPUGraphScalarFormat = GPUGraphScalarFormat> =
18
+ Readonly<{
19
+ type: 'scalar';
20
+ format: T;
21
+ }>;
22
+ export type GPUGraphVectorDescriptor<T extends GPUGraphVectorFormat = GPUGraphVectorFormat> =
23
+ Readonly<{
24
+ type: 'vector';
25
+ format: T;
26
+ length?: number;
27
+ chunkLengths?: readonly number[];
28
+ }>;
29
+ export type GPUGraphTextureExtent =
30
+ | Readonly<{width: number; height: number; depth?: number}>
31
+ | Readonly<{like: string; scale?: readonly [number, number]}>;
32
+ export type GPUGraphTextureDescriptor<Format extends TextureFormat = TextureFormat> = Readonly<{
33
+ type: 'texture';
34
+ format: Format;
35
+ dimension?: GraphTextureDimension;
36
+ extent?: GPUGraphTextureExtent;
37
+ mipLevels?: number;
38
+ samples?: number;
39
+ }>;
40
+ export type GPUGraphValueDescriptor =
41
+ | GPUGraphScalarDescriptor
42
+ | GPUGraphVectorDescriptor
43
+ | GPUGraphTextureDescriptor;
44
+
45
+ /** Pure logical descriptors: no allocation, device, or registration. */
46
+ export const gpuGraph = {
47
+ texture<Format extends TextureFormat>(
48
+ props: Omit<GPUGraphTextureDescriptor<Format>, 'type'>
49
+ ): GPUGraphTextureDescriptor<Format> {
50
+ return Object.freeze({
51
+ ...props,
52
+ type: 'texture',
53
+ ...(props.extent
54
+ ? {
55
+ extent: Object.freeze({
56
+ ...props.extent,
57
+ ...('scale' in props.extent && props.extent.scale
58
+ ? {scale: Object.freeze([...props.extent.scale]) as readonly [number, number]}
59
+ : {})
60
+ })
61
+ }
62
+ : {})
63
+ });
64
+ },
65
+ scalar<T extends GPUGraphScalarFormat>(format: T): GPUGraphScalarDescriptor<T> {
66
+ return Object.freeze({type: 'scalar', format});
67
+ },
68
+ vector<T extends GPUGraphVectorFormat>(
69
+ format: T,
70
+ shape: {length?: number; chunkLengths?: readonly number[]} = {}
71
+ ): GPUGraphVectorDescriptor<T> {
72
+ return Object.freeze({
73
+ type: 'vector',
74
+ format,
75
+ ...shape,
76
+ ...(shape.chunkLengths ? {chunkLengths: Object.freeze([...shape.chunkLengths])} : {})
77
+ });
78
+ }
79
+ };
80
+
16
81
  /** Backend-independent scalar state owned by a GPUGraph. */
17
82
  export class GPUGraphScalar<T extends GPUGraphScalarFormat = GPUGraphScalarFormat> {
18
83
  readonly id: string;
19
84
  readonly format: T;
20
- constructor(id: string, format: T) {
85
+ constructor(
86
+ id: string,
87
+ format: T,
88
+ readonly role?: GPUGraphValueRole
89
+ ) {
21
90
  if (!id) throw new Error('GPUGraphScalar id is required');
22
91
  this.id = id;
23
92
  this.format = format;
@@ -38,6 +107,7 @@ export class GPUGraphVector<T extends GPUGraphVectorFormat = GPUGraphVectorForma
38
107
  /** Optional fixed-length constraint; undefined means execution-sized. */
39
108
  readonly length?: number;
40
109
  readonly external: boolean;
110
+ readonly role?: GPUGraphValueRole;
41
111
  /** Optional physical partition lengths; omitted external topology is supplied by its binding. */
42
112
  readonly chunkLengths?: readonly number[];
43
113
  constructor(props: {
@@ -46,6 +116,7 @@ export class GPUGraphVector<T extends GPUGraphVectorFormat = GPUGraphVectorForma
46
116
  length?: number;
47
117
  external?: boolean;
48
118
  chunkLengths?: readonly number[];
119
+ role?: GPUGraphValueRole;
49
120
  }) {
50
121
  if (!props.id) throw new Error('GPUGraphVector id is required');
51
122
  if (props.length !== undefined && (!Number.isSafeInteger(props.length) || props.length < 0)) {
@@ -55,6 +126,7 @@ export class GPUGraphVector<T extends GPUGraphVectorFormat = GPUGraphVectorForma
55
126
  this.format = props.format;
56
127
  this.length = props.length;
57
128
  this.external = props.external ?? false;
129
+ this.role = props.role;
58
130
  if (props.chunkLengths) {
59
131
  if (
60
132
  props.chunkLengths.some(length => !Number.isSafeInteger(length) || length < 0) ||
@@ -72,8 +144,10 @@ export class GPUGraphVector<T extends GPUGraphVectorFormat = GPUGraphVectorForma
72
144
  export class GPUGraphTexture<Format extends TextureFormat = TextureFormat> {
73
145
  readonly id: string;
74
146
  readonly format: Format;
75
- readonly width: number;
76
- readonly height: number;
147
+ readonly width?: number;
148
+ readonly height?: number;
149
+ readonly role?: GPUGraphValueRole;
150
+ readonly descriptor?: GPUGraphTextureDescriptor<Format>;
77
151
  readonly usage: number;
78
152
  readonly dimension?: GraphTextureDimension;
79
153
  readonly depth?: number;
@@ -82,7 +156,17 @@ export class GPUGraphTexture<Format extends TextureFormat = TextureFormat> {
82
156
  /** Caller-owned storage, optionally bound at compilation or supplied when encoding. */
83
157
  readonly external: boolean;
84
158
 
85
- constructor(props: GPUGraphTextureProps<Format> & {id: string}) {
159
+ constructor(
160
+ props: Omit<GPUGraphTextureProps<Format>, 'width' | 'height'> & {
161
+ id: string;
162
+ width?: number;
163
+ height?: number;
164
+ role?: GPUGraphValueRole;
165
+ descriptor?: GPUGraphTextureDescriptor<Format>;
166
+ }
167
+ ) {
168
+ this.role = props.role;
169
+ this.descriptor = props.descriptor ? gpuGraph.texture(props.descriptor) : undefined;
86
170
  this.id = props.id;
87
171
  this.format = props.format;
88
172
  this.width = props.width;