@luma.gl/graph 10.0.0-alpha.11 → 10.0.0-alpha.13

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 (131) hide show
  1. package/LICENSE +4 -4
  2. package/THIRD-PARTY-NOTICES.md +19 -0
  3. package/dist/arrow/gpu/arrow-gpu-vector-adapters.cjs +6 -6
  4. package/dist/arrow/gpu/arrow-gpu-vector-adapters.d.ts.map +1 -1
  5. package/dist/arrow/gpu/arrow-gpu-vector-adapters.js +16 -6
  6. package/dist/arrow/gpu/arrow-gpu-vector-adapters.js.map +1 -1
  7. package/dist/arrow/gpu/arrow-gpu-vector-conversion.cjs +3 -2
  8. package/dist/arrow/gpu/arrow-gpu-vector-conversion.d.ts.map +1 -1
  9. package/dist/arrow/gpu/arrow-gpu-vector-conversion.js +3 -2
  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/internal.cjs.map +2 -2
  13. package/dist/dist.dev.js +2148 -454
  14. package/dist/dist.min.js +437 -123
  15. package/dist/experimental/index.cjs.map +1 -1
  16. package/dist/experimental/simulation/index.cjs.map +1 -1
  17. package/dist/gpu-core/compiled-gpu-graph.cjs +15 -0
  18. package/dist/gpu-core/compiled-gpu-graph.d.ts +1 -0
  19. package/dist/gpu-core/compiled-gpu-graph.d.ts.map +1 -1
  20. package/dist/gpu-core/compiled-gpu-graph.js +15 -0
  21. package/dist/gpu-core/compiled-gpu-graph.js.map +1 -1
  22. package/dist/gpu-core/gpu-command-graph.cjs +9 -0
  23. package/dist/gpu-core/gpu-command-graph.d.ts +2 -0
  24. package/dist/gpu-core/gpu-command-graph.d.ts.map +1 -1
  25. package/dist/gpu-core/gpu-command-graph.js +9 -0
  26. package/dist/gpu-core/gpu-command-graph.js.map +1 -1
  27. package/dist/gpu-core/gpu-expression-plan.cjs +4 -0
  28. package/dist/gpu-core/gpu-expression-plan.d.ts.map +1 -1
  29. package/dist/gpu-core/gpu-expression-plan.js +5 -0
  30. package/dist/gpu-core/gpu-expression-plan.js.map +1 -1
  31. package/dist/gpu-core/gpu-graph-compiler.cjs +48 -0
  32. package/dist/gpu-core/gpu-graph-compiler.d.ts +1 -0
  33. package/dist/gpu-core/gpu-graph-compiler.d.ts.map +1 -1
  34. package/dist/gpu-core/gpu-graph-compiler.js +50 -0
  35. package/dist/gpu-core/gpu-graph-compiler.js.map +1 -1
  36. package/dist/gpu-core/gpu-group-aggregation.cjs +7 -15
  37. package/dist/gpu-core/gpu-group-aggregation.d.ts.map +1 -1
  38. package/dist/gpu-core/gpu-group-aggregation.js +7 -18
  39. package/dist/gpu-core/gpu-group-aggregation.js.map +1 -1
  40. package/dist/gpu-core/gpu-reduction.cjs +6 -14
  41. package/dist/gpu-core/gpu-reduction.d.ts.map +1 -1
  42. package/dist/gpu-core/gpu-reduction.js +6 -15
  43. package/dist/gpu-core/gpu-reduction.js.map +1 -1
  44. package/dist/gpu-core/gpu-semantic-aggregation-operation.cjs +112 -0
  45. package/dist/gpu-core/gpu-semantic-aggregation-operation.d.ts +71 -0
  46. package/dist/gpu-core/gpu-semantic-aggregation-operation.d.ts.map +1 -0
  47. package/dist/gpu-core/gpu-semantic-aggregation-operation.js +92 -0
  48. package/dist/gpu-core/gpu-semantic-aggregation-operation.js.map +1 -0
  49. package/dist/gpu-core/gpu-shader-pass-kernel.cjs +267 -0
  50. package/dist/gpu-core/gpu-shader-pass-kernel.d.ts +29 -0
  51. package/dist/gpu-core/gpu-shader-pass-kernel.d.ts.map +1 -0
  52. package/dist/gpu-core/gpu-shader-pass-kernel.js +260 -0
  53. package/dist/gpu-core/gpu-shader-pass-kernel.js.map +1 -0
  54. package/dist/gpu-core/gpu-shader-pass-operation.cjs +85 -0
  55. package/dist/gpu-core/gpu-shader-pass-operation.d.ts +32 -0
  56. package/dist/gpu-core/gpu-shader-pass-operation.d.ts.map +1 -0
  57. package/dist/gpu-core/gpu-shader-pass-operation.js +74 -0
  58. package/dist/gpu-core/gpu-shader-pass-operation.js.map +1 -0
  59. package/dist/gpu-core/index.cjs +8 -0
  60. package/dist/gpu-core/index.cjs.map +3 -3
  61. package/dist/gpu-core/index.d.ts +4 -0
  62. package/dist/gpu-core/index.d.ts.map +1 -1
  63. package/dist/gpu-core/index.js +2 -0
  64. package/dist/gpu-core/index.js.map +1 -1
  65. package/dist/gpu-data/cpu-gpu-vector-transfer.cjs +7 -2
  66. package/dist/gpu-data/cpu-gpu-vector-transfer.d.ts.map +1 -1
  67. package/dist/gpu-data/cpu-gpu-vector-transfer.js +9 -6
  68. package/dist/gpu-data/cpu-gpu-vector-transfer.js.map +1 -1
  69. package/dist/gpu-data/gpu-data-buffer-props.cjs +41 -0
  70. package/dist/gpu-data/gpu-data-buffer-props.d.ts +5 -0
  71. package/dist/gpu-data/gpu-data-buffer-props.d.ts.map +1 -0
  72. package/dist/gpu-data/gpu-data-buffer-props.js +19 -0
  73. package/dist/gpu-data/gpu-data-buffer-props.js.map +1 -0
  74. package/dist/gpu-data/index.cjs.map +3 -3
  75. package/dist/gpu-dggs/benchmarks.cjs.map +1 -1
  76. package/dist/gpu-graph/index.cjs +5 -0
  77. package/dist/gpu-graph/index.cjs.map +3 -3
  78. package/dist/gpu-graph/index.d.ts +2 -0
  79. package/dist/gpu-graph/index.d.ts.map +1 -1
  80. package/dist/gpu-graph/index.js +1 -0
  81. package/dist/gpu-graph/index.js.map +1 -1
  82. package/dist/gpu-graph-algorithms/benchmarks.cjs.map +1 -1
  83. package/dist/gpu-graph-algorithms/index.cjs.map +1 -1
  84. package/dist/gpu-physics/index.cjs.map +1 -1
  85. package/dist/gpu-project/benchmarks.cjs.map +2 -2
  86. package/dist/gpu-project/crs.cjs.map +2 -2
  87. package/dist/gpu-project/graph.cjs.map +3 -3
  88. package/dist/gpu-project/index.cjs.map +2 -2
  89. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.cjs +53 -5
  90. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.d.ts +3 -1
  91. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.d.ts.map +1 -1
  92. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.js +58 -5
  93. package/dist/gpu-simple-graph/compiled-gpu-expression-graph.js.map +1 -1
  94. package/dist/gpu-simple-graph/index.cjs.map +3 -3
  95. package/dist/gpu-sql/gpu-sql.cjs +112 -100
  96. package/dist/gpu-sql/gpu-sql.d.ts +1 -1
  97. package/dist/gpu-sql/gpu-sql.d.ts.map +1 -1
  98. package/dist/gpu-sql/gpu-sql.js +120 -102
  99. package/dist/gpu-sql/gpu-sql.js.map +1 -1
  100. package/dist/gpu-sql/index.cjs.map +2 -2
  101. package/dist/gpu-tables/index.cjs.map +2 -2
  102. package/dist/gpu-tables/table/gpu-table.cjs +2 -2
  103. package/dist/gpu-tables/table/gpu-table.d.ts.map +1 -1
  104. package/dist/gpu-tables/table/gpu-table.js +2 -2
  105. package/dist/gpu-tables/table/gpu-table.js.map +1 -1
  106. package/dist/index.cjs +8 -0
  107. package/dist/index.cjs.map +3 -3
  108. package/dist/index.d.ts +4 -0
  109. package/dist/index.d.ts.map +1 -1
  110. package/dist/index.js +2 -0
  111. package/dist/index.js.map +1 -1
  112. package/package.json +10 -8
  113. package/src/arrow/gpu/arrow-gpu-vector-adapters.ts +17 -6
  114. package/src/arrow/gpu/arrow-gpu-vector-conversion.ts +3 -2
  115. package/src/gpu-core/compiled-gpu-graph.ts +18 -0
  116. package/src/gpu-core/gpu-command-graph.ts +10 -0
  117. package/src/gpu-core/gpu-expression-plan.ts +6 -0
  118. package/src/gpu-core/gpu-graph-compiler.ts +61 -0
  119. package/src/gpu-core/gpu-group-aggregation.ts +38 -50
  120. package/src/gpu-core/gpu-reduction.ts +32 -42
  121. package/src/gpu-core/gpu-semantic-aggregation-operation.ts +150 -0
  122. package/src/gpu-core/gpu-shader-pass-kernel.ts +307 -0
  123. package/src/gpu-core/gpu-shader-pass-operation.ts +103 -0
  124. package/src/gpu-core/index.ts +8 -0
  125. package/src/gpu-data/cpu-gpu-vector-transfer.ts +10 -7
  126. package/src/gpu-data/gpu-data-buffer-props.ts +25 -0
  127. package/src/gpu-graph/index.ts +9 -0
  128. package/src/gpu-simple-graph/compiled-gpu-expression-graph.ts +59 -5
  129. package/src/gpu-sql/gpu-sql.ts +130 -103
  130. package/src/gpu-tables/table/gpu-table.ts +2 -1
  131. package/src/index.ts +11 -0
