Blender  V3.3
blender/device.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #include "blender/device.h"
5 #include "blender/session.h"
6 #include "blender/util.h"
7 
8 #include "util/foreach.h"
9 
11 
19 
21 };
22 
24 {
25  BL::RenderSettings b_r = b_scene.render();
26 
27  if (b_r.threads_mode() == BL::RenderSettings::threads_mode_FIXED)
28  return b_r.threads();
29  else
30  return 0;
31 }
32 
33 DeviceInfo blender_device_info(BL::Preferences &b_preferences, BL::Scene &b_scene, bool background)
34 {
35  PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
36 
37  /* Find cycles preferences. */
38  PointerRNA cpreferences;
39  for (BL::Addon &b_addon : b_preferences.addons) {
40  if (b_addon.module() == "cycles") {
41  cpreferences = b_addon.preferences().ptr;
42  break;
43  }
44  }
45 
46  /* Default to CPU device. */
48 
51 
52  if (devices.empty()) {
53  device = Device::dummy_device("Found no Cycles device of the specified type");
54  }
55  else {
56  int threads = blender_device_threads(b_scene);
57  device = Device::get_multi_device(devices, threads, background);
58  }
59  }
60  else if (get_enum(cscene, "device") == 1) {
61  /* Test if we are using GPU devices. */
62  ComputeDevice compute_device = (ComputeDevice)get_enum(
63  cpreferences, "compute_device_type", COMPUTE_DEVICE_NUM, COMPUTE_DEVICE_CPU);
64 
65  if (compute_device != COMPUTE_DEVICE_CPU) {
66  /* Query GPU devices with matching types. */
68  if (compute_device == COMPUTE_DEVICE_CUDA) {
70  }
71  else if (compute_device == COMPUTE_DEVICE_OPTIX) {
73  }
74  else if (compute_device == COMPUTE_DEVICE_HIP) {
76  }
77  else if (compute_device == COMPUTE_DEVICE_METAL) {
79  }
80  else if (compute_device == COMPUTE_DEVICE_ONEAPI) {
82  }
84 
85  /* Match device preferences and available devices. */
86  vector<DeviceInfo> used_devices;
87  RNA_BEGIN (&cpreferences, device, "devices") {
88  if (get_boolean(device, "use")) {
89  string id = get_string(device, "id");
90  foreach (DeviceInfo &info, devices) {
91  if (info.id == id) {
92  used_devices.push_back(info);
93  break;
94  }
95  }
96  }
97  }
98  RNA_END;
99 
100  if (!used_devices.empty()) {
101  int threads = blender_device_threads(b_scene);
102  device = Device::get_multi_device(used_devices, threads, background);
103  }
104  /* Else keep using the CPU device that was set before. */
105  }
106  }
107 
108  if (!get_boolean(cpreferences, "peer_memory")) {
109  device.has_peer_memory = false;
110  }
111 
112  if (get_boolean(cpreferences, "use_metalrt")) {
113  device.use_metalrt = true;
114  }
115 
116  return device;
117 }
118 
unsigned int uint
Definition: BLI_sys_types.h:67
struct Scene Scene
#define RNA_BEGIN(sptr, itemptr, propname)
Definition: RNA_access.h:543
#define RNA_END
Definition: RNA_access.h:550
ComputeDevice
@ COMPUTE_DEVICE_ONEAPI
@ COMPUTE_DEVICE_HIP
@ COMPUTE_DEVICE_METAL
@ COMPUTE_DEVICE_CUDA
@ COMPUTE_DEVICE_OPTIX
@ COMPUTE_DEVICE_NUM
@ COMPUTE_DEVICE_CPU
int blender_device_threads(BL::Scene &b_scene)
DeviceInfo blender_device_info(BL::Preferences &b_preferences, BL::Scene &b_scene, bool background)
static DeviceTypeMask device_override
bool has_peer_memory
Definition: device/device.h:70
bool use_metalrt
Definition: device/device.h:72
static DeviceInfo dummy_device(const string &error_msg="")
static vector< DeviceInfo > available_devices(uint device_type_mask=DEVICE_MASK_ALL)
static DeviceInfo get_multi_device(const vector< DeviceInfo > &subdevices, int threads, bool background)
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
static bool get_boolean(PointerRNA &ptr, const char *name)
static int get_enum(PointerRNA &ptr, const char *name, int num_values=-1, int default_value=-1)
static string get_string(PointerRNA &ptr, const char *name)
@ DEVICE_MASK_OPTIX
Definition: device/device.h:51
@ DEVICE_MASK_CPU
Definition: device/device.h:49
@ DEVICE_MASK_HIP
Definition: device/device.h:52
@ DEVICE_MASK_ALL
Definition: device/device.h:55
@ DEVICE_MASK_CUDA
Definition: device/device.h:50
@ DEVICE_MASK_METAL
Definition: device/device.h:53
@ DEVICE_MASK_ONEAPI
Definition: device/device.h:54
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
Vector< CPUDevice > devices
list of all CPUDevices. for every hardware thread an instance of CPUDevice is created
ListBase threads
list of all thread for every CPUDevice in cpudevices a thread exists.
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:5167