Efficiently copying Swift Array to memory buffer for iOS Metal

后端 未结 3 453
再見小時候
再見小時候 2021-02-04 18:23

I am writing an iOS application using Apple\'s new Metal framework. I have an array of Matrix4 objects (see Ray Wenderlich\'s tutorial) that I need to pass in to a shader via

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 19:12

    A Swift Array is promised to be contiguous memory, but you need to make sure it's really a Swift Array and not secretly an NSArray. If you want to be completely certain, use a ContiguousArray. That will ensure contiguous memory even if the objects in it are bridgeable to ObjC. If you want even more control over the memory, look at ManagedBuffer.

    With that, you should be using newBufferWithBytesNoCopy(length:options:deallocator) to create a MTL buffer around your existing memory.

提交回复
热议问题