@luma.gl/graph 10.0.0-beta.3 → 10.0.0-beta.4

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 (125) hide show
  1. package/dist/arrow/gpu/arrow-gpu-table-conversion.cjs +66 -30
  2. package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts +7 -1
  3. package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts.map +1 -1
  4. package/dist/arrow/gpu/arrow-gpu-table-conversion.js +78 -32
  5. package/dist/arrow/gpu/arrow-gpu-table-conversion.js.map +1 -1
  6. package/dist/arrow/gpu/arrow-gpu-vector-conversion.cjs +7 -0
  7. package/dist/arrow/gpu/arrow-gpu-vector-conversion.d.ts +2 -0
  8. package/dist/arrow/gpu/arrow-gpu-vector-conversion.d.ts.map +1 -1
  9. package/dist/arrow/gpu/arrow-gpu-vector-conversion.js +14 -0
  10. package/dist/arrow/gpu/arrow-gpu-vector-conversion.js.map +1 -1
  11. package/dist/arrow/index.cjs.map +2 -2
  12. package/dist/arrow/vectors/arrow-temporal-gpu-vector.cjs +1 -2
  13. package/dist/arrow/vectors/arrow-temporal-gpu-vector.js +2 -2
  14. package/dist/arrow/vectors/arrow-temporal-gpu-vector.js.map +1 -1
  15. package/dist/dist.dev.js +62 -27
  16. package/dist/dist.min.js +48 -47
  17. package/dist/gpu-core/gpu-command-graph-budget-controller.cjs +10 -2
  18. package/dist/gpu-core/gpu-command-graph-budget-controller.d.ts +4 -0
  19. package/dist/gpu-core/gpu-command-graph-budget-controller.d.ts.map +1 -1
  20. package/dist/gpu-core/gpu-command-graph-budget-controller.js +10 -2
  21. package/dist/gpu-core/gpu-command-graph-budget-controller.js.map +1 -1
  22. package/dist/gpu-core/gpu-group-aggregation.cjs +45 -21
  23. package/dist/gpu-core/gpu-group-aggregation.d.ts +4 -0
  24. package/dist/gpu-core/gpu-group-aggregation.d.ts.map +1 -1
  25. package/dist/gpu-core/gpu-group-aggregation.js +58 -24
  26. package/dist/gpu-core/gpu-group-aggregation.js.map +1 -1
  27. package/dist/gpu-core/index.cjs.map +1 -1
  28. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.cjs +6 -2
  29. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.d.ts +2 -1
  30. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.d.ts.map +1 -1
  31. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.js +6 -2
  32. package/dist/gpu-dataframe/gpu-global-aggregation-compiler.js.map +1 -1
  33. package/dist/gpu-dataframe/gpu-global-sort-compiler.cjs +61 -54
  34. package/dist/gpu-dataframe/gpu-global-sort-compiler.d.ts +6 -1
  35. package/dist/gpu-dataframe/gpu-global-sort-compiler.d.ts.map +1 -1
  36. package/dist/gpu-dataframe/gpu-global-sort-compiler.js +61 -55
  37. package/dist/gpu-dataframe/gpu-global-sort-compiler.js.map +1 -1
  38. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.cjs +88 -13
  39. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.d.ts +11 -4
  40. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.d.ts.map +1 -1
  41. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.js +96 -16
  42. package/dist/gpu-dataframe/gpu-group-aggregation-compiler.js.map +1 -1
  43. package/dist/gpu-dataframe/gpu-group-by-query.cjs +12 -3
  44. package/dist/gpu-dataframe/gpu-group-by-query.d.ts +8 -1
  45. package/dist/gpu-dataframe/gpu-group-by-query.d.ts.map +1 -1
  46. package/dist/gpu-dataframe/gpu-group-by-query.js +13 -3
  47. package/dist/gpu-dataframe/gpu-group-by-query.js.map +1 -1
  48. package/dist/gpu-dataframe/gpu-join-compiler.cjs +53 -2
  49. package/dist/gpu-dataframe/gpu-join-compiler.d.ts +9 -1
  50. package/dist/gpu-dataframe/gpu-join-compiler.d.ts.map +1 -1
  51. package/dist/gpu-dataframe/gpu-join-compiler.js +51 -0
  52. package/dist/gpu-dataframe/gpu-join-compiler.js.map +1 -1
  53. package/dist/gpu-dataframe/gpu-query-compiler.cjs +24 -0
  54. package/dist/gpu-dataframe/gpu-query-compiler.d.ts +3 -1
  55. package/dist/gpu-dataframe/gpu-query-compiler.d.ts.map +1 -1
  56. package/dist/gpu-dataframe/gpu-query-compiler.js +25 -0
  57. package/dist/gpu-dataframe/gpu-query-compiler.js.map +1 -1
  58. package/dist/gpu-dataframe/index.cjs.map +3 -3
  59. package/dist/gpu-graph/index.cjs.map +1 -1
  60. package/dist/gpu-graph-algorithms/benchmarks.cjs.map +2 -2
  61. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.cjs +182 -62
  62. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.d.ts +29 -2
  63. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.d.ts.map +1 -1
  64. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.js +200 -64
  65. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.js.map +1 -1
  66. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.cjs +135 -9
  67. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.d.ts +55 -8
  68. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.d.ts.map +1 -1
  69. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.js +145 -14
  70. package/dist/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.js.map +1 -1
  71. package/dist/gpu-graph-algorithms/index.cjs.map +3 -3
  72. package/dist/gpu-physics/articulation-tree-shader.cjs +177 -0
  73. package/dist/gpu-physics/articulation-tree-shader.d.ts +3 -0
  74. package/dist/gpu-physics/articulation-tree-shader.d.ts.map +1 -0
  75. package/dist/gpu-physics/articulation-tree-shader.js +159 -0
  76. package/dist/gpu-physics/articulation-tree-shader.js.map +1 -0
  77. package/dist/gpu-physics/gpu-articulation-tree.cjs +203 -0
  78. package/dist/gpu-physics/gpu-articulation-tree.d.ts +81 -0
  79. package/dist/gpu-physics/gpu-articulation-tree.d.ts.map +1 -0
  80. package/dist/gpu-physics/gpu-articulation-tree.js +205 -0
  81. package/dist/gpu-physics/gpu-articulation-tree.js.map +1 -0
  82. package/dist/gpu-physics/gpu-rigid-body-world.cjs +62 -0
  83. package/dist/gpu-physics/gpu-rigid-body-world.d.ts +12 -0
  84. package/dist/gpu-physics/gpu-rigid-body-world.d.ts.map +1 -1
  85. package/dist/gpu-physics/gpu-rigid-body-world.js +76 -0
  86. package/dist/gpu-physics/gpu-rigid-body-world.js.map +1 -1
  87. package/dist/gpu-physics/index.cjs +3 -0
  88. package/dist/gpu-physics/index.cjs.map +3 -3
  89. package/dist/gpu-physics/index.d.ts +1 -0
  90. package/dist/gpu-physics/index.d.ts.map +1 -1
  91. package/dist/gpu-physics/index.js +1 -0
  92. package/dist/gpu-physics/index.js.map +1 -1
  93. package/dist/gpu-physics/rigid-body-joints.d.ts +2 -0
  94. package/dist/gpu-physics/rigid-body-joints.d.ts.map +1 -1
  95. package/dist/gpu-physics/rigid-body-joints.js.map +1 -1
  96. package/dist/gpu-project/graph.cjs.map +2 -2
  97. package/dist/gpu-simple-graph/index.cjs.map +2 -2
  98. package/dist/gpu-sql/gpu-sql.cjs +167 -47
  99. package/dist/gpu-sql/gpu-sql.d.ts +6 -1
  100. package/dist/gpu-sql/gpu-sql.d.ts.map +1 -1
  101. package/dist/gpu-sql/gpu-sql.js +187 -47
  102. package/dist/gpu-sql/gpu-sql.js.map +1 -1
  103. package/dist/gpu-sql/index.cjs.map +3 -3
  104. package/dist/index.cjs.map +1 -1
  105. package/package.json +8 -8
  106. package/src/arrow/gpu/arrow-gpu-table-conversion.ts +94 -40
  107. package/src/arrow/gpu/arrow-gpu-vector-conversion.ts +19 -0
  108. package/src/arrow/vectors/arrow-temporal-gpu-vector.ts +2 -2
  109. package/src/gpu-core/gpu-command-graph-budget-controller.ts +22 -2
  110. package/src/gpu-core/gpu-group-aggregation.ts +68 -30
  111. package/src/gpu-dataframe/gpu-global-aggregation-compiler.ts +9 -2
  112. package/src/gpu-dataframe/gpu-global-sort-compiler.ts +95 -78
  113. package/src/gpu-dataframe/gpu-group-aggregation-compiler.ts +191 -24
  114. package/src/gpu-dataframe/gpu-group-by-query.ts +27 -4
  115. package/src/gpu-dataframe/gpu-join-compiler.ts +75 -0
  116. package/src/gpu-dataframe/gpu-query-compiler.ts +41 -1
  117. package/src/gpu-graph-algorithms/README.md +4 -2
  118. package/src/gpu-graph-algorithms/gpu-graph-single-source-shortest-path-internals.ts +248 -66
  119. package/src/gpu-graph-algorithms/gpu-graph-single-source-shortest-path.ts +218 -20
  120. package/src/gpu-physics/articulation-tree-shader.ts +169 -0
  121. package/src/gpu-physics/gpu-articulation-tree.ts +279 -0
  122. package/src/gpu-physics/gpu-rigid-body-world.ts +89 -0
  123. package/src/gpu-physics/index.ts +7 -0
  124. package/src/gpu-physics/rigid-body-joints.ts +2 -0
  125. package/src/gpu-sql/gpu-sql.ts +248 -57
