Confusion About CIContext, OpenGL and Metal (SWIFT). Does CIContext use CPU or GPU by default?

浪子不回头ぞ 提交于 2019-12-06 14:12:27

I started making this a comment, but I think since WWDC'18 this works best as an answer. I'll edit as others more an expert than I comment, and am willing to delete the entire answer if that's the proper thing to do.

You are on the right track - utilize the GPU when you can and it's a good fit. CoreImage and Metal, while "low-level" technologies that "usually" use the GPU, can use the CPU if that is desired. CoreGraphics? It renders things using the GPU.

Images. A UIImage and a CGImage are actual images. A CIImage however, isn't. The best way to think of it is a "recipe" for an image.

I typically - for now, I'll explain in a moment - stick to CoreImage, CIFilters, CIImages, and GLKViews when working with filters. Using a GLKView against a CIImage means using OpenGL and a single CIContext and EAGLContext. It offers almost as good performance as using MetalKit or MTKViews.

As for using UIKit and it's UIImage and UIImageView, I only do when needed - saving/sharing/uploading, whatever. Stick to the GPU until then.

....

Here's where it starts getting complicated.

Metal is an Apple proprietary API. Since they own the hardware - including the CPU and GPU - they've optimized it for them. It's "pipeline" is somewhat different than OpenGL. Nothing major, just different.

Until WWDC'18, using GLKit, including GLKView, was fine. But all things OpenGL were depricated, and Apple is moving things to Metal. While the performance gain (for now) isn't that great, you may be best off for something new to use MTKView, Metal, and CIContext`.

Look at the answer @matt gave here for a nice way to use MTKViews.

Some independent points:

  • Profile your app to figure out where it's spending that CPU time.
  • If the graphics work is actually not very hard — that is, if your app isn't GPU bound — optimizing the GPU work may not help overall performance.
  • Try to avoid moving data back and forth between the CPU and GPU. Don't keep creating CGImages for each filter output. A major feature of Core Image is the ability to chain filters without doing rendering for each, then render all of the effects at once. Also, as dfd says in his answer, if you render directly to screen rather than creating a UIImage to display in an image view, that would be better.
  • Avoid redundant work. Don't recreate your CIFilter objects every time. If the parameters haven't changed, don't reconfigure them every time.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!