Beginning Metal - Part 4: Indices and Constants | Ray Wenderlich

Learn how to optimize rendering performance using indices. You’ll also learn how to do simple animation and create a scene graph.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3537-beginning-metal/lessons/4

Halfway the tutorial in the draw function you’re wrapping the indexBuffer in a guard statement. But why didn’t you do this with the vertexBuffer as well?
Thanks for the help!

@bababoega - Because it’s an error not to.

commandEncoder.setVertexBuffer(vertexBuffer,
                               offset: 0, at: 0)

setVertexBuffer(_:offset:at:) allows an optional in the buffer.

commandEncoder.drawIndexedPrimitives(type: .triangle, 
                                     indexCount: indices.count,
                                     indexType: .uint16,
                                     indexBuffer: indexBuffer,
                                     indexBufferOffset: 0)

drawIndexedPrimitives(type:indexCount:indexType:indexBuffer:indexBufferOffset:) does not allow the indexBuffer parameter to be an optional.

The guard statement unwraps the optional.