Skip to content

Commit 4db4b07

Browse files
authored
feat: Move prepareDispatch to root, and enable slot bindings (#1794)
1 parent 24e9b6d commit 4db4b07

File tree

18 files changed

+627
-507
lines changed

18 files changed

+627
-507
lines changed

apps/typegpu-docs/src/content/docs/fundamentals/utils.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ Under the hood, it wraps the callback in a `TgpuFn`, creates a compute pipeline,
1010
Since the pipeline is reused, there’s no additional overhead for subsequent calls.
1111

1212
```ts twoslash
13-
import tgpu, { prepareDispatch } from 'typegpu';
13+
import tgpu from 'typegpu';
1414
import * as d from 'typegpu/data';
1515
const root = await tgpu.init();
1616
// ---cut---
1717
const data = root.createMutable(d.arrayOf(d.u32, 8), [0, 1, 2, 3, 4, 5, 6, 7]);
1818

19-
const doubleUp = prepareDispatch(root, (x) => {
19+
const doubleUp = root['~unstable'].prepareDispatch((x) => {
2020
'use gpu';
2121
data.$[x] *= 2;
2222
});
@@ -39,7 +39,7 @@ Buffer initialization commonly uses random number generators.
3939
For that, you can use the [`@typegpu/noise`](TypeGPU/ecosystem/typegpu-noise) library.
4040

4141
```ts twoslash
42-
import tgpu, { prepareDispatch } from 'typegpu';
42+
import tgpu from 'typegpu';
4343
import * as d from 'typegpu/data';
4444
// ---cut---
4545
import { randf } from '@typegpu/noise';
@@ -51,7 +51,7 @@ const waterLevelMutable = root.createMutable(
5151
d.arrayOf(d.arrayOf(d.f32, 512), 1024),
5252
);
5353

54-
prepareDispatch(root, (x, y) => {
54+
root['~unstable'].prepareDispatch((x, y) => {
5555
'use gpu';
5656
randf.seed2(d.vec2f(x, y).div(1024));
5757
waterLevelMutable.$[x][y] = 10 + randf.sample();
@@ -65,7 +65,7 @@ console.log(await waterLevelMutable.read());
6565
The result of `prepareDispatch` can have bind groups bound using the `with` method.
6666

6767
```ts twoslash
68-
import tgpu, { prepareDispatch } from 'typegpu';
68+
import tgpu from 'typegpu';
6969
import * as d from 'typegpu/data';
7070
import * as std from 'typegpu/std';
7171
const root = await tgpu.init();
@@ -84,7 +84,7 @@ const bindGroup2 = root.createBindGroup(layout, {
8484
buffer: buffer2,
8585
});
8686

87-
const test = prepareDispatch(root, (x) => {
87+
const test = root['~unstable'].prepareDispatch((x) => {
8888
'use gpu';
8989
layout.$.buffer[x] *= 2;
9090
});
@@ -123,13 +123,13 @@ Yes, you read that correctly, TypeGPU implements logging to the console on the G
123123
Just call `console.log` like you would in plain JavaScript, and open the console to see the results.
124124

125125
```ts twoslash
126-
import tgpu, { prepareDispatch } from 'typegpu';
126+
import tgpu from 'typegpu';
127127
import * as d from 'typegpu/data';
128128

129129
const root = await tgpu.init();
130130
// ---cut---
131131
const callCountMutable = root.createMutable(d.u32, 0);
132-
const compute = prepareDispatch(root, () => {
132+
const compute = root['~unstable'].prepareDispatch(() => {
133133
'use gpu';
134134
callCountMutable.$ += 1;
135135
console.log('Call number', callCountMutable.$);
@@ -151,7 +151,7 @@ The buffer is of fixed size, which may limit the total amount of information tha
151151
If that's an issue, you may specify the size manually when creating the `root` object.
152152

153153
```ts twoslash
154-
import tgpu, { prepareDispatch } from 'typegpu';
154+
import tgpu from 'typegpu';
155155
import * as d from 'typegpu/data';
156156

157157
const presentationFormat = undefined as any;

apps/typegpu-docs/src/examples/rendering/3d-fish/compute.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import tgpu from 'typegpu';
21
import * as d from 'typegpu/data';
32
import * as std from 'typegpu/std';
43
import * as p from './params.ts';
54
import { computeBindGroupLayout as layout, ModelData } from './schemas.ts';
65
import { projectPointOnLine } from './tgsl-helpers.ts';
76

8-
export const computeShader = tgpu['~unstable'].computeFn({
9-
in: { gid: d.builtin.globalInvocationId },
10-
workgroupSize: [p.workGroupSize],
11-
})((input) => {
12-
const fishIndex = input.gid.x;
7+
export const simulate = (fishIndex: number) => {
8+
'use gpu';
139
// TODO: replace it with struct copy when Chromium is fixed
1410
const fishData = ModelData({
1511
position: layout.$.currentFishData[fishIndex].position,
@@ -131,4 +127,4 @@ export const computeShader = tgpu['~unstable'].computeFn({
131127
);
132128
fishData.position = std.add(fishData.position, translation);
133129
layout.$.nextFishData[fishIndex] = fishData;
134-
});
130+
};

apps/typegpu-docs/src/examples/rendering/3d-fish/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { randf } from '@typegpu/noise';
2-
import tgpu, { prepareDispatch } from 'typegpu';
2+
import tgpu from 'typegpu';
33
import * as d from 'typegpu/data';
44
import * as std from 'typegpu/std';
55
import * as m from 'wgpu-matrix';
6-
import { computeShader } from './compute.ts';
6+
import { simulate } from './compute.ts';
77
import { loadModel } from './load-model.ts';
88
import * as p from './params.ts';
99
import { fragmentShader, vertexShader } from './render.ts';
@@ -98,7 +98,7 @@ function enqueuePresetChanges() {
9898
const buffer0mutable = fishDataBuffers[0].as('mutable');
9999
const buffer1mutable = fishDataBuffers[1].as('mutable');
100100
const seedUniform = root.createUniform(d.f32);
101-
const randomizeFishPositionsOnGPU = prepareDispatch(root, (x) => {
101+
const randomizeFishPositionsOnGPU = root['~unstable'].prepareDispatch((x) => {
102102
'use gpu';
103103
randf.seed2(d.vec2f(d.f32(x), seedUniform.$));
104104
const data = ModelData({
@@ -198,9 +198,7 @@ let depthTexture = root.device.createTexture({
198198
usage: GPUTextureUsage.RENDER_ATTACHMENT,
199199
});
200200

201-
const computePipeline = root['~unstable']
202-
.withCompute(computeShader)
203-
.createPipeline();
201+
const simulateAction = root['~unstable'].prepareDispatch(simulate);
204202

205203
// bind groups
206204

@@ -256,9 +254,9 @@ function frame(timestamp: DOMHighResTimeStamp) {
256254
lastTimestamp = timestamp;
257255
cameraBuffer.write(camera);
258256

259-
computePipeline
257+
simulateAction
260258
.with(computeBindGroups[odd ? 1 : 0])
261-
.dispatchWorkgroups(p.fishAmount / p.workGroupSize);
259+
.dispatch(p.fishAmount);
262260

263261
renderPipeline
264262
.withColorAttachment({

apps/typegpu-docs/src/examples/rendering/3d-fish/params.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as d from 'typegpu/data';
22
import * as std from 'typegpu/std';
33

4-
export const workGroupSize = 256;
5-
64
export const fishAmount = 1024 * 8;
75
export const fishModelScale = 0.07;
86

apps/typegpu-docs/src/examples/simple/increment/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import tgpu, { prepareDispatch } from 'typegpu';
1+
import tgpu from 'typegpu';
22
import * as d from 'typegpu/data';
33

44
const root = await tgpu.init();
55
// Allocating memory for the counter
66
const counter = root.createMutable(d.u32);
77

88
// A 0-dimentional shader function
9-
const gpuIncrement = prepareDispatch(root, () => {
9+
const gpuIncrement = root['~unstable'].prepareDispatch(() => {
1010
'use gpu';
1111
counter.$ += 1;
1212
});

apps/typegpu-docs/src/examples/simulation/boids-next/index.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,8 @@ const computeBindGroupLayout = tgpu.bindGroupLayout({
177177

178178
const { currentTrianglePos, nextTrianglePos } = computeBindGroupLayout.bound;
179179

180-
const mainCompute = tgpu['~unstable'].computeFn({
181-
in: { gid: d.builtin.globalInvocationId },
182-
workgroupSize: [1],
183-
})((input) => {
184-
const index = input.gid.x;
180+
const simulate = (index: number) => {
181+
'use gpu';
185182
const instanceInfo = currentTrianglePos.value[index];
186183
let separation = d.vec2f();
187184
let alignment = d.vec2f();
@@ -250,11 +247,9 @@ const mainCompute = tgpu['~unstable'].computeFn({
250247
instanceInfo.position = std.add(instanceInfo.position, instanceInfo.velocity);
251248

252249
nextTrianglePos.value[index] = instanceInfo;
253-
});
250+
};
254251

255-
const computePipeline = root['~unstable']
256-
.withCompute(mainCompute)
257-
.createPipeline();
252+
const simulateAction = root['~unstable'].prepareDispatch(simulate);
258253

259254
const computeBindGroups = [0, 1].map((idx) =>
260255
root.createBindGroup(computeBindGroupLayout, {
@@ -273,9 +268,9 @@ function frame() {
273268

274269
even = !even;
275270

276-
computePipeline
271+
simulateAction
277272
.with(computeBindGroups[even ? 0 : 1])
278-
.dispatchWorkgroups(triangleAmount);
273+
.dispatch(triangleAmount);
279274

280275
renderPipeline
281276
.withColorAttachment({

0 commit comments

Comments
 (0)