Debian Codegraph @

tests/auto/render/gltexture/tst_gltexture.cpp

raw symbols SCIP
1 // Copyright (C) 2019 Klaralvdalens Datakonsult AB (KDAB).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 // TODO Remove in Qt6
5 #include <QtCore/qcompilerdetection.h>
6 QT_WARNING_DISABLE_DEPRECATED
7
8 #include <QtTest/QTest>
9 #include <Qt3DRender/private/gltexture_p.h>
10 #include <Qt3DRender/qabstracttexture.h>
11 #include <Qt3DRender/qtexturegenerator.h>
12 #include <Qt3DRender/private/qtexture_p.h>
13 #include <Qt3DRender/private/qtextureimage_p.h>
14 #include <QSurfaceFormat>
15 #include <QWindow>
16 #include <QUrl>
17 #include <QOpenGLContext>
18 #include <cstring>
19
20 using namespace Qt3DRender;
21 using namespace Qt3DRender::Render;
22
23 namespace {
24
25 QTextureDataUpdate generateDataUpdate()
26 {
27 QByteArray rawPixelData;
28 rawPixelData.resize(512 * 4 * sizeof(uchar));
29 uchar *pixels = reinterpret_cast<uchar *>(rawPixelData.data());
30 std::memset(pixels, 128U, 512 * 4 * sizeof(uchar));
31
32 // QTextureImageData hold our raw content and size of subcontent
33 Qt3DRender::QTextureImageDataPtr imageData = Qt3DRender::QTextureImageDataPtr::create();
34 imageData->setWidth(512);
35 imageData->setHeight(1);
36 imageData->setMipLevels(1);
37 imageData->setLayers(1);
38 // We upload the 4 components of an RGBA pixel as 4 uchar
39 imageData->setPixelFormat(QOpenGLTexture::RGBA);
40 imageData->setPixelType(QOpenGLTexture::UInt8);
41 imageData->setData(rawPixelData, 4, false);
42
43 Qt3DRender::QTextureDataUpdate update;
44 update.setX(0);
45 update.setY(10);
46 update.setMipLevel(0);
47 update.setData(imageData);
48
49 return update;
50 }
51
52 } // anonymous
53
54 class FakeTexture2DGenerator : public QTextureGenerator
55 {
56 public:
57 QT3D_FUNCTOR(FakeTexture2DGenerator)
58
59 QTextureDataPtr operator ()() override
60 {
61 QTextureDataPtr generatedData = QTextureDataPtr::create();
62 generatedData->setTarget(QAbstractTexture::Target2D);
63 #ifndef QT_OPENGL_ES_2
64 generatedData->setFormat(QAbstractTexture::RGBA8_UNorm);
65 #else
66 generatedData->setFormat(QAbstractTexture::RGBAFormat);
67 #endif
68 generatedData->setWidth(512);
69 generatedData->setHeight(512);
70 generatedData->setDepth(1);
71 generatedData->setLayers(1);
72 return generatedData;
73 }
74
75 bool operator ==(const QTextureGenerator &) const override
76 {
77 return true;
78 }
79 };
80
81 class FakeTexture2DGeneratorAutomatic : public QTextureGenerator
82 {
83 public:
84 QT3D_FUNCTOR(FakeTexture2DGenerator)
85
86 QTextureDataPtr operator ()() override
87 {
88 QTextureDataPtr generatedData = QTextureDataPtr::create();
89 generatedData->setTarget(QAbstractTexture::TargetAutomatic);
90 #ifndef QT_OPENGL_ES_2
91 generatedData->setFormat(QAbstractTexture::RGBA8_UNorm);
92 #else
93 generatedData->setFormat(QAbstractTexture::RGBAFormat);
94 #endif
95 generatedData->setWidth(512);
96 generatedData->setHeight(512);
97 generatedData->setDepth(1);
98 generatedData->setLayers(1);
99 return generatedData;
100 }
101
102 bool operator ==(const QTextureGenerator &) const override
103 {
104 return true;
105 }
106 };
107
108 class FakeDownloadTexture2DGenerator : public QTextureGenerator
109 {
110 public:
111 QT3D_FUNCTOR(FakeDownloadTexture2DGenerator)
112
113 QTextureDataPtr operator ()() override
114 {
115 if (m_hasBeenDownloaded) {
116 generatedData->setTarget(QAbstractTexture::Target2D);
117 generatedData->setFormat(QAbstractTexture::RGBA8_UNorm);
118 generatedData->setWidth(512);
119 generatedData->setHeight(512);
120 generatedData->setDepth(1);
121 generatedData->setLayers(1);
122 } else {
123 generatedData->setTarget(QAbstractTexture::TargetAutomatic);
124 }
125 return generatedData;
126 }
127
128 bool operator ==(const QTextureGenerator &) const override
129 {
130 return true;
131 }
132
133 void setHasBeenDownloaded(bool downloaded)
134 {
135 m_hasBeenDownloaded = downloaded;
136 }
137
138 private:
139 QTextureDataPtr generatedData = QTextureDataPtr::create();
140 bool m_hasBeenDownloaded = false;
141 };
142
143
144
145 class tst_GLTexture : public QObject
146 {
147 Q_OBJECT
148
149 private:
150 QScopedPointer<QWindow> m_window;
151 QOpenGLContext m_glContext;
152
153 private Q_SLOTS:
154
155 void initTestCase()
156 {
157 qRegisterMetaType<TextureProperties>("Qt3DRender::Render::TexturePropertiers");
158 }
159
160 void init()
161 {
162 m_window.reset(new QWindow);
163 m_window->setSurfaceType(QWindow::OpenGLSurface);
164 m_window->setGeometry(0, 0, 10, 10);
165 m_window->create();
166
167 if (!m_glContext.create()) {
168 qWarning() << "Failed to create OpenGL context";
169 return;
170 }
171
172 if (!m_glContext.makeCurrent(m_window.data())) {
173 qWarning() << "Failed to make OpenGL context current";
174 return;
175 }
176 }
177
178 void cleanup()
179 {
180 m_glContext.doneCurrent();
181 }
182
183 void checkDontCreateInternalTextureIfNothingSpecified()
184 {
185 // GIVEN
186 GLTexture texture;
187
188 // THEN
189 QVERIFY(texture.getGLTexture() == nullptr);
190 QVERIFY(!texture.isDirty());
191 QVERIFY(!texture.hasTextureData());
192 QVERIFY(!texture.hasImagesData());
193
194 // WHEN
195 texture.createOrUpdateGLTexture();
196
197 // THEN -> shouldn't have created a texture since format and target
198 // were not specified
199 QVERIFY(texture.getGLTexture() == nullptr);
200
201 // Cleanup
202 texture.destroy();
203 }
204
205 void checkCreatesInternalTexture()
206 {
207 // GIVEN
208 GLTexture texture;
209
210 // WHEN
211 TextureProperties props;
212 props.target = QAbstractTexture::Target2D;
213 #ifndef QT_OPENGL_ES_2
214 props.format = QAbstractTexture::RGBA8_SNorm;
215 #else
216 props.format = QAbstractTexture::RGBAFormat;
217 #endif
218 texture.setProperties(props);
219
220 // THEN
221 QCOMPARE(texture.dirtyFlags(), GLTexture::Properties);
222
223 // WHEN
224 texture.createOrUpdateGLTexture();
225
226 // THEN
227 QOpenGLTexture *internalTexture = texture.getGLTexture();
228 QCOMPARE(internalTexture->target(), QOpenGLTexture::Target2D);
229 QCOMPARE(internalTexture->width(), 1);
230 QCOMPARE(internalTexture->height(), 1);
231 QVERIFY(!texture.isDirty());
232 QVERIFY(!texture.hasTextureData());
233 QVERIFY(!texture.hasImagesData());
234
235 // Cleanup
236 texture.destroy();
237 }
238
239 void checkShouldnotCreateTextureIfTargetIsAutomaticOrFormatUnspecified()
240 {
241 // GIVEN
242 GLTexture texture;
243 TextureProperties props;
244
245 props.target = QAbstractTexture::TargetAutomatic;
246 #ifndef QT_OPENGL_ES_2
247 props.format = QAbstractTexture::RGBA8_SNorm;
248 #else
249 props.format = QAbstractTexture::RGBAFormat;
250 #endif
251 // WHEN
252 texture.setProperties(props);
253
254 // THEN
255 QCOMPARE(texture.dirtyFlags(), GLTexture::Properties);
256
257 // WHEN
258 texture.createOrUpdateGLTexture();
259
260 // THEN -> shouldn't have created a texture since target is automatic
261 QVERIFY(texture.getGLTexture() == nullptr);
262 QCOMPARE(texture.dirtyFlags(), GLTexture::Properties);
263
264 // WHEN
265 props.target = QAbstractTexture::Target2D;
266 props.format = QAbstractTexture::NoFormat;
267 texture.setProperties(props);
268 texture.createOrUpdateGLTexture();
269
270 // THEN -> shouldn't have created a texture since format is not specified
271 QVERIFY(texture.getGLTexture() == nullptr);
272 QCOMPARE(texture.dirtyFlags(), GLTexture::Properties);
273
274 // WHEN
275 props.target = QAbstractTexture::Target2D;
276 props.format = QAbstractTexture::Automatic;
277 texture.setProperties(props);
278 texture.createOrUpdateGLTexture();
279
280 // THEN -> shouldn't have created a texture since format is not specified
281 QVERIFY(texture.getGLTexture() == nullptr);
282 QCOMPARE(texture.dirtyFlags(), GLTexture::Properties);
283
284 // WHEN
285 props.target = QAbstractTexture::Target2D;
286 #ifndef QT_OPENGL_ES_2
287 props.format = QAbstractTexture::RGBA8_SNorm;
288 #else
289 props.format = QAbstractTexture::RGBAFormat;
290 #endif
291 texture.setProperties(props);
292 texture.createOrUpdateGLTexture();
293
294 // THEN
295 QOpenGLTexture *internalTexture = texture.getGLTexture();
296 QCOMPARE(internalTexture->target(), QOpenGLTexture::Target2D);
297 QCOMPARE(internalTexture->width(), 1);
298 QCOMPARE(internalTexture->height(), 1);
299 QVERIFY(!texture.isDirty());
300 QVERIFY(!texture.hasTextureData());
301 QVERIFY(!texture.hasImagesData());
302
303 // Cleanup
304 texture.destroy();
305 }
306
307 void checkCreateTextureWithDataGenerator()
308 {
309 // GIVEN
310 GLTexture texture;
311 texture.createOrUpdateGLTexture();
312
313 // THEN
314 QOpenGLTexture *internalTex = texture.getGLTexture();
315 QVERIFY(internalTex == nullptr);
316 QVERIFY(!texture.isDirty());
317 QVERIFY(!texture.hasTextureData());
318 QVERIFY(!texture.hasImagesData());
319
320 // WHEN
321 auto genTex2D = QSharedPointer<FakeTexture2DGenerator>::create();
322 texture.setGenerator(genTex2D);
323 texture.createOrUpdateGLTexture();
324
325 // THEN
326 QVERIFY(texture.getGLTexture() != nullptr);
327 QVERIFY(texture.getGLTexture() != internalTex);
328
329 internalTex = texture.getGLTexture();
330 QCOMPARE(internalTex->target(), QOpenGLTexture::Target2D);
331 QCOMPARE(internalTex->width(), 512);
332 QCOMPARE(internalTex->height(), 512);
333
334 QVERIFY(texture.hasTextureData());
335 QVERIFY(!texture.isDirty());
336 QVERIFY(!texture.hasImagesData());
337
338 // Cleanup
339 texture.destroy();
340 }
341
342 void checkHandlesDelayedReceptionOfDataGenerator()
343 {
344 // GIVEN
345 GLTexture texture;
346
347 // WHEN
348 auto genDownloadTex2D = QSharedPointer<FakeDownloadTexture2DGenerator>::create();
349 texture.setGenerator(genDownloadTex2D);
350
351 // THEN
352 QVERIFY(!texture.hasTextureData());
353 QVERIFY(texture.isDirty());
354
355 // WHEN
356 texture.createOrUpdateGLTexture();
357
358 // THEN -> shouldn't have created internal texture since target is automatic
359 // but generator should have run to return a QTextureDataPtr
360 QVERIFY(texture.getGLTexture() == nullptr);
361 QVERIFY(texture.isDirty());
362 QVERIFY(!texture.hasTextureData());
363 QVERIFY(!texture.hasImagesData());
364
365 // WHEN
366 genDownloadTex2D->setHasBeenDownloaded(true);
367 texture.setGenerator(genDownloadTex2D);
368
369 // THEN
370 QVERIFY(texture.getGLTexture() == nullptr);
371 QVERIFY(!texture.hasTextureData());
372 QVERIFY(texture.isDirty());
373
374 // WHEN
375 texture.createOrUpdateGLTexture();
376
377 // THEN -> Should have created the texture has target shouldn't be automatic
378 QVERIFY(texture.hasTextureData());
379 QVERIFY(!texture.isDirty());
380 QOpenGLTexture *internalTex = texture.getGLTexture();
381 QVERIFY(internalTex != nullptr);
382 QCOMPARE(internalTex->target(), QOpenGLTexture::Target2D);
383 QCOMPARE(internalTex->width(), 512);
384 QCOMPARE(internalTex->height(), 512);
385
386 // Cleanup
387 texture.destroy();
388 }
389
390 void checkTakesTargetFromGeneratorIfTargetNotSpecified()
391 {
392 // GIVEN
393 GLTexture texture;
394 TextureProperties props;
395 props.target = QAbstractTexture::TargetAutomatic;
396
397 // WHEN
398 auto genDownloadTex2D = QSharedPointer<FakeTexture2DGenerator>::create();
399 texture.setGenerator(genDownloadTex2D);
400 texture.setProperties(props);
401
402 // THEN
403 QVERIFY(!texture.hasTextureData());
404 QVERIFY(texture.isDirty());
405
406 // WHEN
407 texture.createOrUpdateGLTexture();
408
409 // THEN -> Should have created the texture has target shouldn't be automatic
410 QVERIFY(texture.hasTextureData());
411 QVERIFY(!texture.isDirty());
412 QOpenGLTexture *internalTex = texture.getGLTexture();
413 QVERIFY(internalTex != nullptr);
414 QCOMPARE(internalTex->target(), QOpenGLTexture::Target2D);
415 QCOMPARE(internalTex->width(), 512);
416 QCOMPARE(internalTex->height(), 512);
417
418 // Cleanup
419 texture.destroy();
420 }
421
422 void checkKeepsSetTargetIfGeneratorTargetNotSpecified()
423 {
424 // GIVEN
425 GLTexture texture;
426 TextureProperties props;
427 props.target = QAbstractTexture::Target2D;
428
429 // WHEN
430 auto genDownloadTex2D = QSharedPointer<FakeTexture2DGeneratorAutomatic>::create();
431 texture.setGenerator(genDownloadTex2D);
432 texture.setProperties(props);
433
434 // THEN
435 QVERIFY(!texture.hasTextureData());
436 QVERIFY(texture.isDirty());
437
438 // WHEN
439 texture.createOrUpdateGLTexture();
440
441 // THEN -> Should have created the texture has target shouldn't be automatic
442 QVERIFY(texture.hasTextureData());
443 QVERIFY(!texture.isDirty());
444 QOpenGLTexture *internalTex = texture.getGLTexture();
445 QVERIFY(internalTex != nullptr);
446 QCOMPARE(internalTex->target(), QOpenGLTexture::Target2D);
447 QCOMPARE(internalTex->width(), 512);
448 QCOMPARE(internalTex->height(), 512);
449
450 // Cleanup
451 texture.destroy();
452 }
453
454 void checkDontCreateTextureIfGeneratorAndTargetAreAutomatic()
455 {
456 // GIVEN
457 GLTexture texture;
458 TextureProperties props;
459 props.target = QAbstractTexture::TargetAutomatic;
460
461 // WHEN
462 auto genDownloadTex2D = QSharedPointer<FakeTexture2DGeneratorAutomatic>::create();
463 texture.setGenerator(genDownloadTex2D);
464 texture.setProperties(props);
465
466 // THEN
467 QVERIFY(!texture.hasTextureData());
468 QVERIFY(texture.isDirty());
469
470 // WHEN
471 texture.createOrUpdateGLTexture();
472
473 // THEN -> Should have created the texture has target shouldn't be automatic
474 QVERIFY(!texture.hasTextureData());
475 QVERIFY(texture.isDirty());
476 QVERIFY(texture.getGLTexture() == nullptr);
477
478 // Cleanup
479 texture.destroy();
480 }
481
482 void checkRecreatesTextureOnPropertyChanged()
483 {
484 // GIVEN
485 GLTexture texture;
486
487 // WHEN
488 TextureProperties props;
489 props.target = QAbstractTexture::Target2D;
490 #ifndef QT_OPENGL_ES_2
491 props.format = QAbstractTexture::RGBA8_SNorm;
492 #else
493 props.format = QAbstractTexture::RGBAFormat;
494 #endif
495 texture.setProperties(props);
496
497 // THEN
498 QCOMPARE(texture.dirtyFlags(), GLTexture::Properties);
499
500 // WHEN
501 texture.createOrUpdateGLTexture();
502
503 // THEN
504 QOpenGLTexture *internalTexture = texture.getGLTexture();
505 QCOMPARE(internalTexture->target(), QOpenGLTexture::Target2D);
506 QCOMPARE(internalTexture->width(), 1);
507 QCOMPARE(internalTexture->height(), 1);
508 QVERIFY(!texture.isDirty());
509 QVERIFY(!texture.hasTextureData());
510 QVERIFY(!texture.hasImagesData());
511
512 // WHEN
513 props.width = 256;
514 props.height = 256;
515 texture.setProperties(props);
516 texture.createOrUpdateGLTexture();
517
518 // THEN
519 QOpenGLTexture *internalTexture2 = texture.getGLTexture();
520 QCOMPARE(internalTexture2->target(), QOpenGLTexture::Target2D);
521 QCOMPARE(internalTexture2->width(), 256);
522 QCOMPARE(internalTexture2->height(), 256);
523 QVERIFY(!texture.isDirty());
524 QVERIFY(!texture.hasTextureData());
525 QVERIFY(!texture.hasImagesData());
526 QVERIFY(texture.wasTextureRecreated());
527
528 // Technically pointer should be different but that's up to the system
529 // so can't really be tested
530
531 // Cleanup
532 texture.destroy();
533 }
534
535 void checkDoesntRecreateTextureOnParameterChanged()
536 {
537 // GIVEN
538 GLTexture texture;
539
540 // WHEN
541 TextureProperties props;
542 props.target = QAbstractTexture::Target2D;
543 #ifndef QT_OPENGL_ES_2
544 props.format = QAbstractTexture::RGBA8_SNorm;
545 #else
546 props.format = QAbstractTexture::RGBAFormat;
547 #endif
548 texture.setProperties(props);
549
550 TextureParameters params;
551 params.magnificationFilter = QAbstractTexture::Linear;
552 params.minificationFilter = QAbstractTexture::Linear;
553 texture.setParameters(params);
554
555 // THEN
556 QCOMPARE(texture.dirtyFlags(), GLTexture::Properties|GLTexture::Parameters);
557
558 // WHEN
559 texture.createOrUpdateGLTexture();
560
561 // THEN
562 QOpenGLTexture *internalTexture = texture.getGLTexture();
563 QCOMPARE(internalTexture->target(), QOpenGLTexture::Target2D);
564 QCOMPARE(internalTexture->width(), 1);
565 QCOMPARE(internalTexture->height(), 1);
566 QCOMPARE(internalTexture->minificationFilter(), QOpenGLTexture::Linear);
567 QCOMPARE(internalTexture->magnificationFilter(), QOpenGLTexture::Linear);
568 QVERIFY(!texture.isDirty());
569 QVERIFY(!texture.hasTextureData());
570 QVERIFY(!texture.hasImagesData());
571
572 // WHEN
573 params.magnificationFilter = QAbstractTexture::Nearest;
574 params.minificationFilter = QAbstractTexture::Nearest;
575 texture.setParameters(params);
576
577 // THEN
578 QCOMPARE(texture.dirtyFlags(),GLTexture::Parameters);
579
580 // WHEN
581 texture.createOrUpdateGLTexture();
582
583 // THEN
584 QOpenGLTexture *internalTexture2 = texture.getGLTexture();
585 QCOMPARE(internalTexture->target(), QOpenGLTexture::Target2D);
586 QCOMPARE(internalTexture->width(), 1);
587 QCOMPARE(internalTexture->height(), 1);
588 QCOMPARE(internalTexture->minificationFilter(), QOpenGLTexture::Nearest);
589 QCOMPARE(internalTexture->magnificationFilter(), QOpenGLTexture::Nearest);
590 QVERIFY(!texture.isDirty());
591 QVERIFY(!texture.hasTextureData());
592 QVERIFY(!texture.hasImagesData());
593 QCOMPARE(internalTexture, internalTexture2);
594 QVERIFY(!texture.wasTextureRecreated());
595
596 // Cleanup
597 texture.destroy();
598 }
599
600 void checkDestroy()
601 {
602 // GIVEN
603 GLTexture texture;
604 TextureProperties props;
605 props.target = QAbstractTexture::Target2D;
606 #ifndef QT_OPENGL_ES_2
607 props.format = QAbstractTexture::RGBA8_SNorm;
608 #else
609 props.format = QAbstractTexture::RGBAFormat;
610 #endif
611 texture.setProperties(props);
612
613 // WHEN
614 texture.createOrUpdateGLTexture();
615 texture.setGenerator(QSharedPointer<FakeTexture2DGenerator>::create());
616 QList<GLTexture::Image> images;
617 QImageTextureDataFunctorPtr gen = QImageTextureDataFunctorPtr::create(QUrl("qrc:/image.jpg"), true);
618 images.push_back({gen, 1, 0, QAbstractTexture::CubeMapPositiveX});
619 texture.setImages(images);
620
621 // THEN
622 QVERIFY(texture.getGLTexture() != nullptr);
623
624 // WHEN
625 texture.destroy();
626
627 // THEN
628 QVERIFY(texture.getGLTexture() == nullptr);
629 QVERIFY(!texture.isDirty());
630 QCOMPARE(texture.properties(), TextureProperties());
631 QCOMPARE(texture.parameters(), TextureParameters());
632 QCOMPARE(texture.images().size(), 0);
633 QVERIFY(!texture.hasImagesData());
634 QVERIFY(!texture.hasTextureData());
635 QVERIFY(!texture.dataGenerator());
636 }
637
638 void checkRecreateTextureOnNewImageDataChanged()
639 {
640 // GIVEN
641 GLTexture texture;
642 TextureProperties props;
643 props.target = QAbstractTexture::Target2D;
644 #ifndef QT_OPENGL_ES_2
645 props.format = QAbstractTexture::RGBA8_UNorm;
646 #else
647 props.format = QAbstractTexture::RGBAFormat;
648 #endif
649 props.width = 1;
650 props.height = 1;
651 texture.setProperties(props);
652
653 // WHEN
654 texture.createOrUpdateGLTexture();
655
656 // THEN
657 QVERIFY(texture.getGLTexture() != nullptr);
658 QVERIFY(!texture.isDirty());
659
660 // WHEN
661 texture.setGenerator(QSharedPointer<FakeTexture2DGenerator>::create());
662 QList<GLTexture::Image> images;
663 // Test image has a size of 512/512
664 QImageTextureDataFunctorPtr gen = QImageTextureDataFunctorPtr::create(QUrl("qrc:/image.jpg"), true);
665 images.push_back({gen, 1, 0, QAbstractTexture::CubeMapPositiveX});
666 texture.setImages(images);
667
668 // THEN
669 QCOMPARE(texture.dirtyFlags(), GLTexture::TextureImageData|GLTexture::TextureData);
670
671 // WHEN
672 texture.createOrUpdateGLTexture();
673
674 // THEN
675 QOpenGLTexture *internalTexture = texture.getGLTexture();
676 QCOMPARE(internalTexture->target(), QOpenGLTexture::Target2D);
677 QCOMPARE(internalTexture->width(), 512);
678 QCOMPARE(internalTexture->height(), 512);
679 QVERIFY(!texture.isDirty());
680 QVERIFY(texture.hasTextureData());
681 QVERIFY(texture.wasTextureRecreated());
682
683 // Cleanup
684 texture.destroy();
685 }
686
687 void checkDoesntRecreateTextureOnNewTextureDataUpdate()
688 {
689 // GIVEN
690 GLTexture texture;
691 TextureProperties props;
692 props.target = QAbstractTexture::Target2D;
693 #ifndef QT_OPENGL_ES_2
694 props.format = QAbstractTexture::RGBA8_UNorm;
695 #else
696 props.format = QAbstractTexture::RGBAFormat;
697 #endif
698 props.width = 512;
699 props.height = 512;
700 texture.setProperties(props);
701
702 // WHEN
703 texture.createOrUpdateGLTexture();
704
705 // THEN
706 QVERIFY(texture.getGLTexture() != nullptr);
707 QVERIFY(!texture.isDirty());
708
709 // WHEN
710 QByteArray rawPixelData;
711 rawPixelData.resize(512 * 4 * sizeof(uchar));
712 uchar *pixels = reinterpret_cast<uchar *>(rawPixelData.data());
713 std::memset(pixels, 128U, 512 * 4 * sizeof(uchar));
714
715 // QTextureImageData hold our raw content and size of subcontent
716 Qt3DRender::QTextureImageDataPtr imageData = Qt3DRender::QTextureImageDataPtr::create();
717 imageData->setWidth(512);
718 imageData->setHeight(1);
719 imageData->setMipLevels(1);
720 imageData->setLayers(1);
721 // We upload the 4 components of an RGBA pixel as 4 uchar
722 imageData->setPixelFormat(QOpenGLTexture::RGBA);
723 imageData->setPixelType(QOpenGLTexture::UInt8);
724 imageData->setData(rawPixelData, 4, false);
725
726 Qt3DRender::QTextureDataUpdate update;
727 update.setX(0);
728 update.setY(10);
729 update.setMipLevel(0);
730 update.setData(imageData);
731
732 texture.addTextureDataUpdates({update});
733
734 // THEN
735 QCOMPARE(texture.dirtyFlags(), GLTexture::TextureData);
736
737 // WHEN
738 texture.createOrUpdateGLTexture();
739 QVERIFY(texture.getGLTexture() != nullptr);
740 QVERIFY(!texture.isDirty());
741 QVERIFY(!texture.wasTextureRecreated());
742
743 // Cleanup
744 texture.destroy();
745 }
746
747 void checkDoesntCrashWhenSendingIncompatibleTextureDataUpdate_data()
748 {
749 QTest::addColumn<TextureProperties>("properties");
750 QTest::addColumn<Qt3DRender::QTextureDataUpdate>("update");
751
752 QTextureDataUpdate update;
753 TextureProperties props;
754 props.target = QAbstractTexture::Target2D;
755 #ifndef QT_OPENGL_ES_2
756 props.format = QAbstractTexture::RGBA8_UNorm;
757 #else
758 props.format = QAbstractTexture::RGBAFormat;
759 #endif
760 props.width = 512;
761 props.height = 512;
762 QTest::newRow("empty") << props << update;
763
764 update = generateDataUpdate();
765 update.setMipLevel(4);
766 QTest::newRow("invalid_mip_level") << props << update;
767
768 update.setX(512);
769 QTest::newRow("out_of_bound_x") << props << update;
770
771 update.setX(0);
772 update.setY(512);
773 QTest::newRow("out_of_bound_y") << props << update;
774
775 #ifndef QT_OPENGL_ES_2
776 update.setX(0);
777 update.setY(0);
778 update.setZ(512);
779 props.target = QAbstractTexture::Target3D;
780 props.depth = 512;
781 QTest::newRow("out_of_bound_z") << props << update;
782 #endif
783 update.setY(0);
784 update.setZ(0);
785 update.setX(256);
786 update.data()->setWidth(512);
787 QTest::newRow("out_of_bound_width") << props << update;
788
789 update.setX(0);
790 update.setY(256);
791 update.setZ(0);
792 update.data()->setHeight(512);
793 QTest::newRow("out_of_bound_height") << props << update;
794
795 #ifndef QT_OPENGL_ES_2
796 update.setX(0);
797 update.setY(0);
798 update.setZ(256);
799 update.data()->setDepth(512);
800 QTest::newRow("out_of_bound_depth") << props << update;
801
802 update.setX(0);
803 update.setY(0);
804 update.setZ(0);
805 update.data()->setDepth(0);
806 update.setLayer(8);
807 props.target = QAbstractTexture::Target2DArray;
808 props.layers = 4;
809 QTest::newRow("out_of_bound_layer") << props << update;
810 #endif
811 }
812
813
814 void checkDoesntCrashWhenSendingIncompatibleTextureDataUpdate()
815 {
816 // GIVEN
817 GLTexture texture;
818 QFETCH(TextureProperties, properties);
819 QFETCH(QTextureDataUpdate, update);
820 texture.setProperties(properties);
821
822 // WHEN
823 texture.createOrUpdateGLTexture();
824
825 // THEN
826 QVERIFY(texture.getGLTexture() != nullptr);
827 QVERIFY(!texture.isDirty());
828
829 // WHEN
830 texture.addTextureDataUpdates({update});
831
832 // THEN
833 QCOMPARE(texture.dirtyFlags(), GLTexture::TextureData);
834
835 // WHEN
836 texture.createOrUpdateGLTexture();
837
838 // THEN -> shouldn't crash
839 QVERIFY(texture.getGLTexture() != nullptr);
840 QVERIFY(!texture.isDirty());
841 QVERIFY(!texture.wasTextureRecreated());
842
843 // Cleanup
844 texture.destroy();
845 }
846
847 void checkPropertiesAfterLoadTextureDataFromImages()
848 {
849 // GIVEN
850 GLTexture texture;
851 TextureProperties props;
852 props.target = QAbstractTexture::TargetCubeMap;
853 props.format = QAbstractTexture::Automatic;
854 props.width = 1;
855 props.height = 1;
856 texture.setProperties(props);
857 QList<GLTexture::Image> images;
858 // test a image texture data generator whose url is invalid
859 QImageTextureDataFunctorPtr gen = QImageTextureDataFunctorPtr::create(QUrl(), true);
860 images.push_back({gen, 0, 0, QAbstractTexture::CubeMapPositiveX});
861 texture.setImages(images);
862
863 // WHEN
864 texture.createOrUpdateGLTexture();
865
866 // THEN
867 QCOMPARE(texture.properties().format, QAbstractTexture::Automatic);
868
869 // WHEN
870 // test a image texture data generator whose url is valid
871 gen = QImageTextureDataFunctorPtr::create(QUrl("qrc:/image.jpg"), true);
872 images.clear();
873 images.push_back({gen, 0, 0, QAbstractTexture::CubeMapPositiveX});
874 texture.setImages(images);
875 texture.createOrUpdateGLTexture();
876
877 // THEN
878 QVERIFY(texture.properties().format != QAbstractTexture::Automatic);
879 QVERIFY(texture.properties().format != QAbstractTexture::NoFormat);
880
881 // Cleanup
882 texture.destroy();
883 }
884 };
885
886 QTEST_MAIN(tst_GLTexture);
887
888 #include "tst_gltexture.moc"