How to see the vertex descriptor of the MDLMesh helper geometries

Hi all,

In the book chapters, we use the VertexDescriptor.defaultLayout to specify the vertex layout of our meshes and conform them to the render pipelines:

extension MDLVertexDescriptor {
  static var defaultLayout: MDLVertexDescriptor = {
      let vertexDescriptor = MDLVertexDescriptor()
      // fill in the vertexDescriptor
      return vertexDescriptor
  }
}

I am having hard times understanding how do we conform other type of meshes to this?

Or to paraphrase my question, how do I conform a mesh created by:

MDLMesh(
    boxWithExtent extent: vector_float3,
    segments: vector_uint3,
    inwardNormals: Bool,
    geometryType: MDLGeometryType,
    allocator: MDLMeshBufferAllocator?
)

The spec hardly says anything about the vertex layout of this mesh. I can see only one vertex buffer is returned, so the normals, pos, uv, etc must be somehow interleaved. By trial and error I can see that the first 3 floats are positions, but can’t understand how to unpack the rest and fit it into the VertexDescriptor.defaultLayout

Thanks!

@nikoloffgeorgi - so sorry - I missed this one.

When you have an MDLMesh, you can print out its vertex descriptor to the console.

let mdlMesh = MDLMesh(
  boxWithExtent: [0.8, 0.8, 0.8],
  segments: [10, 10, 10],
  inwardNormals: false,
  geometryType: .triangles,
  allocator: allocator
)
print(mdlMesh.vertexDescriptor)

This gives the result for your box:

<MDLVertexDescriptor: 0x600000d18000 attributes:(
    "<MDLVertexAttribute: 0x600001810e80 name=position format=Float3 bufferIndex=0 offset=0>",
    "<MDLVertexAttribute: 0x600001810e40 name=normal format=Float3 bufferIndex=0 offset=12>",
    "<MDLVertexAttribute: 0x600001810fc0 name=textureCoordinate format=Float2 bufferIndex=0 offset=24>"
) layouts:{
    0 = "<MDLVertexBufferLayout: 0x600000f28790 stride=32>";
}>

So all the attributes are in buffer[0] with a stride of 32. position and normal are float3 and textureCoordinate is float2.

You can create a new pipeline state for this vertex descriptor instead of using defaultVertexDescriptor. You could also decide to reconfigure defaultVertexDescriptor depending on how you want your models to import.

1 Like

I also think that assigning an MDLVertexDescriptor to the MDLMesh will rearrange the buffers when you convert it to an MTKMesh. :crossed_fingers:

This playground appears to work.

VertexDescriptor.playground.zip (13.7 KB)

Here I have used MDLVertexDescriptor.defaultLayout from the lighting chapter, and assigned it to the box mesh. The MDLMesh to MTKMesh conversion appears to take place correctly, according to the new vertex descriptor.

1 Like