问题描述
我一直在使用Metal渲染多面体.当我尝试使用newBufferWithBytes()渲染一个二十面体实体时,会出现问题,该实体的顶点几乎只有1680字节的数据大小.然后整个应用程序停止运行,CPU和GPU框架都降为零,除了Metal视图冻结之外,一切恢复正常.
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.
我在实现中使用常规多面体作为Node的子类.
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)
}
}
推荐答案
感谢Nehal女士的建议.我已经相应地修改了答案.
Thanks for Ms. Nehal's suggestion. I have modified the answer accordingly.
根据Apple文档:金属功能集表,下面列出了硬件,最大MTLBuffer长度均为256 MB.
According to Apple Documentation: Metal Feature Set Tables, with the hardware listed below, Maximum MTLBuffer length are all 256 MB.
iOS 8和9上的Apple A7〜A9 GPU.
Apple A7 ~ A9 GPU on iOS 8 and 9.
OS X 10.11:MacBook(2015年初),MacBook Air(2012年中或更高版本),MacBook Pro(2012年中或更高版本),Mac Mini(2012年末或更高版本),iMac(2012年末或更高版本)和Mac专业版(2013年末及更高版本).
OS X 10.11: MacBook (early 2015), MacBook Air (mid 2012 or newer), MacBook Pro (mid 2012 or newer), Mac Mini (late 2012 or newer), iMac (late 2012 or newer), and Mac Pro (late 2013 and newer).
这篇关于newBufferWithBytes()是否有大小限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!