From a3d3aaeee0e3af69d7329beb1321eef3d04c79ad Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Mon, 24 Apr 2023 14:55:37 +0200 Subject: [PATCH] rhi: Add another resource update batch autotest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Exercise the validity of update batches, i.e. that one can safely commit it in later frames as well, as long as all related buffers and textures stay valid. Change-Id: Ia943e4b37141fe17253eeae32010e0f8d92c1583 Reviewed-by: Qt CI Bot Reviewed-by: Christian Strømme --- tests/auto/gui/rhi/qrhi/tst_qrhi.cpp | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/tests/auto/gui/rhi/qrhi/tst_qrhi.cpp b/tests/auto/gui/rhi/qrhi/tst_qrhi.cpp index b0f071bbd0..53ad597713 100644 --- a/tests/auto/gui/rhi/qrhi/tst_qrhi.cpp +++ b/tests/auto/gui/rhi/qrhi/tst_qrhi.cpp @@ -78,6 +78,8 @@ private slots: void resourceUpdateBatchTextureRawDataStride(); void resourceUpdateBatchLotsOfResources_data(); void resourceUpdateBatchLotsOfResources(); + void resourceUpdateBatchBetweenFrames_data(); + void resourceUpdateBatchBetweenFrames(); void invalidPipeline_data(); void invalidPipeline(); void srbLayoutCompatibility_data(); @@ -1532,6 +1534,86 @@ void tst_QRhi::resourceUpdateBatchLotsOfResources() submitResourceUpdates(rhi.data(), b); } +void tst_QRhi::resourceUpdateBatchBetweenFrames_data() +{ + rhiTestData(); +} + +void tst_QRhi::resourceUpdateBatchBetweenFrames() +{ + QFETCH(QRhi::Implementation, impl); + QFETCH(QRhiInitParams *, initParams); + + QScopedPointer rhi(QRhi::create(impl, initParams, QRhi::Flags(), nullptr)); + if (!rhi) + QSKIP("QRhi could not be created, skipping testing resource updates"); + + QImage image(128, 128, QImage::Format_RGBA8888_Premultiplied); + image.fill(Qt::red); + static const float bufferData[64] = {}; + + QRhiCommandBuffer *cb = nullptr; + QRhi::FrameOpResult result = rhi->beginOffscreenFrame(&cb); + QVERIFY(result == QRhi::FrameOpSuccess); + QVERIFY(cb); + + static const int TEXTURE_COUNT = 123; + static const int BUFFER_COUNT = 456; + + QRhiResourceUpdateBatch *u = rhi->nextResourceUpdateBatch(); + std::vector> textures; + std::vector> buffers; + + for (int i = 0; i < TEXTURE_COUNT; ++i) { + std::unique_ptr texture(rhi->newTexture(QRhiTexture::RGBA8, + image.size(), + 1, + QRhiTexture::UsedAsTransferSource)); + QVERIFY(texture->create()); + u->uploadTexture(texture.get(), image); + textures.push_back(std::move(texture)); + } + + for (int i = 0; i < BUFFER_COUNT; ++i) { + std::unique_ptr buffer(rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, 256)); + QVERIFY(buffer->create()); + u->uploadStaticBuffer(buffer.get(), bufferData); + buffers.push_back(std::move(buffer)); + } + + rhi->endOffscreenFrame(); + cb = nullptr; + + // 'u' stays valid, commit it in another frame + + result = rhi->beginOffscreenFrame(&cb); + QVERIFY(result == QRhi::FrameOpSuccess); + QVERIFY(cb); + + cb->resourceUpdate(u); // this should work + + rhi->endOffscreenFrame(); + + u = rhi->nextResourceUpdateBatch(); + QRhiReadbackResult readResult; + bool readCompleted = false; + readResult.completed = [&readCompleted] { readCompleted = true; }; + u->readBackTexture(textures[5].get(), &readResult); + + QVERIFY(submitResourceUpdates(rhi.data(), u)); + QVERIFY(readCompleted); + QCOMPARE(readResult.format, QRhiTexture::RGBA8); + QCOMPARE(readResult.pixelSize, image.size()); + + QImage wrapperImage(reinterpret_cast(readResult.data.constData()), + readResult.pixelSize.width(), readResult.pixelSize.height(), + QImage::Format_RGBA8888_Premultiplied); + for (int y = 0; y < image.height(); ++y) { + for (int x = 0; x < image.width(); ++x) + QCOMPARE(wrapperImage.pixel(x, y), qRgba(255, 0, 0, 255)); + } +} + static QShader loadShader(const char *name) { QFile f(QString::fromUtf8(name));