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
To create a schema corresponding to a pointer data type, wrap the inner schema with a pointer constructor corresponding to the appropriate address space.
377
+
378
+
For address spaces `function`, `private`, `workgroup`, `storage` and `uniform`, use `d.ptrFn`, `d.ptrPrivate`, `d.ptrWorkgroup`, `d.ptrStorage` and `d.ptrUniform` respectively.
379
+
380
+
```ts twoslash
381
+
import*asdfrom'typegpu/data';
382
+
// ---cut---
383
+
const vecPtrFn =d.ptrFn(d.vec3f);
384
+
// ^?
385
+
386
+
const vecPtrPrivate =d.ptrPrivate(d.vec2f);
387
+
// ^?
388
+
```
389
+
390
+
:::note
391
+
In order to use `workgroup`, `storage` and `uniform` pointers as function parameters,
392
+
WGSL needs the `unrestricted_pointer_parameters` extension.
393
+
394
+
This extension is enabled automatically when available.
395
+
:::
396
+
347
397
## Copy constructors
348
398
349
399
One of the biggest differences between JavaScript and WGSL is the existence of value/reference types.
Copy file name to clipboardExpand all lines: apps/typegpu-docs/src/content/docs/fundamentals/tgsl.mdx
+34-11Lines changed: 34 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ For the TGSL functions to work, you need to use the dedicated build plugin -- [u
18
18
## Usage
19
19
20
20
Instead of using a WGSL code string, you can pass TGSL to the tgpu function shell as an argument instead.
21
-
Functions from the WGSL standard library (*distance, arrayLength, workgroupBarrier, [etc.](https://github.com/software-mansion/TypeGPU/blob/release/packages/typegpu/src/std/index.ts)*) are accessible through the `typegpu/std`endpoint.
21
+
Functions from the WGSL standard library (*distance, arrayLength, workgroupBarrier, [etc.](https://github.com/software-mansion/TypeGPU/blob/release/packages/typegpu/src/std/index.ts)*) are accessible through the `typegpu/std`entrypoint.
22
22
The package also includes functions for vector and matrix operators (*add, eq, lt...*).
TGSL-implemented functions can also be invoked on the CPU, as along as they do not use any GPU-exclusive functionalities, like buffers or textures (regardless of whether they are marked as *"kernel"* or not).
106
+
## Executing TGSL functions in JS
107
+
108
+
TGSL-implemented functions can be invoked on the CPU,
109
+
as along as they do not use any GPU-exclusive functionalities,
110
+
like buffers or textures (regardless of whether they are marked as *"kernel"* or not).
111
+
112
+
Keep in mind that you cannot execute entry-point functions in JavaScript.
107
113
108
114
## What to keep in mind
109
115
110
116
***TGSL limitations** --
111
-
For a function to be valid TGSL, it must consist only of supported JS syntax (again, see [tinyest-for-wgsl repository](https://github.com/software-mansion/TypeGPU/blob/release/packages/tinyest-for-wgsl/src/parsers.ts)), possibly including references to bound buffers, constant variables defined outside of the function, other TGSL functions etc.
117
+
For a function to be valid TGSL, it must consist only of supported JS syntax (again, see [tinyest-for-wgsl repository](https://github.com/software-mansion/TypeGPU/blob/release/packages/tinyest-for-wgsl/src/parsers.ts)), possibly including references to bound buffers, constant variables defined outside the function, other TGSL functions etc.
112
118
This means that, for example, `console.log()` calls will not work on the GPU.
113
119
114
120
***Differences between JS on the CPU and GPU** --
115
-
TGSL is developed to work on the GPU the same as on the CPU as much as possible,
121
+
TGSL is developed to work on the GPU the same as on the CPU as much as possible,
116
122
however because of the fundamental differences between the JavaScript and WGSL languages, it is not guaranteed to always be the case.
117
123
118
-
Currently the biggest known difference is that vectors, matrices and structs are treated as reference types in JavaScript and value types in WGSL.
119
-
That is, on the WGSL side, the assignment operator copies the value instead of the reference, and two different vectors can be equal to each other if only they store the same values, unlike in JS, where they need to point to the same reference.
120
-
To somehow alleviate this issue, when passing arguments to tgpu functions on JS side, we perform a deep copy of them (note that in WGSL arguments are immutable by default).
124
+
Currently, the biggest known difference is that vectors, matrices and structs are treated as reference types in JavaScript and value types in WGSL.
125
+
That is, on the WGSL side, the assignment operator copies the value instead of the reference, and two different vectors can be equal to each other if only they store the same values, unlike in JS, where they need to point to the same reference.
126
+
To somehow alleviate this issue, when passing arguments to tgpu functions on JS side, we perform a deep copy of them (note that in WGSL arguments are immutable by default).
121
127
122
128
When using TGSL on the GPU, the behavior is that of WGSL, not JS, as one would expect.
123
129
Therefore some WGSL knowledge is still required, even when opting out for TGSL.
0 commit comments