Skip to content

Commit bfd9e4b

Browse files
authored
docs: index buffers (#1496)
1 parent 33d31b2 commit bfd9e4b

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,20 @@ import ListItem from '../../../components/ListItem.astro';
315315
data will be copied to the staging buffer before being read. After the data is read, the staging buffer will be destroyed.
316316
</ListItem>
317317
:::
318+
319+
## Index Buffers
320+
TypeGPU also allows for the use of index buffers. An index buffer is a buffer containing a sequence of `u32` or `u16` indices, which indicate in which order vertices will be processed. This is particulary useful for rendering geometry, where vertices are often shared between multiple primitives (e.g., triangles), as it avoids duplicating vertex data.
321+
You may refer to the [pipelines](/TypeGPU/fundamentals/pipelines/#drawing-with-drawindexed) section for usage details.
322+
323+
```ts twoslash
324+
import tgpu from 'typegpu';
325+
import * as d from 'typegpu/data';
326+
327+
const root = await tgpu.init();
328+
// ---cut---
329+
const indexBuffer = root
330+
.createBuffer(d.arrayOf(d.u16, 6), [0, 2, 1, 0, 3, 2])
331+
.$usage('index');
332+
333+
```
334+

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,65 @@ Unlike render pipelines, after running this method, the execution is not submitt
329329
In order to do so, `root['~unstable'].flush()` needs to be run.
330330
However, that is usually not necessary, as it is done automatically when trying to read the result of computation.
331331

332+
### Drawing with `drawIndexed`
333+
334+
The `drawIndexed` is analogous to draw, but takes advantage of [index buffer](/TypeGPU/fundamentals/buffers/#index-buffers) to explicitly map vertex data onto primitives. When using an index buffer, you don't need to list every vertex for every primitive explicitly. Instead, you provide a list of unique vertices in a vertex buffer. Then, the index buffer defines how these vertices are connected to form primitives.
335+
336+
```ts twoslash
337+
import tgpu from 'typegpu';
338+
import * as d from 'typegpu/data';
339+
340+
const root = await tgpu.init();
341+
const presentationFormat = "rgba8unorm";
342+
343+
const colorBuffer = root
344+
.createBuffer(d.arrayOf(d.vec4f, 4), [
345+
d.vec4f(1, 0, 0, 1), // Red
346+
d.vec4f(0, 1, 0, 1), // Green
347+
d.vec4f(0, 0, 1, 1), // Blue
348+
d.vec4f(1, 1, 0, 1), // Yellow
349+
])
350+
.$usage('vertex');
351+
352+
const vertex = tgpu['~unstable'].vertexFn({
353+
in: {
354+
idx: d.builtin.vertexIndex,
355+
color: d.vec4f,
356+
},
357+
out: {
358+
color: d.vec4f,
359+
pos: d.builtin.position,
360+
},
361+
})(({ idx, color }) => {
362+
return {
363+
color,
364+
pos: d.vec4f(d.vec2f(1, 1), 1, 1),
365+
};
366+
});
367+
368+
const vertexLayout = tgpu.vertexLayout(d.arrayOf(d.vec4f));
369+
const mainFragment = tgpu['~unstable'].fragmentFn({
370+
in: {
371+
color: d.vec4f,
372+
},
373+
out: d.vec4f,
374+
})((input) => input.color);
375+
// ---cut---
376+
const indexBuffer = root
377+
.createBuffer(d.arrayOf(d.u16, 6), [0, 2, 1, 0, 3, 2])
378+
.$usage('index');
379+
380+
const pipeline = root['~unstable']
381+
.withVertex(vertex, { color: vertexLayout.attrib })
382+
.withFragment(mainFragment, { format: presentationFormat })
383+
.createPipeline()
384+
.withIndexBuffer(indexBuffer);
385+
386+
pipeline
387+
.with(vertexLayout, colorBuffer)
388+
.drawIndexed(6);
389+
```
390+
332391
## Low-level render pipeline execution API
333392

334393
The higher-level API has several limitations, therefore another way of executing pipelines is exposed, for some custom, more demanding scenarios. For example, with the high-level API, it is not possible to execute multiple pipelines per one render pass. It also may be missing some more niche features of the WebGPU API.

0 commit comments

Comments
 (0)