Skip to content

Commit 48ddfbd

Browse files
authored
feat: Change 'kernel' to 'use gpu' (#1798)
1 parent 27495e8 commit 48ddfbd

File tree

42 files changed

+222
-246
lines changed

Some content is hidden

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

42 files changed

+222
-246
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This feature is under heavy development and is yet to reach stability.
88
:::
99

1010
**TGSL (TypeGPU Shading Language)** is a subset of JavaScript used to define functions that run on the GPU via TypeGPU.
11-
It works by transpiling JavaScript into a compact AST format, called [tinyest](https://www.npmjs.com/package/tinyest),
11+
It works by transpiling JavaScript into a compact AST format, called [tinyest](https://www.npmjs.com/package/tinyest),
1212
which is then used to generate equivalent WGSL.
1313

1414
You can check the current state of supported JavaScript syntax in the [tinyest-for-wgsl repository](https://github.com/software-mansion/TypeGPU/blob/release/packages/tinyest-for-wgsl/src/parsers.ts).
@@ -91,19 +91,19 @@ When using TGSL, you don't need to explicitly declare function externals with th
9191
The [TypeGPU Build Plugin](/TypeGPU/tooling/unplugin-typegpu/) automatically extracts all external definitions from the function's AST.
9292
:::
9393

94-
Sometimes, we are unable to recognize functions that are supposed to be TGSL. For that case, we have a *"kernel"* directive.
94+
Sometimes, we are unable to recognize functions that are supposed to be TGSL. For that case, we have a *"use gpu"* directive.
9595

9696
```ts
9797
const patternFn = tgpu.fn([d.vec2f], d.f32);
9898

9999
const patternCheckers = patternFn((uv) => {
100-
'kernel';
100+
'use gpu';
101101
const suv = floor(mul(20, uv));
102102
return suv.x + suv.y - 2 * floor((suv.x + suv.y) * 0.5);
103103
});
104104

105105
const patternSolid = patternFn(() => {
106-
'kernel';
106+
'use gpu';
107107
return 1;
108108
});
109109
```
@@ -112,7 +112,7 @@ const patternSolid = patternFn(() => {
112112

113113
TGSL-implemented functions can be invoked on the CPU,
114114
as along as they do not use any GPU-exclusive functionalities,
115-
like buffers or textures (regardless of whether they are marked as *"kernel"* or not).
115+
like buffers or textures (regardless of whether they are marked with *"use gpu"* or not).
116116

117117
Keep in mind that you cannot execute entry-point functions in JavaScript.
118118

@@ -149,10 +149,10 @@ const bgColor = root.createUniform(d.vec4f, d.vec4f(0.114, 0.447, 0.941, 1));
149149
const fragmentTgsl = tgpu['~unstable'].fragmentFn({ out: d.vec4f })(() => {
150150
return bgColor.$;
151151
// ^?
152-
});
152+
});
153153

154-
const fragmentWgsl = tgpu['~unstable'].fragmentFn({ out: d.vec4f })`{
155-
return bgColor;
154+
const fragmentWgsl = tgpu['~unstable'].fragmentFn({ out: d.vec4f })`{
155+
return bgColor;
156156
}
157157
`.$uses({ bgColor });
158158
```
@@ -176,12 +176,12 @@ const fn = tgpu.fn([d.ptrFn(d.vec3f)], d.vec3f)((ptr) => {
176176
ptr.x += 1;
177177
//^?
178178
return ptr;
179-
});
179+
});
180180
```
181181

182182
* **When to use TGSL instead of WGSL** --
183183
Writing the code using TGSL has a few significant advantages.
184-
It allows defining utils only once and using them both as a kernel and host functions,
184+
It allows defining utils only once and using them both as GPU and CPU functions,
185185
as well as enables complete syntax highlighting and autocomplete in TypeGPU function definitions, leading to a better developer UX.
186186
However, it sometimes might be better to choose WGSL for certain functions.
187187
Since JavaScript doesn't support operator overloading, functions including complex matrix operations can be more readable in WGSL.

apps/typegpu-docs/src/content/docs/fundamentals/timestamp-queries.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ description: How to use timestamp queries to measure the performance of your pip
77
This feature is under heavy development and is yet to reach stability.
88
:::
99

10-
To measure kernel execution time, you can use **timestamp queries**, which instruct compute or render passes to write timestamps at the start and end of execution. By reading back these timestamps, you can calculate the pipeline’s execution duration in nanoseconds.
10+
To measure shader execution time, you can use **timestamp queries**, which instruct compute or render passes to write timestamps at the start and end of execution. By reading back these timestamps, you can calculate the pipeline’s execution duration in nanoseconds.
1111

1212
:::tip
1313
To use performance callbacks and timestamp queries, you must enable the `timestamp-query` feature on your GPU device. See [Enabling Features](/TypeGPU/fundamentals/enabling-features) for details.
1414
:::
1515

1616
TypeGPU offers two ways to employ timestamp queries:
1717

18-
1. **High-level API** via the pipeline’s `.withPerformanceCallback()` method
18+
1. **High-level API** via the pipeline’s `.withPerformanceCallback()` method
1919
2. **Low-level API** using a `TgpuQuerySet`, which you can attach to a TypeGPU pipeline or a raw WebGPU command encoder
2020

2121
## Performance callbacks

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const root = await tgpu.init();
1717
const data = root.createMutable(d.arrayOf(d.u32, 8), [0, 1, 2, 3, 4, 5, 6, 7]);
1818

1919
const doubleUp = prepareDispatch(root, (x) => {
20-
'kernel';
20+
'use gpu';
2121
data.$[x] *= 2;
2222
});
2323

@@ -30,7 +30,7 @@ console.log(await data.read()); // [0, 8, 16, 24, 16, 20, 24, 28]
3030
```
3131

3232
:::note
33-
Remember to mark the callback with `'kernel'` directive to let TypeGPU know that this function is TGSL.
33+
Remember to mark the callback with the `'use gpu'` directive to let TypeGPU know that this function is TGSL.
3434
:::
3535

3636
The callback can have up to three arguments (dimensions).
@@ -52,7 +52,7 @@ const waterLevelMutable = root.createMutable(
5252
);
5353

5454
prepareDispatch(root, (x, y) => {
55-
'kernel';
55+
'use gpu';
5656
randf.seed2(d.vec2f(x, y).div(1024));
5757
waterLevelMutable.$[x][y] = 10 + randf.sample();
5858
}).dispatch(1024, 512);
@@ -85,7 +85,7 @@ const bindGroup2 = root.createBindGroup(layout, {
8585
});
8686

8787
const test = prepareDispatch(root, (x) => {
88-
'kernel';
88+
'use gpu';
8989
layout.$.buffer[x] *= 2;
9090
});
9191

@@ -130,12 +130,12 @@ const root = await tgpu.init();
130130
// ---cut---
131131
const callCountMutable = root.createMutable(d.u32, 0);
132132
const compute = prepareDispatch(root, () => {
133-
'kernel';
133+
'use gpu';
134134
callCountMutable.$ += 1;
135135
console.log('Call number', callCountMutable.$);
136136
});
137137

138-
compute.dispatch();
138+
compute.dispatch();
139139
compute.dispatch();
140140

141141
// Eventually...

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ The package includes the following functionalities:
2727
```
2828

2929
However, if the implementation function, or the shell, is referenced via a variable, the plugin will not recognize it as TGSL,
30-
thus to make it work, the function needs to be marked with a `"kernel"` directive.
30+
thus to make it work, the function needs to be marked with a `'use gpu'` directive.
3131

3232
```ts
3333
const addFn = tgpu.fn([d.u32, d.u32], d.u32);
3434

3535
const add = addFn((a, b) => {
36-
'kernel';
36+
'use gpu';
3737
return a + b;
3838
});
3939
```
4040

4141
```ts
4242
const addImpl = (a, b) => {
43-
'kernel';
43+
'use gpu';
4444
return a + b;
4545
};
4646

@@ -128,7 +128,7 @@ interface Options {
128128
autoNamingEnabled?: boolean;
129129

130130
/**
131-
* Skipping files that don't contain "typegpu", "tgpu" or "kernel".
131+
* Skipping files that don't contain 'typegpu', 'tgpu' or 'use gpu'.
132132
* In case this early pruning hinders transformation, you
133133
* can disable it.
134134
*

apps/typegpu-docs/src/examples/algorithms/matrix-next/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const state = {
1919
second: [] as number[],
2020
result: [] as number[],
2121
},
22-
kernelTime: 0,
22+
gpuTime: 0,
2323
};
2424

2525
const root = await tgpu.init({
@@ -57,7 +57,7 @@ function createBindGroup() {
5757

5858
function createPipelines() {
5959
const performanceCallback = (start: bigint, end: bigint) => {
60-
state.kernelTime = Number(end - start) / 1_000_000;
60+
state.gpuTime = Number(end - start) / 1_000_000;
6161
};
6262

6363
const optimized = root['~unstable']
@@ -195,11 +195,11 @@ async function compute() {
195195
cpu: 'CPU',
196196
}[state.strategy];
197197

198-
const showKernel = state.strategy !== 'cpu' && hasTimestampQuery;
198+
const showGpu = state.strategy !== 'cpu' && hasTimestampQuery;
199199
updateTimingDisplay(
200200
strategyName,
201201
totalTime,
202-
showKernel ? state.kernelTime : undefined,
202+
showGpu ? state.gpuTime : undefined,
203203
);
204204

205205
printMatrixToHtml(
@@ -227,15 +227,15 @@ generateMatrices();
227227
function updateTimingDisplay(
228228
strategy: string,
229229
totalTime: number,
230-
kernelTime?: number,
230+
gpuTime?: number,
231231
) {
232232
if (!timingDisplay) return;
233233

234-
timingDisplay.innerHTML = kernelTime !== undefined
234+
timingDisplay.innerHTML = gpuTime !== undefined
235235
? `<div>${strategy} computation:</div>
236236
<div style="font-size: 0.9em; margin-top: 0.25rem;">
237-
Total: ${totalTime.toFixed(2)}ms | Kernel: ${
238-
kernelTime >= 0.01 ? kernelTime.toFixed(2) : '<0.01'
237+
Total: ${totalTime.toFixed(2)}ms | GPU: ${
238+
gpuTime >= 0.01 ? gpuTime.toFixed(2) : '<0.01'
239239
}ms
240240
</div>`
241241
: `${strategy} computation: ${totalTime.toFixed(2)}ms`;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const buffer0mutable = fishDataBuffers[0].as('mutable');
9999
const buffer1mutable = fishDataBuffers[1].as('mutable');
100100
const seedUniform = root.createUniform(d.f32);
101101
const randomizeFishPositionsOnGPU = prepareDispatch(root, (x) => {
102-
'kernel';
102+
'use gpu';
103103
randf.seed2(d.vec2f(d.f32(x), seedUniform.$));
104104
const data = ModelData({
105105
position: d.vec3f(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { add, cos, dot, normalize, sin } from 'typegpu/std';
33
import type { Line3 } from './schemas.ts';
44

55
export const projectPointOnLine = (point: d.v3f, line: Line3): d.v3f => {
6-
'kernel';
6+
'use gpu';
77
const pointVector = point.sub(line.origin);
88
const projection = dot(pointVector, line.dir);
99
return line.origin.add(line.dir.mul(projection));
@@ -20,7 +20,7 @@ export const applySinWave = (
2020
vertex: PosAndNormal,
2121
time: number,
2222
) => {
23-
'kernel';
23+
'use gpu';
2424
const a = -60.1;
2525
const b = 0.8;
2626
const c = 6.1;

apps/typegpu-docs/src/examples/rendering/caustics/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ const mainVertex = tgpu['~unstable'].vertexFn({
2121
* location.
2222
*/
2323
const tilePattern = (uv: d.v2f): number => {
24-
'kernel';
24+
'use gpu';
2525
const tiledUv = std.fract(uv);
2626
const proximity = std.abs(std.sub(std.mul(tiledUv, 2), 1));
2727
const maxProximity = std.max(proximity.x, proximity.y);
2828
return std.saturate(std.pow(1 - maxProximity, 0.6) * 5);
2929
};
3030

3131
const caustics = (uv: d.v2f, time: number, profile: d.v3f): d.v3f => {
32-
'kernel';
32+
'use gpu';
3333
const distortion = perlin3d.sample(d.vec3f(std.mul(uv, 0.5), time * 0.2));
3434
// Distorting UV coordinates
3535
const uv2 = std.add(uv, distortion);
@@ -42,7 +42,7 @@ const caustics = (uv: d.v2f, time: number, profile: d.v3f): d.v3f => {
4242
* in the XY plane (around the imaginary Z axis)
4343
*/
4444
const rotateXY = (angle: number): d.m2x2f => {
45-
'kernel';
45+
'use gpu';
4646
return d.mat2x2f(
4747
/* right */ d.vec2f(std.cos(angle), std.sin(angle)),
4848
/* up */ d.vec2f(-std.sin(angle), std.cos(angle)),

apps/typegpu-docs/src/examples/rendering/cubemap-reflection/helpers.ts

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

44
export const unpackVec2u = (packed: d.v2u): d.v4f => {
5-
'kernel';
5+
'use gpu';
66
const xy = std.unpack2x16float(packed.x);
77
const zw = std.unpack2x16float(packed.y);
88
return d.vec4f(xy, zw);
99
};
1010

1111
export const packVec2u = (toPack: d.v4f): d.v2u => {
12-
'kernel';
12+
'use gpu';
1313
const xy = std.pack2x16float(toPack.xy);
1414
const zw = std.pack2x16float(toPack.zw);
1515
return d.vec2u(xy, zw);
1616
};
1717

1818
export const getAverageNormal = (v1: d.v4f, v2: d.v4f, v3: d.v4f): d.v4f => {
19-
'kernel';
19+
'use gpu';
2020
const edge1 = std.sub(v2.xyz, v1.xyz);
2121
const edge2 = std.sub(v3.xyz, v1.xyz);
2222
return std.normalize(d.vec4f(std.cross(edge1, edge2), 0));
2323
};
2424

2525
export const calculateMidpoint = (v1: d.v4f, v2: d.v4f): d.v4f => {
26-
'kernel';
26+
'use gpu';
2727
return d.vec4f(std.mul(0.5, std.add(v1.xyz, v2.xyz)), 1);
2828
};

apps/typegpu-docs/src/examples/rendering/disco/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as d from 'typegpu/data';
22
import * as std from 'typegpu/std';
33

44
export const palette = (t: number): d.v3f => {
5-
'kernel';
5+
'use gpu';
66
const a = d.vec3f(0.50, 0.59, 0.85);
77
const b = d.vec3f(0.18, 0.42, 0.40);
88
const c = d.vec3f(0.18, 0.48, 0.41);

0 commit comments

Comments
 (0)