为使用CVMetalTextureCacheCreateTextureFromImage创建的纹理指定TextureUsage-(ios)

时间:2018-09-10 11:14:05

标签: ios swift metalkit

我只是尝试使用CVPixelBuffer创建纹理。

var pixelBuffer: CVPixelBuffer?

let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
             kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue,
             kCVPixelBufferMetalCompatibilityKey: kCFBooleanTrue]

var status = CVPixelBufferCreate(nil, Int(image.size.width), Int(image.size.width),
                                 kCVPixelFormatType_32BGRA, attrs as CFDictionary,
                                 &pixelBuffer)
assert(status == noErr)

let coreImage = CIImage(image: image)!
let context = CIContext(mtlDevice: MTLCreateSystemDefaultDevice()!)
context.render(coreImage, to: pixelBuffer!)

var textureWrapper: CVMetalTexture?
var textureCache:CVMetalTextureCache?

let successfullyCreationOfTextureCache = CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, device, nil, &textureCache)


status = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault,
                                                   GPUManager.shared.textureCache, pixelBuffer!, nil, .bgra8Unorm,
                                                   CVPixelBufferGetWidth(pixelBuffer!), CVPixelBufferGetHeight(pixelBuffer!), 0, &textureWrapper)

let texture = CVMetalTextureGetTexture(textureWrapper!)!
根据Apple文档,

表示CVMetalTextureCache对象可直接从GPU图像缓冲区读取或写入。因此,我只需要指定创建的纹理(.shaderWrite,.shaderRead)的纹理用法即可。

有什么建议吗?

0 个答案:

没有答案