style cleanup: block comments
[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(list<DeviceTask>& tasks, int num)
62 {
63         if(type == SHADER) {
64                 num = min(shader_w, num);
65
66                 for(int i = 0; i < num; i++) {
67                         int tx = shader_x + (shader_w/num)*i;
68                         int tw = (i == num-1)? shader_w - i*(shader_w/num): shader_w/num;
69
70                         DeviceTask task = *this;
71
72                         task.shader_x = tx;
73                         task.shader_w = tw;
74
75                         tasks.push_back(task);
76                 }
77         }
78         else {
79                 num = min(h, num);
80
81                 for(int i = 0; i < num; i++) {
82                         int ty = y + (h/num)*i;
83                         int th = (i == num-1)? h - i*(h/num): h/num;
84
85                         DeviceTask task = *this;
86
87                         task.y = ty;
88                         task.h = th;
89
90                         tasks.push_back(task);
91                 }
92         }
93 }
94
95 /* Device */
96
97 void Device::pixels_alloc(device_memory& mem)
98 {
99         mem_alloc(mem, MEM_READ_WRITE);
100 }
101
102 void Device::pixels_copy_from(device_memory& mem, int y, int w, int h)
103 {
104         mem_copy_from(mem, y, w, h, sizeof(uint8_t)*4);
105 }
106
107 void Device::pixels_free(device_memory& mem)
108 {
109         mem_free(mem);
110 }
111
112 void Device::draw_pixels(device_memory& rgba, int y, int w, int h, int dy, int width, int height, bool transparent)
113 {
114         pixels_copy_from(rgba, y, w, h);
115
116         if(transparent) {
117                 glEnable(GL_BLEND);
118                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
119         }
120
121         glPixelZoom((float)width/(float)w, (float)height/(float)h);
122         glRasterPos2f(0, dy);
123
124         uint8_t *pixels = (uint8_t*)rgba.data_pointer;
125
126         /* for multi devices, this assumes the ineffecient method that we allocate
127          * all pixels on the device even though we only render to a subset */
128         pixels += 4*y*w;
129
130         glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
131
132         glRasterPos2f(0.0f, 0.0f);
133         glPixelZoom(1.0f, 1.0f);
134
135         if(transparent)
136                 glDisable(GL_BLEND);
137 }
138
139 Device *Device::create(DeviceInfo& info, bool background, int threads)
140 {
141         Device *device;
142
143         switch(info.type) {
144                 case DEVICE_CPU:
145                         device = device_cpu_create(info, threads);
146                         break;
147 #ifdef WITH_CUDA
148                 case DEVICE_CUDA:
149                         if(cuLibraryInit())
150                                 device = device_cuda_create(info, background);
151                         else
152                                 device = NULL;
153                         break;
154 #endif
155 #ifdef WITH_MULTI
156                 case DEVICE_MULTI:
157                         device = device_multi_create(info, background);
158                         break;
159 #endif
160 #ifdef WITH_NETWORK
161                 case DEVICE_NETWORK:
162                         device = device_network_create(info, "127.0.0.1");
163                         break;
164 #endif
165 #ifdef WITH_OPENCL
166                 case DEVICE_OPENCL:
167                         if(clLibraryInit())
168                                 device = device_opencl_create(info, background);
169                         else
170                                 device = NULL;
171                         break;
172 #endif
173                 default:
174                         return NULL;
175         }
176
177         if(device)
178                 device->info = info;
179
180         return device;
181 }
182
183 DeviceType Device::type_from_string(const char *name)
184 {
185         if(strcmp(name, "cpu") == 0)
186                 return DEVICE_CPU;
187         else if(strcmp(name, "cuda") == 0)
188                 return DEVICE_CUDA;
189         else if(strcmp(name, "opencl") == 0)
190                 return DEVICE_OPENCL;
191         else if(strcmp(name, "network") == 0)
192                 return DEVICE_NETWORK;
193         else if(strcmp(name, "multi") == 0)
194                 return DEVICE_MULTI;
195         
196         return DEVICE_NONE;
197 }
198
199 string Device::string_from_type(DeviceType type)
200 {
201         if(type == DEVICE_CPU)
202                 return "cpu";
203         else if(type == DEVICE_CUDA)
204                 return "cuda";
205         else if(type == DEVICE_OPENCL)
206                 return "opencl";
207         else if(type == DEVICE_NETWORK)
208                 return "network";
209         else if(type == DEVICE_MULTI)
210                 return "multi";
211         
212         return "";
213 }
214
215 vector<DeviceType>& Device::available_types()
216 {
217         static vector<DeviceType> types;
218         static bool types_init = false;
219
220         if(!types_init) {
221                 types.push_back(DEVICE_CPU);
222
223 #ifdef WITH_CUDA
224                 if(cuLibraryInit())
225                         types.push_back(DEVICE_CUDA);
226 #endif
227
228 #ifdef WITH_OPENCL
229                 if(clLibraryInit())
230                         types.push_back(DEVICE_OPENCL);
231 #endif
232
233 #ifdef WITH_NETWORK
234                 types.push_back(DEVICE_NETWORK);
235 #endif
236 #ifdef WITH_MULTI
237                 types.push_back(DEVICE_MULTI);
238 #endif
239
240                 types_init = true;
241         }
242
243         return types;
244 }
245
246 vector<DeviceInfo>& Device::available_devices()
247 {
248         static vector<DeviceInfo> devices;
249         static bool devices_init = false;
250
251         if(!devices_init) {
252 #ifdef WITH_CUDA
253                 if(cuLibraryInit())
254                         device_cuda_info(devices);
255 #endif
256
257 #ifdef WITH_OPENCL
258                 if(clLibraryInit())
259                         device_opencl_info(devices);
260 #endif
261
262 #ifdef WITH_MULTI
263                 device_multi_info(devices);
264 #endif
265
266                 device_cpu_info(devices);
267
268 #ifdef WITH_NETWORK
269                 device_network_info(devices);
270 #endif
271
272                 devices_init = true;
273         }
274
275         return devices;
276 }
277
278 CCL_NAMESPACE_END
279