CGImage到MPSTexture或MPSImage

Chr*_*ris 2 cgimage swift metal metalkit

我有一个CGImage,它由CVPixelbuffer(ARGB)构成.我想将CGImage转换为MTLTexture.我用:

  let texture: MTLTexture = try m_textureLoader.newTexture(with: cgImage, options: [MTKTextureLoaderOptionSRGB : NSNumber(value: true)] )
Run Code Online (Sandbox Code Playgroud)

后来我想在具有3个通道的MPSImage中使用纹理:

let sid   = MPSImageDescriptor(channelFormat: MPSImageFeatureChannelFormat.float16, width: 40, height: 40, featureChannels: 3)
preImage    = MPSTemporaryImage(commandBuffer:  commandBuffer, imageDescriptor: sid)
lanczos.encode(commandBuffer: commandBuffer, sourceTexture: texture!, destinationTexture: preImage.texture)
scale.encode  (commandBuffer: commandBuffer, sourceImage: preImage, destinationImage: srcImage)
Run Code Online (Sandbox Code Playgroud)

现在我的问题:textureLoader.newTexture(...)如何将四个ARGB通道映射到MPSImageDescriptor中指定的3个通道?如何确保使用RGB组件而不是ARG?有没有办法指定频道映射?

谢谢,克里斯

Mat*_*ans 5

为什么不直接从CVPixelBuffer构造MTLTexture?更快!

在程序开始时执行一次:

// declare this somewhere, so we can re-use it
var textureCache: CVMetalTextureCache?

// create the texture cache object
guard CVMetalTextureCacheCreate(kCFAllocatorDefault, nil, device, nil, &textureCache) == kCVReturnSuccess else {
  print("Error: could not create a texture cache")
  return false
}
Run Code Online (Sandbox Code Playgroud)

一旦你有了CVPixelBuffer就这样做:

let width = CVPixelBufferGetWidth(pixelBuffer)
let height = CVPixelBufferGetHeight(pixelBuffer)

var texture: CVMetalTexture?
CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache,
      pixelBuffer, nil, .bgra8Unorm, width, height, 0, &texture)

if let texture = texture {
  metalTexture = CVMetalTextureGetTexture(texture)
}
Run Code Online (Sandbox Code Playgroud)

现在metalTexture包含一个带有CVPixelBuffer内容的MTLTexture对象.