2023-12-29 15:12:41 +08:00
|
|
|
#include "gpu.h"
|
|
|
|
|
|
|
|
|
|
#import <Metal/MTLDevice.h>
|
2024-09-26 16:43:54 +08:00
|
|
|
#import <IOKit/kext/KextManager.h>
|
2023-12-29 15:12:41 +08:00
|
|
|
|
2025-06-17 14:07:18 +08:00
|
|
|
#ifndef MAC_OS_VERSION_26_0
|
|
|
|
|
#define MTLGPUFamilyMetal4 ((MTLGPUFamily) 5002)
|
|
|
|
|
#endif
|
2023-12-29 15:12:41 +08:00
|
|
|
#ifndef MAC_OS_VERSION_13_0
|
|
|
|
|
#define MTLGPUFamilyMetal3 ((MTLGPUFamily) 5001)
|
|
|
|
|
#endif
|
2024-05-19 09:31:08 +08:00
|
|
|
#ifndef MAC_OS_X_VERSION_10_15
|
|
|
|
|
#define MTLFeatureSet_macOS_GPUFamily1_v4 ((MTLFeatureSet) 10004)
|
|
|
|
|
#define MTLFeatureSet_macOS_GPUFamily2_v1 ((MTLFeatureSet) 10005)
|
|
|
|
|
#endif
|
2023-12-29 15:12:41 +08:00
|
|
|
|
2024-09-26 16:43:54 +08:00
|
|
|
const char* ffGpuDetectDriverVersion(FFlist* gpus)
|
|
|
|
|
{
|
|
|
|
|
if (@available(macOS 10.7, *))
|
|
|
|
|
{
|
|
|
|
|
NSMutableArray* arr = NSMutableArray.new;
|
|
|
|
|
FF_LIST_FOR_EACH(FFGPUResult, x, *gpus)
|
|
|
|
|
[arr addObject:@(x->driver.chars)];
|
|
|
|
|
|
|
|
|
|
NSDictionary* dict = CFBridgingRelease(KextManagerCopyLoadedKextInfo((__bridge CFArrayRef)arr, (__bridge CFArrayRef)@[@"CFBundleVersion"]));
|
|
|
|
|
FF_LIST_FOR_EACH(FFGPUResult, x, *gpus)
|
|
|
|
|
{
|
|
|
|
|
NSString* version = dict[@(x->driver.chars)][@"CFBundleVersion"];
|
|
|
|
|
if (version)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufAppendC(&x->driver, ' ');
|
|
|
|
|
ffStrbufAppendS(&x->driver, version.UTF8String);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-25 09:19:32 +08:00
|
|
|
return NULL;
|
2024-09-26 16:43:54 +08:00
|
|
|
}
|
|
|
|
|
return "Unsupported macOS version";
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 15:12:41 +08:00
|
|
|
const char* ffGpuDetectMetal(FFlist* gpus)
|
|
|
|
|
{
|
2024-05-19 09:31:08 +08:00
|
|
|
if (@available(macOS 10.13, *))
|
2023-12-29 15:12:41 +08:00
|
|
|
{
|
|
|
|
|
for (id<MTLDevice> device in MTLCopyAllDevices())
|
|
|
|
|
{
|
|
|
|
|
FFGPUResult* gpu = NULL;
|
|
|
|
|
FF_LIST_FOR_EACH(FFGPUResult, x, *gpus)
|
|
|
|
|
{
|
2024-01-11 09:53:20 +08:00
|
|
|
if (x->deviceId == device.registryID)
|
2023-12-29 15:12:41 +08:00
|
|
|
{
|
|
|
|
|
gpu = x;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!gpu) continue;
|
|
|
|
|
|
2024-05-19 09:31:08 +08:00
|
|
|
#ifndef MAC_OS_X_VERSION_10_15
|
|
|
|
|
if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily2_v1])
|
|
|
|
|
ffStrbufSetStatic(&gpu->platformApi, "Metal Feature Set 2");
|
|
|
|
|
else if ([device supportsFeatureSet:MTLFeatureSet_macOS_GPUFamily1_v1])
|
|
|
|
|
ffStrbufSetStatic(&gpu->platformApi, "Metal Feature Set 1");
|
|
|
|
|
#else // MAC_OS_X_VERSION_10_15
|
2025-07-28 15:39:35 +08:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
|
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
|
2025-06-17 14:07:18 +08:00
|
|
|
if ([device supportsFamily:MTLGPUFamilyMetal4])
|
|
|
|
|
ffStrbufSetStatic(&gpu->platformApi, "Metal 4");
|
|
|
|
|
else if ([device supportsFamily:MTLGPUFamilyMetal3])
|
2023-12-29 15:12:41 +08:00
|
|
|
ffStrbufSetStatic(&gpu->platformApi, "Metal 3");
|
2025-07-28 15:39:35 +08:00
|
|
|
#pragma clang diagnostic pop
|
2023-12-29 15:12:41 +08:00
|
|
|
else if ([device supportsFamily:MTLGPUFamilyCommon3])
|
|
|
|
|
ffStrbufSetStatic(&gpu->platformApi, "Metal Common 3");
|
|
|
|
|
else if ([device supportsFamily:MTLGPUFamilyCommon2])
|
|
|
|
|
ffStrbufSetStatic(&gpu->platformApi, "Metal Common 2");
|
|
|
|
|
else if ([device supportsFamily:MTLGPUFamilyCommon1])
|
|
|
|
|
ffStrbufSetStatic(&gpu->platformApi, "Metal Common 1");
|
|
|
|
|
|
2025-07-10 16:16:45 +08:00
|
|
|
gpu->type = device.hasUnifiedMemory ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE;
|
2024-09-19 15:38:43 +08:00
|
|
|
gpu->index = (uint32_t) device.locationNumber;
|
2025-02-05 14:06:06 +08:00
|
|
|
|
|
|
|
|
if (device.hasUnifiedMemory && device.recommendedMaxWorkingSetSize > 0)
|
|
|
|
|
gpu->shared.total = device.recommendedMaxWorkingSetSize;
|
2025-02-11 14:15:32 +08:00
|
|
|
#endif
|
2023-12-29 15:12:41 +08:00
|
|
|
}
|
2024-01-11 09:53:20 +08:00
|
|
|
return NULL;
|
2023-12-29 15:12:41 +08:00
|
|
|
}
|
2024-01-11 09:53:20 +08:00
|
|
|
return "Metal API is not supported by this macOS version";
|
2023-12-29 15:12:41 +08:00
|
|
|
}
|