Non-Interleaved Buffer Layouts

Hello -

I was playing around with a non-interleaved vertex layout and hit a snag. When I am loading a model, I now get an array of vertex buffers from MTKMesh objects. This is what I expect in this case —- the descriptor has two layouts: one for position data and another for color.

In the book, we learn to use an interleaved format and map the data using attributes. But in this case, when I get the array of buffers from the model, I have no way of knowing which buffer is position and which is color, and therefore, have to guess at which buffer the data is assigned to.

What am I missing?

By happenstance, I discovered that the standard SceneKit template uses this sort of layout. After studying how the template works, the solution (I believe) is to offset the index in this loop:

for (index, _) in mesh.mtkMesh.vertexDescriptor.layouts.enumerated() {

let buffer = mesh.mtkMesh.vertexBuffers[index]

renderEncoder.setVertexBuffer(buffer.buffer, offset: buffer.offset,

                             index: index + **OFFSET HERE**)

}

I was perplex by the SceneKit code because it uses index to set the buffer in the last line of code. I was expecting a solution that did not depend on all of the vertex attributes being in the range of 0 - 2 (when using position/normal/uv attributes). In the code I was experimenting with, I had added new indices starting at 4, which was causing a crash.

in my case, offsetting by 4 solved my confusion.

1 Like

@lducot2 Thank you for sharing your solution - much appreciated!