Cycles: viewport render now takes scene color management settings into account,
[blender.git] / intern / cycles / device / device_multi.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16
17 #include <stdlib.h>
18 #include <sstream>
19
20 #include "device.h"
21 #include "device_intern.h"
22 #include "device_network.h"
23
24 #include "buffers.h"
25
26 #include "util_foreach.h"
27 #include "util_list.h"
28 #include "util_map.h"
29 #include "util_time.h"
30
31 CCL_NAMESPACE_BEGIN
32
33 class MultiDevice : public Device
34 {
35 public:
36         struct SubDevice {
37                 SubDevice(Device *device_)
38                 : device(device_) {}
39
40                 Device *device;
41                 map<device_ptr, device_ptr> ptr_map;
42         };
43
44         list<SubDevice> devices;
45         device_ptr unique_ptr;
46
47         MultiDevice(DeviceInfo& info, Stats &stats, bool background_)
48         : Device(stats), unique_ptr(1)
49         {
50                 Device *device;
51                 background = background_;
52
53                 foreach(DeviceInfo& subinfo, info.multi_devices) {
54                         device = Device::create(subinfo, stats, background);
55                         devices.push_back(SubDevice(device));
56                 }
57
58 #if 0 //def WITH_NETWORK
59                 /* try to add network devices */
60                 ServerDiscovery discovery(true);
61                 time_sleep(1.0);
62
63                 list<string> servers = discovery.get_server_list();
64
65                 foreach(string& server, servers) {
66                         device = device_network_create(info, server.c_str());
67                         if(device)
68                                 devices.push_back(SubDevice(device));
69                 }
70 #endif
71         }
72
73         ~MultiDevice()
74         {
75                 foreach(SubDevice& sub, devices)
76                         delete sub.device;
77         }
78
79         const string& error_message()
80         {
81                 foreach(SubDevice& sub, devices) {
82                         if(sub.device->error_message() != "") {
83                                 if(error_msg == "")
84                                         error_msg = sub.device->error_message();
85                                 break;
86                         }
87                 }
88
89                 return error_msg;
90         }
91
92         bool load_kernels(bool experimental)
93         {
94                 foreach(SubDevice& sub, devices)
95                         if(!sub.device->load_kernels(experimental))
96                                 return false;
97
98                 return true;
99         }
100
101         void mem_alloc(device_memory& mem, MemoryType type)
102         {
103                 foreach(SubDevice& sub, devices) {
104                         mem.device_pointer = 0;
105                         sub.device->mem_alloc(mem, type);
106                         sub.ptr_map[unique_ptr] = mem.device_pointer;
107                 }
108
109                 mem.device_pointer = unique_ptr++;
110         }
111
112         void mem_copy_to(device_memory& mem)
113         {
114                 device_ptr tmp = mem.device_pointer;
115
116                 foreach(SubDevice& sub, devices) {
117                         mem.device_pointer = sub.ptr_map[tmp];
118                         sub.device->mem_copy_to(mem);
119                 }
120
121                 mem.device_pointer = tmp;
122         }
123
124         void mem_copy_from(device_memory& mem, int y, int w, int h, int elem)
125         {
126                 device_ptr tmp = mem.device_pointer;
127                 int i = 0, sub_h = h/devices.size();
128
129                 foreach(SubDevice& sub, devices) {
130                         int sy = y + i*sub_h;
131                         int sh = (i == (int)devices.size() - 1)? h - sub_h*i: sub_h;
132
133                         mem.device_pointer = sub.ptr_map[tmp];
134                         sub.device->mem_copy_from(mem, sy, w, sh, elem);
135                         i++;
136                 }
137
138                 mem.device_pointer = tmp;
139         }
140
141         void mem_zero(device_memory& mem)
142         {
143                 device_ptr tmp = mem.device_pointer;
144
145                 foreach(SubDevice& sub, devices) {
146                         mem.device_pointer = sub.ptr_map[tmp];
147                         sub.device->mem_zero(mem);
148                 }
149
150                 mem.device_pointer = tmp;
151         }
152
153         void mem_free(device_memory& mem)
154         {
155                 device_ptr tmp = mem.device_pointer;
156
157                 foreach(SubDevice& sub, devices) {
158                         mem.device_pointer = sub.ptr_map[tmp];
159                         sub.device->mem_free(mem);
160                         sub.ptr_map.erase(sub.ptr_map.find(tmp));
161                 }
162
163                 mem.device_pointer = 0;
164         }
165
166         void const_copy_to(const char *name, void *host, size_t size)
167         {
168                 foreach(SubDevice& sub, devices)
169                         sub.device->const_copy_to(name, host, size);
170         }
171
172         void tex_alloc(const char *name, device_memory& mem, bool interpolation, bool periodic)
173         {
174                 foreach(SubDevice& sub, devices) {
175                         mem.device_pointer = 0;
176                         sub.device->tex_alloc(name, mem, interpolation, periodic);
177                         sub.ptr_map[unique_ptr] = mem.device_pointer;
178                 }
179
180                 mem.device_pointer = unique_ptr++;
181         }
182
183         void tex_free(device_memory& mem)
184         {
185                 device_ptr tmp = mem.device_pointer;
186
187                 foreach(SubDevice& sub, devices) {
188                         mem.device_pointer = sub.ptr_map[tmp];
189                         sub.device->tex_free(mem);
190                         sub.ptr_map.erase(sub.ptr_map.find(tmp));
191                 }
192
193                 mem.device_pointer = 0;
194         }
195
196         void pixels_alloc(device_memory& mem)
197         {
198                 foreach(SubDevice& sub, devices) {
199                         mem.device_pointer = 0;
200                         sub.device->pixels_alloc(mem);
201                         sub.ptr_map[unique_ptr] = mem.device_pointer;
202                 }
203
204                 mem.device_pointer = unique_ptr++;
205         }
206
207         void pixels_free(device_memory& mem)
208         {
209                 device_ptr tmp = mem.device_pointer;
210
211                 foreach(SubDevice& sub, devices) {
212                         mem.device_pointer = sub.ptr_map[tmp];
213                         sub.device->pixels_free(mem);
214                         sub.ptr_map.erase(sub.ptr_map.find(tmp));
215                 }
216
217                 mem.device_pointer = 0;
218         }
219
220         void pixels_copy_from(device_memory& mem, int y, int w, int h)
221         {
222                 device_ptr tmp = mem.device_pointer;
223                 int i = 0, sub_h = h/devices.size();
224
225                 foreach(SubDevice& sub, devices) {
226                         int sy = y + i*sub_h;
227                         int sh = (i == (int)devices.size() - 1)? h - sub_h*i: sub_h;
228
229                         mem.device_pointer = sub.ptr_map[tmp];
230                         sub.device->pixels_copy_from(mem, sy, w, sh);
231                         i++;
232                 }
233
234                 mem.device_pointer = tmp;
235         }
236
237         void draw_pixels(device_memory& rgba, int y, int w, int h, int dy, int width, int height, bool transparent)
238         {
239                 device_ptr tmp = rgba.device_pointer;
240                 int i = 0, sub_h = h/devices.size();
241                 int sub_height = height/devices.size();
242
243                 foreach(SubDevice& sub, devices) {
244                         int sy = y + i*sub_h;
245                         int sh = (i == (int)devices.size() - 1)? h - sub_h*i: sub_h;
246                         int sheight = (i == (int)devices.size() - 1)? height - sub_height*i: sub_height;
247                         int sdy = dy + i*sub_height;
248                         /* adjust math for w/width */
249
250                         rgba.device_pointer = sub.ptr_map[tmp];
251                         sub.device->draw_pixels(rgba, sy, w, sh, sdy, width, sheight, transparent);
252                         i++;
253                 }
254
255                 rgba.device_pointer = tmp;
256         }
257
258         void map_tile(Device *sub_device, RenderTile& tile)
259         {
260                 foreach(SubDevice& sub, devices) {
261                         if(sub.device == sub_device) {
262                                 if(tile.buffer) tile.buffer = sub.ptr_map[tile.buffer];
263                                 if(tile.rng_state) tile.rng_state = sub.ptr_map[tile.rng_state];
264                         }
265                 }
266         }
267
268         int device_number(Device *sub_device)
269         {
270                 int i = 0;
271
272                 foreach(SubDevice& sub, devices) {
273                         if(sub.device == sub_device)
274                                 return i;
275                         i++;
276                 }
277
278                 return -1;
279         }
280
281         void task_add(DeviceTask& task)
282         {
283                 list<DeviceTask> tasks;
284                 task.split(tasks, devices.size());
285
286                 foreach(SubDevice& sub, devices) {
287                         if(!tasks.empty()) {
288                                 DeviceTask subtask = tasks.front();
289                                 tasks.pop_front();
290
291                                 if(task.buffer) subtask.buffer = sub.ptr_map[task.buffer];
292                                 if(task.rgba_byte) subtask.rgba_byte = sub.ptr_map[task.rgba_byte];
293                                 if(task.rgba_half) subtask.rgba_half = sub.ptr_map[task.rgba_half];
294                                 if(task.shader_input) subtask.shader_input = sub.ptr_map[task.shader_input];
295                                 if(task.shader_output) subtask.shader_output = sub.ptr_map[task.shader_output];
296
297                                 sub.device->task_add(subtask);
298                         }
299                 }
300         }
301
302         void task_wait()
303         {
304                 foreach(SubDevice& sub, devices)
305                         sub.device->task_wait();
306         }
307
308         void task_cancel()
309         {
310                 foreach(SubDevice& sub, devices)
311                         sub.device->task_cancel();
312         }
313 };
314
315 Device *device_multi_create(DeviceInfo& info, Stats &stats, bool background)
316 {
317         return new MultiDevice(info, stats, background);
318 }
319
320 static bool device_multi_add(vector<DeviceInfo>& devices, DeviceType type, bool with_display, bool with_advanced_shading, const char *id_fmt, int num)
321 {
322         DeviceInfo info;
323
324         /* create map to find duplicate descriptions */
325         map<string, int> dupli_map;
326         map<string, int>::iterator dt;
327         int num_added = 0, num_display = 0;
328
329         info.advanced_shading = with_advanced_shading;
330         info.pack_images = false;
331
332         foreach(DeviceInfo& subinfo, devices) {
333                 if(subinfo.type == type) {
334                         if(subinfo.advanced_shading != info.advanced_shading)
335                                 continue;
336                         if(subinfo.display_device) {
337                                 if(with_display)
338                                         num_display++;
339                                 else
340                                         continue;
341                         }
342
343                         string key = subinfo.description;
344
345                         if(dupli_map.find(key) == dupli_map.end())
346                                 dupli_map[key] = 1;
347                         else
348                                 dupli_map[key]++;
349
350                         info.multi_devices.push_back(subinfo);
351                         if(subinfo.display_device)
352                                 info.display_device = true;
353                         info.pack_images = info.pack_images || subinfo.pack_images;
354                         num_added++;
355                 }
356         }
357
358         if(num_added <= 1 || (with_display && num_display == 0))
359                 return false;
360
361         /* generate string */
362         stringstream desc;
363         vector<string> last_tokens;
364         bool first = true;
365
366         for(dt = dupli_map.begin(); dt != dupli_map.end(); dt++) {
367                 if(!first) desc << " + ";
368                 first = false;
369
370                 /* get name and count */
371                 string name = dt->first;
372                 int count = dt->second;
373
374                 /* strip common prefixes */
375                 vector<string> tokens;
376                 string_split(tokens, dt->first);
377
378                 if(tokens.size() > 1) {
379                         int i;
380
381                         for(i = 0; i < tokens.size() && i < last_tokens.size(); i++)
382                                 if(tokens[i] != last_tokens[i])
383                                         break;
384
385                         name = "";
386                         for(; i < tokens.size(); i++) {
387                                 name += tokens[i];
388                                 if(i != tokens.size() - 1)
389                                         name += " ";
390                         }
391                 }
392
393                 last_tokens = tokens;
394
395                 /* add */
396                 if(count > 1)
397                         desc << name << " (" << count << "x)";
398                 else
399                         desc << name;
400         }
401
402         /* add info */
403         info.type = DEVICE_MULTI;
404         info.description = desc.str();
405         info.id = string_printf(id_fmt, num);
406         info.display_device = with_display;
407         info.num = 0;
408
409         if(with_display)
410                 devices.push_back(info);
411         else
412                 devices.insert(devices.begin(), info);
413         
414         return true;
415 }
416
417 void device_multi_info(vector<DeviceInfo>& devices)
418 {
419         int num = 0;
420
421         if(!device_multi_add(devices, DEVICE_CUDA, false, true, "CUDA_MULTI_%d", num++))
422                 device_multi_add(devices, DEVICE_CUDA, false, false, "CUDA_MULTI_%d", num++);
423         if(!device_multi_add(devices, DEVICE_CUDA, true, true, "CUDA_MULTI_%d", num++))
424                 device_multi_add(devices, DEVICE_CUDA, true, false, "CUDA_MULTI_%d", num++);
425
426         num = 0;
427         if(!device_multi_add(devices, DEVICE_OPENCL, false, true, "OPENCL_MULTI_%d", num++))
428                 device_multi_add(devices, DEVICE_OPENCL, false, false, "OPENCL_MULTI_%d", num++);
429         if(!device_multi_add(devices, DEVICE_OPENCL, true, true, "OPENCL_MULTI_%d", num++))
430                 device_multi_add(devices, DEVICE_OPENCL, true, false, "OPENCL_MULTI_%d", num++);
431 }
432
433 CCL_NAMESPACE_END
434