I have a Metal texture, I want to access its data from Swift by making it a float4 array (so that I can access each pixel 4 color components).
I discovered this method
Another option is to create an array of the appropriate size and pass the address to the underlying storage to the function:
var pixelData = Array(repeating: float4(), count: myTextureSizeInFloat4)
pixelData.withUnsafeMutableBytes {
texture.getBytes($0.baseAddress!, ...)
}
Inside the closure, $0
is a UnsafeMutableRawBufferPointer
representing the array storage as a collection of bytes, and
$0.baseAddress
is a pointer to the first byte.