Blender  V3.3
device.mm
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2021-2022 Blender Foundation */
3 
4 #ifdef WITH_METAL
5 
6 # include "device/metal/device.h"
8 
9 #endif
10 
11 #include "util/debug.h"
12 #include "util/set.h"
13 #include "util/system.h"
14 
16 
17 #ifdef WITH_METAL
18 
19 Device *device_metal_create(const DeviceInfo &info, Stats &stats, Profiler &profiler)
20 {
21  return new MetalDevice(info, stats, profiler);
22 }
23 
24 bool device_metal_init()
25 {
26  return true;
27 }
28 
30 {
31  auto usable_devices = MetalInfo::get_usable_devices();
32  /* Devices are numbered consecutively across platforms. */
33  set<string> unique_ids;
34  int device_index = 0;
35  for (id<MTLDevice> &device : usable_devices) {
36  /* Compute unique ID for persistent user preferences. */
37  string device_name = MetalInfo::get_device_name(device);
38 
39  string id = string("METAL_") + device_name;
40 
41  /* Hardware ID might not be unique, add device number in that case. */
42  if (unique_ids.find(id) != unique_ids.end()) {
43  id += string_printf("_ID_%d", device_index);
44  }
45  unique_ids.insert(id);
46 
47  /* Create DeviceInfo. */
48  DeviceInfo info;
49  info.type = DEVICE_METAL;
50  info.description = string_remove_trademark(string(device_name));
51 
52  info.num = device_index;
53  /* We don't know if it's used for display, but assume it is. */
54  info.display_device = true;
55  info.denoisers = DENOISER_NONE;
56  info.id = id;
57 
58  devices.push_back(info);
59  device_index++;
60  }
61 }
62 
64 {
65  string result = "";
66  auto allDevices = MTLCopyAllDevices();
67  uint32_t num_devices = (uint32_t)allDevices.count;
68  if (num_devices == 0) {
69  return "No Metal devices found\n";
70  }
71  result += string_printf("Number of devices: %u\n", num_devices);
72 
73  for (id<MTLDevice> device in allDevices) {
74  string device_name = MetalInfo::get_device_name(device);
75  result += string_printf("\t\tDevice: %s\n", device_name.c_str());
76  }
77 
78  return result;
79 }
80 
81 #else
82 
83 Device *device_metal_create(const DeviceInfo &info, Stats &stats, Profiler &profiler)
84 {
85  return nullptr;
86 }
87 
89 {
90  return false;
91 }
92 
94 {
95 }
96 
98 {
99  return "";
100 }
101 
102 #endif
103 
DenoiserTypeMask denoisers
Definition: device/device.h:73
bool display_device
Definition: device/device.h:66
DeviceType type
Definition: device/device.h:62
string description
Definition: device/device.h:63
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
@ DENOISER_NONE
Definition: denoise.h:17
@ DEVICE_METAL
Definition: device/device.h:43
CCL_NAMESPACE_BEGIN Device * device_metal_create(const DeviceInfo &info, Stats &stats, Profiler &profiler)
Definition: device.mm:83
string device_metal_capabilities()
Definition: device.mm:97
bool device_metal_init()
Definition: device.mm:88
void device_metal_info(vector< DeviceInfo > &devices)
Definition: device.mm:93
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:152
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition: string.cpp:22