Search code examples
objective-cswiftmacosgpucalayer

CALayer choose which GPU


On machines with multiple GPUs, is it possible to programatically tell CALayer which GPU to use?

I can't seem to find any information about this online or in Apple's docs. If you cannot choose, how is the appropriate GPU determined?


Solution

  • It looks like you need to use Metal to do your rendering within CoreAnimation. According to Apple's Documentation, you'll do this by referencing the CAMetalLayer class object, which inherits directly from CALayer. That information is laid out here:

    https://developer.apple.com/documentation/quartzcore/cametallayer

    There are a couple properties that are exposed by the CAMetalLayer, such as preferredDevice which Apple describes as:

    On systems with a single GPU, this method returns the default device object; see MTLCreateSystemDefaultDevice(). On systems with more than one GPU, this method returns the MTLDevice that was last used to composite and present the CAMetalLayer. This device object usually corresponds to the GPU associated with the screen that’s displaying the layer. If you set the layer’s device property to this device object, you reduce the number of cross-GPU texture copies that Core Animation must perform to present the layer’s contents onscreen.

    All of that being said, I think you need to take a look directly at the Metal documentation, which provides a pretty decent path forward for interacting with the GPU/eGPU. That can be found here:

    https://developer.apple.com/documentation/metal/mtldevice

    That link will take you to the protocol for interacting with the various GPU devices and it does discuss multiple GPUs in operation. Good luck!