OpenCV - OpenGL - OpenCL Interop
Hi guys
I'm writing a very basic program to monitor performance when copying from a cv::ogl::Texture2D to a cv::ogl::Buffer (using the copyTo function), and from there to an OpenCL cv::UMat (using cv::ogl::mapGLBuffer). It seems to me on paper this should work all on the GPU but I'm having trouble even running this code:
#include "mainwindow.h"
#include <QApplication>
#include <opencv2/core.hpp>
#include <opencv2/core/opengl.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/core/mat.hpp>
#include <QOpenGLWidget>
#include <QOpenGLExtraFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLExtensions>
cv::UMat cvUMat;
cv::ogl::Texture2D* cvglTexture;
cv::ogl::Buffer cvglBuffer;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
cv::ocl::setUseOpenCL(true);
assert(cv::ocl::haveOpenCL());
assert(cv::ocl::useOpenCL());
cv::ocl::Context::getDefault().create(cv::ocl::Device::TYPE_GPU);
cvglTexture->create(640,480,cv::ogl::Texture2D::Format::RGBA);
return a.exec();
}
This is the error I get:
Exception at 0x7ffc886da799, code: 0xe06d7363: C++ exception, flags=0x1 (execution cannot be continued) (first chance) in opencv_world430d!cv::UMat::deallocate
And this is my stack:
1 RaiseException KERNELBASE 0x7ffc886da799
2 CxxThrowException VCRUNTIME140D 0x7ffc67097ec7
3 cv::UMat::deallocate opencv_world430d 0x7ffc12f21236
4 cv::UMat::deallocate opencv_world430d 0x7ffc12f21387
5 cv::UMat::deallocate opencv_world430d 0x7ffc12e7f464
6 main main.cpp 30 0x7ff79585297f
7 WinMain qtmain_win.cpp 104 0x7ff79585667d
8 invoke_main exe_common.inl 107 0x7ff795854aad
9 __scrt_common_main_seh exe_common.inl 288 0x7ff79585499e
10 __scrt_common_main exe_common.inl 331 0x7ff79585485e
11 WinMainCRTStartup exe_winmain.cpp 17 0x7ff795854b39
12 BaseThreadInitThunk KERNEL32 0x7ffc894e7bd4
13 RtlUserThreadStart ntdll 0x7ffc8ac6ce51
I'm using QT and OpenCV 430 (debug) built for VC15 (I'm using the QT 5.12.0 MSVC2017 compiler) on windows. I have other projects that use cv::UMat and everything runs smooth there but looks like there are some complications with OpenGL interop. Any thoughts on where to get started and what to check would definitely be helpful!
Cheers!
Why use these features, which may work for this or that, maybe?
Why not use native OpenGL 4.3, so you can specify the compute shader and everything?
https://github.com/sjhalayka/qjs_comp...
There is no need for OpenCL.
btw:
cvglTexture is never initialized. why the pointer at all ?
please check return value !
also:
please check, if OpenGL support is enabled at all
Thanks for your replies guys!
The reason I'm trying to get the OpenGL / OpenCL interop to work is because I'm trying to use OpenCV's machine learning modules on the GPU. UMats are based on OpenCL which is great, but I need to deliver the input data in form of OpenGL textures.
I actually made a little progress with this, Berak you were correct, I thought I had OpenGL support in this build but I didn't. I build OpenCV from source with OpenGL support and was able to get past the initial issue.
The main issue I face right now is copying the OpenGL buffer data over to the OpenCL UMat. In the code I'm about to post, I'm able to copy an OpenGL texture into a buffer. This works ok, still uses CPU which if I understand correctly, it shouldn't but thats fine ...(more)
Here is some code to better clarify my issue right now:
Hmmm. I am not familiar with the cv::ogl stuff.
I use a vector of float or unsigned int to hold the texture data.
Are you familiar with the vector container?
Yes, I'm familiar with the vector container. The issue is the input will always be in the form of an OpenGL texture handle which I'll need to use to get a UMat ideally all on the GPU