• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #ifndef BOOST_COMPUTE_IMAGE_IMAGE2D_HPP
12 #define BOOST_COMPUTE_IMAGE_IMAGE2D_HPP
13 
14 #include <boost/throw_exception.hpp>
15 
16 #include <boost/compute/config.hpp>
17 #include <boost/compute/context.hpp>
18 #include <boost/compute/exception/opencl_error.hpp>
19 #include <boost/compute/image/image_format.hpp>
20 #include <boost/compute/image/image_object.hpp>
21 #include <boost/compute/detail/get_object_info.hpp>
22 #include <boost/compute/type_traits/type_name.hpp>
23 #include <boost/compute/utility/extents.hpp>
24 
25 namespace boost {
26 namespace compute {
27 
28 // forward declarations
29 class command_queue;
30 
31 /// \class image2d
32 /// \brief An OpenCL 2D image object
33 ///
34 /// For example, to create a 640x480 8-bit RGBA image:
35 ///
36 /// \snippet test/test_image2d.cpp create_image
37 ///
38 /// \see image_format, image3d
39 class image2d : public image_object
40 {
41 public:
42     /// Creates a null image2d object.
image2d()43     image2d()
44         : image_object()
45     {
46     }
47 
48     /// Creates a new image2d object.
49     ///
50     /// \see_opencl_ref{clCreateImage}
image2d(const context & context,size_t image_width,size_t image_height,const image_format & format,cl_mem_flags flags=read_write,void * host_ptr=0,size_t image_row_pitch=0)51     image2d(const context &context,
52             size_t image_width,
53             size_t image_height,
54             const image_format &format,
55             cl_mem_flags flags = read_write,
56             void *host_ptr = 0,
57             size_t image_row_pitch = 0)
58     {
59         cl_int error = 0;
60 
61         #ifdef BOOST_COMPUTE_CL_VERSION_1_2
62         cl_image_desc desc;
63         desc.image_type = CL_MEM_OBJECT_IMAGE2D;
64         desc.image_width = image_width;
65         desc.image_height = image_height;
66         desc.image_depth = 1;
67         desc.image_array_size = 0;
68         desc.image_row_pitch = image_row_pitch;
69         desc.image_slice_pitch = 0;
70         desc.num_mip_levels = 0;
71         desc.num_samples = 0;
72         #ifdef BOOST_COMPUTE_CL_VERSION_2_0
73         desc.mem_object = 0;
74         #else
75         desc.buffer = 0;
76         #endif
77 
78         m_mem = clCreateImage(context,
79                               flags,
80                               format.get_format_ptr(),
81                               &desc,
82                               host_ptr,
83                               &error);
84         #else
85         m_mem = clCreateImage2D(context,
86                                 flags,
87                                 format.get_format_ptr(),
88                                 image_width,
89                                 image_height,
90                                 image_row_pitch,
91                                 host_ptr,
92                                 &error);
93         #endif
94 
95         if(!m_mem){
96             BOOST_THROW_EXCEPTION(opencl_error(error));
97         }
98     }
99 
100     /// \internal_ (deprecated)
image2d(const context & context,cl_mem_flags flags,const image_format & format,size_t image_width,size_t image_height,size_t image_row_pitch=0,void * host_ptr=0)101     image2d(const context &context,
102             cl_mem_flags flags,
103             const image_format &format,
104             size_t image_width,
105             size_t image_height,
106             size_t image_row_pitch = 0,
107             void *host_ptr = 0)
108     {
109         cl_int error = 0;
110 
111         #ifdef BOOST_COMPUTE_CL_VERSION_1_2
112         cl_image_desc desc;
113         desc.image_type = CL_MEM_OBJECT_IMAGE2D;
114         desc.image_width = image_width;
115         desc.image_height = image_height;
116         desc.image_depth = 1;
117         desc.image_array_size = 0;
118         desc.image_row_pitch = image_row_pitch;
119         desc.image_slice_pitch = 0;
120         desc.num_mip_levels = 0;
121         desc.num_samples = 0;
122         #ifdef BOOST_COMPUTE_CL_VERSION_2_0
123         desc.mem_object = 0;
124         #else
125         desc.buffer = 0;
126         #endif
127 
128         m_mem = clCreateImage(context,
129                               flags,
130                               format.get_format_ptr(),
131                               &desc,
132                               host_ptr,
133                               &error);
134         #else
135         m_mem = clCreateImage2D(context,
136                                 flags,
137                                 format.get_format_ptr(),
138                                 image_width,
139                                 image_height,
140                                 image_row_pitch,
141                                 host_ptr,
142                                 &error);
143         #endif
144 
145         if(!m_mem){
146             BOOST_THROW_EXCEPTION(opencl_error(error));
147         }
148     }
149 
150     /// Creates a new image2d as a copy of \p other.
image2d(const image2d & other)151     image2d(const image2d &other)
152       : image_object(other)
153     {
154     }
155 
156     /// Copies the image2d from \p other.
operator =(const image2d & other)157     image2d& operator=(const image2d &other)
158     {
159         image_object::operator=(other);
160 
161         return *this;
162     }
163 
164     #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
165     /// Move-constructs a new image object from \p other.
image2d(image2d && other)166     image2d(image2d&& other) BOOST_NOEXCEPT
167         : image_object(std::move(other))
168     {
169     }
170 
171     /// Move-assigns the image from \p other to \c *this.
operator =(image2d && other)172     image2d& operator=(image2d&& other) BOOST_NOEXCEPT
173     {
174         image_object::operator=(std::move(other));
175 
176         return *this;
177     }
178     #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
179 
180     /// Destroys the image2d object.
~image2d()181     ~image2d()
182     {
183     }
184 
185     /// Returns the size (width, height) of the image.
size() const186     extents<2> size() const
187     {
188         extents<2> size;
189         size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
190         size[1] = get_info<size_t>(CL_IMAGE_HEIGHT);
191         return size;
192     }
193 
194     /// Returns the origin of the image (\c 0, \c 0).
origin() const195     extents<2> origin() const
196     {
197         return extents<2>();
198     }
199 
200     /// Returns information about the image.
201     ///
202     /// \see_opencl_ref{clGetImageInfo}
203     template<class T>
get_info(cl_image_info info) const204     T get_info(cl_image_info info) const
205     {
206         return detail::get_object_info<T>(clGetImageInfo, m_mem, info);
207     }
208 
209     /// \overload
210     template<int Enum>
211     typename detail::get_object_info_type<image2d, Enum>::type
212     get_info() const;
213 
214     /// Returns the supported image formats for the context.
215     ///
216     /// \see_opencl_ref{clGetSupportedImageFormats}
217     static std::vector<image_format>
get_supported_formats(const context & context,cl_mem_flags flags=read_write)218     get_supported_formats(const context &context, cl_mem_flags flags = read_write)
219     {
220         return image_object::get_supported_formats(context, CL_MEM_OBJECT_IMAGE2D, flags);
221     }
222 
223     /// Returns \c true if \p format is a supported 2D image format for
224     /// \p context.
is_supported_format(const image_format & format,const context & context,cl_mem_flags flags=read_write)225     static bool is_supported_format(const image_format &format,
226                                     const context &context,
227                                     cl_mem_flags flags = read_write)
228     {
229         return image_object::is_supported_format(
230             format, context, CL_MEM_OBJECT_IMAGE2D, flags
231         );
232     }
233 
234     /// Creates a new image with a copy of the data in \c *this. Uses \p queue
235     /// to perform the copy operation.
236     image2d clone(command_queue &queue) const;
237 };
238 
239 /// \internal_ define get_info() specializations for image2d
240 BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image2d,
241     ((cl_image_format, CL_IMAGE_FORMAT))
242     ((size_t, CL_IMAGE_ELEMENT_SIZE))
243     ((size_t, CL_IMAGE_ROW_PITCH))
244     ((size_t, CL_IMAGE_SLICE_PITCH))
245     ((size_t, CL_IMAGE_WIDTH))
246     ((size_t, CL_IMAGE_HEIGHT))
247     ((size_t, CL_IMAGE_DEPTH))
248 )
249 
250 namespace detail {
251 
252 // set_kernel_arg() specialization for image2d
253 template<>
254 struct set_kernel_arg<image2d> : public set_kernel_arg<image_object> { };
255 
256 } // end detail namespace
257 } // end compute namespace
258 } // end boost namespace
259 
260 BOOST_COMPUTE_TYPE_NAME(boost::compute::image2d, image2d_t)
261 
262 #endif // BOOST_COMPUTE_IMAGE_IMAGE2D_HPP
263