简体   繁体   English

iOS代码在运行时识别金属支持?

[英]iOS code to identify metal support in runtime?

Usually, I use the below code to identify the iOS version of the device. 通常,我使用以下代码来识别设备的iOS版本。

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. 使用A7(或更好)GPU和iOS 8.0的Apple设备支持Metal。

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 ? 如何获取布尔变量MetalSupported的值?

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. 你正在寻找特定于Metal的东西是好的 - 通常,iOS版本检查和硬件名称检查是脆弱的,因为它们依赖于你的应用程序知道所有可以运行它的操作系统版本和设备。 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. 如果Apple要回去发布一个增加了Metal支持的iOS 7.x版本(好吧,似乎不太可能),或者支持Metal的设备,但不是你正在寻找的硬件名称之一(似乎更有可能) ),你将无法跟踪所有这些事情,并更新你的应用程序来管理它们。

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: 只是尝试获取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. 请注意,只测试Metal框架类的存在并没有帮助 - 这些类在任何运行iOS 8的设备上都存在(一直回到iPhone 4s和iPad 2),无论该设备是否具有Metal功能GPU。

In Simulator, Metal is supported as of iOS 13 / tvOS 13 when running on macOS 10.15. 在Simulator中,当在macOS 10.15上运行时,从iOS 13 / tvOS 13开始支持Metal。 Use the same strategy: call MTLCreateSystemDefaultDevice() . 使用相同的策略:调用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. 如果它返回nil那么您将在较旧的模拟器上运行,或者在Metal不可用的环境中运行。

On iOS, you should just check MTLCreateSystemDefaultDevice() . 在iOS上,您应该只检查MTLCreateSystemDefaultDevice() If it returns a valid device, you're good to go. 如果它返回一个有效的设备,你很高兴。 On macOS, you need to be careful; 在macOS上,你需要小心; use [MTLCopyAllDevices() count] to determine if you have any supported metal devices available. 使用[MTLCopyAllDevices() count]确定您是否有任何支持的金属设备可用。

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). 您应该避免在macOS上使用MTLCreateSystemDefaultDevice() ,因为这会强制多路复用开关切换到离散GPU(例如:如果您正在处理具有离散和集成图形之间自动图形切换的笔记本电脑)。

Ricster explained clearly about all the methods to identify the device which supports metal at runtime. Ricster清楚地解释了在运行时识别支持金属的设备的所有方法。 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. 如果你不能通过包含金属库在你的类中使用MTLCreateSystemDefaultDevice(),请使用设备信息(iOS版本,gpu / cpu架构),但是在使用设备信息时需要考虑Ricster解释的所有情况。

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. 使用Subtype和type变量来标识设备和其他信息。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM