Blender V2.61 - r43446

device.h

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 #ifndef __DEVICE_H__
00020 #define __DEVICE_H__
00021 
00022 #include <stdlib.h>
00023 
00024 #include "device_memory.h"
00025 
00026 #include "util_list.h"
00027 #include "util_string.h"
00028 #include "util_thread.h"
00029 #include "util_types.h"
00030 #include "util_vector.h"
00031 
00032 CCL_NAMESPACE_BEGIN
00033 
00034 class Progress;
00035 
00036 /* Device Types */
00037 
00038 enum DeviceType {
00039     DEVICE_NONE,
00040     DEVICE_CPU,
00041     DEVICE_OPENCL,
00042     DEVICE_CUDA,
00043     DEVICE_NETWORK,
00044     DEVICE_MULTI
00045 };
00046 
00047 class DeviceInfo {
00048 public:
00049     DeviceType type;
00050     string description;
00051     string id;
00052     int num;
00053     bool display_device;
00054     vector<DeviceInfo> multi_devices;
00055 
00056     DeviceInfo()
00057     {
00058         type = DEVICE_CPU;
00059         id = "CPU";
00060         num = 0;
00061         display_device = false;
00062     }
00063 };
00064 
00065 /* Device Task */
00066 
00067 class DeviceTask {
00068 public:
00069     typedef enum { PATH_TRACE, TONEMAP, SHADER } Type;
00070     Type type;
00071 
00072     int x, y, w, h;
00073     device_ptr rng_state;
00074     device_ptr rgba;
00075     device_ptr buffer;
00076     int sample;
00077     int resolution;
00078     int offset, stride;
00079 
00080     device_ptr shader_input;
00081     device_ptr shader_output;
00082     int shader_eval_type;
00083     int shader_x, shader_w;
00084 
00085     DeviceTask(Type type = PATH_TRACE);
00086 
00087     void split(list<DeviceTask>& tasks, int num);
00088     void split(ThreadQueue<DeviceTask>& tasks, int num);
00089     void split_max_size(list<DeviceTask>& tasks, int max_size);
00090 };
00091 
00092 /* Device */
00093 
00094 class Device {
00095 protected:
00096     Device() {}
00097 
00098     bool background;
00099     string error_msg;
00100 
00101 public:
00102     virtual ~Device() {}
00103 
00104     virtual bool support_full_kernel() = 0;
00105 
00106     /* info */
00107     virtual string description() = 0;
00108     virtual const string& error_message() { return error_msg; }
00109 
00110     /* regular memory */
00111     virtual void mem_alloc(device_memory& mem, MemoryType type) = 0;
00112     virtual void mem_copy_to(device_memory& mem) = 0;
00113     virtual void mem_copy_from(device_memory& mem,
00114         int y, int w, int h, int elem) = 0;
00115     virtual void mem_zero(device_memory& mem) = 0;
00116     virtual void mem_free(device_memory& mem) = 0;
00117 
00118     /* constant memory */
00119     virtual void const_copy_to(const char *name, void *host, size_t size) = 0;
00120 
00121     /* texture memory */
00122     virtual void tex_alloc(const char *name, device_memory& mem,
00123         bool interpolation = false, bool periodic = false) {};
00124     virtual void tex_free(device_memory& mem) {};
00125 
00126     /* pixel memory */
00127     virtual void pixels_alloc(device_memory& mem);
00128     virtual void pixels_copy_from(device_memory& mem, int y, int w, int h);
00129     virtual void pixels_free(device_memory& mem);
00130 
00131     /* open shading language, only for CPU device */
00132     virtual void *osl_memory() { return NULL; }
00133 
00134     /* load/compile kernels, must be called before adding tasks */ 
00135     virtual bool load_kernels(bool experimental) { return true; }
00136 
00137     /* tasks */
00138     virtual void task_add(DeviceTask& task) = 0;
00139     virtual void task_wait() = 0;
00140     virtual void task_cancel() = 0;
00141     
00142     /* opengl drawing */
00143     virtual void draw_pixels(device_memory& mem, int y, int w, int h,
00144         int dy, int width, int height, bool transparent);
00145 
00146 #ifdef WITH_NETWORK
00147     /* networking */
00148     void server_run();
00149 #endif
00150 
00151     /* static */
00152     static Device *create(DeviceInfo& info, bool background = true, int threads = 0);
00153 
00154     static DeviceType type_from_string(const char *name);
00155     static string string_from_type(DeviceType type);
00156     static vector<DeviceType>& available_types();
00157     static vector<DeviceInfo>& available_devices();
00158 };
00159 
00160 CCL_NAMESPACE_END
00161 
00162 #endif /* __DEVICE_H__ */
00163