I'm trying to add multiple objects to the scene, starting from Xcode 9's Metal Game App template.
I've modified the template as little as possible, to render multiple objects, based on this answer: https://stackoverflow.com/a/37424817/518169
My problems is that the second object doesn't appear, and I cannot debug this issue. Adding print lines show the buffer is updated, but I'm not sure it's the same state on the GPU as well.
Why is the second object not drawn? Is it something about how I'm handling the buffers, or it's something in the draw pipeline (like clearing the screen twice for example)?
_ = inFlightSemaphore.wait(timeout: DispatchTime.distantFuture)
if let commandBuffer = commandQueue.makeCommandBuffer() {
let semaphore = inFlightSemaphore
commandBuffer.addCompletedHandler { (_) -> Swift.Void in
semaphore.signal()
}
updateDynamicBufferState()
scene.updateGameState()
uniforms[0].projectionMatrix = scene.projectionMatrix
if let renderPassDescriptor = view.currentRenderPassDescriptor,
let renderEncoder = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) {
renderEncoder.label = "Primary Render Encoder"
for node in scene.nodes { // ------ FOR LOOP START
uniforms[0].modelViewMatrix = node.modelViewMatrix
renderEncoder.pushDebugGroup("Draw Node: \(node.name)")
renderEncoder.setCullMode(node.cullMode)
renderEncoder.setFrontFacing(node.frontFacing)
renderEncoder.setRenderPipelineState(node.pipelineState)
renderEncoder.setDepthStencilState(depthState)
renderEncoder.setVertexBuffer(dynamicUniformBuffer, offset: uniformBufferOffset, index: 2)
renderEncoder.setFragmentBuffer(dynamicUniformBuffer, offset: uniformBufferOffset, index: 2)
node.render(renderEncoder: renderEncoder) // setVertexBuffer, setFragmentTexture, drawIndexedPrimitives
renderEncoder.popDebugGroup()
} // ------ FOR LOOP END
renderEncoder.endEncoding()
if let drawable = view.currentDrawable {
commandBuffer.present(drawable)
}
}
commandBuffer.commit()
}