Cycles: Make all #include statements relative to cycles source directory
[blender.git] / intern / cycles / device / device_opencl.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 #ifdef WITH_OPENCL
18
19 #include "device/opencl/opencl.h"
20
21 #include "device/device_intern.h"
22
23 #include "util/util_foreach.h"
24 #include "util/util_logging.h"
25
26 CCL_NAMESPACE_BEGIN
27
28 Device *device_opencl_create(DeviceInfo& info, Stats &stats, bool background)
29 {
30         vector<OpenCLPlatformDevice> usable_devices;
31         OpenCLInfo::get_usable_devices(&usable_devices);
32         assert(info.num < usable_devices.size());
33         const OpenCLPlatformDevice& platform_device = usable_devices[info.num];
34         const string& platform_name = platform_device.platform_name;
35         const cl_device_type device_type = platform_device.device_type;
36         if(OpenCLInfo::kernel_use_split(platform_name, device_type)) {
37                 VLOG(1) << "Using split kernel.";
38                 return opencl_create_split_device(info, stats, background);
39         } else {
40                 VLOG(1) << "Using mega kernel.";
41                 return opencl_create_mega_device(info, stats, background);
42         }
43 }
44
45 bool device_opencl_init(void)
46 {
47         static bool initialized = false;
48         static bool result = false;
49
50         if(initialized)
51                 return result;
52
53         initialized = true;
54
55         if(OpenCLInfo::device_type() != 0) {
56                 int clew_result = clewInit();
57                 if(clew_result == CLEW_SUCCESS) {
58                         VLOG(1) << "CLEW initialization succeeded.";
59                         result = true;
60                 }
61                 else {
62                         VLOG(1) << "CLEW initialization failed: "
63                                 << ((clew_result == CLEW_ERROR_ATEXIT_FAILED)
64                                     ? "Error setting up atexit() handler"
65                                     : "Error opening the library");
66                 }
67         }
68         else {
69                 VLOG(1) << "Skip initializing CLEW, platform is force disabled.";
70                 result = false;
71         }
72
73         return result;
74 }
75
76 void device_opencl_info(vector<DeviceInfo>& devices)
77 {
78         vector<OpenCLPlatformDevice> usable_devices;
79         OpenCLInfo::get_usable_devices(&usable_devices);
80         /* Devices are numbered consecutively across platforms. */
81         int num_devices = 0;
82         foreach(OpenCLPlatformDevice& platform_device, usable_devices) {
83                 const string& platform_name = platform_device.platform_name;
84                 const cl_device_type device_type = platform_device.device_type;
85                 const string& device_name = platform_device.device_name;
86                 string hardware_id = platform_device.hardware_id;
87                 if(hardware_id == "") {
88                         hardware_id = string_printf("ID_%d", num_devices);
89                 }
90
91                 DeviceInfo info;
92                 info.type = DEVICE_OPENCL;
93                 info.description = string_remove_trademark(string(device_name));
94                 info.num = num_devices;
95                 /* We don't know if it's used for display, but assume it is. */
96                 info.display_device = true;
97                 info.advanced_shading = OpenCLInfo::kernel_use_advanced_shading(platform_name);
98                 info.pack_images = true;
99                 info.use_split_kernel = OpenCLInfo::kernel_use_split(platform_name,
100                                                                      device_type);
101                 info.id = string("OPENCL_") + platform_name + "_" + device_name + "_" + hardware_id;
102                 devices.push_back(info);
103                 num_devices++;
104         }
105 }
106
107 string device_opencl_capabilities(void)
108 {
109         if(OpenCLInfo::device_type() == 0) {
110                 return "All OpenCL devices are forced to be OFF";
111         }
112         string result = "";
113         string error_msg = "";  /* Only used by opencl_assert(), but in the future
114                                  * it could also be nicely reported to the console.
115                                  */
116         cl_uint num_platforms = 0;
117         opencl_assert(clGetPlatformIDs(0, NULL, &num_platforms));
118         if(num_platforms == 0) {
119                 return "No OpenCL platforms found\n";
120         }
121         result += string_printf("Number of platforms: %u\n", num_platforms);
122
123         vector<cl_platform_id> platform_ids;
124         platform_ids.resize(num_platforms);
125         opencl_assert(clGetPlatformIDs(num_platforms, &platform_ids[0], NULL));
126
127 #define APPEND_STRING_INFO(func, id, name, what) \
128         do { \
129                 char data[1024] = "\0"; \
130                 opencl_assert(func(id, what, sizeof(data), &data, NULL)); \
131                 result += string_printf("%s: %s\n", name, data); \
132         } while(false)
133 #define APPEND_PLATFORM_STRING_INFO(id, name, what) \
134         APPEND_STRING_INFO(clGetPlatformInfo, id, "\tPlatform " name, what)
135 #define APPEND_DEVICE_STRING_INFO(id, name, what) \
136         APPEND_STRING_INFO(clGetDeviceInfo, id, "\t\t\tDevice " name, what)
137
138         vector<cl_device_id> device_ids;
139         for(cl_uint platform = 0; platform < num_platforms; ++platform) {
140                 cl_platform_id platform_id = platform_ids[platform];
141
142                 result += string_printf("Platform #%u\n", platform);
143
144                 APPEND_PLATFORM_STRING_INFO(platform_id, "Name", CL_PLATFORM_NAME);
145                 APPEND_PLATFORM_STRING_INFO(platform_id, "Vendor", CL_PLATFORM_VENDOR);
146                 APPEND_PLATFORM_STRING_INFO(platform_id, "Version", CL_PLATFORM_VERSION);
147                 APPEND_PLATFORM_STRING_INFO(platform_id, "Profile", CL_PLATFORM_PROFILE);
148                 APPEND_PLATFORM_STRING_INFO(platform_id, "Extensions", CL_PLATFORM_EXTENSIONS);
149
150                 cl_uint num_devices = 0;
151                 opencl_assert(clGetDeviceIDs(platform_ids[platform],
152                                              CL_DEVICE_TYPE_ALL,
153                                              0,
154                                              NULL,
155                                              &num_devices));
156                 result += string_printf("\tNumber of devices: %u\n", num_devices);
157
158                 device_ids.resize(num_devices);
159                 opencl_assert(clGetDeviceIDs(platform_ids[platform],
160                                              CL_DEVICE_TYPE_ALL,
161                                              num_devices,
162                                              &device_ids[0],
163                                              NULL));
164                 for(cl_uint device = 0; device < num_devices; ++device) {
165                         cl_device_id device_id = device_ids[device];
166
167                         result += string_printf("\t\tDevice: #%u\n", device);
168
169                         APPEND_DEVICE_STRING_INFO(device_id, "Name", CL_DEVICE_NAME);
170                         APPEND_DEVICE_STRING_INFO(device_id, "Vendor", CL_DEVICE_VENDOR);
171                         APPEND_DEVICE_STRING_INFO(device_id, "OpenCL C Version", CL_DEVICE_OPENCL_C_VERSION);
172                         APPEND_DEVICE_STRING_INFO(device_id, "Profile", CL_DEVICE_PROFILE);
173                         APPEND_DEVICE_STRING_INFO(device_id, "Version", CL_DEVICE_VERSION);
174                         APPEND_DEVICE_STRING_INFO(device_id, "Extensions", CL_DEVICE_EXTENSIONS);
175                 }
176         }
177
178 #undef APPEND_STRING_INFO
179 #undef APPEND_PLATFORM_STRING_INFO
180 #undef APPEND_DEVICE_STRING_INFO
181
182         return result;
183 }
184
185 CCL_NAMESPACE_END
186
187 #endif /* WITH_OPENCL */