Merge remote-tracking branch 'origin/2.4' into merge-2.4
[profile/ivi/opencv.git] / modules / ocl / src / split_merge.cpp
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 //    Jia Haipeng, jiahaipeng95@gmail.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 #include "precomp.hpp"
47 #include "opencl_kernels.hpp"
48
49 using namespace cv;
50 using namespace cv::ocl;
51 namespace cv
52 {
53     namespace ocl
54     {
55         namespace split_merge
56         {
57             static void merge_vector_run(const oclMat *mat_src, size_t n, oclMat &mat_dst)
58             {
59                 if(!mat_dst.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && mat_dst.type() == CV_64F)
60                 {
61                     CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
62                     return;
63                 }
64
65                 Context  *clCxt = mat_dst.clCxt;
66                 int channels = mat_dst.oclchannels();
67                 int depth = mat_dst.depth();
68
69                 String kernelName = "merge_vector";
70
71                 int vector_lengths[4][7] = {{0, 0, 0, 0, 0, 0, 0},
72                     {2, 2, 1, 1, 1, 1, 1},
73                     {4, 4, 2, 2 , 1, 1, 1},
74                     {1, 1, 1, 1, 1, 1, 1}
75                 };
76
77                 size_t vector_length = vector_lengths[channels - 1][depth];
78                 int offset_cols = (mat_dst.offset / mat_dst.elemSize()) & (vector_length - 1);
79                 int cols = divUp(mat_dst.cols + offset_cols, vector_length);
80
81                 size_t localThreads[3]  = { 64, 4, 1 };
82                 size_t globalThreads[3] = { cols, mat_dst.rows, 1 };
83
84                 int dst_step1 = mat_dst.cols * mat_dst.elemSize();
85                 std::vector<std::pair<size_t , const void *> > args;
86                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst.data));
87                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.step));
88                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.offset));
89                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[0].data));
90                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[0].step));
91                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[0].offset));
92                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[1].data));
93                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[1].step));
94                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[1].offset));
95
96                 if(channels == 4)
97                 {
98                     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
99                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
100                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
101
102                     if(n == 3)
103                     {
104                         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
105                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
106                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
107                     }
108                     else if( n == 4)
109                     {
110                         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[3].data));
111                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[3].step));
112                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[3].offset));
113                     }
114                 }
115
116                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.rows));
117                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
118                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step1));
119
120                 openCLExecuteKernel(clCxt, &merge_mat, kernelName, globalThreads, localThreads, args, channels, depth);
121             }
122             static void merge(const oclMat *mat_src, size_t n, oclMat &mat_dst)
123             {
124                 CV_Assert(mat_src);
125                 CV_Assert(n > 0);
126
127                 int depth = mat_src[0].depth();
128                 Size size = mat_src[0].size();
129
130                 int total_channels = 0;
131
132                 for(size_t i = 0; i < n; ++i)
133                 {
134                     CV_Assert(depth == mat_src[i].depth());
135                     CV_Assert(size == mat_src[i].size());
136
137                     total_channels += mat_src[i].oclchannels();
138                 }
139
140                 CV_Assert(total_channels <= 4);
141
142                 if(total_channels == 1)
143                 {
144                     mat_src[0].copyTo(mat_dst);
145                     return;
146                 }
147
148                 mat_dst.create(size, CV_MAKETYPE(depth, total_channels));
149                 merge_vector_run(mat_src, n, mat_dst);
150             }
151             static void split_vector_run(const oclMat &src, oclMat *dst)
152             {
153
154                 if(!src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && src.type() == CV_64F)
155                 {
156                     CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
157                     return;
158                 }
159
160                 Context  *clCtx = src.clCxt;
161                 int channels = src.channels();
162                 int depth = src.depth();
163                 depth = (depth == CV_8S) ? CV_8U : depth;
164                 depth = (depth == CV_16S) ? CV_16U : depth;
165
166                 String kernelName = "split_vector";
167
168                 size_t VEC_SIZE = 4;
169
170                 std::vector<std::pair<size_t , const void *> > args;
171                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&src.data));
172                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&src.step));
173                 int srcOffsetXBytes = src.offset % src.step;
174                 int srcOffsetY = src.offset / src.step;
175                 cl_int2 srcOffset = {{srcOffsetXBytes, srcOffsetY}};
176                 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&srcOffset));
177
178                 bool dst0Aligned = false, dst1Aligned = false, dst2Aligned = false, dst3Aligned = false;
179                 int alignSize = dst[0].elemSize1() * VEC_SIZE;
180                 int alignMask = alignSize - 1;
181
182                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst[0].data));
183                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst[0].step));
184                 int dst0OffsetXBytes = dst[0].offset % dst[0].step;
185                 int dst0OffsetY = dst[0].offset / dst[0].step;
186                 cl_int2 dst0Offset = {{dst0OffsetXBytes, dst0OffsetY}};
187                 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&dst0Offset));
188                 if ((dst0OffsetXBytes & alignMask) == 0)
189                     dst0Aligned = true;
190
191                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst[1].data));
192                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst[1].step));
193                 int dst1OffsetXBytes = dst[1].offset % dst[1].step;
194                 int dst1OffsetY = dst[1].offset / dst[1].step;
195                 cl_int2 dst1Offset = {{dst1OffsetXBytes, dst1OffsetY}};
196                 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&dst1Offset));
197                 if ((dst1OffsetXBytes & alignMask) == 0)
198                     dst1Aligned = true;
199
200                 // DON'T MOVE VARIABLES INTO 'IF' BODY
201                 int dst2OffsetXBytes, dst2OffsetY;
202                 cl_int2 dst2Offset;
203                 int dst3OffsetXBytes, dst3OffsetY;
204                 cl_int2 dst3Offset;
205                 if (channels >= 3)
206                 {
207                     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst[2].data));
208                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst[2].step));
209                     dst2OffsetXBytes = dst[2].offset % dst[2].step;
210                     dst2OffsetY = dst[2].offset / dst[2].step;
211                     dst2Offset.s[0] = dst2OffsetXBytes; dst2Offset.s[1] = dst2OffsetY;
212                     args.push_back( std::make_pair( sizeof(cl_int2), (void *)&dst2Offset));
213                     if ((dst2OffsetXBytes & alignMask) == 0)
214                         dst2Aligned = true;
215                 }
216
217                 if (channels >= 4)
218                 {
219                     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&dst[3].data));
220                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst[3].step));
221                     dst3OffsetXBytes = dst[3].offset % dst[3].step;
222                     dst3OffsetY = dst[3].offset / dst[3].step;
223                     dst3Offset.s[0] = dst3OffsetXBytes; dst3Offset.s[1] = dst3OffsetY;
224                     args.push_back( std::make_pair( sizeof(cl_int2), (void *)&dst3Offset));
225                     if ((dst3OffsetXBytes & alignMask) == 0)
226                         dst3Aligned = true;
227                 }
228
229                 cl_int2 size = {{ src.cols, src.rows }};
230                 args.push_back( std::make_pair( sizeof(cl_int2), (void *)&size));
231
232                 String build_options =
233                         cv::format("-D VEC_SIZE=%d -D DATA_DEPTH=%d -D DATA_CHAN=%d",
234                                    (int)VEC_SIZE, depth, channels);
235
236                 if (dst0Aligned)
237                     build_options = build_options + " -D DST0_ALIGNED";
238                 if (dst1Aligned)
239                     build_options = build_options + " -D DST1_ALIGNED";
240                 if (dst2Aligned)
241                     build_options = build_options + " -D DST2_ALIGNED";
242                 if (dst3Aligned)
243                     build_options = build_options + " -D DST3_ALIGNED";
244
245                 const DeviceInfo& devInfo = clCtx->getDeviceInfo();
246
247                 // TODO Workaround for issues. Need to investigate a problem.
248                 if (channels == 2
249                         && devInfo.deviceType == CVCL_DEVICE_TYPE_CPU
250                         && devInfo.platform->platformVendor.find("Intel") != std::string::npos
251                         && (devInfo.deviceVersion.find("Build 56860") != std::string::npos
252                             || devInfo.deviceVersion.find("Build 76921") != std::string::npos
253                             || devInfo.deviceVersion.find("Build 78712") != std::string::npos))
254                     build_options = build_options + " -D BYPASS_VSTORE=true";
255
256                 size_t globalThreads[3] = { divUp(src.cols, VEC_SIZE), src.rows, 1 };
257                 openCLExecuteKernel(clCtx, &split_mat, kernelName, globalThreads, NULL, args, -1, -1, build_options.c_str());
258             }
259             static void split(const oclMat &mat_src, oclMat *mat_dst)
260             {
261                 CV_Assert(mat_dst);
262
263                 int depth = mat_src.depth();
264                 int num_channels = mat_src.channels();
265                 Size size = mat_src.size();
266
267                 if (num_channels == 1)
268                 {
269                     mat_src.copyTo(mat_dst[0]);
270                     return;
271                 }
272
273                 for (int i = 0; i < mat_src.oclchannels(); i++)
274                     mat_dst[i].create(size, CV_MAKETYPE(depth, 1));
275
276                 split_vector_run(mat_src, mat_dst);
277             }
278         }
279     }
280 }
281
282 void cv::ocl::merge(const oclMat *src, size_t n, oclMat &dst)
283 {
284     split_merge::merge(src, n, dst);
285 }
286 void cv::ocl::merge(const std::vector<oclMat> &src, oclMat &dst)
287 {
288     split_merge::merge(&src[0], src.size(), dst);
289 }
290
291 void cv::ocl::split(const oclMat &src, oclMat *dst)
292 {
293     split_merge::split(src, dst);
294 }
295 void cv::ocl::split(const oclMat &src, std::vector<oclMat> &dst)
296 {
297     dst.resize(src.oclchannels()); // TODO Why oclchannels?
298     if(src.oclchannels() > 0)
299         split_merge::split(src, &dst[0]);
300 }