i have the same problem. When i stared to port my image processing framework based on MTL I faced with the nonsense.
i think you can try to use the follow trick i've used to solve the issue:
1. create MTLBuffer
let imageBuffer = device.newBufferWithLength( imageByteCount, options: MTLResourceOptions.CPUCacheModeDefaultCache)
2. create Blit operation encoder
let blitEncoder = commandBuffer.blitCommandEncoder()
3. copy texture to shared buffer
blitEncoder.copyFromTexture(texture,
sourceSlice: 0,
sourceLevel: 0,
sourceOrigin: MTLOrigin(x: 0, y: 0, z: 0),
sourceSize: MTLSize.width: width, height: height, depth: 1),
toBuffer: imageBuffer,
destinationOffset: 0,
destinationBytesPerRow: bytesPerRow,
destinationBytesPerImage: 0)
blitEncoder.endEncoding()
4. do somthing with buffer content
var rawData = [UInt8](count: width*height*components, repeatedValue: 0)
if texture.pixelFormat == .RGBA16Unorm {
for var i=0; i < rawData.count; i++ {
var pixel = UInt16()
let address = UnsafePointer<UInt16>(imageBuffer.contents())+i
memcpy(&pixel, address, sizeof(UInt16))
rawData[i] = UInt8(pixel>>8)
}
}
else{
memcpy(&rawData, imageBuffer.contents(), imageBuffer.length)
}
let cgprovider = CGDataProviderCreateWithData(nil, &rawData, imageByteCount, nil)
I soppouse it works slower then getBytes work version can, but it works. I hope this solution helps to solve your task.