Group Group Group Group Group Group Group Group Group

Chapter 5 meaning of BufferIndex

Hello forum fellows

I am at chapter 5 (indeed) of MBT.

I read this « MDLVertexAttribute(name: MDLVertexAttributeNormal, format: .float3, offset: offset, bufferIndex: 0) ».

And I still don’t figure out the meaning of bufferIndex.
Do you have any readings that you could advise to make me … maybe understand this concept.

Regards
Take care
Stay home

Behr
(at home)

This is described in Apple’s documentation:

https://developer.apple.com/documentation/modelio/mdlvertexattribute/1391088-bufferindex

However, that really only explains it if you know it already.

Imagine that you have a buffer of three vertices containing:

position, normal, position, normal, position, normal

That is one single buffer.

That set of vertices could be described with two buffers though:

position, position, position

normal, normal, normal

Your MDLVertexAttribute would use buffer index 0 for the first, and buffer index 1 for the second.

You would describe the Vertex Descriptor layout stride for each buffer. The stride is how many bytes between each vertex.

The first example in one buffer would have a layout[0].stride of float3 + float3.

The second example in two buffers would have layout[0].stride as float3 and layout[1].stride as float3.

When you send your vertex buffers to the GPU, in the final project, in draw(in:), you are only sending the first vertex buffer, described by buffer index 0 in the MDLVertexDescriptor:

let vertexBuffer = mesh.mtkMesh.vertexBuffers[0].buffer
renderEncoder.setVertexBuffer(vertexBuffer, offset: 0,
                              index: 0)

Later on, when you add tangent normals (chapter 6 textures), you’ll add a second vertex descriptor buffer, and you’ll loop through the mtkMesh.vertexBuffers to make sure that both buffers are sent to the GPU.



I wrote this before I realised that you were talking about the buffer index on the vertex attributes :roll_eyes::D, so I’ll just leave it here. Ignore it if you want to :slight_smile:

MTLBuffers contain chunks of data that you can pass to the GPU.

The GPU has a very strict IO interface, and when you transfer the MTLBuffer from RAM to GPU RAM, you assign buffers to indexed slots in an argument table on the GPU.

Depending on the device, there are limits on how many of these slots are available.

You send MTLBuffers to an indexed slot in the buffer argument table, MTLTextures to the texture argument table, and sampler states to the sampler state argument table.

There are 31 slots in the texture argument table on early iOS devices, whereas there are 128 slots on macOS. Meaning that you can render with a lot more textures on macOS than iOS.

You can see limits in Apple’s Feature Set document: