From 653fdeb073a525a676bf0c12511c3f74bad2095e Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 23 Nov 2015 15:27:48 +0100 Subject: Lights phase 1: infrastructure QAbstractLight becomes QLight and gets its own backend node. This way we can easily gather all lights for the scene and filter them when building render commands. Both the frontend and backend remain a subclass of (Q)ShaderData but will not be part of the ordinary ShaderData component list. This prevents mixing up ShaderDatas and Lights but allows reusing the same underlying infrastructure so that properties can automatically be transformed for example. It is worth noting that the position property for lights is now removed: the position is determined by the entity's (to which the light component belongs) position. A number of changes are made to ShaderData itself as backend subclassing with different managers is not straightforward. For now the distance between the rendered entity and the entity with the light component is calculated and lights will be chosen based on this distance. A framegraph node for controlling this will be added in future patches. No uniform setting or shader changes are included here. Task-number: QTBUG-48834 Change-Id: I43a6c5f9420d4254d798c558bd58680b2b09eceb Reviewed-by: Sean Harmer --- src/render/materialsystem/shaderdata.cpp | 81 +++++++++++++++++++++----------- src/render/materialsystem/shaderdata_p.h | 36 +++++++------- 2 files changed, 72 insertions(+), 45 deletions(-) (limited to 'src/render/materialsystem') diff --git a/src/render/materialsystem/shaderdata.cpp b/src/render/materialsystem/shaderdata.cpp index c030225c4..9e07c5810 100644 --- a/src/render/materialsystem/shaderdata.cpp +++ b/src/render/materialsystem/shaderdata.cpp @@ -44,6 +44,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE @@ -55,9 +56,8 @@ namespace Render { QList ShaderData::m_updatedShaderData; ShaderData::ShaderData() - : QBackendNode() - , m_manager(Q_NULLPTR) - , m_mutex(new QMutex()) + : m_mutex(new QMutex) + , m_managers(Q_NULLPTR) { } @@ -66,6 +66,25 @@ ShaderData::~ShaderData() delete m_mutex; } +void ShaderData::setManagers(NodeManagers *managers) +{ + m_managers = managers; +} + +ShaderData *ShaderData::lookupResource(NodeManagers *managers, const QNodeId &id) +{ + ShaderData *shaderData = managers->shaderDataManager()->lookupResource(id); + if (!shaderData) + shaderData = managers->lightManager()->lookupResource(id); + + return shaderData; +} + +ShaderData *ShaderData::lookupResource(const QNodeId &id) +{ + return ShaderData::lookupResource(m_managers, id); +} + void ShaderData::updateFromPeer(Qt3DCore::QNode *peer) { m_properties.clear(); @@ -76,7 +95,7 @@ void ShaderData::updateFromPeer(Qt3DCore::QNode *peer) } // Call by cleanup job (single thread) -void ShaderData::clearUpdate() +void ShaderData::clearUpdatedProperties() { m_updatedProperties.clear(); const QHash::const_iterator end = m_nestedShaderDataProperties.end(); @@ -85,19 +104,29 @@ void ShaderData::clearUpdate() while (it != end) { if (it.value().userType() == QMetaType::QVariantList) { Q_FOREACH (const QVariant &v, it.value().value()) { - ShaderData *nested = m_manager->lookupResource(v.value()); + ShaderData *nested = lookupResource(v.value()); if (nested != Q_NULLPTR) - nested->clearUpdate(); + nested->clearUpdatedProperties(); } } else { - ShaderData *nested = m_manager->lookupResource(it.value().value()); + ShaderData *nested = lookupResource(it.value().value()); if (nested != Q_NULLPTR) - nested->clearUpdate(); + nested->clearUpdatedProperties(); } ++it; } } +void ShaderData::cleanup(NodeManagers *managers) +{ + Q_FOREACH (const Qt3DCore::QNodeId &id, m_updatedShaderData) { + ShaderData *shaderData = ShaderData::lookupResource(managers, id); + if (shaderData) + shaderData->clearUpdatedProperties(); + } + m_updatedShaderData.clear(); +} + // Called by renderview jobs (several concurrent threads) /*! \internal @@ -108,7 +137,7 @@ void ShaderData::clearUpdate() \note This needs to be performed for every top level ShaderData every time it is used. As we don't know if the transformed properties use the same viewMatrix for all RenderViews. */ -bool ShaderData::needsUpdate(const QMatrix4x4 &viewMatrix) +bool ShaderData::updateViewTransform(const QMatrix4x4 &viewMatrix) { // We can't perform this only once as we don't know if we would be call as the root or a // nested ShaderData @@ -137,13 +166,13 @@ bool ShaderData::needsUpdate(const QMatrix4x4 &viewMatrix) QVariantList updatedNodes; bool nestedNeedsUpdate = false; Q_FOREACH (const QVariant &v, it.value().value()) { - ShaderData *nested = m_manager->lookupResource(v.value()); + ShaderData *nested = lookupResource(v.value()); if (nested != Q_NULLPTR) { // We need to add the nested nodes to the updated property list // as we need to maintain order // if node[0] doesn't need update but node[1] does, // if we only have a single element, the renderer would update element [0] - nestedNeedsUpdate |= nested->needsUpdate(viewMatrix); + nestedNeedsUpdate |= nested->updateViewTransform(viewMatrix); updatedNodes << v; } } @@ -151,8 +180,8 @@ bool ShaderData::needsUpdate(const QMatrix4x4 &viewMatrix) if (nestedNeedsUpdate && !updatedNodes.empty()) m_updatedProperties.insert(it.key(), updatedNodes); } else { - ShaderData *nested = m_manager->lookupResource(it.value().value()); - if (nested != Q_NULLPTR && nested->needsUpdate(viewMatrix)) + ShaderData *nested = lookupResource(it.value().value()); + if (nested != Q_NULLPTR && nested->updateViewTransform(viewMatrix)) m_updatedProperties.insert(it.key(), it.value()); } ++it; @@ -160,10 +189,10 @@ bool ShaderData::needsUpdate(const QMatrix4x4 &viewMatrix) return m_updatedProperties.size() > 0; } -void ShaderData::updateTransformedProperties(const QMatrix4x4 &nodeWorldMatrix) +bool ShaderData::updateWorldTransform(const QMatrix4x4 &worldMatrix) { - if (m_worldMatrix != nodeWorldMatrix) { - m_worldMatrix = nodeWorldMatrix; + if (m_worldMatrix != worldMatrix) { + m_worldMatrix = worldMatrix; const QHash::const_iterator transformedEnd = m_transformedProperties.end(); QHash::const_iterator transformedIt = m_transformedProperties.begin(); @@ -179,12 +208,13 @@ void ShaderData::updateTransformedProperties(const QMatrix4x4 &nodeWorldMatrix) ++transformedIt; } } + return m_updatedProperties.size() > 0; } // This will add the ShaderData to be cleared from updates at the end of the frame // by the cleanup job // Called by renderview jobs (several concurrent threads) -void ShaderData::addToClearUpdateList() +void ShaderData::markDirty() { QMutexLocker lock(m_mutex); if (!ShaderData::m_updatedShaderData.contains(peerUuid())) @@ -236,11 +266,6 @@ void ShaderData::readPeerProperties(QShaderData *shaderData) } } -void ShaderData::setManager(ShaderDataManager *manager) -{ - m_manager = manager; -} - void ShaderData::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) { if (!m_propertyReader.isNull()) { @@ -286,28 +311,28 @@ void ShaderData::sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) } } -RenderShaderDataFunctor::RenderShaderDataFunctor(ShaderDataManager *manager) - : m_manager(manager) +RenderShaderDataFunctor::RenderShaderDataFunctor(NodeManagers *managers) + : m_managers(managers) { } Qt3DCore::QBackendNode *RenderShaderDataFunctor::create(Qt3DCore::QNode *frontend, const Qt3DCore::QBackendNodeFactory *factory) const { - ShaderData *backend = m_manager->getOrCreateResource(frontend->id()); + ShaderData *backend = m_managers->shaderDataManager()->getOrCreateResource(frontend->id()); backend->setFactory(factory); - backend->setManager(m_manager); + backend->setManagers(m_managers); backend->setPeer(frontend); return backend; } Qt3DCore::QBackendNode *RenderShaderDataFunctor::get(const Qt3DCore::QNodeId &id) const { - return m_manager->lookupResource(id); + return m_managers->shaderDataManager()->lookupResource(id); } void RenderShaderDataFunctor::destroy(const Qt3DCore::QNodeId &id) const { - m_manager->releaseResource(id); + m_managers->shaderDataManager()->releaseResource(id); } } // namespace Render diff --git a/src/render/materialsystem/shaderdata_p.h b/src/render/materialsystem/shaderdata_p.h index b45395f07..d87ae8644 100644 --- a/src/render/materialsystem/shaderdata_p.h +++ b/src/render/materialsystem/shaderdata_p.h @@ -62,7 +62,7 @@ namespace Render { class GraphicsContext; class UniformBuffer; -class ShaderDataManager; +class NodeManagers; class Q_AUTOTEST_EXPORT ShaderData : public Qt3DCore::QBackendNode { @@ -70,25 +70,25 @@ public: ShaderData(); ~ShaderData(); - void updateFromPeer(Qt3DCore::QNode *peer) Q_DECL_OVERRIDE; - inline QHash properties() const { return m_properties; } - inline QHash updatedProperties() const { return m_updatedProperties; } + QHash properties() const { return m_properties; } + QHash updatedProperties() const { return m_updatedProperties; } + + // Called by FramePreparationJob + bool updateWorldTransform(const QMatrix4x4 &worldMatrix); - // Called by cleanup job - inline static QList updatedShaderDataList() { return m_updatedShaderData; } - inline static void clearShaderDataList() { return m_updatedShaderData.clear(); } - void clearUpdate(); + // Call by RenderViewJob + void markDirty(); + bool updateViewTransform(const QMatrix4x4 &viewMatrix); - // Call by RenderViewJobs - void addToClearUpdateList(); - bool needsUpdate(const QMatrix4x4 &viewMatrix); + // Called by FrameCleanupJob + static void cleanup(NodeManagers *managers); - void updateTransformedProperties(const QMatrix4x4 &nodeWordlTransform); + void updateFromPeer(Qt3DCore::QNode *peer) Q_DECL_OVERRIDE; + void setManagers(NodeManagers *managers); protected: void sceneChangeEvent(const Qt3DCore::QSceneChangePtr &e) Q_DECL_OVERRIDE; -private: // 1 to 1 match with frontend properties, modified only by sceneChangeEvent QHash m_originalProperties; // 1 to 1 match with frontend properties apart from Transformed @@ -100,14 +100,16 @@ private: PropertyReaderInterfacePtr m_propertyReader; QHash m_nestedShaderDataProperties; QHash m_transformedProperties; - ShaderDataManager *m_manager; QMutex *m_mutex; static QList m_updatedShaderData; QMatrix4x4 m_worldMatrix; QMatrix4x4 m_viewMatrix; + NodeManagers *m_managers; void readPeerProperties(QShaderData *peer); - void setManager(ShaderDataManager *manager); + void clearUpdatedProperties(); + static ShaderData *lookupResource(NodeManagers *managers, const Qt3DCore::QNodeId &id); + ShaderData *lookupResource(const Qt3DCore::QNodeId &id); friend class RenderShaderDataFunctor; }; @@ -115,14 +117,14 @@ private: class RenderShaderDataFunctor : public Qt3DCore::QBackendNodeFunctor { public: - explicit RenderShaderDataFunctor(ShaderDataManager *manager); + explicit RenderShaderDataFunctor(NodeManagers *managers); Qt3DCore::QBackendNode *create(Qt3DCore::QNode *frontend, const Qt3DCore::QBackendNodeFactory *factory) const Q_DECL_FINAL; Qt3DCore::QBackendNode *get(const Qt3DCore::QNodeId &id) const Q_DECL_FINAL; void destroy(const Qt3DCore::QNodeId &id) const Q_DECL_FINAL; private: - ShaderDataManager *m_manager; + NodeManagers *m_managers; }; } // namespace Render -- cgit v1.2.3