aboutsummaryrefslogtreecommitdiff
path: root/src/core/GLES_COMPUTE/cs_shaders/normalization_layer.cs
blob: f3cb52eb9e8d098191d8e467e421b5e8f8fc8bf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (c) 2017 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in;

#include "helpers_cs.h"

/** Apply cross map normalization and in map normalization
 *
 * @note Alpha parameter / norm_size should be given as a preprocessor argument using "#define COEFF x"
 * @note BETA parameter in the normalization equation should be given as a preprocessor argument using "#define BETA x"
 * @note KAPPA parameter in the normalization equation should be given as a preprocessor argument using "#define KAPPA x"
 * @note Number of elements on the right or left side to normalize across should be given as a preprocessor argument using "#define RADIUS x"
 *
 * @param[in]  src1_ptr   Pointer to the first source tensor. Supported data types: F32
 * @param[in]  src1_attrs The attributes of the first source tensor
 * @param[in]  src2_ptr   Pointer to the second source tensor. Supported data types: Same as @p src1_ptr
 * @param[in]  src2_attrs The attributes of the second source tensor
 * @param[out] dst_ptr    Pointer to the destination tensor. Supported data types: Same as @p src1_ptr
 * @param[in]  dst_attrs  The attributes of the destination tensor
 */
SHADER_PARAMS_DECLARATION
{
    Tensor3DAttributes src1_attrs;
    Tensor3DAttributes src2_attrs;
    Tensor3DAttributes dst_attrs;
};
TENSOR_DECLARATION(1, src1Buffer, float, src1_ptr, src1_shift, 2, readonly);
TENSOR_DECLARATION(2, src2Buffer, float, src2_ptr, src2_shift, 2, readonly);
TENSOR_DECLARATION(3, dstBuffer, float, dst_ptr, dst_shift, 2, writeonly);

#ifdef CROSS_MAP
void main(void)
{
    Tensor3DIterator src1_iter = CONVERT_TO_TENSOR3D_ITERATOR(src1_attrs, src1_shift);
    Tensor3DIterator src2_iter = CONVERT_TO_TENSOR3D_ITERATOR(src2_attrs, src2_shift);
    Tensor3DIterator dst_iter  = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);

    float acc = 0.0;

    int num_of_slices = int(gl_NumWorkGroups.z * gl_WorkGroupSize.z);
    int current_slice = int(gl_GlobalInvocationID.z);

    int left_slice  = max(current_slice - int(RADIUS), int(0));
    int right_slice = min(current_slice + int(RADIUS), int(num_of_slices - 1));

    for(int i = left_slice; i <= right_slice; i++)
    {
        acc += LOAD(src2_ptr, TENSOR3D_OFFSET(src2_iter, 0, 0, i - current_slice));
    }

    float normalized = pow(float(KAPPA) + float(COEFF) * acc, float(BETA));

    float normalized_pixel = (LOAD_CURRENT_ITEM(src1_ptr, src1_iter)) / normalized;

    STORE_CURRENT_ITEM(dst_ptr, dst_iter, normalized_pixel);
}

#elif defined(IN_MAP_1D)
void main(void)
{
    Tensor3DIterator src1_iter = CONVERT_TO_TENSOR3D_ITERATOR(src1_attrs, src1_shift);
    Tensor3DIterator src2_iter = CONVERT_TO_TENSOR3D_ITERATOR(src2_attrs, src2_shift);
    Tensor3DIterator dst_iter  = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);

    float acc = 0.0;

    int num_of_items_x = int(gl_NumWorkGroups.x * gl_WorkGroupSize.x);
    int current_pos    = int(gl_GlobalInvocationID.x);

    int left_pos  = max(current_pos - int(RADIUS), int(0));
    int right_pos = min(current_pos + int(RADIUS), int(num_of_items_x + -1));

    for(int i = left_pos; i <= right_pos; i++)
    {
        acc += LOAD(src2_ptr, TENSOR3D_OFFSET(src2_iter, i - current_pos, 0, 0));
    }

    float normalized = pow(float(KAPPA) + float(COEFF) * acc, float(BETA));

    float normalized_pixel = (LOAD_CURRENT_ITEM(src1_ptr, src1_iter)) / normalized;

    STORE_CURRENT_ITEM(dst_ptr, dst_iter, normalized_pixel);
}
#endif /*CROSS_MAP*/