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_IMAGE1D_HPP
12 #define BOOST_COMPUTE_IMAGE_IMAGE1D_HPP
13 
14 #include <boost/throw_exception.hpp>
15 
16 #include <boost/compute/config.hpp>
17 #include <boost/compute/exception/opencl_error.hpp>
18 #include <boost/compute/image/image_format.hpp>
19 #include <boost/compute/image/image_object.hpp>
20 #include <boost/compute/type_traits/type_name.hpp>
21 #include <boost/compute/utility/extents.hpp>
22 
23 namespace boost {
24 namespace compute {
25 
26 // forward declarations
27 class command_queue;
28 
29 /// \class image1d
30 /// \brief An OpenCL 1D image object
31 ///
32 /// \opencl_version_warning{1,2}
33 ///
34 /// \see image_format, image2d
35 class image1d : public image_object
36 {
37 public:
38     /// Creates a null image1d object.
image1d()39     image1d()
40         : image_object()
41     {
42     }
43 
44     /// Creates a new image1d object.
45     ///
46     /// \see_opencl_ref{clCreateImage}
image1d(const context & context,size_t image_width,const image_format & format,cl_mem_flags flags=read_write,void * host_ptr=0)47     image1d(const context &context,
48             size_t image_width,
49             const image_format &format,
50             cl_mem_flags flags = read_write,
51             void *host_ptr = 0)
52     {
53     #ifdef BOOST_COMPUTE_CL_VERSION_1_2
54         cl_image_desc desc;
55         desc.image_type = CL_MEM_OBJECT_IMAGE1D;
56         desc.image_width = image_width;
57         desc.image_height = 1;
58         desc.image_depth = 1;
59         desc.image_array_size = 0;
60         desc.image_row_pitch = 0;
61         desc.image_slice_pitch = 0;
62         desc.num_mip_levels = 0;
63         desc.num_samples = 0;
64     #ifdef BOOST_COMPUTE_CL_VERSION_2_0
65         desc.mem_object = 0;
66     #else
67         desc.buffer = 0;
68     #endif
69 
70         cl_int error = 0;
71 
72         m_mem = clCreateImage(
73             context, flags, format.get_format_ptr(), &desc, host_ptr, &error
74         );
75 
76         if(!m_mem){
77             BOOST_THROW_EXCEPTION(opencl_error(error));
78         }
79     #else
80         // image1d objects are only supported in OpenCL 1.2 and later
81         BOOST_THROW_EXCEPTION(opencl_error(CL_IMAGE_FORMAT_NOT_SUPPORTED));
82     #endif
83     }
84 
85     /// Creates a new image1d as a copy of \p other.
image1d(const image1d & other)86     image1d(const image1d &other)
87       : image_object(other)
88     {
89     }
90 
91     /// Copies the image1d from \p other.
operator =(const image1d & other)92     image1d& operator=(const image1d &other)
93     {
94         image_object::operator=(other);
95 
96         return *this;
97     }
98 
99     #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
100     /// Move-constructs a new image object from \p other.
image1d(image1d && other)101     image1d(image1d&& other) BOOST_NOEXCEPT
102         : image_object(std::move(other))
103     {
104     }
105 
106     /// Move-assigns the image from \p other to \c *this.
operator =(image1d && other)107     image1d& operator=(image1d&& other) BOOST_NOEXCEPT
108     {
109         image_object::operator=(std::move(other));
110 
111         return *this;
112     }
113     #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
114 
115     /// Destroys the image1d object.
~image1d()116     ~image1d()
117     {
118     }
119 
120     /// Returns the size (width) of the image.
size() const121     extents<1> size() const
122     {
123         extents<1> size;
124         size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
125         return size;
126     }
127 
128     /// Returns the origin of the image (\c 0).
origin() const129     extents<1> origin() const
130     {
131         return extents<1>();
132     }
133 
134     /// Returns information about the image.
135     ///
136     /// \see_opencl_ref{clGetImageInfo}
137     template<class T>
get_info(cl_image_info info) const138     T get_info(cl_image_info info) const
139     {
140         return get_image_info<T>(info);
141     }
142 
143     /// \overload
144     template<int Enum>
145     typename detail::get_object_info_type<image1d, Enum>::type
146     get_info() const;
147 
148     /// Returns the supported image formats for the context.
149     ///
150     /// \see_opencl_ref{clGetSupportedImageFormats}
151     static std::vector<image_format>
get_supported_formats(const context & context,cl_mem_flags flags=read_write)152     get_supported_formats(const context &context, cl_mem_flags flags = read_write)
153     {
154     #ifdef BOOST_COMPUTE_CL_VERSION_1_2
155         return image_object::get_supported_formats(context, CL_MEM_OBJECT_IMAGE1D, flags);
156     #else
157         return std::vector<image_format>();
158     #endif
159     }
160 
161     /// Returns \c true if \p format is a supported 1D image format for
162     /// \p context.
is_supported_format(const image_format & format,const context & context,cl_mem_flags flags=read_write)163     static bool is_supported_format(const image_format &format,
164                                     const context &context,
165                                     cl_mem_flags flags = read_write)
166     {
167     #ifdef BOOST_COMPUTE_CL_VERSION_1_2
168         return image_object::is_supported_format(
169             format, context, CL_MEM_OBJECT_IMAGE1D, flags
170         );
171     #else
172         return false;
173     #endif
174     }
175 
176     /// Creates a new image with a copy of the data in \c *this. Uses \p queue
177     /// to perform the copy operation.
178     image1d clone(command_queue &queue) const;
179 };
180 
181 /// \internal_ define get_info() specializations for image1d
182 BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image1d,
183     ((cl_image_format, CL_IMAGE_FORMAT))
184     ((size_t, CL_IMAGE_ELEMENT_SIZE))
185     ((size_t, CL_IMAGE_ROW_PITCH))
186     ((size_t, CL_IMAGE_SLICE_PITCH))
187     ((size_t, CL_IMAGE_WIDTH))
188     ((size_t, CL_IMAGE_HEIGHT))
189     ((size_t, CL_IMAGE_DEPTH))
190 )
191 
192 namespace detail {
193 
194 // set_kernel_arg() specialization for image1d
195 template<>
196 struct set_kernel_arg<image1d> : public set_kernel_arg<image_object> { };
197 
198 } // end detail namespace
199 } // end compute namespace
200 } // end boost namespace
201 
202 BOOST_COMPUTE_TYPE_NAME(boost::compute::image1d, image1d_t)
203 
204 #endif // BOOST_COMPUTE_IMAGE_IMAGE1D_HPP
205