| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DRender/private/uniform_p.h> |
| 6 | |
| 7 | using namespace Qt3DRender; |
| 8 | using namespace Qt3DRender::Render; |
| 9 | |
| 10 | class tst_Uniform : public QObject |
| 11 | { |
| 12 | Q_OBJECT |
| 13 | private Q_SLOTS: |
| 14 | |
| 15 | void checkInitialState() |
| 16 | { |
| 17 | // GIVEN |
| 18 | UniformValue v; |
| 19 | // THEN |
| 20 | QVERIFY(v.constData<float>()[0] == 0.0f); |
| 21 | QVERIFY(v.constData<float>()[1] == 0.0f); |
| 22 | QVERIFY(v.constData<float>()[2] == 0.0f); |
| 23 | QVERIFY(v.constData<float>()[3] == 0.0f); |
| 24 | } |
| 25 | |
| 26 | void checkDefaultCTors() |
| 27 | { |
| 28 | { |
| 29 | // GIVEN |
| 30 | UniformValue v(883); |
| 31 | // THEN |
| 32 | QCOMPARE(v.constData<int>()[0], 883); |
| 33 | QCOMPARE(v.constData<int>()[1], 0); |
| 34 | QCOMPARE(v.constData<int>()[2], 0); |
| 35 | QCOMPARE(v.constData<int>()[3], 0); |
| 36 | } |
| 37 | { |
| 38 | // GIVEN |
| 39 | UniformValue v(1584U); |
| 40 | // THEN |
| 41 | QCOMPARE(v.constData<uint>()[0], 1584U); |
| 42 | QCOMPARE(v.constData<uint>()[1], 0U); |
| 43 | QCOMPARE(v.constData<uint>()[2], 0U); |
| 44 | QCOMPARE(v.constData<uint>()[3], 0U); |
| 45 | } |
| 46 | { |
| 47 | // GIVEN |
| 48 | UniformValue v(454.0f); |
| 49 | // THEN |
| 50 | QCOMPARE(v.constData<float>()[0], 454.0f); |
| 51 | QCOMPARE(v.constData<float>()[1], 0.0f); |
| 52 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 53 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 54 | } |
| 55 | { |
| 56 | // GIVEN |
| 57 | UniformValue v(350.0); |
| 58 | // THEN |
| 59 | // Note: Uniform value does a double -> float conversion |
| 60 | QCOMPARE(v.constData<float>()[0], 350.0f); |
| 61 | QCOMPARE(v.constData<float>()[1], 0.0f); |
| 62 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 63 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 64 | } |
| 65 | { |
| 66 | // GIVEN |
| 67 | UniformValue v(true); |
| 68 | // THEN |
| 69 | QCOMPARE(v.constData<bool>()[0], true); |
| 70 | QCOMPARE(v.constData<bool>()[1], false); |
| 71 | QCOMPARE(v.constData<bool>()[2], false); |
| 72 | QCOMPARE(v.constData<bool>()[3], false); |
| 73 | } |
| 74 | { |
| 75 | // GIVEN |
| 76 | UniformValue v(QVector2D(355.0f, 383.0f)); |
| 77 | // THEN |
| 78 | QCOMPARE(v.constData<float>()[0], 355.0f); |
| 79 | QCOMPARE(v.constData<float>()[1], 383.0f); |
| 80 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 81 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 82 | } |
| 83 | { |
| 84 | // GIVEN |
| 85 | UniformValue v(Vector3D(572.0f, 355.0f, 383.0f)); |
| 86 | // THEN |
| 87 | QCOMPARE(v.constData<float>()[0], 572.0f); |
| 88 | QCOMPARE(v.constData<float>()[1], 355.0f); |
| 89 | QCOMPARE(v.constData<float>()[2], 383.0f); |
| 90 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 91 | } |
| 92 | { |
| 93 | // GIVEN |
| 94 | UniformValue v(Vector4D(355.0f, 383.0f, 1340.0f, 1603.0f)); |
| 95 | // THEN |
| 96 | QCOMPARE(v.constData<float>()[0], 355.0f); |
| 97 | QCOMPARE(v.constData<float>()[1], 383.0f); |
| 98 | QCOMPARE(v.constData<float>()[2], 1340.0f); |
| 99 | QCOMPARE(v.constData<float>()[3], 1603.0f); |
| 100 | } |
| 101 | { |
| 102 | // GIVEN |
| 103 | const QMatrix4x4 m1; |
| 104 | QMatrix4x4 m2; |
| 105 | m2.rotate(90.0f, 1.0f, 0.0f, 0.0f); |
| 106 | QMatrix4x4 m3; |
| 107 | m3.scale(2.5f); |
| 108 | QMatrix4x4 m4; |
| 109 | m4.translate(1.0f, 2.0f, 3.0f); |
| 110 | |
| 111 | const QVector<QMatrix4x4> matrices = { m1, m2, m3, m4 }; |
| 112 | UniformValue v(matrices); |
| 113 | |
| 114 | // THEN |
| 115 | for (int j = 0; j < matrices.size(); ++j) { |
| 116 | for (int i = 0; i < 16; ++i) { |
| 117 | QCOMPARE(v.constData<float>()[16 * j + i], matrices[j].constData()[i]); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | { |
| 122 | // GIVEN |
| 123 | const Qt3DCore::QNodeId nodeId = Qt3DCore::QNodeId::createId(); |
| 124 | UniformValue v(nodeId); |
| 125 | |
| 126 | // THEN |
| 127 | QCOMPARE(uint(v.byteSize()), sizeof(Qt3DCore::QNodeId)); |
| 128 | QCOMPARE(v.constData<Qt3DCore::QNodeId>()[0], nodeId); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void checkFromVariant() |
| 133 | { |
| 134 | { |
| 135 | // GIVEN |
| 136 | UniformValue v = UniformValue::fromVariant(QVariant(883)); |
| 137 | // THEN |
| 138 | QCOMPARE(v.constData<int>()[0], 883); |
| 139 | QCOMPARE(v.constData<int>()[1], 0); |
| 140 | QCOMPARE(v.constData<int>()[2], 0); |
| 141 | QCOMPARE(v.constData<int>()[3], 0); |
| 142 | } |
| 143 | { |
| 144 | // GIVEN |
| 145 | UniformValue v = UniformValue::fromVariant(QVariant(1584U)); |
| 146 | // THEN |
| 147 | QCOMPARE(v.constData<uint>()[0], 1584U); |
| 148 | QCOMPARE(v.constData<uint>()[1], 0U); |
| 149 | QCOMPARE(v.constData<uint>()[2], 0U); |
| 150 | QCOMPARE(v.constData<uint>()[3], 0U); |
| 151 | } |
| 152 | { |
| 153 | // GIVEN |
| 154 | UniformValue v = UniformValue::fromVariant(QVariant(454.0f)); |
| 155 | // THEN |
| 156 | QCOMPARE(v.constData<float>()[0], 454.0f); |
| 157 | QCOMPARE(v.constData<float>()[1], 0.0f); |
| 158 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 159 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 160 | } |
| 161 | { |
| 162 | // GIVEN |
| 163 | UniformValue v = UniformValue::fromVariant(QVariant(350.0)); |
| 164 | // THEN |
| 165 | // Note: Uniform value does a double -> float conversion |
| 166 | QCOMPARE(v.constData<float>()[0], 350.0f); |
| 167 | QCOMPARE(v.constData<float>()[1], 0.0f); |
| 168 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 169 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 170 | } |
| 171 | { |
| 172 | // GIVEN |
| 173 | UniformValue v = UniformValue::fromVariant(QVariant(true)); |
| 174 | // THEN |
| 175 | QCOMPARE(v.constData<bool>()[0], true); |
| 176 | QCOMPARE(v.constData<bool>()[1], false); |
| 177 | QCOMPARE(v.constData<bool>()[2], false); |
| 178 | QCOMPARE(v.constData<bool>()[3], false); |
| 179 | } |
| 180 | { |
| 181 | // GIVEN |
| 182 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QVector2D(355.0f, 383.0f))); |
| 183 | // THEN |
| 184 | QCOMPARE(v.constData<float>()[0], 355.0f); |
| 185 | QCOMPARE(v.constData<float>()[1], 383.0f); |
| 186 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 187 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 188 | } |
| 189 | { |
| 190 | // GIVEN |
| 191 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QVector3D(572.0f, 355.0f, 383.0f))); |
| 192 | // THEN |
| 193 | QCOMPARE(v.constData<float>()[0], 572.0f); |
| 194 | QCOMPARE(v.constData<float>()[1], 355.0f); |
| 195 | QCOMPARE(v.constData<float>()[2], 383.0f); |
| 196 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 197 | } |
| 198 | { |
| 199 | // GIVEN |
| 200 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QVector4D(355.0f, 383.0f, 1340.0f, 1603.0f))); |
| 201 | // THEN |
| 202 | QCOMPARE(v.constData<float>()[0], 355.0f); |
| 203 | QCOMPARE(v.constData<float>()[1], 383.0f); |
| 204 | QCOMPARE(v.constData<float>()[2], 1340.0f); |
| 205 | QCOMPARE(v.constData<float>()[3], 1603.0f); |
| 206 | } |
| 207 | { |
| 208 | // GIVEN |
| 209 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QPoint(427, 396))); |
| 210 | // THEN |
| 211 | QCOMPARE(v.constData<int>()[0], 427); |
| 212 | QCOMPARE(v.constData<int>()[1], 396); |
| 213 | QCOMPARE(v.constData<int>()[2], 0); |
| 214 | QCOMPARE(v.constData<int>()[3], 0); |
| 215 | } |
| 216 | { |
| 217 | // GIVEN |
| 218 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QSize(427, 396))); |
| 219 | // THEN |
| 220 | QCOMPARE(v.constData<int>()[0], 427); |
| 221 | QCOMPARE(v.constData<int>()[1], 396); |
| 222 | QCOMPARE(v.constData<int>()[2], 0); |
| 223 | QCOMPARE(v.constData<int>()[3], 0); |
| 224 | } |
| 225 | { |
| 226 | // GIVEN |
| 227 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QRect(427, 396, 454, 1584))); |
| 228 | // THEN |
| 229 | QCOMPARE(v.constData<int>()[0], 427); |
| 230 | QCOMPARE(v.constData<int>()[1], 396); |
| 231 | QCOMPARE(v.constData<int>()[2], 454); |
| 232 | QCOMPARE(v.constData<int>()[3], 1584); |
| 233 | } |
| 234 | { |
| 235 | // GIVEN |
| 236 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QPointF(427, 396))); |
| 237 | // THEN |
| 238 | QCOMPARE(v.constData<float>()[0], 427.0f); |
| 239 | QCOMPARE(v.constData<float>()[1], 396.0f); |
| 240 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 241 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 242 | } |
| 243 | { |
| 244 | // GIVEN |
| 245 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QSizeF(427, 396))); |
| 246 | // THEN |
| 247 | QCOMPARE(v.constData<float>()[0], 427.0f); |
| 248 | QCOMPARE(v.constData<float>()[1], 396.0f); |
| 249 | QCOMPARE(v.constData<float>()[2], 0.0f); |
| 250 | QCOMPARE(v.constData<float>()[3], 0.0f); |
| 251 | } |
| 252 | { |
| 253 | // GIVEN |
| 254 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QRectF(427, 396, 454, 1584))); |
| 255 | // THEN |
| 256 | QCOMPARE(v.constData<float>()[0], 427.0f); |
| 257 | QCOMPARE(v.constData<float>()[1], 396.0f); |
| 258 | QCOMPARE(v.constData<float>()[2], 454.0f); |
| 259 | QCOMPARE(v.constData<float>()[3], 1584.0f); |
| 260 | } |
| 261 | { |
| 262 | // GIVEN |
| 263 | UniformValue v = UniformValue::fromVariant(QVariant::fromValue(QMatrix4x4())); |
| 264 | // THEN |
| 265 | QCOMPARE(v.constData<float>()[ 0], 1.0f); |
| 266 | QCOMPARE(v.constData<float>()[ 1], 0.0f); |
| 267 | QCOMPARE(v.constData<float>()[ 2], 0.0f); |
| 268 | QCOMPARE(v.constData<float>()[ 3], 0.0f); |
| 269 | |
| 270 | QCOMPARE(v.constData<float>()[ 4], 0.0f); |
| 271 | QCOMPARE(v.constData<float>()[ 5], 1.0f); |
| 272 | QCOMPARE(v.constData<float>()[ 6], 0.0f); |
| 273 | QCOMPARE(v.constData<float>()[ 7], 0.0f); |
| 274 | |
| 275 | QCOMPARE(v.constData<float>()[ 8], 0.0f); |
| 276 | QCOMPARE(v.constData<float>()[ 9], 0.0f); |
| 277 | QCOMPARE(v.constData<float>()[10], 1.0f); |
| 278 | QCOMPARE(v.constData<float>()[11], 0.0f); |
| 279 | |
| 280 | QCOMPARE(v.constData<float>()[12], 0.0f); |
| 281 | QCOMPARE(v.constData<float>()[13], 0.0f); |
| 282 | QCOMPARE(v.constData<float>()[14], 0.0f); |
| 283 | QCOMPARE(v.constData<float>()[15], 1.0f); |
| 284 | } |
| 285 | { |
| 286 | // GIVEN |
| 287 | QVariant variants = QVariantList() << QVariant(427.0f) << QVariant(454.0f) << QVariant(883.0f) << QVariant(1340.0f); |
| 288 | UniformValue v = UniformValue::fromVariant(variants); |
| 289 | |
| 290 | // THEN |
| 291 | QCOMPARE(v.constData<float>()[0], 427.0f); |
| 292 | QCOMPARE(v.constData<float>()[1], 454.0f); |
| 293 | QCOMPARE(v.constData<float>()[2], 883.0f); |
| 294 | QCOMPARE(v.constData<float>()[3], 1340.0f); |
| 295 | } |
| 296 | { |
| 297 | // GIVEN |
| 298 | QVariant variants = QVariantList() << QVariant::fromValue(QVector4D(2.0f, 16.0f, 8.0f, 4.0f)) << QVariant(QVector4D(3.0f, 24.0f, 12.0f, 6.0f)); |
| 299 | UniformValue v = UniformValue::fromVariant(variants); |
| 300 | |
| 301 | // THEN |
| 302 | QCOMPARE(v.constData<float>()[0], 2.0f); |
| 303 | QCOMPARE(v.constData<float>()[1], 16.0f); |
| 304 | QCOMPARE(v.constData<float>()[2], 8.0f); |
| 305 | QCOMPARE(v.constData<float>()[3], 4.0f); |
| 306 | QCOMPARE(v.constData<float>()[4], 3.0f); |
| 307 | QCOMPARE(v.constData<float>()[5], 24.0f); |
| 308 | QCOMPARE(v.constData<float>()[6], 12.0f); |
| 309 | QCOMPARE(v.constData<float>()[7], 6.0f); |
| 310 | } |
| 311 | { |
| 312 | // GIVEN |
| 313 | QVariant variants = QVariantList() << QVariant(427) << QVariant(454) << QVariant(883) << QVariant(1340); |
| 314 | UniformValue v = UniformValue::fromVariant(variants); |
| 315 | |
| 316 | // THEN |
| 317 | QCOMPARE(v.constData<int>()[0], 427); |
| 318 | QCOMPARE(v.constData<int>()[1], 454); |
| 319 | QCOMPARE(v.constData<int>()[2], 883); |
| 320 | QCOMPARE(v.constData<int>()[3], 1340); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void checkComparison() |
| 325 | { |
| 326 | #ifdef Q_OS_MACOS |
| 327 | QSKIP("Ignoring on the mac for now, crashes in 10.14"); |
| 328 | #endif |
| 329 | // GIVEN |
| 330 | const UniformValue v1(Vector3D(454.0f, 883.0f, 572.0f)); |
| 331 | UniformValue v2(454.0f); |
| 332 | |
| 333 | // THEN |
| 334 | QVERIFY(!(v1 == v2)); |
| 335 | QVERIFY(v1 != v2); |
| 336 | |
| 337 | // WHEN |
| 338 | v2 = UniformValue::fromVariant(QVariant::fromValue(Vector3D(454.0f, 883.0f, 572.0f))); |
| 339 | // THEN |
| 340 | QVERIFY(v1 == v2); |
| 341 | QVERIFY(!(v1 != v2)); |
| 342 | |
| 343 | // WHEN |
| 344 | v2 = UniformValue::fromVariant(QVariant::fromValue(Vector3D(454.0f, 883.0f, 572.0f))); |
| 345 | // THEN |
| 346 | QVERIFY(v1 == v2); |
| 347 | QVERIFY(!(v1 != v2)); |
| 348 | |
| 349 | // WHEN |
| 350 | v2 = UniformValue::fromVariant(454.0f); |
| 351 | // THEN |
| 352 | QVERIFY(!(v1 == v2)); |
| 353 | QVERIFY(v1 != v2); |
| 354 | } |
| 355 | |
| 356 | void checkSetData() |
| 357 | { |
| 358 | // GIVEN |
| 359 | const QMatrix4x4 m1; |
| 360 | QMatrix4x4 m2; |
| 361 | m2.rotate(90.0f, 1.0f, 0.0f, 0.0f); |
| 362 | QMatrix4x4 m3; |
| 363 | m3.scale(2.5f); |
| 364 | QMatrix4x4 m4; |
| 365 | m4.translate(1.0f, 2.0f, 3.0f); |
| 366 | |
| 367 | const QVector<QMatrix4x4> matrices1 = { m1, m2, m3, m4 }; |
| 368 | UniformValue v(matrices1); |
| 369 | |
| 370 | // WHEN |
| 371 | const QVector<QMatrix4x4> matrices2 = { m4, m3, m2, m1, m4 }; |
| 372 | v.setData(matrices2); |
| 373 | |
| 374 | // THEN |
| 375 | for (int j = 0; j < matrices2.size(); ++j) { |
| 376 | for (int i = 0; i < 16; ++i) { |
| 377 | QCOMPARE(v.constData<float>()[16 * j + i], matrices2[j].constData()[i]); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // GIVEN |
| 382 | const int positionCount = 10; |
| 383 | QVector<QVector3D> positions(positionCount); |
| 384 | for (int i = 0; i < positionCount; ++i) { |
| 385 | const QVector3D p(float(i), 10.0f * i, 100.0f * i); |
| 386 | positions[i] = p; |
| 387 | } |
| 388 | |
| 389 | UniformValue positionsUniform; |
| 390 | |
| 391 | // WHEN |
| 392 | positionsUniform.setData(positions); |
| 393 | |
| 394 | // THEN |
| 395 | const QVector3D *data = positionsUniform.constData<QVector3D>(); |
| 396 | for (int i = 0; i < positionCount; ++i) { |
| 397 | QCOMPARE(*(data + i), positions[i]); |
| 398 | } |
| 399 | } |
| 400 | }; |
| 401 | |
| 402 | |
| 403 | QTEST_APPLESS_MAIN(tst_Uniform) |
| 404 | |
| 405 | #include "tst_uniform.moc" |