| 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 |  | 
| 30 | #include <QtTest/QTest> | 
| 31 | #include <Qt3DRender/qcomputecommand.h> | 
| 32 | #include <Qt3DRender/private/qcomputecommand_p.h> | 
| 33 | #include <QObject> | 
| 34 | #include <QSignalSpy> | 
| 35 | #include <Qt3DCore/private/qnodecreatedchangegenerator_p.h> | 
| 36 | #include <Qt3DCore/qnodecreatedchange.h> | 
| 37 | #include "testpostmanarbiter.h" | 
| 38 |  | 
| 39 | class tst_QComputeCommand : public QObject | 
| 40 | { | 
| 41 |     Q_OBJECT | 
| 42 |  | 
| 43 | private Q_SLOTS: | 
| 44 |  | 
| 45 |     void checkDefaultConstruction() | 
| 46 |     { | 
| 47 |         // GIVEN | 
| 48 |         Qt3DRender::QComputeCommand computeCommand; | 
| 49 |  | 
| 50 |         // THEN | 
| 51 |         QCOMPARE(computeCommand.workGroupX(), 1); | 
| 52 |         QCOMPARE(computeCommand.workGroupY(), 1); | 
| 53 |         QCOMPARE(computeCommand.workGroupZ(), 1); | 
| 54 |         QCOMPARE(computeCommand.runType(), Qt3DRender::QComputeCommand::Continuous); | 
| 55 |     } | 
| 56 |  | 
| 57 |     void checkPropertyChanges() | 
| 58 |     { | 
| 59 |         // GIVEN | 
| 60 |         Qt3DRender::QComputeCommand computeCommand; | 
| 61 |  | 
| 62 |         { | 
| 63 |             // WHEN | 
| 64 |             QSignalSpy spy(&computeCommand, SIGNAL(workGroupXChanged())); | 
| 65 |             const int newValue = 128; | 
| 66 |             computeCommand.setWorkGroupX(newValue); | 
| 67 |  | 
| 68 |             // THEN | 
| 69 |             QVERIFY(spy.isValid()); | 
| 70 |             QCOMPARE(computeCommand.workGroupX(), newValue); | 
| 71 |             QCOMPARE(spy.count(), 1); | 
| 72 |  | 
| 73 |             // WHEN | 
| 74 |             spy.clear(); | 
| 75 |             computeCommand.setWorkGroupX(newValue); | 
| 76 |  | 
| 77 |             // THEN | 
| 78 |             QCOMPARE(computeCommand.workGroupX(), newValue); | 
| 79 |             QCOMPARE(spy.count(), 0); | 
| 80 |         } | 
| 81 |         { | 
| 82 |             // WHEN | 
| 83 |             QSignalSpy spy(&computeCommand, SIGNAL(workGroupYChanged())); | 
| 84 |             const int newValue = 256; | 
| 85 |             computeCommand.setWorkGroupY(newValue); | 
| 86 |  | 
| 87 |             // THEN | 
| 88 |             QVERIFY(spy.isValid()); | 
| 89 |             QCOMPARE(computeCommand.workGroupY(), newValue); | 
| 90 |             QCOMPARE(spy.count(), 1); | 
| 91 |  | 
| 92 |             // WHEN | 
| 93 |             spy.clear(); | 
| 94 |             computeCommand.setWorkGroupY(newValue); | 
| 95 |  | 
| 96 |             // THEN | 
| 97 |             QCOMPARE(computeCommand.workGroupY(), newValue); | 
| 98 |             QCOMPARE(spy.count(), 0); | 
| 99 |         } | 
| 100 |         { | 
| 101 |             // WHEN | 
| 102 |             QSignalSpy spy(&computeCommand, SIGNAL(workGroupZChanged())); | 
| 103 |             const int newValue = 512; | 
| 104 |             computeCommand.setWorkGroupZ(newValue); | 
| 105 |  | 
| 106 |             // THEN | 
| 107 |             QVERIFY(spy.isValid()); | 
| 108 |             QCOMPARE(computeCommand.workGroupZ(), newValue); | 
| 109 |             QCOMPARE(spy.count(), 1); | 
| 110 |  | 
| 111 |             // WHEN | 
| 112 |             spy.clear(); | 
| 113 |             computeCommand.setWorkGroupZ(newValue); | 
| 114 |  | 
| 115 |             // THEN | 
| 116 |             QCOMPARE(computeCommand.workGroupZ(), newValue); | 
| 117 |             QCOMPARE(spy.count(), 0); | 
| 118 |         } | 
| 119 |         { | 
| 120 |             // WHEN | 
| 121 |             QSignalSpy spy(&computeCommand, SIGNAL(runTypeChanged())); | 
| 122 |             const Qt3DRender::QComputeCommand::RunType newValue = Qt3DRender::QComputeCommand::Manual; | 
| 123 |             computeCommand.setRunType(newValue); | 
| 124 |  | 
| 125 |             // THEN | 
| 126 |             QVERIFY(spy.isValid()); | 
| 127 |             QCOMPARE(computeCommand.runType(), newValue); | 
| 128 |             QCOMPARE(spy.count(), 1); | 
| 129 |  | 
| 130 |             // WHEN | 
| 131 |             spy.clear(); | 
| 132 |             computeCommand.setRunType(newValue); | 
| 133 |  | 
| 134 |             // THEN | 
| 135 |             QCOMPARE(computeCommand.runType(), newValue); | 
| 136 |             QCOMPARE(spy.count(), 0); | 
| 137 |         } | 
| 138 |     } | 
| 139 |  | 
| 140 |     void checkCreationData() | 
| 141 |     { | 
| 142 |         // GIVEN | 
| 143 |         Qt3DRender::QComputeCommand computeCommand; | 
| 144 |  | 
| 145 |         computeCommand.setWorkGroupX(128); | 
| 146 |         computeCommand.setWorkGroupY(512); | 
| 147 |         computeCommand.setWorkGroupZ(1024); | 
| 148 |         computeCommand.setRunType(Qt3DRender::QComputeCommand::Manual); | 
| 149 |  | 
| 150 |         // WHEN | 
| 151 |         QVector<Qt3DCore::QNodeCreatedChangeBasePtr> creationChanges; | 
| 152 |  | 
| 153 |         { | 
| 154 |             Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&computeCommand); | 
| 155 |             creationChanges = creationChangeGenerator.creationChanges(); | 
| 156 |         } | 
| 157 |  | 
| 158 |         // THEN | 
| 159 |         { | 
| 160 |             QCOMPARE(creationChanges.size(), 1); | 
| 161 |  | 
| 162 |             const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QComputeCommandData>>(src: creationChanges.first()); | 
| 163 |             const Qt3DRender::QComputeCommandData cloneData = creationChangeData->data; | 
| 164 |  | 
| 165 |             QCOMPARE(computeCommand.workGroupX(), cloneData.workGroupX); | 
| 166 |             QCOMPARE(computeCommand.workGroupY(), cloneData.workGroupY); | 
| 167 |             QCOMPARE(computeCommand.workGroupZ(), cloneData.workGroupZ); | 
| 168 |             QCOMPARE(computeCommand.runType(), cloneData.runType); | 
| 169 |             QCOMPARE(0, cloneData.frameCount); | 
| 170 |             QCOMPARE(computeCommand.id(), creationChangeData->subjectId()); | 
| 171 |             QCOMPARE(computeCommand.isEnabled(), true); | 
| 172 |             QCOMPARE(computeCommand.isEnabled(), creationChangeData->isNodeEnabled()); | 
| 173 |             QCOMPARE(computeCommand.metaObject(), creationChangeData->metaObject()); | 
| 174 |         } | 
| 175 |  | 
| 176 |         // WHEN | 
| 177 |         computeCommand.setEnabled(false); | 
| 178 |  | 
| 179 |         { | 
| 180 |             Qt3DCore::QNodeCreatedChangeGenerator creationChangeGenerator(&computeCommand); | 
| 181 |             creationChanges = creationChangeGenerator.creationChanges(); | 
| 182 |         } | 
| 183 |  | 
| 184 |         // THEN | 
| 185 |         { | 
| 186 |             QCOMPARE(creationChanges.size(), 1); | 
| 187 |  | 
| 188 |             const auto creationChangeData = qSharedPointerCast<Qt3DCore::QNodeCreatedChange<Qt3DRender::QComputeCommandData>>(src: creationChanges.first()); | 
| 189 |             const Qt3DRender::QComputeCommandData cloneData = creationChangeData->data; | 
| 190 |  | 
| 191 |             QCOMPARE(computeCommand.workGroupX(), cloneData.workGroupX); | 
| 192 |             QCOMPARE(computeCommand.workGroupY(), cloneData.workGroupY); | 
| 193 |             QCOMPARE(computeCommand.workGroupZ(), cloneData.workGroupZ); | 
| 194 |             QCOMPARE(computeCommand.id(), creationChangeData->subjectId()); | 
| 195 |             QCOMPARE(computeCommand.isEnabled(), false); | 
| 196 |             QCOMPARE(computeCommand.runType(), cloneData.runType); | 
| 197 |             QCOMPARE(0, cloneData.frameCount); | 
| 198 |             QCOMPARE(computeCommand.isEnabled(), creationChangeData->isNodeEnabled()); | 
| 199 |             QCOMPARE(computeCommand.metaObject(), creationChangeData->metaObject()); | 
| 200 |         } | 
| 201 |     } | 
| 202 |  | 
| 203 |     void checkWorkGroupXUpdate() | 
| 204 |     { | 
| 205 |         // GIVEN | 
| 206 |         TestArbiter arbiter; | 
| 207 |         Qt3DRender::QComputeCommand computeCommand; | 
| 208 |         arbiter.setArbiterOnNode(&computeCommand); | 
| 209 |  | 
| 210 |         { | 
| 211 |             // WHEN | 
| 212 |             computeCommand.setWorkGroupX(256); | 
| 213 |             QCoreApplication::processEvents(); | 
| 214 |  | 
| 215 |             // THEN | 
| 216 |             QCOMPARE(arbiter.events.size(), 0); | 
| 217 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 218 |             QCOMPARE(arbiter.dirtyNodes.front(), &computeCommand); | 
| 219 |  | 
| 220 |             arbiter.dirtyNodes.clear(); | 
| 221 |         } | 
| 222 |  | 
| 223 |         { | 
| 224 |             // WHEN | 
| 225 |             computeCommand.setWorkGroupX(256); | 
| 226 |             QCoreApplication::processEvents(); | 
| 227 |  | 
| 228 |             // THEN | 
| 229 |             QCOMPARE(arbiter.dirtyNodes.size(), 0); | 
| 230 |         } | 
| 231 |  | 
| 232 |     } | 
| 233 |  | 
| 234 |     void checkWorkGroupYUpdate() | 
| 235 |     { | 
| 236 |         // GIVEN | 
| 237 |         TestArbiter arbiter; | 
| 238 |         Qt3DRender::QComputeCommand computeCommand; | 
| 239 |         arbiter.setArbiterOnNode(&computeCommand); | 
| 240 |  | 
| 241 |         { | 
| 242 |             // WHEN | 
| 243 |             computeCommand.setWorkGroupY(512); | 
| 244 |  | 
| 245 |             // THEN | 
| 246 |             QCOMPARE(arbiter.events.size(), 0); | 
| 247 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 248 |             QCOMPARE(arbiter.dirtyNodes.front(), &computeCommand); | 
| 249 |  | 
| 250 |             arbiter.dirtyNodes.clear(); | 
| 251 |         } | 
| 252 |  | 
| 253 |         { | 
| 254 |             // WHEN | 
| 255 |             computeCommand.setWorkGroupY(512); | 
| 256 |  | 
| 257 |             // THEN | 
| 258 |             QCOMPARE(arbiter.events.size(), 0); | 
| 259 |             QCOMPARE(arbiter.dirtyNodes.size(), 0); | 
| 260 |         } | 
| 261 |  | 
| 262 |     } | 
| 263 |  | 
| 264 |     void checkWorkGroupZUpdate() | 
| 265 |     { | 
| 266 |         // GIVEN | 
| 267 |         TestArbiter arbiter; | 
| 268 |         Qt3DRender::QComputeCommand computeCommand; | 
| 269 |         arbiter.setArbiterOnNode(&computeCommand); | 
| 270 |  | 
| 271 |         { | 
| 272 |             // WHEN | 
| 273 |             computeCommand.setWorkGroupZ(64); | 
| 274 |             QCoreApplication::processEvents(); | 
| 275 |  | 
| 276 |             // THEN | 
| 277 |             QCOMPARE(arbiter.events.size(), 0); | 
| 278 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 279 |             QCOMPARE(arbiter.dirtyNodes.front(), &computeCommand); | 
| 280 |  | 
| 281 |             arbiter.dirtyNodes.clear(); | 
| 282 |         } | 
| 283 |  | 
| 284 |         { | 
| 285 |             // WHEN | 
| 286 |             computeCommand.setWorkGroupZ(64); | 
| 287 |             QCoreApplication::processEvents(); | 
| 288 |  | 
| 289 |             // THEN | 
| 290 |             QCOMPARE(arbiter.events.size(), 0); | 
| 291 |             QCOMPARE(arbiter.dirtyNodes.size(), 0); | 
| 292 |         } | 
| 293 |  | 
| 294 |     } | 
| 295 |  | 
| 296 |     void checkRunTypeUpdate() | 
| 297 |     { | 
| 298 |         // GIVEN | 
| 299 |         TestArbiter arbiter; | 
| 300 |         Qt3DRender::QComputeCommand computeCommand; | 
| 301 |         arbiter.setArbiterOnNode(&computeCommand); | 
| 302 |  | 
| 303 |         { | 
| 304 |             // WHEN | 
| 305 |             computeCommand.setRunType(Qt3DRender::QComputeCommand::Manual); | 
| 306 |             QCoreApplication::processEvents(); | 
| 307 |  | 
| 308 |             // THEN | 
| 309 |             QCOMPARE(arbiter.events.size(), 0); | 
| 310 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 311 |             QCOMPARE(arbiter.dirtyNodes.front(), &computeCommand); | 
| 312 |  | 
| 313 |             arbiter.dirtyNodes.clear(); | 
| 314 |         } | 
| 315 |  | 
| 316 |         { | 
| 317 |             // WHEN | 
| 318 |             computeCommand.setRunType(Qt3DRender::QComputeCommand::Manual); | 
| 319 |             QCoreApplication::processEvents(); | 
| 320 |  | 
| 321 |             // THEN | 
| 322 |             QCOMPARE(arbiter.events.size(), 0); | 
| 323 |             QCOMPARE(arbiter.dirtyNodes.size(), 0); | 
| 324 |         } | 
| 325 |     } | 
| 326 |  | 
| 327 |     void checkTrigger() | 
| 328 |     { | 
| 329 |         // GIVEN | 
| 330 |         TestArbiter arbiter; | 
| 331 |         Qt3DRender::QComputeCommand computeCommand; | 
| 332 |         arbiter.setArbiterOnNode(&computeCommand); | 
| 333 |         computeCommand.setRunType(Qt3DRender::QComputeCommand::Manual); | 
| 334 |         computeCommand.setEnabled(false); | 
| 335 |         QCoreApplication::processEvents(); | 
| 336 |         arbiter.events.clear(); | 
| 337 |  | 
| 338 |         { | 
| 339 |             // WHEN | 
| 340 |             computeCommand.trigger(frameCount: 1); | 
| 341 |             QCoreApplication::processEvents(); | 
| 342 |  | 
| 343 |             // THEN | 
| 344 |             QCOMPARE(arbiter.events.size(), 0); | 
| 345 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 346 |             QCOMPARE(arbiter.dirtyNodes.front(), &computeCommand); | 
| 347 |             QCOMPARE(computeCommand.isEnabled(), true); | 
| 348 |  | 
| 349 |             computeCommand.setEnabled(false); | 
| 350 |             QCoreApplication::processEvents(); | 
| 351 |             arbiter.dirtyNodes.clear(); | 
| 352 |         } | 
| 353 |  | 
| 354 |         { | 
| 355 |             // WHEN | 
| 356 |             computeCommand.trigger(frameCount: 2); | 
| 357 |             QCoreApplication::processEvents(); | 
| 358 |  | 
| 359 |             // THEN | 
| 360 |             QCOMPARE(arbiter.events.size(), 0); | 
| 361 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 362 |             QCOMPARE(arbiter.dirtyNodes.front(), &computeCommand); | 
| 363 |             QCOMPARE(computeCommand.isEnabled(), true); | 
| 364 |  | 
| 365 |             computeCommand.setEnabled(false); | 
| 366 |             QCoreApplication::processEvents(); | 
| 367 |             arbiter.events.clear(); | 
| 368 |             arbiter.dirtyNodes.clear(); | 
| 369 |         } | 
| 370 |  | 
| 371 |         { | 
| 372 |             // WHEN | 
| 373 |             computeCommand.trigger(workGroupX: 10, workGroupY: 11, workGroupZ: 12, frameCount: 1); | 
| 374 |             QCoreApplication::processEvents(); | 
| 375 |  | 
| 376 |             // THEN | 
| 377 |             QCOMPARE(arbiter.events.size(), 0); | 
| 378 |             QCOMPARE(arbiter.dirtyNodes.size(), 1); | 
| 379 |             QCOMPARE(arbiter.dirtyNodes.front(), &computeCommand); | 
| 380 |             QCOMPARE(computeCommand.isEnabled(), true); | 
| 381 |             QCOMPARE(computeCommand.workGroupX(), 10); | 
| 382 |             QCOMPARE(computeCommand.workGroupY(), 11); | 
| 383 |             QCOMPARE(computeCommand.workGroupZ(), 12); | 
| 384 |  | 
| 385 |             computeCommand.setEnabled(false); | 
| 386 |             arbiter.dirtyNodes.clear(); | 
| 387 |         } | 
| 388 |     } | 
| 389 |  | 
| 390 | }; | 
| 391 |  | 
| 392 | QTEST_MAIN(tst_QComputeCommand) | 
| 393 |  | 
| 394 | #include "tst_qcomputecommand.moc" | 
| 395 |  |