@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
@@ -5,6 +5,8 @@
5
5
  import {CompiledGPUGraph} from './compiled-gpu-graph';
6
6
  import type {GPUGraphCompileOptions} from './gpu-expression-plan';
7
7
  import type {Device} from '@luma.gl/core';
8
+ import type {GPUVector} from '@luma.gl/graph/gpu-data';
9
+ import type {GPUGraphOutputs} from './gpu-texture-plan';
8
10
 
9
11
  import type {TextureFormat} from '@luma.gl/core';
10
12
  import type {GraphTextureViewProps} from './gpu-command-graph';
@@ -17,6 +19,11 @@ import {
17
19
  type GPUGraphOperation
18
20
  } from './gpu-operation';
19
21
  import {
22
+ type GPUGraphScalarDescriptor,
23
+ type GPUGraphTextureDescriptor,
24
+ type GPUGraphVectorDescriptor,
25
+ type GPUGraphValueDescriptor,
26
+ type GPUGraphValueRole,
20
27
  GPUGraphScalar,
21
28
  GPUGraphTexture,
22
29
  GPUGraphTextureView,
@@ -41,6 +48,66 @@ export class GPUGraph {
41
48
  this.addOperationLike(operation);
42
49
  return this;
43
50
  }
51
+ addInput<T extends GPUGraphScalarFormat>(
52
+ id: string,
53
+ descriptor: GPUGraphScalarDescriptor<T>
54
+ ): GPUGraphScalar<T>;
55
+ addInput<T extends GPUGraphVectorFormat>(
56
+ id: string,
57
+ descriptor: GPUGraphVectorDescriptor<T>
58
+ ): GPUGraphVector<T>;
59
+ addInput<Format extends TextureFormat>(
60
+ id: string,
61
+ descriptor: GPUGraphTextureDescriptor<Format>
62
+ ): GPUGraphTexture<Format>;
63
+ addInput(
64
+ id: string,
65
+ descriptor: GPUGraphValueDescriptor
66
+ ): GPUGraphScalar | GPUGraphVector | GPUGraphTexture {
67
+ return this.addDeclaration(id, descriptor, 'input');
68
+ }
69
+ /** State is initialized, borrowed vector storage supplied for every invocation. */
70
+ addState<T extends GPUGraphVectorFormat>(
71
+ id: string,
72
+ descriptor: GPUGraphVectorDescriptor<T>
73
+ ): GPUGraphVector<T> {
74
+ if (descriptor.type !== 'vector') throw new Error('Graph state requires vector storage');
75
+ return this.addDeclaration(id, descriptor, 'state') as GPUGraphVector<T>;
76
+ }
77
+ /** @internal */
78
+ addDeclaration(
79
+ id: string,
80
+ descriptor: GPUGraphValueDescriptor,
81
+ role?: GPUGraphValueRole
82
+ ): GPUGraphScalar | GPUGraphVector | GPUGraphTexture {
83
+ this.assertValueId(id);
84
+ if (descriptor.type === 'texture') {
85
+ const texture = new GPUGraphTexture({
86
+ id,
87
+ ...descriptor,
88
+ usage: 0,
89
+ role,
90
+ descriptor,
91
+ external: role === 'input',
92
+ ...('width' in (descriptor.extent ?? {}) ? descriptor.extent : {})
93
+ });
94
+ this.programTextures.set(id, texture);
95
+ return texture;
96
+ }
97
+ if (descriptor.type === 'scalar') {
98
+ const scalar = new GPUGraphScalar(id, descriptor.format, role);
99
+ this.programScalars.set(id, scalar);
100
+ return scalar;
101
+ }
102
+ const vector = new GPUGraphVector({
103
+ id,
104
+ ...descriptor,
105
+ role,
106
+ external: role === 'input' || role === 'state'
107
+ });
108
+ this.programVectors.set(id, vector);
109
+ return vector;
110
+ }
44
111
  scalar<T extends GPUGraphScalarFormat>(id: string, format: T): GPUGraphScalar<T> {
45
112
  this.assertValueId(id);
46
113
  const scalar = new GPUGraphScalar(id, format);
@@ -82,8 +149,43 @@ export class GPUGraph {
82
149
  }
83
150
  return new GPUGraphTextureView(texture, props);
84
151
  }
85
- compile(device: Device, options: GPUGraphCompileOptions = {}): CompiledGPUGraph {
86
- return new CompiledGPUGraph(device, this, options);
152
+ compile<Outputs extends GPUGraphOutputs = Record<string, GPUVector>>(
153
+ device: Device,
154
+ options: GPUGraphCompileOptions = {}
155
+ ): CompiledGPUGraph<Outputs> {
156
+ return new CompiledGPUGraph<Outputs>(device, this, options);
157
+ }
158
+ /** @internal */
159
+ constrainResult(vector: GPUGraphVector, descriptor: GPUGraphVectorDescriptor): GPUGraphVector {
160
+ if (
161
+ vector.length !== undefined &&
162
+ descriptor.length !== undefined &&
163
+ vector.length !== descriptor.length
164
+ )
165
+ throw new Error(`Incompatible result length ${vector.id}`);
166
+ if (
167
+ vector.chunkLengths &&
168
+ descriptor.chunkLengths &&
169
+ (vector.chunkLengths.length !== descriptor.chunkLengths.length ||
170
+ vector.chunkLengths.some((length, index) => length !== descriptor.chunkLengths![index]))
171
+ )
172
+ throw new Error(`Incompatible result chunks ${vector.id}`);
173
+ const constrained = new GPUGraphVector({
174
+ ...vector,
175
+ length: vector.length ?? descriptor.length,
176
+ chunkLengths: vector.chunkLengths ?? descriptor.chunkLengths
177
+ });
178
+ this.programVectors.set(vector.id, constrained);
179
+ return constrained;
180
+ }
181
+ /** @internal */
182
+ clone(): GPUGraph {
183
+ const program = new GPUGraph({id: this.id});
184
+ for (const [name, value] of this.programScalars) program.programScalars.set(name, value);
185
+ for (const [name, value] of this.programVectors) program.programVectors.set(name, value);
186
+ for (const [name, value] of this.programTextures) program.programTextures.set(name, value);
187
+ program.programOperations.push(...this.programOperations);
188
+ return program;
87
189
  }
88
190
  get textures(): readonly GPUGraphTexture[] {
89
191
  return Object.freeze([...this.programTextures.values()]);
@@ -3,7 +3,9 @@
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
5
  import type {DeviceFeature} from '@luma.gl/core';
6
+ import type {GraphTextureUsage} from './gpu-command-graph';
6
7
  import {GPUGraphScalar, GPUGraphVector, GPUGraphTexture} from './gpu-graph-value';
8
+ import type {GPUGraphValueDescriptor} from './gpu-graph-value';
7
9
  import type {GPUGraph} from './gpu-graph';
8
10
 
9
11
  /** A logical declaration or its name; never a bound buffer or command-graph view. */
@@ -17,13 +19,24 @@ export type GPUOperationVectorShape =
17
19
  export type GPUOperationValueUse = {
18
20
  value: GPUOperationValueReference;
19
21
  access: 'read' | 'write' | 'read-write';
22
+ /** Required texture access; drives allocation usage and binding validation. */
23
+ textureUsage?: GraphTextureUsage;
20
24
  /** Vector outputs only. Omitted shapes use the output declaration or supplied storage. */
21
25
  shape?: GPUOperationVectorShape;
22
26
  };
23
27
 
28
+ export type GPUOperationOutputDeclarations = Readonly<Record<string, GPUGraphValueDescriptor>>;
29
+ export type GPUOperationContractContext = {
30
+ getValue(name: string): GPUGraphValueDescriptor;
31
+ };
32
+
24
33
  /** Compiler-facing dependencies and outputs, separate from diagnostic metadata. */
25
34
  export type GPUOperationContract = {
26
35
  values: readonly GPUOperationValueUse[];
36
+ /** Logical results owned by this operation. Types may depend on earlier logical values. */
37
+ outputs?:
38
+ | GPUOperationOutputDeclarations
39
+ | ((context: GPUOperationContractContext) => GPUOperationOutputDeclarations);
27
40
  requiredFeatures?: readonly DeviceFeature[];
28
41
  };
29
42
 
@@ -57,6 +70,11 @@ export function validateGPUOperationContract(
57
70
  ): void {
58
71
  for (const use of contract.values) {
59
72
  const declaration = resolveGPUOperationValue(program, use.value);
73
+ if (declaration instanceof GPUGraphTexture && use.shape && 'like' in use.shape) {
74
+ if (!(resolveGPUOperationValue(program, use.shape.like) instanceof GPUGraphTexture))
75
+ throw new Error('Texture shape requires a texture source');
76
+ continue;
77
+ }
60
78
  if (!use.shape) continue;
61
79
  if (!(declaration instanceof GPUGraphVector) || use.access === 'read')
62
80
  throw new Error('Operation shapes require writable vectors');
@@ -2,6 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
 
5
+ import {setGPUComputeDispatchWorkgroups} from './gpu-command-dispatch-metadata';
5
6
  import {type GPUCommandNode, createGPUComputeCommandNode} from './gpu-command-node';
6
7
  import {type Binding, type Device} from '@luma.gl/core';
7
8
  import {Kernel} from '../execution/compute/kernel';
@@ -694,7 +695,13 @@ ${props.segmentFlags ? `var<workgroup> segmentScratch: array<u32, ${SCAN_WORKGRO
694
695
  })
695
696
  );
696
697
 
697
- return nodes;
698
+ return nodes.map(node =>
699
+ setGPUComputeDispatchWorkgroups(node, [
700
+ props.dispatchLayout.x,
701
+ props.dispatchLayout.y,
702
+ props.dispatchLayout.z
703
+ ])
704
+ );
698
705
  }
699
706
 
700
707
  /** Uses empirical adapter calibration when supplied, otherwise preserves capability selection. */
@@ -931,7 +938,13 @@ ${props.offsetSegmentPrefixes ? '@group(0) @binding(3) var<storage, read> offset
931
938
  })
932
939
  );
933
940
 
934
- return nodes;
941
+ return nodes.map(node =>
942
+ setGPUComputeDispatchWorkgroups(node, [
943
+ props.dispatchLayout.x,
944
+ props.dispatchLayout.y,
945
+ props.dispatchLayout.z
946
+ ])
947
+ );
935
948
  }
936
949
 
937
950
  type GPUScanDispatchLayout = GPUBoundedDispatchLayout;
@@ -0,0 +1,51 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {assert, type CommandEncoder} from '@luma.gl/core';
6
+ import {getTextureBindingInfo, type GPUGraphTextureBinding} from './gpu-texture-plan';
7
+
8
+ /** Bit-preserving copies of selected mip/layer ranges; no sampling or format conversion. @internal */
9
+ export function encodeTextureCopy(
10
+ encoder: CommandEncoder,
11
+ input: GPUGraphTextureBinding,
12
+ output: GPUGraphTextureBinding
13
+ ): void {
14
+ const source = getTextureBindingInfo(input);
15
+ const destination = getTextureBindingInfo(output);
16
+ validateTextureCopy(input, output);
17
+ for (let mipLevel = 0; mipLevel < source.mipLevels; mipLevel++) {
18
+ encoder.copyTextureToTexture({
19
+ sourceTexture: source.texture,
20
+ destinationTexture: destination.texture,
21
+ mipLevel: source.baseMipLevel + mipLevel,
22
+ destinationMipLevel: destination.baseMipLevel + mipLevel,
23
+ origin: [0, 0, source.baseArrayLayer],
24
+ destinationOrigin: [0, 0, destination.baseArrayLayer],
25
+ width: Math.max(1, source.width >> mipLevel),
26
+ height: Math.max(1, source.height >> mipLevel),
27
+ depthOrArrayLayers:
28
+ source.dimension === '3d' ? Math.max(1, source.depth >> mipLevel) : source.depth
29
+ });
30
+ }
31
+ }
32
+
33
+ /** Validate copy compatibility before encoding any commands. @internal */
34
+ export function validateTextureCopy(
35
+ input: GPUGraphTextureBinding,
36
+ output: GPUGraphTextureBinding
37
+ ): void {
38
+ const source = getTextureBindingInfo(input);
39
+ const destination = getTextureBindingInfo(output);
40
+ assert(
41
+ source.texture !== destination.texture &&
42
+ source.samples === 1 &&
43
+ destination.samples === 1 &&
44
+ source.format === destination.format &&
45
+ source.width === destination.width &&
46
+ source.height === destination.height &&
47
+ source.depth === destination.depth &&
48
+ source.mipLevels === destination.mipLevels &&
49
+ source.dimension === destination.dimension
50
+ );
51
+ }
@@ -0,0 +1,248 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {assert, Texture, TextureView, type Device, type TextureViewProps} from '@luma.gl/core';
6
+ import type {GPUVector} from '@luma.gl/graph/gpu-data';
7
+ import type {GPUGraphTexture, GPUGraphTextureDescriptor} from './gpu-graph-value';
8
+ import type {GPUOperationContract} from './gpu-operation-contract';
9
+ import {getGPUOperationValueName} from './gpu-operation-contract';
10
+ import type {GraphTextureUsage, GraphTextureViewProps} from './gpu-command-graph';
11
+
12
+ export type GPUGraphTextureBinding = Texture | TextureView;
13
+ export type GPUGraphOutput = GPUVector | GPUGraphTextureBinding;
14
+ export type GPUGraphOutputs = Record<string, GPUGraphOutput>;
15
+
16
+ /** Selected view geometry, without creating another view. @internal */
17
+ export function getTextureBindingInfo(binding: GPUGraphTextureBinding) {
18
+ const texture = binding instanceof TextureView ? binding.texture : binding;
19
+ const props: Partial<TextureViewProps> = binding instanceof TextureView ? binding.props : {};
20
+ const baseMipLevel = props.baseMipLevel ?? 0;
21
+ const baseArrayLayer = props.baseArrayLayer ?? 0;
22
+ const dimension = props.dimension ?? texture.dimension;
23
+ const mipLevels = props.mipLevelCount ?? texture.mipLevels - baseMipLevel;
24
+ const depth =
25
+ dimension === '3d'
26
+ ? Math.max(1, texture.depth >> baseMipLevel)
27
+ : (props.arrayLayerCount ??
28
+ (dimension === 'cube' ? 6 : dimension === '2d' ? 1 : texture.depth - baseArrayLayer));
29
+ // Format reinterpretation and partial aspects need separate binding/operation semantics.
30
+ assert((props.format ?? texture.format) === texture.format && (props.aspect ?? 'all') === 'all');
31
+ assert(!binding.destroyed && !texture.destroyed);
32
+ assert(
33
+ Number.isInteger(baseMipLevel) &&
34
+ baseMipLevel >= 0 &&
35
+ Number.isInteger(mipLevels) &&
36
+ mipLevels > 0 &&
37
+ baseMipLevel + mipLevels <= texture.mipLevels
38
+ );
39
+ assert(
40
+ Number.isInteger(baseArrayLayer) &&
41
+ baseArrayLayer >= 0 &&
42
+ Number.isInteger(depth) &&
43
+ depth > 0 &&
44
+ (dimension === '3d' ? baseArrayLayer === 0 : baseArrayLayer + depth <= texture.depth)
45
+ );
46
+ return {
47
+ texture,
48
+ format: texture.format,
49
+ dimension,
50
+ width: Math.max(1, texture.width >> baseMipLevel),
51
+ height: Math.max(1, texture.height >> baseMipLevel),
52
+ depth,
53
+ mipLevels,
54
+ samples: texture.samples,
55
+ baseMipLevel,
56
+ baseArrayLayer
57
+ };
58
+ }
59
+
60
+ /** Compose a logical view relative to an execution-supplied view. @internal */
61
+ export function getBoundTextureViewProps(
62
+ binding: TextureView,
63
+ props: GraphTextureViewProps = {}
64
+ ): GraphTextureViewProps {
65
+ const info = getTextureBindingInfo(binding);
66
+ const baseMipLevel = props.baseMipLevel ?? 0;
67
+ const baseArrayLayer = props.baseArrayLayer ?? 0;
68
+ const dimension = props.dimension ?? info.dimension;
69
+ const mipLevelCount = props.mipLevelCount ?? info.mipLevels - baseMipLevel;
70
+ const arrayLayerCount =
71
+ props.arrayLayerCount ??
72
+ (dimension === 'cube'
73
+ ? 6
74
+ : dimension === '2d' || dimension === '3d'
75
+ ? 1
76
+ : info.depth - baseArrayLayer);
77
+ // Nested selections cannot escape the caller-supplied view.
78
+ assert(
79
+ baseMipLevel >= 0 &&
80
+ baseMipLevel + mipLevelCount <= info.mipLevels &&
81
+ baseArrayLayer >= 0 &&
82
+ baseArrayLayer + arrayLayerCount <= info.depth
83
+ );
84
+ return {
85
+ ...props,
86
+ dimension,
87
+ baseMipLevel: info.baseMipLevel + baseMipLevel,
88
+ mipLevelCount,
89
+ baseArrayLayer: info.baseArrayLayer + baseArrayLayer,
90
+ arrayLayerCount
91
+ };
92
+ }
93
+
94
+ const TEXTURE_USAGE: Record<GraphTextureUsage, number> = {
95
+ sampled: Texture.SAMPLE,
96
+ 'storage-read': Texture.STORAGE,
97
+ 'storage-write': Texture.STORAGE,
98
+ 'storage-read-write': Texture.STORAGE,
99
+ 'render-attachment': Texture.RENDER,
100
+ 'copy-source': Texture.COPY_SRC,
101
+ 'copy-destination': Texture.COPY_DST
102
+ };
103
+
104
+ /** Per-invocation texture allocation and ownership, shared by independent executors. @internal */
105
+ export class GPUTextureInvocation {
106
+ readonly values = new Map<string, GPUGraphTextureBinding>();
107
+ private readonly owned = new Set<Texture>();
108
+ constructor(
109
+ device: Device,
110
+ declarations: readonly GPUGraphTexture[],
111
+ contracts: readonly GPUOperationContract[],
112
+ bindings: Readonly<Record<string, GPUVector | number | GPUGraphTextureBinding>>,
113
+ targets: Readonly<Record<string, GPUGraphOutput>>
114
+ ) {
115
+ const usages = new Map<string, number>();
116
+ const writable = new Set<string>();
117
+ for (const contract of contracts)
118
+ for (const use of contract.values) {
119
+ const name = getGPUOperationValueName(use.value);
120
+ if (!declarations.some(value => value.id === name)) continue;
121
+ // Every reusable texture access declares its physical purpose.
122
+ assert(use.textureUsage);
123
+ const usage = use.textureUsage;
124
+ assert(
125
+ (use.access === 'read' && ['sampled', 'storage-read', 'copy-source'].includes(usage)) ||
126
+ (use.access === 'write' &&
127
+ ['storage-write', 'render-attachment', 'copy-destination'].includes(usage)) ||
128
+ (use.access === 'read-write' &&
129
+ ['storage-read-write', 'render-attachment'].includes(usage))
130
+ );
131
+ usages.set(name, (usages.get(name) ?? 0) | TEXTURE_USAGE[usage]);
132
+ if (use.access !== 'read') writable.add(name);
133
+ }
134
+ try {
135
+ const pending = new Set(declarations);
136
+ while (pending.size) {
137
+ let progress = false;
138
+ for (const declaration of pending) {
139
+ const descriptor: GPUGraphTextureDescriptor = declaration.descriptor ?? {
140
+ type: 'texture',
141
+ format: declaration.format,
142
+ dimension: declaration.dimension,
143
+ extent:
144
+ declaration.width === undefined
145
+ ? undefined
146
+ : {width: declaration.width, height: declaration.height!, depth: declaration.depth},
147
+ mipLevels: declaration.mipLevels,
148
+ samples: declaration.samples
149
+ };
150
+ const extent = descriptor.extent;
151
+ const template = extent && 'like' in extent ? this.values.get(extent.like) : undefined;
152
+ if (extent && 'like' in extent && !template) continue;
153
+ const source = template ? getTextureBindingInfo(template) : undefined;
154
+ const scale = extent && 'like' in extent ? (extent.scale ?? [1, 1]) : [1, 1];
155
+ assert(scale.every(value => Number.isFinite(value) && value > 0));
156
+ const width = source
157
+ ? Math.max(1, Math.round(source.width * scale[0]))
158
+ : extent && 'width' in extent
159
+ ? extent.width
160
+ : undefined;
161
+ const height = source
162
+ ? Math.max(1, Math.round(source.height * scale[1]))
163
+ : extent && 'height' in extent
164
+ ? extent.height
165
+ : undefined;
166
+ const dimension = descriptor.dimension ?? source?.dimension ?? '2d';
167
+ // Cube results always contain six faces, including when sized from a 2d input.
168
+ const depth =
169
+ dimension === 'cube'
170
+ ? 6
171
+ : (source?.depth ?? (extent && 'depth' in extent ? extent.depth : undefined));
172
+ const mipLevels = descriptor.mipLevels ?? source?.mipLevels ?? 1;
173
+ const samples = descriptor.samples ?? source?.samples ?? 1;
174
+ const usage = (usages.get(declaration.id) ?? 0) | declaration.usage;
175
+ // Copy commands cannot read or write multisampled storage.
176
+ assert(samples === 1 || !(usage & (Texture.COPY_SRC | Texture.COPY_DST)));
177
+ let binding = targets[declaration.id] ?? bindings[declaration.id];
178
+ if (binding === undefined) {
179
+ // Input textures are always supplied; produced extents must be inferable before encoding.
180
+ assert(
181
+ declaration.role !== 'input' &&
182
+ !declaration.external &&
183
+ width !== undefined &&
184
+ height !== undefined &&
185
+ usage !== 0
186
+ );
187
+ assert(
188
+ Number.isSafeInteger(width) && width > 0 && Number.isSafeInteger(height) && height > 0
189
+ );
190
+ const texture = device.createTexture({
191
+ id: declaration.id,
192
+ format: descriptor.format,
193
+ dimension,
194
+ width,
195
+ height,
196
+ depth: depth ?? 1,
197
+ mipLevels,
198
+ samples,
199
+ usage: usage | (samples === 1 ? Texture.COPY_SRC : 0) | Texture.SAMPLE
200
+ });
201
+ this.owned.add(texture);
202
+ binding = texture;
203
+ }
204
+ assert(binding instanceof Texture || binding instanceof TextureView);
205
+ const info = getTextureBindingInfo(binding);
206
+ assert(info.samples === 1 || !(usage & (Texture.COPY_SRC | Texture.COPY_DST)));
207
+ assert(
208
+ info.texture.device === device &&
209
+ info.format === descriptor.format &&
210
+ info.dimension === dimension &&
211
+ (width === undefined || info.width === width) &&
212
+ (height === undefined || info.height === height) &&
213
+ (depth === undefined || info.depth === depth) &&
214
+ ((descriptor.mipLevels === undefined && declaration.role === 'input') ||
215
+ info.mipLevels === mipLevels) &&
216
+ ((descriptor.samples === undefined && declaration.role === 'input') ||
217
+ info.samples === samples) &&
218
+ (info.texture.props.usage & usage) === usage
219
+ );
220
+ this.values.set(declaration.id, binding);
221
+ pending.delete(declaration);
222
+ progress = true;
223
+ }
224
+ assert(progress); // Missing or cyclic texture extent dependency.
225
+ }
226
+ // Conservative alias policy: writable values cannot share backing storage with another value.
227
+ for (const name of writable) {
228
+ const texture = getTextureBindingInfo(this.values.get(name)!).texture;
229
+ for (const [otherName, value] of this.values)
230
+ assert(name === otherName || getTextureBindingInfo(value).texture !== texture);
231
+ }
232
+ } catch (error) {
233
+ this.destroy();
234
+ throw error;
235
+ }
236
+ }
237
+ finish(outputs: readonly string[]): void {
238
+ for (const name of outputs) {
239
+ const value = this.values.get(name);
240
+ if (value instanceof Texture) this.owned.delete(value);
241
+ }
242
+ this.destroy();
243
+ }
244
+ destroy(): void {
245
+ for (const texture of this.owned) texture.destroy();
246
+ this.owned.clear();
247
+ }
248
+ }
@@ -0,0 +1,102 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {encodeTextureCopy} from './gpu-texture-copy';
6
+ import {assert, Buffer} from '@luma.gl/core';
7
+ import type {GPUOperationLoweringContext} from './gpu-graph-lowering';
8
+ import {GPUUploadOperation, GPUCopyOperation} from './gpu-transfer-operation';
9
+
10
+ /** WebGPU transfers participate in command ordering and caller-owned encoding. @internal */
11
+ export function lowerGPUUpload<Parameters>(
12
+ operation: GPUUploadOperation,
13
+ context: GPUOperationLoweringContext<Parameters>
14
+ ): void {
15
+ const output = context.resolveVector(operation.output);
16
+ const chunks = operation.getData();
17
+ for (const [index, destination] of output.data.entries()) {
18
+ if (!destination.length) continue;
19
+ const data = chunks[index];
20
+ context.graph.addCopyPass({
21
+ id: `${operation.id}-${index}`,
22
+ resources: [{buffer: destination, usage: 'copy-destination'}],
23
+ compile: ({device}) => {
24
+ const staging = device.createBuffer({data, usage: Buffer.COPY_SRC});
25
+ return {
26
+ encode: ({commandEncoder, getBuffer}) => {
27
+ const packed = destination.byteStride === destination.rowByteLength;
28
+ for (let row = 0; row < destination.length; row += packed ? destination.length : 1) {
29
+ commandEncoder.copyBufferToBuffer({
30
+ sourceBuffer: staging,
31
+ sourceOffset: row * destination.rowByteLength,
32
+ destinationBuffer: getBuffer(destination),
33
+ destinationOffset: destination.byteOffset + row * destination.byteStride,
34
+ size: packed ? data.byteLength : destination.rowByteLength
35
+ });
36
+ }
37
+ },
38
+ destroy: () => staging.destroy()
39
+ };
40
+ }
41
+ });
42
+ }
43
+ }
44
+
45
+ /** @internal */
46
+ export function lowerGPUCopy<Parameters>(
47
+ operation: GPUCopyOperation,
48
+ context: GPUOperationLoweringContext<Parameters>
49
+ ): void {
50
+ const input = context.resolveVector(operation.input);
51
+ const output = context.resolveVector(operation.output);
52
+ // Copy preserves each chunk and row; no repartitioning or format conversion.
53
+ assert(input.format === output.format && input.data.length === output.data.length);
54
+ for (const [index, source] of input.data.entries()) {
55
+ const destination = output.data[index];
56
+ assert(source.length === destination.length);
57
+ if (!source.length) continue;
58
+ context.graph.addCopyPass({
59
+ id: `${operation.id}-${index}`,
60
+ resources: [
61
+ {buffer: source, usage: 'copy-source'},
62
+ {buffer: destination, usage: 'copy-destination'}
63
+ ],
64
+ compile: () => ({
65
+ encode: ({commandEncoder, getBuffer}) => {
66
+ const packed =
67
+ source.byteStride === source.rowByteLength &&
68
+ destination.byteStride === destination.rowByteLength;
69
+ for (let row = 0; row < source.length; row += packed ? source.length : 1) {
70
+ commandEncoder.copyBufferToBuffer({
71
+ sourceBuffer: getBuffer(source),
72
+ sourceOffset: source.byteOffset + row * source.byteStride,
73
+ destinationBuffer: getBuffer(destination),
74
+ destinationOffset: destination.byteOffset + row * destination.byteStride,
75
+ size: source.rowByteLength * (packed ? source.length : 1)
76
+ });
77
+ }
78
+ }
79
+ })
80
+ });
81
+ }
82
+ }
83
+
84
+ /** Texture copies use the same operation and preserve selected mip/layer ranges. @internal */
85
+ export function lowerGPUTextureCopy<Parameters>(
86
+ operation: GPUCopyOperation,
87
+ context: GPUOperationLoweringContext<Parameters>
88
+ ): void {
89
+ const input = context.resolveTextureView(operation.input);
90
+ const output = context.resolveTextureView(operation.output);
91
+ context.graph.addCopyPass({
92
+ id: operation.id,
93
+ resources: [
94
+ {texture: input, usage: 'copy-source'},
95
+ {texture: output, usage: 'copy-destination'}
96
+ ],
97
+ compile: () => ({
98
+ encode: ({commandEncoder, getTextureView}) =>
99
+ encodeTextureCopy(commandEncoder, getTextureView(input), getTextureView(output))
100
+ })
101
+ });
102
+ }