| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | |
| 5 | #include <QtTest/QTest> |
| 6 | #include <Qt3DRender/qcomputecommand.h> |
| 7 | #include <Qt3DRender/private/qcomputecommand_p.h> |
| 8 | #include <Qt3DRender/private/computecommand_p.h> |
| 9 | #include <Qt3DRender/private/nodemanagers_p.h> |
| 10 | #include <Qt3DRender/private/managers_p.h> |
| 11 | #include <Qt3DCore/private/qbackendnode_p.h> |
| 12 | #include <Qt3DCore/private/qaspectmanager_p.h> |
| 13 | #include <Qt3DCore/private/qscene_p.h> |
| 14 | #include "qbackendnodetester.h" |
| 15 | #include "testrenderer.h" |
| 16 | #include "testarbiter.h" |
| 17 | |
| 18 | |
| 19 | class tst_ComputeCommand : public Qt3DCore::QBackendNodeTester |
| 20 | { |
| 21 | Q_OBJECT |
| 22 | |
| 23 | private Q_SLOTS: |
| 24 | |
| 25 | void checkInitialState() |
| 26 | { |
| 27 | // GIVEN |
| 28 | Qt3DRender::Render::ComputeCommand backendComputeCommand; |
| 29 | |
| 30 | // THEN |
| 31 | QCOMPARE(backendComputeCommand.isEnabled(), false); |
| 32 | QVERIFY(backendComputeCommand.peerId().isNull()); |
| 33 | QCOMPARE(backendComputeCommand.x(), 1); |
| 34 | QCOMPARE(backendComputeCommand.y(), 1); |
| 35 | QCOMPARE(backendComputeCommand.z(), 1); |
| 36 | QCOMPARE(backendComputeCommand.hasReachedFrameCount(), false); |
| 37 | QCOMPARE(backendComputeCommand.runType(), Qt3DRender::QComputeCommand::Continuous); |
| 38 | QCOMPARE(backendComputeCommand.frameCount(), 0); |
| 39 | } |
| 40 | |
| 41 | void checkCleanupState() |
| 42 | { |
| 43 | // GIVEN |
| 44 | TestRenderer renderer; |
| 45 | Qt3DRender::Render::ComputeCommand backendComputeCommand; |
| 46 | Qt3DRender::QComputeCommand computeCommand; |
| 47 | computeCommand.setWorkGroupX(256); |
| 48 | computeCommand.setWorkGroupY(512); |
| 49 | computeCommand.setWorkGroupZ(128); |
| 50 | computeCommand.setRunType(Qt3DRender::QComputeCommand::Manual); |
| 51 | computeCommand.trigger(1); |
| 52 | |
| 53 | // WHEN |
| 54 | backendComputeCommand.setRenderer(&renderer); |
| 55 | simulateInitializationSync(&computeCommand, &backendComputeCommand); |
| 56 | |
| 57 | backendComputeCommand.setEnabled(true); |
| 58 | backendComputeCommand.hasReachedFrameCount(); |
| 59 | |
| 60 | backendComputeCommand.cleanup(); |
| 61 | |
| 62 | // THEN |
| 63 | QCOMPARE(backendComputeCommand.isEnabled(), false); |
| 64 | QCOMPARE(backendComputeCommand.hasReachedFrameCount(), false); |
| 65 | } |
| 66 | |
| 67 | void checkInitializeFromPeer() |
| 68 | { |
| 69 | // GIVEN |
| 70 | TestRenderer renderer; |
| 71 | Qt3DRender::QComputeCommand computeCommand; |
| 72 | computeCommand.setWorkGroupX(256); |
| 73 | computeCommand.setWorkGroupY(512); |
| 74 | computeCommand.setWorkGroupZ(128); |
| 75 | computeCommand.setRunType(Qt3DRender::QComputeCommand::Manual); |
| 76 | computeCommand.trigger(6); |
| 77 | |
| 78 | { |
| 79 | // WHEN |
| 80 | Qt3DRender::Render::ComputeCommand backendComputeCommand; |
| 81 | backendComputeCommand.setRenderer(&renderer); |
| 82 | simulateInitializationSync(&computeCommand, &backendComputeCommand); |
| 83 | |
| 84 | // THEN |
| 85 | QCOMPARE(backendComputeCommand.isEnabled(), true); |
| 86 | QCOMPARE(backendComputeCommand.peerId(), computeCommand.id()); |
| 87 | QCOMPARE(backendComputeCommand.x(), computeCommand.workGroupX()); |
| 88 | QCOMPARE(backendComputeCommand.y(), computeCommand.workGroupY()); |
| 89 | QCOMPARE(backendComputeCommand.z(), computeCommand.workGroupZ()); |
| 90 | QCOMPARE(backendComputeCommand.runType(), computeCommand.runType()); |
| 91 | QCOMPARE(backendComputeCommand.frameCount(), 6); |
| 92 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ComputeDirty); |
| 93 | } |
| 94 | renderer.clearDirtyBits(Qt3DRender::Render::AbstractRenderer::AllDirty); |
| 95 | { |
| 96 | // WHEN |
| 97 | Qt3DRender::Render::ComputeCommand backendComputeCommand; |
| 98 | backendComputeCommand.setRenderer(&renderer); |
| 99 | computeCommand.setEnabled(false); |
| 100 | simulateInitializationSync(&computeCommand, &backendComputeCommand); |
| 101 | |
| 102 | // THEN |
| 103 | QCOMPARE(backendComputeCommand.peerId(), computeCommand.id()); |
| 104 | QCOMPARE(backendComputeCommand.isEnabled(), false); |
| 105 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ComputeDirty); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void checkSceneChangeEvents() |
| 110 | { |
| 111 | // GIVEN |
| 112 | Qt3DRender::Render::ComputeCommand backendComputeCommand; |
| 113 | Qt3DRender::QComputeCommand computeCommand; |
| 114 | TestRenderer renderer; |
| 115 | backendComputeCommand.setRenderer(&renderer); |
| 116 | simulateInitializationSync(&computeCommand, &backendComputeCommand); |
| 117 | |
| 118 | // THEN |
| 119 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ComputeDirty); |
| 120 | renderer.clearDirtyBits(Qt3DRender::Render::AbstractRenderer::AllDirty); |
| 121 | |
| 122 | { |
| 123 | // WHEN |
| 124 | const bool newValue = false; |
| 125 | computeCommand.setEnabled(newValue); |
| 126 | backendComputeCommand.syncFromFrontEnd(&computeCommand, false); |
| 127 | |
| 128 | // THEN |
| 129 | QCOMPARE(backendComputeCommand.isEnabled(), newValue); |
| 130 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ComputeDirty); |
| 131 | renderer.clearDirtyBits(Qt3DRender::Render::AbstractRenderer::AllDirty); |
| 132 | } |
| 133 | { |
| 134 | // WHEN |
| 135 | const int newValue = 128; |
| 136 | computeCommand.setWorkGroupX(newValue); |
| 137 | backendComputeCommand.syncFromFrontEnd(&computeCommand, false); |
| 138 | |
| 139 | // THEN |
| 140 | QCOMPARE(backendComputeCommand.x(), newValue); |
| 141 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ComputeDirty); |
| 142 | renderer.clearDirtyBits(Qt3DRender::Render::AbstractRenderer::AllDirty); |
| 143 | } |
| 144 | { |
| 145 | // WHEN |
| 146 | const int newValue = 64; |
| 147 | computeCommand.setWorkGroupY(newValue); |
| 148 | backendComputeCommand.syncFromFrontEnd(&computeCommand, false); |
| 149 | |
| 150 | // THEN |
| 151 | QCOMPARE(backendComputeCommand.y(), newValue); |
| 152 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ComputeDirty); |
| 153 | renderer.clearDirtyBits(Qt3DRender::Render::AbstractRenderer::AllDirty); |
| 154 | } |
| 155 | { |
| 156 | // WHEN |
| 157 | const int newValue = 32; |
| 158 | computeCommand.setWorkGroupZ(newValue); |
| 159 | backendComputeCommand.syncFromFrontEnd(&computeCommand, false); |
| 160 | |
| 161 | // THEN |
| 162 | QCOMPARE(backendComputeCommand.z(), newValue); |
| 163 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ComputeDirty); |
| 164 | renderer.clearDirtyBits(Qt3DRender::Render::AbstractRenderer::AllDirty); |
| 165 | } |
| 166 | { |
| 167 | // WHEN |
| 168 | const Qt3DRender::QComputeCommand::RunType newValue = Qt3DRender::QComputeCommand::Manual; |
| 169 | computeCommand.setRunType(newValue); |
| 170 | backendComputeCommand.syncFromFrontEnd(&computeCommand, false); |
| 171 | |
| 172 | // THEN |
| 173 | QCOMPARE(backendComputeCommand.runType(), newValue); |
| 174 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ComputeDirty); |
| 175 | renderer.clearDirtyBits(Qt3DRender::Render::AbstractRenderer::AllDirty); |
| 176 | } |
| 177 | { |
| 178 | // WHEN |
| 179 | const int newValue = 32; |
| 180 | computeCommand.trigger(newValue); |
| 181 | backendComputeCommand.syncFromFrontEnd(&computeCommand, false); |
| 182 | |
| 183 | // THEN |
| 184 | QCOMPARE(backendComputeCommand.frameCount(), newValue); |
| 185 | QVERIFY(renderer.dirtyBits() & Qt3DRender::Render::AbstractRenderer::ComputeDirty); |
| 186 | renderer.clearDirtyBits(Qt3DRender::Render::AbstractRenderer::AllDirty); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void checkUpdateFrameCount() |
| 191 | { |
| 192 | // GIVEN |
| 193 | TestRenderer renderer; |
| 194 | |
| 195 | Qt3DRender::QComputeCommand computeCommand; |
| 196 | Qt3DRender::Render::ComputeCommand backendComputeCommand; |
| 197 | |
| 198 | computeCommand.setWorkGroupX(256); |
| 199 | computeCommand.setWorkGroupY(512); |
| 200 | computeCommand.setWorkGroupZ(128); |
| 201 | computeCommand.setRunType(Qt3DRender::QComputeCommand::Manual); |
| 202 | computeCommand.trigger(6); |
| 203 | |
| 204 | |
| 205 | backendComputeCommand.setRenderer(&renderer); |
| 206 | simulateInitializationSync(&computeCommand, &backendComputeCommand); |
| 207 | |
| 208 | for (int i = 0; i < 5; ++i) { |
| 209 | // WHEN |
| 210 | backendComputeCommand.updateFrameCount(); |
| 211 | |
| 212 | // THEN |
| 213 | QCOMPARE(backendComputeCommand.frameCount(), 6 - (i + 1)); |
| 214 | QCOMPARE(backendComputeCommand.isEnabled(), true); |
| 215 | QCOMPARE(backendComputeCommand.hasReachedFrameCount(), false); |
| 216 | } |
| 217 | |
| 218 | // WHEN |
| 219 | backendComputeCommand.updateFrameCount(); |
| 220 | |
| 221 | // THEN |
| 222 | QCOMPARE(backendComputeCommand.hasReachedFrameCount(), true); |
| 223 | QCOMPARE(backendComputeCommand.frameCount(), 0); |
| 224 | // Backend stays with enabled == true, frontend will be updated |
| 225 | // to be disabled and backend should be disabled on the next sync |
| 226 | |
| 227 | // WHEN |
| 228 | computeCommand.setEnabled(false); |
| 229 | backendComputeCommand.syncFromFrontEnd(&computeCommand, false); |
| 230 | |
| 231 | // THEN |
| 232 | QCOMPARE(backendComputeCommand.isEnabled(), false); |
| 233 | QCOMPARE(backendComputeCommand.frameCount(), 0); |
| 234 | |
| 235 | // WHEN |
| 236 | computeCommand.trigger(1); |
| 237 | backendComputeCommand.syncFromFrontEnd(&computeCommand, false); |
| 238 | |
| 239 | // THEN |
| 240 | QCOMPARE(computeCommand.isEnabled(), true); |
| 241 | QCOMPARE(backendComputeCommand.isEnabled(), true); |
| 242 | QCOMPARE(backendComputeCommand.hasReachedFrameCount(), false); |
| 243 | QCOMPARE(backendComputeCommand.frameCount(), 1); |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | QTEST_MAIN(tst_ComputeCommand) |
| 248 | |
| 249 | #include "tst_computecommand.moc" |