2
* Copyright 2011, Blender Foundation.
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.
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.
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.
23
#include "device_intern.h"
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"
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)
46
void DeviceTask::split_max_size(list<DeviceTask>& tasks, int max_size)
51
num = (shader_w + max_size - 1)/max_size;
54
max_size = max(1, max_size/w);
55
num = (h + max_size - 1)/max_size;
61
void DeviceTask::split(ThreadQueue<DeviceTask>& queue, int num)
63
list<DeviceTask> tasks;
66
foreach(DeviceTask& task, tasks)
70
void DeviceTask::split(list<DeviceTask>& tasks, int num)
73
num = min(shader_w, num);
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;
79
DeviceTask task = *this;
84
tasks.push_back(task);
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;
94
DeviceTask task = *this;
99
tasks.push_back(task);
106
void Device::pixels_alloc(device_memory& mem)
108
mem_alloc(mem, MEM_READ_WRITE);
111
void Device::pixels_copy_from(device_memory& mem, int y, int w, int h)
113
mem_copy_from(mem, y, w, h, sizeof(uint8_t)*4);
116
void Device::pixels_free(device_memory& mem)
121
void Device::draw_pixels(device_memory& rgba, int y, int w, int h, int dy, int width, int height, bool transparent)
123
pixels_copy_from(rgba, y, w, h);
127
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
130
glPixelZoom((float)width/(float)w, (float)height/(float)h);
131
glRasterPos2f(0, dy);
133
uint8_t *pixels = (uint8_t*)rgba.data_pointer;
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 */
139
glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
141
glRasterPos2f(0.0f, 0.0f);
142
glPixelZoom(1.0f, 1.0f);
148
Device *Device::create(DeviceInfo& info, bool background, int threads)
154
device = device_cpu_create(info, threads);
159
device = device_cuda_create(info, background);
166
device = device_multi_create(info, background);
171
device = device_network_create(info, "127.0.0.1");
177
device = device_opencl_create(info, background);
192
DeviceType Device::type_from_string(const char *name)
194
if(strcmp(name, "cpu") == 0)
196
else if(strcmp(name, "cuda") == 0)
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)
208
string Device::string_from_type(DeviceType type)
210
if(type == DEVICE_CPU)
212
else if(type == DEVICE_CUDA)
214
else if(type == DEVICE_OPENCL)
216
else if(type == DEVICE_NETWORK)
218
else if(type == DEVICE_MULTI)
224
vector<DeviceType>& Device::available_types()
226
static vector<DeviceType> types;
227
static bool types_init = false;
230
types.push_back(DEVICE_CPU);
234
types.push_back(DEVICE_CUDA);
239
types.push_back(DEVICE_OPENCL);
243
types.push_back(DEVICE_NETWORK);
246
types.push_back(DEVICE_MULTI);
255
vector<DeviceInfo>& Device::available_devices()
257
static vector<DeviceInfo> devices;
258
static bool devices_init = false;
263
device_cuda_info(devices);
268
device_opencl_info(devices);
272
device_multi_info(devices);
275
device_cpu_info(devices);
278
device_network_info(devices);