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 labColorBuffer has 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:

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 U while it is bound to an unrelated type T is undefined.

… also you need deinitialize ?

When dealing with low-level types like Float you 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.

More Like This

Retrieving data ...

This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Developer Forums Participation Agreement.