include/opencv2/core/cuda_devptrs.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other GpuMaterials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_CORE_DEVPTRS_HPP__
00044 #define __OPENCV_CORE_DEVPTRS_HPP__
00045 
00046 #ifdef __cplusplus
00047 
00048 #ifdef __CUDACC__
00049     #define __CV_GPU_HOST_DEVICE__ __host__ __device__ __forceinline__
00050 #else
00051     #define __CV_GPU_HOST_DEVICE__
00052 #endif
00053 
00054 namespace cv
00055 {
00056     namespace gpu
00057     {
00058         // Simple lightweight structures that encapsulates information about an image on device.
00059         // It is intended to pass to nvcc-compiled code. GpuMat depends on headers that nvcc can't compile
00060 
00061         template <bool expr> struct StaticAssert;
00062         template <> struct StaticAssert<true> {static __CV_GPU_HOST_DEVICE__ void check(){}};
00063 
00064         template<typename T> struct DevPtr
00065         {
00066             typedef T elem_type;
00067             typedef int index_type;
00068 
00069             enum { elem_size = sizeof(elem_type) };
00070 
00071             T* data;
00072 
00073             __CV_GPU_HOST_DEVICE__ DevPtr() : data(0) {}
00074             __CV_GPU_HOST_DEVICE__ DevPtr(T* data_) : data(data_) {}
00075 
00076             __CV_GPU_HOST_DEVICE__ size_t elemSize() const { return elem_size; }
00077             __CV_GPU_HOST_DEVICE__ operator       T*()       { return data; }
00078             __CV_GPU_HOST_DEVICE__ operator const T*() const { return data; }
00079         };
00080 
00081         template<typename T> struct PtrSz : public DevPtr<T>
00082         {
00083             __CV_GPU_HOST_DEVICE__ PtrSz() : size(0) {}
00084             __CV_GPU_HOST_DEVICE__ PtrSz(T* data_, size_t size_) : DevPtr<T>(data_), size(size_) {}
00085 
00086             size_t size;
00087         };
00088 
00089         template<typename T> struct PtrStep : public DevPtr<T>
00090         {
00091             __CV_GPU_HOST_DEVICE__ PtrStep() : step(0) {}
00092             __CV_GPU_HOST_DEVICE__ PtrStep(T* data_, size_t step_) : DevPtr<T>(data_), step(step_) {}
00093 
00095             size_t step;
00096 
00097             __CV_GPU_HOST_DEVICE__       T* ptr(int y = 0)       { return (      T*)( (      char*)DevPtr<T>::data + y * step); }
00098             __CV_GPU_HOST_DEVICE__ const T* ptr(int y = 0) const { return (const T*)( (const char*)DevPtr<T>::data + y * step); }
00099 
00100             __CV_GPU_HOST_DEVICE__       T& operator ()(int y, int x)       { return ptr(y)[x]; }
00101             __CV_GPU_HOST_DEVICE__ const T& operator ()(int y, int x) const { return ptr(y)[x]; }
00102         };
00103 
00104         template <typename T> struct PtrStepSz : public PtrStep<T>
00105         {
00106             __CV_GPU_HOST_DEVICE__ PtrStepSz() : cols(0), rows(0) {}
00107             __CV_GPU_HOST_DEVICE__ PtrStepSz(int rows_, int cols_, T* data_, size_t step_)
00108                 : PtrStep<T>(data_, step_), cols(cols_), rows(rows_) {}
00109 
00110             template <typename U>
00111             explicit PtrStepSz(const PtrStepSz<U>& d) : PtrStep<T>((T*)d.data, d.step), cols(d.cols), rows(d.rows){}
00112 
00113             int cols;
00114             int rows;
00115         };
00116 
00117         typedef PtrStepSz<unsigned char> PtrStepSzb;
00118         typedef PtrStepSz<float> PtrStepSzf;
00119         typedef PtrStepSz<int> PtrStepSzi;
00120 
00121         typedef PtrStep<unsigned char> PtrStepb;
00122         typedef PtrStep<float> PtrStepf;
00123         typedef PtrStep<int> PtrStepi;
00124 
00125 
00126 #if defined __GNUC__
00127     #define __CV_GPU_DEPR_BEFORE__
00128     #define __CV_GPU_DEPR_AFTER__ __attribute__ ((deprecated))
00129 #elif defined(__MSVC__) //|| defined(__CUDACC__)
00130     #pragma deprecated(DevMem2D_)
00131     #define __CV_GPU_DEPR_BEFORE__ __declspec(deprecated)
00132     #define __CV_GPU_DEPR_AFTER__
00133 #else
00134     #define __CV_GPU_DEPR_BEFORE__
00135     #define __CV_GPU_DEPR_AFTER__
00136 #endif
00137 
00138         template <typename T> struct __CV_GPU_DEPR_BEFORE__ DevMem2D_ : public PtrStepSz<T>
00139         {
00140             DevMem2D_() {}
00141             DevMem2D_(int rows_, int cols_, T* data_, size_t step_) : PtrStepSz<T>(rows_, cols_, data_, step_) {}
00142 
00143             template <typename U>
00144             explicit __CV_GPU_DEPR_BEFORE__ DevMem2D_(const DevMem2D_<U>& d) : PtrStepSz<T>(d.rows, d.cols, (T*)d.data, d.step) {}
00145         } __CV_GPU_DEPR_AFTER__ ;
00146 
00147         typedef DevMem2D_<unsigned char> DevMem2Db;
00148         typedef DevMem2Db DevMem2D;
00149         typedef DevMem2D_<float> DevMem2Df;
00150         typedef DevMem2D_<int> DevMem2Di;
00151 
00152         template<typename T> struct PtrElemStep_ : public PtrStep<T>
00153         {
00154             PtrElemStep_(const DevMem2D_<T>& mem) : PtrStep<T>(mem.data, mem.step)
00155             {
00156                 StaticAssert<256 % sizeof(T) == 0>::check();
00157 
00158                 PtrStep<T>::step /= PtrStep<T>::elem_size;
00159             }
00160             __CV_GPU_HOST_DEVICE__ T* ptr(int y = 0) { return PtrStep<T>::data + y * PtrStep<T>::step; }
00161             __CV_GPU_HOST_DEVICE__ const T* ptr(int y = 0) const { return PtrStep<T>::data + y * PtrStep<T>::step; }
00162 
00163             __CV_GPU_HOST_DEVICE__ T& operator ()(int y, int x) { return ptr(y)[x]; }
00164             __CV_GPU_HOST_DEVICE__ const T& operator ()(int y, int x) const { return ptr(y)[x]; }
00165         };
00166 
00167         template<typename T> struct PtrStep_ : public PtrStep<T>
00168         {
00169             PtrStep_() {}
00170             PtrStep_(const DevMem2D_<T>& mem) : PtrStep<T>(mem.data, mem.step) {}
00171         };
00172 
00173         typedef PtrElemStep_<unsigned char> PtrElemStep;
00174         typedef PtrElemStep_<float> PtrElemStepf;
00175         typedef PtrElemStep_<int> PtrElemStepi;
00176 
00177 //#undef __CV_GPU_DEPR_BEFORE__
00178 //#undef __CV_GPU_DEPR_AFTER__
00179 
00180     }
00181 }
00182 
00183 #endif // __cplusplus
00184 
00185 #endif /* __OPENCV_CORE_DEVPTRS_HPP__ */