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-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.
19 // Niko Li, newlife20080214@gmail.com
20 // Yao Wang, bitwangyaoyao@gmail.com
22 // Redistribution and use in source and binary forms, with or without modification,
23 // are permitted provided that the following conditions are met:
25 // * Redistribution's of source code must retain the above copyright notice,
26 // this list of conditions and the following disclaimer.
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.
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.
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.
48 #include "precomp.hpp"
51 #define GPU_MATRIX_MALLOC_STEP(step) (((step) + ALIGN - 1) / ALIGN) * ALIGN
54 using namespace cv::ocl;
57 ////////////////////////////////////////////////////////////////////////
58 //////////////////////////////// oclMat ////////////////////////////////
59 ////////////////////////////////////////////////////////////////////////
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;
75 ////////////////////////////////////////////////////////////////////////
77 static void convert_C3C4(const cl_mem &src, oclMat &dst)
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];
87 sprintf(compile_option, "-D GENTYPE4=uchar4");
90 sprintf(compile_option, "-D GENTYPE4=char4");
93 sprintf(compile_option, "-D GENTYPE4=ushort4");
96 sprintf(compile_option, "-D GENTYPE4=short4");
99 sprintf(compile_option, "-D GENTYPE4=int4");
102 sprintf(compile_option, "-D GENTYPE4=float4");
105 sprintf(compile_option, "-D GENTYPE4=double4");
108 CV_Error(CV_StsUnsupportedFormat, "unknown depth");
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));
118 size_t globalThreads[3] = {((dst.wholecols * dst.wholerows + 3) / 4 + 255) / 256 * 256, 1, 1};
119 size_t localThreads[3] = {256, 1, 1};
121 openCLExecuteKernel(clCxt, &convertC3C4, kernelName, globalThreads, localThreads, args, -1, -1, compile_option);
123 ////////////////////////////////////////////////////////////////////////
125 static void convert_C4C3(const oclMat &src, cl_mem &dst)
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];
135 sprintf(compile_option, "-D GENTYPE4=uchar4");
138 sprintf(compile_option, "-D GENTYPE4=char4");
141 sprintf(compile_option, "-D GENTYPE4=ushort4");
144 sprintf(compile_option, "-D GENTYPE4=short4");
147 sprintf(compile_option, "-D GENTYPE4=int4");
150 sprintf(compile_option, "-D GENTYPE4=float4");
153 sprintf(compile_option, "-D GENTYPE4=double4");
156 CV_Error(CV_StsUnsupportedFormat, "unknown depth");
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));
167 size_t globalThreads[3] = {((src.wholecols * src.wholerows + 3) / 4 + 255) / 256 * 256, 1, 1};
168 size_t localThreads[3] = {256, 1, 1};
170 openCLExecuteKernel(clCxt, &convertC3C4, kernelName, globalThreads, localThreads, args, -1, -1, compile_option);
173 void cv::ocl::oclMat::upload(const Mat &m)
175 CV_DbgAssert(!m.empty());
178 m.locateROI(wholeSize, ofs);
179 // int type = m.type();
180 // if(m.oclchannels() == 3)
182 // type = CV_MAKETYPE(m.depth(), 4);
184 create(wholeSize, m.type());
186 if(m.channels() == 3)
188 int pitch = wholeSize.width * 3 * m.elemSize1();
189 int tail_padding = m.elemSize1() * 3072;
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);
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++)
205 // int *a = cputemp+i*wholeSize.width * 3,*b = cpudata + i*this->step/sizeof(int);
206 // for(int j=0;j<wholeSize.width;j++)
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]);
215 openCLSafeCall(clReleaseMemObject(temp));
219 openCLMemcpy2D(clCxt, data, step, m.datastart, m.step, wholeSize.width * elemSize(), wholeSize.height, clMemcpyHostToDevice);
224 offset = ofs.y * step + ofs.x * elemSize();
225 //download_channels = m.channels();
228 void cv::ocl::oclMat::download(cv::Mat &m) const
230 CV_DbgAssert(!this->empty());
232 // if(download_channels == 3)
234 // t = CV_MAKETYPE(depth(), 3);
236 m.create(wholerows, wholecols, type());
238 if(m.channels() == 3)
240 int pitch = wholecols * 3 * m.elemSize1();
241 int tail_padding = m.elemSize1() * 3072;
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);
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++)
257 // int *a = cputemp+i*wholecols * 3,*b = cpudata + i*this->step/sizeof(int);
258 // for(int j=0;j<wholecols;j++)
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]);
267 openCLSafeCall(clReleaseMemObject(temp));
271 openCLMemcpy2D(clCxt, m.data, m.step, data, step, wholecols * elemSize(), wholerows, clMemcpyDeviceToHost);
275 locateROI(wholesize, ofs);
276 m.adjustROI(-ofs.y, ofs.y + rows - wholerows, -ofs.x, ofs.x + cols - wholecols);
279 /////////////////////common//////////////////////////////////////
280 inline int divUp(int total, int grain)
282 return (total + grain - 1) / grain;
284 ///////////////////////////////////////////////////////////////////////////
285 ////////////////////////////////// CopyTo /////////////////////////////////
286 ///////////////////////////////////////////////////////////////////////////
287 static void copy_to_with_mask(const oclMat &src, oclMat &dst, const oclMat &mask, string kernelName)
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);
293 vector<pair<size_t , const void *> > args;
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"}
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];
305 globalThreads[0] = divUp(dst.cols, localThreads[0]) * localThreads[0];
306 globalThreads[1] = divUp(dst.rows, localThreads[1]) * localThreads[1];
307 globalThreads[2] = 1;
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();
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 ));
324 openCLExecuteKernel(dst.clCxt , &operator_copyToM, kernelName, globalThreads,
325 localThreads, args, -1, -1, compile_option);
328 void cv::ocl::oclMat::copyTo( oclMat &m ) const
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);
336 void cv::ocl::oclMat::copyTo( oclMat &mat, const oclMat &mask) const
344 mat.create(size(), type());
345 copy_to_with_mask(*this, mat, mask, "copy_to_with_mask");
349 ///////////////////////////////////////////////////////////////////////////
350 //////////////////////////////// ConvertTo ////////////////////////////////
351 ///////////////////////////////////////////////////////////////////////////
352 static void convert_run(const oclMat &src, oclMat &dst, double alpha, double beta)
354 string kernelName = "convert_to_S";
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)
370 globalThreads[0] = ((dst.cols + 4) / 4 + localThreads[0]) / localThreads[0] * localThreads[0];
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());
385 void cv::ocl::oclMat::convertTo( oclMat &dst, int rtype, double alpha, double beta ) const
387 //cout << "cv::ocl::oclMat::convertTo()" << endl;
389 bool noScale = fabs(alpha - 1) < std::numeric_limits<double>::epsilon()
390 && fabs(beta) < std::numeric_limits<double>::epsilon();
395 rtype = CV_MAKETYPE(CV_MAT_DEPTH(rtype), channels());
397 //int scn = channels();
398 int sdepth = depth(), ddepth = CV_MAT_DEPTH(rtype);
399 if( sdepth == ddepth && noScale )
406 const oclMat *psrc = this;
407 if( sdepth != ddepth && psrc == &dst )
408 psrc = &(temp = *this);
410 dst.create( size(), rtype );
411 convert_run(*psrc, dst, alpha, beta);
414 ///////////////////////////////////////////////////////////////////////////
415 //////////////////////////////// setTo ////////////////////////////////////
416 ///////////////////////////////////////////////////////////////////////////
417 oclMat &cv::ocl::oclMat::operator = (const Scalar &s)
419 //cout << "cv::ocl::oclMat::=" << endl;
423 static void set_to_withoutmask_run(const oclMat &dst, const Scalar &scalar, string kernelName)
425 vector<pair<size_t , const void *> > args;
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)
435 globalThreads[0] = ((dst.cols + 4) / 4 + localThreads[0] - 1) / localThreads[0] * localThreads[0];
437 char compile_option[32];
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())
458 sprintf(compile_option, "-D GENTYPE=uchar");
459 args.push_back( make_pair( sizeof(cl_uchar) , (void *)&val.uval.s[0] ));
462 sprintf(compile_option, "-D GENTYPE=uchar4");
463 args.push_back( make_pair( sizeof(cl_uchar4) , (void *)&val.uval ));
466 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
477 sprintf(compile_option, "-D GENTYPE=char");
478 args.push_back( make_pair( sizeof(cl_char) , (void *)&val.cval.s[0] ));
481 sprintf(compile_option, "-D GENTYPE=char4");
482 args.push_back( make_pair( sizeof(cl_char4) , (void *)&val.cval ));
485 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
496 sprintf(compile_option, "-D GENTYPE=ushort");
497 args.push_back( make_pair( sizeof(cl_ushort) , (void *)&val.usval.s[0] ));
500 sprintf(compile_option, "-D GENTYPE=ushort4");
501 args.push_back( make_pair( sizeof(cl_ushort4) , (void *)&val.usval ));
504 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
515 sprintf(compile_option, "-D GENTYPE=short");
516 args.push_back( make_pair( sizeof(cl_short) , (void *)&val.shval.s[0] ));
519 sprintf(compile_option, "-D GENTYPE=short4");
520 args.push_back( make_pair( sizeof(cl_short4) , (void *)&val.shval ));
523 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
534 sprintf(compile_option, "-D GENTYPE=int");
535 args.push_back( make_pair( sizeof(cl_int) , (void *)&val.ival.s[0] ));
538 sprintf(compile_option, "-D GENTYPE=int2");
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 ));
545 sprintf(compile_option, "-D GENTYPE=int4");
546 args.push_back( make_pair( sizeof(cl_int4) , (void *)&val.ival ));
549 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
560 sprintf(compile_option, "-D GENTYPE=float");
561 args.push_back( make_pair( sizeof(cl_float) , (void *)&val.fval.s[0] ));
564 sprintf(compile_option, "-D GENTYPE=float4");
565 args.push_back( make_pair( sizeof(cl_float4) , (void *)&val.fval ));
568 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
579 sprintf(compile_option, "-D GENTYPE=double");
580 args.push_back( make_pair( sizeof(cl_double) , (void *)&val.dval.s[0] ));
583 sprintf(compile_option, "-D GENTYPE=double4");
584 args.push_back( make_pair( sizeof(cl_double4) , (void *)&val.dval ));
587 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
591 CV_Error(CV_StsUnsupportedFormat, "unknown depth");
593 #ifdef CL_VERSION_1_2
594 if(dst.offset == 0 && dst.cols == dst.wholecols)
596 clEnqueueFillBuffer(dst.clCxt->impl->clCmdQueue, (cl_mem)dst.data, args[0].second, args[0].first, 0, dst.step * dst.rows, 0, NULL, NULL);
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);
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);
619 static void set_to_withmask_run(const oclMat &dst, const Scalar &scalar, const oclMat &mask, string kernelName)
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];
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())
650 sprintf(compile_option, "-D GENTYPE=uchar");
651 args.push_back( make_pair( sizeof(cl_uchar) , (void *)&val.uval.s[0] ));
654 sprintf(compile_option, "-D GENTYPE=uchar4");
655 args.push_back( make_pair( sizeof(cl_uchar4) , (void *)&val.uval ));
658 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
669 sprintf(compile_option, "-D GENTYPE=char");
670 args.push_back( make_pair( sizeof(cl_char) , (void *)&val.cval.s[0] ));
673 sprintf(compile_option, "-D GENTYPE=char4");
674 args.push_back( make_pair( sizeof(cl_char4) , (void *)&val.cval ));
677 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
688 sprintf(compile_option, "-D GENTYPE=ushort");
689 args.push_back( make_pair( sizeof(cl_ushort) , (void *)&val.usval.s[0] ));
692 sprintf(compile_option, "-D GENTYPE=ushort4");
693 args.push_back( make_pair( sizeof(cl_ushort4) , (void *)&val.usval ));
696 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
707 sprintf(compile_option, "-D GENTYPE=short");
708 args.push_back( make_pair( sizeof(cl_short) , (void *)&val.shval.s[0] ));
711 sprintf(compile_option, "-D GENTYPE=short4");
712 args.push_back( make_pair( sizeof(cl_short4) , (void *)&val.shval ));
715 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
726 sprintf(compile_option, "-D GENTYPE=int");
727 args.push_back( make_pair( sizeof(cl_int) , (void *)&val.ival.s[0] ));
730 sprintf(compile_option, "-D GENTYPE=int4");
731 args.push_back( make_pair( sizeof(cl_int4) , (void *)&val.ival ));
734 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
745 sprintf(compile_option, "-D GENTYPE=float");
746 args.push_back( make_pair( sizeof(cl_float) , (void *)&val.fval.s[0] ));
749 sprintf(compile_option, "-D GENTYPE=float4");
750 args.push_back( make_pair( sizeof(cl_float4) , (void *)&val.fval ));
753 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
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())
764 sprintf(compile_option, "-D GENTYPE=double");
765 args.push_back( make_pair( sizeof(cl_double) , (void *)&val.dval.s[0] ));
768 sprintf(compile_option, "-D GENTYPE=double4");
769 args.push_back( make_pair( sizeof(cl_double4) , (void *)&val.dval ));
772 CV_Error(CV_StsUnsupportedFormat, "unsupported channels");
776 CV_Error(CV_StsUnsupportedFormat, "unknown depth");
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);
790 oclMat &cv::ocl::oclMat::setTo(const Scalar &scalar, const oclMat &mask)
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());
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));
806 if(type() == CV_8UC1)
808 set_to_withoutmask_run(*this, scalar, "set_to_without_mask_C1_D0");
812 set_to_withoutmask_run(*this, scalar, "set_to_without_mask");
817 set_to_withmask_run(*this, scalar, mask, "set_to_with_mask");
823 oclMat cv::ocl::oclMat::reshape(int new_cn, int new_rows) const
825 if( new_rows != 0 && new_rows != rows)
829 CV_Error( CV_StsBadFunc,
831 "oclMat's number of rows can not be changed for current version" );
837 int cn = oclchannels();
845 int total_width = cols * cn;
849 if ((new_cn > total_width || total_width % new_cn != 0) && new_rows == 0)
851 new_rows = rows * total_width / new_cn;
855 if (new_rows != 0 && new_rows != rows)
859 int total_size = total_width * rows;
865 CV_Error(CV_BadStep, "The matrix is not continuous, thus its number of rows can not be changed");
869 if ((unsigned)new_rows > (unsigned)total_size)
871 CV_Error(CV_StsOutOfRange, "Bad new number of rows");
875 total_width = total_size / new_rows;
879 if (total_width * new_rows != total_size)
881 CV_Error(CV_StsBadArg, "The total number of matrix elements is not divisible by the new number of rows");
887 hdr.step = total_width * elemSize1();
893 int new_width = total_width / new_cn;
897 if (new_width * new_cn != total_width)
899 CV_Error(CV_BadNumChannels, "The total width is not divisible by the new number of channels");
903 hdr.cols = new_width;
905 hdr.wholecols = new_width;
907 hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn - 1) << CV_CN_SHIFT);
915 void cv::ocl::oclMat::create(int _rows, int _cols, int _type)
917 clCxt = Context::getContext();
920 //download_channels = CV_MAT_CN(_type);
921 //if(download_channels==3)
923 // _type = CV_MAKE_TYPE((CV_MAT_DEPTH(_type)),4);
925 if( rows == _rows && cols == _cols && type() == _type && data )
929 CV_DbgAssert( _rows >= 0 && _cols >= 0 );
930 if( _rows > 0 && _cols > 0 )
932 flags = Mat::MAGIC_VAL + _type;
937 size_t esz = elemSize();
940 openCLMallocPitch(clCxt, &dev_ptr, &step, GPU_MATRIX_MALLOC_STEP(esz * cols), rows);
941 //openCLMallocPitch(clCxt,&dev_ptr, &step, esz * cols, rows);
943 if (esz * cols == step)
944 flags |= Mat::CONTINUOUS_FLAG;
946 int64 _nettosize = (int64)step * rows;
947 size_t nettosize = (size_t)_nettosize;
949 datastart = data = (uchar *)dev_ptr;
950 dataend = data + nettosize;
952 refcount = (int *)fastMalloc(sizeof(*refcount));
957 void cv::ocl::oclMat::release()
959 //cout << "cv::ocl::oclMat::release()" << endl;
960 if( refcount && CV_XADD(refcount, -1) == 1 )
963 openCLFree(datastart);
965 data = datastart = dataend = 0;
966 step = rows = cols = 0;
967 offset = wholerows = wholecols = 0;
971 oclMat& cv::ocl::oclMat::operator+=( const oclMat& m )
973 add(*this, m, *this);
977 oclMat& cv::ocl::oclMat::operator-=( const oclMat& m )
979 subtract(*this, m, *this);
983 oclMat& cv::ocl::oclMat::operator*=( const oclMat& m )
985 multiply(*this, m, *this);
989 oclMat& cv::ocl::oclMat::operator/=( const oclMat& m )
991 divide(*this, m, *this);