Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2016, 2017 ARM Limited. |
| 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 | |
| 25 | layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in; |
| 26 | |
| 27 | #include "helpers_cs.h" |
| 28 | |
| 29 | /** Performs an affine transformation on an image interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel FP16. |
| 30 | * |
| 31 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: FP16. |
| 32 | * @param[in] src_attrs The attributes of the source tensor |
| 33 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: FP16. (Must be the same as the input) |
| 34 | * @param[in] dst_attrs The attributes of the destination tensor |
| 35 | * @param[in] input_width Input image width |
| 36 | * @param[in] input_height Input image height |
| 37 | * @param[in] scale_x The scale factor along x dimension |
| 38 | * @param[in] scale_y The scale factor along y dimension |
| 39 | */ |
| 40 | SHADER_PARAMS_DECLARATION |
| 41 | { |
| 42 | ImageAttributes src_attrs; |
| 43 | ImageAttributes dst_attrs; |
| 44 | float input_width; |
| 45 | float input_height; |
| 46 | float scale_x; |
| 47 | float scale_y; |
| 48 | }; |
| 49 | |
| 50 | #if defined(DATA_TYPE_FP16) |
| 51 | #if defined(SCALE_NEAREST_GENERIC) |
| 52 | TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly); |
| 53 | TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, writeonly); |
| 54 | |
| 55 | vec4[2] transform_nearest(vec2 coord, vec2 scale) |
| 56 | { |
| 57 | vec4 in_x_coords = vec4(coord.x, 1.f + coord.x, 2.f + coord.x, 3.f + coord.x); |
| 58 | |
| 59 | vec4[2] t; |
| 60 | t[0] = (in_x_coords + (vec4(0.5f))) * scale.x; |
| 61 | t[1] = vec4((coord.y + 0.5f) * scale.y); |
| 62 | |
| 63 | return t; |
| 64 | } |
| 65 | |
| 66 | vec4[2] clamp_to_border_with_size(vec4[2] coords, float width, float height, float border_size) |
| 67 | { |
| 68 | vec4[2] c; |
| 69 | c[0] = clamp(coords[0], 0.0f - border_size, width - 1.f + border_size); |
| 70 | c[1] = clamp(coords[1], 0.0f - border_size, height - 1.f + border_size); |
| 71 | |
| 72 | return c; |
| 73 | } |
| 74 | |
| 75 | void main() |
| 76 | { |
| 77 | ImageIterator src_iter = CONVERT_TO_IMAGE_ITERATOR_NO_STEP(src_attrs, src_shift); |
| 78 | ImageIterator dst_iter = CONVERT_TO_IMAGE_ITERATOR(dst_attrs, dst_shift); |
| 79 | |
| 80 | vec2 r = vec2(scale_x, scale_y); |
| 81 | vec4[2] tc = clamp_to_border_with_size(transform_nearest(vec2(gl_GlobalInvocationID.x << uint(2), gl_GlobalInvocationID.y), r), input_width, input_height, float(BORDER_SIZE)); |
| 82 | |
| 83 | mediump vec2 s = vec2(0.0f); |
| 84 | mediump vec4 d = vec4(0.0f); |
| 85 | |
| 86 | for(int i = 0; i < 4; i++) |
| 87 | { |
| 88 | uint offset = image_offset_in_bytes(src_iter, int(tc[0][i]), int(tc[1][i])); |
| 89 | |
| 90 | s = LOAD_UNPACK2_HALF(src_ptr, uint(offset >> src_shift)); |
| 91 | |
| 92 | if(offset % uint(4) == uint(0)) |
| 93 | { |
| 94 | d[i] = s.x; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | d[i] = s.y; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | STORE_PACK4_CURRENT_ITEM_HALF(dst_ptr, dst_iter, d); |
| 103 | } |
| 104 | #elif defined(SCALE_NEAREST_8X) /* SCALE_NEAREST_GENERIC */ |
| 105 | TENSOR_DECLARATION(1, srcBuffer, uvec2, src_ptr, src_shift, 3, readonly); |
| 106 | TENSOR_DECLARATION(2, dstBuffer, uvec4, dst_ptr, dst_shift, 4, writeonly); |
| 107 | |
| 108 | void main() |
| 109 | { |
| 110 | ImageIterator src_iter = CONVERT_TO_IMAGE_ITERATOR_NO_STEP(src_attrs, src_shift); |
| 111 | ImageIterator dst_iter = CONVERT_TO_IMAGE_ITERATOR(dst_attrs, dst_shift); |
| 112 | |
| 113 | uvec2 tc = uvec2(gl_GlobalInvocationID.x << uint(2), gl_GlobalInvocationID.y >> uint(1)); |
| 114 | |
| 115 | mediump vec4 s = vec4(0.0f); |
| 116 | mediump vec4[2] d; |
| 117 | |
| 118 | uint offset = image_offset_in_bytes(src_iter, int(tc[0]), int(tc[1])); |
| 119 | s = LOAD_UNPACK4_HALF(src_ptr, uint(offset >> src_shift)); |
| 120 | |
| 121 | d[0] = vec4(s.x, s.x, s.y, s.y); |
| 122 | d[1] = vec4(s.z, s.z, s.w, s.w); |
| 123 | |
| 124 | STORE_PACK8_CURRENT_ITEM_HALF(dst_ptr, dst_iter, d); |
| 125 | } |
| 126 | #endif /* SCALE_NEAREST_GENERIC */ |
| 127 | |
| 128 | #else /* DATA_TYPE_FP16 */ |
| 129 | #error Data type not supported |
| 130 | #endif /* DATA_TYPE_FP16 */ |