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
25QT_BEGIN_NAMESPACE
26
27namespace Qt3DRender {
28
29namespace Render {
30
31class GraphicsContext;
32class GLBuffer;
33class NodeManagers;
34
35class Q_3DRENDERSHARED_PRIVATE_EXPORT ShaderData : public BackendNode
36{
37public:
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 bool hasPropertyValuesForBlock(int blockNameId) const;
78 const PropertyValuesForBlock &propertyValuesForBlock(int blockNameId) const;
79 void generatePropertyValuesForBlock(const QString &blockName);
80
81protected:
82 PropertyReaderInterfacePtr m_propertyReader;
83
84 // 1 to 1 match with frontend properties
85 QHash<QString, PropertyValue> m_originalProperties;
86
87 // BlockNameId to array of pair of BlockName+PropertyName PropertyValue
88 std::unordered_map<int, PropertyValuesForBlock> m_blockNameToPropertyValues;
89
90 Matrix4x4 m_worldMatrix;
91 NodeManagers *m_managers;
92
93 static ShaderData *lookupResource(NodeManagers *managers, Qt3DCore::QNodeId id);
94 ShaderData *lookupResource(Qt3DCore::QNodeId id);
95
96 friend class RenderShaderDataFunctor;
97
98private:
99 mutable std::shared_mutex m_lock;
100};
101
102class RenderShaderDataFunctor : public Qt3DCore::QBackendNodeMapper
103{
104public:
105 explicit RenderShaderDataFunctor(AbstractRenderer *renderer, NodeManagers *managers);
106
107 Qt3DCore::QBackendNode *create(Qt3DCore::QNodeId id) const final;
108 Qt3DCore::QBackendNode *get(Qt3DCore::QNodeId id) const final;
109 void destroy(Qt3DCore::QNodeId id) const final;
110
111private:
112 NodeManagers *m_managers;
113 AbstractRenderer *m_renderer;
114};
115
116} // namespace Render
117
118} // namespace Qt3DRender
119
120QT_END_NAMESPACE
121
122Q_DECLARE_METATYPE(Qt3DRender::Render::ShaderData*) // LCOV_EXCL_LINE
123
124#endif // QT3DRENDER_RENDER_SHADERDATA_P_H
125

source code of qt3d/src/render/materialsystem/shaderdata_p.h