Merge pull request #524 from LeonidBeynenson:add_float_and_unsigned_parameters_to_cv_...
[profile/ivi/opencv.git] / modules / ocl / src / matrix_operations.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 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // @Authors
19 //    Niko Li, newlife20080214@gmail.com
20 //    Yao Wang, bitwangyaoyao@gmail.com
21 //
22 // Redistribution and use in source and binary forms, with or without modification,
23 // are permitted provided that the following conditions are met:
24 //
25 //   * Redistribution's of source code must retain the above copyright notice,
26 //     this list of conditions and the following disclaimer.
27 //
28 //   * Redistribution's in binary form must reproduce the above copyright notice,
29 //     this list of conditions and the following disclaimer in the documentation
30 //     and/or other oclMaterials provided with the distribution.
31 //
32 //   * The name of the copyright holders may not be used to endorse or promote products
33 //     derived from this software without specific prior written permission.
34 //
35 // This software is provided by the copyright holders and contributors "as is" and
36 // any express or implied warranties, including, but not limited to, the implied
37 // warranties of merchantability and fitness for a particular purpose are disclaimed.
38 // In no event shall the Intel Corporation or contributors be liable for any direct,
39 // indirect, incidental, special, exemplary, or consequential damages
40 // (including, but not limited to, procurement of substitute goods or services;
41 // loss of use, data, or profits; or business interruption) however caused
42 // and on any theory of liability, whether in contract, strict liability,
43 // or tort (including negligence or otherwise) arising in any way out of
44 // the use of this software, even if advised of the possibility of such damage.
45 //
46 //M*/
47
48 #include "precomp.hpp"
49
50 #define ALIGN 32
51 #define GPU_MATRIX_MALLOC_STEP(step) (((step) + ALIGN - 1) / ALIGN) * ALIGN
52
53 using namespace cv;
54 using namespace cv::ocl;
55 using namespace std;
56
57 ////////////////////////////////////////////////////////////////////////
58 //////////////////////////////// oclMat ////////////////////////////////
59 ////////////////////////////////////////////////////////////////////////
60
61 //helper routines
62 namespace cv
63 {
64     namespace ocl
65     {
66         ///////////////////////////OpenCL kernel strings///////////////////////////
67         extern const char *operator_copyToM;
68         extern const char *operator_convertTo;
69         extern const char *operator_setTo;
70         extern const char *operator_setToM;
71         extern const char *convertC3C4;
72     }
73 }
74
75 ////////////////////////////////////////////////////////////////////////
76 // convert_C3C4
77 static void convert_C3C4(const cl_mem &src, oclMat &dst)
78 {
79     int dstStep_in_pixel = dst.step1() / dst.oclchannels();
80     int pixel_end = dst.wholecols * dst.wholerows - 1;
81     Context *clCxt = dst.clCxt;
82     string kernelName = "convertC3C4";
83     char compile_option[32];
84     switch(dst.depth())
85     {
86     case 0:
87         sprintf(compile_option, "-D GENTYPE4=uchar4");
88         break;
89     case 1:
90         sprintf(compile_option, "-D GENTYPE4=char4");
91         break;
92     case 2:
93         sprintf(compile_option, "-D GENTYPE4=ushort4");
94         break;
95     case 3:
96         sprintf(compile_option, "-D GENTYPE4=short4");
97         break;
98     case 4:
99         sprintf(compile_option, "-D GENTYPE4=int4");
100         break;
101     case 5:
102         sprintf(compile_option, "-D GENTYPE4=float4");
103         break;
104     case 6:
105         sprintf(compile_option, "-D GENTYPE4=double4");
106         break;
107     default:
108         CV_Error(CV_StsUnsupportedFormat, "unknown depth");
109     }
110     vector< pair<size_t, const void *> > args;
111     args.push_back( make_pair( sizeof(cl_mem), (void *)&src));
112     args.push_back( make_pair( sizeof(cl_mem), (void *)&dst.data));
113     args.push_back( make_pair( sizeof(cl_int), (void *)&dst.wholecols));
114     args.push_back( make_pair( sizeof(cl_int), (void *)&dst.wholerows));
115     args.push_back( make_pair( sizeof(cl_int), (void *)&dstStep_in_pixel));
116     args.push_back( make_pair( sizeof(cl_int), (void *)&pixel_end));
117
118     size_t globalThreads[3] = {((dst.wholecols * dst.wholerows + 3) / 4 + 255) / 256 * 256, 1, 1};
119     size_t localThreads[3] = {256, 1, 1};
120
121     openCLExecuteKernel(clCxt, &convertC3C4, kernelName, globalThreads, localThreads, args, -1, -1, compile_option);
122 }
123 ////////////////////////////////////////////////////////////////////////
124 // convert_C4C3
125 static void convert_C4C3(const oclMat &src, cl_mem &dst)
126 {
127     int srcStep_in_pixel = src.step1() / src.oclchannels();
128     int pixel_end = src.wholecols * src.wholerows - 1;
129     Context *clCxt = src.clCxt;
130     string kernelName = "convertC4C3";
131     char compile_option[32];
132     switch(src.depth())
133     {
134     case 0:
135         sprintf(compile_option, "-D GENTYPE4=uchar4");
136         break;
137     case 1:
138         sprintf(compile_option, "-D GENTYPE4=char4");
139         break;
140     case 2:
141         sprintf(compile_option, "-D GENTYPE4=ushort4");
142         break;
143     case 3:
144         sprintf(compile_option, "-D GENTYPE4=short4");
145         break;
146     case 4:
147         sprintf(compile_option, "-D GENTYPE4=int4");
148         break;
149     case 5:
150         sprintf(compile_option, "-D GENTYPE4=float4");
151         break;
152     case 6:
153         sprintf(compile_option, "-D GENTYPE4=double4");
154         break;
155     default:
156         CV_Error(CV_StsUnsupportedFormat, "unknown depth");
157     }
158
159     vector< pair<size_t, const void *> > args;
160     args.push_back( make_pair( sizeof(cl_mem), (void *)&src.data));
161     args.push_back( make_pair( sizeof(cl_mem), (void *)&dst));
162     args.push_back( make_pair( sizeof(cl_int), (void *)&src.wholecols));
163     args.push_back( make_pair( sizeof(cl_int), (void *)&src.wholerows));
164     args.push_back( make_pair( sizeof(cl_int), (void *)&srcStep_in_pixel));
165     args.push_back( make_pair( sizeof(cl_int), (void *)&pixel_end));
166
167     size_t globalThreads[3] = {((src.wholecols * src.wholerows + 3) / 4 + 255) / 256 * 256, 1, 1};
168     size_t localThreads[3] = {256, 1, 1};
169
170     openCLExecuteKernel(clCxt, &convertC3C4, kernelName, globalThreads, localThreads, args, -1, -1, compile_option);
171 }
172
173 void cv::ocl::oclMat::upload(const Mat &m)
174 {
175     CV_DbgAssert(!m.empty());
176     Size wholeSize;
177     Point ofs;
178     m.locateROI(wholeSize, ofs);
179     //   int type = m.type();
180     //   if(m.oclchannels() == 3)
181     //{
182     //  type = CV_MAKETYPE(m.depth(), 4);
183     //}
184     create(wholeSize, m.type());
185
186     if(m.channels() == 3)
187     {
188         int pitch = wholeSize.width * 3 * m.elemSize1();
189         int tail_padding = m.elemSize1() * 3072;
190         int err;
191         cl_mem temp = clCreateBuffer(clCxt->impl->clContext, CL_MEM_READ_WRITE,
192                                      (pitch * wholeSize.height + tail_padding - 1) / tail_padding * tail_padding, 0, &err);
193         openCLVerifyCall(err);
194
195         openCLMemcpy2D(clCxt, temp, pitch, m.datastart, m.step, wholeSize.width * m.elemSize(), wholeSize.height, clMemcpyHostToDevice, 3);
196         convert_C3C4(temp, *this);
197         //int* cputemp=new int[wholeSize.height*wholeSize.width * 3];
198         //int* cpudata=new int[this->step*this->wholerows/sizeof(int)];
199         //openCLSafeCall(clEnqueueReadBuffer(clCxt->impl->clCmdQueue, temp, CL_TRUE,
200         //                                              0, wholeSize.height*wholeSize.width * 3* sizeof(int), cputemp, 0, NULL, NULL));
201         //openCLSafeCall(clEnqueueReadBuffer(clCxt->impl->clCmdQueue, (cl_mem)data, CL_TRUE,
202         //                                              0, this->step*this->wholerows, cpudata, 0, NULL, NULL));
203         //for(int i=0;i<wholeSize.height;i++)
204         //{
205         //      int *a = cputemp+i*wholeSize.width * 3,*b = cpudata + i*this->step/sizeof(int);
206         //      for(int j=0;j<wholeSize.width;j++)
207         //      {
208         //              if((a[3*j] != b[4*j])||(a[3*j+1] != b[4*j+1])||(a[3*j+2] != b[4*j+2]))
209         //                      printf("rows=%d,cols=%d,cputtemp=%d,%d,%d;cpudata=%d,%d,%d\n",
210         //                      i,j,a[3*j],a[3*j+1],a[3*j+2],b[4*j],b[4*j+1],b[4*j+2]);
211         //      }
212         //}
213         //delete []cputemp;
214         //delete []cpudata;
215         openCLSafeCall(clReleaseMemObject(temp));
216     }
217     else
218     {
219         openCLMemcpy2D(clCxt, data, step, m.datastart, m.step, wholeSize.width * elemSize(), wholeSize.height, clMemcpyHostToDevice);
220     }
221
222     rows = m.rows;
223     cols = m.cols;
224     offset = ofs.y * step + ofs.x * elemSize();
225     //download_channels = m.channels();
226 }
227
228 void cv::ocl::oclMat::download(cv::Mat &m) const
229 {
230     CV_DbgAssert(!this->empty());
231     //   int t = type();
232     //   if(download_channels == 3)
233     //{
234     //  t = CV_MAKETYPE(depth(), 3);
235     //}
236     m.create(wholerows, wholecols, type());
237
238     if(m.channels() == 3)
239     {
240         int pitch = wholecols * 3 * m.elemSize1();
241         int tail_padding = m.elemSize1() * 3072;
242         int err;
243         cl_mem temp = clCreateBuffer(clCxt->impl->clContext, CL_MEM_READ_WRITE,
244                                      (pitch * wholerows + tail_padding - 1) / tail_padding * tail_padding, 0, &err);
245         openCLVerifyCall(err);
246
247         convert_C4C3(*this, temp);
248         openCLMemcpy2D(clCxt, m.data, m.step, temp, pitch, wholecols * m.elemSize(), wholerows, clMemcpyDeviceToHost, 3);
249         //int* cputemp=new int[wholecols*wholerows * 3];
250         //int* cpudata=new int[this->step*this->wholerows/sizeof(int)];
251         //openCLSafeCall(clEnqueueReadBuffer(clCxt->impl->clCmdQueue, temp, CL_TRUE,
252         //                                              0, wholecols*wholerows * 3* sizeof(int), cputemp, 0, NULL, NULL));
253         //openCLSafeCall(clEnqueueReadBuffer(clCxt->impl->clCmdQueue, (cl_mem)data, CL_TRUE,
254         //                                              0, this->step*this->wholerows, cpudata, 0, NULL, NULL));
255         //for(int i=0;i<wholerows;i++)
256         //{
257         //      int *a = cputemp+i*wholecols * 3,*b = cpudata + i*this->step/sizeof(int);
258         //      for(int j=0;j<wholecols;j++)
259         //      {
260         //              if((a[3*j] != b[4*j])||(a[3*j+1] != b[4*j+1])||(a[3*j+2] != b[4*j+2]))
261         //                      printf("rows=%d,cols=%d,cputtemp=%d,%d,%d;cpudata=%d,%d,%d\n",
262         //                      i,j,a[3*j],a[3*j+1],a[3*j+2],b[4*j],b[4*j+1],b[4*j+2]);
263         //      }
264         //}
265         //delete []cputemp;
266         //delete []cpudata;
267         openCLSafeCall(clReleaseMemObject(temp));
268     }
269     else
270     {
271         openCLMemcpy2D(clCxt, m.data, m.step, data, step, wholecols * elemSize(), wholerows, clMemcpyDeviceToHost);
272     }
273     Size wholesize;
274     Point ofs;
275     locateROI(wholesize, ofs);
276     m.adjustROI(-ofs.y, ofs.y + rows - wholerows, -ofs.x, ofs.x + cols - wholecols);
277 }
278
279 /////////////////////common//////////////////////////////////////
280 inline int divUp(int total, int grain)
281 {
282     return (total + grain - 1) / grain;
283 }
284 ///////////////////////////////////////////////////////////////////////////
285 ////////////////////////////////// CopyTo /////////////////////////////////
286 ///////////////////////////////////////////////////////////////////////////
287 static void copy_to_with_mask(const oclMat &src, oclMat &dst, const oclMat &mask, string kernelName)
288 {
289     CV_DbgAssert( dst.rows == mask.rows && dst.cols == mask.cols &&
290                   src.rows == dst.rows && src.cols == dst.cols
291                   && mask.type() == CV_8UC1);
292
293     vector<pair<size_t , const void *> > args;
294
295     std::string string_types[4][7] = {{"uchar", "char", "ushort", "short", "int", "float", "double"},
296         {"uchar2", "char2", "ushort2", "short2", "int2", "float2", "double2"},
297         {"uchar3", "char3", "ushort3", "short3", "int3", "float3", "double3"},
298         {"uchar4", "char4", "ushort4", "short4", "int4", "float4", "double4"}
299     };
300     char compile_option[32];
301     sprintf(compile_option, "-D GENTYPE=%s", string_types[dst.oclchannels() - 1][dst.depth()].c_str());
302     size_t localThreads[3] = {16, 16, 1};
303     size_t globalThreads[3];
304
305     globalThreads[0] = divUp(dst.cols, localThreads[0]) * localThreads[0];
306     globalThreads[1] = divUp(dst.rows, localThreads[1]) * localThreads[1];
307     globalThreads[2] = 1;
308
309     int dststep_in_pixel = dst.step / dst.elemSize(), dstoffset_in_pixel = dst.offset / dst.elemSize();
310     int srcstep_in_pixel = src.step / src.elemSize(), srcoffset_in_pixel = src.offset / src.elemSize();
311
312     args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
313     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
314     args.push_back( make_pair( sizeof(cl_mem) , (void *)&mask.data ));
315     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
316     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
317     args.push_back( make_pair( sizeof(cl_int) , (void *)&srcstep_in_pixel ));
318     args.push_back( make_pair( sizeof(cl_int) , (void *)&srcoffset_in_pixel ));
319     args.push_back( make_pair( sizeof(cl_int) , (void *)&dststep_in_pixel ));
320     args.push_back( make_pair( sizeof(cl_int) , (void *)&dstoffset_in_pixel ));
321     args.push_back( make_pair( sizeof(cl_int) , (void *)&mask.step ));
322     args.push_back( make_pair( sizeof(cl_int) , (void *)&mask.offset ));
323
324     openCLExecuteKernel(dst.clCxt , &operator_copyToM, kernelName, globalThreads,
325                         localThreads, args, -1, -1, compile_option);
326 }
327
328 void cv::ocl::oclMat::copyTo( oclMat &m ) const
329 {
330     CV_DbgAssert(!this->empty());
331     m.create(size(), type());
332     openCLCopyBuffer2D(clCxt, m.data, m.step, m.offset,
333                        data, step, cols * elemSize(), rows, offset);
334 }
335
336 void cv::ocl::oclMat::copyTo( oclMat &mat, const oclMat &mask) const
337 {
338     if (mask.empty())
339     {
340         copyTo(mat);
341     }
342     else
343     {
344         mat.create(size(), type());
345         copy_to_with_mask(*this, mat, mask, "copy_to_with_mask");
346     }
347 }
348
349 ///////////////////////////////////////////////////////////////////////////
350 //////////////////////////////// ConvertTo ////////////////////////////////
351 ///////////////////////////////////////////////////////////////////////////
352 static void convert_run(const oclMat &src, oclMat &dst, double alpha, double beta)
353 {
354     string kernelName = "convert_to_S";
355     stringstream idxStr;
356     idxStr << src.depth();
357     kernelName += idxStr.str();
358     float alpha_f = alpha, beta_f = beta;
359     CV_DbgAssert(src.rows == dst.rows && src.cols == dst.cols);
360     vector<pair<size_t , const void *> > args;
361     size_t localThreads[3] = {16, 16, 1};
362     size_t globalThreads[3];
363     globalThreads[0] = (dst.cols + localThreads[0] - 1) / localThreads[0] * localThreads[0];
364     globalThreads[1] = (dst.rows + localThreads[1] - 1) / localThreads[1] * localThreads[1];
365     globalThreads[2] = 1;
366     int dststep_in_pixel = dst.step / dst.elemSize(), dstoffset_in_pixel = dst.offset / dst.elemSize();
367     int srcstep_in_pixel = src.step / src.elemSize(), srcoffset_in_pixel = src.offset / src.elemSize();
368     if(dst.type() == CV_8UC1)
369     {
370         globalThreads[0] = ((dst.cols + 4) / 4 + localThreads[0]) / localThreads[0] * localThreads[0];
371     }
372     args.push_back( make_pair( sizeof(cl_mem) , (void *)&src.data ));
373     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
374     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.cols ));
375     args.push_back( make_pair( sizeof(cl_int) , (void *)&src.rows ));
376     args.push_back( make_pair( sizeof(cl_int) , (void *)&srcstep_in_pixel ));
377     args.push_back( make_pair( sizeof(cl_int) , (void *)&srcoffset_in_pixel ));
378     args.push_back( make_pair( sizeof(cl_int) , (void *)&dststep_in_pixel ));
379     args.push_back( make_pair( sizeof(cl_int) , (void *)&dstoffset_in_pixel ));
380     args.push_back( make_pair( sizeof(cl_float) , (void *)&alpha_f ));
381     args.push_back( make_pair( sizeof(cl_float) , (void *)&beta_f ));
382     openCLExecuteKernel(dst.clCxt , &operator_convertTo, kernelName, globalThreads,
383                         localThreads, args, dst.oclchannels(), dst.depth());
384 }
385 void cv::ocl::oclMat::convertTo( oclMat &dst, int rtype, double alpha, double beta ) const
386 {
387     //cout << "cv::ocl::oclMat::convertTo()" << endl;
388
389     bool noScale = fabs(alpha - 1) < std::numeric_limits<double>::epsilon()
390                    && fabs(beta) < std::numeric_limits<double>::epsilon();
391
392     if( rtype < 0 )
393         rtype = type();
394     else
395         rtype = CV_MAKETYPE(CV_MAT_DEPTH(rtype), channels());
396
397     //int scn = channels();
398     int sdepth = depth(), ddepth = CV_MAT_DEPTH(rtype);
399     if( sdepth == ddepth && noScale )
400     {
401         copyTo(dst);
402         return;
403     }
404
405     oclMat temp;
406     const oclMat *psrc = this;
407     if( sdepth != ddepth && psrc == &dst )
408         psrc = &(temp = *this);
409
410     dst.create( size(), rtype );
411     convert_run(*psrc, dst, alpha, beta);
412 }
413
414 ///////////////////////////////////////////////////////////////////////////
415 //////////////////////////////// setTo ////////////////////////////////////
416 ///////////////////////////////////////////////////////////////////////////
417 oclMat &cv::ocl::oclMat::operator = (const Scalar &s)
418 {
419     //cout << "cv::ocl::oclMat::=" << endl;
420     setTo(s);
421     return *this;
422 }
423 static void set_to_withoutmask_run(const oclMat &dst, const Scalar &scalar, string kernelName)
424 {
425     vector<pair<size_t , const void *> > args;
426
427     size_t localThreads[3] = {16, 16, 1};
428     size_t globalThreads[3];
429     globalThreads[0] = (dst.cols + localThreads[0] - 1) / localThreads[0] * localThreads[0];
430     globalThreads[1] = (dst.rows + localThreads[1] - 1) / localThreads[1] * localThreads[1];
431     globalThreads[2] = 1;
432     int step_in_pixel = dst.step / dst.elemSize(), offset_in_pixel = dst.offset / dst.elemSize();
433     if(dst.type() == CV_8UC1)
434     {
435         globalThreads[0] = ((dst.cols + 4) / 4 + localThreads[0] - 1) / localThreads[0] * localThreads[0];
436     }
437     char compile_option[32];
438     union sc
439     {
440         cl_uchar4 uval;
441         cl_char4  cval;
442         cl_ushort4 usval;
443         cl_short4 shval;
444         cl_int4 ival;
445         cl_float4 fval;
446         cl_double4 dval;
447     } val;
448     switch(dst.depth())
449     {
450     case CV_8U:
451         val.uval.s[0] = saturate_cast<uchar>(scalar.val[0]);
452         val.uval.s[1] = saturate_cast<uchar>(scalar.val[1]);
453         val.uval.s[2] = saturate_cast<uchar>(scalar.val[2]);
454         val.uval.s[3] = saturate_cast<uchar>(scalar.val[3]);
455         switch(dst.oclchannels())
456         {
457         case 1:
458             sprintf(compile_option, "-D GENTYPE=uchar");
459             args.push_back( make_pair( sizeof(cl_uchar) , (void *)&val.uval.s[0] ));
460             break;
461         case 4:
462             sprintf(compile_option, "-D GENTYPE=uchar4");
463             args.push_back( make_pair( sizeof(cl_uchar4) , (void *)&val.uval ));
464             break;
465         default:
466             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
467         }
468         break;
469     case CV_8S:
470         val.cval.s[0] = saturate_cast<char>(scalar.val[0]);
471         val.cval.s[1] = saturate_cast<char>(scalar.val[1]);
472         val.cval.s[2] = saturate_cast<char>(scalar.val[2]);
473         val.cval.s[3] = saturate_cast<char>(scalar.val[3]);
474         switch(dst.oclchannels())
475         {
476         case 1:
477             sprintf(compile_option, "-D GENTYPE=char");
478             args.push_back( make_pair( sizeof(cl_char) , (void *)&val.cval.s[0] ));
479             break;
480         case 4:
481             sprintf(compile_option, "-D GENTYPE=char4");
482             args.push_back( make_pair( sizeof(cl_char4) , (void *)&val.cval ));
483             break;
484         default:
485             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
486         }
487         break;
488     case CV_16U:
489         val.usval.s[0] = saturate_cast<ushort>(scalar.val[0]);
490         val.usval.s[1] = saturate_cast<ushort>(scalar.val[1]);
491         val.usval.s[2] = saturate_cast<ushort>(scalar.val[2]);
492         val.usval.s[3] = saturate_cast<ushort>(scalar.val[3]);
493         switch(dst.oclchannels())
494         {
495         case 1:
496             sprintf(compile_option, "-D GENTYPE=ushort");
497             args.push_back( make_pair( sizeof(cl_ushort) , (void *)&val.usval.s[0] ));
498             break;
499         case 4:
500             sprintf(compile_option, "-D GENTYPE=ushort4");
501             args.push_back( make_pair( sizeof(cl_ushort4) , (void *)&val.usval ));
502             break;
503         default:
504             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
505         }
506         break;
507     case CV_16S:
508         val.shval.s[0] = saturate_cast<short>(scalar.val[0]);
509         val.shval.s[1] = saturate_cast<short>(scalar.val[1]);
510         val.shval.s[2] = saturate_cast<short>(scalar.val[2]);
511         val.shval.s[3] = saturate_cast<short>(scalar.val[3]);
512         switch(dst.oclchannels())
513         {
514         case 1:
515             sprintf(compile_option, "-D GENTYPE=short");
516             args.push_back( make_pair( sizeof(cl_short) , (void *)&val.shval.s[0] ));
517             break;
518         case 4:
519             sprintf(compile_option, "-D GENTYPE=short4");
520             args.push_back( make_pair( sizeof(cl_short4) , (void *)&val.shval ));
521             break;
522         default:
523             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
524         }
525         break;
526     case CV_32S:
527         val.ival.s[0] = saturate_cast<int>(scalar.val[0]);
528         val.ival.s[1] = saturate_cast<int>(scalar.val[1]);
529         val.ival.s[2] = saturate_cast<int>(scalar.val[2]);
530         val.ival.s[3] = saturate_cast<int>(scalar.val[3]);
531         switch(dst.oclchannels())
532         {
533         case 1:
534             sprintf(compile_option, "-D GENTYPE=int");
535             args.push_back( make_pair( sizeof(cl_int) , (void *)&val.ival.s[0] ));
536             break;
537         case 2:
538             sprintf(compile_option, "-D GENTYPE=int2");
539             cl_int2 i2val;
540             i2val.s[0] = val.ival.s[0];
541             i2val.s[1] = val.ival.s[1];
542             args.push_back( make_pair( sizeof(cl_int2) , (void *)&i2val ));
543             break;
544         case 4:
545             sprintf(compile_option, "-D GENTYPE=int4");
546             args.push_back( make_pair( sizeof(cl_int4) , (void *)&val.ival ));
547             break;
548         default:
549             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
550         }
551         break;
552     case CV_32F:
553         val.fval.s[0] = scalar.val[0];
554         val.fval.s[1] = scalar.val[1];
555         val.fval.s[2] = scalar.val[2];
556         val.fval.s[3] = scalar.val[3];
557         switch(dst.oclchannels())
558         {
559         case 1:
560             sprintf(compile_option, "-D GENTYPE=float");
561             args.push_back( make_pair( sizeof(cl_float) , (void *)&val.fval.s[0] ));
562             break;
563         case 4:
564             sprintf(compile_option, "-D GENTYPE=float4");
565             args.push_back( make_pair( sizeof(cl_float4) , (void *)&val.fval ));
566             break;
567         default:
568             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
569         }
570         break;
571     case CV_64F:
572         val.dval.s[0] = scalar.val[0];
573         val.dval.s[1] = scalar.val[1];
574         val.dval.s[2] = scalar.val[2];
575         val.dval.s[3] = scalar.val[3];
576         switch(dst.oclchannels())
577         {
578         case 1:
579             sprintf(compile_option, "-D GENTYPE=double");
580             args.push_back( make_pair( sizeof(cl_double) , (void *)&val.dval.s[0] ));
581             break;
582         case 4:
583             sprintf(compile_option, "-D GENTYPE=double4");
584             args.push_back( make_pair( sizeof(cl_double4) , (void *)&val.dval ));
585             break;
586         default:
587             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
588         }
589         break;
590     default:
591         CV_Error(CV_StsUnsupportedFormat, "unknown depth");
592     }
593 #ifdef CL_VERSION_1_2
594     if(dst.offset == 0 && dst.cols == dst.wholecols)
595     {
596         clEnqueueFillBuffer(dst.clCxt->impl->clCmdQueue, (cl_mem)dst.data, args[0].second, args[0].first, 0, dst.step * dst.rows, 0, NULL, NULL);
597     }
598     else
599     {
600         args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
601         args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols ));
602         args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.rows ));
603         args.push_back( make_pair( sizeof(cl_int) , (void *)&step_in_pixel ));
604         args.push_back( make_pair( sizeof(cl_int) , (void *)&offset_in_pixel));
605         openCLExecuteKernel(dst.clCxt , &operator_setTo, kernelName, globalThreads,
606                             localThreads, args, -1, -1, compile_option);
607     }
608 #else
609     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
610     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols ));
611     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.rows ));
612     args.push_back( make_pair( sizeof(cl_int) , (void *)&step_in_pixel ));
613     args.push_back( make_pair( sizeof(cl_int) , (void *)&offset_in_pixel));
614     openCLExecuteKernel(dst.clCxt , &operator_setTo, kernelName, globalThreads,
615                         localThreads, args, -1, -1, compile_option);
616 #endif
617 }
618
619 static void set_to_withmask_run(const oclMat &dst, const Scalar &scalar, const oclMat &mask, string kernelName)
620 {
621     CV_DbgAssert( dst.rows == mask.rows && dst.cols == mask.cols);
622     vector<pair<size_t , const void *> > args;
623     size_t localThreads[3] = {16, 16, 1};
624     size_t globalThreads[3];
625     globalThreads[0] = (dst.cols + localThreads[0] - 1) / localThreads[0] * localThreads[0];
626     globalThreads[1] = (dst.rows + localThreads[1] - 1) / localThreads[1] * localThreads[1];
627     globalThreads[2] = 1;
628     int step_in_pixel = dst.step / dst.elemSize(), offset_in_pixel = dst.offset / dst.elemSize();
629     char compile_option[32];
630     union sc
631     {
632         cl_uchar4 uval;
633         cl_char4  cval;
634         cl_ushort4 usval;
635         cl_short4 shval;
636         cl_int4 ival;
637         cl_float4 fval;
638         cl_double4 dval;
639     } val;
640     switch(dst.depth())
641     {
642     case CV_8U:
643         val.uval.s[0] = saturate_cast<uchar>(scalar.val[0]);
644         val.uval.s[1] = saturate_cast<uchar>(scalar.val[1]);
645         val.uval.s[2] = saturate_cast<uchar>(scalar.val[2]);
646         val.uval.s[3] = saturate_cast<uchar>(scalar.val[3]);
647         switch(dst.oclchannels())
648         {
649         case 1:
650             sprintf(compile_option, "-D GENTYPE=uchar");
651             args.push_back( make_pair( sizeof(cl_uchar) , (void *)&val.uval.s[0] ));
652             break;
653         case 4:
654             sprintf(compile_option, "-D GENTYPE=uchar4");
655             args.push_back( make_pair( sizeof(cl_uchar4) , (void *)&val.uval ));
656             break;
657         default:
658             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
659         }
660         break;
661     case CV_8S:
662         val.cval.s[0] = saturate_cast<char>(scalar.val[0]);
663         val.cval.s[1] = saturate_cast<char>(scalar.val[1]);
664         val.cval.s[2] = saturate_cast<char>(scalar.val[2]);
665         val.cval.s[3] = saturate_cast<char>(scalar.val[3]);
666         switch(dst.oclchannels())
667         {
668         case 1:
669             sprintf(compile_option, "-D GENTYPE=char");
670             args.push_back( make_pair( sizeof(cl_char) , (void *)&val.cval.s[0] ));
671             break;
672         case 4:
673             sprintf(compile_option, "-D GENTYPE=char4");
674             args.push_back( make_pair( sizeof(cl_char4) , (void *)&val.cval ));
675             break;
676         default:
677             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
678         }
679         break;
680     case CV_16U:
681         val.usval.s[0] = saturate_cast<ushort>(scalar.val[0]);
682         val.usval.s[1] = saturate_cast<ushort>(scalar.val[1]);
683         val.usval.s[2] = saturate_cast<ushort>(scalar.val[2]);
684         val.usval.s[3] = saturate_cast<ushort>(scalar.val[3]);
685         switch(dst.oclchannels())
686         {
687         case 1:
688             sprintf(compile_option, "-D GENTYPE=ushort");
689             args.push_back( make_pair( sizeof(cl_ushort) , (void *)&val.usval.s[0] ));
690             break;
691         case 4:
692             sprintf(compile_option, "-D GENTYPE=ushort4");
693             args.push_back( make_pair( sizeof(cl_ushort4) , (void *)&val.usval ));
694             break;
695         default:
696             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
697         }
698         break;
699     case CV_16S:
700         val.shval.s[0] = saturate_cast<short>(scalar.val[0]);
701         val.shval.s[1] = saturate_cast<short>(scalar.val[1]);
702         val.shval.s[2] = saturate_cast<short>(scalar.val[2]);
703         val.shval.s[3] = saturate_cast<short>(scalar.val[3]);
704         switch(dst.oclchannels())
705         {
706         case 1:
707             sprintf(compile_option, "-D GENTYPE=short");
708             args.push_back( make_pair( sizeof(cl_short) , (void *)&val.shval.s[0] ));
709             break;
710         case 4:
711             sprintf(compile_option, "-D GENTYPE=short4");
712             args.push_back( make_pair( sizeof(cl_short4) , (void *)&val.shval ));
713             break;
714         default:
715             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
716         }
717         break;
718     case CV_32S:
719         val.ival.s[0] = saturate_cast<int>(scalar.val[0]);
720         val.ival.s[1] = saturate_cast<int>(scalar.val[1]);
721         val.ival.s[2] = saturate_cast<int>(scalar.val[2]);
722         val.ival.s[3] = saturate_cast<int>(scalar.val[3]);
723         switch(dst.oclchannels())
724         {
725         case 1:
726             sprintf(compile_option, "-D GENTYPE=int");
727             args.push_back( make_pair( sizeof(cl_int) , (void *)&val.ival.s[0] ));
728             break;
729         case 4:
730             sprintf(compile_option, "-D GENTYPE=int4");
731             args.push_back( make_pair( sizeof(cl_int4) , (void *)&val.ival ));
732             break;
733         default:
734             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
735         }
736         break;
737     case CV_32F:
738         val.fval.s[0] = scalar.val[0];
739         val.fval.s[1] = scalar.val[1];
740         val.fval.s[2] = scalar.val[2];
741         val.fval.s[3] = scalar.val[3];
742         switch(dst.oclchannels())
743         {
744         case 1:
745             sprintf(compile_option, "-D GENTYPE=float");
746             args.push_back( make_pair( sizeof(cl_float) , (void *)&val.fval.s[0] ));
747             break;
748         case 4:
749             sprintf(compile_option, "-D GENTYPE=float4");
750             args.push_back( make_pair( sizeof(cl_float4) , (void *)&val.fval ));
751             break;
752         default:
753             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
754         }
755         break;
756     case CV_64F:
757         val.dval.s[0] = scalar.val[0];
758         val.dval.s[1] = scalar.val[1];
759         val.dval.s[2] = scalar.val[2];
760         val.dval.s[3] = scalar.val[3];
761         switch(dst.oclchannels())
762         {
763         case 1:
764             sprintf(compile_option, "-D GENTYPE=double");
765             args.push_back( make_pair( sizeof(cl_double) , (void *)&val.dval.s[0] ));
766             break;
767         case 4:
768             sprintf(compile_option, "-D GENTYPE=double4");
769             args.push_back( make_pair( sizeof(cl_double4) , (void *)&val.dval ));
770             break;
771         default:
772             CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
773         }
774         break;
775     default:
776         CV_Error(CV_StsUnsupportedFormat, "unknown depth");
777     }
778     args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data ));
779     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols ));
780     args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.rows ));
781     args.push_back( make_pair( sizeof(cl_int) , (void *)&step_in_pixel ));
782     args.push_back( make_pair( sizeof(cl_int) , (void *)&offset_in_pixel ));
783     args.push_back( make_pair( sizeof(cl_mem) , (void *)&mask.data ));
784     args.push_back( make_pair( sizeof(cl_int) , (void *)&mask.step ));
785     args.push_back( make_pair( sizeof(cl_int) , (void *)&mask.offset ));
786     openCLExecuteKernel(dst.clCxt , &operator_setToM, kernelName, globalThreads,
787                         localThreads, args, -1, -1, compile_option);
788 }
789
790 oclMat &cv::ocl::oclMat::setTo(const Scalar &scalar, const oclMat &mask)
791 {
792     //cout << "cv::ocl::oclMat::setTo()" << endl;
793     CV_Assert(mask.type() == CV_8UC1);
794     CV_Assert( this->depth() >= 0 && this->depth() <= 6 );
795     CV_DbgAssert( !this->empty());
796     //cl_int status;
797     //cl_mem mem;
798     //mem = clCreateBuffer(this->clCxt->clContext,CL_MEM_READ_WRITE,
799     //                   sizeof(double)*4,NULL,&status);
800     //openCLVerifyCall(status);
801     //double* s =  (double *)scalar.val;
802     //openCLSafeCall(clEnqueueWriteBuffer(this->clCxt->clCmdQueue,
803     //                   (cl_mem)mem,1,0,sizeof(double)*4,s,0,0,0));
804     if (mask.empty())
805     {
806         if(type() == CV_8UC1)
807         {
808             set_to_withoutmask_run(*this, scalar, "set_to_without_mask_C1_D0");
809         }
810         else
811         {
812             set_to_withoutmask_run(*this, scalar, "set_to_without_mask");
813         }
814     }
815     else
816     {
817         set_to_withmask_run(*this, scalar, mask, "set_to_with_mask");
818     }
819
820     return *this;
821 }
822
823 oclMat cv::ocl::oclMat::reshape(int new_cn, int new_rows) const
824 {
825     if( new_rows != 0 && new_rows != rows)
826
827     {
828
829         CV_Error( CV_StsBadFunc,
830
831                   "oclMat's number of rows can not be changed for current version" );
832
833     }
834
835     oclMat hdr = *this;
836
837     int cn = oclchannels();
838
839     if (new_cn == 0)
840
841         new_cn = cn;
842
843
844
845     int total_width = cols * cn;
846
847
848
849     if ((new_cn > total_width || total_width % new_cn != 0) && new_rows == 0)
850
851         new_rows = rows * total_width / new_cn;
852
853
854
855     if (new_rows != 0 && new_rows != rows)
856
857     {
858
859         int total_size = total_width * rows;
860
861
862
863         if (!isContinuous())
864
865             CV_Error(CV_BadStep, "The matrix is not continuous, thus its number of rows can not be changed");
866
867
868
869         if ((unsigned)new_rows > (unsigned)total_size)
870
871             CV_Error(CV_StsOutOfRange, "Bad new number of rows");
872
873
874
875         total_width = total_size / new_rows;
876
877
878
879         if (total_width * new_rows != total_size)
880
881             CV_Error(CV_StsBadArg, "The total number of matrix elements is not divisible by the new number of rows");
882
883
884
885         hdr.rows = new_rows;
886
887         hdr.step = total_width * elemSize1();
888
889     }
890
891
892
893     int new_width = total_width / new_cn;
894
895
896
897     if (new_width * new_cn != total_width)
898
899         CV_Error(CV_BadNumChannels, "The total width is not divisible by the new number of channels");
900
901
902
903     hdr.cols = new_width;
904
905     hdr.wholecols = new_width;
906
907     hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn - 1) << CV_CN_SHIFT);
908
909
910
911     return hdr;
912
913 }
914
915 void cv::ocl::oclMat::create(int _rows, int _cols, int _type)
916 {
917     clCxt = Context::getContext();
918     /* core logic */
919     _type &= TYPE_MASK;
920     //download_channels = CV_MAT_CN(_type);
921     //if(download_channels==3)
922     //{
923     //  _type = CV_MAKE_TYPE((CV_MAT_DEPTH(_type)),4);
924     //}
925     if( rows == _rows && cols == _cols && type() == _type && data )
926         return;
927     if( data )
928         release();
929     CV_DbgAssert( _rows >= 0 && _cols >= 0 );
930     if( _rows > 0 && _cols > 0 )
931     {
932         flags = Mat::MAGIC_VAL + _type;
933         rows = _rows;
934         cols = _cols;
935         wholerows = _rows;
936         wholecols = _cols;
937         size_t esz = elemSize();
938
939         void *dev_ptr;
940         openCLMallocPitch(clCxt, &dev_ptr, &step, GPU_MATRIX_MALLOC_STEP(esz * cols), rows);
941         //openCLMallocPitch(clCxt,&dev_ptr, &step, esz * cols, rows);
942
943         if (esz * cols == step)
944             flags |= Mat::CONTINUOUS_FLAG;
945
946         int64 _nettosize = (int64)step * rows;
947         size_t nettosize = (size_t)_nettosize;
948
949         datastart = data = (uchar *)dev_ptr;
950         dataend = data + nettosize;
951
952         refcount = (int *)fastMalloc(sizeof(*refcount));
953         *refcount = 1;
954     }
955 }
956
957 void cv::ocl::oclMat::release()
958 {
959     //cout << "cv::ocl::oclMat::release()" << endl;
960     if( refcount && CV_XADD(refcount, -1) == 1 )
961     {
962         fastFree(refcount);
963         openCLFree(datastart);
964     }
965     data = datastart = dataend = 0;
966     step = rows = cols = 0;
967     offset = wholerows = wholecols = 0;
968     refcount = 0;
969 }
970
971 oclMat& cv::ocl::oclMat::operator+=( const oclMat& m )
972 {
973     add(*this, m, *this);
974     return *this;
975 }
976
977 oclMat& cv::ocl::oclMat::operator-=( const oclMat& m )
978 {
979     subtract(*this, m, *this);
980     return *this;
981 }
982
983 oclMat& cv::ocl::oclMat::operator*=( const oclMat& m )
984 {
985     multiply(*this, m, *this);
986     return *this;
987 }
988
989 oclMat& cv::ocl::oclMat::operator/=( const oclMat& m )
990 {
991     divide(*this, m, *this);
992     return *this;
993 }