Skip to content

Commit e994076

Browse files
committed
Merge branch 'main' into feat/tgpu-three
2 parents bdee185 + 55bbb41 commit e994076

File tree

100 files changed

+5825
-1396
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+5825
-1396
lines changed

apps/typegpu-docs/src/components/translator/lib/rolldown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { InputOptions, OutputOptions } from '@rolldown/browser';
22
import { join } from 'pathe';
33

44
export interface BundleResult {
5-
output: Record<string, string | Uint8Array>;
5+
output: Record<string, string | Uint8Array<ArrayBuffer>>;
66
warnings?: string[] | undefined;
77
}
88

@@ -88,7 +88,7 @@ export async function bundle(
8888
result.output.map((chunk) =>
8989
chunk.type === 'chunk'
9090
? [chunk.fileName, chunk.code]
91-
: [chunk.fileName, chunk.source]
91+
: [chunk.fileName, chunk.source.slice()]
9292
),
9393
);
9494

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ function manhattanDistance(a: d.v3f, b: d.v3f) {
323323
const dy = std.abs(a.y - b.y);
324324
const dz = std.abs(a.z - b.z);
325325

326-
return std.max(dx, std.max(dy, dz));
326+
return std.max(dx, dy, dz);
327327
}
328328
```
329329

apps/typegpu-docs/src/content/docs/fundamentals/utils.mdx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,76 @@ tgpu.resolve([innerPipeline]);
128128
```
129129
:::
130130

131+
## *tgpu.comptime*
132+
133+
`tgpu['~unstable'].comptime(func)` creates a version of `func` that instead of being transpiled to WGSL, will be called during the WGSL code generation.
134+
This can be used to precompute and inject a value into the final shader code.
135+
136+
```ts twoslash
137+
import tgpu from 'typegpu';
138+
import * as d from 'typegpu/data';
139+
140+
const hsvToRgb = tgpu.fn([d.f32, d.f32, d.f32], d.vec3f)``;
141+
142+
// ---cut---
143+
const injectRand01 = tgpu['~unstable']
144+
.comptime(() => Math.random());
145+
146+
const getColor = tgpu.fn([d.vec3f])((diffuse) => {
147+
'use gpu';
148+
const albedo = hsvToRgb(injectRand01(), 1, 0.5);
149+
return albedo.mul(diffuse);
150+
});
151+
```
152+
153+
Note how the function passed into `comptime` doesn't have to be marked with
154+
`'use gpu'` and can use `Math`. That's because the function doesn't execute on the GPU, it gets
155+
executed before the shader code gets sent to the GPU.
156+
157+
## *tgpu.rawCodeSnippet*
158+
159+
When working on top of some existing shader code, sometimes you may know for certain that some variable will be already defined and should be accessible in the code.
160+
In such scenario you can use `tgpu['~unstable'].rawCodeSnippet` -- an advanced API that creates a typed shader expression which can be injected into the final shader bundle upon use.
161+
162+
```ts twoslash
163+
import tgpu from 'typegpu';
164+
import * as d from 'typegpu/data';
165+
166+
// ---cut---
167+
// `EXISTING_GLOBAL` is an identifier that we know will be in the
168+
// final shader bundle, but we cannot
169+
// refer to it in any other way.
170+
const existingGlobal = tgpu['~unstable']
171+
.rawCodeSnippet('EXISTING_GLOBAL', d.f32, 'constant');
172+
173+
const foo = tgpu.fn([], d.f32)(() => {
174+
'use gpu';
175+
return existingGlobal.$ * 22;
176+
});
177+
178+
const wgsl = tgpu.resolve([foo]);
179+
// fn foo() -> f32 {
180+
// return (EXISTING_GLOBAL * 22f);
181+
// }
182+
```
183+
184+
:::note
185+
There currently is no way to create a `rawCodeSnippet` that refers to an existing function.
186+
:::
187+
188+
### Which origin to choose?
189+
190+
The optional third parameter `origin` lets TypeGPU transpiler know how to optimize the code snippet, as well as allows for some transpilation-time validity checks.
191+
192+
Usually `'runtime'` (the default) is a safe bet, but if you're sure that the expression or
193+
computation is constant (either a reference to a constant, a numeric literal,
194+
or an operation on constants), then pass `'constant'` as it might lead to better
195+
optimizations.
196+
197+
If what the expression is a direct reference to an existing value (e.g. a uniform, a
198+
storage binding, ...), then choose from `'uniform'`, `'mutable'`, `'readonly'`, `'workgroup'`,
199+
`'private'` or `'handle'` depending on the address space of the referred value.
200+
131201
## *console.log*
132202

133203
Yes, you read that correctly, TypeGPU implements logging to the console on the GPU!

apps/typegpu-docs/src/content/docs/reference/shader-generation.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ There are essentially three types of origins:
200200
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'`, `'constant-ref'` and `'this-function'`.
201201
- `'uniform'`, `'mutable'`, `'readonly'`, `'workgroup'`, `'private'`, `'function'`, `'handle'` all reflect address spaces that values can belong to, and
202202
we use them to determine what kind of pointer type they are.
203-
- `'constant-ref'` is a reference to a value stored in a `tgpu.const`. They're different from `constant`s, as we know that even if they're referential (non-primitive), the developer cannot mutate them.
203+
- `'constant-tgpu-const-ref'` is a reference to a value stored in a `tgpu.const`. They're different from `constant`s, as we know that even if they're referential (non-primitive), the developer cannot mutate them.
204+
- `'runtime-tgpu-const-ref'` is a reference to a value stored in a `tgpu.const` but accessed in a way that isn’t constant at shader‑creation time (for example, indexed using a runtime value).
204205
- `'this-function'` lets us track whether values originates from the function we're currently generating, or the function that called us.
205206
- **Argument Origins**: This group is dedicated to exactly one origin: 'argument'. It represents values that are passed as arguments to functions.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<canvas data-fit-to-container></canvas>

0 commit comments

Comments
 (0)