Skip to content

Commit

Permalink
Fix vanilla Index Buffer chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
eliemichel committed Jul 5, 2024
1 parent c1df6c3 commit 1b020cb
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions basic-3d-rendering/input-geometry/index-buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Using the index buffer adds an **overhead** of `6 * sizeof(uint16_t)` = 12 bytes

This split of data reorganizes a bit our buffer initialization method:

```{lit} C++, InitializeBuffers method (replace)
```{lit} C++, InitializeBuffers method (replace, also for tangle root "Vanilla")
void Application::InitializeBuffers() {
{{Define point data}}
{{Define index data}}
Expand Down Expand Up @@ -165,6 +165,22 @@ private: // Application attributes
```
````

And as usual, we release buffers in `Terminate()`

````{tab} With webgpu.hpp
```{lit} C++, Terminate (prepend)
pointBuffer.release();
indexBuffer.release();
```
````

````{tab} Vanilla webgpu.h
```{lit} C++, Terminate (prepend, for tangle root "Vanilla")
wgpuBufferRelease(pointBuffer);
wgpuBufferRelease(indexBuffer);
```
````

```{lit} C++, Create point buffer (hidden, append, also for tangle root "Vanilla")
// It is not easy with the auto-generation of code to remove the previously
// defined `vertexBuffer` attribute, but at the same time some compilers
Expand Down Expand Up @@ -242,10 +258,10 @@ renderPass.drawIndexed(indexCount, 1, 0, 0, 0);
wgpuRenderPassEncoderSetPipeline(renderPass, pipeline);

// Set both vertex and index buffers
wgpuRenderPassEncoderSetVertexBuffer(renderPass, 0, vertexBuffer, 0, wgpuBufferGetSize(pointData));
wgpuRenderPassEncoderSetVertexBuffer(renderPass, 0, pointBuffer, 0, wgpuBufferGetSize(pointBuffer));
// The second argument must correspond to the choice of uint16_t or uint32_t
// we've done when creating the index buffer.
wgpuRenderPassEncoderSetIndexBuffer(renderPass, indexBuffer, WGPUIndexFormat_Uint16, 0, wgpuBufferGetSize(indexData));
wgpuRenderPassEncoderSetIndexBuffer(renderPass, indexBuffer, WGPUIndexFormat_Uint16, 0, wgpuBufferGetSize(indexBuffer));

// Replace `draw()` with `drawIndexed()` and `vertexCount` with `indexCount`
// The extra argument is an offset within the index buffer.
Expand Down Expand Up @@ -274,7 +290,7 @@ let ratio = 640.0 / 480.0; // The width and height of the target surface
out.position = vec4f(in.position.x, in.position.y * ratio, 0.0, 1.0);
```

```{lit} rust, Vertex shader body (hidden, also for tangle root "Vanilla")
```{lit} rust, Vertex shader body (replace, hidden, also for tangle root "Vanilla")
var out: VertexOutput; // create the output struct
{{Vertex shader position}}
out.color = in.color; // forward the color attribute to the fragment shader
Expand Down

0 comments on commit 1b020cb

Please sign in to comment.