Cycles: multi GPU rendering support.
[blender.git] / intern / cycles / device / device.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "device.h"
23 #include "device_intern.h"
24
25 #include "util_cuda.h"
26 #include "util_debug.h"
27 #include "util_foreach.h"
28 #include "util_math.h"
29 #include "util_opencl.h"
30 #include "util_opengl.h"
31 #include "util_types.h"
32 #include "util_vector.h"
33
34 CCL_NAMESPACE_BEGIN
35
36 /* Device Task */
37
38 DeviceTask::DeviceTask(Type type_)
39 : type(type_), x(0), y(0), w(0), h(0), rng_state(0), rgba(0), buffer(0),
40   sample(0), resolution(0),
41   shader_input(0), shader_output(0),
42   shader_eval_type(0), shader_x(0), shader_w(0)
43 {
44 }
45
46 void DeviceTask::split_max_size(list<DeviceTask>& tasks, int max_size)
47 {
48         int num;
49
50         if(type == SHADER) {
51                 num = (shader_w + max_size - 1)/max_size;
52         }
53         else {
54                 max_size = max(1, max_size/w);
55                 num = (h + max_size - 1)/max_size;
56         }
57
58         split(tasks, num);
59 }
60
61 void DeviceTask::split(ThreadQueue<DeviceTask>& queue, int num)
62 {
63         list<DeviceTask> tasks;
64         split(tasks, num);
65
66         foreach(DeviceTask& task, tasks)
67                 queue.push(task);
68 }
69
70 void DeviceTask::split(list<DeviceTask>& tasks, int num)
71 {
72         if(type == SHADER) {
73                 num = min(shader_w, num);
74
75                 for(int i = 0; i < num; i++) {
76                         int tx = shader_x + (shader_w/num)*i;
77                         int tw = (i == num-1)? shader_w - i*(shader_w/num): shader_w/num;
78
79                         DeviceTask task = *this;
80
81                         task.shader_x = tx;
82                         task.shader_w = tw;
83
84                         tasks.push_back(task);
85                 }
86         }
87         else {
88                 num = min(h, num);
89
90                 for(int i = 0; i < num; i++) {
91                         int ty = y + (h/num)*i;
92                         int th = (i == num-1)? h - i*(h/num): h/num;
93
94                         DeviceTask task = *this;
95
96                         task.y = ty;
97                         task.h = th;
98
99                         tasks.push_back(task);
100                 }
101         }
102 }
103
104 /* Device */
105
106 void Device::pixels_alloc(device_memory& mem)
107 {
108         mem_alloc(mem, MEM_READ_WRITE);
109 }
110
111 void Device::pixels_copy_from(device_memory& mem, int y, int w, int h)
112 {
113         mem_copy_from(mem, y, w, h, sizeof(uint8_t)*4);
114 }
115
116 void Device::pixels_free(device_memory& mem)
117 {
118         mem_free(mem);
119 }
120
121 void Device::draw_pixels(device_memory& rgba, int y, int w, int h, int dy, int width, int height, bool transparent)
122 {
123         pixels_copy_from(rgba, y, w, h);
124
125         if(transparent) {
126                 glEnable(GL_BLEND);
127                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
128         }
129
130         glPixelZoom((float)width/(float)w, (float)height/(float)h);
131         glRasterPos2f(0, dy);
132
133         uint8_t *pixels = (uint8_t*)rgba.data_pointer;
134
135         /* for multi devices, this assumes the ineffecient method that we allocate
136            all pixels on the device even though we only render to a subset */
137         pixels += 4*y*w;
138
139         glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
140
141         glRasterPos2f(0.0f, 0.0f);
142         glPixelZoom(1.0f, 1.0f);
143
144         if(transparent)
145                 glDisable(GL_BLEND);
146 }
147
148 Device *Device::create(DeviceInfo& info, bool background, int threads)
149 {
150         Device *device;
151
152         switch(info.type) {
153                 case DEVICE_CPU:
154                         device = device_cpu_create(info, threads);
155                         break;
156 #ifdef WITH_CUDA
157                 case DEVICE_CUDA:
158                         if(cuLibraryInit())
159                                 device = device_cuda_create(info, background);
160                         else
161                                 device = NULL;
162                         break;
163 #endif
164 #ifdef WITH_MULTI
165                 case DEVICE_MULTI:
166                         device = device_multi_create(info, background);
167                         break;
168 #endif
169 #ifdef WITH_NETWORK
170                 case DEVICE_NETWORK:
171                         device = device_network_create(info, "127.0.0.1");
172                         break;
173 #endif
174 #ifdef WITH_OPENCL
175                 case DEVICE_OPENCL:
176                         if(clLibraryInit())
177                                 device = device_opencl_create(info, background);
178                         else
179                                 device = NULL;
180                         break;
181 #endif
182                 default:
183                         return NULL;
184         }
185
186         return device;
187 }
188
189 DeviceType Device::type_from_string(const char *name)
190 {
191         if(strcmp(name, "cpu") == 0)
192                 return DEVICE_CPU;
193         else if(strcmp(name, "cuda") == 0)
194                 return DEVICE_CUDA;
195         else if(strcmp(name, "opencl") == 0)
196                 return DEVICE_OPENCL;
197         else if(strcmp(name, "network") == 0)
198                 return DEVICE_NETWORK;
199         else if(strcmp(name, "multi") == 0)
200                 return DEVICE_MULTI;
201         
202         return DEVICE_NONE;
203 }
204
205 string Device::string_from_type(DeviceType type)
206 {
207         if(type == DEVICE_CPU)
208                 return "cpu";
209         else if(type == DEVICE_CUDA)
210                 return "cuda";
211         else if(type == DEVICE_OPENCL)
212                 return "opencl";
213         else if(type == DEVICE_NETWORK)
214                 return "network";
215         else if(type == DEVICE_MULTI)
216                 return "multi";
217         
218         return "";
219 }
220
221 vector<DeviceType>& Device::available_types()
222 {
223         static vector<DeviceType> types;
224         static bool types_init = false;
225
226         if(!types_init) {
227                 types.push_back(DEVICE_CPU);
228
229 #ifdef WITH_CUDA
230                 if(cuLibraryInit())
231                         types.push_back(DEVICE_CUDA);
232 #endif
233
234 #ifdef WITH_OPENCL
235                 if(clLibraryInit())
236                         types.push_back(DEVICE_OPENCL);
237 #endif
238
239 #ifdef WITH_NETWORK
240                 types.push_back(DEVICE_NETWORK);
241 #endif
242 #ifdef WITH_MULTI
243                 types.push_back(DEVICE_MULTI);
244 #endif
245
246                 types_init = true;
247         }
248
249         return types;
250 }
251
252 vector<DeviceInfo>& Device::available_devices()
253 {
254         static vector<DeviceInfo> devices;
255         static bool devices_init = false;
256
257         if(!devices_init) {
258                 device_cpu_info(devices);
259
260 #ifdef WITH_CUDA
261                 if(cuLibraryInit())
262                         device_cuda_info(devices);
263 #endif
264
265 #ifdef WITH_OPENCL
266                 if(clLibraryInit())
267                         device_opencl_info(devices);
268 #endif
269
270 #ifdef WITH_MULTI
271                 device_multi_info(devices);
272 #endif
273
274 #ifdef WITH_NETWORK
275                 device_network_info(devices);
276 #endif
277
278                 devices_init = true;
279         }
280
281         return devices;
282 }
283
284 CCL_NAMESPACE_END
285