我创建了一个由浮点值数组填充的缓冲区。不幸的是,当我尝试查询其contents()
属性时(尝试调试问题时),我得到了EXC_BAD_ACCESS。这是代码:
let inputData = [Float32](repeatElement(5, count: 16384)) // Declaration
// Declaration of the buffer and filling contents...
inputBuffer = device.makeBuffer(bytes: inputData, length: MemoryLayout<Float32>.size * inputData.count, options: MTLResourceOptions())
// Crash occurs here:
let contents = inputBuffer.contents().load(as: [Float32].self)
我想知道为什么发生崩溃。其他类似的缓冲区声明也可以正常工作,因此我想这可能是访问内容的问题。
要使用该load
方法访问数组内容,您需要指定数组元素类型(和可选的偏移量)。例如:
let firstElement = inputBuffer.contents().load(fromByteOffset: 0, as: Float.self)
等等。您试图将第一个元素加载为,[Float]
这可能解释了崩溃的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句