Merge pull request #4192 from avershov:opencl-opengl-buffer
This commit is contained in:
@@ -537,6 +537,27 @@ CV_EXPORTS void convertToGLTexture2D(InputArray src, Texture2D& texture);
|
||||
*/
|
||||
CV_EXPORTS void convertFromGLTexture2D(const Texture2D& texture, OutputArray dst);
|
||||
|
||||
/** @brief Maps Buffer object to process on CL side (convert to UMat).
|
||||
|
||||
Function creates CL buffer from GL one, and then constructs UMat that can be used
|
||||
to process buffer data with OpenCV functions. Note that in current implementation
|
||||
UMat constructed this way doesn't own corresponding GL buffer object, so it is
|
||||
the user responsibility to close down CL/GL buffers relationships by explicitly
|
||||
calling unmapGLBuffer() function.
|
||||
@param buffer - source Buffer object.
|
||||
@param accessFlags - data access flags (ACCESS_READ|ACCESS_WRITE).
|
||||
@return Returns UMat object
|
||||
*/
|
||||
CV_EXPORTS UMat mapGLBuffer(const Buffer& buffer, int accessFlags = ACCESS_READ|ACCESS_WRITE);
|
||||
|
||||
/** @brief Unmaps Buffer object (releases UMat, previously mapped from Buffer).
|
||||
|
||||
Function must be called explicitly by the user for each UMat previously constructed
|
||||
by the call to mapGLBuffer() function.
|
||||
@param u - source UMat, created by mapGLBuffer().
|
||||
*/
|
||||
CV_EXPORTS void unmapGLBuffer(UMat& u);
|
||||
|
||||
}} // namespace cv::ogl
|
||||
|
||||
namespace cv { namespace cuda {
|
||||
|
@@ -1804,4 +1804,84 @@ void convertFromGLTexture2D(const Texture2D& texture, OutputArray dst)
|
||||
#endif
|
||||
}
|
||||
|
||||
//void mapGLBuffer(const Buffer& buffer, UMat& dst, int accessFlags)
|
||||
UMat mapGLBuffer(const Buffer& buffer, int accessFlags)
|
||||
{
|
||||
(void)buffer; (void)accessFlags;
|
||||
#if !defined(HAVE_OPENGL)
|
||||
NO_OPENGL_SUPPORT_ERROR;
|
||||
#elif !defined(HAVE_OPENCL)
|
||||
NO_OPENCL_SUPPORT_ERROR;
|
||||
#else
|
||||
using namespace cv::ocl;
|
||||
Context& ctx = Context::getDefault();
|
||||
cl_context context = (cl_context)ctx.ptr();
|
||||
cl_command_queue clQueue = (cl_command_queue)Queue::getDefault().ptr();
|
||||
|
||||
int clAccessFlags = 0;
|
||||
switch (accessFlags & (ACCESS_READ|ACCESS_WRITE))
|
||||
{
|
||||
default:
|
||||
case ACCESS_READ|ACCESS_WRITE:
|
||||
clAccessFlags = CL_MEM_READ_WRITE;
|
||||
break;
|
||||
case ACCESS_READ:
|
||||
clAccessFlags = CL_MEM_READ_ONLY;
|
||||
break;
|
||||
case ACCESS_WRITE:
|
||||
clAccessFlags = CL_MEM_WRITE_ONLY;
|
||||
break;
|
||||
}
|
||||
|
||||
cl_int status = 0;
|
||||
cl_mem clBuffer = clCreateFromGLBuffer(context, clAccessFlags, buffer.bufId(), &status);
|
||||
if (status != CL_SUCCESS)
|
||||
CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clCreateFromGLBuffer failed");
|
||||
|
||||
gl::Finish();
|
||||
|
||||
status = clEnqueueAcquireGLObjects(clQueue, 1, &clBuffer, 0, NULL, NULL);
|
||||
if (status != CL_SUCCESS)
|
||||
CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueAcquireGLObjects failed");
|
||||
|
||||
size_t step = buffer.cols() * buffer.elemSize();
|
||||
int rows = buffer.rows();
|
||||
int cols = buffer.cols();
|
||||
int type = buffer.type();
|
||||
|
||||
UMat u;
|
||||
convertFromBuffer(clBuffer, step, rows, cols, type, u);
|
||||
return u;
|
||||
#endif
|
||||
}
|
||||
|
||||
void unmapGLBuffer(UMat& u)
|
||||
{
|
||||
(void)u;
|
||||
#if !defined(HAVE_OPENGL)
|
||||
NO_OPENGL_SUPPORT_ERROR;
|
||||
#elif !defined(HAVE_OPENCL)
|
||||
NO_OPENCL_SUPPORT_ERROR;
|
||||
#else
|
||||
using namespace cv::ocl;
|
||||
cl_command_queue clQueue = (cl_command_queue)Queue::getDefault().ptr();
|
||||
|
||||
cl_mem clBuffer = (cl_mem)u.handle(ACCESS_READ);
|
||||
|
||||
u.release();
|
||||
|
||||
cl_int status = clEnqueueReleaseGLObjects(clQueue, 1, &clBuffer, 0, NULL, NULL);
|
||||
if (status != CL_SUCCESS)
|
||||
CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clEnqueueReleaseGLObjects failed");
|
||||
|
||||
status = clFinish(clQueue);
|
||||
if (status != CL_SUCCESS)
|
||||
CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clFinish failed");
|
||||
|
||||
status = clReleaseMemObject(clBuffer);
|
||||
if (status != CL_SUCCESS)
|
||||
CV_Error(cv::Error::OpenCLApiCallError, "OpenCL: clReleaseMemObject failed");
|
||||
#endif
|
||||
}
|
||||
|
||||
}} // namespace cv::ogl
|
||||
|
Reference in New Issue
Block a user