gpt4 book ai didi

c++ - glCreateShader 给出相同的 ID

转载 作者:太空宇宙 更新时间:2023-11-04 16:01:57 26 4
gpt4 key购买 nike

背景:

我的 c++/OpenGL3.1/GLSL/Qt 程序中有一个 Shader 类。我的程序使用多个基于不同 GLSL 源文件的着色器。

我的应用程序可以运行许多基于 QGLWidget 实现的不同 3D 渲染器,并且每个渲染器都创建自己的着色器。

当我创建我的第一个 3d 渲染器并初始化我的着色器时,着色器 ID 是在 glCreateShader 和 glCreateProgram 的帮助下生成的,没有任何问题。

问题:

但是当我创建第二个 3d 渲染器时,检索 ID 的 OGL 函数给出完全相同的 ID,但我希望有新的。这意味着我的两个渲染器会将数据发送到同一个 GPU 程序...

很明显,在 GPU 程序中,统一变量混合在一起,当运行第二个渲染器时,第一个渲染器显示出奇怪的渲染。

确实,当我关闭两个渲染器中的一个时,所有着色器都被杀死...第二个渲染器无法显示任何内容。

想法?

我完全迷路了,我的逻辑推论是 glCreateShader 和 glCreateProgram 根据它们自己的线程 ID 给出 ID。 QGLWidget可能运行自己的线程来调用渲染函数,可能会影响持久化...

知道如何解决这个问题吗?

最佳答案

如果您的两个 QOpenGLWidget 共享相同的父窗口,那么默认情况下它们共享相同的上下文。如果您不想那样做,更简单的方法可能是使用您的第二个 QOpenGLWidget 创建一个新的顶级小部件(任何没有父级的 QWidget)。

请注意,这与旧的 QGLWidget 类不同。 From Qt documentation :

When multiple QOpenGLWidgets are added as children to the same top-level widget, their contexts will share with each other. This does not apply for QOpenGLWidget instances that belong to different windows.

This means that all QOpenGLWidgets in the same window can access each other's sharable resources, like textures, and there is no need for an extra "global share" context, as was the case with QGLWidget.

关于c++ - glCreateShader 给出相同的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42303249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com