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/reference/shader-generation.mdx
+60Lines changed: 60 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -130,3 +130,63 @@ Data type inference is the basis for generating signatures for functions just fr
130
130
:::
131
131
132
132
### Origins
133
+
134
+
Origins are enumerable values that describe where a value came from (or didn't come from). Used mainly for:
135
+
- Determining if we're using a value that refers to something else (to create an implicit pointer). This mimics the behavior we
136
+
expect in JS, and doesn't perform unwanted copies on data. Example:
137
+
```ts
138
+
constfoo= () => {
139
+
'use gpu';
140
+
// The type of both expressions is `Boid`, yet one is a
141
+
// reference to an existing value, and the other is a
142
+
// value-type (ephemeral) and would disappear if we didn't
143
+
// assign it to a variable or use it.
144
+
constfirstBoid=layout.$.boids[0];
145
+
constnewBoid=Boid();
146
+
constboidPos=newBoid.pos;
147
+
};
148
+
```
149
+
Generates:
150
+
```wgsl
151
+
fn foo() {
152
+
let firstBoid = (&boids[0]); // typed as ptr<storage, Boid, read_write>
153
+
var newBoid =Boid(); // typed as Boid
154
+
let boidPos = (&newBoid.pos); // typed as ptr<function, vec3f>
155
+
}
156
+
```
157
+
- Detecting illegal uses of our APIs. One example is mutating a value that was passed in as an argument. Since we want the developer to have control over
158
+
passing something as value or as reference (pointer), we have to limit the dev's ability to mutate values that were passed in as arguments if they didn't
159
+
use refs (pointer instances). Otherwise, the generated WGSL won't act as we expect.
160
+
```ts
161
+
constadvance= (pos:d.v3f) => {
162
+
'use gpu';
163
+
pos.x+=1;
164
+
};
165
+
166
+
constmain= () => {
167
+
'use gpu';
168
+
constpos=d.vec3f(0, 0, 0);
169
+
advance(pos);
170
+
// pos.x === 1 in JS
171
+
};
172
+
```
173
+
Generates:
174
+
```wgsl
175
+
fn advance(pos:vec3f) {
176
+
pos.x+=1;
177
+
}
178
+
179
+
fn main() {
180
+
let pos =vec3f(0, 0, 0);
181
+
advance(pos);
182
+
// pos.x === 0 in WGSL
183
+
}
184
+
```
185
+
186
+
There are essentially three types of origins:
187
+
- **Ephemeral Origins**: These origins represent values that are created or derived from other values. They are typically used for creating new instances or
188
+
performing operations that produce new values. Examples include creating a new `Boid` instance or calculating a new position based on an existing one. These
189
+
include `'runtime'` and `'constant'`
190
+
- **Referential Origins**: These origins represent references to existing values. They are typically used for accessing or modifying existing data. Examples
191
+
include accessing the position of an existing `Boid` instance or modifying the position of an existing `Boid` instance. These include `'uniform'`, `'mutable'`, `'readonly'`, `'workgroup'`, `'private'`, `'function'`, `'handle'` and `'constant-ref'`
192
+
- **Argument Origins**: This group is dedicated to exactly one origin: 'argument'. It represents values that are passed as arguments to functions.
0 commit comments