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/fundamentals/buffers.mdx
+131Lines changed: 131 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -216,6 +216,137 @@ If you pass an unmapped buffer, the data will be written to the buffer using `GP
216
216
If you passed your own buffer to the `root.createBuffer` function, you need to ensure it has the `GPUBufferUsage.COPY_DST` usage flag if you want to write to it using the `write` method.
217
217
:::
218
218
219
+
### Permissive write inputs
220
+
221
+
`.write()` accepts several equivalent forms — you don't need to construct typed instances:
222
+
223
+
| Input form | Example for `vec3f`| Example for `mat2x2f`| Notes |
| TypedArray |`new Float32Array([1, 2, 3])`|`new Float32Array([1, 2, 3, 4])`| Bytes copied as-is — must match GPU layout |
228
+
| ArrayBuffer |`rawBytes`|`rawBytes`| Bytes copied as-is — must match GPU layout |
229
+
230
+
When data already lives in tuples or typed arrays, skipping typed instance construction avoids allocating TypeGPU wrapper objects, which can reduce garbage-collector pressure in hot paths such as per-frame updates or simulation ticks.
:::note[TypedArray and ArrayBuffer inputs are copied as-is]
278
+
Wherever a `TypedArray` or `ArrayBuffer` is passed, the bytes are copied directly without any interpretation. The data must already match the GPU memory layout, including any padding. For example, each element in `d.arrayOf(d.vec3f, N)` occupies **16 bytes** (12 bytes of data + 4 bytes of padding), so the input must include those padding bytes.
const data =awaitbuffer.read(); // will be [0, 1, 2, 4, 5, 6]
319
+
```
320
+
321
+
An optional `endOffset` specifies the byte offset at which writing stops entirely.
322
+
Combined with `startOffset` and `d.memoryLayoutOf`, this lets you write to a precise region of the buffer. If ommitted, writing will continue until the end of the provided data or the end of the buffer, whichever comes first.
323
+
324
+
:::note
325
+
Both offsets are **byte-based**. Any component whose byte position falls at or beyond `endOffset` is not written, which means offsets that do not align to schema element boundaries can result in partial elements being written. Use `d.memoryLayoutOf` to target whole elements safely.
326
+
:::
327
+
328
+
```ts twoslash
329
+
importtgpu, { d } from'typegpu';
330
+
const root =awaittgpu.init();
331
+
// ---cut---
332
+
const schema =d.arrayOf(d.vec3u, 4);
333
+
const buffer =root.createBuffer(schema);
334
+
335
+
// Get the byte offsets of element [1] (start) and element [2] (stop)
// Write one vec3u at element [1], stopping before element [2]
340
+
buffer.write([d.vec3u(4, 5, 6)], {
341
+
startOffset: startLayout.offset,
342
+
endOffset: endLayout.offset,
343
+
});
344
+
```
345
+
346
+
:::note
347
+
In this particular case the `writePartial` method described in the next section would be a more convenient option, but the `startOffset` and `endOffset` options are useful for writing bigger slices of data.
348
+
:::
349
+
219
350
### Partial writes
220
351
221
352
When you want to update only a subset of a buffer’s fields, you can use the `.writePartial(data)` method. This method updates only the fields provided in the `data` object and leaves the rest unchanged.
0 commit comments