Merge pull request #1541 from SpecLad: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 oclMaterials 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 <vector>
48
49 using namespace cv;
50 using namespace cv::ocl;
51
52 ////////////////////////////////////////////////////////////////////////
53 ///////////////// oclMat merge and split ///////////////////////////////
54 ////////////////////////////////////////////////////////////////////////
55
56 namespace cv
57 {
58     namespace ocl
59     {
60         ///////////////////////////OpenCL kernel strings///////////////////////////
61         extern const char *merge_mat;
62         extern const char *split_mat;
63     }
64 }
65 namespace cv
66 {
67     namespace ocl
68     {
69         namespace split_merge
70         {
71             static void merge_vector_run(const oclMat *mat_src, size_t n, oclMat &mat_dst)
72             {
73                 if(!mat_dst.clCxt->supportsFeature(Context::CL_DOUBLE) && mat_dst.type() == CV_64F)
74                 {
75                     CV_Error(Error::GpuNotSupported, "Selected device don't support double\r\n");
76                     return;
77                 }
78
79                 Context  *clCxt = mat_dst.clCxt;
80                 int channels = mat_dst.oclchannels();
81                 int depth = mat_dst.depth();
82
83                 String kernelName = "merge_vector";
84
85                 int vector_lengths[4][7] = {{0, 0, 0, 0, 0, 0, 0},
86                     {2, 2, 1, 1, 1, 1, 1},
87                     {4, 4, 2, 2 , 1, 1, 1},
88                     {1, 1, 1, 1, 1, 1, 1}
89                 };
90
91                 size_t vector_length = vector_lengths[channels - 1][depth];
92                 int offset_cols = (mat_dst.offset / mat_dst.elemSize()) & (vector_length - 1);
93                 int cols = divUp(mat_dst.cols + offset_cols, vector_length);
94
95                 size_t localThreads[3]  = { 64, 4, 1 };
96                 size_t globalThreads[3] = { cols, mat_dst.rows, 1 };
97
98                 int dst_step1 = mat_dst.cols * mat_dst.elemSize();
99                 std::vector<std::pair<size_t , const void *> > args;
100                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst.data));
101                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.step));
102                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.offset));
103                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[0].data));
104                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[0].step));
105                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[0].offset));
106                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[1].data));
107                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[1].step));
108                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[1].offset));
109
110                 if(channels == 4)
111                 {
112                     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
113                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
114                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
115
116                     if(n == 3)
117                     {
118                         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[2].data));
119                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].step));
120                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[2].offset));
121                     }
122                     else if( n == 4)
123                     {
124                         args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src[3].data));
125                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[3].step));
126                         args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src[3].offset));
127                     }
128                 }
129
130                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst.rows));
131                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
132                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step1));
133
134                 openCLExecuteKernel(clCxt, &merge_mat, kernelName, globalThreads, localThreads, args, channels, depth);
135             }
136             static void merge(const oclMat *mat_src, size_t n, oclMat &mat_dst)
137             {
138                 CV_Assert(mat_src);
139                 CV_Assert(n > 0);
140
141                 int depth = mat_src[0].depth();
142                 Size size = mat_src[0].size();
143
144                 int total_channels = 0;
145
146                 for(size_t i = 0; i < n; ++i)
147                 {
148                     CV_Assert(depth == mat_src[i].depth());
149                     CV_Assert(size == mat_src[i].size());
150
151                     total_channels += mat_src[i].oclchannels();
152                 }
153
154                 CV_Assert(total_channels <= 4);
155
156                 if(total_channels == 1)
157                 {
158                     mat_src[0].copyTo(mat_dst);
159                     return;
160                 }
161
162                 mat_dst.create(size, CV_MAKETYPE(depth, total_channels));
163                 merge_vector_run(mat_src, n, mat_dst);
164             }
165             static void split_vector_run(const oclMat &mat_src, oclMat *mat_dst)
166             {
167
168                 if(!mat_src.clCxt->supportsFeature(Context::CL_DOUBLE) && mat_src.type() == CV_64F)
169                 {
170                     CV_Error(Error::GpuNotSupported, "Selected device don't support double\r\n");
171                     return;
172                 }
173
174                 Context  *clCxt = mat_src.clCxt;
175                 int channels = mat_src.oclchannels();
176                 int depth = mat_src.depth();
177
178                 String kernelName = "split_vector";
179
180                 int vector_lengths[4][7] = {{0, 0, 0, 0, 0, 0, 0},
181                     {4, 4, 2, 2, 1, 1, 1},
182                     {4, 4, 2, 2 , 1, 1, 1},
183                     {4, 4, 2, 2, 1, 1, 1}
184                 };
185
186                 size_t vector_length = vector_lengths[channels - 1][mat_dst[0].depth()];
187
188                 int max_offset_cols = 0;
189                 for(int i = 0; i < channels; i++)
190                 {
191                     int offset_cols = (mat_dst[i].offset / mat_dst[i].elemSize()) & (vector_length - 1);
192                     if(max_offset_cols < offset_cols)
193                         max_offset_cols = offset_cols;
194                 }
195
196                 int cols =  vector_length == 1 ? divUp(mat_src.cols, vector_length)
197                             : divUp(mat_src.cols + max_offset_cols, vector_length);
198
199                 size_t localThreads[3]  = { 64, 4, 1 };
200                 size_t globalThreads[3] = { cols, mat_src.rows, 1 };
201
202                 int dst_step1 = mat_dst[0].cols * mat_dst[0].elemSize();
203                 std::vector<std::pair<size_t , const void *> > args;
204                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
205                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.step));
206                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.offset));
207                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[0].data));
208                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[0].step));
209                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[0].offset));
210                 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[1].data));
211                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[1].step));
212                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[1].offset));
213                 if(channels >= 3)
214                 {
215
216                     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[2].data));
217                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[2].step));
218                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[2].offset));
219                 }
220                 if(channels >= 4)
221                 {
222                     args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[3].data));
223                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[3].step));
224                     args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[3].offset));
225                 }
226
227                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.rows));
228                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
229                 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step1));
230
231                 openCLExecuteKernel(clCxt, &split_mat, kernelName, globalThreads, localThreads, args, channels, depth);
232             }
233             static void split(const oclMat &mat_src, oclMat *mat_dst)
234             {
235                 CV_Assert(mat_dst);
236
237                 int depth = mat_src.depth();
238                 int num_channels = mat_src.oclchannels();
239                 Size size = mat_src.size();
240
241                 if(num_channels == 1)
242                 {
243                     mat_src.copyTo(mat_dst[0]);
244                     return;
245                 }
246
247                 int i;
248                 for(i = 0; i < num_channels; i++)
249                     mat_dst[i].create(size, CV_MAKETYPE(depth, 1));
250
251                 split_vector_run(mat_src, mat_dst);
252             }
253         }
254     }
255 }
256
257 void cv::ocl::merge(const oclMat *src, size_t n, oclMat &dst)
258 {
259     split_merge::merge(src, n, dst);
260 }
261 void cv::ocl::merge(const std::vector<oclMat> &src, oclMat &dst)
262 {
263     split_merge::merge(&src[0], src.size(), dst);
264 }
265
266 void cv::ocl::split(const oclMat &src, oclMat *dst)
267 {
268     split_merge::split(src, dst);
269 }
270 void cv::ocl::split(const oclMat &src, std::vector<oclMat> &dst)
271 {
272     dst.resize(src.oclchannels());
273     if(src.oclchannels() > 0)
274         split_merge::split(src, &dst[0]);
275 }