@@ -49,7 +49,10 @@ const LU_QUERY_WORKGROUP_SIZE = 256;
49
49
 
50
50
  /** Borrows projected columns while the query owns its working table. @internal */
51
51
  export function projectGPUDataFrameQueryResult<Selection extends GPUTypeMap>(
52
- context: GPUDataFrameQueryExtensionContext<Selection>,
52
+ context: Pick<
53
+ GPUDataFrameQueryExtensionContext<Selection>,
54
+ 'table' | 'validity' | 'dictionaries'
55
+ >,
53
56
  columns: readonly (keyof Selection & string)[]
54
57
  ): GPUDataFrame<Selection> {
55
58
  const working = new GPUDataFrame<Selection>({
@@ -690,6 +693,43 @@ function addGPUQueryControlUpload(
690
693
  });
691
694
  }
692
695
 
696
+ /** Adds a predicate on an already prepared graph table into a caller-supplied mask. @internal */
697
+ export function addGPUDataFrameResultPredicate<T extends GPUTypeMap>(
698
+ graph: GPUCommandGraph<GPUDataFrameQueryParameters>,
699
+ queryId: string,
700
+ source: GPUDataFrame<T>,
701
+ predicate: GPUExpression<boolean, string>,
702
+ output: GraphVectorView<'uint32'>
703
+ ): void {
704
+ const plan = makeGPUQueryExpressionShaderPlan(source, [predicate], [], []);
705
+ validateGPUQueryBindingCapacity(plan, graph);
706
+ const sourceViews = importGPUQuerySourceViews(graph, source, plan, queryId);
707
+ const controls =
708
+ plan.controls.length && output.length
709
+ ? createTransientView(
710
+ graph,
711
+ queryId + '-controls',
712
+ 'uint32',
713
+ plan.controls.length * 2,
714
+ Buffer.STORAGE | Buffer.COPY_DST
715
+ )
716
+ : undefined;
717
+ if (controls) addGPUQueryControlUpload(graph, queryId, controls, plan);
718
+ for (const [batchIndex, chunk] of output.data.entries()) {
719
+ if (!chunk.length) continue;
720
+ addGPUQueryPredicatePass(graph, {
721
+ id: queryId + '-batch-' + batchIndex,
722
+ batchIndex,
723
+ columns: plan.columns,
724
+ sourceViews,
725
+ derivedViews: new Map(),
726
+ controls,
727
+ output: chunk,
728
+ plan
729
+ });
730
+ }
731
+ }
732
+
693
733
  /** Emits one closed-AST expression kernel for one preserved, nonempty source record batch. */
694
734
  function addGPUQueryPredicatePass(
695
735
  graph: GPUCommandGraph<GPUDataFrameQueryParameters>,
@@ -81,8 +81,10 @@ extends beyond the standard and does not become a seventh official workload.
81
81
  one chosen vertex. Existing nonnegative `float32` edge weights determine route cost; missing
82
82
  weights mean every relationship costs one. Unreachable vertices receive `+Infinity`, equal-cost
83
83
  routes choose fewer hops and then the lowest stable predecessor, and invalid or negative weights
84
- fail closed. Bounded synchronized relaxation and an optional GPU convergence flag keep the actual
85
- iteration budget visible; they do not imply automatic convergence or support negative weights.
84
+ fail closed. Synchronized relaxation stops on the GPU in the first round that changes nothing;
85
+ `run()` continues in batches past the 1,024 rounds that one command graph encodes and reports
86
+ `converged` and `iterations`. Caller-owned `seeds` let one compiled search serve any source, or
87
+ several sources at once. Negative weights are not supported.
86
88
 
87
89
  `GPUGraphAlgorithmsLocalClusteringCoefficient` asks whether each vertex's distinct incoming-or-outgoing
88
90
  neighbors also connect to one another. Directed graphs count distinct directed links among those
@@ -3,10 +3,11 @@
3
3
  // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
4
  // SPDX-FileComment: Independently implemented for WebGPU; inspired by NVIDIA RAPIDS cuGraph.
5
5
 
6
- import {type Binding} from '@luma.gl/core';
6
+ import {Buffer, type Binding} from '@luma.gl/core';
7
7
  import {Kernel} from '../execution/compute/kernel';
8
8
  import type {
9
9
  GPUCommandGraph,
10
+ GPUCommandGraphGPUIndirectCondition,
10
11
  GraphBufferUse,
11
12
  GraphDataView,
12
13
  GraphVectorView
@@ -28,6 +29,26 @@ const SHORTEST_PATH_WORKGROUP_SIZE = 256;
28
29
  const POSITIVE_INFINITY_BITS = 0x7f800000;
29
30
  const UNREACHABLE_PREDECESSOR = 0xffffffff;
30
31
 
32
+ /**
33
+ * Layout of the uint32 control record shared by every round. Elements 0-2 are the indirect
34
+ * dispatch command of each round pass; x is zero once the search stops. @internal
35
+ */
36
+ export const SHORTEST_PATH_CONTROL_DISPATCH_X = 0;
37
+ /** Control element counting the relaxation rounds that ran. @internal */
38
+ export const SHORTEST_PATH_CONTROL_ITERATIONS = 3;
39
+ /** Control element that is nonzero when invalid weights or adjacency overflow stop the search. @internal */
40
+ export const SHORTEST_PATH_CONTROL_FAILED = 4;
41
+ /** Control element mirroring the convergence status. @internal */
42
+ export const SHORTEST_PATH_CONTROL_CONVERGED = 5;
43
+ /** Byte length of the control record. @internal */
44
+ export const SHORTEST_PATH_CONTROL_BYTE_LENGTH = 32;
45
+
46
+ /** Buffers that must survive between the separately compiled graphs of a batched run. @internal */
47
+ export type GPUGraphAlgorithmsShortestPathPersistentState = {
48
+ /** Control record with `STORAGE | INDIRECT` usage. */
49
+ control: Buffer;
50
+ };
51
+
31
52
  type ShortestPathDataView = GraphDataView<'uint32'> | GraphDataView<'float32'>;
32
53
 
33
54
  type ImportedShortestPathAdjacency = {
@@ -41,12 +62,15 @@ type ImportedShortestPathState = {
41
62
  id: string;
42
63
  vertexCount: number;
43
64
  edgeCount: number;
44
- sourceVertex: number;
45
65
  maxIterations: number;
46
66
  distances: GraphDataView<'float32'>;
47
67
  predecessors: GraphDataView<'uint32'>;
48
68
  converged: GraphDataView<'uint32'>;
49
69
  invalidWeightCount: GraphDataView<'uint32'>;
70
+ control: GraphDataView<'uint32'>;
71
+ roundCondition: GPUCommandGraphGPUIndirectCondition;
72
+ useIncoming: boolean;
73
+ seeds?: GraphDataView<'uint32'>;
50
74
  previousDistances?: GraphDataView<'float32'>;
51
75
  changed?: GraphDataView<'uint32'>;
52
76
  sourceVertices?: GraphVectorView<'uint32'>;
@@ -68,14 +92,65 @@ type ShortestPathPassProps = {
68
92
  source: string;
69
93
  bindings: Record<string, ShortestPathBinding>;
70
94
  dispatchLayout: GPUBoundedDispatchLayout;
95
+ condition?: GPUCommandGraphGPUIndirectCondition;
96
+ /** Vertex uploaded to a node-owned `seeds` buffer bound after the graph bindings. */
97
+ ownedSeed?: number;
71
98
  };
72
99
 
73
- /** Adds GPU weighted Bellman-Ford passes using an explicit bounded dispatch limit. @internal */
100
+ /**
101
+ * Adds initialization and `roundCount` GPU-gated weighted Bellman-Ford rounds to one graph,
102
+ * using an explicit bounded dispatch limit. `roundCount` defaults to `min(maxIterations, 1024)`.
103
+ * @internal
104
+ */
74
105
  export function addGPUGraphAlgorithmsSingleSourceShortestPathToGraphWithDispatchLimit<Parameters>(
75
106
  search: GPUGraphAlgorithmsSingleSourceShortestPath,
76
107
  commandGraph: GPUCommandGraph<Parameters>,
77
- maxComputeWorkgroupsPerDimension: number
108
+ maxComputeWorkgroupsPerDimension: number,
109
+ roundCount = Math.min(search.maxIterations, 1024)
110
+ ): void {
111
+ const state = importShortestPathState(search, commandGraph, maxComputeWorkgroupsPerDimension, {
112
+ rounds: roundCount > 0
113
+ });
114
+ addInitialization(commandGraph, state, search);
115
+ addRounds(commandGraph, state, roundCount, 0);
116
+ }
117
+
118
+ /** Adds only the reset, weight validation, status, and seed passes of a batched run. @internal */
119
+ export function addGPUGraphAlgorithmsShortestPathInitializationToGraph<Parameters>(
120
+ search: GPUGraphAlgorithmsSingleSourceShortestPath,
121
+ commandGraph: GPUCommandGraph<Parameters>,
122
+ maxComputeWorkgroupsPerDimension: number,
123
+ persistent: GPUGraphAlgorithmsShortestPathPersistentState
124
+ ): void {
125
+ const state = importShortestPathState(search, commandGraph, maxComputeWorkgroupsPerDimension, {
126
+ rounds: false,
127
+ persistent
128
+ });
129
+ addInitialization(commandGraph, state, search);
130
+ }
131
+
132
+ /** Adds `roundCount` GPU-gated rounds that continue from the current distances. @internal */
133
+ export function addGPUGraphAlgorithmsShortestPathRoundsToGraph<Parameters>(
134
+ search: GPUGraphAlgorithmsSingleSourceShortestPath,
135
+ commandGraph: GPUCommandGraph<Parameters>,
136
+ maxComputeWorkgroupsPerDimension: number,
137
+ roundCount: number,
138
+ persistent: GPUGraphAlgorithmsShortestPathPersistentState
78
139
  ): void {
140
+ const state = importShortestPathState(search, commandGraph, maxComputeWorkgroupsPerDimension, {
141
+ rounds: true,
142
+ persistent
143
+ });
144
+ addRounds(commandGraph, state, roundCount, 0);
145
+ }
146
+
147
+ /** Imports caller-owned vectors and declares the scratch and control views of one graph. */
148
+ function importShortestPathState<Parameters>(
149
+ search: GPUGraphAlgorithmsSingleSourceShortestPath,
150
+ commandGraph: GPUCommandGraph<Parameters>,
151
+ maxComputeWorkgroupsPerDimension: number,
152
+ options: {rounds: boolean; persistent?: GPUGraphAlgorithmsShortestPathPersistentState}
153
+ ): ImportedShortestPathState {
79
154
  const directed = search.topology.graph.directed;
80
155
  const useIncoming = directed && search.direction === 'incoming';
81
156
  const primaryAdjacency = importShortestPathAdjacency(
@@ -83,12 +158,34 @@ export function addGPUGraphAlgorithmsSingleSourceShortestPathToGraphWithDispatch
83
158
  `${search.id}-${useIncoming ? 'incoming' : 'outgoing'}`,
84
159
  useIncoming ? search.topology.reverse! : search.topology.forward
85
160
  );
161
+ const controlDescriptor = {
162
+ id: `${search.id}-control`,
163
+ byteLength: SHORTEST_PATH_CONTROL_BYTE_LENGTH,
164
+ usage: Buffer.STORAGE | Buffer.INDIRECT | Buffer.COPY_SRC
165
+ };
166
+ const controlBuffer = options.persistent
167
+ ? commandGraph.importBuffer(controlDescriptor, options.persistent.control)
168
+ : commandGraph.createTransientBuffer(controlDescriptor);
86
169
  const state: ImportedShortestPathState = {
87
170
  id: search.id,
88
171
  vertexCount: search.topology.graph.vertexCount,
89
172
  edgeCount: search.topology.graph.edgeCount,
90
- sourceVertex: search.sourceVertex,
91
173
  maxIterations: search.maxIterations,
174
+ useIncoming,
175
+ ...(search.seeds
176
+ ? {seeds: commandGraph.importGPUVector(`${search.id}-seeds`, search.seeds).data[0]}
177
+ : {}),
178
+ control: commandGraph.createDataView(controlBuffer, {
179
+ format: 'uint32',
180
+ length: SHORTEST_PATH_CONTROL_BYTE_LENGTH / Uint32Array.BYTES_PER_ELEMENT
181
+ }),
182
+ roundCondition: {
183
+ id: `${search.id}-round-active`,
184
+ source: 'gpu',
185
+ mode: 'indirect',
186
+ buffer: controlBuffer,
187
+ byteOffset: SHORTEST_PATH_CONTROL_DISPATCH_X * Uint32Array.BYTES_PER_ELEMENT
188
+ },
92
189
  distances: commandGraph.importGPUVector(`${search.id}-distances`, search.distances).data[0],
93
190
  predecessors: commandGraph.importGPUVector(`${search.id}-predecessors`, search.predecessors)
94
191
  .data[0],
@@ -99,7 +196,7 @@ export function addGPUGraphAlgorithmsSingleSourceShortestPathToGraphWithDispatch
99
196
  ? commandGraph.importGPUVector(`${search.id}-invalid-weight-count`, search.invalidWeightCount)
100
197
  .data[0]
101
198
  : createTransientView(commandGraph, `${search.id}-invalid-weight-scratch`, 'uint32', 1),
102
- ...(search.maxIterations > 0
199
+ ...(options.rounds
103
200
  ? {
104
201
  previousDistances: createTransientView(
105
202
  commandGraph,
@@ -138,7 +235,15 @@ export function addGPUGraphAlgorithmsSingleSourceShortestPathToGraphWithDispatch
138
235
  : {}),
139
236
  maxComputeWorkgroupsPerDimension
140
237
  };
238
+ return state;
239
+ }
141
240
 
241
+ /** Resets outputs, validates weights, publishes the control record, and places the seeds. */
242
+ function addInitialization<Parameters>(
243
+ commandGraph: GPUCommandGraph<Parameters>,
244
+ state: ImportedShortestPathState,
245
+ search: GPUGraphAlgorithmsSingleSourceShortestPath
246
+ ): void {
142
247
  addInitializationPass(commandGraph, state);
143
248
  for (const [chunkIndex, weights] of state.sourceWeights?.data.entries() ?? []) {
144
249
  if (weights.length > 0) {
@@ -151,9 +256,19 @@ export function addGPUGraphAlgorithmsSingleSourceShortestPathToGraphWithDispatch
151
256
  });
152
257
  }
153
258
  }
154
- addSourcePass(commandGraph, state);
259
+ addStatusPass(commandGraph, state);
260
+ addSeedPass(commandGraph, state, search);
261
+ }
155
262
 
156
- for (let iteration = 0; iteration < state.maxIterations; iteration++) {
263
+ /** Adds GPU-gated rounds; `firstIteration` only names the nodes. */
264
+ function addRounds<Parameters>(
265
+ commandGraph: GPUCommandGraph<Parameters>,
266
+ state: ImportedShortestPathState,
267
+ roundCount: number,
268
+ firstIteration: number
269
+ ): void {
270
+ const useIncoming = state.useIncoming;
271
+ for (let iteration = firstIteration; iteration < firstIteration + roundCount; iteration++) {
157
272
  addSnapshotPass(commandGraph, state, iteration);
158
273
  addRelaxationPass(commandGraph, {
159
274
  state,
@@ -215,14 +330,12 @@ function addInitializationPass<Parameters>(
215
330
  const bindings: Record<string, ShortestPathBinding> = {
216
331
  distances: {view: state.distances, usage: 'storage-write', atomic: true},
217
332
  predecessors: {view: state.predecessors, usage: 'storage-write', atomic: true},
218
- converged: {view: state.converged, usage: 'storage-write', atomic: true},
219
333
  invalidWeightCount: {view: state.invalidWeightCount, usage: 'storage-write', atomic: true}
220
334
  };
221
335
  const source = /* wgsl */ `
222
336
  const VERTEX_COUNT: u32 = ${state.vertexCount}u;
223
337
  const DISTANCES_OFFSET: u32 = ${getViewElementOffset(state.distances)}u;
224
338
  const PREDECESSORS_OFFSET: u32 = ${getViewElementOffset(state.predecessors)}u;
225
- const CONVERGED_OFFSET: u32 = ${getViewElementOffset(state.converged)}u;
226
339
  const INVALID_WEIGHT_COUNT_OFFSET: u32 = ${getViewElementOffset(state.invalidWeightCount)}u;
227
340
  ${getBindingDeclarations(bindings)}
228
341
 
@@ -233,7 +346,6 @@ fn main(
233
346
  ) {
234
347
  ${getInvocationIndexSource(state, Math.max(state.vertexCount, 1))}
235
348
  if (index == 0u) {
236
- atomicStore(&converged[CONVERGED_OFFSET], 0u);
237
349
  atomicStore(&invalidWeightCount[INVALID_WEIGHT_COUNT_OFFSET], 0u);
238
350
  }
239
351
  if (index >= VERTEX_COUNT) { return; }
@@ -299,42 +411,96 @@ fn main(
299
411
  });
300
412
  }
301
413
 
302
- /** Publishes the source only after complete adjacency and weight status are known. */
303
- function addSourcePass<Parameters>(
414
+ /** Publishes failure, trivial convergence, and the first round dispatch after weight validation. */
415
+ function addStatusPass<Parameters>(
304
416
  commandGraph: GPUCommandGraph<Parameters>,
305
417
  state: ImportedShortestPathState
306
418
  ): void {
307
419
  const bindings: Record<string, ShortestPathBinding> = {
308
- distances: {view: state.distances, usage: 'storage-read-write', atomic: true},
309
- converged: {view: state.converged, usage: 'storage-read-write', atomic: true},
420
+ converged: {view: state.converged, usage: 'storage-write'},
421
+ control: {view: state.control, usage: 'storage-write'},
310
422
  invalidWeightCount: {view: state.invalidWeightCount, usage: 'storage-read'},
311
423
  ...getOverflowBindings(state)
312
424
  };
313
425
  const triviallyConverged = state.vertexCount <= 1 || state.edgeCount === 0;
426
+ const stopsBeforeRounds = triviallyConverged || state.maxIterations === 0;
427
+ const roundLayout = getDispatchLayout(state, Math.max(state.vertexCount, 1));
314
428
  const source = /* wgsl */ `
315
- const VERTEX_COUNT: u32 = ${state.vertexCount}u;
316
- const SOURCE_VERTEX: u32 = ${state.sourceVertex}u;
317
- const DISTANCES_OFFSET: u32 = ${getViewElementOffset(state.distances)}u;
318
429
  const CONVERGED_OFFSET: u32 = ${getViewElementOffset(state.converged)}u;
430
+ const CONTROL_OFFSET: u32 = ${getViewElementOffset(state.control)}u;
319
431
  const INVALID_WEIGHT_COUNT_OFFSET: u32 = ${getViewElementOffset(state.invalidWeightCount)}u;
320
432
  ${getOverflowConstants(state)}
321
433
  ${getBindingDeclarations(bindings)}
322
434
 
323
435
  @compute @workgroup_size(1)
324
436
  fn main() {
325
- if (invalidWeightCount[INVALID_WEIGHT_COUNT_OFFSET] != 0u || ${getOverflowGuard(state)}) {
437
+ let failed = invalidWeightCount[INVALID_WEIGHT_COUNT_OFFSET] != 0u || ${getOverflowGuard(state)};
438
+ let converges = !failed && ${triviallyConverged};
439
+ let stopped = failed || ${stopsBeforeRounds};
440
+ control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_DISPATCH_X}u] = select(${roundLayout.x}u, 0u, stopped);
441
+ control[CONTROL_OFFSET + 1u] = ${roundLayout.y}u;
442
+ control[CONTROL_OFFSET + 2u] = ${roundLayout.z}u;
443
+ control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_ITERATIONS}u] = 0u;
444
+ control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_FAILED}u] = select(0u, 1u, failed);
445
+ control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_CONVERGED}u] = select(0u, 1u, converges);
446
+ converged[CONVERGED_OFFSET] = select(0u, 1u, converges);
447
+ }`;
448
+ addShortestPathPass(commandGraph, {
449
+ id: `${state.id}-status`,
450
+ source,
451
+ bindings,
452
+ dispatchLayout: {x: 1, y: 1, z: 1}
453
+ });
454
+ }
455
+
456
+ /**
457
+ * Places every valid seed at distance zero unless the search failed.
458
+ *
459
+ * A constant `sourceVertex` is uploaded to a node-owned buffer at compile time, so the shader
460
+ * source, and with it the cached pipeline, does not depend on the source vertex.
461
+ */
462
+ function addSeedPass<Parameters>(
463
+ commandGraph: GPUCommandGraph<Parameters>,
464
+ state: ImportedShortestPathState,
465
+ search: GPUGraphAlgorithmsSingleSourceShortestPath
466
+ ): void {
467
+ const seedCount = state.seeds?.length ?? 1;
468
+ const bindings: Record<string, ShortestPathBinding> = {
469
+ control: {view: state.control, usage: 'storage-read'},
470
+ distances: {view: state.distances, usage: 'storage-read-write', atomic: true},
471
+ ...(state.seeds ? {seeds: {view: state.seeds, usage: 'storage-read' as const}} : {})
472
+ };
473
+ const declarations = getBindingDeclarations(bindings);
474
+ const ownedSeedLocation = Object.keys(bindings).length;
475
+ const source = /* wgsl */ `
476
+ const VERTEX_COUNT: u32 = ${state.vertexCount}u;
477
+ const SEED_COUNT: u32 = ${seedCount}u;
478
+ const SEEDS_OFFSET: u32 = ${state.seeds ? getViewElementOffset(state.seeds) : 0}u;
479
+ const CONTROL_OFFSET: u32 = ${getViewElementOffset(state.control)}u;
480
+ const DISTANCES_OFFSET: u32 = ${getViewElementOffset(state.distances)}u;
481
+ ${declarations}
482
+ ${state.seeds ? '' : `@group(0) @binding(${ownedSeedLocation}) var<storage, read> seeds: array<u32>;`}
483
+
484
+ @compute @workgroup_size(${SHORTEST_PATH_WORKGROUP_SIZE})
485
+ fn main(
486
+ @builtin(workgroup_id) workgroupId: vec3<u32>,
487
+ @builtin(local_invocation_index) localInvocationIndex: u32
488
+ ) {
489
+ ${getInvocationIndexSource(state, seedCount)}
490
+ if (index >= SEED_COUNT || control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_FAILED}u] != 0u) {
326
491
  return;
327
492
  }
328
- if (VERTEX_COUNT > 0u) {
329
- atomicStore(&distances[DISTANCES_OFFSET + SOURCE_VERTEX], 0u);
493
+ let vertex = seeds[SEEDS_OFFSET + index];
494
+ if (vertex < VERTEX_COUNT) {
495
+ atomicStore(&distances[DISTANCES_OFFSET + vertex], 0u);
330
496
  }
331
- atomicStore(&converged[CONVERGED_OFFSET], ${triviallyConverged ? '1u' : '0u'});
332
497
  }`;
333
498
  addShortestPathPass(commandGraph, {
334
- id: `${state.id}-source`,
499
+ id: `${state.id}-seeds`,
335
500
  source,
336
501
  bindings,
337
- dispatchLayout: {x: 1, y: 1, z: 1}
502
+ dispatchLayout: getDispatchLayout(state, seedCount),
503
+ ...(state.seeds ? {} : {ownedSeed: search.sourceVertex!})
338
504
  });
339
505
  }
340
506
 
@@ -349,15 +515,13 @@ function addSnapshotPass<Parameters>(
349
515
  const bindings: Record<string, ShortestPathBinding> = {
350
516
  distances: {view: state.distances, usage: 'storage-read-write', atomic: true},
351
517
  previousDistances: {view: previousDistances, usage: 'storage-write'},
352
- changed: {view: changed, usage: 'storage-write', atomic: true},
353
- converged: {view: state.converged, usage: 'storage-read'}
518
+ changed: {view: changed, usage: 'storage-write', atomic: true}
354
519
  };
355
520
  const source = /* wgsl */ `
356
521
  const VERTEX_COUNT: u32 = ${state.vertexCount}u;
357
522
  const DISTANCES_OFFSET: u32 = ${getViewElementOffset(state.distances)}u;
358
523
  const PREVIOUS_DISTANCES_OFFSET: u32 = ${getViewElementOffset(previousDistances)}u;
359
524
  const CHANGED_OFFSET: u32 = ${getViewElementOffset(changed)}u;
360
- const CONVERGED_OFFSET: u32 = ${getViewElementOffset(state.converged)}u;
361
525
  ${getBindingDeclarations(bindings)}
362
526
 
363
527
  @compute @workgroup_size(${SHORTEST_PATH_WORKGROUP_SIZE})
@@ -367,7 +531,7 @@ fn main(
367
531
  ) {
368
532
  ${getInvocationIndexSource(state, Math.max(state.vertexCount, 1))}
369
533
  if (index == 0u) { atomicStore(&changed[CHANGED_OFFSET], 0u); }
370
- if (index >= VERTEX_COUNT || converged[CONVERGED_OFFSET] != 0u) { return; }
534
+ if (index >= VERTEX_COUNT) { return; }
371
535
  previousDistances[PREVIOUS_DISTANCES_OFFSET + index] =
372
536
  bitcast<f32>(atomicLoad(&distances[DISTANCES_OFFSET + index]));
373
537
  }`;
@@ -375,7 +539,8 @@ fn main(
375
539
  id: `${state.id}-iteration-${iteration}-snapshot`,
376
540
  source,
377
541
  bindings,
378
- dispatchLayout: getDispatchLayout(state, Math.max(state.vertexCount, 1))
542
+ dispatchLayout: getDispatchLayout(state, Math.max(state.vertexCount, 1)),
543
+ condition: state.roundCondition
379
544
  });
380
545
  }
381
546
 
@@ -400,8 +565,7 @@ function addRelaxationPass<Parameters>(
400
565
  : {}),
401
566
  previousDistances: {view: previousDistances, usage: 'storage-read'},
402
567
  distances: {view: state.distances, usage: 'storage-read-write', atomic: true},
403
- changed: {view: changed, usage: 'storage-read-write', atomic: true},
404
- converged: {view: state.converged, usage: 'storage-read'}
568
+ changed: {view: changed, usage: 'storage-read-write', atomic: true}
405
569
  };
406
570
  const weightOffset = adjacency.edgeWeights
407
571
  ? `const WEIGHTS_OFFSET: u32 = ${getViewElementOffset(adjacency.edgeWeights)}u;`
@@ -415,7 +579,6 @@ const NEIGHBORS_OFFSET: u32 = ${getViewElementOffset(adjacency.neighbors)}u;
415
579
  const PREVIOUS_DISTANCES_OFFSET: u32 = ${getViewElementOffset(previousDistances)}u;
416
580
  const DISTANCES_OFFSET: u32 = ${getViewElementOffset(state.distances)}u;
417
581
  const CHANGED_OFFSET: u32 = ${getViewElementOffset(changed)}u;
418
- const CONVERGED_OFFSET: u32 = ${getViewElementOffset(state.converged)}u;
419
582
  ${weightOffset}
420
583
  ${getBindingDeclarations(bindings)}
421
584
 
@@ -425,7 +588,7 @@ fn main(
425
588
  @builtin(local_invocation_index) localInvocationIndex: u32
426
589
  ) {
427
590
  ${getInvocationIndexSource(state, state.vertexCount)}
428
- if (index >= VERTEX_COUNT || converged[CONVERGED_OFFSET] != 0u) { return; }
591
+ if (index >= VERTEX_COUNT) { return; }
429
592
  let distance = previousDistances[PREVIOUS_DISTANCES_OFFSET + index];
430
593
  if (bitcast<u32>(distance) >= 0x7f800000u) { return; }
431
594
  let first = min(offsets[OFFSETS_OFFSET + index], CAPACITY);
@@ -444,7 +607,8 @@ fn main(
444
607
  id: `${state.id}-iteration-${props.iteration}-relax-${props.direction}`,
445
608
  source,
446
609
  bindings,
447
- dispatchLayout: getDispatchLayout(state, state.vertexCount)
610
+ dispatchLayout: getDispatchLayout(state, state.vertexCount),
611
+ condition: state.roundCondition
448
612
  });
449
613
  }
450
614
 
@@ -458,16 +622,13 @@ function addPredecessorResetPass<Parameters>(
458
622
  const bindings: Record<string, ShortestPathBinding> = {
459
623
  previousDistances: {view: previousDistances, usage: 'storage-read'},
460
624
  distances: {view: state.distances, usage: 'storage-read-write', atomic: true},
461
- predecessors: {view: state.predecessors, usage: 'storage-read-write', atomic: true},
462
- converged: {view: state.converged, usage: 'storage-read'}
625
+ predecessors: {view: state.predecessors, usage: 'storage-read-write', atomic: true}
463
626
  };
464
627
  const source = /* wgsl */ `
465
628
  const VERTEX_COUNT: u32 = ${state.vertexCount}u;
466
- const SOURCE_VERTEX: u32 = ${state.sourceVertex}u;
467
629
  const PREVIOUS_DISTANCES_OFFSET: u32 = ${getViewElementOffset(previousDistances)}u;
468
630
  const DISTANCES_OFFSET: u32 = ${getViewElementOffset(state.distances)}u;
469
631
  const PREDECESSORS_OFFSET: u32 = ${getViewElementOffset(state.predecessors)}u;
470
- const CONVERGED_OFFSET: u32 = ${getViewElementOffset(state.converged)}u;
471
632
  ${getBindingDeclarations(bindings)}
472
633
 
473
634
  @compute @workgroup_size(${SHORTEST_PATH_WORKGROUP_SIZE})
@@ -476,9 +637,8 @@ fn main(
476
637
  @builtin(local_invocation_index) localInvocationIndex: u32
477
638
  ) {
478
639
  ${getInvocationIndexSource(state, state.vertexCount)}
479
- if (index >= VERTEX_COUNT || index == SOURCE_VERTEX || converged[CONVERGED_OFFSET] != 0u) {
480
- return;
481
- }
640
+ // A seed keeps distance zero, so it never takes this branch and keeps the sentinel parent.
641
+ if (index >= VERTEX_COUNT) { return; }
482
642
  let previous = bitcast<u32>(previousDistances[PREVIOUS_DISTANCES_OFFSET + index]);
483
643
  let current = atomicLoad(&distances[DISTANCES_OFFSET + index]);
484
644
  if (current < previous) {
@@ -489,7 +649,8 @@ fn main(
489
649
  id: `${state.id}-iteration-${iteration}-reset-predecessors`,
490
650
  source,
491
651
  bindings,
492
- dispatchLayout: getDispatchLayout(state, state.vertexCount)
652
+ dispatchLayout: getDispatchLayout(state, state.vertexCount),
653
+ condition: state.roundCondition
493
654
  });
494
655
  }
495
656
 
@@ -513,8 +674,7 @@ function addPredecessorPass<Parameters>(
513
674
  : {}),
514
675
  previousDistances: {view: previousDistances, usage: 'storage-read'},
515
676
  distances: {view: state.distances, usage: 'storage-read-write', atomic: true},
516
- predecessors: {view: state.predecessors, usage: 'storage-read-write', atomic: true},
517
- converged: {view: state.converged, usage: 'storage-read'}
677
+ predecessors: {view: state.predecessors, usage: 'storage-read-write', atomic: true}
518
678
  };
519
679
  const weightOffset = adjacency.edgeWeights
520
680
  ? `const WEIGHTS_OFFSET: u32 = ${getViewElementOffset(adjacency.edgeWeights)}u;`
@@ -522,14 +682,12 @@ function addPredecessorPass<Parameters>(
522
682
  const weight = adjacency.edgeWeights ? 'edgeWeights[WEIGHTS_OFFSET + slot]' : '1.0';
523
683
  const source = /* wgsl */ `
524
684
  const VERTEX_COUNT: u32 = ${state.vertexCount}u;
525
- const SOURCE_VERTEX: u32 = ${state.sourceVertex}u;
526
685
  const CAPACITY: u32 = ${adjacency.neighbors.length}u;
527
686
  const OFFSETS_OFFSET: u32 = ${getViewElementOffset(adjacency.offsets)}u;
528
687
  const NEIGHBORS_OFFSET: u32 = ${getViewElementOffset(adjacency.neighbors)}u;
529
688
  const PREVIOUS_DISTANCES_OFFSET: u32 = ${getViewElementOffset(previousDistances)}u;
530
689
  const DISTANCES_OFFSET: u32 = ${getViewElementOffset(state.distances)}u;
531
690
  const PREDECESSORS_OFFSET: u32 = ${getViewElementOffset(state.predecessors)}u;
532
- const CONVERGED_OFFSET: u32 = ${getViewElementOffset(state.converged)}u;
533
691
  ${weightOffset}
534
692
  ${getBindingDeclarations(bindings)}
535
693
 
@@ -539,14 +697,15 @@ fn main(
539
697
  @builtin(local_invocation_index) localInvocationIndex: u32
540
698
  ) {
541
699
  ${getInvocationIndexSource(state, state.vertexCount)}
542
- if (index >= VERTEX_COUNT || converged[CONVERGED_OFFSET] != 0u) { return; }
700
+ if (index >= VERTEX_COUNT) { return; }
543
701
  let sourceDistance = previousDistances[PREVIOUS_DISTANCES_OFFSET + index];
544
702
  if (bitcast<u32>(sourceDistance) >= 0x7f800000u) { return; }
545
703
  let first = min(offsets[OFFSETS_OFFSET + index], CAPACITY);
546
704
  let last = min(offsets[OFFSETS_OFFSET + index + 1u], CAPACITY);
547
705
  for (var slot = first; slot < last; slot++) {
548
706
  let neighbor = neighbors[NEIGHBORS_OFFSET + slot];
549
- if (neighbor >= VERTEX_COUNT || neighbor == SOURCE_VERTEX) { continue; }
707
+ // A seed never improves, so the first condition below also keeps its sentinel parent.
708
+ if (neighbor >= VERTEX_COUNT) { continue; }
550
709
  let previousDistance = bitcast<u32>(previousDistances[PREVIOUS_DISTANCES_OFFSET + neighbor]);
551
710
  let currentDistance = atomicLoad(&distances[DISTANCES_OFFSET + neighbor]);
552
711
  let candidate = bitcast<u32>(sourceDistance + ${weight});
@@ -559,11 +718,17 @@ fn main(
559
718
  id: `${state.id}-iteration-${props.iteration}-predecessors-${props.direction}`,
560
719
  source,
561
720
  bindings,
562
- dispatchLayout: getDispatchLayout(state, state.vertexCount)
721
+ dispatchLayout: getDispatchLayout(state, state.vertexCount),
722
+ condition: state.roundCondition
563
723
  });
564
724
  }
565
725
 
566
- /** Publishes fixed-point convergence without synchronously reading status back to the CPU. */
726
+ /**
727
+ * Counts the round and stops later rounds on convergence or at the round budget.
728
+ *
729
+ * Stopping writes a zero x dimension into the shared indirect dispatch command, so every later
730
+ * round pass dispatches no workgroups, in this submission and in later ones.
731
+ */
567
732
  function addConvergencePass<Parameters>(
568
733
  commandGraph: GPUCommandGraph<Parameters>,
569
734
  state: ImportedShortestPathState,
@@ -572,26 +737,29 @@ function addConvergencePass<Parameters>(
572
737
  const changed = state.changed!;
573
738
  const bindings: Record<string, ShortestPathBinding> = {
574
739
  changed: {view: changed, usage: 'storage-read'},
575
- converged: {view: state.converged, usage: 'storage-read-write', atomic: true},
576
- invalidWeightCount: {view: state.invalidWeightCount, usage: 'storage-read'},
577
- ...getOverflowBindings(state)
740
+ converged: {view: state.converged, usage: 'storage-write'},
741
+ control: {view: state.control, usage: 'storage-read-write'}
578
742
  };
579
- const reachesSimplePathBound = iteration + 1 >= state.vertexCount - 1;
580
743
  const source = /* wgsl */ `
744
+ const VERTEX_COUNT: u32 = ${state.vertexCount}u;
745
+ const MAX_ITERATIONS: u32 = ${state.maxIterations}u;
581
746
  const CHANGED_OFFSET: u32 = ${getViewElementOffset(changed)}u;
582
747
  const CONVERGED_OFFSET: u32 = ${getViewElementOffset(state.converged)}u;
583
- const INVALID_WEIGHT_COUNT_OFFSET: u32 = ${getViewElementOffset(state.invalidWeightCount)}u;
584
- ${getOverflowConstants(state)}
748
+ const CONTROL_OFFSET: u32 = ${getViewElementOffset(state.control)}u;
585
749
  ${getBindingDeclarations(bindings)}
586
750
 
587
751
  @compute @workgroup_size(1)
588
752
  fn main() {
589
- if (invalidWeightCount[INVALID_WEIGHT_COUNT_OFFSET] != 0u || ${getOverflowGuard(state)}) {
590
- atomicStore(&converged[CONVERGED_OFFSET], 0u);
591
- return;
592
- }
593
- if (changed[CHANGED_OFFSET] == 0u${reachesSimplePathBound ? ' || true' : ''}) {
594
- atomicStore(&converged[CONVERGED_OFFSET], 1u);
753
+ if (control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_DISPATCH_X}u] == 0u) { return; }
754
+ let iterations = control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_ITERATIONS}u] + 1u;
755
+ control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_ITERATIONS}u] = iterations;
756
+ // No change, or V - 1 rounds, which bound every simple path, mean every distance is final.
757
+ if (changed[CHANGED_OFFSET] == 0u || iterations + 1u >= VERTEX_COUNT) {
758
+ converged[CONVERGED_OFFSET] = 1u;
759
+ control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_CONVERGED}u] = 1u;
760
+ control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_DISPATCH_X}u] = 0u;
761
+ } else if (iterations >= MAX_ITERATIONS) {
762
+ control[CONTROL_OFFSET + ${SHORTEST_PATH_CONTROL_DISPATCH_X}u] = 0u;
595
763
  }