@@ -0,0 +1,150 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {assert} from '@luma.gl/core';
6
+ import {gpuGraph, type GPUGraphVector} from './gpu-graph-value';
7
+ import type {GPUOperation} from './gpu-operation';
8
+ import {getGPUOperationValueName, type GPUOperationContract} from './gpu-operation-contract';
9
+ import type {GPUGroupAggregationOperation} from './gpu-group-aggregation';
10
+ import type {GPUReductionOperation} from './gpu-reduction';
11
+
12
+ type ScalarFormat = 'uint32' | 'sint32' | 'float32';
13
+
14
+ /** Logical inputs and destination for dense grouped statistics. */
15
+ export type GPUGraphGroupAggregationProps = {
16
+ /** Stable operation identifier. Defaults to the output name plus a suffix. */
17
+ id?: string;
18
+ /** Dense unsigned group keys; out-of-range keys are ignored. */
19
+ keys: string | GPUGraphVector<'uint32'>;
20
+ /** Optional row selection: zero rejects, nonzero accepts. */
21
+ mask?: string | GPUGraphVector<'uint32'>;
22
+ /** Output cardinality. May be omitted when output storage/shape is declared separately. */
23
+ groupCount?: number;
24
+ } & (
25
+ | {
26
+ /** Count accepted rows per group. */
27
+ operation: 'count';
28
+ /** Logical unsigned count destination. */
29
+ output: string | GPUGraphVector<'uint32'>;
30
+ values?: never;
31
+ }
32
+ | {
33
+ /** Statistic computed over finite float32 contributions. */
34
+ operation: Exclude<GPUGroupAggregationOperation, 'count'>;
35
+ /** One float32 contribution per key. */
36
+ values: string | GPUGraphVector<'float32'>;
37
+ /** Logical float32 statistic destination. */
38
+ output: string | GPUGraphVector<'float32'>;
39
+ }
40
+ );
41
+
42
+ /** Dense grouped statistics lowered to GPUGroupAggregation by GPUGraphCompiler. */
43
+ export class GPUGraphGroupAggregation implements GPUOperation {
44
+ /** Semantic operation discriminator. */
45
+ readonly type = 'group-aggregation';
46
+ /** Stable operation identifier. */
47
+ readonly id: string;
48
+ /** Immutable logical operation arguments. */
49
+ readonly props: Readonly<GPUGraphGroupAggregationProps>;
50
+
51
+ /** Describe grouped reduction without allocating or reading GPU storage. */
52
+ constructor(props: GPUGraphGroupAggregationProps) {
53
+ // Dense group destinations must have positive integer cardinality.
54
+ assert(
55
+ props.groupCount === undefined ||
56
+ (Number.isSafeInteger(props.groupCount) && props.groupCount > 0)
57
+ );
58
+ this.id = props.id ?? `${getGPUOperationValueName(props.output)}-group-aggregation`;
59
+ this.props = Object.freeze({...props});
60
+ }
61
+
62
+ /** Logical dependencies and optional inferred output shape. */
63
+ get contract(): GPUOperationContract {
64
+ const {keys, mask, output, groupCount} = this.props;
65
+ const values = this.props.operation === 'count' ? undefined : this.props.values;
66
+ return {
67
+ outputs: ({getValue}) => {
68
+ for (const input of [keys, mask]) {
69
+ if (input) {
70
+ const descriptor = getValue(getGPUOperationValueName(input));
71
+ assert(descriptor.type === 'vector' && descriptor.format === 'uint32');
72
+ }
73
+ }
74
+ if (values) {
75
+ const descriptor = getValue(getGPUOperationValueName(values));
76
+ assert(descriptor.type === 'vector' && descriptor.format === 'float32');
77
+ }
78
+ return {
79
+ [getGPUOperationValueName(output)]: gpuGraph.vector(
80
+ this.props.operation === 'count' ? 'uint32' : 'float32',
81
+ {length: groupCount}
82
+ )
83
+ };
84
+ },
85
+ values: [
86
+ {value: keys, access: 'read'},
87
+ ...(values ? [{value: values, access: 'read' as const}] : []),
88
+ ...(mask ? [{value: mask, access: 'read' as const}] : []),
89
+ {value: output, access: 'write'}
90
+ ]
91
+ };
92
+ }
93
+ }
94
+
95
+ /** Logical scalar reduction inputs and destination. */
96
+ export type GPUGraphReductionProps = {
97
+ /** Stable operation identifier. Defaults to the output name plus a suffix. */
98
+ id?: string;
99
+ /** Scalar uint32, sint32, or float32 input. */
100
+ input: string | GPUGraphVector<ScalarFormat>;
101
+ /** Optional nonzero/zero row selection. */
102
+ mask?: string | GPUGraphVector<'uint32'>;
103
+ /** Statistic to compute; extent produces minimum followed by maximum. */
104
+ operation: GPUReductionOperation;
105
+ /** Logical result destination, with the same format as input. */
106
+ output: string | GPUGraphVector<ScalarFormat>;
107
+ };
108
+
109
+ /** Scalar statistics lowered to GPUReduction by GPUGraphCompiler. */
110
+ export class GPUGraphReduction implements GPUOperation {
111
+ /** Semantic operation discriminator. */
112
+ readonly type = 'reduction';
113
+ /** Stable operation identifier. */
114
+ readonly id: string;
115
+ /** Immutable logical operation arguments. */
116
+ readonly props: Readonly<GPUGraphReductionProps>;
117
+
118
+ /** Describe a scalar reduction without allocating or reading GPU storage. */
119
+ constructor(props: GPUGraphReductionProps) {
120
+ this.id = props.id ?? `${getGPUOperationValueName(props.output)}-reduction`;
121
+ this.props = Object.freeze({...props});
122
+ }
123
+
124
+ /** Infer a packed one-row result, or two rows for extent. */
125
+ get contract(): GPUOperationContract {
126
+ const {input, mask, output, operation} = this.props;
127
+ return {
128
+ outputs: ({getValue}) => {
129
+ const descriptor = getValue(getGPUOperationValueName(input));
130
+ assert(descriptor.type === 'vector');
131
+ const {format} = descriptor;
132
+ assert(format === 'uint32' || format === 'sint32' || format === 'float32');
133
+ if (mask) {
134
+ const selection = getValue(getGPUOperationValueName(mask));
135
+ assert(selection.type === 'vector' && selection.format === 'uint32');
136
+ }
137
+ return {
138
+ [getGPUOperationValueName(output)]: gpuGraph.vector(format, {
139
+ length: operation === 'extent' ? 2 : 1
140
+ })
141
+ };
142
+ },
143
+ values: [
144
+ {value: input, access: 'read'},
145
+ ...(mask ? [{value: mask, access: 'read' as const}] : []),
146
+ {value: output, access: 'write'}
147
+ ]
148
+ };
149
+ }
150
+ }
@@ -0,0 +1,307 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {
6
+ assert,
7
+ Buffer,
8
+ type Binding,
9
+ type CommandEncoder,
10
+ type Device,
11
+ type RenderPass,
12
+ type Sampler,
13
+ type VertexArray
14
+ } from '@luma.gl/core';
15
+ import {GPUVector} from '@luma.gl/graph/gpu-data';
16
+ import {RenderKernel} from '../execution/compute/render-kernel';
17
+ import type {GPUShaderPassOperation} from './gpu-shader-pass-operation';
18
+ import type {GPUExpressionProgram} from './gpu-expression-plan';
19
+ import type {GPUOperationLoweringContext} from './gpu-graph-lowering';
20
+ import {getGPUScalarWGSLType} from './gpu-scalar';
21
+ import {getTextureBindingInfo, type GPUGraphTextureBinding} from './gpu-texture-plan';
22
+
23
+ const VERTEX_SOURCE = `
24
+ struct GraphShaderPassVertex {
25
+ @builtin(position) position: vec4<f32>,
26
+ @location(0) textureCoordinates: vec2<f32>
27
+ };
28
+ @vertex fn vertexMain(@builtin(vertex_index) index: u32) -> GraphShaderPassVertex {
29
+ let position = vec2<f32>(f32((index << 1u) & 2u), f32(index & 2u));
30
+ var result: GraphShaderPassVertex;
31
+ result.position = vec4<f32>(position * 2.0 - 1.0, 0.0, 1.0);
32
+ result.textureCoordinates = vec2<f32>(position.x, 1.0 - position.y);
33
+ return result;
34
+ }`;
35
+
36
+ /** One immutable pipeline; resources and uniforms are supplied independently for each draw. @internal */
37
+ export class GPUShaderPassKernel {
38
+ private kernel!: RenderKernel;
39
+ private vertexArray!: VertexArray;
40
+ private readonly samplers: Record<string, Sampler> = Object.create(null);
41
+ private readonly parameterFormats: readonly ('float32' | 'uint32' | 'sint32')[];
42
+ private readonly parameterByteLength: number;
43
+
44
+ constructor(
45
+ readonly device: Device,
46
+ readonly operation: GPUShaderPassOperation,
47
+ program: Pick<GPUExpressionProgram, 'textures' | 'scalars'>
48
+ ) {
49
+ assert(device.type === 'webgpu');
50
+ this.parameterFormats = Object.values(operation.parameters).map(name => {
51
+ const scalar = program.scalars.find(value => value.id === name);
52
+ assert(scalar);
53
+ return scalar.format;
54
+ });
55
+ this.parameterByteLength = Math.ceil(this.parameterFormats.length / 4) * 16;
56
+ // This first shader-pass contract samples filterable float color and writes float color.
57
+ for (const name of Object.values(operation.inputs)) {
58
+ const input = program.textures.find(texture => texture.id === name)!;
59
+ const info = device.getTextureFormatInfo(input.format);
60
+ assert(
61
+ !info.integer &&
62
+ info.attachment !== 'depth' &&
63
+ info.attachment !== 'depth-stencil' &&
64
+ device.getTextureFormatCapabilities(input.format).filter
65
+ );
66
+ }
67
+ const outputInfo = device.getTextureFormatInfo(operation.outputDescriptor.format);
68
+ assert(
69
+ !outputInfo.integer &&
70
+ device.getTextureFormatCapabilities(operation.outputDescriptor.format).render
71
+ );
72
+ const shaderBindings: import('@luma.gl/core').ShaderLayout['bindings'] = [
73
+ ...Object.keys(operation.inputs).map((name, location) => ({
74
+ name,
75
+ location,
76
+ group: 0,
77
+ type: 'texture' as const,
78
+ viewDimension: '2d' as const,
79
+ sampleType: 'float' as const
80
+ })),
81
+ ...Object.keys(operation.samplers).map((name, index) => ({
82
+ name,
83
+ location: Object.keys(operation.inputs).length + index,
84
+ group: 0,
85
+ type: 'sampler' as const
86
+ }))
87
+ ];
88
+ let binding = 0;
89
+ const declarations = [
90
+ ...Object.keys(operation.inputs).map(
91
+ name => `@group(0) @binding(${binding++}) var ${name}: texture_2d<f32>;`
92
+ ),
93
+ ...Object.keys(operation.samplers).map(
94
+ name => `@group(0) @binding(${binding++}) var ${name}: sampler;`
95
+ )
96
+ ];
97
+ if (this.parameterFormats.length) {
98
+ shaderBindings.push({name: 'parameters', location: binding, group: 0, type: 'uniform'});
99
+ declarations.push(
100
+ `struct GraphShaderPassParameters {${Object.keys(operation.parameters)
101
+ .map((name, index) => `${name}: ${getGPUScalarWGSLType(this.parameterFormats[index])}`)
102
+ .join(
103
+ ','
104
+ )}};\n@group(0) @binding(${binding}) var<uniform> parameters: GraphShaderPassParameters;`
105
+ );
106
+ }
107
+ try {
108
+ this.kernel = new RenderKernel(device, {
109
+ id: operation.id,
110
+ vertexSource: VERTEX_SOURCE,
111
+ fragmentSource: `${declarations.join('\n')}\n${operation.fragmentSource}\n
112
+ @fragment fn fragmentMain(@location(0) textureCoordinates: vec2<f32>) -> @location(0) vec4<f32> {
113
+ return shade(textureCoordinates);
114
+ }`,
115
+ vertexEntryPoint: 'vertexMain',
116
+ fragmentEntryPoint: 'fragmentMain',
117
+ topology: 'triangle-list',
118
+ shaderLayout: {attributes: [], bindings: shaderBindings},
119
+ colorAttachmentFormats: [operation.outputDescriptor.format],
120
+ parameters: {depthWriteEnabled: false}
121
+ });
122
+ this.vertexArray = device.createVertexArray({
123
+ shaderLayout: this.kernel.pipeline.shaderLayout,
124
+ bufferLayout: []
125
+ });
126
+ for (const [name, props] of Object.entries(operation.samplers))
127
+ this.samplers[name] = device.createSampler(props);
128
+ } catch (error) {
129
+ this.destroy();
130
+ throw error;
131
+ }
132
+ }
133
+
134
+ /** Validate selected view geometry before either executor records commands. */
135
+ validateTextures(textures: ReadonlyMap<string, GPUGraphTextureBinding>): void {
136
+ for (const name of [...Object.values(this.operation.inputs), this.operation.output]) {
137
+ const binding = textures.get(name);
138
+ assert(binding);
139
+ const info = getTextureBindingInfo(binding);
140
+ assert(
141
+ info.texture.device === this.device &&
142
+ info.dimension === '2d' &&
143
+ info.depth === 1 &&
144
+ info.samples === 1
145
+ );
146
+ if (name === this.operation.output) assert(info.mipLevels === 1);
147
+ }
148
+ }
149
+
150
+ /** Full graph lowers to ordinary resource-tracked copy and render nodes. */
151
+ lower<Parameters>(context: GPUOperationLoweringContext<Parameters>): void {
152
+ const operation = this.operation;
153
+ const inputs = Object.fromEntries(
154
+ Object.entries(operation.inputs).map(([name, value]) => [
155
+ name,
156
+ context.resolveTextureView(value)
157
+ ])
158
+ );
159
+ const output = context.resolveTextureView(operation.output);
160
+ const parameters = this.parameterFormats.length
161
+ ? context.graph.createTransientBuffer({
162
+ id: context.graph.getUniqueResourceId(`${operation.id}-parameters`),
163
+ byteLength: this.parameterByteLength,
164
+ usage: Buffer.UNIFORM | Buffer.COPY_DST
165
+ })
166
+ : undefined;
167
+ if (parameters) {
168
+ const sources = Object.values(operation.parameters).map(
169
+ name => context.resolveScalar(name).view
170
+ );
171
+ context.graph.addCopyPass({
172
+ id: `${operation.id}-parameters`,
173
+ resources: [
174
+ ...sources.map(buffer => ({buffer, usage: 'copy-source' as const})),
175
+ {buffer: parameters, usage: 'copy-destination'}
176
+ ],
177
+ compile: () => ({
178
+ encode: ({commandEncoder, getBuffer}) => {
179
+ sources.forEach((source, index) =>
180
+ commandEncoder.copyBufferToBuffer({
181
+ sourceBuffer: getBuffer(source),
182
+ sourceOffset: source.byteOffset,
183
+ destinationBuffer: getBuffer(parameters),
184
+ destinationOffset: index * 4,
185
+ size: 4
186
+ })
187
+ );
188
+ }
189
+ })
190
+ });
191
+ }
192
+ context.graph.addRenderPass({
193
+ id: operation.id,
194
+ resources: [
195
+ ...Object.values(inputs).map(texture => ({texture, usage: 'sampled' as const})),
196
+ ...(parameters ? [{buffer: parameters, usage: 'uniform' as const}] : [])
197
+ ],
198
+ attachments: {colorAttachments: [output]},
199
+ compile: () => ({
200
+ getRenderPassProps: () => ({clearColor: [0, 0, 0, 0], clearDepth: false}),
201
+ encode: ({renderPass, getTextureView, getBuffer}) =>
202
+ this.draw(
203
+ renderPass,
204
+ Object.fromEntries(
205
+ Object.entries(inputs).map(([name, view]) => [name, getTextureView(view)])
206
+ ),
207
+ parameters ? getBuffer(parameters) : undefined
208
+ )
209
+ })
210
+ });
211
+ }
212
+
213
+ /** SimpleGraph owns only per-invocation framebuffer/uniform wrappers, never bound textures. */
214
+ prepare(
215
+ textures: ReadonlyMap<string, GPUGraphTextureBinding>,
216
+ values: ReadonlyMap<string, GPUVector | number>
217
+ ) {
218
+ this.validateTextures(textures);
219
+ const inputs = Object.fromEntries(
220
+ Object.entries(this.operation.inputs).map(([name, value]) => [name, textures.get(value)!])
221
+ );
222
+ const output = textures.get(this.operation.output)!;
223
+ const info = getTextureBindingInfo(output);
224
+ const data = new Uint8Array(this.parameterByteLength);
225
+ const view = new DataView(data.buffer);
226
+ const parameterValues = Object.values(this.operation.parameters).map((name, index) => {
227
+ const value = values.get(name);
228
+ assert(value !== undefined);
229
+ if (typeof value === 'number') {
230
+ switch (this.parameterFormats[index]) {
231
+ case 'float32':
232
+ view.setFloat32(index * 4, value, true);
233
+ break;
234
+ case 'sint32':
235
+ view.setInt32(index * 4, value, true);
236
+ break;
237
+ case 'uint32':
238
+ view.setUint32(index * 4, value, true);
239
+ break;
240
+ }
241
+ } else assert(value.length === 1 && value.data.length === 1);
242
+ return value;
243
+ });
244
+ const parameters = data.byteLength
245
+ ? this.device.createBuffer({data, usage: Buffer.UNIFORM | Buffer.COPY_DST})
246
+ : undefined;
247
+ let framebuffer;
248
+ try {
249
+ framebuffer = this.device.createFramebuffer({
250
+ width: info.width,
251
+ height: info.height,
252
+ colorAttachments: [output]
253
+ });
254
+ } catch (error) {
255
+ parameters?.destroy();
256
+ throw error;
257
+ }
258
+ return {
259
+ encode: (commandEncoder: CommandEncoder) => {
260
+ this.validateTextures(textures);
261
+ if (parameters)
262
+ parameterValues.forEach((value, index) => {
263
+ if (!(value instanceof GPUVector)) return;
264
+ const chunk = value.data[0];
265
+ const buffer = chunk.buffer instanceof Buffer ? chunk.buffer : chunk.buffer.buffer;
266
+ commandEncoder.copyBufferToBuffer({
267
+ sourceBuffer: buffer,
268
+ sourceOffset: chunk.byteOffset,
269
+ destinationBuffer: parameters,
270
+ destinationOffset: index * 4,
271
+ size: 4
272
+ });
273
+ });
274
+ const pass = commandEncoder.beginRenderPass({
275
+ framebuffer,
276
+ clearColor: [0, 0, 0, 0],
277
+ clearDepth: false
278
+ });
279
+ try {
280
+ this.draw(pass, inputs, parameters);
281
+ } finally {
282
+ pass.end();
283
+ }
284
+ },
285
+ destroy: () => {
286
+ framebuffer.destroy();
287
+ parameters?.destroy();
288
+ }
289
+ };
290
+ }
291
+
292
+ destroy(): void {
293
+ for (const sampler of Object.values(this.samplers)) sampler.destroy();
294
+ this.vertexArray?.destroy();
295
+ this.kernel?.destroy();
296
+ }
297
+
298
+ private draw(
299
+ renderPass: RenderPass,
300
+ inputs: Record<string, GPUGraphTextureBinding>,
301
+ parameters?: Buffer
302
+ ): void {
303
+ const bindings: Record<string, Binding> = {...inputs, ...this.samplers};
304
+ if (parameters) bindings['parameters'] = parameters;
305
+ this.kernel.draw(renderPass, {vertexArray: this.vertexArray, vertexCount: 3, bindings});
306
+ }
307
+ }
@@ -0,0 +1,103 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {assert, type SamplerProps, type TextureFormatColor} from '@luma.gl/core';
6
+ import {gpuGraph, type GPUGraphTextureDescriptor} from './gpu-graph-value';
7
+ import type {GPUOperation} from './gpu-operation';
8
+ import type {GPUOperationContract} from './gpu-operation-contract';
9
+
10
+ export type GPUShaderPassOperationProps = {
11
+ id?: string;
12
+ /** WGSL defining fn shade(textureCoordinates: vec2<f32>) -> vec4<f32>. */
13
+ fragmentSource: string;
14
+ /** Shader texture binding names mapped to logical graph texture names. */
15
+ inputs: Readonly<Record<string, string>>;
16
+ /** Explicit, immutable color sampler descriptions, keyed by shader binding name. */
17
+ samplers?: Readonly<Record<string, SamplerProps>>;
18
+ /** Fields of the generated parameters uniform, mapped to graph scalar names. */
19
+ parameters?: Readonly<Record<string, string>>;
20
+ output: string;
21
+ /** Single-sampled 2D color result with an explicit fixed or inherited extent. */
22
+ outputDescriptor: GPUGraphTextureDescriptor<TextureFormatColor>;
23
+ };
24
+
25
+ /** One fullscreen WGSL draw, shared by GPUGraph and WebGPU SimpleGraph. */
26
+ export class GPUShaderPassOperation implements GPUOperation {
27
+ readonly type = 'shader-pass';
28
+ readonly id: string;
29
+ readonly fragmentSource: string;
30
+ readonly inputs: Readonly<Record<string, string>>;
31
+ readonly samplers: Readonly<Record<string, Readonly<SamplerProps>>>;
32
+ readonly parameters: Readonly<Record<string, string>>;
33
+ readonly output: string;
34
+ readonly outputDescriptor: GPUGraphTextureDescriptor<TextureFormatColor>;
35
+
36
+ constructor(props: GPUShaderPassOperationProps) {
37
+ this.id = props.id ?? `${props.output}-shader-pass`;
38
+ this.fragmentSource = props.fragmentSource;
39
+ this.inputs = Object.freeze({...props.inputs});
40
+ this.parameters = Object.freeze({...props.parameters});
41
+ this.samplers = Object.freeze(
42
+ Object.fromEntries(
43
+ Object.entries(props.samplers ?? {}).map(([name, sampler]) => [
44
+ name,
45
+ Object.freeze({...sampler})
46
+ ])
47
+ )
48
+ );
49
+ this.output = props.output;
50
+ const descriptor = props.outputDescriptor;
51
+ // This operation writes one complete 2D color attachment, without mip generation or MSAA.
52
+ assert(
53
+ descriptor.extent &&
54
+ (descriptor.dimension ?? '2d') === '2d' &&
55
+ (descriptor.mipLevels ?? 1) === 1 &&
56
+ (descriptor.samples ?? 1) === 1
57
+ );
58
+ this.outputDescriptor = gpuGraph.texture({
59
+ ...descriptor,
60
+ dimension: '2d',
61
+ mipLevels: 1,
62
+ samples: 1
63
+ });
64
+ const bindings = [...Object.keys(this.inputs), ...Object.keys(this.samplers)];
65
+ // Binding names are WGSL identifiers; reserve the generated uniform and entry-point names.
66
+ assert(new Set(bindings).size === bindings.length);
67
+ for (const name of [...bindings, ...Object.keys(this.parameters)])
68
+ assert(
69
+ /^[A-Za-z][A-Za-z0-9_]*$/.test(name) &&
70
+ !['parameters', 'shade', 'vertexMain', 'fragmentMain'].includes(name) &&
71
+ !name.startsWith('GraphShaderPass')
72
+ );
73
+ for (const sampler of Object.values(this.samplers))
74
+ assert((sampler.type ?? 'color-sampler') === 'color-sampler');
75
+ }
76
+
77
+ get contract(): GPUOperationContract {
78
+ return {
79
+ values: [
80
+ ...Object.values(this.inputs).map(value => ({
81
+ value,
82
+ access: 'read' as const,
83
+ textureUsage: 'sampled' as const
84
+ })),
85
+ ...Object.values(this.parameters).map(value => ({value, access: 'read' as const})),
86
+ {value: this.output, access: 'write', textureUsage: 'render-attachment'}
87
+ ],
88
+ outputs: ({getValue}) => {
89
+ for (const name of Object.values(this.inputs)) {
90
+ const input = getValue(name);
91
+ // Texture views may select one layer of an array, but the logical input is 2D.
92
+ assert(
93
+ input.type === 'texture' &&
94
+ (input.dimension ?? '2d') === '2d' &&
95
+ (input.samples ?? 1) === 1
96
+ );
97
+ }
98
+ for (const name of Object.values(this.parameters)) assert(getValue(name).type === 'scalar');
99
+ return {[this.output]: this.outputDescriptor};
100
+ }
101
+ };
102
+ }
103
+ }
@@ -686,6 +686,8 @@ export type {
686
686
  } from './gpu-operation-contract';
