Pono Pono - 1 month ago 23
Swift Question

Using EAGLContext with CIContext on MacOS

I have a simple Swift command-line MacOS application and I'm struggling to set

EAGLContext
:

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


The above code gives me:

Use of unresolved identifier 'EAGLContext'


no matter what modules I load:

import CoreImage
import OpenGL
import QuartzCore
import GLKit


Now the question is: does
CIContext
use OpenGL rendering by default?

When I initialize a new
CIContext
without any options:

let context = CIContext()


and set env variable
CI_PRINT_TREE
to
1
the console output gives me:

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


so it does process my filter in the GPU context, right?. Is there a way to explicitly set GPU rendering or is GPU a default context?

Answer

According to Docs

There is a way to initialize CIContext on CPU or GPU explicitly

Also you can specify Metal or OpenGL

Creating a Context for CPU-Based Rendering

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

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

Creating a Context for GPU-Based Rendering with OpenGL:

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.

Creating a Context for GPU-Based Rendering with Metal

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.