1// 2// Copyright 2022 The ANGLE Project Authors. All rights reserved. 3// Use of this source code is governed by a BSD-style license that can be 4// found in the LICENSE file. 5// 6 7#include "common/apple_platform_utils.h" 8 9#include <Metal/Metal.h> 10 11namespace angle 12{ 13 14bool IsMetalRendererAvailable() 15{ 16 static bool queriedSystemDevice = false; 17 static bool gpuFamilySufficient = false; 18 19 // We only support macos 10.13+ and 11 for now. Since they are requirements for Metal 2.0. 20#if TARGET_OS_SIMULATOR 21 if (ANGLE_APPLE_AVAILABLE_XCI(10.13, 13.1, 13)) 22#else 23 if (ANGLE_APPLE_AVAILABLE_XCI(10.13, 13.1, 11)) 24#endif 25 { 26 if (!queriedSystemDevice) 27 { 28 ANGLE_APPLE_OBJC_SCOPE 29 { 30 queriedSystemDevice = true; 31 auto device = [MTLCreateSystemDefaultDevice() ANGLE_APPLE_AUTORELEASE]; 32 if (!device) 33 { 34 return false; 35 } 36 37 // -[MTLDevice supportsFamily] introduced in macOS 10.15, Catalyst 13.1, iOS 13. 38#if defined(ANGLE_PLATFORM_MACOS) || defined(ANGLE_PLATFORM_MACCATALYST) 39 // Old Macs, such as MacBookPro11,4, cannot use ANGLE's Metal backend. 40 // This check can be removed once they are no longer supported. 41 if (ANGLE_APPLE_AVAILABLE_XCI(10.15, 13.1, 13)) 42 { 43 if ([device supportsFamily:MTLGPUFamilyMac2]) 44 gpuFamilySufficient = true; 45 } 46 else 47 { 48 // Hardcode constant to sidestep compiler errors. Call will 49 // return false on older macOS versions. 50 const NSUInteger macFamily2v1 = 10005; 51 ANGLE_APPLE_ALLOW_DEPRECATED_BEGIN 52 if ([device supportsFeatureSet:static_cast<MTLFeatureSet>(macFamily2v1)]) 53 gpuFamilySufficient = true; 54 ANGLE_APPLE_ALLOW_DEPRECATED_END 55 } 56#elif ANGLE_PLATFORM_IOS_FAMILY && !ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR 57 // Hardcode constant to sidestep compiler errors. Call will 58 // return false on older macOS versions. 59 const NSUInteger iosFamily3v1 = 4; 60 if ([device supportsFeatureSet:static_cast<MTLFeatureSet>(iosFamily3v1)]) 61 gpuFamilySufficient = true; 62#elif ANGLE_PLATFORM_IOS_FAMILY && ANGLE_PLATFORM_IOS_FAMILY_SIMULATOR 63 // FIXME: Currently we do not have good simulator query, as it does not support 64 // the whole feature set needed for iOS. 65 gpuFamilySufficient = true; 66#endif 67 } 68 } 69 70 return gpuFamilySufficient; 71 } 72 return false; 73} 74 75} // namespace angle 76