@luma.gl/graph 10.0.0-alpha.5 → 10.0.0-alpha.6
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.
- package/LICENSE +38 -0
- package/README.md +7 -1
- package/dist/arrow/dggs/dggs-gpu-polygons.cjs +667 -0
- package/dist/arrow/dggs/dggs-gpu-polygons.d.ts +75 -0
- package/dist/arrow/dggs/dggs-gpu-polygons.d.ts.map +1 -0
- package/dist/arrow/dggs/dggs-gpu-polygons.js +651 -0
- package/dist/arrow/dggs/dggs-gpu-polygons.js.map +1 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-adapter.cjs +411 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-adapter.d.ts +31 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-adapter.d.ts.map +1 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-adapter.js +395 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-adapter.js.map +1 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-conversion.cjs +304 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-conversion.d.ts +32 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-conversion.d.ts.map +1 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-conversion.js +293 -0
- package/dist/arrow/geoarrow/arrow-geoarrow-conversion.js.map +1 -0
- package/dist/arrow/geoarrow/arrow-polygon-tessellation.cjs +232 -0
- package/dist/arrow/geoarrow/arrow-polygon-tessellation.d.ts +54 -0
- package/dist/arrow/geoarrow/arrow-polygon-tessellation.d.ts.map +1 -0
- package/dist/arrow/geoarrow/arrow-polygon-tessellation.js +222 -0
- package/dist/arrow/geoarrow/arrow-polygon-tessellation.js.map +1 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-adapters.cjs +314 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-adapters.d.ts +42 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-adapters.d.ts.map +1 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-adapters.js +318 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-adapters.js.map +1 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-readback.cjs +204 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-readback.d.ts +30 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-readback.d.ts.map +1 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-readback.js +195 -0
- package/dist/arrow/gpu/arrow-gpu-analytics-readback.js.map +1 -0
- package/dist/arrow/gpu/arrow-gpu-table-adapters.cjs +257 -0
- package/dist/arrow/gpu/arrow-gpu-table-adapters.d.ts +45 -0
- package/dist/arrow/gpu/arrow-gpu-table-adapters.d.ts.map +1 -0
- package/dist/arrow/gpu/arrow-gpu-table-adapters.js +257 -0
- package/dist/arrow/gpu/arrow-gpu-table-adapters.js.map +1 -0
- package/dist/arrow/gpu/arrow-gpu-table-conversion.cjs +230 -0
- package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts +71 -0
- package/dist/arrow/gpu/arrow-gpu-table-conversion.d.ts.map +1 -0
- package/dist/arrow/gpu/arrow-gpu-table-conversion.js +215 -0
- package/dist/arrow/gpu/arrow-gpu-table-conversion.js.map +1 -0
- package/dist/arrow/gpu/arrow-input-schema.cjs +36 -0
- package/dist/arrow/gpu/arrow-input-schema.d.ts +41 -0
- package/dist/arrow/gpu/arrow-input-schema.d.ts.map +1 -0
- package/dist/arrow/gpu/arrow-input-schema.js +17 -0
- package/dist/arrow/gpu/arrow-input-schema.js.map +1 -0
- package/dist/arrow/index.cjs +90 -0
- package/dist/arrow/index.cjs.map +6 -0
- package/dist/arrow/index.d.ts +9 -0
- package/dist/arrow/index.d.ts.map +1 -0
- package/dist/arrow/index.js +12 -0
- package/dist/arrow/index.js.map +1 -0
- package/dist/arrow/internal.cjs +30 -0
- package/dist/arrow/internal.cjs.map +6 -0
- package/dist/arrow/internal.d.ts +19 -0
- package/dist/arrow/internal.d.ts.map +1 -0
- package/dist/arrow/internal.js +7 -0
- package/dist/arrow/internal.js.map +1 -0
- package/dist/dist.dev.js +283 -12
- package/dist/dist.min.js +29 -29
- package/dist/geospatial/geospatial-utils.cjs +12 -11
- package/dist/geospatial/geospatial-utils.d.ts +1 -1
- package/dist/geospatial/geospatial-utils.d.ts.map +1 -1
- package/dist/geospatial/geospatial-utils.js +3 -2
- package/dist/geospatial/geospatial-utils.js.map +1 -1
- package/dist/geospatial/index.cjs.map +2 -2
- package/dist/gpu-a5/index.cjs.map +1 -1
- package/dist/gpu-dggs/benchmarks.cjs.map +1 -1
- package/dist/gpu-dggs/gpu-dggs-cell-projection.cjs +2 -2
- package/dist/gpu-dggs/gpu-dggs-cell-projection.js +1 -1
- package/dist/gpu-dggs/gpu-dggs-cell-projection.js.map +1 -1
- package/dist/gpu-dggs/index.cjs.map +1 -1
- package/dist/gpu-h3/index.cjs.map +1 -1
- package/dist/gpu-project/benchmarks.cjs.map +2 -2
- package/dist/gpu-project/crs.cjs.map +2 -2
- package/dist/gpu-project/index.cjs.map +2 -2
- package/dist/gpu-project/projection-program.cjs +2 -2
- package/dist/gpu-project/projection-program.d.ts +1 -1
- package/dist/gpu-project/projection-program.d.ts.map +1 -1
- package/dist/gpu-project/projection-program.js +1 -1
- package/dist/gpu-project/projection-program.js.map +1 -1
- package/dist/gpu-sql/lu-sql.d.ts +1 -1
- package/dist/gpu-sql/lu-sql.js +1 -1
- package/dist/gpu-tables/engine/gpu-table-shader-bindings.d.ts +1 -1
- package/dist/gpu-tables/engine/gpu-table-shader-bindings.d.ts.map +1 -1
- package/dist/gpu-vector-search/index.cjs.map +1 -1
- package/dist/utils/assemble-compute-kernel.cjs +2 -2
- package/dist/utils/assemble-compute-kernel.d.ts +2 -1
- package/dist/utils/assemble-compute-kernel.d.ts.map +1 -1
- package/dist/utils/assemble-compute-kernel.js +2 -2
- package/dist/utils/assemble-compute-kernel.js.map +1 -1
- package/package.json +19 -7
- package/src/arrow/dggs/dggs-gpu-polygons.ts +820 -0
- package/src/arrow/geoarrow/arrow-geoarrow-adapter.ts +519 -0
- package/src/arrow/geoarrow/arrow-geoarrow-conversion.ts +415 -0
- package/src/arrow/geoarrow/arrow-polygon-tessellation.ts +369 -0
- package/src/arrow/gpu/arrow-gpu-analytics-adapters.ts +474 -0
- package/src/arrow/gpu/arrow-gpu-analytics-readback.ts +294 -0
- package/src/arrow/gpu/arrow-gpu-table-adapters.ts +378 -0
- package/src/arrow/gpu/arrow-gpu-table-conversion.ts +324 -0
- package/src/arrow/gpu/arrow-input-schema.ts +98 -0
- package/src/arrow/index.ts +79 -0
- package/src/arrow/internal.ts +23 -0
- package/src/geospatial/geospatial-utils.ts +3 -7
- package/src/gpu-dggs/gpu-dggs-cell-projection.ts +1 -1
- package/src/gpu-project/projection-program.ts +1 -1
- package/src/gpu-sql/lu-sql.ts +1 -1
- package/src/gpu-tables/engine/gpu-table-shader-bindings.ts +1 -1
- package/src/utils/assemble-compute-kernel.ts +3 -7
|
@@ -0,0 +1,820 @@
|
|
|
1
|
+
// luma.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {Buffer, type Binding, type Device} from '@luma.gl/core';
|
|
6
|
+
import {createArrowComputeKernel} from '@luma.gl/gpgpu/_internal/arrow';
|
|
7
|
+
import {DynamicBuffer} from '@luma.gl/core/dynamic';
|
|
8
|
+
import {dggs} from '@luma.gl/shaders';
|
|
9
|
+
import {GPUData, GPUVector, type GPUVectorFormat} from '@luma.gl/gpgpu/gpu-data';
|
|
10
|
+
import {
|
|
11
|
+
Data,
|
|
12
|
+
DataType,
|
|
13
|
+
Field,
|
|
14
|
+
FixedSizeList,
|
|
15
|
+
Float32,
|
|
16
|
+
List,
|
|
17
|
+
Uint64,
|
|
18
|
+
Utf8,
|
|
19
|
+
Vector
|
|
20
|
+
} from 'apache-arrow';
|
|
21
|
+
import {makeGPUVectorFromArrow} from '@luma.gl/gpgpu/arrow';
|
|
22
|
+
import {getArrowUtf8DataBufferSource} from '@luma.gl/gpgpu/_internal/arrow';
|
|
23
|
+
|
|
24
|
+
type DggsCellPathCoordinateType = List<FixedSizeList<Float32>>;
|
|
25
|
+
export type DggsCellPathCoordinateFormat = 'float32' | 'fp64-split';
|
|
26
|
+
|
|
27
|
+
/** DGGS cell key encodings accepted by the GPU key and polygon helpers. */
|
|
28
|
+
export type DggsCellEncoding = 'geohash' | 'quadkey' | 's2' | 'a5' | 'h3';
|
|
29
|
+
|
|
30
|
+
/** Options for expanding DGGS Uint64 cell keys into GPU polygon paths. */
|
|
31
|
+
export type DggsCellPathGPUVectorOptions = {
|
|
32
|
+
/** Stable label used for transient and generated GPU resources. */
|
|
33
|
+
id?: string;
|
|
34
|
+
/** Input cell key encoding. */
|
|
35
|
+
encoding: DggsCellEncoding;
|
|
36
|
+
/**
|
|
37
|
+
* Output coordinate layout. `fp64-split` emits [longitudeHigh, latitudeHigh,
|
|
38
|
+
* longitudeLow, latitudeLow], but DGGS decoding currently computes Float32
|
|
39
|
+
* coordinates so low components are zero.
|
|
40
|
+
*/
|
|
41
|
+
coordinateFormat?: DggsCellPathCoordinateFormat;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Options for parsing UTF-8 DGGS cell keys into Uint64 GPU keys. */
|
|
45
|
+
export type DggsCellKeyGPUVectorOptions = {
|
|
46
|
+
/** Stable label used for transient and generated GPU resources. */
|
|
47
|
+
id?: string;
|
|
48
|
+
/** Input cell key encoding. */
|
|
49
|
+
encoding: DggsCellEncoding;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
/** Prepared DGGS Uint64 keys plus transient allocation accounting. */
|
|
53
|
+
export type PreparedDggsCellKeyGPUVector = {
|
|
54
|
+
/** GPU-only Uint64 keys, encoded for the selected DGGS format. */
|
|
55
|
+
keys: GPUVector;
|
|
56
|
+
/** Bytes in the generated key buffer. */
|
|
57
|
+
keyByteLength: number;
|
|
58
|
+
/** Bytes in transient string byte, offset, and config buffers. */
|
|
59
|
+
transientByteLength: number;
|
|
60
|
+
/** Releases owned generated GPU key resources. */
|
|
61
|
+
destroy: () => void;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** Prepared DGGS polygon paths plus generated allocation accounting. */
|
|
65
|
+
export type PreparedDggsCellPathGPUVector = {
|
|
66
|
+
/** GPU-only List<FixedSizeList<Float32>[2|4]> path polygons, one row per cell key. */
|
|
67
|
+
paths: GPUVector;
|
|
68
|
+
/** Boundary points generated per input cell, including the closing point. */
|
|
69
|
+
pointCount: number;
|
|
70
|
+
/** Bytes in the generated path coordinate buffer. */
|
|
71
|
+
pathByteLength: number;
|
|
72
|
+
/** Bytes in transient compute input buffers. */
|
|
73
|
+
transientByteLength: number;
|
|
74
|
+
/** Releases owned generated GPU path resources. */
|
|
75
|
+
destroy: () => void;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const DGGS_DEFAULT_CELL_POLYGON_POINT_COUNT = 5;
|
|
79
|
+
const DGGS_A5_CELL_POLYGON_POINT_COUNT = 6;
|
|
80
|
+
const DGGS_H3_CELL_POLYGON_POINT_COUNT = 7;
|
|
81
|
+
const GEOHASH_MAXIMUM_LENGTH = 12;
|
|
82
|
+
const GEOHASH_LENGTH_BIT_OFFSET = 60n;
|
|
83
|
+
const QUADKEY_MAXIMUM_LENGTH = 29;
|
|
84
|
+
const QUADKEY_LENGTH_BIT_OFFSET = 58n;
|
|
85
|
+
const A5_HEX_MAXIMUM_LENGTH = 16;
|
|
86
|
+
const H3_HEX_MAXIMUM_LENGTH = 16;
|
|
87
|
+
const UINT64_WORD_MASK = 0xffffffffn;
|
|
88
|
+
const GEOHASH_BASE32_CODES = '0123456789bcdefghjkmnpqrstuvwxyz';
|
|
89
|
+
const GEOHASH_BASE32_CODE_MAP = new Map<string, number>(
|
|
90
|
+
Array.from(GEOHASH_BASE32_CODES, (character, index) => [character, index])
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Packs a geohash string into a Uint64 key consumed by the DGGS WGSL helpers.
|
|
95
|
+
*
|
|
96
|
+
* The high 4 bits store length and the low 60 bits store 5-bit base32 codes.
|
|
97
|
+
*/
|
|
98
|
+
export function packDggsGeohashKey(geohash: string): bigint {
|
|
99
|
+
const normalizedGeohash = geohash.toLowerCase();
|
|
100
|
+
if (normalizedGeohash.length < 1 || normalizedGeohash.length > GEOHASH_MAXIMUM_LENGTH) {
|
|
101
|
+
throw new Error(`Geohash keys must contain 1-${GEOHASH_MAXIMUM_LENGTH} characters`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let encodedValue = 0n;
|
|
105
|
+
for (const character of normalizedGeohash) {
|
|
106
|
+
const characterCode = GEOHASH_BASE32_CODE_MAP.get(character);
|
|
107
|
+
if (characterCode === undefined) {
|
|
108
|
+
throw new Error(`Invalid geohash character "${character}"`);
|
|
109
|
+
}
|
|
110
|
+
encodedValue = (encodedValue << 5n) | BigInt(characterCode);
|
|
111
|
+
}
|
|
112
|
+
return (BigInt(normalizedGeohash.length) << GEOHASH_LENGTH_BIT_OFFSET) | encodedValue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Packs a quadkey string into a Uint64 key consumed by the DGGS WGSL helpers.
|
|
117
|
+
*
|
|
118
|
+
* The high 6 bits store length and the low 58 bits store 2-bit base4 digits.
|
|
119
|
+
*/
|
|
120
|
+
export function packDggsQuadkeyKey(quadkey: string): bigint {
|
|
121
|
+
if (quadkey.length < 1 || quadkey.length > QUADKEY_MAXIMUM_LENGTH) {
|
|
122
|
+
throw new Error(`Quadkey keys must contain 1-${QUADKEY_MAXIMUM_LENGTH} digits`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
let encodedValue = 0n;
|
|
126
|
+
for (const digitCharacter of quadkey) {
|
|
127
|
+
const digit = digitCharacter.charCodeAt(0) - 48;
|
|
128
|
+
if (digit < 0 || digit > 3) {
|
|
129
|
+
throw new Error(`Invalid quadkey digit "${digitCharacter}"`);
|
|
130
|
+
}
|
|
131
|
+
encodedValue = (encodedValue << 2n) | BigInt(digit);
|
|
132
|
+
}
|
|
133
|
+
return (BigInt(quadkey.length) << QUADKEY_LENGTH_BIT_OFFSET) | encodedValue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Creates an S2 CellId from a face and child-position digits, mainly for examples and tests. */
|
|
137
|
+
export function packDggsS2CellKey(face: number, childPositions: readonly number[]): bigint {
|
|
138
|
+
if (!Number.isInteger(face) || face < 0 || face > 5) {
|
|
139
|
+
throw new Error('S2 face must be an integer in [0, 5]');
|
|
140
|
+
}
|
|
141
|
+
if (childPositions.length > 30) {
|
|
142
|
+
throw new Error('S2 child position arrays cannot exceed 30 levels');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
let positionBits = 0n;
|
|
146
|
+
for (const childPosition of childPositions) {
|
|
147
|
+
if (!Number.isInteger(childPosition) || childPosition < 0 || childPosition > 3) {
|
|
148
|
+
throw new Error('S2 child positions must be integers in [0, 3]');
|
|
149
|
+
}
|
|
150
|
+
positionBits = (positionBits << 2n) | BigInt(childPosition);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const level = BigInt(childPositions.length);
|
|
154
|
+
const sentinelBitOffset = 2n * (30n - level);
|
|
155
|
+
return (
|
|
156
|
+
(BigInt(face) << 61n) | (positionBits << (sentinelBitOffset + 1n)) | (1n << sentinelBitOffset)
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Normalizes a native A5 Uint64 cell id from a bigint or hexadecimal string. */
|
|
161
|
+
export function packDggsA5CellKey(cellId: bigint | string): bigint {
|
|
162
|
+
return packDggsHexCellKey(cellId, 'A5', A5_HEX_MAXIMUM_LENGTH);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Normalizes a native H3 Uint64 cell id from a bigint or hexadecimal string. */
|
|
166
|
+
export function packDggsH3CellKey(cellId: bigint | string): bigint {
|
|
167
|
+
return packDggsHexCellKey(cellId, 'H3', H3_HEX_MAXIMUM_LENGTH);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function packDggsHexCellKey(
|
|
171
|
+
cellId: bigint | string,
|
|
172
|
+
encodingName: string,
|
|
173
|
+
maximumHexLength: number
|
|
174
|
+
): bigint {
|
|
175
|
+
if (typeof cellId === 'bigint') {
|
|
176
|
+
return BigInt.asUintN(64, cellId);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const normalizedCellId = cellId.trim().replace(/^0x/i, '');
|
|
180
|
+
if (normalizedCellId.length < 1 || normalizedCellId.length > maximumHexLength) {
|
|
181
|
+
throw new Error(
|
|
182
|
+
`${encodingName} cell ids must contain 1-${maximumHexLength} hexadecimal digits`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
if (!/^[0-9a-fA-F]+$/.test(normalizedCellId)) {
|
|
186
|
+
throw new Error(`Invalid ${encodingName} cell id "${cellId}"`);
|
|
187
|
+
}
|
|
188
|
+
return BigInt.asUintN(64, BigInt(`0x${normalizedCellId}`));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Expands prepared DGGS Uint64 keys into closed Float32 polygon paths on WebGPU. */
|
|
192
|
+
export function convertDggsCellKeysToGPUPaths(
|
|
193
|
+
device: Device,
|
|
194
|
+
keys: Vector<Uint64> | GPUVector,
|
|
195
|
+
options: DggsCellPathGPUVectorOptions
|
|
196
|
+
): PreparedDggsCellPathGPUVector {
|
|
197
|
+
if (device.type !== 'webgpu') {
|
|
198
|
+
throw new Error('convertDggsCellKeysToGPUPaths requires a WebGPU device');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const resourceIdentifier = options.id || `dggs-${options.encoding}-cell-polygons`;
|
|
202
|
+
const cellPolygonPointCount = getDggsCellPolygonPointCount(options.encoding);
|
|
203
|
+
const coordinateFormat = options.coordinateFormat || 'float32';
|
|
204
|
+
const pathValueComponentCount = getDggsCellPathCoordinateComponentCount(coordinateFormat);
|
|
205
|
+
const pathFormat = `vertex-list<float32x${pathValueComponentCount}>` as GPUVectorFormat;
|
|
206
|
+
const pathType = makeDggsCellPathCoordinateType(pathValueComponentCount);
|
|
207
|
+
const ownsKeyVector = !(keys instanceof GPUVector);
|
|
208
|
+
const keyVector = ownsKeyVector
|
|
209
|
+
? makeGPUVectorFromArrow(device, keys, {
|
|
210
|
+
id: `${resourceIdentifier}-keys`,
|
|
211
|
+
name: `${options.encoding}Keys`,
|
|
212
|
+
format: 'uint32x2'
|
|
213
|
+
})
|
|
214
|
+
: keys;
|
|
215
|
+
assertDggsCellKeyVector(keyVector, 'convertDggsCellKeysToGPUPaths');
|
|
216
|
+
|
|
217
|
+
const pathDataChunks: Array<GPUData> = [];
|
|
218
|
+
let pathByteLength = 0;
|
|
219
|
+
let transientByteLength = ownsKeyVector ? getUint64KeyVectorByteLength(keyVector) : 0;
|
|
220
|
+
|
|
221
|
+
for (const [chunkIndex, keyData] of keyVector.data.entries()) {
|
|
222
|
+
const valueOffsets = makeFixedCellPathValueOffsets(keyData.length, cellPolygonPointCount);
|
|
223
|
+
const generatedPathByteLength =
|
|
224
|
+
keyData.length *
|
|
225
|
+
cellPolygonPointCount *
|
|
226
|
+
pathValueComponentCount *
|
|
227
|
+
Float32Array.BYTES_PER_ELEMENT;
|
|
228
|
+
const pathValuesBuffer = new DynamicBuffer(device, {
|
|
229
|
+
id: `${resourceIdentifier}-path-values-${chunkIndex}`,
|
|
230
|
+
usage: Buffer.VERTEX | Buffer.STORAGE | Buffer.COPY_DST | Buffer.COPY_SRC,
|
|
231
|
+
byteLength: Math.max(generatedPathByteLength, Float32Array.BYTES_PER_ELEMENT)
|
|
232
|
+
});
|
|
233
|
+
const extractionConfigBuffer = device.createBuffer({
|
|
234
|
+
id: `${resourceIdentifier}-config-${chunkIndex}`,
|
|
235
|
+
usage: Buffer.STORAGE | Buffer.COPY_DST | Buffer.COPY_SRC,
|
|
236
|
+
data: new Uint32Array([keyData.length])
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
dispatchDggsCellPathExtractionCompute(device, {
|
|
240
|
+
resourceIdentifier: `${resourceIdentifier}-${chunkIndex}`,
|
|
241
|
+
encoding: options.encoding,
|
|
242
|
+
coordinateFormat,
|
|
243
|
+
keys: getGPUDataBinding(keyData, keyData.length * keyData.byteStride),
|
|
244
|
+
extractionConfig: extractionConfigBuffer,
|
|
245
|
+
pathValues: pathValuesBuffer,
|
|
246
|
+
cellCount: keyData.length
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
extractionConfigBuffer.destroy();
|
|
250
|
+
transientByteLength += Uint32Array.BYTES_PER_ELEMENT;
|
|
251
|
+
pathByteLength += generatedPathByteLength;
|
|
252
|
+
pathDataChunks.push(
|
|
253
|
+
new GPUData({
|
|
254
|
+
buffer: pathValuesBuffer,
|
|
255
|
+
dataType: pathType,
|
|
256
|
+
format: pathFormat,
|
|
257
|
+
length: keyData.length,
|
|
258
|
+
valueOffsets,
|
|
259
|
+
valueLength: keyData.length * cellPolygonPointCount,
|
|
260
|
+
valueByteLength: generatedPathByteLength,
|
|
261
|
+
stride: pathValueComponentCount,
|
|
262
|
+
byteStride: pathValueComponentCount * Float32Array.BYTES_PER_ELEMENT,
|
|
263
|
+
rowByteLength: pathValueComponentCount * Float32Array.BYTES_PER_ELEMENT,
|
|
264
|
+
ownsBuffer: true,
|
|
265
|
+
readbackMetadata: {
|
|
266
|
+
kind: 'variable-length-attribute',
|
|
267
|
+
valueOffsets,
|
|
268
|
+
nullCount: 0,
|
|
269
|
+
valueByteLength: generatedPathByteLength
|
|
270
|
+
}
|
|
271
|
+
})
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (ownsKeyVector) {
|
|
276
|
+
keyVector.destroy();
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const paths = new GPUVector({
|
|
280
|
+
type: 'data',
|
|
281
|
+
name: 'paths',
|
|
282
|
+
dataType: pathType,
|
|
283
|
+
format: pathFormat,
|
|
284
|
+
data: pathDataChunks,
|
|
285
|
+
stride: pathValueComponentCount,
|
|
286
|
+
byteStride: pathValueComponentCount * Float32Array.BYTES_PER_ELEMENT,
|
|
287
|
+
rowByteLength: pathValueComponentCount * Float32Array.BYTES_PER_ELEMENT,
|
|
288
|
+
ownsData: true
|
|
289
|
+
});
|
|
290
|
+
let destroyed = false;
|
|
291
|
+
|
|
292
|
+
return {
|
|
293
|
+
paths,
|
|
294
|
+
pointCount: cellPolygonPointCount,
|
|
295
|
+
pathByteLength,
|
|
296
|
+
transientByteLength,
|
|
297
|
+
destroy: () => {
|
|
298
|
+
if (destroyed) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
destroyed = true;
|
|
302
|
+
paths.destroy();
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Parses UTF-8 DGGS cell keys into GPU-only Uint64 keys on WebGPU. */
|
|
308
|
+
export function convertDggsCellIdsToGPUKeys(
|
|
309
|
+
device: Device,
|
|
310
|
+
strings: Vector<Utf8>,
|
|
311
|
+
options: DggsCellKeyGPUVectorOptions
|
|
312
|
+
): PreparedDggsCellKeyGPUVector {
|
|
313
|
+
if (device.type !== 'webgpu') {
|
|
314
|
+
throw new Error('convertDggsCellIdsToGPUKeys requires a WebGPU device');
|
|
315
|
+
}
|
|
316
|
+
if (!DataType.isUtf8(strings.type)) {
|
|
317
|
+
throw new Error('convertDggsCellIdsToGPUKeys requires a Vector<Utf8>');
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
const resourceIdentifier = options.id || `dggs-${options.encoding}-cell-keys`;
|
|
321
|
+
const keyType = new Uint64();
|
|
322
|
+
const keyDataChunks: Array<GPUData> = [];
|
|
323
|
+
let keyByteLength = 0;
|
|
324
|
+
let transientByteLength = 0;
|
|
325
|
+
|
|
326
|
+
for (const [chunkIndex, stringData] of strings.data.entries()) {
|
|
327
|
+
const stringBytes = getArrowUtf8DataBufferSource(stringData as Data<Utf8>);
|
|
328
|
+
const stringOffsets = getNormalizedUtf8Offsets(stringData as Data<Utf8>);
|
|
329
|
+
const packedStringBytes = packUtf8BytesAsUint32Words(stringBytes);
|
|
330
|
+
const stringBytesBuffer = device.createBuffer({
|
|
331
|
+
id: `${resourceIdentifier}-string-bytes-${chunkIndex}`,
|
|
332
|
+
usage: Buffer.STORAGE | Buffer.COPY_DST | Buffer.COPY_SRC,
|
|
333
|
+
data: packedStringBytes
|
|
334
|
+
});
|
|
335
|
+
const stringOffsetsBuffer = device.createBuffer({
|
|
336
|
+
id: `${resourceIdentifier}-string-offsets-${chunkIndex}`,
|
|
337
|
+
usage: Buffer.STORAGE | Buffer.COPY_DST | Buffer.COPY_SRC,
|
|
338
|
+
data: new Uint32Array(stringOffsets.buffer, stringOffsets.byteOffset, stringOffsets.length)
|
|
339
|
+
});
|
|
340
|
+
const stringConfigBuffer = device.createBuffer({
|
|
341
|
+
id: `${resourceIdentifier}-string-config-${chunkIndex}`,
|
|
342
|
+
usage: Buffer.STORAGE | Buffer.COPY_DST | Buffer.COPY_SRC,
|
|
343
|
+
data: new Uint32Array([stringData.length])
|
|
344
|
+
});
|
|
345
|
+
const keyBufferByteLength = stringData.length * BigUint64Array.BYTES_PER_ELEMENT;
|
|
346
|
+
const keyBuffer = new DynamicBuffer(device, {
|
|
347
|
+
id: `${resourceIdentifier}-keys-${chunkIndex}`,
|
|
348
|
+
usage: Buffer.VERTEX | Buffer.STORAGE | Buffer.COPY_DST | Buffer.COPY_SRC,
|
|
349
|
+
byteLength: Math.max(keyBufferByteLength, BigUint64Array.BYTES_PER_ELEMENT)
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
dispatchDggsCellKeyParsingCompute(device, {
|
|
353
|
+
resourceIdentifier: `${resourceIdentifier}-${chunkIndex}`,
|
|
354
|
+
encoding: options.encoding,
|
|
355
|
+
stringBytes: stringBytesBuffer,
|
|
356
|
+
stringOffsets: stringOffsetsBuffer,
|
|
357
|
+
stringConfig: stringConfigBuffer,
|
|
358
|
+
keys: keyBuffer,
|
|
359
|
+
stringCount: stringData.length
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
stringBytesBuffer.destroy();
|
|
363
|
+
stringOffsetsBuffer.destroy();
|
|
364
|
+
stringConfigBuffer.destroy();
|
|
365
|
+
|
|
366
|
+
keyByteLength += keyBufferByteLength;
|
|
367
|
+
transientByteLength +=
|
|
368
|
+
packedStringBytes.byteLength + stringOffsets.byteLength + Uint32Array.BYTES_PER_ELEMENT;
|
|
369
|
+
keyDataChunks.push(
|
|
370
|
+
new GPUData({
|
|
371
|
+
buffer: keyBuffer,
|
|
372
|
+
dataType: keyType,
|
|
373
|
+
format: 'uint32x2',
|
|
374
|
+
length: stringData.length,
|
|
375
|
+
stride: 1,
|
|
376
|
+
byteStride: BigUint64Array.BYTES_PER_ELEMENT,
|
|
377
|
+
rowByteLength: BigUint64Array.BYTES_PER_ELEMENT,
|
|
378
|
+
ownsBuffer: true
|
|
379
|
+
})
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const keys = new GPUVector({
|
|
384
|
+
type: 'data',
|
|
385
|
+
name: `${options.encoding}Keys`,
|
|
386
|
+
dataType: keyType,
|
|
387
|
+
format: 'uint32x2',
|
|
388
|
+
data: keyDataChunks,
|
|
389
|
+
stride: 1,
|
|
390
|
+
byteStride: BigUint64Array.BYTES_PER_ELEMENT,
|
|
391
|
+
rowByteLength: BigUint64Array.BYTES_PER_ELEMENT,
|
|
392
|
+
ownsData: true
|
|
393
|
+
});
|
|
394
|
+
let destroyed = false;
|
|
395
|
+
|
|
396
|
+
return {
|
|
397
|
+
keys,
|
|
398
|
+
keyByteLength,
|
|
399
|
+
transientByteLength,
|
|
400
|
+
destroy: () => {
|
|
401
|
+
if (destroyed) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
destroyed = true;
|
|
405
|
+
keys.destroy();
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function dispatchDggsCellPathExtractionCompute(
|
|
411
|
+
device: Device,
|
|
412
|
+
props: {
|
|
413
|
+
resourceIdentifier: string;
|
|
414
|
+
encoding: DggsCellEncoding;
|
|
415
|
+
coordinateFormat: DggsCellPathCoordinateFormat;
|
|
416
|
+
keys: Binding;
|
|
417
|
+
extractionConfig: Buffer;
|
|
418
|
+
pathValues: DynamicBuffer;
|
|
419
|
+
cellCount: number;
|
|
420
|
+
}
|
|
421
|
+
): void {
|
|
422
|
+
const kernelBindings = {
|
|
423
|
+
dggsCellKeys: props.keys,
|
|
424
|
+
dggsCellExtractionConfig: props.extractionConfig,
|
|
425
|
+
dggsCellPathValues: props.pathValues
|
|
426
|
+
};
|
|
427
|
+
const kernel = createArrowComputeKernel(device, {
|
|
428
|
+
id: `${props.resourceIdentifier}-extract`,
|
|
429
|
+
source: getDggsCellPathExtractionSource(props.encoding, props.coordinateFormat),
|
|
430
|
+
modules: [dggs],
|
|
431
|
+
shaderLayout: {
|
|
432
|
+
bindings: [
|
|
433
|
+
{name: 'dggsCellKeys', type: 'read-only-storage', group: 0, location: 0},
|
|
434
|
+
{name: 'dggsCellExtractionConfig', type: 'read-only-storage', group: 0, location: 1},
|
|
435
|
+
{name: 'dggsCellPathValues', type: 'storage', group: 0, location: 2}
|
|
436
|
+
]
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
if (props.cellCount > 0) {
|
|
440
|
+
const computePass = device.beginComputePass({});
|
|
441
|
+
kernel.dispatch(computePass, {x: Math.ceil(props.cellCount / 64), bindings: kernelBindings});
|
|
442
|
+
computePass.end();
|
|
443
|
+
device.submit();
|
|
444
|
+
}
|
|
445
|
+
kernel.destroy();
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function dispatchDggsCellKeyParsingCompute(
|
|
449
|
+
device: Device,
|
|
450
|
+
props: {
|
|
451
|
+
resourceIdentifier: string;
|
|
452
|
+
encoding: DggsCellEncoding;
|
|
453
|
+
stringBytes: Buffer;
|
|
454
|
+
stringOffsets: Buffer;
|
|
455
|
+
stringConfig: Buffer;
|
|
456
|
+
keys: DynamicBuffer;
|
|
457
|
+
stringCount: number;
|
|
458
|
+
}
|
|
459
|
+
): void {
|
|
460
|
+
const kernelBindings = {
|
|
461
|
+
dggsStringBytes: props.stringBytes,
|
|
462
|
+
dggsStringOffsets: props.stringOffsets,
|
|
463
|
+
dggsStringParsingConfig: props.stringConfig,
|
|
464
|
+
dggsParsedKeys: props.keys
|
|
465
|
+
};
|
|
466
|
+
const kernel = createArrowComputeKernel(device, {
|
|
467
|
+
id: `${props.resourceIdentifier}-parse`,
|
|
468
|
+
source: getDggsCellKeyParsingSource(props.encoding),
|
|
469
|
+
modules: [dggs],
|
|
470
|
+
shaderLayout: {
|
|
471
|
+
bindings: [
|
|
472
|
+
{name: 'dggsStringBytes', type: 'read-only-storage', group: 0, location: 0},
|
|
473
|
+
{name: 'dggsStringOffsets', type: 'read-only-storage', group: 0, location: 1},
|
|
474
|
+
{name: 'dggsStringParsingConfig', type: 'read-only-storage', group: 0, location: 2},
|
|
475
|
+
{name: 'dggsParsedKeys', type: 'storage', group: 0, location: 3}
|
|
476
|
+
]
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
if (props.stringCount > 0) {
|
|
480
|
+
const computePass = device.beginComputePass({});
|
|
481
|
+
kernel.dispatch(computePass, {x: Math.ceil(props.stringCount / 64), bindings: kernelBindings});
|
|
482
|
+
computePass.end();
|
|
483
|
+
device.submit();
|
|
484
|
+
}
|
|
485
|
+
kernel.destroy();
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function getDggsCellPathExtractionSource(
|
|
489
|
+
encoding: DggsCellEncoding,
|
|
490
|
+
coordinateFormat: DggsCellPathCoordinateFormat
|
|
491
|
+
): string {
|
|
492
|
+
const boundaryFunction = getDggsBoundaryFunction(encoding, coordinateFormat);
|
|
493
|
+
const cellPolygonPointCount = getDggsCellPolygonPointCount(encoding);
|
|
494
|
+
const pathValueType = coordinateFormat === 'fp64-split' ? 'vec4f' : 'vec2f';
|
|
495
|
+
return /* wgsl */ `\
|
|
496
|
+
@group(0) @binding(0) var<storage, read> dggsCellKeys : array<vec2u>;
|
|
497
|
+
@group(0) @binding(1) var<storage, read> dggsCellExtractionConfig : array<u32>;
|
|
498
|
+
@group(0) @binding(2) var<storage, read_write> dggsCellPathValues : array<${pathValueType}>;
|
|
499
|
+
|
|
500
|
+
@compute @workgroup_size(64)
|
|
501
|
+
fn main(@builtin(global_invocation_id) globalInvocationId : vec3u) {
|
|
502
|
+
let cellIndex = globalInvocationId.x;
|
|
503
|
+
if (cellIndex >= dggsCellExtractionConfig[0]) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
let cellKey = dggs_u64_from_little_endian_words(dggsCellKeys[cellIndex]);
|
|
508
|
+
let pathStart = cellIndex * ${cellPolygonPointCount}u;
|
|
509
|
+
var pointIndex = 0u;
|
|
510
|
+
loop {
|
|
511
|
+
if (pointIndex >= ${cellPolygonPointCount}u) {
|
|
512
|
+
break;
|
|
513
|
+
}
|
|
514
|
+
dggsCellPathValues[pathStart + pointIndex] = ${boundaryFunction}(cellKey, pointIndex);
|
|
515
|
+
pointIndex += 1u;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
`;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
function getDggsBoundaryFunction(
|
|
522
|
+
encoding: DggsCellEncoding,
|
|
523
|
+
coordinateFormat: DggsCellPathCoordinateFormat
|
|
524
|
+
): string {
|
|
525
|
+
const suffix = coordinateFormat === 'fp64-split' ? '_fp64_split' : '';
|
|
526
|
+
switch (encoding) {
|
|
527
|
+
case 'geohash':
|
|
528
|
+
return `dggs_geohash_get_boundary_point${suffix}`;
|
|
529
|
+
case 'quadkey':
|
|
530
|
+
return `dggs_quadkey_get_boundary_point${suffix}`;
|
|
531
|
+
case 's2':
|
|
532
|
+
return `dggs_s2_get_boundary_point${suffix}`;
|
|
533
|
+
case 'a5':
|
|
534
|
+
return `dggs_a5_get_boundary_point${suffix}`;
|
|
535
|
+
case 'h3':
|
|
536
|
+
return `dggs_h3_get_boundary_point${suffix}`;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function getDggsCellKeyParsingSource(encoding: DggsCellEncoding): string {
|
|
541
|
+
const parsingFunction = getDggsStringParsingFunction(encoding);
|
|
542
|
+
return /* wgsl */ `\
|
|
543
|
+
@group(0) @binding(0) var<storage, read> dggsStringBytes : array<u32>;
|
|
544
|
+
@group(0) @binding(1) var<storage, read> dggsStringOffsets : array<u32>;
|
|
545
|
+
@group(0) @binding(2) var<storage, read> dggsStringParsingConfig : array<u32>;
|
|
546
|
+
@group(0) @binding(3) var<storage, read_write> dggsParsedKeys : array<vec2u>;
|
|
547
|
+
|
|
548
|
+
fn dggs_read_string_byte(byteIndex: u32) -> u32 {
|
|
549
|
+
let word = dggsStringBytes[byteIndex / 4u];
|
|
550
|
+
return (word >> ((byteIndex % 4u) * 8u)) & 0xffu;
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
fn dggs_get_lowercase_ascii(byte: u32) -> u32 {
|
|
554
|
+
if (byte >= 65u && byte <= 90u) {
|
|
555
|
+
return byte + 32u;
|
|
556
|
+
}
|
|
557
|
+
return byte;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
fn dggs_get_geohash_character_code(byte: u32) -> u32 {
|
|
561
|
+
let lowercaseByte = dggs_get_lowercase_ascii(byte);
|
|
562
|
+
if (lowercaseByte >= 48u && lowercaseByte <= 57u) {
|
|
563
|
+
return lowercaseByte - 48u;
|
|
564
|
+
}
|
|
565
|
+
if (lowercaseByte >= 98u && lowercaseByte <= 104u) {
|
|
566
|
+
return lowercaseByte - 88u;
|
|
567
|
+
}
|
|
568
|
+
if (lowercaseByte >= 106u && lowercaseByte <= 107u) {
|
|
569
|
+
return lowercaseByte - 89u;
|
|
570
|
+
}
|
|
571
|
+
if (lowercaseByte >= 109u && lowercaseByte <= 110u) {
|
|
572
|
+
return lowercaseByte - 90u;
|
|
573
|
+
}
|
|
574
|
+
if (lowercaseByte >= 112u && lowercaseByte <= 122u) {
|
|
575
|
+
return lowercaseByte - 91u;
|
|
576
|
+
}
|
|
577
|
+
return 0u;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
fn dggs_get_hex_character_code(byte: u32) -> u32 {
|
|
581
|
+
let lowercaseByte = dggs_get_lowercase_ascii(byte);
|
|
582
|
+
if (lowercaseByte >= 48u && lowercaseByte <= 57u) {
|
|
583
|
+
return lowercaseByte - 48u;
|
|
584
|
+
}
|
|
585
|
+
if (lowercaseByte >= 97u && lowercaseByte <= 102u) {
|
|
586
|
+
return lowercaseByte - 87u;
|
|
587
|
+
}
|
|
588
|
+
return 0u;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
fn dggs_get_hex_character_code_or_invalid(byte: u32) -> u32 {
|
|
592
|
+
let lowercaseByte = dggs_get_lowercase_ascii(byte);
|
|
593
|
+
if (lowercaseByte >= 48u && lowercaseByte <= 57u) {
|
|
594
|
+
return lowercaseByte - 48u;
|
|
595
|
+
}
|
|
596
|
+
if (lowercaseByte >= 97u && lowercaseByte <= 102u) {
|
|
597
|
+
return lowercaseByte - 87u;
|
|
598
|
+
}
|
|
599
|
+
return 16u;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
fn dggs_parse_geohash_string(startOffset: u32, endOffset: u32) -> vec2u {
|
|
603
|
+
let stringLength = min(endOffset - startOffset, ${GEOHASH_MAXIMUM_LENGTH}u);
|
|
604
|
+
var key = vec2u(0u);
|
|
605
|
+
var characterIndex = 0u;
|
|
606
|
+
loop {
|
|
607
|
+
if (characterIndex >= stringLength) {
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
let characterCode = dggs_get_geohash_character_code(
|
|
611
|
+
dggs_read_string_byte(startOffset + characterIndex)
|
|
612
|
+
);
|
|
613
|
+
key = dggs_u64_shift_left(key, 5u) | vec2u(0u, characterCode);
|
|
614
|
+
characterIndex += 1u;
|
|
615
|
+
}
|
|
616
|
+
return dggs_u64_set_bits(key, ${GEOHASH_LENGTH_BIT_OFFSET}u, 4u, stringLength);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
fn dggs_parse_quadkey_string(startOffset: u32, endOffset: u32) -> vec2u {
|
|
620
|
+
let stringLength = min(endOffset - startOffset, ${QUADKEY_MAXIMUM_LENGTH}u);
|
|
621
|
+
var key = vec2u(0u);
|
|
622
|
+
var characterIndex = 0u;
|
|
623
|
+
loop {
|
|
624
|
+
if (characterIndex >= stringLength) {
|
|
625
|
+
break;
|
|
626
|
+
}
|
|
627
|
+
let byte = dggs_read_string_byte(startOffset + characterIndex);
|
|
628
|
+
var digit = 0u;
|
|
629
|
+
if (byte >= 48u && byte <= 51u) {
|
|
630
|
+
digit = byte - 48u;
|
|
631
|
+
}
|
|
632
|
+
key = dggs_u64_shift_left(key, 2u) | vec2u(0u, digit);
|
|
633
|
+
characterIndex += 1u;
|
|
634
|
+
}
|
|
635
|
+
return dggs_u64_set_bits(key, ${QUADKEY_LENGTH_BIT_OFFSET}u, 6u, stringLength);
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
fn dggs_parse_s2_token_string(startOffset: u32, endOffset: u32) -> vec2u {
|
|
639
|
+
let stringLength = endOffset - startOffset;
|
|
640
|
+
if (stringLength == 1u) {
|
|
641
|
+
let byte = dggs_get_lowercase_ascii(dggs_read_string_byte(startOffset));
|
|
642
|
+
if (byte == 120u) {
|
|
643
|
+
return vec2u(0u);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
var key = vec2u(0u);
|
|
648
|
+
var characterIndex = 0u;
|
|
649
|
+
loop {
|
|
650
|
+
if (characterIndex >= 16u) {
|
|
651
|
+
break;
|
|
652
|
+
}
|
|
653
|
+
var digit = 0u;
|
|
654
|
+
if (characterIndex < stringLength) {
|
|
655
|
+
digit = dggs_get_hex_character_code(dggs_read_string_byte(startOffset + characterIndex));
|
|
656
|
+
}
|
|
657
|
+
key = dggs_u64_shift_left(key, 4u) | vec2u(0u, digit);
|
|
658
|
+
characterIndex += 1u;
|
|
659
|
+
}
|
|
660
|
+
return key;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
fn dggs_parse_a5_hex_string(startOffset: u32, endOffset: u32) -> vec2u {
|
|
664
|
+
var firstOffset = startOffset;
|
|
665
|
+
if (endOffset - firstOffset >= 2u) {
|
|
666
|
+
let firstByte = dggs_read_string_byte(firstOffset);
|
|
667
|
+
let secondByte = dggs_get_lowercase_ascii(dggs_read_string_byte(firstOffset + 1u));
|
|
668
|
+
if (firstByte == 48u && secondByte == 120u) {
|
|
669
|
+
firstOffset += 2u;
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
let stringLength = endOffset - firstOffset;
|
|
674
|
+
if (stringLength == 0u || stringLength > ${A5_HEX_MAXIMUM_LENGTH}u) {
|
|
675
|
+
return vec2u(0u);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
var key = vec2u(0u);
|
|
679
|
+
var characterIndex = 0u;
|
|
680
|
+
loop {
|
|
681
|
+
if (characterIndex >= stringLength) {
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
let digit = dggs_get_hex_character_code_or_invalid(
|
|
685
|
+
dggs_read_string_byte(firstOffset + characterIndex)
|
|
686
|
+
);
|
|
687
|
+
if (digit >= 16u) {
|
|
688
|
+
return vec2u(0u);
|
|
689
|
+
}
|
|
690
|
+
key = dggs_u64_shift_left(key, 4u) | vec2u(0u, digit);
|
|
691
|
+
characterIndex += 1u;
|
|
692
|
+
}
|
|
693
|
+
return key;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
fn dggs_parse_h3_hex_string(startOffset: u32, endOffset: u32) -> vec2u {
|
|
697
|
+
return dggs_parse_a5_hex_string(startOffset, endOffset);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
@compute @workgroup_size(64)
|
|
701
|
+
fn main(@builtin(global_invocation_id) globalInvocationId : vec3u) {
|
|
702
|
+
let stringIndex = globalInvocationId.x;
|
|
703
|
+
if (stringIndex >= dggsStringParsingConfig[0]) {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
let startOffset = dggsStringOffsets[stringIndex];
|
|
708
|
+
let endOffset = dggsStringOffsets[stringIndex + 1u];
|
|
709
|
+
let key = ${parsingFunction}(startOffset, endOffset);
|
|
710
|
+
dggsParsedKeys[stringIndex] = dggs_u64_to_little_endian_words(key);
|
|
711
|
+
}
|
|
712
|
+
`;
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
function getDggsStringParsingFunction(encoding: DggsCellEncoding): string {
|
|
716
|
+
switch (encoding) {
|
|
717
|
+
case 'geohash':
|
|
718
|
+
return 'dggs_parse_geohash_string';
|
|
719
|
+
case 'quadkey':
|
|
720
|
+
return 'dggs_parse_quadkey_string';
|
|
721
|
+
case 's2':
|
|
722
|
+
return 'dggs_parse_s2_token_string';
|
|
723
|
+
case 'a5':
|
|
724
|
+
return 'dggs_parse_a5_hex_string';
|
|
725
|
+
case 'h3':
|
|
726
|
+
return 'dggs_parse_h3_hex_string';
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
function makeDggsCellPathCoordinateType(componentCount: number): DggsCellPathCoordinateType {
|
|
731
|
+
const coordinateType = new FixedSizeList(
|
|
732
|
+
componentCount,
|
|
733
|
+
new Field('values', new Float32(), false)
|
|
734
|
+
);
|
|
735
|
+
return new List(new Field('coordinates', coordinateType, false));
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
function getDggsCellPathCoordinateComponentCount(
|
|
739
|
+
coordinateFormat: DggsCellPathCoordinateFormat
|
|
740
|
+
): number {
|
|
741
|
+
return coordinateFormat === 'fp64-split' ? 4 : 2;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function makeFixedCellPathValueOffsets(cellCount: number, pointCount: number): Int32Array {
|
|
745
|
+
const valueOffsets = new Int32Array(cellCount + 1);
|
|
746
|
+
for (let cellIndex = 0; cellIndex <= cellCount; cellIndex++) {
|
|
747
|
+
valueOffsets[cellIndex] = cellIndex * pointCount;
|
|
748
|
+
}
|
|
749
|
+
return valueOffsets;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
function getDggsCellPolygonPointCount(encoding: DggsCellEncoding): number {
|
|
753
|
+
switch (encoding) {
|
|
754
|
+
case 'a5':
|
|
755
|
+
return DGGS_A5_CELL_POLYGON_POINT_COUNT;
|
|
756
|
+
case 'h3':
|
|
757
|
+
return DGGS_H3_CELL_POLYGON_POINT_COUNT;
|
|
758
|
+
default:
|
|
759
|
+
return DGGS_DEFAULT_CELL_POLYGON_POINT_COUNT;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
function assertDggsCellKeyVector(keys: GPUVector, callerName: string): void {
|
|
764
|
+
const keyType = keys.dataType;
|
|
765
|
+
if (!DataType.isInt(keyType) || keyType.bitWidth !== 64 || keyType.isSigned !== false) {
|
|
766
|
+
throw new Error(`${callerName} requires Uint64 keys`);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
function getUint64KeyVectorByteLength(keys: GPUVector): number {
|
|
771
|
+
return keys.data.reduce((byteLength, data) => byteLength + data.length * data.byteStride, 0);
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/** Splits a Uint64 DGGS key into low/high Uint32 words for WGSL `vec2<u32>` inputs. */
|
|
775
|
+
export function getDggsUint64Words(value: bigint): [number, number] {
|
|
776
|
+
const normalizedValue = BigInt.asUintN(64, value);
|
|
777
|
+
return [
|
|
778
|
+
Number(normalizedValue & UINT64_WORD_MASK),
|
|
779
|
+
Number((normalizedValue >> 32n) & UINT64_WORD_MASK)
|
|
780
|
+
];
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
function getGPUDataBinding(data: GPUData, size?: number): Binding {
|
|
784
|
+
return {
|
|
785
|
+
buffer: getGPUDataBuffer(data),
|
|
786
|
+
offset: data.byteOffset,
|
|
787
|
+
...(size && size > 0 ? {size} : {})
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
function getGPUDataBuffer(data: GPUData): Buffer {
|
|
792
|
+
return data.buffer instanceof DynamicBuffer ? data.buffer.buffer : data.buffer;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
function getNormalizedUtf8Offsets(data: Data<Utf8>): Uint32Array {
|
|
796
|
+
const sourceOffsets = data.valueOffsets as Int32Array | undefined;
|
|
797
|
+
const normalizedOffsets = new Uint32Array(data.length + 1);
|
|
798
|
+
if (!sourceOffsets) {
|
|
799
|
+
return normalizedOffsets;
|
|
800
|
+
}
|
|
801
|
+
// Arrow exposes Utf8 valueOffsets as the logical row slice for data.offset.
|
|
802
|
+
const firstOffset = sourceOffsets[0] ?? 0;
|
|
803
|
+
for (let offsetIndex = 0; offsetIndex <= data.length; offsetIndex++) {
|
|
804
|
+
normalizedOffsets[offsetIndex] = Math.max(
|
|
805
|
+
0,
|
|
806
|
+
(sourceOffsets[offsetIndex] ?? firstOffset) - firstOffset
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
return normalizedOffsets;
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
function packUtf8BytesAsUint32Words(bytes: Uint8Array): Uint32Array {
|
|
813
|
+
const packedWords = new Uint32Array(Math.max(1, Math.ceil(bytes.length / 4)));
|
|
814
|
+
for (let byteIndex = 0; byteIndex < bytes.length; byteIndex++) {
|
|
815
|
+
const wordIndex = byteIndex >> 2;
|
|
816
|
+
packedWords[wordIndex] =
|
|
817
|
+
(packedWords[wordIndex] ?? 0) | ((bytes[byteIndex] ?? 0) << ((byteIndex & 3) * 8));
|
|
818
|
+
}
|
|
819
|
+
return packedWords;
|
|
820
|
+
}
|