You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TypeGPU allows writing shaders by composing typed functions, which are special wrappers around WGSL code.
17
-
These functions can reference outside resources, like other user-defined or helper functions, buffers, bind group layouts etc.
12
+
TypeGPU functions let you define shader logic in a modular and type-safe way.
13
+
Their signatures are fully visible to TypeScript, enabling tooling and static checks.
14
+
Dependencies, including GPU resources or other functions, are resolved automatically, with no duplication or name clashes.
15
+
This also supports distributing shader logic across multiple modules or packages.
16
+
Imported functions from external sources are automatically resolved and embedded into the final shader when referenced.
18
17
19
-
## Creating a function
18
+
## Defining a function
20
19
21
-
Functions are constructed by first defining their shells, which specify their inputs and outputs.
22
-
Then the actual WGSL implementation is passed in as an argument to a shell invocation. If the code string is a template literal, you can omit the parentheses, which may result in a more compact Biome/Prettier formatting.
20
+
In order to construct a TypeGPU function, you need to start by defining its shell, an object holding only the input and output types.
21
+
The shell constructor `tgpu.fn` relies on [TypeGPU schemas](/TypeGPU/fundamentals/data-schemas), objects that represent WGSL data types and assist in generating shader code at runtime.
22
+
It accepts two arguments:
23
23
24
-
The following code defines a function that accepts one argument and returns one value.
24
+
- An array of schemas representing argument types,
25
+
- (Optionally) a schema representing the return type.
let color = mix(vec4f(0.769, 0.392, 1.0, 1), vec4f(0.114, 0.447, 0.941, 1), ratio);
36
-
return color;
37
-
};
35
+
const getGradientColor =tgpu.fn(
36
+
[d.f32],
37
+
d.vec4f
38
+
) /* wgsl */`(ratio: f32) -> vec4f {
39
+
var purple = vec4f(0.769, 0.392, 1.0, 1);
40
+
var blue = vec4f(0.114, 0.447, 0.941, 1);
41
+
return mix(purple, blue, ratio);
42
+
}`;
38
43
```
39
44
40
-
Ifyou'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.
45
+
:::tip
46
+
If you're using Visual Studio Code, you can use [this extension](https://marketplace.visualstudio.com/items?itemName=ggsimm.wgsl-literal) that brings syntax highlighting to the code fragments marked with `/* wgsl */` comments.
47
+
:::
48
+
49
+
Since type information is already present in the shell, the WGSL header can be simplified to include only the argument names.
Functions can use external resources passed via the `$uses` method.
66
+
Externals can include anything that can be resolved to WGSL by TypeGPU (numbers, vectors, matrices, constants, TypeGPU functions, buffer usages, textures, samplers, slots, accessors etc.).
46
67
47
-
```ts
48
-
const getBlue = tgpu.fn([], d.vec4f)`() ->vec4f {
68
+
```ts twoslash
69
+
importtgpufrom'typegpu';
70
+
import*asdfrom'typegpu/data';
71
+
72
+
// ---cut---
73
+
const getBlueFunction =tgpu.fn([], d.vec4f)`() {
49
74
return vec4f(0.114, 0.447, 0.941, 1);
50
75
}`;
51
76
52
-
const purple = d.vec4f(0.769, 0.392, 1.0, 1);
77
+
// calling a schema to create a value on the JS side
The`getGradientColor`function, when resolved to WGSL, includes the definitions of all used external resources:
86
+
You can check yourself what `getGradientColor`resolves to by calling [`tgpu.resolve`](/TypeGPU/fundamentals/resolve), all relevant definitions will be automatically included:
62
87
63
88
```wgsl
64
-
fn getBlue_1() -> vec4f {
89
+
// results of calling tgpu.resolve({ externals: { getGradientColor } })
90
+
fn getBlueFunction_1() -> vec4f{
65
91
return vec4f(0.114, 0.447, 0.941, 1);
66
92
}
67
93
68
-
fn getGradientColor_0(ratio: f32) -> vec4f {
69
-
let color = mix(vec4f(0.769, 0.392, 1, 1), getBlue_1(), ratio);
Whenentryfunction inputs or outputs are specified as objects containing builtins and inter-stage variables, the WGSL implementations need to access these arguments as passed in via structs.
112
-
TypeGPU schemas for these structs are created automatically by the library and their definitions are included when resolving the functions.
113
-
Input values are accessible through the `in` keyword, while the automatically created structs for input and output shall be referenced in implementation as `In` and `Out` respectively.
241
+
Resolved WGSL for the pipeline including the two entry point functions above is equivalent (with respect to some cleanup) to the following:
0 commit comments