image_object.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_IMAGE_OBJECT_HPP
  11. #define BOOST_COMPUTE_IMAGE_IMAGE_OBJECT_HPP
  12. #include <algorithm>
  13. #include <vector>
  14. #include <boost/compute/config.hpp>
  15. #include <boost/compute/memory_object.hpp>
  16. #include <boost/compute/detail/get_object_info.hpp>
  17. #include <boost/compute/image/image_format.hpp>
  18. namespace boost {
  19. namespace compute {
  20. /// \class image_object
  21. /// \brief Base-class for image objects.
  22. ///
  23. /// The image_object class is the base-class for image objects on compute
  24. /// devices.
  25. ///
  26. /// \see image1d, image2d, image3d
  27. class image_object : public memory_object
  28. {
  29. public:
  30. image_object()
  31. : memory_object()
  32. {
  33. }
  34. explicit image_object(cl_mem mem, bool retain = true)
  35. : memory_object(mem, retain)
  36. {
  37. }
  38. image_object(const image_object &other)
  39. : memory_object(other)
  40. {
  41. }
  42. image_object& operator=(const image_object &other)
  43. {
  44. if(this != &other){
  45. memory_object::operator=(other);
  46. }
  47. return *this;
  48. }
  49. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  50. image_object(image_object&& other) BOOST_NOEXCEPT
  51. : memory_object(std::move(other))
  52. {
  53. }
  54. /// \internal_
  55. image_object& operator=(image_object&& other) BOOST_NOEXCEPT
  56. {
  57. memory_object::operator=(std::move(other));
  58. return *this;
  59. }
  60. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  61. /// Destroys the image object.
  62. ~image_object()
  63. {
  64. }
  65. /// Returns information about the image object.
  66. ///
  67. /// \see_opencl_ref{clGetImageInfo}
  68. template<class T>
  69. T get_image_info(cl_mem_info info) const
  70. {
  71. return detail::get_object_info<T>(clGetImageInfo, m_mem, info);
  72. }
  73. /// Returns the format for the image.
  74. image_format format() const
  75. {
  76. return image_format(get_image_info<cl_image_format>(CL_IMAGE_FORMAT));
  77. }
  78. /// \internal_ (deprecated)
  79. image_format get_format() const
  80. {
  81. return format();
  82. }
  83. /// Returns the width of the image.
  84. size_t width() const
  85. {
  86. return get_image_info<size_t>(CL_IMAGE_WIDTH);
  87. }
  88. /// Returns the height of the image.
  89. ///
  90. /// For 1D images, this function will return \c 1.
  91. size_t height() const
  92. {
  93. return get_image_info<size_t>(CL_IMAGE_HEIGHT);
  94. }
  95. /// Returns the depth of the image.
  96. ///
  97. /// For 1D and 2D images, this function will return \c 1.
  98. size_t depth() const
  99. {
  100. return get_image_info<size_t>(CL_IMAGE_DEPTH);
  101. }
  102. /// Returns the supported image formats for the \p type in \p context.
  103. ///
  104. /// \see_opencl_ref{clGetSupportedImageFormats}
  105. static std::vector<image_format>
  106. get_supported_formats(const context &context,
  107. cl_mem_object_type type,
  108. cl_mem_flags flags = read_write)
  109. {
  110. cl_uint count = 0;
  111. clGetSupportedImageFormats(context, flags, type, 0, 0, &count);
  112. std::vector<cl_image_format> cl_formats(count);
  113. clGetSupportedImageFormats(context, flags, type, count, &cl_formats[0], 0);
  114. std::vector<image_format> formats;
  115. formats.reserve(count);
  116. for(cl_uint i = 0; i < count; i++){
  117. formats.push_back(image_format(cl_formats[i]));
  118. }
  119. return formats;
  120. }
  121. /// Returns \c true if \p format is a supported image format for
  122. /// \p type in \p context with \p flags.
  123. static bool is_supported_format(const image_format &format,
  124. const context &context,
  125. cl_mem_object_type type,
  126. cl_mem_flags flags = read_write)
  127. {
  128. const std::vector<image_format> formats =
  129. get_supported_formats(context, type, flags);
  130. return std::find(formats.begin(), formats.end(), format) != formats.end();
  131. }
  132. };
  133. namespace detail {
  134. // set_kernel_arg() specialization for image_object
  135. template<>
  136. struct set_kernel_arg<image_object> : public set_kernel_arg<memory_object> { };
  137. } // end detail namespace
  138. } // end compute namespace
  139. } // end boost namespace
  140. #endif // BOOST_COMPUTE_IMAGE_IMAGE_OBJECT_HPP