13

通常,我使用下面的代码来识别设备的 iOS 版本。

if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)

以类似的方式,我正在尝试为该设备找到 Metal 支持。配备 A7(或更好)GPU 和 iOS 8.0 的 Apple 设备支持 Metal。

这是我希望我的代码工作的方式:

if (MetalSupported == true) {
  // metal programming
} else {
  // opengles2 programming
}

如何获取布尔变量的值MetalSupported

4

4 回答 4

18

您正在寻找特定于 Metal 的东西,这很好——通常,iOS 版本检查和硬件名称检查是脆弱的,因为它们依赖于您的应用了解所有可以运行它的操作系统版本和设备。如果 Apple 要返回并发布添加了 Metal 支持的 iOS 7.x 版本(好吧,似乎不太可能),或者支持 Metal 但不是您正在查看的硬件名称之一的设备(似乎更有可能),您将不得不跟踪所有这些事情并更新您的应用程序来管理它们。

无论如何,检查您正在运行的设备是否足以满足您出色的图形代码的最佳方法?只需尝试获取一个MTLDevice对象:

id<MTLDevice> device = MTLCreateSystemDefaultDevice();
if (device) {
    // ready to rock 
} else {
    // back to OpenGL
}

Note that just testing for the presence of a Metal framework class doesn't help — those classes are there on any device running iOS 8 (all the way back to iPhone 4s & iPad 2), regardless of whether that device has a Metal-capable GPU.

In Simulator, Metal is supported as of iOS 13 / tvOS 13 when running on macOS 10.15. Use the same strategy: call MTLCreateSystemDefaultDevice(). If it returns an object then your simulator code is running in an environment where the simulator is hardware-accelerated. If it returns nil then you're running on an older simulator or in an environment where Metal is not available.

于 2015-04-27T18:55:31.913 回答
8

On iOS, you should just check MTLCreateSystemDefaultDevice(). If it returns a valid device, you’re good to go. On macOS, you need to be careful; use [MTLCopyAllDevices() count] to determine if you have any supported metal devices available.

You should avoid using MTLCreateSystemDefaultDevice() on macOS because that can force a mux switch to the discrete GPU (eg: if you're dealing with a laptop with automatic graphics switching between a discrete and integrated graphics).

于 2016-11-08T11:14:43.763 回答
0

Ricster explained clearly about all the methods to identify the device which supports metal at runtime. If you cannot use MTLCreateSystemDefaultDevice() in your class by including metal libraries, use device informations(iOS version,gpu/cpu architecture),but you need to consider all the cases explained by Ricster when using device informations.

void deviceConfigurations(){
        size_t size;
        cpu_type_t type;
        cpu_subtype_t subtype;
        size = sizeof(type);
        sysctlbyname("hw.cputype", &type, &size, NULL, 0);

        size = sizeof(subtype);
        sysctlbyname("hw.cpusubtype", &subtype, &size, NULL, 0);
}

Use Subtype and type variable to identify device and other informations.

于 2015-05-05T07:02:26.067 回答
-2

我认为最好的方法是尝试获得金属类之一。

Class metalDeviceClass = NSClassFromString(@"MTLDevice");
BOOL isMetalAvailable = metalDeviceClass != nil;
if (isMetalAvailable) {
    NSLog(@"Metal available");
} else {
    NSLog(@"Metal not available");
}
于 2015-04-22T07:57:32.577 回答