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
Copy file name to clipboardExpand all lines: apps/typegpu-docs/src/content/docs/fundamentals/data-schemas.mdx
+18-5Lines changed: 18 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -120,8 +120,15 @@ const firstElement = mat[0]; // 1.1 in JS, but resolves to invalid WGSL!
120
120
121
121
### Operators
122
122
123
-
As you may know, it is currently impossible to overload operators in JavaScript.
124
-
For that reason, TypeGPU provides `std` functions imitating those.
123
+
For performing math operations on vectors and matrices, you can either use the standard JavaScript operators like
124
+
`+`, `-`, `*`, `/` and `%` when inside functions marked with `'use gpu'`, use functions provided on the `std` namespace,
125
+
or `.add`, `.sub`, `.mul`, `.div` and `.mod` functions on the values themselves.
126
+
127
+
:::note
128
+
Use of `tsover` is required for operator overloading support, [refer to their documentation on how to set it up in your project](https://software-mansion-labs.github.io/tsover/docs).
129
+
130
+
When operating on scalars, you can still use `+`, `<=`, `!` etc., whether you adopt `tsover` or not.
Copy file name to clipboardExpand all lines: apps/typegpu-docs/src/content/docs/fundamentals/functions/index.mdx
+5-9Lines changed: 5 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,7 +82,7 @@ fn main() -> vec2f {
82
82
```
83
83
84
84
You can already notice a few things about TypeGPU functions:
85
-
- Using operators like `+`, `-`, `*`, `/`, etc. is perfectly valid on numbers.
85
+
- Using operators like `+`, `-`, `*`, `/`, etc. is perfectly valid on numbers (and on vectors/matrices thanks to [`tsover`](https://software-mansion-labs.github.io/tsover/)).
86
86
- TS types are properly inferred, feel free to hover over the variables to see their types.
87
87
- The generated code closely matches your source code.
88
88
@@ -287,9 +287,9 @@ Our aim with TypeGPU functions is not to allow arbitrary JavaScript to be suppor
287
287
:::
288
288
289
289
***Operators** --
290
-
JavaScript does not support operator overloading.
291
-
This means that, while you can still use operators for numbers,
292
-
you have to use supplementary functions from `typegpu/std` (*add, mul, eq, lt, ge...*) for operations involving vectors and matrices, or use a fluent interface (*abc.mul(xyz), ...*)
290
+
We support using regular operators on vectors and matrices with full type inference thanks to [tsover](https://software-mansion-labs.github.io/tsover/).
291
+
Without it, you can still use operators for numbers,
292
+
but for vectors and matrices you have to use supplementary functions from `typegpu/std` (*add, mul, eq, lt, ge...*), or use a fluent interface (*abc.mul(xyz), ...*)
293
293
294
294
***Calling other functions** --
295
295
Only functions marked with `'use gpu'` can be called from within a shader. There are two exceptions to that rule: [`console.log`](/TypeGPU/fundamentals/utils#consolelog), which allows for tracking runtime behavior
@@ -440,11 +440,7 @@ Writing shader code in JavaScript has a few significant advantages.
440
440
It allows defining utilities once and using them both on the GPU and CPU,
441
441
as well as enables complete syntax highlighting and autocomplete in TypeGPU function definitions, leading to a better developer experience.
442
442
443
-
However, there are cases where WGSL might be more suitable.
444
-
Since JavaScript doesn't support operator overloading, functions including complex matrix or vector operations can be more readable in WGSL.
445
-
Writing WGSL becomes a necessity whenever TypeGPU does not yet support some feature or standard library function yet.
446
-
447
-
Luckily, you don't have to choose one or the other for the entire project. It is possible to mix and match WGSL and JavaScript at every step of the way, so you're not locked into one or the other.
443
+
However, there are cases where WGSL might be more suitable. Writing WGSL becomes a necessity whenever TypeGPU does not yet support some feature or standard library function yet. Luckily, you don't have to choose one or the other for the entire project. It is possible to mix and match WGSL and JavaScript at every step of the way, so you're not locked into one or the other.
Copy file name to clipboardExpand all lines: apps/typegpu-docs/src/content/docs/getting-started.mdx
+14Lines changed: 14 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,6 +63,20 @@ Then add the following to your `tsconfig.json`:
63
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
64
:::
65
65
66
+
:::tip
67
+
For overloaded operator support in JavaScript/TypeScript shaders, [setup `tsover` in your project](https://software-mansion-labs.github.io/tsover/docs).
68
+
69
+
```ts twoslash
70
+
import { d } from'typegpu';
71
+
// ---cut-before---
72
+
function foo() {
73
+
'use gpu';
74
+
const value =d.vec3f() *20-0.5;
75
+
// ^?
76
+
}
77
+
```
78
+
:::
79
+
66
80
## Live Examples
67
81
68
82
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.
0 commit comments