I have been rendering polyhedron using Metal. The problem happens when I tried to render an icosahedron entity, whose vertices consists of barely 1680 bytes of data in size using newBufferWithBytes(). Then the entire app halt, both CPU and GPU frame drop to zero, and everything went back to normal, except Metal view freezes.
I have regular polyhedrons as Node's subclasses in implementation.
class Node {
let name : String
var vertexBuffer: MTLBuffer?
var uniformBuffer: MTLBuffer?
var vertexCount : Int = 0
var device : MTLDevice
init(name: String, vertices: [Vertex], device: MTLDevice){
self.name = name
self.device = device
var floatBuffer : [Float] = []
for vertex in vertices {
floatBuffer += vertex.floatBuffer
}
let floatBufferSize = floatBuffer.count * sizeof(Float)
self.vertexBuffer = device.newBufferWithBytes(&floatBuffer, length: floatBufferSize, options: nil)
self.vertexCount = floatBuffer.count
}
func render(commandEncoder: MTLRenderCommandEncoder, parentModelViewMatrix: Matrix4, projectionMatrix: Matrix4){
commandEncoder.setVertexBuffer(self.vertexBuffer, offset: 0, atIndex: 0)
// set up uniform transformation matrices
var nodeModelMatrix = self.modelMatrix()
nodeModelMatrix.multiplyLeft(parentModelViewMatrix)
let matrixSize = sizeof(Float) * Matrix4.numberOfElements()
uniformBuffer = device.newBufferWithLength(matrixSize * 2, options: .OptionCPUCacheModeDefault)
var bufferPointer = uniformBuffer?.contents()
memcpy(bufferPointer!, nodeModelMatrix.raw(), matrixSize)
memcpy(bufferPointer! + matrixSize, projectionMatrix.raw(), matrixSize)
commandEncoder.setVertexBuffer(self.uniformBuffer, offset: 0, atIndex: 1)
// can draw
commandEncoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: self.vertexCount, instanceCount: 1)
}
}