00001 #ifndef GPUIP_H_
00002 #define GPUIP_H_
00003
00004 #include <string>
00005 #include <map>
00006 #include <vector>
00007 #include <iostream>
00008 #include <stdexcept>
00009
00010 #ifdef _GPUIP_PYTHON_BINDINGS
00011 #include <boost/shared_ptr.hpp>
00012 #else
00013 #include <tr1/memory>
00014 #endif
00015
00018 namespace gpuip {
00019
00022 #define GPUIP_ERROR -1.0
00023
00025 enum GpuEnvironment {
00028 OpenCL,
00029
00032 CUDA,
00033
00036 GLSL };
00037
00044 struct Buffer
00045 {
00047 #ifdef _GPUIP_PYTHON_BINDINGS
00048 typedef boost::shared_ptr<Buffer> Ptr;
00049 #else
00050 typedef std::tr1::shared_ptr<Buffer> Ptr;
00051 #endif
00052
00053 enum CopyOperation{
00055 COPY_TO_GPU,
00057 COPY_FROM_GPU };
00058
00060 enum Type{
00062 UNSIGNED_BYTE,
00063
00065 HALF,
00066
00069 FLOAT };
00070
00071 Buffer(const std::string & name, Type type, unsigned int channels);
00072
00077 const std::string name;
00078
00079 Type type;
00080
00085 unsigned int channels;
00086 };
00087
00092 template<typename T>
00093 struct Parameter
00094 {
00095 Parameter(const std::string & n, T v) : name(n), value(v) {}
00096 std::string name;
00097 T value;
00098 };
00099
00104 struct Kernel
00105 {
00107 #ifdef _GPUIP_PYTHON_BINDINGS
00108 typedef boost::shared_ptr<Kernel> Ptr;
00109 #else
00110 typedef std::tr1::shared_ptr<Kernel> Ptr;
00111 #endif
00112
00116 struct BufferLink
00117 {
00118 BufferLink(Buffer::Ptr buffer_, const std::string & name_);
00119
00121 Buffer::Ptr buffer;
00122
00126 std::string name;
00127 };
00128 Kernel(const std::string & name);
00129
00135 const std::string name;
00136
00140 std::string code;
00141
00145 std::vector<BufferLink> inBuffers;
00146
00150 std::vector<BufferLink> outBuffers;
00151
00155 std::vector<Parameter<int> > paramsInt;
00156
00160 std::vector<Parameter<float> > paramsFloat;
00161 };
00162
00167 class ImageProcessor
00168 {
00169 public:
00171 #ifdef _GPUIP_PYTHON_BINDINGS
00172 typedef boost::shared_ptr<ImageProcessor> Ptr;
00173 #else
00174 typedef std::tr1::shared_ptr<ImageProcessor> Ptr;
00175 #endif
00176
00178 static ImageProcessor::Ptr Create(GpuEnvironment env);
00179
00180 virtual ~ImageProcessor() {}
00181
00183 static bool CanCreate(GpuEnvironment env);
00184
00186 GpuEnvironment Environment() const
00187 {
00188 return _env;
00189 }
00190
00192 void SetDimensions(unsigned int width, unsigned int height);
00193
00195 unsigned int Width() const
00196 {
00197 return _w;
00198 }
00199
00201 unsigned int Height() const
00202 {
00203 return _h;
00204 }
00205
00218 Buffer::Ptr CreateBuffer(const std::string & name,
00219 Buffer::Type type,
00220 unsigned int channels);
00221
00235 Kernel::Ptr CreateKernel(const std::string & name);
00236
00246 virtual double Allocate(std::string * error);
00247
00255 virtual double Build(std::string * error);
00256
00265 virtual double Run(std::string * error);
00266
00279 virtual double Copy(Buffer::Ptr buffer,
00280 Buffer::CopyOperation operation,
00281 void * data,
00282 std::string * error);
00283
00293 virtual std::string BoilerplateCode(Kernel::Ptr kernel) const;
00294
00295 protected:
00296 ImageProcessor(GpuEnvironment env);
00297
00298 const GpuEnvironment _env;
00299 unsigned int _w;
00300 unsigned int _h;
00301 std::map<std::string, Buffer::Ptr> _buffers;
00302 std::vector<Kernel::Ptr> _kernels;
00303
00304 unsigned int _BufferSize(Buffer::Ptr buffer) const;
00305
00306 private:
00307 ImageProcessor();
00308 ImageProcessor(const ImageProcessor &);
00309 void operator=(const ImageProcessor &);
00310 };
00311
00312 }
00313
00314 #endif