OpenGL 与 OpenMP 总是出现段错误

发布于 2024-12-02 02:58:28 字数 674 浏览 0 评论 0原文

我的程序中有一个循环,它将用像素(GL_POINTS)填充 3D 立方体,因此为了加快速度,我认为我可以使用 OpenMP 并在我的多核处理器中分离这个 for 循环。

问题是,每当我在循环中使用 OpenMP 时,程序都会出现段错误,这是循环的代码:

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();

如您所见,代码只是从 m_data 数组中获取一些信息,然后用它调用 glColor3ub 和 glVertex3f ,如果我运行这段代码没有#pragma,代码运行得很好。

gdb 向我显示程序在到达 glColor3ub 时出现段错误,这表明问题出在 openGL 上,也许该函数不是线程安全的?我可以做一些事情来纠正代码吗?

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?

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

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

发布评论

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

评论(1

柏拉图鍀咏恒 2024-12-09 02:58:28

不要搞乱单个 OpenGL 上下文和多线程,或者通过关键部分保护 OpenGL 的每次使用(这不会给您带来任何性能方面的好处)。您可能可以做的是使用顶点数组/缓冲区(无论如何都会更快)并使用多个线程填充其数据,然后再在单个线程中绘制数据。

如果一个线程设置了当前颜色并在绘制顶点之前未调度,会发生什么情况?但肯定会发生的是,驱动程序在某些操作过程中被中断,并且其内部数据完全混乱。

OpenGL绝对不是线程安全的。

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.

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