webgpu-computed 0.0.16 → 0.0.18
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/README.md +456 -54
- package/package.json +1 -1
- package/src/index.js +35 -37
- package/src/utils/GpuComputed.d.ts +4 -4
package/README.md
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
# webgpu-computed
|
|
2
2
|
|
|
3
|
-
🌐 Other
|
|
3
|
+
🌐 Other language versions:
|
|
4
|
+
- [中文](https://github.com/xiaguochuqiu/webgpu-computed/blob/main/README.zh.md)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
A simplified WebGPU compute library that wraps verbose initialization and buffer management, allowing developers to focus on WGSL shader logic.
|
|
6
|
+
A simplified WebGPU compute library that encapsulates tedious initialization and buffer management, allowing developers to focus on WGSL shader logic.
|
|
8
7
|
|
|
9
8
|
## Features
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
10
|
+
- 🚀 Simplified WebGPU initialization
|
|
11
|
+
- 📦 Automatic buffer management and layout calculation
|
|
12
|
+
- 🔧 Support for complex data structures (vectors, matrices)
|
|
13
|
+
- ⚡ High-performance GPU computing
|
|
14
|
+
- 📚 Built-in common WGSL functions
|
|
15
|
+
- ✅ Support for Node.js environment
|
|
16
|
+
- 🛠️ TypeScript support
|
|
17
|
+
- 📖 Detailed Chinese documentation and examples
|
|
18
|
+
- 🔄 Support for buffer reuse
|
|
19
|
+
- ⚛️ Support for atomic operations (u32)
|
|
21
20
|
|
|
22
21
|
## Installation
|
|
23
22
|
|
|
@@ -37,13 +36,13 @@ import { GpuComputed } from 'webgpu-computed';
|
|
|
37
36
|
// Initialize WebGPU
|
|
38
37
|
await GpuComputed.init();
|
|
39
38
|
|
|
40
|
-
// In Node.js, call
|
|
39
|
+
// In Node.js environment, call after use:
|
|
41
40
|
// GpuComputed.destroy()
|
|
42
41
|
```
|
|
43
42
|
|
|
44
|
-
### 2.
|
|
43
|
+
### 2. Perform Simple Computation
|
|
45
44
|
|
|
46
|
-
|
|
45
|
+
Here's a simple vector addition example:
|
|
47
46
|
|
|
48
47
|
```javascript
|
|
49
48
|
import { GpuComputed } from 'webgpu-computed';
|
|
@@ -55,7 +54,7 @@ const data = {
|
|
|
55
54
|
output: new Array(4).fill(0) // Output buffer
|
|
56
55
|
};
|
|
57
56
|
|
|
58
|
-
// WGSL
|
|
57
|
+
// WGSL compute code
|
|
59
58
|
const code = `
|
|
60
59
|
output[index] = inputA[index] + inputB[index];
|
|
61
60
|
`;
|
|
@@ -65,7 +64,7 @@ GpuComputed.computed({
|
|
|
65
64
|
code,
|
|
66
65
|
data,
|
|
67
66
|
synchronize: ["output"], // Fields to return
|
|
68
|
-
workgroupCount: [1] //
|
|
67
|
+
workgroupCount: [1] // Workgroup count
|
|
69
68
|
}).then(results => {
|
|
70
69
|
console.log(results); // [[1.5, 3.5, 5.5, 7.5]]
|
|
71
70
|
})
|
|
@@ -89,25 +88,26 @@ const code = `
|
|
|
89
88
|
output[index].vel = positions[index].vel * 2.0;
|
|
90
89
|
`;
|
|
91
90
|
|
|
91
|
+
// Execute computation
|
|
92
92
|
GpuComputed.computed({
|
|
93
93
|
code,
|
|
94
94
|
data,
|
|
95
|
-
synchronize: ["output"],
|
|
96
|
-
workgroupCount: [1]
|
|
95
|
+
synchronize: ["output"], // Fields to return
|
|
96
|
+
workgroupCount: [1] // Workgroup count
|
|
97
97
|
}).then(results => {
|
|
98
|
-
console.log(results);
|
|
98
|
+
console.log(results); // [[1.100000023841858,2.200000047683716,3.299999952316284,0,0.20000000298023224,0.4000000059604645,0.6000000238418579,0,4.400000095367432,5.5,6.599999904632568,0,0.800000011920929,1,1.2000000476837158,0]]
|
|
99
99
|
})
|
|
100
100
|
```
|
|
101
101
|
|
|
102
102
|
### 4. Using Different Data Types
|
|
103
103
|
|
|
104
|
-
#### Unsigned
|
|
104
|
+
#### Using Unsigned Integers (u32)
|
|
105
105
|
|
|
106
106
|
```javascript
|
|
107
107
|
import { GpuComputed } from 'webgpu-computed';
|
|
108
108
|
|
|
109
109
|
const data = {
|
|
110
|
-
counters: new Uint32Array([0, 1, 2, 3]),
|
|
110
|
+
counters: new Uint32Array([0, 1, 2, 3]), // u32 array
|
|
111
111
|
output: new Uint32Array(4)
|
|
112
112
|
};
|
|
113
113
|
|
|
@@ -125,13 +125,13 @@ const results = await GpuComputed.computed({
|
|
|
125
125
|
console.log(results[0]); // [0, 2, 4, 6]
|
|
126
126
|
```
|
|
127
127
|
|
|
128
|
-
#### Atomic Operations
|
|
128
|
+
#### Using Atomic Operations
|
|
129
129
|
|
|
130
130
|
```javascript
|
|
131
131
|
import { GpuComputed, AtomicUint32Array } from 'webgpu-computed';
|
|
132
132
|
|
|
133
133
|
const data = {
|
|
134
|
-
atomicCounter: new AtomicUint32Array([0]),
|
|
134
|
+
atomicCounter: new AtomicUint32Array([0]), // Atomic counter
|
|
135
135
|
output: new Uint32Array(4)
|
|
136
136
|
};
|
|
137
137
|
|
|
@@ -152,36 +152,121 @@ console.log(results[0]); // [1, 2, 3, 4]
|
|
|
152
152
|
|
|
153
153
|
### 5. Manually Creating a GpuComputed Instance
|
|
154
154
|
|
|
155
|
-
If you need
|
|
155
|
+
If you need finer control, you can create a GpuComputed instance directly:
|
|
156
156
|
|
|
157
157
|
```javascript
|
|
158
158
|
import { GpuComputed } from 'webgpu-computed';
|
|
159
159
|
|
|
160
|
+
// 1. Define data template
|
|
160
161
|
const template = {
|
|
161
162
|
inputA: [] as number[],
|
|
162
163
|
inputB: [] as number[],
|
|
163
164
|
output: [] as number[]
|
|
164
165
|
};
|
|
165
166
|
|
|
167
|
+
// 2. Create instance
|
|
166
168
|
const gpuComputed = new GpuComputed(template, {
|
|
167
169
|
code: `
|
|
168
170
|
output[index] = inputA[index] + inputB[index];
|
|
169
171
|
`,
|
|
170
|
-
workgroupSize: [32, 1, 1]
|
|
172
|
+
workgroupSize: [32, 1, 1] // Optional: Custom workgroup size
|
|
171
173
|
});
|
|
172
174
|
|
|
175
|
+
// 3. Initialize pipeline
|
|
173
176
|
await gpuComputed.initPipeline();
|
|
174
177
|
|
|
178
|
+
// 4. Prepare data
|
|
175
179
|
const data = {
|
|
176
180
|
inputA: [1.0, 2.0, 3.0, 4.0],
|
|
177
181
|
inputB: [0.5, 1.5, 2.5, 3.5],
|
|
178
182
|
output: new Array(4).fill(0)
|
|
179
183
|
};
|
|
180
184
|
|
|
185
|
+
// 5. Create bind group
|
|
181
186
|
const bindGroup = gpuComputed.createBindGroup(data);
|
|
187
|
+
|
|
188
|
+
// 6. Execute computation
|
|
182
189
|
const results = await gpuComputed.computed(bindGroup, [1], ['output']);
|
|
183
190
|
|
|
184
|
-
console.log(results[0]);
|
|
191
|
+
console.log(results[0]); // [1.5, 3.5, 5.5, 7.5]
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
#### Buffer Reuse
|
|
195
|
+
|
|
196
|
+
```javascript
|
|
197
|
+
// First computation
|
|
198
|
+
const data1 = { input: [1, 2, 3], output: [0, 0, 0] };
|
|
199
|
+
const bindGroup1 = gpuComputed.createBindGroup(data1);
|
|
200
|
+
const results1 = await gpuComputed.computed(bindGroup1, [1], ['output']);
|
|
201
|
+
|
|
202
|
+
// Reuse the output buffer from the first computation for the second
|
|
203
|
+
// Note: The template must still include output to build the compute code
|
|
204
|
+
// const template = {
|
|
205
|
+
// input: [] as number[],
|
|
206
|
+
// output: [] as number[]
|
|
207
|
+
// };
|
|
208
|
+
const data2 = { input: [4, 5, 6]};
|
|
209
|
+
const bindGroup2 = gpuComputed.createBindGroup(data2, bindGroup1); // Reuse output buffer
|
|
210
|
+
const results2 = await gpuComputed.computed(bindGroup2, [1], ['output']);
|
|
211
|
+
|
|
212
|
+
console.log(results2[0]); // [4, 5, 6] output buffer reused
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
#### Using Struct Data
|
|
216
|
+
|
|
217
|
+
```javascript
|
|
218
|
+
// Define struct template
|
|
219
|
+
const structTemplate = {
|
|
220
|
+
particles: {
|
|
221
|
+
layout: [
|
|
222
|
+
{ name: 'position', type: 'vec3' },
|
|
223
|
+
{ name: 'velocity', type: 'vec3' },
|
|
224
|
+
{ name: 'mass', type: 'f32' }
|
|
225
|
+
]
|
|
226
|
+
},
|
|
227
|
+
output: {
|
|
228
|
+
layout: [
|
|
229
|
+
{ name: 'position', type: 'vec3' },
|
|
230
|
+
{ name: 'velocity', type: 'vec3' },
|
|
231
|
+
{ name: 'mass', type: 'f32' }
|
|
232
|
+
]
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const gpuComputed = new GpuComputed(structTemplate, {
|
|
237
|
+
code: `
|
|
238
|
+
output[index].position = particles[index].position + particles[index].velocity;
|
|
239
|
+
output[index].velocity = particles[index].velocity * 2.0;
|
|
240
|
+
output[index].mass = particles[index].mass * 1.5;
|
|
241
|
+
`
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
await gpuComputed.initPipeline();
|
|
245
|
+
|
|
246
|
+
const data = {
|
|
247
|
+
particles: [
|
|
248
|
+
{ position: [1, 2, 3], velocity: [0.1, 0.2, 0.3], mass: 1.0 },
|
|
249
|
+
{ position: [4, 5, 6], velocity: [0.4, 0.5, 0.6], mass: 2.0 }
|
|
250
|
+
],
|
|
251
|
+
output: [
|
|
252
|
+
{ position: [0, 0, 0], velocity: [0, 0, 0], mass: 0 },
|
|
253
|
+
{ position: [0, 0, 0], velocity: [0, 0, 0], mass: 0 }
|
|
254
|
+
]
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const bindGroup = gpuComputed.createBindGroup(data);
|
|
258
|
+
const results = await gpuComputed.computed(bindGroup, [1], ['output']);
|
|
259
|
+
|
|
260
|
+
console.log(results[0]); // Mapped data
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
#### Data Mapping
|
|
264
|
+
|
|
265
|
+
When using structs, you can use the `dataMap` method to map results back to the original structure:
|
|
266
|
+
|
|
267
|
+
```javascript
|
|
268
|
+
const mappedData = gpuComputed.dataMap(results[0], 'output');
|
|
269
|
+
console.log(mappedData); // Returns structured object array
|
|
185
270
|
```
|
|
186
271
|
|
|
187
272
|
## API Reference
|
|
@@ -194,41 +279,358 @@ console.log(results[0]);
|
|
|
194
279
|
|
|
195
280
|
Initializes the WebGPU environment. Must be called before using other features.
|
|
196
281
|
|
|
282
|
+
**Returns**: `Promise<void>`
|
|
283
|
+
|
|
284
|
+
**Throws**: If the browser does not support WebGPU or fails to get adapter/device
|
|
285
|
+
|
|
197
286
|
##### `GpuComputed.computed(options)`
|
|
198
287
|
|
|
199
|
-
Executes a GPU
|
|
288
|
+
Executes a GPU compute task.
|
|
200
289
|
|
|
201
290
|
**Parameters**:
|
|
202
291
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
292
|
+
- `code` (string): WGSL compute code
|
|
293
|
+
- `data` (object): Input/output data object
|
|
294
|
+
- `workgroupCount` (array): Workgroup count [x, y?, z?]
|
|
295
|
+
- `workgroupSize` (array, optional): Workgroup size, default [32, 1, 1]
|
|
296
|
+
- `globalInvocationIdName` (string, optional): Global invocation ID variable name, default "grid"
|
|
297
|
+
- `workgroupIndexName` (string, optional): Workgroup index variable name, default "index"
|
|
298
|
+
- `synchronize` (array, optional): Array of buffer names to sync back to CPU
|
|
299
|
+
- `beforeCodes` (array, optional): WGSL code snippets before the compute function
|
|
300
|
+
- `onSuccess` (function, optional): Success callback function
|
|
301
|
+
|
|
302
|
+
**Returns**: `Promise<Array<Float32Array | Uint32Array | Int32Array>>` - Data of synchronized buffers
|
|
303
|
+
|
|
304
|
+
### Data Types
|
|
305
|
+
|
|
306
|
+
Supports the following WGSL types:
|
|
307
|
+
|
|
308
|
+
- `f32`: Single-precision float
|
|
309
|
+
- `u32`: Unsigned 32-bit integer
|
|
310
|
+
- `vec2`: 2D vector
|
|
311
|
+
- `vec3`: 3D vector
|
|
312
|
+
- `vec4`: 4D vector
|
|
313
|
+
- `mat3x3`: 3x3 matrix
|
|
314
|
+
- `mat4x4`: 4x4 matrix
|
|
315
|
+
|
|
316
|
+
### Supported JavaScript Types
|
|
317
|
+
|
|
318
|
+
- `number[]`: Number array (automatically converted to Float32Array)
|
|
319
|
+
- `Float32Array`: 32-bit float array
|
|
320
|
+
- `Uint32Array`: 32-bit unsigned integer array
|
|
321
|
+
- `Int32Array`: 32-bit signed integer array
|
|
322
|
+
- `AtomicUint32Array`: Atomic unsigned integer array
|
|
323
|
+
|
|
324
|
+
### Built-in WGSL Functions
|
|
325
|
+
|
|
326
|
+
The library provides some common WGSL helper functions:
|
|
327
|
+
|
|
328
|
+
#### Quaternion Rotation
|
|
329
|
+
|
|
330
|
+
```wgsl
|
|
331
|
+
fn quat_rotate(q: vec4<f32>, v: vec3<f32>) -> vec3<f32>
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
Usage example:
|
|
335
|
+
|
|
336
|
+
```javascript
|
|
337
|
+
import { WGSL_Fun } from 'webgpu-computed';
|
|
338
|
+
|
|
339
|
+
await GpuComputed.computed({
|
|
340
|
+
code: "",
|
|
341
|
+
data: {....},
|
|
342
|
+
beforeCodes:[WGSL_Fun.quat_rotate]
|
|
343
|
+
})
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
#### Point in OBB Detection
|
|
347
|
+
|
|
348
|
+
```wgsl
|
|
349
|
+
fn point_in_obb(point: vec3<f32>, center: vec3<f32>, halfSize: vec3<f32>, quat: vec4<f32>) -> bool
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
## Advanced Usage
|
|
353
|
+
|
|
354
|
+
### Custom Workgroup Configuration
|
|
355
|
+
|
|
356
|
+
```javascript
|
|
357
|
+
await GpuComputed.computed({
|
|
358
|
+
code: '...',
|
|
359
|
+
data: {...},
|
|
360
|
+
workgroupCount: [4, 4], // 16 workgroups
|
|
361
|
+
workgroupSize: [16, 16], // 256 threads per workgroup
|
|
362
|
+
});
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
### Buffer Reuse
|
|
366
|
+
|
|
367
|
+
```javascript
|
|
368
|
+
// Create initial bind group
|
|
369
|
+
const initialData = { buffer: new Float32Array(1000) };
|
|
370
|
+
const bindGroup = gpuComputed.createBindGroup(initialData);
|
|
371
|
+
|
|
372
|
+
// Reuse buffer for multiple computations
|
|
373
|
+
for (let i = 0; i < 10; i++) {
|
|
374
|
+
const newData = { buffer: new Float32Array(1000).fill(i) };
|
|
375
|
+
const reusedBindGroup = gpuComputed.createBindGroup(newData, bindGroup);
|
|
376
|
+
const results = await gpuComputed.computed(reusedBindGroup, [32], ['buffer']);
|
|
377
|
+
// Process results...
|
|
378
|
+
}
|
|
379
|
+
```
|
|
380
|
+
|
|
381
|
+
### Synchronizing Data Back to CPU
|
|
382
|
+
|
|
383
|
+
```javascript
|
|
384
|
+
const results = await GpuComputed.computed({
|
|
385
|
+
code: '...',
|
|
386
|
+
data: {...},
|
|
387
|
+
synchronize: ['output'], // Specify buffers to sync
|
|
388
|
+
workgroupCount: [1]
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
// results contains synchronized data
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
### Callback Functions
|
|
395
|
+
|
|
396
|
+
```javascript
|
|
397
|
+
await GpuComputed.computed({
|
|
398
|
+
code: '...',
|
|
399
|
+
data: {...},
|
|
400
|
+
workgroupCount: [1],
|
|
401
|
+
onSuccess: ({ gpuComputed, group, results }) => {
|
|
402
|
+
console.log('Computation complete', results);
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
## Example Project
|
|
408
|
+
|
|
409
|
+
```js
|
|
410
|
+
import { GpuComputed } from "webgpu-computed"
|
|
411
|
+
import * as WGSL_Fun from "webgpu-computed"
|
|
412
|
+
|
|
413
|
+
// 1. Initialize WebGPU
|
|
414
|
+
console.log('Initializing WebGPU...');
|
|
415
|
+
await GpuComputed.init();
|
|
416
|
+
console.log('WebGPU initialized successfully');
|
|
417
|
+
|
|
418
|
+
// 2. Simple array computation example
|
|
419
|
+
console.log('\n=== Simple Array Computation ===');
|
|
420
|
+
const simpleData = {
|
|
421
|
+
inputA: [1.0, 2.0, 3.0, 4.0],
|
|
422
|
+
inputB: [0.5, 1.5, 2.5, 3.5],
|
|
423
|
+
output: new Array(4).fill(0)
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
const simpleCode = `
|
|
427
|
+
output[index] = inputA[index] + inputB[index];
|
|
428
|
+
`;
|
|
429
|
+
|
|
430
|
+
const simpleResults = await GpuComputed.computed({
|
|
431
|
+
code: simpleCode,
|
|
432
|
+
data: simpleData,
|
|
433
|
+
workgroupCount: [1],
|
|
434
|
+
synchronize: ['output']
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
console.log('Simple computation results:', simpleResults[0]); // [1.5, 3.5, 5.5, 7.5]
|
|
438
|
+
|
|
439
|
+
// 3. Using u32 type example
|
|
440
|
+
console.log('\n=== Using u32 Type ===');
|
|
441
|
+
const u32Data = {
|
|
442
|
+
counters: new Uint32Array([10, 20, 30, 40]),
|
|
443
|
+
output: new Uint32Array(4)
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
const u32Code = `
|
|
447
|
+
output[index] = counters[index] + 5u;
|
|
448
|
+
`;
|
|
449
|
+
|
|
450
|
+
const u32Results = await GpuComputed.computed({
|
|
451
|
+
code: u32Code,
|
|
452
|
+
data: u32Data,
|
|
453
|
+
workgroupCount: [1],
|
|
454
|
+
synchronize: ['output']
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
console.log('u32 computation results:', u32Results[0]); // [15, 25, 35, 45]
|
|
458
|
+
|
|
459
|
+
// 4. Atomic operations example
|
|
460
|
+
console.log('\n=== Atomic Operations Example ===');
|
|
461
|
+
const atomicData = {
|
|
462
|
+
counter: new AtomicUint32Array([0]),
|
|
463
|
+
results: new Uint32Array(4)
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
const atomicCode = `
|
|
467
|
+
let oldValue = atomicAdd(&counter[0], 1u);
|
|
468
|
+
results[index] = oldValue;
|
|
469
|
+
`;
|
|
470
|
+
|
|
471
|
+
const atomicResults = await GpuComputed.computed({
|
|
472
|
+
code: atomicCode,
|
|
473
|
+
data: atomicData,
|
|
474
|
+
workgroupCount: [1],
|
|
475
|
+
synchronize: ['results']
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
console.log('Atomic operation results:', atomicResults[0]); // [0, 1, 2, 3]
|
|
479
|
+
|
|
480
|
+
// 5. Complex data structure example (structs)
|
|
481
|
+
console.log('\n=== Complex Data Structure Computation ===');
|
|
482
|
+
const complexData = {
|
|
483
|
+
particles: [
|
|
484
|
+
{ position: [1.0, 2.0, 3.0], velocity: [0.1, 0.2, 0.3], mass: 1.0 },
|
|
485
|
+
{ position: [4.0, 5.0, 6.0], velocity: [0.4, 0.5, 0.6], mass: 2.0 }
|
|
486
|
+
],
|
|
487
|
+
output: [
|
|
488
|
+
{ position: [0, 0, 0], velocity: [0, 0, 0], mass: 0 },
|
|
489
|
+
{ position: [0, 0, 0], velocity: [0, 0, 0], mass: 0 }
|
|
490
|
+
]
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
const complexCode = `
|
|
494
|
+
output[index].position = particles[index].position + particles[index].velocity;
|
|
495
|
+
output[index].velocity = particles[index].velocity * 2.0;
|
|
496
|
+
output[index].mass = particles[index].mass * 1.5;
|
|
497
|
+
`;
|
|
498
|
+
|
|
499
|
+
const complexResults = await GpuComputed.computed({
|
|
500
|
+
code: complexCode,
|
|
501
|
+
data: complexData,
|
|
502
|
+
workgroupCount: [1],
|
|
503
|
+
synchronize: ['output']
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
console.log('Complex computation results:', complexResults[0]);
|
|
507
|
+
|
|
508
|
+
// 6. Using built-in WGSL functions example
|
|
509
|
+
console.log('\n=== Using Built-in WGSL Functions ===');
|
|
510
|
+
const wgslFunData = {
|
|
511
|
+
points: [
|
|
512
|
+
{
|
|
513
|
+
x: 1.0, y: 0.0, z: 0.0
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
x: 0.0, y: 1.0, z: 0.0
|
|
517
|
+
},
|
|
518
|
+
{
|
|
519
|
+
x: -1.0, y: 0.0, z: 0.0
|
|
520
|
+
}
|
|
521
|
+
],
|
|
522
|
+
obbCenter: [0.0, 0.0, 0.0],
|
|
523
|
+
obbHalfSize: [2.0, 2.0, 2.0],
|
|
524
|
+
obbRotation: [0.0, 0.0, 0.0, 1.0], // Unit quaternion, no rotation
|
|
525
|
+
results: new Array(3).fill(0)
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
const wgslFunCode = `
|
|
529
|
+
let point = vec3(points[index].x, points[index].y, points[index].z);
|
|
530
|
+
let center = vec3<f32>(obbCenter[0], obbCenter[1], obbCenter[2]);
|
|
531
|
+
let halfSize = vec3<f32>(obbHalfSize[0], obbHalfSize[1], obbHalfSize[2]);
|
|
532
|
+
let quat = vec4<f32>(obbRotation[0], obbRotation[1], obbRotation[2], obbRotation[3]);
|
|
533
|
+
|
|
534
|
+
if (point_in_obb(point, center, halfSize, quat)) {
|
|
535
|
+
results[index] = 1.0;
|
|
536
|
+
} else {
|
|
537
|
+
results[index] = 0.0;
|
|
538
|
+
}
|
|
539
|
+
`;
|
|
540
|
+
|
|
541
|
+
const wgslFunResults = await GpuComputed.computed({
|
|
542
|
+
code: wgslFunCode,
|
|
543
|
+
data: wgslFunData,
|
|
544
|
+
workgroupCount: [1],
|
|
545
|
+
beforeCodes: [WGSL_Fun.quat_rotate, WGSL_Fun.point_in_obb, /** Add your own function code */],
|
|
546
|
+
synchronize: ['results']
|
|
547
|
+
});
|
|
548
|
+
|
|
549
|
+
console.log('OBB detection results:', wgslFunResults[0]); // [1, 1, 1] All points inside OBB
|
|
550
|
+
|
|
551
|
+
// 7. Custom workgroup configuration example
|
|
552
|
+
console.log('\n=== Custom Workgroup Configuration ===');
|
|
553
|
+
const largeData = {
|
|
554
|
+
largeArray: new Array(1024).fill(0).map((_, i) => i * 1.0),
|
|
555
|
+
output: new Array(1024).fill(0)
|
|
556
|
+
};
|
|
557
|
+
|
|
558
|
+
const largeCode = `
|
|
559
|
+
output[index] = largeArray[index] * 2.0;
|
|
560
|
+
`;
|
|
561
|
+
|
|
562
|
+
const largeResults = await GpuComputed.computed({
|
|
563
|
+
code: largeCode,
|
|
564
|
+
data: largeData,
|
|
565
|
+
workgroupCount: [32], // 32 workgroups
|
|
566
|
+
workgroupSize: [32, 1, 1], // 32 threads per workgroup, total 1024 threads
|
|
567
|
+
synchronize: ['output']
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
console.log('Large array computation results (first 10):', largeResults[0].slice(0, 10));
|
|
571
|
+
|
|
572
|
+
// 8. Using callback functions example
|
|
573
|
+
console.log('\n=== Using Callback Functions ===');
|
|
574
|
+
const callbackData = {
|
|
575
|
+
values: [10.0, 20.0, 30.0],
|
|
576
|
+
squares: new Array(3).fill(0)
|
|
577
|
+
};
|
|
578
|
+
|
|
579
|
+
const callbackCode = `
|
|
580
|
+
squares[index] = values[index] * values[index];
|
|
581
|
+
`;
|
|
582
|
+
|
|
583
|
+
await GpuComputed.computed({
|
|
584
|
+
code: callbackCode,
|
|
585
|
+
data: callbackData,
|
|
586
|
+
workgroupCount: [1],
|
|
587
|
+
synchronize: ['squares'],
|
|
588
|
+
onSuccess: ({ gpuComputed, group, results }) => {
|
|
589
|
+
console.log('Callback triggered, square computation results:', results[0]); // [100, 400, 900]
|
|
590
|
+
}
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
// 9. Multi-dimensional workgroups example
|
|
594
|
+
console.log('\n=== Multi-dimensional Workgroups ===');
|
|
595
|
+
const matrixData = {
|
|
596
|
+
matrixA: new Array(16).fill(0).map((_, i) => i * 1.0),
|
|
597
|
+
matrixB: new Array(16).fill(0).map((_, i) => (i + 1) * 1.0),
|
|
598
|
+
result: new Array(16).fill(0)
|
|
599
|
+
};
|
|
600
|
+
|
|
601
|
+
const matrixCode = `
|
|
602
|
+
let x = index % 4u;
|
|
603
|
+
let y = index / 4u;
|
|
604
|
+
let idx = y * 4u + x;
|
|
605
|
+
result[idx] = matrixA[idx] + matrixB[idx];
|
|
606
|
+
`;
|
|
607
|
+
|
|
608
|
+
const matrixResults = await GpuComputed.computed({
|
|
609
|
+
code: matrixCode,
|
|
610
|
+
data: matrixData,
|
|
611
|
+
workgroupCount: [4, 4], // 4x4 workgroup grid
|
|
612
|
+
workgroupSize: [1, 1, 1], // 1 thread per workgroup
|
|
613
|
+
synchronize: ['result']
|
|
614
|
+
});
|
|
615
|
+
|
|
616
|
+
console.log('Matrix computation results:', matrixResults[0]);
|
|
617
|
+
|
|
618
|
+
console.log('\nAll feature examples completed!');
|
|
619
|
+
```
|
|
222
620
|
|
|
223
621
|
## Browser Support
|
|
224
622
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
623
|
+
- Chrome 113+
|
|
624
|
+
- Edge 113+
|
|
625
|
+
- Firefox (partial support)
|
|
626
|
+
- Safari (partial support)
|
|
229
627
|
|
|
230
628
|
Ensure the browser supports the WebGPU API.
|
|
231
629
|
|
|
630
|
+
## Contributing
|
|
631
|
+
|
|
632
|
+
Issues and Pull Requests are welcome!
|
|
633
|
+
|
|
232
634
|
## License
|
|
233
635
|
|
|
234
|
-
ISC License
|
|
636
|
+
ISC License
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -40,8 +40,8 @@ function U(f) {
|
|
|
40
40
|
const e = Object.keys(f), r = e.map((c) => {
|
|
41
41
|
const l = f[c];
|
|
42
42
|
if (Array.isArray(l)) {
|
|
43
|
-
for (const
|
|
44
|
-
if ($[
|
|
43
|
+
for (const y of ["vec2", "vec3", "vec4", "mat3x3", "mat4x4"])
|
|
44
|
+
if ($[y] === l.length) return y;
|
|
45
45
|
throw new Error(`${c} 不支持的数组长度 ${l.length}`);
|
|
46
46
|
}
|
|
47
47
|
if (typeof l == "number") return "f32";
|
|
@@ -50,15 +50,15 @@ function U(f) {
|
|
|
50
50
|
if (e.length !== r.length) throw new Error("keys 与 types 长度不一致");
|
|
51
51
|
let s = 0;
|
|
52
52
|
const u = e.map((c, l) => {
|
|
53
|
-
const
|
|
54
|
-
s += _(s,
|
|
53
|
+
const y = r[l];
|
|
54
|
+
s += _(s, y);
|
|
55
55
|
const a = {
|
|
56
56
|
name: c,
|
|
57
|
-
type:
|
|
57
|
+
type: y,
|
|
58
58
|
offset: s,
|
|
59
|
-
size: $[
|
|
59
|
+
size: $[y]
|
|
60
60
|
};
|
|
61
|
-
return s += $[
|
|
61
|
+
return s += $[y], a;
|
|
62
62
|
}), t = Math.max(...r.map((c) => E[c]));
|
|
63
63
|
return {
|
|
64
64
|
stride: s + (t - s % t) % t,
|
|
@@ -111,7 +111,7 @@ class T {
|
|
|
111
111
|
r(t.layout);
|
|
112
112
|
const n = t.layout[t.layout.length - 1], c = n.offset + n.size;
|
|
113
113
|
let l = 1;
|
|
114
|
-
for (const
|
|
114
|
+
for (const y of t.layout) l = Math.max(l, E[y.type]);
|
|
115
115
|
t.stride = P(c, l);
|
|
116
116
|
};
|
|
117
117
|
Object.keys(e).forEach((t) => {
|
|
@@ -133,9 +133,9 @@ class T {
|
|
|
133
133
|
if (!this.template) throw new Error("初始化计算管线错误,未找到可用数据模版");
|
|
134
134
|
await T.init();
|
|
135
135
|
const e = this.template, { device: r } = await this.getDevice(), s = [], u = [], t = [];
|
|
136
|
-
this.device = r, Object.keys(e).forEach((i,
|
|
136
|
+
this.device = r, Object.keys(e).forEach((i, d) => {
|
|
137
137
|
if (t.push({
|
|
138
|
-
binding:
|
|
138
|
+
binding: d,
|
|
139
139
|
// 绑定到组里的0号位插槽
|
|
140
140
|
visibility: GPUShaderStage.COMPUTE,
|
|
141
141
|
// 数据在哪些阶段可以使用, 计算着色器、片元着色器、顶点着色器
|
|
@@ -144,20 +144,20 @@ class T {
|
|
|
144
144
|
}
|
|
145
145
|
}), m(e[i])) {
|
|
146
146
|
const g = e[i], h = g.map((b) => `${b.name}:${b.type === "f32" ? "f32" : b.type + "<f32>"}`).join(","), v = `${S(i)}Struct`;
|
|
147
|
-
s.push(`struct ${S(i)}Struct {${h}};`), u.push(`@group(0) @binding(${
|
|
147
|
+
s.push(`struct ${S(i)}Struct {${h}};`), u.push(`@group(0) @binding(${d}) var<storage, read_write> ${i}: ${v};`);
|
|
148
148
|
} else if (B(e[i])) {
|
|
149
149
|
const g = e[i], h = g.layout.map((b) => `${b.name}:${b.type === "f32" ? "f32" : b.type + "<f32>"}`).join(","), v = `${S(i)}Struct`;
|
|
150
|
-
s.push(`struct ${v} {${h}};`), u.push(`@group(0) @binding(${
|
|
150
|
+
s.push(`struct ${v} {${h}};`), u.push(`@group(0) @binding(${d}) var<storage, read_write> ${i}: array<${v}>;`);
|
|
151
151
|
} else if (ArrayBuffer.isView(e[i]) && !(e[i] instanceof DataView)) {
|
|
152
152
|
const g = e[i], h = q(g);
|
|
153
|
-
g instanceof D ? u.push(`@group(0) @binding(${
|
|
153
|
+
g instanceof D ? u.push(`@group(0) @binding(${d}) var<storage, read_write> ${i}: array<atomic<${h}>>;`) : u.push(`@group(0) @binding(${d}) var<storage, read_write> ${i}: array<${h}>;`);
|
|
154
154
|
}
|
|
155
155
|
});
|
|
156
156
|
const {
|
|
157
157
|
beforeCodes: n = [],
|
|
158
158
|
workgroupSize: c = [32, 1, 1],
|
|
159
159
|
globalInvocationIdName: l = "grid",
|
|
160
|
-
workgroupIndexName:
|
|
160
|
+
workgroupIndexName: y = "index",
|
|
161
161
|
code: a = ""
|
|
162
162
|
} = this.option ?? {}, o = (
|
|
163
163
|
/*wgsl*/
|
|
@@ -168,7 +168,7 @@ ${u.join("")}
|
|
|
168
168
|
|
|
169
169
|
@compute @workgroup_size(${c.join(",")})
|
|
170
170
|
fn main(@builtin(global_invocation_id) ${l}: vec3<u32>) {
|
|
171
|
-
var ${
|
|
171
|
+
var ${y} = ${l}.x;
|
|
172
172
|
${a}
|
|
173
173
|
}
|
|
174
174
|
`
|
|
@@ -200,23 +200,23 @@ ${u.join("")}
|
|
|
200
200
|
function c(a, o, p = 0, i) {
|
|
201
201
|
if (ArrayBuffer.isView(a) || Array.isArray(a)) return a;
|
|
202
202
|
if (!i) {
|
|
203
|
-
const
|
|
203
|
+
const d = o[o.length - 1], g = d.offset + d.size;
|
|
204
204
|
let h = 1;
|
|
205
205
|
for (const v of o) h = Math.max(h, E[v.type]);
|
|
206
206
|
i = i ?? new Float32Array(P(g, h)).fill(0);
|
|
207
207
|
}
|
|
208
|
-
return o.forEach((
|
|
209
|
-
let g = a[
|
|
208
|
+
return o.forEach((d) => {
|
|
209
|
+
let g = a[d.name];
|
|
210
210
|
Array.isArray(g) || (g = [g]);
|
|
211
|
-
for (let h = 0; h <
|
|
212
|
-
i[p +
|
|
211
|
+
for (let h = 0; h < d.size; h++)
|
|
212
|
+
i[p + d.offset + h] = Number(g[h] ?? 0);
|
|
213
213
|
}), i;
|
|
214
214
|
}
|
|
215
215
|
function l(a, o) {
|
|
216
216
|
if (ArrayBuffer.isView(a) || typeof a[0] == "number") return a;
|
|
217
217
|
const p = new Float32Array(o.stride * a.length).fill(0);
|
|
218
|
-
return a.forEach((i,
|
|
219
|
-
const g =
|
|
218
|
+
return a.forEach((i, d) => {
|
|
219
|
+
const g = d * o.stride;
|
|
220
220
|
c(i, o.layout, g, p);
|
|
221
221
|
}), p;
|
|
222
222
|
}
|
|
@@ -229,13 +229,13 @@ ${u.join("")}
|
|
|
229
229
|
const o = u[a], p = e[a];
|
|
230
230
|
let i = [];
|
|
231
231
|
m(o) ? i = c(p, o) : B(o) ? i = l(p, o) : (Array.isArray(p) || ArrayBuffer.isView(p)) && (i = p);
|
|
232
|
-
let
|
|
233
|
-
if (o instanceof Float32Array ?
|
|
232
|
+
let d = null;
|
|
233
|
+
if (o instanceof Float32Array ? d = new Float32Array(i) : o instanceof Uint32Array ? d = new Uint32Array(i) : o instanceof Int32Array ? d = new Int32Array(i) : d = new Float32Array(i), !d) throw new Error("不支持的数组类型" + o);
|
|
234
234
|
const g = s.createBuffer({
|
|
235
|
-
size:
|
|
235
|
+
size: d.byteLength,
|
|
236
236
|
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
|
237
237
|
});
|
|
238
|
-
s.queue.writeBuffer(g, 0,
|
|
238
|
+
s.queue.writeBuffer(g, 0, d), t.push({ name: a, buffer: g });
|
|
239
239
|
}), {
|
|
240
240
|
group: s.createBindGroup({
|
|
241
241
|
layout: this.groupLayout,
|
|
@@ -257,9 +257,9 @@ ${u.join("")}
|
|
|
257
257
|
const s = this.template[r], u = e.length / s.stride, t = [];
|
|
258
258
|
for (let n = 0; n < u; n++) {
|
|
259
259
|
const c = n * s.stride, l = {};
|
|
260
|
-
s.layout.forEach((
|
|
261
|
-
const a = e.slice(c +
|
|
262
|
-
l[
|
|
260
|
+
s.layout.forEach((y) => {
|
|
261
|
+
const a = e.slice(c + y.offset, c + y.offset + y.size);
|
|
262
|
+
l[y.name] = a.length === 1 ? a[0] : [...a];
|
|
263
263
|
}), t.push(l);
|
|
264
264
|
}
|
|
265
265
|
return t;
|
|
@@ -268,7 +268,7 @@ ${u.join("")}
|
|
|
268
268
|
const s = this.template[r], u = {};
|
|
269
269
|
return s.forEach((t) => {
|
|
270
270
|
const n = e.slice(t.offset, t.offset + t.size);
|
|
271
|
-
u[t.name] = n.length === 1 ? n[0] : n;
|
|
271
|
+
u[t.name] = n.length === 1 ? n[0] : [...n];
|
|
272
272
|
}), u;
|
|
273
273
|
}
|
|
274
274
|
return e;
|
|
@@ -293,17 +293,15 @@ ${u.join("")}
|
|
|
293
293
|
}
|
|
294
294
|
}).filter((o) => !!o);
|
|
295
295
|
u.queue.submit([n.finish()]), await u.queue.onSubmittedWorkDone();
|
|
296
|
-
const
|
|
296
|
+
const y = /* @__PURE__ */ new Map();
|
|
297
297
|
return await Promise.all(
|
|
298
298
|
l.map(async (o) => {
|
|
299
299
|
await o.buffer.mapAsync(GPUMapMode.READ);
|
|
300
300
|
const p = o.buffer.getMappedRange();
|
|
301
301
|
let i = null;
|
|
302
|
-
this.template[o.name] instanceof Float32Array ? i = new Float32Array(p) : this.template[o.name] instanceof Uint32Array ? i = new Uint32Array(p) : this.template[o.name] instanceof Int32Array ? i = new Int32Array(p) : i = new Float32Array(p);
|
|
303
|
-
const y = [...i];
|
|
304
|
-
d.set(o.name, y);
|
|
302
|
+
this.template[o.name] instanceof Float32Array ? i = new Float32Array(p) : this.template[o.name] instanceof Uint32Array ? i = new Uint32Array(p) : this.template[o.name] instanceof Int32Array ? i = new Int32Array(p) : i = new Float32Array(p), y.set(o.name, i);
|
|
305
303
|
})
|
|
306
|
-
), s.map((o) =>
|
|
304
|
+
), s.map((o) => y.get(o));
|
|
307
305
|
}
|
|
308
306
|
/** 初始化gpu设备
|
|
309
307
|
* @returns
|
|
@@ -359,8 +357,8 @@ ${u.join("")}
|
|
|
359
357
|
*/
|
|
360
358
|
static async computed(e) {
|
|
361
359
|
let { data: r, map: s = !1, workgroupCount: u, synchronize: t, onSuccess: n, ...c } = e;
|
|
362
|
-
const l = await this.fromByData({ data: r, ...c }),
|
|
363
|
-
return n && n({ gpuComputed: l, group:
|
|
360
|
+
const l = await this.fromByData({ data: r, ...c }), y = l.createBindGroup(r), a = await l.computed(y, u, t);
|
|
361
|
+
return n && n({ gpuComputed: l, group: y, results: a }), s ? a.map((o, p) => l.dataMap(o, t[p])) : a;
|
|
364
362
|
}
|
|
365
363
|
}
|
|
366
364
|
const M = (
|
|
@@ -74,14 +74,14 @@ export declare class GpuComputed {
|
|
|
74
74
|
* @param array
|
|
75
75
|
* @param key
|
|
76
76
|
*/
|
|
77
|
-
dataMap(array:
|
|
77
|
+
dataMap(array: Float32Array | Uint32Array | Int32Array, key: string): Float32Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike> | Record<string, number | number[]> | Record<string, number | number[]>[];
|
|
78
78
|
/** 开始计算
|
|
79
79
|
* @param group 数据组
|
|
80
80
|
* @param workgroupCount 工作组大小
|
|
81
81
|
* @param synchronize 需要同步的数据字段
|
|
82
82
|
* @returns
|
|
83
83
|
*/
|
|
84
|
-
computed(group: BufferGroup, workgroupCount: [number, number?, number?], synchronize?: string[]): Promise<
|
|
84
|
+
computed(group: BufferGroup, workgroupCount: [number, number?, number?], synchronize?: string[]): Promise<(Float32Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike>)[]>;
|
|
85
85
|
/** 初始化gpu设备
|
|
86
86
|
* @returns
|
|
87
87
|
*/
|
|
@@ -112,8 +112,8 @@ export declare class GpuComputed {
|
|
|
112
112
|
onSuccess?: (opt: {
|
|
113
113
|
gpuComputed: GpuComputed;
|
|
114
114
|
group: BufferGroup;
|
|
115
|
-
results:
|
|
115
|
+
results: (Float32Array | Uint32Array | Int32Array)[];
|
|
116
116
|
}) => void;
|
|
117
|
-
} & GpuComputedOption): Promise<(
|
|
117
|
+
} & GpuComputedOption): Promise<(Float32Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Uint32Array<ArrayBufferLike> | Record<string, number | number[]> | Record<string, number | number[]>[])[]>;
|
|
118
118
|
}
|
|
119
119
|
export {};
|