Blender V2.61 - r43446

device_network.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include "device.h"
00020 #include "device_intern.h"
00021 #include "device_network.h"
00022 
00023 #include "util_foreach.h"
00024 
00025 CCL_NAMESPACE_BEGIN
00026 
00027 #ifdef WITH_NETWORK
00028 
00029 class NetworkDevice : public Device
00030 {
00031 public:
00032     boost::asio::io_service io_service;
00033     tcp::socket socket;
00034 
00035     NetworkDevice(const char *address)
00036     : socket(io_service)
00037     {
00038         stringstream portstr;
00039         portstr << SERVER_PORT;
00040 
00041         tcp::resolver resolver(io_service);
00042         tcp::resolver::query query(address, portstr.str());
00043         tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
00044         tcp::resolver::iterator end;
00045 
00046         boost::system::error_code error = boost::asio::error::host_not_found;
00047         while(error && endpoint_iterator != end)
00048         {
00049             socket.close();
00050             socket.connect(*endpoint_iterator++, error);
00051         }
00052         if(error)
00053             throw boost::system::system_error(error);
00054     }
00055 
00056     ~NetworkDevice()
00057     {
00058     }
00059 
00060     bool support_full_kernel()
00061     {
00062         return false;
00063     }
00064 
00065     string description()
00066     {
00067         RPCSend snd(socket, "description");
00068         snd.write();
00069 
00070         RPCReceive rcv(socket);
00071         string desc_string;
00072 
00073         *rcv.archive & desc_string;
00074 
00075         return desc_string + " (remote)";
00076     }
00077 
00078     void mem_alloc(device_memory& mem, MemoryType type)
00079     {
00080 #if 0
00081         RPCSend snd(socket, "mem_alloc");
00082 
00083         snd.archive & size & type;
00084         snd.write();
00085 
00086         RPCReceive rcv(socket);
00087 
00088         device_ptr mem;
00089         *rcv.archive & mem;
00090 
00091         return mem;
00092 #endif
00093     }
00094 
00095     void mem_copy_to(device_memory& mem)
00096     {
00097 #if 0
00098         RPCSend snd(socket, "mem_copy_to");
00099 
00100         snd.archive & mem & size;
00101         snd.write();
00102         snd.write_buffer(host, size);
00103 #endif
00104     }
00105 
00106     void mem_copy_from(device_memory& mem, int y, int w, int h, int elem)
00107     {
00108 #if 0
00109         RPCSend snd(socket, "mem_copy_from");
00110 
00111         snd.archive & mem & offset & size;
00112         snd.write();
00113 
00114         RPCReceive rcv(socket);
00115         rcv.read_buffer(host, size);
00116 #endif
00117     }
00118 
00119     void mem_zero(device_memory& mem)
00120     {
00121 #if 0
00122         RPCSend snd(socket, "mem_zero");
00123 
00124         snd.archive & mem & size;
00125         snd.write();
00126 #endif
00127     }
00128 
00129     void mem_free(device_memory& mem)
00130     {
00131 #if 0
00132         if(mem) {
00133             RPCSend snd(socket, "mem_free");
00134 
00135             snd.archive & mem;
00136             snd.write();
00137         }
00138 #endif
00139     }
00140 
00141     void const_copy_to(const char *name, void *host, size_t size)
00142     {
00143         RPCSend snd(socket, "const_copy_to");
00144 
00145         string name_string(name);
00146 
00147         snd.archive & name_string & size;
00148         snd.write();
00149         snd.write_buffer(host, size);
00150     }
00151 
00152     void tex_alloc(const char *name, device_memory& mem, bool interpolation, bool periodic)
00153     {
00154 #if 0
00155         RPCSend snd(socket, "tex_alloc");
00156 
00157         string name_string(name);
00158 
00159         snd.archive & name_string & width & height & datatype & components & interpolation;
00160         snd.write();
00161 
00162         size_t size = width*height*components*datatype_size(datatype);
00163         snd.write_buffer(host, size);
00164 
00165         RPCReceive rcv(socket);
00166 
00167         device_ptr mem;
00168         *rcv.archive & mem;
00169 
00170         return mem;
00171 #endif
00172     }
00173 
00174     void tex_free(device_memory& mem)
00175     {
00176 #if 0
00177         if(mem) {
00178             RPCSend snd(socket, "tex_free");
00179 
00180             snd.archive & mem;
00181             snd.write();
00182         }
00183 #endif
00184     }
00185 
00186     void path_trace(int x, int y, int w, int h, device_ptr buffer, device_ptr rng_state, int sample)
00187     {
00188 #if 0
00189         RPCSend snd(socket, "path_trace");
00190 
00191         snd.archive & x & y & w & h & buffer & rng_state & sample;
00192         snd.write();
00193 #endif
00194     }
00195 
00196     void tonemap(int x, int y, int w, int h, device_ptr rgba, device_ptr buffer, int sample, int resolution)
00197     {
00198 #if 0
00199         RPCSend snd(socket, "tonemap");
00200 
00201         snd.archive & x & y & w & h & rgba & buffer & sample & resolution;
00202         snd.write();
00203 #endif
00204     }
00205 
00206     void task_add(DeviceTask& task)
00207     {
00208         if(task.type == DeviceTask::TONEMAP)
00209             tonemap(task.x, task.y, task.w, task.h, task.rgba, task.buffer, task.sample, task.resolution);
00210         else if(task.type == DeviceTask::PATH_TRACE)
00211             path_trace(task.x, task.y, task.w, task.h, task.buffer, task.rng_state, task.sample);
00212     }
00213 
00214     void task_wait()
00215     {
00216     }
00217 
00218     void task_cancel()
00219     {
00220     }
00221 };
00222 
00223 Device *device_network_create(DeviceInfo& info, const char *address)
00224 {
00225     return new NetworkDevice(address);
00226 }
00227 
00228 void device_network_info(vector<DeviceInfo>& devices)
00229 {
00230     DeviceInfo info;
00231 
00232     info.type = DEVICE_NETWORK;
00233     info.description = "Network Device";
00234     info.id = "NETWORK";
00235     info.num = 0;
00236 
00237     devices.push_back(info);
00238 }
00239 
00240 void Device::server_run()
00241 {
00242     try
00243     {
00244         /* starts thread that responds to discovery requests */
00245         ServerDiscovery discovery;
00246 
00247         for(;;)
00248         {
00249 
00250             /* accept connection */
00251             boost::asio::io_service io_service;
00252             tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), SERVER_PORT));
00253 
00254             tcp::socket socket(io_service);
00255             acceptor.accept(socket);
00256 
00257             /* receive remote function calls */
00258             for(;;) {
00259                 RPCReceive rcv(socket);
00260 
00261                 if(rcv.name == "description") {
00262                     string desc = description();
00263 
00264                     RPCSend snd(socket);
00265                     snd.archive & desc;
00266                     snd.write();
00267                 }
00268                 else if(rcv.name == "mem_alloc") {
00269 #if 0
00270                     MemoryType type;
00271                     size_t size;
00272                     device_ptr mem;
00273 
00274                     *rcv.archive & size & type;
00275                     mem = mem_alloc(size, type);
00276 
00277                     RPCSend snd(socket);
00278                     snd.archive & mem;
00279                     snd.write();
00280 #endif
00281                 }
00282                 else if(rcv.name == "mem_copy_to") {
00283 #if 0
00284                     device_ptr mem;
00285                     size_t size;
00286 
00287                     *rcv.archive & mem & size;
00288 
00289                     vector<char> host_vector(size);
00290                     rcv.read_buffer(&host_vector[0], size);
00291 
00292                     mem_copy_to(mem, &host_vector[0], size);
00293 #endif
00294                 }
00295                 else if(rcv.name == "mem_copy_from") {
00296 #if 0
00297                     device_ptr mem;
00298                     size_t offset, size;
00299 
00300                     *rcv.archive & mem & offset & size;
00301 
00302                     vector<char> host_vector(size);
00303 
00304                     mem_copy_from(&host_vector[0], mem, offset, size);
00305 
00306                     RPCSend snd(socket);
00307                     snd.write();
00308                     snd.write_buffer(&host_vector[0], size);
00309 #endif
00310                 }
00311                 else if(rcv.name == "mem_zero") {
00312 #if 0
00313                     device_ptr mem;
00314                     size_t size;
00315 
00316                     *rcv.archive & mem & size;
00317                     mem_zero(mem, size);
00318 #endif
00319                 }
00320                 else if(rcv.name == "mem_free") {
00321 #if 0
00322                     device_ptr mem;
00323 
00324                     *rcv.archive & mem;
00325                     mem_free(mem);
00326 #endif
00327                 }
00328                 else if(rcv.name == "const_copy_to") {
00329                     string name_string;
00330                     size_t size;
00331 
00332                     *rcv.archive & name_string & size;
00333 
00334                     vector<char> host_vector(size);
00335                     rcv.read_buffer(&host_vector[0], size);
00336 
00337                     const_copy_to(name_string.c_str(), &host_vector[0], size);
00338                 }
00339                 else if(rcv.name == "tex_alloc") {
00340 #if 0
00341                     string name_string;
00342                     DataType datatype;
00343                     device_ptr mem;
00344                     size_t width, height;
00345                     int components;
00346                     bool interpolation;
00347 
00348                     *rcv.archive & name_string & width & height & datatype & components & interpolation;
00349 
00350                     size_t size = width*height*components*datatype_size(datatype);
00351 
00352                     vector<char> host_vector(size);
00353                     rcv.read_buffer(&host_vector[0], size);
00354 
00355                     mem = tex_alloc(name_string.c_str(), &host_vector[0], width, height, datatype, components, interpolation);
00356 
00357                     RPCSend snd(socket);
00358                     snd.archive & mem;
00359                     snd.write();
00360 #endif
00361                 }
00362                 else if(rcv.name == "tex_free") {
00363 #if 0
00364                     device_ptr mem;
00365 
00366                     *rcv.archive & mem;
00367                     tex_free(mem);
00368 #endif
00369                 }
00370                 else if(rcv.name == "path_trace") {
00371 #if 0
00372                     device_ptr buffer, rng_state;
00373                     int x, y, w, h;
00374                     int sample;
00375 
00376                     *rcv.archive & x & y & w & h & buffer & rng_state & sample;
00377                     path_trace(x, y, w, h, buffer, rng_state, sample);
00378 #endif
00379                 }
00380                 else if(rcv.name == "tonemap") {
00381 #if 0
00382                     device_ptr rgba, buffer;
00383                     int x, y, w, h;
00384                     int sample, resolution;
00385 
00386                     *rcv.archive & x & y & w & h & rgba & buffer & sample & resolution;
00387                     tonemap(x, y, w, h, rgba, buffer, sample, resolution);
00388 #endif
00389                 }
00390             }
00391         }
00392     }
00393     catch(exception& e)
00394     {
00395         cerr << "Network server exception: " << e.what() << endl;
00396     }
00397 }
00398 
00399 #endif
00400 
00401 CCL_NAMESPACE_END
00402