Hello Caroline, I want to know how to confirm the last parameter “bufferIndex” in init(name:format:offset:buferIndex:) function.

Hi there!

I don’t understand what you mean by “confirm”. Here’s the documentation on MDLVertexAttribute if that helps:

Sorry Caroline cuz I am a developer from oversea so that sometimes I can not express my mind exactly. I mean how to determine last parameter “bufferIndex” in MDLVertexAttribute initializer

You speak more languages than I do :smiley:

Have you experimented with looking at the render results using the GPU debugger?

This is a list of the vertex data buffers from Chapter 8, Animation:

Buffer 0 contains position, normal, uv etc. Buffer 1 contains tangent and Buffer 2 contains bitangent.

This is the contents of buffer 0:

You can exactly match these buffers and attributes to those laid out in defaultVertexDescriptor.

If I decide for some reason that I want the normals to have their own buffer, in defaultVertexDescriptor, I can change the code:

    // normal attribute
    vertexDescriptor.attributes[Int(Normal.rawValue)] =
      MDLVertexAttribute(name: MDLVertexAttributeNormal,
                         format: .float3,
//                         offset: offset,
//                         bufferIndex: Int(BufferIndexVertices.rawValue))
                         offset: 0,
                         bufferIndex: 3)
//    offset += MemoryLayout<float3>.stride

That’s using a new bufferIndex of 3. So at the end of that method I have to describe what that layout is:

 vertexDescriptor.layouts[3] =
      MDLVertexBufferLayout(stride: MemoryLayout<float3>.stride)

I now have 4 vertex buffers:

Buffer 0 doesn’t contain the normals, but Buffer 3 does.

Notice two things:

  1. I arbitrarily chose buffer 3 to put the normals in. I can choose which buffers to put vertex attributes in, as long as I tell the vertex descriptor which buffer the attribute is going in, and what the offset in that buffer is. I also have to tell the vertex descriptor the stride of that buffer.
  2. Because I’m using stage_in in the shader and name each attribute in VertexIn, when the GPU fetches the vertex, it can look at the vertex descriptor to match up buffers and attributes. When I change the layout of the vertex buffers in the vertex descriptor, I don’t have to change code in the shader at all.

When I initially chose the buffers to use, I examined what Model I/O gave me when reading in the obj file, and used the same structure. But, as you can see, I can decide to change that.

1 Like