| 203 | } |
| 204 | |
| 205 | Status AllocateTensorMemory(const CLContext& context, const CLDevice& device, |
| 206 | int width, int height, int channels, |
| 207 | DataType data_type, TensorStorageType storage_type, |
| 208 | CLMemory* result) { |
| 209 | switch (storage_type) { |
| 210 | case TensorStorageType::BUFFER: { |
| 211 | const size_t data_size = |
| 212 | width * height * AlignByN(channels, 4) * SizeOf(data_type); |
| 213 | cl_int error_code; |
| 214 | cl_mem memory = clCreateBuffer(context.context(), CL_MEM_READ_WRITE, |
| 215 | data_size, nullptr, &error_code); |
| 216 | if (!memory) { |
| 217 | return UnknownError( |
| 218 | absl::StrCat("Failed to allocate device memory with clCreateBuffer", |
| 219 | CLErrorCodeToString(error_code))); |
| 220 | } |
| 221 | *result = CLMemory(memory, true); |
| 222 | return OkStatus(); |
| 223 | } |
| 224 | case TensorStorageType::TEXTURE_2D: { |
| 225 | cl_image_desc desc; |
| 226 | desc.image_type = CL_MEM_OBJECT_IMAGE2D; |
| 227 | desc.image_width = width; |
| 228 | desc.image_height = height * IntegralDivideRoundUp(channels, 4); |
| 229 | desc.image_depth = 0; |
| 230 | desc.image_row_pitch = 0; |
| 231 | desc.image_slice_pitch = 0; |
| 232 | desc.num_mip_levels = 0; |
| 233 | desc.num_samples = 0; |
| 234 | desc.buffer = nullptr; |
| 235 | |
| 236 | cl_image_format format; |
| 237 | format.image_channel_order = CL_RGBA; |
| 238 | format.image_channel_data_type = ToImageChannelType(data_type); |
| 239 | |
| 240 | cl_int error_code; |
| 241 | cl_mem memory = CreateImage2DLegacy(context.context(), CL_MEM_READ_WRITE, |
| 242 | &format, &desc, nullptr, &error_code); |
| 243 | if (error_code != CL_SUCCESS) { |
| 244 | return UnknownError( |
| 245 | absl::StrCat("Failed to create Texture2D (clCreateImage)", |
| 246 | CLErrorCodeToString(error_code))); |
| 247 | } |
| 248 | |
| 249 | *result = CLMemory(memory, true); |
| 250 | return OkStatus(); |
| 251 | } |
| 252 | case TensorStorageType::TEXTURE_ARRAY: { |
| 253 | cl_image_desc desc; |
| 254 | desc.image_type = CL_MEM_OBJECT_IMAGE2D_ARRAY; |
| 255 | desc.image_width = width; |
| 256 | desc.image_height = height; |
| 257 | desc.image_depth = 0; |
| 258 | int layers_count = IntegralDivideRoundUp(channels, 4); |
| 259 | // Adreno bug. b/131099086 |
| 260 | if (layers_count == 1 && !device.SupportsOneLayerTextureArray()) { |
| 261 | layers_count = 2; |
| 262 | } |
no test coverage detected