Blender V5.3
util.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifdef WITH_METAL
6
7# include "device/metal/util.h"
8# include "util/string.h"
9
10# include <IOKit/IOKitLib.h>
11
13
14string MetalInfo::get_device_name(id<MTLDevice> device)
15{
16 string device_name = [device.name UTF8String];
17
18 /* Append the GPU core count so we can distinguish between GPU variants in benchmarks. */
19 int gpu_core_count = get_apple_gpu_core_count(device);
20 device_name += string_printf(gpu_core_count ? " (GPU - %d cores)" : " (GPU)", gpu_core_count);
21
22 return device_name;
23}
24
25int MetalInfo::get_apple_gpu_core_count(id<MTLDevice> device)
26{
27 int core_count = 0;
28 io_service_t gpu_service = IOServiceGetMatchingService(
29 kIOMainPortDefault, IORegistryEntryIDMatching(device.registryID));
30 if (CFNumberRef numberRef = (CFNumberRef)IORegistryEntryCreateCFProperty(
31 gpu_service, CFSTR("gpu-core-count"), nullptr, 0))
32 {
33 if (CFGetTypeID(numberRef) == CFNumberGetTypeID()) {
34 CFNumberGetValue(numberRef, kCFNumberSInt32Type, &core_count);
35 }
36 CFRelease(numberRef);
37 }
38 return core_count;
39}
40
41AppleGPUArchitecture MetalInfo::get_apple_gpu_architecture(id<MTLDevice> device)
42{
43 const char *device_name = [device.name UTF8String];
44 if (strstr(device_name, "M1")) {
45 return APPLE_M1;
46 }
47 if (strstr(device_name, "M2")) {
48 return get_apple_gpu_core_count(device) <= 10 ? APPLE_M2 : APPLE_M2_BIG;
49 }
50 if (strstr(device_name, "M3")) {
51 return APPLE_M3;
52 }
53 return APPLE_UNKNOWN;
54}
55
56int MetalInfo::optimal_sort_partition_elements()
57{
58 if (auto *str = getenv("CYCLES_METAL_SORT_PARTITION_ELEMENTS")) {
59 return atoi(str);
60 }
61
62 /* On M1 and M2 GPUs, we see better cache utilization if we partition the active indices before
63 * sorting each partition by material. Partitioning into chunks of 65536 elements results in an
64 * overall render time speedup of up to 15%. */
65
66 return 65536;
67}
68
69const vector<id<MTLDevice>> &MetalInfo::get_usable_devices()
70{
71 static vector<id<MTLDevice>> usable_devices;
72 static bool already_enumerated = false;
73
74 if (already_enumerated) {
75 return usable_devices;
76 }
77
78 metal_printf("Usable Metal devices:");
79 for (id<MTLDevice> device in MTLCopyAllDevices()) {
80 string device_name = get_device_name(device);
81 bool usable = false;
82
83 /* NOTE: This @available check sets the Cycles minimum macOS version. */
84 if (@available(macos 13.0, *)) {
85 const char *device_name_char = [device.name UTF8String];
86 if (!(strstr(device_name_char, "Intel") || strstr(device_name_char, "AMD")) &&
87 strstr(device_name_char, "Apple"))
88 {
89 /* TODO: Implement a better way to identify device vendor instead of relying on name. */
90 /* We only support Apple Silicon GPUs which all have unified memory, but explicitly check
91 * just in case it ever changes. */
92 usable = [device hasUnifiedMemory];
93 }
94 }
95
96 if (usable) {
97 metal_printf("- %s", device_name.c_str());
98 [device retain];
99 usable_devices.push_back(device);
100 }
101 else {
102 metal_printf(" (skipping \"%s\")", device_name.c_str());
103 }
104 }
105 if (usable_devices.empty()) {
106 metal_printf(" No usable Metal devices found");
107 }
108 already_enumerated = true;
109
110 return usable_devices;
111}
112
114
115#endif /* WITH_METAL */
#define CCL_NAMESPACE_END
#define str(s)
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25