CGImage 到 MPSTexture 或 MPSImage

CGImage to MPSTexture or MPSImage

我有一个由 CVPixelbuffer (ARGB) 构建的 CGImage。我想将该 CGImage 转换为 MTLTexture。我使用:

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

稍后我想在具有 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)

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

谢谢,克里斯

为什么不直接从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
}

一旦你有你的 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)
}

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