使OpenCL在Qt5中与OpenGL配合使用的正确方法是什么? [英] What is the correct way to get OpenCL to play nice with OpenGL in Qt5?

查看:925
本文介绍了使OpenCL在Qt5中与OpenGL配合使用的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了一些非官方资源,以了解如何使OpenCL在OpenGL和Qt5上能够很好地发挥作用,每种资源都有不同的复杂度:

I have found several unofficial sources for how to get OpenCL to play nice with OpenGL and Qt5, each with different levels of complexity:

  • https://github.com/smistad/Qt-OpenGL-OpenCL-Interoperability
  • https://github.com/petoknm/QtOpenCLGLInterop
  • http://www.krazer.com/?p=109

有这些示例很好,但是它们没有回答以下问题:最小的确切步骤是什么,才能使 Qt5 小部件程序显示以下结果:在 OpenCL内核中进行的计算,然后直接转移到由 Qt5 启动的附加的 OpenGL上下文中?

Having these examples is nice, however they don't answer the following question: What exact steps are the minimum required to have a Qt5 widgets program display the result of a calculation made in OpenCL kernel and then transferred directly to attached OpenGL context initiated by Qt5?

子问题包括:

  • 将Qt5中的OpenGL上下文公开给OpenCL的正确方法是什么?
  • 如何首先启动Qt5应用程序,以确保正确设置OpenGL上下文以与OpenCL配合使用?
  • 应如何初始化OpenCL以使其与Qt5中的OpenGL上下文兼容?
  • 要在Qt5支持的平台上运行该功能,我必须注意哪些怪癖?
  • 是不是有一种官方"方式来做到这一点,还是Digia正在开发这种方式?

请注意,我主要对使用OpenGL作为窗口小部件(而不是窗口/全屏)感兴趣.

Note, I am primarily interested in using OpenGL as a widget as opposed to a window/full-screen.

推荐答案

我通过以下策略在Mac,Linux和Windows上一起使用了Qt5和OpenCL:

I have used Qt5 and OpenCL together on mac, linux and windows with the following strategy:

  1. 创建QGLWidget和GL上下文(此示例创建两个GL上下文,一个用于QGLwidget中的Qt/可视化,一个用于OpenCL,称为mainGLContext,在执行多线程时非常有用.这两个上下文将能够共享数据.)

QGLWidget* widget = new QGLWidget;
QGLContext* mainGLContext = new QGLContext(QGLFormat::defaultFormat(), widget);
mainGLContext->create();

  1. 使用OpenGL上下文创建OpenCL上下文.这是特定于平台的.对于Linux,您使用glx,对于Windows wgl和mac cgl共享组.下面是我用来创建上下文属性以实现互操作性的函数.在Linux和Windows上使用display变量,您可以通过glXGetCurrentDisplay()和wglGetCurrentDC()来获取它.

cl_context_properties* createInteropContextProperties(
        const cl::Platform &platform,
        cl_context_properties OpenGLContext,
        cl_context_properties display) {
#if defined(__APPLE__) || defined(__MACOSX)
CGLSetCurrentContext((CGLContextObj)OpenGLContext);
CGLShareGroupObj shareGroup = CGLGetShareGroup((CGLContextObj)OpenGLContext);
if(shareGroup == NULL)
throw Exception("Not able to get sharegroup");
    cl_context_properties * cps = new cl_context_properties[3];
    cps[0] = CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE;
    cps[1] = (cl_context_properties)shareGroup;
    cps[2] = 0;

#else
#ifdef _WIN32
    // Windows
    cl_context_properties * cps = new cl_context_properties[7];
    cps[0] = CL_GL_CONTEXT_KHR;
    cps[1] = OpenGLContext;
    cps[2] = CL_WGL_HDC_KHR;
    cps[3] = display;
    cps[4] = CL_CONTEXT_PLATFORM;
    cps[5] = (cl_context_properties) (platform)();
    cps[6] = 0;
#else
    cl_context_properties * cps = new cl_context_properties[7];
    cps[0] = CL_GL_CONTEXT_KHR;
    cps[1] = OpenGLContext;
    cps[2] = CL_GLX_DISPLAY_KHR;
    cps[3] = display;
    cps[4] = CL_CONTEXT_PLATFORM;
    cps[5] = (cl_context_properties) (platform)();
    cps[6] = 0;
#endif
#endif
    return cps;
}

  1. 通常您想执行多线程处理,让一个线程执行Qt事件处理,而在另一个线程中执行一些OpenCL处理.请记住在每个线程中将GL上下文设为当前".为此,请在QGLContext对象上使用makeCurrent和moveToThread函数.您可以在此处找到有关如何执行此操作的详细信息: https ://github.com/smistad/FAST/blob/master/source/FAST/Visualization/Window.cpp

我不知道使用Qt OpenCL包装器来创建OpenCL上下文.

I don't know of a Qt OpenCL wrapper to create the OpenCL context.

这篇关于使OpenCL在Qt5中与OpenGL配合使用的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