From bc85979d57b61ded28439f09cd0641eb92e6b3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Fri, 17 Apr 2020 15:32:58 +0200 Subject: rhi: Make Qt3DQuickWindow work with RHI Change-Id: Ic9bb79e780006e49d1baa13fd15f52344b81c86b Reviewed-by: Paul Lemire --- src/extras/defaults/qt3dwindow.cpp | 161 ++++++++++----------- src/extras/defaults/qt3dwindow.h | 3 + .../rhi/graphicshelpers/submissioncontext.cpp | 1 - .../rhi/graphicshelpers/submissioncontext_p.h | 4 - src/quick3d/quick3dextras/qt3dquickwindow.cpp | 17 +-- src/quick3d/quick3dextras/quick3dextras.pro | 2 +- 6 files changed, 79 insertions(+), 109 deletions(-) (limited to 'src') diff --git a/src/extras/defaults/qt3dwindow.cpp b/src/extras/defaults/qt3dwindow.cpp index fa48dba50..6d1ab8327 100644 --- a/src/extras/defaults/qt3dwindow.cpp +++ b/src/extras/defaults/qt3dwindow.cpp @@ -60,6 +60,7 @@ #include #include #include +#include #include #include @@ -105,95 +106,9 @@ Qt3DWindow::Qt3DWindow(QScreen *screen, Qt3DRender::API api) if (!d->parentWindow) d->connectToScreen(screen ? screen : d->topLevelScreen.data()); - // If the user pass an API through the environment, we use that over the one passed as argument. - const auto userRequestedApi = qgetenv("QT3D_RHI_DEFAULT_API").toLower(); - if (!userRequestedApi.isEmpty()) { - if (userRequestedApi == QByteArrayLiteral("opengl")) { - api = Qt3DRender::API::OpenGL; - } else if (userRequestedApi == QByteArrayLiteral("vulkan")) { - api = Qt3DRender::API::Vulkan; - } else if (userRequestedApi == QByteArrayLiteral("metal")) { - api = Qt3DRender::API::Metal; - } else if (userRequestedApi == QByteArrayLiteral("d3d11")) { - api = Qt3DRender::API::DirectX; - } else if (userRequestedApi == QByteArrayLiteral("null")) { - api = Qt3DRender::API::Null; - } - } - - // We have to set the environment so that the backend is able to read it. - // Qt6: FIXME - switch (api) - { - case Qt3DRender::API::OpenGL: - qputenv("QT3D_RHI_DEFAULT_API", "opengl"); - setSurfaceType(QSurface::OpenGLSurface); - break; - case Qt3DRender::API::DirectX: - qputenv("QT3D_RHI_DEFAULT_API", "d3d11"); - setSurfaceType(QSurface::OpenGLSurface); - break; - case Qt3DRender::API::Null: - qputenv("QT3D_RHI_DEFAULT_API", "null"); - setSurfaceType(QSurface::OpenGLSurface); - break; - case Qt3DRender::API::Metal: - qputenv("QT3D_RHI_DEFAULT_API", "metal"); - setSurfaceType(QSurface::MetalSurface); - break; -#if QT_CONFIG(vulkan) - case Qt3DRender::API::Vulkan: - { - static QVulkanInstance inst; -#ifndef Q_OS_ANDROID - inst.setLayers(QByteArrayList() << "VK_LAYER_LUNARG_standard_validation"); -#else - inst.setLayers(QByteArrayList() - << "VK_LAYER_GOOGLE_threading" - << "VK_LAYER_LUNARG_parameter_validation" - << "VK_LAYER_LUNARG_object_tracker" - << "VK_LAYER_LUNARG_core_validation" - << "VK_LAYER_LUNARG_image" - << "VK_LAYER_LUNARG_swapchain" - << "VK_LAYER_GOOGLE_unique_objects"); -#endif - inst.setExtensions(QByteArrayList() - << "VK_KHR_get_physical_device_properties2"); - Q_ASSERT (inst.create()); - setVulkanInstance(&inst); - - qputenv("QT3D_RHI_DEFAULT_API", "vulkan"); - setSurfaceType(QSurface::VulkanSurface); - break; - } -#endif - default: - break; - } - + setupWindowSurface(this, api); resize(1024, 768); - - QSurfaceFormat format = QSurfaceFormat::defaultFormat(); -#ifdef QT_OPENGL_ES_2 - format.setRenderableType(QSurfaceFormat::OpenGLES); -#else - if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { - format.setVersion(4, 3); - format.setProfile(QSurfaceFormat::CoreProfile); - } -#endif - if (!userRequestedApi.isEmpty()) { - // This is used for RHI - format.setVersion(1, 0); - } - - format.setDepthBufferSize(24); - format.setSamples(4); - format.setStencilBufferSize(8); - setFormat(format); - QSurfaceFormat::setDefaultFormat(format); - d->m_aspectEngine->registerAspect(d->m_renderAspect); d->m_aspectEngine->registerAspect(d->m_inputAspect); d->m_aspectEngine->registerAspect(d->m_logicAspect); @@ -301,7 +216,6 @@ void Qt3DWindow::showEvent(QShowEvent *e) d->m_initialized = true; } - QWindow::showEvent(e); } @@ -331,6 +245,77 @@ bool Qt3DWindow::event(QEvent *e) return QWindow::event(e); } +void setupWindowSurface(QWindow *window, Qt3DRender::API api) noexcept +{ + // If the user pass an API through the environment, we use that over the one passed as argument. + const auto userRequestedApi = qgetenv("QT3D_RHI_DEFAULT_API").toLower(); + if (!userRequestedApi.isEmpty()) { + if (userRequestedApi == QByteArrayLiteral("opengl")) { + api = Qt3DRender::API::OpenGL; + } else if (userRequestedApi == QByteArrayLiteral("vulkan")) { + api = Qt3DRender::API::Vulkan; + } else if (userRequestedApi == QByteArrayLiteral("metal")) { + api = Qt3DRender::API::Metal; + } else if (userRequestedApi == QByteArrayLiteral("d3d11")) { + api = Qt3DRender::API::DirectX; + } else if (userRequestedApi == QByteArrayLiteral("null")) { + api = Qt3DRender::API::Null; + } + } + + // We have to set the environment so that the backend is able to read it. + // Qt6: FIXME + switch (api) + { + case Qt3DRender::API::OpenGL: + qputenv("QT3D_RHI_DEFAULT_API", "opengl"); + window->setSurfaceType(QSurface::OpenGLSurface); + break; + case Qt3DRender::API::DirectX: + qputenv("QT3D_RHI_DEFAULT_API", "d3d11"); + window->setSurfaceType(QSurface::OpenGLSurface); + break; + case Qt3DRender::API::Null: + qputenv("QT3D_RHI_DEFAULT_API", "null"); + window->setSurfaceType(QSurface::OpenGLSurface); + break; + case Qt3DRender::API::Metal: + qputenv("QT3D_RHI_DEFAULT_API", "metal"); + window->setSurfaceType(QSurface::MetalSurface); + break; +#if QT_CONFIG(vulkan) + case Qt3DRender::API::Vulkan: + { + qputenv("QT3D_RHI_DEFAULT_API", "vulkan"); + window->setSurfaceType(QSurface::VulkanSurface); + window->setVulkanInstance(&Qt3DRender::staticVulkanInstance()); + break; + } +#endif + default: + break; + } + QSurfaceFormat format = QSurfaceFormat::defaultFormat(); +#ifdef QT_OPENGL_ES_2 + format.setRenderableType(QSurfaceFormat::OpenGLES); +#else + if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { + format.setVersion(4, 3); + format.setProfile(QSurfaceFormat::CoreProfile); + } +#endif + if (!userRequestedApi.isEmpty()) { + // This is used for RHI + format.setVersion(1, 0); + } + + format.setDepthBufferSize(24); + format.setSamples(4); + format.setStencilBufferSize(8); + window->setFormat(format); + QSurfaceFormat::setDefaultFormat(format); +} + } // Qt3DExtras QT_END_NAMESPACE diff --git a/src/extras/defaults/qt3dwindow.h b/src/extras/defaults/qt3dwindow.h index 0d277e119..c982746a8 100644 --- a/src/extras/defaults/qt3dwindow.h +++ b/src/extras/defaults/qt3dwindow.h @@ -119,6 +119,9 @@ private: Q_DECLARE_PRIVATE(Qt3DWindow) }; +Q_3DEXTRASSHARED_EXPORT +void setupWindowSurface(QWindow* window, Qt3DRender::API) noexcept; + } // Qt3DExtras QT_END_NAMESPACE diff --git a/src/plugins/renderers/rhi/graphicshelpers/submissioncontext.cpp b/src/plugins/renderers/rhi/graphicshelpers/submissioncontext.cpp index 748138540..1986a4392 100644 --- a/src/plugins/renderers/rhi/graphicshelpers/submissioncontext.cpp +++ b/src/plugins/renderers/rhi/graphicshelpers/submissioncontext.cpp @@ -91,7 +91,6 @@ #if QT_CONFIG(vulkan) #include -#include #endif #include diff --git a/src/plugins/renderers/rhi/graphicshelpers/submissioncontext_p.h b/src/plugins/renderers/rhi/graphicshelpers/submissioncontext_p.h index 6a1091e51..82b08f05d 100644 --- a/src/plugins/renderers/rhi/graphicshelpers/submissioncontext_p.h +++ b/src/plugins/renderers/rhi/graphicshelpers/submissioncontext_p.h @@ -70,10 +70,6 @@ QT_BEGIN_NAMESPACE -#if QT_CONFIG(vulkan) -class QVulkanInstance; -#endif - class QAbstractOpenGLFunctions; namespace Qt3DRender { diff --git a/src/quick3d/quick3dextras/qt3dquickwindow.cpp b/src/quick3d/quick3dextras/qt3dquickwindow.cpp index dfe245936..ead45d51d 100644 --- a/src/quick3d/quick3dextras/qt3dquickwindow.cpp +++ b/src/quick3d/quick3dextras/qt3dquickwindow.cpp @@ -49,6 +49,7 @@ ****************************************************************************/ #include +#include #include "qt3dquickwindow_p.h" #include #include @@ -113,24 +114,10 @@ Qt3DQuickWindow::Qt3DQuickWindow(QWindow *parent) : QWindow(*new Qt3DQuickWindowPrivate(), parent) { Q_D(Qt3DQuickWindow); - setSurfaceType(QSurface::OpenGLSurface); resize(1024, 768); - QSurfaceFormat format = QSurfaceFormat::defaultFormat(); -#ifdef QT_OPENGL_ES_2 - format.setRenderableType(QSurfaceFormat::OpenGLES); -#else - if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { - format.setVersion(4, 3); - format.setProfile(QSurfaceFormat::CoreProfile); - } -#endif - format.setDepthBufferSize(24); - format.setSamples(4); - format.setStencilBufferSize(8); - setFormat(format); - QSurfaceFormat::setDefaultFormat(format); + Qt3DExtras::setupWindowSurface(this, Qt3DRender::API::OpenGL); d->m_renderAspect = new Qt3DRender::QRenderAspect; if (parent && parent->screen()) diff --git a/src/quick3d/quick3dextras/quick3dextras.pro b/src/quick3d/quick3dextras/quick3dextras.pro index 40de4484a..0408e8c6b 100644 --- a/src/quick3d/quick3dextras/quick3dextras.pro +++ b/src/quick3d/quick3dextras/quick3dextras.pro @@ -2,7 +2,7 @@ TARGET = Qt3DQuickExtras MODULE = 3dquickextras DEFINES += BUILD_QT3D_MODULE -QT += core core-private qml qml-private 3dcore 3dinput 3dquick 3dquick-private 3drender 3drender-private 3dlogic 3dextras +QT += core core-private qml qml-private 3dcore 3dinput 3dquick 3dquick-private 3drender 3drender-private 3dlogic 3dextras 3dextras-private CONFIG -= precompile_header -- cgit v1.2.3