Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:01

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_IMAGE_IMAGE_SAMPLER_HPP
0012 #define BOOST_COMPUTE_IMAGE_IMAGE_SAMPLER_HPP
0013 
0014 #include <boost/throw_exception.hpp>
0015 
0016 #include <boost/compute/config.hpp>
0017 #include <boost/compute/context.hpp>
0018 #include <boost/compute/kernel.hpp>
0019 #include <boost/compute/detail/get_object_info.hpp>
0020 #include <boost/compute/detail/assert_cl_success.hpp>
0021 #include <boost/compute/exception/opencl_error.hpp>
0022 #include <boost/compute/type_traits/type_name.hpp>
0023 
0024 namespace boost {
0025 namespace compute {
0026 
0027 /// \class image_sampler
0028 /// \brief An OpenCL image sampler object
0029 ///
0030 /// \see image2d, image_format
0031 class image_sampler
0032 {
0033 public:
0034     enum addressing_mode {
0035         none = CL_ADDRESS_NONE,
0036         clamp_to_edge = CL_ADDRESS_CLAMP_TO_EDGE,
0037         clamp = CL_ADDRESS_CLAMP,
0038         repeat = CL_ADDRESS_REPEAT
0039     };
0040 
0041     enum filter_mode {
0042         nearest = CL_FILTER_NEAREST,
0043         linear = CL_FILTER_LINEAR
0044     };
0045 
0046     image_sampler()
0047         : m_sampler(0)
0048     {
0049     }
0050 
0051     image_sampler(const context &context,
0052                   bool normalized_coords,
0053                   cl_addressing_mode addressing_mode,
0054                   cl_filter_mode filter_mode)
0055     {
0056         cl_int error = 0;
0057 
0058         #ifdef BOOST_COMPUTE_CL_VERSION_2_0
0059         std::vector<cl_sampler_properties> sampler_properties;
0060         sampler_properties.push_back(CL_SAMPLER_NORMALIZED_COORDS);
0061         sampler_properties.push_back(cl_sampler_properties(normalized_coords));
0062         sampler_properties.push_back(CL_SAMPLER_ADDRESSING_MODE);
0063         sampler_properties.push_back(cl_sampler_properties(addressing_mode));
0064         sampler_properties.push_back(CL_SAMPLER_FILTER_MODE);
0065         sampler_properties.push_back(cl_sampler_properties(filter_mode));
0066         sampler_properties.push_back(cl_sampler_properties(0));
0067 
0068         m_sampler = clCreateSamplerWithProperties(
0069             context, &sampler_properties[0], &error
0070         );
0071         #else
0072         m_sampler = clCreateSampler(
0073             context, normalized_coords, addressing_mode, filter_mode, &error
0074         );
0075         #endif
0076 
0077         if(!m_sampler){
0078             BOOST_THROW_EXCEPTION(opencl_error(error));
0079         }
0080     }
0081 
0082     explicit image_sampler(cl_sampler sampler, bool retain = true)
0083         : m_sampler(sampler)
0084     {
0085         if(m_sampler && retain){
0086             clRetainSampler(m_sampler);
0087         }
0088     }
0089 
0090     /// Creates a new image sampler object as a copy of \p other.
0091     image_sampler(const image_sampler &other)
0092         : m_sampler(other.m_sampler)
0093     {
0094         if(m_sampler){
0095             clRetainSampler(m_sampler);
0096         }
0097     }
0098 
0099     /// Copies the image sampler object from \p other to \c *this.
0100     image_sampler& operator=(const image_sampler &other)
0101     {
0102         if(this != &other){
0103             if(m_sampler){
0104                 clReleaseSampler(m_sampler);
0105             }
0106 
0107             m_sampler = other.m_sampler;
0108 
0109             if(m_sampler){
0110                 clRetainSampler(m_sampler);
0111             }
0112         }
0113 
0114         return *this;
0115     }
0116 
0117     #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
0118     image_sampler(image_sampler&& other) BOOST_NOEXCEPT
0119         : m_sampler(other.m_sampler)
0120     {
0121         other.m_sampler = 0;
0122     }
0123 
0124     image_sampler& operator=(image_sampler&& other) BOOST_NOEXCEPT
0125     {
0126         if(m_sampler){
0127             clReleaseSampler(m_sampler);
0128         }
0129 
0130         m_sampler = other.m_sampler;
0131         other.m_sampler = 0;
0132 
0133         return *this;
0134     }
0135     #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
0136 
0137     /// Destroys the image sampler object.
0138     ~image_sampler()
0139     {
0140         if(m_sampler){
0141             BOOST_COMPUTE_ASSERT_CL_SUCCESS(
0142                 clReleaseSampler(m_sampler)
0143             );
0144         }
0145     }
0146 
0147     /// Returns the underlying \c cl_sampler object.
0148     cl_sampler& get() const
0149     {
0150         return const_cast<cl_sampler &>(m_sampler);
0151     }
0152 
0153     /// Returns the context for the image sampler object.
0154     context get_context() const
0155     {
0156         return context(get_info<cl_context>(CL_SAMPLER_CONTEXT));
0157     }
0158 
0159     /// Returns information about the sampler.
0160     ///
0161     /// \see_opencl_ref{clGetSamplerInfo}
0162     template<class T>
0163     T get_info(cl_sampler_info info) const
0164     {
0165         return detail::get_object_info<T>(clGetSamplerInfo, m_sampler, info);
0166     }
0167 
0168     /// \overload
0169     template<int Enum>
0170     typename detail::get_object_info_type<image_sampler, Enum>::type
0171     get_info() const;
0172 
0173     /// Returns \c true if the sampler is the same at \p other.
0174     bool operator==(const image_sampler &other) const
0175     {
0176         return m_sampler == other.m_sampler;
0177     }
0178 
0179     /// Returns \c true if the sampler is different from \p other.
0180     bool operator!=(const image_sampler &other) const
0181     {
0182         return m_sampler != other.m_sampler;
0183     }
0184 
0185     operator cl_sampler() const
0186     {
0187         return m_sampler;
0188     }
0189 
0190 private:
0191     cl_sampler m_sampler;
0192 };
0193 
0194 /// \internal_ define get_info() specializations for image_sampler
0195 BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image_sampler,
0196     ((cl_uint, CL_SAMPLER_REFERENCE_COUNT))
0197     ((cl_context, CL_SAMPLER_CONTEXT))
0198     ((cl_addressing_mode, CL_SAMPLER_ADDRESSING_MODE))
0199     ((cl_filter_mode, CL_SAMPLER_FILTER_MODE))
0200     ((bool, CL_SAMPLER_NORMALIZED_COORDS))
0201 )
0202 
0203 namespace detail {
0204 
0205 // set_kernel_arg specialization for image samplers
0206 template<>
0207 struct set_kernel_arg<image_sampler>
0208 {
0209     void operator()(kernel &kernel_, size_t index, const image_sampler &sampler)
0210     {
0211         kernel_.set_arg(index, sampler.get());
0212     }
0213 };
0214 
0215 } // end detail namespace
0216 } // end compute namespace
0217 } // end boost namespace
0218 
0219 BOOST_COMPUTE_TYPE_NAME(boost::compute::image_sampler, sampler_t)
0220 
0221 #endif // BOOST_COMPUTE_IMAGE_IMAGE_SAMPLER_HPP