1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
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 <qbackendnodetester.h>
31#include <Qt3DRender/private/attribute_p.h>
32#include <Qt3DRender/qbuffer.h>
33#include "testrenderer.h"
34
35class tst_Attribute : public Qt3DCore::QBackendNodeTester
36{
37 Q_OBJECT
38
39private Q_SLOTS:
40
41 void checkPeerPropertyMirroring()
42 {
43 // GIVEN
44 TestRenderer renderer;
45 Qt3DRender::Render::Attribute renderAttribute;
46 renderAttribute.setRenderer(&renderer);
47
48 Qt3DRender::QAttribute attribute;
49 attribute.setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
50 attribute.setByteOffset(1200);
51 attribute.setByteStride(883);
52 attribute.setCount(427);
53 attribute.setDivisor(305);
54 attribute.setName(QStringLiteral("C3"));
55 attribute.setVertexBaseType(Qt3DRender::QAttribute::UnsignedShort);
56 attribute.setVertexSize(3);
57
58 Qt3DRender::QBuffer buffer;
59 buffer.setUsage(Qt3DRender::QBuffer::DynamicCopy);
60 buffer.setData(QByteArrayLiteral("Corvette"));
61 attribute.setBuffer(&buffer);
62
63 // WHEN
64 simulateInitializationSync(frontend: &attribute, backend: &renderAttribute);
65
66 // THEN
67 QCOMPARE(renderAttribute.peerId(), attribute.id());
68 QCOMPARE(renderAttribute.isDirty(), true);
69 QCOMPARE(renderAttribute.vertexBaseType(), attribute.vertexBaseType());
70 QCOMPARE(renderAttribute.vertexSize(), attribute.vertexSize());
71 QCOMPARE(renderAttribute.attributeType(), attribute.attributeType());
72 QCOMPARE(renderAttribute.byteOffset(), attribute.byteOffset());
73 QCOMPARE(renderAttribute.byteStride(), attribute.byteStride());
74 QCOMPARE(renderAttribute.count(), attribute.count());
75 QCOMPARE(renderAttribute.divisor(), attribute.divisor());
76 QCOMPARE(renderAttribute.name(), attribute.name());
77 QCOMPARE(renderAttribute.bufferId(), buffer.id());
78 }
79
80 void checkInitialAndCleanedUpState()
81 {
82 // GIVEN
83 TestRenderer renderer;
84 Qt3DRender::Render::Attribute renderAttribute;
85 renderAttribute.setRenderer(&renderer);
86
87 // THEN
88 QVERIFY(renderAttribute.peerId().isNull());
89 QVERIFY(renderAttribute.bufferId().isNull());
90 QVERIFY(renderAttribute.name().isEmpty());
91 QCOMPARE(renderAttribute.isDirty(), false);
92 QCOMPARE(renderAttribute.vertexBaseType(), Qt3DRender::QAttribute::Float);
93 QCOMPARE(renderAttribute.vertexSize(), 1U);
94 QCOMPARE(renderAttribute.attributeType(), Qt3DRender::QAttribute::VertexAttribute);
95 QCOMPARE(renderAttribute.byteOffset(), 0U);
96 QCOMPARE(renderAttribute.byteStride(), 0U);
97 QCOMPARE(renderAttribute.count(), 0U);
98 QCOMPARE(renderAttribute.divisor(), 0U);
99
100 // GIVEN
101 Qt3DRender::QAttribute attribute;
102 attribute.setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
103 attribute.setByteOffset(1200);
104 attribute.setByteStride(883);
105 attribute.setCount(427);
106 attribute.setDivisor(305);
107 attribute.setName(QStringLiteral("C3"));
108 attribute.setVertexBaseType(Qt3DRender::QAttribute::Double);
109 attribute.setVertexSize(4);
110 Qt3DRender::QBuffer buffer;
111 buffer.setUsage(Qt3DRender::QBuffer::DynamicCopy);
112 buffer.setData(QByteArrayLiteral("C7"));
113 attribute.setBuffer(&buffer);
114
115 // WHEN
116 simulateInitializationSync(frontend: &attribute, backend: &renderAttribute);
117 renderAttribute.cleanup();
118
119 // THEN
120 QVERIFY(renderAttribute.bufferId().isNull());
121 QVERIFY(renderAttribute.name().isEmpty());
122 QCOMPARE(renderAttribute.isDirty(), false);
123 QCOMPARE(renderAttribute.vertexBaseType(), Qt3DRender::QAttribute::Float);
124 QCOMPARE(renderAttribute.vertexSize(), 1U);
125 QCOMPARE(renderAttribute.attributeType(), Qt3DRender::QAttribute::VertexAttribute);
126 QCOMPARE(renderAttribute.byteOffset(), 0U);
127 QCOMPARE(renderAttribute.byteStride(), 0U);
128 QCOMPARE(renderAttribute.count(), 0U);
129 QCOMPARE(renderAttribute.divisor(), 0U);
130 }
131
132 void checkPropertyChanges()
133 {
134 // GIVEN
135 Qt3DRender::QAttribute attribute;
136 TestRenderer renderer;
137 Qt3DRender::Render::Attribute renderAttribute;
138 renderAttribute.setRenderer(&renderer);
139 simulateInitializationSync(frontend: &attribute, backend: &renderAttribute);
140 renderAttribute.cleanup();
141 renderer.resetDirty();
142
143 QVERIFY(!renderAttribute.isDirty());
144 QVERIFY(!renderer.dirtyBits());
145
146 // WHEN
147 attribute.setVertexBaseType(Qt3DRender::QAttribute::Int);
148 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
149
150 // THEN
151 QCOMPARE(renderAttribute.vertexBaseType(), Qt3DRender::QAttribute::Int);
152 QVERIFY(renderAttribute.isDirty());
153 QVERIFY(renderer.dirtyBits() != 0);
154
155 renderAttribute.unsetDirty();
156 renderer.resetDirty();
157 QVERIFY(!renderAttribute.isDirty());
158 QVERIFY(!renderer.dirtyBits());
159
160 // WHEN
161 attribute.setVertexSize(3);
162 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
163
164 // THEN
165 QCOMPARE(renderAttribute.vertexSize(), 3U);
166 QVERIFY(renderAttribute.isDirty());
167 QVERIFY(renderer.dirtyBits() != 0);
168
169 renderAttribute.unsetDirty();
170 renderer.resetDirty();
171 QVERIFY(!renderAttribute.isDirty());
172
173 // WHEN
174 attribute.setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
175 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
176
177 // THEN
178 QCOMPARE(renderAttribute.attributeType(), Qt3DRender::QAttribute::IndexAttribute);
179 QVERIFY(renderAttribute.isDirty());
180 QVERIFY(renderer.dirtyBits() != 0);
181
182 renderAttribute.unsetDirty();
183 renderer.resetDirty();
184 QVERIFY(!renderAttribute.isDirty());
185
186 // WHEN
187 attribute.setAttributeType(Qt3DRender::QAttribute::DrawIndirectAttribute);
188 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
189
190 // THEN
191 QCOMPARE(renderAttribute.attributeType(), Qt3DRender::QAttribute::DrawIndirectAttribute);
192 QVERIFY(renderAttribute.isDirty());
193 QVERIFY(renderer.dirtyBits() != 0);
194
195 renderAttribute.unsetDirty();
196 renderer.resetDirty();
197 QVERIFY(!renderAttribute.isDirty());
198
199 // WHEN
200 attribute.setCount(1340);
201 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
202
203 // THEN
204 QCOMPARE(renderAttribute.count(), 1340U);
205 QVERIFY(renderAttribute.isDirty());
206 QVERIFY(renderer.dirtyBits() != 0);
207
208 renderAttribute.unsetDirty();
209 renderer.resetDirty();
210 QVERIFY(!renderAttribute.isDirty());
211
212 // WHEN
213 attribute.setName(QStringLiteral("L88"));
214 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
215
216 // THEN
217 QCOMPARE(renderAttribute.name(), QStringLiteral("L88"));
218 QVERIFY(renderAttribute.isDirty());
219 QVERIFY(renderer.dirtyBits() != 0);
220
221 renderAttribute.unsetDirty();
222 renderer.resetDirty();
223 QVERIFY(!renderAttribute.isDirty());
224
225 // WHEN
226 attribute.setByteOffset(555U);
227 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
228
229 // THEN
230 QCOMPARE(renderAttribute.byteOffset(), 555U);
231 QVERIFY(renderAttribute.isDirty());
232 QVERIFY(renderer.dirtyBits() != 0);
233
234 renderAttribute.unsetDirty();
235 renderer.resetDirty();
236 QVERIFY(!renderAttribute.isDirty());
237
238 // WHEN
239 attribute.setByteStride(454);
240 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
241
242 // THEN
243 QCOMPARE(renderAttribute.byteStride(), 454U);
244 QVERIFY(renderAttribute.isDirty());
245 QVERIFY(renderer.dirtyBits() != 0);
246
247 renderAttribute.unsetDirty();
248 renderer.resetDirty();
249 QVERIFY(!renderAttribute.isDirty());
250
251 // WHEN
252 attribute.setDivisor(1450);
253 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
254
255 // THEN
256 QCOMPARE(renderAttribute.divisor(), 1450U);
257 QVERIFY(renderAttribute.isDirty());
258 QVERIFY(renderer.dirtyBits() != 0);
259
260 renderAttribute.unsetDirty();
261 renderer.resetDirty();
262 QVERIFY(!renderAttribute.isDirty());
263
264 // WHEN
265 auto buffer = new Qt3DRender::QBuffer();
266 attribute.setBuffer(buffer);
267 renderAttribute.syncFromFrontEnd(frontEnd: &attribute, firstTime: false);
268
269 // THEN
270 QCOMPARE(renderAttribute.bufferId(), buffer->id());
271 QVERIFY(renderAttribute.isDirty());
272 QVERIFY(renderer.dirtyBits() != 0);
273
274 renderAttribute.unsetDirty();
275 renderer.resetDirty();
276 QVERIFY(!renderAttribute.isDirty());
277 }
278};
279
280
281QTEST_APPLESS_MAIN(tst_Attribute)
282
283#include "tst_attribute.moc"
284

source code of qt3d/tests/auto/render/attribute/tst_attribute.cpp