Blender V5.3
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
9#include "BKE_scene.hh"
10#include "RNA_prototypes.hh"
11
13
24
26{
27 blender::RenderData &b_r = b_scene.r;
28 int threads_override = blender::BLI_system_num_threads_override_get();
29 if (threads_override > 0 || (b_r.mode & blender::R_FIXED_THREADS) != 0) {
30 return BKE_render_num_threads(&b_r);
31 }
32
33 return 0;
34}
35
37{
38 if (!get_boolean(cpreferences, "peer_memory")) {
39 info.has_peer_memory = false;
40 }
41
42 if (info.type == DEVICE_METAL) {
43 const MetalRTSetting use_metalrt = (MetalRTSetting)get_enum(
44 cpreferences, "metalrt", METALRT_NUM_SETTINGS, METALRT_AUTO);
45
47 if (use_metalrt == METALRT_OFF) {
48 info.use_hardware_raytracing = false;
49 }
50 else if (use_metalrt == METALRT_ON) {
51 info.use_hardware_raytracing = true;
52 }
53 }
54
55 if (info.type == DEVICE_ONEAPI && !get_boolean(cpreferences, "use_oneapirt")) {
56 info.use_hardware_raytracing = false;
57 }
58
59 if (info.type == DEVICE_HIP && !get_boolean(cpreferences, "use_hiprt")) {
60 info.use_hardware_raytracing = false;
61 }
62}
63
64static void adjust_device_info(DeviceInfo &device, blender::PointerRNA cpreferences, bool preview)
65{
66 adjust_device_info_from_preferences(device, cpreferences);
67 for (DeviceInfo &info : device.multi_devices) {
68 adjust_device_info_from_preferences(info, cpreferences);
69 }
70
71 if (preview) {
72 /* Disable specialization for preview renders. */
74 }
75 else {
77 cpreferences,
78 "kernel_optimization_level",
81 }
82}
83
85 blender::Scene &b_scene,
86 bool background,
87 bool preview,
88 DeviceInfo &preferences_device)
89{
90 blender::PointerRNA scene_rna_ptr = RNA_id_pointer_create(&b_scene.id);
91 blender::PointerRNA cscene = RNA_pointer_get(&scene_rna_ptr, "cycles");
92
93 /* Find cycles preferences. */
94 blender::PointerRNA cpreferences;
95 for (blender::bAddon &b_addon : b_preferences.addons) {
96 if (STREQ(b_addon.module, "cycles")) {
97 blender::PointerRNA addon_rna_ptr = RNA_pointer_create_discrete(
98 nullptr, blender::RNA_Addon, &b_addon);
99 cpreferences = RNA_pointer_get(&addon_rna_ptr, "preferences");
100 break;
101 }
102 }
103
104 /* Default to CPU device. */
106
107 /* Device, which is chosen in the Blender Preferences. Default to CPU device. */
108 preferences_device = cpu_device;
109
110 /* Test if we are using GPU devices. */
111 const ComputeDevice compute_device = (ComputeDevice)get_enum(
112 cpreferences, "compute_device_type", COMPUTE_DEVICE_NUM, COMPUTE_DEVICE_CPU);
113
114 if (compute_device != COMPUTE_DEVICE_CPU) {
115 /* Query GPU devices with matching types. */
116 uint mask = DEVICE_MASK_CPU;
117 if (compute_device == COMPUTE_DEVICE_CUDA) {
119 }
120 else if (compute_device == COMPUTE_DEVICE_OPTIX) {
122 }
123 else if (compute_device == COMPUTE_DEVICE_HIP) {
125 }
126 else if (compute_device == COMPUTE_DEVICE_METAL) {
128 }
129 else if (compute_device == COMPUTE_DEVICE_ONEAPI) {
131 }
133
134 /* Match device preferences and available devices. */
135 vector<DeviceInfo> used_devices;
137 for (RNA_collection_begin(&cpreferences, "devices", &rna_iter); rna_iter.valid;
138 RNA_property_collection_next(&rna_iter))
139 {
140 blender::PointerRNA device = rna_iter.ptr;
141 if (get_boolean(device, "use")) {
142 const string id = get_string(device, "id");
143 for (const DeviceInfo &info : devices) {
144 if (info.id == id) {
145 /* Devices not meeting driver requirements are still enumerated for UI reporting,
146 * but must not be added to the active render device list. */
147 if (info.meets_driver_requirement) {
148 used_devices.push_back(info);
149 }
150 break;
151 }
152 }
153 }
154 }
156
157 if (!used_devices.empty()) {
158 const int threads = blender_device_threads(b_scene);
159 preferences_device = Device::get_multi_device(used_devices, threads, background);
160 }
161 }
162
163 adjust_device_info(preferences_device, cpreferences, preview);
164 adjust_device_info(cpu_device, cpreferences, preview);
165
166 /* Device, which will be used, according to Settings, Scene preferences and command line
167 * parameters. */
168 DeviceInfo device;
169
171 const vector<DeviceInfo> available_devices = Device::available_devices(
173 vector<DeviceInfo> devices;
174 for (const DeviceInfo &info : available_devices) {
175 /* Devices not meeting driver requirements are still enumerated for UI reporting,
176 * but must not be added to the active render device list. */
177 if (info.meets_driver_requirement) {
178 devices.push_back(info);
179 }
180 }
181
182 if (devices.empty()) {
183 device = Device::dummy_device("Found no Cycles device of the specified type");
184 }
185 else {
186 const int threads = blender_device_threads(b_scene);
187 device = Device::get_multi_device(devices, threads, background);
188 }
189 adjust_device_info(device, cpreferences, preview);
190 }
191 else {
192 /* 1 is a "GPU compute" in properties.py for Scene settings. */
193 if (get_enum(cscene, "device") == 1) {
194 device = preferences_device;
195 }
196 else {
197 device = cpu_device;
198 }
199 }
200
201 return device;
202}
203
int blender_device_threads(blender::Scene &b_scene)
static void adjust_device_info_from_preferences(DeviceInfo &info, blender::PointerRNA cpreferences)
DeviceInfo blender_device_info(blender::UserDef &b_preferences, blender::Scene &b_scene, bool background, bool preview, DeviceInfo &preferences_device)
static void adjust_device_info(DeviceInfo &device, blender::PointerRNA cpreferences, bool preview)
ComputeDevice
@ COMPUTE_DEVICE_ONEAPI
@ COMPUTE_DEVICE_HIP
@ COMPUTE_DEVICE_METAL
@ COMPUTE_DEVICE_CUDA
@ COMPUTE_DEVICE_OPTIX
@ COMPUTE_DEVICE_NUM
@ COMPUTE_DEVICE_CPU
#define STREQ(a, b)
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)
#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)
void RNA_property_collection_end(CollectionPropertyIterator *iter)
int BLI_system_num_threads_override_get()
Definition threads.cc:299
struct RenderData r
ListBaseT< bAddon > addons