@antv/g-webgpu 0.7.0 → 0.7.1

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 CHANGED
@@ -3,51 +3,53 @@
3
3
  ```typescript
4
4
  import { World } from '@antv/g-webgpu';
5
5
 
6
- const canvas = document.getElementById('application');
7
-
8
- const world = World.create({
9
- canvas,
6
+ const world = new World({
7
+ engineOptions: {
8
+ supportCompute: true,
9
+ },
10
10
  });
11
11
 
12
- const renderer = world.createRenderer();
13
- const scene = world.createScene();
14
- const boxEntity = world.createEntity();
15
- scene.addEntity(boxEntity);
16
-
17
- const camera = world
18
- .createCamera()
19
- .setPosition(0, 0, 2)
20
- .setPerspective(0.1, 5, 75, canvas.width / canvas.height);
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
- world
42
- .createRenderable(boxEntity)
43
- .setGeometry(boxGeometry)
44
- .setMaterial(material);
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
- // create a render loop
47
- const render = () => {
48
- renderer.render(view);
49
- frameId = window.requestAnimationFrame(render);
50
- };
28
+ 使用 TS 编写 Shader:
51
29
 
52
- render();
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
+ ```