image2d.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_IMAGE_IMAGE2D_HPP
  11. #define BOOST_COMPUTE_IMAGE_IMAGE2D_HPP
  12. #include <boost/throw_exception.hpp>
  13. #include <boost/compute/config.hpp>
  14. #include <boost/compute/context.hpp>
  15. #include <boost/compute/exception/opencl_error.hpp>
  16. #include <boost/compute/image/image_format.hpp>
  17. #include <boost/compute/image/image_object.hpp>
  18. #include <boost/compute/detail/get_object_info.hpp>
  19. #include <boost/compute/type_traits/type_name.hpp>
  20. #include <boost/compute/utility/extents.hpp>
  21. namespace boost {
  22. namespace compute {
  23. // forward declarations
  24. class command_queue;
  25. /// \class image2d
  26. /// \brief An OpenCL 2D image object
  27. ///
  28. /// For example, to create a 640x480 8-bit RGBA image:
  29. ///
  30. /// \snippet test/test_image2d.cpp create_image
  31. ///
  32. /// \see image_format, image3d
  33. class image2d : public image_object
  34. {
  35. public:
  36. /// Creates a null image2d object.
  37. image2d()
  38. : image_object()
  39. {
  40. }
  41. /// Creates a new image2d object.
  42. ///
  43. /// \see_opencl_ref{clCreateImage}
  44. image2d(const context &context,
  45. size_t image_width,
  46. size_t image_height,
  47. const image_format &format,
  48. cl_mem_flags flags = read_write,
  49. void *host_ptr = 0,
  50. size_t image_row_pitch = 0)
  51. {
  52. cl_int error = 0;
  53. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  54. cl_image_desc desc;
  55. desc.image_type = CL_MEM_OBJECT_IMAGE2D;
  56. desc.image_width = image_width;
  57. desc.image_height = image_height;
  58. desc.image_depth = 1;
  59. desc.image_array_size = 0;
  60. desc.image_row_pitch = image_row_pitch;
  61. desc.image_slice_pitch = 0;
  62. desc.num_mip_levels = 0;
  63. desc.num_samples = 0;
  64. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  65. desc.mem_object = 0;
  66. #else
  67. desc.buffer = 0;
  68. #endif
  69. m_mem = clCreateImage(context,
  70. flags,
  71. format.get_format_ptr(),
  72. &desc,
  73. host_ptr,
  74. &error);
  75. #else
  76. m_mem = clCreateImage2D(context,
  77. flags,
  78. format.get_format_ptr(),
  79. image_width,
  80. image_height,
  81. image_row_pitch,
  82. host_ptr,
  83. &error);
  84. #endif
  85. if(!m_mem){
  86. BOOST_THROW_EXCEPTION(opencl_error(error));
  87. }
  88. }
  89. /// \internal_ (deprecated)
  90. image2d(const context &context,
  91. cl_mem_flags flags,
  92. const image_format &format,
  93. size_t image_width,
  94. size_t image_height,
  95. size_t image_row_pitch = 0,
  96. void *host_ptr = 0)
  97. {
  98. cl_int error = 0;
  99. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  100. cl_image_desc desc;
  101. desc.image_type = CL_MEM_OBJECT_IMAGE2D;
  102. desc.image_width = image_width;
  103. desc.image_height = image_height;
  104. desc.image_depth = 1;
  105. desc.image_array_size = 0;
  106. desc.image_row_pitch = image_row_pitch;
  107. desc.image_slice_pitch = 0;
  108. desc.num_mip_levels = 0;
  109. desc.num_samples = 0;
  110. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  111. desc.mem_object = 0;
  112. #else
  113. desc.buffer = 0;
  114. #endif
  115. m_mem = clCreateImage(context,
  116. flags,
  117. format.get_format_ptr(),
  118. &desc,
  119. host_ptr,
  120. &error);
  121. #else
  122. m_mem = clCreateImage2D(context,
  123. flags,
  124. format.get_format_ptr(),
  125. image_width,
  126. image_height,
  127. image_row_pitch,
  128. host_ptr,
  129. &error);
  130. #endif
  131. if(!m_mem){
  132. BOOST_THROW_EXCEPTION(opencl_error(error));
  133. }
  134. }
  135. /// Creates a new image2d as a copy of \p other.
  136. image2d(const image2d &other)
  137. : image_object(other)
  138. {
  139. }
  140. /// Copies the image2d from \p other.
  141. image2d& operator=(const image2d &other)
  142. {
  143. image_object::operator=(other);
  144. return *this;
  145. }
  146. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  147. /// Move-constructs a new image object from \p other.
  148. image2d(image2d&& other) BOOST_NOEXCEPT
  149. : image_object(std::move(other))
  150. {
  151. }
  152. /// Move-assigns the image from \p other to \c *this.
  153. image2d& operator=(image2d&& other) BOOST_NOEXCEPT
  154. {
  155. image_object::operator=(std::move(other));
  156. return *this;
  157. }
  158. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  159. /// Destroys the image2d object.
  160. ~image2d()
  161. {
  162. }
  163. /// Returns the size (width, height) of the image.
  164. extents<2> size() const
  165. {
  166. extents<2> size;
  167. size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
  168. size[1] = get_info<size_t>(CL_IMAGE_HEIGHT);
  169. return size;
  170. }
  171. /// Returns the origin of the image (\c 0, \c 0).
  172. extents<2> origin() const
  173. {
  174. return extents<2>();
  175. }
  176. /// Returns information about the image.
  177. ///
  178. /// \see_opencl_ref{clGetImageInfo}
  179. template<class T>
  180. T get_info(cl_image_info info) const
  181. {
  182. return detail::get_object_info<T>(clGetImageInfo, m_mem, info);
  183. }
  184. /// \overload
  185. template<int Enum>
  186. typename detail::get_object_info_type<image2d, Enum>::type
  187. get_info() const;
  188. /// Returns the supported image formats for the context.
  189. ///
  190. /// \see_opencl_ref{clGetSupportedImageFormats}
  191. static std::vector<image_format>
  192. get_supported_formats(const context &context, cl_mem_flags flags = read_write)
  193. {
  194. return image_object::get_supported_formats(context, CL_MEM_OBJECT_IMAGE2D, flags);
  195. }
  196. /// Returns \c true if \p format is a supported 2D image format for
  197. /// \p context.
  198. static bool is_supported_format(const image_format &format,
  199. const context &context,
  200. cl_mem_flags flags = read_write)
  201. {
  202. return image_object::is_supported_format(
  203. format, context, CL_MEM_OBJECT_IMAGE2D, flags
  204. );
  205. }
  206. /// Creates a new image with a copy of the data in \c *this. Uses \p queue
  207. /// to perform the copy operation.
  208. image2d clone(command_queue &queue) const;
  209. };
  210. /// \internal_ define get_info() specializations for image2d
  211. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image2d,
  212. ((cl_image_format, CL_IMAGE_FORMAT))
  213. ((size_t, CL_IMAGE_ELEMENT_SIZE))
  214. ((size_t, CL_IMAGE_ROW_PITCH))
  215. ((size_t, CL_IMAGE_SLICE_PITCH))
  216. ((size_t, CL_IMAGE_WIDTH))
  217. ((size_t, CL_IMAGE_HEIGHT))
  218. ((size_t, CL_IMAGE_DEPTH))
  219. )
  220. namespace detail {
  221. // set_kernel_arg() specialization for image2d
  222. template<>
  223. struct set_kernel_arg<image2d> : public set_kernel_arg<image_object> { };
  224. } // end detail namespace
  225. } // end compute namespace
  226. } // end boost namespace
  227. BOOST_COMPUTE_TYPE_NAME(boost::compute::image2d, image2d_t)
  228. #endif // BOOST_COMPUTE_IMAGE_IMAGE2D_HPP