@manycore/aholo-splat-transform 1.2.8 → 1.2.10

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 (97) hide show
  1. package/CHANGELOG.md +124 -113
  2. package/README.md +39 -39
  3. package/THIRD_PARTY_LICENSES.txt +1373 -1373
  4. package/bin/cli.js +125 -118
  5. package/dist/SplatData.d.ts +67 -67
  6. package/dist/SplatData.js +167 -150
  7. package/dist/constant.d.ts +3 -3
  8. package/dist/constant.js +13 -13
  9. package/dist/file/IFile.d.ts +5 -5
  10. package/dist/file/IFile.js +1 -1
  11. package/dist/file/esz.d.ts +11 -11
  12. package/dist/file/esz.js +337 -322
  13. package/dist/file/index.d.ts +8 -8
  14. package/dist/file/index.js +7 -7
  15. package/dist/file/ksplat.d.ts +12 -12
  16. package/dist/file/ksplat.js +293 -231
  17. package/dist/file/lcc.d.ts +11 -11
  18. package/dist/file/lcc.js +161 -158
  19. package/dist/file/ply.d.ts +13 -13
  20. package/dist/file/ply.js +439 -390
  21. package/dist/file/sog.d.ts +80 -80
  22. package/dist/file/sog.js +525 -494
  23. package/dist/file/splat.d.ts +6 -6
  24. package/dist/file/splat.js +119 -99
  25. package/dist/file/spz.d.ts +11 -11
  26. package/dist/file/spz.js +597 -583
  27. package/dist/file/voxel.d.ts +43 -37
  28. package/dist/file/voxel.js +411 -280
  29. package/dist/index.d.ts +33 -33
  30. package/dist/index.js +54 -54
  31. package/dist/native/index.d.ts +54 -54
  32. package/dist/native/index.js +122 -129
  33. package/dist/native/utils.d.ts +1 -0
  34. package/dist/native/utils.js +54 -0
  35. package/dist/tasks/AutoChunkLodTask.d.ts +13 -13
  36. package/dist/tasks/AutoChunkLodTask.js +117 -117
  37. package/dist/tasks/AutoLodTask.d.ts +10 -10
  38. package/dist/tasks/AutoLodTask.js +20 -20
  39. package/dist/tasks/BaseTask.d.ts +15 -15
  40. package/dist/tasks/BaseTask.js +5 -5
  41. package/dist/tasks/FlexLodTask.d.ts +12 -12
  42. package/dist/tasks/FlexLodTask.js +54 -44
  43. package/dist/tasks/ModifyTask.d.ts +9 -9
  44. package/dist/tasks/ModifyTask.js +166 -156
  45. package/dist/tasks/ReadTask.d.ts +9 -9
  46. package/dist/tasks/ReadTask.js +29 -29
  47. package/dist/tasks/SkeletonLodTask.d.ts +10 -10
  48. package/dist/tasks/SkeletonLodTask.js +176 -156
  49. package/dist/tasks/VoxelTask.d.ts +35 -30
  50. package/dist/tasks/VoxelTask.js +40 -37
  51. package/dist/tasks/WriteTask.d.ts +12 -12
  52. package/dist/tasks/WriteTask.js +70 -70
  53. package/dist/utils/BufferReader.d.ts +12 -12
  54. package/dist/utils/BufferReader.js +45 -45
  55. package/dist/utils/Logger.d.ts +11 -11
  56. package/dist/utils/Logger.js +40 -40
  57. package/dist/utils/StreamChunkDecoder.d.ts +16 -16
  58. package/dist/utils/StreamChunkDecoder.js +31 -31
  59. package/dist/utils/index.d.ts +27 -27
  60. package/dist/utils/index.js +101 -101
  61. package/dist/utils/k-means.d.ts +4 -4
  62. package/dist/utils/k-means.js +340 -341
  63. package/dist/utils/math.d.ts +46 -46
  64. package/dist/utils/math.js +350 -346
  65. package/dist/utils/quantize-1d.d.ts +4 -4
  66. package/dist/utils/quantize-1d.js +164 -164
  67. package/dist/utils/sh-rotate.d.ts +2 -2
  68. package/dist/utils/sh-rotate.js +236 -175
  69. package/dist/utils/splat.d.ts +21 -21
  70. package/dist/utils/splat.js +397 -387
  71. package/dist/utils/voxel/binary.d.ts +8 -0
  72. package/dist/utils/voxel/binary.js +176 -0
  73. package/dist/utils/voxel/common.d.ts +178 -162
  74. package/dist/utils/voxel/common.js +1752 -1682
  75. package/dist/utils/voxel/coplanar-merge.d.ts +63 -63
  76. package/dist/utils/voxel/coplanar-merge.js +818 -819
  77. package/dist/utils/voxel/filter-cluster.d.ts +20 -0
  78. package/dist/utils/voxel/filter-cluster.js +628 -0
  79. package/dist/utils/voxel/gpu-dilation.d.ts +2 -2
  80. package/dist/utils/voxel/gpu-dilation.js +677 -656
  81. package/dist/utils/voxel/marching-cubes.d.ts +42 -42
  82. package/dist/utils/voxel/marching-cubes.js +1645 -1657
  83. package/dist/utils/voxel/mesh.d.ts +3 -3
  84. package/dist/utils/voxel/mesh.js +130 -130
  85. package/dist/utils/voxel/nav.d.ts +29 -29
  86. package/dist/utils/voxel/nav.js +1068 -1043
  87. package/dist/utils/voxel/postprocess.d.ts +23 -23
  88. package/dist/utils/voxel/postprocess.js +408 -375
  89. package/dist/utils/voxel/voxel-faces.d.ts +18 -18
  90. package/dist/utils/voxel/voxel-faces.js +662 -663
  91. package/dist/utils/voxel/voxelize.d.ts +34 -33
  92. package/dist/utils/voxel/voxelize.js +1208 -1193
  93. package/dist/utils/webgpu.d.ts +8 -8
  94. package/dist/utils/webgpu.js +122 -122
  95. package/package.json +38 -39
  96. package/dist/native/cpp/bin/linux/binding.node +0 -0
  97. package/dist/native/cpp/bin/windows/binding.node +0 -0
