1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_IMAGE_IMAGE2D_HPP
12 #define BOOST_COMPUTE_IMAGE_IMAGE2D_HPP
14 #include <boost/throw_exception.hpp>
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>
28 // forward declarations
32 /// \brief An OpenCL 2D image object
34 /// For example, to create a 640x480 8-bit RGBA image:
36 /// \snippet test/test_image2d.cpp create_image
38 /// \see image_format, image3d
39 class image2d : public image_object
42 /// Creates a null image2d object.
48 /// Creates a new image2d object.
50 /// \see_opencl_ref{clCreateImage}
51 image2d(const context &context,
54 const image_format &format,
55 cl_mem_flags flags = read_write,
57 size_t image_row_pitch = 0)
63 desc.image_type = CL_MEM_OBJECT_IMAGE2D;
64 desc.image_width = image_width;
65 desc.image_height = image_height;
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;
78 m_mem = clCreateImage(context,
80 format.get_format_ptr(),
85 m_mem = clCreateImage2D(context,
87 format.get_format_ptr(),
96 BOOST_THROW_EXCEPTION(opencl_error(error));
100 /// \internal_ (deprecated)
101 image2d(const context &context,
103 const image_format &format,
106 size_t image_row_pitch = 0,
111 #ifdef CL_VERSION_1_2
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 CL_VERSION_2_0
128 m_mem = clCreateImage(context,
130 format.get_format_ptr(),
135 m_mem = clCreateImage2D(context,
137 format.get_format_ptr(),
146 BOOST_THROW_EXCEPTION(opencl_error(error));
150 /// Creates a new image2d as a copy of \p other.
151 image2d(const image2d &other)
152 : image_object(other)
156 /// Copies the image2d from \p other.
157 image2d& operator=(const image2d &other)
159 image_object::operator=(other);
164 #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
165 /// Move-constructs a new image object from \p other.
166 image2d(image2d&& other) BOOST_NOEXCEPT
167 : image_object(std::move(other))
171 /// Move-assigns the image from \p other to \c *this.
172 image2d& operator=(image2d&& other) BOOST_NOEXCEPT
174 image_object::operator=(std::move(other));
178 #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
180 /// Destroys the image2d object.
185 /// Returns the size (width, height) of the image.
186 extents<2> size() const
189 size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
190 size[1] = get_info<size_t>(CL_IMAGE_HEIGHT);
194 /// Returns the origin of the image (\c 0, \c 0).
195 extents<2> origin() const
200 /// Returns information about the image.
202 /// \see_opencl_ref{clGetImageInfo}
204 T get_info(cl_image_info info) const
206 return detail::get_object_info<T>(clGetImageInfo, m_mem, info);
211 typename detail::get_object_info_type<image2d, Enum>::type
214 /// Returns the supported image formats for the context.
216 /// \see_opencl_ref{clGetSupportedImageFormats}
217 static std::vector<image_format>
218 get_supported_formats(const context &context, cl_mem_flags flags = read_write)
220 return image_object::get_supported_formats(context, CL_MEM_OBJECT_IMAGE2D, flags);
223 /// Returns \c true if \p format is a supported 2D image format for
225 static bool is_supported_format(const image_format &format,
226 const context &context,
227 cl_mem_flags flags = read_write)
229 return image_object::is_supported_format(
230 format, context, CL_MEM_OBJECT_IMAGE2D, flags
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;
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))
252 // set_kernel_arg() specialization for image2d
254 struct set_kernel_arg<image2d> : public set_kernel_arg<image_object> { };
256 } // end detail namespace
257 } // end compute namespace
258 } // end boost namespace
260 BOOST_COMPUTE_TYPE_NAME(boost::compute::image2d, image2d_t)
262 #endif // BOOST_COMPUTE_IMAGE_IMAGE2D_HPP