okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
37.5 KiB1075 linesraw
1//==---- image.hpp --------------------------------*- C++ -*----------------==//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __DPCT_IMAGE_HPP__
10#define __DPCT_IMAGE_HPP__
11
12#include <sycl/sycl.hpp>
13
14#include "memory.hpp"
15#include "util.hpp"
16
17namespace dpct {
18
19enum class image_channel_data_type {
20  signed_int,
21  unsigned_int,
22  fp,
23};
24
25enum class image_type {
26  standard,
27  array,
28};
29
30class image_channel;
31class image_wrapper_base;
32namespace detail {
33/// Image object type traits, with accessor type and sampled data type defined.
34/// The data type of an image accessor must be one of sycl::int4, sycl::uint4,
35/// sycl::float4 and sycl::half4. The data type of accessors with 8bits/16bits
36/// channel width will be 32 bits. sycl::half is an exception.
37template <class T> struct image_trait {
38  using acc_data_t = sycl::vec<T, 4>;
39  template <int dimensions>
40  using accessor_t =
41      sycl::accessor<acc_data_t, dimensions, sycl::access_mode::read,
42                         sycl::access::target::image>;
43  template <int dimensions>
44  using array_accessor_t =
45      sycl::accessor<acc_data_t, dimensions, sycl::access_mode::read,
46                         sycl::access::target::image_array>;
47  using data_t = T;
48  using elem_t = T;
49  static constexpr image_channel_data_type data_type =
50      std::is_integral<T>::value
51          ? (std::is_signed<T>::value ? image_channel_data_type::signed_int
52                                      : image_channel_data_type::unsigned_int)
53          : image_channel_data_type::fp;
54  static constexpr int channel_num = 1;
55};
56template <>
57struct image_trait<std::uint8_t> : public image_trait<std::uint32_t> {
58  using data_t = std::uint8_t;
59  using elem_t = data_t;
60};
61template <>
62struct image_trait<std::uint16_t>
63    : public image_trait<std::uint32_t> {
64  using data_t = std::uint16_t;
65  using elem_t = data_t;
66};
67template <>
68struct image_trait<std::int8_t> : public image_trait<std::int32_t> {
69  using data_t = std::int8_t;
70  using elem_t = data_t;
71};
72template <>
73struct image_trait<std::int16_t> : public image_trait<std::int32_t> {
74  using data_t = std::int16_t;
75  using elem_t = data_t;
76};
77template <>
78struct image_trait<char>
79    : public image_trait<typename std::conditional<
80          std::is_signed<char>::value, signed char, unsigned char>::type> {};
81
82template <class T>
83struct image_trait<sycl::vec<T, 1>> : public image_trait<T> {};
84
85template <class T>
86struct image_trait<sycl::vec<T, 2>> : public image_trait<T> {
87  using data_t = sycl::vec<T, 2>;
88  static constexpr int channel_num = 2;
89};
90
91template <class T>
92struct image_trait<sycl::vec<T, 3>>
93    : public image_trait<sycl::vec<T, 4>> {
94  static constexpr int channel_num = 3;
95};
96
97template <class T>
98struct image_trait<sycl::vec<T, 4>> : public image_trait<T> {
99  using data_t = sycl::vec<T, 4>;
100  static constexpr int channel_num = 4;
101};
102
103/// Functor to fetch data from read result of an image accessor.
104template <class T> struct fetch_data {
105  using return_t = typename image_trait<T>::data_t;
106  using acc_data_t = typename image_trait<T>::acc_data_t;
107
108  return_t operator()(acc_data_t &&original_data) {
109    return (return_t)original_data.r();
110  }
111};
112template <class T>
113struct fetch_data<sycl::vec<T, 1>> : public fetch_data<T> {};
114template <class T> struct fetch_data<sycl::vec<T, 2>> {
115  using return_t = typename image_trait<sycl::vec<T, 2>>::data_t;
116  using acc_data_t = typename image_trait<sycl::vec<T, 2>>::acc_data_t;
117
118  return_t operator()(acc_data_t &&origin_data) {
119    return return_t(origin_data.r(), origin_data.g());
120  }
121};
122template <class T>
123struct fetch_data<sycl::vec<T, 3>>
124    : public fetch_data<sycl::vec<T, 4>> {};
125template <class T> struct fetch_data<sycl::vec<T, 4>> {
126  using return_t = typename image_trait<sycl::vec<T, 4>>::data_t;
127  using acc_data_t = typename image_trait<sycl::vec<T, 4>>::acc_data_t;
128
129  return_t operator()(acc_data_t &&origin_data) {
130    return return_t(origin_data.r(), origin_data.g(), origin_data.b(),
131                    origin_data.a());
132  }
133};
134
135/// Create image according with given type \p T and \p dims.
136template <class T> static image_wrapper_base *create_image_wrapper(int dims);
137
138/// Create image with given data type \p T, channel order and dims
139template <class T>
140static image_wrapper_base *create_image_wrapper(unsigned channel_num, int dims);
141
142/// Create image with channel info and specified dimensions.
143static image_wrapper_base *create_image_wrapper(image_channel channel, int dims);
144
145} // namespace detail
146
147struct image_matrix_desc {
148  size_t width = 0;
149  size_t height = 0;
150  size_t depth = 0;
151  sycl::image_channel_type channel_type =
152      sycl::image_channel_type::signed_int32;
153  unsigned num_channels = 0;
154};
155
156/// Image channel info, include channel number, order, data width and type
157class image_channel {
158  image_channel_data_type _type = image_channel_data_type::signed_int;
159  /// Number of channels.
160  unsigned _channel_num = 0;
161  /// Total size of all channels in bytes.
162  unsigned _total_size = 0;
163  /// Size of each channel in bytes.
164  unsigned _channel_size = 0;
165
166public:
167  /// Create image channel info according to template argument \p T.
168  template <class T> static image_channel create() {
169    image_channel channel;
170    channel.set_channel_size(detail::image_trait<T>::channel_num,
171                             sizeof(typename detail::image_trait<T>::elem_t) *
172                                 8);
173    channel.set_channel_data_type(detail::image_trait<T>::data_type);
174    return channel;
175  }
176
177  image_channel() = default;
178
179  image_channel_data_type get_channel_data_type() { return _type; }
180  void set_channel_data_type(image_channel_data_type type) { _type = type; }
181
182  unsigned get_total_size() const { return _total_size; }
183
184  unsigned get_channel_num() const { return _channel_num; }
185  void set_channel_num(unsigned channel_num) {
186    _channel_num = channel_num;
187    _total_size = _channel_size * _channel_num;
188  }
189
190  /// image_channel constructor.
191  /// \param r Channel r width in bits.
192  /// \param g Channel g width in bits. Should be same with \p r, or zero.
193  /// \param b Channel b width in bits. Should be same with \p g, or zero.
194  /// \param a Channel a width in bits. Should be same with \p b, or zero.
195  /// \param data_type Image channel data type: signed_nt, unsigned_int or fp.
196  image_channel(int r, int g, int b, int a, image_channel_data_type data_type) {
197    _type = data_type;
198    if (a) {
199      assert(r == a && "SYCL doesn't support different channel size");
200      assert(r == b && "SYCL doesn't support different channel size");
201      assert(r == g && "SYCL doesn't support different channel size");
202      set_channel_size(4, a);
203    } else if (b) {
204      assert(r == b && "SYCL doesn't support different channel size");
205      assert(r == g && "SYCL doesn't support different channel size");
206      set_channel_size(3, b);
207    } else if (g) {
208      assert(r == g && "SYCL doesn't support different channel size");
209      set_channel_size(2, g);
210    } else {
211      set_channel_size(1, r);
212    }
213  }
214
215  sycl::image_channel_type get_channel_type() const {
216    if (_channel_size == 4) {
217      if (_type == image_channel_data_type::signed_int)
218        return sycl::image_channel_type::signed_int32;
219      else if (_type == image_channel_data_type::unsigned_int)
220        return sycl::image_channel_type::unsigned_int32;
221      else if (_type == image_channel_data_type::fp)
222        return sycl::image_channel_type::fp32;
223    } else if (_channel_size == 2) {
224      if (_type == image_channel_data_type::signed_int)
225        return sycl::image_channel_type::signed_int16;
226      else if (_type == image_channel_data_type::unsigned_int)
227        return sycl::image_channel_type::unsigned_int16;
228      else if (_type == image_channel_data_type::fp)
229        return sycl::image_channel_type::fp16;
230    } else {
231      if (_type == image_channel_data_type::signed_int)
232        return sycl::image_channel_type::signed_int8;
233      else if (_type == image_channel_data_type::unsigned_int)
234        return sycl::image_channel_type::unsigned_int8;
235    }
236    assert(false && "unexpected channel data kind and channel size");
237    return sycl::image_channel_type::signed_int32;
238  }
239  void set_channel_type(sycl::image_channel_type type) {
240    switch (type) {
241    case sycl::image_channel_type::unsigned_int8:
242      _type = image_channel_data_type::unsigned_int;
243      _channel_size = 1;
244      break;
245    case sycl::image_channel_type::unsigned_int16:
246      _type = image_channel_data_type::unsigned_int;
247      _channel_size = 2;
248      break;
249    case sycl::image_channel_type::unsigned_int32:
250      _type = image_channel_data_type::unsigned_int;
251      _channel_size = 4;
252      break;
253    case sycl::image_channel_type::signed_int8:
254      _type = image_channel_data_type::signed_int;
255      _channel_size = 1;
256      break;
257    case sycl::image_channel_type::signed_int16:
258      _type = image_channel_data_type::signed_int;
259      _channel_size = 2;
260      break;
261    case sycl::image_channel_type::signed_int32:
262      _type = image_channel_data_type::signed_int;
263      _channel_size = 4;
264      break;
265    case sycl::image_channel_type::fp16:
266      _type = image_channel_data_type::fp;
267      _channel_size = 2;
268      break;
269    case sycl::image_channel_type::fp32:
270      _type = image_channel_data_type::fp;
271      _channel_size = 4;
272      break;
273    default:
274      break;
275    }
276    _total_size = _channel_size * _channel_num;
277  }
278
279  sycl::image_channel_order get_channel_order() const {
280    switch (_channel_num) {
281    case 1:
282      return sycl::image_channel_order::r;
283    case 2:
284      return sycl::image_channel_order::rg;
285    case 3:
286      return sycl::image_channel_order::rgb;
287    case 4:
288      return sycl::image_channel_order::rgba;
289    default:
290      return sycl::image_channel_order::r;
291    }
292  }
293  /// Get the size for each channel in bits.
294  unsigned get_channel_size() const { return _channel_size * 8; }
295
296  /// Set channel size.
297  /// \param in_channel_num Channels number to set.
298  /// \param channel_size Size for each channel in bits.
299  void set_channel_size(unsigned in_channel_num,
300                        unsigned channel_size) {
301    if (in_channel_num < _channel_num)
302      return;
303    _channel_num = in_channel_num;
304    _channel_size = channel_size / 8;
305    _total_size = _channel_size * _channel_num;
306  }
307};
308
309/// 2D or 3D matrix data for image.
310class image_matrix {
311  image_channel _channel;
312  int _range[3] = {1, 1, 1};
313  int _dims = 0;
314  void *_host_data = nullptr;
315
316  /// Set range of each dimension.
317  template <int dimensions> void set_range(sycl::range<dimensions> range) {
318    for (int i = 0; i < dimensions; ++i)
319      _range[i] = range[i];
320    _dims = dimensions;
321  }
322
323  template <int... DimIdx>
324  sycl::range<sizeof...(DimIdx)> get_range(integer_sequence<DimIdx...>) {
325    return sycl::range<sizeof...(DimIdx)>(_range[DimIdx]...);
326  }
327
328public:
329  /// Constructor with channel info and dimension size info.
330  template <int dimensions>
331  image_matrix(image_channel channel, sycl::range<dimensions> range,
332               image_type type = image_type::standard)
333      : _channel(channel) {
334    if (type == image_type::array && range[1] == 0) {
335      range[1] = range[2];
336      range[2] = 0;
337    }
338    set_range(range);
339    _host_data = std::malloc(range.size() * _channel.get_total_size());
340  }
341  image_matrix(sycl::image_channel_type channel_type, unsigned channel_num,
342               size_t x, size_t y) {
343    _channel.set_channel_type(channel_type);
344    _channel.set_channel_num(channel_num);
345    _dims = 1;
346    _range[0] = x;
347    if (y) {
348      _dims = 2;
349      _range[1] = y;
350    }
351    _host_data = std::malloc(_range[0] * _range[1] * _channel.get_total_size());
352  }
353  image_matrix(const image_matrix_desc *desc)
354      : image_matrix(desc->channel_type, desc->num_channels, desc->width,
355                     desc->height) {}
356
357  /// Construct a new image class with the matrix data.
358  template <int dimensions> sycl::image<dimensions> *create_image() {
359    return create_image<dimensions>(_channel);
360  }
361  /// Construct a new image class with the matrix data.
362  template <int dimensions>
363  sycl::image<dimensions> *create_image(image_channel channel) {
364    return new sycl::image<dimensions>(
365        _host_data, channel.get_channel_order(), channel.get_channel_type(),
366        get_range(make_index_sequence<dimensions>()),
367        sycl::property::image::use_host_ptr());
368  }
369
370  /// Get channel info.
371  inline image_channel get_channel() { return _channel; }
372  /// Get range of the image.
373  sycl::range<3> get_range() {
374    return sycl::range<3>(_range[0], _range[1], _range[2]);
375  }
376  /// Get matrix dims.
377  inline int get_dims() { return _dims; }
378  /// Convert to pitched data.
379  pitched_data to_pitched_data() {
380    return pitched_data(_host_data, _range[0] * _channel.get_total_size(),
381                        _range[0], _range[1]);
382  }
383
384  ~image_matrix() {
385    if (_host_data)
386      std::free(_host_data);
387    _host_data = nullptr;
388  }
389};
390using image_matrix_p = image_matrix *;
391
392enum class image_data_type { matrix, linear, pitch, unsupport };
393
394#ifdef SYCL_EXT_ONEAPI_BINDLESS_IMAGES
395namespace experimental {
396class image_mem_wrapper;
397}
398#endif
399
400/// Image data info.
401class image_data {
402public:
403  image_data() { _type = image_data_type::unsupport; }
404  image_data(image_matrix_p matrix_data) { set_data(matrix_data); }
405  image_data(void *data_ptr, size_t x_size, image_channel channel) {
406    set_data(data_ptr, x_size, channel);
407  }
408  image_data(void *data_ptr, size_t x_size, size_t y_size, size_t pitch_size,
409             image_channel channel) {
410    set_data(data_ptr, x_size, y_size, pitch_size, channel);
411  }
412  void set_data(image_matrix_p matrix_data) {
413    _type = image_data_type::matrix;
414    _data = matrix_data;
415    _channel = matrix_data->get_channel();
416  }
417#ifdef SYCL_EXT_ONEAPI_BINDLESS_IMAGES
418  void set_data(experimental::image_mem_wrapper *image_mem) {
419    _type = image_data_type::matrix;
420    _data = image_mem;
421  }
422#endif
423  void set_data(void *data_ptr, size_t x_size, image_channel channel) {
424    _type = image_data_type::linear;
425    _data = data_ptr;
426    _x = x_size;
427    _channel = channel;
428  }
429  void set_data(void *data_ptr, size_t x_size, size_t y_size, size_t pitch_size,
430                image_channel channel) {
431    _type = image_data_type::pitch;
432    _data = data_ptr;
433    _x = x_size;
434    _y = y_size;
435    _pitch = pitch_size;
436    _channel = channel;
437  }
438
439  image_data_type get_data_type() const { return _type; }
440  void set_data_type(image_data_type type) { _type = type; }
441
442  void *get_data_ptr() const { return _data; }
443  void set_data_ptr(void *data) { _data = data; }
444
445  size_t get_x() const { return _x; }
446  void set_x(size_t x) { _x = x; }
447
448  size_t get_y() const { return _y; }
449  void set_y(size_t y) { _y = y; }
450
451  size_t get_pitch() const { return _pitch; }
452  void set_pitch(size_t pitch) { _pitch = pitch; }
453
454  image_channel get_channel() const { return _channel; }
455  void set_channel(image_channel channel) { _channel = channel; }
456
457  image_channel_data_type get_channel_data_type() {
458    return _channel.get_channel_data_type();
459  }
460  void set_channel_data_type(image_channel_data_type type) {
461    _channel.set_channel_data_type(type);
462  }
463
464  unsigned get_channel_size() { return _channel.get_channel_size(); }
465  void set_channel_size(unsigned channel_num, unsigned channel_size) {
466    return _channel.set_channel_size(channel_num, channel_size);
467  }
468
469  unsigned get_channel_num() { return _channel.get_channel_num(); }
470  void set_channel_num(unsigned num) {
471    return _channel.set_channel_num(num);
472  }
473
474  sycl::image_channel_type get_channel_type() {
475    return _channel.get_channel_type();
476  }
477  void set_channel_type(sycl::image_channel_type type) {
478    return _channel.set_channel_type(type);
479  }
480
481private:
482  image_data_type _type;
483  void *_data = nullptr;
484  size_t _x, _y, _pitch;
485  image_channel _channel;
486};
487
488/// Image sampling info, include addressing mode, filtering mode and
489/// normalization info.
490class sampling_info {
491  sycl::addressing_mode _addressing_mode =
492      sycl::addressing_mode::clamp_to_edge;
493  sycl::filtering_mode _filtering_mode = sycl::filtering_mode::nearest;
494  sycl::coordinate_normalization_mode _coordinate_normalization_mode =
495      sycl::coordinate_normalization_mode::unnormalized;
496  // Dictates the method in which sampling between mipmap levels is performed.
497  sycl::filtering_mode _mipmap_filtering = sycl::filtering_mode::nearest;
498  // Defines the minimum mipmap level from which we can sample, with the minimum
499  // value being 0.
500  float _min_mipmap_level_clamp = 0.f;
501  // Defines the maximum mipmap level from which we can sample. This value
502  // cannot be higher than the number of allocated levels.
503  float _max_mipmap_level_clamp = 0.f;
504  // Dictates the anisotropic ratio used when sampling the mipmap with
505  // anisotropic filtering.
506  float _max_anisotropy = 0.f;
507
508public:
509  sycl::addressing_mode get_addressing_mode() const noexcept {
510    // Make sure the return value is legal addressing_mode when using memset.
511    if ((unsigned)_addressing_mode == 0)
512      return sycl::addressing_mode::clamp_to_edge;
513    return _addressing_mode;
514  }
515  void set(sycl::addressing_mode addressing_mode) noexcept {
516    _addressing_mode = addressing_mode;
517  }
518
519  sycl::filtering_mode get_filtering_mode() const noexcept {
520    // Make sure the return value is legal filtering_mode when using memset.
521    return _filtering_mode == sycl::filtering_mode::linear
522               ? sycl::filtering_mode::linear
523               : sycl::filtering_mode::nearest;
524  }
525  void set(sycl::filtering_mode filtering_mode) noexcept {
526    _filtering_mode = filtering_mode;
527  }
528
529  sycl::coordinate_normalization_mode
530  get_coordinate_normalization_mode() const noexcept {
531    return _coordinate_normalization_mode;
532  }
533  void set(sycl::coordinate_normalization_mode
534               coordinate_normalization_mode) noexcept {
535    _coordinate_normalization_mode = coordinate_normalization_mode;
536  }
537
538  bool is_coordinate_normalized() const noexcept {
539    return _coordinate_normalization_mode ==
540           sycl::coordinate_normalization_mode::normalized;
541  }
542  void set_coordinate_normalization_mode(int is_normalized) noexcept {
543    _coordinate_normalization_mode =
544        is_normalized ? sycl::coordinate_normalization_mode::normalized
545                      : sycl::coordinate_normalization_mode::unnormalized;
546  }
547
548  /// Get the method in which sampling between mipmap levels is performed.
549  /// \returns The method in which sampling between mipmap levels is performed.
550  sycl::filtering_mode get_mipmap_filtering() const noexcept {
551    // Make sure the return value is legal filtering_mode when using memset.
552    return _mipmap_filtering == sycl::filtering_mode::linear
553               ? sycl::filtering_mode::linear
554               : sycl::filtering_mode::nearest;
555  }
556  /// Set the method in which sampling between mipmap levels is performed.
557  /// \param [in] filtering_mode The method in which sampling between mipmap
558  /// levels is performed.
559  void set_mipmap_filtering(sycl::filtering_mode filtering_mode) noexcept {
560    _mipmap_filtering = filtering_mode;
561  }
562
563  /// Get the minimum mipmap level from which we can sample
564  /// \returns The minimum mipmap level from which we can sample.
565  float get_min_mipmap_level_clamp() const noexcept {
566    return _min_mipmap_level_clamp;
567  }
568  /// Set the minimum mipmap level from which we can sample
569  /// \param [in] min_mipmap_level_clamp The minimum mipmap level from which we
570  /// can sample.
571  void set_min_mipmap_level_clamp(float min_mipmap_level_clamp) noexcept {
572    _min_mipmap_level_clamp = min_mipmap_level_clamp;
573  }
574
575  /// Get the maximum mipmap level from which we can sample.
576  /// \returns The maximum mipmap level from which we can sample.
577  float get_max_mipmap_level_clamp() const noexcept {
578    return _max_mipmap_level_clamp;
579  }
580  /// Set the maximum mipmap level from which we can sample.
581  /// \param [in] max_mipmap_level_clamp The maximum mipmap level from which we
582  /// can sample.
583  void set_max_mipmap_level_clamp(float max_mipmap_level_clamp) noexcept {
584    _max_mipmap_level_clamp = max_mipmap_level_clamp;
585  }
586
587  /// Get the anisotropic ratio used when sampling the mipmap with
588  // anisotropic filtering.
589  /// \returns The anisotropic ratio used when sampling the mipmap with
590  // anisotropic filtering.
591  float get_max_anisotropy() const noexcept { return _max_anisotropy; }
592  /// Set the anisotropic ratio used when sampling the mipmap with
593  // anisotropic filtering.
594  /// \param [in] max_anisotropy The anisotropic ratio used when sampling the
595  /// mipmap with anisotropic filtering.
596  void set_max_anisotropy(float max_anisotropy) noexcept {
597    _max_anisotropy = max_anisotropy;
598  }
599
600  void set(sycl::addressing_mode addressing_mode,
601           sycl::filtering_mode filtering_mode,
602           sycl::coordinate_normalization_mode
603               coordinate_normalization_mode) noexcept {
604    set(addressing_mode);
605    set(filtering_mode);
606    set(coordinate_normalization_mode);
607  }
608  void set(sycl::addressing_mode addressing_mode,
609           sycl::filtering_mode filtering_mode, int is_normalized) noexcept {
610    set(addressing_mode);
611    set(filtering_mode);
612    set_coordinate_normalization_mode(is_normalized);
613  }
614
615  sycl::sampler get_sampler() const {
616    return sycl::sampler(_coordinate_normalization_mode, _addressing_mode,
617                         _filtering_mode);
618  }
619};
620
621/// Image base class.
622class image_wrapper_base {
623  sampling_info _sampling_info;
624  image_data _data;
625
626public:
627  virtual ~image_wrapper_base() = 0;
628
629  void attach(image_data data) { set_data(data); }
630  /// Attach matrix data to this class.
631  void attach(image_matrix *matrix) {
632    detach();
633    image_wrapper_base::set_data(image_data(matrix));
634  }
635  /// Attach matrix data to this class.
636  void attach(image_matrix *matrix, image_channel channel) {
637    attach(matrix);
638    image_wrapper_base::set_channel(channel);
639  }
640  /// Attach linear data to this class.
641  void attach(const void *ptr, size_t count) {
642    attach(ptr, count, get_channel());
643  }
644  /// Attach linear data to this class.
645  void attach(const void *ptr, size_t count, image_channel channel) {
646    detach();
647    image_wrapper_base::set_data(image_data(const_cast<void *>(ptr), count, channel));
648  }
649  /// Attach 2D data to this class.
650  void attach(const void *data, size_t x, size_t y, size_t pitch) {
651    attach(data, x, y, pitch, get_channel());
652  }
653  /// Attach 2D data to this class.
654  void attach(const void *data, size_t x, size_t y, size_t pitch,
655              image_channel channel) {
656    detach();
657    image_wrapper_base::set_data(
658        image_data(const_cast<void *>(data), x, y, pitch, channel));
659  }
660  /// Attach device_ptr data to this class.
661  void attach(const image_matrix_desc *desc, device_ptr ptr, size_t pitch) {
662    detach();
663    image_channel channel;
664    channel.set_channel_num(desc->num_channels);
665    channel.set_channel_type(desc->channel_type);
666    image_wrapper_base::set_data(
667        image_data(ptr, desc->width, desc->height, pitch, channel));
668  }
669  /// Detach data.
670  virtual void detach() {}
671
672  sampling_info get_sampling_info() { return _sampling_info; }
673  void set_sampling_info(sampling_info info) {
674    _sampling_info = info;
675  }
676  const image_data &get_data() { return _data; }
677  void set_data(image_data data) { _data = data; }
678
679  image_channel get_channel() { return _data.get_channel(); }
680  void set_channel(image_channel channel) { _data.set_channel(channel); }
681
682  image_channel_data_type get_channel_data_type() {
683    return _data.get_channel_data_type();
684  }
685  void set_channel_data_type(image_channel_data_type type) {
686    _data.set_channel_data_type(type);
687  }
688
689  unsigned get_channel_size() { return _data.get_channel_size(); }
690  void set_channel_size(unsigned channel_num, unsigned channel_size) {
691    return _data.set_channel_size(channel_num, channel_size);
692  }
693
694  sycl::addressing_mode get_addressing_mode() {
695    return _sampling_info.get_addressing_mode();
696  }
697  void set(sycl::addressing_mode addressing_mode) {
698    _sampling_info.set(addressing_mode);
699  }
700
701  sycl::filtering_mode get_filtering_mode() {
702    return _sampling_info.get_filtering_mode();
703  }
704  void set(sycl::filtering_mode filtering_mode) {
705    _sampling_info.set(filtering_mode);
706  }
707
708  sycl::coordinate_normalization_mode get_coordinate_normalization_mode() {
709    return _sampling_info.get_coordinate_normalization_mode();
710  }
711  void
712  set(sycl::coordinate_normalization_mode coordinate_normalization_mode) {
713    _sampling_info.set(coordinate_normalization_mode);
714  }
715
716  bool is_coordinate_normalized() {
717    return _sampling_info.is_coordinate_normalized();
718  }
719  void set_coordinate_normalization_mode(int is_normalized) {
720    _sampling_info.set_coordinate_normalization_mode(is_normalized);
721  }
722  void
723  set(sycl::addressing_mode addressing_mode,
724      sycl::filtering_mode filtering_mode,
725      sycl::coordinate_normalization_mode coordinate_normalization_mode) {
726    set(addressing_mode);
727    set(filtering_mode);
728    set(coordinate_normalization_mode);
729  }
730  void set(sycl::addressing_mode addressing_mode,
731           sycl::filtering_mode filtering_mode, int is_normalized) {
732    set(addressing_mode);
733    set(filtering_mode);
734    set_coordinate_normalization_mode(is_normalized);
735  }
736
737  unsigned get_channel_num() { return _data.get_channel_num(); }
738  void set_channel_num(unsigned num) {
739    return _data.set_channel_num(num);
740  }
741
742  sycl::image_channel_type get_channel_type() {
743    return _data.get_channel_type();
744  }
745  void set_channel_type(sycl::image_channel_type type) {
746    return _data.set_channel_type(type);
747  }
748
749  sycl::sampler get_sampler() {
750    sycl::sampler smp = _sampling_info.get_sampler();
751    /// linear memory only used for sycl::filtering_mode::nearest.
752    if (_data.get_data_type() == image_data_type::linear) {
753      smp = sycl::sampler(smp.get_coordinate_normalization_mode(),
754                          smp.get_addressing_mode(),
755                          sycl::filtering_mode::nearest);
756    }
757    return smp;
758  }
759};
760inline image_wrapper_base::~image_wrapper_base() {}
761using image_wrapper_base_p = image_wrapper_base *;
762
763template <class T, int dimensions, bool IsImageArray> class image_accessor_ext;
764
765/// Image class, wrapper of sycl::image.
766template <class T, int dimensions, bool IsImageArray = false> class image_wrapper : public image_wrapper_base {
767  sycl::image<dimensions> *_image = nullptr;
768
769#ifndef DPCT_USM_LEVEL_NONE
770  std::vector<char> _host_buffer;
771#endif
772
773public:
774  void create_image(sycl::queue &q = get_default_queue()) {
775    if (_image)
776      return;
777    auto &data = get_data();
778    if (data.get_data_type() == image_data_type::matrix) {
779      _image = static_cast<image_matrix_p>(data.get_data_ptr())
780          ->create_image<dimensions>(data.get_channel());
781      return;
782    }
783    auto ptr = data.get_data_ptr();
784    auto channel = data.get_channel();
785
786    if (detail::get_pointer_attribute(q, ptr) == detail::pointer_access_attribute::device_only) {
787#ifdef DPCT_USM_LEVEL_NONE
788      ptr = get_buffer(ptr).get_host_access().get_pointer();
789#else
790      auto sz = data.get_x();
791      if (data.get_data_type() == image_data_type::pitch)
792        sz *= channel.get_total_size() * data.get_y();
793      _host_buffer.resize(sz);
794      q.memcpy(_host_buffer.data(), ptr, sz);
795      ptr = _host_buffer.data();
796#endif
797    }
798
799    if constexpr (dimensions == 1) {
800      assert(data.get_data_type() == image_data_type::linear);
801      _image = new sycl::image<1>(
802        ptr, channel.get_channel_order(), channel.get_channel_type(),
803        sycl::range<1>(data.get_x() / channel.get_total_size()));
804    } else if constexpr (dimensions == 2) {
805      assert(data.get_data_type() == image_data_type::pitch);
806      _image = new sycl::image<2>(ptr, channel.get_channel_order(),
807                                  channel.get_channel_type(),
808                                  sycl::range<2>(data.get_x(), data.get_y()),
809                                  sycl::range<1>(data.get_pitch()));
810    } else {
811      throw std::runtime_error("3D image only support matrix data");
812    }
813    return;
814  }
815
816  using acc_data_t = typename detail::image_trait<T>::acc_data_t;
817  using accessor_t =
818      typename image_accessor_ext<T, IsImageArray ? (dimensions - 1) : dimensions,
819                              IsImageArray>::accessor_t;
820
821  image_wrapper() { set_channel(image_channel::create<T>()); }
822  ~image_wrapper() override { detach(); }
823
824  /// Get image accessor.
825  accessor_t get_access(sycl::handler &cgh) {
826    assert(_image != nullptr && "Image not created");
827    return accessor_t(*_image, cgh);
828  }
829
830  /// Detach data.
831  void detach() override {
832    if (_image)
833      delete _image;
834    _image = nullptr;
835  }
836};
837
838/// Wrap sampler and image accessor together.
839template <class T, int dimensions, bool IsImageArray = false>
840class image_accessor_ext {
841public:
842  using accessor_t =
843      typename detail::image_trait<T>::template accessor_t<dimensions>;
844  using data_t = typename detail::image_trait<T>::data_t;
845  sycl::sampler _sampler;
846  accessor_t _img_acc;
847
848public:
849  image_accessor_ext(sycl::sampler sampler, accessor_t acc)
850      : _sampler(sampler), _img_acc(acc) {}
851
852  /// Read data from accessor.
853  template <bool Available = dimensions == 3>
854  typename std::enable_if<Available, data_t>::type read(float x, float y,
855                                                        float z) {
856    return detail::fetch_data<T>()(
857        _img_acc.read(sycl::float4(x, y, z, 0), _sampler));
858  }
859  /// Read data from accessor.
860  template <class Coord0, class Coord1, class Coord2,
861            bool Available = dimensions == 3 &&
862                             std::is_integral<Coord0>::value
863                                 &&std::is_integral<Coord1>::value
864                                     &&std::is_integral<Coord2>::value>
865  typename std::enable_if<Available, data_t>::type read(Coord0 x, Coord1 y,
866                                                        Coord2 z) {
867    return detail::fetch_data<T>()(
868        _img_acc.read(sycl::int4(x, y, z, 0), _sampler));
869  }
870  /// Read data from accessor.
871  template <bool Available = dimensions == 2>
872  typename std::enable_if<Available, data_t>::type read(float x, float y) {
873    return detail::fetch_data<T>()(
874        _img_acc.read(sycl::float2(x, y), _sampler));
875  }
876  /// Read data from accessor.
877  template <class Coord0, class Coord1,
878            bool Available = dimensions == 2 &&
879                             std::is_integral<Coord0>::value
880                                 &&std::is_integral<Coord1>::value>
881  typename std::enable_if<Available, data_t>::type read(Coord0 x, Coord1 y) {
882    return detail::fetch_data<T>()(
883        _img_acc.read(sycl::int2(x, y), _sampler));
884  }
885  /// Read data from accessor.
886  template <bool Available = dimensions == 1>
887  typename std::enable_if<Available, data_t>::type read(float x) {
888    return detail::fetch_data<T>()(_img_acc.read(x, _sampler));
889  }
890  /// Read data from accessor.
891  template <class CoordT,
892            bool Available = dimensions == 1 && std::is_integral<CoordT>::value>
893  typename std::enable_if<Available, data_t>::type read(CoordT x) {
894    return detail::fetch_data<T>()(_img_acc.read(x, _sampler));
895  }
896
897  /// Read data from accessor.
898  template <bool Available = dimensions == 3>
899  typename std::enable_if<Available, data_t>::type read_byte(float x, float y,
900                                                             float z) {
901    return detail::fetch_data<T>()(
902        _img_acc.read(sycl::float4(x / sizeof(T), y, z, 0), _sampler));
903  }
904  /// Read data from accessor.
905  template <class Coord0, class Coord1, class Coord2,
906            bool Available = dimensions == 3 &&
907                             std::is_integral<Coord0>::value
908                                 &&std::is_integral<Coord1>::value
909                                     &&std::is_integral<Coord2>::value>
910  typename std::enable_if<Available, data_t>::type read_byte(Coord0 x, Coord1 y,
911                                                             Coord2 z) {
912    return detail::fetch_data<T>()(
913        _img_acc.read(sycl::int4(x / sizeof(T), y, z, 0), _sampler));
914  }
915
916  /// Read data from accessor.
917  template <class Coord0, class Coord1,
918            bool Available = dimensions == 2 &&
919                             std::is_integral<Coord0>::value
920                                 &&std::is_integral<Coord1>::value>
921  typename std::enable_if<Available, data_t>::type read_byte(Coord0 x,
922                                                             Coord1 y) {
923    return detail::fetch_data<T>()(
924        _img_acc.read(sycl::int2(x / sizeof(T), y), _sampler));
925  }
926  /// Read data from accessor.
927  template <bool Available = dimensions == 2>
928  typename std::enable_if<Available, data_t>::type read_byte(float x, float y) {
929    return detail::fetch_data<T>()(
930        _img_acc.read(sycl::float2(x / sizeof(T), y), _sampler));
931  }
932  /// Read data from accessor.
933  template <class CoordT,
934            bool Available = dimensions == 1 && std::is_integral<CoordT>::value>
935  typename std::enable_if<Available, data_t>::type read_byte(CoordT x) {
936    return detail::fetch_data<T>()(_img_acc.read(x / sizeof(T), _sampler));
937  }
938
939  /// Read data from accessor.
940  template <bool Available = dimensions == 1>
941  typename std::enable_if<Available, data_t>::type read_byte(float x) {
942    return detail::fetch_data<T>()(_img_acc.read(x / sizeof(T), _sampler));
943  }
944};
945
946template <class T, int dimensions> class image_accessor_ext<T, dimensions, true> {
947public:
948  using accessor_t =
949      typename detail::image_trait<T>::template array_accessor_t<dimensions>;
950  using data_t = typename detail::image_trait<T>::data_t;
951  sycl::sampler _sampler;
952  accessor_t _img_acc;
953
954public:
955  image_accessor_ext(sycl::sampler sampler, accessor_t acc)
956      : _sampler(sampler), _img_acc(acc) {}
957
958  /// Read data from accessor.
959  template <bool Available = dimensions == 2>
960  typename std::enable_if<Available, data_t>::type read(int index, float x,
961                                                        float y) {
962    return detail::fetch_data<T>()(
963        _img_acc[index].read(sycl::float2(x, y), _sampler));
964  }
965  /// Read data from accessor.
966  template <bool Available = dimensions == 2>
967  typename std::enable_if<Available, data_t>::type read(int index, int x, int y) {
968    return detail::fetch_data<T>()(
969        _img_acc[index].read(sycl::int2(x, y), _sampler));
970  }
971  /// Read data from accessor.
972  template <bool Available = dimensions == 1>
973  typename std::enable_if<Available, data_t>::type read(int index, float x) {
974    return detail::fetch_data<T>()(
975        _img_acc[index].read(x, _sampler));
976  }
977  /// Read data from accessor.
978  template <bool Available = dimensions == 1>
979  typename std::enable_if<Available, data_t>::type read(int index, int x) {
980    return detail::fetch_data<T>()(
981        _img_acc[index].read(x, _sampler));
982  }
983};
984
985/// Create image wrapper according to image data and sampling info.
986/// \return Pointer to image wrapper base class.
987/// \param data Image data used to create image wrapper.
988/// \param info Image sampling info used to create image wrapper.
989/// \returns Pointer to base class of created image wrapper object.
990static inline image_wrapper_base *create_image_wrapper(image_data data,
991                              sampling_info info = {}) {
992  image_channel channel;
993  int dims = 1;
994  if (data.get_data_type() == image_data_type::matrix) {
995    auto matrix = (image_matrix_p)data.get_data_ptr();
996    channel = matrix->get_channel();
997    dims = matrix->get_dims();
998  } else {
999    if (data.get_data_type() == image_data_type::pitch) {
1000      dims = 2;
1001    }
1002    channel = data.get_channel();
1003  }
1004
1005  if (auto ret = detail::create_image_wrapper(channel, dims)) {
1006    ret->set_sampling_info(info);
1007    ret->set_data(data);
1008    return ret;
1009  }
1010  return nullptr;
1011}
1012
1013namespace detail {
1014static inline pitched_data to_pitched_data(image_matrix *image) {
1015  return image->to_pitched_data();
1016}
1017
1018/// Create image according with given type \p T and \p dims.
1019template <class T> static image_wrapper_base *create_image_wrapper(int dims) {
1020  switch (dims) {
1021  case 1:
1022    return new image_wrapper<T, 1>();
1023  case 2:
1024    return new image_wrapper<T, 2>();
1025  case 3:
1026    return new image_wrapper<T, 3>();
1027  default:
1028    return nullptr;
1029  }
1030}
1031/// Create image with given data type \p T, channel order and dims
1032template <class T>
1033static image_wrapper_base *create_image_wrapper(unsigned channel_num, int dims) {
1034  switch (channel_num) {
1035  case 1:
1036    return create_image_wrapper<T>(dims);
1037  case 2:
1038    return create_image_wrapper<sycl::vec<T, 2>>(dims);
1039  case 3:
1040    return create_image_wrapper<sycl::vec<T, 3>>(dims);
1041  case 4:
1042    return create_image_wrapper<sycl::vec<T, 4>>(dims);
1043  default:
1044    return nullptr;
1045  }
1046}
1047
1048/// Create image with channel info and specified dimensions.
1049static image_wrapper_base *create_image_wrapper(image_channel channel, int dims) {
1050  switch (channel.get_channel_type()) {
1051  case sycl::image_channel_type::fp16:
1052    return create_image_wrapper<sycl::half>(channel.get_channel_num(), dims);
1053  case sycl::image_channel_type::fp32:
1054    return create_image_wrapper<float>(channel.get_channel_num(), dims);
1055  case sycl::image_channel_type::signed_int8:
1056    return create_image_wrapper<std::int8_t>(channel.get_channel_num(), dims);
1057  case sycl::image_channel_type::signed_int16:
1058    return create_image_wrapper<std::int16_t>(channel.get_channel_num(), dims);
1059  case sycl::image_channel_type::signed_int32:
1060    return create_image_wrapper<std::int32_t>(channel.get_channel_num(), dims);
1061  case sycl::image_channel_type::unsigned_int8:
1062    return create_image_wrapper<std::uint8_t>(channel.get_channel_num(), dims);
1063  case sycl::image_channel_type::unsigned_int16:
1064    return create_image_wrapper<std::uint16_t>(channel.get_channel_num(), dims);
1065  case sycl::image_channel_type::unsigned_int32:
1066    return create_image_wrapper<std::uint32_t>(channel.get_channel_num(), dims);
1067  default:
1068    return nullptr;
1069  }
1070}
1071} // namespace detail
1072
1073} // namespace dpct
1074
1075#endif // !__DPCT_IMAGE_HPP__