image3d.hpp 7.9 KB

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