687
687
 
688
688
  export {GPUUploadOperation, GPUCopyOperation} from './gpu-transfer-operation';
689
+ export {GPUShaderPassOperation} from './gpu-shader-pass-operation';
690
+ export type {GPUShaderPassOperationProps} from './gpu-shader-pass-operation';
689
691
  export type {GPUUploadOperationProps, GPUCopyOperationProps} from './gpu-transfer-operation';
690
692
  export {GPUCompactionOperation} from './gpu-compaction-operation';
691
693
  export type {GPUCompactionOperationProps} from './gpu-compaction-operation';
@@ -700,3 +702,9 @@ export type {
700
702
  GPUCPUVector,
701
703
  GPUCPUResult
702
704
  } from './gpu-cpu-operation';
705
+
706
+ export {GPUGraphGroupAggregation, GPUGraphReduction} from './gpu-semantic-aggregation-operation';
707
+ export type {
708
+ GPUGraphGroupAggregationProps,
709
+ GPUGraphReductionProps
710
+ } from './gpu-semantic-aggregation-operation';
@@ -6,6 +6,7 @@ import {assert, Buffer, type Device, type BufferProps} from '@luma.gl/core';
6
6
  import {CPUData} from './cpu-data';
7
7
  import {CPUVector} from './cpu-vector';
