首页 > 解决方案 > 关于 CIContext、OpenGL 和 Metal (SWIFT) 的困惑。CIContext 默认使用 CPU 还是 GPU?

问题描述

所以我正在制作一个应用程序,其中一些主要功能围绕着将 CIFilters 应用于图像。

let context = CIContext()
let context = CIContext(eaglContext: EAGLContext(api: .openGLES3)!)
let context = CIContext(mtlDevice: MTLCreateSystemDefaultDevice()!)

所有这些都在我的 CameraViewController 上为我提供了相同的 CPU 使用率 (70%),我将过滤器应用于帧并更新图像视图。所有这些似乎都以完全相同的方式工作,这让我觉得我错过了一些重要的信息。

例如,使用 AVFoundation 我从相机获取每一帧应用过滤器并使用新图像更新图像视图。

let context = CIContext()

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    connection.videoOrientation = orientation
    connection.isVideoMirrored = !cameraModeIsBack
    let videoOutput = AVCaptureVideoDataOutput()
    videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main)

    let sharpenFilter = CIFilter(name: "CISharpenLuminance")
    let saturationFilter = CIFilter(name: "CIColorControls")
    let contrastFilter = CIFilter(name: "CIColorControls")
    let pixellateFilter = CIFilter(name: "CIPixellate")

    let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
    var cameraImage = CIImage(cvImageBuffer: pixelBuffer!)

    saturationFilter?.setValue(cameraImage, forKey: kCIInputImageKey)
    saturationFilter?.setValue(saturationValue, forKey: "inputSaturation")
    var cgImage = context.createCGImage((saturationFilter?.outputImage!)!, from: cameraImage.extent)!
    cameraImage = CIImage(cgImage: cgImage)

    sharpenFilter?.setValue(cameraImage, forKey: kCIInputImageKey)
    sharpenFilter?.setValue(sharpnessValue, forKey: kCIInputSharpnessKey)
    cgImage = context.createCGImage((sharpenFilter?.outputImage!)!, from: (cameraImage.extent))!
    cameraImage = CIImage(cgImage: cgImage)

    contrastFilter?.setValue(cameraImage, forKey: "inputImage")
    contrastFilter?.setValue(contrastValue, forKey: "inputContrast")
    cgImage = context.createCGImage((contrastFilter?.outputImage!)!, from: (cameraImage.extent))!
    cameraImage = CIImage(cgImage: cgImage)

    pixellateFilter?.setValue(cameraImage, forKey: kCIInputImageKey)
    pixellateFilter?.setValue(pixelateValue, forKey: kCIInputScaleKey)
    cgImage = context.createCGImage((pixellateFilter?.outputImage!)!, from: (cameraImage.extent))!
    applyChanges(image: cgImage)

}

另一个例子是我如何将更改仅应用于普通图像(我使用滑块来完成所有这些)

   func imagePixelate(sliderValue: CGFloat){
    let cgImg = image?.cgImage
    let ciImg = CIImage(cgImage: cgImg!)
    let pixellateFilter = CIFilter(name: "CIPixellate")
    pixellateFilter?.setValue(ciImg, forKey: kCIInputImageKey)
    pixellateFilter?.setValue(sliderValue, forKey: kCIInputScaleKey)
    let outputCIImg = pixellateFilter?.outputImage!
    let outputCGImg = context.createCGImage(outputCIImg!, from: (outputCIImg?.extent)!)
    let outputUIImg = UIImage(cgImage:outputCGImg!, scale:(originalImage?.scale)!, orientation: originalOrientation!)
    imageSource[0] = ImageSource(image: outputUIImg)
    slideshow.setImageInputs(imageSource)
    currentFilteredImage = outputUIImg
}

差不多:

  1. 从 UiImg 创建 CgImg
  2. 从 CgImg 创建 CiImg
  3. 使用上下文应用过滤器并转换回 UiImg
  4. 使用新的 UIImg 更新任何视图

这在我的 iPhone X 上运行良好,在我的 iPhone 6 上也运行良好。由于我的应用程序非常完整,因此我希望尽可能地对其进行优化。我也浏览了很多关于使用 OpenGL 和 Metal 来做事的文档,但似乎不知道如何开始。

我一直以为我是在 CPU 上运行这些进程,但使用 OpenGL 和 Metal 创建上下文并没有带来任何改进。我是否需要使用 MetalKit 视图或 GLKit 视图(eaglContext 似乎已完全弃用)?我该如何翻译这个?苹果文档似乎乏善可陈。

标签: iosswiftopenglmetal

解决方案


我开始对此发表评论,但我认为自从 WWDC'18 以来,这最适合作为答案。我会像其他人一样编辑,而不是评论,如果这是正确的做法,我愿意删除整个答案。

您走在正确的轨道上 - 尽可能使用 GPU,它非常适合。CoreImage 和 Metal 虽然“通常”使用 GPU 的“低级”技术,但如果需要,可以使用 CPU。核心图形?它使用 GPU渲染事物。

图片。AUIImage和 aCGImage是实际图像。然而CIImage,不是。思考它的最佳方式是图像的“配方”。

我通常——现在,我稍后会解释——坚持使用 CoreImage、CIFiltersCIImages 以及GLKViews使用过滤器时。对 CIImage 使用 GLKView 意味着使用OpenGLand 单个CIContextand EAGLContext。它提供的性能几乎与使用MetalKitor一样好MTKViews

至于使用UIKitand it's UIImageand UIImageView,我只在需要时使用 - 保存/共享/上传等。在那之前坚持使用 GPU。

……

这就是它开始变得复杂的地方。

Metal 是 Apple 专有的 API。由于他们拥有硬件——包括 CPU 和 GPU——他们已经为他们优化了硬件。它的“管道”与 OpenGL 有所不同。没什么大不了的,只是不一样。

在 WWDC'18 之前,使用GLKit,包括GLKView,都很好。但是 OpenGL 的所有东西都被贬低了,苹果正在把东西转移到 Metal 上。虽然性能提升(目前)不是那么好,但您可能最好使用新的东西来使用 MTKView、Metal 和 CIContext`。

查看@matt 在这里给出的答案,了解使用 MTKViews 的好方法。


推荐阅读