diff options
-rw-r--r-- | src/opencl/qclworksize.cpp | 51 | ||||
-rw-r--r-- | src/opencl/qclworksize.h | 3 | ||||
-rw-r--r-- | tests/auto/qcl/tst_qcl.cpp | 36 |
3 files changed, 90 insertions, 0 deletions
diff --git a/src/opencl/qclworksize.cpp b/src/opencl/qclworksize.cpp index 79a351a..8a7c0fa 100644 --- a/src/opencl/qclworksize.cpp +++ b/src/opencl/qclworksize.cpp @@ -220,4 +220,55 @@ QCLWorkSize QCLWorkSize::toLocalWorkSize(const QCLDevice &device) const device.maximumWorkItemsPerGroup()); } +/*! + Returns the string form of this work size, with components + separated by 'x'. + + \sa fromString() +*/ +QString QCLWorkSize::toString() const +{ + if (m_dim == 1) { + return QString::number(qulonglong(m_sizes[0])); + } else if (m_dim == 2) { + return QString::number(qulonglong(m_sizes[0])) + QLatin1Char('x') + + QString::number(qulonglong(m_sizes[1])); + } else { + return QString::number(qulonglong(m_sizes[0])) + QLatin1Char('x') + + QString::number(qulonglong(m_sizes[1])) + QLatin1Char('x') + + QString::number(qulonglong(m_sizes[2])); + } +} + +/*! + Returns the work size that corresponds to the contents of \a str. + Components are assumed to be separated by 'x'. + + \sa toString() +*/ +QCLWorkSize QCLWorkSize::fromString(const QString &str) +{ + QStringList split = str.split(QLatin1Char('x')); + if (split.size() >= 3) { + return QCLWorkSize(size_t(split[0].toULongLong()), + size_t(split[1].toULongLong()), + size_t(split[2].toULongLong())); + } else if (split.size() == 2) { + return QCLWorkSize(size_t(split[0].toULongLong()), + size_t(split[1].toULongLong())); + } else if (split.size() == 1) { + // An empty string will turn into a single-element list, + // which we want to result in (1, 1, 1), not (0, 1, 1) + // so it matches the default QCLWorkSize(). + bool ok; + qulonglong value = split[0].toULongLong(&ok); + if (ok) + return QCLWorkSize(value); + else + return QCLWorkSize(); + } else { + return QCLWorkSize(); + } +} + QT_END_NAMESPACE diff --git a/src/opencl/qclworksize.h b/src/opencl/qclworksize.h index 0c5541e..e0c5b7f 100644 --- a/src/opencl/qclworksize.h +++ b/src/opencl/qclworksize.h @@ -82,6 +82,9 @@ public: (const QCLWorkSize &maxWorkItemSize, size_t maxItemsPerGroup) const; QCLWorkSize toLocalWorkSize(const QCLDevice &device) const; + QString toString() const; + static QCLWorkSize fromString(const QString &str); + private: size_t m_dim; size_t m_sizes[3]; diff --git a/tests/auto/qcl/tst_qcl.cpp b/tests/auto/qcl/tst_qcl.cpp index c0f3c4e..04d6df7 100644 --- a/tests/auto/qcl/tst_qcl.cpp +++ b/tests/auto/qcl/tst_qcl.cpp @@ -572,6 +572,42 @@ void tst_QCL::workSize() QVERIFY(size4.width() == size4.sizes()[0]); QVERIFY(size4.height() == size4.sizes()[1]); QVERIFY(size4.depth() == size4.sizes()[2]); + + QCLWorkSize size5; + size5 = QCLWorkSize::fromString(QLatin1String("")); + QVERIFY(size5.dimensions() == 1); + QVERIFY(size5.width() == 1); + QVERIFY(size5.height() == 1); + QVERIFY(size5.depth() == 1); + QCOMPARE(size5.toString(), QLatin1String("1")); + + size5 = QCLWorkSize::fromString(QLatin1String("23")); + QVERIFY(size5.dimensions() == 1); + QVERIFY(size5.width() == 23); + QVERIFY(size5.height() == 1); + QVERIFY(size5.depth() == 1); + QCOMPARE(size5.toString(), QLatin1String("23")); + + size5 = QCLWorkSize::fromString(QLatin1String("23x6")); + QVERIFY(size5.dimensions() == 2); + QVERIFY(size5.width() == 23); + QVERIFY(size5.height() == 6); + QVERIFY(size5.depth() == 1); + QCOMPARE(size5.toString(), QLatin1String("23x6")); + + size5 = QCLWorkSize::fromString(QLatin1String("23 x 6 x 43")); + QVERIFY(size5.dimensions() == 3); + QVERIFY(size5.width() == 23); + QVERIFY(size5.height() == 6); + QVERIFY(size5.depth() == 43); + QCOMPARE(size5.toString(), QLatin1String("23x6x43")); + + size5 = QCLWorkSize::fromString(QLatin1String("23x6x43x1")); + QVERIFY(size5.dimensions() == 3); + QVERIFY(size5.width() == 23); + QVERIFY(size5.height() == 6); + QVERIFY(size5.depth() == 43); + QCOMPARE(size5.toString(), QLatin1String("23x6x43")); } // Test QCLImageFormat. |