Merge remote-tracking branch 'origin/2.4' into merge-2.4
[profile/ivi/opencv.git] / modules / ocl / src / opencl / imgproc_convolve.cl
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Jiang Liyuan, jlyuan001.good@163.com
19 //
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
22 //
23 //   * Redistribution's of source code must retain the above copyright notice,
24 //     this list of conditions and the following disclaimer.
25 //
26 //   * Redistribution's in binary form must reproduce the above copyright notice,
27 //     this list of conditions and the following disclaimer in the documentation
28 //     and/or other materials provided with the distribution.
29 //
30 //   * The name of the copyright holders may not be used to endorse or promote products
31 //     derived from this software without specific prior written permission.
32 //
33 // This software is provided by the copyright holders and contributors as is and
34 // any express or implied warranties, including, but not limited to, the implied
35 // warranties of merchantability and fitness for a particular purpose are disclaimed.
36 // In no event shall the Intel Corporation or contributors be liable for any direct,
37 // indirect, incidental, special, exemplary, or consequential damages
38 // (including, but not limited to, procurement of substitute goods or services;
39 // loss of use, data, or profits; or business interruption) however caused
40 // and on any theory of liability, whether in contract, strict liability,
41 // or tort (including negligence or otherwise) arising in any way out of
42 // the use of this software, even if advised of the possibility of such damage.
43 //
44 //M*/
45
46 #ifdef DOUBLE_SUPPORT
47 #ifdef cl_amd_fp64
48 #pragma OPENCL EXTENSION cl_amd_fp64:enable
49 #elif defined (cl_khr_fp64)
50 #pragma OPENCL EXTENSION cl_khr_fp64:enable
51 #endif
52 #endif
53
54 /************************************** convolve **************************************/
55
56 __kernel void convolve_D5(__global float *src, __global float *temp1, __global float *dst,
57                           int rows, int cols, int src_step, int dst_step,int k_step, int kWidth, int kHeight,
58                           int src_offset, int dst_offset, int koffset)
59 {
60     __local float smem[16 + 2 * 8][16 + 2 * 8];
61
62     int x = get_local_id(0);
63     int y = get_local_id(1);
64     int gx = get_global_id(0);
65     int gy = get_global_id(1);
66
67             // x | x 0 | 0
68             // -----------
69             // x | x 0 | 0
70             // 0 | 0 0 | 0
71             // -----------
72             // 0 | 0 0 | 0
73     smem[y][x] = src[min(max(gy - 8, 0), rows - 1) * src_step + min(max(gx - 8, 0), cols - 1) + src_offset];
74
75             // 0 | 0 x | x
76             // -----------
77             // 0 | 0 x | x
78             // 0 | 0 0 | 0
79             // -----------
80             // 0 | 0 0 | 0
81     smem[y][x + 16] = src[min(max(gy - 8, 0), rows - 1) * src_step + min(gx + 8, cols - 1) + src_offset];
82
83             // 0 | 0 0 | 0
84             // -----------
85             // 0 | 0 0 | 0
86             // x | x 0 | 0
87             // -----------
88             // x | x 0 | 0
89     smem[y + 16][x] = src[min(gy + 8, rows - 1) * src_step + min(max(gx - 8, 0), cols - 1) + src_offset];
90
91             // 0 | 0 0 | 0
92             // -----------
93             // 0 | 0 0 | 0
94             // 0 | 0 x | x
95             // -----------
96             // 0 | 0 x | x
97     smem[y + 16][x + 16] = src[min(gy + 8, rows - 1) * src_step + min(gx + 8, cols - 1) + src_offset];
98
99     barrier(CLK_LOCAL_MEM_FENCE);
100
101     if (gx < cols && gy < rows)
102     {
103         float res = 0;
104
105         for (int i = 0; i < kHeight; ++i)
106             for (int j = 0; j < kWidth; ++j)
107                 res += smem[y + 8 - kHeight / 2 + i][x + 8 - kWidth / 2 + j] * temp1[i * k_step + j + koffset];
108
109         dst[gy * dst_step + gx + dst_offset] = res;
110     }
111 }