The issue you’re having is the result of SE-0107 UnsafeRawPointer API, which puts Swift’s type aliasing rules on a firm footing. If you’re working with low-level APIs like Metal, I strongly recommend you read the UnsafeRawPointer Migration doc before going further.
As to your specific issue, I’m not a Metal expert but it seems that you’re initialisation of
labColorBufferhas caused Metal to allocate raw memory that you want to treat as an array of
Float. If that’s correct, you can do this like so:
let vData = pData.bindMemory(to: Float.self, capacity: 4) vData.initialize(to: 0.250, count: 4)
The thing to watch out for here is that you don’t bind that raw memory to any other type. That doesn’t look like it’s be a problem in this case, but make sure to avoid it in general. As the docs say:
A memory location may only be bound to one type at a time. The behavior of accessing memory as type
Uwhile it is bound to an unrelated type
… also you need deinitialize ?
When dealing with low-level types like
Floatyou don’t need to worry too much deinitialisation; Swift does not need to, for example, release reference counts before the memory goes away.
On the initialisation front, you’ll want to make sure you set up all the elements to avoid junk values leaking out to the rest of your code. You can use
UnsafeMutableRawPointer.initializeMemory(to:count:)(or one of its variants) to do this, as I’ve shown above.
Share and Enjoy
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"