1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2010-2013, Advanced Micro Devices, Inc., all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of the copyright holders may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors as is and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #ifdef BORDER_REPLICATE
43 //BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh
44 #define ADDR_L(i, l_edge, r_edge) ((i) < (l_edge) ? (l_edge) : (i))
45 #define ADDR_R(i, r_edge, addr) ((i) >= (r_edge) ? (r_edge)-1 : (addr))
46 #define ADDR_H(i, t_edge, b_edge) ((i) < (t_edge) ? (t_edge) :(i))
47 #define ADDR_B(i, b_edge, addr) ((i) >= (b_edge) ? (b_edge)-1 :(addr))
51 //BORDER_REFLECT: fedcba|abcdefgh|hgfedcb
52 #define ADDR_L(i, l_edge, r_edge) ((i) < (l_edge) ? -(i)-1 : (i))
53 #define ADDR_R(i, r_edge, addr) ((i) >= (r_edge) ? -(i)-1+((r_edge)<<1) : (addr))
54 #define ADDR_H(i, t_edge, b_edge) ((i) < (t_edge) ? -(i)-1 : (i))
55 #define ADDR_B(i, b_edge, addr) ((i) >= (b_edge) ? -(i)-1+((b_edge)<<1) : (addr))
58 #ifdef BORDER_REFLECT_101
59 //BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba
60 #define ADDR_L(i, l_edge, r_edge) ((i) < (l_edge) ? -(i) : (i))
61 #define ADDR_R(i, r_edge, addr) ((i) >= (r_edge) ? -(i)-2+((r_edge)<<1) : (addr))
62 #define ADDR_H(i, t_edge, b_edge) ((i) < (t_edge) ? -(i) : (i))
63 #define ADDR_B(i, b_edge, addr) ((i) >= (b_edge) ? -(i)-2+((b_edge)<<1) : (addr))
66 //blur function does not support BORDER_WRAP
68 //BORDER_WRAP: cdefgh|abcdefgh|abcdefg
69 #define ADDR_L(i, l_edge, r_edge) ((i) < (l_edge) ? (i)+(r_edge) : (i))
70 #define ADDR_R(i, r_edge, addr) ((i) >= (r_edge) ? (i)-(r_edge) : (addr))
71 #define ADDR_H(i, t_edge, b_edge) ((i) < (t_edge) ? (i)+(b_edge) : (i))
72 #define ADDR_B(i, b_edge, addr) ((i) >= (b_edge) ? (i)-(b_edge) : (addr))
75 #ifdef EXTRA_EXTRAPOLATION // border > src image size
76 #ifdef BORDER_CONSTANT
78 #elif defined BORDER_REPLICATE
79 #define EXTRAPOLATE(x, y, minX, minY, maxX, maxY) \
81 x = max(min(x, maxX - 1), minX); \
82 y = max(min(y, maxY - 1), minY); \
84 #elif defined BORDER_WRAP
85 #define EXTRAPOLATE(x, y, minX, minY, maxX, maxY) \
88 x -= ((x - maxX + 1) / maxX) * maxX; \
92 y -= ((y - maxY + 1) / maxY) * maxY; \
96 #elif defined(BORDER_REFLECT) || defined(BORDER_REFLECT_101)
97 #define EXTRAPOLATE_(x, y, minX, minY, maxX, maxY, delta) \
99 if (maxX - minX == 1) \
105 x = -(x - minX) - 1 + delta; \
107 x = maxX - 1 - (x - maxX) - delta; \
109 while (x >= maxX || x < minX); \
111 if (maxY - minY == 1) \
117 y = -(y - minY) - 1 + delta; \
119 y = maxY - 1 - (y - maxY) - delta; \
121 while (y >= maxY || y < minY); \
123 #ifdef BORDER_REFLECT
124 #define EXTRAPOLATE(x, y, minX, minY, maxX, maxY) EXTRAPOLATE_(x, y, minX, minY, maxX, maxY, 0)
125 #elif defined(BORDER_REFLECT_101)
126 #define EXTRAPOLATE(x, y, minX, minY, maxX, maxY) EXTRAPOLATE_(x, y, minX, minY, maxX, maxY, 1)
129 #error No extrapolation method
132 #define EXTRAPOLATE(x, y, minX, minY, maxX, maxY) \
134 int _row = y - minY, _col = x - minX; \
135 _row = ADDR_H(_row, 0, maxY - minY); \
136 _row = ADDR_B(_row, maxY - minY, _row); \
139 _col = ADDR_L(_col, 0, maxX - minX); \
140 _col = ADDR_R(_col, maxX - minX, _col); \
147 #pragma OPENCL EXTENSION cl_amd_fp64:enable
148 #elif defined (cl_khr_fp64)
149 #pragma OPENCL EXTENSION cl_khr_fp64:enable
151 #define FPTYPE double
152 #define CONVERT_TO_FPTYPE CAT(convert_double, VEC_SIZE)
155 #define CONVERT_TO_FPTYPE CAT(convert_float, VEC_SIZE)
159 #define BASE_TYPE uchar
160 #elif DATA_DEPTH == 1
161 #define BASE_TYPE char
162 #elif DATA_DEPTH == 2
163 #define BASE_TYPE ushort
164 #elif DATA_DEPTH == 3
165 #define BASE_TYPE short
166 #elif DATA_DEPTH == 4
167 #define BASE_TYPE int
168 #elif DATA_DEPTH == 5
169 #define BASE_TYPE float
170 #elif DATA_DEPTH == 6
171 #define BASE_TYPE double
176 #define __CAT(x, y) x##y
177 #define CAT(x, y) __CAT(x, y)
181 #define ushort1 ushort
185 #define double1 double
187 #define convert_uchar1_sat_rte convert_uchar_sat_rte
188 #define convert_char1_sat_rte convert_char_sat_rte
189 #define convert_ushort1_sat_rte convert_ushort_sat_rte
190 #define convert_short1_sat_rte convert_short_sat_rte
191 #define convert_int1_sat_rte convert_int_sat_rte
192 #define convert_float1
193 #define convert_double1
195 #if DATA_DEPTH == 5 || DATA_DEPTH == 6
196 #define CONVERT_TO_TYPE CAT(CAT(convert_, BASE_TYPE), VEC_SIZE)
198 #define CONVERT_TO_TYPE CAT(CAT(CAT(convert_, BASE_TYPE), VEC_SIZE), _sat_rte)
201 #define VEC_SIZE DATA_CHAN
203 #define VEC_TYPE CAT(BASE_TYPE, VEC_SIZE)
204 #define TYPE VEC_TYPE
206 #define SCALAR_TYPE CAT(FPTYPE, VEC_SIZE)
208 #define INTERMEDIATE_TYPE CAT(FPTYPE, VEC_SIZE)
217 #define DEBUG_ONLY(x) x
218 #define ASSERT(condition) do { if (!(condition)) { printf("BUG in boxFilter kernel (global=%d,%d): " #condition "\n", get_global_id(0), get_global_id(1)); } } while (0)
220 #define DEBUG_ONLY(x) (void)0
221 #define ASSERT(condition) (void)0
225 inline INTERMEDIATE_TYPE readSrcPixel(int2 pos, __global TYPE *src, const unsigned int srcStepBytes, const struct RectCoords srcCoords
226 #ifdef BORDER_CONSTANT
227 , SCALAR_TYPE borderValue
231 #ifdef BORDER_ISOLATED
232 if(pos.x >= srcCoords.x1 && pos.y >= srcCoords.y1 && pos.x < srcCoords.x2 && pos.y < srcCoords.y2)
234 if(pos.x >= 0 && pos.y >= 0 && pos.x < srcCoords.x2 && pos.y < srcCoords.y2)
237 __global TYPE* ptr = (__global TYPE*)((__global char*)src + pos.x * sizeof(TYPE) + pos.y * srcStepBytes);
238 return CONVERT_TO_FPTYPE(*ptr);
242 #ifdef BORDER_CONSTANT
245 int selected_col = pos.x;
246 int selected_row = pos.y;
248 EXTRAPOLATE(selected_col, selected_row,
249 #ifdef BORDER_ISOLATED
250 srcCoords.x1, srcCoords.y1,
254 srcCoords.x2, srcCoords.y2
257 // debug border mapping
258 //printf("pos=%d,%d --> %d, %d\n", pos.x, pos.y, selected_col, selected_row);
260 pos = (int2)(selected_col, selected_row);
261 if(pos.x >= 0 && pos.y >= 0 && pos.x < srcCoords.x2 && pos.y < srcCoords.y2)
263 __global TYPE* ptr = (__global TYPE*)((__global char*)src + pos.x * sizeof(TYPE) + pos.y * srcStepBytes);
264 return CONVERT_TO_FPTYPE(*ptr);
269 DEBUG_ONLY(printf("BUG in boxFilter kernel\n"));
270 return (FPTYPE)(0.0f);
276 // INPUT PARAMETER: BLOCK_SIZE_Y (via defines)
279 __attribute__((reqd_work_group_size(LOCAL_SIZE, 1, 1)))
280 void filter2D(__global TYPE *src, const unsigned int srcStepBytes, const int4 srcRC,
281 __global TYPE *dst, const unsigned int dstStepBytes, const int4 dstRC,
282 #ifdef BORDER_CONSTANT
283 SCALAR_TYPE borderValue,
285 __constant FPTYPE* kernelData // transposed: [KERNEL_SIZE_X][KERNEL_SIZE_Y2_ALIGNED]
288 const struct RectCoords srcCoords = {srcRC.s0, srcRC.s1, srcRC.s2, srcRC.s3}; // for non-isolated border: offsetX, offsetY, wholeX, wholeY
289 struct RectCoords dstCoords = {dstRC.s0, dstRC.s1, dstRC.s2, dstRC.s3};
291 const int local_id = get_local_id(0);
292 const int x = local_id + (LOCAL_SIZE - (KERNEL_SIZE_X - 1)) * get_group_id(0) - ANCHOR_X;
293 const int y = get_global_id(1) * BLOCK_SIZE_Y;
295 INTERMEDIATE_TYPE data[KERNEL_SIZE_Y];
296 __local INTERMEDIATE_TYPE sumOfCols[LOCAL_SIZE];
298 int2 srcPos = (int2)(srcCoords.x1 + x, srcCoords.y1 + y - ANCHOR_Y);
300 int2 pos = (int2)(dstCoords.x1 + x, dstCoords.y1 + y);
301 __global TYPE* dstPtr = (__global TYPE*)((__global char*)dst + pos.x * sizeof(TYPE) + pos.y * dstStepBytes); // Pointer can be out of bounds!
302 bool writeResult = (local_id >= ANCHOR_X && local_id < LOCAL_SIZE - (KERNEL_SIZE_X - 1 - ANCHOR_X) &&
303 pos.x >= dstCoords.x1 && pos.x < dstCoords.x2);
306 bool readAllpixels = true;
307 int sy_index = 0; // current index in data[] array
309 dstCoords.y2 = min(dstCoords.y2, pos.y + BLOCK_SIZE_Y);
311 pos.y < dstCoords.y2;
313 dstPtr = (__global TYPE*)((__global char*)dstPtr + dstStepBytes))
316 ASSERT(pos.y < dstCoords.y2);
320 int sy = readAllpixels ? 0 : -1; sy < (readAllpixels ? KERNEL_SIZE_Y : 0);
322 int sy = 0, sy_index = 0; sy < KERNEL_SIZE_Y;
326 data[sy + sy_index] = readSrcPixel(srcPos, src, srcStepBytes, srcCoords
327 #ifdef BORDER_CONSTANT
333 INTERMEDIATE_TYPE total_sum = 0;
334 for (int sx = 0; sx < KERNEL_SIZE_X; sx++)
337 __constant FPTYPE* k = &kernelData[KERNEL_SIZE_Y2_ALIGNED * sx
339 + KERNEL_SIZE_Y - sy_index
342 INTERMEDIATE_TYPE tmp_sum = 0;
343 for (int sy = 0; sy < KERNEL_SIZE_Y; sy++)
345 tmp_sum += data[sy] * k[sy];
348 sumOfCols[local_id] = tmp_sum;
349 barrier(CLK_LOCAL_MEM_FENCE);
352 int id = local_id + sx - ANCHOR_X;
353 if (id >= 0 && id < LOCAL_SIZE)
354 total_sum += sumOfCols[id];
356 barrier(CLK_LOCAL_MEM_FENCE);
361 ASSERT(pos.y >= dstCoords.y1 && pos.y < dstCoords.y2);
362 *dstPtr = CONVERT_TO_TYPE(total_sum);
366 readAllpixels = false;
367 #if BLOCK_SIZE_Y > KERNEL_SIZE_Y
368 sy_index = (sy_index + 1 <= KERNEL_SIZE_Y) ? sy_index + 1 : 1;
372 #endif // BLOCK_SIZE_Y == 1