diff options
Diffstat (limited to 'src/plugins/renderers/rhi/graphicshelpers')
3 files changed, 79 insertions, 172 deletions
diff --git a/src/plugins/renderers/rhi/graphicshelpers/graphicshelpers.pri b/src/plugins/renderers/rhi/graphicshelpers/graphicshelpers.pri deleted file mode 100644 index e156d3ce5..000000000 --- a/src/plugins/renderers/rhi/graphicshelpers/graphicshelpers.pri +++ /dev/null @@ -1,9 +0,0 @@ -#DEFINES += QT3D_RENDER_ASPECT_OPENGL_DEBUG - -INCLUDEPATH += $$PWD - -HEADERS += \ - $$PWD/submissioncontext_p.h - -SOURCES += \ - $$PWD/submissioncontext.cpp diff --git a/src/plugins/renderers/rhi/graphicshelpers/submissioncontext.cpp b/src/plugins/renderers/rhi/graphicshelpers/submissioncontext.cpp index 6d62a6e51..f0dcb044f 100644 --- a/src/plugins/renderers/rhi/graphicshelpers/submissioncontext.cpp +++ b/src/plugins/renderers/rhi/graphicshelpers/submissioncontext.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB). -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt3D module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB). +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "submissioncontext_p.h" @@ -62,7 +26,9 @@ #include <Qt3DRender/private/managers_p.h> #include <Qt3DRender/private/attachmentpack_p.h> #include <Qt3DRender/private/stringtoint_p.h> -#include <Qt3DRender/private/vulkaninstance_p.h> +#if QT_CONFIG(qt3d_vulkan) && QT_CONFIG(vulkan) +# include <Qt3DRender/private/vulkaninstance_p.h> +#endif #include <QGuiApplication> #include <texture_p.h> #include <rendercommand_p.h> @@ -75,46 +41,17 @@ #include <private/qdebug_p.h> #include <QSurface> #include <QWindow> -#include <QtShaderTools/private/qshaderbaker_p.h> - -#ifdef Q_OS_WIN -#include <QtGui/private/qrhid3d11_p.h> -#endif - -#if defined(Q_OS_MACOS) || defined(Q_OS_IOS) -#include <QtGui/private/qrhimetal_p.h> -#endif - -#ifndef QT_NO_OPENGL -#include <QtGui/private/qrhigles2_p.h> -#endif +#include <rhi/qrhi.h> +#include <rhi/qshaderbaker.h> -#if QT_CONFIG(qt3d_vulkan) -#include <QtGui/private/qrhivulkan_p.h> -#endif #include <bitset> QT_BEGIN_NAMESPACE -using namespace Qt3DCore; - namespace Qt3DRender { namespace Render { namespace Rhi { -static QHash<unsigned int, SubmissionContext *> static_contexts; - -unsigned int nextFreeContextId() noexcept -{ - for (unsigned int i = 0; i < 0xffff; ++i) { - if (!static_contexts.contains(i)) - return i; - } - - qFatal("Couldn't find free context ID"); - return 0; -} - namespace { //RHIBuffer::Type attributeTypeToGLBufferType(QAttribute::AttributeType type) noexcept @@ -476,6 +413,13 @@ void applyStateHelper(const StencilMask *state, QRhiGraphicsPipeline *gp) noexce gp->setStencilReadMask(std::get<1>(values)); } +void applyStateHelper(const LineWidth *state, QRhiGraphicsPipeline *gp) noexcept +{ + const auto values = state->values(); + gp->setLineWidth(std::get<0>(values)); + // no GL_LINE_SMOOTH equivalent on RHI +} + static QShader::Stage rhiShaderStage(QShaderProgram::ShaderType type) noexcept { switch (type) { @@ -499,15 +443,11 @@ static QShader::Stage rhiShaderStage(QShaderProgram::ShaderType type) noexcept } // anonymous SubmissionContext::SubmissionContext() - : m_ownsRhiCtx(false), + : m_initialized(false), + m_ownsRhiCtx(false), m_drivenExternally(false), - m_id(nextFreeContextId()), - m_surface(nullptr), - m_renderTargetFormat(QAbstractTexture::NoFormat), m_material(nullptr), m_renderer(nullptr), - m_uboTempArray(QByteArray(1024, 0)), - m_initialized(false), m_rhi(nullptr), m_currentSwapChain(nullptr), m_currentRenderPassDescriptor(nullptr), @@ -518,7 +458,6 @@ SubmissionContext::SubmissionContext() m_fallbackSurface(nullptr) #endif { - static_contexts[m_id] = this; m_contextInfo.m_api = QGraphicsApiFilter::RHI; // We set those version numbers because QShaderGenerator wants major > 0 @@ -529,9 +468,6 @@ SubmissionContext::SubmissionContext() SubmissionContext::~SubmissionContext() { releaseResources(); - - Q_ASSERT(static_contexts[m_id] == this); - static_contexts.remove(m_id); } void SubmissionContext::initialize() @@ -578,7 +514,7 @@ void SubmissionContext::initialize() QRhi::Flags rhiFlags = QRhi::EnableDebugMarkers; -#if QT_CONFIG(qt3d_vulkan) +#if QT_CONFIG(qt3d_vulkan) && QT_CONFIG(vulkan) if (requestedApi == Qt3DRender::API::Vulkan) { QRhiVulkanInitParams params; params.inst = &Qt3DRender::staticVulkanInstance(); @@ -645,8 +581,6 @@ bool SubmissionContext::beginDrawing(QSurface *surface) { Q_ASSERT(surface); - m_surface = surface; - Q_ASSERT(isInitialized()); // In the Scene3D case it does not make sense to create SwapChains as we @@ -661,7 +595,7 @@ bool SubmissionContext::beginDrawing(QSurface *surface) QRhiSwapChain *swapChain = swapChainInfo->swapChain; // Resize swapchain if needed - if (m_surface->size() != swapChain->currentPixelSize()) { + if (surface->size() != swapChain->currentPixelSize()) { bool couldRebuild = swapChain->createOrResize(); if (!couldRebuild) return false; @@ -838,6 +772,11 @@ void SubmissionContext::releaseResources() m_renderBufferHash.clear(); RHI_UNIMPLEMENTED; + if (m_currentUpdates) { + m_currentUpdates->release(); + m_currentUpdates = nullptr; + } + // Free RHI resources { qCDebug(Backend) << Q_FUNC_INFO; @@ -1308,7 +1247,7 @@ void SubmissionContext::uploadDataToRHIBuffer(Buffer *buffer, RHIBuffer *b) // We have a partial update if (update->offset >= 0) { // accumulate sequential updates as single one - int bufferSize = update->data.size(); + qsizetype bufferSize = update->data.size(); auto it2 = it + 1; while ((it2 != updates.end()) && (it2->offset - update->offset == bufferSize)) { bufferSize += it2->data.size(); @@ -1390,7 +1329,7 @@ void preprocessRHIShader(std::vector<QByteArray> &shaderCodes) "\\s*,\\s*std140.*)\\)\\s*uniform\\s*([a-zA-Z0-9_]+)")); auto replaceBinding = [&bindings, &assignedBindings]( - int &offset, QRegularExpressionMatch &match, QString &code, + qsizetype &offset, QRegularExpressionMatch &match, QString &code, int indexCapture, int variableCapture) noexcept { int index = match.captured(indexCapture).toInt(); QByteArray variable = match.captured(variableCapture).toUtf8(); @@ -1404,9 +1343,9 @@ void preprocessRHIShader(std::vector<QByteArray> &shaderCodes) return; } - const int indexStartOffset = match.capturedStart(indexCapture); - const int indexEndOffset = match.capturedEnd(indexCapture); - const int indexLength = indexEndOffset - indexStartOffset; + const qsizetype indexStartOffset = match.capturedStart(indexCapture); + const qsizetype indexEndOffset = match.capturedEnd(indexCapture); + const qsizetype indexLength = indexEndOffset - indexStartOffset; code.replace(indexStartOffset, indexLength, QByteArray::number(index)); } @@ -1414,9 +1353,9 @@ void preprocessRHIShader(std::vector<QByteArray> &shaderCodes) bindings.emplace(std::move(variable), index); } else { int indexToUse = it->second; - const int indexStartOffset = match.capturedStart(indexCapture); - const int indexEndOffset = match.capturedEnd(indexCapture); - const int indexLength = indexEndOffset - indexStartOffset; + const qsizetype indexStartOffset = match.capturedStart(indexCapture); + const qsizetype indexEndOffset = match.capturedEnd(indexCapture); + const qsizetype indexLength = indexEndOffset - indexStartOffset; code.replace(indexStartOffset, indexLength, QByteArray::number(indexToUse)); } // This may fail in the case where the replaced offset is an incredibly long number, @@ -1429,7 +1368,7 @@ void preprocessRHIShader(std::vector<QByteArray> &shaderCodes) QString shaderString = shaderCode; // Regex for the sampler variables - int offset = 0; + qsizetype offset = 0; auto match = samplerRegex.match(shaderString, offset); while (match.hasMatch()) { const int indexCapture = 1; @@ -1454,26 +1393,45 @@ void preprocessRHIShader(std::vector<QByteArray> &shaderCodes) } } -int glslVersionForFormat(const QSurfaceFormat &format) noexcept +QShaderVersion glslVersionForFormat(const QSurfaceFormat &format) noexcept { const int major = format.majorVersion(); const int minor = format.minorVersion(); - - static const QHash<std::pair<int, int>, int> glVersionToGLSLVersion = { - { { 4, 6 }, 460 }, { { 4, 5 }, 450 }, { { 4, 4 }, 440 }, { { 4, 3 }, 430 }, - { { 4, 2 }, 420 }, { { 4, 1 }, 410 }, { { 4, 0 }, 400 }, { { 3, 3 }, 330 }, - { { 3, 2 }, 150 }, { { 3, 2 }, 120 }, { { 3, 1 }, 120 }, - }; - - const auto it = glVersionToGLSLVersion.find({ major, minor }); - if (it == glVersionToGLSLVersion.end()) { - if (major < 3) { - return 120; + const auto type = format.renderableType(); + + if (type != QSurfaceFormat::OpenGLES) { + static const QHash<std::pair<int, int>, int> glVersionToGLSLVersion = { + { { 4, 6 }, 460 }, { { 4, 5 }, 450 }, { { 4, 4 }, 440 }, { { 4, 3 }, 430 }, + { { 4, 2 }, 420 }, { { 4, 1 }, 410 }, { { 4, 0 }, 400 }, { { 3, 3 }, 330 }, + { { 3, 2 }, 150 }, { { 3, 2 }, 120 }, { { 3, 1 }, 120 }, + }; + + const auto it = glVersionToGLSLVersion.find({ major, minor }); + if (it == glVersionToGLSLVersion.end()) { + if (major < 3) { + return 120; + } else { + return major * 100 + minor * 10; + } } else { - return major * 100 + minor * 10; + return *it; + } + } + else { + static const QHash<std::pair<int, int>, int> glVersionToGLSLVersion = { + { { 3, 2 }, 320 }, { { 3, 1 }, 310 }, { { 3, 0 }, 300 }, + }; + + const auto it = glVersionToGLSLVersion.find({ major, minor }); + if (it == glVersionToGLSLVersion.end()) { + if (major < 3) { + return {100, QShaderVersion::GlslEs}; + } else { + return {major * 100 + minor * 10, QShaderVersion::GlslEs}; + } + } else { + return {*it, QShaderVersion::GlslEs}; } - } else { - return *it; } } } @@ -1559,12 +1517,14 @@ void SubmissionContext::loadShader(Shader *shaderNode, ShaderManager *shaderMana const std::vector<Qt3DCore::QNodeId> &sharedShaderIds = rhiShaderManager->shaderIdsForProgram(rhiShader); if (sharedShaderIds.size() == 1) { - // Shader in the cache hasn't been loaded yet - // We want a copy of the QByteArray as preprocessRHIShader will - // modify them - std::vector<QByteArray> shaderCodes = shaderNode->shaderCode(); - preprocessRHIShader(shaderCodes); - rhiShader->setShaderCode(shaderCodes); + { + // Shader in the cache hasn't been loaded yet + // We want a copy of the QByteArray as preprocessRHIShader will + // modify them + std::vector<QByteArray> shaderCodes = shaderNode->shaderCode(); + preprocessRHIShader(shaderCodes); + rhiShader->setShaderCode(std::move(shaderCodes)); + } const ShaderCreationInfo loadResult = createShaderProgram(rhiShader); shaderNode->setStatus(loadResult.linkSucceeded ? QShaderProgram::Ready diff --git a/src/plugins/renderers/rhi/graphicshelpers/submissioncontext_p.h b/src/plugins/renderers/rhi/graphicshelpers/submissioncontext_p.h index f48c11234..bb639ccfc 100644 --- a/src/plugins/renderers/rhi/graphicshelpers/submissioncontext_p.h +++ b/src/plugins/renderers/rhi/graphicshelpers/submissioncontext_p.h @@ -1,42 +1,6 @@ -/**************************************************************************** -** -** Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB). -** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt3D module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 Klaralvdalens Datakonsult AB (KDAB). +// Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QT3DRENDER_RENDER_RHI_SUBMISSIONCONTEXT_H #define QT3DRENDER_RENDER_RHI_SUBMISSIONCONTEXT_H @@ -63,8 +27,8 @@ #include <shadervariables_p.h> #include <rhihandle_types_p.h> #include <QSurface> -#include <QtGui/private/qrhi_p.h> #include <QOffscreenSurface> +#include <rhi/qrhi.h> QT_BEGIN_NAMESPACE @@ -207,23 +171,15 @@ private: // States void applyState(const StateVariant &state, QRhiGraphicsPipeline *graphicsPipeline); + bool m_initialized; bool m_ownsRhiCtx; bool m_drivenExternally; - const unsigned int m_id; - QSurface *m_surface; - QSize m_surfaceSize; QHash<Qt3DCore::QNodeId, HRHIBuffer> m_renderBufferHash; - QHash<Qt3DCore::QNodeId, GLuint> m_renderTargets; - QHash<GLuint, QSize> m_renderTargetsSize; - QAbstractTexture::TextureFormat m_renderTargetFormat; Material *m_material; Renderer *m_renderer; - QByteArray m_uboTempArray; - - bool m_initialized; GraphicsApiFilterData m_contextInfo; |