Cycles: render passes for CUDA cards with compute model >= 2.x.
[blender.git] / intern / cycles / device / device_network.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 "device.h"
20 #include "device_intern.h"
21 #include "device_network.h"
22
23 #include "util_foreach.h"
24
25 CCL_NAMESPACE_BEGIN
26
27 #ifdef WITH_NETWORK
28
29 class NetworkDevice : public Device
30 {
31 public:
32         boost::asio::io_service io_service;
33         tcp::socket socket;
34
35         NetworkDevice(const char *address)
36         : socket(io_service)
37         {
38                 stringstream portstr;
39                 portstr << SERVER_PORT;
40
41                 tcp::resolver resolver(io_service);
42                 tcp::resolver::query query(address, portstr.str());
43                 tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
44                 tcp::resolver::iterator end;
45
46                 boost::system::error_code error = boost::asio::error::host_not_found;
47                 while(error && endpoint_iterator != end)
48                 {
49                         socket.close();
50                         socket.connect(*endpoint_iterator++, error);
51                 }
52                 if(error)
53                         throw boost::system::system_error(error);
54         }
55
56         ~NetworkDevice()
57         {
58         }
59
60         void mem_alloc(device_memory& mem, MemoryType type)
61         {
62 #if 0
63                 RPCSend snd(socket, "mem_alloc");
64
65                 snd.archive & size & type;
66                 snd.write();
67
68                 RPCReceive rcv(socket);
69
70                 device_ptr mem;
71                 *rcv.archive & mem;
72
73                 return mem;
74 #endif
75         }
76
77         void mem_copy_to(device_memory& mem)
78         {
79 #if 0
80                 RPCSend snd(socket, "mem_copy_to");
81
82                 snd.archive & mem & size;
83                 snd.write();
84                 snd.write_buffer(host, size);
85 #endif
86         }
87
88         void mem_copy_from(device_memory& mem, int y, int w, int h, int elem)
89         {
90 #if 0
91                 RPCSend snd(socket, "mem_copy_from");
92
93                 snd.archive & mem & offset & size;
94                 snd.write();
95
96                 RPCReceive rcv(socket);
97                 rcv.read_buffer(host, size);
98 #endif
99         }
100
101         void mem_zero(device_memory& mem)
102         {
103 #if 0
104                 RPCSend snd(socket, "mem_zero");
105
106                 snd.archive & mem & size;
107                 snd.write();
108 #endif
109         }
110
111         void mem_free(device_memory& mem)
112         {
113 #if 0
114                 if(mem) {
115                         RPCSend snd(socket, "mem_free");
116
117                         snd.archive & mem;
118                         snd.write();
119                 }
120 #endif
121         }
122
123         void const_copy_to(const char *name, void *host, size_t size)
124         {
125                 RPCSend snd(socket, "const_copy_to");
126
127                 string name_string(name);
128
129                 snd.archive & name_string & size;
130                 snd.write();
131                 snd.write_buffer(host, size);
132         }
133
134         void tex_alloc(const char *name, device_memory& mem, bool interpolation, bool periodic)
135         {
136 #if 0
137                 RPCSend snd(socket, "tex_alloc");
138
139                 string name_string(name);
140
141                 snd.archive & name_string & width & height & datatype & components & interpolation;
142                 snd.write();
143
144                 size_t size = width*height*components*datatype_size(datatype);
145                 snd.write_buffer(host, size);
146
147                 RPCReceive rcv(socket);
148
149                 device_ptr mem;
150                 *rcv.archive & mem;
151
152                 return mem;
153 #endif
154         }
155
156         void tex_free(device_memory& mem)
157         {
158 #if 0
159                 if(mem) {
160                         RPCSend snd(socket, "tex_free");
161
162                         snd.archive & mem;
163                         snd.write();
164                 }
165 #endif
166         }
167
168         void path_trace(int x, int y, int w, int h, device_ptr buffer, device_ptr rng_state, int sample)
169         {
170 #if 0
171                 RPCSend snd(socket, "path_trace");
172
173                 snd.archive & x & y & w & h & buffer & rng_state & sample;
174                 snd.write();
175 #endif
176         }
177
178         void tonemap(int x, int y, int w, int h, device_ptr rgba, device_ptr buffer, int sample, int resolution)
179         {
180 #if 0
181                 RPCSend snd(socket, "tonemap");
182
183                 snd.archive & x & y & w & h & rgba & buffer & sample & resolution;
184                 snd.write();
185 #endif
186         }
187
188         void task_add(DeviceTask& task)
189         {
190                 if(task.type == DeviceTask::TONEMAP)
191                         tonemap(task.x, task.y, task.w, task.h, task.rgba, task.buffer, task.sample, task.resolution);
192                 else if(task.type == DeviceTask::PATH_TRACE)
193                         path_trace(task.x, task.y, task.w, task.h, task.buffer, task.rng_state, task.sample);
194         }
195
196         void task_wait()
197         {
198         }
199
200         void task_cancel()
201         {
202         }
203 };
204
205 Device *device_network_create(DeviceInfo& info, const char *address)
206 {
207         return new NetworkDevice(address);
208 }
209
210 void device_network_info(vector<DeviceInfo>& devices)
211 {
212         DeviceInfo info;
213
214         info.type = DEVICE_NETWORK;
215         info.description = "Network Device";
216         info.id = "NETWORK";
217         info.num = 0;
218
219         devices.push_back(info);
220 }
221
222 void Device::server_run()
223 {
224         try
225         {
226                 /* starts thread that responds to discovery requests */
227                 ServerDiscovery discovery;
228
229                 for(;;)
230                 {
231
232                         /* accept connection */
233                         boost::asio::io_service io_service;
234                         tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), SERVER_PORT));
235
236                         tcp::socket socket(io_service);
237                         acceptor.accept(socket);
238
239                         /* receive remote function calls */
240                         for(;;) {
241                                 RPCReceive rcv(socket);
242
243                                 if(rcv.name == "description") {
244                                         string desc = description();
245
246                                         RPCSend snd(socket);
247                                         snd.archive & desc;
248                                         snd.write();
249                                 }
250                                 else if(rcv.name == "mem_alloc") {
251 #if 0
252                                         MemoryType type;
253                                         size_t size;
254                                         device_ptr mem;
255
256                                         *rcv.archive & size & type;
257                                         mem = mem_alloc(size, type);
258
259                                         RPCSend snd(socket);
260                                         snd.archive & mem;
261                                         snd.write();
262 #endif
263                                 }
264                                 else if(rcv.name == "mem_copy_to") {
265 #if 0
266                                         device_ptr mem;
267                                         size_t size;
268
269                                         *rcv.archive & mem & size;
270
271                                         vector<char> host_vector(size);
272                                         rcv.read_buffer(&host_vector[0], size);
273
274                                         mem_copy_to(mem, &host_vector[0], size);
275 #endif
276                                 }
277                                 else if(rcv.name == "mem_copy_from") {
278 #if 0
279                                         device_ptr mem;
280                                         size_t offset, size;
281
282                                         *rcv.archive & mem & offset & size;
283
284                                         vector<char> host_vector(size);
285
286                                         mem_copy_from(&host_vector[0], mem, offset, size);
287
288                                         RPCSend snd(socket);
289                                         snd.write();
290                                         snd.write_buffer(&host_vector[0], size);
291 #endif
292                                 }
293                                 else if(rcv.name == "mem_zero") {
294 #if 0
295                                         device_ptr mem;
296                                         size_t size;
297
298                                         *rcv.archive & mem & size;
299                                         mem_zero(mem, size);
300 #endif
301                                 }
302                                 else if(rcv.name == "mem_free") {
303 #if 0
304                                         device_ptr mem;
305
306                                         *rcv.archive & mem;
307                                         mem_free(mem);
308 #endif
309                                 }
310                                 else if(rcv.name == "const_copy_to") {
311                                         string name_string;
312                                         size_t size;
313
314                                         *rcv.archive & name_string & size;
315
316                                         vector<char> host_vector(size);
317                                         rcv.read_buffer(&host_vector[0], size);
318
319                                         const_copy_to(name_string.c_str(), &host_vector[0], size);
320                                 }
321                                 else if(rcv.name == "tex_alloc") {
322 #if 0
323                                         string name_string;
324                                         DataType datatype;
325                                         device_ptr mem;
326                                         size_t width, height;
327                                         int components;
328                                         bool interpolation;
329
330                                         *rcv.archive & name_string & width & height & datatype & components & interpolation;
331
332                                         size_t size = width*height*components*datatype_size(datatype);
333
334                                         vector<char> host_vector(size);
335                                         rcv.read_buffer(&host_vector[0], size);
336
337                                         mem = tex_alloc(name_string.c_str(), &host_vector[0], width, height, datatype, components, interpolation);
338
339                                         RPCSend snd(socket);
340                                         snd.archive & mem;
341                                         snd.write();
342 #endif
343                                 }
344                                 else if(rcv.name == "tex_free") {
345 #if 0
346                                         device_ptr mem;
347
348                                         *rcv.archive & mem;
349                                         tex_free(mem);
350 #endif
351                                 }
352                                 else if(rcv.name == "path_trace") {
353 #if 0
354                                         device_ptr buffer, rng_state;
355                                         int x, y, w, h;
356                                         int sample;
357
358                                         *rcv.archive & x & y & w & h & buffer & rng_state & sample;
359                                         path_trace(x, y, w, h, buffer, rng_state, sample);
360 #endif
361                                 }
362                                 else if(rcv.name == "tonemap") {
363 #if 0
364                                         device_ptr rgba, buffer;
365                                         int x, y, w, h;
366                                         int sample, resolution;
367
368                                         *rcv.archive & x & y & w & h & rgba & buffer & sample & resolution;
369                                         tonemap(x, y, w, h, rgba, buffer, sample, resolution);
370 #endif
371                                 }
372                         }
373                 }
374         }
375         catch(exception& e)
376         {
377                 cerr << "Network server exception: " << e.what() << endl;
378         }
379 }
380
381 #endif
382
383 CCL_NAMESPACE_END
384