Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017, 2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +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 "helpers.h" |
| 25 | #include "warp_helpers.h" |
| 26 | |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 27 | #ifndef DEPTH_OUT |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | /** Performs a remapping of an input image to an output given two remapping image using nearest neighbor as interpolation. |
| 29 | * |
| 30 | * This kernel performs remapping with this method of pixel coordinate translation: |
| 31 | * out(x,y) = in(mapx(x,y), mapy(x,y)); |
| 32 | * |
| 33 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 34 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 35 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 36 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 37 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 38 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 39 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 40 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 41 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 42 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 43 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 44 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 45 | * @param[in] mapx_ptr Pointer to the x remapping image. Supported data types: F32. |
| 46 | * @param[in] mapx_stride_x Stride of the remapping image in X dimension (in bytes) |
| 47 | * @param[in] mapx_step_x mapx_stride_x * number of elements along X processed per work item (in bytes) |
| 48 | * @param[in] mapx_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 49 | * @param[in] mapx_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 50 | * @param[in] mapx_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 51 | * @param[in] mapy_ptr Pointer to the x remapping image. Supported data types: F32. |
| 52 | * @param[in] mapy_stride_x Stride of the remapping image in X dimension (in bytes) |
| 53 | * @param[in] mapy_step_x mapy_stride_x * number of elements along X processed per work item (in bytes) |
| 54 | * @param[in] mapy_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 55 | * @param[in] mapy_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 56 | * @param[in] mapy_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 57 | * @param[in] width Width of the input image |
| 58 | * @param[in] height Height of the input image |
| 59 | */ |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 60 | __kernel void remap_nearest_neighbour_nchw( |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 61 | IMAGE_DECLARATION(in), |
| 62 | IMAGE_DECLARATION(out), |
| 63 | IMAGE_DECLARATION(mapx), |
| 64 | IMAGE_DECLARATION(mapy), |
| 65 | const float width, |
| 66 | const float height) |
| 67 | { |
| 68 | Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in); |
| 69 | Image out = CONVERT_TO_IMAGE_STRUCT(out); |
| 70 | Image mapx = CONVERT_TO_IMAGE_STRUCT(mapx); |
| 71 | Image mapy = CONVERT_TO_IMAGE_STRUCT(mapy); |
| 72 | |
| 73 | float4 mapx_coords = vload4(0, (__global float *)mapx.ptr); |
| 74 | float4 mapy_coords = vload4(0, (__global float *)mapy.ptr); |
| 75 | float8 map_coords = (float8)(mapx_coords.s0, mapy_coords.s0, mapx_coords.s1, mapy_coords.s1, |
| 76 | mapx_coords.s2, mapy_coords.s2, mapx_coords.s3, mapy_coords.s3); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | |
| 78 | vstore4(read_texels4(&in, convert_int8(clamp_to_border(map_coords, width, height))), 0, out.ptr); |
| 79 | } |
| 80 | |
| 81 | /** Performs a remapping of an input image to an output given two remapping image using bilinear as interpolation. |
| 82 | * |
| 83 | * This kernel performs remapping with this method of pixel coordinate translation: |
| 84 | * out(x,y) = in(mapx(x,y), mapy(x,y)); |
| 85 | * |
| 86 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8. |
| 87 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 88 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 89 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 90 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 91 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
| 92 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8. |
| 93 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 94 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 95 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 96 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 97 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 98 | * @param[in] mapx_ptr Pointer to the x remapping image. Supported data types: F32. |
| 99 | * @param[in] mapx_stride_x Stride of the remapping image in X dimension (in bytes) |
| 100 | * @param[in] mapx_step_x mapx_stride_x * number of elements along X processed per work item (in bytes) |
| 101 | * @param[in] mapx_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 102 | * @param[in] mapx_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 103 | * @param[in] mapx_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 104 | * @param[in] mapy_ptr Pointer to the x remapping image. Supported data types: F32. |
| 105 | * @param[in] mapy_stride_x Stride of the remapping image in X dimension (in bytes) |
| 106 | * @param[in] mapy_step_x mapy_stride_x * number of elements along X processed per work item (in bytes) |
| 107 | * @param[in] mapy_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 108 | * @param[in] mapy_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 109 | * @param[in] mapy_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 110 | * @param[in] width Width of the input image |
| 111 | * @param[in] height Height of the input image |
| 112 | */ |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 113 | __kernel void remap_bilinear_nchw( |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 114 | IMAGE_DECLARATION(in), |
| 115 | IMAGE_DECLARATION(out), |
| 116 | IMAGE_DECLARATION(mapx), |
| 117 | IMAGE_DECLARATION(mapy), |
| 118 | const float width, |
| 119 | const float height) |
| 120 | { |
| 121 | Image in = CONVERT_TO_IMAGE_STRUCT_NO_STEP(in); |
| 122 | Image out = CONVERT_TO_IMAGE_STRUCT(out); |
| 123 | Image mapx = CONVERT_TO_IMAGE_STRUCT(mapx); |
| 124 | Image mapy = CONVERT_TO_IMAGE_STRUCT(mapy); |
| 125 | |
| 126 | float4 mapx_coords = vload4(0, (__global float *)mapx.ptr); |
| 127 | float4 mapy_coords = vload4(0, (__global float *)mapy.ptr); |
| 128 | float8 map_coords = (float8)(mapx_coords.s0, mapy_coords.s0, mapx_coords.s1, mapy_coords.s1, |
| 129 | mapx_coords.s2, mapy_coords.s2, mapx_coords.s3, mapy_coords.s3); |
| 130 | |
| 131 | vstore4(bilinear_interpolate(&in, clamp_to_border(map_coords, width, height), width, height), 0, out.ptr); |
| 132 | } |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 133 | #else // DEPTH_OUT |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 134 | /** Performs a remapping of an input image to an output given two remapping image using nearest neighbor as interpolation. |
| 135 | * Also applies constant border value, "border_val", if "CONSTANT_BORDER" is set. |
| 136 | * |
| 137 | * This kernel performs remapping with this method of pixel coordinate translation: |
| 138 | * out(x,y) = in(mapx(x,y), mapy(x,y)); |
| 139 | * |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 140 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8,F16. |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 141 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 142 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 143 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 144 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 145 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 146 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8,F16. |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 147 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 148 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 149 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 150 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 151 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 152 | * @param[in] mapx_ptr Pointer to the x remapping image. Supported data types: F32. |
| 153 | * @param[in] mapx_stride_x Stride of the remapping image in X dimension (in bytes) |
| 154 | * @param[in] mapx_step_x mapx_stride_x * number of elements along X processed per work item (in bytes) |
| 155 | * @param[in] mapx_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 156 | * @param[in] mapx_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 157 | * @param[in] mapx_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 158 | * @param[in] mapy_ptr Pointer to the x remapping image. Supported data types: F32. |
| 159 | * @param[in] mapy_stride_x Stride of the remapping image in X dimension (in bytes) |
| 160 | * @param[in] mapy_step_x mapy_stride_x * number of elements along X processed per work item (in bytes) |
| 161 | * @param[in] mapy_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 162 | * @param[in] mapy_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 163 | * @param[in] mapy_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 164 | * @param[in] width Width of the input image |
| 165 | * @param[in] height Height of the input image |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 166 | * @param[in] border_val Value to use for border around input tensor when in CONSTANT border is selected |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 167 | */ |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 168 | __kernel void remap_nearest_neighbour_nhwc( |
| 169 | TENSOR4D_DECLARATION(in), |
| 170 | TENSOR4D_DECLARATION(out), |
| 171 | TENSOR4D_DECLARATION(mapx), |
| 172 | TENSOR4D_DECLARATION(mapy), |
| 173 | const float width, |
| 174 | const float height |
| 175 | #ifdef CONSTANT_BORDER |
| 176 | , |
| 177 | const DATA_TYPE border_val |
| 178 | #endif // CONSTANT_BORDER |
| 179 | ) |
| 180 | { |
| 181 | Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0); |
| 182 | Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT); |
| 183 | Tensor4D mapx = CONVERT_TO_TENSOR4D_STRUCT(mapx, DEPTH_OUT); |
| 184 | Tensor4D mapy = CONVERT_TO_TENSOR4D_STRUCT(mapy, DEPTH_OUT); |
| 185 | |
| 186 | float mapx_coord = (float) * (__global float *)mapx.ptr; |
| 187 | float mapy_coord = (float) * (__global float *)mapy.ptr; |
| 188 | |
| 189 | #ifdef CONSTANT_BORDER |
| 190 | if(mapx_coord < 0 || mapx_coord > width - 1 || mapy_coord < 0 || mapy_coord > height - 1) |
| 191 | { |
| 192 | *((__global DATA_TYPE *)out.ptr) = border_val; |
| 193 | return; |
| 194 | } |
| 195 | #else // CONSTANT_BORDER |
| 196 | mapx_coord = clamp(mapx_coord, 0.0f, width - 1); |
| 197 | mapy_coord = clamp(mapy_coord, 0.0f, height - 1); |
| 198 | #endif // CONSTANT_BORDER |
| 199 | *((__global DATA_TYPE *)out.ptr) = *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(mapx_coord), convert_int(mapy_coord), (get_global_id(2) / DEPTH_OUT))); |
| 200 | } |
| 201 | |
| 202 | /** Performs a remapping of an input image to an output given two remapping image using bilinear as interpolation. |
| 203 | * Also applies constant border value, "border_val", if "CONSTANT_BORDER" is set. |
| 204 | * |
| 205 | * This kernel performs remapping with this method of pixel coordinate translation: |
| 206 | * out(x,y) = in(mapx(x,y), mapy(x,y)); |
| 207 | * |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 208 | * @param[in] in_ptr Pointer to the source image. Supported data types: U8,F16. |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 209 | * @param[in] in_stride_x Stride of the source image in X dimension (in bytes) |
| 210 | * @param[in] in_step_x in_stride_x * number of elements along X processed per work item (in bytes) |
| 211 | * @param[in] in_stride_y Stride of the source image in Y dimension (in bytes) |
| 212 | * @param[in] in_step_y in_stride_y * number of elements along Y processed per work item (in bytes) |
| 213 | * @param[in] in_offset_first_element_in_bytes Offset of the first element in the source image |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 214 | * @param[out] out_ptr Pointer to the destination image. Supported data types: U8,F16. |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 215 | * @param[in] out_stride_x Stride of the destination image in X dimension (in bytes) |
| 216 | * @param[in] out_step_x out_stride_x * number of elements along X processed per work item (in bytes) |
| 217 | * @param[in] out_stride_y Stride of the destination image in Y dimension (in bytes) |
| 218 | * @param[in] out_step_y out_stride_y * number of elements along Y processed per work item (in bytes) |
| 219 | * @param[in] out_offset_first_element_in_bytes Offset of the first element in the destination image |
| 220 | * @param[in] mapx_ptr Pointer to the x remapping image. Supported data types: F32. |
| 221 | * @param[in] mapx_stride_x Stride of the remapping image in X dimension (in bytes) |
| 222 | * @param[in] mapx_step_x mapx_stride_x * number of elements along X processed per work item (in bytes) |
| 223 | * @param[in] mapx_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 224 | * @param[in] mapx_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 225 | * @param[in] mapx_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 226 | * @param[in] mapy_ptr Pointer to the x remapping image. Supported data types: F32. |
| 227 | * @param[in] mapy_stride_x Stride of the remapping image in X dimension (in bytes) |
| 228 | * @param[in] mapy_step_x mapy_stride_x * number of elements along X processed per work item (in bytes) |
| 229 | * @param[in] mapy_stride_y Stride of the remapping image in Y dimension (in bytes) |
| 230 | * @param[in] mapy_step_y mapy_stride_y * number of elements along Y processed per work item (in bytes) |
| 231 | * @param[in] mapy_offset_first_element_in_bytes Offset of the first element in the remapping image |
| 232 | * @param[in] width Width of the input image |
| 233 | * @param[in] height Height of the input image |
Freddie Liardet | ef5aac6 | 2021-06-10 16:45:58 +0100 | [diff] [blame] | 234 | * @param[in] border_val Value to use for border around input tensor when in CONSTANT border is selected |
Frederick Liardet | 36dff9f | 2021-04-22 21:13:21 +0100 | [diff] [blame] | 235 | */ |
| 236 | __kernel void remap_bilinear_nhwc( |
| 237 | TENSOR4D_DECLARATION(in), |
| 238 | TENSOR4D_DECLARATION(out), |
| 239 | TENSOR4D_DECLARATION(mapx), |
| 240 | TENSOR4D_DECLARATION(mapy), |
| 241 | const float width, |
| 242 | const float height |
| 243 | #ifdef CONSTANT_BORDER |
| 244 | , |
| 245 | const DATA_TYPE border_val |
| 246 | #endif // CONSTANT_BORDER |
| 247 | ) |
| 248 | { |
| 249 | Tensor4D in = CONVERT_TO_TENSOR4D_STRUCT_NO_STEP(in, 0); |
| 250 | Tensor4D out = CONVERT_TO_TENSOR4D_STRUCT(out, DEPTH_OUT); |
| 251 | Tensor4D mapx = CONVERT_TO_TENSOR4D_STRUCT(mapx, DEPTH_OUT); |
| 252 | Tensor4D mapy = CONVERT_TO_TENSOR4D_STRUCT(mapy, DEPTH_OUT); |
| 253 | |
| 254 | float mapx_coord = (float) * (__global float *)mapx.ptr; |
| 255 | float mapy_coord = (float) * (__global float *)mapy.ptr; |
| 256 | |
| 257 | #ifdef CONSTANT_BORDER |
| 258 | if(mapx_coord < 0 || mapx_coord > width - 1 || mapy_coord < 0 || mapy_coord > height - 1) |
| 259 | { |
| 260 | *((__global DATA_TYPE *)out.ptr) = border_val; |
| 261 | return; |
| 262 | } |
| 263 | #endif // CONSTANT_BORDER |
| 264 | |
| 265 | const float new_xf = floor(mapx_coord); |
| 266 | const float new_yf = floor(mapy_coord); |
| 267 | const float clamped_x = clamp(new_xf, 0.0f, width - 1); |
| 268 | const float clamped_x1 = clamp(new_xf + 1, 0.0f, width - 1); |
| 269 | const float clamped_y = clamp(new_yf, 0.0f, height - 1); |
| 270 | const float clamped_y1 = clamp(new_yf + 1, 0.0f, height - 1); |
| 271 | |
| 272 | float4 ins = (float4)(*((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))), |
| 273 | *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y), (get_global_id(2) / DEPTH_OUT))), |
| 274 | *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT))), |
| 275 | *((__global DATA_TYPE *)tensor4D_offset(&in, get_global_id(0), convert_int(clamped_x1), convert_int(clamped_y1), (get_global_id(2) / DEPTH_OUT)))); |
| 276 | |
| 277 | const float a = mapx_coord - new_xf; |
| 278 | const float b = 1.f - a; |
| 279 | const float a1 = mapy_coord - new_yf; |
| 280 | const float b1 = 1.f - a1; |
| 281 | const float fr = ((ins.s0 * b * b1) + (ins.s1 * a * b1) + (ins.s2 * b * a1) + (ins.s3 * a * a1)); |
| 282 | |
| 283 | *((__global DATA_TYPE *)out.ptr) = CONVERT(fr, DATA_TYPE); |
| 284 | } |
| 285 | |
| 286 | #endif // DEPTH_OUT |