我们如何使用OpenGL将OpenCL内核中计算的像素数据显示在屏幕上?

发布于 2025-01-31 21:14:29 字数 1244 浏览 2 评论 0原文

我有兴趣在C ++中编写实时射线跟踪应用程序,并且听说使用OpenCl-Opengl互操作性是做到这一点的好方法(以充分利用GPU),因此我已经开始使用此使用此功能来编写C ++项目互操作性并使用GLFW进行窗口管理。我应该提到,尽管我有一些编码经验,但在尝试此项目之前,我没有太多的C ++,也没有与OpenCL或OpenGL合作,因此,如果考虑到这一点,我将不胜感激(即,初学者 - 友好的术语是首选)。

到目前为止,我已经能够通过使用顶点缓冲区对象获得OpenCl-Opengl互操作性。我还证明,我可以使用RGBA数组(至少在CPU上)创建图像数据,将其发送到使用GlteXimage2D()的OpenGL纹理中,并使用GlblitFrameBuffer()显示它。

我的问题是我不知道如何创建能够计算像素数据的OpenCL内核,以便可以将其作为Glteximage2d()中的数据参数给出。我知道要使用互操作性,我们必须首先创建openGL对象,然后从这些对象创建opencl对象,以将数据写入这些对象共享内存,因此我假设我必须首先创建一个空的OpenGL阵列,然后创建一个OpenCL Array从中使用的对象将适当的内核在使用OpenGl数组对象作为GlTeximage2d()中的数据参数之前将其写入像素数据,但是我不确定要使用哪种样本的对象,并且没有看到任何证明这一点的示例。一个简单的示例,显示OpenCL如何为OpenGL纹理图像创建像素数据(假设有效的OpenCl-Opengl上下文)将不胜感激。请不要留下任何线路,因为我可能无法填写空白!

我上面描述的用于实现射线示踪剂的方法也很可能是不可能的,或者至少不推荐的,因此,如果是这种情况,请概述一个建议的替代方法,用于发送OpenCL内核计算的Pixel数据到OpenGl,然后随后将其绘制为屏幕。此类似的问题对我来说没有足够的细节,并且CL/GL Interop链接不起作用。答案提到可以使用渲染器而不是纹理来实现这一点,但它在用于RenderBuffer对象的Khronos OpenGL Wiki 向他们发送像素数据的唯一方法是通过像素传输操作,但我找不到有关如何以这种方式初始化数据的任何直接解释。

请注意,我正在使用OpenCL C(无C ++绑定)。

I am interested in writing a real-time ray tracing application in c++ and I heard that using OpenCL-OpenGL interoperability is a good way to do this (to make good use of the GPU), so I have started writing a c++ project using this interoperability and using GLFW for window management. I should mention that although I have some coding experience, I do not have so much in c++ and have not worked with OpenCL or OpenGL before attempting this project, so I would appreciate it if answers are given with this in mind (that is, beginner-friendly terminology is preferred).

So far I have been able to get OpenCL-OpenGL interoperability working with an example using a vertex buffer object. I have also demonstrated that I can create image data with an RGBA array (at least on the CPU), send this to an OpenGL texture with glTexImage2D() and display it using glBlitFramebuffer().

My problem is that I don't know how to create an OpenCL kernel that is able to calculate pixel data such that it can be given as the data parameter in glTexImage2D(). I understand that to use the interoperability, we must first create OpenGL objects and then create OpenCL objects from these to write the data on as these objects share memory, so I am assuming I must first create an empty OpenGL array object then create an OpenCL array object from this to apply an appropriate kernel to which would write the pixel data before using the OpenGL array object as the data parameter in glTexImage2D(), but I am not sure what kind of object to use and have not seen any examples demonstrating this. A simple example showing how OpenCL can create pixel data for an OpenGL texture image (assuming a valid OpenCL-OpenGL context) would be much appreciated. Please do not leave any line out as I might not be able to fill in the blanks!

It's also very possible that the method I described above for implementing a ray tracer is not possible or at least not recommended, so if this is the case please outline an advised alternate method for sending OpenCL kernel calculated pixel data to OpenGL and subsequently drawing this to the screen. The answer to this similar question does not go into enough detail for me and the CL/GL interop link is not working. The answer mentions that this can be achieved using a renderbuffer rather than a texture, but it says at the bottom of the Khronos OpenGL wiki for Renderbuffer Objects that the only way to send pixel data to them is via pixel transfer operations but I can not find any straightforward explanation for how to initialize data this way.

Note that I am using OpenCL c (no c++ bindings).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-07 21:14:29

从第二个para中,您将使用平台特定的glx_display / wgl_hdc和gl_context属性的opencl上下文来与OpenGL互操作,并且您可以创建一个Vertex Buffer对象,该对象可以根据OpenGL和OpenC和OpenC和OpenC L和OPENC来读取 /书写。

这是大部分工作。在OpenGL中,您可以将任何VBO复制为带有null的纹理的纹理,

glBindBuffer(GL_PIXEL_UNPACK_BUFER, myVBO);
glTexSubImage2D(GL_TEXTURE_2D, level, x, y, width, height, format, size, NULL);

这意味着从GPU存储器(Uncer Ackate Buffer)而不是CPU内存复制。

与从常规CPU内存复制一样,如果不是32位,则可能需要更改像素对齐。

From your second para you are creating an OpenCL context with a platform specific combination of GLX_DISPLAY / WGL_HDC and GL_CONTEXT properties to interoperate with OpenGL, and you can create a vertex buffer object that can be read/written as necessary by both OpenGL and OpenCL.

That's most of the work. In OpenGL you can copy any VBO into a texture with

glBindBuffer(GL_PIXEL_UNPACK_BUFER, myVBO);
glTexSubImage2D(GL_TEXTURE_2D, level, x, y, width, height, format, size, NULL);

with the NULL at the end meaning to copy from GPU memory (the unpack buffer) rather than CPU memory.

As with copying from regular CPU memory, you might also need to change the pixel alignment if it isn't 32 bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文