c++ - 从上下文共享的 QOpenGLWidget 崩溃
问题描述
我在顶级小部件中有两个QOpenGLWidget
s( view1
, ) 作为子级。view2
Qt 文档说“当多个 QOpenGLWidgets 作为子级添加到同一个顶级小部件时,它们的上下文将相互共享”。所以,view1
并view2
分享 OpenGL 上下文。我试图渲染在view1
's context 中初始化的相同场景,并且应用程序在 view2's 中崩溃paintGL()
。我做错了什么?
这是简化的代码:
#include <QtGui/QtGui>
#include <QtWidgets/QOpenGLWidget>
#include <QtWidgets/QApplication>
#include <QtWidgets/QHBoxLayout>
static const char *vertexShaderSource =
"attribute vec4 posAttr;\n"
"void main() {\n"
" gl_Position = posAttr;\n"
"}\n";
static const char *fragmentShaderSource =
"void main() {\n"
" gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\n";
QOpenGLShaderProgram *program = nullptr;
QOpenGLBuffer arrayBuf;
int posAttr = -1;
class View3D : public QOpenGLWidget {
public:
View3D()
{
setMinimumSize(300, 200);
}
private:
auto initializeGL() -> void override
{
if (program)
return;
program = new QOpenGLShaderProgram(this);
program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
program->link();
posAttr = program->attributeLocation("posAttr");
program->bind();
GLfloat vertices[] = {
0.0f, 0.707f, 0.f,
-0.5f, -0.5f, 0.f,
0.5f, -0.5f, 0.f,
};
arrayBuf.create();
arrayBuf.bind();
arrayBuf.allocate(vertices, sizeof(vertices));
program->enableAttributeArray(posAttr);
program->setAttributeBuffer(posAttr, GL_FLOAT, 0, 3, 0);
program->release();
}
auto paintGL() -> void override
{
auto f = context()->functions();
const auto dpr = devicePixelRatio();
f->glViewport(0, 0, width() * dpr, height() * dpr);
f->glClear(GL_COLOR_BUFFER_BIT);
program->bind();
arrayBuf.bind();
program->enableAttributeArray(posAttr);
f->glDrawArrays(GL_TRIANGLES, 0, 3);
program->disableAttributeArray(posAttr);
arrayBuf.release();
program->release();
}
};
auto main(int argc, char **argv) -> int
{
QApplication app{argc, argv};
QWidget w;
auto hbox = new QHBoxLayout{&w};
hbox->addWidget(new View3D); // view1
hbox->addWidget(new View3D); // view2
w.show();
return app.exec();
}
解决方案
似乎顶点状态不应在上下文之间共享,即使这些上下文是共享的。
这是固定版本:
#include <QtGui/QtGui>
#include <QtWidgets/QOpenGLWidget>
#include <QtWidgets/QApplication>
#include <QtWidgets/QHBoxLayout>
static const char *vertexShaderSource =
"attribute vec4 posAttr;\n"
"void main() {\n"
" gl_Position = posAttr;\n"
"}\n";
static const char *fragmentShaderSource =
"void main() {\n"
" gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"}\n";
QOpenGLBuffer *arrayBuf = nullptr;
QOpenGLShaderProgram *program = nullptr;
class View3D : public QOpenGLWidget {
public:
View3D()
{
setMinimumSize(300, 200);
}
private:
int posAttr = -1;
auto initializeGL() -> void override
{
if (!arrayBuf) {
arrayBuf = new QOpenGLBuffer;
arrayBuf->create();
arrayBuf->bind();
GLfloat vertices[] = {
0.0f, 0.707f, 0.f,
-0.5f, -0.5f, 0.f,
0.5f, -0.5f, 0.f,
};
arrayBuf->allocate(vertices, sizeof(vertices));
}
if (!program) {
program = new QOpenGLShaderProgram(this);
program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
program->link();
}
posAttr = program->attributeLocation("posAttr");
program->bind();
arrayBuf->bind();
program->enableAttributeArray(posAttr);
program->setAttributeBuffer(posAttr, GL_FLOAT, 0, 3, 0);
program->release();
}
auto paintGL() -> void override
{
auto f = context()->functions();
const auto dpr = devicePixelRatio();
f->glViewport(0, 0, width() * dpr, height() * dpr);
f->glClear(GL_COLOR_BUFFER_BIT);
program->bind();
arrayBuf->bind();
program->enableAttributeArray(posAttr);
f->glDrawArrays(GL_TRIANGLES, 0, 3);
program->disableAttributeArray(posAttr);
arrayBuf->release();
program->release();
}
};
auto main(int argc, char **argv) -> int
{
QApplication app{argc, argv};
QWidget w;
auto hbox = new QHBoxLayout{&w};
hbox->addWidget(new View3D); // view1
hbox->addWidget(new View3D); // view2
w.show();
return app.exec();
}
从观察来看,共享顶点属性状态也会导致问题。我知道我不能通过 OpenGL 规范共享 VAO,但我从未在这里明确使用过 VAO。
我不确定这是否受到规范或驱动程序错误的抑制。但是,至少在 NVidia 驱动程序上,很明显您不应该在任何情况下共享顶点属性状态,包括共享上下文。
推荐阅读
- javascript - 如何修复'无法读取'null'的属性'id'
- dart - 在平面图上放置点的建议
- http - 无法使用证书身份验证将 Microsoft Flows 与 HTTP 请求连接
- r - R dplyr:使用自定义函数的逐行操作
- python - 如何使用默认和非托管数据库运行 Django 单元测试?
- elasticsearch - elasticsearch 术语聚合查询不返回任何结果
- node.js - 如何从 Electron 渲染器进程内存转储中清理敏感的 Node.js 缓冲区?
- twilio - 如何通过 php 中的 CURL 使用 twillo 通知 API 发送批量消息?
- jqgrid - 遍历 jqGrid 更新一列的每一行
- django - Django - 按品牌和其他特征过滤类别中的产品