@antv/g-webgpu 0.7.0 → 0.7.2
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 +44 -42
- package/dist/gwebgpu.js +1 -1
- package/es/Kernel.d.ts +21 -0
- package/es/Kernel.js.map +1 -1
- package/es/World.d.ts +13 -0
- package/es/World.js +2 -2
- package/es/World.js.map +1 -1
- package/es/index.d.ts +3 -0
- package/es/utils/canvas.d.ts +1 -0
- package/es/utils/typedarray.d.ts +1 -0
- package/lib/Kernel.js.map +1 -1
- package/lib/World.js +1 -1
- package/lib/World.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -3,51 +3,53 @@
|
|
|
3
3
|
```typescript
|
|
4
4
|
import { World } from '@antv/g-webgpu';
|
|
5
5
|
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
const world = new World({
|
|
7
|
+
engineOptions: {
|
|
8
|
+
supportCompute: true,
|
|
9
|
+
},
|
|
10
10
|
});
|
|
11
11
|
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const view = world
|
|
23
|
-
.createView()
|
|
24
|
-
.setCamera(camera)
|
|
25
|
-
.setScene(scene)
|
|
26
|
-
.setViewport({
|
|
27
|
-
x: 0,
|
|
28
|
-
y: 0,
|
|
29
|
-
width: canvas.width,
|
|
30
|
-
height: canvas.height,
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
// create geometry, material and attach them to mesh
|
|
34
|
-
const boxGeometry = world.createGeometry(Geometry.BOX, {
|
|
35
|
-
halfExtents: [0.5, 0.5, 0.5],
|
|
36
|
-
});
|
|
37
|
-
const material = world.createMaterial(Material.BASIC).setUniform({
|
|
38
|
-
color: [1, 0, 0, 1],
|
|
12
|
+
const compute = world.createComputePipeline({
|
|
13
|
+
shader: `
|
|
14
|
+
//...
|
|
15
|
+
`, // 下一节的 Shader 文本
|
|
16
|
+
dispatch: [1, 1, 1],
|
|
17
|
+
onCompleted: (result) => {
|
|
18
|
+
console.log(result); // [2, 4, 6, 8, 10, 12, 14, 16]
|
|
19
|
+
world.destroy(); // 计算完成后销毁相关 GPU 资源
|
|
20
|
+
},
|
|
39
21
|
});
|
|
40
22
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
23
|
+
// 绑定输入到 Compute Shader 中的两个参数
|
|
24
|
+
world.setBinding(compute, 'vectorA', [1, 2, 3, 4, 5, 6, 7, 8]);
|
|
25
|
+
world.setBinding(compute, 'vectorB', [1, 2, 3, 4, 5, 6, 7, 8]);
|
|
26
|
+
```
|
|
45
27
|
|
|
46
|
-
|
|
47
|
-
const render = () => {
|
|
48
|
-
renderer.render(view);
|
|
49
|
-
frameId = window.requestAnimationFrame(render);
|
|
50
|
-
};
|
|
28
|
+
使用 TS 编写 Shader:
|
|
51
29
|
|
|
52
|
-
|
|
53
|
-
|
|
30
|
+
```typescript
|
|
31
|
+
import { globalInvocationID } from 'g-webgpu';
|
|
32
|
+
|
|
33
|
+
@numthreads(8, 1, 1)
|
|
34
|
+
class Add2Vectors {
|
|
35
|
+
@in @out
|
|
36
|
+
vectorA: float[];
|
|
37
|
+
|
|
38
|
+
@in
|
|
39
|
+
vectorB: float[];
|
|
40
|
+
|
|
41
|
+
sum(a: float, b: float): float {
|
|
42
|
+
return a + b;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@main
|
|
46
|
+
compute() {
|
|
47
|
+
// 获取当前线程处理的数据
|
|
48
|
+
const a = this.vectorA[globalInvocationID.x];
|
|
49
|
+
const b = this.vectorB[globalInvocationID.x];
|
|
50
|
+
|
|
51
|
+
// 输出当前线程处理完毕的数据,即两个向量相加后的结果
|
|
52
|
+
this.vectorA[globalInvocationID.x] = this.sum(a, b);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
```
|