image1d.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2015 Kyle Lutz <[email protected]>
  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_IMAGE1D_HPP
  11. #define BOOST_COMPUTE_IMAGE_IMAGE1D_HPP
  12. #include <boost/throw_exception.hpp>
  13. #include <boost/compute/config.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 image1d
  24. /// \brief An OpenCL 1D image object
  25. ///
  26. /// \opencl_version_warning{1,2}
  27. ///
  28. /// \see image_format, image2d
  29. class image1d : public image_object
  30. {
  31. public:
  32. /// Creates a null image1d object.
  33. image1d()
  34. : image_object()
  35. {
  36. }
  37. /// Creates a new image1d object.
  38. ///
  39. /// \see_opencl_ref{clCreateImage}
  40. image1d(const context &context,
  41. size_t image_width,
  42. const image_format &format,
  43. cl_mem_flags flags = read_write,
  44. void *host_ptr = 0)
  45. {
  46. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  47. cl_image_desc desc;
  48. desc.image_type = CL_MEM_OBJECT_IMAGE1D;
  49. desc.image_width = image_width;
  50. desc.image_height = 1;
  51. desc.image_depth = 1;
  52. desc.image_array_size = 0;
  53. desc.image_row_pitch = 0;
  54. desc.image_slice_pitch = 0;
  55. desc.num_mip_levels = 0;
  56. desc.num_samples = 0;
  57. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  58. desc.mem_object = 0;
  59. #else
  60. desc.buffer = 0;
  61. #endif
  62. cl_int error = 0;
  63. m_mem = clCreateImage(
  64. context, flags, format.get_format_ptr(), &desc, host_ptr, &error
  65. );
  66. if(!m_mem){
  67. BOOST_THROW_EXCEPTION(opencl_error(error));
  68. }
  69. #else
  70. // image1d objects are only supported in OpenCL 1.2 and later
  71. BOOST_THROW_EXCEPTION(opencl_error(CL_IMAGE_FORMAT_NOT_SUPPORTED));
  72. #endif
  73. }
  74. /// Creates a new image1d as a copy of \p other.
  75. image1d(const image1d &other)
  76. : image_object(other)
  77. {
  78. }
  79. /// Copies the image1d from \p other.
  80. image1d& operator=(const image1d &other)
  81. {
  82. image_object::operator=(other);
  83. return *this;
  84. }
  85. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  86. /// Move-constructs a new image object from \p other.
  87. image1d(image1d&& other) BOOST_NOEXCEPT
  88. : image_object(std::move(other))
  89. {
  90. }
  91. /// Move-assigns the image from \p other to \c *this.
  92. image1d& operator=(image1d&& other) BOOST_NOEXCEPT
  93. {
  94. image_object::operator=(std::move(other));
  95. return *this;
  96. }
  97. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  98. /// Destroys the image1d object.
  99. ~image1d()
  100. {
  101. }
  102. /// Returns the size (width) of the image.
  103. extents<1> size() const
  104. {
  105. extents<1> size;
  106. size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
  107. return size;
  108. }
  109. /// Returns the origin of the image (\c 0).
  110. extents<1> origin() const
  111. {
  112. return extents<1>();
  113. }
  114. /// Returns information about the image.
  115. ///
  116. /// \see_opencl_ref{clGetImageInfo}
  117. template<class T>
  118. T get_info(cl_image_info info) const
  119. {
  120. return get_image_info<T>(info);
  121. }
  122. /// \overload
  123. template<int Enum>
  124. typename detail::get_object_info_type<image1d, Enum>::type
  125. get_info() const;
  126. /// Returns the supported image formats for the context.
  127. ///
  128. /// \see_opencl_ref{clGetSupportedImageFormats}
  129. static std::vector<image_format>
  130. get_supported_formats(const context &context, cl_mem_flags flags = read_write)
  131. {
  132. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  133. return image_object::get_supported_formats(context, CL_MEM_OBJECT_IMAGE1D, flags);
  134. #else
  135. return std::vector<image_format>();
  136. #endif
  137. }
  138. /// Returns \c true if \p format is a supported 1D image format for
  139. /// \p context.
  140. static bool is_supported_format(const image_format &format,
  141. const context &context,
  142. cl_mem_flags flags = read_write)
  143. {
  144. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  145. return image_object::is_supported_format(
  146. format, context, CL_MEM_OBJECT_IMAGE1D, flags
  147. );
  148. #else
  149. return false;
  150. #endif
  151. }
  152. /// Creates a new image with a copy of the data in \c *this. Uses \p queue
  153. /// to perform the copy operation.
  154. image1d clone(command_queue &queue) const;
  155. };
  156. /// \internal_ define get_info() specializations for image1d
  157. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image1d,
  158. ((cl_image_format, CL_IMAGE_FORMAT))
  159. ((size_t, CL_IMAGE_ELEMENT_SIZE))
  160. ((size_t, CL_IMAGE_ROW_PITCH))
  161. ((size_t, CL_IMAGE_SLICE_PITCH))
  162. ((size_t, CL_IMAGE_WIDTH))
  163. ((size_t, CL_IMAGE_HEIGHT))
  164. ((size_t, CL_IMAGE_DEPTH))
  165. )
  166. namespace detail {
  167. // set_kernel_arg() specialization for image1d
  168. template<>
  169. struct set_kernel_arg<image1d> : public set_kernel_arg<image_object> { };
  170. } // end detail namespace
  171. } // end compute namespace
  172. } // end boost namespace
  173. BOOST_COMPUTE_TYPE_NAME(boost::compute::image1d, image1d_t)
  174. #endif // BOOST_COMPUTE_IMAGE_IMAGE1D_HPP