2012-09-24 20:28:35 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
|
|
|
|
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// @Authors
|
|
|
|
// Peng Xiao, pengxiao@multicorewareinc.com
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other oclMaterials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors as is and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
2013-03-18 18:42:27 +08:00
|
|
|
#include "precomp.hpp"
|
2012-09-24 20:28:35 +08:00
|
|
|
|
2013-02-22 18:33:16 +08:00
|
|
|
#ifndef CL_VERSION_1_2
|
|
|
|
#define CL_VERSION_1_2 0
|
|
|
|
#endif
|
2012-09-24 20:28:35 +08:00
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
namespace ocl
|
|
|
|
{
|
|
|
|
|
|
|
|
inline int divUp(int total, int grain)
|
|
|
|
{
|
|
|
|
return (total + grain - 1) / grain;
|
|
|
|
}
|
|
|
|
|
|
|
|
// provide additional methods for the user to interact with the command queue after a task is fired
|
2013-03-21 00:13:46 +08:00
|
|
|
static void openCLExecuteKernel_2(Context *clCxt , const char **source, cv::String kernelName, size_t globalThreads[3],
|
2013-02-25 00:14:01 +08:00
|
|
|
size_t localThreads[3], std::vector< std::pair<size_t, const void *> > &args, int channels,
|
2012-10-11 16:22:47 +08:00
|
|
|
int depth, char *build_options, FLUSH_MODE finish_mode)
|
2012-09-24 20:28:35 +08:00
|
|
|
{
|
|
|
|
//construct kernel name
|
|
|
|
//The rule is functionName_Cn_Dn, C represent Channels, D Represent DataType Depth, n represent an integer number
|
|
|
|
//for exmaple split_C2_D2, represent the split kernel with channels =2 and dataType Depth = 2(Data type is char)
|
2013-03-21 00:13:46 +08:00
|
|
|
cv::Stringstream idxStr;
|
2012-09-24 20:28:35 +08:00
|
|
|
if(channels != -1)
|
|
|
|
idxStr << "_C" << channels;
|
|
|
|
if(depth != -1)
|
|
|
|
idxStr << "_D" << depth;
|
|
|
|
kernelName += idxStr.str();
|
|
|
|
|
|
|
|
cl_kernel kernel;
|
|
|
|
kernel = openCLGetKernelFromSource(clCxt, source, kernelName, build_options);
|
|
|
|
|
|
|
|
if ( localThreads != NULL)
|
2012-10-11 16:22:47 +08:00
|
|
|
{
|
2012-09-24 20:28:35 +08:00
|
|
|
globalThreads[0] = divUp(globalThreads[0], localThreads[0]) * localThreads[0];
|
|
|
|
globalThreads[1] = divUp(globalThreads[1], localThreads[1]) * localThreads[1];
|
|
|
|
globalThreads[2] = divUp(globalThreads[2], localThreads[2]) * localThreads[2];
|
|
|
|
|
2012-10-22 15:14:22 +08:00
|
|
|
//size_t blockSize = localThreads[0] * localThreads[1] * localThreads[2];
|
|
|
|
cv::ocl::openCLVerifyKernel(clCxt, kernel, localThreads);
|
2012-09-24 20:28:35 +08:00
|
|
|
}
|
2012-10-22 15:14:22 +08:00
|
|
|
for(size_t i = 0; i < args.size(); i ++)
|
2012-09-24 20:28:35 +08:00
|
|
|
openCLSafeCall(clSetKernelArg(kernel, i, args[i].first, args[i].second));
|
|
|
|
|
2013-03-18 05:59:24 +08:00
|
|
|
openCLSafeCall(clEnqueueNDRangeKernel((cl_command_queue)clCxt->oclCommandQueue(), kernel, 3, NULL, globalThreads,
|
2012-10-11 16:22:47 +08:00
|
|
|
localThreads, 0, NULL, NULL));
|
2012-09-24 20:28:35 +08:00
|
|
|
|
|
|
|
switch(finish_mode)
|
|
|
|
{
|
|
|
|
case CLFINISH:
|
2013-03-18 05:59:24 +08:00
|
|
|
clFinish((cl_command_queue)clCxt->oclCommandQueue());
|
2012-09-24 20:28:35 +08:00
|
|
|
case CLFLUSH:
|
2013-03-18 05:59:24 +08:00
|
|
|
clFlush((cl_command_queue)clCxt->oclCommandQueue());
|
2012-09-24 20:28:35 +08:00
|
|
|
break;
|
|
|
|
case DISABLE:
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
openCLSafeCall(clReleaseKernel(kernel));
|
|
|
|
}
|
|
|
|
|
2013-03-21 00:13:46 +08:00
|
|
|
void openCLExecuteKernel2(Context *clCxt , const char **source, cv::String kernelName,
|
2012-10-11 16:22:47 +08:00
|
|
|
size_t globalThreads[3], size_t localThreads[3],
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > &args, int channels, int depth, FLUSH_MODE finish_mode)
|
2012-09-24 20:28:35 +08:00
|
|
|
{
|
|
|
|
openCLExecuteKernel2(clCxt, source, kernelName, globalThreads, localThreads, args,
|
2012-10-11 16:22:47 +08:00
|
|
|
channels, depth, NULL, finish_mode);
|
2012-09-24 20:28:35 +08:00
|
|
|
}
|
2013-03-21 00:13:46 +08:00
|
|
|
void openCLExecuteKernel2(Context *clCxt , const char **source, cv::String kernelName,
|
2012-10-11 16:22:47 +08:00
|
|
|
size_t globalThreads[3], size_t localThreads[3],
|
2013-02-25 00:14:01 +08:00
|
|
|
std::vector< std::pair<size_t, const void *> > &args, int channels, int depth, char *build_options, FLUSH_MODE finish_mode)
|
2012-09-24 20:28:35 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
openCLExecuteKernel_2(clCxt, source, kernelName, globalThreads, localThreads, args, channels, depth,
|
2012-10-11 16:22:47 +08:00
|
|
|
build_options, finish_mode);
|
2012-09-24 20:28:35 +08:00
|
|
|
}
|
2013-02-22 21:33:30 +08:00
|
|
|
|
2013-03-22 00:59:18 +08:00
|
|
|
cl_mem bindTexture(const oclMat &mat)
|
2013-02-22 11:23:43 +08:00
|
|
|
{
|
|
|
|
cl_mem texture;
|
|
|
|
cl_image_format format;
|
|
|
|
int err;
|
|
|
|
int depth = mat.depth();
|
|
|
|
int channels = mat.channels();
|
|
|
|
|
|
|
|
switch(depth)
|
|
|
|
{
|
|
|
|
case CV_8U:
|
|
|
|
format.image_channel_data_type = CL_UNSIGNED_INT8;
|
|
|
|
break;
|
|
|
|
case CV_32S:
|
|
|
|
format.image_channel_data_type = CL_UNSIGNED_INT32;
|
|
|
|
break;
|
|
|
|
case CV_32F:
|
|
|
|
format.image_channel_data_type = CL_FLOAT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::exception();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch(channels)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
format.image_channel_order = CL_R;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
format.image_channel_order = CL_RGB;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
format.image_channel_order = CL_RGBA;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::exception();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if CL_VERSION_1_2
|
|
|
|
cl_image_desc desc;
|
|
|
|
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
|
|
|
|
desc.image_width = mat.cols;
|
|
|
|
desc.image_height = mat.rows;
|
|
|
|
desc.image_depth = 0;
|
|
|
|
desc.image_array_size = 1;
|
|
|
|
desc.image_row_pitch = 0;
|
|
|
|
desc.image_slice_pitch = 0;
|
|
|
|
desc.buffer = NULL;
|
|
|
|
desc.num_mip_levels = 0;
|
|
|
|
desc.num_samples = 0;
|
2013-03-18 05:59:24 +08:00
|
|
|
texture = clCreateImage((cl_context)mat.clCxt->oclContext(), CL_MEM_READ_WRITE, &format, &desc, NULL, &err);
|
2013-02-22 11:23:43 +08:00
|
|
|
#else
|
|
|
|
texture = clCreateImage2D(
|
2013-03-18 18:42:27 +08:00
|
|
|
(cl_context)mat.clCxt->oclContext(),
|
2013-02-22 11:23:43 +08:00
|
|
|
CL_MEM_READ_WRITE,
|
|
|
|
&format,
|
|
|
|
mat.cols,
|
|
|
|
mat.rows,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
&err);
|
|
|
|
#endif
|
|
|
|
size_t origin[] = { 0, 0, 0 };
|
|
|
|
size_t region[] = { mat.cols, mat.rows, 1 };
|
|
|
|
|
|
|
|
cl_mem devData;
|
|
|
|
if (mat.cols * mat.elemSize() != mat.step)
|
|
|
|
{
|
2013-03-18 05:59:24 +08:00
|
|
|
devData = clCreateBuffer((cl_context)mat.clCxt->oclContext(), CL_MEM_READ_ONLY, mat.cols * mat.rows
|
2013-02-22 11:23:43 +08:00
|
|
|
* mat.elemSize(), NULL, NULL);
|
|
|
|
const size_t regin[3] = {mat.cols * mat.elemSize(), mat.rows, 1};
|
2013-03-18 05:59:24 +08:00
|
|
|
clEnqueueCopyBufferRect((cl_command_queue)mat.clCxt->oclCommandQueue(), (cl_mem)mat.data, devData, origin, origin,
|
2013-02-22 11:23:43 +08:00
|
|
|
regin, mat.step, 0, mat.cols * mat.elemSize(), 0, 0, NULL, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
devData = (cl_mem)mat.data;
|
|
|
|
}
|
|
|
|
|
2013-03-18 05:59:24 +08:00
|
|
|
clEnqueueCopyBufferToImage((cl_command_queue)mat.clCxt->oclCommandQueue(), devData, texture, 0, origin, region, 0, NULL, 0);
|
2013-02-22 11:23:43 +08:00
|
|
|
if ((mat.cols * mat.elemSize() != mat.step))
|
|
|
|
{
|
2013-03-18 05:59:24 +08:00
|
|
|
clFinish((cl_command_queue)mat.clCxt->oclCommandQueue());
|
2013-02-22 11:23:43 +08:00
|
|
|
clReleaseMemObject(devData);
|
|
|
|
}
|
|
|
|
|
|
|
|
openCLSafeCall(err);
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
void releaseTexture(cl_mem& texture)
|
|
|
|
{
|
|
|
|
openCLFree(texture);
|
|
|
|
}
|
2013-02-27 10:24:03 +08:00
|
|
|
|
|
|
|
bool support_image2d(Context *clCxt)
|
|
|
|
{
|
|
|
|
static const char * _kernel_string = "__kernel void test_func(image2d_t img) {}";
|
|
|
|
static bool _isTested = false;
|
|
|
|
static bool _support = false;
|
|
|
|
if(_isTested)
|
|
|
|
{
|
|
|
|
return _support;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
cv::ocl::openCLGetKernelFromSource(clCxt, &_kernel_string, "test_func");
|
2013-03-18 05:59:24 +08:00
|
|
|
//_support = true;
|
2013-02-27 10:24:03 +08:00
|
|
|
}
|
|
|
|
catch (const cv::Exception& e)
|
|
|
|
{
|
|
|
|
if(e.code == -217)
|
|
|
|
{
|
|
|
|
_support = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// throw e once again
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_isTested = true;
|
|
|
|
return _support;
|
|
|
|
}
|
2012-09-24 20:28:35 +08:00
|
|
|
}//namespace ocl
|
|
|
|
|
|
|
|
}//namespace cv
|