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
@@ -91,12 +90,27 @@ You can already notice a few things about TypeGPU functions:
91
90
- TS types are properly inferred, feel free to hover over the variables to see their types.
92
91
- The generated code closely matches your source code.
93
92
93
+
:::caution[Using numeric literals]
94
+
Be mindful when using numeric literals. Numbers with nothing after the decimal point (those that pass the `Number.isInteger` test) are inferred as integers, the rest as floats.
95
+
```js
96
+
const foo =1.1; // generates: const foo = 1.1f;
97
+
const bar =1.0; // generates: const bar = 1i;
98
+
const baz =123; // generates: const baz = 123i;
99
+
```
100
+
To ensure that a literal will be coerced to a specific type, you can wrap them in a schema:
To make this all work, we perform a small transformation to functions marked with `'use gpu'`, leaving all other code untouched. Every project's setup is different, and we want to be as non-invasive as possible. The [unplugin-typegpu](/TypeGPU/tooling/unplugin-typegpu) package hooks into existing bundlers and build tools, extracts ASTs from TypeGPU functions and compacts them into our custom format called [tinyest](https://www.npmjs.com/package/tinyest). This metadata is injected into the final JS bundle, then used to efficiently generate equivalent WGSL at runtime.
110
+
To make this all work, we perform a small transformation to functions marked with `'use gpu'`. Every project's setup is different, and we want to be as non-invasive as possible. The [unplugin-typegpu](/TypeGPU/tooling/unplugin-typegpu) package hooks into existing bundlers and build tools, extracts ASTs from TypeGPU functions and compacts them into our custom format called [tinyest](https://www.npmjs.com/package/tinyest). This metadata is injected into the final JS bundle, then used to efficiently generate equivalent WGSL at runtime.
97
111
98
112
:::tip
99
-
If you wish for all WGSL generation to happen at build time, combine [unplugin-macros](https://github.com/unplugin/unplugin-macros) and our [resolve API](/TypeGPU/fundamentals/resolve).
113
+
If all your shader code is predetermined, or you want to precompute a set of variants ahead of time, you can combine [unplugin-macros](https://github.com/unplugin/unplugin-macros) and our [resolve API](/TypeGPU/fundamentals/resolve).
TypeGPU provides a set of standard functions under `typegpu/std`, which you can use in your own TypeGPU functions. Our goal is for all functions to have matching
199
-
behavior on the CPU and GPU, which unlocks many possibilities (shader unit testing, shared business logic, and more...).
200
-
201
-
```ts twoslash
202
-
import*asdfrom'typegpu/data';
203
-
import*asstdfrom'typegpu/std';
204
-
205
-
function manhattanDistance(a:d.v3f, b:d.v3f) {
206
-
'use gpu';
207
-
const dx =std.abs(a.x-b.x);
208
-
const dy =std.abs(a.y-b.y);
209
-
const dz =std.abs(a.z-b.z);
210
-
211
-
returnstd.max(dx, std.max(dy, dz));
212
-
}
213
-
```
214
-
215
210
You can explore the set of standard functions in the [API Reference](/TypeGPU/api/typegpu/std/functions/abs/).
216
211
217
212
### The outer scope
@@ -310,6 +305,27 @@ you have to use supplementary functions from `typegpu/std` (*add, mul, eq, lt, g
310
305
311
306
***Math.\*** --
312
307
Utility functions on the `Math` object can't automatically run on the GPU, but can usually be swapped with functions exported from `typegpu/std`.
308
+
Additionally, if you're able to pull the call to `Math.*` out of the function, you can store the result in a constant and use it in the function
309
+
no problem.
310
+
311
+
### Standard library
312
+
313
+
TypeGPU provides a set of standard functions under `typegpu/std`, which you can use in your own TypeGPU functions. Our goal is for all functions to have matching
314
+
behavior on the CPU and GPU, which unlocks many possibilities (shader unit testing, shared business logic, and more...).
Copy file name to clipboardExpand all lines: apps/typegpu-docs/src/content/docs/getting-started.mdx
+32-10Lines changed: 32 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,23 +27,45 @@ Install TypeGPU using the package manager of your choice.
27
27
</TabItem>
28
28
</Tabs>
29
29
30
-
You can then import it in JavaScript/TypeScript code.
30
+
TypeScript doesn't yet ship WebGPU types by default, so we install them separately:
31
31
32
-
```ts
33
-
// main.ts
32
+
<TabssyncKey="package-manager">
33
+
<TabItemlabel="npm"icon="seti:npm">
34
+
```sh frame=none
35
+
npm install --save-dev @webgpu/types
36
+
```
37
+
</TabItem>
38
+
<TabItemlabel="pnpm"icon="pnpm">
39
+
```sh frame=none
40
+
pnpm add -D @webgpu/types
41
+
```
42
+
</TabItem>
43
+
<TabItemlabel="yarn"icon="seti:yarn">
44
+
```sh frame=none
45
+
yarn add -D @webgpu/types
46
+
```
47
+
</TabItem>
48
+
</Tabs>
34
49
35
-
importtgpufrom'typegpu';
50
+
Then add the following to your `tsconfig.json`:
36
51
37
-
// ...
52
+
```json
53
+
{
54
+
"compilerOptions": {
55
+
"types": ["@webgpu/types"],
56
+
// ...
57
+
},
58
+
// ...
59
+
}
38
60
```
39
61
40
-
## Live Examples
41
-
42
-
Our [live examples](/TypeGPU/examples) showcase many use-cases of TypeGPU. Feel free to check them out! You can also open each of them on StackBlitz in order to edit the code and see the preview update live.
62
+
:::note
63
+
TypeGPU features related to writing shaders in JavaScript/TypeScript rely on an [additional build tool that hooks into existing bundlers called unplugin-typegpu](/TypeGPU/tooling/unplugin-typegpu). Make sure to set it up as well if you plan on using these features.
64
+
:::
43
65
44
-
## StackBlitz
66
+
## Live Examples
45
67
46
-
You can play with TypeGPU right in the browser using our [StackBlitz example](https://stackblitz.com/edit/typegpu-example?file=src%2Fmain.ts).
68
+
Our [live examples](/TypeGPU/examples) showcase many use-cases of TypeGPU. Feel free to check them out! You can also open each of them on StackBlitz in order to edit the code and see the preview update live, or to bootstrap your own project.
Besides transpiling JS into AST, the plugin also collects external references, so it is not necessary to pass them to the `$uses` method anymore.
26
+
Besides parsing JS into an AST, the plugin also collects external references, so it is not necessary to pass them to the `$uses` method (as is required when implementing functions in WGSL).
51
27
52
28
-**Automatic naming of tgpu objects**
53
29
@@ -96,7 +72,7 @@ import { defineConfig } from 'vite';
0 commit comments