Is it possible to bind OpenCV GpuMat as an OpenGL texture?

I could not find any link other than:

http://answers.opencv.org/question/9512/how-to-bind-gpumat-to-texture/

which discusses the CUDA approach.

Ideally, I would like to update the OpenGL texture with the contents of cv::gpu::GpuMat without copying it to the CPU and without using CUDA directly (although I assume this may be needed until this function is added).

+8
opencv gpgpu cuda opengl textures
source share
2 answers

OpenCV supports OpenGL. See the header file opencv2/core/opengl_interop.hpp . You can copy the contents of GpuMat to Texture:

 cv::gpu::GpuMat d_mat(768, 1024, CV_8UC3); cv::ogl::Texture2D tex; tex.copyFrom(d_mat); tex.bind(); // draw something 

You can also use the cv::ogl::Buffer class. This is a shell for OpenGL buffer memory. This memory can be mapped to CUDA memory without copying memory:

 cv::ogl::Buffer ogl_buf(1, 1000, CV_32FC3); cv::gpu::GpuMat d_mat = ogl_buf.mapDevice(); // fill d_mat with CUDA ogl_buf.unmapDevice(); // use ogl_buf in OpenGL ogl_buf.bind(cv::ogl::Buffer::ARRAY_BUFFER); glDrawArray(...); 
+11
source share

Well, here is my understanding, maybe not 100% correct, and apologize for my non-mater-lingual-English.

GpuMat uses global memory . However, OpenGL textures are located in Texture memory , which is specifically designed for the texture equipment of the GPU (and not for CUDA cores). Texture memory is organized like regular linear 2D / 3D arrays, it can use a specific Space Filling Curve to organize its content to optimize the texture caching speed, so you cannot get a device pointer to Texture. You can only access Texture Mem directly in CUDA using the Texture Fetch (read-only) function or Surface R / W.

The current OpenCV implementation does not seem to use the cuda texture / surface function. You must copy from textures to global memory in order to link them as GpuMat s. Well, not really "binding."

This thread describes a CUDA approach that records OpenGL textures.

Either wait for OpenCV to implement the new feature, or wait for NVIDIA to have a new GPU architecture combining Texture Mem and Global Mem, or wait for someone to invent a special EMP device to crack these memories ... XD

+4
source share

All Articles