From 355bc691fcc0b20ebdc8b74e7cfec283bd9b3980 Mon Sep 17 00:00:00 2001 From: peng xiao Date: Thu, 2 May 2013 14:44:59 +0800 Subject: [PATCH 1/3] Add OpenCL version 1.2 query into ocl::Context::supportsFeature(). Add backwards portability for OpenCL 1.1 when OpenCV executables are compiled with OpenCL 1.2 profile support. --- modules/ocl/include/opencv2/ocl/ocl.hpp | 2 +- modules/ocl/src/initialization.cpp | 6 +++ modules/ocl/src/mcwutil.cpp | 54 ++++++++++++++----------- 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/modules/ocl/include/opencv2/ocl/ocl.hpp b/modules/ocl/include/opencv2/ocl/ocl.hpp index 613179f8b6..b46511e9b6 100644 --- a/modules/ocl/include/opencv2/ocl/ocl.hpp +++ b/modules/ocl/include/opencv2/ocl/ocl.hpp @@ -155,7 +155,7 @@ namespace cv static Context* getContext(); static void setContext(Info &oclinfo); - enum {CL_DOUBLE, CL_UNIFIED_MEM}; + enum {CL_DOUBLE, CL_UNIFIED_MEM, CL_VER_1_2}; bool supportsFeature(int ftype); size_t computeUnits(); void* oclContext(); diff --git a/modules/ocl/src/initialization.cpp b/modules/ocl/src/initialization.cpp index 856064c32e..799c49c50c 100644 --- a/modules/ocl/src/initialization.cpp +++ b/modules/ocl/src/initialization.cpp @@ -130,6 +130,7 @@ namespace cv cl_platform_id oclplatform; std::vector devices; std::vector devName; + std::string clVersion; cl_context oclcontext; cl_command_queue clCmdQueue; @@ -304,6 +305,7 @@ namespace cv char deviceName[256]; int devcienums = 0; + char clVersion[256]; for (unsigned i = 0; i < numPlatforms; ++i) { cl_uint numsdev; @@ -319,6 +321,8 @@ namespace cv Info ocltmpinfo; ocltmpinfo.impl->oclplatform = platforms[i]; + openCLSafeCall(clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, sizeof(clVersion), clVersion, NULL)); + ocltmpinfo.impl->clVersion = clVersion; for(unsigned j = 0; j < numsdev; ++j) { ocltmpinfo.impl->devices.push_back(devices[j]); @@ -997,6 +1001,8 @@ namespace cv return impl->double_support == 1; case CL_UNIFIED_MEM: return impl->unified_memory == 1; + case CL_VER_1_2: + return impl->clVersion.find("OpenCL 1.2") != string::npos; default: return false; } diff --git a/modules/ocl/src/mcwutil.cpp b/modules/ocl/src/mcwutil.cpp index 15df8e044a..ed3258b3d7 100644 --- a/modules/ocl/src/mcwutil.cpp +++ b/modules/ocl/src/mcwutil.cpp @@ -124,7 +124,7 @@ namespace cv build_options, finish_mode); } - cl_mem bindTexture(const oclMat &mat) + cl_mem bindTexture(const oclMat &mat) { cl_mem texture; cl_image_format format; @@ -162,30 +162,37 @@ namespace cv CV_Error(-1, "Image forma is not supported"); break; } + if(Context::getContext()->supportsFeature(Context::CL_VER_1_2)) + { #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; - texture = clCreateImage((cl_context)mat.clCxt->oclContext(), CL_MEM_READ_WRITE, &format, &desc, NULL, &err); + 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; + texture = clCreateImage((cl_context)mat.clCxt->oclContext(), CL_MEM_READ_WRITE, &format, &desc, NULL, &err); #else - texture = clCreateImage2D( - (cl_context)mat.clCxt->oclContext(), - CL_MEM_READ_WRITE, - &format, - mat.cols, - mat.rows, - 0, - NULL, - &err); + CV_Error(CV_StsBadFunc, "Non-deprecated image creation API call is not supported."); #endif + } + else + { + texture = clCreateImage2D( + (cl_context)mat.clCxt->oclContext(), + CL_MEM_READ_WRITE, + &format, + mat.cols, + mat.rows, + 0, + NULL, + &err); + } size_t origin[] = { 0, 0, 0 }; size_t region[] = { mat.cols, mat.rows, 1 }; @@ -198,7 +205,7 @@ namespace cv clEnqueueCopyBufferRect((cl_command_queue)mat.clCxt->oclCommandQueue(), (cl_mem)mat.data, devData, origin, origin, regin, mat.step, 0, mat.cols * mat.elemSize(), 0, 0, NULL, NULL); clFlush((cl_command_queue)mat.clCxt->oclCommandQueue()); - } + } else { devData = (cl_mem)mat.data; @@ -214,7 +221,6 @@ namespace cv openCLSafeCall(err); return texture; } - void releaseTexture(cl_mem& texture) { openCLFree(texture); From 1eca49f40b213d03751cb1667887261e4c8c4d5a Mon Sep 17 00:00:00 2001 From: peng xiao Date: Fri, 3 May 2013 09:45:56 +0800 Subject: [PATCH 2/3] ocl: Enable backward binary portability for setTo function. --- modules/ocl/src/matrix_operations.cpp | 20 ++++++++------------ modules/ocl/src/mcwutil.cpp | 14 +++++--------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/modules/ocl/src/matrix_operations.cpp b/modules/ocl/src/matrix_operations.cpp index 87d1d375ef..268a1fe9b5 100644 --- a/modules/ocl/src/matrix_operations.cpp +++ b/modules/ocl/src/matrix_operations.cpp @@ -593,11 +593,16 @@ static void set_to_withoutmask_run(const oclMat &dst, const Scalar &scalar, stri CV_Error(CV_StsUnsupportedFormat, "unknown depth"); } #ifdef CL_VERSION_1_2 - if(dst.offset == 0 && dst.cols == dst.wholecols) + //this enables backwards portability to + //run on OpenCL 1.1 platform if library binaries are compiled with OpenCL 1.2 support + if(Context::getContext()->supportsFeature(Context::CL_VER_1_2) && + dst.offset == 0 && dst.cols == dst.wholecols) { - clEnqueueFillBuffer((cl_command_queue)dst.clCxt->oclCommandQueue(), (cl_mem)dst.data, args[0].second, args[0].first, 0, dst.step * dst.rows, 0, NULL, NULL); + clEnqueueFillBuffer((cl_command_queue)dst.clCxt->oclCommandQueue(), + (cl_mem)dst.data, args[0].second, args[0].first, 0, dst.step * dst.rows, 0, NULL, NULL); } else +#endif { args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data )); args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols )); @@ -605,17 +610,8 @@ static void set_to_withoutmask_run(const oclMat &dst, const Scalar &scalar, stri args.push_back( make_pair( sizeof(cl_int) , (void *)&step_in_pixel )); args.push_back( make_pair( sizeof(cl_int) , (void *)&offset_in_pixel)); openCLExecuteKernel(dst.clCxt , &operator_setTo, kernelName, globalThreads, - localThreads, args, -1, -1, compile_option); + localThreads, args, -1, -1, compile_option); } -#else - args.push_back( make_pair( sizeof(cl_mem) , (void *)&dst.data )); - args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.cols )); - args.push_back( make_pair( sizeof(cl_int) , (void *)&dst.rows )); - args.push_back( make_pair( sizeof(cl_int) , (void *)&step_in_pixel )); - args.push_back( make_pair( sizeof(cl_int) , (void *)&offset_in_pixel)); - openCLExecuteKernel(dst.clCxt , &operator_setTo, kernelName, globalThreads, - localThreads, args, -1, -1, compile_option); -#endif } static void set_to_withmask_run(const oclMat &dst, const Scalar &scalar, const oclMat &mask, string kernelName) diff --git a/modules/ocl/src/mcwutil.cpp b/modules/ocl/src/mcwutil.cpp index ed3258b3d7..013af2d8aa 100644 --- a/modules/ocl/src/mcwutil.cpp +++ b/modules/ocl/src/mcwutil.cpp @@ -45,10 +45,6 @@ #include "precomp.hpp" -#ifndef CL_VERSION_1_2 -#define CL_VERSION_1_2 0 -#endif - using namespace std; namespace cv @@ -162,9 +158,11 @@ namespace cv CV_Error(-1, "Image forma is not supported"); break; } +#ifdef CL_VERSION_1_2 + //this enables backwards portability to + //run on OpenCL 1.1 platform if library binaries are compiled with OpenCL 1.2 support if(Context::getContext()->supportsFeature(Context::CL_VER_1_2)) { -#if CL_VERSION_1_2 cl_image_desc desc; desc.image_type = CL_MEM_OBJECT_IMAGE2D; desc.image_width = mat.cols; @@ -176,12 +174,10 @@ namespace cv desc.buffer = NULL; desc.num_mip_levels = 0; desc.num_samples = 0; - texture = clCreateImage((cl_context)mat.clCxt->oclContext(), CL_MEM_READ_WRITE, &format, &desc, NULL, &err); -#else - CV_Error(CV_StsBadFunc, "Non-deprecated image creation API call is not supported."); -#endif + texture = clCreateImage((cl_context)mat.clCxt->oclContext(), CL_MEM_READ_WRITE, &format, &desc, NULL, &err); } else +#endif { texture = clCreateImage2D( (cl_context)mat.clCxt->oclContext(), From d34e7eca605cb495feb4a5448fccf4ca5779f089 Mon Sep 17 00:00:00 2001 From: peng xiao Date: Mon, 6 May 2013 17:16:45 +0800 Subject: [PATCH 3/3] Suppress warning when compiling deprecated OpenCL function on GNU compilers. --- modules/ocl/src/mcwutil.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/modules/ocl/src/mcwutil.cpp b/modules/ocl/src/mcwutil.cpp index 013af2d8aa..e56e2f15d0 100644 --- a/modules/ocl/src/mcwutil.cpp +++ b/modules/ocl/src/mcwutil.cpp @@ -179,6 +179,10 @@ namespace cv else #endif { +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif texture = clCreateImage2D( (cl_context)mat.clCxt->oclContext(), CL_MEM_READ_WRITE, @@ -188,6 +192,9 @@ namespace cv 0, NULL, &err); +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif } size_t origin[] = { 0, 0, 0 }; size_t region[] = { mat.cols, mat.rows, 1 };