1 // 2 // Copyright 2021 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 // cl_utils.cpp: Helper functions for the CL front end 7 8 #include "libANGLE/cl_utils.h" 9 10 #include "libANGLE/renderer/CLExtensions.h" 11 12 namespace cl 13 { 14 GetChannelCount(cl_channel_order channelOrder)15size_t GetChannelCount(cl_channel_order channelOrder) 16 { 17 size_t count = 0u; 18 switch (channelOrder) 19 { 20 case CL_R: 21 case CL_A: 22 case CL_LUMINANCE: 23 case CL_INTENSITY: 24 case CL_DEPTH: 25 count = 1u; 26 break; 27 case CL_RG: 28 case CL_RA: 29 case CL_Rx: 30 count = 2u; 31 break; 32 case CL_RGB: 33 case CL_RGx: 34 case CL_sRGB: 35 count = 3u; 36 break; 37 case CL_RGBA: 38 case CL_ARGB: 39 case CL_BGRA: 40 case CL_ABGR: 41 case CL_RGBx: 42 case CL_sRGBA: 43 case CL_sBGRA: 44 case CL_sRGBx: 45 count = 4u; 46 break; 47 default: 48 break; 49 } 50 return count; 51 } 52 GetElementSize(const cl_image_format & image_format)53size_t GetElementSize(const cl_image_format &image_format) 54 { 55 size_t size = 0u; 56 switch (image_format.image_channel_data_type) 57 { 58 case CL_SNORM_INT8: 59 case CL_UNORM_INT8: 60 case CL_SIGNED_INT8: 61 case CL_UNSIGNED_INT8: 62 size = GetChannelCount(image_format.image_channel_order); 63 break; 64 case CL_SNORM_INT16: 65 case CL_UNORM_INT16: 66 case CL_SIGNED_INT16: 67 case CL_UNSIGNED_INT16: 68 case CL_HALF_FLOAT: 69 size = 2u * GetChannelCount(image_format.image_channel_order); 70 break; 71 case CL_SIGNED_INT32: 72 case CL_UNSIGNED_INT32: 73 case CL_FLOAT: 74 size = 4u * GetChannelCount(image_format.image_channel_order); 75 break; 76 case CL_UNORM_SHORT_565: 77 case CL_UNORM_SHORT_555: 78 size = 2u; 79 break; 80 case CL_UNORM_INT_101010: 81 case CL_UNORM_INT_101010_2: 82 size = 4u; 83 break; 84 default: 85 break; 86 } 87 return size; 88 } 89 IsValidImageFormat(const cl_image_format * imageFormat,const rx::CLExtensions & extensions)90bool IsValidImageFormat(const cl_image_format *imageFormat, const rx::CLExtensions &extensions) 91 { 92 if (imageFormat == nullptr) 93 { 94 return false; 95 } 96 switch (imageFormat->image_channel_order) 97 { 98 case CL_R: 99 case CL_A: 100 case CL_LUMINANCE: 101 case CL_INTENSITY: 102 case CL_RG: 103 case CL_RA: 104 case CL_RGB: 105 case CL_RGBA: 106 case CL_ARGB: 107 case CL_BGRA: 108 break; 109 110 case CL_Rx: 111 case CL_RGx: 112 case CL_RGBx: 113 if (extensions.version < CL_MAKE_VERSION(1, 1, 0)) 114 { 115 return false; 116 } 117 break; 118 119 case CL_ABGR: 120 case CL_sRGB: 121 case CL_sRGBA: 122 case CL_sBGRA: 123 case CL_sRGBx: 124 if (extensions.version < CL_MAKE_VERSION(2, 0, 0)) 125 { 126 return false; 127 } 128 break; 129 130 case CL_DEPTH: 131 // CL_DEPTH can only be used if channel data type = CL_UNORM_INT16 or CL_FLOAT. 132 if (imageFormat->image_channel_data_type != CL_UNORM_INT16 && 133 imageFormat->image_channel_data_type != CL_FLOAT) 134 { 135 return false; 136 } 137 if (!extensions.khrDepthImages) 138 { 139 return false; 140 } 141 break; 142 143 default: 144 return false; 145 } 146 switch (imageFormat->image_channel_data_type) 147 { 148 case CL_SNORM_INT8: 149 case CL_SNORM_INT16: 150 case CL_UNORM_INT8: 151 case CL_UNORM_INT16: 152 case CL_SIGNED_INT8: 153 case CL_SIGNED_INT16: 154 case CL_SIGNED_INT32: 155 case CL_UNSIGNED_INT8: 156 case CL_UNSIGNED_INT16: 157 case CL_UNSIGNED_INT32: 158 case CL_HALF_FLOAT: 159 case CL_FLOAT: 160 break; 161 162 case CL_UNORM_SHORT_565: 163 case CL_UNORM_SHORT_555: 164 case CL_UNORM_INT_101010: 165 if (imageFormat->image_channel_order != CL_RGB && 166 imageFormat->image_channel_order != CL_RGBx) 167 { 168 return false; 169 } 170 break; 171 172 case CL_UNORM_INT_101010_2: 173 if (extensions.version < CL_MAKE_VERSION(2, 1, 0) || 174 imageFormat->image_channel_order != CL_RGBA) 175 { 176 return false; 177 } 178 break; 179 180 default: 181 return false; 182 } 183 return true; 184 } 185 186 } // namespace cl 187