8
8
  import {GPUData} from './gpu-data';
9
+ import {getGPUDataBufferProps, PORTABLE_GPU_DATA_USAGE} from './gpu-data-buffer-props';
9
10
  import {GPUVector} from './gpu-vector';
10
11
  import {getGPUVectorFormatInfo, type GPUVectorFormat} from './gpu-vector-format';
11
12
 
@@ -30,6 +31,13 @@ export function makeGPUVectorFromCPUVector<T extends GPUVectorFormat>(
30
31
  assert(
31
32
  inferredIndexType && (!options.indexType || options.indexType === inferredIndexType)
32
33
  );
34
+ const defaults =
35
+ options.usage === undefined
36
+ ? getGPUDataBufferProps(device, inferredIndexType)
37
+ : {
38
+ usage: PORTABLE_GPU_DATA_USAGE,
39
+ indexType: undefined
40
+ };
33
41
  const count = info.vertexList || info.valueList ? chunk.valueLength : chunk.length;
34
42
  const bytes = new Uint8Array(count * info.byteLength);
35
43
  for (let index = 0; index < count; index++)
@@ -45,15 +53,10 @@ export function makeGPUVectorFromCPUVector<T extends GPUVectorFormat>(
45
53
  new GPUData({
46
54
  buffer: device.createBuffer({
47
55
  data: bytes.length ? bytes : new Uint8Array(4),
48
- usage:
49
- Buffer.COPY_SRC |
50
- Buffer.COPY_DST |
51
- Buffer.STORAGE |
52
- Buffer.VERTEX |
53
- (options.usage ?? 0),
56
+ usage: (defaults.usage ?? 0) | (options.usage ?? 0),
54
57
  indexType:
55
58
  options.indexType ??
56
- ((options.usage ?? 0) & Buffer.INDEX ? inferredIndexType : undefined)
59
+ ((options.usage ?? 0) & Buffer.INDEX ? inferredIndexType : defaults.indexType)
57
60
  }),
58
61
  format: vector.format,
59
62
  length: chunk.length,
@@ -0,0 +1,25 @@
1
+ // luma.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import {Buffer, type BufferProps, type Device} from '@luma.gl/core';
6
+
7
+ export const PORTABLE_GPU_DATA_USAGE =
8
+ Buffer.STORAGE | Buffer.VERTEX | Buffer.COPY_SRC | Buffer.COPY_DST;
9
+
10
+ /** Usage defaults for general-purpose GPUData backing storage. */
11
+ export function getGPUDataBufferProps(
12
+ device: Device,
13
+ indexType: BufferProps['indexType'] = 'uint32'
14
+ ): Pick<BufferProps, 'usage' | 'indexType'> {
15
+ const usage = PORTABLE_GPU_DATA_USAGE;
16
+ if (device.type !== 'webgpu') {
17
+ // WebGL fixes the buffer target on first bind, so INDEX would prevent vertex use.
18
+ return {usage};
19
+ }
20
+ return {
21
+ usage: usage | Buffer.INDEX | Buffer.UNIFORM | Buffer.INDIRECT | Buffer.QUERY_RESOLVE,
22
+ // Explicit index metadata prevents Device from widening raw Uint8Array uploads.
23
+ indexType
24
+ };
25
+ }
@@ -82,3 +82,12 @@ export type {
82
82
  GPUCPUVector,
83
83
  GPUCPUResult
84
84
  } from '../gpu-core/gpu-cpu-operation';
85
+
86
+ export {
87
+ GPUGraphGroupAggregation,
88
+ GPUGraphReduction
89
+ } from '../gpu-core/gpu-semantic-aggregation-operation';
90
+ export type {
91
+ GPUGraphGroupAggregationProps,
92
+ GPUGraphReductionProps
93
+ } from '../gpu-core/gpu-semantic-aggregation-operation';