okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
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 { 20signed_int , 21unsigned_int , 22fp , 23}; 24 25enum class image_type { 26standard , 27array , 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 { 38using acc_data_t = sycl::vec < T ,4 > ; 39template < int dimensions > 40using accessor_t = 41 sycl::accessor < acc_data_t ,dimensions , sycl::access_mode::read , 42 sycl::access::target::image > ; 43template < int dimensions > 44using array_accessor_t = 45 sycl::accessor < acc_data_t ,dimensions , sycl::access_mode::read , 46 sycl::access::target::image_array > ; 47using data_t = T ; 48using elem_t = T ; 49static 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 ; 54static constexpr int channel_num = 1 ; 55}; 56template <> 57struct image_trait < std::uint8_t > :public image_trait < std::uint32_t > { 58using data_t = std::uint8_t ; 59using elem_t = data_t ; 60}; 61template <> 62struct image_trait < std::uint16_t > 63 :public image_trait < std::uint32_t > { 64using data_t = std::uint16_t ; 65using elem_t = data_t ; 66}; 67template <> 68struct image_trait < std::int8_t > :public image_trait < std::int32_t > { 69using data_t = std::int8_t ; 70using elem_t = data_t ; 71}; 72template <> 73struct image_trait < std::int16_t > :public image_trait < std::int32_t > { 74using data_t = std::int16_t ; 75using 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 > { 87using data_t = sycl::vec < T ,2 > ; 88static 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 >> { 94static constexpr int channel_num = 3 ; 95}; 96 97template < class T > 98struct image_trait < sycl::vec < T ,4 >> :public image_trait < T > { 99using data_t = sycl::vec < T ,4 > ; 100static 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 { 105using return_t = typename image_trait < T > ::data_t ; 106using acc_data_t = typename image_trait < T > ::acc_data_t ; 107 108return_t operator()(acc_data_t && original_data ) { 109return (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 >> { 115using return_t = typename image_trait < sycl::vec < T ,2 >> ::data_t ; 116using acc_data_t = typename image_trait < sycl::vec < T ,2 >> ::acc_data_t ; 117 118return_t operator()(acc_data_t && origin_data ) { 119return 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 >> { 126using return_t = typename image_trait < sycl::vec < T ,4 >> ::data_t ; 127using acc_data_t = typename image_trait < sycl::vec < T ,4 >> ::acc_data_t ; 128 129return_t operator()(acc_data_t && origin_data ) { 130return return_t (origin_data .r (),origin_data .g (),origin_data .b (), 131origin_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 { 148size_t width = 0 ; 149size_t height = 0 ; 150size_t depth = 0 ; 151 sycl::image_channel_type channel_type = 152 sycl::image_channel_type::signed_int32 ; 153unsigned num_channels = 0 ; 154}; 155 156/// Image channel info, include channel number, order, data width and type 157class image_channel { 158image_channel_data_type _type = image_channel_data_type::signed_int ; 159/// Number of channels. 160unsigned _channel_num = 0 ; 161/// Total size of all channels in bytes. 162unsigned _total_size = 0 ; 163/// Size of each channel in bytes. 164unsigned _channel_size = 0 ; 165 166public : 167/// Create image channel info according to template argument \p T. 168template < class T > static image_channel create () { 169image_channel channel ; 170channel .set_channel_size (detail::image_trait < T > ::channel_num , 171sizeof (typename detail::image_trait < T > ::elem_t )* 1728 ); 173channel .set_channel_data_type (detail::image_trait < T > ::data_type ); 174return channel ; 175 } 176 177image_channel ()= default ; 178 179image_channel_data_type get_channel_data_type () {return _type ; } 180void set_channel_data_type (image_channel_data_type type ) {_type = type ; } 181 182unsigned get_total_size ()const {return _total_size ; } 183 184unsigned get_channel_num ()const {return _channel_num ; } 185void 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. 196image_channel (int r ,int g ,int b ,int a ,image_channel_data_type data_type ) { 197_type = data_type ; 198if (a ) { 199assert (r == a && "SYCL doesn't support different channel size" ); 200assert (r == b && "SYCL doesn't support different channel size" ); 201assert (r == g && "SYCL doesn't support different channel size" ); 202set_channel_size (4 ,a ); 203 }else if (b ) { 204assert (r == b && "SYCL doesn't support different channel size" ); 205assert (r == g && "SYCL doesn't support different channel size" ); 206set_channel_size (3 ,b ); 207 }else if (g ) { 208assert (r == g && "SYCL doesn't support different channel size" ); 209set_channel_size (2 ,g ); 210 }else { 211set_channel_size (1 ,r ); 212 } 213 } 214 215 sycl::image_channel_type get_channel_type ()const { 216if (_channel_size == 4 ) { 217if (_type == image_channel_data_type::signed_int ) 218return sycl::image_channel_type::signed_int32 ; 219else if (_type == image_channel_data_type::unsigned_int ) 220return sycl::image_channel_type::unsigned_int32 ; 221else if (_type == image_channel_data_type::fp ) 222return sycl::image_channel_type::fp32 ; 223 }else if (_channel_size == 2 ) { 224if (_type == image_channel_data_type::signed_int ) 225return sycl::image_channel_type::signed_int16 ; 226else if (_type == image_channel_data_type::unsigned_int ) 227return sycl::image_channel_type::unsigned_int16 ; 228else if (_type == image_channel_data_type::fp ) 229return sycl::image_channel_type::fp16 ; 230 }else { 231if (_type == image_channel_data_type::signed_int ) 232return sycl::image_channel_type::signed_int8 ; 233else if (_type == image_channel_data_type::unsigned_int ) 234return sycl::image_channel_type::unsigned_int8 ; 235 } 236assert (false&& "unexpected channel data kind and channel size" ); 237return sycl::image_channel_type::signed_int32 ; 238 } 239void set_channel_type (sycl::image_channel_type type ) { 240switch (type ) { 241case sycl::image_channel_type::unsigned_int8 : 242_type = image_channel_data_type::unsigned_int ; 243_channel_size = 1 ; 244break ; 245case sycl::image_channel_type::unsigned_int16 : 246_type = image_channel_data_type::unsigned_int ; 247_channel_size = 2 ; 248break ; 249case sycl::image_channel_type::unsigned_int32 : 250_type = image_channel_data_type::unsigned_int ; 251_channel_size = 4 ; 252break ; 253case sycl::image_channel_type::signed_int8 : 254_type = image_channel_data_type::signed_int ; 255_channel_size = 1 ; 256break ; 257case sycl::image_channel_type::signed_int16 : 258_type = image_channel_data_type::signed_int ; 259_channel_size = 2 ; 260break ; 261case sycl::image_channel_type::signed_int32 : 262_type = image_channel_data_type::signed_int ; 263_channel_size = 4 ; 264break ; 265case sycl::image_channel_type::fp16 : 266_type = image_channel_data_type::fp ; 267_channel_size = 2 ; 268break ; 269case sycl::image_channel_type::fp32 : 270_type = image_channel_data_type::fp ; 271_channel_size = 4 ; 272break ; 273default : 274break ; 275 } 276_total_size = _channel_size * _channel_num ; 277 } 278 279 sycl::image_channel_order get_channel_order ()const { 280switch (_channel_num ) { 281case 1 : 282return sycl::image_channel_order::r ; 283case 2 : 284return sycl::image_channel_order::rg ; 285case 3 : 286return sycl::image_channel_order::rgb ; 287case 4 : 288return sycl::image_channel_order::rgba ; 289default : 290return sycl::image_channel_order::r ; 291 } 292 } 293/// Get the size for each channel in bits. 294unsigned 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. 299void set_channel_size (unsigned in_channel_num , 300unsigned channel_size ) { 301if (in_channel_num < _channel_num ) 302return ; 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 { 311image_channel _channel ; 312int _range [3 ]= {1 ,1 ,1 }; 313int _dims = 0 ; 314void * _host_data = nullptr ; 315 316/// Set range of each dimension. 317template < int dimensions > void set_range (sycl::range < dimensions > range ) { 318for (int i = 0 ;i < dimensions ;++ i ) 319_range [i ]= range [i ]; 320_dims = dimensions ; 321 } 322 323template < int ...DimIdx > 324 sycl::range < sizeof ...(DimIdx )> get_range (integer_sequence < DimIdx ...> ) { 325return sycl::range < sizeof ...(DimIdx )> (_range [DimIdx ]...); 326 } 327 328public : 329/// Constructor with channel info and dimension size info. 330template < int dimensions > 331image_matrix (image_channel channel , sycl::range < dimensions > range , 332image_type type = image_type::standard ) 333 :_channel (channel ) { 334if (type == image_type::array && range [1 ]== 0 ) { 335range [1 ]= range [2 ]; 336range [2 ]= 0 ; 337 } 338set_range (range ); 339_host_data = std::malloc (range .size ()* _channel .get_total_size ()); 340 } 341image_matrix (sycl::image_channel_type channel_type ,unsigned channel_num , 342size_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 ; 347if (y ) { 348_dims = 2 ; 349_range [1 ]= y ; 350 } 351_host_data = std::malloc (_range [0 ]* _range [1 ]* _channel .get_total_size ()); 352 } 353image_matrix (const image_matrix_desc * desc ) 354 :image_matrix (desc -> channel_type ,desc -> num_channels ,desc -> width , 355desc -> height ) {} 356 357/// Construct a new image class with the matrix data. 358template < int dimensions > sycl::image < dimensions > * create_image () { 359return create_image < dimensions > (_channel ); 360 } 361/// Construct a new image class with the matrix data. 362template < int dimensions > 363 sycl::image < dimensions > * create_image (image_channel channel ) { 364return new sycl::image < dimensions > ( 365_host_data ,channel .get_channel_order (),channel .get_channel_type (), 366get_range (make_index_sequence < dimensions > ()), 367 sycl::property::image::use_host_ptr ()); 368 } 369 370/// Get channel info. 371inline image_channel get_channel () {return _channel ; } 372/// Get range of the image. 373 sycl::range < 3 > get_range () { 374return sycl::range < 3 > (_range [0 ],_range [1 ],_range [2 ]); 375 } 376/// Get matrix dims. 377inline int get_dims () {return _dims ; } 378/// Convert to pitched data. 379pitched_data to_pitched_data () { 380return pitched_data (_host_data ,_range [0 ]* _channel .get_total_size (), 381_range [0 ],_range [1 ]); 382 } 383 384 ~image_matrix () { 385if (_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 : 403image_data () {_type = image_data_type::unsupport ; } 404image_data (image_matrix_p matrix_data ) {set_data (matrix_data ); } 405image_data (void * data_ptr ,size_t x_size ,image_channel channel ) { 406set_data (data_ptr ,x_size ,channel ); 407 } 408image_data (void * data_ptr ,size_t x_size ,size_t y_size ,size_t pitch_size , 409image_channel channel ) { 410set_data (data_ptr ,x_size ,y_size ,pitch_size ,channel ); 411 } 412void 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 418void set_data (experimental::image_mem_wrapper * image_mem ) { 419_type = image_data_type::matrix ; 420_data = image_mem ; 421 } 422#endif 423void 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 } 429void set_data (void * data_ptr ,size_t x_size ,size_t y_size ,size_t pitch_size , 430image_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 439image_data_type get_data_type ()const {return _type ; } 440void set_data_type (image_data_type type ) {_type = type ; } 441 442void * get_data_ptr ()const {return _data ; } 443void set_data_ptr (void * data ) {_data = data ; } 444 445size_t get_x ()const {return _x ; } 446void set_x (size_t x ) {_x = x ; } 447 448size_t get_y ()const {return _y ; } 449void set_y (size_t y ) {_y = y ; } 450 451size_t get_pitch ()const {return _pitch ; } 452void set_pitch (size_t pitch ) {_pitch = pitch ; } 453 454image_channel get_channel ()const {return _channel ; } 455void set_channel (image_channel channel ) {_channel = channel ; } 456 457image_channel_data_type get_channel_data_type () { 458return _channel .get_channel_data_type (); 459 } 460void set_channel_data_type (image_channel_data_type type ) { 461_channel .set_channel_data_type (type ); 462 } 463 464unsigned get_channel_size () {return _channel .get_channel_size (); } 465void set_channel_size (unsigned channel_num ,unsigned channel_size ) { 466return _channel .set_channel_size (channel_num ,channel_size ); 467 } 468 469unsigned get_channel_num () {return _channel .get_channel_num (); } 470void set_channel_num (unsigned num ) { 471return _channel .set_channel_num (num ); 472 } 473 474 sycl::image_channel_type get_channel_type () { 475return _channel .get_channel_type (); 476 } 477void set_channel_type (sycl::image_channel_type type ) { 478return _channel .set_channel_type (type ); 479 } 480 481private : 482image_data_type _type ; 483void * _data = nullptr ; 484size_t _x ,_y ,_pitch ; 485image_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. 500float _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. 503float _max_mipmap_level_clamp = 0.f ; 504// Dictates the anisotropic ratio used when sampling the mipmap with 505// anisotropic filtering. 506float _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. 511if ((unsigned )_addressing_mode == 0 ) 512return sycl::addressing_mode::clamp_to_edge ; 513return _addressing_mode ; 514 } 515void 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. 521return _filtering_mode == sycl::filtering_mode::linear 522 ? sycl::filtering_mode::linear 523 : sycl::filtering_mode::nearest ; 524 } 525void set (sycl::filtering_mode filtering_mode )noexcept { 526_filtering_mode = filtering_mode ; 527 } 528 529 sycl::coordinate_normalization_mode 530get_coordinate_normalization_mode ()const noexcept { 531return _coordinate_normalization_mode ; 532 } 533void set (sycl::coordinate_normalization_mode 534coordinate_normalization_mode )noexcept { 535_coordinate_normalization_mode = coordinate_normalization_mode ; 536 } 537 538bool is_coordinate_normalized ()const noexcept { 539return _coordinate_normalization_mode == 540 sycl::coordinate_normalization_mode::normalized ; 541 } 542void set_coordinate_normalization_mode (int is_normalized )noexcept { 543_coordinate_normalization_mode = 544is_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. 552return _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. 559void 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. 565float get_min_mipmap_level_clamp ()const noexcept { 566return _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. 571void 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. 577float get_max_mipmap_level_clamp ()const noexcept { 578return _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. 583void 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. 591float 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. 596void set_max_anisotropy (float max_anisotropy )noexcept { 597_max_anisotropy = max_anisotropy ; 598 } 599 600void set (sycl::addressing_mode addressing_mode , 601 sycl::filtering_mode filtering_mode , 602 sycl::coordinate_normalization_mode 603coordinate_normalization_mode )noexcept { 604set (addressing_mode ); 605set (filtering_mode ); 606set (coordinate_normalization_mode ); 607 } 608void set (sycl::addressing_mode addressing_mode , 609 sycl::filtering_mode filtering_mode ,int is_normalized )noexcept { 610set (addressing_mode ); 611set (filtering_mode ); 612set_coordinate_normalization_mode (is_normalized ); 613 } 614 615 sycl::sampler get_sampler ()const { 616return sycl::sampler (_coordinate_normalization_mode ,_addressing_mode , 617_filtering_mode ); 618 } 619}; 620 621/// Image base class. 622class image_wrapper_base { 623sampling_info _sampling_info ; 624image_data _data ; 625 626public : 627virtual ~image_wrapper_base ()= 0; 628 629void attach (image_data data ) {set_data (data ); } 630/// Attach matrix data to this class. 631void attach (image_matrix * matrix ) { 632detach (); 633 image_wrapper_base::set_data (image_data (matrix )); 634 } 635/// Attach matrix data to this class. 636void attach (image_matrix * matrix ,image_channel channel ) { 637attach (matrix ); 638 image_wrapper_base::set_channel (channel ); 639 } 640/// Attach linear data to this class. 641void attach (const void * ptr ,size_t count ) { 642attach (ptr ,count ,get_channel ()); 643 } 644/// Attach linear data to this class. 645void attach (const void * ptr ,size_t count ,image_channel channel ) { 646detach (); 647 image_wrapper_base::set_data (image_data (const_cast < void *> (ptr ),count ,channel )); 648 } 649/// Attach 2D data to this class. 650void attach (const void * data ,size_t x ,size_t y ,size_t pitch ) { 651attach (data ,x ,y ,pitch ,get_channel ()); 652 } 653/// Attach 2D data to this class. 654void attach (const void * data ,size_t x ,size_t y ,size_t pitch , 655image_channel channel ) { 656detach (); 657 image_wrapper_base::set_data ( 658image_data (const_cast < void *> (data ),x ,y ,pitch ,channel )); 659 } 660/// Attach device_ptr data to this class. 661void attach (const image_matrix_desc * desc ,device_ptr ptr ,size_t pitch ) { 662detach (); 663image_channel channel ; 664channel .set_channel_num (desc -> num_channels ); 665channel .set_channel_type (desc -> channel_type ); 666 image_wrapper_base::set_data ( 667image_data (ptr ,desc -> width ,desc -> height ,pitch ,channel )); 668 } 669/// Detach data. 670virtual void detach () {} 671 672sampling_info get_sampling_info () {return _sampling_info ; } 673void set_sampling_info (sampling_info info ) { 674_sampling_info = info ; 675 } 676const image_data & get_data () {return _data ; } 677void set_data (image_data data ) {_data = data ; } 678 679image_channel get_channel () {return _data .get_channel (); } 680void set_channel (image_channel channel ) {_data .set_channel (channel ); } 681 682image_channel_data_type get_channel_data_type () { 683return _data .get_channel_data_type (); 684 } 685void set_channel_data_type (image_channel_data_type type ) { 686_data .set_channel_data_type (type ); 687 } 688 689unsigned get_channel_size () {return _data .get_channel_size (); } 690void set_channel_size (unsigned channel_num ,unsigned channel_size ) { 691return _data .set_channel_size (channel_num ,channel_size ); 692 } 693 694 sycl::addressing_mode get_addressing_mode () { 695return _sampling_info .get_addressing_mode (); 696 } 697void set (sycl::addressing_mode addressing_mode ) { 698_sampling_info .set (addressing_mode ); 699 } 700 701 sycl::filtering_mode get_filtering_mode () { 702return _sampling_info .get_filtering_mode (); 703 } 704void set (sycl::filtering_mode filtering_mode ) { 705_sampling_info .set (filtering_mode ); 706 } 707 708 sycl::coordinate_normalization_mode get_coordinate_normalization_mode () { 709return _sampling_info .get_coordinate_normalization_mode (); 710 } 711void 712set (sycl::coordinate_normalization_mode coordinate_normalization_mode ) { 713_sampling_info .set (coordinate_normalization_mode ); 714 } 715 716bool is_coordinate_normalized () { 717return _sampling_info .is_coordinate_normalized (); 718 } 719void set_coordinate_normalization_mode (int is_normalized ) { 720_sampling_info .set_coordinate_normalization_mode (is_normalized ); 721 } 722void 723set (sycl::addressing_mode addressing_mode , 724 sycl::filtering_mode filtering_mode , 725 sycl::coordinate_normalization_mode coordinate_normalization_mode ) { 726set (addressing_mode ); 727set (filtering_mode ); 728set (coordinate_normalization_mode ); 729 } 730void set (sycl::addressing_mode addressing_mode , 731 sycl::filtering_mode filtering_mode ,int is_normalized ) { 732set (addressing_mode ); 733set (filtering_mode ); 734set_coordinate_normalization_mode (is_normalized ); 735 } 736 737unsigned get_channel_num () {return _data .get_channel_num (); } 738void set_channel_num (unsigned num ) { 739return _data .set_channel_num (num ); 740 } 741 742 sycl::image_channel_type get_channel_type () { 743return _data .get_channel_type (); 744 } 745void set_channel_type (sycl::image_channel_type type ) { 746return _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. 752if (_data .get_data_type ()== image_data_type::linear ) { 753smp = sycl::sampler (smp .get_coordinate_normalization_mode (), 754smp .get_addressing_mode (), 755 sycl::filtering_mode::nearest ); 756 } 757return 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 : 774void create_image (sycl::queue & q = get_default_queue ()) { 775if (_image ) 776return ; 777auto & data = get_data (); 778if (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 ()); 781return ; 782 } 783auto ptr = data .get_data_ptr (); 784auto channel = data .get_channel (); 785 786if (detail::get_pointer_attribute (q ,ptr )== detail::pointer_access_attribute::device_only ) { 787#ifdef DPCT_USM_LEVEL_NONE 788ptr = get_buffer (ptr ).get_host_access ().get_pointer (); 789#else 790auto sz = data .get_x (); 791if (data .get_data_type ()== image_data_type::pitch ) 792sz *=channel .get_total_size ()* data .get_y (); 793_host_buffer .resize (sz ); 794q .memcpy (_host_buffer .data (),ptr ,sz ); 795ptr = _host_buffer .data (); 796#endif 797 } 798 799if constexpr (dimensions == 1 ) { 800assert (data .get_data_type ()== image_data_type::linear ); 801_image = new sycl::image < 1 > ( 802ptr ,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 ) { 805assert (data .get_data_type ()== image_data_type::pitch ); 806_image = new sycl::image < 2 > (ptr ,channel .get_channel_order (), 807channel .get_channel_type (), 808 sycl::range < 2 > (data .get_x (),data .get_y ()), 809 sycl::range < 1 > (data .get_pitch ())); 810 }else { 811throw std::runtime_error ("3D image only support matrix data" ); 812 } 813return ; 814 } 815 816using acc_data_t = typename detail::image_trait < T > ::acc_data_t ; 817using accessor_t = 818typename image_accessor_ext < T ,IsImageArray ? (dimensions - 1 ) :dimensions , 819IsImageArray > ::accessor_t ; 820 821image_wrapper () {set_channel (image_channel::create < T > ()); } 822 ~image_wrapper ()override {detach (); } 823 824/// Get image accessor. 825accessor_t get_access (sycl::handler & cgh ) { 826assert (_image != nullptr && "Image not created" ); 827return accessor_t (* _image ,cgh ); 828 } 829 830/// Detach data. 831void detach ()override { 832if (_image ) 833delete _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 : 842using accessor_t = 843typename detail::image_trait < T > ::template accessor_t < dimensions > ; 844using data_t = typename detail::image_trait < T > ::data_t ; 845 sycl::sampler _sampler ; 846accessor_t _img_acc ; 847 848public : 849image_accessor_ext (sycl::sampler sampler ,accessor_t acc ) 850 :_sampler (sampler ),_img_acc (acc ) {} 851 852/// Read data from accessor. 853template < bool Available = dimensions == 3 > 854typename std::enable_if < Available ,data_t > ::type read (float x ,float y , 855float z ) { 856return detail::fetch_data < T > ()( 857_img_acc .read (sycl::float4 (x ,y ,z ,0 ),_sampler )); 858 } 859/// Read data from accessor. 860template < class Coord0 ,class Coord1 ,class Coord2 , 861bool Available = dimensions == 3 && 862 std::is_integral < Coord0 > ::value 863&& std::is_integral < Coord1 > ::value 864&& std::is_integral < Coord2 > ::value > 865typename std::enable_if < Available ,data_t > ::type read (Coord0 x ,Coord1 y , 866Coord2 z ) { 867return detail::fetch_data < T > ()( 868_img_acc .read (sycl::int4 (x ,y ,z ,0 ),_sampler )); 869 } 870/// Read data from accessor. 871template < bool Available = dimensions == 2 > 872typename std::enable_if < Available ,data_t > ::type read (float x ,float y ) { 873return detail::fetch_data < T > ()( 874_img_acc .read (sycl::float2 (x ,y ),_sampler )); 875 } 876/// Read data from accessor. 877template < class Coord0 ,class Coord1 , 878bool Available = dimensions == 2 && 879 std::is_integral < Coord0 > ::value 880&& std::is_integral < Coord1 > ::value > 881typename std::enable_if < Available ,data_t > ::type read (Coord0 x ,Coord1 y ) { 882return detail::fetch_data < T > ()( 883_img_acc .read (sycl::int2 (x ,y ),_sampler )); 884 } 885/// Read data from accessor. 886template < bool Available = dimensions == 1 > 887typename std::enable_if < Available ,data_t > ::type read (float x ) { 888return detail::fetch_data < T > ()(_img_acc .read (x ,_sampler )); 889 } 890/// Read data from accessor. 891template < class CoordT , 892bool Available = dimensions == 1 && std::is_integral < CoordT > ::value > 893typename std::enable_if < Available ,data_t > ::type read (CoordT x ) { 894return detail::fetch_data < T > ()(_img_acc .read (x ,_sampler )); 895 } 896 897/// Read data from accessor. 898template < bool Available = dimensions == 3 > 899typename std::enable_if < Available ,data_t > ::type read_byte (float x ,float y , 900float z ) { 901return detail::fetch_data < T > ()( 902_img_acc .read (sycl::float4 (x /sizeof (T ),y ,z ,0 ),_sampler )); 903 } 904/// Read data from accessor. 905template < class Coord0 ,class Coord1 ,class Coord2 , 906bool Available = dimensions == 3 && 907 std::is_integral < Coord0 > ::value 908&& std::is_integral < Coord1 > ::value 909&& std::is_integral < Coord2 > ::value > 910typename std::enable_if < Available ,data_t > ::type read_byte (Coord0 x ,Coord1 y , 911Coord2 z ) { 912return detail::fetch_data < T > ()( 913_img_acc .read (sycl::int4 (x /sizeof (T ),y ,z ,0 ),_sampler )); 914 } 915 916/// Read data from accessor. 917template < class Coord0 ,class Coord1 , 918bool Available = dimensions == 2 && 919 std::is_integral < Coord0 > ::value 920&& std::is_integral < Coord1 > ::value > 921typename std::enable_if < Available ,data_t > ::type read_byte (Coord0 x , 922Coord1 y ) { 923return detail::fetch_data < T > ()( 924_img_acc .read (sycl::int2 (x /sizeof (T ),y ),_sampler )); 925 } 926/// Read data from accessor. 927template < bool Available = dimensions == 2 > 928typename std::enable_if < Available ,data_t > ::type read_byte (float x ,float y ) { 929return detail::fetch_data < T > ()( 930_img_acc .read (sycl::float2 (x /sizeof (T ),y ),_sampler )); 931 } 932/// Read data from accessor. 933template < class CoordT , 934bool Available = dimensions == 1 && std::is_integral < CoordT > ::value > 935typename std::enable_if < Available ,data_t > ::type read_byte (CoordT x ) { 936return detail::fetch_data < T > ()(_img_acc .read (x /sizeof (T ),_sampler )); 937 } 938 939/// Read data from accessor. 940template < bool Available = dimensions == 1 > 941typename std::enable_if < Available ,data_t > ::type read_byte (float x ) { 942return 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 : 948using accessor_t = 949typename detail::image_trait < T > ::template array_accessor_t < dimensions > ; 950using data_t = typename detail::image_trait < T > ::data_t ; 951 sycl::sampler _sampler ; 952accessor_t _img_acc ; 953 954public : 955image_accessor_ext (sycl::sampler sampler ,accessor_t acc ) 956 :_sampler (sampler ),_img_acc (acc ) {} 957 958/// Read data from accessor. 959template < bool Available = dimensions == 2 > 960typename std::enable_if < Available ,data_t > ::type read (int index ,float x , 961float y ) { 962return detail::fetch_data < T > ()( 963_img_acc [index ].read (sycl::float2 (x ,y ),_sampler )); 964 } 965/// Read data from accessor. 966template < bool Available = dimensions == 2 > 967typename std::enable_if < Available ,data_t > ::type read (int index ,int x ,int y ) { 968return detail::fetch_data < T > ()( 969_img_acc [index ].read (sycl::int2 (x ,y ),_sampler )); 970 } 971/// Read data from accessor. 972template < bool Available = dimensions == 1 > 973typename std::enable_if < Available ,data_t > ::type read (int index ,float x ) { 974return detail::fetch_data < T > ()( 975_img_acc [index ].read (x ,_sampler )); 976 } 977/// Read data from accessor. 978template < bool Available = dimensions == 1 > 979typename std::enable_if < Available ,data_t > ::type read (int index ,int x ) { 980return 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 , 991sampling_info info = {}) { 992image_channel channel ; 993int dims = 1 ; 994if ( data . get_data_type () == image_data_type:: matrix ) { 995auto matrix = (image_matrix_p) data . get_data_ptr (); 996channel = matrix -> get_channel (); 997dims = matrix -> get_dims (); 998} else { 999if ( data . get_data_type () == image_data_type:: pitch ) { 1000dims = 2 ; 1001} 1002channel = data . get_channel (); 1003} 1004 1005if (auto ret = detail:: create_image_wrapper ( channel , dims )) { 1006ret -> set_sampling_info ( info ); 1007ret -> set_data ( data ); 1008return ret ; 1009} 1010return nullptr ; 1011} 1012 1013namespace detail { 1014static inline pitched_data to_pitched_data (image_matrix * image ) { 1015return 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 ) { 1020switch ( dims ) { 1021case 1 : 1022return new image_wrapper < T, 1 > (); 1023case 2 : 1024return new image_wrapper < T, 2 > (); 1025case 3 : 1026return new image_wrapper < T, 3 > (); 1027default : 1028return 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 ) { 1034switch ( channel_num ) { 1035case 1 : 1036return create_image_wrapper < T > ( dims ); 1037case 2 : 1038return create_image_wrapper < sycl::vec < T, 2 >> ( dims ); 1039case 3 : 1040return create_image_wrapper < sycl::vec < T, 3 >> ( dims ); 1041case 4 : 1042return create_image_wrapper < sycl::vec < T, 4 >> ( dims ); 1043default : 1044return 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 ) { 1050switch ( channel . get_channel_type ()) { 1051case sycl::image_channel_type:: fp16 : 1052return create_image_wrapper < sycl::half > ( channel . get_channel_num (), dims ); 1053case sycl::image_channel_type:: fp32 : 1054return create_image_wrapper < float > ( channel . get_channel_num (), dims ); 1055case sycl::image_channel_type:: signed_int8 : 1056return create_image_wrapper < std::int8_t > ( channel . get_channel_num (), dims ); 1057case sycl::image_channel_type:: signed_int16 : 1058return create_image_wrapper < std::int16_t > ( channel . get_channel_num (), dims ); 1059case sycl::image_channel_type:: signed_int32 : 1060return create_image_wrapper < std::int32_t > ( channel . get_channel_num (), dims ); 1061case sycl::image_channel_type:: unsigned_int8 : 1062return create_image_wrapper < std::uint8_t > ( channel . get_channel_num (), dims ); 1063case sycl::image_channel_type:: unsigned_int16 : 1064return create_image_wrapper < std::uint16_t > ( channel . get_channel_num (), dims ); 1065case sycl::image_channel_type:: unsigned_int32 : 1066return create_image_wrapper < std::uint32_t > ( channel . get_channel_num (), dims ); 1067default : 1068return nullptr ; 1069} 1070} 1071} // namespace detail 1072 1073} // namespace dpct 1074 1075#endif // !__DPCT_IMAGE_HPP__