texture.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #pragma once
  44. #ifndef OPENCV_CUDEV_PTR2D_TEXTURE_HPP
  45. #define OPENCV_CUDEV_PTR2D_TEXTURE_HPP
  46. #include <cstring>
  47. #include "../common.hpp"
  48. #include "glob.hpp"
  49. #include "gpumat.hpp"
  50. #include "traits.hpp"
  51. #if CUDART_VERSION >= 5050
  52. namespace
  53. {
  54. template <typename T> struct CvCudevTextureRef
  55. {
  56. typedef texture<T, cudaTextureType2D, cudaReadModeElementType> TexRef;
  57. static TexRef ref;
  58. __host__ static void bind(const cv::cudev::GlobPtrSz<T>& mat,
  59. bool normalizedCoords = false,
  60. cudaTextureFilterMode filterMode = cudaFilterModePoint,
  61. cudaTextureAddressMode addressMode = cudaAddressModeClamp)
  62. {
  63. ref.normalized = normalizedCoords;
  64. ref.filterMode = filterMode;
  65. ref.addressMode[0] = addressMode;
  66. ref.addressMode[1] = addressMode;
  67. ref.addressMode[2] = addressMode;
  68. cudaChannelFormatDesc desc = cudaCreateChannelDesc<T>();
  69. CV_CUDEV_SAFE_CALL( cudaBindTexture2D(0, &ref, mat.data, &desc, mat.cols, mat.rows, mat.step) );
  70. }
  71. __host__ static void unbind()
  72. {
  73. cudaUnbindTexture(ref);
  74. }
  75. };
  76. template <typename T>
  77. typename CvCudevTextureRef<T>::TexRef CvCudevTextureRef<T>::ref;
  78. }
  79. #endif
  80. namespace cv { namespace cudev {
  81. //! @addtogroup cudev
  82. //! @{
  83. #if CUDART_VERSION >= 5050
  84. template <typename T> struct TexturePtr
  85. {
  86. typedef T value_type;
  87. typedef float index_type;
  88. cudaTextureObject_t texObj;
  89. __device__ __forceinline__ T operator ()(float y, float x) const
  90. {
  91. #if CV_CUDEV_ARCH < 300
  92. // Use the texture reference
  93. return tex2D(CvCudevTextureRef<T>::ref, x, y);
  94. #else
  95. // Use the texture object
  96. return tex2D<T>(texObj, x, y);
  97. #endif
  98. }
  99. };
  100. template <typename T> struct Texture : TexturePtr<T>
  101. {
  102. int rows, cols;
  103. bool cc30;
  104. __host__ explicit Texture(const GlobPtrSz<T>& mat,
  105. bool normalizedCoords = false,
  106. cudaTextureFilterMode filterMode = cudaFilterModePoint,
  107. cudaTextureAddressMode addressMode = cudaAddressModeClamp)
  108. {
  109. cc30 = deviceSupports(FEATURE_SET_COMPUTE_30);
  110. rows = mat.rows;
  111. cols = mat.cols;
  112. if (cc30)
  113. {
  114. // Use the texture object
  115. cudaResourceDesc texRes;
  116. std::memset(&texRes, 0, sizeof(texRes));
  117. texRes.resType = cudaResourceTypePitch2D;
  118. texRes.res.pitch2D.devPtr = mat.data;
  119. texRes.res.pitch2D.height = mat.rows;
  120. texRes.res.pitch2D.width = mat.cols;
  121. texRes.res.pitch2D.pitchInBytes = mat.step;
  122. texRes.res.pitch2D.desc = cudaCreateChannelDesc<T>();
  123. cudaTextureDesc texDescr;
  124. std::memset(&texDescr, 0, sizeof(texDescr));
  125. texDescr.normalizedCoords = normalizedCoords;
  126. texDescr.filterMode = filterMode;
  127. texDescr.addressMode[0] = addressMode;
  128. texDescr.addressMode[1] = addressMode;
  129. texDescr.addressMode[2] = addressMode;
  130. texDescr.readMode = cudaReadModeElementType;
  131. CV_CUDEV_SAFE_CALL( cudaCreateTextureObject(&this->texObj, &texRes, &texDescr, 0) );
  132. }
  133. else
  134. {
  135. // Use the texture reference
  136. CvCudevTextureRef<T>::bind(mat, normalizedCoords, filterMode, addressMode);
  137. }
  138. }
  139. __host__ ~Texture()
  140. {
  141. if (cc30)
  142. {
  143. // Use the texture object
  144. cudaDestroyTextureObject(this->texObj);
  145. }
  146. else
  147. {
  148. // Use the texture reference
  149. CvCudevTextureRef<T>::unbind();
  150. }
  151. }
  152. };
  153. template <typename T> struct PtrTraits< Texture<T> > : PtrTraitsBase<Texture<T>, TexturePtr<T> >
  154. {
  155. };
  156. #else
  157. template <typename T> struct TexturePtr
  158. {
  159. typedef T value_type;
  160. typedef float index_type;
  161. cudaTextureObject_t texObj;
  162. __device__ __forceinline__ T operator ()(float y, float x) const
  163. {
  164. #if CV_CUDEV_ARCH >= 300
  165. // Use the texture object
  166. return tex2D<T>(texObj, x, y);
  167. #else
  168. CV_UNUSED(y);
  169. CV_UNUSED(x);
  170. return T();
  171. #endif
  172. }
  173. };
  174. template <typename T> struct Texture : TexturePtr<T>
  175. {
  176. int rows, cols;
  177. __host__ explicit Texture(const GlobPtrSz<T>& mat,
  178. bool normalizedCoords = false,
  179. cudaTextureFilterMode filterMode = cudaFilterModePoint,
  180. cudaTextureAddressMode addressMode = cudaAddressModeClamp)
  181. {
  182. CV_Assert( deviceSupports(FEATURE_SET_COMPUTE_30) );
  183. rows = mat.rows;
  184. cols = mat.cols;
  185. // Use the texture object
  186. cudaResourceDesc texRes;
  187. std::memset(&texRes, 0, sizeof(texRes));
  188. texRes.resType = cudaResourceTypePitch2D;
  189. texRes.res.pitch2D.devPtr = mat.data;
  190. texRes.res.pitch2D.height = mat.rows;
  191. texRes.res.pitch2D.width = mat.cols;
  192. texRes.res.pitch2D.pitchInBytes = mat.step;
  193. texRes.res.pitch2D.desc = cudaCreateChannelDesc<T>();
  194. cudaTextureDesc texDescr;
  195. std::memset(&texDescr, 0, sizeof(texDescr));
  196. texDescr.normalizedCoords = normalizedCoords;
  197. texDescr.filterMode = filterMode;
  198. texDescr.addressMode[0] = addressMode;
  199. texDescr.addressMode[1] = addressMode;
  200. texDescr.addressMode[2] = addressMode;
  201. texDescr.readMode = cudaReadModeElementType;
  202. CV_CUDEV_SAFE_CALL( cudaCreateTextureObject(&this->texObj, &texRes, &texDescr, 0) );
  203. }
  204. __host__ ~Texture()
  205. {
  206. // Use the texture object
  207. cudaDestroyTextureObject(this->texObj);
  208. }
  209. };
  210. template <typename T> struct PtrTraits< Texture<T> > : PtrTraitsBase<Texture<T>, TexturePtr<T> >
  211. {
  212. };
  213. #endif
  214. //! @}
  215. }}
  216. #endif