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