Skip to content

Commit e875215

Browse files
authored
docs: tsover usage (#2209)
1 parent 4a0f67c commit e875215

3 files changed

Lines changed: 37 additions & 14 deletions

File tree

apps/typegpu-docs/src/content/docs/fundamentals/data-schemas.mdx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,15 @@ const firstElement = mat[0]; // 1.1 in JS, but resolves to invalid WGSL!
120120

121121
### Operators
122122

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.
131+
:::
125132

126133
```ts twoslash
127134
import { d, std } from 'typegpu';
@@ -130,6 +137,12 @@ const vec1 = d.vec3f(1, 2, 3);
130137
const vec2 = std.add(vec1, 3); // d.vec3f(4, 5, 6)
131138
// ^?
132139

140+
function foo() {
141+
'use gpu';
142+
const vec1 = d.vec3f(1, 2, 3) * 2; // d.vec3f(2, 4, 6)
143+
// ^?
144+
}
145+
133146
const mat = d.mat3x3f(
134147
2, 0, 0,
135148
0, 4, 0,
@@ -143,14 +156,14 @@ const comparison = std.le(d.vec3f(2, 3, 4), d.vec3f(3)); // d.vec3b(true, true,
143156
// ^?
144157
```
145158

146-
Currently, implemented operators include:
159+
Currently, implemented `std` operations include:
147160

148161
- arithmetic operators `add`, `sub`, `mul`, `div`, `mod`, `neg`,
149162
- comparison operators `eq`, `ne`, `lt`, `le`, `gt`, `ge`,
150163
- logical operators `not`, `and`, `or`.
151164

152-
When operating on scalars, you can still use `+`, `<=`, `!` etc.,
153-
the purpose of these `std` functions is to enable working on vectors and matrices.
165+
Vectors currently overload the binary `+`, `-`, `*`, `/` and `%` operators, while matrices
166+
overload `+`, `-` and `*`.
154167

155168
For arithmetic operators, you can also use the infix notation.
156169

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn main() -> vec2f {
8282
```
8383

8484
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/)).
8686
- TS types are properly inferred, feel free to hover over the variables to see their types.
8787
- The generated code closely matches your source code.
8888

@@ -287,9 +287,9 @@ Our aim with TypeGPU functions is not to allow arbitrary JavaScript to be suppor
287287
:::
288288

289289
* **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), ...*)
293293

294294
* **Calling other functions** --
295295
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.
440440
It allows defining utilities once and using them both on the GPU and CPU,
441441
as well as enables complete syntax highlighting and autocomplete in TypeGPU function definitions, leading to a better developer experience.
442442

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.
448444

449445
## Entry functions
450446

apps/typegpu-docs/src/content/docs/getting-started.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ Then add the following to your `tsconfig.json`:
6363
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.
6464
:::
6565

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+
6680
## Live Examples
6781

6882
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

Comments
 (0)