Blender V5.0
blender/device.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "blender/device.h"
6#include "blender/session.h"
7#include "blender/util.h"
8
10
21
22int blender_device_threads(BL::Scene &b_scene)
23{
24 BL::RenderSettings b_r = b_scene.render();
25
26 if (b_r.threads_mode() == BL::RenderSettings::threads_mode_FIXED) {
27 return b_r.threads();
28 }
29
30 return 0;
31}
32
34{
35 if (!get_boolean(cpreferences, "peer_memory")) {
36 info.has_peer_memory = false;
37 }
38
39 if (info.type == DEVICE_METAL) {
40 const MetalRTSetting use_metalrt = (MetalRTSetting)get_enum(
41 cpreferences, "metalrt", METALRT_NUM_SETTINGS, METALRT_AUTO);
42
44 if (use_metalrt == METALRT_OFF) {
45 info.use_hardware_raytracing = false;
46 }
47 else if (use_metalrt == METALRT_ON) {
48 info.use_hardware_raytracing = true;
49 }
50 }
51
52 if (info.type == DEVICE_ONEAPI && !get_boolean(cpreferences, "use_oneapirt")) {
53 info.use_hardware_raytracing = false;
54 }
55
56 if (info.type == DEVICE_HIP && !get_boolean(cpreferences, "use_hiprt")) {
57 info.use_hardware_raytracing = false;
58 }
59}
60
61static void adjust_device_info(DeviceInfo &device, PointerRNA cpreferences, bool preview)
62{
63 adjust_device_info_from_preferences(device, cpreferences);
64 for (DeviceInfo &info : device.multi_devices) {
65 adjust_device_info_from_preferences(info, cpreferences);
66 }
67
68 if (preview) {
69 /* Disable specialization for preview renders. */
71 }
72 else {
74 cpreferences,
75 "kernel_optimization_level",
78 }
79}
80
81DeviceInfo blender_device_info(BL::Preferences &b_preferences,
82 BL::Scene &b_scene,
83 bool background,
84 bool preview,
85 DeviceInfo &preferences_device)
86{
87 PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
88
89 /* Find cycles preferences. */
90 PointerRNA cpreferences;
91 for (BL::Addon &b_addon : b_preferences.addons) {
92 if (b_addon.module() == "cycles") {
93 cpreferences = b_addon.preferences().ptr;
94 break;
95 }
96 }
97
98 /* Default to CPU device. */
100
101 /* Device, which is chosen in the Blender Preferences. Default to CPU device. */
102 preferences_device = cpu_device;
103
104 /* Test if we are using GPU devices. */
105 const ComputeDevice compute_device = (ComputeDevice)get_enum(
106 cpreferences, "compute_device_type", COMPUTE_DEVICE_NUM, COMPUTE_DEVICE_CPU);
107
108 if (compute_device != COMPUTE_DEVICE_CPU) {
109 /* Query GPU devices with matching types. */
111 if (compute_device == COMPUTE_DEVICE_CUDA) {
113 }
114 else if (compute_device == COMPUTE_DEVICE_OPTIX) {
116 }
117 else if (compute_device == COMPUTE_DEVICE_HIP) {
119 }
120 else if (compute_device == COMPUTE_DEVICE_METAL) {
122 }
123 else if (compute_device == COMPUTE_DEVICE_ONEAPI) {
125 }
127
128 /* Match device preferences and available devices. */
129 vector<DeviceInfo> used_devices;
130 RNA_BEGIN (&cpreferences, device, "devices") {
131 if (get_boolean(device, "use")) {
132 const string id = get_string(device, "id");
133 for (const DeviceInfo &info : devices) {
134 if (info.id == id) {
135 used_devices.push_back(info);
136 break;
137 }
138 }
139 }
140 }
141 RNA_END;
142
143 if (!used_devices.empty()) {
144 const int threads = blender_device_threads(b_scene);
145 preferences_device = Device::get_multi_device(used_devices, threads, background);
146 }
147 }
148
149 adjust_device_info(preferences_device, cpreferences, preview);
150 adjust_device_info(cpu_device, cpreferences, preview);
151
152 /* Device, which will be used, according to Settings, Scene preferences and command line
153 * parameters. */
154 DeviceInfo device;
155
158
159 if (devices.empty()) {
160 device = Device::dummy_device("Found no Cycles device of the specified type");
161 }
162 else {
163 const int threads = blender_device_threads(b_scene);
164 device = Device::get_multi_device(devices, threads, background);
165 }
166 adjust_device_info(device, cpreferences, preview);
167 }
168 else {
169 /* 1 is a "GPU compute" in properties.py for Scene settings. */
170 if (get_enum(cscene, "device") == 1) {
171 device = preferences_device;
172 }
173 else {
174 device = cpu_device;
175 }
176 }
177
178 return device;
179}
180
unsigned int uint
#define RNA_BEGIN(sptr, itemptr, propname)
#define RNA_END
static void adjust_device_info_from_preferences(DeviceInfo &info, PointerRNA cpreferences)
DeviceInfo blender_device_info(BL::Preferences &b_preferences, BL::Scene &b_scene, bool background, bool preview, DeviceInfo &preferences_device)
ComputeDevice
@ COMPUTE_DEVICE_ONEAPI
@ COMPUTE_DEVICE_HIP
@ COMPUTE_DEVICE_METAL
@ COMPUTE_DEVICE_CUDA
@ COMPUTE_DEVICE_OPTIX
@ COMPUTE_DEVICE_NUM
@ COMPUTE_DEVICE_CPU
static void adjust_device_info(DeviceInfo &device, PointerRNA cpreferences, bool preview)
int blender_device_threads(BL::Scene &b_scene)
static DeviceTypeMask device_override
KernelOptimizationLevel kernel_optimization_level
vector< DeviceInfo > multi_devices
bool has_peer_memory
bool use_metalrt_by_default
DeviceType type
bool use_hardware_raytracing
static DeviceInfo dummy_device(const string &error_msg="")
static DeviceInfo get_multi_device(const vector< DeviceInfo > &subdevices, const int threads, bool background)
static vector< DeviceInfo > available_devices(const uint device_type_mask=DEVICE_MASK_ALL)
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)
#define CCL_NAMESPACE_END
KernelOptimizationLevel
@ KERNEL_OPTIMIZATION_LEVEL_OFF
@ KERNEL_OPTIMIZATION_LEVEL_FULL
@ KERNEL_OPTIMIZATION_NUM_LEVELS
@ DEVICE_MASK_OPTIX
@ DEVICE_MASK_CPU
@ DEVICE_MASK_HIP
@ DEVICE_MASK_ALL
@ DEVICE_MASK_CUDA
@ DEVICE_MASK_METAL
@ DEVICE_MASK_ONEAPI
MetalRTSetting
@ METALRT_OFF
@ METALRT_NUM_SETTINGS
@ METALRT_ON
@ METALRT_AUTO
@ DEVICE_METAL
@ DEVICE_HIP
@ DEVICE_ONEAPI
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
PointerRNA RNA_pointer_get(PointerRNA *ptr, const char *name)