596
764
  }`;
597
765
  addShortestPathPass(commandGraph, {
@@ -653,15 +821,18 @@ function addShortestPathPass<Parameters>(
653
821
  commandGraph: GPUCommandGraph<Parameters>,
654
822
  props: ShortestPathPassProps
655
823
  ): void {
824
+ const names = Object.keys(props.bindings);
825
+ if (props.ownedSeed !== undefined) names.push('seeds');
656
826
  commandGraph.addComputePass({
657
827
  id: props.id,
658
828
  resources: Object.values(props.bindings).map(({view, usage}) => ({buffer: view, usage})),
829
+ ...(props.condition ? {condition: props.condition} : {}),
659
830
  compile: ({device}) => {
660
831
  const kernel = new Kernel(device, {
661
832
  id: props.id,
662
833
  source: props.source,
663
834
  shaderLayout: {
664
- bindings: Object.keys(props.bindings).map((name, location) => ({
835
+ bindings: names.map((name, location) => ({
665
836
  name,
666
837
  type: 'storage' as const,
667
838
  group: 0,
@@ -669,6 +840,14 @@ function addShortestPathPass<Parameters>(
669
840
  }))
670
841
  }
671
842
  });
843
+ const ownedSeed =
844
+ props.ownedSeed === undefined
845
+ ? undefined
846
+ : device.createBuffer({
847
+ id: `${props.id}-source-vertex`,
848
+ data: Uint32Array.of(props.ownedSeed),
849
+ usage: Buffer.STORAGE
850
+ });
672
851
 
673
852
  return {
674
853
  encode: ({computePass, getBuffer}) => {
@@ -676,15 +855,18 @@ function addShortestPathPass<Parameters>(
676
855
  for (const [name, binding] of Object.entries(props.bindings)) {
677
856
  bindings[name] = getViewBinding(binding.view, getBuffer);
678
857
  }
679
- const kernelBindings = bindings;
858
+ if (ownedSeed) bindings['seeds'] = ownedSeed;
680
859
  kernel.dispatch(computePass, {
681
- bindings: kernelBindings,
860
+ bindings,
682
861
  x: props.dispatchLayout.x,
683
862
  y: props.dispatchLayout.y,
684
863
  z: props.dispatchLayout.z
685
864
  });
686
865
  },
687
- destroy: () => kernel.destroy()
866
+ destroy: () => {
867
+ kernel.destroy();
868
+ ownedSeed?.destroy();
869
+ }
688
870
  };
689
871
  }
690
872
  });