我曾在本书《 makeBuffer(bytes:length:options:)并未按照本书中的说明运行。下面的相关代码
let vertexData: [Float] = [
0.0, 0.5, 0.0,
-1.0, -0.5, 0.0,
1.0, -0.5, 0.0
]
let dataSize = vertexData.count * MemoryLayout.size(ofValue: vertexData[0])
vertexBuffer = device.makeBuffer(bytes: vertexData, length: dataSize,
options: [.storageModePrivate]) // error on this line
...
let renderEncoder = commandBuffer?.makeRenderCommandEncoder(
descriptor: renderPassDescriptor)
renderEncoder?.setVertexBuffer(vertexBuffer, offset: 0, index: 0)
// skipping other setups
renderEncoder?.endEncoding()
将在运行时导致崩溃,这是错误日志:
-[MTLDebugDevice newBufferWithBytes:length:options:]:494: failed assertion `storageModePrivate incompatible with ...WithBytes variant of newBuffer'
但是,如果我使用
[]
作为options
中makeBuffer(bytes:length:options:)
的参数,则该程序将正常运行:
vertexBuffer = device.makeBuffer(bytes: vertexData, length: dataSize,
options: []) // this will run
但是为什么会这样呢?
我在《金属编程指南》一书中使用了一个用于iOS的简单Metal程序,但是makeBuffer(bytes:length:options :)不能按书中所述工作。下面的相关代码让...