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