From 0ecfaf3b9ee25f2dc110a0ce424ecafb4f2cac25 Mon Sep 17 00:00:00 2001
From: Pierre de La Morinerie <kemenaran@gmail.com>
Date: Wed, 22 Nov 2023 22:59:35 +0100
Subject: [PATCH] Metal: enable concurrent compilation only if available
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Bug: https://bugs.dolphin-emu.org/issues/13404

On macOS 13.6 / Intel HD 5000, Dolphin crashes with this message:

> -[MTLIGAccelDevice setShouldMaximizeConcurrentCompilation:]: unrecognized selector

This should be available on all macOS 13.3+ systems – but when using OCLP drivers,
some devices use an older version of Metal.framework, which doesn't expose the selector.

This concerns Intel Ivy Bridge, Haswell and Nvidia Kepler when using OCLP on macOS 13.3
or newer.
(See
https://github.com/dortania/OpenCore-Legacy-Patcher/blob/34676702f494a2a789c514cc76dba19b8b7206b1/docs/PATCHEXPLAIN.md?plain=1#L354C1-L354C83)

As the behavior is an optional optimization anyway, perform a dynamic
detection to avoid crashing if the feature is not available.
---
 Source/Core/VideoBackends/Metal/MTLMain.mm | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/Source/Core/VideoBackends/Metal/MTLMain.mm b/Source/Core/VideoBackends/Metal/MTLMain.mm
index 740b8c8a9c..cddb8f1c7c 100644
--- a/Source/Core/VideoBackends/Metal/MTLMain.mm
+++ b/Source/Core/VideoBackends/Metal/MTLMain.mm
@@ -96,8 +96,23 @@ bool Metal::VideoBackend::Initialize(const WindowSystemInfo& wsi)
     Util::PopulateBackendInfoFeatures(&g_Config, adapter);
 
 #if TARGET_OS_OSX
-    if (@available(macOS 13.3, *))
+// This should be available on all macOS 13.3+ systems – but when using OCLP drivers, some devices
+// fail with "Unrecognized selector -[MTLIGAccelDevice setShouldMaximizeConcurrentCompilation:]"
+//
+// This concerns Intel Ivy Bridge, Haswell and Nvidia Kepler on macOS 13.3 or newer.
+// (See
+// https://github.com/dortania/OpenCore-Legacy-Patcher/blob/34676702f494a2a789c514cc76dba19b8b7206b1/docs/PATCHEXPLAIN.md?plain=1#L354C1-L354C83)
+//
+// Perform the feature detection dynamically instead.
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wunguarded-availability"
+
+    if ([adapter respondsToSelector:@selector(setShouldMaximizeConcurrentCompilation:)])
+    {
       [adapter setShouldMaximizeConcurrentCompilation:YES];
+    }
+
+#pragma clang diagnostic pop
 #endif
 
     UpdateActiveConfig();