Ch 8 buffer management question

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)?


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.


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.

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.

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