~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to intern/cycles/device/device.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        if(device)
 
187
                device->info = info;
 
188
 
 
189
        return device;
 
190
}
 
191
 
 
192
DeviceType Device::type_from_string(const char *name)
 
193
{
 
194
        if(strcmp(name, "cpu") == 0)
 
195
                return DEVICE_CPU;
 
196
        else if(strcmp(name, "cuda") == 0)
 
197
                return DEVICE_CUDA;
 
198
        else if(strcmp(name, "opencl") == 0)
 
199
                return DEVICE_OPENCL;
 
200
        else if(strcmp(name, "network") == 0)
 
201
                return DEVICE_NETWORK;
 
202
        else if(strcmp(name, "multi") == 0)
 
203
                return DEVICE_MULTI;
 
204
        
 
205
        return DEVICE_NONE;
 
206
}
 
207
 
 
208
string Device::string_from_type(DeviceType type)
 
209
{
 
210
        if(type == DEVICE_CPU)
 
211
                return "cpu";
 
212
        else if(type == DEVICE_CUDA)
 
213
                return "cuda";
 
214
        else if(type == DEVICE_OPENCL)
 
215
                return "opencl";
 
216
        else if(type == DEVICE_NETWORK)
 
217
                return "network";
 
218
        else if(type == DEVICE_MULTI)
 
219
                return "multi";
 
220
        
 
221
        return "";
 
222
}
 
223
 
 
224
vector<DeviceType>& Device::available_types()
 
225
{
 
226
        static vector<DeviceType> types;
 
227
        static bool types_init = false;
 
228
 
 
229
        if(!types_init) {
 
230
                types.push_back(DEVICE_CPU);
 
231
 
 
232
#ifdef WITH_CUDA
 
233
                if(cuLibraryInit())
 
234
                        types.push_back(DEVICE_CUDA);
 
235
#endif
 
236
 
 
237
#ifdef WITH_OPENCL
 
238
                if(clLibraryInit())
 
239
                        types.push_back(DEVICE_OPENCL);
 
240
#endif
 
241
 
 
242
#ifdef WITH_NETWORK
 
243
                types.push_back(DEVICE_NETWORK);
 
244
#endif
 
245
#ifdef WITH_MULTI
 
246
                types.push_back(DEVICE_MULTI);
 
247
#endif
 
248
 
 
249
                types_init = true;
 
250
        }
 
251
 
 
252
        return types;
 
253
}
 
254
 
 
255
vector<DeviceInfo>& Device::available_devices()
 
256
{
 
257
        static vector<DeviceInfo> devices;
 
258
        static bool devices_init = false;
 
259
 
 
260
        if(!devices_init) {
 
261
#ifdef WITH_CUDA
 
262
                if(cuLibraryInit())
 
263
                        device_cuda_info(devices);
 
264
#endif
 
265
 
 
266
#ifdef WITH_OPENCL
 
267
                if(clLibraryInit())
 
268
                        device_opencl_info(devices);
 
269
#endif
 
270
 
 
271
#ifdef WITH_MULTI
 
272
                device_multi_info(devices);
 
273
#endif
 
274
 
 
275
                device_cpu_info(devices);
 
276
 
 
277
#ifdef WITH_NETWORK
 
278
                device_network_info(devices);
 
279
#endif
 
280
 
 
281
                devices_init = true;
 
282
        }
 
283
 
 
284
        return devices;
 
285
}
 
286
 
 
287
CCL_NAMESPACE_END
 
288