image_sampler.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_SAMPLER_HPP
  11. #define BOOST_COMPUTE_IMAGE_IMAGE_SAMPLER_HPP
  12. #include <boost/throw_exception.hpp>
  13. #include <boost/compute/config.hpp>
  14. #include <boost/compute/context.hpp>
  15. #include <boost/compute/kernel.hpp>
  16. #include <boost/compute/detail/get_object_info.hpp>
  17. #include <boost/compute/detail/assert_cl_success.hpp>
  18. #include <boost/compute/exception/opencl_error.hpp>
  19. #include <boost/compute/type_traits/type_name.hpp>
  20. namespace boost {
  21. namespace compute {
  22. /// \class image_sampler
  23. /// \brief An OpenCL image sampler object
  24. ///
  25. /// \see image2d, image_format
  26. class image_sampler
  27. {
  28. public:
  29. enum addressing_mode {
  30. none = CL_ADDRESS_NONE,
  31. clamp_to_edge = CL_ADDRESS_CLAMP_TO_EDGE,
  32. clamp = CL_ADDRESS_CLAMP,
  33. repeat = CL_ADDRESS_REPEAT
  34. };
  35. enum filter_mode {
  36. nearest = CL_FILTER_NEAREST,
  37. linear = CL_FILTER_LINEAR
  38. };
  39. image_sampler()
  40. : m_sampler(0)
  41. {
  42. }
  43. image_sampler(const context &context,
  44. bool normalized_coords,
  45. cl_addressing_mode addressing_mode,
  46. cl_filter_mode filter_mode)
  47. {
  48. cl_int error = 0;
  49. #ifdef BOOST_COMPUTE_CL_VERSION_2_0
  50. std::vector<cl_sampler_properties> sampler_properties;
  51. sampler_properties.push_back(CL_SAMPLER_NORMALIZED_COORDS);
  52. sampler_properties.push_back(cl_sampler_properties(normalized_coords));
  53. sampler_properties.push_back(CL_SAMPLER_ADDRESSING_MODE);
  54. sampler_properties.push_back(cl_sampler_properties(addressing_mode));
  55. sampler_properties.push_back(CL_SAMPLER_FILTER_MODE);
  56. sampler_properties.push_back(cl_sampler_properties(filter_mode));
  57. sampler_properties.push_back(cl_sampler_properties(0));
  58. m_sampler = clCreateSamplerWithProperties(
  59. context, &sampler_properties[0], &error
  60. );
  61. #else
  62. m_sampler = clCreateSampler(
  63. context, normalized_coords, addressing_mode, filter_mode, &error
  64. );
  65. #endif
  66. if(!m_sampler){
  67. BOOST_THROW_EXCEPTION(opencl_error(error));
  68. }
  69. }
  70. explicit image_sampler(cl_sampler sampler, bool retain = true)
  71. : m_sampler(sampler)
  72. {
  73. if(m_sampler && retain){
  74. clRetainSampler(m_sampler);
  75. }
  76. }
  77. /// Creates a new image sampler object as a copy of \p other.
  78. image_sampler(const image_sampler &other)
  79. : m_sampler(other.m_sampler)
  80. {
  81. if(m_sampler){
  82. clRetainSampler(m_sampler);
  83. }
  84. }
  85. /// Copies the image sampler object from \p other to \c *this.
  86. image_sampler& operator=(const image_sampler &other)
  87. {
  88. if(this != &other){
  89. if(m_sampler){
  90. clReleaseSampler(m_sampler);
  91. }
  92. m_sampler = other.m_sampler;
  93. if(m_sampler){
  94. clRetainSampler(m_sampler);
  95. }
  96. }
  97. return *this;
  98. }
  99. #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
  100. image_sampler(image_sampler&& other) BOOST_NOEXCEPT
  101. : m_sampler(other.m_sampler)
  102. {
  103. other.m_sampler = 0;
  104. }
  105. image_sampler& operator=(image_sampler&& other) BOOST_NOEXCEPT
  106. {
  107. if(m_sampler){
  108. clReleaseSampler(m_sampler);
  109. }
  110. m_sampler = other.m_sampler;
  111. other.m_sampler = 0;
  112. return *this;
  113. }
  114. #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
  115. /// Destroys the image sampler object.
  116. ~image_sampler()
  117. {
  118. if(m_sampler){
  119. BOOST_COMPUTE_ASSERT_CL_SUCCESS(
  120. clReleaseSampler(m_sampler)
  121. );
  122. }
  123. }
  124. /// Returns the underlying \c cl_sampler object.
  125. cl_sampler& get() const
  126. {
  127. return const_cast<cl_sampler &>(m_sampler);
  128. }
  129. /// Returns the context for the image sampler object.
  130. context get_context() const
  131. {
  132. return context(get_info<cl_context>(CL_SAMPLER_CONTEXT));
  133. }
  134. /// Returns information about the sampler.
  135. ///
  136. /// \see_opencl_ref{clGetSamplerInfo}
  137. template<class T>
  138. T get_info(cl_sampler_info info) const
  139. {
  140. return detail::get_object_info<T>(clGetSamplerInfo, m_sampler, info);
  141. }
  142. /// \overload
  143. template<int Enum>
  144. typename detail::get_object_info_type<image_sampler, Enum>::type
  145. get_info() const;
  146. /// Returns \c true if the sampler is the same at \p other.
  147. bool operator==(const image_sampler &other) const
  148. {
  149. return m_sampler == other.m_sampler;
  150. }
  151. /// Returns \c true if the sampler is different from \p other.
  152. bool operator!=(const image_sampler &other) const
  153. {
  154. return m_sampler != other.m_sampler;
  155. }
  156. operator cl_sampler() const
  157. {
  158. return m_sampler;
  159. }
  160. private:
  161. cl_sampler m_sampler;
  162. };
  163. /// \internal_ define get_info() specializations for image_sampler
  164. BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image_sampler,
  165. ((cl_uint, CL_SAMPLER_REFERENCE_COUNT))
  166. ((cl_context, CL_SAMPLER_CONTEXT))
  167. ((cl_addressing_mode, CL_SAMPLER_ADDRESSING_MODE))
  168. ((cl_filter_mode, CL_SAMPLER_FILTER_MODE))
  169. ((bool, CL_SAMPLER_NORMALIZED_COORDS))
  170. )
  171. namespace detail {
  172. // set_kernel_arg specialization for image samplers
  173. template<>
  174. struct set_kernel_arg<image_sampler>
  175. {
  176. void operator()(kernel &kernel_, size_t index, const image_sampler &sampler)
  177. {
  178. kernel_.set_arg(index, sampler.get());
  179. }
  180. };
  181. } // end detail namespace
  182. } // end compute namespace
  183. } // end boost namespace
  184. BOOST_COMPUTE_TYPE_NAME(boost::compute::image_sampler, sampler_t)
  185. #endif // BOOST_COMPUTE_IMAGE_IMAGE_SAMPLER_HPP