DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
image.hpp
Go to the documentation of this file.
1 //==------------ image.hpp -------------------------------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
14 #include <CL/sycl/event.hpp>
15 #include <CL/sycl/stl.hpp>
16 #include <CL/sycl/types.hpp>
17 #include <cstddef>
19 
21 namespace sycl {
22 
23 enum class image_channel_order : unsigned int {
24  a = 0,
25  r = 1,
26  rx = 2,
27  rg = 3,
28  rgx = 4,
29  ra = 5,
30  rgb = 6,
31  rgbx = 7,
32  rgba = 8,
33  argb = 9,
34  bgra = 10,
35  intensity = 11,
36  luminance = 12,
37  abgr = 13,
38  ext_oneapi_srgba = 14 // OpenCL 2.0
39 };
40 
41 enum class image_channel_type : unsigned int {
42  snorm_int8 = 0,
43  snorm_int16 = 1,
44  unorm_int8 = 2,
45  unorm_int16 = 3,
46  unorm_short_565 = 4,
47  unorm_short_555 = 5,
48  unorm_int_101010 = 6,
49  signed_int8 = 7,
50  signed_int16 = 8,
51  signed_int32 = 9,
52  unsigned_int8 = 10,
53  unsigned_int16 = 11,
54  unsigned_int32 = 12,
55  fp16 = 13,
56  fp32 = 14
57 };
58 
59 using byte = unsigned char;
60 
62 
72 template <int Dimensions = 1, typename AllocatorT = cl::sycl::image_allocator>
73 class image {
74 public:
76  const range<Dimensions> &Range, const property_list &PropList = {}) {
77  impl = std::make_shared<detail::image_impl<Dimensions>>(
78  Order, Type, Range,
79  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(),
80  PropList);
81  }
82 
84  const range<Dimensions> &Range, AllocatorT Allocator,
85  const property_list &PropList = {}) {
86  impl = std::make_shared<detail::image_impl<Dimensions>>(
87  Order, Type, Range,
88  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
89  Allocator),
90  PropList);
91  }
92 
93  /* Available only when: dimensions >1 */
94  template <bool B = (Dimensions > 1)>
96  const range<Dimensions> &Range,
97  const typename detail::enable_if_t<B, range<Dimensions - 1>> &Pitch,
98  const property_list &PropList = {}) {
99  impl = std::make_shared<detail::image_impl<Dimensions>>(
100  Order, Type, Range, Pitch,
101  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(),
102  PropList);
103  }
104 
105  /* Available only when: dimensions >1 */
106  template <bool B = (Dimensions > 1)>
108  const range<Dimensions> &Range,
109  const typename detail::enable_if_t<B, range<Dimensions - 1>> &Pitch,
110  AllocatorT Allocator, const property_list &PropList = {}) {
111  impl = std::make_shared<detail::image_impl<Dimensions>>(
112  Order, Type, Range, Pitch,
113  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
114  Allocator),
115  PropList);
116  }
117 
118  image(void *HostPointer, image_channel_order Order, image_channel_type Type,
119  const range<Dimensions> &Range, const property_list &PropList = {}) {
120  impl = std::make_shared<detail::image_impl<Dimensions>>(
121  HostPointer, Order, Type, Range,
122  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(),
123  PropList);
124  }
125 
126  image(void *HostPointer, image_channel_order Order, image_channel_type Type,
127  const range<Dimensions> &Range, AllocatorT Allocator,
128  const property_list &PropList = {}) {
129  impl = std::make_shared<detail::image_impl<Dimensions>>(
130  HostPointer, Order, Type, Range,
131  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
132  Allocator),
133  PropList);
134  }
135 
136  image(const void *HostPointer, image_channel_order Order,
137  image_channel_type Type, const range<Dimensions> &Range,
138  const property_list &PropList = {}) {
139  impl = std::make_shared<detail::image_impl<Dimensions>>(
140  HostPointer, Order, Type, Range,
141  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(),
142  PropList);
143  }
144 
145  image(const void *HostPointer, image_channel_order Order,
146  image_channel_type Type, const range<Dimensions> &Range,
147  AllocatorT Allocator, const property_list &PropList = {}) {
148  impl = std::make_shared<detail::image_impl<Dimensions>>(
149  HostPointer, Order, Type, Range,
150  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
151  Allocator),
152  PropList);
153  }
154 
155  /* Available only when: dimensions >1 */
156  template <bool B = (Dimensions > 1)>
157  image(void *HostPointer, image_channel_order Order, image_channel_type Type,
158  const range<Dimensions> &Range,
159  const typename detail::enable_if_t<B, range<Dimensions - 1>> &Pitch,
160  const property_list &PropList = {}) {
161  impl = std::make_shared<detail::image_impl<Dimensions>>(
162  HostPointer, Order, Type, Range, Pitch,
163  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(),
164  PropList);
165  }
166 
167  /* Available only when: dimensions >1 */
168  template <bool B = (Dimensions > 1)>
169  image(void *HostPointer, image_channel_order Order, image_channel_type Type,
170  const range<Dimensions> &Range,
171  const typename detail::enable_if_t<B, range<Dimensions - 1>> &Pitch,
172  AllocatorT Allocator, const property_list &PropList = {}) {
173  impl = std::make_shared<detail::image_impl<Dimensions>>(
174  HostPointer, Order, Type, Range, Pitch,
175  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
176  Allocator),
177  PropList);
178  }
179 
180  image(std::shared_ptr<void> &HostPointer, image_channel_order Order,
181  image_channel_type Type, const range<Dimensions> &Range,
182  const property_list &PropList = {}) {
183  impl = std::make_shared<detail::image_impl<Dimensions>>(
184  HostPointer, Order, Type, Range,
185  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(),
186  PropList);
187  }
188 
189  image(std::shared_ptr<void> &HostPointer, image_channel_order Order,
190  image_channel_type Type, const range<Dimensions> &Range,
191  AllocatorT Allocator, const property_list &PropList = {}) {
192  impl = std::make_shared<detail::image_impl<Dimensions>>(
193  HostPointer, Order, Type, Range,
194  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
195  Allocator),
196  PropList);
197  }
198 
199  /* Available only when: dimensions >1 */
200  template <bool B = (Dimensions > 1)>
201  image(std::shared_ptr<void> &HostPointer, image_channel_order Order,
202  image_channel_type Type, const range<Dimensions> &Range,
203  const typename detail::enable_if_t<B, range<Dimensions - 1>> &Pitch,
204  const property_list &PropList = {}) {
205  impl = std::make_shared<detail::image_impl<Dimensions>>(
206  HostPointer, Order, Type, Range, Pitch,
207  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(),
208  PropList);
209  }
210 
211  /* Available only when: dimensions >1 */
212  template <bool B = (Dimensions > 1)>
213  image(std::shared_ptr<void> &HostPointer, image_channel_order Order,
214  image_channel_type Type, const range<Dimensions> &Range,
215  const typename detail::enable_if_t<B, range<Dimensions - 1>> &Pitch,
216  AllocatorT Allocator, const property_list &PropList = {}) {
217  impl = std::make_shared<detail::image_impl<Dimensions>>(
218  HostPointer, Order, Type, Range, Pitch,
219  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>(
220  Allocator),
221  PropList);
222  }
223 
224 #ifdef __SYCL_INTERNAL_API
225  image(cl_mem ClMemObject, const context &SyclContext,
226  event AvailableEvent = {}) {
227  impl = std::make_shared<detail::image_impl<Dimensions>>(
228  ClMemObject, SyclContext, AvailableEvent,
229  make_unique_ptr<detail::SYCLMemObjAllocatorHolder<AllocatorT>>());
230  }
231 #endif
232 
233  /* -- common interface members -- */
234 
235  image(const image &rhs) = default;
236 
237  image(image &&rhs) = default;
238 
239  image &operator=(const image &rhs) = default;
240 
241  image &operator=(image &&rhs) = default;
242 
243  ~image() = default;
244 
245  bool operator==(const image &rhs) const { return impl == rhs.impl; }
246 
247  bool operator!=(const image &rhs) const { return !(*this == rhs); }
248 
249  /* -- property interface members -- */
250  template <typename propertyT> bool has_property() const {
251  return impl->template has_property<propertyT>();
252  }
253 
254  template <typename propertyT> propertyT get_property() const {
255  return impl->template get_property<propertyT>();
256  }
257 
258  range<Dimensions> get_range() const { return impl->get_range(); }
259 
260  /* Available only when: dimensions >1 */
261  template <bool B = (Dimensions > 1)>
263  return impl->get_pitch();
264  }
265 
266  // Returns the size of the image storage in bytes
267  size_t get_size() const { return impl->getSize(); }
268 
269  // Returns the total number of elements in the image
270  __SYCL2020_DEPRECATED("get_count() is deprecated, please use size() instead")
271  size_t get_count() const { return size(); }
272  size_t size() const noexcept { return impl->get_count(); }
273 
274  // Returns the allocator provided to the image
275  AllocatorT get_allocator() const {
276  return impl->template get_allocator<AllocatorT>();
277  }
278 
279  template <typename DataT, access::mode AccessMode>
280  accessor<detail::EnableIfImgAccDataT<DataT>, Dimensions, AccessMode,
281  access::target::image, access::placeholder::false_t,
283  get_access(handler &commandGroupHandler) {
284  return accessor<DataT, Dimensions, AccessMode, access::target::image,
285  access::placeholder::false_t,
287  commandGroupHandler);
288  }
289 
290  template <typename DataT, access::mode AccessMode>
291  accessor<detail::EnableIfImgAccDataT<DataT>, Dimensions, AccessMode,
292  access::target::host_image, access::placeholder::false_t,
295  return accessor<DataT, Dimensions, AccessMode, access::target::host_image,
296  access::placeholder::false_t,
298  }
299 
300  template <typename Destination = std::nullptr_t>
301  void set_final_data(Destination finalData = nullptr) {
302  impl->set_final_data(finalData);
303  }
304 
305  void set_write_back(bool flag = true) { impl->set_write_back(flag); }
306 
307 private:
308  std::shared_ptr<detail::image_impl<Dimensions>> impl;
309 
310  template <class Obj>
311  friend decltype(Obj::impl) detail::getSyclObjImpl(const Obj &SyclObject);
312 };
313 
314 } // namespace sycl
315 } // __SYCL_INLINE_NAMESPACE(cl)
316 
317 namespace std {
318 template <int Dimensions, typename AllocatorT>
319 struct hash<cl::sycl::image<Dimensions, AllocatorT>> {
321  return hash<std::shared_ptr<cl::sycl::detail::image_impl<Dimensions>>>()(
323  }
324 };
325 } // namespace std
cl::sycl::image::get_access
accessor< detail::EnableIfImgAccDataT< DataT >, Dimensions, AccessMode, access::target::image, access::placeholder::false_t, ext::oneapi::accessor_property_list<> > get_access(handler &commandGroupHandler)
Definition: image.hpp:283
cl::sycl::image::image
image(std::shared_ptr< void > &HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const property_list &PropList={})
Definition: image.hpp:180
cl::sycl::image_channel_order::ra
@ ra
cl::sycl::detail::aligned_allocator
Definition: aligned_allocator.hpp:23
cl::sycl::image::has_property
bool has_property() const
Definition: image.hpp:250
cl::sycl::image_channel_type::unsigned_int8
@ unsigned_int8
cl::sycl::image_channel_order::rx
@ rx
cl::sycl::image::image
image(void *HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, AllocatorT Allocator, const property_list &PropList={})
Definition: image.hpp:126
stl.hpp
cl::sycl::image_channel_type::unorm_int16
@ unorm_int16
cl::sycl::image::get_range
range< Dimensions > get_range() const
Definition: image.hpp:258
cl::sycl::image::size
size_t size() const noexcept
Definition: image.hpp:272
cl::sycl::image::image
image(std::shared_ptr< void > &HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const typename detail::enable_if_t< B, range< Dimensions - 1 >> &Pitch, const property_list &PropList={})
Definition: image.hpp:201
cl::sycl::image_channel_type::snorm_int8
@ snorm_int8
accessor_property_list.hpp
cl::sycl::image_channel_type::unsigned_int32
@ unsigned_int32
cl::sycl::image::image
image(std::shared_ptr< void > &HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, AllocatorT Allocator, const property_list &PropList={})
Definition: image.hpp:189
event.hpp
cl::sycl::property_list
Objects of the property_list class are containers for the SYCL properties.
Definition: property_list.hpp:26
cl::sycl::image_channel_order::rgb
@ rgb
cl::sycl::image::operator==
bool operator==(const image &rhs) const
Definition: image.hpp:245
std::hash< cl::sycl::image< Dimensions, AllocatorT > >::operator()
size_t operator()(const cl::sycl::image< Dimensions, AllocatorT > &I) const
Definition: image.hpp:320
cl::sycl::image::image
image(void *HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const typename detail::enable_if_t< B, range< Dimensions - 1 >> &Pitch, const property_list &PropList={})
Definition: image.hpp:157
cl::sycl::range< Dimensions >
cl::sycl::image_channel_type::unorm_int_101010
@ unorm_int_101010
cl::sycl::image_channel_type::snorm_int16
@ snorm_int16
cl::sycl::image::set_write_back
void set_write_back(bool flag=true)
Definition: image.hpp:305
cl::sycl::image::get_access
accessor< detail::EnableIfImgAccDataT< DataT >, Dimensions, AccessMode, access::target::host_image, access::placeholder::false_t, ext::oneapi::accessor_property_list<> > get_access()
Definition: image.hpp:294
cl::sycl::image_channel_type::fp32
@ fp32
char
cl::sycl::image::image
image(image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const property_list &PropList={})
Definition: image.hpp:75
cl::sycl::image_channel_type::signed_int32
@ signed_int32
cl::sycl::image::get_size
size_t get_size() const
Definition: image.hpp:267
cl::sycl::image_channel_type::unorm_short_565
@ unorm_short_565
cl::sycl::image_channel_type::unsigned_int16
@ unsigned_int16
generic_type_traits.hpp
cl::sycl::image::get_pitch
detail::enable_if_t< B, range< Dimensions - 1 > > get_pitch() const
Definition: image.hpp:262
image_impl.hpp
cl::sycl::image_channel_type::unorm_int8
@ unorm_int8
cl::sycl::image_channel_order::rgx
@ rgx
cl::sycl::image_channel_type::unorm_short_555
@ unorm_short_555
cl::sycl::image_channel_order::argb
@ argb
cl::sycl::accessor
Buffer accessor.
Definition: accessor.hpp:225
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::image_channel_order::rgba
@ rgba
cl::sycl::image_channel_order::r
@ r
cl::sycl::image::image
image(image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, AllocatorT Allocator, const property_list &PropList={})
Definition: image.hpp:83
cl::sycl::image::image
image(image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const typename detail::enable_if_t< B, range< Dimensions - 1 >> &Pitch, const property_list &PropList={})
Definition: image.hpp:95
cl::sycl::image_channel_order::rg
@ rg
cl::sycl::image_channel_order::abgr
@ abgr
cl::sycl::image_channel_order::bgra
@ bgra
cl::sycl::handler
Command group handler class.
Definition: handler.hpp:361
cl::sycl::aspect::fp16
@ fp16
cl::sycl::ext::oneapi::accessor_property_list
Objects of the accessor_property_list class are containers for the SYCL properties.
Definition: property_list.hpp:19
cl::sycl::image
Defines a shared image data.
Definition: image_impl.hpp:29
cl::sycl::image::image
image(const void *HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, AllocatorT Allocator, const property_list &PropList={})
Definition: image.hpp:145
cl::sycl::image_channel_order::a
@ a
cl::sycl::image_channel_order::rgbx
@ rgbx
cl::sycl::image::get_allocator
AllocatorT get_allocator() const
Definition: image.hpp:275
cl::sycl::access::target::image
@ image
cl::sycl::image::image
image(std::shared_ptr< void > &HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const typename detail::enable_if_t< B, range< Dimensions - 1 >> &Pitch, AllocatorT Allocator, const property_list &PropList={})
Definition: image.hpp:213
cl::sycl::detail::getSyclObjImpl
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: common.hpp:198
cl::sycl::image::set_final_data
void set_final_data(Destination finalData=nullptr)
Definition: image.hpp:301
cl::sycl::info::event
event
Definition: info_desc.hpp:283
cl::sycl::image::__SYCL2020_DEPRECATED
__SYCL2020_DEPRECATED("get_count() is deprecated, please use size() instead") size_t get_count() const
Definition: image.hpp:270
std
Definition: accessor.hpp:2532
cl::sycl::image_channel_order::intensity
@ intensity
cl::sycl::image_channel_order::ext_oneapi_srgba
@ ext_oneapi_srgba
cl::sycl::image::image
image(void *HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const typename detail::enable_if_t< B, range< Dimensions - 1 >> &Pitch, AllocatorT Allocator, const property_list &PropList={})
Definition: image.hpp:169
cl::sycl::image::image
image(void *HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const property_list &PropList={})
Definition: image.hpp:118
common.hpp
cl::sycl::image_channel_order::luminance
@ luminance
cl::sycl::image_channel_type
image_channel_type
Definition: image.hpp:41
cl::sycl::info::context
context
Definition: info_desc.hpp:41
types.hpp
cl::sycl::detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: stl_type_traits.hpp:24
cl::sycl::image::get_property
propertyT get_property() const
Definition: image.hpp:254
cl::sycl::image::image
image(image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const typename detail::enable_if_t< B, range< Dimensions - 1 >> &Pitch, AllocatorT Allocator, const property_list &PropList={})
Definition: image.hpp:107
cl::sycl::image::operator!=
bool operator!=(const image &rhs) const
Definition: image.hpp:247
cl::sycl::image::image
image(const void *HostPointer, image_channel_order Order, image_channel_type Type, const range< Dimensions > &Range, const property_list &PropList={})
Definition: image.hpp:136
cl::sycl::image_channel_order
image_channel_order
Definition: image.hpp:23
cl::sycl::image_channel_type::signed_int8
@ signed_int8
cl::sycl::image_channel_type::signed_int16
@ signed_int16
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12