| 1 | // Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DRender/qshaderimage.h> |
| 6 | #include <Qt3DRender/qtexture.h> |
| 7 | #include <Qt3DRender/private/qshaderimage_p.h> |
| 8 | #include <QObject> |
| 9 | #include <QSignalSpy> |
| 10 | #include "testarbiter.h" |
| 11 | |
| 12 | class tst_QShaderImage : public QObject |
| 13 | { |
| 14 | Q_OBJECT |
| 15 | |
| 16 | private Q_SLOTS: |
| 17 | |
| 18 | void initTestCase() |
| 19 | { |
| 20 | qRegisterMetaType<Qt3DRender::QAbstractTexture*>("Qt3DRender::QAbstractTexture*"); |
| 21 | |
| 22 | } |
| 23 | |
| 24 | void checkDefaultConstruction() |
| 25 | { |
| 26 | // GIVEN |
| 27 | Qt3DRender::QShaderImage shaderImage; |
| 28 | |
| 29 | // THEN |
| 30 | QVERIFY(shaderImage.texture() == nullptr); |
| 31 | QCOMPARE(shaderImage.layered(), false); |
| 32 | QCOMPARE(shaderImage.mipLevel(), 0); |
| 33 | QCOMPARE(shaderImage.layer(), 0); |
| 34 | QCOMPARE(shaderImage.access(), Qt3DRender::QShaderImage::ReadWrite); |
| 35 | QCOMPARE(shaderImage.format(), Qt3DRender::QShaderImage::Automatic); |
| 36 | } |
| 37 | |
| 38 | void checkPropertyChanges() |
| 39 | { |
| 40 | // GIVEN |
| 41 | Qt3DRender::QShaderImage shaderImage; |
| 42 | |
| 43 | { |
| 44 | // WHEN |
| 45 | QSignalSpy spy(&shaderImage, SIGNAL(textureChanged(Qt3DRender::QAbstractTexture*))); |
| 46 | Qt3DRender::QTexture2D newValue; |
| 47 | shaderImage.setTexture(&newValue); |
| 48 | |
| 49 | // THEN |
| 50 | QVERIFY(spy.isValid()); |
| 51 | QCOMPARE(shaderImage.texture(), &newValue); |
| 52 | QCOMPARE(spy.size(), 1); |
| 53 | |
| 54 | // WHEN |
| 55 | spy.clear(); |
| 56 | shaderImage.setTexture(&newValue); |
| 57 | |
| 58 | // THEN |
| 59 | QCOMPARE(shaderImage.texture(), &newValue); |
| 60 | QCOMPARE(spy.size(), 0); |
| 61 | } |
| 62 | { |
| 63 | // WHEN |
| 64 | QSignalSpy spy(&shaderImage, SIGNAL(layeredChanged(bool))); |
| 65 | const bool newValue = true; |
| 66 | shaderImage.setLayered(newValue); |
| 67 | |
| 68 | // THEN |
| 69 | QVERIFY(spy.isValid()); |
| 70 | QCOMPARE(shaderImage.layered(), newValue); |
| 71 | QCOMPARE(spy.size(), 1); |
| 72 | |
| 73 | // WHEN |
| 74 | spy.clear(); |
| 75 | shaderImage.setLayered(newValue); |
| 76 | |
| 77 | // THEN |
| 78 | QCOMPARE(shaderImage.layered(), newValue); |
| 79 | QCOMPARE(spy.size(), 0); |
| 80 | } |
| 81 | { |
| 82 | // WHEN |
| 83 | QSignalSpy spy(&shaderImage, SIGNAL(mipLevelChanged(int))); |
| 84 | const int newValue = 12; |
| 85 | shaderImage.setMipLevel(newValue); |
| 86 | |
| 87 | // THEN |
| 88 | QVERIFY(spy.isValid()); |
| 89 | QCOMPARE(shaderImage.mipLevel(), newValue); |
| 90 | QCOMPARE(spy.size(), 1); |
| 91 | |
| 92 | // WHEN |
| 93 | spy.clear(); |
| 94 | shaderImage.setMipLevel(newValue); |
| 95 | |
| 96 | // THEN |
| 97 | QCOMPARE(shaderImage.mipLevel(), newValue); |
| 98 | QCOMPARE(spy.size(), 0); |
| 99 | } |
| 100 | { |
| 101 | // WHEN |
| 102 | QSignalSpy spy(&shaderImage, SIGNAL(layerChanged(int))); |
| 103 | const int newValue = 2; |
| 104 | shaderImage.setLayer(newValue); |
| 105 | |
| 106 | // THEN |
| 107 | QVERIFY(spy.isValid()); |
| 108 | QCOMPARE(shaderImage.layer(), newValue); |
| 109 | QCOMPARE(spy.size(), 1); |
| 110 | |
| 111 | // WHEN |
| 112 | spy.clear(); |
| 113 | shaderImage.setLayer(newValue); |
| 114 | |
| 115 | // THEN |
| 116 | QCOMPARE(shaderImage.layer(), newValue); |
| 117 | QCOMPARE(spy.size(), 0); |
| 118 | } |
| 119 | { |
| 120 | // WHEN |
| 121 | QSignalSpy spy(&shaderImage, SIGNAL(accessChanged(Access))); |
| 122 | const Qt3DRender::QShaderImage::Access newValue = Qt3DRender::QShaderImage::ReadOnly; |
| 123 | shaderImage.setAccess(newValue); |
| 124 | |
| 125 | // THEN |
| 126 | QVERIFY(spy.isValid()); |
| 127 | QCOMPARE(shaderImage.access(), newValue); |
| 128 | QCOMPARE(spy.size(), 1); |
| 129 | |
| 130 | // WHEN |
| 131 | spy.clear(); |
| 132 | shaderImage.setAccess(newValue); |
| 133 | |
| 134 | // THEN |
| 135 | QCOMPARE(shaderImage.access(), newValue); |
| 136 | QCOMPARE(spy.size(), 0); |
| 137 | } |
| 138 | { |
| 139 | // WHEN |
| 140 | QSignalSpy spy(&shaderImage, SIGNAL(formatChanged(ImageFormat))); |
| 141 | const Qt3DRender::QShaderImage::ImageFormat newValue = Qt3DRender::QShaderImage::RG8U; |
| 142 | shaderImage.setFormat(newValue); |
| 143 | |
| 144 | // THEN |
| 145 | QVERIFY(spy.isValid()); |
| 146 | QCOMPARE(shaderImage.format(), newValue); |
| 147 | QCOMPARE(spy.size(), 1); |
| 148 | |
| 149 | // WHEN |
| 150 | spy.clear(); |
| 151 | shaderImage.setFormat(newValue); |
| 152 | |
| 153 | // THEN |
| 154 | QCOMPARE(shaderImage.format(), newValue); |
| 155 | QCOMPARE(spy.size(), 0); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | void checkTextureUpdate() |
| 160 | { |
| 161 | // GIVEN |
| 162 | TestArbiter arbiter; |
| 163 | Qt3DRender::QShaderImage shaderImage; |
| 164 | arbiter.setArbiterOnNode(&shaderImage); |
| 165 | Qt3DRender::QTexture2D t; |
| 166 | |
| 167 | { |
| 168 | // WHEN |
| 169 | shaderImage.setTexture(&t); |
| 170 | QCoreApplication::processEvents(); |
| 171 | |
| 172 | // THEN |
| 173 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 174 | QCOMPARE(arbiter.dirtyNodes().front(), &shaderImage); |
| 175 | |
| 176 | arbiter.clear(); |
| 177 | } |
| 178 | |
| 179 | { |
| 180 | // WHEN |
| 181 | shaderImage.setTexture(&t); |
| 182 | QCoreApplication::processEvents(); |
| 183 | |
| 184 | // THEN |
| 185 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | void checkLayeredUpdate() |
| 191 | { |
| 192 | // GIVEN |
| 193 | TestArbiter arbiter; |
| 194 | Qt3DRender::QShaderImage shaderImage; |
| 195 | arbiter.setArbiterOnNode(&shaderImage); |
| 196 | |
| 197 | { |
| 198 | // WHEN |
| 199 | shaderImage.setLayered(true); |
| 200 | QCoreApplication::processEvents(); |
| 201 | |
| 202 | // THEN |
| 203 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 204 | QCOMPARE(arbiter.dirtyNodes().front(), &shaderImage); |
| 205 | |
| 206 | arbiter.clear(); |
| 207 | } |
| 208 | |
| 209 | { |
| 210 | // WHEN |
| 211 | shaderImage.setLayered(true); |
| 212 | QCoreApplication::processEvents(); |
| 213 | |
| 214 | // THEN |
| 215 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 216 | } |
| 217 | |
| 218 | } |
| 219 | |
| 220 | void checkMipLevelUpdate() |
| 221 | { |
| 222 | // GIVEN |
| 223 | TestArbiter arbiter; |
| 224 | Qt3DRender::QShaderImage shaderImage; |
| 225 | arbiter.setArbiterOnNode(&shaderImage); |
| 226 | |
| 227 | { |
| 228 | // WHEN |
| 229 | shaderImage.setMipLevel(5); |
| 230 | QCoreApplication::processEvents(); |
| 231 | |
| 232 | // THEN |
| 233 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 234 | QCOMPARE(arbiter.dirtyNodes().front(), &shaderImage); |
| 235 | |
| 236 | arbiter.clear(); |
| 237 | } |
| 238 | |
| 239 | { |
| 240 | // WHEN |
| 241 | shaderImage.setMipLevel(5); |
| 242 | QCoreApplication::processEvents(); |
| 243 | |
| 244 | // THEN |
| 245 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 246 | } |
| 247 | |
| 248 | } |
| 249 | |
| 250 | void checkLayerUpdate() |
| 251 | { |
| 252 | // GIVEN |
| 253 | TestArbiter arbiter; |
| 254 | Qt3DRender::QShaderImage shaderImage; |
| 255 | arbiter.setArbiterOnNode(&shaderImage); |
| 256 | |
| 257 | { |
| 258 | // WHEN |
| 259 | shaderImage.setLayer(8); |
| 260 | QCoreApplication::processEvents(); |
| 261 | |
| 262 | // THEN |
| 263 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 264 | QCOMPARE(arbiter.dirtyNodes().front(), &shaderImage); |
| 265 | |
| 266 | arbiter.clear(); |
| 267 | } |
| 268 | |
| 269 | { |
| 270 | // WHEN |
| 271 | shaderImage.setLayer(8); |
| 272 | QCoreApplication::processEvents(); |
| 273 | |
| 274 | // THEN |
| 275 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 276 | } |
| 277 | |
| 278 | } |
| 279 | |
| 280 | void checkAccessUpdate() |
| 281 | { |
| 282 | // GIVEN |
| 283 | TestArbiter arbiter; |
| 284 | Qt3DRender::QShaderImage shaderImage; |
| 285 | arbiter.setArbiterOnNode(&shaderImage); |
| 286 | |
| 287 | { |
| 288 | // WHEN |
| 289 | shaderImage.setAccess(Qt3DRender::QShaderImage::WriteOnly); |
| 290 | QCoreApplication::processEvents(); |
| 291 | |
| 292 | // THEN |
| 293 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 294 | QCOMPARE(arbiter.dirtyNodes().front(), &shaderImage); |
| 295 | |
| 296 | arbiter.clear(); |
| 297 | } |
| 298 | |
| 299 | { |
| 300 | // WHEN |
| 301 | shaderImage.setAccess(Qt3DRender::QShaderImage::WriteOnly); |
| 302 | QCoreApplication::processEvents(); |
| 303 | |
| 304 | // THEN |
| 305 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 306 | } |
| 307 | |
| 308 | } |
| 309 | |
| 310 | void checkFormatUpdate() |
| 311 | { |
| 312 | // GIVEN |
| 313 | TestArbiter arbiter; |
| 314 | Qt3DRender::QShaderImage shaderImage; |
| 315 | arbiter.setArbiterOnNode(&shaderImage); |
| 316 | |
| 317 | { |
| 318 | // WHEN |
| 319 | shaderImage.setFormat(Qt3DRender::QShaderImage::RG16F); |
| 320 | QCoreApplication::processEvents(); |
| 321 | |
| 322 | // THEN |
| 323 | QCOMPARE(arbiter.dirtyNodes().size(), 1); |
| 324 | QCOMPARE(arbiter.dirtyNodes().front(), &shaderImage); |
| 325 | |
| 326 | arbiter.clear(); |
| 327 | } |
| 328 | |
| 329 | { |
| 330 | // WHEN |
| 331 | shaderImage.setFormat(Qt3DRender::QShaderImage::RG16F); |
| 332 | QCoreApplication::processEvents(); |
| 333 | |
| 334 | // THEN |
| 335 | QCOMPARE(arbiter.dirtyNodes().size(), 0); |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | |
| 340 | void checkTextureBookeeping() |
| 341 | { |
| 342 | // GIVEN |
| 343 | Qt3DRender::QShaderImage shaderImage; |
| 344 | QSignalSpy spy(&shaderImage, SIGNAL(textureChanged(Qt3DRender::QAbstractTexture*))); |
| 345 | |
| 346 | { |
| 347 | // WHEN |
| 348 | Qt3DRender::QTexture2D tex; |
| 349 | shaderImage.setTexture(&tex); |
| 350 | |
| 351 | // THEN |
| 352 | QVERIFY(spy.isValid()); |
| 353 | QCOMPARE(shaderImage.texture(), &tex); |
| 354 | QCOMPARE(spy.size(), 1); |
| 355 | |
| 356 | // WHEN |
| 357 | spy.clear(); |
| 358 | } |
| 359 | // THEN |
| 360 | QCOMPARE(spy.size(), 1); |
| 361 | QVERIFY(shaderImage.texture() == nullptr); |
| 362 | } |
| 363 | |
| 364 | }; |
| 365 | |
| 366 | QTEST_MAIN(tst_QShaderImage) |
| 367 | |
| 368 | #include "tst_qshaderimage.moc" |