iOS code to identify metal support in runtime?

2019-04-07 08:28发布

Usually, I use the below code to identify the iOS version of the device.

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

In a similar way, I'm trying to find Metal support for the device. Metal is supported for Apple devices with the A7 (or better) GPU and iOS 8.0.

This is the way I expect my code to work:

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

How do I get the value for the Boolean variable MetalSupported ?

4条回答
孤傲高冷的网名
2楼-- · 2019-04-07 09:17

I think the best way is to try to get one of the metal classes.

Class metalDeviceClass = NSClassFromString(@"MTLDevice");
BOOL isMetalAvailable = metalDeviceClass != nil;
if (isMetalAvailable) {
    NSLog(@"Metal available");
} else {
    NSLog(@"Metal not available");
}
查看更多
冷血范
3楼-- · 2019-04-07 09:21

It's good that you're looking for something specific to Metal — generally, iOS version checks and hardware name checks are fragile, because they rely on your app knowing all of the OS versions and devices that could ever run it. If Apple were to go back and release an iOS 7.x version that added Metal support (okay, seems unlikely), or a device that supports Metal but isn't one of the hardware names you're looking at (seems much more likely), you'd be stuck having to track all of those things down and update your app to manage them.

Anyway, the best way to check whether the device you're running on is Metal enough for your awesome graphics code? Just try to get a MTLDevice object:

id<MTLDevice> device = MTLCreateSystemDefaultDevice();
if (device) {
    // ready to rock                                                                     
查看更多
倾城 Initia
4楼-- · 2019-04-07 09:29

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.

查看更多
Animai°情兽
5楼-- · 2019-04-07 09:30

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).

查看更多
登录 后发表回答