Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2020 Arm Limited. |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/Error.h" |
| 25 | #include "arm_compute/core/Types.h" |
| 26 | |
| 27 | #include "src/core/CL/CLUtils.h" |
| 28 | |
Gian Marco Iodice | 6f93134 | 2020-09-15 14:17:41 +0100 | [diff] [blame] | 29 | cl::Image2D arm_compute::create_image2d_from_buffer(const cl::Context &ctx, const cl::Buffer &buffer, const TensorShape &shape2d, DataType data_type, size_t image_row_pitch) |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 30 | { |
Gian Marco Iodice | 6f93134 | 2020-09-15 14:17:41 +0100 | [diff] [blame] | 31 | cl_channel_type cl_data_type; |
| 32 | |
| 33 | switch(data_type) |
| 34 | { |
| 35 | case DataType::F32: |
| 36 | cl_data_type = CL_FLOAT; |
| 37 | break; |
| 38 | case DataType::F16: |
| 39 | cl_data_type = CL_HALF_FLOAT; |
| 40 | break; |
| 41 | default: |
| 42 | ARM_COMPUTE_ERROR("Data type not support with OpenCL image2d"); |
| 43 | } |
| 44 | |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 45 | cl_mem cl_image; |
| 46 | cl_int err = CL_SUCCESS; |
| 47 | |
Gian Marco Iodice | 6f93134 | 2020-09-15 14:17:41 +0100 | [diff] [blame] | 48 | const cl_image_format format = { CL_RGBA, cl_data_type }; |
Gian Marco Iodice | 781cba7 | 2020-06-19 16:56:57 +0100 | [diff] [blame] | 49 | |
| 50 | cl_image_desc desc; |
| 51 | memset(&desc, 0, sizeof(desc)); |
| 52 | desc.image_type = CL_MEM_OBJECT_IMAGE2D; |
| 53 | desc.mem_object = buffer(); |
| 54 | desc.image_row_pitch = image_row_pitch; |
| 55 | desc.image_width = shape2d[0]; |
| 56 | desc.image_height = shape2d[1]; |
| 57 | |
| 58 | cl_image = clCreateImage(ctx(), CL_MEM_READ_ONLY, &format, &desc, nullptr, &err); |
| 59 | |
| 60 | ARM_COMPUTE_UNUSED(err); |
| 61 | ARM_COMPUTE_ERROR_ON_MSG(err != CL_SUCCESS, "Error during the creation of CL image from buffer"); |
| 62 | |
| 63 | return cl::Image2D(cl_image); |
| 64 | } |