2

我有一个由 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 组件而不是例如 ARG ?有没有办法指定通道映射?

谢谢,克里斯

4

1 回答 1

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
}

拥有 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 对象。

于 2017-07-05T14:27:13.570 回答