# Ch 8 buffer management question

**URL:** <https://forums.kodeco.com/t/ch-8-buffer-management-question/53748>\
**Category:** Metal by Tutorials\
**Created:** [October 24, 2018, 5:23pm UTC](https://forums.kodeco.com/t/ch-8-buffer-management-question/53748 "2018-10-24T17:23:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cocheret](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cocheret/32/105841_2.png) [@cocheret](https://forums.kodeco.com/u/cocheret)\
**Post date:** [October 24, 2018, 5:23pm UTC](https://forums.kodeco.com/t/ch-8-buffer-management-question/53748/1 "2018-10-24T17:23:31Z")

</div>

There is a lot of magic going on with the glTF code, which I haven’t yet had time to go through. However, I can see that it is managing the pipeline state including setting up things like the vertex descriptors.

What is confusing me most is the following code…

```
for attribute in submesh.attributes {
  renderEncoder.setVertexBuffer(buffers[attribute.bufferIndex], offset: attribute.offset, index: attribute.index)
}
renderEncoder.drawIndexedPrimitives(type: .triangle, indexCount: submesh.indexCount, indexType: submesh.indexType, indexBuffer: submesh.indexBuffer!, indexBufferOffset: submesh.indexBufferOffset)

```

In particular, the setVertexBuffer call. Let’s say all of the attributes are laid out in a single buffer (either by vertex or by attribute). Doesn’t calling setVertexBuffer in a loop like this cause that single buffer to be loaded multiple times into the GPU (once for each attribute)?

Thx,  
~chuck

---

<div class="post-metadata">

**Author:** ![cocheret](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cocheret/32/105841_2.png) [@cocheret](https://forums.kodeco.com/u/cocheret)\
**Post date:** [October 24, 2018, 6:37pm UTC](https://forums.kodeco.com/t/ch-8-buffer-management-question/53748/2 "2018-10-24T18:37:10Z")

</div>

Looking at a frame capture, it definitely looks like the same buffer is being referenced multiple times with different offsets each time setVertexBuffer is called. See image below. Interestingly, you can see how the vertex descriptor is set up in the pipeline state. Rather than referring to the same buffer and having the attributes at different offsets, the buffer is loaded multiple times with different offsets and the attributes are all described as offset 0.

![image](https://files.chunter.kodeco.com/original/3X/1/7/1705b74e050df8c50dd0ed3043be3367b5381d48.jpeg)

I hardcoded a hack to call setVertexBuffer once and to fix the vertex descriptor so the attributes start at the appropriate offsets and it works.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [October 25, 2018, 9:13am UTC](https://forums.kodeco.com/t/ch-8-buffer-management-question/53748/3 "2018-10-25T09:13:29Z")

</div>

I’m on holiday without a computer for a few days. I’m glad you found a better solution. I’d like to examine it when I return after the weekend.

Also I’ll address your other `roughness` post, as I think there was a reason for making it a `float3` but I can’t remember offhand what it was.

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [October 29, 2018, 4:23am UTC](https://forums.kodeco.com/t/ch-8-buffer-management-question/53748/4 "2018-10-29T04:23:55Z")

</div>

glTF allows for multiple layouts. Although in this case the attributes are in a single buffer, they aren’t guaranteed to be.
