Blender V4.3
device.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/device.h"
10
11#endif
12
13#include "util/debug.h"
14#include "util/set.h"
15#include "util/system.h"
16
18
19#ifdef WITH_METAL
20
22 Stats &stats,
23 Profiler &profiler,
24 bool headless)
25{
26 return new MetalDevice(info, stats, profiler, headless);
27}
28
30{
31 return true;
32}
33
35{
36 MetalDeviceKernels::static_deinitialize();
37}
38
40{
41 auto usable_devices = MetalInfo::get_usable_devices();
42 /* Devices are numbered consecutively across platforms. */
43 set<string> unique_ids;
44 int device_index = 0;
45 for (id<MTLDevice> &device : usable_devices) {
46 /* Compute unique ID for persistent user preferences. */
47 string device_name = MetalInfo::get_device_name(device);
48
49 string id = string("METAL_") + device_name;
50
51 /* Hardware ID might not be unique, add device number in that case. */
52 if (unique_ids.find(id) != unique_ids.end()) {
53 id += string_printf("_ID_%d", device_index);
54 }
55 unique_ids.insert(id);
56
57 /* Create DeviceInfo. */
58 DeviceInfo info;
59 info.type = DEVICE_METAL;
60 info.description = string_remove_trademark(string(device_name));
61
62 info.num = device_index;
63 /* We don't know if it's used for display, but assume it is. */
64 info.display_device = true;
66 info.id = id;
67# if defined(WITH_OPENIMAGEDENOISE)
68# if OIDN_VERSION >= 20300
69 if (oidnIsMetalDeviceSupported(device)) {
70# else
71 if (OIDNDenoiserGPU::is_device_supported(info)) {
72# endif
74 }
75# endif
76
77 info.has_nanovdb = true;
78
79 /* MNEE caused "Compute function exceeds available temporary registers" in macOS < 13 due to a
80 * bug in spill buffer allocation sizing. */
81 info.has_mnee = false;
82 if (@available(macos 13.0, *)) {
83 info.has_mnee = true;
84 }
85
86 info.use_hardware_raytracing = false;
87
88 /* MetalRT now uses features exposed in Xcode versions corresponding to macOS 14+, so don't
89 * expose it in builds from older Xcode versions. */
90# if defined(MAC_OS_VERSION_14_0)
91 if (@available(macos 14.0, *)) {
92 info.use_hardware_raytracing = device.supportsRaytracing;
93
94 /* Use hardware raytracing for faster rendering on architectures that support it. */
95 info.use_metalrt_by_default = device.supportsRaytracing &&
96 (MetalInfo::get_apple_gpu_architecture(device) >= APPLE_M3);
97 }
98# endif
99
100 devices.push_back(info);
101 device_index++;
102
103 VLOG_INFO << "Added device \"" << info.description << "\" with id \"" << info.id << "\".";
104
106 VLOG_INFO << "Device with id \"" << info.id << "\" supports "
108 }
109}
110
112{
113 string result = "";
114 auto allDevices = MTLCopyAllDevices();
115 uint32_t num_devices = (uint32_t)allDevices.count;
116 if (num_devices == 0) {
117 return "No Metal devices found\n";
118 }
119 result += string_printf("Number of devices: %u\n", num_devices);
120
121 for (id<MTLDevice> device in allDevices) {
122 string device_name = MetalInfo::get_device_name(device);
123 result += string_printf("\t\tDevice: %s\n", device_name.c_str());
124 }
125
126 return result;
127}
128
129#else
130
131Device *device_metal_create(const DeviceInfo &info, Stats &stats, Profiler &profiler)
132{
133 return nullptr;
134}
135
137{
138 return false;
139}
140
142
144{
145 return "";
146}
147
148#endif
149
DenoiserTypeMask denoisers
bool display_device
bool use_metalrt_by_default
DeviceType type
string description
bool use_hardware_raytracing
CCL_NAMESPACE_BEGIN const char * denoiserTypeToHumanReadable(DenoiserType type)
Definition denoise.cpp:9
@ DENOISER_NONE
Definition denoise.h:18
@ DENOISER_OPENIMAGEDENOISE
Definition denoise.h:15
#define CCL_NAMESPACE_END
@ DEVICE_METAL
void device_metal_exit()
string device_metal_capabilities()
Definition device.mm:143
CCL_NAMESPACE_BEGIN Device * device_metal_create(const DeviceInfo &info, Stats &stats, Profiler &profiler)
Definition device.mm:131
bool device_metal_init()
Definition device.mm:136
void device_metal_info(vector< DeviceInfo > &devices)
Definition device.mm:141
#define VLOG_INFO
Definition log.h:72
Vector< CPUDevice > devices
list of all CPUDevices. for every hardware thread an instance of CPUDevice is created
unsigned int uint32_t
Definition stdint.h:80
string string_remove_trademark(const string &s)
Definition string.cpp:155
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:23