Cycles: merge of changes from tomato branch.
[blender-staging.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 */
37
38 void Device::pixels_alloc(device_memory& mem)
39 {
40         mem_alloc(mem, MEM_READ_WRITE);
41 }
42
43 void Device::pixels_copy_from(device_memory& mem, int y, int w, int h)
44 {
45         mem_copy_from(mem, y, w, h, sizeof(uint8_t)*4);
46 }
47
48 void Device::pixels_free(device_memory& mem)
49 {
50         mem_free(mem);
51 }
52
53 void Device::draw_pixels(device_memory& rgba, int y, int w, int h, int dy, int width, int height, bool transparent)
54 {
55         pixels_copy_from(rgba, y, w, h);
56
57         if(transparent) {
58                 glEnable(GL_BLEND);
59                 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
60         }
61
62         glPixelZoom((float)width/(float)w, (float)height/(float)h);
63         glRasterPos2f(0, dy);
64
65         uint8_t *pixels = (uint8_t*)rgba.data_pointer;
66
67         /* for multi devices, this assumes the ineffecient method that we allocate
68          * all pixels on the device even though we only render to a subset */
69         pixels += 4*y*w;
70
71         glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
72
73         glRasterPos2f(0.0f, 0.0f);
74         glPixelZoom(1.0f, 1.0f);
75
76         if(transparent)
77                 glDisable(GL_BLEND);
78 }
79
80 Device *Device::create(DeviceInfo& info, bool background, int threads)
81 {
82         Device *device;
83
84         switch(info.type) {
85                 case DEVICE_CPU:
86                         device = device_cpu_create(info, threads);
87                         break;
88 #ifdef WITH_CUDA
89                 case DEVICE_CUDA:
90                         if(cuLibraryInit())
91                                 device = device_cuda_create(info, background);
92                         else
93                                 device = NULL;
94                         break;
95 #endif
96 #ifdef WITH_MULTI
97                 case DEVICE_MULTI:
98                         device = device_multi_create(info, background);
99                         break;
100 #endif
101 #ifdef WITH_NETWORK
102                 case DEVICE_NETWORK:
103                         device = device_network_create(info, "127.0.0.1");
104                         break;
105 #endif
106 #ifdef WITH_OPENCL
107                 case DEVICE_OPENCL:
108                         if(clLibraryInit())
109                                 device = device_opencl_create(info, background);
110                         else
111                                 device = NULL;
112                         break;
113 #endif
114                 default:
115                         return NULL;
116         }
117
118         if(device)
119                 device->info = info;
120
121         return device;
122 }
123
124 DeviceType Device::type_from_string(const char *name)
125 {
126         if(strcmp(name, "cpu") == 0)
127                 return DEVICE_CPU;
128         else if(strcmp(name, "cuda") == 0)
129                 return DEVICE_CUDA;
130         else if(strcmp(name, "opencl") == 0)
131                 return DEVICE_OPENCL;
132         else if(strcmp(name, "network") == 0)
133                 return DEVICE_NETWORK;
134         else if(strcmp(name, "multi") == 0)
135                 return DEVICE_MULTI;
136         
137         return DEVICE_NONE;
138 }
139
140 string Device::string_from_type(DeviceType type)
141 {
142         if(type == DEVICE_CPU)
143                 return "cpu";
144         else if(type == DEVICE_CUDA)
145                 return "cuda";
146         else if(type == DEVICE_OPENCL)
147                 return "opencl";
148         else if(type == DEVICE_NETWORK)
149                 return "network";
150         else if(type == DEVICE_MULTI)
151                 return "multi";
152         
153         return "";
154 }
155
156 vector<DeviceType>& Device::available_types()
157 {
158         static vector<DeviceType> types;
159         static bool types_init = false;
160
161         if(!types_init) {
162                 types.push_back(DEVICE_CPU);
163
164 #ifdef WITH_CUDA
165                 if(cuLibraryInit())
166                         types.push_back(DEVICE_CUDA);
167 #endif
168
169 #ifdef WITH_OPENCL
170                 if(clLibraryInit())
171                         types.push_back(DEVICE_OPENCL);
172 #endif
173
174 #ifdef WITH_NETWORK
175                 types.push_back(DEVICE_NETWORK);
176 #endif
177 #ifdef WITH_MULTI
178                 types.push_back(DEVICE_MULTI);
179 #endif
180
181                 types_init = true;
182         }
183
184         return types;
185 }
186
187 vector<DeviceInfo>& Device::available_devices()
188 {
189         static vector<DeviceInfo> devices;
190         static bool devices_init = false;
191
192         if(!devices_init) {
193 #ifdef WITH_CUDA
194                 if(cuLibraryInit())
195                         device_cuda_info(devices);
196 #endif
197
198 #ifdef WITH_OPENCL
199                 if(clLibraryInit())
200                         device_opencl_info(devices);
201 #endif
202
203 #ifdef WITH_MULTI
204                 device_multi_info(devices);
205 #endif
206
207                 device_cpu_info(devices);
208
209 #ifdef WITH_NETWORK
210                 device_network_info(devices);
211 #endif
212
213                 devices_init = true;
214         }
215
216         return devices;
217 }
218
219 CCL_NAMESPACE_END
220