From d9675966287521fdc158430f5bce5370cbb80cce Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 7 Sep 2021 15:53:59 +0200 Subject: rhi: Improve srb layout serialization helpers Be idiomatic and return the output iterator one past the last element. Otherwise passing in a plain pointer (as exercised by the autotest now) fails to function because we write over the same 4 elements again and again for each binding. Change-Id: If74463fa5140ffa2b1d5be97b71868848ad46614 Reviewed-by: Andy Nichols (cherry picked from commit ce9d0491f2a3836991c3c44fa70c32f1f606ab28) Reviewed-by: Qt Cherry-pick Bot --- src/gui/rhi/qrhi.cpp | 2 +- src/gui/rhi/qrhi_p.h | 5 +++-- tests/auto/gui/rhi/qrhi/tst_qrhi.cpp | 9 +++++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/gui/rhi/qrhi.cpp b/src/gui/rhi/qrhi.cpp index 420e70cecf..64168b4dc6 100644 --- a/src/gui/rhi/qrhi.cpp +++ b/src/gui/rhi/qrhi.cpp @@ -3058,7 +3058,7 @@ void QRhiImplementation::updateLayoutDesc(QRhiShaderResourceBindings *srb) const QRhiShaderResourceBinding::Data *d = b.data(); srb->m_layoutDescHash ^= uint(d->binding) ^ uint(d->stage) ^ uint(d->type) ^ uint(d->type == QRhiShaderResourceBinding::SampledTexture ? d->u.stex.count : 1); - d->serialize(layoutDescAppender); + layoutDescAppender = d->serialize(layoutDescAppender); } } diff --git a/src/gui/rhi/qrhi_p.h b/src/gui/rhi/qrhi_p.h index 81b00cd07f..527d72e486 100644 --- a/src/gui/rhi/qrhi_p.h +++ b/src/gui/rhi/qrhi_p.h @@ -413,13 +413,14 @@ public: } u; template - void serialize(Output dst) const + Output serialize(Output dst) const { // must write out exactly LAYOUT_DESC_ENTRIES_PER_BINDING elements here *dst++ = quint32(binding); *dst++ = quint32(stage); *dst++ = quint32(type); *dst++ = quint32(type == QRhiShaderResourceBinding::SampledTexture ? u.stex.count : 1); + return dst; } }; @@ -434,7 +435,7 @@ public: Output dst) { while (first != last) { - first->data()->serialize(dst); + dst = first->data()->serialize(dst); ++first; } } diff --git a/tests/auto/gui/rhi/qrhi/tst_qrhi.cpp b/tests/auto/gui/rhi/qrhi/tst_qrhi.cpp index 603acc03de..8398dac3eb 100644 --- a/tests/auto/gui/rhi/qrhi/tst_qrhi.cpp +++ b/tests/auto/gui/rhi/qrhi/tst_qrhi.cpp @@ -3273,6 +3273,7 @@ void tst_QRhi::srbLayoutCompatibility() QVERIFY(srb2->isLayoutCompatible(srb1.data())); QCOMPARE(srb1->serializedLayoutDescription(), srb2->serializedLayoutDescription()); + QVERIFY(srb1->serializedLayoutDescription().count() == 0); } // different count (not compatible) @@ -3290,6 +3291,8 @@ void tst_QRhi::srbLayoutCompatibility() QVERIFY(!srb2->isLayoutCompatible(srb1.data())); QVERIFY(srb1->serializedLayoutDescription() != srb2->serializedLayoutDescription()); + QVERIFY(srb1->serializedLayoutDescription().count() == 0); + QVERIFY(srb2->serializedLayoutDescription().count() == 1 * QRhiShaderResourceBinding::LAYOUT_DESC_ENTRIES_PER_BINDING); } // full match (compatible) @@ -3314,6 +3317,7 @@ void tst_QRhi::srbLayoutCompatibility() QVERIFY(!srb1->serializedLayoutDescription().isEmpty()); QVERIFY(!srb2->serializedLayoutDescription().isEmpty()); QCOMPARE(srb1->serializedLayoutDescription(), srb2->serializedLayoutDescription()); + QVERIFY(srb1->serializedLayoutDescription().count() == 2 * QRhiShaderResourceBinding::LAYOUT_DESC_ENTRIES_PER_BINDING); // see what we would get if a binding list got serialized "manually", without pulling it out from the srb after building // (the results should be identical) @@ -3323,6 +3327,11 @@ void tst_QRhi::srbLayoutCompatibility() QVector layoutDesc2; QRhiShaderResourceBinding::serializeLayoutDescription(srb2->cbeginBindings(), srb2->cendBindings(), std::back_inserter(layoutDesc2)); QCOMPARE(layoutDesc2, srb2->serializedLayoutDescription()); + + // exercise with an "output iterator" different from back_inserter + quint32 layoutDesc3[2 * QRhiShaderResourceBinding::LAYOUT_DESC_ENTRIES_PER_BINDING]; + QRhiShaderResourceBinding::serializeLayoutDescription(srb1->cbeginBindings(), srb1->cendBindings(), layoutDesc3); + QVERIFY(!memcmp(layoutDesc3, layoutDesc1.constData(), sizeof(quint32) * 2 * QRhiShaderResourceBinding::LAYOUT_DESC_ENTRIES_PER_BINDING)); } // different visibility (not compatible) -- cgit v1.2.3