Is there a size limit to newBufferWithBytes()?

可紊 提交于 2019-12-11 10:21:27

问题


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)
}
}

回答1:


Thanks for Ms. Nehal's suggestion. I have modified the answer accordingly.

According to Apple Documentation: Metal Feature Set Tables, with the hardware listed below, Maximum MTLBuffer length are all 256 MB.

Apple A7 ~ A9 GPU on iOS 8 and 9.

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).



来源:https://stackoverflow.com/questions/30578202/is-there-a-size-limit-to-newbufferwithbytes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!