aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/scenegraph/qsgdefaultcontext.cpp
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@qt.io>2019-04-23 09:40:59 +0200
committerLaszlo Agocs <laszlo.agocs@qt.io>2019-07-04 10:44:26 +0200
commit341ab7708049b1a3f559b76f16393e688951a938 (patch)
tree0203da53d1adf5025fa85ecc38e0e37bce35c46f /src/quick/scenegraph/qsgdefaultcontext.cpp
parent1f01b44d20471a7f4a5029a4c0049e8296749fef (diff)
Add the graphics api independent scenegraph port
Opt in via environment variables: QSG_RHI=1 -> enable using QRhi instead of GL QSG_RHI_BACKEND -> set to vulkan, metal, d3d11, gl to override the default (the default is d3d11 on Windows, metal on Mac, gl elsewhere) Or force a given rhi backend via the existing QQuickWindow::setSceneGraphBackend(). Otherwise the default behavior is the same as before, the rhi code path is never active by default. -no-opengl builds are supported in the sense that they work and default to the software backend. However, the rhi code path cannot currently be used in such builds, even though QRhi from qtbase is fully functional with Vulkan, D3D, or Metal even when qtbase was configured with -no-opengl. This cannot be utilized by Quick atm due to OpenGL usage being all over the place in the sources corresponding to the default backend, and those host the rhi code path as well. This will be cleaned up hopefully in Qt 6, with the removal all direct OpenGL usage. Other env.vars.: QSG_RHI_DEBUG_LAYER=1 -> enable D3D debug or Vulkan validation layer (assuming the system is set up for this) QSG_RHI_SHADEREFFECT_DEBUG=1 -> print stuff from ShaderEffect QSG_SAMPLES=1,2,4,... -> MSAA sample count (but QSurfaceFormat works too) QT_D3D_ADAPTER_INDEX=0,1,... -> D3D adapter index QT_VK_PHYSICAL_DEVICE_INDEX=0,1,... -> Vulkan physical device index QSG_RHI_UINT32_INDEX=1 -> always use uint index data (both merged/unmerged, convert when needed - with some rhi backends this is implicit) QSG_RENDER_LOOP -> to override the render loop as usual. The default with RHI is threaded for Metal, threaded for Vulkan on Windows, basic for Vulkan on Linux and Android (to be checked later), while the existing rules apply for OpenGL. Not supported when running with QRhi: - particles - compressed atlases (though this is transparent to the apps) - QSGRenderNode - QQuickRenderControl - QQuickFramebufferObject - certain QQuickWindow functionality that depends directly on OpenGL - anisotropic filtering for textures - native text may lack some gamma correction - QSGEngine applicability unclear - some QML profiler logs may be incorrect or irrelevant Change-Id: I7822e99ad79e342e4166275da6e9e66498d76521 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/quick/scenegraph/qsgdefaultcontext.cpp')
-rw-r--r--src/quick/scenegraph/qsgdefaultcontext.cpp114
1 files changed, 88 insertions, 26 deletions
diff --git a/src/quick/scenegraph/qsgdefaultcontext.cpp b/src/quick/scenegraph/qsgdefaultcontext.cpp
index a5b2b04c93..ea01b0a3b4 100644
--- a/src/quick/scenegraph/qsgdefaultcontext.cpp
+++ b/src/quick/scenegraph/qsgdefaultcontext.cpp
@@ -45,8 +45,9 @@
#include <QtQuick/private/qsgdefaultglyphnode_p.h>
#include <QtQuick/private/qsgdistancefieldglyphnode_p.h>
#include <QtQuick/private/qsgdistancefieldglyphnode_p_p.h>
-#include <QtQuick/private/qsgrenderloop_p.h>
-#include <QtQuick/private/qsgdefaultlayer_p.h>
+#include <QtQuick/private/qsgopengllayer_p.h>
+#include <QtQuick/private/qsgrhisupport_p.h>
+#include <QtQuick/private/qsgrhilayer_p.h>
#include <QtQuick/private/qsgdefaultrendercontext_p.h>
#include <QtQuick/private/qsgdefaultrectanglenode_p.h>
#include <QtQuick/private/qsgdefaultimagenode_p.h>
@@ -54,25 +55,29 @@
#if QT_CONFIG(quick_sprite)
#include <QtQuick/private/qsgdefaultspritenode_p.h>
#endif
+#include <QtQuick/private/qsgrhishadereffectnode_p.h>
#include <QtGui/QOpenGLContext>
#include <QtGui/QOpenGLFramebufferObject>
-#include <QtQuick/QQuickWindow>
+#include <QtQuick/private/qquickwindow_p.h>
#include <private/qqmlglobal_p.h>
#include <algorithm>
+#include <QtGui/private/qrhi_p.h>
+#include <QtGui/private/qrhigles2_p.h>
+
QT_BEGIN_NAMESPACE
namespace QSGMultisampleAntialiasing {
class ImageNode : public QSGDefaultInternalImageNode {
public:
+ ImageNode(QSGDefaultRenderContext *rc) : QSGDefaultInternalImageNode(rc) { }
void setAntialiasing(bool) override { }
};
-
class RectangleNode : public QSGDefaultInternalRectangleNode {
public:
void setAntialiasing(bool) override { }
@@ -118,7 +123,7 @@ void QSGDefaultContext::renderContextInitialized(QSGRenderContext *renderContext
{
m_mutex.lock();
- auto openglRenderContext = static_cast<const QSGDefaultRenderContext *>(renderContext);
+ auto rc = static_cast<const QSGDefaultRenderContext *>(renderContext);
if (m_antialiasingMethod == UndecidedAntialiasing) {
if (Q_UNLIKELY(qEnvironmentVariableIsSet("QSG_ANTIALIASING_METHOD"))) {
const QByteArray aaType = qgetenv("QSG_ANTIALIASING_METHOD");
@@ -127,12 +132,8 @@ void QSGDefaultContext::renderContextInitialized(QSGRenderContext *renderContext
else if (aaType == "vertex")
m_antialiasingMethod = VertexAntialiasing;
}
- if (m_antialiasingMethod == UndecidedAntialiasing) {
- if (openglRenderContext->openglContext()->format().samples() > 0)
- m_antialiasingMethod = MsaaAntialiasing;
- else
- m_antialiasingMethod = VertexAntialiasing;
- }
+ if (m_antialiasingMethod == UndecidedAntialiasing)
+ m_antialiasingMethod = rc->msaaSampleCount() > 1 ? MsaaAntialiasing : VertexAntialiasing;
}
// With OpenGL ES, except for Angle on Windows, use GrayAntialiasing, unless
@@ -141,15 +142,23 @@ void QSGDefaultContext::renderContextInitialized(QSGRenderContext *renderContext
if (!m_distanceFieldAntialiasingDecided) {
m_distanceFieldAntialiasingDecided = true;
#ifndef Q_OS_WIN32
- if (openglRenderContext->openglContext()->isOpenGLES())
- m_distanceFieldAntialiasing = QSGGlyphNode::GrayAntialiasing;
+ if (rc->rhi()) {
+ if (rc->rhi()->backend() == QRhi::OpenGLES2
+ && static_cast<const QRhiGles2NativeHandles *>(rc->rhi()->nativeHandles())->context->isOpenGLES())
+ {
+ m_distanceFieldAntialiasing = QSGGlyphNode::GrayAntialiasing;
+ }
+ } else {
+ if (rc->openglContext()->isOpenGLES())
+ m_distanceFieldAntialiasing = QSGGlyphNode::GrayAntialiasing;
+ }
#endif
}
static bool dumped = false;
- if (!dumped && QSG_LOG_INFO().isDebugEnabled()) {
+ if (!dumped && QSG_LOG_INFO().isDebugEnabled() && !rc->rhi()) {
dumped = true;
- QSurfaceFormat format = openglRenderContext->openglContext()->format();
+ QSurfaceFormat format = rc->openglContext()->format();
QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions();
qCDebug(QSG_LOG_INFO, "R/G/B/A Buffers: %d %d %d %d", format.redBufferSize(),
format.greenBufferSize(), format.blueBufferSize(), format.alphaBufferSize());
@@ -160,10 +169,10 @@ void QSGDefaultContext::renderContextInitialized(QSGRenderContext *renderContext
qCDebug(QSG_LOG_INFO, "GL_RENDERER: %s",
(const char*)funcs->glGetString(GL_RENDERER));
qCDebug(QSG_LOG_INFO, "GL_VERSION: %s", (const char*)funcs->glGetString(GL_VERSION));
- QByteArrayList exts = openglRenderContext->openglContext()->extensions().values();
+ QByteArrayList exts = rc->openglContext()->extensions().values();
std::sort(exts.begin(), exts.end());
qCDebug(QSG_LOG_INFO, "GL_EXTENSIONS: %s", exts.join(' ').constData());
- qCDebug(QSG_LOG_INFO, "Max Texture Size: %d", openglRenderContext->maxTextureSize());
+ qCDebug(QSG_LOG_INFO, "Max Texture Size: %d", rc->maxTextureSize());
qCDebug(QSG_LOG_INFO, "Debug context: %s",
format.testOption(QSurfaceFormat::DebugContext) ? "true" : "false");
}
@@ -187,11 +196,11 @@ QSGInternalRectangleNode *QSGDefaultContext::createInternalRectangleNode()
: new QSGDefaultInternalRectangleNode;
}
-QSGInternalImageNode *QSGDefaultContext::createInternalImageNode()
+QSGInternalImageNode *QSGDefaultContext::createInternalImageNode(QSGRenderContext *renderContext)
{
return m_antialiasingMethod == MsaaAntialiasing
- ? new QSGMultisampleAntialiasing::ImageNode
- : new QSGDefaultInternalImageNode;
+ ? new QSGMultisampleAntialiasing::ImageNode(static_cast<QSGDefaultRenderContext *>(renderContext))
+ : new QSGDefaultInternalImageNode(static_cast<QSGDefaultRenderContext *>(renderContext));
}
QSGPainterNode *QSGDefaultContext::createPainterNode(QQuickPaintedItem *item)
@@ -202,7 +211,7 @@ QSGPainterNode *QSGDefaultContext::createPainterNode(QQuickPaintedItem *item)
QSGGlyphNode *QSGDefaultContext::createGlyphNode(QSGRenderContext *rc, bool preferNativeGlyphNode)
{
if (m_distanceFieldDisabled || preferNativeGlyphNode) {
- return new QSGDefaultGlyphNode;
+ return new QSGDefaultGlyphNode(rc);
} else {
QSGDistanceFieldGlyphNode *node = new QSGDistanceFieldGlyphNode(rc);
node->setPreferredAntialiasingMode(m_distanceFieldAntialiasing);
@@ -212,7 +221,11 @@ QSGGlyphNode *QSGDefaultContext::createGlyphNode(QSGRenderContext *rc, bool pref
QSGLayer *QSGDefaultContext::createLayer(QSGRenderContext *renderContext)
{
- return new QSGDefaultLayer(renderContext);
+ auto rc = static_cast<const QSGDefaultRenderContext *>(renderContext);
+ if (rc->rhi())
+ return new QSGRhiLayer(renderContext);
+ else
+ return new QSGOpenGLLayer(renderContext);
}
QSurfaceFormat QSGDefaultContext::defaultSurfaceFormat() const
@@ -275,24 +288,73 @@ QSGSpriteNode *QSGDefaultContext::createSpriteNode()
}
#endif
+QSGGuiThreadShaderEffectManager *QSGDefaultContext::createGuiThreadShaderEffectManager()
+{
+ if (QSGRhiSupport::instance()->isRhiEnabled())
+ return new QSGRhiGuiThreadShaderEffectManager;
+
+ return nullptr;
+}
+
+QSGShaderEffectNode *QSGDefaultContext::createShaderEffectNode(QSGRenderContext *renderContext,
+ QSGGuiThreadShaderEffectManager *mgr)
+{
+ if (QSGRhiSupport::instance()->isRhiEnabled()) {
+ return new QSGRhiShaderEffectNode(static_cast<QSGDefaultRenderContext *>(renderContext),
+ static_cast<QSGRhiGuiThreadShaderEffectManager *>(mgr));
+ }
+
+ return nullptr;
+}
+
QSGRendererInterface::GraphicsApi QSGDefaultContext::graphicsApi() const
{
- return OpenGL;
+ return QSGRhiSupport::instance()->graphicsApi();
+}
+
+void *QSGDefaultContext::getResource(QQuickWindow *window, Resource resource) const
+{
+ if (!window)
+ return nullptr;
+
+ // Unlike the graphicsApi and shaderType and similar queries, getting a
+ // native resource is only possible when there is an initialized
+ // rendercontext, or rather, only within rendering a frame, as per
+ // QSGRendererInterface docs. This is good since getting some things is
+ // only possible within a beginFrame - endFrame with the RHI.
+
+ const QSGDefaultRenderContext *rc = static_cast<const QSGDefaultRenderContext *>(
+ QQuickWindowPrivate::get(window)->context);
+ QSGRhiSupport *rhiSupport = QSGRhiSupport::instance();
+
+ switch (resource) {
+ case OpenGLContextResource:
+ if (rhiSupport->graphicsApi() == OpenGL)
+ return rc->openglContext();
+ else
+ return const_cast<void *>(rhiSupport->rifResource(resource, rc));
+#if QT_CONFIG(vulkan)
+ case VulkanInstanceResource:
+ return window->vulkanInstance();
+#endif
+ default:
+ return const_cast<void *>(rhiSupport->rifResource(resource, rc));
+ }
}
QSGRendererInterface::ShaderType QSGDefaultContext::shaderType() const
{
- return GLSL;
+ return QSGRhiSupport::instance()->isRhiEnabled() ? RhiShader : GLSL;
}
QSGRendererInterface::ShaderCompilationTypes QSGDefaultContext::shaderCompilationType() const
{
- return RuntimeCompilation;
+ return QSGRhiSupport::instance()->isRhiEnabled() ? OfflineCompilation : RuntimeCompilation;
}
QSGRendererInterface::ShaderSourceTypes QSGDefaultContext::shaderSourceType() const
{
- return ShaderSourceString | ShaderSourceFile;
+ return QSGRhiSupport::instance()->isRhiEnabled() ? ShaderSourceFile : (ShaderSourceString | ShaderSourceFile);
}
QT_END_NAMESPACE