Skip to content

Commit caeb212

Browse files
committed
Merge branch 'main' into feat/layout-value
2 parents 3cab275 + fbd420f commit caeb212

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+2712
-1457
lines changed

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ These functions can reference outside resources, like other user-defined or help
1919
## Creating a function
2020

2121
Functions are constructed by first defining their shells, which specify their inputs and outputs.
22-
Then the actual WGSL implementation is passed in through the `does` method.
22+
Then the actual WGSL implementation is passed in as an argument to a shell invocation.
2323

2424
The following code defines a function that accepts one argument and returns one value.
2525

2626
```ts
2727
const getGradientColor = tgpu['~unstable']
28-
.fn([d.f32], d.vec4f)
29-
.does(`(ratio: f32) -> vec4f {
28+
.fn({ ratio: d.f32 }, d.vec4f)(`{
3029
let color = mix(vec4f(0.769, 0.392, 1.0, 1), d.vec4f(0.114, 0.447, 0.941, 1), ratio);
3130
return color;
3231
}`)
@@ -42,14 +41,14 @@ Externals can be any value or TypeGPU resource that can be resolved to WGSL (fun
4241

4342
```ts
4443
const getBlue = tgpu['~unstable']
45-
.fn([], d.f32)
46-
.does('() -> f32 { return 0.941; }');
44+
.fn({}, d.vec4f)(`{
45+
return vec4f(0.114, 0.447, 0.941, 1);
46+
}`);
4747

4848
const purple = d.vec4f(0.769, 0.392, 1.0, 1);
4949

5050
const getGradientColor = tgpu['~unstable']
51-
.fn([d.f32], d.vec4f)
52-
.does(`(ratio: f32) -> vec4f {
51+
.fn({ ratio: d.f32 }, d.vec4f)(`{
5352
let color = mix(purple, getBlue(), ratio);
5453
return color;
5554
}`)
@@ -60,7 +59,9 @@ const getGradientColor = tgpu['~unstable']
6059
The `getGradientColor` function, when resolved to WGSL, includes the definitions of all used external resources:
6160

6261
```wgsl
63-
fn getBlue_1() -> f32 { return 0.941; }
62+
fn getBlue_1() -> vec4f {
63+
return vec4f(0.114, 0.447, 0.941, 1);
64+
}
6465
6566
fn getGradientColor_0(ratio: f32) -> vec4f {
6667
let color = mix(vec4f(0.769, 0.392, 1, 1), getBlue_1(), ratio);
@@ -82,8 +83,7 @@ const mainVertex = tgpu['~unstable']
8283
.vertexFn({
8384
in: { vertexIndex: d.builtin.vertexIndex },
8485
out: { outPos: d.builtin.position, uv: d.vec2f },
85-
})
86-
.does(/* wgsl */ `(input: VertexInput) -> VertexOutput {
86+
})(/* wgsl */ `{
8787
var pos = array<vec2f, 3>(
8888
vec2(0.0, 0.5),
8989
vec2(-0.5, -0.5),
@@ -96,19 +96,19 @@ const mainVertex = tgpu['~unstable']
9696
vec2(1.0, 0.0),
9797
);
9898
99-
return VertexOutput(vec4f(pos[input.vertexIndex], 0.0, 1.0), uv[input.vertexIndex]);
99+
return Out(vec4f(pos[in.vertexIndex], 0.0, 1.0), uv[in.vertexIndex]);
100100
}`);
101101

102102
const mainFragment = tgpu['~unstable']
103-
.fragmentFn({ in: { uv: d.vec2f }, out: d.vec4f })
104-
.does(/* wgsl */ `(input: FragmentInput) -> @location(0) vec4f {
105-
return getGradientColor((input.uv[0] + input.uv[1]) / 2);
103+
.fragmentFn({ in: { uv: d.vec2f }, out: d.vec4f })(/* wgsl */ `{
104+
return getGradientColor((in.uv[0] + in.uv[1]) / 2);
106105
}`)
107106
.$uses({ getGradientColor });
108107
```
109108

110-
When entry function 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 (see *VertexInput*, *VertexOutput*, *FragmentInput*).
111-
TypeGPU schemas for these structs are created automatically by the library and their definitions are included when resolving the functions. The names referenced in the implementations do not matter.
109+
When entry function 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.
110+
TypeGPU schemas for these structs are created automatically by the library and their definitions are included when resolving the functions.
111+
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.
112112

113113
## Usage in pipelines
114114

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ You can think of slots as holes in the shader that can be filled in from the out
99
```ts
1010
const filterColorSlot = tgpu.slot<vec3f>(); // => TgpuSlot<vec3f>
1111

12-
const filter = tgpu.fn([vec3f]).does((color) => {
12+
const filter = tgpu.fn([vec3f])((color) => {
1313
const filterColor = filterColorSlot.$; // => vec3f
1414

1515
return mul(color, filterColor);
@@ -29,7 +29,7 @@ const filterWithGreen = filter
2929
```ts
3030
const safeLoopSlot = tgpu.slot(false);
3131

32-
const processObjects = tgpu.fn([f32], f32).does(() => {
32+
const processObjects = tgpu.fn([f32], f32)(() => {
3333
let len = objects.length;
3434

3535
if (safeLoopSlot.$) { // <- evaluated at compile time
@@ -49,12 +49,12 @@ const processObjects = tgpu.fn([f32], f32).does(() => {
4949
```ts
5050
// physics.ts
5151

52-
const defaultGravity = tgpu.fn([vec2f], vec2f).does(() => vec2f(0, 9.8));
52+
const defaultGravity = tgpu.fn([vec2f], vec2f)(() => vec2f(0, 9.8));
5353

5454
export const getGravitySlot = tgpu.slot(defaultGravity);
5555
// ^ TgpuSlot<TgpuFn<[Vec2f], Vec2f>>
5656

57-
export const stepPhysics = tgpu.fn([]).does(() => {
57+
export const stepPhysics = tgpu.fn([])(() => {
5858
for (const obj of objects.$) {
5959
// Calling whatever implementation was provided.
6060
const gravity = getGravitySlot.$(obj.position);
@@ -70,15 +70,13 @@ export const stepPhysics = tgpu.fn([]).does(() => {
7070
import { stepPhysics, getGravitySlot } from './physics';
7171

7272
const getGravityTowardsCenter = tgpu
73-
.fn([vec2f], vec2f)
74-
.does((position) => mul(normalize(position), -1));
73+
.fn([vec2f], vec2f)((position) => mul(normalize(position), -1));
7574

7675
const stepPhysicsAltered = stepPhysics
7776
.with(getGravitySlot, getGravityTowardsCenter);
7877

7978
const main = tgpu
80-
.computeFn()
81-
.does(() => {
79+
.computeFn()(() => {
8280
stepPhysicsAltered(); // <- Will use altered gravity.
8381
});
8482
```

apps/typegpu-docs/src/content/docs/reference/naming-convention.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ We give tgpu functions *camelCase* names, without any special suffix or prefix.
2525

2626
```ts
2727
const createBoid = tgpu
28-
.fn([], Boid)
29-
.does(() => ({
28+
.fn([], Boid)(() => ({
3029
pos: vec2f(),
3130
vel: vec2f(0, 1),
3231
}));

apps/typegpu-docs/src/content/docs/tutorials/triangle-to-boids/index.mdx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ const VertexOutput = d.ioStruct({
9292
});
9393

9494
const vertexMain = tgpu
95-
.vertexFn([tgpu.builtin.vertexIndex], VertexOutput)
96-
.does((idx) => {
95+
.vertexFn([tgpu.builtin.vertexIndex], VertexOutput)((idx) => {
9796
const vertices = [
9897
vec2f(0, 0.5),
9998
vec2f(-0.5, -0.5),
@@ -108,8 +107,7 @@ const vertexMain = tgpu
108107
});
109108

110109
const fragmentMain = tgpu
111-
.fragmentFn([], vec4f)
112-
.does(() => {
110+
.fragmentFn([], vec4f)(() => {
113111
// let's go with a simple purple for now
114112
return vec4f(0.7686, 0.3922, 1.0, 1.0);
115113
});
@@ -122,8 +120,7 @@ what arguments it accepts, and what it should return:
122120

123121
```ts
124122
const vertexMain = tgpu
125-
.vertexFn([tgpu.builtin.vertexIndex], VertexOutput)
126-
.does(/* accept parameter of type (p0: number) => { position: vec4f } */);
123+
.vertexFn([tgpu.builtin.vertexIndex], VertexOutput)(/* accept parameter of type (p0: number) => { position: vec4f } */);
127124
```
128125

129126

@@ -1503,3 +1500,4 @@ import step9webgpu from 'code/step9-webgpu.ts?raw';
15031500

15041501
Congratulations! You've successfully implemented the boids flocking algorithm in WebGPU using TypeGPU.
15051502
Along the way, you learned about creating and using a TypeGPU runtime, writing shader code, managing buffers, creating pipelines, and using slots. For more information, refer to the TypeGPU documentation. Thank you for following along and happy coding!
1503+

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export const computeShader = tgpu['~unstable']
99
.computeFn({
1010
in: { gid: d.builtin.globalInvocationId },
1111
workgroupSize: [p.workGroupSize],
12-
})
13-
.does((input) => {
12+
})((input) => {
1413
const fishIndex = input.gid.x;
1514
const fishData = layout.$.currentFishData[fishIndex];
1615
let separation = d.vec3f();
@@ -72,11 +71,11 @@ export const computeShader = tgpu['~unstable']
7271
}
7372

7473
if (layout.$.mouseRay.activated === 1) {
75-
const distanceVector = distanceVectorFromLine(
76-
layout.$.mouseRay.pointX,
77-
layout.$.mouseRay.pointY,
78-
fishData.position,
79-
);
74+
const distanceVector = distanceVectorFromLine({
75+
lineStart: layout.$.mouseRay.pointX,
76+
lineEnd: layout.$.mouseRay.pointY,
77+
point: fishData.position,
78+
});
8079
const limit = p.fishMouseRayRepulsionDistance;
8180
const str =
8281
std.pow(2, std.clamp(limit - std.length(distanceVector), 0, limit)) - 1;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ export const vertexShader = tgpu['~unstable']
1414
.vertexFn({
1515
in: { ...ModelVertexInput, instanceIndex: d.builtin.instanceIndex },
1616
out: ModelVertexOutput,
17-
})
18-
.does((input) => {
17+
})((input) => {
1918
// rotate the model so that it aligns with model's direction of movement
2019
// https://simple.wikipedia.org/wiki/Pitch,_yaw,_and_roll
2120
const currentModelData = layout.$.modelData[input.instanceIndex];
@@ -88,8 +87,10 @@ export const vertexShader = tgpu['~unstable']
8887
.$name('vertexShader');
8988

9089
const sampleTexture = tgpu['~unstable']
91-
.fn([d.vec2f], d.vec4f)
92-
.does(/* wgsl */ `(uv: vec2f) -> vec4f {
90+
.fn(
91+
{ uv: d.vec2f },
92+
d.vec4f,
93+
)(/* wgsl */ `{
9394
return textureSample(layout.$.modelTexture, layout.$.sampler, uv);
9495
}`)
9596
.$uses({ layout })
@@ -99,13 +100,12 @@ export const fragmentShader = tgpu['~unstable']
99100
.fragmentFn({
100101
in: ModelVertexOutput,
101102
out: d.vec4f,
102-
})
103-
.does((input) => {
103+
})((input) => {
104104
// shade the fragment in Phong reflection model
105105
// https://en.wikipedia.org/wiki/Phong_reflection_model
106106
// then apply sea fog and sea desaturation
107107

108-
const textureColorWithAlpha = sampleTexture(input.textureUV); // base color
108+
const textureColorWithAlpha = sampleTexture({ uv: input.textureUV }); // base color
109109
const textureColor = textureColorWithAlpha.xyz;
110110

111111
const ambient = std.mul(0.5, std.mul(textureColor, p.lightColor));

apps/typegpu-docs/src/content/examples/rendering/3d-fish/tgsl-helpers.ts

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ import * as d from 'typegpu/data';
33
import * as std from 'typegpu/std';
44

55
export const distanceVectorFromLine = tgpu['~unstable']
6-
.fn([d.vec3f, d.vec3f, d.vec3f], d.vec3f)
7-
.does((lineStart, lineEnd, point) => {
8-
const lineDirection = std.normalize(std.sub(lineEnd, lineStart));
9-
const pointVector = std.sub(point, lineStart);
6+
.fn(
7+
{ lineStart: d.vec3f, lineEnd: d.vec3f, point: d.vec3f },
8+
d.vec3f,
9+
)((args) => {
10+
const lineDirection = std.normalize(std.sub(args.lineEnd, args.lineStart));
11+
const pointVector = std.sub(args.point, args.lineStart);
1012
const projection = std.dot(pointVector, lineDirection);
11-
const closestPoint = std.add(lineStart, std.mul(projection, lineDirection));
12-
return std.sub(point, closestPoint);
13+
const closestPoint = std.add(
14+
args.lineStart,
15+
std.mul(projection, lineDirection),
16+
);
17+
return std.sub(args.point, closestPoint);
1318
})
1419
.$name('distance vector from line');
1520

@@ -18,34 +23,34 @@ const ApplySinWaveReturnSchema = d.struct({
1823
normal: d.vec3f,
1924
});
2025

21-
export const applySinWave = tgpu['~unstable']
22-
.fn([d.u32, d.u32, d.vec3f, d.vec3f], ApplySinWaveReturnSchema)
23-
.does((index, time, position, normal) => {
24-
const a = 60.1;
25-
const b = 0.8;
26-
const c = 10.1;
27-
// z += sin(index + (time / a + x) / b) / c
26+
export const applySinWave = tgpu['~unstable'].fn(
27+
[d.u32, d.u32, d.vec3f, d.vec3f],
28+
ApplySinWaveReturnSchema,
29+
)((index, time, position, normal) => {
30+
const a = 60.1;
31+
const b = 0.8;
32+
const c = 10.1;
33+
// z += sin(index + (time / a + x) / b) / c
2834

29-
const positionModification = d.vec3f(
30-
0,
31-
0,
32-
std.sin(d.f32(index) + (d.f32(time) / a + position.x) / b) / c,
33-
);
35+
const positionModification = d.vec3f(
36+
0,
37+
0,
38+
std.sin(d.f32(index) + (d.f32(time) / a + position.x) / b) / c,
39+
);
3440

35-
const coeff =
36-
std.cos(d.f32(index) + (d.f32(time) / a + position.x) / b) / c;
37-
const newOX = std.normalize(d.vec3f(1, 0, coeff));
38-
const newOZ = d.vec3f(-newOX.z, 0, newOX.x);
39-
const newNormalXZ = std.add(
40-
std.mul(normal.x, newOX),
41-
std.mul(normal.z, newOZ),
42-
);
41+
const coeff = std.cos(d.f32(index) + (d.f32(time) / a + position.x) / b) / c;
42+
const newOX = std.normalize(d.vec3f(1, 0, coeff));
43+
const newOZ = d.vec3f(-newOX.z, 0, newOX.x);
44+
const newNormalXZ = std.add(
45+
std.mul(normal.x, newOX),
46+
std.mul(normal.z, newOZ),
47+
);
4348

44-
const wavedNormal = d.vec3f(newNormalXZ.x, normal.y, newNormalXZ.z);
45-
const wavedPosition = std.add(position, positionModification);
49+
const wavedNormal = d.vec3f(newNormalXZ.x, normal.y, newNormalXZ.z);
50+
const wavedPosition = std.add(position, positionModification);
4651

47-
return ApplySinWaveReturnSchema({
48-
position: wavedPosition,
49-
normal: wavedNormal,
50-
});
52+
return ApplySinWaveReturnSchema({
53+
position: wavedPosition,
54+
normal: wavedNormal,
5155
});
56+
});

0 commit comments

Comments
 (0)