1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 Paul Lemire <paul.lemire350@gmail.com> |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt3D module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #include <QtTest/QTest> |
30 | #include <Qt3DRender/private/updateshaderdatatransformjob_p.h> |
31 | #include <Qt3DRender/private/updateworldtransformjob_p.h> |
32 | #include <Qt3DRender/private/nodemanagers_p.h> |
33 | #include <Qt3DRender/private/managers_p.h> |
34 | #include <Qt3DRender/qrenderaspect.h> |
35 | #include <Qt3DCore/qentity.h> |
36 | #include <Qt3DRender/qshaderdata.h> |
37 | #include <Qt3DRender/qcamera.h> |
38 | #include <Qt3DRender/private/shaderdata_p.h> |
39 | #include <Qt3DRender/private/qrenderaspect_p.h> |
40 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> |
41 | #include <Qt3DCore/private/qnodevisitor_p.h> |
42 | #include "qmlscenereader.h" |
43 | |
44 | QT_BEGIN_NAMESPACE |
45 | |
46 | namespace Qt3DRender { |
47 | |
48 | QVector<Qt3DCore::QNode *> getNodesForCreation(Qt3DCore::QNode *root) |
49 | { |
50 | using namespace Qt3DCore; |
51 | |
52 | QVector<QNode *> nodes; |
53 | Qt3DCore::QNodeVisitor visitor; |
54 | visitor.traverse(rootNode_: root, fN: [&nodes](QNode *node) { |
55 | nodes.append(t: node); |
56 | |
57 | // Store the metaobject of the node in the QNode so that we have it available |
58 | // to us during destruction in the QNode destructor. This allows us to send |
59 | // the QNodeId and the metaobject as typeinfo to the backend aspects so they |
60 | // in turn can find the correct QBackendNodeMapper object to handle the destruction |
61 | // of the corresponding backend nodes. |
62 | QNodePrivate *d = QNodePrivate::get(q: node); |
63 | d->m_typeInfo = const_cast<QMetaObject*>(QNodePrivate::findStaticMetaObject(metaObject: node->metaObject())); |
64 | |
65 | // Mark this node as having been handled for creation so that it is picked up |
66 | d->m_hasBackendNode = true; |
67 | }); |
68 | |
69 | return nodes; |
70 | } |
71 | |
72 | QVector<Qt3DCore::NodeTreeChange> nodeTreeChangesForNodes(const QVector<Qt3DCore::QNode *> nodes) |
73 | { |
74 | QVector<Qt3DCore::NodeTreeChange> nodeTreeChanges; |
75 | nodeTreeChanges.reserve(size: nodes.size()); |
76 | |
77 | for (Qt3DCore::QNode *n : nodes) { |
78 | nodeTreeChanges.push_back(t: { |
79 | .id: n->id(), |
80 | .metaObj: Qt3DCore::QNodePrivate::get(q: n)->m_typeInfo, |
81 | .type: Qt3DCore::NodeTreeChange::Added, |
82 | .node: n |
83 | }); |
84 | } |
85 | |
86 | return nodeTreeChanges; |
87 | } |
88 | |
89 | class TestAspect : public Qt3DRender::QRenderAspect |
90 | { |
91 | public: |
92 | TestAspect(Qt3DCore::QNode *root) |
93 | : Qt3DRender::QRenderAspect(Qt3DRender::QRenderAspect::Synchronous) |
94 | , m_sceneRoot(nullptr) |
95 | { |
96 | Qt3DRender::QRenderAspect::onRegistered(); |
97 | |
98 | const QVector<Qt3DCore::QNode *> nodes = getNodesForCreation(root); |
99 | d_func()->setRootAndCreateNodes(rootObject: qobject_cast<Qt3DCore::QEntity *>(object: root), nodesTreeChanges: nodeTreeChangesForNodes(nodes)); |
100 | |
101 | Qt3DRender::Render::Entity *rootEntity = nodeManagers()->lookupResource<Qt3DRender::Render::Entity, Render::EntityManager>(id: rootEntityId()); |
102 | Q_ASSERT(rootEntity); |
103 | m_sceneRoot = rootEntity; |
104 | } |
105 | |
106 | ~TestAspect() |
107 | { |
108 | QRenderAspect::onUnregistered(); |
109 | } |
110 | |
111 | void onRegistered() { Qt3DRender::QRenderAspect::onRegistered(); } |
112 | void onUnregistered() { Qt3DRender::QRenderAspect::onUnregistered(); } |
113 | |
114 | Qt3DRender::Render::NodeManagers *nodeManagers() const { return d_func()->m_renderer->nodeManagers(); } |
115 | Qt3DRender::Render::FrameGraphNode *frameGraphRoot() const { return d_func()->m_renderer->frameGraphRoot(); } |
116 | Qt3DRender::Render::RenderSettings *renderSettings() const { return d_func()->m_renderer->settings(); } |
117 | Qt3DRender::Render::Entity *sceneRoot() const { return m_sceneRoot; } |
118 | |
119 | private: |
120 | Qt3DRender::Render::Entity *m_sceneRoot; |
121 | }; |
122 | |
123 | } // Qt3DRender |
124 | |
125 | QT_END_NAMESPACE |
126 | |
127 | namespace { |
128 | |
129 | void runRequiredJobs(Qt3DRender::TestAspect *test) |
130 | { |
131 | Qt3DRender::Render::UpdateWorldTransformJob updateWorldTransform; |
132 | updateWorldTransform.setRoot(test->sceneRoot()); |
133 | updateWorldTransform.setManagers(test->nodeManagers()); |
134 | updateWorldTransform.run(); |
135 | } |
136 | |
137 | struct NodeCollection |
138 | { |
139 | explicit NodeCollection(Qt3DRender::TestAspect *aspect, QObject *frontendRoot) |
140 | : shaderData(frontendRoot->findChildren<Qt3DRender::QShaderData *>()) |
141 | { |
142 | // THEN |
143 | QCOMPARE(aspect->nodeManagers()->shaderDataManager()->activeHandles().size(), shaderData.size()); |
144 | |
145 | for (const Qt3DRender::QShaderData *s : qAsConst(t&: shaderData)) { |
146 | Qt3DRender::Render::ShaderData *backend = aspect->nodeManagers()->shaderDataManager()->lookupResource(id: s->id()); |
147 | QVERIFY(backend != nullptr); |
148 | backendShaderData.push_back(t: backend); |
149 | } |
150 | } |
151 | |
152 | QList<Qt3DRender::QShaderData *> shaderData; |
153 | QVector<Qt3DRender::Render::ShaderData *> backendShaderData; |
154 | }; |
155 | |
156 | } // anonymous |
157 | |
158 | class tst_UpdateShaderDataTransformJob : public QObject |
159 | { |
160 | Q_OBJECT |
161 | |
162 | private Q_SLOTS: |
163 | |
164 | void checkInitialState() |
165 | { |
166 | // GIVEN |
167 | Qt3DRender::Render::UpdateShaderDataTransformJob backendUpdateShaderDataTransformJob; |
168 | |
169 | // THEN |
170 | QVERIFY(backendUpdateShaderDataTransformJob.managers() == nullptr); |
171 | } |
172 | |
173 | void checkInitializeState() |
174 | { |
175 | // GIVEN |
176 | Qt3DRender::Render::UpdateShaderDataTransformJob backendUpdateShaderDataTransformJob; |
177 | Qt3DRender::Render::NodeManagers managers; |
178 | |
179 | // WHEN |
180 | backendUpdateShaderDataTransformJob.setManagers(&managers); |
181 | |
182 | // THEN |
183 | QVERIFY(backendUpdateShaderDataTransformJob.managers() == &managers); |
184 | } |
185 | |
186 | void checkRunModelToEye() |
187 | { |
188 | // GIVEN |
189 | QmlSceneReader sceneReader(QUrl("qrc:/test_scene_model_to_eye.qml" )); |
190 | QScopedPointer<Qt3DCore::QNode> root(qobject_cast<Qt3DCore::QNode *>(object: sceneReader.root())); |
191 | QVERIFY(root); |
192 | QScopedPointer<Qt3DRender::TestAspect> test(new Qt3DRender::TestAspect(root.data())); |
193 | |
194 | // Properly compute the world transforms |
195 | runRequiredJobs(test: test.data()); |
196 | |
197 | // WHEN |
198 | Qt3DRender::QCamera *camera = root->findChild<Qt3DRender::QCamera *>(); |
199 | const NodeCollection collection(test.data(), root.data()); |
200 | |
201 | // THEN |
202 | QCOMPARE(collection.shaderData.size(), 1); |
203 | QVERIFY(camera != nullptr); |
204 | |
205 | // WHEN |
206 | Qt3DRender::Render::ShaderData *backendShaderData = collection.backendShaderData.first(); |
207 | |
208 | // THEN |
209 | QCOMPARE(backendShaderData->properties().size(), 3); |
210 | QVERIFY(backendShaderData->properties().contains(QStringLiteral("eyePosition" ))); |
211 | QVERIFY(backendShaderData->properties().contains(QStringLiteral("eyePositionTransformed" ))); |
212 | |
213 | QCOMPARE(backendShaderData->properties()[QStringLiteral("eyePosition" )].value.value<QVector3D>(), QVector3D(1.0f, 1.0f, 1.0f)); |
214 | QCOMPARE(backendShaderData->properties()[QStringLiteral("eyePositionTransformed" )].value.toInt(), int(Qt3DRender::Render::ShaderData::ModelToEye)); |
215 | |
216 | // WHEN |
217 | Qt3DRender::Render::UpdateShaderDataTransformJob backendUpdateShaderDataTransformJob; |
218 | backendUpdateShaderDataTransformJob.setManagers(test->nodeManagers()); |
219 | backendUpdateShaderDataTransformJob.run(); |
220 | |
221 | // THEN |
222 | // See scene file to find translation |
223 | QCOMPARE(backendShaderData->getTransformedProperty(QStringLiteral("eyePosition" ), Matrix4x4(camera->viewMatrix())).value<Vector3D>(), |
224 | Matrix4x4(camera->viewMatrix()) * (Vector3D(1.0f, 1.0f, 1.0f) + Vector3D(0.0f, 5.0f, 0.0f))); |
225 | } |
226 | |
227 | void checkRunModelToWorld() |
228 | { |
229 | // GIVEN |
230 | QmlSceneReader sceneReader(QUrl("qrc:/test_scene_model_to_world.qml" )); |
231 | QScopedPointer<Qt3DCore::QNode> root(qobject_cast<Qt3DCore::QNode *>(object: sceneReader.root())); |
232 | QVERIFY(root); |
233 | QScopedPointer<Qt3DRender::TestAspect> test(new Qt3DRender::TestAspect(root.data())); |
234 | |
235 | // Properly compute the world transforms |
236 | runRequiredJobs(test: test.data()); |
237 | |
238 | // WHEN |
239 | Qt3DRender::QCamera *camera = root->findChild<Qt3DRender::QCamera *>(); |
240 | const NodeCollection collection(test.data(), root.data()); |
241 | |
242 | // THEN |
243 | QCOMPARE(collection.shaderData.size(), 1); |
244 | QVERIFY(camera != nullptr); |
245 | |
246 | // WHEN |
247 | Qt3DRender::Render::ShaderData *backendShaderData = collection.backendShaderData.first(); |
248 | |
249 | // THEN |
250 | QCOMPARE(backendShaderData->properties().size(), 3); |
251 | QVERIFY(backendShaderData->properties().contains(QStringLiteral("position" ))); |
252 | QVERIFY(backendShaderData->properties().contains(QStringLiteral("positionTransformed" ))); |
253 | |
254 | QCOMPARE(backendShaderData->properties()[QStringLiteral("position" )].value.value<QVector3D>(), QVector3D(1.0f, 1.0f, 1.0f)); |
255 | QCOMPARE(backendShaderData->properties()[QStringLiteral("positionTransformed" )].value.toInt(), int(Qt3DRender::Render::ShaderData::ModelToWorld)); |
256 | |
257 | // WHEN |
258 | Qt3DRender::Render::UpdateShaderDataTransformJob backendUpdateShaderDataTransformJob; |
259 | backendUpdateShaderDataTransformJob.setManagers(test->nodeManagers()); |
260 | backendUpdateShaderDataTransformJob.run(); |
261 | |
262 | // THEN |
263 | // See scene file to find translation |
264 | QCOMPARE(backendShaderData->getTransformedProperty(QStringLiteral("position" ), Matrix4x4(camera->viewMatrix())).value<Vector3D>(), |
265 | Vector3D(1.0f, 1.0f, 1.0f) + Vector3D(5.0f, 5.0f, 5.0f)); |
266 | } |
267 | }; |
268 | |
269 | QTEST_MAIN(tst_UpdateShaderDataTransformJob) |
270 | |
271 | #include "tst_updateshaderdatatransformjob.moc" |
272 | |