如何将 bgra8Unorm iOS-Metal 纹理转换为 rgba8Unorm 纹理?

How to convert bgra8Unorm iOS-Metal texture to rgba8Unorm texture?

我正在使用 iOS 11、XCode 9 和 Metal 2。我有一个 MTLTexture 像素格式 bgra8Unorm。我无法更改此像素格式,因为根据 pixelFormat documentation:

The pixel format for a Metal layer must be bgra8Unorm, bgra8Unorm_srgb, rgba16Float, BGRA10_XR, or bgra10_XR_sRGB.

其他像素格式不适合我的应用。

现在我想从纹理创建一个 UIImage。我可以通过从纹理中提取像素字节来做到这一点 (doc):

getBytes(_:bytesPerRow:bytesPerImage:from:mipmapLevel:slice:)

我正在处理这些字节以获得 UIImage:

func getUIImageForRGBAData(data: Data) -> UIImage? {
    let d = (data as NSData)

    let width = GlobalConfiguration.textureWidth
    let height = GlobalConfiguration.textureHeight
    let rowBytes = width * 4
    let size = rowBytes * height

    let pointer = malloc(size)
    memcpy(pointer, d.bytes, d.length)

    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let context = CGContext(data: pointer, width: width, height: height, bitsPerComponent: 8, bytesPerRow: rowBytes, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!

    let imgRef = context.makeImage()
    let image = UIImage(cgImage: imgRef!)
    return image
}

但是,CGContext 假设像素是 rgba8 格式。例如,红色纹理像素在最终的 UIImage 中是蓝色的。有没有办法在此过程中更改像素格式以获得正确的颜色?

此函数会将 .bgra8Unorm 纹理的字节混合成 RGBA 顺序并根据数据创建 UIImage

func makeImage(from texture: MTLTexture) -> UIImage? {
    let width = texture.width
    let height = texture.height
    let bytesPerRow = width * 4

    let data = UnsafeMutableRawPointer.allocate(bytes: bytesPerRow * height, alignedTo: 4)
    defer {
        data.deallocate(bytes: bytesPerRow * height, alignedTo: 4)
    }

    let region = MTLRegionMake2D(0, 0, width, height)
    texture.getBytes(data, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0)

    var buffer = vImage_Buffer(data: data, height: UInt(height), width: UInt(width), rowBytes: bytesPerRow)

    let map: [UInt8] = [2, 1, 0, 3]
    vImagePermuteChannels_ARGB8888(&buffer, &buffer, map, 0)

    guard let colorSpace = CGColorSpace(name: CGColorSpace.genericRGBLinear) else { return nil }
    guard let context = CGContext(data: data, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow,
                                  space: colorSpace, bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue) else { return nil }
    guard let cgImage = context.makeImage() else { return nil }

    return UIImage(cgImage: cgImage)
}

警告:此功能非常昂贵。每一帧都从金属纹理创建图像几乎不是您想要做的。