Skip to content

Commit de727ca

Browse files
committed
Merge branch 'main' into feat/byeflush
2 parents c3404e0 + eef9852 commit de727ca

File tree

105 files changed

+3618
-1102
lines changed

Some content is hidden

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

105 files changed

+3618
-1102
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Every `root.create*` function creates a typed resource.
4343

4444
| Function | Description |
4545
| --- | --- |
46-
| <div className="w-max">`root.createBuffer`</div> | Creates a typed buffer with a given data-type and, optionally, an initial value. More information in [the next chapter](/TypeGPU/fundamentals/buffers). |
46+
| <div className="w-max">`root.createBuffer`</div> | Creates a typed buffer with a given data-type and, optionally, an initial value. More information in [the Buffers chapter](/TypeGPU/fundamentals/buffers). |
4747

4848
## Unwrapping resources
4949

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ Keep in mind that you cannot execute entry-point functions in JavaScript.
115115

116116
* **TGSL limitations** --
117117
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.
118-
This means that, for example, `console.log()` calls will not work on the GPU.
118+
This means that, for example, `Math.sqrt(n)` calls will not work on the GPU.
119+
One exception to this is `console.log()`, about which you can read more [here](/TypeGPU/fundamentals/utils/#consolelog).
119120

120121
* **Differences between JS on the CPU and GPU** --
121122
TGSL is developed to work on the GPU the same as on the CPU as much as possible,

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,85 @@ a batch, just split the batch in two.
184184
:::danger
185185
Nested batching is not supported and will result in a runtime error.
186186
:::
187+
188+
## *console.log*
189+
190+
Yes, you read that correctly, TypeGPU implements logging to the console on the GPU!
191+
Just call `console.log` like you would in plain JavaScript, and open the console to see the results.
192+
193+
```ts twoslash
194+
import tgpu, { prepareDispatch } from 'typegpu';
195+
import * as d from 'typegpu/data';
196+
197+
const root = await tgpu.init();
198+
// ---cut---
199+
const callCountMutable = root.createMutable(d.u32, 0);
200+
const dispatch = prepareDispatch(root, () => {
201+
'kernel';
202+
callCountMutable.$ += 1;
203+
console.log('Call number', callCountMutable.$);
204+
});
205+
206+
dispatch();
207+
dispatch();
208+
209+
// Eventually...
210+
// "[GPU] Call number 1"
211+
// "[GPU] Call number 2"
212+
```
213+
214+
Under the hood, TypeGPU translates `console.log` to a series of serializing functions that write the logged arguments to a buffer that is read and deserialized after every draw/dispatch call.
215+
216+
The buffer is of fixed size, which may limit the total amount of information that can be logged; if the buffer overflows, additional logs are dropped.
217+
If that's an issue, you may specify the size manually when creating the `root` object.
218+
219+
```ts twoslash
220+
import tgpu, { prepareDispatch } from 'typegpu';
221+
import * as d from 'typegpu/data';
222+
223+
const presentationFormat = undefined as any;
224+
const canvas = undefined as any;
225+
const context = canvas.getContext('webgpu') as any;
226+
// ---cut---
227+
const root = await tgpu.init({
228+
unstable_logOptions: {
229+
logCountLimit: 32,
230+
logSizeLimit: 8, // in bytes, enough to fit 2*u32
231+
},
232+
});
233+
234+
/* vertex shader */
235+
236+
const mainFragment = tgpu['~unstable'].fragmentFn({
237+
in: { pos: d.builtin.position },
238+
out: d.vec4f,
239+
})(({ pos }) => {
240+
// this log fits in 8 bytes
241+
// static strings do not count towards the serialized log size
242+
console.log('X:', d.u32(pos.x), 'Y:', d.u32(pos.y));
243+
return d.vec4f(0, 1, 1, 1);
244+
});
245+
246+
/* pipeline creation and draw call */
247+
```
248+
249+
:::note
250+
The logs are written to console only after the dispatch finishes and the buffer is read.
251+
This may happen with a noticeable delay.
252+
:::
253+
254+
:::caution
255+
When using `console.log`, atomic operations are injected into the WGSL code to safely synchronize logging from multiple threads.
256+
This synchronization can introduce overhead and significantly impact shader performance.
257+
:::
258+
259+
There are some limitations (some of which we intend to alleviate in the future):
260+
261+
- `console.log` only works when used in TGSL, when calling or resolving a TypeGPU pipeline.
262+
Otherwise, for example when using `tgpu.resolve` on a WGSL template, logs are ignored.
263+
- `console.log` only works in fragment and compute shaders.
264+
This is due to [WebGPU limitation](https://www.w3.org/TR/WGSL/#address-space) that does not allow modifying buffers during the vertex shader stage.
265+
- TypeGPU needs to handle every logged data type individually.
266+
Currently, the only supported types are `bool`, `u32`, `vec2u`, `vec3u` and `vec4u`.
267+
- `console.log` currently does not support template literals and string substitutions.
268+
- Other `console` methods like `clear` or `warn` are not yet supported.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)