Merge branch 'blender2.7'
[blender.git] / intern / cycles / blender / blender_device.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "blender/blender_device.h"
18 #include "blender/blender_util.h"
19
20 CCL_NAMESPACE_BEGIN
21
22 int 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         else
29                 return 0;
30 }
31
32 DeviceInfo blender_device_info(BL::Preferences& b_preferences, BL::Scene& b_scene, bool background)
33 {
34         PointerRNA cscene = RNA_pointer_get(&b_scene.ptr, "cycles");
35
36         /* Default to CPU device. */
37         DeviceInfo device = Device::available_devices(DEVICE_MASK_CPU).front();
38
39         if(get_enum(cscene, "device") == 2) {
40                 /* Find network device. */
41                 vector<DeviceInfo> devices = Device::available_devices(DEVICE_MASK_NETWORK);
42                 if(!devices.empty()) {
43                         device = devices.front();
44                 }
45         }
46         else if(get_enum(cscene, "device") == 1) {
47                 /* Find cycles preferences. */
48                 PointerRNA cpreferences;
49
50                 BL::Preferences::addons_iterator b_addon_iter;
51                 for(b_preferences.addons.begin(b_addon_iter); b_addon_iter != b_preferences.addons.end(); ++b_addon_iter) {
52                         if(b_addon_iter->module() == "cycles") {
53                                 cpreferences = b_addon_iter->preferences().ptr;
54                                 break;
55                         }
56                 }
57
58                 /* Test if we are using GPU devices. */
59                 enum ComputeDevice {
60                         COMPUTE_DEVICE_CPU = 0,
61                         COMPUTE_DEVICE_CUDA = 1,
62                         COMPUTE_DEVICE_OPENCL = 2,
63                         COMPUTE_DEVICE_NUM = 3,
64                 };
65
66                 ComputeDevice compute_device = (ComputeDevice)get_enum(cpreferences,
67                                                                        "compute_device_type",
68                                                                        COMPUTE_DEVICE_NUM,
69                                                                        COMPUTE_DEVICE_CPU);
70
71                 if(compute_device != COMPUTE_DEVICE_CPU) {
72                         /* Query GPU devices with matching types. */
73                         uint mask = DEVICE_MASK_CPU;
74                         if(compute_device == COMPUTE_DEVICE_CUDA) {
75                                 mask |= DEVICE_MASK_CUDA;
76                         }
77                         else if(compute_device == COMPUTE_DEVICE_OPENCL) {
78                                 mask |= DEVICE_MASK_OPENCL;
79                         }
80                         vector<DeviceInfo> devices = Device::available_devices(mask);
81
82                         /* Match device preferences and available devices. */
83                         vector<DeviceInfo> used_devices;
84                         RNA_BEGIN(&cpreferences, device, "devices") {
85                                 if(get_boolean(device, "use")) {
86                                         string id = get_string(device, "id");
87                                         foreach(DeviceInfo& info, devices) {
88                                                 if(info.id == id) {
89                                                         used_devices.push_back(info);
90                                                         break;
91                                                 }
92                                         }
93                                 }
94                         } RNA_END;
95
96                         if(!used_devices.empty()) {
97                                 int threads = blender_device_threads(b_scene);
98                                 device = Device::get_multi_device(used_devices,
99                                                                   threads,
100                                                                   background);
101                         }
102                         /* Else keep using the CPU device that was set before. */
103                 }
104         }
105
106         return device;
107 }
108
109 CCL_NAMESPACE_END