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 // Third party copyrights are property of their respective owners.
18 // Jia Haipeng, jiahaipeng95@gmail.com
20 // Redistribution and use in source and binary forms, with or without modification,
21 // are permitted provided that the following conditions are met:
23 // * Redistribution's of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
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.
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.
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.
46 #include "precomp.hpp"
47 #include "opencl_kernels.hpp"
50 using namespace cv::ocl;
57 static void merge_vector_run(const oclMat *mat_src, size_t n, oclMat &mat_dst)
59 if(!mat_dst.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && mat_dst.type() == CV_64F)
61 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
65 Context *clCxt = mat_dst.clCxt;
66 int channels = mat_dst.oclchannels();
67 int depth = mat_dst.depth();
69 String kernelName = "merge_vector";
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},
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);
81 size_t localThreads[3] = { 64, 4, 1 };
82 size_t globalThreads[3] = { cols, mat_dst.rows, 1 };
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));
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));
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));
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));
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));
120 openCLExecuteKernel(clCxt, &merge_mat, kernelName, globalThreads, localThreads, args, channels, depth);
122 static void merge(const oclMat *mat_src, size_t n, oclMat &mat_dst)
127 int depth = mat_src[0].depth();
128 Size size = mat_src[0].size();
130 int total_channels = 0;
132 for(size_t i = 0; i < n; ++i)
134 CV_Assert(depth == mat_src[i].depth());
135 CV_Assert(size == mat_src[i].size());
137 total_channels += mat_src[i].oclchannels();
140 CV_Assert(total_channels <= 4);
142 if(total_channels == 1)
144 mat_src[0].copyTo(mat_dst);
148 mat_dst.create(size, CV_MAKETYPE(depth, total_channels));
149 merge_vector_run(mat_src, n, mat_dst);
151 static void split_vector_run(const oclMat &mat_src, oclMat *mat_dst)
154 if(!mat_src.clCxt->supportsFeature(FEATURE_CL_DOUBLE) && mat_src.type() == CV_64F)
156 CV_Error(Error::OpenCLDoubleNotSupported, "Selected device doesn't support double");
160 Context *clCxt = mat_src.clCxt;
161 int channels = mat_src.oclchannels();
162 int depth = mat_src.depth();
164 String kernelName = "split_vector";
166 int vector_lengths[4][7] = {{0, 0, 0, 0, 0, 0, 0},
167 {4, 4, 2, 2, 1, 1, 1},
168 {4, 4, 2, 2 , 1, 1, 1},
169 {4, 4, 2, 2, 1, 1, 1}
172 size_t vector_length = vector_lengths[channels - 1][mat_dst[0].depth()];
174 int max_offset_cols = 0;
175 for(int i = 0; i < channels; i++)
177 int offset_cols = (mat_dst[i].offset / mat_dst[i].elemSize()) & (vector_length - 1);
178 if(max_offset_cols < offset_cols)
179 max_offset_cols = offset_cols;
182 int cols = vector_length == 1 ? divUp(mat_src.cols, vector_length)
183 : divUp(mat_src.cols + max_offset_cols, vector_length);
185 size_t localThreads[3] = { 64, 4, 1 };
186 size_t globalThreads[3] = { cols, mat_src.rows, 1 };
188 int dst_step1 = mat_dst[0].cols * mat_dst[0].elemSize();
189 std::vector<std::pair<size_t , const void *> > args;
190 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_src.data));
191 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.step));
192 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.offset));
193 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[0].data));
194 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[0].step));
195 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[0].offset));
196 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[1].data));
197 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[1].step));
198 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[1].offset));
202 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[2].data));
203 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[2].step));
204 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[2].offset));
208 args.push_back( std::make_pair( sizeof(cl_mem), (void *)&mat_dst[3].data));
209 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[3].step));
210 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_dst[3].offset));
213 args.push_back( std::make_pair( sizeof(cl_int), (void *)&mat_src.rows));
214 args.push_back( std::make_pair( sizeof(cl_int), (void *)&cols));
215 args.push_back( std::make_pair( sizeof(cl_int), (void *)&dst_step1));
217 openCLExecuteKernel(clCxt, &split_mat, kernelName, globalThreads, localThreads, args, channels, depth);
219 static void split(const oclMat &mat_src, oclMat *mat_dst)
223 int depth = mat_src.depth();
224 int num_channels = mat_src.oclchannels();
225 Size size = mat_src.size();
227 if(num_channels == 1)
229 mat_src.copyTo(mat_dst[0]);
234 for(i = 0; i < num_channels; i++)
235 mat_dst[i].create(size, CV_MAKETYPE(depth, 1));
237 split_vector_run(mat_src, mat_dst);
243 void cv::ocl::merge(const oclMat *src, size_t n, oclMat &dst)
245 split_merge::merge(src, n, dst);
247 void cv::ocl::merge(const std::vector<oclMat> &src, oclMat &dst)
249 split_merge::merge(&src[0], src.size(), dst);
252 void cv::ocl::split(const oclMat &src, oclMat *dst)
254 split_merge::split(src, dst);
256 void cv::ocl::split(const oclMat &src, std::vector<oclMat> &dst)
258 dst.resize(src.oclchannels());
259 if(src.oclchannels() > 0)
260 split_merge::split(src, &dst[0]);