使用 Metal API 时如何请求使用集成 GPU?

How to request use of integrated GPU when using Metal API?

根据 Apple 文档,将键 "NSSupportsAutomaticGraphicsSwitching" 的值 "YES"(或 true)添加到 OSX 应用程序的 Info.plist 文件时,集成 GPU将在双 GPU 系统(与独立 GPU 相对)上调用。这很有用,因为集成 GPU(虽然性能较低)足以满足我的应用程序的需求并且消耗更少的能量。

不幸的是,按照上述方法构建并随后检查 Activity 监视器(能量选项卡:"Requires High Perf GPU" 列)显示我启用了 Metal API 的应用程序仍在使用独立的 GPU,尽管请求集成 GPU。

有什么方法可以提示 Metal 系统本身使用集成 GPU 吗?

问题是 Metal API 默认使用独立 GPU。使用以下代码以及上面详述的正确 Info.plist 配置,会导致使用集成 GPU:

    NSArray<id<MTLDevice>> *devices = MTLCopyAllDevices();

    gpu_ = nil;

    // Low power device is sufficient - try to use it!
    for (id<MTLDevice> device in devices) {
        if (device.isLowPower) {
            gpu_ = device;
            break;
        }
    }

    // below: probably not necessary since there is always 
    // integrated GPU, but doesn't hurt.
    if (gpu_ == nil)
        gpu_ = MTLCreateSystemDefaultDevice();

如果您使用的是 MTKView,请记住将 gpu_ 传递给它的 initWithFrame:device: 方法。