okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1//===--- bindless_images.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_BINDLESS_IMAGE_HPP__ 10#define __DPCT_BINDLESS_IMAGE_HPP__ 11 12#ifdef _WIN32 13// DirectX headers 14#include <d3d11.h> 15#include <dxgi.h> 16#include <dxgi1_2.h> 17#endif // _WIN32 18 19namespace dpct { 20namespace experimental { 21 22#ifdef SYCL_EXT_ONEAPI_BINDLESS_IMAGES 23 24/// The wrapper structure for external memory resource handle desc 25class external_mem_handle_desc { 26#ifdef _WIN32 27void * handle ; 28const void * name ; 29#else 30int handle ; 31#endif // _WIN32 32 sycl::ext::oneapi::experimental::external_mem_handle_type handle_type ; 33size_t size_in_bytes ; 34int flags ; 35 36public : 37external_mem_handle_desc () { 38#ifdef _WIN32 39handle = nullptr ; 40name = nullptr ; 41#else 42handle = -1 ; 43#endif // _WIN32 44handle_type = 45 sycl::ext::oneapi::experimental::external_mem_handle_type::opaque_fd ; 46size_in_bytes = 0 ; 47flags = 0 ; 48 } 49 50#ifdef _WIN32 51/// handle setter 52/// \param [in] win32_handle The win32 NT handle of the external memory 53/// resource 54void set_win32_handle (void * win32_handle ) {handle = win32_handle ; } 55 56/// name setter 57/// \param [in] win32_obj_name The object of the external memory resource 58void set_win32_obj_name (const void * win32_obj_name ) {name = win32_obj_name ; } 59#else 60/// fd setter 61/// \param [in] fd The file descriptor of the external memory resource 62void set_fd_handle (int fd ) {handle = fd ; } 63#endif // _WIN32 64 65/// handle_type setter 66/// \param [in] type The type of the external memory resource handle 67void set_handle_type ( 68 sycl::ext::oneapi::experimental::external_mem_handle_type type ) { 69handle_type = type ; 70 } 71 72/// size setter 73/// \param [in] size The size (in bytes) of the external memory resource 74void set_res_size (unsigned long long size ) {size_in_bytes = size ; } 75 76/// flags setter 77/// \param [in] flags The flags used to handle importing of external memory 78/// resource 79void set_flags (int flags ) {this -> flags = flags ; } 80 81#ifdef _WIN32 82/// handle getter 83/// \param [out] handle The win32 NT handle of the external memory resource 84void * get_win32_handle () {return handle ; } 85 86/// name getter 87/// \param [out] name The object of the external memory resource 88const void * get_win32_obj_name () {return name ; } 89#else 90/// fd getter 91/// \param [out] fd The file descriptor of the external memory resource 92int get_fd_handle () {return handle ; } 93#endif // _WIN32 94 95/// handle_type getter 96/// \param [out] handle_type The type of the external memory resource handle 97 sycl::ext::oneapi::experimental::external_mem_handle_type get_handle_type () { 98return handle_type ; 99 } 100 101/// size getter 102/// \param [out] size The size (in bytes) of the external memory resource 103unsigned long long get_res_size () {return size_in_bytes ; } 104 105/// flags getter 106/// \param [out] flags The flags used to handle importing of external memory 107/// resource 108int get_flags () {return flags ; } 109}; 110 111/// The wrapper structure for external image memory desc 112class external_mem_img_desc { 113 sycl::range < 3 > size ; 114image_channel channel ; 115unsigned int num_levels ; 116 sycl::ext::oneapi::experimental::image_type type ; 117 118public : 119external_mem_img_desc () { 120size = sycl::range < 3 > {0 ,0 ,0 }; 121channel = image_channel::create < float > (); 122num_levels = 1 ; 123type = sycl::ext::oneapi::experimental::image_type::standard ; 124 } 125 126/// size setter 127/// \param [in] img_dims The dimensions of the imported image resource 128void set_size (sycl::range < 3 > img_dims ) { 129size = img_dims ; 130 131for (int i = 0 ;i < 3 ;i ++ ) { 132if (size [i ]== 1 ) { 133size [i ]= 0 ; 134 } 135 } 136 } 137 138/// image_channel setter 139/// \param [in] img_ch The channel info of the imported image resource 140void set_image_channel (image_channel img_ch ) {channel = img_ch ; } 141 142/// num_levels setter 143/// \param [in] numLevels The no. of levels in the imported image resource 144void set_num_levels (unsigned int num_levels ) { 145this -> num_levels = num_levels ; 146 147if (num_levels > 1 ) { 148set_image_type (sycl::ext::oneapi::experimental::image_type::mipmap ); 149 } 150 } 151 152/// type setter 153/// \param [in] img_type The type of the imported image resource 154void set_image_type (sycl::ext::oneapi::experimental::image_type img_type ) { 155type = img_type ; 156 } 157 158/// size getter 159/// \param [out] size The dimensions of the imported image resource 160 sycl::range < 3 > get_size () {return size ; } 161 162/// image_channel getter 163/// \param [out] image_channel The channel info of the imported image resource 164image_channel get_image_channel () {return channel ; } 165 166/// num_levels getter 167/// \param [out] num_levels The no. of levels in the imported image resource 168unsigned int get_num_levels () {return num_levels ; } 169 170/// type getter 171/// \param [out] type The image type of the imported image resource 172 sycl::ext::oneapi::experimental::image_type get_image_type () {return type ; } 173}; 174 175/// The wrapper structure for external buffer memory desc 176class external_mem_buf_desc { 177uint64_t size_in_bytes ; 178uint64_t offset ; 179unsigned int flags ; 180 181public : 182external_mem_buf_desc () { 183size_in_bytes = 0 ; 184offset = 0 ; 185flags = 0 ; 186 } 187 188/// size setter 189/// \param [in] size The size (in bytes) of the external buffer resource 190void set_res_size (uint64_t size ) {size_in_bytes = size ; } 191 192/// offset setter 193/// \param [in] offset The memory offset of the external buffer resource 194void set_mem_offset (uint64_t offset ) {this -> offset = offset ; } 195 196/// flags setter 197/// \param [in] flags The flags used to import external buffer resource 198void set_flags (unsigned int flags ) {this -> flags = flags ; } 199 200/// size getter 201/// \param [out] size_in_bytes The size (in bytes) of the external buffer res 202uint64_t get_res_size () {return size_in_bytes ; } 203 204/// offset getter 205/// \param [out] offset The memory offset of the external buffer resource 206uint64_t get_mem_offset () {return offset ; } 207 208/// flags getter 209/// \param [out] flags The flags used to import external buffer resource 210unsigned int get_flags () {return flags ; } 211}; 212 213/// The wrapper class for external semaphore resource handle desc 214class external_sem_handle_desc { 215#ifdef _WIN32 216void * handle ; 217const void * name ; 218#else 219int handle ; 220#endif // _WIN32 221 sycl::ext::oneapi::experimental::external_semaphore_handle_type handle_type ; 222int flags ; 223 224public : 225external_sem_handle_desc () { 226#ifdef _WIN32 227handle = nullptr ; 228name = nullptr ; 229#else 230handle = -1 ; 231#endif // _WIN32 232handle_type = sycl::ext::oneapi::experimental:: 233 external_semaphore_handle_type::opaque_fd ; 234flags = 0 ; 235 } 236 237#ifdef _WIN32 238/// handle setter 239/// \param [in] win32_handle The win32 NT handle of the external semaphore 240/// resource 241void set_win32_handle (void * win32_handle ) {handle = win32_handle ; } 242 243/// name setter 244/// \param [in] win32_obj_name The object of the external semaphore resource 245void set_win32_obj_name (const void * win32_obj_name ) {name = win32_obj_name ; } 246#else 247/// fd setter 248/// \param [in] fd The file descriptor of the external semaphore resource 249void set_fd_handle (int fd ) {handle = fd ; } 250#endif // _WIN32 251 252/// handle_type setter 253/// \param [in] type The type of the external semaphore resource handle 254void set_handle_type ( 255 sycl::ext::oneapi::experimental::external_semaphore_handle_type type ) { 256handle_type = type ; 257 } 258 259/// flags setter 260/// \param [in] flags The flags used to handle importing of external semaphore 261/// resource 262void set_flags (int flags ) {this -> flags = flags ; } 263 264#ifdef _WIN32 265/// handle getter 266/// \param [out] handle The win32 NT handle of the external semaphore resource 267void * get_win32_handle () {return handle ; } 268 269/// name getter 270/// \param [out] name The object of the external semaphore resource 271const void * get_win32_obj_name () {return name ; } 272#else 273/// fd getter 274/// \param [out] fd The file descriptor of the external semaphore resource 275int get_fd_handle () {return handle ; } 276#endif // _WIN32 277 278/// handle_type getter 279/// \param [out] handle_type The type of the external semaphore resource 280/// handle 281 sycl::ext::oneapi::experimental::external_semaphore_handle_type 282get_handle_type () { 283return handle_type ; 284 } 285 286/// flags getter 287/// \param [out] flags The flags used to handle importing of external 288/// semaphore 289int get_flags () {return flags ; } 290}; 291 292/// The wrapper structure for external semaphore signal/wait params 293class external_sem_params { 294uint64_t value ; 295 296public : 297external_sem_params () {value = 0 ; } 298 299/// value setter 300/// \param [in] val The state value of the semaphore 301void set_value (unsigned long long val ) {value = val ; } 302 303/// value getter 304/// \param [out] value The state value of the semaphore 305unsigned long long get_value () {return value ; } 306}; 307 308/// The wrapper class of external semaphore 309class external_sem_wrapper { 310 sycl::ext::oneapi::experimental::external_semaphore ext_sem ; 311 sycl::ext::oneapi::experimental::external_semaphore_handle_type 312ext_sem_handle_type ; 313 314public : 315/// Create external semaphore wrapper. 316/// \param [in] ext_sem The external semaphore resource object 317/// \param [in] ext_sem_handle_type The external semaphore resource handle 318/// type 319external_sem_wrapper ( 320 sycl::ext::oneapi::experimental::external_semaphore ext_sem , 321 sycl::ext::oneapi::experimental::external_semaphore_handle_type 322ext_sem_handle_type ) { 323this -> ext_sem = ext_sem ; 324this -> ext_sem_handle_type = ext_sem_handle_type ; 325 } 326 327/// destory external semaphore resource 328 ~external_sem_wrapper () { 329 sycl::ext::oneapi::experimental::release_external_semaphore ( 330ext_sem ,get_default_queue ()); 331 } 332 333/// ext_sem getter 334/// \param [out] ext_sem The external semaphore resource object 335 sycl::ext::oneapi::experimental::external_semaphore get () {return ext_sem ; } 336 337/// ext_sem_handle_type getter 338/// \param [out] ext_sem_handle_type The external semaphore resource handle 339/// type 340 sycl::ext::oneapi::experimental::external_semaphore_handle_type 341get_handle_type () { 342return ext_sem_handle_type ; 343 } 344}; 345 346/// The wrapper class of bindless image memory handle. 347class image_mem_wrapper { 348public : 349/// Create bindless image memory wrapper. 350/// \tparam dimensions The dimensions of image memory. 351/// \param [in] channel The image channel used to create bindless image 352/// \param [in] range The sizes of each dimension of bindless image memory. 353/// memory. 354template < int dimensions = 3 > 355image_mem_wrapper (image_channel channel , sycl::range < dimensions > range , 356 sycl::ext::oneapi::experimental::image_type type = 357 sycl::ext::oneapi::experimental::image_type::standard , 358unsigned int num_levels = 1 ) 359 :_channel (channel ) { 360unsigned array_size = 1 ; 361if (type == sycl::ext::oneapi::experimental::image_type::array ) { 362assert (dimensions == 3 ); 363array_size = range [2 ]; 364range [2 ]= 0 ; 365 } 366_desc = sycl::ext::oneapi::experimental::image_descriptor ( 367range ,_channel .get_channel_num (),_channel .get_channel_type (),type , 368num_levels ,array_size ); 369auto q = get_default_queue (); 370_handle = alloc_image_mem (_desc ,q ); 371init_mip_level_wrappers (q ); 372 } 373/// Create bindless image memory wrapper. 374/// \param [in] channel The image channel used to create bindless image 375/// \param [in] width The width of bindless image memory. 376/// \param [in] height The height of bindless image memory. 377/// \param [in] depth The depth of bindless image memory. 378image_mem_wrapper (image_channel channel ,size_t width ,size_t height = 0 , 379size_t depth = 0 ) 380 :image_mem_wrapper (channel , {width ,height ,depth }) {} 381/// Create bindless image memory wrapper. 382/// \param [in] desc The image descriptor used to create bindless image 383image_mem_wrapper ( 384const sycl::ext::oneapi::experimental::image_descriptor * desc ) 385 :_desc (* desc ) { 386_channel .set_channel_type (desc -> channel_type ); 387_channel .set_channel_num (desc -> num_channels ); 388auto q = get_default_queue (); 389_handle = alloc_image_mem (_desc ,q ); 390 } 391/// Create bindless image memory wrapper. 392/// \param [in] desc The image descriptor of bindless image. 393/// \param [in] img_mem_handle The memory handle of bindless image. 394image_mem_wrapper ( 395 sycl::ext::oneapi::experimental::image_descriptor desc , 396 sycl::ext::oneapi::experimental::image_mem_handle img_mem_handle ) 397 :_desc (desc ),_handle (img_mem_handle ) { 398_channel .set_channel_type (_desc .channel_type ); 399#if (__SYCL_COMPILER_VERSION && __SYCL_COMPILER_VERSION >=20240725 ) 400_channel .set_channel_num (_desc .num_channels ); 401#endif 402auto q = get_default_queue (); 403init_mip_level_wrappers (q ); 404 } 405/// Create bindless image memory wrapper. 406/// \param [in] extMem SYCL external memory resource object. 407/// \param [in] ext_img_mem_desc The img desc of the imported image resource. 408image_mem_wrapper (sycl::ext::oneapi::experimental::external_mem extMem , 409external_mem_img_desc * ext_img_mem_desc ) { 410auto q = get_default_queue (); 411unsigned int array_size = 1 ; 412 413_channel = ext_img_mem_desc -> get_image_channel (); 414_desc = sycl::ext::oneapi::experimental::image_descriptor ( 415ext_img_mem_desc -> get_size (),_channel .get_channel_num (), 416_channel .get_channel_type (),ext_img_mem_desc -> get_image_type (), 417ext_img_mem_desc -> get_num_levels (),array_size ); 418_handle = sycl::ext::oneapi::experimental::map_external_image_memory ( 419extMem ,_desc ,q ); 420 421init_mip_level_wrappers (q ); 422 } 423/// Create bindless image memory wrapper. 424/// \param [in] desc The image descriptor of bindless image. 425/// \param [in] num_levels Number of mipmap levels to allocate 426image_mem_wrapper (sycl::ext::oneapi::experimental::image_descriptor desc , 427unsigned int num_levels ) 428 :_desc (desc ) { 429_desc .type = sycl::ext::oneapi::experimental::image_type::mipmap ; 430_desc .num_levels = num_levels ; 431auto q = get_default_queue (); 432_handle = alloc_image_mem (_desc ,q ); 433init_mip_level_wrappers (q ); 434 } 435 436image_mem_wrapper (const image_mem_wrapper & )= delete ; 437image_mem_wrapper & operator= (const image_mem_wrapper & )= delete ; 438/// Destroy bindless image memory wrapper. 439 ~image_mem_wrapper () { 440if (_sub_wrappers ) { 441 std::destroy_n (_sub_wrappers ,_desc .num_levels ); 442 std::free (_sub_wrappers ); 443 } 444free_image_mem (_handle ,_desc .type ,get_default_queue ()); 445 } 446/// Get the image channel of the bindless image memory. 447/// \returns The image channel of bindless image memory. 448image_channel get_channel ()const noexcept {return _channel ; } 449/// Get the sizes of each dimension of the bindless image memory. 450/// \returns The sizes of each dimension of bindless image memory. 451 sycl::range < 3 > get_range ()const { 452return {_desc .width ,_desc .height ,_desc .depth }; 453 } 454/// Get the image descriptor of the bindless image memory. 455/// \returns The image descriptor of bindless image memory. 456const sycl::ext::oneapi::experimental::image_descriptor & 457get_desc ()const noexcept { 458return _desc ; 459 } 460/// Get the image handle of the bindless image memory. 461/// \returns The image handle of bindless image memory. 462 sycl::ext::oneapi::experimental::image_mem_handle 463get_handle ()const noexcept { 464return _handle ; 465 } 466/// Get the image mip level of the bindless image memory. 467/// \returns The image mip level of the bindless image memory. 468image_mem_wrapper * get_mip_level (unsigned int level ) { 469if (_desc .type == sycl::ext::oneapi::experimental::image_type::standard ) { 470assert (level == 0 ); 471return this ; 472 } 473 474assert (_desc .type == sycl::ext::oneapi::experimental::image_type::mipmap ); 475return _sub_wrappers + level ; 476 } 477 478 sycl::ext::oneapi::experimental::image_type get_image_type (void ) { 479return _desc .type ; 480 } 481 482private : 483image_mem_wrapper ( 484const image_channel & channel , 485const sycl::ext::oneapi::experimental::image_descriptor & desc , 486const sycl::ext::oneapi::experimental::image_mem_handle & handle ) 487 :_channel (channel ),_desc (desc ),_handle (handle ) {} 488 489/// Initialize image mem wrappers for all the levels of mipmap image 490/// \param [in] q The Queue to be used to query the mip levels 491void init_mip_level_wrappers (sycl::queue q ) { 492auto num_levels = _desc .num_levels ; 493if (_desc .type == sycl::ext::oneapi::experimental::image_type::mipmap ) { 494assert (num_levels > 1 ); 495_sub_wrappers = (image_mem_wrapper * )std::malloc ( 496sizeof (image_mem_wrapper )* num_levels ); 497for (unsigned i = 0 ;i < num_levels ;++ i ) 498new (_sub_wrappers + i )image_mem_wrapper ( 499_channel ,_desc .get_mip_level_desc (i ), 500 sycl::ext::oneapi::experimental::get_mip_level_mem_handle ( 501_handle ,i ,q .get_device (),q .get_context ())); 502 } 503 } 504 505image_channel _channel ; 506 sycl::ext::oneapi::experimental::image_descriptor _desc ; 507 sycl::ext::oneapi::experimental::image_mem_handle _handle ; 508image_mem_wrapper * _sub_wrappers {nullptr }; 509}; 510 511/// The base wrapper class of external memory resource handle. 512class external_mem_wrapper_base { 513public : 514/// Cleans up the external mem wrapper by releasing the SYCL external 515/// memory handle 516virtual ~external_mem_wrapper_base () {} 517 518external_mem_wrapper_base (const external_mem_wrapper_base & )= delete ; 519 520external_mem_wrapper_base & 521 operator= (const external_mem_wrapper_base & )= delete ; 522 523const sycl::ext::oneapi::experimental::external_mem 524get_external_mem ()const noexcept { 525return _res_external_mem ; 526 } 527 528/// Map the buf/texture resource memory and get its pointer/mem handle. 529/// \param [in] q The queue used to map the resource with. 530void map_resource (sycl::queue q ) { 531if (_res_is_buffer ) { 532if (_res_buf_ptr ) { 533throw std::runtime_error ("Resource is already mapped! " 534"Resource cannot be mapped more than once." ); 535 } 536 537#if (__SYCL_COMPILER_VERSION && __SYCL_COMPILER_VERSION >=20240823 ) 538_res_buf_ptr = 539 sycl::ext::oneapi::experimental::map_external_linear_memory ( 540get_external_mem (),0 ,_res_size_bytes ,q ); 541#else 542throw std::runtime_error ( 543"Mapping a buffer resoure is not yet supported!" ); 544#endif 545 }else { 546if (_res_img_mem_wrapper_ptr ) { 547throw std::runtime_error ("Resource is already mapped! " 548"Resource cannot be mapped more than once." ); 549 } 550 551auto img_mem_handle = 552 sycl::ext::oneapi::experimental::map_external_image_memory ( 553get_external_mem (),_res_img_desc ,q ); 554 555_res_img_mem_wrapper_ptr = 556new image_mem_wrapper (_res_img_desc ,img_mem_handle ); 557 } 558 } 559 560/// Gets the pointer and size of the mapped resource 561/// \param [out] ptr The pointer to the resource memory. 562/// \param [out] size The size of the resource memory. 563/// and resource size. 564void get_mapped_pointer (void ** ptr ,size_t * size ) { 565if (!_res_is_buffer ) { 566throw std::runtime_error ( 567"Non buffer resouce cannot be accessed as a pointer!" ); 568 } 569 570* ptr = _res_buf_ptr ; 571if (!* ptr ) { 572throw std::runtime_error ( 573"Resource is not mapped! " 574"Resource should be mapped before accessing its memory" ); 575 } 576 577* size = _res_size_bytes ; 578 } 579 580/// Returns mapped memory as image_mem_wrapper* 581image_mem_wrapper * get_mapped_mipmapped_array () { 582if (_res_is_buffer ) { 583throw std::runtime_error ( 584"Buffer resource cannot be accessed as an array!" ); 585 } 586if (!_res_img_mem_wrapper_ptr ) { 587throw std::runtime_error ( 588"Resource is not mapped! " 589"Resource should be mapped before accessing its memory." ); 590 } 591 592return _res_img_mem_wrapper_ptr ; 593 } 594 595/// Returns mapped subresource memory as image_mem_wrapper* 596/// \param [in] array_index The layer of the image array to be returned. 597/// \param [in] mip_level The level of the mipmap image to be returned. 598image_mem_wrapper * get_sub_resource_mapped_array (unsigned int array_index , 599unsigned int mip_level ) { 600if (_res_is_buffer ) { 601throw std::runtime_error ( 602"Buffer resouce cannot be accessed as an array!" ); 603 } 604if (array_index != 0 ) { 605throw std::runtime_error ("Retrieving a particular layer from image array " 606"is not yet supported!" ); 607 } 608if (array_index >=_res_img_desc .array_size ) { 609throw std::runtime_error ( 610"Array index requested exceeds total array size in resource!" ); 611 } 612if (mip_level >=_res_img_desc .num_levels ) { 613throw std::runtime_error ( 614"Mip level requested exceeds total num of levels in resource!" ); 615 } 616if (!_res_img_mem_wrapper_ptr ) { 617throw std::runtime_error ( 618"Resouce is not mapped! " 619"Resource should be mapped before accessing its memory" ); 620 } 621 622return (mip_level == 0 ) 623 ?_res_img_mem_wrapper_ptr 624 :_res_img_mem_wrapper_ptr -> get_mip_level (mip_level ); 625 } 626 627/// Unmap the buf/texture resource memory and get its pointer/mem handle. 628/// \param [in] q The queue used to unmap the resource with. 629void unmap_resource (sycl::queue q ) { 630if (_res_is_buffer ) { 631if (!_res_buf_ptr ) { 632throw std::runtime_error ("Resource is not mapped! " 633"Resource should be mapped before unmapping." ); 634 } 635 636_res_buf_ptr = nullptr ; 637 638throw std::runtime_error ( 639"Unmapping a buffer resoure is not yet supported!" ); 640 }else { 641if (!_res_img_mem_wrapper_ptr ) { 642throw std::runtime_error ("Resource is not mapped! " 643"Resource should be mapped before unmapping." ); 644 } 645 646delete _res_img_mem_wrapper_ptr ; 647_res_img_mem_wrapper_ptr = nullptr ; 648 } 649 } 650 651protected : 652/// Init external mem handle for a given resource. 653external_mem_wrapper_base ( 654 sycl::ext::oneapi::experimental::external_mem external_mem , 655unsigned reg_flags ) 656 :_res_external_mem (external_mem ),_res_reg_flags (reg_flags ) {} 657 658unsigned _res_reg_flags = 0 ; 659bool _res_is_buffer = false; 660size_t _res_size_bytes = 0 ; 661 662 sycl::ext::oneapi::experimental::image_descriptor _res_img_desc ; 663 sycl::ext::oneapi::experimental::external_mem _res_external_mem ; 664 665private : 666void * _res_buf_ptr = nullptr ; 667image_mem_wrapper * _res_img_mem_wrapper_ptr = nullptr ; 668}; 669 670#ifdef _WIN32 671class external_mem_wrapper :public external_mem_wrapper_base { 672public : 673/// Creates external memory resource wrapper for a given DX11 resource. 674/// \param [in] d3d11_res Pointer to the ID3D11Resource to be wrapped. 675/// \param [in] reg_flags Registration flags used for resource registration. 676external_mem_wrapper (ID3D11Resource * d3d11_res ,unsigned reg_flags ) 677 :_res_D3D11 (d3d11_res ), 678external_mem_wrapper_base ( 679 sycl::ext::oneapi::experimental::external_mem (),reg_flags ) { 680// Check if the resource is already registered to prevent double 681// registration. 682if (_res_D3D11_set .find (_res_D3D11 )!= _res_D3D11_set .end ()) { 683throw std::runtime_error ("This D3D11 resource is already registered!" ); 684 } 685 686// Query the information from D3D11 resource object 687query_res_info (_res_D3D11 ); 688 689// Create a shared handle for the DX11 resource to enable interoperability. 690_res_win_nt_handle = create_shared_handle (_res_D3D11 ); 691 692// Prepare the external memory resource descriptor for SYCL interop. 693 sycl::ext::oneapi::experimental::resource_win32_handle ext_mem_win_handle { 694_res_win_nt_handle }; 695 696 sycl::ext::oneapi::experimental::external_mem_descriptor < 697 sycl::ext::oneapi::experimental::resource_win32_handle > 698ext_mem_desc {ext_mem_win_handle , 699 sycl::ext::oneapi::experimental::external_mem_handle_type:: 700win32_nt_dx12_resource , 701_res_size_bytes }; 702 703// Import resource's external memory into SYCL for use with SYCL APIs. 704_res_external_mem = sycl::ext::oneapi::experimental::external_mem ( 705 sycl::ext::oneapi::experimental::import_external_memory ( 706ext_mem_desc ,get_default_queue ())); 707 708// Register the resource to track its uniqueness. 709_res_D3D11_set .insert (_res_D3D11 ); 710 } 711 712/// Cleans up the external mem wrapper by releasing the SYCL external 713/// memory handle and the shared handle created for the DX11 resource. 714virtual ~external_mem_wrapper () { 715// Release the mutex when done 716_res_keyed_mutex -> ReleaseSync (0 ); 717 718// Release the keyed mutex interface 719_res_keyed_mutex -> Release (); 720 721// Close the shared handle to the DX11 resource. 722CloseHandle (_res_win_nt_handle ); 723 724// Release the resource's external memory associated with the SYCL external 725// mem handle. 726 sycl::ext::oneapi::experimental::release_external_memory ( 727get_external_mem (),get_default_queue ()); 728 729// Remove the resource from the tracking set. 730if (_res_D3D11_set .find (_res_D3D11 )!= _res_D3D11_set .end ()) { 731_res_D3D11_set .erase (_res_D3D11 ); 732 } 733 } 734 735private : 736inline static std::set < ID3D11Resource *> _res_D3D11_set ; 737 738ID3D11Resource * _res_D3D11 = nullptr ; 739IDXGIKeyedMutex * _res_keyed_mutex = nullptr ; 740HANDLE _res_win_nt_handle = nullptr ; 741 742/// Helper function to query the properties of DX11 resource 743void query_res_info (ID3D11Resource * resource ) { 744unsigned int res_width = 0 ; 745unsigned int res_height = 0 ; 746unsigned int res_depth = 0 ; 747unsigned int res_num_levels = 1 ; 748unsigned int res_arr_size = 1 ; 749 750image_channel channel ; 751 752// Get the dimension info of DX11 resource 753D3D11_RESOURCE_DIMENSION dimension ; 754resource -> GetType (& dimension ); 755 756switch (dimension ) { 757case D3D11_RESOURCE_DIMENSION_BUFFER : { 758_res_is_buffer = true; 759 760D3D11_BUFFER_DESC desc ; 761 ((ID3D11Buffer * )resource )-> GetDesc (& desc ); 762 763_res_size_bytes = desc .ByteWidth ; 764break ; 765 } 766case D3D11_RESOURCE_DIMENSION_TEXTURE1D : { 767D3D11_TEXTURE1D_DESC desc ; 768 ((ID3D11Texture1D * )resource )-> GetDesc (& desc ); 769 770res_width = desc .Width ; 771res_arr_size = desc .ArraySize ; 772res_num_levels = (desc .MipLevels ?desc .MipLevels :1 ); 773 774channel = get_img_ch_info (desc .Format ); 775_res_size_bytes = res_width ; 776break ; 777 } 778case D3D11_RESOURCE_DIMENSION_TEXTURE2D : { 779D3D11_TEXTURE2D_DESC desc ; 780 ((ID3D11Texture2D * )resource )-> GetDesc (& desc ); 781 782res_width = desc .Width ; 783res_height = desc .Height ; 784res_arr_size = desc .ArraySize ; 785res_num_levels = (desc .MipLevels ?desc .MipLevels :1 ); 786 787channel = get_img_ch_info (desc .Format ); 788_res_size_bytes = res_width * res_height ; 789break ; 790 } 791case D3D11_RESOURCE_DIMENSION_TEXTURE3D : { 792D3D11_TEXTURE3D_DESC desc ; 793 ((ID3D11Texture3D * )resource )-> GetDesc (& desc ); 794 795res_width = desc .Width ; 796res_height = desc .Height ; 797res_depth = desc .Depth ; 798res_num_levels = (desc .MipLevels ?desc .MipLevels :1 ); 799 800channel = get_img_ch_info (desc .Format ); 801_res_size_bytes = res_width * res_height * res_depth ; 802break ; 803 } 804default : 805throw std::runtime_error ("Unsupported DX11 resource type!" ); 806break ; 807 } 808 809if (!_res_is_buffer ) { 810if (res_arr_size > 1 ) { 811throw std::runtime_error ( 812"Images with multiple array levels are not supported. " 813"Only standard and mipmap images are supported!" ); 814 } 815 816// Calculate the size of a resource in bytes by multiplyting its dims 817// with arr size, num levels & channel size 818_res_size_bytes *= 819res_arr_size * res_num_levels * channel .get_total_size (); 820 821 sycl::ext::oneapi::experimental::image_type img_type = 822 (res_num_levels > 1 ) 823 ? sycl::ext::oneapi::experimental::image_type::mipmap 824 : sycl::ext::oneapi::experimental::image_type::standard ; 825 826_res_img_desc = sycl::ext::oneapi::experimental::image_descriptor { 827 sycl::range < 3 > {res_width ,res_height ,res_depth }, 828channel .get_channel_num (), 829channel .get_channel_type (), 830img_type , 831res_num_levels , 832res_arr_size }; 833 } 834 } 835 836/// Helper function to create shared handle for DXD11 resource 837HANDLE create_shared_handle (ID3D11Resource * resource ) { 838HANDLE win_nt_handle = nullptr ; 839 840IDXGIResource1 * pDXGIResource1 ; 841resource -> QueryInterface (__uuidof (IDXGIResource1 ), 842reinterpret_cast < void **> (& pDXGIResource1 )); 843 844if (_res_is_buffer ) { 845pDXGIResource1 -> GetSharedHandle (& win_nt_handle ); 846 }else { 847pDXGIResource1 -> CreateSharedHandle ( 848nullptr ,DXGI_SHARED_RESOURCE_READ |DXGI_SHARED_RESOURCE_WRITE , 849nullptr ,& win_nt_handle ); 850 } 851 852pDXGIResource1 -> Release (); 853 854// Query for the keyed mutex interface 855resource -> QueryInterface (__uuidof (IDXGIKeyedMutex ), 856reinterpret_cast < void **> (& _res_keyed_mutex )); 857 858// Use the keyed mutex to synchronize access to the shared resource 859_res_keyed_mutex -> AcquireSync (0 ,INFINITE ); 860 861return win_nt_handle ; 862 } 863 864/// Helper function to get the texture channel info of DX11 resource using 865/// DXGI format 866image_channel get_img_ch_info (DXGI_FORMAT format ) { 867image_channel channel ; 868 869switch (format ) { 870case DXGI_FORMAT_R16G16B16A16_FLOAT : 871channel = image_channel::create < sycl::half4 > (); 872break ; 873case DXGI_FORMAT_R8G8B8A8_UNORM : 874channel = image_channel::create < sycl::uchar4 > (); 875break ; 876case DXGI_FORMAT_R16G16_FLOAT : 877channel = image_channel::create < sycl::half2 > (); 878break ; 879case DXGI_FORMAT_R32_FLOAT : 880channel = image_channel::create < float > (); 881break ; 882default : 883throw std::runtime_error ("Unsupported DX11 resource format!" ); 884break ; 885 } 886 887return channel ; 888 } 889}; 890#endif // _WIN32 891 892namespace detail { 893struct sampled_image_handle_compare { 894template < class T > bool operator()(T L ,T R )const { 895return L .raw_handle < R .raw_handle ; 896 } 897}; 898 899inline std::pair < image_data ,sampling_info > & get_img_info_map ( 900const sycl::ext::oneapi::experimental::sampled_image_handle handle ) { 901static std::map < sycl::ext::oneapi::experimental::sampled_image_handle , 902 std::pair < image_data ,sampling_info > , 903sampled_image_handle_compare > 904img_info_map ; 905return img_info_map [handle ]; 906} 907 908inline std::pair < image_data ,sampling_info > & get_img_info_map ( 909const sycl::ext::oneapi::experimental::unsampled_image_handle handle ) { 910static std::map < sycl::ext::oneapi::experimental::unsampled_image_handle , 911 std::pair < image_data ,sampling_info > , 912sampled_image_handle_compare > 913img_info_map ; 914return img_info_map [handle ]; 915} 916 917static inline size_t 918get_ele_size (const sycl::ext::oneapi::experimental::image_descriptor & decs ) { 919size_t channel_size ; 920switch (decs .channel_type ) { 921case sycl::image_channel_type::signed_int8 : 922case sycl::image_channel_type::unsigned_int8 : 923channel_size = 1 ; 924break ; 925case sycl::image_channel_type::fp16 : 926case sycl::image_channel_type::signed_int16 : 927case sycl::image_channel_type::unsigned_int16 : 928channel_size = 2 ; 929break ; 930case sycl::image_channel_type::fp32 : 931case sycl::image_channel_type::signed_int32 : 932case sycl::image_channel_type::unsigned_int32 : 933channel_size = 4 ; 934break ; 935default : 936throw std::runtime_error ("Unsupported channel_type in get_ele_size!" ); 937break ; 938 } 939return decs .num_channels * channel_size ; 940} 941 942static inline sycl::event 943dpct_memcpy (sycl::ext::oneapi::experimental::image_mem_handle src , 944const sycl::ext::oneapi::experimental::image_descriptor & desc_src , 945size_t w_offset_src ,size_t h_offset_src ,void * dest ,size_t p , 946size_t w ,size_t h , sycl::queue q ) { 947const auto ele_size = get_ele_size (desc_src ); 948const auto src_offset = 949 sycl::range < 3 > (w_offset_src /ele_size ,h_offset_src ,0 ); 950const auto dest_offset = sycl::range < 3 > (0 ,0 ,0 ); 951const auto dest_extend = sycl::range < 3 > (p /ele_size ,0 ,0 ); 952const auto copy_extend = sycl::range < 3 > (w /ele_size ,h ,0 ); 953return q .ext_oneapi_copy (src ,src_offset ,desc_src ,dest ,dest_offset , 954dest_extend ,copy_extend ); 955} 956 957static inline std::vector < sycl::event > dpct_memcpy_to_host ( 958 sycl::ext::oneapi::experimental::image_mem_handle src , 959const sycl::ext::oneapi::experimental::image_descriptor & desc_src , 960size_t w_offset_src ,size_t h_offset_src ,void * dest_host_ptr ,size_t s , 961 sycl::queue q ) { 962 std::vector < sycl::event > event_list ; 963const auto ele_size = get_ele_size (desc_src ); 964const auto w = desc_src .width * ele_size ; 965size_t offset_dest = 0 ; 966while (s - offset_dest > w - w_offset_src ) { 967const auto src_offset = 968 sycl::range < 3 > (w_offset_src /ele_size ,h_offset_src ,0 ); 969const auto dest_offset = sycl::range < 3 > (offset_dest /ele_size ,0 ,0 ); 970const auto dest_extend = sycl::range < 3 > (0 ,0 ,0 ); 971const auto copy_extend = 972 sycl::range < 3 > ((w - w_offset_src ) /ele_size ,1 ,0 ); 973event_list .push_back (q .ext_oneapi_copy (src ,src_offset ,desc_src , 974dest_host_ptr ,dest_offset , 975dest_extend ,copy_extend )); 976offset_dest += w - w_offset_src ; 977w_offset_src = 0 ; 978++ h_offset_src ; 979 } 980if (s - offset_dest > 0 ) { 981const auto src_offset = 982 sycl::range < 3 > (w_offset_src /ele_size ,h_offset_src ,0 ); 983const auto dest_offset = sycl::range < 3 > (offset_dest /ele_size ,0 ,0 ); 984const auto dest_extend = sycl::range < 3 > (0 ,0 ,0 ); 985const auto copy_extend = sycl::range < 3 > ((s - offset_dest ) /ele_size ,1 ,0 ); 986event_list .push_back (q .ext_oneapi_copy (src ,src_offset ,desc_src , 987dest_host_ptr ,dest_offset , 988dest_extend ,copy_extend )); 989 } 990return event_list ; 991} 992 993static inline std::vector < sycl::event > 994dpct_memcpy (sycl::ext::oneapi::experimental::image_mem_handle src , 995const sycl::ext::oneapi::experimental::image_descriptor & desc_src , 996size_t w_offset_src ,size_t h_offset_src ,void * dest ,size_t s , 997 sycl::queue q ) { 998if (dpct::detail::get_pointer_attribute (q ,dest )== 999 dpct::detail::pointer_access_attribute::device_only ) { 1000 std::vector < sycl::event > event_list ; 1001 dpct::detail::host_buffer buf (s ,q ,event_list ); 1002auto copy_events = dpct_memcpy_to_host (src ,desc_src ,w_offset_src , 1003h_offset_src ,buf .get_ptr (),s ,q ); 1004event_list .push_back (dpct::detail::dpct_memcpy ( 1005q ,dest ,buf .get_ptr (),s , memcpy_direction::host_to_device , 1006copy_events )); 1007return event_list ; 1008 } 1009return dpct_memcpy_to_host (src ,desc_src ,w_offset_src ,h_offset_src ,dest ,s , 1010q ); 1011} 1012 1013static inline sycl::event 1014dpct_memcpy (const void * src , 1015 sycl::ext::oneapi::experimental::image_mem_handle dest , 1016const sycl::ext::oneapi::experimental::image_descriptor & desc_dest , 1017size_t w_offset_dest ,size_t h_offset_dest ,size_t p ,size_t w , 1018size_t h , sycl::queue q ) { 1019const auto ele_size = get_ele_size (desc_dest ); 1020const auto src_offset = sycl::range < 3 > (0 ,0 ,0 ); 1021const auto src_extend = sycl::range < 3 > (p /ele_size ,0 ,0 ); 1022const auto dest_offset = 1023 sycl::range < 3 > (w_offset_dest /ele_size ,h_offset_dest ,0 ); 1024const auto copy_extend = sycl::range < 3 > (w /ele_size ,h ,0 ); 1025// TODO: Remove const_cast after refining the signature of ext_oneapi_copy. 1026return q .ext_oneapi_copy (const_cast < void *> (src ),src_offset ,src_extend , 1027dest ,dest_offset ,desc_dest ,copy_extend ); 1028} 1029 1030static inline std::vector < sycl::event > dpct_memcpy_from_host ( 1031const void * src_host_ptr , 1032 sycl::ext::oneapi::experimental::image_mem_handle dest , 1033const sycl::ext::oneapi::experimental::image_descriptor & desc_dest , 1034size_t w_offset_dest ,size_t h_offset_dest ,size_t s , sycl::queue q ) { 1035 std::vector < sycl::event > event_list ; 1036const auto ele_size = get_ele_size (desc_dest ); 1037const auto w = desc_dest .width * ele_size ; 1038size_t offset_src = 0 ; 1039while (s - offset_src >=w - w_offset_dest ) { 1040const auto src_offset = sycl::range < 3 > (offset_src /ele_size ,0 ,0 ); 1041const auto src_extend = sycl::range < 3 > (0 ,0 ,0 ); 1042const auto dest_offset = 1043 sycl::range < 3 > (w_offset_dest /ele_size ,h_offset_dest ,0 ); 1044const auto copy_extend = 1045 sycl::range < 3 > ((w - w_offset_dest ) /ele_size ,1 ,0 ); 1046// TODO: Remove const_cast after refining the signature of ext_oneapi_copy. 1047event_list .push_back (q .ext_oneapi_copy ( 1048const_cast < void *> (src_host_ptr ),src_offset ,src_extend ,dest , 1049dest_offset ,desc_dest ,copy_extend )); 1050offset_src += w - w_offset_dest ; 1051w_offset_dest = 0 ; 1052++ h_offset_dest ; 1053 } 1054if (s - offset_src > 0 ) { 1055const auto src_offset = sycl::range < 3 > (offset_src /ele_size ,0 ,0 ); 1056const auto src_extend = sycl::range < 3 > (0 ,0 ,0 ); 1057const auto dest_offset = 1058 sycl::range < 3 > (w_offset_dest /ele_size ,h_offset_dest ,0 ); 1059const auto copy_extend = sycl::range < 3 > ((s - offset_src ) /ele_size ,1 ,0 ); 1060// TODO: Remove const_cast after refining the signature of ext_oneapi_copy. 1061event_list .push_back (q .ext_oneapi_copy ( 1062const_cast < void *> (src_host_ptr ),src_offset ,src_extend ,dest , 1063dest_offset ,desc_dest ,copy_extend )); 1064 } 1065return event_list ; 1066} 1067 1068static inline std::vector < sycl::event > dpct_memcpy ( 1069const void * src , sycl::ext::oneapi::experimental::image_mem_handle dest , 1070const sycl::ext::oneapi::experimental::image_descriptor & desc_dest , 1071size_t w_offset_dest ,size_t h_offset_dest ,size_t s , sycl::queue q ) { 1072if (dpct::detail::get_pointer_attribute (q ,src )== 1073 dpct::detail::pointer_access_attribute::device_only ) { 1074 std::vector < sycl::event > event_list ; 1075 dpct::detail::host_buffer buf (s ,q ,event_list ); 1076event_list .push_back (dpct::detail::dpct_memcpy ( 1077q ,buf .get_ptr (),src ,s , memcpy_direction::device_to_host )); 1078auto copy_events = dpct_memcpy_from_host ( 1079buf .get_ptr (),dest ,desc_dest ,w_offset_dest ,h_offset_dest ,s ,q ); 1080event_list .insert (event_list .end (),copy_events .begin (),copy_events .end ()); 1081return event_list ; 1082 } 1083return dpct_memcpy_from_host (src ,dest ,desc_dest ,w_offset_dest , 1084h_offset_dest ,s ,q ); 1085} 1086 1087static inline sycl::event 1088dpct_memcpy (const image_mem_wrapper * src ,const sycl::id < 3 > & src_id , 1089const size_t src_x_offest_byte ,pitched_data & dest , 1090const sycl::id < 3 > & dest_id ,const size_t dest_x_offest_byte , 1091const sycl::range < 3 > & size ,const size_t copy_x_size_byte , 1092 sycl::queue q ) { 1093const auto ele_size = get_ele_size (src -> get_desc ()); 1094const auto src_offset = sycl::range < 3 > ( 1095src_x_offest_byte != 0 ?src_x_offest_byte /ele_size :src_id [0 ], 1096src_id [1 ],src_id [2 ]); 1097const auto dest_offset = sycl::range < 3 > ( 1098dest_x_offest_byte != 0 ?dest_x_offest_byte /ele_size :dest_id [0 ], 1099dest_id [1 ],dest_id [2 ]); 1100const auto dest_extend = 1101 sycl::range < 3 > (dest .get_pitch () /ele_size ,dest .get_y (),1 ); 1102const auto copy_extend = sycl::range < 3 > ( 1103copy_x_size_byte != 0 ?copy_x_size_byte /ele_size :size [0 ],size [1 ], 1104size [2 ]); 1105return q .ext_oneapi_copy (src -> get_handle (),src_offset ,src -> get_desc (), 1106dest .get_data_ptr (),dest_offset ,dest_extend , 1107copy_extend ); 1108} 1109 1110static inline sycl::event 1111dpct_memcpy (pitched_data src ,const sycl::id < 3 > & src_id , 1112const size_t src_x_offest_byte ,image_mem_wrapper * dest , 1113const sycl::id < 3 > & dest_id ,const size_t dest_x_offest_byte , 1114const sycl::range < 3 > & size ,const size_t copy_x_size_byte , 1115 sycl::queue q ) { 1116const auto ele_size = get_ele_size (dest -> get_desc ()); 1117const auto src_offset = sycl::range < 3 > ( 1118src_x_offest_byte != 0 ?src_x_offest_byte /ele_size :src_id [0 ], 1119src_id [1 ],src_id [2 ]); 1120const auto src_extend = 1121 sycl::range < 3 > (src .get_pitch () /ele_size ,src .get_y (),1 ); 1122const auto dest_offset = sycl::range < 3 > ( 1123dest_x_offest_byte != 0 ?dest_x_offest_byte /ele_size :dest_id [0 ], 1124dest_id [1 ],dest_id [2 ]); 1125const auto copy_extend = sycl::range < 3 > ( 1126copy_x_size_byte != 0 ?copy_x_size_byte /ele_size :size [0 ],size [1 ], 1127size [2 ]); 1128return q .ext_oneapi_copy (src .get_data_ptr (),src_offset ,src_extend , 1129dest -> get_handle (),dest_offset ,dest -> get_desc (), 1130copy_extend ); 1131} 1132 1133static inline sycl::event 1134dpct_memcpy (const image_mem_wrapper * src ,const sycl::id < 3 > & src_id , 1135const size_t src_x_offest_byte ,image_mem_wrapper * dest , 1136const sycl::id < 3 > & dest_id ,const size_t dest_x_offest_byte , 1137const sycl::range < 3 > & size ,const size_t copy_x_size_byte , 1138 sycl::queue q ) { 1139// TODO: Need change logic when sycl support image_mem to image_mem copy. 1140const auto from_ele_size = get_ele_size (src -> get_desc ()); 1141const auto to_ele_size = get_ele_size (dest -> get_desc ()); 1142const auto copy_extend = sycl::range < 3 > ( 1143copy_x_size_byte != 0 ?copy_x_size_byte /from_ele_size :size [0 ], 1144size [1 ],size [2 ]); 1145 std::vector < sycl::event > event_list ; 1146 dpct::detail::host_buffer buf ( 1147copy_extend .size ()* std::max (from_ele_size ,to_ele_size ),q ,event_list ); 1148auto to = pitched_data (buf .get_ptr (),copy_extend [0 ]* from_ele_size , 1149copy_extend [0 ],copy_extend [1 ]); 1150dpct_memcpy (src ,src_id ,src_x_offest_byte ,to , sycl::id < 3 > (0 ,0 ,0 ),0 , 1151copy_extend ,0 ,q ); 1152auto from = pitched_data (buf .get_ptr (),copy_extend [0 ]* to_ele_size , 1153copy_extend [0 ],copy_extend [1 ]); 1154event_list .push_back (dpct_memcpy (from , sycl::id < 3 > (0 ,0 ,0 ),0 ,dest ,dest_id , 1155dest_x_offest_byte ,copy_extend ,0 ,q )); 1156return event_list .front (); 1157} 1158 1159template < typename T > 1160inline bool check_duplicate_entries (int count ,T ** entries ) { 1161 std::set < T *> uniqueEntries ; 1162 1163for (size_t i = 0 ;i < count ;++ i ) { 1164auto result = uniqueEntries .insert (entries [i ]); 1165if (!result .second ) { 1166return true; 1167 } 1168 } 1169 1170return false; 1171} 1172}// namespace detail 1173 1174/// Get image_mem_wrapper according to sampled image handle. 1175/// \param [in] handle The bindless image handle. 1176/// \returns The image_mem_wrapper of sampled image. 1177inline image_mem_wrapper *& get_img_mem ( 1178const sycl::ext::oneapi::experimental::sampled_image_handle handle ) { 1179static std::map < sycl::ext::oneapi::experimental::sampled_image_handle , 1180image_mem_wrapper * , detail::sampled_image_handle_compare > 1181img_mem_map ; 1182return img_mem_map [handle ]; 1183} 1184 1185/// Get image_mem_wrapper according to unsampled image handle. 1186/// \param [in] handle The unsampled bindless image handle. 1187/// \returns The image_mem_wrapper of unsampled image. 1188inline image_mem_wrapper *& get_img_mem ( 1189const sycl::ext::oneapi::experimental::unsampled_image_handle handle ) { 1190static std::map < sycl::ext::oneapi::experimental::unsampled_image_handle , 1191image_mem_wrapper * , detail::sampled_image_handle_compare > 1192img_mem_map ; 1193return img_mem_map [handle ]; 1194} 1195 1196/// Associate an image memory wrapper with a bindless unsampled image handle in 1197/// the global registry. 1198/// 1199/// Inserts or updates an entry in the internal image memory map, using the 1200/// specified bindless unsampled image handle as the key and the provided image 1201/// memory wrapper as the mapped value. 1202/// \param [in] handle The unsampled bindless image handle. 1203/// \param [in] img_mem The image_mem_wrapper associated with the unsampled handle. 1204static inline void set_img_mem ( 1205const sycl::ext::oneapi::experimental::unsampled_image_handle handle , 1206image_mem_wrapper * img_mem ) { 1207 dpct::experimental::get_img_mem (handle )= img_mem ; 1208} 1209 1210/// Associate an image memory wrapper with a bindless sampled image handle in 1211/// the global registry. 1212/// 1213/// Inserts or updates an entry in the internal image memory map, using the 1214/// specified bindless unsampled image handle as the key and the provided image 1215/// memory wrapper as the mapped value. 1216/// \param [in] handle The sampled bindless image handle. 1217/// \param [in] img_mem The image_mem_wrapper associated with the sampled handle. 1218static inline void set_img_mem ( 1219const sycl::ext::oneapi::experimental::sampled_image_handle handle , 1220image_mem_wrapper * img_mem ) { 1221 dpct::experimental::get_img_mem (handle )= img_mem ; 1222} 1223 1224#ifdef _WIN32 1225/// Map the resource memories to mem handles 1226/// \param [in] count The count of resources to map. 1227/// \param [in] handles The external mem wrappers used to map the resources. 1228/// \param [in] q The queue used to map the resource with. 1229inline void map_resources (int count ,external_mem_wrapper ** handles , 1230queue_ptr q_ptr = & get_default_queue ()) { 1231if (detail::check_duplicate_entries (count ,handles )) { 1232throw std::runtime_error ( 1233"Duplicate handle entries found during resource mapping!" ); 1234 } 1235 1236for (int i = 0 ;i < count ;i ++ ) { 1237handles [i ]-> map_resource (* q_ptr ); 1238 } 1239} 1240 1241/// Unmap the resource memories to mem handles 1242/// \param [in] count The count of resources to unmap. 1243/// \param [in] handles The external mem wrappers used to unmap the resources. 1244/// \param [in] q The queue used to unmap the resource with. 1245inline void unmap_resources (int count ,external_mem_wrapper ** handles , 1246queue_ptr q_ptr = & get_default_queue ()) { 1247if (detail::check_duplicate_entries (count ,handles )) { 1248throw std::runtime_error ( 1249"Duplicate handle entries found during resource unmapping!" ); 1250 } 1251 1252for (int i = 0 ;i < count ;i ++ ) { 1253handles [i ]-> unmap_resource (* q_ptr ); 1254 } 1255} 1256#endif // _WIN32 1257 1258/// Imports resource's external memory into a SYCL external memory object. 1259/// \param [in] extMem SYCL external memory object ptr to be initialized. 1260/// \param [in] memHandleDesc Pointer to the external memory resource handle 1261/// descriptor. 1262/// \throws std::runtime_error If no valid external memory resource handle is 1263/// provided. 1264inline void 1265import_external_memory (sycl::ext::oneapi::experimental::external_mem * extMem , 1266external_mem_handle_desc * memHandleDesc ) { 1267#ifdef _WIN32 1268if (memHandleDesc -> get_win32_handle ()) { 1269* extMem = sycl::ext::oneapi::experimental::import_external_memory ( 1270 sycl::ext::oneapi::experimental::external_mem_descriptor < 1271 sycl::ext::oneapi::experimental::resource_win32_handle > { 1272 {memHandleDesc -> get_win32_handle ()}, 1273memHandleDesc -> get_handle_type (), 1274memHandleDesc -> get_res_size ()}, 1275get_default_queue ()); 1276 }else if (memHandleDesc -> get_win32_obj_name ()) { 1277throw std::runtime_error ( 1278"Importing external memory resource using Windows NT name" 1279" type is not supported. Only NT handle type is" 1280" supported" ); 1281 } 1282#else 1283if (memHandleDesc -> get_fd_handle ()!= -1 ) { 1284* extMem = sycl::ext::oneapi::experimental::import_external_memory ( 1285 sycl::ext::oneapi::experimental::external_mem_descriptor < 1286 sycl::ext::oneapi::experimental::resource_fd > { 1287 {memHandleDesc -> get_fd_handle ()}, 1288memHandleDesc -> get_handle_type (), 1289memHandleDesc -> get_res_size ()}, 1290get_default_queue ()); 1291 } 1292#endif // _WIN32 1293else { 1294 throwstd ::runtime_error ( 1295"No external memory resource handle is set for importing!" ); 1296 } 1297} 1298 1299/// Imports external semaphore resource into a SYCL external semaphore object. 1300/// \param [in] extSem SYCL external semaphore object ptr to be initialized. 1301/// \param [in] semHandleDesc Pointer to the external semaphore resource handle 1302/// descriptor. 1303/// \throws std::runtime_error If no valid external semaphore resource handle is 1304/// provided. 1305inline void import_external_semaphore (external_sem_wrapper ** extSem , 1306external_sem_handle_desc * semHandleDesc ) { 1307 sycl::ext::oneapi::experimental::external_semaphore extSemObj ; 1308#ifdef _WIN32 1309if (semHandleDesc -> get_win32_handle ()) { 1310extSemObj = sycl::ext::oneapi::experimental::import_external_semaphore ( 1311 sycl::ext::oneapi::experimental::external_semaphore_descriptor < 1312 sycl::ext::oneapi::experimental::resource_win32_handle > { 1313 {semHandleDesc -> get_win32_handle ()}, 1314semHandleDesc -> get_handle_type ()}, 1315get_default_queue ()); 1316 }else if (semHandleDesc -> get_win32_obj_name ()) { 1317throw std::runtime_error ( 1318"Importing external semaphore resource using Windows NT name" 1319" type is not supported. Only NT handle type is" 1320" supported" ); 1321 } 1322#else 1323if (semHandleDesc -> get_fd_handle ()!= -1 ) { 1324extSemObj = sycl::ext::oneapi::experimental::import_external_semaphore ( 1325 sycl::ext::oneapi::experimental::external_semaphore_descriptor < 1326 sycl::ext::oneapi::experimental::resource_fd > { 1327 {semHandleDesc -> get_fd_handle ()},semHandleDesc -> get_handle_type ()}, 1328get_default_queue ()); 1329 } 1330#endif // _WIN32 1331else { 1332 throwstd ::runtime_error ( 1333"No external semaphore resource handle is set for importing!" ); 1334 } 1335 1336* extSem = 1337new external_sem_wrapper (extSemObj ,semHandleDesc -> get_handle_type ()); 1338} 1339 1340/// Signals the external semaphore resource. 1341/// \param [in] extSem ptr to dpct external semaphore resource ptr 1342/// \param [in] semSignalParams Pointer to the external semaphore signal 1343/// parameters. 1344/// \param [in] numExtSems Number of external semaphores to signal. 1345/// \param [in] q_ptr The queue used to signal the external semaphore resource. 1346inline void signal_external_semaphores (external_sem_wrapper ** extSem , 1347external_sem_params * semSignalParams , 1348unsigned int numExtSems , 1349queue_ptr q_ptr = & get_default_queue ()) { 1350for (int i = 0 ;i < numExtSems ;i ++ ) { 1351auto extSemType = extSem [i ]-> get_handle_type (); 1352switch (extSemType ) { 1353#ifdef _WIN32 1354case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1355win32_nt_dx12_fence : 1356case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1357timeline_win32_nt_handle : 1358q_ptr -> ext_oneapi_signal_external_semaphore ( 1359extSem [i ]-> get (),semSignalParams [i ].get_value ()); 1360break ; 1361case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1362win32_nt_handle : 1363q_ptr -> ext_oneapi_signal_external_semaphore (extSem [i ]-> get ()); 1364break ; 1365#else // _WIN32 1366case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1367timeline_fd : 1368q_ptr -> ext_oneapi_signal_external_semaphore ( 1369extSem [i ]-> get (),semSignalParams [i ].get_value ()); 1370break ; 1371case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1372opaque_fd : 1373q_ptr -> ext_oneapi_signal_external_semaphore (extSem [i ]-> get ()); 1374break ; 1375#endif // _WIN32 1376default : 1377throw std::runtime_error ( 1378"Unsupported external semaphore resource handle type!" ); 1379break ; 1380 } 1381 } 1382} 1383 1384/// Wait for the external semaphore resource. 1385/// \param [in] extSem ptr to dpct external semaphore resource ptr 1386/// \param [in] semWaitParams Pointer to the external semaphore wait parameters. 1387/// \param [in] numExtSems Number of external semaphores to wait. 1388/// \param [in] q_ptr The queue used to wait on the external semaphore resource. 1389inline void wait_external_semaphores (external_sem_wrapper ** extSem , 1390external_sem_params * semWaitParams , 1391unsigned int numExtSems , 1392queue_ptr q_ptr = & get_default_queue ()) { 1393for (int i = 0 ;i < numExtSems ;i ++ ) { 1394auto extSemType = extSem [i ]-> get_handle_type (); 1395switch (extSemType ) { 1396#ifdef _WIN32 1397case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1398win32_nt_dx12_fence : 1399case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1400timeline_win32_nt_handle : 1401q_ptr -> ext_oneapi_wait_external_semaphore (extSem [i ]-> get (), 1402semWaitParams [i ].get_value ()); 1403break ; 1404case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1405win32_nt_handle : 1406q_ptr -> ext_oneapi_wait_external_semaphore (extSem [i ]-> get ()); 1407break ; 1408#else // _WIN32 1409case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1410timeline_fd : 1411q_ptr -> ext_oneapi_wait_external_semaphore (extSem [i ]-> get (), 1412semWaitParams [i ].get_value ()); 1413break ; 1414case sycl::ext::oneapi::experimental::external_semaphore_handle_type:: 1415opaque_fd : 1416q_ptr -> ext_oneapi_wait_external_semaphore (extSem [i ]-> get ()); 1417break ; 1418#endif // _WIN32 1419default : 1420throw std::runtime_error ( 1421"Unsupported external semaphore resource handle type!" ); 1422break ; 1423 } 1424 } 1425} 1426 1427/// Create bindless image according to image data and sampling info. 1428/// \param [in] data The image data used to create bindless image. 1429/// \param [in] info The image sampling info used to create bindless image. 1430/// \param [in] q The queue where the image creation be executed. 1431/// \returns The sampled image handle of created bindless image. 1432static inline sycl::ext::oneapi::experimental::sampled_image_handle 1433create_bindless_image (image_data data ,sampling_info info , 1434 sycl::queue q = get_default_queue ()) { 1435auto samp = sycl::ext::oneapi::experimental::bindless_image_sampler ( 1436info .get_addressing_mode (),info .get_coordinate_normalization_mode (), 1437info .get_filtering_mode (),info .get_mipmap_filtering (), 1438info .get_min_mipmap_level_clamp (),info .get_max_mipmap_level_clamp (), 1439info .get_max_anisotropy ()); 1440 1441switch (data .get_data_type ()) { 1442case image_data_type::linear : { 1443// linear memory only use sycl::filtering_mode::nearest. 1444samp .filtering = sycl::filtering_mode::nearest ; 1445// TODO: Use pointer to create image when bindless image support. 1446auto mem = new image_mem_wrapper ( 1447data .get_channel (),data .get_x () /data .get_channel ().get_total_size ()); 1448auto img = sycl::ext::oneapi::experimental::create_image ( 1449mem -> get_handle (),samp ,mem -> get_desc (),q ); 1450set_img_mem (img ,mem ); 1451auto ptr = data .get_data_ptr (); 1452#ifdef DPCT_USM_LEVEL_NONE 1453q .ext_oneapi_copy (get_buffer (ptr ).get_host_access ().get_pointer (), 1454mem -> get_handle (),mem -> get_desc ()) 1455 .wait (); 1456#else 1457q .ext_oneapi_copy (ptr ,mem -> get_handle (),mem -> get_desc ()).wait (); 1458#endif 1459 detail::get_img_info_map (img )= {data ,info }; 1460return img ; 1461 } 1462case image_data_type::pitch : { 1463#ifdef DPCT_USM_LEVEL_NONE 1464auto mem = 1465new image_mem_wrapper (data .get_channel (),data .get_x (),data .get_y ()); 1466auto img = sycl::ext::oneapi::experimental::create_image ( 1467mem -> get_handle (),samp ,mem -> get_desc (),q ); 1468set_img_mem (img ,mem ); 1469q .ext_oneapi_copy ( 1470get_buffer (data .get_data_ptr ()).get_host_access ().get_pointer (), 1471mem -> get_handle (),mem -> get_desc ()) 1472 .wait (); 1473#else 1474auto desc = sycl::ext::oneapi::experimental::image_descriptor ( 1475 {data .get_x (),data .get_y ()},data .get_channel ().get_channel_num (), 1476data .get_channel_type ()); 1477auto img = sycl::ext::oneapi::experimental::create_image ( 1478data .get_data_ptr (),data .get_pitch (),samp ,desc ,q ); 1479#endif 1480 detail::get_img_info_map (img )= {data ,info }; 1481return img ; 1482 } 1483case image_data_type::matrix : { 1484const auto mem = static_cast < image_mem_wrapper *> (data .get_data_ptr ()); 1485auto img = sycl::ext::oneapi::experimental::create_image ( 1486mem -> get_handle (),samp ,mem -> get_desc (),q ); 1487 detail::get_img_info_map (img )= {data ,info }; 1488return img ; 1489 } 1490default : 1491throw std::runtime_error ( 1492"Unsupported image_data_type in create_bindless_image!" ); 1493break ; 1494 } 1495// Must not reach here. 1496return sycl::ext::oneapi::experimental::sampled_image_handle (); 1497} 1498 1499/// Create bindless image according to image data. 1500/// \param [in] data The image data used to create bindless image. 1501/// \param [in] q The queue where the image creation be executed. 1502/// \returns The sampled image handle of created bindless image. 1503static inline sycl::ext::oneapi::experimental::unsampled_image_handle 1504create_bindless_image (image_data data , sycl::queue q = get_default_queue ()) { 1505switch (data .get_data_type ()) { 1506case image_data_type::linear : { 1507// TODO: Use pointer to create image when bindless image support. 1508auto mem = new image_mem_wrapper ( 1509data .get_channel (),data .get_x () /data .get_channel ().get_total_size ()); 1510auto img = sycl::ext::oneapi::experimental::create_image ( 1511mem -> get_handle (),mem -> get_desc (),q ); 1512set_img_mem (img ,mem ); 1513auto ptr = data .get_data_ptr (); 1514#ifdef DPCT_USM_LEVEL_NONE 1515q .ext_oneapi_copy (get_buffer (ptr ).get_host_access ().get_pointer (), 1516mem -> get_handle (),mem -> get_desc ()) 1517 .wait (); 1518#else 1519q .ext_oneapi_copy (ptr ,mem -> get_handle (),mem -> get_desc ()).wait (); 1520#endif 1521return img ; 1522 } 1523case image_data_type::pitch : { 1524auto mem = 1525new image_mem_wrapper (data .get_channel (),data .get_x (),data .get_y ()); 1526auto img = sycl::ext::oneapi::experimental::create_image ( 1527mem -> get_handle (),mem -> get_desc (),q ); 1528set_img_mem (img ,mem ); 1529#ifdef DPCT_USM_LEVEL_NONE 1530q .ext_oneapi_copy ( 1531get_buffer (data .get_data_ptr ()).get_host_access ().get_pointer (), 1532mem -> get_handle (),mem -> get_desc ()) 1533 .wait (); 1534#else 1535q .ext_oneapi_copy (data .get_data_ptr (),mem -> get_handle (),mem -> get_desc ()) 1536 .wait (); 1537 1538#endif 1539return img ; 1540 } 1541case image_data_type::matrix : { 1542const auto mem = static_cast < image_mem_wrapper *> (data .get_data_ptr ()); 1543auto img = sycl::ext::oneapi::experimental::create_image ( 1544mem -> get_handle (),mem -> get_desc (),q ); 1545return img ; 1546 } 1547default : 1548throw std::runtime_error ( 1549"Unsupported image_data_type in create_bindless_image!" ); 1550break ; 1551 } 1552// Must not reach here. 1553return sycl::ext::oneapi::experimental::unsampled_image_handle (); 1554} 1555 1556/// Destroy bindless image. 1557/// \param [in] handle The bindless image should be destroyed. 1558/// \param [in] q The queue where the image destruction be executed. 1559template < class T > 1560static inline void destroy_bindless_image (T handle , 1561 sycl::queue q = get_default_queue ()) { 1562auto & mem = get_img_mem (handle ); 1563if (mem ) { 1564delete mem ; 1565mem = nullptr ; 1566 } 1567 sycl::ext::oneapi::experimental::destroy_image_handle (handle ,q ); 1568} 1569 1570/// Get the image data according to sampled image handle. 1571/// \param [in] handle The bindless image handle. 1572/// \returns The image data of sampled image. 1573template < class T > static inline image_data get_data (const T handle ) { 1574return detail::get_img_info_map (handle ).first ; 1575} 1576 1577/// Get the sampling info according to sampled image handle. 1578/// \param [in] handle The bindless image handle. 1579/// \returns The sampling info of sampled image. 1580static inline sampling_info get_sampling_info ( 1581const sycl::ext::oneapi::experimental::sampled_image_handle handle ) { 1582return detail::get_img_info_map (handle ).second ; 1583} 1584 1585/// The base class of different template specialization bindless_image_wrapper 1586/// class. 1587class bindless_image_wrapper_base { 1588public : 1589bindless_image_wrapper_base () { 1590// Make sure that singleton class dev_mgr will destruct later than this. 1591 dev_mgr::instance (); 1592 } 1593 1594/// Destroy bindless image wrapper. 1595 ~bindless_image_wrapper_base () { 1596destroy_bindless_image (_img ,get_default_queue ()); 1597 } 1598 1599/// Attach linear data to bindless image. 1600/// \param [in] data The linear data used to create bindless image. 1601/// \param [in] size The size of linear data used to create bindless image. 1602/// \param [in] channel The image channel used to create bindless image. 1603/// \param [in] q The queue where the image creation be executed. 1604void attach (void * data ,size_t size ,const image_channel & channel , 1605 sycl::queue q = get_default_queue ()) { 1606detach (q ); 1607auto samp = sycl::ext::oneapi::experimental::bindless_image_sampler ( 1608_addressing_mode ,_coordinate_normalization_mode ,_filtering_mode ); 1609// TODO: Use pointer to create image when bindless image support. 1610auto mem = new image_mem_wrapper (channel ,size ); 1611_img = sycl::ext::oneapi::experimental::create_image ( 1612mem -> get_handle (),samp ,mem -> get_desc (),q ); 1613set_img_mem (_img ,mem ); 1614auto ptr = data ; 1615#ifdef DPCT_USM_LEVEL_NONE 1616q .ext_oneapi_copy (get_buffer (data ).get_host_access ().get_pointer (), 1617mem -> get_handle (),mem -> get_desc ()) 1618 .wait (); 1619#else 1620q .ext_oneapi_copy (ptr ,mem -> get_handle (),mem -> get_desc ()).wait (); 1621#endif 1622 } 1623 1624/// Attach linear data to bindless image. 1625/// \param [in] data The linear data used to create bindless image. 1626/// \param [in] size The size of linear data used to create bindless image. 1627/// \param [in] q The queue where the image creation be executed. 1628void attach (void * data ,size_t size , sycl::queue q = get_default_queue ()) { 1629attach (data ,size ,_channel ,q ); 1630 } 1631 1632/// Attach device_ptr data to bindless image. 1633/// \param [in] desc The image_descriptor used to create bindless image. 1634/// \param [in] ptr The data pointer used to create bindless image. 1635/// \param [in] pitch The pitch of 2D data used to create bindless image. 1636/// \param [in] q The queue where the image creation be executed. 1637void attach (const sycl::ext::oneapi::experimental::image_descriptor * desc , 1638device_ptr ptr ,size_t pitch , 1639 sycl::queue q = get_default_queue ()) { 1640detach (q ); 1641auto samp = sycl::ext::oneapi::experimental::bindless_image_sampler ( 1642_addressing_mode ,_coordinate_normalization_mode ,_filtering_mode ); 1643#ifdef DPCT_USM_LEVEL_NONE 1644auto mem = new image_mem_wrapper (desc ); 1645_img = sycl::ext::oneapi::experimental::create_image (mem -> get_handle (), 1646samp ,* desc ,q ); 1647set_img_mem (_img ,mem ); 1648q .ext_oneapi_copy (get_buffer (ptr ).get_host_access ().get_pointer (), 1649mem -> get_handle (),mem -> get_desc ()) 1650 .wait (); 1651#else 1652_img = sycl::ext::oneapi::experimental::create_image (ptr ,pitch ,samp , 1653* desc ,q ); 1654#endif 1655 } 1656 1657/// Attach 2D data to bindless image. 1658/// \param [in] data The 2D data used to create bindless image. 1659/// \param [in] width The width of 2D data used to create bindless image. 1660/// \param [in] height The height of 2D data used to create bindless image. 1661/// \param [in] pitch The pitch of 2D data used to create bindless image. 1662/// \param [in] channel The image channel used to create bindless image. 1663/// \param [in] q The queue where the image creation be executed. 1664void attach (void * data ,size_t width ,size_t height ,size_t pitch , 1665const image_channel & channel , 1666 sycl::queue q = get_default_queue ()) { 1667auto desc = sycl::ext::oneapi::experimental::image_descriptor ( 1668 {width ,height },channel .get_channel_num (),channel .get_channel_type ()); 1669attach (& desc ,static_cast < device_ptr > (data ),pitch ,q ); 1670 } 1671 1672/// Attach 2D data to bindless image. 1673/// \param [in] data The 2D data used to create bindless image. 1674/// \param [in] width The width of 2D data used to create bindless image. 1675/// \param [in] height The height of 2D data used to create bindless image. 1676/// \param [in] pitch The pitch of 2D data used to create bindless image. 1677/// \param [in] q The queue where the image creation be executed. 1678void attach (void * data ,size_t width ,size_t height ,size_t pitch , 1679 sycl::queue q = get_default_queue ()) { 1680attach (data ,width ,height ,pitch ,_channel ,q ); 1681 } 1682 1683/// Attach image memory to bindless image. 1684/// \param [in] mem The image memory used to create bindless image. 1685/// \param [in] q The queue where the image creation be executed. 1686void attach (image_mem_wrapper * mem ,const image_channel & channel , 1687 sycl::queue q = get_default_queue ()) { 1688detach (q ); 1689auto samp = sycl::ext::oneapi::experimental::bindless_image_sampler ( 1690_addressing_mode ,_coordinate_normalization_mode ,_filtering_mode ); 1691_img = sycl::ext::oneapi::experimental::create_image ( 1692mem -> get_handle (),samp ,mem -> get_desc (),q ); 1693set_img_mem (_img ,mem ); 1694 } 1695 1696/// Attach image memory to bindless image. 1697/// \param [in] mem The image memory used to create bindless image. 1698/// \param [in] q The queue where the image creation be executed. 1699void attach (image_mem_wrapper * mem , sycl::queue q = get_default_queue ()) { 1700attach (mem ,_channel ,q ); 1701 } 1702 1703/// Detach bindless image data. 1704/// \param [in] q The queue where the image destruction be executed. 1705void detach (sycl::queue q = get_default_queue ()) { 1706destroy_bindless_image (_img ,q ); 1707 } 1708 1709/// Set image channel of bindless image. 1710/// \param [in] channel The image channel used to set. 1711void set_channel (image_channel channel ) {_channel = channel ; } 1712/// Get image channel of bindless image. 1713/// \returns The image channel of bindless image. 1714image_channel get_channel () {return _channel ; } 1715 1716/// Set channel size of image channel. 1717/// \param [in] channel_num The channels number to set. 1718/// \param [in] channel_size The size for each channel in bits. 1719void set_channel_size (unsigned channel_num ,unsigned channel_size ) { 1720return _channel .set_channel_size (channel_num ,channel_size ); 1721 } 1722/// Get channel size of image channel. 1723/// \returns The size for each channel in bits. 1724unsigned get_channel_size () {return _channel .get_channel_size (); } 1725 1726/// Set image channel data type of image channel. 1727/// \param [in] type The image channel data type to set. 1728void set_channel_data_type (image_channel_data_type type ) { 1729_channel .set_channel_data_type (type ); 1730 } 1731/// Get image channel data type of image channel. 1732/// \returns The image channel data type of image channel. 1733image_channel_data_type get_channel_data_type () { 1734return _channel .get_channel_data_type (); 1735 } 1736 1737/// Set channel num of bindless image. 1738/// \param [in] filtering_mode The channel num used to set. 1739void set_channel_num (unsigned num ) {return _channel .set_channel_num (num ); } 1740 1741/// Set channel type of bindless image. 1742/// \param [in] filtering_mode The channel type used to set. 1743void set_channel_type (sycl::image_channel_type type ) { 1744return _channel .set_channel_type (type ); 1745 } 1746 1747/// Set addressing mode of bindless image. 1748/// \param [in] addressing_mode The addressing mode used to set. 1749void set (sycl::addressing_mode addressing_mode ) { 1750_addressing_mode = addressing_mode ; 1751 } 1752/// Get addressing mode of bindless image. 1753/// \returns The addressing mode of bindless image. 1754 sycl::addressing_mode get_addressing_mode () {return _addressing_mode ; } 1755 1756/// Set coordinate normalization mode of bindless image. 1757/// \param [in] coordinate_normalization_mode The coordinate normalization 1758/// mode used to set. 1759void set (sycl::coordinate_normalization_mode coordinate_normalization_mode ) { 1760_coordinate_normalization_mode = coordinate_normalization_mode ; 1761 } 1762/// Set coordinate normalization mode of bindless image. 1763/// \param [in] is_normalized Determine whether the coordinate should be 1764/// normalized. 1765void set_coordinate_normalization_mode (int is_normalized ) { 1766_coordinate_normalization_mode = 1767is_normalized ? sycl::coordinate_normalization_mode::normalized 1768 : sycl::coordinate_normalization_mode::unnormalized ; 1769 } 1770/// Get coordinate normalization mode of bindless image. 1771/// \returns The coordinate normalization mode of bindless image. 1772bool is_coordinate_normalized () { 1773return _coordinate_normalization_mode == 1774 sycl::coordinate_normalization_mode::normalized ; 1775 } 1776 1777/// Set filtering mode of bindless image. 1778/// \param [in] filtering_mode The filtering mode used to set. 1779void set (sycl::filtering_mode filtering_mode ) { 1780_filtering_mode = filtering_mode ; 1781 } 1782/// Get filtering mode of bindless image. 1783/// \returns The filtering mode of bindless image. 1784 sycl::filtering_mode get_filtering_mode () {return _filtering_mode ; } 1785 1786/// Get bindless image handle. 1787/// \returns The sampled image handle of created bindless image. 1788inline sycl::ext::oneapi::experimental::sampled_image_handle get_handle () { 1789return _img ; 1790 } 1791 1792/// Set mipmap sample filtering mode for bindless image handle 1793/// \param [in] filtering_mode The mipmap filtering mode 1794inline void set_mip_filtering_mode (sycl::filtering_mode mode ) { 1795auto sampling_info = get_sampling_info (_img ); 1796sampling_info .set_mipmap_filtering (mode ); 1797 } 1798 1799/// Get mipmap sample filtering mode for bindless image handle 1800/// \return The mipmap filtering mode 1801inline sycl::filtering_mode get_mip_filtering_mode (void ) { 1802auto sampling_info = get_sampling_info (_img ); 1803return sampling_info .get_mipmap_filtering (); 1804 } 1805 1806/// Get mipmap sample filtering mode for bindless image handle 1807/// \return The mipmap filtering mode 1808inline void get_mip_level_clamp (float * min_level_clamp , 1809float * max_level_clamp ) { 1810auto sampling_info = get_sampling_info (_img ); 1811* min_level_clamp = sampling_info .get_min_mipmap_level_clamp (); 1812* max_level_clamp = sampling_info .get_max_mipmap_level_clamp (); 1813 } 1814 1815/// Get mipmap memory wrapper attached the bindless image 1816/// \return The mipmap memory wrapper 1817inline image_mem_wrapper * get_attached_mipmap_data (void ) { 1818auto mem = get_img_mem (_img ); 1819 1820if (mem -> get_image_type ()!= 1821 sycl::ext::oneapi::experimental::image_type::mipmap ) 1822throw std::runtime_error ("Bindless image data is not of mipmap type" ); 1823 1824return mem ; 1825 } 1826 1827private : 1828image_channel _channel ; 1829 sycl::addressing_mode _addressing_mode = sycl::addressing_mode::clamp_to_edge ; 1830 sycl::coordinate_normalization_mode _coordinate_normalization_mode = 1831 sycl::coordinate_normalization_mode::unnormalized ; 1832 sycl::filtering_mode _filtering_mode = sycl::filtering_mode::nearest ; 1833 sycl::ext::oneapi::experimental::sampled_image_handle _img {0 }; 1834}; 1835 1836/// The wrapper class of bindless image. 1837template < class T ,int dimensions > 1838class bindless_image_wrapper :public bindless_image_wrapper_base { 1839public : 1840/// Create bindless image wrapper according to template argument \p T. 1841bindless_image_wrapper () {set_channel (image_channel::create < T > ()); } 1842}; 1843 1844/// Asynchronously copies from the image memory to memory specified by a 1845/// pointer, The return of the function does NOT guarantee the copy is 1846/// completed. 1847/// \param [in] dest The destination memory address. 1848/// \param [in] p The pitch of destination memory. 1849/// \param [in] src The source image memory. 1850/// \param [in] w_offset_src The x offset of source image memory. 1851/// \param [in] h_offset_src The y offset of source image memory. 1852/// \param [in] w The width of matrix to be copied. 1853/// \param [in] h The height of matrix to be copied. 1854/// \param [in] q The queue to execute the copy task. 1855static inline void async_dpct_memcpy (void * dest ,size_t p , 1856const image_mem_wrapper * src , 1857size_t w_offset_src ,size_t h_offset_src , 1858size_t w ,size_t h , 1859 sycl::queue q = get_default_queue ()) { 1860 detail::dpct_memcpy (src -> get_handle (),src -> get_desc (),w_offset_src , 1861h_offset_src ,dest ,p ,w ,h ,q ); 1862} 1863 1864/// Synchronously copies from the image memory to memory specified by a 1865/// pointer, The function will return after the copy is completed. 1866/// \param [in] dest The destination memory address. 1867/// \param [in] p The pitch of destination memory. 1868/// \param [in] src The source image memory. 1869/// \param [in] w_offset_src The x offset of source image memory. 1870/// \param [in] h_offset_src The y offset of source image memory. 1871/// \param [in] w The width of matrix to be copied. 1872/// \param [in] h The height of matrix to be copied. 1873/// \param [in] q The queue to execute the copy task. 1874static inline void dpct_memcpy (void * dest ,size_t p , 1875const image_mem_wrapper * src , 1876size_t w_offset_src ,size_t h_offset_src , 1877size_t w ,size_t h , 1878 sycl::queue q = get_default_queue ()) { 1879 detail::dpct_memcpy (src -> get_handle (),src -> get_desc (),w_offset_src , 1880h_offset_src ,dest ,p ,w ,h ,q ) 1881 .wait (); 1882} 1883 1884/// Asynchronously copies from the image memory to memory specified by a 1885/// pointer, The return of the function does NOT guarantee the copy is 1886/// completed. 1887/// \param [in] dest The destination memory address. 1888/// \param [in] src The source image memory. 1889/// \param [in] w_offset_src The x offset of source image memory. 1890/// \param [in] h_offset_src The y offset of source image memory. 1891/// \param [in] s The size to be copied. 1892/// \param [in] q The queue to execute the copy task. 1893static inline void async_dpct_memcpy (void * dest ,const image_mem_wrapper * src , 1894size_t w_offset_src ,size_t h_offset_src , 1895size_t s , 1896 sycl::queue q = get_default_queue ()) { 1897 detail::dpct_memcpy (src -> get_handle (),src -> get_desc (),w_offset_src , 1898h_offset_src ,dest ,s ,q ); 1899} 1900 1901/// Synchronously copies from the image memory to memory specified by a 1902/// pointer, The function will return after the copy is completed. 1903/// \param [in] dest The destination memory address. 1904/// \param [in] src The source image memory. 1905/// \param [in] w_offset_src The x offset of source image memory. 1906/// \param [in] h_offset_src The y offset of source image memory. 1907/// \param [in] s The size to be copied. 1908/// \param [in] q The queue to execute the copy task. 1909static inline void dpct_memcpy (void * dest ,const image_mem_wrapper * src , 1910size_t w_offset_src ,size_t h_offset_src , 1911size_t s , sycl::queue q = get_default_queue ()) { 1912 sycl::event::wait (detail::dpct_memcpy (src -> get_handle (),src -> get_desc (), 1913w_offset_src ,h_offset_src ,dest ,s , 1914q )); 1915} 1916 1917/// Asynchronously copies from memory specified by a pointer to the image 1918/// memory, The return of the function does NOT guarantee the copy is completed. 1919/// \param [in] dest The destination image memory. 1920/// \param [in] w_offset_dest The x offset of destination image memory. 1921/// \param [in] h_offset_dest The y offset of destination image memory. 1922/// \param [in] src The source memory address. 1923/// \param [in] p The pitch of source memory. 1924/// \param [in] w The width of matrix to be copied. 1925/// \param [in] h The height of matrix to be copied. 1926/// \param [in] q The queue to execute the copy task. 1927static inline void async_dpct_memcpy (image_mem_wrapper * dest , 1928size_t w_offset_dest ,size_t h_offset_dest , 1929const void * src ,size_t p ,size_t w , 1930size_t h , 1931 sycl::queue q = get_default_queue ()) { 1932 detail::dpct_memcpy (src ,dest -> get_handle (),dest -> get_desc (),w_offset_dest , 1933h_offset_dest ,p ,w ,h ,q ); 1934} 1935 1936/// Synchronously copies from memory specified by a pointer to the image 1937/// memory, The function will return after the copy is completed. 1938/// \param [in] dest The destination image memory. 1939/// \param [in] w_offset_dest The x offset of destination image memory. 1940/// \param [in] h_offset_dest The y offset of destination image memory. 1941/// \param [in] src The source memory address. 1942/// \param [in] p The pitch of source memory. 1943/// \param [in] w The width of matrix to be copied. 1944/// \param [in] h The height of matrix to be copied. 1945/// \param [in] q The queue to execute the copy task. 1946static inline void dpct_memcpy (image_mem_wrapper * dest ,size_t w_offset_dest , 1947size_t h_offset_dest ,const void * src ,size_t p , 1948size_t w ,size_t h , 1949 sycl::queue q = get_default_queue ()) { 1950 detail::dpct_memcpy (src ,dest -> get_handle (),dest -> get_desc (),w_offset_dest , 1951h_offset_dest ,p ,w ,h ,q ) 1952 .wait (); 1953} 1954 1955/// Asynchronously copies from memory specified by a pointer to the image 1956/// memory, The return of the function does NOT guarantee the copy is completed. 1957/// \param [in] dest The destination image memory. 1958/// \param [in] w_offset_dest The x offset of destination image memory. 1959/// \param [in] h_offset_dest The y offset of destination image memory. 1960/// \param [in] src The source memory address. 1961/// \param [in] s The size to be copied. 1962/// \param [in] q The queue to execute the copy task. 1963static inline void async_dpct_memcpy (image_mem_wrapper * dest , 1964size_t w_offset_dest ,size_t h_offset_dest , 1965const void * src ,size_t s , 1966 sycl::queue q = get_default_queue ()) { 1967 detail::dpct_memcpy (src ,dest -> get_handle (),dest -> get_desc (),w_offset_dest , 1968h_offset_dest ,s ,q ); 1969} 1970 1971/// Synchronously copies from memory specified by a pointer to the image 1972/// memory, The function will return after the copy is completed. 1973/// \param [in] dest The destination image memory. 1974/// \param [in] w_offset_dest The x offset of destination image memory. 1975/// \param [in] h_offset_dest The y offset of destination image memory. 1976/// \param [in] src The source memory address. 1977/// \param [in] s The size to be copied. 1978/// \param [in] q The queue to execute the copy task. 1979static inline void dpct_memcpy (image_mem_wrapper * dest ,size_t w_offset_dest , 1980size_t h_offset_dest ,const void * src ,size_t s , 1981 sycl::queue q = get_default_queue ()) { 1982 sycl::event::wait (detail::dpct_memcpy (src ,dest -> get_handle (), 1983dest -> get_desc (),w_offset_dest , 1984h_offset_dest ,s ,q )); 1985} 1986 1987/// Synchronously copies from image memory to the image memory, The function 1988/// will return after the copy is completed. 1989/// \param [in] dest The destination image memory. 1990/// \param [in] w_offset_dest The x offset of destination image memory. 1991/// \param [in] h_offset_dest The y offset of destination image memory. 1992/// \param [in] src The source image memory. 1993/// \param [in] w_offset_src The x offset of source image memory. 1994/// \param [in] h_offset_src The y offset of source image memory. 1995/// \param [in] w The width of matrix to be copied. 1996/// \param [in] h The height of matrix to be copied. 1997/// \param [in] q The queue to execute the copy task. 1998static inline void dpct_memcpy (image_mem_wrapper * dest ,size_t w_offset_dest , 1999size_t h_offset_dest , 2000const image_mem_wrapper * src , 2001size_t w_offset_src ,size_t h_offset_src , 2002size_t w ,size_t h , 2003 sycl::queue q = get_default_queue ()) { 2004auto temp = (void * )sycl::malloc_device (w * h ,q ); 2005// TODO: Need change logic when sycl support image_mem to image_mem copy. 2006dpct_memcpy (temp ,w ,src ,w_offset_src ,h_offset_src ,w ,h ,q ); 2007dpct_memcpy (dest ,w_offset_dest ,h_offset_dest ,temp ,w ,w ,h ,q ); 2008 sycl::free (temp ,q ); 2009} 2010 2011/// Synchronously copies from image memory to the image memory, The function 2012/// will return after the copy is completed. 2013/// \param [in] dest The destination image memory. 2014/// \param [in] w_offset_dest The x offset of destination image memory. 2015/// \param [in] h_offset_dest The y offset of destination image memory. 2016/// \param [in] src The source image memory. 2017/// \param [in] w_offset_src The x offset of source image memory. 2018/// \param [in] h_offset_src The y offset of source image memory. 2019/// \param [in] s The size to be copied. 2020/// \param [in] q The queue to execute the copy task. 2021static inline void dpct_memcpy (image_mem_wrapper * dest ,size_t w_offset_dest , 2022size_t h_offset_dest , 2023const image_mem_wrapper * src , 2024size_t w_offset_src ,size_t h_offset_src , 2025size_t s , sycl::queue q = get_default_queue ()) { 2026auto temp = (void * )sycl::malloc_device (s ,q ); 2027// TODO: Need change logic when sycl support image_mem to image_mem copy. 2028dpct_memcpy (temp ,src ,w_offset_src ,h_offset_src ,s ,q ); 2029dpct_memcpy (dest ,w_offset_dest ,h_offset_dest ,temp ,s ,q ); 2030 sycl::free (temp ,q ); 2031} 2032// A wrapper for sycl fetch_image function for the byte addressing image. 2033template < typename DataT ,typename HintT = DataT ,typename CoordT > 2034DataT fetch_image_by_byte ( 2035const sycl::ext::oneapi::experimental::unsampled_image_handle & imageHandle , 2036CoordT && coords ) { 2037if constexpr (std::is_scalar_v < CoordT > ) { 2038return sycl::ext::oneapi::experimental::fetch_image < DataT ,HintT ,CoordT > ( 2039imageHandle ,coords /sizeof (DataT )); 2040 }else { 2041coords [0 ]= coords [0 ] /sizeof (DataT ); 2042return sycl::ext::oneapi::experimental::fetch_image < DataT ,HintT ,CoordT > ( 2043imageHandle ,coords ); 2044 } 2045} 2046 2047using image_mem_wrapper_ptr = image_mem_wrapper * ; 2048#ifdef _WIN32 2049using external_mem_wrapper_ptr = external_mem_wrapper * ; 2050#else 2051using external_mem_wrapper_ptr = external_mem_wrapper_base * ; 2052#endif // _WIN32 2053using external_sem_wrapper_ptr = external_sem_wrapper * ; 2054using bindless_image_wrapper_base_p = bindless_image_wrapper_base * ; 2055 2056#endif 2057 2058}// namespace experimental 2059}// namespace dpct 2060 2061#endif // !__DPCT_BINDLESS_IMAGE_HPP__