IOS to identify run-time metal support?

I usually use the code below to identify the version of an iOS device.

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

Similarly, I am 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 exactly how I expect my code to work:

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

How do I get the value for a boolean variable MetalSupported ?

+8
source share
4 answers

It's good that you are looking for something specific for Metal - as a rule, iOS version checks and hardware name checks are fragile because they rely on your application to know all versions of the OS and devices that could ever run it. If Apple came back and released an iOS 7.x version that added Metal support (well, it seems unlikely), or a device that supports Metal, but is not one of the hardware names that you are looking at (it seems much more likely ).), you will be stuck with the need to track all of these things and update your application to manage them.

In any case, the best way to check if the device you are running on is enough for your awesome graphics code? Just try to get the MTLDevice object:

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

Please note that just checking for the presence of the Metal Framework class does not help - these classes are on any device running iOS 8 (up to iPhone 4s and iPad 2), regardless of whether this device has Metal support. GPU

In Simulator Metal, it has been supported since iOS 13 / tvOS 13 on macOS 10.15. Use the same strategy: call MTLCreateSystemDefaultDevice() . If it returns an object, then your simulator code works in an environment where the simulator accelerates hardware. If it returns nil then you are working on an older simulator or in an environment where Metal is not available.

+12
source

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

You should avoid using MTLCreateSystemDefaultDevice() on macOS, as this can cause the multiplexer to switch to a discrete graphics processor (for example, if you are dealing with a laptop with automatic graphics that switch between discrete and integrated graphics).

+7
source

Rickster clearly explained all the methods for identifying a device that supports metal at runtime. If you cannot use MTLCreateSystemDefaultDevice () in your class by including metal libraries, use device information (iOS version, gpu / cpu architecture), but you need to consider all the cases explained by Ricster when using device information.

 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 a subtype and enter a variable to identify the device and other information.

0
source

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"); } 
-2
source

All Articles