@@ -50,8 +50,7 @@ struct sampled_image_handle {
5050
5151 sampled_image_handle () : raw_handle(~0 ) {}
5252
53- sampled_image_handle (raw_image_handle_type raw_image_handle)
54- : raw_handle(raw_image_handle) {}
53+ sampled_image_handle (raw_image_handle_type handle) : raw_handle(handle) {}
5554
5655 raw_image_handle_type raw_handle;
5756};
@@ -792,6 +791,43 @@ template <typename DataT> constexpr bool is_recognized_standard_type() {
792791 std::is_floating_point_v<DataT> || std::is_same_v<DataT, sycl::half>);
793792}
794793
794+ #ifdef __SYCL_DEVICE_ONLY__
795+
796+ // Image types used for generating SPIR-V
797+ template <int NDims>
798+ using OCLImageTyRead =
799+ typename sycl::detail::opencl_image_type<NDims, sycl::access::mode::read,
800+ sycl::access::target::image>::type;
801+
802+ template <int NDims>
803+ using OCLImageTyWrite =
804+ typename sycl::detail::opencl_image_type<NDims, sycl::access::mode::write,
805+ sycl::access::target::image>::type;
806+
807+ // Macros are required because it is not legal for a function to return
808+ // a variable of type 'opencl_image_type'.
809+ #if defined(__SPIR__)
810+ #define CONVERT_HANDLE_TO_IMAGE (raw_handle, ImageType ) \
811+ __spirv_ConvertHandleToImageINTEL<ImageType>(raw_handle)
812+
813+ #define CONVERT_HANDLE_TO_SAMPLED_IMAGE (raw_handle, NDims ) \
814+ __spirv_ConvertHandleToSampledImageINTEL< \
815+ typename sycl::detail::sampled_opencl_image_type< \
816+ detail::OCLImageTyRead<NDims>>::type>(raw_handle)
817+
818+ #define FETCH_UNSAMPLED_IMAGE (DataT, raw_handle, coords ) \
819+ __invoke__ImageRead<DataT>(raw_handle, coords)
820+ #else
821+ #define CONVERT_HANDLE_TO_IMAGE (raw_handle, ImageType ) raw_handle
822+
823+ #define CONVERT_HANDLE_TO_SAMPLED_IMAGE (raw_handle, NDims ) raw_handle
824+
825+ #define FETCH_UNSAMPLED_IMAGE (DataT, raw_handle, coords ) \
826+ __invoke__ImageFetch<DataT>(raw_handle, coords)
827+ #endif
828+
829+ #endif
830+
795831} // namespace detail
796832
797833/* *
@@ -826,15 +862,23 @@ DataT fetch_image(const unsampled_image_handle &imageHandle [[maybe_unused]],
826862
827863#ifdef __SYCL_DEVICE_ONLY__
828864 if constexpr (detail::is_recognized_standard_type<DataT>()) {
829- return __invoke__ImageFetch<DataT>(imageHandle.raw_handle , coords);
865+ return FETCH_UNSAMPLED_IMAGE (
866+ DataT,
867+ CONVERT_HANDLE_TO_IMAGE (imageHandle.raw_handle ,
868+ detail::OCLImageTyRead<coordSize>),
869+ coords);
870+
830871 } else {
831872 static_assert (sizeof (HintT) == sizeof (DataT),
832873 " When trying to read a user-defined type, HintT must be of "
833874 " the same size as the user-defined DataT." );
834875 static_assert (detail::is_recognized_standard_type<HintT>(),
835876 " HintT must always be a recognized standard type" );
836- return sycl::bit_cast<DataT>(
837- __invoke__ImageFetch<HintT>(imageHandle.raw_handle , coords));
877+ return sycl::bit_cast<DataT>(FETCH_UNSAMPLED_IMAGE (
878+ HintT,
879+ CONVERT_HANDLE_TO_IMAGE (imageHandle.raw_handle ,
880+ detail::OCLImageTyRead<coordSize>),
881+ coords));
838882 }
839883#else
840884 assert (false ); // Bindless images not yet implemented on host
@@ -907,10 +951,13 @@ DataT fetch_image(const sampled_image_handle &imageHandle [[maybe_unused]],
907951
908952#ifdef __SYCL_DEVICE_ONLY__
909953 if constexpr (detail::is_recognized_standard_type<DataT>()) {
910- return __invoke__SampledImageFetch<DataT>(imageHandle.raw_handle , coords);
954+ return __invoke__SampledImageFetch<DataT>(
955+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle.raw_handle , coordSize),
956+ coords);
911957 } else {
912- return sycl::bit_cast<DataT>(
913- __invoke__SampledImageFetch<HintT>(imageHandle.raw_handle , coords));
958+ return sycl::bit_cast<DataT>(__invoke__SampledImageFetch<HintT>(
959+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle.raw_handle , coordSize),
960+ coords));
914961 }
915962#else
916963 assert (false ); // Bindless images not yet implemented on host.
@@ -954,10 +1001,13 @@ DataT sample_image(const sampled_image_handle &imageHandle [[maybe_unused]],
9541001
9551002#ifdef __SYCL_DEVICE_ONLY__
9561003 if constexpr (detail::is_recognized_standard_type<DataT>()) {
957- return __invoke__ImageRead<DataT>(imageHandle.raw_handle , coords);
1004+ return __invoke__ImageRead<DataT>(
1005+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle.raw_handle , coordSize),
1006+ coords);
9581007 } else {
959- return sycl::bit_cast<DataT>(
960- __invoke__ImageRead<HintT>(imageHandle.raw_handle , coords));
1008+ return sycl::bit_cast<DataT>(__invoke__ImageRead<HintT>(
1009+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle.raw_handle , coordSize),
1010+ coords));
9611011 }
9621012#else
9631013 assert (false ); // Bindless images not yet implemented on host.
@@ -1026,15 +1076,18 @@ DataT sample_mipmap(const sampled_image_handle &imageHandle [[maybe_unused]],
10261076
10271077#ifdef __SYCL_DEVICE_ONLY__
10281078 if constexpr (detail::is_recognized_standard_type<DataT>()) {
1029- return __invoke__ImageReadLod<DataT>(imageHandle.raw_handle , coords, level);
1079+ return __invoke__ImageReadLod<DataT>(
1080+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle.raw_handle , coordSize),
1081+ coords, level);
10301082 } else {
10311083 static_assert (sizeof (HintT) == sizeof (DataT),
10321084 " When trying to read a user-defined type, HintT must be of "
10331085 " the same size as the user-defined DataT." );
10341086 static_assert (detail::is_recognized_standard_type<HintT>(),
10351087 " HintT must always be a recognized standard type" );
1036- return sycl::bit_cast<DataT>(
1037- __invoke__ImageReadLod<HintT>(imageHandle.raw_handle , coords, level));
1088+ return sycl::bit_cast<DataT>(__invoke__ImageReadLod<HintT>(
1089+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle.raw_handle , coordSize),
1090+ coords, level));
10381091 }
10391092#else
10401093 assert (false ); // Bindless images not yet implemented on host
@@ -1070,16 +1123,18 @@ DataT sample_mipmap(const sampled_image_handle &imageHandle [[maybe_unused]],
10701123
10711124#ifdef __SYCL_DEVICE_ONLY__
10721125 if constexpr (detail::is_recognized_standard_type<DataT>()) {
1073- return __invoke__ImageReadGrad<DataT>(imageHandle.raw_handle , coords, dX,
1074- dY);
1126+ return __invoke__ImageReadGrad<DataT>(
1127+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle.raw_handle , coordSize),
1128+ coords, dX, dY);
10751129 } else {
10761130 static_assert (sizeof (HintT) == sizeof (DataT),
10771131 " When trying to read a user-defined type, HintT must be of "
10781132 " the same size as the user-defined DataT." );
10791133 static_assert (detail::is_recognized_standard_type<HintT>(),
10801134 " HintT must always be a recognized standard type" );
1081- return sycl::bit_cast<DataT>(
1082- __invoke__ImageReadGrad<HintT>(imageHandle.raw_handle , coords, dX, dY));
1135+ return sycl::bit_cast<DataT>(__invoke__ImageReadGrad<HintT>(
1136+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle.raw_handle , coordSize),
1137+ coords, dX, dY));
10831138 }
10841139#else
10851140 assert (false ); // Bindless images not yet implemented on host
@@ -1224,16 +1279,20 @@ DataT fetch_image_array(const unsampled_image_handle &imageHandle
12241279
12251280#ifdef __SYCL_DEVICE_ONLY__
12261281 if constexpr (detail::is_recognized_standard_type<DataT>()) {
1227- return __invoke__ImageArrayFetch<DataT>(imageHandle.raw_handle , coords,
1228- arrayLayer);
1282+ return __invoke__ImageArrayFetch<DataT>(
1283+ CONVERT_HANDLE_TO_IMAGE (imageHandle.raw_handle ,
1284+ detail::OCLImageTyRead<coordSize>),
1285+ coords, arrayLayer);
12291286 } else {
12301287 static_assert (sizeof (HintT) == sizeof (DataT),
12311288 " When trying to fetch a user-defined type, HintT must be of "
12321289 " the same size as the user-defined DataT." );
12331290 static_assert (detail::is_recognized_standard_type<HintT>(),
12341291 " HintT must always be a recognized standard type" );
12351292 return sycl::bit_cast<DataT>(__invoke__ImageArrayFetch<HintT>(
1236- imageHandle.raw_handle , coords, arrayLayer));
1293+ CONVERT_HANDLE_TO_IMAGE (imageHandle.raw_handle ,
1294+ detail::OCLImageTyRead<coordSize>),
1295+ coords, arrayLayer));
12371296 }
12381297#else
12391298 assert (false ); // Bindless images not yet implemented on host.
@@ -1277,19 +1336,21 @@ DataT fetch_cubemap(const unsampled_image_handle &imageHandle,
12771336template <typename DataT, typename HintT = DataT>
12781337DataT sample_cubemap (const sampled_image_handle &imageHandle [[maybe_unused]],
12791338 const sycl::float3 &dirVec [[maybe_unused]]) {
1339+ [[maybe_unused]] constexpr size_t NDims = 2 ;
12801340
12811341#ifdef __SYCL_DEVICE_ONLY__
12821342 if constexpr (detail::is_recognized_standard_type<DataT>()) {
1283- return __invoke__ImageReadCubemap<DataT, uint64_t >(imageHandle. raw_handle ,
1284- dirVec);
1343+ return __invoke__ImageReadCubemap<DataT, uint64_t >(
1344+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle. raw_handle , NDims), dirVec);
12851345 } else {
12861346 static_assert (sizeof (HintT) == sizeof (DataT),
12871347 " When trying to read a user-defined type, HintT must be of "
12881348 " the same size as the user-defined DataT." );
12891349 static_assert (detail::is_recognized_standard_type<HintT>(),
12901350 " HintT must always be a recognized standard type" );
12911351 return sycl::bit_cast<DataT>(__invoke__ImageReadCubemap<HintT, uint64_t >(
1292- imageHandle.raw_handle , dirVec));
1352+ CONVERT_HANDLE_TO_SAMPLED_IMAGE (imageHandle.raw_handle , NDims),
1353+ dirVec));
12931354 }
12941355#else
12951356 assert (false ); // Bindless images not yet implemented on host
@@ -1318,12 +1379,17 @@ void write_image(unsampled_image_handle imageHandle [[maybe_unused]],
13181379
13191380#ifdef __SYCL_DEVICE_ONLY__
13201381 if constexpr (detail::is_recognized_standard_type<DataT>()) {
1321- __invoke__ImageWrite ((uint64_t )imageHandle.raw_handle , coords, color);
1382+ __invoke__ImageWrite (
1383+ CONVERT_HANDLE_TO_IMAGE (imageHandle.raw_handle ,
1384+ detail::OCLImageTyWrite<coordSize>),
1385+ coords, color);
13221386 } else {
13231387 // Convert DataT to a supported backend write type when user-defined type is
13241388 // passed
1325- __invoke__ImageWrite ((uint64_t )imageHandle.raw_handle , coords,
1326- detail::convert_color (color));
1389+ __invoke__ImageWrite (
1390+ CONVERT_HANDLE_TO_IMAGE (imageHandle.raw_handle ,
1391+ detail::OCLImageTyWrite<coordSize>),
1392+ coords, detail::convert_color (color));
13271393 }
13281394#else
13291395 assert (false ); // Bindless images not yet implemented on host
@@ -1354,13 +1420,17 @@ void write_image_array(unsampled_image_handle imageHandle [[maybe_unused]],
13541420
13551421#ifdef __SYCL_DEVICE_ONLY__
13561422 if constexpr (detail::is_recognized_standard_type<DataT>()) {
1357- __invoke__ImageArrayWrite (static_cast <uint64_t >(imageHandle.raw_handle ),
1358- coords, arrayLayer, color);
1423+ __invoke__ImageArrayWrite (
1424+ CONVERT_HANDLE_TO_IMAGE (imageHandle.raw_handle ,
1425+ detail::OCLImageTyRead<coordSize>),
1426+ coords, arrayLayer, color);
13591427 } else {
13601428 // Convert DataT to a supported backend write type when user-defined type is
13611429 // passed
1362- __invoke__ImageArrayWrite (static_cast <uint64_t >(imageHandle.raw_handle ),
1363- coords, arrayLayer, detail::convert_color (color));
1430+ __invoke__ImageArrayWrite (
1431+ CONVERT_HANDLE_TO_IMAGE (imageHandle.raw_handle ,
1432+ detail::OCLImageTyRead<coordSize>),
1433+ coords, arrayLayer, detail::convert_color (color));
13641434 }
13651435#else
13661436 assert (false ); // Bindless images not yet implemented on host.
0 commit comments