webgpu-computed 0.0.9 → 0.0.11
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 +125 -126
- package/package.json +9 -2
- package/src/index.js +1 -1
- package/README.en.md +0 -496
package/README.md
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
1
|
# webgpu-computed
|
|
2
2
|
|
|
3
|
-
🌐
|
|
4
|
-
- [
|
|
3
|
+
🌐 Other language versions:
|
|
4
|
+
- [简体中文](https://github.com/xiaguochuqiu/webgpu-computed/blob/main/README.zh.md)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
A simplified WebGPU computing library that encapsulates tedious initialization and buffer management, allowing developers to focus on WGSL shader logic.
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## Features
|
|
9
9
|
|
|
10
|
-
- 🚀
|
|
11
|
-
- 📦
|
|
12
|
-
- 🔧
|
|
13
|
-
- ⚡
|
|
14
|
-
- 📚
|
|
15
|
-
- ✅
|
|
16
|
-
- 🛠️ TypeScript
|
|
17
|
-
- 📖
|
|
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 English documentation and examples
|
|
18
18
|
|
|
19
|
-
##
|
|
19
|
+
## Installation
|
|
20
20
|
|
|
21
21
|
```bash
|
|
22
22
|
npm install webgpu-computed
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
##
|
|
25
|
+
## Quick Start
|
|
26
26
|
|
|
27
|
-
### 1.
|
|
27
|
+
### 1. Initialize WebGPU
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
Before using any computing features, you need to initialize the WebGPU environment:
|
|
30
30
|
|
|
31
31
|
```javascript
|
|
32
32
|
import { GpuComputed } from 'webgpu-computed';
|
|
33
33
|
|
|
34
|
-
//
|
|
34
|
+
// Initialize WebGPU
|
|
35
35
|
await GpuComputed.init();
|
|
36
36
|
|
|
37
|
-
//
|
|
37
|
+
// After using in Node.js environment, please call:
|
|
38
38
|
// GpuComputed.destroy()
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
### 2.
|
|
41
|
+
### 2. Perform Simple Computation
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
Here is a simple vector addition example:
|
|
44
44
|
|
|
45
45
|
```javascript
|
|
46
46
|
import { GpuComputed } from 'webgpu-computed';
|
|
47
47
|
|
|
48
|
-
//
|
|
48
|
+
// Prepare data
|
|
49
49
|
const data = {
|
|
50
50
|
inputA: [1.0, 2.0, 3.0, 4.0],
|
|
51
51
|
inputB: [0.5, 1.5, 2.5, 3.5],
|
|
52
|
-
output: new Array(4).fill(0) //
|
|
52
|
+
output: new Array(4).fill(0) // Output buffer
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
-
// WGSL
|
|
55
|
+
// WGSL computation code
|
|
56
56
|
const code = `
|
|
57
57
|
output[index] = inputA[index] + inputB[index];
|
|
58
58
|
`;
|
|
59
59
|
|
|
60
|
-
//
|
|
60
|
+
// Execute computation
|
|
61
61
|
GpuComputed.computed({
|
|
62
62
|
code,
|
|
63
63
|
data,
|
|
64
|
-
synchronize: ["output"], //
|
|
65
|
-
workgroupCount: [1] //
|
|
64
|
+
synchronize: ["output"], // Fields to return
|
|
65
|
+
workgroupCount: [1] // Number of workgroups
|
|
66
66
|
}).then(results => {
|
|
67
67
|
console.log(results); // [[1.5, 3.5, 5.5, 7.5]]
|
|
68
68
|
})
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
-
### 3.
|
|
71
|
+
### 3. Using Complex Data Structures
|
|
72
72
|
|
|
73
|
-
|
|
73
|
+
The library supports vector and matrix types:
|
|
74
74
|
|
|
75
75
|
```javascript
|
|
76
76
|
const data = {
|
|
@@ -86,62 +86,62 @@ const code = `
|
|
|
86
86
|
output[index].vel = positions[index].vel * 2.0;
|
|
87
87
|
`;
|
|
88
88
|
|
|
89
|
-
//
|
|
89
|
+
// Execute computation
|
|
90
90
|
GpuComputed.computed({
|
|
91
91
|
code,
|
|
92
92
|
data,
|
|
93
|
-
synchronize: ["output"], //
|
|
94
|
-
workgroupCount: [1] //
|
|
93
|
+
synchronize: ["output"], // Fields to return
|
|
94
|
+
workgroupCount: [1] // Number of workgroups
|
|
95
95
|
}).then(results => {
|
|
96
96
|
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]]
|
|
97
97
|
})
|
|
98
98
|
```
|
|
99
99
|
|
|
100
|
-
### 4.
|
|
100
|
+
### 4. Manually Create GpuComputed Instance
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
If you need more fine-grained control, you can directly create a GpuComputed instance:
|
|
103
103
|
|
|
104
104
|
```javascript
|
|
105
105
|
import { GpuComputed } from 'webgpu-computed';
|
|
106
106
|
|
|
107
|
-
// 1.
|
|
107
|
+
// 1. Define data template
|
|
108
108
|
const template = {
|
|
109
109
|
inputA: [] as number[],
|
|
110
110
|
inputB: [] as number[],
|
|
111
111
|
output: [] as number[]
|
|
112
112
|
};
|
|
113
113
|
|
|
114
|
-
// 2.
|
|
114
|
+
// 2. Create instance
|
|
115
115
|
const gpuComputed = new GpuComputed(template, {
|
|
116
116
|
code: `
|
|
117
117
|
output[index] = inputA[index] + inputB[index];
|
|
118
118
|
`,
|
|
119
|
-
workgroupSize: [32, 1, 1] //
|
|
119
|
+
workgroupSize: [32, 1, 1] // Optional: custom workgroup size
|
|
120
120
|
});
|
|
121
121
|
|
|
122
|
-
// 3.
|
|
122
|
+
// 3. Initialize pipeline
|
|
123
123
|
await gpuComputed.initPipeline();
|
|
124
124
|
|
|
125
|
-
// 4.
|
|
125
|
+
// 4. Prepare data
|
|
126
126
|
const data = {
|
|
127
127
|
inputA: [1.0, 2.0, 3.0, 4.0],
|
|
128
128
|
inputB: [0.5, 1.5, 2.5, 3.5],
|
|
129
129
|
output: new Array(4).fill(0)
|
|
130
130
|
};
|
|
131
131
|
|
|
132
|
-
// 5.
|
|
132
|
+
// 5. Create bind group
|
|
133
133
|
const bindGroup = gpuComputed.createBindGroup(data);
|
|
134
134
|
|
|
135
|
-
// 6.
|
|
135
|
+
// 6. Execute computation
|
|
136
136
|
const results = await gpuComputed.computed(bindGroup, [1], ['output']);
|
|
137
137
|
|
|
138
138
|
console.log(results[0]); // [1.5, 3.5, 5.5, 7.5]
|
|
139
139
|
```
|
|
140
140
|
|
|
141
|
-
####
|
|
141
|
+
#### Using Struct Data
|
|
142
142
|
|
|
143
143
|
```javascript
|
|
144
|
-
//
|
|
144
|
+
// Define struct template
|
|
145
145
|
const structTemplate = {
|
|
146
146
|
particles: {
|
|
147
147
|
layout: [
|
|
@@ -183,72 +183,72 @@ const data = {
|
|
|
183
183
|
const bindGroup = gpuComputed.createBindGroup(data);
|
|
184
184
|
const results = await gpuComputed.computed(bindGroup, [1], ['output']);
|
|
185
185
|
|
|
186
|
-
console.log(results[0]); //
|
|
186
|
+
console.log(results[0]); // Mapped data
|
|
187
187
|
```
|
|
188
188
|
|
|
189
|
-
####
|
|
189
|
+
#### Data Mapping
|
|
190
190
|
|
|
191
|
-
|
|
191
|
+
When using structs, you can use the `dataMap` method to map results back to the original structure:
|
|
192
192
|
|
|
193
193
|
```javascript
|
|
194
194
|
const mappedData = gpuComputed.dataMap(results[0], 'output');
|
|
195
|
-
console.log(mappedData); //
|
|
195
|
+
console.log(mappedData); // Returns structured object array
|
|
196
196
|
```
|
|
197
197
|
|
|
198
|
-
## API
|
|
198
|
+
## API Reference
|
|
199
199
|
|
|
200
|
-
### GpuComputed
|
|
200
|
+
### GpuComputed Class
|
|
201
201
|
|
|
202
|
-
####
|
|
202
|
+
#### Static Methods
|
|
203
203
|
|
|
204
204
|
##### `GpuComputed.init()`
|
|
205
205
|
|
|
206
|
-
|
|
206
|
+
Initializes the WebGPU environment. Must be called before using other features.
|
|
207
207
|
|
|
208
|
-
|
|
208
|
+
**Returns**: `Promise<void>`
|
|
209
209
|
|
|
210
|
-
|
|
210
|
+
**Throws**: If the browser does not support WebGPU or fails to obtain adapter/device
|
|
211
211
|
|
|
212
212
|
##### `GpuComputed.computed(options)`
|
|
213
213
|
|
|
214
|
-
|
|
214
|
+
Executes a GPU computation task.
|
|
215
215
|
|
|
216
|
-
|
|
216
|
+
**Parameters**:
|
|
217
217
|
|
|
218
|
-
- `code` (string): WGSL
|
|
219
|
-
- `data` (object):
|
|
220
|
-
- `workgroupCount` (array):
|
|
221
|
-
- `workgroupSize` (array,
|
|
222
|
-
- `globalInvocationIdName` (string,
|
|
223
|
-
- `workgroupIndexName` (string,
|
|
224
|
-
- `synchronize` (array,
|
|
225
|
-
- `beforeCodes` (array,
|
|
226
|
-
- `onSuccess` (function,
|
|
218
|
+
- `code` (string): WGSL computation code
|
|
219
|
+
- `data` (object): Input/output data object
|
|
220
|
+
- `workgroupCount` (array): Number of workgroups [x, y?, z?]
|
|
221
|
+
- `workgroupSize` (array, optional): Workgroup size, default [32, 1, 1]
|
|
222
|
+
- `globalInvocationIdName` (string, optional): Global invocation ID variable name, default "grid"
|
|
223
|
+
- `workgroupIndexName` (string, optional): Workgroup index variable name, default "index"
|
|
224
|
+
- `synchronize` (array, optional): Array of buffer names to synchronize back to CPU
|
|
225
|
+
- `beforeCodes` (array, optional): WGSL code snippets before the computation function
|
|
226
|
+
- `onSuccess` (function, optional): Success callback function
|
|
227
227
|
|
|
228
|
-
|
|
228
|
+
**Returns**: `Promise<Array<Float32Array>>` - Data from synchronized buffers
|
|
229
229
|
|
|
230
|
-
###
|
|
230
|
+
### Data Types
|
|
231
231
|
|
|
232
|
-
|
|
232
|
+
Supports the following WGSL types:
|
|
233
233
|
|
|
234
|
-
- `f32`:
|
|
235
|
-
- `vec2`: 2D
|
|
236
|
-
- `vec3`: 3D
|
|
237
|
-
- `vec4`: 4D
|
|
238
|
-
- `mat3x3`: 3x3
|
|
239
|
-
- `mat4x4`: 4x4
|
|
234
|
+
- `f32`: Single-precision float
|
|
235
|
+
- `vec2`: 2D vector
|
|
236
|
+
- `vec3`: 3D vector
|
|
237
|
+
- `vec4`: 4D vector
|
|
238
|
+
- `mat3x3`: 3x3 matrix
|
|
239
|
+
- `mat4x4`: 4x4 matrix
|
|
240
240
|
|
|
241
|
-
###
|
|
241
|
+
### Built-in WGSL Functions
|
|
242
242
|
|
|
243
|
-
|
|
243
|
+
The library provides some commonly used WGSL helper functions:
|
|
244
244
|
|
|
245
|
-
####
|
|
245
|
+
#### Quaternion Rotation
|
|
246
246
|
|
|
247
247
|
```wgsl
|
|
248
248
|
fn quat_rotate(q: vec4<f32>, v: vec3<f32>) -> vec3<f32>
|
|
249
249
|
```
|
|
250
250
|
|
|
251
|
-
|
|
251
|
+
Usage example:
|
|
252
252
|
|
|
253
253
|
```javascript
|
|
254
254
|
import { WGSL_Fun } from 'webgpu-computed';
|
|
@@ -260,64 +260,64 @@ await GpuComputed.computed({
|
|
|
260
260
|
})
|
|
261
261
|
```
|
|
262
262
|
|
|
263
|
-
####
|
|
263
|
+
#### Point in OBB Detection
|
|
264
264
|
|
|
265
265
|
```wgsl
|
|
266
266
|
fn point_in_obb(point: vec3<f32>, center: vec3<f32>, halfSize: vec3<f32>, quat: vec4<f32>) -> bool
|
|
267
267
|
```
|
|
268
268
|
|
|
269
|
-
##
|
|
269
|
+
## Advanced Usage
|
|
270
270
|
|
|
271
|
-
###
|
|
271
|
+
### Custom Workgroup Configuration
|
|
272
272
|
|
|
273
273
|
```javascript
|
|
274
274
|
await GpuComputed.computed({
|
|
275
275
|
code: '...',
|
|
276
276
|
data: {...},
|
|
277
|
-
workgroupCount: [4, 4], // 16
|
|
278
|
-
workgroupSize: [16, 16], //
|
|
277
|
+
workgroupCount: [4, 4], // 16 workgroups
|
|
278
|
+
workgroupSize: [16, 16], // 256 threads per workgroup
|
|
279
279
|
});
|
|
280
280
|
```
|
|
281
281
|
|
|
282
|
-
###
|
|
282
|
+
### Synchronizing Data Back to CPU
|
|
283
283
|
|
|
284
284
|
```javascript
|
|
285
285
|
const results = await GpuComputed.computed({
|
|
286
286
|
code: '...',
|
|
287
287
|
data: {...},
|
|
288
|
-
synchronize: ['output'], //
|
|
288
|
+
synchronize: ['output'], // Specify buffers to synchronize
|
|
289
289
|
workgroupCount: [1]
|
|
290
290
|
});
|
|
291
291
|
|
|
292
|
-
// results
|
|
292
|
+
// results contains synchronized data
|
|
293
293
|
```
|
|
294
294
|
|
|
295
|
-
###
|
|
295
|
+
### Callback Function
|
|
296
296
|
|
|
297
297
|
```javascript
|
|
298
298
|
await GpuComputed.computed({
|
|
299
299
|
code: '...',
|
|
300
300
|
data: {...},
|
|
301
301
|
workgroupCount: [1],
|
|
302
|
-
onSuccess: ({
|
|
303
|
-
console.log('
|
|
302
|
+
onSuccess: ({ gpuComputed, group, results }) => {
|
|
303
|
+
console.log('Computation completed', results);
|
|
304
304
|
}
|
|
305
305
|
});
|
|
306
306
|
```
|
|
307
307
|
|
|
308
|
-
##
|
|
308
|
+
## Example Project
|
|
309
309
|
|
|
310
310
|
```js
|
|
311
311
|
import { GpuComputed } from "webgpu-computed"
|
|
312
312
|
import * as WGSL_Fun from "webgpu-computed"
|
|
313
313
|
|
|
314
|
-
// 1.
|
|
315
|
-
console.log('
|
|
314
|
+
// 1. Initialize WebGPU
|
|
315
|
+
console.log('Initializing WebGPU...');
|
|
316
316
|
await GpuComputed.init();
|
|
317
|
-
console.log('WebGPU
|
|
317
|
+
console.log('WebGPU initialized successfully');
|
|
318
318
|
|
|
319
|
-
// 2.
|
|
320
|
-
console.log('\n===
|
|
319
|
+
// 2. Simple array computation example
|
|
320
|
+
console.log('\n=== Simple Array Computation ===');
|
|
321
321
|
const simpleData = {
|
|
322
322
|
inputA: [1.0, 2.0, 3.0, 4.0],
|
|
323
323
|
inputB: [0.5, 1.5, 2.5, 3.5],
|
|
@@ -335,10 +335,10 @@ const simpleResults = await GpuComputed.computed({
|
|
|
335
335
|
synchronize: ['output']
|
|
336
336
|
});
|
|
337
337
|
|
|
338
|
-
console.log('
|
|
338
|
+
console.log('Simple computation result:', simpleResults[0]); // [1.5, 3.5, 5.5, 7.5]
|
|
339
339
|
|
|
340
|
-
// 3.
|
|
341
|
-
console.log('\n===
|
|
340
|
+
// 3. Complex data structure example (struct)
|
|
341
|
+
console.log('\n=== Complex Data Structure Computation ===');
|
|
342
342
|
const complexData = {
|
|
343
343
|
particles: [
|
|
344
344
|
{ position: [1.0, 2.0, 3.0], velocity: [0.1, 0.2, 0.3], mass: 1.0 },
|
|
@@ -363,10 +363,10 @@ const complexResults = await GpuComputed.computed({
|
|
|
363
363
|
synchronize: ['output']
|
|
364
364
|
});
|
|
365
365
|
|
|
366
|
-
console.log('
|
|
366
|
+
console.log('Complex computation result:', complexResults[0]);
|
|
367
367
|
|
|
368
|
-
// 4.
|
|
369
|
-
console.log('\n===
|
|
368
|
+
// 4. Using built-in WGSL functions example
|
|
369
|
+
console.log('\n=== Using Built-in WGSL Functions ===');
|
|
370
370
|
const wgslFunData = {
|
|
371
371
|
points: [
|
|
372
372
|
{
|
|
@@ -381,7 +381,7 @@ const wgslFunData = {
|
|
|
381
381
|
],
|
|
382
382
|
obbCenter: [0.0, 0.0, 0.0],
|
|
383
383
|
obbHalfSize: [2.0, 2.0, 2.0],
|
|
384
|
-
obbRotation: [0.0, 0.0, 0.0, 1.0], //
|
|
384
|
+
obbRotation: [0.0, 0.0, 0.0, 1.0], // Unit quaternion, no rotation
|
|
385
385
|
results: new Array(3).fill(0)
|
|
386
386
|
};
|
|
387
387
|
|
|
@@ -402,14 +402,14 @@ const wgslFunResults = await GpuComputed.computed({
|
|
|
402
402
|
code: wgslFunCode,
|
|
403
403
|
data: wgslFunData,
|
|
404
404
|
workgroupCount: [1],
|
|
405
|
-
beforeCodes: [WGSL_Fun.quat_rotate, WGSL_Fun.point_in_obb, /**
|
|
405
|
+
beforeCodes: [WGSL_Fun.quat_rotate, WGSL_Fun.point_in_obb, /** Add your own function code */],
|
|
406
406
|
synchronize: ['results']
|
|
407
407
|
});
|
|
408
408
|
|
|
409
|
-
console.log('OBB
|
|
409
|
+
console.log('OBB detection result:', wgslFunResults[0]); // [1, 1, 1] All points are inside the OBB
|
|
410
410
|
|
|
411
|
-
// 5.
|
|
412
|
-
console.log('\n===
|
|
411
|
+
// 5. Custom workgroup configuration example
|
|
412
|
+
console.log('\n=== Custom Workgroup Configuration ===');
|
|
413
413
|
const largeData = {
|
|
414
414
|
largeArray: new Array(1024).fill(0).map((_, i) => i * 1.0),
|
|
415
415
|
output: new Array(1024).fill(0)
|
|
@@ -422,15 +422,15 @@ const largeCode = `
|
|
|
422
422
|
const largeResults = await GpuComputed.computed({
|
|
423
423
|
code: largeCode,
|
|
424
424
|
data: largeData,
|
|
425
|
-
workgroupCount: [32], // 32
|
|
426
|
-
workgroupSize: [32, 1, 1], //
|
|
425
|
+
workgroupCount: [32], // 32 workgroups
|
|
426
|
+
workgroupSize: [32, 1, 1], // 32 threads per workgroup, total 1024 threads
|
|
427
427
|
synchronize: ['output']
|
|
428
428
|
});
|
|
429
429
|
|
|
430
|
-
console.log('
|
|
430
|
+
console.log('Large array computation result (first 10):', largeResults[0].slice(0, 10));
|
|
431
431
|
|
|
432
|
-
// 6.
|
|
433
|
-
console.log('\n===
|
|
432
|
+
// 6. Using callback function example
|
|
433
|
+
console.log('\n=== Using Callback Function ===');
|
|
434
434
|
const callbackData = {
|
|
435
435
|
values: [10.0, 20.0, 30.0],
|
|
436
436
|
squares: new Array(3).fill(0)
|
|
@@ -445,13 +445,13 @@ await GpuComputed.computed({
|
|
|
445
445
|
data: callbackData,
|
|
446
446
|
workgroupCount: [1],
|
|
447
447
|
synchronize: ['squares'],
|
|
448
|
-
onSuccess: ({
|
|
449
|
-
console.log('
|
|
448
|
+
onSuccess: ({ gpuComputed, group, results }) => {
|
|
449
|
+
console.log('Callback triggered, square computation result:', results[0]); // [100, 400, 900]
|
|
450
450
|
}
|
|
451
451
|
});
|
|
452
452
|
|
|
453
|
-
// 7.
|
|
454
|
-
console.log('\n===
|
|
453
|
+
// 7. Multi-dimensional workgroup example
|
|
454
|
+
console.log('\n=== Multi-dimensional Workgroup ===');
|
|
455
455
|
const matrixData = {
|
|
456
456
|
matrixA: new Array(16).fill(0).map((_, i) => i * 1.0),
|
|
457
457
|
matrixB: new Array(16).fill(0).map((_, i) => (i + 1) * 1.0),
|
|
@@ -468,30 +468,29 @@ const matrixCode = `
|
|
|
468
468
|
const matrixResults = await GpuComputed.computed({
|
|
469
469
|
code: matrixCode,
|
|
470
470
|
data: matrixData,
|
|
471
|
-
workgroupCount: [4, 4], // 4x4
|
|
472
|
-
workgroupSize: [1, 1, 1], //
|
|
471
|
+
workgroupCount: [4, 4], // 4x4 workgroup grid
|
|
472
|
+
workgroupSize: [1, 1, 1], // 1 thread per workgroup
|
|
473
473
|
synchronize: ['result']
|
|
474
474
|
});
|
|
475
475
|
|
|
476
|
-
console.log('
|
|
476
|
+
console.log('Matrix computation result:', matrixResults[0]);
|
|
477
477
|
|
|
478
|
-
console.log('\
|
|
478
|
+
console.log('\nAll feature examples completed!');
|
|
479
479
|
```
|
|
480
480
|
|
|
481
|
-
##
|
|
481
|
+
## Browser Support
|
|
482
482
|
|
|
483
483
|
- Chrome 113+
|
|
484
484
|
- Edge 113+
|
|
485
|
-
- Firefox (
|
|
486
|
-
- Safari (
|
|
485
|
+
- Firefox (partial support)
|
|
486
|
+
- Safari (partial support)
|
|
487
487
|
|
|
488
|
-
|
|
488
|
+
Ensure the browser supports the WebGPU API.
|
|
489
489
|
|
|
490
|
-
##
|
|
490
|
+
## Contributing
|
|
491
491
|
|
|
492
|
-
|
|
492
|
+
Welcome to submit Issues and Pull Requests!
|
|
493
493
|
|
|
494
|
-
##
|
|
495
|
-
|
|
496
|
-
ISC License
|
|
494
|
+
## License
|
|
497
495
|
|
|
496
|
+
ISC License
|
package/package.json
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webgpu-computed",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"description": "对webgpu的封装,处理了繁琐的前置工作,只关注wgsl本身逻辑",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
8
|
},
|
|
9
|
-
"keywords": [
|
|
9
|
+
"keywords": [
|
|
10
|
+
"webgpu",
|
|
11
|
+
"gpu",
|
|
12
|
+
"compute",
|
|
13
|
+
"wgsl",
|
|
14
|
+
"typescript",
|
|
15
|
+
"javascript"
|
|
16
|
+
],
|
|
10
17
|
"dependencies": {
|
|
11
18
|
"webgpu": "^0.3.8"
|
|
12
19
|
},
|
package/src/index.js
CHANGED
|
@@ -204,7 +204,7 @@ ${s.join("")}
|
|
|
204
204
|
if (!(n in e)) throw new Error(`传入的数据中,不存在${n}字段`);
|
|
205
205
|
const i = a[n], u = e[n];
|
|
206
206
|
let l = [];
|
|
207
|
-
w(i) ? l = r(u, i) : $(i) ? l = o(u, i) : Array.isArray(u) && l
|
|
207
|
+
w(i) ? l = r(u, i) : $(i) ? l = o(u, i) : Array.isArray(u) && (l = u);
|
|
208
208
|
const y = new Float32Array(l), p = t.createBuffer({
|
|
209
209
|
size: y.byteLength,
|
|
210
210
|
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.COPY_SRC | GPUBufferUsage.STORAGE
|
package/README.en.md
DELETED
|
@@ -1,496 +0,0 @@
|
|
|
1
|
-
# webgpu-computed
|
|
2
|
-
|
|
3
|
-
🌐 Other language versions:
|
|
4
|
-
- [简体中文](./README.md)
|
|
5
|
-
|
|
6
|
-
A simplified WebGPU computing library that encapsulates tedious initialization and buffer management, allowing developers to focus on WGSL shader logic.
|
|
7
|
-
|
|
8
|
-
## Features
|
|
9
|
-
|
|
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 English documentation and examples
|
|
18
|
-
|
|
19
|
-
## Installation
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
npm install webgpu-computed
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
## Quick Start
|
|
26
|
-
|
|
27
|
-
### 1. Initialize WebGPU
|
|
28
|
-
|
|
29
|
-
Before using any computing features, you need to initialize the WebGPU environment:
|
|
30
|
-
|
|
31
|
-
```javascript
|
|
32
|
-
import { GpuComputed } from 'webgpu-computed';
|
|
33
|
-
|
|
34
|
-
// Initialize WebGPU
|
|
35
|
-
await GpuComputed.init();
|
|
36
|
-
|
|
37
|
-
// After using in Node.js environment, please call:
|
|
38
|
-
// GpuComputed.destroy()
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
### 2. Perform Simple Computation
|
|
42
|
-
|
|
43
|
-
Here is a simple vector addition example:
|
|
44
|
-
|
|
45
|
-
```javascript
|
|
46
|
-
import { GpuComputed } from 'webgpu-computed';
|
|
47
|
-
|
|
48
|
-
// Prepare data
|
|
49
|
-
const data = {
|
|
50
|
-
inputA: [1.0, 2.0, 3.0, 4.0],
|
|
51
|
-
inputB: [0.5, 1.5, 2.5, 3.5],
|
|
52
|
-
output: new Array(4).fill(0) // Output buffer
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
// WGSL computation code
|
|
56
|
-
const code = `
|
|
57
|
-
output[index] = inputA[index] + inputB[index];
|
|
58
|
-
`;
|
|
59
|
-
|
|
60
|
-
// Execute computation
|
|
61
|
-
GpuComputed.computed({
|
|
62
|
-
code,
|
|
63
|
-
data,
|
|
64
|
-
synchronize: ["output"], // Fields to return
|
|
65
|
-
workgroupCount: [1] // Number of workgroups
|
|
66
|
-
}).then(results => {
|
|
67
|
-
console.log(results); // [[1.5, 3.5, 5.5, 7.5]]
|
|
68
|
-
})
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
### 3. Using Complex Data Structures
|
|
72
|
-
|
|
73
|
-
The library supports vector and matrix types:
|
|
74
|
-
|
|
75
|
-
```javascript
|
|
76
|
-
const data = {
|
|
77
|
-
positions: [
|
|
78
|
-
{ pos: [1.0, 2.0, 3.0], vel: [0.1, 0.2, 0.3] },
|
|
79
|
-
{ pos: [4.0, 5.0, 6.0], vel: [0.4, 0.5, 0.6] }
|
|
80
|
-
],
|
|
81
|
-
output: new Array(2).fill({ pos: [0,0,0], vel: [0,0,0] })
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const code = `
|
|
85
|
-
output[index].pos = positions[index].pos + positions[index].vel;
|
|
86
|
-
output[index].vel = positions[index].vel * 2.0;
|
|
87
|
-
`;
|
|
88
|
-
|
|
89
|
-
// Execute computation
|
|
90
|
-
GpuComputed.computed({
|
|
91
|
-
code,
|
|
92
|
-
data,
|
|
93
|
-
synchronize: ["output"], // Fields to return
|
|
94
|
-
workgroupCount: [1] // Number of workgroups
|
|
95
|
-
}).then(results => {
|
|
96
|
-
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]]
|
|
97
|
-
})
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
### 4. Manually Create GpuComputed Instance
|
|
101
|
-
|
|
102
|
-
If you need more fine-grained control, you can directly create a GpuComputed instance:
|
|
103
|
-
|
|
104
|
-
```javascript
|
|
105
|
-
import { GpuComputed } from 'webgpu-computed';
|
|
106
|
-
|
|
107
|
-
// 1. Define data template
|
|
108
|
-
const template = {
|
|
109
|
-
inputA: [] as number[],
|
|
110
|
-
inputB: [] as number[],
|
|
111
|
-
output: [] as number[]
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
// 2. Create instance
|
|
115
|
-
const gpuComputed = new GpuComputed(template, {
|
|
116
|
-
code: `
|
|
117
|
-
output[index] = inputA[index] + inputB[index];
|
|
118
|
-
`,
|
|
119
|
-
workgroupSize: [32, 1, 1] // Optional: custom workgroup size
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
// 3. Initialize pipeline
|
|
123
|
-
await gpuComputed.initPipeline();
|
|
124
|
-
|
|
125
|
-
// 4. Prepare data
|
|
126
|
-
const data = {
|
|
127
|
-
inputA: [1.0, 2.0, 3.0, 4.0],
|
|
128
|
-
inputB: [0.5, 1.5, 2.5, 3.5],
|
|
129
|
-
output: new Array(4).fill(0)
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
// 5. Create bind group
|
|
133
|
-
const bindGroup = gpuComputed.createBindGroup(data);
|
|
134
|
-
|
|
135
|
-
// 6. Execute computation
|
|
136
|
-
const results = await gpuComputed.computed(bindGroup, [1], ['output']);
|
|
137
|
-
|
|
138
|
-
console.log(results[0]); // [1.5, 3.5, 5.5, 7.5]
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
#### Using Struct Data
|
|
142
|
-
|
|
143
|
-
```javascript
|
|
144
|
-
// Define struct template
|
|
145
|
-
const structTemplate = {
|
|
146
|
-
particles: {
|
|
147
|
-
layout: [
|
|
148
|
-
{ name: 'position', type: 'vec3' },
|
|
149
|
-
{ name: 'velocity', type: 'vec3' },
|
|
150
|
-
{ name: 'mass', type: 'f32' }
|
|
151
|
-
]
|
|
152
|
-
},
|
|
153
|
-
output: {
|
|
154
|
-
layout: [
|
|
155
|
-
{ name: 'position', type: 'vec3' },
|
|
156
|
-
{ name: 'velocity', type: 'vec3' },
|
|
157
|
-
{ name: 'mass', type: 'f32' }
|
|
158
|
-
]
|
|
159
|
-
}
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
const gpuComputed = new GpuComputed(structTemplate, {
|
|
163
|
-
code: `
|
|
164
|
-
output[index].position = particles[index].position + particles[index].velocity;
|
|
165
|
-
output[index].velocity = particles[index].velocity * 2.0;
|
|
166
|
-
output[index].mass = particles[index].mass * 1.5;
|
|
167
|
-
`
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
await gpuComputed.initPipeline();
|
|
171
|
-
|
|
172
|
-
const data = {
|
|
173
|
-
particles: [
|
|
174
|
-
{ position: [1, 2, 3], velocity: [0.1, 0.2, 0.3], mass: 1.0 },
|
|
175
|
-
{ position: [4, 5, 6], velocity: [0.4, 0.5, 0.6], mass: 2.0 }
|
|
176
|
-
],
|
|
177
|
-
output: [
|
|
178
|
-
{ position: [0, 0, 0], velocity: [0, 0, 0], mass: 0 },
|
|
179
|
-
{ position: [0, 0, 0], velocity: [0, 0, 0], mass: 0 }
|
|
180
|
-
]
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
const bindGroup = gpuComputed.createBindGroup(data);
|
|
184
|
-
const results = await gpuComputed.computed(bindGroup, [1], ['output']);
|
|
185
|
-
|
|
186
|
-
console.log(results[0]); // Mapped data
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
#### Data Mapping
|
|
190
|
-
|
|
191
|
-
When using structs, you can use the `dataMap` method to map results back to the original structure:
|
|
192
|
-
|
|
193
|
-
```javascript
|
|
194
|
-
const mappedData = gpuComputed.dataMap(results[0], 'output');
|
|
195
|
-
console.log(mappedData); // Returns structured object array
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
## API Reference
|
|
199
|
-
|
|
200
|
-
### GpuComputed Class
|
|
201
|
-
|
|
202
|
-
#### Static Methods
|
|
203
|
-
|
|
204
|
-
##### `GpuComputed.init()`
|
|
205
|
-
|
|
206
|
-
Initializes the WebGPU environment. Must be called before using other features.
|
|
207
|
-
|
|
208
|
-
**Returns**: `Promise<void>`
|
|
209
|
-
|
|
210
|
-
**Throws**: If the browser does not support WebGPU or fails to obtain adapter/device
|
|
211
|
-
|
|
212
|
-
##### `GpuComputed.computed(options)`
|
|
213
|
-
|
|
214
|
-
Executes a GPU computation task.
|
|
215
|
-
|
|
216
|
-
**Parameters**:
|
|
217
|
-
|
|
218
|
-
- `code` (string): WGSL computation code
|
|
219
|
-
- `data` (object): Input/output data object
|
|
220
|
-
- `workgroupCount` (array): Number of workgroups [x, y?, z?]
|
|
221
|
-
- `workgroupSize` (array, optional): Workgroup size, default [32, 1, 1]
|
|
222
|
-
- `globalInvocationIdName` (string, optional): Global invocation ID variable name, default "grid"
|
|
223
|
-
- `workgroupIndexName` (string, optional): Workgroup index variable name, default "index"
|
|
224
|
-
- `synchronize` (array, optional): Array of buffer names to synchronize back to CPU
|
|
225
|
-
- `beforeCodes` (array, optional): WGSL code snippets before the computation function
|
|
226
|
-
- `onSuccess` (function, optional): Success callback function
|
|
227
|
-
|
|
228
|
-
**Returns**: `Promise<Array<Float32Array>>` - Data from synchronized buffers
|
|
229
|
-
|
|
230
|
-
### Data Types
|
|
231
|
-
|
|
232
|
-
Supports the following WGSL types:
|
|
233
|
-
|
|
234
|
-
- `f32`: Single-precision float
|
|
235
|
-
- `vec2`: 2D vector
|
|
236
|
-
- `vec3`: 3D vector
|
|
237
|
-
- `vec4`: 4D vector
|
|
238
|
-
- `mat3x3`: 3x3 matrix
|
|
239
|
-
- `mat4x4`: 4x4 matrix
|
|
240
|
-
|
|
241
|
-
### Built-in WGSL Functions
|
|
242
|
-
|
|
243
|
-
The library provides some commonly used WGSL helper functions:
|
|
244
|
-
|
|
245
|
-
#### Quaternion Rotation
|
|
246
|
-
|
|
247
|
-
```wgsl
|
|
248
|
-
fn quat_rotate(q: vec4<f32>, v: vec3<f32>) -> vec3<f32>
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
Usage example:
|
|
252
|
-
|
|
253
|
-
```javascript
|
|
254
|
-
import { WGSL_Fun } from 'webgpu-computed';
|
|
255
|
-
|
|
256
|
-
await GpuComputed.computed({
|
|
257
|
-
code: "",
|
|
258
|
-
data: {....},
|
|
259
|
-
beforeCodes:[WGSL_Fun.quat_rotate]
|
|
260
|
-
})
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
#### Point in OBB Detection
|
|
264
|
-
|
|
265
|
-
```wgsl
|
|
266
|
-
fn point_in_obb(point: vec3<f32>, center: vec3<f32>, halfSize: vec3<f32>, quat: vec4<f32>) -> bool
|
|
267
|
-
```
|
|
268
|
-
|
|
269
|
-
## Advanced Usage
|
|
270
|
-
|
|
271
|
-
### Custom Workgroup Configuration
|
|
272
|
-
|
|
273
|
-
```javascript
|
|
274
|
-
await GpuComputed.computed({
|
|
275
|
-
code: '...',
|
|
276
|
-
data: {...},
|
|
277
|
-
workgroupCount: [4, 4], // 16 workgroups
|
|
278
|
-
workgroupSize: [16, 16], // 256 threads per workgroup
|
|
279
|
-
});
|
|
280
|
-
```
|
|
281
|
-
|
|
282
|
-
### Synchronizing Data Back to CPU
|
|
283
|
-
|
|
284
|
-
```javascript
|
|
285
|
-
const results = await GpuComputed.computed({
|
|
286
|
-
code: '...',
|
|
287
|
-
data: {...},
|
|
288
|
-
synchronize: ['output'], // Specify buffers to synchronize
|
|
289
|
-
workgroupCount: [1]
|
|
290
|
-
});
|
|
291
|
-
|
|
292
|
-
// results contains synchronized data
|
|
293
|
-
```
|
|
294
|
-
|
|
295
|
-
### Callback Function
|
|
296
|
-
|
|
297
|
-
```javascript
|
|
298
|
-
await GpuComputed.computed({
|
|
299
|
-
code: '...',
|
|
300
|
-
data: {...},
|
|
301
|
-
workgroupCount: [1],
|
|
302
|
-
onSuccess: ({ gpuComputed, group, results }) => {
|
|
303
|
-
console.log('Computation completed', results);
|
|
304
|
-
}
|
|
305
|
-
});
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
## Example Project
|
|
309
|
-
|
|
310
|
-
```js
|
|
311
|
-
import { GpuComputed } from "webgpu-computed"
|
|
312
|
-
import * as WGSL_Fun from "webgpu-computed"
|
|
313
|
-
|
|
314
|
-
// 1. Initialize WebGPU
|
|
315
|
-
console.log('Initializing WebGPU...');
|
|
316
|
-
await GpuComputed.init();
|
|
317
|
-
console.log('WebGPU initialized successfully');
|
|
318
|
-
|
|
319
|
-
// 2. Simple array computation example
|
|
320
|
-
console.log('\n=== Simple Array Computation ===');
|
|
321
|
-
const simpleData = {
|
|
322
|
-
inputA: [1.0, 2.0, 3.0, 4.0],
|
|
323
|
-
inputB: [0.5, 1.5, 2.5, 3.5],
|
|
324
|
-
output: new Array(4).fill(0)
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
const simpleCode = `
|
|
328
|
-
output[index] = inputA[index] + inputB[index];
|
|
329
|
-
`;
|
|
330
|
-
|
|
331
|
-
const simpleResults = await GpuComputed.computed({
|
|
332
|
-
code: simpleCode,
|
|
333
|
-
data: simpleData,
|
|
334
|
-
workgroupCount: [1],
|
|
335
|
-
synchronize: ['output']
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
console.log('Simple computation result:', simpleResults[0]); // [1.5, 3.5, 5.5, 7.5]
|
|
339
|
-
|
|
340
|
-
// 3. Complex data structure example (struct)
|
|
341
|
-
console.log('\n=== Complex Data Structure Computation ===');
|
|
342
|
-
const complexData = {
|
|
343
|
-
particles: [
|
|
344
|
-
{ position: [1.0, 2.0, 3.0], velocity: [0.1, 0.2, 0.3], mass: 1.0 },
|
|
345
|
-
{ position: [4.0, 5.0, 6.0], velocity: [0.4, 0.5, 0.6], mass: 2.0 }
|
|
346
|
-
],
|
|
347
|
-
output: [
|
|
348
|
-
{ position: [0, 0, 0], velocity: [0, 0, 0], mass: 0 },
|
|
349
|
-
{ position: [0, 0, 0], velocity: [0, 0, 0], mass: 0 }
|
|
350
|
-
]
|
|
351
|
-
};
|
|
352
|
-
|
|
353
|
-
const complexCode = `
|
|
354
|
-
output[index].position = particles[index].position + particles[index].velocity;
|
|
355
|
-
output[index].velocity = particles[index].velocity * 2.0;
|
|
356
|
-
output[index].mass = particles[index].mass * 1.5;
|
|
357
|
-
`;
|
|
358
|
-
|
|
359
|
-
const complexResults = await GpuComputed.computed({
|
|
360
|
-
code: complexCode,
|
|
361
|
-
data: complexData,
|
|
362
|
-
workgroupCount: [1],
|
|
363
|
-
synchronize: ['output']
|
|
364
|
-
});
|
|
365
|
-
|
|
366
|
-
console.log('Complex computation result:', complexResults[0]);
|
|
367
|
-
|
|
368
|
-
// 4. Using built-in WGSL functions example
|
|
369
|
-
console.log('\n=== Using Built-in WGSL Functions ===');
|
|
370
|
-
const wgslFunData = {
|
|
371
|
-
points: [
|
|
372
|
-
{
|
|
373
|
-
x: 1.0, y: 0.0, z: 0.0
|
|
374
|
-
},
|
|
375
|
-
{
|
|
376
|
-
x: 0.0, y: 1.0, z: 0.0
|
|
377
|
-
},
|
|
378
|
-
{
|
|
379
|
-
x: -1.0, y: 0.0, z: 0.0
|
|
380
|
-
}
|
|
381
|
-
],
|
|
382
|
-
obbCenter: [0.0, 0.0, 0.0],
|
|
383
|
-
obbHalfSize: [2.0, 2.0, 2.0],
|
|
384
|
-
obbRotation: [0.0, 0.0, 0.0, 1.0], // Unit quaternion, no rotation
|
|
385
|
-
results: new Array(3).fill(0)
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
const wgslFunCode = `
|
|
389
|
-
let point = vec3(points[index].x, points[index].y, points[index].z);
|
|
390
|
-
let center = vec3<f32>(obbCenter[0], obbCenter[1], obbCenter[2]);
|
|
391
|
-
let halfSize = vec3<f32>(obbHalfSize[0], obbHalfSize[1], obbHalfSize[2]);
|
|
392
|
-
let quat = vec4<f32>(obbRotation[0], obbRotation[1], obbRotation[2], obbRotation[3]);
|
|
393
|
-
|
|
394
|
-
if (point_in_obb(point, center, halfSize, quat)) {
|
|
395
|
-
results[index] = 1.0;
|
|
396
|
-
} else {
|
|
397
|
-
results[index] = 0.0;
|
|
398
|
-
}
|
|
399
|
-
`;
|
|
400
|
-
|
|
401
|
-
const wgslFunResults = await GpuComputed.computed({
|
|
402
|
-
code: wgslFunCode,
|
|
403
|
-
data: wgslFunData,
|
|
404
|
-
workgroupCount: [1],
|
|
405
|
-
beforeCodes: [WGSL_Fun.quat_rotate, WGSL_Fun.point_in_obb, /** Add your own function code */],
|
|
406
|
-
synchronize: ['results']
|
|
407
|
-
});
|
|
408
|
-
|
|
409
|
-
console.log('OBB detection result:', wgslFunResults[0]); // [1, 1, 1] All points are inside the OBB
|
|
410
|
-
|
|
411
|
-
// 5. Custom workgroup configuration example
|
|
412
|
-
console.log('\n=== Custom Workgroup Configuration ===');
|
|
413
|
-
const largeData = {
|
|
414
|
-
largeArray: new Array(1024).fill(0).map((_, i) => i * 1.0),
|
|
415
|
-
output: new Array(1024).fill(0)
|
|
416
|
-
};
|
|
417
|
-
|
|
418
|
-
const largeCode = `
|
|
419
|
-
output[index] = largeArray[index] * 2.0;
|
|
420
|
-
`;
|
|
421
|
-
|
|
422
|
-
const largeResults = await GpuComputed.computed({
|
|
423
|
-
code: largeCode,
|
|
424
|
-
data: largeData,
|
|
425
|
-
workgroupCount: [32], // 32 workgroups
|
|
426
|
-
workgroupSize: [32, 1, 1], // 32 threads per workgroup, total 1024 threads
|
|
427
|
-
synchronize: ['output']
|
|
428
|
-
});
|
|
429
|
-
|
|
430
|
-
console.log('Large array computation result (first 10):', largeResults[0].slice(0, 10));
|
|
431
|
-
|
|
432
|
-
// 6. Using callback function example
|
|
433
|
-
console.log('\n=== Using Callback Function ===');
|
|
434
|
-
const callbackData = {
|
|
435
|
-
values: [10.0, 20.0, 30.0],
|
|
436
|
-
squares: new Array(3).fill(0)
|
|
437
|
-
};
|
|
438
|
-
|
|
439
|
-
const callbackCode = `
|
|
440
|
-
squares[index] = values[index] * values[index];
|
|
441
|
-
`;
|
|
442
|
-
|
|
443
|
-
await GpuComputed.computed({
|
|
444
|
-
code: callbackCode,
|
|
445
|
-
data: callbackData,
|
|
446
|
-
workgroupCount: [1],
|
|
447
|
-
synchronize: ['squares'],
|
|
448
|
-
onSuccess: ({ gpuComputed, group, results }) => {
|
|
449
|
-
console.log('Callback triggered, square computation result:', results[0]); // [100, 400, 900]
|
|
450
|
-
}
|
|
451
|
-
});
|
|
452
|
-
|
|
453
|
-
// 7. Multi-dimensional workgroup example
|
|
454
|
-
console.log('\n=== Multi-dimensional Workgroup ===');
|
|
455
|
-
const matrixData = {
|
|
456
|
-
matrixA: new Array(16).fill(0).map((_, i) => i * 1.0),
|
|
457
|
-
matrixB: new Array(16).fill(0).map((_, i) => (i + 1) * 1.0),
|
|
458
|
-
result: new Array(16).fill(0)
|
|
459
|
-
};
|
|
460
|
-
|
|
461
|
-
const matrixCode = `
|
|
462
|
-
let x = index % 4u;
|
|
463
|
-
let y = index / 4u;
|
|
464
|
-
let idx = y * 4u + x;
|
|
465
|
-
result[idx] = matrixA[idx] + matrixB[idx];
|
|
466
|
-
`;
|
|
467
|
-
|
|
468
|
-
const matrixResults = await GpuComputed.computed({
|
|
469
|
-
code: matrixCode,
|
|
470
|
-
data: matrixData,
|
|
471
|
-
workgroupCount: [4, 4], // 4x4 workgroup grid
|
|
472
|
-
workgroupSize: [1, 1, 1], // 1 thread per workgroup
|
|
473
|
-
synchronize: ['result']
|
|
474
|
-
});
|
|
475
|
-
|
|
476
|
-
console.log('Matrix computation result:', matrixResults[0]);
|
|
477
|
-
|
|
478
|
-
console.log('\nAll feature examples completed!');
|
|
479
|
-
```
|
|
480
|
-
|
|
481
|
-
## Browser Support
|
|
482
|
-
|
|
483
|
-
- Chrome 113+
|
|
484
|
-
- Edge 113+
|
|
485
|
-
- Firefox (partial support)
|
|
486
|
-
- Safari (partial support)
|
|
487
|
-
|
|
488
|
-
Ensure the browser supports the WebGPU API.
|
|
489
|
-
|
|
490
|
-
## Contributing
|
|
491
|
-
|
|
492
|
-
Welcome to submit Issues and Pull Requests!
|
|
493
|
-
|
|
494
|
-
## License
|
|
495
|
-
|
|
496
|
-
ISC License
|