OpenGL with OpenMP always segfault

一个人想着一个人 提交于 2019-12-10 19:30:12

问题


I have in my program a loop that will fill an 3D cube with pixels (GL_POINTS), so to speed up things a little I thought i could use OpenMP and separate this for loop in my multi-core processor.

The problem is that any time I use OpenMP in the loop the program segfaults, here is the code of the loop:

glBegin(GL_POINTS);
#pragma omp parallel for
for (int a = 0; a < m_width * m_height; a++)
{
    uint8_t r, g, b;
    r = m_data[a * m_channels];
    g = m_data[a * m_channels + 1];
    b = m_data[a * m_channels + 2];

    glColor3ub(r, g, b);
    glVertex3f(r / 255.0 - 0.5, g / 255.0 - 0.5, b / 255.0 - 0.5);
}
glEnd();

As you can see, the code just get some information from m_data array and then call glColor3ub and glVertex3f with it, if I run this code without the #pragma the code runs great.

The gdb shows me that the program segfaults when it reach the glColor3ub, making clear that the problem is something with openGL, maybe the function is not thread-safe? Can I make something to correct the code?


回答1:


Don't mess with a single OpenGL context and multithreading, or guard every use of OpenGL by a critical section (which won't buy you anything performance-wise). What you can probably do is use vertex arrays/buffers (which will be faster anyway) and fill their data using multiple threads before drawing it in a single thread.

What should happen if one thread sets the current color and gets unscheduled before it draws the vertex? But what definitely will happen is that the driver gets interrupted in the middle of some operation and its internal data gets totally messed up.

OpenGL is definitely not thread safe.



来源:https://stackoverflow.com/questions/7233477/opengl-with-openmp-always-segfault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!