| 1 | // Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <qgraphicsutils_p.h> |
| 6 | |
| 7 | class tst_QGraphicsUtils : public QObject |
| 8 | { |
| 9 | Q_OBJECT |
| 10 | private slots: |
| 11 | void fillScalarInDataArray(); |
| 12 | void fillArray(); |
| 13 | void fillScalarWithOffsets(); |
| 14 | void fillMatrix4x4(); |
| 15 | void fillMatrix3x4(); |
| 16 | void fillMatrix4x3(); |
| 17 | void fillMatrixArray(); |
| 18 | }; |
| 19 | |
| 20 | void tst_QGraphicsUtils::fillScalarInDataArray() |
| 21 | { |
| 22 | Qt3DRender::Render::OpenGL::ShaderUniform description; |
| 23 | |
| 24 | description.m_size = 1; |
| 25 | description.m_offset = 0; |
| 26 | description.m_arrayStride = 10; |
| 27 | |
| 28 | QVector4D testVector(8.0f, 8.0f, 3.0f, 1.0f); |
| 29 | const GLfloat *vectorData = Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(testVector, 1, 4); |
| 30 | |
| 31 | for (int i = 0; i < 4; i++) { |
| 32 | if (i == 0) |
| 33 | QVERIFY(vectorData[i] == testVector.x()); |
| 34 | else if (i == 1) |
| 35 | QVERIFY(vectorData[i] == testVector.y()); |
| 36 | else if (i == 2) |
| 37 | QVERIFY(vectorData[i] == testVector.z()); |
| 38 | else if (i == 3) |
| 39 | QVERIFY(vectorData[i] == testVector.w()); |
| 40 | } |
| 41 | |
| 42 | QByteArray data(description.m_size * 4 * sizeof(GLfloat), 0); |
| 43 | char *innerData = data.data(); |
| 44 | |
| 45 | // Checked that we are not overflowing |
| 46 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataArray(innerData, vectorData, description, 2); |
| 47 | for (int i = 0; i < 4; ++i) { |
| 48 | if (i < 2) |
| 49 | QVERIFY(vectorData[i] == ((GLfloat*)innerData)[i]); |
| 50 | else |
| 51 | QVERIFY(((GLfloat*)innerData)[i] == 0.0f); |
| 52 | } |
| 53 | |
| 54 | // Check that all values are copied |
| 55 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataArray(innerData, vectorData, description, 4); |
| 56 | for (int i = 0; i < 4; ++i) |
| 57 | QVERIFY(vectorData[i] == ((GLfloat*)innerData)[i]); |
| 58 | |
| 59 | // check that offsetting works |
| 60 | description.m_offset = 16; |
| 61 | data = QByteArray(description.m_size * 8 * sizeof(GLfloat), 0); |
| 62 | innerData = data.data(); |
| 63 | |
| 64 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataArray(innerData, vectorData, description, 4); |
| 65 | for (int i = 0; i < 8; ++i) { |
| 66 | if (i < 4) |
| 67 | QVERIFY(((GLfloat*)innerData)[i] == 0.0f); |
| 68 | else |
| 69 | QVERIFY(vectorData[i - 4] == ((GLfloat*)innerData)[i]); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void tst_QGraphicsUtils::fillArray() |
| 74 | { |
| 75 | QVector4D testVector(8.0f, 8.0f, 3.0f, 1.0f); |
| 76 | QVector4D testVector2(3.0f, 5.0f, 0.0f, 7.0f); |
| 77 | QVector4D testVector3(4.0f, 5.0f, 4.0f, 2.0f); |
| 78 | |
| 79 | QVariantList variantList = QVariantList() << testVector << testVector2 << testVector3; |
| 80 | const GLfloat *vectorData = Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(QVariant(variantList), 3, 4); |
| 81 | |
| 82 | Qt3DRender::Render::OpenGL::ShaderUniform description; |
| 83 | |
| 84 | description.m_size = 3; |
| 85 | description.m_offset = 16; |
| 86 | description.m_arrayStride = 16; |
| 87 | |
| 88 | QByteArray data(description.m_size * (4 + description.m_arrayStride) * sizeof(GLfloat) + description.m_offset, 0); |
| 89 | char *innerData = data.data(); |
| 90 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataArray(innerData, vectorData, description, 4); |
| 91 | |
| 92 | int offset = description.m_offset / sizeof(GLfloat); |
| 93 | int stride = description.m_arrayStride / sizeof(GLfloat); |
| 94 | |
| 95 | GLfloat *innerDataFloat = (GLfloat*)innerData; |
| 96 | |
| 97 | for (int i = 0; i < 3; ++i) { |
| 98 | for (int j = 0; j < 4; ++j) { |
| 99 | int idx = i * 4 + j; |
| 100 | QVERIFY(innerDataFloat[offset + j] == vectorData[idx]); |
| 101 | } |
| 102 | offset += stride; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void tst_QGraphicsUtils::fillScalarWithOffsets() |
| 107 | { |
| 108 | // Simulates Uniform Block |
| 109 | |
| 110 | // uniform Block { |
| 111 | // vec3 position; // Offset 0 - 12 bytes |
| 112 | // vec3 direction; // Offset 16 - 12 bytes |
| 113 | // vec4 color; // Offset 32 - 16 bytes |
| 114 | // float intensity; // Offset 48 - bytes |
| 115 | // } // total size 64 bytes |
| 116 | |
| 117 | QVector3D position(8.0f, 8.0f, 3.0f); |
| 118 | QVector3D direction(3.0f, 5.0f, 2.0f); |
| 119 | QVector4D color(4.0f, 5.0f, 4.0f, 1.0f); |
| 120 | float intensity = 1.0f; |
| 121 | |
| 122 | Qt3DRender::Render::OpenGL::ShaderUniform posUniform; |
| 123 | posUniform.m_size = 1; |
| 124 | posUniform.m_arrayStride = 0; |
| 125 | posUniform.m_matrixStride = 0; |
| 126 | posUniform.m_offset = 0; |
| 127 | |
| 128 | Qt3DRender::Render::OpenGL::ShaderUniform dirUniform; |
| 129 | dirUniform.m_size = 1; |
| 130 | dirUniform.m_arrayStride = 0; |
| 131 | dirUniform.m_matrixStride = 0; |
| 132 | dirUniform.m_offset = 16; |
| 133 | |
| 134 | Qt3DRender::Render::OpenGL::ShaderUniform colUniform; |
| 135 | colUniform.m_size = 1; |
| 136 | colUniform.m_arrayStride = 0; |
| 137 | colUniform.m_matrixStride = 0; |
| 138 | colUniform.m_offset = 32; |
| 139 | |
| 140 | Qt3DRender::Render::OpenGL::ShaderUniform intUniform; |
| 141 | intUniform.m_size = 1; |
| 142 | intUniform.m_arrayStride = 0; |
| 143 | intUniform.m_matrixStride = 0; |
| 144 | intUniform.m_offset = 48; |
| 145 | |
| 146 | QVector<GLfloat> data(16); |
| 147 | void *innerData = data.data(); |
| 148 | |
| 149 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataArray(innerData, |
| 150 | Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(position, 1, 3), |
| 151 | posUniform, 3); |
| 152 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataArray(innerData, |
| 153 | Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(direction, 1, 3), |
| 154 | dirUniform, 3); |
| 155 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataArray(innerData, |
| 156 | Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(color, 1, 4), |
| 157 | colUniform, 4); |
| 158 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataArray(innerData, |
| 159 | Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(intensity, 1, 1), |
| 160 | intUniform, 1); |
| 161 | |
| 162 | GLfloat *floatData = (GLfloat*)innerData; |
| 163 | |
| 164 | // Check first 16 bytes - position |
| 165 | QVERIFY(floatData[0] == position.x()); |
| 166 | QVERIFY(floatData[1] == position.y()); |
| 167 | QVERIFY(floatData[2] == position.z()); |
| 168 | QVERIFY(floatData[3] == 0.0f); |
| 169 | // Check 16 - 32 bytes - direction |
| 170 | QVERIFY(floatData[4] == direction.x()); |
| 171 | QVERIFY(floatData[5] == direction.y()); |
| 172 | QVERIFY(floatData[6] == direction.z()); |
| 173 | QVERIFY(floatData[7] == 0.0f); |
| 174 | // Check 32 - 48 bytes - color |
| 175 | QVERIFY(floatData[8] == color.x()); |
| 176 | QVERIFY(floatData[9] == color.y()); |
| 177 | QVERIFY(floatData[10] == color.z()); |
| 178 | QVERIFY(floatData[11] == color.w()); |
| 179 | // Check 48 - 64 bytes - intensity |
| 180 | QVERIFY(floatData[12] == intensity); |
| 181 | QVERIFY(floatData[13] == 0.0f); |
| 182 | QVERIFY(floatData[14] == 0.0f); |
| 183 | QVERIFY(floatData[15] == 0.0f); |
| 184 | } |
| 185 | |
| 186 | void tst_QGraphicsUtils::fillMatrix4x4() |
| 187 | { |
| 188 | // row major |
| 189 | QMatrix4x4 mat(1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f, 16.0f); |
| 190 | |
| 191 | // column major |
| 192 | const GLfloat *matData = Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(mat, 1, 16); |
| 193 | |
| 194 | Qt3DRender::Render::OpenGL::ShaderUniform description; |
| 195 | |
| 196 | description.m_size = 1; |
| 197 | description.m_offset = 0; |
| 198 | description.m_arrayStride = 0; |
| 199 | description.m_matrixStride = 16; |
| 200 | |
| 201 | |
| 202 | QByteArray data(description.m_size * 16 * sizeof(GLfloat), 0); |
| 203 | char *innerData = data.data(); |
| 204 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataMatrixArray(innerData, matData, description, 4, 4); |
| 205 | // Check for no offset/no stride |
| 206 | for (int i = 0; i < 16; ++i) |
| 207 | QVERIFY((((GLfloat *)innerData)[i]) == matData[i]); |
| 208 | |
| 209 | description.m_offset = 12; |
| 210 | data = QByteArray((description.m_size * 16 + description.m_offset) * sizeof(GLfloat), 0); |
| 211 | innerData = data.data(); |
| 212 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataMatrixArray(innerData, matData, description, 4, 4); |
| 213 | // Check with 12 offset/no stride |
| 214 | for (int i = 0; i < 16; ++i) |
| 215 | QVERIFY((((GLfloat *)innerData)[3 + i]) == matData[i]); |
| 216 | |
| 217 | description.m_matrixStride = 16; |
| 218 | data = QByteArray((description.m_size * 16 + 4 * description.m_matrixStride + description.m_offset) * sizeof(GLfloat), 0); |
| 219 | innerData = data.data(); |
| 220 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataMatrixArray(innerData, matData, description, 4, 4); |
| 221 | // Check with 10 offset/ 16 stride |
| 222 | int offset = description.m_offset / sizeof(GLfloat); |
| 223 | int matrixStride = description.m_matrixStride / sizeof(GLfloat); |
| 224 | |
| 225 | for (int col = 0; col < 4; ++col) { |
| 226 | for (int row = 0; row < 4; ++row) |
| 227 | QVERIFY((((GLfloat *)innerData)[offset + row]) == matData[col * 4 + row]); |
| 228 | offset += matrixStride; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void tst_QGraphicsUtils::fillMatrix3x4() |
| 233 | { |
| 234 | QMatrix3x4 mat; |
| 235 | |
| 236 | mat.fill(6.0f); |
| 237 | const GLfloat *matData = Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(QVariant::fromValue(mat), 1, 12); |
| 238 | |
| 239 | Qt3DRender::Render::OpenGL::ShaderUniform description; |
| 240 | |
| 241 | description.m_size = 1; |
| 242 | description.m_offset = 16; |
| 243 | description.m_arrayStride = 0; |
| 244 | description.m_matrixStride = 12; |
| 245 | |
| 246 | QByteArray data((description.m_size * 12 + 3 * description.m_matrixStride + description.m_offset) * sizeof(GLfloat), 0); |
| 247 | char *innerData = data.data(); |
| 248 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataMatrixArray(innerData, matData, description, 3, 4); |
| 249 | // Check with 16 offset/ 12 stride |
| 250 | int offset = description.m_offset / sizeof(GLfloat); |
| 251 | int matrixStride = description.m_matrixStride / sizeof(GLfloat); |
| 252 | |
| 253 | for (int col = 0; col < 3; ++col) { |
| 254 | for (int row = 0; row < 4; ++row) |
| 255 | QVERIFY((((GLfloat *)innerData)[offset + row]) == matData[col * 4 + row]); |
| 256 | offset += matrixStride; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void tst_QGraphicsUtils::fillMatrix4x3() |
| 261 | { |
| 262 | QMatrix4x3 mat; |
| 263 | |
| 264 | mat.fill(6.0f); |
| 265 | const GLfloat *matData = Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(QVariant::fromValue(mat), 1, 12); |
| 266 | |
| 267 | Qt3DRender::Render::OpenGL::ShaderUniform description; |
| 268 | |
| 269 | description.m_size = 1; |
| 270 | description.m_offset = 16; |
| 271 | description.m_arrayStride = 0; |
| 272 | description.m_matrixStride = 16; |
| 273 | |
| 274 | QByteArray data((description.m_size * 12 + 4 * description.m_matrixStride + description.m_offset) * sizeof(GLfloat), 0); |
| 275 | char *innerData = data.data(); |
| 276 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataMatrixArray(innerData, matData, description, 4, 3); |
| 277 | // Check with 16 offset/ 16 stride |
| 278 | int offset = description.m_offset / sizeof(GLfloat); |
| 279 | int matrixStride = description.m_matrixStride / sizeof(GLfloat); |
| 280 | |
| 281 | for (int col = 0; col < 4; ++col) { |
| 282 | for (int row = 0; row < 3; ++row) |
| 283 | QVERIFY((((GLfloat *)innerData)[offset + row]) == matData[col * 3 + row]); |
| 284 | offset += matrixStride; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void tst_QGraphicsUtils::fillMatrixArray() |
| 289 | { |
| 290 | QMatrix4x3 mat1; |
| 291 | QMatrix4x3 mat2; |
| 292 | QMatrix4x3 mat3; |
| 293 | mat1.fill(6.0f); |
| 294 | mat2.fill(2.0f); |
| 295 | mat3.fill(7.0f); |
| 296 | |
| 297 | QVariantList matrices = QVariantList() << QVariant::fromValue(mat1) << QVariant::fromValue(mat2) << QVariant::fromValue(mat3); |
| 298 | |
| 299 | const GLfloat *matData = Qt3DRender::Render::OpenGL::QGraphicsUtils::valueArrayFromVariant<GLfloat>(QVariant::fromValue(matrices), 3, 12); |
| 300 | |
| 301 | Qt3DRender::Render::OpenGL::ShaderUniform description; |
| 302 | |
| 303 | description.m_size = 3; |
| 304 | description.m_offset = 12; |
| 305 | description.m_arrayStride = 4; |
| 306 | description.m_matrixStride = 16; |
| 307 | |
| 308 | QByteArray data((description.m_size * (12 + 4 * description.m_matrixStride + description.m_arrayStride) + description.m_offset) * sizeof(GLfloat), 0); |
| 309 | char *innerData = data.data(); |
| 310 | Qt3DRender::Render::OpenGL::QGraphicsUtils::fillDataMatrixArray(innerData, matData, description, 4, 3); |
| 311 | // Check with 12 offset/ 4 array stride / 16 matrix stride |
| 312 | int offset = description.m_offset / sizeof(GLfloat); |
| 313 | int matrixStride = description.m_matrixStride / sizeof(GLfloat); |
| 314 | int arrayStride = description.m_arrayStride / sizeof(GLfloat); |
| 315 | |
| 316 | for (int i = 0; i < 3; ++i) { |
| 317 | for (int col = 0; col < 4; ++col) { |
| 318 | for (int row = 0; row < 3; ++row) { |
| 319 | int idx = i * 4 * 3 + col * 3 + row; |
| 320 | QVERIFY((((GLfloat *)innerData)[offset + row]) == matData[idx]); |
| 321 | } |
| 322 | offset += matrixStride; |
| 323 | } |
| 324 | offset += arrayStride; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | QTEST_APPLESS_MAIN(tst_QGraphicsUtils) |
| 329 | |
| 330 | #include "tst_qgraphicsutils.moc" |