关于 CIContext、OpenGL 和 Metal 的混淆 (SWIFT)。 CIContext 默认使用 CPU 还是 GPU?
Confusion About CIContext, OpenGL and Metal (SWIFT). Does CIContext use CPU or GPU by default?
所以我正在制作一个应用程序,其中一些主要功能围绕着将 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
}
差不多:
- 从 UiImg 创建 CgImg
- 从 CgImg 创建 CiImg
- 使用上下文应用过滤器并转换回 UiImg
- 使用新的 UiImg 更新任何视图
这在我的 iPhone X 上运行良好,在我的 iPhone 6 上也运行良好。由于我的应用程序已经非常完整,所以我希望尽可能地对其进行优化。我也浏览了很多关于使用 OpenGL 和 Metal 做事的文档,但似乎无法弄清楚如何开始。
我一直以为我是 运行 CPU 上的这些进程,但使用 OpenGL 和 Metal 创建上下文并没有提供任何改进。我是否需要使用 MetalKit 视图或 GLKit 视图(eaglContext 似乎已完全弃用)?我如何翻译这个? apple文档好像没啥亮点
我开始对此发表评论,但我认为自 WWDC'18 以来,这最适合作为答案。我会像其他人一样进行编辑,而不是我发表评论,如果这是正确的做法,我愿意删除整个答案。
您走在正确的轨道上 - 尽可能使用 GPU,它非常适合。 CoreImage 和 Metal,虽然 "usually" 使用 GPU 的 "low-level" 技术,但如果需要, 可以 使用 CPU。核心图形?它使用 GPU 渲染 东西。
图片。 UIImage
和 CGImage
是实际图像。然而,CIImage
不是。想到它的最佳方式是 "recipe" 图像。
我通常 - 现在,我稍后会解释 - 在使用过滤器时坚持使用 CoreImage、CIFilters
、CIImages 和 GLKViews
。针对 CIImage 使用 GLKView 意味着使用 OpenGL
和单个 CIContext
和 EAGLContext
。它提供 几乎 与使用 MetalKit
或 MTKViews
.
一样好的性能
至于使用UIKit
和UIImage
和UIImageView
,我只是在需要的时候才做——saving/sharing/uploading,随便。在那之前坚持使用 GPU。
.....
这里开始变得复杂。
Metal 是 Apple 专有的 API。由于他们拥有硬件 - 包括 CPU 和 GPU - 他们已经为他们优化了它。它 "pipeline" 与 OpenGL 有点不同。没什么大不了的,只是不同。
直到 WWDC'18,使用 GLKit
,包括 GLKView
都很好。但是 OpenGL 的所有东西都被贬低了,Apple 正在将东西转移到 Metal。虽然性能增益(目前)不是那么好,但您最好使用 MTKView、Metal 和 CIContext` 的新东西。
查看@matt 给出的答案 了解使用 MTKViews 的好方法。
一些独立的点:
- 分析您的应用,找出它在哪些地方花费了 CPU 时间。
- 如果图形工作实际上不是很困难——也就是说,如果您的应用程序不受 GPU 限制——优化 GPU 工作可能无助于提高整体性能。
- 尽量避免在 CPU 和 GPU 之间来回移动数据。不要为每个过滤器输出继续创建
CGImage
s。 Core Image 的一大特点是 ability to chain filters 无需对每个效果进行渲染,然后一次渲染所有效果。另外,正如 dfd 在他的回答中所说,如果您直接渲染到屏幕而不是创建一个 UIImage
以在图像视图中显示,那会更好。
- 避免重复工作。不要每次都重新创建
CIFilter
对象。如果参数没变,不要每次都重新配置。
所以我正在制作一个应用程序,其中一些主要功能围绕着将 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
}
差不多:
- 从 UiImg 创建 CgImg
- 从 CgImg 创建 CiImg
- 使用上下文应用过滤器并转换回 UiImg
- 使用新的 UiImg 更新任何视图
这在我的 iPhone X 上运行良好,在我的 iPhone 6 上也运行良好。由于我的应用程序已经非常完整,所以我希望尽可能地对其进行优化。我也浏览了很多关于使用 OpenGL 和 Metal 做事的文档,但似乎无法弄清楚如何开始。
我一直以为我是 运行 CPU 上的这些进程,但使用 OpenGL 和 Metal 创建上下文并没有提供任何改进。我是否需要使用 MetalKit 视图或 GLKit 视图(eaglContext 似乎已完全弃用)?我如何翻译这个? apple文档好像没啥亮点
我开始对此发表评论,但我认为自 WWDC'18 以来,这最适合作为答案。我会像其他人一样进行编辑,而不是我发表评论,如果这是正确的做法,我愿意删除整个答案。
您走在正确的轨道上 - 尽可能使用 GPU,它非常适合。 CoreImage 和 Metal,虽然 "usually" 使用 GPU 的 "low-level" 技术,但如果需要, 可以 使用 CPU。核心图形?它使用 GPU 渲染 东西。
图片。 UIImage
和 CGImage
是实际图像。然而,CIImage
不是。想到它的最佳方式是 "recipe" 图像。
我通常 - 现在,我稍后会解释 - 在使用过滤器时坚持使用 CoreImage、CIFilters
、CIImages 和 GLKViews
。针对 CIImage 使用 GLKView 意味着使用 OpenGL
和单个 CIContext
和 EAGLContext
。它提供 几乎 与使用 MetalKit
或 MTKViews
.
至于使用UIKit
和UIImage
和UIImageView
,我只是在需要的时候才做——saving/sharing/uploading,随便。在那之前坚持使用 GPU。
.....
这里开始变得复杂。
Metal 是 Apple 专有的 API。由于他们拥有硬件 - 包括 CPU 和 GPU - 他们已经为他们优化了它。它 "pipeline" 与 OpenGL 有点不同。没什么大不了的,只是不同。
直到 WWDC'18,使用 GLKit
,包括 GLKView
都很好。但是 OpenGL 的所有东西都被贬低了,Apple 正在将东西转移到 Metal。虽然性能增益(目前)不是那么好,但您最好使用 MTKView、Metal 和 CIContext` 的新东西。
查看@matt 给出的答案
一些独立的点:
- 分析您的应用,找出它在哪些地方花费了 CPU 时间。
- 如果图形工作实际上不是很困难——也就是说,如果您的应用程序不受 GPU 限制——优化 GPU 工作可能无助于提高整体性能。
- 尽量避免在 CPU 和 GPU 之间来回移动数据。不要为每个过滤器输出继续创建
CGImage
s。 Core Image 的一大特点是 ability to chain filters 无需对每个效果进行渲染,然后一次渲染所有效果。另外,正如 dfd 在他的回答中所说,如果您直接渲染到屏幕而不是创建一个UIImage
以在图像视图中显示,那会更好。 - 避免重复工作。不要每次都重新创建
CIFilter
对象。如果参数没变,不要每次都重新配置。