ios - 关于 CIContext、OpenGL 和 Metal (SWIFT) 的困惑。 CIContext 默认使用 CPU 还是 GPU?

标签 ios swift opengl metal

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

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

所有这些都让我在我的 CameraViewController 上使用了大约相同的 CPU 使用率 (70%),我在其中将滤镜应用于帧并更新 ImageView 。所有这些似乎都以完全相同的方式工作,这让我觉得我错过了一些重要的信息。

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

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)

}

另一个例子是我如何将更改仅应用于普通图像(我对所有这些都使用 slider )

   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 更新任何 View

这在我的 iPhone X 上运行良好,在我的 iPhone 6 上也运行良好。由于我的应用程序已经非常完整,所以我希望尽可能地对其进行优化。我也浏览了很多关于使用 OpenGL 和 Metal 做事情的文档,但似乎无法弄清楚如何开始。

我一直认为我是在 CPU 上运行这些进程,但使用 OpenGL 和 Metal 创建上下文并没有提供任何改进。我是否需要使用 MetalKit View 或 GLKit View (eaglContext 似乎已完全弃用)?我如何翻译这个? apple 的文档貌似乏善可陈。

最佳答案

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

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

图像。 UIImageCGImage 是实际图像。但是,CIImage 不是。最好的理解方式是图像的“配方”。

我通常 - 现在,我稍后会解释 - 在使用过滤器时坚持使用 CoreImage、CIFilters、CIImages 和 GLKViews。针对 CIImage 使用 GLKView 意味着使用 OpenGL 和单个 CIContextEAGLContext。它提供的性能几乎与使用 MetalKitMTKViews 一样好。

至于使用 UIKit 和它的 UIImageUIImageView,我只在需要时才做 - 保存/分享/上传等等。在那之前坚持使用 GPU。

....

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

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

在 WWDC'18 之前,使用 GLKit(包括 GLKView)没问题。但是 OpenGL 的所有东西都被贬低了,Apple 正在将东西转移到 Metal。虽然性能增益(目前)不是那么好,但您可能最好使用 MTKView、Metal 和 CIContext` 的新东西。

看@matt给的答案here了解使用 MTKViews 的好方法。

关于ios - 关于 CIContext、OpenGL 和 Metal (SWIFT) 的困惑。 CIContext 默认使用 CPU 还是 GPU?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51922595/

相关文章:

swift - 矩阵排序和转置 swift4

c++ - 使用具有透视图的移动相机时如何避免像素化几何体?

c++ - 旋转时照明的 GL_POINT_SPRITE_ARB(广告牌)问题

swift - 将代码从 swift 更改为 swift 2.0

iphone - 在各种队列中运行多个 NSTimer 实例

ios - tableview cell滑动时出现特殊jamton的问题如何解决?

ios - UIStepper 增量动态文本框

iOS:快速返回 nil 中的 dateformatter

c++ - GLEW 无法与 CMAKE 一起使用

ios - std::unique_ptr 作为 objective-c 中的 @property