@@ -1,341 +1,340 @@
1
- import { clusterAverage } from '../native/index.js';
2
- import { logger } from './index.js';
3
- // in the 1d case we use quantile-based initialization for better handling of skewed data
4
- function initializeCentroids1D(data, centroids) {
5
- const n = data.length;
6
- const k = centroids.length;
7
- // Sort data to compute quantiles
8
- const sorted = Float32Array.from(data).sort((a, b) => a - b);
9
- for (let i = 0; i < k; ++i) {
10
- // Place centroid at the center of its expected cluster region
11
- const quantile = (2 * i + 1) / (2 * k);
12
- const index = Math.min(Math.floor(quantile * n), n - 1);
13
- centroids[i] = sorted[index];
14
- }
15
- }
16
- ;
17
- // use floyd's algorithm to pick m unique random indices from 0..n-1
18
- function pickRandomIndices(n, m) {
19
- const chosen = new Set();
20
- for (let j = n - m; j < n; j++) {
21
- const t = Math.floor(Math.random() * (j + 1));
22
- chosen.add(chosen.has(t) ? j : t);
23
- }
24
- return [...chosen];
25
- }
26
- ;
27
- function initializeCentroids(dataTable, centroids) {
28
- const indices = pickRandomIndices(dataTable[0].length, centroids[0].length);
29
- for (let i = 0; i < centroids[0].length; i++) {
30
- for (let j = 0; j < dataTable.length; j++) {
31
- centroids[j][i] = dataTable[j][indices[i]];
32
- }
33
- }
34
- }
35
- ;
36
- const chunkSize = 128;
37
- const workgroupSize = 64;
38
- function clusterWgsl(numColumns) {
39
- return /* wgsl */ `
40
- struct Uniforms {
41
- numPoints: u32,
42
- numCentroids: u32
43
- };
44
-
45
- @group(0) @binding(0) var<uniform> uniforms: Uniforms;
46
- @group(0) @binding(1) var<storage, read> points: array<f32>;
47
- @group(0) @binding(2) var<storage, read> centroids: array<f32>;
48
- @group(0) @binding(3) var<storage, read_write> results: array<u32>;
49
-
50
- const numColumns = ${numColumns}; // number of columns in the points and centroids tables
51
- const chunkSize = ${chunkSize}u; // must be a multiple of 64
52
- const workgroupSize = ${workgroupSize}u;
53
- var<workgroup> sharedChunk: array<f32, numColumns * chunkSize>;
54
-
55
- // calculate the squared distance between the point and centroid
56
- fn calcDistanceSqr(point: array<f32, numColumns>, centroid: u32) -> f32 {
57
- var result = 0.0;
58
-
59
- var ci = centroid * numColumns;
60
-
61
- for (var i = 0u; i < numColumns; i++) {
62
- let v = f32(point[i] - sharedChunk[ci+i]);
63
- result += v * v;
64
- }
65
-
66
- return result;
67
- }
68
-
69
- @compute @workgroup_size(workgroupSize)
70
- fn main(
71
- @builtin(local_invocation_index) local_id : u32,
72
- @builtin(global_invocation_id) global_id: vec3u,
73
- @builtin(num_workgroups) num_workgroups: vec3u
74
- ) {
75
- // calculate row index for this thread point
76
- let pointIndex = global_id.x + global_id.y * num_workgroups.x * workgroupSize;
77
-
78
- // copy the point data from global memory
79
- var point: array<f32, numColumns>;
80
- if (pointIndex < uniforms.numPoints) {
81
- for (var i = 0u; i < numColumns; i++) {
82
- point[i] = points[pointIndex * numColumns + i];
83
- }
84
- }
85
-
86
- var mind = 1000000.0;
87
- var mini = 0u;
88
-
89
- // work through the list of centroids in shared memory chunks
90
- let numChunks = u32(ceil(f32(uniforms.numCentroids) / f32(chunkSize)));
91
- for (var i = 0u; i < numChunks; i++) {
92
-
93
- // copy this thread's slice of the centroid shared chunk data
94
- let dstRow = local_id * (chunkSize / workgroupSize);
95
- let srcRow = min(uniforms.numCentroids, i * chunkSize + local_id * chunkSize / workgroupSize);
96
- let numRows = min(uniforms.numCentroids, srcRow + chunkSize / workgroupSize) - srcRow;
97
-
98
- var dst = dstRow * numColumns;
99
- var src = srcRow * numColumns;
100
-
101
- for (var c = 0u; c < numRows * numColumns; c++) {
102
- sharedChunk[dst + c] = centroids[src + c];
103
- }
104
-
105
- // wait for all threads to finish writing their part of centroids shared memory buffer
106
- workgroupBarrier();
107
-
108
- // loop over the next chunk of centroids finding the closest
109
- if (pointIndex < uniforms.numPoints) {
110
- let thisChunkSize = min(chunkSize, uniforms.numCentroids - i * chunkSize);
111
- for (var c = 0u; c < thisChunkSize; c++) {
112
- let d = calcDistanceSqr(point, c);
113
- if (d < mind) {
114
- mind = d;
115
- mini = i * chunkSize + c;
116
- }
117
- }
118
- }
119
-
120
- // next loop will overwrite the shared memory, so wait
121
- workgroupBarrier();
122
- }
123
-
124
- if (pointIndex < uniforms.numPoints) {
125
- results[pointIndex] = mini;
126
- }
127
- }
128
- `;
129
- }
130
- function interleaveData(result, dataTable, numRows, rowOffset) {
131
- const numColumns = dataTable.length;
132
- for (let c = 0; c < numColumns; ++c) {
133
- const column = dataTable[c];
134
- for (let r = 0; r < numRows; ++r) {
135
- result[r * numColumns + c] = column[rowOffset + r];
136
- }
137
- }
138
- }
139
- const MAX_CONCURRENCY_BATCHES = 10;
140
- class GpuClustering {
141
- constructor(device, numPoints, numColumns, numCentroids) {
142
- this.device = device;
143
- this.numPoints = numPoints;
144
- this.numColumns = numColumns;
145
- this.numCentroids = numCentroids;
146
- const workgroupsPerBatch = Math.min(device.limits.maxComputeWorkgroupsPerDimension, // device dispatch limit
147
- Math.floor(device.limits.maxBufferSize / (numColumns * workgroupSize * 4)), // point storage limit
148
- Math.ceil(numPoints / workgroupSize) // max limit
149
- );
150
- this.batchSize = workgroupsPerBatch * workgroupSize;
151
- this.numBatches = Math.ceil(numPoints / this.batchSize);
152
- this.concurrencyBatches = Math.min(MAX_CONCURRENCY_BATCHES, this.numBatches);
153
- this.concurrencyRuns = Math.ceil(this.numBatches / this.concurrencyBatches);
154
- const shader = device.createShaderModule({
155
- code: clusterWgsl(numColumns),
156
- });
157
- const pipeline = device.createComputePipeline({
158
- layout: 'auto',
159
- compute: {
160
- module: shader,
161
- entryPoint: 'main'
162
- }
163
- });
164
- const pointsBackBuffer = new Float32Array(numColumns * this.batchSize);
165
- const centroidsBackBuffer = new Float32Array(numColumns * numCentroids);
166
- const uniformBackBuffer = new Uint32Array([0, numCentroids]);
167
- const pointsBuffers = [];
168
- const centroidsBuffer = device.createBuffer({
169
- size: centroidsBackBuffer.byteLength,
170
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE
171
- });
172
- const uniformBuffer = device.createBuffer({
173
- size: 256 * this.concurrencyBatches,
174
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.UNIFORM
175
- });
176
- const resultBuffer = device.createBuffer({
177
- size: this.concurrencyBatches * this.batchSize * 4,
178
- usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
179
- });
180
- const resultReadBackBuffer = device.createBuffer({
181
- size: this.concurrencyBatches * this.batchSize * 4,
182
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ
183
- });
184
- const layout = pipeline.getBindGroupLayout(0);
185
- const bindGroups = [];
186
- for (let i = 0; i < this.concurrencyBatches; i++) {
187
- const pointsBuffer = device.createBuffer({
188
- size: pointsBackBuffer.byteLength,
189
- usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE
190
- });
191
- pointsBuffers.push(pointsBuffer);
192
- bindGroups.push(device.createBindGroup({
193
- layout,
194
- entries: [{
195
- binding: 0,
196
- resource: {
197
- buffer: uniformBuffer,
198
- offset: i * 256,
199
- size: 8
200
- }
201
- }, {
202
- binding: 1,
203
- resource: pointsBuffer,
204
- }, {
205
- binding: 2,
206
- resource: centroidsBuffer,
207
- }, {
208
- binding: 3,
209
- resource: {
210
- buffer: resultBuffer,
211
- offset: i * this.batchSize * 4,
212
- size: this.batchSize * 4
213
- }
214
- }]
215
- }));
216
- }
217
- this.resource = {
218
- pipeline,
219
- bindGroups,
220
- gpuBuffers: {
221
- uniform: uniformBuffer,
222
- points: pointsBuffers,
223
- centroids: centroidsBuffer,
224
- result: resultBuffer,
225
- resultReadBack: resultReadBackBuffer,
226
- },
227
- backBuffers: {
228
- uniform: uniformBackBuffer,
229
- points: pointsBackBuffer,
230
- centroids: centroidsBackBuffer,
231
- },
232
- uploadedBatches: [],
233
- };
234
- logger.info(`GPU k-means kernel bootstrapped with batch ${workgroupsPerBatch}*${workgroupSize}*${this.numBatches}, concurrency: ${this.concurrencyBatches}, runs: ${this.concurrencyRuns}`);
235
- }
236
- async execute(points, centroids, labels) {
237
- const { device, numPoints, numColumns, numCentroids, numBatches, batchSize, resource, concurrencyBatches, concurrencyRuns } = this;
238
- // upload centroid data to gpu
239
- interleaveData(resource.backBuffers.centroids, centroids, numCentroids, 0);
240
- device.queue.writeBuffer(resource.gpuBuffers.centroids, 0, resource.backBuffers.centroids.buffer);
241
- for (let i = 0; i < concurrencyRuns; i++) {
242
- const batchStart = i * concurrencyBatches;
243
- let resultCount = 0;
244
- for (let j = 0; j < concurrencyBatches; j++) {
245
- const batchIndex = batchStart + j;
246
- if (batchIndex >= numBatches) {
247
- break;
248
- }
249
- const currentBatchSize = Math.min(numPoints - batchIndex * batchSize, batchSize);
250
- resultCount += currentBatchSize;
251
- // write this batch of point data to gpu
252
- if (resource.uploadedBatches[j] !== batchIndex) {
253
- interleaveData(resource.backBuffers.points, points, currentBatchSize, batchIndex * batchSize);
254
- device.queue.writeBuffer(resource.gpuBuffers.points[j], 0, resource.backBuffers.points.buffer, 0, numColumns * currentBatchSize * 4);
255
- resource.backBuffers.uniform[0] = currentBatchSize;
256
- device.queue.writeBuffer(resource.gpuBuffers.uniform, 256 * j, resource.backBuffers.uniform.buffer, 0, 8);
257
- resource.uploadedBatches[j] = batchIndex;
258
- }
259
- }
260
- const encoder = device.createCommandEncoder();
261
- const computePass = encoder.beginComputePass();
262
- computePass.setPipeline(resource.pipeline);
263
- for (let j = 0; j < concurrencyBatches; j++) {
264
- const batchIndex = batchStart + j;
265
- if (batchIndex >= numBatches) {
266
- break;
267
- }
268
- const currentBatchSize = Math.min(numPoints - batchIndex * batchSize, batchSize);
269
- const groups = Math.ceil(currentBatchSize / workgroupSize);
270
- computePass.setBindGroup(0, resource.bindGroups[j]);
271
- computePass.dispatchWorkgroups(groups);
272
- }
273
- computePass.end();
274
- encoder.copyBufferToBuffer(resource.gpuBuffers.result, 0, resource.gpuBuffers.resultReadBack, 0, resultCount * 4);
275
- device.queue.submit([encoder.finish()]);
276
- await resource.gpuBuffers.resultReadBack.mapAsync(GPUMapMode.READ);
277
- const mapped = resource.gpuBuffers.resultReadBack.getMappedRange();
278
- labels.set(new Uint32Array(mapped, 0, resultCount), batchStart * batchSize);
279
- resource.gpuBuffers.resultReadBack.unmap();
280
- }
281
- ;
282
- }
283
- destroy() {
284
- this.resource.gpuBuffers.uniform.destroy();
285
- this.resource.gpuBuffers.centroids.destroy();
286
- this.resource.gpuBuffers.result.destroy();
287
- this.resource.gpuBuffers.resultReadBack.destroy();
288
- for (const buffer of this.resource.gpuBuffers.points) {
289
- buffer.destroy();
290
- }
291
- }
292
- }
293
- function groupLabels(labels, k) {
294
- const clusters = [];
295
- for (let i = 0; i < k; ++i) {
296
- clusters[i] = [];
297
- }
298
- for (let i = 0; i < labels.length; ++i) {
299
- clusters[labels[i]].push(i);
300
- }
301
- return clusters.map(c => new Uint32Array(c));
302
- }
303
- ;
304
- // https://github.com/playcanvas/splat-transform/blob/main/src/lib/spatial/k-means.ts
305
- export async function kmeans(points, k, iterations, device) {
306
- const numRows = points.length > 0 ? points[0].length : 0;
307
- if (numRows < k) {
308
- return {
309
- centroids: points,
310
- // use a typed array here so downstream code can rely on
311
- // labels supporting subarray(), even in this early-return
312
- // path used for very small datasets.
313
- labels: new Uint32Array(numRows).map((_, i) => i)
314
- };
315
- }
316
- const centroids = points.map(_ => new Float32Array(k));
317
- if (points.length === 1) {
318
- initializeCentroids1D(points[0], centroids[0]);
319
- }
320
- else {
321
- initializeCentroids(points, centroids);
322
- }
323
- const gpuClustering = new GpuClustering(device, numRows, points.length, k);
324
- const labels = new Uint32Array(numRows);
325
- let converged = false;
326
- let steps = 0;
327
- while (!converged) {
328
- logger.info(`kmeans iteration ${steps + 1}`);
329
- await gpuClustering.execute(points, centroids, labels);
330
- clusterAverage(points, groupLabels(labels, k), centroids);
331
- steps++;
332
- if (steps >= iterations) {
333
- converged = true;
334
- }
335
- }
336
- gpuClustering.destroy();
337
- return {
338
- centroids,
339
- labels
340
- };
341
- }
1
+ import { clusterAverage } from '../native/index.js';
2
+ import { logger } from './index.js';
3
+ // in the 1d case we use quantile-based initialization for better handling of skewed data
4
+ function initializeCentroids1D(data, centroids) {
5
+ const n = data.length;
6
+ const k = centroids.length;
7
+ // Sort data to compute quantiles
8
+ const sorted = Float32Array.from(data).sort((a, b) => a - b);
9
+ for (let i = 0; i < k; ++i) {
10
+ // Place centroid at the center of its expected cluster region
11
+ const quantile = (2 * i + 1) / (2 * k);
12
+ const index = Math.min(Math.floor(quantile * n), n - 1);
13
+ centroids[i] = sorted[index];
14
+ }
15
+ }
16
+ // use floyd's algorithm to pick m unique random indices from 0..n-1
17
+ function pickRandomIndices(n, m) {
18
+ const chosen = new Set();
19
+ for (let j = n - m; j < n; j++) {
20
+ const t = Math.floor(Math.random() * (j + 1));
21
+ chosen.add(chosen.has(t) ? j : t);
22
+ }
23
+ return [...chosen];
24
+ }
25
+ function initializeCentroids(dataTable, centroids) {
26
+ const indices = pickRandomIndices(dataTable[0].length, centroids[0].length);
27
+ for (let i = 0; i < centroids[0].length; i++) {
28
+ for (let j = 0; j < dataTable.length; j++) {
29
+ centroids[j][i] = dataTable[j][indices[i]];
30
+ }
31
+ }
32
+ }
33
+ const chunkSize = 128;
34
+ const workgroupSize = 64;
35
+ function clusterWgsl(numColumns) {
36
+ return /* wgsl */ `
37
+ struct Uniforms {
38
+ numPoints: u32,
39
+ numCentroids: u32
40
+ };
41
+
42
+ @group(0) @binding(0) var<uniform> uniforms: Uniforms;
43
+ @group(0) @binding(1) var<storage, read> points: array<f32>;
44
+ @group(0) @binding(2) var<storage, read> centroids: array<f32>;
45
+ @group(0) @binding(3) var<storage, read_write> results: array<u32>;
46
+
47
+ const numColumns = ${numColumns}; // number of columns in the points and centroids tables
48
+ const chunkSize = ${chunkSize}u; // must be a multiple of 64
49
+ const workgroupSize = ${workgroupSize}u;
50
+ var<workgroup> sharedChunk: array<f32, numColumns * chunkSize>;
51
+
52
+ // calculate the squared distance between the point and centroid
53
+ fn calcDistanceSqr(point: array<f32, numColumns>, centroid: u32) -> f32 {
54
+ var result = 0.0;
55
+
56
+ var ci = centroid * numColumns;
57
+
58
+ for (var i = 0u; i < numColumns; i++) {
59
+ let v = f32(point[i] - sharedChunk[ci+i]);
60
+ result += v * v;
61
+ }
62
+
63
+ return result;
64
+ }
65
+
66
+ @compute @workgroup_size(workgroupSize)
67
+ fn main(
68
+ @builtin(local_invocation_index) local_id : u32,
69
+ @builtin(global_invocation_id) global_id: vec3u,
70
+ @builtin(num_workgroups) num_workgroups: vec3u
71
+ ) {
72
+ // calculate row index for this thread point
73
+ let pointIndex = global_id.x + global_id.y * num_workgroups.x * workgroupSize;
74
+
75
+ // copy the point data from global memory
76
+ var point: array<f32, numColumns>;
77
+ if (pointIndex < uniforms.numPoints) {
78
+ for (var i = 0u; i < numColumns; i++) {
79
+ point[i] = points[pointIndex * numColumns + i];
80
+ }
81
+ }
82
+
83
+ var mind = 1000000.0;
84
+ var mini = 0u;
85
+
86
+ // work through the list of centroids in shared memory chunks
87
+ let numChunks = u32(ceil(f32(uniforms.numCentroids) / f32(chunkSize)));
88
+ for (var i = 0u; i < numChunks; i++) {
89
+
90
+ // copy this thread's slice of the centroid shared chunk data
91
+ let dstRow = local_id * (chunkSize / workgroupSize);
92
+ let srcRow = min(uniforms.numCentroids, i * chunkSize + local_id * chunkSize / workgroupSize);
93
+ let numRows = min(uniforms.numCentroids, srcRow + chunkSize / workgroupSize) - srcRow;
94
+
95
+ var dst = dstRow * numColumns;
96
+ var src = srcRow * numColumns;
97
+
98
+ for (var c = 0u; c < numRows * numColumns; c++) {
99
+ sharedChunk[dst + c] = centroids[src + c];
100
+ }
101
+
102
+ // wait for all threads to finish writing their part of centroids shared memory buffer
103
+ workgroupBarrier();
104
+
105
+ // loop over the next chunk of centroids finding the closest
106
+ if (pointIndex < uniforms.numPoints) {
107
+ let thisChunkSize = min(chunkSize, uniforms.numCentroids - i * chunkSize);
108
+ for (var c = 0u; c < thisChunkSize; c++) {
109
+ let d = calcDistanceSqr(point, c);
110
+ if (d < mind) {
111
+ mind = d;
112
+ mini = i * chunkSize + c;
113
+ }
114
+ }
115
+ }
116
+
117
+ // next loop will overwrite the shared memory, so wait
118
+ workgroupBarrier();
119
+ }
120
+
121
+ if (pointIndex < uniforms.numPoints) {
122
+ results[pointIndex] = mini;
123
+ }
124
+ }
125
+ `;
126
+ }
127
+ function interleaveData(result, dataTable, numRows, rowOffset) {
128
+ const numColumns = dataTable.length;
129
+ for (let c = 0; c < numColumns; ++c) {
130
+ const column = dataTable[c];
131
+ for (let r = 0; r < numRows; ++r) {
132
+ result[r * numColumns + c] = column[rowOffset + r];
133
+ }
134
+ }
135
+ }
136
+ const MAX_CONCURRENCY_BATCHES = 10;
137
+ class GpuClustering {
138
+ constructor(device, numPoints, numColumns, numCentroids) {
139
+ this.device = device;
140
+ this.numPoints = numPoints;
141
+ this.numColumns = numColumns;
142
+ this.numCentroids = numCentroids;
143
+ const workgroupsPerBatch = Math.min(device.limits.maxComputeWorkgroupsPerDimension, // device dispatch limit
144
+ Math.floor(device.limits.maxBufferSize / (numColumns * workgroupSize * 4)), // point storage limit
145
+ Math.ceil(numPoints / workgroupSize));
146
+ this.batchSize = workgroupsPerBatch * workgroupSize;
147
+ this.numBatches = Math.ceil(numPoints / this.batchSize);
148
+ this.concurrencyBatches = Math.min(MAX_CONCURRENCY_BATCHES, this.numBatches);
149
+ this.concurrencyRuns = Math.ceil(this.numBatches / this.concurrencyBatches);
150
+ const shader = device.createShaderModule({
151
+ code: clusterWgsl(numColumns),
152
+ });
153
+ const pipeline = device.createComputePipeline({
154
+ layout: 'auto',
155
+ compute: {
156
+ module: shader,
157
+ entryPoint: 'main',
158
+ },
159
+ });
160
+ const pointsBackBuffer = new Float32Array(numColumns * this.batchSize);
161
+ const centroidsBackBuffer = new Float32Array(numColumns * numCentroids);
162
+ const uniformBackBuffer = new Uint32Array([0, numCentroids]);
163
+ const pointsBuffers = [];
164
+ const centroidsBuffer = device.createBuffer({
165
+ size: centroidsBackBuffer.byteLength,
166
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE,
167
+ });
168
+ const uniformBuffer = device.createBuffer({
169
+ size: 256 * this.concurrencyBatches,
170
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.UNIFORM,
171
+ });
172
+ const resultBuffer = device.createBuffer({
173
+ size: this.concurrencyBatches * this.batchSize * 4,
174
+ usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE,
175
+ });
176
+ const resultReadBackBuffer = device.createBuffer({
177
+ size: this.concurrencyBatches * this.batchSize * 4,
178
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
179
+ });
180
+ const layout = pipeline.getBindGroupLayout(0);
181
+ const bindGroups = [];
182
+ for (let i = 0; i < this.concurrencyBatches; i++) {
183
+ const pointsBuffer = device.createBuffer({
184
+ size: pointsBackBuffer.byteLength,
185
+ usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE,
186
+ });
187
+ pointsBuffers.push(pointsBuffer);
188
+ bindGroups.push(device.createBindGroup({
189
+ layout,
190
+ entries: [
191
+ {
192
+ binding: 0,
193
+ resource: {
194
+ buffer: uniformBuffer,
195
+ offset: i * 256,
196
+ size: 8,
197
+ },
198
+ },
199
+ {
200
+ binding: 1,
201
+ resource: pointsBuffer,
202
+ },
203
+ {
204
+ binding: 2,
205
+ resource: centroidsBuffer,
206
+ },
207
+ {
208
+ binding: 3,
209
+ resource: {
210
+ buffer: resultBuffer,
211
+ offset: i * this.batchSize * 4,
212
+ size: this.batchSize * 4,
213
+ },
214
+ },
215
+ ],
216
+ }));
217
+ }
218
+ this.resource = {
219
+ pipeline,
220
+ bindGroups,
221
+ gpuBuffers: {
222
+ uniform: uniformBuffer,
223
+ points: pointsBuffers,
224
+ centroids: centroidsBuffer,
225
+ result: resultBuffer,
226
+ resultReadBack: resultReadBackBuffer,
227
+ },
228
+ backBuffers: {
229
+ uniform: uniformBackBuffer,
230
+ points: pointsBackBuffer,
231
+ centroids: centroidsBackBuffer,
232
+ },
233
+ uploadedBatches: [],
234
+ };
235
+ logger.info(`GPU k-means kernel bootstrapped with batch ${workgroupsPerBatch}*${workgroupSize}*${this.numBatches}, concurrency: ${this.concurrencyBatches}, runs: ${this.concurrencyRuns}`);
236
+ }
237
+ async execute(points, centroids, labels) {
238
+ const { device, numPoints, numColumns, numCentroids, numBatches, batchSize, resource, concurrencyBatches, concurrencyRuns, } = this;
239
+ // upload centroid data to gpu
240
+ interleaveData(resource.backBuffers.centroids, centroids, numCentroids, 0);
241
+ device.queue.writeBuffer(resource.gpuBuffers.centroids, 0, resource.backBuffers.centroids.buffer);
242
+ for (let i = 0; i < concurrencyRuns; i++) {
243
+ const batchStart = i * concurrencyBatches;
244
+ let resultCount = 0;
245
+ for (let j = 0; j < concurrencyBatches; j++) {
246
+ const batchIndex = batchStart + j;
247
+ if (batchIndex >= numBatches) {
248
+ break;
249
+ }
250
+ const currentBatchSize = Math.min(numPoints - batchIndex * batchSize, batchSize);
251
+ resultCount += currentBatchSize;
252
+ // write this batch of point data to gpu
253
+ if (resource.uploadedBatches[j] !== batchIndex) {
254
+ interleaveData(resource.backBuffers.points, points, currentBatchSize, batchIndex * batchSize);
255
+ device.queue.writeBuffer(resource.gpuBuffers.points[j], 0, resource.backBuffers.points.buffer, 0, numColumns * currentBatchSize * 4);
256
+ resource.backBuffers.uniform[0] = currentBatchSize;
257
+ device.queue.writeBuffer(resource.gpuBuffers.uniform, 256 * j, resource.backBuffers.uniform.buffer, 0, 8);
258
+ resource.uploadedBatches[j] = batchIndex;
259
+ }
260
+ }
261
+ const encoder = device.createCommandEncoder();
262
+ const computePass = encoder.beginComputePass();
263
+ computePass.setPipeline(resource.pipeline);
264
+ for (let j = 0; j < concurrencyBatches; j++) {
265
+ const batchIndex = batchStart + j;
266
+ if (batchIndex >= numBatches) {
267
+ break;
268
+ }
269
+ const currentBatchSize = Math.min(numPoints - batchIndex * batchSize, batchSize);
270
+ const groups = Math.ceil(currentBatchSize / workgroupSize);
271
+ computePass.setBindGroup(0, resource.bindGroups[j]);
272
+ computePass.dispatchWorkgroups(groups);
273
+ }
274
+ computePass.end();
275
+ encoder.copyBufferToBuffer(resource.gpuBuffers.result, 0, resource.gpuBuffers.resultReadBack, 0, resultCount * 4);
276
+ device.queue.submit([encoder.finish()]);
277
+ await resource.gpuBuffers.resultReadBack.mapAsync(GPUMapMode.READ);
278
+ const mapped = resource.gpuBuffers.resultReadBack.getMappedRange();
279
+ labels.set(new Uint32Array(mapped, 0, resultCount), batchStart * batchSize);
280
+ resource.gpuBuffers.resultReadBack.unmap();
281
+ }
282
+ }
283
+ destroy() {
284
+ this.resource.gpuBuffers.uniform.destroy();
285
+ this.resource.gpuBuffers.centroids.destroy();
286
+ this.resource.gpuBuffers.result.destroy();
287
+ this.resource.gpuBuffers.resultReadBack.destroy();
288
+ for (const buffer of this.resource.gpuBuffers.points) {
289
+ buffer.destroy();
290
+ }
291
+ }
292
+ }
293
+ function groupLabels(labels, k) {
294
+ const clusters = [];
295
+ for (let i = 0; i < k; ++i) {
296
+ clusters[i] = [];
297
+ }
298
+ for (let i = 0; i < labels.length; ++i) {
299
+ clusters[labels[i]].push(i);
300
+ }
301
+ return clusters.map(c => new Uint32Array(c));
302
+ }
303
+ // https://github.com/playcanvas/splat-transform/blob/main/src/lib/spatial/k-means.ts
304
+ export async function kmeans(points, k, iterations, device) {
305
+ const numRows = points.length > 0 ? points[0].length : 0;
306
+ if (numRows < k) {
307
+ return {
308
+ centroids: points,
309
+ // use a typed array here so downstream code can rely on
310
+ // labels supporting subarray(), even in this early-return
311
+ // path used for very small datasets.
312
+ labels: new Uint32Array(numRows).map((_, i) => i),
313
+ };
314
+ }
315
+ const centroids = points.map(_ => new Float32Array(k));
316
+ if (points.length === 1) {
317
+ initializeCentroids1D(points[0], centroids[0]);
318
+ }
319
+ else {
320
+ initializeCentroids(points, centroids);
321
+ }
322
+ const gpuClustering = new GpuClustering(device, numRows, points.length, k);
323
+ const labels = new Uint32Array(numRows);
324
+ let converged = false;
325
+ let steps = 0;
326
+ while (!converged) {
327
+ logger.info(`kmeans iteration ${steps + 1}`);
328
+ await gpuClustering.execute(points, centroids, labels);
329
+ clusterAverage(points, groupLabels(labels, k), centroids);
330
+ steps++;
331
+ if (steps >= iterations) {
332
+ converged = true;
333
+ }
334
+ }
335
+ gpuClustering.destroy();
336
+ return {
337
+ centroids,
338
+ labels,
339
+ };
340
+ }