Skip to content

Commit 0aa8c5c

Browse files
authored
docs: Update tgpu.fn API (#1247)
1 parent 7a84f3c commit 0aa8c5c

File tree

2 files changed

+22
-29
lines changed

2 files changed

+22
-29
lines changed

apps/typegpu-docs/src/content/docs/fundamentals/functions/index.mdx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,18 @@ The following code defines a function that accepts one argument and returns one
2525

2626
```ts
2727
const getGradientColor = tgpu['~unstable']
28-
.fn(
29-
{ ratio: d.f32 },
30-
d.vec4f,
31-
)(`{
28+
.fn([d.f32], d.vec4f)(/* wgsl */ `(ratio: f32) -> vec4f {
3229
let color = mix(vec4f(0.769, 0.392, 1.0, 1), vec4f(0.114, 0.447, 0.941, 1), ratio);
3330
return color;
34-
}`)
35-
.$name('getGradientColor');
31+
}`);
3632

3733
// or
3834

39-
const getGradientColor = tgpu['~unstable'].fn({ ratio: d.f32 }, d.vec4f)`{
40-
let color = mix(vec4f(0.769, 0.392, 1.0, 1), vec4f(0.114, 0.447, 0.941, 1), ratio);
41-
return color;
42-
}`.$name('getGradientColor');
35+
const getGradientColor = tgpu['~unstable']
36+
.fn([d.f32], d.vec4f) /* wgsl */`(ratio: f32) -> vec4f {
37+
let color = mix(vec4f(0.769, 0.392, 1.0, 1), vec4f(0.114, 0.447, 0.941, 1), ratio);
38+
return color;
39+
};
4340
```
4441

4542
If you're using Visual Studio Code, you can use an [extension](https://marketplace.visualstudio.com/items?itemName=ggsimm.wgsl-literal) that brings syntax highlighting to the code fragments marked with `/* wgsl */` comments.
@@ -50,18 +47,18 @@ Functions can use external resources passed inside a record via the `$uses` meth
5047
Externals can be any value or TypeGPU resource that can be resolved to WGSL (functions, buffer usages, slots, accessors, constants, variables, declarations, vectors, matrices, textures, samplers etc.).
5148

5249
```ts
53-
const getBlue = tgpu['~unstable'].fn({}, d.vec4f)`{
50+
const getBlue = tgpu['~unstable'].fn([], d.vec4f)`() -> vec4f {
5451
return vec4f(0.114, 0.447, 0.941, 1);
5552
}`;
5653
5754
const purple = d.vec4f(0.769, 0.392, 1.0, 1);
5855
59-
const getGradientColor = tgpu['~unstable'].fn({ ratio: d.f32 }, d.vec4f)`{
60-
let color = mix(purple, getBlue(), ratio);
61-
return color;
62-
}`
63-
.$uses({ purple, getBlue })
64-
.$name('getGradientColor');
56+
const getGradientColor = tgpu['~unstable']
57+
.fn([d.f32], d.vec4f)`(ratio: f32) -> vec4f {
58+
let color = mix(purple, getBlue(), ratio);
59+
return color;
60+
}
61+
`.$uses({ purple, getBlue });
6562
```
6663

6764
The `getGradientColor` function, when resolved to WGSL, includes the definitions of all used external resources:

apps/typegpu-docs/src/content/docs/tooling/unplugin-typegpu.mdx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,37 @@ The package includes the following functionalities:
2323
import tgpu from 'typegpu';
2424
import * as d from 'typegpu/data';
2525

26-
const add = tgpu['~unstable'].fn(
27-
{ a: d.u32, b: d.u32 },
28-
d.u32,
29-
)(({ a, b }) => a + b);
26+
const add = tgpu['~unstable'].fn([d.u32, d.u32], d.u32)(
27+
(a, b) => a + b,
28+
);
3029
```
3130

3231
However, if the implementation function, or the shell, is referenced via a variable, the plugin will not recognize it as TGSL,
3332
thus to make it work, the function needs to be marked with a `"kernel"` directive.
3433

3534
```ts
36-
const addFn = tgpu['~unstable'].fn({ a: d.u32, b: d.u32 }, d.u32);
35+
const addFn = tgpu['~unstable'].fn([d.u32, d.u32], d.u32);
3736

38-
const add = addFn(({ a, b }) => {
37+
const add = addFn((a, b) => {
3938
'kernel';
4039
return a + b;
4140
});
4241
```
4342

4443
```ts
45-
const addImpl = ({ a, b }) => {
44+
const addImpl = (a, b) => {
4645
'kernel';
4746
return a + b;
4847
};
4948

50-
const add = tgpu['~unstable'].fn({ a: d.u32, b: d.u32 }, d.u32)(addImpl);
49+
const add = tgpu['~unstable'].fn([d.u32, d.u32], d.u32)(addImpl);
5150
```
5251

5352
After transpiling the function, the JS implementation is removed from the bundle in order to save space.
5453
To be able to invoke the function both on GPU and CPU, it needs to be marked with `"kernel & js"` directive;
5554

5655
```ts
57-
const add = tgpu['~unstable'].fn(
58-
{ a: d.u32, b: d.u32 },
59-
d.u32,
60-
)(({ a, b }) => {
56+
const add = tgpu['~unstable'].fn([d.u32, d.u32], d.u32)((a, b) => {
6157
'kernel & js';
6258
return a + b;
6359
});

0 commit comments

Comments
 (0)