Update contents of MTLBuffer in Metal

二次信任 提交于 2019-12-06 06:09:53

问题


I need help to replace the contents of a MTLBuffer without creating a new one. Content in both cases are Float Arrays.

let vector:[Float] = [0,1,2,3,4,5,6,7,8,9]   
let byteLength = arr1.count*MemoryLayout<Float>.size
let buffer = metalDevice.makeBuffer(bytes: &vector, length: byteLength, options: MTLResourceOptions())

let vector2:[Float] = [10,20,30,40,50,60,70,80,90]

I understand buffer.contents() gives us a UnsafeMutableRawPointer. I would like to create a new UnsafeMutableRawPointer from vector2 and replace the contents of buffer.

Thanks in advance!


回答1:


You can do this with memcpy, but the slightly Swiftier way is:

buffer.contents().copyBytes(from: vector2, count: vector2.count * MemoryLayout<Float>.stride)

In general, prefer stride over size when calculating the length of an array in bytes. If the type is not primitive and has any padding, size will not account for this, and you'll copy fewer bytes than you intended.



来源:https://stackoverflow.com/questions/42561558/update-contents-of-mtlbuffer-in-metal

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