| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QT3DRENDER_RENDER_SHADERDATA_P_H |
| 5 | #define QT3DRENDER_RENDER_SHADERDATA_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists for the convenience |
| 12 | // of other Qt classes. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <Qt3DRender/private/backendnode_p.h> |
| 19 | #include <Qt3DRender/qshaderdata.h> |
| 20 | #include <Qt3DCore/private/matrix4x4_p.h> |
| 21 | #include <mutex> |
| 22 | #include <shared_mutex> |
| 23 | #include <unordered_map> |
| 24 | |
| 25 | QT_BEGIN_NAMESPACE |
| 26 | |
| 27 | namespace Qt3DRender { |
| 28 | |
| 29 | namespace Render { |
| 30 | |
| 31 | class GraphicsContext; |
| 32 | class GLBuffer; |
| 33 | class NodeManagers; |
| 34 | |
| 35 | class Q_3DRENDERSHARED_PRIVATE_EXPORT ShaderData : public BackendNode |
| 36 | { |
| 37 | public: |
| 38 | enum TransformType { |
| 39 | NoTransform = -1, |
| 40 | ModelToEye = 0, |
| 41 | ModelToWorld, |
| 42 | ModelToWorldDirection |
| 43 | }; |
| 44 | struct PropertyValue { |
| 45 | QVariant value; |
| 46 | bool isNode; |
| 47 | bool isArray; |
| 48 | bool isTransformed; |
| 49 | QString transformedPropertyName; |
| 50 | }; |
| 51 | |
| 52 | ShaderData(); |
| 53 | ~ShaderData(); |
| 54 | |
| 55 | const QHash<QString, PropertyValue> &properties() const { return m_originalProperties; } |
| 56 | |
| 57 | // Called by FramePreparationJob |
| 58 | void updateWorldTransform(const Matrix4x4 &worldMatrix); |
| 59 | |
| 60 | QVariant getTransformedProperty(const PropertyValue *v, const Matrix4x4 &viewMatrix) const noexcept; |
| 61 | |
| 62 | // Unit tests purposes only |
| 63 | TransformType propertyTransformType(const QString &name) const; |
| 64 | |
| 65 | void setManagers(NodeManagers *managers); |
| 66 | |
| 67 | void syncFromFrontEnd(const Qt3DCore::QNode *frontEnd, bool firstTime) override; |
| 68 | |
| 69 | #ifdef Q_OS_WIN |
| 70 | // To get MSVC to compile even though we don't need any cleanup |
| 71 | void cleanup() {} |
| 72 | #endif |
| 73 | |
| 74 | // Block.Property nameId, property nameId, PropertyValue * |
| 75 | using PropertyValuesForBlock = std::vector<std::tuple<int, int, const PropertyValue *>>; |
| 76 | |
| 77 | const PropertyValuesForBlock &propertyValuesForBlock(int blockNameId) const; |
| 78 | void generatePropertyValuesForBlockIfNeeded(const QString &blockName); |
| 79 | |
| 80 | protected: |
| 81 | PropertyReaderInterfacePtr m_propertyReader; |
| 82 | |
| 83 | // 1 to 1 match with frontend properties |
| 84 | QHash<QString, PropertyValue> m_originalProperties; |
| 85 | |
| 86 | // BlockNameId to array of pair of BlockName+PropertyName PropertyValue |
| 87 | std::unordered_map<int, PropertyValuesForBlock> m_blockNameToPropertyValues; |
| 88 | |
| 89 | Matrix4x4 m_worldMatrix; |
| 90 | NodeManagers *m_managers; |
| 91 | |
| 92 | static ShaderData *lookupResource(NodeManagers *managers, Qt3DCore::QNodeId id); |
| 93 | ShaderData *lookupResource(Qt3DCore::QNodeId id); |
| 94 | |
| 95 | friend class RenderShaderDataFunctor; |
| 96 | |
| 97 | private: |
| 98 | mutable std::shared_mutex m_lock; |
| 99 | }; |
| 100 | |
| 101 | class RenderShaderDataFunctor : public Qt3DCore::QBackendNodeMapper |
| 102 | { |
| 103 | public: |
| 104 | explicit RenderShaderDataFunctor(AbstractRenderer *renderer, NodeManagers *managers); |
| 105 | |
| 106 | Qt3DCore::QBackendNode *create(Qt3DCore::QNodeId id) const final; |
| 107 | Qt3DCore::QBackendNode *get(Qt3DCore::QNodeId id) const final; |
| 108 | void destroy(Qt3DCore::QNodeId id) const final; |
| 109 | |
| 110 | private: |
| 111 | NodeManagers *m_managers; |
| 112 | AbstractRenderer *m_renderer; |
| 113 | }; |
| 114 | |
| 115 | } // namespace Render |
| 116 | |
| 117 | } // namespace Qt3DRender |
| 118 | |
| 119 | QT_END_NAMESPACE |
| 120 | |
| 121 | Q_DECLARE_METATYPE(Qt3DRender::Render::ShaderData*) // LCOV_EXCL_LINE |
| 122 | |
| 123 | #endif // QT3DRENDER_RENDER_SHADERDATA_P_H |
| 124 | |