Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 1 | /* |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [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 | |
| 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 | |
Frank Lei | 1bfc784 | 2018-01-04 13:02:40 +0800 | [diff] [blame] | 29 | // We DO have to use highp for DATA_TYPE_FP16 float here to calculate the coordinates of source tensor. float is highp by default, but we still write it down here to make it more clearly, and mediump is only used for src/dst tensor in shader body. |
| 30 | precision highp float; |
| 31 | |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 32 | /** Performs an affine transformation on an tensor interpolating with the NEAREAST NEIGHBOUR method. Input and output are single channel FP16. |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 33 | * |
| 34 | * @param[in] src_ptr Pointer to the source tensor. Supported data types: FP16. |
| 35 | * @param[in] src_attrs The attributes of the source tensor |
| 36 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: FP16. (Must be the same as the input) |
| 37 | * @param[in] dst_attrs The attributes of the destination tensor |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 38 | * @param[in] input_width Input tensor width |
| 39 | * @param[in] input_height Input tensor height |
Frank Lei | 1bfc784 | 2018-01-04 13:02:40 +0800 | [diff] [blame] | 40 | * @param[in] scale The scale factor along x/y dimension |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 41 | */ |
| 42 | SHADER_PARAMS_DECLARATION |
| 43 | { |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 44 | Tensor3DAttributes src_attrs; |
| 45 | Tensor3DAttributes dst_attrs; |
| 46 | float input_width; |
| 47 | float input_height; |
| 48 | vec2 scale; |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | #if defined(DATA_TYPE_FP16) |
| 52 | #if defined(SCALE_NEAREST_GENERIC) |
| 53 | TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly); |
| 54 | TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, writeonly); |
| 55 | |
| 56 | vec4[2] transform_nearest(vec2 coord, vec2 scale) |
| 57 | { |
| 58 | vec4 in_x_coords = vec4(coord.x, 1.f + coord.x, 2.f + coord.x, 3.f + coord.x); |
| 59 | |
| 60 | vec4[2] t; |
| 61 | t[0] = (in_x_coords + (vec4(0.5f))) * scale.x; |
| 62 | t[1] = vec4((coord.y + 0.5f) * scale.y); |
| 63 | |
| 64 | return t; |
| 65 | } |
| 66 | |
| 67 | vec4[2] clamp_to_border_with_size(vec4[2] coords, float width, float height, float border_size) |
| 68 | { |
| 69 | vec4[2] c; |
| 70 | c[0] = clamp(coords[0], 0.0f - border_size, width - 1.f + border_size); |
| 71 | c[1] = clamp(coords[1], 0.0f - border_size, height - 1.f + border_size); |
| 72 | |
| 73 | return c; |
| 74 | } |
| 75 | |
| 76 | void main() |
| 77 | { |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 78 | Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift); |
| 79 | Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift); |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 80 | |
Frank Lei | 1bfc784 | 2018-01-04 13:02:40 +0800 | [diff] [blame] | 81 | vec4[2] tc = clamp_to_border_with_size(transform_nearest(vec2(gl_GlobalInvocationID.x << uint(2), gl_GlobalInvocationID.y), scale), input_width, input_height, float(BORDER_SIZE)); |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 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 | { |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 88 | uint offset_in_bytes = tensor3D_offset_in_bytes(src_iter, int(tc[0][i]), int(tc[1][i]), int(gl_GlobalInvocationID.z)); |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 89 | |
Frank Lei | 1bfc784 | 2018-01-04 13:02:40 +0800 | [diff] [blame] | 90 | s = LOAD_UNPACK2_HALF(src_ptr, uint(offset_in_bytes >> src_shift)); |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 91 | |
Frank Lei | 1bfc784 | 2018-01-04 13:02:40 +0800 | [diff] [blame] | 92 | if(offset_in_bytes % uint(4) == uint(0)) |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 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 | { |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 110 | Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift); |
| 111 | Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift); |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 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 | |
Frank Lei | 4406fd6 | 2018-02-01 14:47:14 +0800 | [diff] [blame] | 118 | s = LOAD_UNPACK4_HALF(src_ptr, TENSOR3D_OFFSET(src_iter, int(tc[0]), int(tc[1]), int(gl_GlobalInvocationID.z))); |
Frank Lei | 57a150a | 2017-12-19 10:14:57 +0800 | [diff] [blame] | 119 | |
| 120 | d[0] = vec4(s.x, s.x, s.y, s.y); |
| 121 | d[1] = vec4(s.z, s.z, s.w, s.w); |
| 122 | |
| 123 | STORE_PACK8_CURRENT_ITEM_HALF(dst_ptr, dst_iter, d); |
| 124 | } |
| 125 | #endif /* SCALE_NEAREST_GENERIC */ |
| 126 | |
| 127 | #else /* DATA_TYPE_FP16 */ |
| 128 | #error Data type not supported |
| 129 | #endif /* DATA_TYPE_FP16 */ |