@@ -423,12 +423,26 @@ template <typename ChannelType>
423423void convertReadData (const vec<ChannelType, 4 > PixelData,
424424 const image_channel_type ImageChannelType,
425425 vec<cl_half, 4 > &RetData) {
426-
426+ vec<cl_float, 4 > RetDataFloat;
427427 switch (ImageChannelType) {
428428 case image_channel_type::snorm_int8:
429+ // max(-1.0f, (half)c / 127.0f)
430+ RetDataFloat = (PixelData.template convert <cl_float>()) / 127 .0f ;
431+ RetDataFloat = cl::sycl::fmax (RetDataFloat, -1 );
432+ break ;
429433 case image_channel_type::snorm_int16:
434+ // max(-1.0f, (half)c / 32767.0f)
435+ RetDataFloat = (PixelData.template convert <cl_float>()) / 32767 .0f ;
436+ RetDataFloat = cl::sycl::fmax (RetDataFloat, -1 );
437+ break ;
430438 case image_channel_type::unorm_int8:
439+ // (half)c / 255.0f
440+ RetDataFloat = (PixelData.template convert <cl_float>()) / 255 .0f ;
441+ break ;
431442 case image_channel_type::unorm_int16:
443+ // (half)c / 65535.0f
444+ RetDataFloat = (PixelData.template convert <cl_float>()) / 65535 .0f ;
445+ break ;
432446 case image_channel_type::unorm_short_565:
433447 case image_channel_type::unorm_short_555:
434448 case image_channel_type::unorm_int_101010:
@@ -452,7 +466,7 @@ void convertReadData(const vec<ChannelType, 4> PixelData,
452466 PI_INVALID_VALUE);
453467 case image_channel_type::fp16:
454468 RetData = PixelData.template convert <cl_half>();
455- break ;
469+ return ;
456470 case image_channel_type::fp32:
457471 throw cl::sycl::invalid_parameter_error (
458472 " Datatype to read - cl_half4 is incompatible with the "
@@ -461,6 +475,7 @@ void convertReadData(const vec<ChannelType, 4> PixelData,
461475 default :
462476 break ;
463477 }
478+ RetData = RetDataFloat.template convert <cl_half>();
464479}
465480
466481// Converts data to write into appropriate datatype based on the channel of the
@@ -629,12 +644,20 @@ template <typename ChannelType>
629644vec<ChannelType, 4 >
630645convertWriteData (const vec<cl_half, 4 > WriteData,
631646 const image_channel_type ImageChannelType) {
632-
647+ vec<cl_float, 4 > WriteDataFloat = WriteData. convert <cl_float>();
633648 switch (ImageChannelType) {
634649 case image_channel_type::snorm_int8:
650+ // convert_char_sat_rte(h * 127.0f)
651+ return processFloatDataToPixel<ChannelType>(WriteDataFloat, 127 .0f );
635652 case image_channel_type::snorm_int16:
653+ // convert_short_sat_rte(h * 32767.0f)
654+ return processFloatDataToPixel<ChannelType>(WriteDataFloat, 32767 .0f );
636655 case image_channel_type::unorm_int8:
656+ // convert_uchar_sat_rte(h * 255.0f)
657+ return processFloatDataToPixel<ChannelType>(WriteDataFloat, 255 .0f );
637658 case image_channel_type::unorm_int16:
659+ // convert_ushort_sat_rte(h * 65535.0f)
660+ return processFloatDataToPixel<ChannelType>(WriteDataFloat, 65535 .0f );
638661 case image_channel_type::unorm_short_565:
639662 case image_channel_type::unorm_short_555:
640663 case image_channel_type::unorm_int_101010:
@@ -994,10 +1017,13 @@ DataT ReadPixelDataLinearFiltMode(const cl_int8 CoordValues,
9941017// ImgChannelType.
9951018// Convert to DataT as per conversion rules in section 8.3 in OpenCL Spec.
9961019//
997- // TODO:
998- // Extend support for Step2 and Step3 for Linear Filtering Mode.
999- // Extend support to find out of bounds Coordinates and return appropriate
1000- // value based on Addressing Mode.
1020+ // TODO: Add additional check for half datatype read.
1021+ // Based on OpenCL spec 2.0:
1022+ // "The read_imageh calls that take integer coordinates must use a sampler with
1023+ // filter mode set to CLK_FILTER_NEAREST, normalized coordinates set to
1024+ // CLK_NORMALIZED_COORDS_FALSE and addressing mode set to
1025+ // CLK_ADDRESS_CLAMP_TO_EDGE, CLK_ADDRESS_CLAMP or CLK_ADDRESS_NONE; otherwise
1026+ // the values returned are undefined."
10011027
10021028template <typename CoordT, typename DataT>
10031029DataT imageReadSamplerHostImpl (const CoordT &Coords, const sampler &Smpl,
0 commit comments