arm_compute v18.02
[platform/upstream/armcl.git] / src / core / CL / cl_kernels / convolution_rectangle.cl
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 #include "convolution3x3.cl"
25 #include "convolution5x5.cl"
26 #include "convolution7x7.cl"
27 #include "convolution9x9.cl"
28 #include "helpers.h"
29
30 #define MAT_INDEX(i) MAT##i
31
32 #ifndef DATA_TYPE
33 #define DATA_TYPE short
34 #endif /* DATA_TYPE */
35
36 #ifndef COMPUTE_TYPE
37 #define COMPUTE_TYPE int
38 #endif /* COMPUTE_TYPE */
39
40 #ifndef DATA_TYPE_OUT
41 #define DATA_TYPE_OUT uchar
42 #endif /* DATA_TYPE_OUT */
43
44 #ifndef DYNAMIC_MATRIX_CONVOLUTION
45
46 /** Apply a rectangle matrix to a single channel U8 input image and output a single channel image including borders
47  *
48  * @attention The matrix coefficients(MAT0, MAT1, ... MAT80, SCALE), MATRIX_WIDTH, MATRIX_HEIGHT, COMPUTE_TYPE, DATA_TYPE, DATA_TYPE_OUT need to be passed at compile time:\n
49  * e.g. -DMAT0=0 -DMAT1=1, ... -DMAT80=80, -DSCALE=6, -DMATRIX_WIDTH=3, -DMATRIX_HEIGHT=5, -DCOMPUTE_TYPE=int, -DDATA_TYPE=int, -DDATA_TYPE_OUT=int
50  *
51  * @param[in]  src_ptr                           Pointer to the source image. Supported data types: U8
52  * @param[in]  src_stride_x                      Stride of the source image in X dimension (in bytes)
53  * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
54  * @param[in]  src_stride_y                      Stride of the source image in Y dimension (in bytes)
55  * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
56  * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source image
57  * @param[out] dst_ptr                           Pointer to the destination image. Supported data types: U8, S16
58  * @param[in]  dst_stride_x                      Stride of the destination image in X dimension (in bytes)
59  * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
60  * @param[in]  dst_stride_y                      Stride of the destination image in Y dimension (in bytes)
61  * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
62  * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination image
63  */
64 __kernel void convolution_rectangle(
65     IMAGE_DECLARATION(src),
66     IMAGE_DECLARATION(dst))
67 {
68     Image src = CONVERT_TO_IMAGE_STRUCT(src);
69     Image dst = CONVERT_TO_IMAGE_STRUCT(dst);
70
71     short matrix_coeff[81] =
72     {
73         MAT0, MAT1, MAT2, MAT3, MAT4, MAT5, MAT6, MAT7, MAT8,
74         MAT9, MAT10, MAT11, MAT12, MAT13, MAT14, MAT15, MAT16, MAT17,
75         MAT18, MAT19, MAT20, MAT21, MAT22, MAT23, MAT24, MAT25, MAT26,
76         MAT27, MAT28, MAT29, MAT30, MAT31, MAT32, MAT33, MAT34, MAT35,
77         MAT36, MAT37, MAT38, MAT39, MAT40, MAT41, MAT42, MAT43, MAT44,
78         MAT45, MAT46, MAT47, MAT48, MAT49, MAT50, MAT51, MAT52, MAT53,
79         MAT54, MAT55, MAT56, MAT57, MAT58, MAT59, MAT60, MAT61, MAT62,
80         MAT63, MAT64, MAT65, MAT66, MAT67, MAT68, MAT69, MAT70, MAT71,
81         MAT72, MAT73, MAT74, MAT75, MAT76, MAT77, MAT78, MAT79, MAT80
82     };
83
84     VEC_DATA_TYPE(DATA_TYPE, 8)
85     pixels = (VEC_DATA_TYPE(DATA_TYPE, 8))0;
86
87     for(int i = 0; i < MATRIX_HEIGHT; i++)
88     {
89 #if MATRIX_WIDTH == 3
90         pixels += convolution1x3(offset(&src, -1, -(MATRIX_HEIGHT / 2) + i), matrix_coeff[0 + i * 3], matrix_coeff[1 + i * 3],
91                                  matrix_coeff[2 + i * 3]);
92 #endif /* MATRIX_WIDTH */
93
94 #if MATRIX_WIDTH == 5
95         pixels += convolution1x5(offset(&src, -2, -(MATRIX_HEIGHT / 2) + i), matrix_coeff[0 + i * 5], matrix_coeff[1 + i * 5],
96                                  matrix_coeff[2 + i * 5], matrix_coeff[3 + i * 5], matrix_coeff[4 + i * 5]);
97 #endif /* MATRIX_WIDTH */
98
99 #if MATRIX_WIDTH == 7
100         pixels += convolution1x7(offset(&src, -3, -(MATRIX_HEIGHT / 2) + i), matrix_coeff[0 + i * 7], matrix_coeff[1 + i * 7],
101                                  matrix_coeff[2 + i * 7], matrix_coeff[3 + i * 7], matrix_coeff[4 + i * 7],
102                                  matrix_coeff[5 + i * 7], matrix_coeff[6 + i * 7]);
103 #endif /* MATRIX_WIDTH */
104
105 #if MATRIX_WIDTH == 9
106         pixels += convolution1x9(offset(&src, -4, -(MATRIX_HEIGHT / 2) + i), matrix_coeff[0 + i * 9], matrix_coeff[1 + i * 9],
107                                  matrix_coeff[2 + i * 9], matrix_coeff[3 + i * 9], matrix_coeff[4 + i * 9],
108                                  matrix_coeff[5 + i * 9], matrix_coeff[6 + i * 9], matrix_coeff[7 + i * 9], matrix_coeff[8 + i * 9]);
109 #endif /* MATRIX_WIDTH */
110     }
111
112     pixels /= (VEC_DATA_TYPE(DATA_TYPE, 8))SCALE;
113
114     // Store the result as is in dst
115     vstore8(CONVERT_SAT(pixels, VEC_DATA_TYPE(DATA_TYPE_OUT, 8)), 0, ((__global DATA_TYPE_OUT *)dst.ptr));
116 }
117
118 #endif /* not DYNAMIC_MATRIX_CONVOLUTION */