Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 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.h" |
| 28 | |
| 29 | layout(std140) uniform shader_params |
| 30 | { |
| 31 | TENSOR3D_PARAM_DECLARATION(src1); |
| 32 | TENSOR3D_PARAM_DECLARATION(src2); |
| 33 | TENSOR3D_PARAM_DECLARATION(dst); |
| 34 | }; |
| 35 | |
| 36 | BUFFER_DECLARATION(src1, 1, float, readonly); |
| 37 | BUFFER_DECLARATION(src2, 2, float, readonly); |
| 38 | BUFFER_DECLARATION(dst, 3, float, writeonly); |
| 39 | |
| 40 | #ifdef CROSS_MAP |
| 41 | /** Apply cross map normalization. |
| 42 | * |
| 43 | * @note Alpha parameter / norm_size should be given as a preprocessor argument using "#define COEFF x" |
| 44 | * @note BETA parameter in the normalization equation should be given as a preprocessor argument using "#define BETA x" |
| 45 | * @note KAPPA parameter in the normalization equation should be given as a preprocessor argument using "#define KAPPA x" |
| 46 | * @note Number of elements on the right or left side to normalize across should be given as a preprocessor argument using "#define RADIUS x" |
| 47 | * |
| 48 | * @param[in] src1_ptr Pointer to the first source tensor. Supported data types: F32 |
| 49 | * @param[in] src1_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 50 | * @param[in] src1_step_x src1_stride_x * number of elements along X processed per workitem(in bytes) |
| 51 | * @param[in] src1_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 52 | * @param[in] src1_step_y src1_stride_y * number of elements along Y processed per workitem(in bytes) |
| 53 | * @param[in] src1_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 54 | * @param[in] src1_step_z src1_stride_z * number of elements along Z processed per workitem(in bytes) |
| 55 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 56 | * @param[in] src2_ptr Pointer to the second source tensor. Supported data types: Same as @p src1_ptr |
| 57 | * @param[in] src2_stride_x Stride of the second source tensor in X dimension (in bytes) |
| 58 | * @param[in] src2_step_x src2_stride_x * number of elements along X processed per workitem(in bytes) |
| 59 | * @param[in] src2_stride_y Stride of the second source tensor in Y dimension (in bytes) |
| 60 | * @param[in] src2_step_y src2_stride_y * number of elements along Y processed per workitem(in bytes) |
| 61 | * @param[in] src2_stride_z Stride of the second source tensor in Z dimension (in bytes) |
| 62 | * @param[in] src2_step_z src2_stride_z * number of elements along Z processed per workitem(in bytes) |
| 63 | * @param[in] src2_offset_first_element_in_bytes The offset of the second element in the second source tensor |
| 64 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: Same as @p src1_ptr |
| 65 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 66 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 67 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 68 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 69 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 70 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 71 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 72 | */ |
| 73 | void main(void) |
| 74 | { |
| 75 | Tensor3D src1 = CONVERT_TO_TENSOR3D_STRUCT(src1); |
| 76 | Tensor3D src2 = CONVERT_TO_TENSOR3D_STRUCT(src2); |
| 77 | Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst); |
| 78 | |
| 79 | float acc = 0.0; |
| 80 | |
| 81 | int num_of_slices = int(gl_NumWorkGroups.z * gl_WorkGroupSize.z); |
| 82 | int current_slice = int(gl_GlobalInvocationID.z); |
| 83 | |
| 84 | int left_slice = max(current_slice - int(RADIUS), int(0)); |
| 85 | int right_slice = min(current_slice + int(RADIUS), int(num_of_slices - 1)); |
| 86 | |
| 87 | for(int i = left_slice; i <= right_slice; i++) |
| 88 | { |
| 89 | acc += src2_ptr[tensor3D_offset(src2, 0, 0, i - current_slice)]; |
| 90 | } |
| 91 | |
| 92 | float normalized = pow(float(KAPPA) + float(COEFF) * acc, float(BETA)); |
| 93 | |
| 94 | float normalized_pixel = (src1_ptr[src1.current_offset]) / normalized; |
| 95 | |
| 96 | dst_ptr[dst.current_offset] = normalized_pixel; |
| 97 | } |
| 98 | |
| 99 | #elif defined(IN_MAP_1D) |
| 100 | /** Apply in map normalization. |
| 101 | * |
| 102 | * @note Alpha parameter / norm_size should be given as a preprocessor argument using "#define COEFF x" |
| 103 | * @note BETA parameter in the normalization equation should be given as a preprocessor argument using "#define BETA x" |
| 104 | * @note KAPPA parameter in the normalization equation should be given as a preprocessor argument using "#define KAPPA x" |
| 105 | * @note Number of elements on the right or left side to normalize across should be given as a preprocessor argument using "#define RADIUS x" |
| 106 | * |
| 107 | * @param[in] src1_ptr Pointer to the first source tensor. Supported data types: F32 |
| 108 | * @param[in] src1_stride_x Stride of the first source tensor in X dimension (in bytes) |
| 109 | * @param[in] src1_step_x src1_stride_x * number of elements along X processed per workitem(in bytes) |
| 110 | * @param[in] src1_stride_y Stride of the first source tensor in Y dimension (in bytes) |
| 111 | * @param[in] src1_step_y src1_stride_y * number of elements along Y processed per workitem(in bytes) |
| 112 | * @param[in] src1_stride_z Stride of the first source tensor in Z dimension (in bytes) |
| 113 | * @param[in] src1_step_z src1_stride_z * number of elements along Z processed per workitem(in bytes) |
| 114 | * @param[in] src1_offset_first_element_in_bytes The offset of the first element in the first source tensor |
| 115 | * @param[in] src2_ptr Pointer to the second source tensor. Supported data types: Same as @p src1_ptr |
| 116 | * @param[in] src2_stride_x Stride of the second source tensor in X dimension (in bytes) |
| 117 | * @param[in] src2_step_x src2_stride_x * number of elements along X processed per workitem(in bytes) |
| 118 | * @param[in] src2_stride_y Stride of the second source tensor in Y dimension (in bytes) |
| 119 | * @param[in] src2_step_y src2_stride_y * number of elements along Y processed per workitem(in bytes) |
| 120 | * @param[in] src2_stride_z Stride of the second source tensor in Z dimension (in bytes) |
| 121 | * @param[in] src2_step_z src2_stride_z * number of elements along Z processed per workitem(in bytes) |
| 122 | * @param[in] src2_offset_first_element_in_bytes The offset of the second element in the second source tensor |
| 123 | * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: Same as @p src1_ptr |
| 124 | * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes) |
| 125 | * @param[in] dst_step_x dst_stride_x * number of elements along X processed per workitem(in bytes) |
| 126 | * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes) |
| 127 | * @param[in] dst_step_y dst_stride_y * number of elements along Y processed per workitem(in bytes) |
| 128 | * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes) |
| 129 | * @param[in] dst_step_z dst_stride_z * number of elements along Z processed per workitem(in bytes) |
| 130 | * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor |
| 131 | */ |
| 132 | void main(void) |
| 133 | { |
| 134 | Tensor3D src1 = CONVERT_TO_TENSOR3D_STRUCT(src1); |
| 135 | Tensor3D src2 = CONVERT_TO_TENSOR3D_STRUCT(src2); |
| 136 | Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst); |
| 137 | |
| 138 | float acc = 0.0; |
| 139 | |
| 140 | int num_of_items_x = int(gl_NumWorkGroups.x * gl_WorkGroupSize.x); |
| 141 | int current_pos = int(gl_GlobalInvocationID.x); |
| 142 | |
| 143 | int left_pos = max(current_pos - int(RADIUS), int(0)); |
| 144 | int right_pos = min(current_pos + int(RADIUS), int(num_of_items_x + -1)); |
| 145 | |
| 146 | for(int i = left_pos; i <= right_pos; i++) |
| 147 | { |
| 148 | acc += src2_ptr[tensor3D_offset(src2, i - current_pos, 0, 0)]; |
| 149 | } |
| 150 | |
| 151 | float normalized = pow(float(KAPPA) + float(COEFF) * acc, float(BETA)); |
| 152 | |
| 153 | float normalized_pixel = (src1_ptr[src1.current_offset]) / normalized; |
| 154 | |
| 155 | dst_ptr[dst.current_offset] = normalized_pixel; |
| 156 | } |
| 157 | #endif /*CROSS_MAP*/ |