简体   繁体   English

在OpenCL内核中使用OpenGL

[英]Using OpenGL in OpenCL kernel

Is there any way to use OpenGL methods when using OpenCL kernels? 使用OpenCL内核时,是否可以使用OpenGL方法? I would like to vectorize my voxel-based alogorithm using GPGPU. 我想使用GPGPU矢量化基于体素的算法。 To give a simple exmaple a paste the current code of my algorithm and maybe somebody could help me with this issue. 举一个简单的例子,粘贴我算法的当前代码,也许有人可以帮助我解决这个问题。

for(int x = 0; x < voxelWorld->getVoxelsX(); x++)
    for(int y = 0; y < voxelWorld->getVoxelsY(); y++)
        for(int z = 0; z < voxelWorld->getVoxelsZ(); z++)
        {
            glPushMatrix();
            glTranslatef((x - voxelWorld->offsetX())*voxelWorld->getVoxelSize(),
                         (y - voxelWorld->offsetY())*voxelWorld->getVoxelSize(),
                         (z - voxelWorld->offsetZ())*voxelWorld->getVoxelSize());
            if(this->doRenderWireframeWorld)
            {
                glDisable(GL_LIGHTING);
                glColor3f(1.0f, 1.0f, 1.0f);
                this->renderVoxel(false);
                glEnable(GL_LIGHTING);
            }

            if(voxelWorld->getVoxel(x, y, z) && this->doRenderVoxels)
            {
                glColor3f(0.0f, 1.0f, 0.0f);
                this->renderVoxel(true);
            }
            glPopMatrix();
        } 

I know that there should be only one loop instead of three nested loops. 我知道应该只有一个循环,而不是三个嵌套循环。 So I would change the code a little bit to: 所以我将代码稍微更改为:

for(int i = 0; i < voxelWorld->getVoxelsX() * voxelWorld->getVoxelsY() * voxelWorld->getVoxelsZ(); i++)

It would be a good start to pass the data to the OpenCL kernel, but what now? 将数据传递给OpenCL内核是一个好的开始,但是现在呢? I would like to do the rendeing inside OpenCL kernel (all the glPushMatrix , glEnable ) but I assume it is impossible. 我想在OpenCL内核(所有glPushMatrixglEnable )中进行渲染,但是我认为这是不可能的。 Does anybody have any expericence doing such things? 有人有做这种事情的经验吗?

Rendering with OpenGL is already parallelized by the GPU, you don't have to do anything further to it than just make the OpenGL drawing calls. 使用OpenGL进行渲染已经由GPU并行化了,您不必做任何其他事情,而不仅仅是进行OpenGL绘图调用。 The OpenGL implementation queues them into batches sent to the GPU, the GPU will process vertices and fragments in parallel (because that's what GPUs do). OpenGL实施将它们排入发送到GPU的批处理中,GPU将并行处理顶点和片段(因为这就是GPU的工作)。

Of course to really unleash the power of the GPU you must stop starving it by using the deprecated, old fashioned, outdated, discarded immediate mode. 当然,要真正释放GPU的功能,您必须通过使用已弃用的,过时的,过时的,废弃的即时模式来停止饥饿。 Ie don't use glBegin, glVertex, glEnd. 即不要使用glBegin,glVertex,glEnd。 Use Vertex Arrays contained in Vertex Buffer Objects. 使用“顶点缓冲区对象”中包含的“顶点数组”。

(Full disclosure: If you'd use Display Lists you could make use of GPUs parallelization power even using immediate mode. But this is not recommended for new programs.) (全部披露:如果您使用显示列表,则即使使用即时模式,也可以利用GPU并行化功能。但是,对于新程序,不建议这样做。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM