【问题标题】:Qt3D Crashing when removing frame graph删除框架图时Qt3D崩溃
【发布时间】:2018-03-14 20:42:36
【问题描述】:

我正在尝试使用 Qt3D 创建一个可以在同一场景中创建多个视图窗口的应用程序。我从 Qt3DWindow 和 Simple C++ Example 中的代码开始,然后开始移动。我想每个视图窗口都会定义自己的框架图(现在只使用一个简单的 QForwardRenderer)和相机,然后我会将每个窗口的框架图添加到场景中的主框架图中。

当我创建多个窗口时,一切似乎都运行良好,但是当我关闭窗口并开始删除框架图时,应用程序崩溃了。它在 Qt3DCore 或 Qt3DRender 模块的某个后台线程上崩溃,我无法访问源代码。据我了解,我应该能够在运行时动态修改框架图,但这不是线程安全的吗?您是否希望将一个框架图批量替换为另一个框架图,而不是像我一样修改活动框架图?

--- 编辑---

我做了更多的测试,如果在从父框架图中删除它的框架图后延迟破坏 QWindow(即它试图渲染到的表面),我不会崩溃。我确实但是在控制台上收到一些警告说:

Qt3D.Renderer.Backend: bool __cdecl Qt3DRender::Render::GraphicsContext::makeCurrent(class QSurface *) makeCurrent 失败

我的猜测是这是一个线程问题,后端仍在尝试使用 QSurface 在主线程上被销毁后进行渲染。我不太喜欢我的解决方案(我只是使用了一个单次计时器来将破坏窗口延迟 1 秒),但总比崩溃要好。

RenderWindow.h

#ifndef RENDERWINDOW_H
#define RENDERWINDOW_H
#include <QWindow>
#include <Qt3DCore>
#include <Qt3DRender>
#include <Qt3DInput>
#include <Qt3DExtras/QForwardRenderer>

class RenderWindow : public QWindow
{
public:
  RenderWindow(QScreen* screen = nullptr);
  ~RenderWindow();

  Qt3DRender::QCamera* camera() const;
  Qt3DRender::QFrameGraphNode* frameGraph() const;

protected:
  void resizeEvent(QResizeEvent *) override;

private:
  // Rendering
  Qt3DRender::QFrameGraphNode* mpFrameGraph;
  Qt3DRender::QCamera* mpCamera;

  static bool msFormatDefined;
};

#endif // RENDERWINDOW_H

RenderWindow.cpp

#include "renderwindow.h"
#include <QDebug>

bool RenderWindow::msFormatDefined = false;

namespace
{
    // Different clear colors so that it's obvious each window is using a
    // different camera and frame graph.
    static QColor sClearColors[] = {
        Qt::darkBlue,
        Qt::blue,
        Qt::darkCyan,
        Qt::cyan
    };
    static int sViewCount = 0;
}

RenderWindow::RenderWindow(QScreen* screen)
    : QWindow(screen)
    , mpFrameGraph(nullptr)
    , mpCamera(new Qt3DRender::QCamera)
{
    setSurfaceType(QWindow::OpenGLSurface);

    // Set the default surface format once
    if (!msFormatDefined)
    {
        QSurfaceFormat format;
        format.setVersion(4, 3);
        format.setProfile(QSurfaceFormat::CoreProfile);
        format.setDepthBufferSize(24);
        format.setSamples(4);
        format.setStencilBufferSize(8);
        setFormat(format);

        QSurfaceFormat::setDefaultFormat(format);
        msFormatDefined = true;
    }

    // Camera
    mpCamera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
    mpCamera->setPosition(QVector3D(0, 0, 40.0f));
    mpCamera->setViewCenter(QVector3D(0, 0, 0));

    // Frame Graph (using forward renderer for now)
    Qt3DExtras::QForwardRenderer* renderer = new Qt3DExtras::QForwardRenderer;
    renderer->setCamera(mpCamera);
    renderer->setSurface(this);
    renderer->setClearColor(sClearColors[sViewCount++ % 4]);
    mpFrameGraph = renderer;
}

RenderWindow::~RenderWindow()
{
    qDebug() << "start ~RenderWindow";

    // Unparent objects.  Probably not necessary but it makes me feel
    // good inside.
    mpFrameGraph->setParent(static_cast<Qt3DCore::QNode*>(nullptr));
    mpCamera->setParent(static_cast<Qt3DCore::QNode*>(nullptr));

    delete mpFrameGraph;
    delete mpCamera;

    qDebug() << "end ~RenderWindow";
}

Qt3DRender::QCamera* RenderWindow::camera() const
{
    return mpCamera;
}

Qt3DRender::QFrameGraphNode* RenderWindow::frameGraph() const
{
    return mpFrameGraph;
}

void RenderWindow::resizeEvent(QResizeEvent *)
{
    mpCamera->setAspectRatio((float)width()/(float)height());
}

Scene.h

#ifndef SCENE_H
#define SCENE_H
#include <Qt3DCore/QEntity>

#include <Qt3DInput/QInputAspect>

#include <Qt3DRender/QFrameGraphNode>
#include <Qt3DRender/QRenderAspect>
#include <Qt3DRender/QRenderSettings>

class RenderWindow;

class Scene
{
public:
    Scene();
    ~Scene();

    Qt3DCore::QEntityPtr rootNode() const;

    void addView(RenderWindow* window);

private:
    void setupScene();

private:
    Qt3DCore::QEntityPtr mpRoot;

    // Frame Graph
    Qt3DRender::QFrameGraphNode* mpFrameGraph;
    Qt3DRender::QRenderSettings* mpRenderSettings;

    // Aspects
    Qt3DCore::QAspectEngine* mpEngine;
    Qt3DRender::QRenderAspect* mpRenderAspect;
    Qt3DInput::QInputAspect* mpInputAspect;
};

#endif // SCENE_H

Scene.cpp

#include "scene.h"

#include <QDebug>
#include <QPropertyAnimation>

#include <Qt3DCore/QTransform>

#include <Qt3DRender/QClearBuffers>

#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QCylinderMesh>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QTorusMesh>

#include "orbittransformcontroller.h"
#include "RenderWindow.h"

Scene::Scene()
    : mpRoot(nullptr)
    , mpFrameGraph(new Qt3DRender::QFrameGraphNode)
    , mpRenderSettings(new Qt3DRender::QRenderSettings)
    , mpEngine(new Qt3DCore::QAspectEngine)
    , mpRenderAspect(new Qt3DRender::QRenderAspect)
    , mpInputAspect(new Qt3DInput::QInputAspect)
{
    mpEngine->registerAspect(mpRenderAspect);

    mpRenderSettings->setActiveFrameGraph(mpFrameGraph);

    setupScene();

    mpRoot->addComponent(mpRenderSettings);
    mpEngine->setRootEntity(mpRoot);
}

Scene::~Scene()
{
    qDebug() << "start ~Scene";

    mpEngine->setRootEntity(Qt3DCore::QEntityPtr());
    mpRoot.clear();

    delete mpEngine;
    // mpRenderSettings and mpFrameGraph are children of the
    // root node and are automatically destroyed when it is.

    qDebug() << "end ~Scene";
}

Qt3DCore::QEntityPtr Scene::rootNode() const
{
    return mpRoot;
}

void Scene::addView(RenderWindow* window)
{
    // Add the window's frame graph to the main frame graph
    if (window->frameGraph())
    {
        window->frameGraph()->setParent(mpFrameGraph);
    }
}

void Scene::setupScene()
{
    mpRoot.reset(new Qt3DCore::QEntity);

    Qt3DCore::QEntity* entity = new Qt3DCore::QEntity;
    entity->setParent(mpRoot.data());

    // Create the material
    Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial(entity);
    material->setAmbient(Qt::black);
    material->setDiffuse(QColor(196, 196, 32));
    material->setSpecular(Qt::white);

    // Torrus
    Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(entity);
    Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh;
    torusMesh->setRadius(5);
    torusMesh->setMinorRadius(1);
    torusMesh->setRings(100);
    torusMesh->setSlices(20);

    Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform;
    torusTransform->setScale3D(QVector3D(1.5, 1, 0.5));
    torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), -45.0f));

    torusEntity->addComponent(torusMesh);
    torusEntity->addComponent(torusTransform);
    torusEntity->addComponent(material);

    // Sphere
    Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(entity);
    Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh;
    sphereMesh->setRadius(3);

    Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;
    /*OrbitTransformController *controller = new OrbitTransformController(sphereTransform);
    controller->setTarget(sphereTransform);
    controller->setRadius(20.0f);

    QPropertyAnimation *sphereRotateTransformAnimation = new QPropertyAnimation(sphereTransform);
    sphereRotateTransformAnimation->setTargetObject(controller);
    sphereRotateTransformAnimation->setPropertyName("angle");
    sphereRotateTransformAnimation->setStartValue(QVariant::fromValue(0));
    sphereRotateTransformAnimation->setEndValue(QVariant::fromValue(360));
    sphereRotateTransformAnimation->setDuration(10000);
    sphereRotateTransformAnimation->setLoopCount(-1);
    sphereRotateTransformAnimation->start();*/

    sphereEntity->addComponent(sphereMesh);
    sphereEntity->addComponent(sphereTransform);
    sphereEntity->addComponent(material);
}

MainWindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include "scene.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void createWindow();

private:
    Ui::MainWindow *ui;
    Scene* scene;
};

#endif // MAINWINDOW_H

MainWindow.cpp

#include "mainwindow.h"
#include <QDebug>
#include "ui_mainwindow.h"
#include "renderwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    scene(new Scene())
{
    ui->setupUi(this);
    connect(ui->createButton, &QPushButton::clicked, this, &MainWindow::createWindow);
}

MainWindow::~MainWindow()
{
    qDebug() << "~MainWindow";

    delete scene;
    delete ui;
}

void MainWindow::createWindow()
{
    RenderWindow* window = new RenderWindow();
    scene->addView(window);
    window->resize(640, 480);
    window->show();

    QVector3D pos[] = {
        QVector3D(0, 0, 40),
        QVector3D(0, 25, -30),
        QVector3D(-20, -20, -20),
        QVector3D(40, 0, 0)
    };
    static int count = 0;
    window->camera()->setPosition(pos[count++%4]);
    window->camera()->setViewCenter(QVector3D(0, 0, 0));

    // Delete the window when it is closed.
    connect(window, &QWindow::visibilityChanged, this, [=](bool on)
    {
        if (!on)
            window->deleteLater();
    });
}

【问题讨论】:

  • 我真的很喜欢 Qt3D,但文档仍然非常有限,而且大多数示例都是针对 QML 和静态场景的,所以我仍然无法弄清楚如何做某些事情。如果有更好的方法来动态添加和删除新的渲染目标,我会全力以赴。
  • 我认为您遇到了我们最近在 Qt3D 中努力修复的一种竞争条件。您看到的问题可能已经在 Qt 5.11 中修复,但我需要先进行测试以验证这一点。如果您有时间,最好也可以在bugreports.qt.io 提交错误报告,以提高在即将发布的版本中修复它的可能性。
  • @dragly 我已经向 Qt 提交了错误报告。

标签: c++ qt qt3d


【解决方案1】:

我已经彻底测试了您的示例并得出了相同的结论。当你过快地销毁窗口时,应用程序崩溃,可能是因为 Qt3D 仍然试图向底层的 QSurface 发出一些 OpenGL 命令。我认为这是一个应该报告的错误。

此问题的“更清洁”解决方法可能是在主窗口中跟踪生成的 3d 窗口。您可以维护一个指向生成的所有窗口的指针列表(并且可能在某个时候被用户关闭)。窗口最终在主窗口的析构函数中被销毁。

【讨论】:

  • 这只是一个简单的测试应用程序,用于玩 Qt3D 的某些方面。在我的实际应用程序中,我会更好地管理我的对象。我不喜欢在应用程序本身完成之前保留未使用的窗口的想法。是否有来自框架图或其他任何形式的通知,当它完成一个我可以触发的表面时,我知道删除窗口是安全的?
  • 我完全同意你的观点,保留窗户并不是一个很好的解决方案。我用 deleteLater() 方法做了一些试验,它推迟删除窗口,直到控件返回到事件循环。不幸的是,这也不能解决问题。
【解决方案2】:

我遇到了完全相同的问题。我在对话框中创建了一个从 Qt3DWindow 派生的类,以便用户可以预览所做选择的效果,当对话框退出时程序崩溃了。事实上,在 Windows 上,这种崩溃也会导致调试器和 Qt Creator 崩溃!

我尝试以各种方式解决此问题,其中一些有所帮助,因为事实证明这是一个线程问题,已在 10 月 23 日修复:

https://github.com/qt/qt3d/commit/3314694004b825263c9b9f2d69bd85da806ccbbc

现在的修复是应用补丁,然后重新编译 Qt。我预计 5.11.3(或者可能是 5.12)很快就会发布,但是如果您在对话框中使用 Qt3D,这个错误是一个杀手。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多