如何在使用Metal API时请求使用集成GPU?
问题描述:
根据Apple文档,当为OSX应用程序的Info.plist文件添加值为“YES”(或true)的关键字“NSSupportsAutomaticGraphicsSwitching”时,将在双GPU系统上调用集成GPU(与独立GPU)。这对集成GPU非常有用 - 虽然性能较低 - 足以满足我们应用的需求,并且消耗更少的能源。如何在使用Metal API时请求使用集成GPU?
不幸的是,根据上面的介绍和随后检查活动监视器(能源标签:“需要高性能GPU”栏)显示,尽管请求集成GPU,但我的支持Metal API的应用程序仍然使用独立GPU。
有什么办法可以给金属系统本身提示使用集成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:
方法。