在 MacOS 上使用 EAGLContext 和 CIContext

Using EAGLContext with CIContext on MacOS

我有一个简单的 Swift 命令行 MacOS 应用程序,我正在努力设置 EAGLContext:

let openGLContext = EAGLContext(API: .OpenGLES3)
let context = CIContext(EAGLContext: openGLContext)

上面的代码给我:

Use of unresolved identifier 'EAGLContext'

无论我加载什么模块:

import CoreImage
import OpenGL
import QuartzCore
import GLKit

现在的问题是:CIContext默认使用OpenGL渲染吗?

当我在没有任何选项的情况下初始化一个新的 CIContext 时:

let context = CIContext()

并将环境变量 CI_PRINT_TREE 设置为 1 控制台输出给我:

initial graph image_get_cgimage (opengl context 1 frame 1)
                                 ^^^^^^^^^^^^^^^^^^^^^^^^

所以它确实在 GPU 上下文中处理我的过滤器,对吧?。有没有办法显式设置 GPU 渲染或 GPU 是默认上下文?

According to Docs

有一种方法可以在 CPU 或 GPU 上显式初始化 CIContext

您也可以指定 Metal 或 OpenGL

为基于 CPU 的渲染创建上下文

init(cgContext: CGContext, options: [String : Any]? = nil)

Creates a Core Image context from a Quartz context, using the specified options.

使用 OpenGL 为基于 GPU 的渲染创建上下文:

init(cglContext: CGLContextObj, pixelFormat: CGLPixelFormatObj?, colorSpace: CGColorSpace?, options: [String : Any]? = nil)

Creates a Core Image context from a CGL context, using the specified options, color space, and pixel format object.

init(eaglContext: EAGLContext)

Creates a Core Image context from an EAGL context.

init(eaglContext: EAGLContext, options: [String : Any]? = nil)

Creates a Core Image context from an EAGL context using the specified options.

init?(forOfflineGPUAt: UInt32)

Creates an OpenGL-based Core Image context using a GPU that is not currently driving a display.

init?(forOfflineGPUAt: UInt32, colorSpace: CGColorSpace?, options: [String : Any]? = nil, sharedContext: CGLContextObj?)

Creates an OpenGL-based Core Image context using a GPU that is not currently driving a display, with the specified options.

使用 Metal 为基于 GPU 的渲染创建上下文

init(mtlDevice: MTLDevice)

Creates a Core Image context using the specified Metal device.

init(mtlDevice: MTLDevice, options: [String : Any]? = nil)

Creates a Core Image context using the specified Metal device and options.

刚刚在可用的 Apple 文档中找到了答案here:

Rendering with an Automatic Context

If you don’t have constraints on how your app interoperates with other graphics technologies, creating a Core Image context is simple: just use the basic init or initWithOptions: initializer. When you do so, Core Image automatically manages resources internally, choosing the appropriate or best available CPU or GPU rendering technology based on the current device and any options you specify.