| 1 | // Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <QtTest/QTest> |
| 5 | #include <Qt3DRender/qrendertargetoutput.h> |
| 6 | #include <Qt3DRender/private/uniform_p.h> |
| 7 | #include <graphicshelpergl3_2_p.h> |
| 8 | #include <Qt3DRender/private/attachmentpack_p.h> |
| 9 | #include <QOpenGLBuffer> |
| 10 | #include <QOpenGLVersionFunctionsFactory> |
| 11 | #include <QOpenGLFunctions_3_2_Core> |
| 12 | #include <QOpenGLShaderProgram> |
| 13 | #include <QOpenGLVertexArrayObject> |
| 14 | #include <QSurfaceFormat> |
| 15 | #include <QElapsedTimer> |
| 16 | |
| 17 | #if !QT_CONFIG(opengles2) && defined(QT_OPENGL_3_2) |
| 18 | |
| 19 | #define TEST_SHOULD_BE_PERFORMED 1 |
| 20 | |
| 21 | using namespace Qt3DRender; |
| 22 | using namespace Qt3DRender::Render; |
| 23 | using namespace Qt3DRender::Render::OpenGL; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | const QByteArray vertCode = QByteArrayLiteral( |
| 28 | "#version 150 core\n" \ |
| 29 | "in vec3 vertexPosition;\n" \ |
| 30 | "in vec2 vertexTexCoord;\n" \ |
| 31 | "out vec2 texCoord;\n" \ |
| 32 | "void main()\n" \ |
| 33 | "{\n" \ |
| 34 | " texCoord = vertexTexCoord;\n" \ |
| 35 | " gl_Position = vec4(vertexPosition, 1.0);\n" \ |
| 36 | "}\n"); |
| 37 | |
| 38 | const QByteArray vertCodeUniformBuffer = QByteArrayLiteral( |
| 39 | "#version 150 core\n" \ |
| 40 | "in vec3 vertexPosition;\n" \ |
| 41 | "in vec2 vertexTexCoord;\n" \ |
| 42 | "in int vertexColorIndex;\n" \ |
| 43 | "out vec2 texCoord;\n" \ |
| 44 | "flat out int colorIndex;\n" \ |
| 45 | "void main()\n" \ |
| 46 | "{\n" \ |
| 47 | " texCoord = vertexTexCoord;\n" \ |
| 48 | " colorIndex = vertexColorIndex;\n" \ |
| 49 | " gl_Position = vec4(vertexPosition, 1.0);\n" \ |
| 50 | "}\n"); |
| 51 | |
| 52 | const QByteArray fragCodeFragOutputs = QByteArrayLiteral( |
| 53 | "#version 150 core\n" \ |
| 54 | "out vec4 color;\n" \ |
| 55 | "out vec2 temp;\n" \ |
| 56 | "void main()\n" \ |
| 57 | "{\n" \ |
| 58 | " color = vec4(1.0, 0.0, 0.0, 1.0);\n" \ |
| 59 | " temp = vec2(1.0, 0.3);\n" \ |
| 60 | "}\n"); |
| 61 | |
| 62 | const QByteArray fragCodeUniformsFloat = QByteArrayLiteral( |
| 63 | "#version 150 core\n" \ |
| 64 | "out vec4 color;\n" \ |
| 65 | "uniform float multiplier;\n" \ |
| 66 | "uniform vec2 multiplierVec2;\n" \ |
| 67 | "uniform vec3 multiplierVec3;\n" \ |
| 68 | "uniform vec4 multiplierVec4;\n" \ |
| 69 | "void main()\n" \ |
| 70 | "{\n" \ |
| 71 | " vec4 randomMult = multiplierVec4 + vec4(multiplierVec3, 0.0) + vec4(multiplierVec2, 0.0, 0.0);\n" \ |
| 72 | " color = vec4(1.0, 0.0, 0.0, 1.0) * randomMult * multiplier;\n" \ |
| 73 | "}\n"); |
| 74 | |
| 75 | const QByteArray fragCodeUniformsInt = QByteArrayLiteral( |
| 76 | "#version 150 core\n" \ |
| 77 | "out vec4 color;\n" \ |
| 78 | "uniform int multiplier;\n" \ |
| 79 | "uniform ivec2 multiplierVec2;\n" \ |
| 80 | "uniform ivec3 multiplierVec3;\n" \ |
| 81 | "uniform ivec4 multiplierVec4;\n" \ |
| 82 | "void main()\n" \ |
| 83 | "{\n" \ |
| 84 | " ivec4 randomMult = multiplierVec4 + ivec4(multiplierVec3, 0) + ivec4(multiplierVec2, 0, 0);\n" \ |
| 85 | " color = ivec4(1, 0, 0, 1) * randomMult * multiplier;\n" \ |
| 86 | "}\n"); |
| 87 | |
| 88 | const QByteArray fragCodeUniformsUInt = QByteArrayLiteral( |
| 89 | "#version 150 core\n" \ |
| 90 | "out vec4 color;\n" \ |
| 91 | "uniform uint multiplier;\n" \ |
| 92 | "uniform uvec2 multiplierVec2;\n" \ |
| 93 | "uniform uvec3 multiplierVec3;\n" \ |
| 94 | "uniform uvec4 multiplierVec4;\n" \ |
| 95 | "void main()\n" \ |
| 96 | "{\n" \ |
| 97 | " uvec4 randomMult = multiplierVec4 + uvec4(multiplierVec3, 0) + uvec4(multiplierVec2, 0, 0);\n" \ |
| 98 | " color = uvec4(1, 0, 0, 1) * randomMult * multiplier;\n" \ |
| 99 | "}\n"); |
| 100 | |
| 101 | const QByteArray fragCodeUniformsFloatMatrices = QByteArrayLiteral( |
| 102 | "#version 150 core\n" \ |
| 103 | "out vec4 color;\n" \ |
| 104 | "uniform mat2 m2;\n" \ |
| 105 | "uniform mat2x3 m23;\n" \ |
| 106 | "uniform mat3x2 m32;\n" \ |
| 107 | "uniform mat2x4 m24;\n" \ |
| 108 | "uniform mat4x2 m42;\n" \ |
| 109 | "uniform mat3 m3;\n" \ |
| 110 | "uniform mat3x4 m34;\n" \ |
| 111 | "uniform mat4x3 m43;\n" \ |
| 112 | "uniform mat4 m4;\n" \ |
| 113 | "void main()\n" \ |
| 114 | "{\n" \ |
| 115 | " float lengthSum = m2[0][0] + m23[0][0] + m32[0][0] + m24[0][0] + m42[0][0] + m3[0][0] + m34[0][0] + m43[0][0] + m4[0][0];\n" \ |
| 116 | " color = vec4(1, 0, 0, 1) * lengthSum;\n" \ |
| 117 | "}\n"); |
| 118 | |
| 119 | const QByteArray fragCodeUniformBuffer = QByteArrayLiteral( |
| 120 | "#version 150 core\n" \ |
| 121 | "out vec4 color;\n" \ |
| 122 | "in vec2 texCoord;\n" \ |
| 123 | "flat in int colorIndex;\n" \ |
| 124 | "uniform ColorArray\n" \ |
| 125 | "{\n" \ |
| 126 | " vec4 colors[256];\n" \ |
| 127 | "};\n" \ |
| 128 | "void main()\n" \ |
| 129 | "{\n" \ |
| 130 | " color = colors[colorIndex] + vec4(texCoord.s, texCoord.t, 0.0, 1.0);\n" \ |
| 131 | "}\n"); |
| 132 | |
| 133 | const QByteArray fragCodeSamplers = QByteArrayLiteral( |
| 134 | "#version 150 core\n" \ |
| 135 | "in vec2 texCoord;\n" \ |
| 136 | "out vec4 color;\n" \ |
| 137 | "uniform sampler1D s1;\n" \ |
| 138 | "uniform sampler2D s2;\n" \ |
| 139 | "uniform sampler2DArray s2a;\n" \ |
| 140 | "uniform sampler3D s3;\n" \ |
| 141 | "uniform samplerCube scube;\n" \ |
| 142 | "uniform sampler2DRect srect;\n" \ |
| 143 | "void main()\n" \ |
| 144 | "{\n" \ |
| 145 | " color = vec4(1, 0, 0, 1) *" \ |
| 146 | " texture(s1, texCoord.x) *" \ |
| 147 | " texture(s2, texCoord) *" \ |
| 148 | " texture(s2a, vec3(texCoord, 0.0)) *" \ |
| 149 | " texture(s3, vec3(texCoord, 0.0)) *" \ |
| 150 | " texture(scube, vec3(texCoord, 0)) *" \ |
| 151 | " texture(srect, texCoord);\n" \ |
| 152 | "}\n"); |
| 153 | |
| 154 | } // anonymous |
| 155 | |
| 156 | class tst_GraphicsHelperGL3_2 : public QObject |
| 157 | { |
| 158 | Q_OBJECT |
| 159 | private Q_SLOTS: |
| 160 | |
| 161 | void init() |
| 162 | { |
| 163 | m_window.reset(new QWindow); |
| 164 | m_window->setSurfaceType(QWindow::OpenGLSurface); |
| 165 | m_window->setGeometry(0, 0, 10, 10); |
| 166 | m_window->create(); |
| 167 | |
| 168 | QSurfaceFormat format; |
| 169 | format.setVersion(3, 2); |
| 170 | format.setProfile(QSurfaceFormat::CoreProfile); |
| 171 | format.setDepthBufferSize(24); |
| 172 | format.setSamples(4); |
| 173 | format.setStencilBufferSize(8); |
| 174 | m_window->setFormat(format); |
| 175 | m_glContext.setFormat(format); |
| 176 | |
| 177 | if (!m_glContext.create()) { |
| 178 | qWarning() << "Failed to create OpenGL context"; |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (!m_glContext.makeCurrent(m_window.data())) { |
| 183 | qWarning() << "Failed to make OpenGL context current"; |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if ((m_func = QOpenGLVersionFunctionsFactory::get<QOpenGLFunctions_3_2_Core>()) != nullptr) { |
| 188 | m_glHelper.initializeHelper(&m_glContext, m_func); |
| 189 | m_initializationSuccessful = true; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void cleanup() |
| 194 | { |
| 195 | m_glContext.doneCurrent(); |
| 196 | } |
| 197 | |
| 198 | void alphaTest() |
| 199 | { |
| 200 | if (!m_initializationSuccessful) |
| 201 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 202 | // Deprecated |
| 203 | } |
| 204 | |
| 205 | void bindBufferBase() |
| 206 | { |
| 207 | if (!m_initializationSuccessful) |
| 208 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 209 | |
| 210 | // GIVEN |
| 211 | GLuint bufferId = 0; |
| 212 | // WHEN |
| 213 | m_func->glGenBuffers(1, &bufferId); |
| 214 | // THEN |
| 215 | QVERIFY(bufferId != 0); |
| 216 | |
| 217 | |
| 218 | // WHEN |
| 219 | m_func->glBindBuffer(GL_UNIFORM_BUFFER, bufferId); |
| 220 | m_glHelper.bindBufferBase(GL_UNIFORM_BUFFER, 2, bufferId); |
| 221 | // THEN |
| 222 | const GLint error = m_func->glGetError(); |
| 223 | QVERIFY(error == 0); |
| 224 | GLint boundToPointBufferId = 0; |
| 225 | m_func->glGetIntegeri_v(GL_UNIFORM_BUFFER_BINDING, 2, &boundToPointBufferId); |
| 226 | QVERIFY(boundToPointBufferId == GLint(bufferId)); |
| 227 | |
| 228 | // Restore to sane state |
| 229 | m_func->glBindBuffer(GL_UNIFORM_BUFFER, 0); |
| 230 | m_func->glDeleteBuffers(1, &bufferId); |
| 231 | } |
| 232 | |
| 233 | void bindFragDataLocation() |
| 234 | { |
| 235 | if (!m_initializationSuccessful) |
| 236 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 237 | |
| 238 | // GIVEN |
| 239 | QOpenGLShaderProgram shaderProgram; |
| 240 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 241 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeFragOutputs); |
| 242 | |
| 243 | // WHEN |
| 244 | QHash<QString, int> fragLocations; |
| 245 | fragLocations.insert(QStringLiteral("temp"), 2); |
| 246 | fragLocations.insert(QStringLiteral("color"), 1); |
| 247 | m_glHelper.bindFragDataLocation(shaderProgram.programId(), fragLocations); |
| 248 | |
| 249 | // THEN |
| 250 | QVERIFY(shaderProgram.link()); |
| 251 | const GLint error = m_func->glGetError(); |
| 252 | QVERIFY(error == 0); |
| 253 | const GLint tempLocation = m_func->glGetFragDataLocation(shaderProgram.programId(), "temp"); |
| 254 | const GLint colorLocation = m_func->glGetFragDataLocation(shaderProgram.programId(), "color"); |
| 255 | QCOMPARE(tempLocation, 2); |
| 256 | QCOMPARE(colorLocation, 1); |
| 257 | } |
| 258 | |
| 259 | void bindFrameBufferAttachment() |
| 260 | { |
| 261 | if (!m_initializationSuccessful) |
| 262 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 263 | |
| 264 | // GIVEN |
| 265 | GLuint fboId; |
| 266 | m_func->glGenFramebuffers(1, &fboId); |
| 267 | |
| 268 | Attachment attachment; |
| 269 | attachment.m_point = QRenderTargetOutput::Color0; |
| 270 | |
| 271 | // THEN |
| 272 | QVERIFY(fboId != 0); |
| 273 | |
| 274 | // WHEN |
| 275 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboId); |
| 276 | |
| 277 | QOpenGLTexture texture(QOpenGLTexture::Target2D); |
| 278 | texture.setSize(512, 512); |
| 279 | texture.setFormat(QOpenGLTexture::RGBA32F); |
| 280 | texture.setMinificationFilter(QOpenGLTexture::Linear); |
| 281 | texture.setMagnificationFilter(QOpenGLTexture::Linear); |
| 282 | texture.setWrapMode(QOpenGLTexture::ClampToEdge); |
| 283 | if (!texture.create()) |
| 284 | qWarning() << "Texture creation failed"; |
| 285 | texture.allocateStorage(); |
| 286 | QVERIFY(texture.isStorageAllocated()); |
| 287 | GLint error = m_func->glGetError(); |
| 288 | QVERIFY(error == 0); |
| 289 | m_glHelper.bindFrameBufferAttachment(&texture, attachment); |
| 290 | |
| 291 | // THEN |
| 292 | GLenum status = m_func->glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); |
| 293 | QVERIFY(status == GL_FRAMEBUFFER_COMPLETE); |
| 294 | |
| 295 | error = m_func->glGetError(); |
| 296 | QVERIFY(error == 0); |
| 297 | GLint textureAttachmentId = 0; |
| 298 | m_func->glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, |
| 299 | GL_COLOR_ATTACHMENT0, |
| 300 | GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, |
| 301 | &textureAttachmentId); |
| 302 | QCOMPARE(GLuint(textureAttachmentId), texture.textureId()); |
| 303 | |
| 304 | // Restore state |
| 305 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 306 | m_func->glDeleteFramebuffers(1, &fboId); |
| 307 | } |
| 308 | |
| 309 | void bindFrameBufferObject() |
| 310 | { |
| 311 | if (!m_initializationSuccessful) |
| 312 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 313 | |
| 314 | // GIVEN |
| 315 | GLuint fboId; |
| 316 | m_func->glGenFramebuffers(1, &fboId); |
| 317 | |
| 318 | // THEN |
| 319 | QVERIFY(fboId != 0); |
| 320 | |
| 321 | // WHEN |
| 322 | m_glHelper.bindFrameBufferObject(fboId, GraphicsHelperInterface::FBODraw); |
| 323 | |
| 324 | // THEN |
| 325 | GLint error = m_func->glGetError(); |
| 326 | QVERIFY(error == 0); |
| 327 | GLint boundindFBOId = 0; |
| 328 | m_func->glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundindFBOId); |
| 329 | QVERIFY(GLuint(boundindFBOId) == fboId); |
| 330 | |
| 331 | // WHEN |
| 332 | m_glHelper.bindFrameBufferObject(fboId, GraphicsHelperInterface::FBORead); |
| 333 | |
| 334 | // THEN |
| 335 | error = m_func->glGetError(); |
| 336 | QVERIFY(error == 0); |
| 337 | boundindFBOId = 0; |
| 338 | m_func->glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING, &boundindFBOId); |
| 339 | QVERIFY(GLuint(boundindFBOId) == fboId); |
| 340 | |
| 341 | // WHEN |
| 342 | m_glHelper.bindFrameBufferObject(fboId, GraphicsHelperInterface::FBOReadAndDraw); |
| 343 | |
| 344 | // THEN |
| 345 | error = m_func->glGetError(); |
| 346 | QVERIFY(error == 0); |
| 347 | boundindFBOId = 0; |
| 348 | m_func->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &boundindFBOId); |
| 349 | QVERIFY(GLuint(boundindFBOId) == fboId); |
| 350 | |
| 351 | // Cleanup |
| 352 | m_func->glDeleteFramebuffers(1, &fboId); |
| 353 | } |
| 354 | |
| 355 | void bindShaderStorageBlock() |
| 356 | { |
| 357 | if (!m_initializationSuccessful) |
| 358 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 359 | // Not supported in OpenGL 3.2 |
| 360 | } |
| 361 | |
| 362 | void bindUniformBlock() |
| 363 | { |
| 364 | if (!m_initializationSuccessful) |
| 365 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 366 | |
| 367 | // GIVEN |
| 368 | QOpenGLShaderProgram shaderProgram; |
| 369 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCodeUniformBuffer); |
| 370 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformBuffer); |
| 371 | QVERIFY(shaderProgram.link()); |
| 372 | |
| 373 | // WHEN |
| 374 | GLint index = m_func->glGetUniformBlockIndex(shaderProgram.programId(), "ColorArray"); |
| 375 | m_glHelper.bindUniformBlock(shaderProgram.programId(), index, 1); |
| 376 | |
| 377 | // THEN |
| 378 | const GLint error = m_func->glGetError(); |
| 379 | QVERIFY(error == 0); |
| 380 | } |
| 381 | |
| 382 | void blendEquation() |
| 383 | { |
| 384 | if (!m_initializationSuccessful) |
| 385 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 386 | |
| 387 | // GIVEN |
| 388 | GLint equation = 0; |
| 389 | m_func->glGetIntegerv(GL_BLEND_EQUATION_RGB, &equation); |
| 390 | QCOMPARE(equation, GL_FUNC_ADD); |
| 391 | |
| 392 | // WHEN |
| 393 | m_glHelper.blendEquation(GL_FUNC_REVERSE_SUBTRACT); |
| 394 | |
| 395 | // THEN |
| 396 | m_func->glGetIntegerv(GL_BLEND_EQUATION_RGB, &equation); |
| 397 | QCOMPARE(equation, GL_FUNC_REVERSE_SUBTRACT); |
| 398 | } |
| 399 | |
| 400 | void blendFunci() |
| 401 | { |
| 402 | if (!m_initializationSuccessful) |
| 403 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 404 | // Not supported by OpenGL 3.2 |
| 405 | } |
| 406 | |
| 407 | void blendFuncSeparatei() |
| 408 | { |
| 409 | if (!m_initializationSuccessful) |
| 410 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 411 | |
| 412 | // Not supported by OpenGL 3.2 |
| 413 | } |
| 414 | |
| 415 | void boundFrameBufferObject() |
| 416 | { |
| 417 | if (!m_initializationSuccessful) |
| 418 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 419 | |
| 420 | // GIVEN |
| 421 | GLuint fboId; |
| 422 | m_func->glGenFramebuffers(1, &fboId); |
| 423 | |
| 424 | // WHEN |
| 425 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboId); |
| 426 | |
| 427 | // THEN |
| 428 | GLint boundBuffer = 0; |
| 429 | m_func->glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &boundBuffer); |
| 430 | QCOMPARE(GLuint(boundBuffer), fboId); |
| 431 | |
| 432 | // THEN |
| 433 | QCOMPARE(m_glHelper.boundFrameBufferObject(), fboId); |
| 434 | |
| 435 | // Reset state |
| 436 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 437 | m_func->glDeleteFramebuffers(1, &fboId); |
| 438 | } |
| 439 | |
| 440 | void checkFrameBufferComplete() |
| 441 | { |
| 442 | if (!m_initializationSuccessful) |
| 443 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 444 | // GIVEN |
| 445 | GLuint fboId; |
| 446 | m_func->glGenFramebuffers(1, &fboId); |
| 447 | |
| 448 | Attachment attachment; |
| 449 | attachment.m_point = QRenderTargetOutput::Color0; |
| 450 | |
| 451 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboId); |
| 452 | |
| 453 | QOpenGLTexture texture(QOpenGLTexture::Target2D); |
| 454 | texture.setSize(512, 512); |
| 455 | texture.setFormat(QOpenGLTexture::RGBA8U); |
| 456 | texture.setMinificationFilter(QOpenGLTexture::Linear); |
| 457 | texture.setMagnificationFilter(QOpenGLTexture::Linear); |
| 458 | texture.create(); |
| 459 | texture.allocateStorage(); |
| 460 | m_glHelper.bindFrameBufferAttachment(&texture, attachment); |
| 461 | |
| 462 | // THEN |
| 463 | GLenum status = m_func->glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); |
| 464 | QVERIFY(status == GL_FRAMEBUFFER_COMPLETE); |
| 465 | |
| 466 | QVERIFY(m_glHelper.checkFrameBufferComplete()); |
| 467 | |
| 468 | // Restore |
| 469 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 470 | m_func->glDeleteFramebuffers(1, &fboId); |
| 471 | } |
| 472 | |
| 473 | void clearBufferf() |
| 474 | { |
| 475 | if (!m_initializationSuccessful) |
| 476 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 477 | |
| 478 | // GIVEN |
| 479 | // GIVEN |
| 480 | GLuint fboId; |
| 481 | m_func->glGenFramebuffers(1, &fboId); |
| 482 | |
| 483 | // THEN |
| 484 | QVERIFY(fboId != 0); |
| 485 | |
| 486 | // WHEN |
| 487 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboId); |
| 488 | // Create 4 attachments |
| 489 | QOpenGLTexture *textures[4]; |
| 490 | for (int i = 0; i < 4; ++i) { |
| 491 | Attachment attachment; |
| 492 | attachment.m_point = static_cast<QRenderTargetOutput::AttachmentPoint>(i); |
| 493 | |
| 494 | QOpenGLTexture *texture = new QOpenGLTexture(QOpenGLTexture::Target2D); |
| 495 | textures[i] = texture; |
| 496 | texture->setSize(512, 512); |
| 497 | texture->setFormat(QOpenGLTexture::RGBA32F); |
| 498 | texture->setMinificationFilter(QOpenGLTexture::Linear); |
| 499 | texture->setMagnificationFilter(QOpenGLTexture::Linear); |
| 500 | texture->setWrapMode(QOpenGLTexture::ClampToEdge); |
| 501 | if (!texture->create()) |
| 502 | qWarning() << "Texture creation failed"; |
| 503 | texture->allocateStorage(); |
| 504 | QVERIFY(texture->isStorageAllocated()); |
| 505 | GLint error = m_func->glGetError(); |
| 506 | QVERIFY(error == 0); |
| 507 | m_glHelper.bindFrameBufferAttachment(texture, attachment); |
| 508 | } |
| 509 | |
| 510 | GLenum status = m_func->glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); |
| 511 | QVERIFY(status == GL_FRAMEBUFFER_COMPLETE); |
| 512 | |
| 513 | // Set Draw buffers |
| 514 | GLenum clearBufferEnum = GL_COLOR_ATTACHMENT3; |
| 515 | m_func->glDrawBuffers(1, &clearBufferEnum); |
| 516 | |
| 517 | const GLint bufferIndex = 0; // index of the element in the draw buffers |
| 518 | GLint error = m_func->glGetError(); |
| 519 | QVERIFY(error == 0); |
| 520 | |
| 521 | // WHEN |
| 522 | const QVector4D clearValue1 = QVector4D(0.5f, 0.2f, 0.4f, 0.8f); |
| 523 | m_func->glClearBufferfv(GL_COLOR, bufferIndex, reinterpret_cast<const float *>(&clearValue1)); |
| 524 | error = m_func->glGetError(); |
| 525 | QVERIFY(error == 0); |
| 526 | |
| 527 | // THEN |
| 528 | QVector<QVector4D> colors(512 * 512); |
| 529 | textures[3]->bind(); |
| 530 | m_func->glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, colors.data()); |
| 531 | textures[3]->release(); |
| 532 | for (const QVector4D &c : colors) |
| 533 | QVERIFY(c == clearValue1); |
| 534 | |
| 535 | // WHEN |
| 536 | const QVector4D clearValue2 = QVector4D(0.4f, 0.5f, 0.4f, 1.0f); |
| 537 | m_glHelper.clearBufferf(bufferIndex, clearValue2); |
| 538 | |
| 539 | // THEN |
| 540 | textures[3]->bind(); |
| 541 | m_func->glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, colors.data()); |
| 542 | textures[3]->release(); |
| 543 | for (const QVector4D &c : colors) |
| 544 | QVERIFY(c == clearValue2); |
| 545 | |
| 546 | // Restore |
| 547 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 548 | m_func->glDeleteFramebuffers(1, &fboId); |
| 549 | for (int i = 0; i < 4; ++i) |
| 550 | delete textures[i]; |
| 551 | } |
| 552 | |
| 553 | void createFrameBufferObject() |
| 554 | { |
| 555 | if (!m_initializationSuccessful) |
| 556 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 557 | |
| 558 | // WHEN |
| 559 | const GLuint fboId = m_glHelper.createFrameBufferObject(); |
| 560 | |
| 561 | // THEN |
| 562 | QVERIFY(fboId != 0); |
| 563 | |
| 564 | // Restore |
| 565 | m_func->glDeleteFramebuffers(1, &fboId); |
| 566 | } |
| 567 | |
| 568 | void depthMask() |
| 569 | { |
| 570 | if (!m_initializationSuccessful) |
| 571 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 572 | |
| 573 | // GIVEN |
| 574 | GLboolean depthWritingEnabled = false; |
| 575 | m_func->glGetBooleanv(GL_DEPTH_WRITEMASK, &depthWritingEnabled); |
| 576 | |
| 577 | // THEN |
| 578 | QVERIFY(depthWritingEnabled); |
| 579 | |
| 580 | // WHEN |
| 581 | m_glHelper.depthMask(GL_FALSE); |
| 582 | |
| 583 | // THEN |
| 584 | m_func->glGetBooleanv(GL_DEPTH_WRITEMASK, &depthWritingEnabled); |
| 585 | QVERIFY(!depthWritingEnabled); |
| 586 | |
| 587 | // WHEN |
| 588 | m_glHelper.depthMask(GL_TRUE); |
| 589 | |
| 590 | // THEN |
| 591 | m_func->glGetBooleanv(GL_DEPTH_WRITEMASK, &depthWritingEnabled); |
| 592 | QVERIFY(depthWritingEnabled); |
| 593 | } |
| 594 | |
| 595 | void depthTest() |
| 596 | { |
| 597 | if (!m_initializationSuccessful) |
| 598 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 599 | |
| 600 | // GIVEN |
| 601 | m_func->glDisable(GL_DEPTH_TEST); |
| 602 | m_func->glDepthFunc(GL_LESS); |
| 603 | |
| 604 | // WHEN |
| 605 | m_glHelper.depthTest(GL_LEQUAL); |
| 606 | |
| 607 | // THEN |
| 608 | QVERIFY(m_func->glIsEnabled(GL_DEPTH_TEST)); |
| 609 | GLint depthMode = 0; |
| 610 | m_func->glGetIntegerv(GL_DEPTH_FUNC, &depthMode); |
| 611 | QCOMPARE(depthMode, GL_LEQUAL); |
| 612 | |
| 613 | // WHEN |
| 614 | m_glHelper.depthTest(GL_LESS); |
| 615 | QVERIFY(m_func->glIsEnabled(GL_DEPTH_TEST)); |
| 616 | m_func->glGetIntegerv(GL_DEPTH_FUNC, &depthMode); |
| 617 | QCOMPARE(depthMode, GL_LESS); |
| 618 | } |
| 619 | |
| 620 | void disableClipPlane() |
| 621 | { |
| 622 | if (!m_initializationSuccessful) |
| 623 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 624 | |
| 625 | // GIVEN |
| 626 | m_func->glEnable(GL_CLIP_DISTANCE0 + 5); |
| 627 | |
| 628 | // THEN |
| 629 | QVERIFY(m_func->glIsEnabled(GL_CLIP_DISTANCE0 + 5)); |
| 630 | |
| 631 | // WHEN |
| 632 | m_glHelper.disableClipPlane(5); |
| 633 | |
| 634 | // THEN |
| 635 | QVERIFY(!m_func->glIsEnabled(GL_CLIP_DISTANCE0 + 5)); |
| 636 | } |
| 637 | |
| 638 | void disablei() |
| 639 | { |
| 640 | if (!m_initializationSuccessful) |
| 641 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 642 | |
| 643 | // GIVEN |
| 644 | m_func->glEnablei(GL_BLEND, 2); |
| 645 | |
| 646 | // THEN |
| 647 | QVERIFY(m_func->glIsEnabledi(GL_BLEND, 2)); |
| 648 | |
| 649 | // WHEN |
| 650 | m_glHelper.disablei(GL_BLEND, 2); |
| 651 | |
| 652 | // THEN |
| 653 | QVERIFY(!m_func->glIsEnabledi(GL_BLEND, 2)); |
| 654 | } |
| 655 | |
| 656 | void disablePrimitiveRestart() |
| 657 | { |
| 658 | if (!m_initializationSuccessful) |
| 659 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 660 | |
| 661 | // GIVEN |
| 662 | m_func->glEnable(GL_PRIMITIVE_RESTART); |
| 663 | |
| 664 | // THEN |
| 665 | QVERIFY(m_func->glIsEnabled(GL_PRIMITIVE_RESTART)); |
| 666 | |
| 667 | // WHEN |
| 668 | m_glHelper.disablePrimitiveRestart(); |
| 669 | |
| 670 | // THEN |
| 671 | QVERIFY(!m_func->glIsEnabled(GL_PRIMITIVE_RESTART)); |
| 672 | } |
| 673 | |
| 674 | void drawBuffers() |
| 675 | { |
| 676 | if (!m_initializationSuccessful) |
| 677 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 678 | |
| 679 | // GIVEN |
| 680 | GLuint fboId; |
| 681 | m_func->glGenFramebuffers(1, &fboId); |
| 682 | |
| 683 | // THEN |
| 684 | QVERIFY(fboId != 0); |
| 685 | |
| 686 | // WHEN |
| 687 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboId); |
| 688 | QOpenGLTexture *textures[4]; |
| 689 | |
| 690 | // Create 4 attachments |
| 691 | for (int i = 0; i < 4; ++i) { |
| 692 | Attachment attachment; |
| 693 | attachment.m_point = static_cast<QRenderTargetOutput::AttachmentPoint>(i); |
| 694 | |
| 695 | QOpenGLTexture *texture = new QOpenGLTexture(QOpenGLTexture::Target2D); |
| 696 | textures[i] = texture; |
| 697 | texture->setSize(512, 512); |
| 698 | texture->setFormat(QOpenGLTexture::RGBA32F); |
| 699 | texture->setMinificationFilter(QOpenGLTexture::Linear); |
| 700 | texture->setMagnificationFilter(QOpenGLTexture::Linear); |
| 701 | texture->setWrapMode(QOpenGLTexture::ClampToEdge); |
| 702 | if (!texture->create()) |
| 703 | qWarning() << "Texture creation failed"; |
| 704 | texture->allocateStorage(); |
| 705 | QVERIFY(texture->isStorageAllocated()); |
| 706 | GLint error = m_func->glGetError(); |
| 707 | QVERIFY(error == 0); |
| 708 | m_glHelper.bindFrameBufferAttachment(texture, attachment); |
| 709 | } |
| 710 | // THEN |
| 711 | GLenum status = m_func->glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); |
| 712 | QVERIFY(status == GL_FRAMEBUFFER_COMPLETE); |
| 713 | |
| 714 | // WHEN |
| 715 | GLenum bufferEnum = GL_COLOR_ATTACHMENT4; |
| 716 | m_func->glDrawBuffers(1, &bufferEnum); |
| 717 | |
| 718 | // THEN |
| 719 | GLint enumValue = -1; |
| 720 | m_func->glGetIntegerv(GL_DRAW_BUFFER0, &enumValue); |
| 721 | QCOMPARE(enumValue, GL_COLOR_ATTACHMENT4); |
| 722 | |
| 723 | // WHEN |
| 724 | GLenum newBufferEnum = GL_COLOR_ATTACHMENT0 + 2; |
| 725 | m_glHelper.drawBuffers(1, &newBufferEnum); |
| 726 | |
| 727 | // THEN |
| 728 | m_func->glGetIntegerv(GL_DRAW_BUFFER0, &enumValue); |
| 729 | QCOMPARE(enumValue, GL_COLOR_ATTACHMENT0 + 2); |
| 730 | |
| 731 | // WHEN |
| 732 | newBufferEnum = 0; |
| 733 | m_glHelper.drawBuffers(1, &newBufferEnum); |
| 734 | |
| 735 | // THEN |
| 736 | m_func->glGetIntegerv(GL_DRAW_BUFFER0, &enumValue); |
| 737 | QCOMPARE(enumValue, GL_NONE); |
| 738 | |
| 739 | // Restore |
| 740 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 741 | m_func->glDeleteFramebuffers(1, &fboId); |
| 742 | for (int i = 0; i < 4; ++i) |
| 743 | delete textures[i]; |
| 744 | } |
| 745 | |
| 746 | void enableClipPlane() |
| 747 | { |
| 748 | if (!m_initializationSuccessful) |
| 749 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 750 | |
| 751 | // GIVEN |
| 752 | m_func->glDisable(GL_CLIP_DISTANCE0 + 4); |
| 753 | |
| 754 | // THEN |
| 755 | QVERIFY(!m_func->glIsEnabled(GL_CLIP_DISTANCE0 + 4)); |
| 756 | |
| 757 | // WHEN |
| 758 | m_glHelper.enableClipPlane(4); |
| 759 | |
| 760 | // THEN |
| 761 | QVERIFY(m_func->glIsEnabled(GL_CLIP_DISTANCE0 + 4)); |
| 762 | } |
| 763 | |
| 764 | void enablei() |
| 765 | { |
| 766 | if (!m_initializationSuccessful) |
| 767 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 768 | |
| 769 | // GIVEN |
| 770 | m_func->glDisablei(GL_BLEND, 4); |
| 771 | |
| 772 | // THEN |
| 773 | QVERIFY(!m_func->glIsEnabledi(GL_BLEND, 4)); |
| 774 | |
| 775 | // WHEN |
| 776 | m_glHelper.enablei(GL_BLEND, 4); |
| 777 | |
| 778 | // THEN |
| 779 | QVERIFY(m_func->glIsEnabledi(GL_BLEND, 4)); |
| 780 | } |
| 781 | |
| 782 | void enablePrimitiveRestart() |
| 783 | { |
| 784 | if (!m_initializationSuccessful) |
| 785 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 786 | |
| 787 | // GIVEN |
| 788 | m_func->glDisable(GL_PRIMITIVE_RESTART); |
| 789 | |
| 790 | // THEN |
| 791 | QVERIFY(!m_func->glIsEnabled(GL_PRIMITIVE_RESTART)); |
| 792 | |
| 793 | // WHEN |
| 794 | m_glHelper.enablePrimitiveRestart(883); |
| 795 | |
| 796 | // THEN |
| 797 | QVERIFY(m_func->glIsEnabled(GL_PRIMITIVE_RESTART)); |
| 798 | GLint restartIndex = 0; |
| 799 | m_func->glGetIntegerv(GL_PRIMITIVE_RESTART_INDEX, &restartIndex); |
| 800 | QCOMPARE(restartIndex, 883); |
| 801 | |
| 802 | // Restore |
| 803 | m_func->glDisable(GL_PRIMITIVE_RESTART); |
| 804 | } |
| 805 | |
| 806 | void enableVertexAttribute() |
| 807 | { |
| 808 | if (!m_initializationSuccessful) |
| 809 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 810 | |
| 811 | // GIVEN |
| 812 | QOpenGLVertexArrayObject vao; |
| 813 | vao.create(); |
| 814 | QOpenGLVertexArrayObject::Binder binder(&vao); |
| 815 | |
| 816 | QOpenGLShaderProgram shaderProgram; |
| 817 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCodeUniformBuffer); |
| 818 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformBuffer); |
| 819 | QVERIFY(shaderProgram.link()); |
| 820 | shaderProgram.bind(); |
| 821 | |
| 822 | // WHEN |
| 823 | GLint positionLocation = m_func->glGetAttribLocation(shaderProgram.programId(), "vertexPosition"); |
| 824 | GLint texCoordLocation = m_func->glGetAttribLocation(shaderProgram.programId(), "vertexTexCoord"); |
| 825 | GLint colorIndexLocation = m_func->glGetAttribLocation(shaderProgram.programId(), "vertexColorIndex"); |
| 826 | m_glHelper.enableVertexAttributeArray(positionLocation); |
| 827 | m_glHelper.enableVertexAttributeArray(texCoordLocation); |
| 828 | m_glHelper.enableVertexAttributeArray(colorIndexLocation); |
| 829 | |
| 830 | // THEN |
| 831 | const GLint error = m_func->glGetError(); |
| 832 | QVERIFY(error == 0); |
| 833 | } |
| 834 | |
| 835 | void frontFace() |
| 836 | { |
| 837 | if (!m_initializationSuccessful) |
| 838 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 839 | |
| 840 | // GIVEN |
| 841 | m_func->glFrontFace(GL_CW); |
| 842 | |
| 843 | // THEN |
| 844 | GLint face = 0; |
| 845 | m_func->glGetIntegerv(GL_FRONT_FACE, &face); |
| 846 | QCOMPARE(face, GL_CW); |
| 847 | |
| 848 | // WHEN |
| 849 | m_glHelper.frontFace(GL_CCW); |
| 850 | |
| 851 | // THEN |
| 852 | m_func->glGetIntegerv(GL_FRONT_FACE, &face); |
| 853 | QCOMPARE(face, GL_CCW); |
| 854 | } |
| 855 | |
| 856 | void getRenderBufferDimensions() |
| 857 | { |
| 858 | if (!m_initializationSuccessful) |
| 859 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 860 | |
| 861 | // GIVEN |
| 862 | GLuint renderBufferId = 0; |
| 863 | m_func->glGenRenderbuffers(1, &renderBufferId); |
| 864 | QVERIFY(renderBufferId != 0); |
| 865 | |
| 866 | // WHEN |
| 867 | m_func->glBindRenderbuffer(GL_RENDERBUFFER, renderBufferId); |
| 868 | m_func->glRenderbufferStorage(GL_RENDERBUFFER, GL_SRGB8_ALPHA8, 512, 512); |
| 869 | m_func->glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 870 | const QSize dimensions = m_glHelper.getRenderBufferDimensions(renderBufferId); |
| 871 | |
| 872 | // THEN |
| 873 | QCOMPARE(dimensions, QSize(512, 512)); |
| 874 | |
| 875 | // Restore |
| 876 | m_func->glDeleteRenderbuffers(1, &renderBufferId); |
| 877 | } |
| 878 | |
| 879 | void getTextureDimensions() |
| 880 | { |
| 881 | if (!m_initializationSuccessful) |
| 882 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 883 | |
| 884 | // GIVEN |
| 885 | QOpenGLTexture texture(QOpenGLTexture::Target2D); |
| 886 | texture.setSize(512, 512); |
| 887 | texture.setFormat(QOpenGLTexture::RGBA8U); |
| 888 | texture.setMinificationFilter(QOpenGLTexture::Linear); |
| 889 | texture.setMagnificationFilter(QOpenGLTexture::Linear); |
| 890 | texture.create(); |
| 891 | texture.allocateStorage(); |
| 892 | |
| 893 | // WHEN |
| 894 | const QSize dimensions = m_glHelper.getTextureDimensions(texture.textureId(), GL_TEXTURE_2D); |
| 895 | |
| 896 | // THEN |
| 897 | QCOMPARE(dimensions, QSize(512, 512)); |
| 898 | } |
| 899 | |
| 900 | void pointSize() |
| 901 | { |
| 902 | if (!m_initializationSuccessful) |
| 903 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 904 | |
| 905 | // GIVEN |
| 906 | m_func->glEnable(GL_PROGRAM_POINT_SIZE); |
| 907 | |
| 908 | // THEN |
| 909 | QVERIFY(m_func->glIsEnabled(GL_PROGRAM_POINT_SIZE)); |
| 910 | GLfloat size = 0; |
| 911 | m_func->glGetFloatv(GL_POINT_SIZE, &size); |
| 912 | QCOMPARE(size, 1.0f); |
| 913 | |
| 914 | // WHEN |
| 915 | m_glHelper.pointSize(false, 0.5f); |
| 916 | |
| 917 | // THEN |
| 918 | QVERIFY(!m_func->glIsEnabled(GL_PROGRAM_POINT_SIZE)); |
| 919 | m_func->glGetFloatv(GL_POINT_SIZE, &size); |
| 920 | QCOMPARE(size, 0.5f); |
| 921 | } |
| 922 | |
| 923 | void maxClipPlaneCount() |
| 924 | { |
| 925 | if (!m_initializationSuccessful) |
| 926 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 927 | |
| 928 | // GIVEN |
| 929 | GLint maxCount = -1; |
| 930 | m_func->glGetIntegerv(GL_MAX_CLIP_PLANES, &maxCount); |
| 931 | |
| 932 | // THEN |
| 933 | QCOMPARE(maxCount, m_glHelper.maxClipPlaneCount()); |
| 934 | } |
| 935 | |
| 936 | void programUniformBlock() |
| 937 | { |
| 938 | if (!m_initializationSuccessful) |
| 939 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 940 | |
| 941 | // GIVEN |
| 942 | QOpenGLShaderProgram shaderProgram; |
| 943 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCodeUniformBuffer); |
| 944 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformBuffer); |
| 945 | QVERIFY(shaderProgram.link()); |
| 946 | |
| 947 | // WHEN |
| 948 | const std::vector<ShaderUniformBlock> activeUniformBlocks = m_glHelper.programUniformBlocks(shaderProgram.programId()); |
| 949 | |
| 950 | // THEN |
| 951 | QCOMPARE(activeUniformBlocks.size(), 1U); |
| 952 | const ShaderUniformBlock uniformBlock = activeUniformBlocks.front(); |
| 953 | |
| 954 | QCOMPARE(uniformBlock.m_activeUniformsCount, 1); |
| 955 | QCOMPARE(uniformBlock.m_name, QStringLiteral("ColorArray")); |
| 956 | |
| 957 | GLint blockIndex = m_func->glGetUniformBlockIndex(shaderProgram.programId(), "ColorArray"); |
| 958 | GLint blockBinding = -1; |
| 959 | m_func->glGetActiveUniformBlockiv(shaderProgram.programId(), blockIndex, GL_UNIFORM_BLOCK_BINDING, &blockBinding); |
| 960 | QCOMPARE(blockIndex, uniformBlock.m_index); |
| 961 | QCOMPARE(blockBinding, uniformBlock.m_binding); |
| 962 | } |
| 963 | |
| 964 | void programAttributesAndLocations() |
| 965 | { |
| 966 | if (!m_initializationSuccessful) |
| 967 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 968 | |
| 969 | // GIVEN |
| 970 | QOpenGLShaderProgram shaderProgram; |
| 971 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 972 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeSamplers); |
| 973 | QVERIFY(shaderProgram.link()); |
| 974 | |
| 975 | // WHEN |
| 976 | std::vector<ShaderAttribute> activeAttributes = m_glHelper.programAttributesAndLocations(shaderProgram.programId()); |
| 977 | |
| 978 | // THEN |
| 979 | QCOMPARE(activeAttributes.size(), 2U); |
| 980 | std::sort(activeAttributes.begin(), activeAttributes.end(), [] (const ShaderAttribute &a, const ShaderAttribute &b) { return a.m_name < b.m_name; }); |
| 981 | |
| 982 | const ShaderAttribute attribute1 = activeAttributes.at(0); |
| 983 | QCOMPARE(attribute1.m_name, QStringLiteral("vertexPosition")); |
| 984 | QCOMPARE(attribute1.m_size, 1); |
| 985 | QCOMPARE(attribute1.m_location, shaderProgram.attributeLocation("vertexPosition")); |
| 986 | QCOMPARE(attribute1.m_type, GLenum(GL_FLOAT_VEC3)); |
| 987 | |
| 988 | const ShaderAttribute attribute2 = activeAttributes.at(1); |
| 989 | QCOMPARE(attribute2.m_name, QStringLiteral("vertexTexCoord")); |
| 990 | QCOMPARE(attribute2.m_size, 1); |
| 991 | QCOMPARE(attribute2.m_location, shaderProgram.attributeLocation("vertexTexCoord")); |
| 992 | QCOMPARE(attribute2.m_type, GLenum(GL_FLOAT_VEC2)); |
| 993 | } |
| 994 | |
| 995 | void programUniformsAndLocations() |
| 996 | { |
| 997 | if (!m_initializationSuccessful) |
| 998 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 999 | |
| 1000 | // GIVEN |
| 1001 | QOpenGLShaderProgram shaderProgram; |
| 1002 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1003 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloat); |
| 1004 | QVERIFY(shaderProgram.link()); |
| 1005 | |
| 1006 | // WHEN |
| 1007 | std::vector<ShaderUniform> activeUniforms = m_glHelper.programUniformsAndLocations(shaderProgram.programId()); |
| 1008 | |
| 1009 | // THEN |
| 1010 | QCOMPARE(activeUniforms.size(), 4U); |
| 1011 | std::sort(activeUniforms.begin(), activeUniforms.end(), [] (const ShaderUniform &a, const ShaderUniform &b) { return a.m_name < b.m_name; }); |
| 1012 | |
| 1013 | const ShaderUniform uniform1 = activeUniforms.at(0); |
| 1014 | QCOMPARE(uniform1.m_location, shaderProgram.uniformLocation("multiplier")); |
| 1015 | QCOMPARE(uniform1.m_offset, -1); |
| 1016 | QCOMPARE(uniform1.m_blockIndex, -1); |
| 1017 | QCOMPARE(uniform1.m_arrayStride, -1); |
| 1018 | QCOMPARE(uniform1.m_matrixStride, -1); |
| 1019 | QCOMPARE(uniform1.m_size, 1); |
| 1020 | QCOMPARE(uniform1.m_type, GLenum(GL_FLOAT)); |
| 1021 | QCOMPARE(uniform1.m_name, QStringLiteral("multiplier")); |
| 1022 | |
| 1023 | const ShaderUniform uniform2 = activeUniforms.at(1); |
| 1024 | QCOMPARE(uniform2.m_location, shaderProgram.uniformLocation("multiplierVec2")); |
| 1025 | QCOMPARE(uniform2.m_offset, -1); |
| 1026 | QCOMPARE(uniform2.m_blockIndex, -1); |
| 1027 | QCOMPARE(uniform2.m_arrayStride, -1); |
| 1028 | QCOMPARE(uniform2.m_matrixStride, -1); |
| 1029 | QCOMPARE(uniform2.m_size, 1); |
| 1030 | QCOMPARE(uniform2.m_type, GLenum(GL_FLOAT_VEC2)); |
| 1031 | QCOMPARE(uniform2.m_name, QStringLiteral("multiplierVec2")); |
| 1032 | |
| 1033 | const ShaderUniform uniform3 = activeUniforms.at(2); |
| 1034 | QCOMPARE(uniform3.m_location, shaderProgram.uniformLocation("multiplierVec3")); |
| 1035 | QCOMPARE(uniform3.m_offset, -1); |
| 1036 | QCOMPARE(uniform3.m_blockIndex, -1); |
| 1037 | QCOMPARE(uniform3.m_arrayStride, -1); |
| 1038 | QCOMPARE(uniform3.m_matrixStride, -1); |
| 1039 | QCOMPARE(uniform3.m_size, 1); |
| 1040 | QCOMPARE(uniform3.m_type, GLenum(GL_FLOAT_VEC3)); |
| 1041 | QCOMPARE(uniform3.m_name, QStringLiteral("multiplierVec3")); |
| 1042 | |
| 1043 | const ShaderUniform uniform4 = activeUniforms.at(3); |
| 1044 | QCOMPARE(uniform4.m_location, shaderProgram.uniformLocation("multiplierVec4")); |
| 1045 | QCOMPARE(uniform4.m_offset, -1); |
| 1046 | QCOMPARE(uniform4.m_blockIndex, -1); |
| 1047 | QCOMPARE(uniform4.m_arrayStride, -1); |
| 1048 | QCOMPARE(uniform4.m_matrixStride, -1); |
| 1049 | QCOMPARE(uniform4.m_size, 1); |
| 1050 | QCOMPARE(uniform4.m_type, GLenum(GL_FLOAT_VEC4)); |
| 1051 | QCOMPARE(uniform4.m_name, QStringLiteral("multiplierVec4")); |
| 1052 | } |
| 1053 | |
| 1054 | void programShaderStorageBlock() |
| 1055 | { |
| 1056 | if (!m_initializationSuccessful) |
| 1057 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1058 | |
| 1059 | // Not supported in 3.2 |
| 1060 | } |
| 1061 | |
| 1062 | void releaseFrameBufferObject() |
| 1063 | { |
| 1064 | if (!m_initializationSuccessful) |
| 1065 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1066 | |
| 1067 | // GIVEN |
| 1068 | GLuint fboId; |
| 1069 | m_func->glGenFramebuffers(1, &fboId); |
| 1070 | |
| 1071 | // THEN |
| 1072 | QVERIFY(fboId != 0); |
| 1073 | |
| 1074 | // WHEN |
| 1075 | m_glHelper.releaseFrameBufferObject(fboId); |
| 1076 | |
| 1077 | // THEN |
| 1078 | QVERIFY(!m_func->glIsFramebuffer(fboId)); |
| 1079 | } |
| 1080 | |
| 1081 | void setMSAAEnabled() |
| 1082 | { |
| 1083 | if (!m_initializationSuccessful) |
| 1084 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1085 | |
| 1086 | // GIVEN |
| 1087 | m_func->glDisable(GL_MULTISAMPLE); |
| 1088 | |
| 1089 | // THEN |
| 1090 | QVERIFY(!m_func->glIsEnabled(GL_MULTISAMPLE)); |
| 1091 | |
| 1092 | // WHEN |
| 1093 | m_glHelper.setMSAAEnabled(true); |
| 1094 | |
| 1095 | // THEN |
| 1096 | QVERIFY(m_func->glIsEnabled(GL_MULTISAMPLE)); |
| 1097 | |
| 1098 | // WHEN |
| 1099 | m_glHelper.setMSAAEnabled(false); |
| 1100 | |
| 1101 | // THEN |
| 1102 | QVERIFY(!m_func->glIsEnabled(GL_MULTISAMPLE)); |
| 1103 | } |
| 1104 | |
| 1105 | void setAlphaCoverageEnabled() |
| 1106 | { |
| 1107 | if (!m_initializationSuccessful) |
| 1108 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1109 | |
| 1110 | // GIVEN |
| 1111 | m_func->glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE); |
| 1112 | |
| 1113 | // THEN |
| 1114 | QVERIFY(!m_func->glIsEnabled(GL_SAMPLE_ALPHA_TO_COVERAGE)); |
| 1115 | |
| 1116 | // WHEN |
| 1117 | m_glHelper.setAlphaCoverageEnabled(true); |
| 1118 | |
| 1119 | // THEN |
| 1120 | QVERIFY(m_func->glIsEnabled(GL_SAMPLE_ALPHA_TO_COVERAGE)); |
| 1121 | |
| 1122 | // WHEN |
| 1123 | m_glHelper.setAlphaCoverageEnabled(false); |
| 1124 | |
| 1125 | // THEN |
| 1126 | QVERIFY(!m_func->glIsEnabled(GL_SAMPLE_ALPHA_TO_COVERAGE)); |
| 1127 | } |
| 1128 | |
| 1129 | void setClipPlane() |
| 1130 | { |
| 1131 | if (!m_initializationSuccessful) |
| 1132 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1133 | |
| 1134 | // Deprecated in 3.3 core |
| 1135 | } |
| 1136 | |
| 1137 | void setSeamlessCubemap() |
| 1138 | { |
| 1139 | if (!m_initializationSuccessful) |
| 1140 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1141 | |
| 1142 | // GIVEN |
| 1143 | m_func->glDisable(GL_TEXTURE_CUBE_MAP_SEAMLESS); |
| 1144 | QVERIFY(!m_func->glIsEnabled(GL_TEXTURE_CUBE_MAP_SEAMLESS)); |
| 1145 | |
| 1146 | // WHEN |
| 1147 | m_glHelper.setSeamlessCubemap(true); |
| 1148 | |
| 1149 | // THEN |
| 1150 | QVERIFY(m_func->glIsEnabled(GL_TEXTURE_CUBE_MAP_SEAMLESS)); |
| 1151 | |
| 1152 | // WHEN |
| 1153 | m_glHelper.setSeamlessCubemap(false); |
| 1154 | |
| 1155 | // THEN |
| 1156 | QVERIFY(!m_func->glIsEnabled(GL_TEXTURE_CUBE_MAP_SEAMLESS)); |
| 1157 | } |
| 1158 | |
| 1159 | void setVerticesPerPatch() |
| 1160 | { |
| 1161 | if (!m_initializationSuccessful) |
| 1162 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1163 | |
| 1164 | // GIVEN |
| 1165 | m_func->glDisable(GL_TEXTURE_CUBE_MAP_SEAMLESS); |
| 1166 | |
| 1167 | // THEN |
| 1168 | QVERIFY(!m_func->glIsEnabled(GL_TEXTURE_CUBE_MAP_SEAMLESS)); |
| 1169 | |
| 1170 | // WHEN |
| 1171 | m_glHelper.setSeamlessCubemap(true); |
| 1172 | |
| 1173 | // THEN |
| 1174 | QVERIFY(m_func->glIsEnabled(GL_TEXTURE_CUBE_MAP_SEAMLESS)); |
| 1175 | |
| 1176 | // WHEN |
| 1177 | m_glHelper.setSeamlessCubemap(false); |
| 1178 | |
| 1179 | // THEN |
| 1180 | QVERIFY(!m_func->glIsEnabled(GL_TEXTURE_CUBE_MAP_SEAMLESS)); |
| 1181 | } |
| 1182 | |
| 1183 | #define SUPPORTS_FEATURE(Feature, IsSupported) \ |
| 1184 | QVERIFY(m_glHelper.supportsFeature(Feature) == IsSupported); |
| 1185 | |
| 1186 | void supportsFeature() |
| 1187 | { |
| 1188 | SUPPORTS_FEATURE(GraphicsHelperInterface::MRT, true); |
| 1189 | SUPPORTS_FEATURE(GraphicsHelperInterface::UniformBufferObject, true); |
| 1190 | SUPPORTS_FEATURE(GraphicsHelperInterface::BindableFragmentOutputs, true); |
| 1191 | SUPPORTS_FEATURE(GraphicsHelperInterface::PrimitiveRestart, true); |
| 1192 | SUPPORTS_FEATURE(GraphicsHelperInterface::RenderBufferDimensionRetrieval, true); |
| 1193 | SUPPORTS_FEATURE(GraphicsHelperInterface::TextureDimensionRetrieval, true); |
| 1194 | SUPPORTS_FEATURE(GraphicsHelperInterface::UniformBufferObject, true); |
| 1195 | SUPPORTS_FEATURE(GraphicsHelperInterface::ShaderStorageObject, false); |
| 1196 | SUPPORTS_FEATURE(GraphicsHelperInterface::Compute, false); |
| 1197 | SUPPORTS_FEATURE(GraphicsHelperInterface::DrawBuffersBlend, false); |
| 1198 | // Tesselation could be true or false depending on extensions so not tested |
| 1199 | SUPPORTS_FEATURE(GraphicsHelperInterface::BlitFramebuffer, true); |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | #define ADD_UNIFORM_ENTRY(FragShader, Name, Type, ComponentSize, ExpectedRawSize) \ |
| 1204 | QTest::newRow(#FragShader"_"#Type) << FragShader << QStringLiteral(Name) << Type << ComponentSize << ExpectedRawSize; |
| 1205 | |
| 1206 | void uniformsByteSize_data() |
| 1207 | { |
| 1208 | QTest::addColumn<QByteArray>("fragShader"); |
| 1209 | QTest::addColumn<QString>("name"); |
| 1210 | QTest::addColumn<int>("type"); |
| 1211 | QTest::addColumn<int>("componentSize"); |
| 1212 | QTest::addColumn<int>("expectedByteSize"); |
| 1213 | |
| 1214 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloat, "multiplier", GL_FLOAT, 1, 4); |
| 1215 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloat, "multiplierVec2", GL_FLOAT_VEC2, 1, 4 * 2); |
| 1216 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloat, "multiplierVec3",GL_FLOAT_VEC3, 1, 4 * 3); |
| 1217 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloat, "multiplierVec4", GL_FLOAT_VEC4, 1, 4 * 4); |
| 1218 | |
| 1219 | ADD_UNIFORM_ENTRY(fragCodeUniformsInt, "multiplier", GL_INT, 1, 4); |
| 1220 | ADD_UNIFORM_ENTRY(fragCodeUniformsInt, "multiplierVec2", GL_INT_VEC2, 1, 4 * 2); |
| 1221 | ADD_UNIFORM_ENTRY(fragCodeUniformsInt, "multiplierVec3", GL_INT_VEC3, 1, 4 * 3); |
| 1222 | ADD_UNIFORM_ENTRY(fragCodeUniformsInt, "multiplierVec4", GL_INT_VEC4, 1, 4 * 4); |
| 1223 | |
| 1224 | ADD_UNIFORM_ENTRY(fragCodeUniformsUInt, "multiplier", GL_UNSIGNED_INT, 1, 4); |
| 1225 | ADD_UNIFORM_ENTRY(fragCodeUniformsUInt, "multiplierVec2", GL_UNSIGNED_INT_VEC2, 1, 4 * 2); |
| 1226 | ADD_UNIFORM_ENTRY(fragCodeUniformsUInt, "multiplierVec3", GL_UNSIGNED_INT_VEC3, 1, 4 * 3); |
| 1227 | ADD_UNIFORM_ENTRY(fragCodeUniformsUInt, "multiplierVec4", GL_UNSIGNED_INT_VEC4, 1, 4 * 4); |
| 1228 | |
| 1229 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m2", GL_FLOAT_MAT2, 1, 4 * 2 * 2); |
| 1230 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m23", GL_FLOAT_MAT2x3, 1, 4 * 2 * 3); |
| 1231 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m32", GL_FLOAT_MAT3x2, 1, 4 * 3 * 2); |
| 1232 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m24", GL_FLOAT_MAT2x4, 1, 4 * 2 * 4); |
| 1233 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m42", GL_FLOAT_MAT4x2, 1, 4 * 4 * 2); |
| 1234 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m3", GL_FLOAT_MAT3, 1, 4 * 3 * 3); |
| 1235 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m34", GL_FLOAT_MAT3x4, 1, 4 * 3 * 4); |
| 1236 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m43", GL_FLOAT_MAT4x3, 1, 4 * 4 * 3); |
| 1237 | ADD_UNIFORM_ENTRY(fragCodeUniformsFloatMatrices, "m4", GL_FLOAT_MAT4, 1, 4 * 4 * 4); |
| 1238 | |
| 1239 | ADD_UNIFORM_ENTRY(fragCodeSamplers, "s1", GL_SAMPLER_1D, 1, 4); |
| 1240 | ADD_UNIFORM_ENTRY(fragCodeSamplers, "s2", GL_SAMPLER_2D, 1, 4); |
| 1241 | ADD_UNIFORM_ENTRY(fragCodeSamplers, "s2a", GL_SAMPLER_2D_ARRAY, 1, 4); |
| 1242 | ADD_UNIFORM_ENTRY(fragCodeSamplers, "s3", GL_SAMPLER_3D, 1, 4); |
| 1243 | ADD_UNIFORM_ENTRY(fragCodeSamplers, "scube", GL_SAMPLER_CUBE, 1, 4); |
| 1244 | ADD_UNIFORM_ENTRY(fragCodeSamplers, "srect", GL_SAMPLER_2D_RECT, 1, 4); |
| 1245 | } |
| 1246 | |
| 1247 | void uniformsByteSize() |
| 1248 | { |
| 1249 | if (!m_initializationSuccessful) |
| 1250 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1251 | |
| 1252 | // GIVEN |
| 1253 | QFETCH(QByteArray, fragShader); |
| 1254 | QFETCH(QString, name); |
| 1255 | QFETCH(int, type); |
| 1256 | QFETCH(int, componentSize); |
| 1257 | QFETCH(int, expectedByteSize); |
| 1258 | |
| 1259 | QOpenGLShaderProgram shaderProgram; |
| 1260 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1261 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragShader); |
| 1262 | QVERIFY(shaderProgram.link()); |
| 1263 | |
| 1264 | GLint location = shaderProgram.uniformLocation(name); |
| 1265 | // WHEN |
| 1266 | const std::vector<ShaderUniform> activeUniforms = m_glHelper.programUniformsAndLocations(shaderProgram.programId()); |
| 1267 | ShaderUniform matchingUniform; |
| 1268 | for (const ShaderUniform &u : activeUniforms) { |
| 1269 | if (u.m_location == location) { |
| 1270 | matchingUniform = u; |
| 1271 | break; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | |
| 1276 | // THEN |
| 1277 | QCOMPARE(matchingUniform.m_location, location); |
| 1278 | QCOMPARE(matchingUniform.m_type, GLuint(type)); |
| 1279 | QCOMPARE(matchingUniform.m_size, componentSize); |
| 1280 | |
| 1281 | // WHEN |
| 1282 | const int computedRawByteSize = m_glHelper.uniformByteSize(matchingUniform); |
| 1283 | |
| 1284 | // THEN |
| 1285 | QCOMPARE(expectedByteSize, computedRawByteSize); |
| 1286 | |
| 1287 | // Restore |
| 1288 | m_func->glUseProgram(0); |
| 1289 | } |
| 1290 | |
| 1291 | void useProgram() |
| 1292 | { |
| 1293 | if (!m_initializationSuccessful) |
| 1294 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1295 | |
| 1296 | // GIVEN |
| 1297 | QOpenGLShaderProgram shaderProgram; |
| 1298 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1299 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeFragOutputs); |
| 1300 | |
| 1301 | // THEN |
| 1302 | QVERIFY(shaderProgram.link()); |
| 1303 | |
| 1304 | GLint currentProg = 0; |
| 1305 | m_func->glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProg); |
| 1306 | QVERIFY(currentProg == 0); |
| 1307 | |
| 1308 | // WHEN |
| 1309 | m_glHelper.useProgram(shaderProgram.programId()); |
| 1310 | |
| 1311 | // THEN |
| 1312 | m_func->glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProg); |
| 1313 | QCOMPARE(GLuint(currentProg), shaderProgram.programId()); |
| 1314 | |
| 1315 | // WHEN |
| 1316 | m_glHelper.useProgram(0); |
| 1317 | |
| 1318 | // THEN |
| 1319 | m_func->glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProg); |
| 1320 | QVERIFY(currentProg == 0); |
| 1321 | } |
| 1322 | |
| 1323 | void vertexAttribDivisor() |
| 1324 | { |
| 1325 | if (!m_initializationSuccessful) |
| 1326 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1327 | // Not available in 3.2 |
| 1328 | } |
| 1329 | |
| 1330 | void vertexAttributePointer() |
| 1331 | { |
| 1332 | if (!m_initializationSuccessful) |
| 1333 | QSKIP("Initialization failed, OpenGL 4.3 Core functions not supported"); |
| 1334 | |
| 1335 | // GIVEN |
| 1336 | QOpenGLVertexArrayObject vao; |
| 1337 | vao.create(); |
| 1338 | QOpenGLVertexArrayObject::Binder binder(&vao); |
| 1339 | |
| 1340 | QOpenGLShaderProgram shaderProgram; |
| 1341 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCodeUniformBuffer); |
| 1342 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformBuffer); |
| 1343 | QVERIFY(shaderProgram.link()); |
| 1344 | |
| 1345 | GLint positionLocation = m_func->glGetAttribLocation(shaderProgram.programId(), "vertexPosition"); |
| 1346 | GLint texCoordLocation = m_func->glGetAttribLocation(shaderProgram.programId(), "vertexTexCoord"); |
| 1347 | GLint colorIndexLocation = m_func->glGetAttribLocation(shaderProgram.programId(), "vertexColorIndex"); |
| 1348 | |
| 1349 | const int vertexCount = 99; |
| 1350 | QOpenGLBuffer positionBuffer(QOpenGLBuffer::VertexBuffer); |
| 1351 | positionBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw); |
| 1352 | positionBuffer.create(); |
| 1353 | positionBuffer.bind(); |
| 1354 | positionBuffer.allocate(vertexCount * sizeof(QVector3D)); |
| 1355 | |
| 1356 | QOpenGLBuffer texCoordBuffer(QOpenGLBuffer::VertexBuffer); |
| 1357 | texCoordBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw); |
| 1358 | texCoordBuffer.create(); |
| 1359 | texCoordBuffer.allocate(vertexCount * sizeof(QVector2D)); |
| 1360 | |
| 1361 | QOpenGLBuffer colorIndexBuffer(QOpenGLBuffer::VertexBuffer); |
| 1362 | colorIndexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw); |
| 1363 | colorIndexBuffer.create(); |
| 1364 | colorIndexBuffer.allocate(vertexCount * sizeof(int)); |
| 1365 | |
| 1366 | // WHEN |
| 1367 | shaderProgram.bind(); |
| 1368 | positionBuffer.bind(); |
| 1369 | m_glHelper.enableVertexAttributeArray(positionLocation); |
| 1370 | m_glHelper.vertexAttributePointer(GL_FLOAT_VEC3, positionLocation, 3, GL_FLOAT, GL_TRUE, 0, 0); |
| 1371 | |
| 1372 | texCoordBuffer.bind(); |
| 1373 | m_glHelper.enableVertexAttributeArray(texCoordLocation); |
| 1374 | m_glHelper.vertexAttributePointer(GL_FLOAT_VEC2, texCoordLocation, 2, GL_FLOAT, GL_TRUE, 0, 0); |
| 1375 | |
| 1376 | colorIndexBuffer.bind(); |
| 1377 | m_glHelper.enableVertexAttributeArray(colorIndexLocation); |
| 1378 | m_glHelper.vertexAttributePointer(GL_INT, colorIndexLocation, 1, GL_INT, GL_TRUE, 0, 0); |
| 1379 | |
| 1380 | // THEN |
| 1381 | const GLint error = m_func->glGetError(); |
| 1382 | QVERIFY(error == 0); |
| 1383 | } |
| 1384 | |
| 1385 | void glUniform1fv() |
| 1386 | { |
| 1387 | if (!m_initializationSuccessful) |
| 1388 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1389 | |
| 1390 | // GIVEN |
| 1391 | QOpenGLShaderProgram shaderProgram; |
| 1392 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1393 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloat); |
| 1394 | QVERIFY(shaderProgram.link()); |
| 1395 | |
| 1396 | // WHEN |
| 1397 | m_func->glUseProgram(shaderProgram.programId()); |
| 1398 | GLfloat value = 883.0f; |
| 1399 | const GLint location = shaderProgram.uniformLocation("multiplier"); |
| 1400 | m_glHelper.glUniform1fv(location, 1, &value); |
| 1401 | |
| 1402 | // THEN |
| 1403 | GLfloat setValue = 0.0f; |
| 1404 | m_func->glGetUniformfv(shaderProgram.programId(), location, &setValue); |
| 1405 | QCOMPARE(value, setValue); |
| 1406 | |
| 1407 | // Restore |
| 1408 | m_func->glUseProgram(0); |
| 1409 | } |
| 1410 | |
| 1411 | void glUniform2fv() |
| 1412 | { |
| 1413 | if (!m_initializationSuccessful) |
| 1414 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1415 | |
| 1416 | // GIVEN |
| 1417 | QOpenGLShaderProgram shaderProgram; |
| 1418 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1419 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloat); |
| 1420 | QVERIFY(shaderProgram.link()); |
| 1421 | |
| 1422 | // WHEN |
| 1423 | m_func->glUseProgram(shaderProgram.programId()); |
| 1424 | GLfloat values[2] = { 383.0f, 427.0f }; |
| 1425 | const GLint location = shaderProgram.uniformLocation("multiplierVec2"); |
| 1426 | m_glHelper.glUniform2fv(location, 1, values); |
| 1427 | |
| 1428 | // THEN |
| 1429 | GLfloat setValues[2] = { 0.0f, 0.0f }; |
| 1430 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1431 | for (int i = 0; i < 2; ++i) |
| 1432 | QCOMPARE(setValues[i], values[i]); |
| 1433 | |
| 1434 | // Restore |
| 1435 | m_func->glUseProgram(0); |
| 1436 | } |
| 1437 | |
| 1438 | void glUniform3fv() |
| 1439 | { |
| 1440 | if (!m_initializationSuccessful) |
| 1441 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1442 | |
| 1443 | // GIVEN |
| 1444 | QOpenGLShaderProgram shaderProgram; |
| 1445 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1446 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloat); |
| 1447 | QVERIFY(shaderProgram.link()); |
| 1448 | |
| 1449 | // WHEN |
| 1450 | m_func->glUseProgram(shaderProgram.programId()); |
| 1451 | GLfloat values[3] = { 572.0f, 1340.0f, 1584.0f }; |
| 1452 | const GLint location = shaderProgram.uniformLocation("multiplierVec3"); |
| 1453 | m_glHelper.glUniform3fv(location, 1, values); |
| 1454 | |
| 1455 | // THEN |
| 1456 | GLfloat setValues[3] = { 0.0f, 0.0f, 0.0f }; |
| 1457 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1458 | for (int i = 0; i < 3; ++i) |
| 1459 | QCOMPARE(setValues[i], values[i]); |
| 1460 | |
| 1461 | // Restore |
| 1462 | m_func->glUseProgram(0); |
| 1463 | } |
| 1464 | |
| 1465 | void glUniform4fv() |
| 1466 | { |
| 1467 | if (!m_initializationSuccessful) |
| 1468 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1469 | |
| 1470 | // GIVEN |
| 1471 | QOpenGLShaderProgram shaderProgram; |
| 1472 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1473 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloat); |
| 1474 | QVERIFY(shaderProgram.link()); |
| 1475 | |
| 1476 | // WHEN |
| 1477 | m_func->glUseProgram(shaderProgram.programId()); |
| 1478 | GLfloat values[4] = { 454.0f, 350.0f, 883.0f, 355.0f }; |
| 1479 | const GLint location = shaderProgram.uniformLocation("multiplierVec4"); |
| 1480 | m_glHelper.glUniform4fv(location, 1, values); |
| 1481 | |
| 1482 | // THEN |
| 1483 | GLfloat setValues[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1484 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1485 | for (int i = 0; i < 4; ++i) |
| 1486 | QCOMPARE(setValues[i], values[i]); |
| 1487 | |
| 1488 | // Restore |
| 1489 | m_func->glUseProgram(0); |
| 1490 | } |
| 1491 | |
| 1492 | void glUniform1iv() |
| 1493 | { |
| 1494 | if (!m_initializationSuccessful) |
| 1495 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1496 | |
| 1497 | // GIVEN |
| 1498 | QOpenGLShaderProgram shaderProgram; |
| 1499 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1500 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsInt); |
| 1501 | QVERIFY(shaderProgram.link()); |
| 1502 | |
| 1503 | // WHEN |
| 1504 | m_func->glUseProgram(shaderProgram.programId()); |
| 1505 | GLint value = 883; |
| 1506 | const GLint location = shaderProgram.uniformLocation("multiplier"); |
| 1507 | m_glHelper.glUniform1iv(location, 1, &value); |
| 1508 | |
| 1509 | // THEN |
| 1510 | GLint setValue = 0; |
| 1511 | m_func->glGetUniformiv(shaderProgram.programId(), location, &setValue); |
| 1512 | QCOMPARE(value, setValue); |
| 1513 | |
| 1514 | // Restore |
| 1515 | m_func->glUseProgram(0); |
| 1516 | } |
| 1517 | |
| 1518 | void glUniform2iv() |
| 1519 | { |
| 1520 | if (!m_initializationSuccessful) |
| 1521 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1522 | |
| 1523 | // GIVEN |
| 1524 | QOpenGLShaderProgram shaderProgram; |
| 1525 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1526 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsInt); |
| 1527 | QVERIFY(shaderProgram.link()); |
| 1528 | |
| 1529 | // WHEN |
| 1530 | m_func->glUseProgram(shaderProgram.programId()); |
| 1531 | GLint values[2] = { 383, 427 }; |
| 1532 | const GLint location = shaderProgram.uniformLocation("multiplierVec2"); |
| 1533 | m_glHelper.glUniform2iv(location, 1, values); |
| 1534 | |
| 1535 | // THEN |
| 1536 | GLint setValues[2] = { 0, 0 }; |
| 1537 | m_func->glGetUniformiv(shaderProgram.programId(), location, setValues); |
| 1538 | for (int i = 0; i < 2; ++i) |
| 1539 | QCOMPARE(values[i], setValues[i]); |
| 1540 | |
| 1541 | // Restore |
| 1542 | m_func->glUseProgram(0); |
| 1543 | } |
| 1544 | |
| 1545 | void glUniform3iv() |
| 1546 | { |
| 1547 | if (!m_initializationSuccessful) |
| 1548 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1549 | |
| 1550 | // GIVEN |
| 1551 | QOpenGLShaderProgram shaderProgram; |
| 1552 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1553 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsInt); |
| 1554 | QVERIFY(shaderProgram.link()); |
| 1555 | |
| 1556 | // WHEN |
| 1557 | m_func->glUseProgram(shaderProgram.programId()); |
| 1558 | GLint values[3] = { 572, 1340, 1584 }; |
| 1559 | const GLint location = shaderProgram.uniformLocation("multiplierVec3"); |
| 1560 | m_glHelper.glUniform3iv(location, 1, values); |
| 1561 | |
| 1562 | // THEN |
| 1563 | GLint setValues[3] = { 0, 0, 0 }; |
| 1564 | m_func->glGetUniformiv(shaderProgram.programId(), location, setValues); |
| 1565 | for (int i = 0; i < 3; ++i) |
| 1566 | QCOMPARE(values[i], setValues[i]); |
| 1567 | |
| 1568 | // Restore |
| 1569 | m_func->glUseProgram(0); |
| 1570 | } |
| 1571 | |
| 1572 | void glUniform4iv() |
| 1573 | { |
| 1574 | if (!m_initializationSuccessful) |
| 1575 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1576 | |
| 1577 | // GIVEN |
| 1578 | QOpenGLShaderProgram shaderProgram; |
| 1579 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1580 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsInt); |
| 1581 | QVERIFY(shaderProgram.link()); |
| 1582 | |
| 1583 | // WHEN |
| 1584 | m_func->glUseProgram(shaderProgram.programId()); |
| 1585 | GLint values[4] = { 454, 350, 883, 355 }; |
| 1586 | const GLint location = shaderProgram.uniformLocation("multiplierVec4"); |
| 1587 | m_glHelper.glUniform4iv(location, 1, values); |
| 1588 | |
| 1589 | // THEN |
| 1590 | GLint setValues[4] = { 0, 0, 0, 0 }; |
| 1591 | m_func->glGetUniformiv(shaderProgram.programId(), location, setValues); |
| 1592 | for (int i = 0; i < 4; ++i) |
| 1593 | QCOMPARE(values[i], setValues[i]); |
| 1594 | |
| 1595 | // Restore |
| 1596 | m_func->glUseProgram(0); |
| 1597 | } |
| 1598 | |
| 1599 | void glUniform1uiv() |
| 1600 | { |
| 1601 | if (!m_initializationSuccessful) |
| 1602 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1603 | |
| 1604 | // GIVEN |
| 1605 | QOpenGLShaderProgram shaderProgram; |
| 1606 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1607 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsUInt); |
| 1608 | QVERIFY(shaderProgram.link()); |
| 1609 | |
| 1610 | // WHEN |
| 1611 | m_func->glUseProgram(shaderProgram.programId()); |
| 1612 | GLuint value = 883U; |
| 1613 | const GLint location = shaderProgram.uniformLocation("multiplier"); |
| 1614 | m_glHelper.glUniform1uiv(location, 1, &value); |
| 1615 | |
| 1616 | // THEN |
| 1617 | GLuint setValue = 0U; |
| 1618 | m_func->glGetUniformuiv(shaderProgram.programId(), location, &setValue); |
| 1619 | QCOMPARE(value, setValue); |
| 1620 | |
| 1621 | // Restore |
| 1622 | m_func->glUseProgram(0); |
| 1623 | } |
| 1624 | |
| 1625 | void glUniform2uiv() |
| 1626 | { |
| 1627 | if (!m_initializationSuccessful) |
| 1628 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1629 | |
| 1630 | // GIVEN |
| 1631 | QOpenGLShaderProgram shaderProgram; |
| 1632 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1633 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsUInt); |
| 1634 | QVERIFY(shaderProgram.link()); |
| 1635 | |
| 1636 | // WHEN |
| 1637 | m_func->glUseProgram(shaderProgram.programId()); |
| 1638 | GLuint values[2] = { 383U, 427U }; |
| 1639 | const GLint location = shaderProgram.uniformLocation("multiplierVec2"); |
| 1640 | m_glHelper.glUniform2uiv(location, 1, values); |
| 1641 | |
| 1642 | // THEN |
| 1643 | GLuint setValues[2] = { 0U, 0U }; |
| 1644 | m_func->glGetUniformuiv(shaderProgram.programId(), location, setValues); |
| 1645 | for (int i = 0; i < 2; ++i) |
| 1646 | QCOMPARE(values[i], setValues[i]); |
| 1647 | |
| 1648 | // Restore |
| 1649 | m_func->glUseProgram(0); |
| 1650 | } |
| 1651 | |
| 1652 | void glUniform3uiv() |
| 1653 | { |
| 1654 | if (!m_initializationSuccessful) |
| 1655 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1656 | |
| 1657 | // GIVEN |
| 1658 | QOpenGLShaderProgram shaderProgram; |
| 1659 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1660 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsUInt); |
| 1661 | QVERIFY(shaderProgram.link()); |
| 1662 | |
| 1663 | // WHEN |
| 1664 | m_func->glUseProgram(shaderProgram.programId()); |
| 1665 | GLuint values[3] = { 572U, 1340U, 1584U }; |
| 1666 | const GLint location = shaderProgram.uniformLocation("multiplierVec3"); |
| 1667 | m_glHelper.glUniform3uiv(location, 1, values); |
| 1668 | |
| 1669 | // THEN |
| 1670 | GLuint setValues[3] = { 0U, 0U, 0U }; |
| 1671 | m_func->glGetUniformuiv(shaderProgram.programId(), location, setValues); |
| 1672 | for (int i = 0; i < 3; ++i) |
| 1673 | QCOMPARE(values[i], setValues[i]); |
| 1674 | |
| 1675 | // Restore |
| 1676 | m_func->glUseProgram(0); |
| 1677 | } |
| 1678 | |
| 1679 | void glUniform4uiv() |
| 1680 | { |
| 1681 | if (!m_initializationSuccessful) |
| 1682 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1683 | |
| 1684 | // GIVEN |
| 1685 | QOpenGLShaderProgram shaderProgram; |
| 1686 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1687 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsUInt); |
| 1688 | QVERIFY(shaderProgram.link()); |
| 1689 | |
| 1690 | // WHEN |
| 1691 | m_func->glUseProgram(shaderProgram.programId()); |
| 1692 | GLuint values[4] = { 454U, 350U, 883U, 355U }; |
| 1693 | const GLint location = shaderProgram.uniformLocation("multiplierVec4"); |
| 1694 | m_glHelper.glUniform4uiv(location, 1, values); |
| 1695 | |
| 1696 | // THEN |
| 1697 | GLuint setValues[4] = { 0U, 0U, 0U, 0U }; |
| 1698 | m_func->glGetUniformuiv(shaderProgram.programId(), location, setValues); |
| 1699 | for (int i = 0; i < 4; ++i) |
| 1700 | QCOMPARE(values[i], setValues[i]); |
| 1701 | |
| 1702 | // Restore |
| 1703 | m_func->glUseProgram(0); |
| 1704 | } |
| 1705 | |
| 1706 | void glUniformMatrix2fv() |
| 1707 | { |
| 1708 | if (!m_initializationSuccessful) |
| 1709 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1710 | |
| 1711 | // GIVEN |
| 1712 | QOpenGLShaderProgram shaderProgram; |
| 1713 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1714 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloatMatrices); |
| 1715 | QVERIFY(shaderProgram.link()); |
| 1716 | |
| 1717 | // WHEN |
| 1718 | m_func->glUseProgram(shaderProgram.programId()); |
| 1719 | GLfloat values[4] = { 454.0f, 350.0f, 883.0f, 355.0f }; |
| 1720 | const GLint location = shaderProgram.uniformLocation("m2"); |
| 1721 | m_glHelper.glUniformMatrix2fv(location, 1, values); |
| 1722 | |
| 1723 | // THEN |
| 1724 | GLfloat setValues[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1725 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1726 | for (int i = 0; i < 4; ++i) |
| 1727 | QCOMPARE(values[i], setValues[i]); |
| 1728 | |
| 1729 | // Restore |
| 1730 | m_func->glUseProgram(0); |
| 1731 | } |
| 1732 | |
| 1733 | void glUniformMatrix3fv() |
| 1734 | { |
| 1735 | if (!m_initializationSuccessful) |
| 1736 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1737 | |
| 1738 | // GIVEN |
| 1739 | QOpenGLShaderProgram shaderProgram; |
| 1740 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1741 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloatMatrices); |
| 1742 | QVERIFY(shaderProgram.link()); |
| 1743 | |
| 1744 | // WHEN |
| 1745 | m_func->glUseProgram(shaderProgram.programId()); |
| 1746 | GLfloat values[9] = { 454.0f, 350.0f, 883.0f, 355.0f, 1340.0f, 1584.0f, 1200.0f, 427.0f, 396.0f }; |
| 1747 | const GLint location = shaderProgram.uniformLocation("m3"); |
| 1748 | m_glHelper.glUniformMatrix3fv(location, 1, values); |
| 1749 | |
| 1750 | // THEN |
| 1751 | GLfloat setValues[9] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1752 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1753 | for (int i = 0; i < 9; ++i) |
| 1754 | QCOMPARE(values[i], setValues[i]); |
| 1755 | |
| 1756 | // Restore |
| 1757 | m_func->glUseProgram(0); |
| 1758 | } |
| 1759 | |
| 1760 | void glUniformMatrix4fv() |
| 1761 | { |
| 1762 | if (!m_initializationSuccessful) |
| 1763 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1764 | |
| 1765 | // GIVEN |
| 1766 | QOpenGLShaderProgram shaderProgram; |
| 1767 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1768 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloatMatrices); |
| 1769 | QVERIFY(shaderProgram.link()); |
| 1770 | |
| 1771 | // WHEN |
| 1772 | m_func->glUseProgram(shaderProgram.programId()); |
| 1773 | GLfloat values[16] = { 454.0f, 350.0f, 883.0f, 355.0f, 1340.0f, 1584.0f, 1200.0f, 427.0f, 396.0f, 1603.0f, 55.0f, 5.7f, 383.0f, 6.2f, 5.3f, 327.0f }; |
| 1774 | const GLint location = shaderProgram.uniformLocation("m4"); |
| 1775 | m_glHelper.glUniformMatrix4fv(location, 1, values); |
| 1776 | |
| 1777 | // THEN |
| 1778 | GLfloat setValues[16] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1779 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1780 | for (int i = 0; i < 16; ++i) |
| 1781 | QCOMPARE(values[i], setValues[i]); |
| 1782 | |
| 1783 | // Restore |
| 1784 | m_func->glUseProgram(0); |
| 1785 | } |
| 1786 | |
| 1787 | void glUniformMatrix2x3fv() |
| 1788 | { |
| 1789 | if (!m_initializationSuccessful) |
| 1790 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1791 | |
| 1792 | // GIVEN |
| 1793 | QOpenGLShaderProgram shaderProgram; |
| 1794 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1795 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloatMatrices); |
| 1796 | QVERIFY(shaderProgram.link()); |
| 1797 | |
| 1798 | // WHEN |
| 1799 | m_func->glUseProgram(shaderProgram.programId()); |
| 1800 | GLfloat values[6] = { 454.0f, 350.0f, 883.0f, 355.0f, 1340.0f, 1584.0f}; |
| 1801 | const GLint location = shaderProgram.uniformLocation("m23"); |
| 1802 | m_glHelper.glUniformMatrix2x3fv(location, 1, values); |
| 1803 | |
| 1804 | // THEN |
| 1805 | GLfloat setValues[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1806 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1807 | for (int i = 0; i < 6; ++i) |
| 1808 | QCOMPARE(values[i], setValues[i]); |
| 1809 | |
| 1810 | // Restore |
| 1811 | m_func->glUseProgram(0); |
| 1812 | } |
| 1813 | |
| 1814 | void glUniformMatrix3x2fv() |
| 1815 | { |
| 1816 | if (!m_initializationSuccessful) |
| 1817 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1818 | |
| 1819 | // GIVEN |
| 1820 | QOpenGLShaderProgram shaderProgram; |
| 1821 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1822 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloatMatrices); |
| 1823 | QVERIFY(shaderProgram.link()); |
| 1824 | |
| 1825 | // WHEN |
| 1826 | m_func->glUseProgram(shaderProgram.programId()); |
| 1827 | GLfloat values[6] = { 454.0f, 350.0f, 883.0f, 355.0f, 1340.0f, 1584.0f}; |
| 1828 | const GLint location = shaderProgram.uniformLocation("m32"); |
| 1829 | m_glHelper.glUniformMatrix3x2fv(location, 1, values); |
| 1830 | |
| 1831 | // THEN |
| 1832 | GLfloat setValues[6] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1833 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1834 | for (int i = 0; i < 6; ++i) |
| 1835 | QCOMPARE(values[i], setValues[i]); |
| 1836 | |
| 1837 | // Restore |
| 1838 | m_func->glUseProgram(0); |
| 1839 | } |
| 1840 | |
| 1841 | void glUniformMatrix2x4fv() |
| 1842 | { |
| 1843 | if (!m_initializationSuccessful) |
| 1844 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1845 | |
| 1846 | // GIVEN |
| 1847 | QOpenGLShaderProgram shaderProgram; |
| 1848 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1849 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloatMatrices); |
| 1850 | QVERIFY(shaderProgram.link()); |
| 1851 | |
| 1852 | // WHEN |
| 1853 | m_func->glUseProgram(shaderProgram.programId()); |
| 1854 | GLfloat values[8] = { 383.0f, 427.0f, 454.0f, 350.0f, 883.0f, 355.0f, 1340.0f, 1584.0f}; |
| 1855 | const GLint location = shaderProgram.uniformLocation("m24"); |
| 1856 | m_glHelper.glUniformMatrix2x4fv(location, 1, values); |
| 1857 | |
| 1858 | // THEN |
| 1859 | GLfloat setValues[8] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1860 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1861 | for (int i = 0; i < 8; ++i) |
| 1862 | QCOMPARE(values[i], setValues[i]); |
| 1863 | |
| 1864 | // Restore |
| 1865 | m_func->glUseProgram(0); |
| 1866 | } |
| 1867 | |
| 1868 | void glUniformMatrix4x2fv() |
| 1869 | { |
| 1870 | if (!m_initializationSuccessful) |
| 1871 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1872 | |
| 1873 | // GIVEN |
| 1874 | QOpenGLShaderProgram shaderProgram; |
| 1875 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1876 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloatMatrices); |
| 1877 | QVERIFY(shaderProgram.link()); |
| 1878 | |
| 1879 | // WHEN |
| 1880 | m_func->glUseProgram(shaderProgram.programId()); |
| 1881 | GLfloat values[8] = { 383.0f, 427.0f, 454.0f, 350.0f, 883.0f, 355.0f, 1340.0f, 1584.0f}; |
| 1882 | const GLint location = shaderProgram.uniformLocation("m42"); |
| 1883 | m_glHelper.glUniformMatrix4x2fv(location, 1, values); |
| 1884 | |
| 1885 | // THEN |
| 1886 | GLfloat setValues[8] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1887 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1888 | for (int i = 0; i < 8; ++i) |
| 1889 | QCOMPARE(values[i], setValues[i]); |
| 1890 | |
| 1891 | // Restore |
| 1892 | m_func->glUseProgram(0); |
| 1893 | } |
| 1894 | |
| 1895 | void glUniformMatrix3x4fv() |
| 1896 | { |
| 1897 | if (!m_initializationSuccessful) |
| 1898 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1899 | |
| 1900 | // GIVEN |
| 1901 | QOpenGLShaderProgram shaderProgram; |
| 1902 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1903 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloatMatrices); |
| 1904 | QVERIFY(shaderProgram.link()); |
| 1905 | |
| 1906 | // WHEN |
| 1907 | m_func->glUseProgram(shaderProgram.programId()); |
| 1908 | GLfloat values[12] = { 55.0f, 5.7f, 383.0f, 6.2f, 5.3f, 383.0f, 427.0f, 454.0f, 350.0f, 883.0f, 355.0f, 1340.0f}; |
| 1909 | const GLint location = shaderProgram.uniformLocation("m34"); |
| 1910 | m_glHelper.glUniformMatrix3x4fv(location, 1, values); |
| 1911 | |
| 1912 | // THEN |
| 1913 | GLfloat setValues[12] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1914 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1915 | for (int i = 0; i < 12; ++i) |
| 1916 | QCOMPARE(values[i], setValues[i]); |
| 1917 | |
| 1918 | // Restore |
| 1919 | m_func->glUseProgram(0); |
| 1920 | } |
| 1921 | |
| 1922 | void glUniformMatrix4x3fv() |
| 1923 | { |
| 1924 | if (!m_initializationSuccessful) |
| 1925 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1926 | |
| 1927 | // GIVEN |
| 1928 | QOpenGLShaderProgram shaderProgram; |
| 1929 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertCode); |
| 1930 | shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragCodeUniformsFloatMatrices); |
| 1931 | QVERIFY(shaderProgram.link()); |
| 1932 | |
| 1933 | // WHEN |
| 1934 | m_func->glUseProgram(shaderProgram.programId()); |
| 1935 | GLfloat values[12] = { 55.0f, 5.7f, 383.0f, 6.2f, 383.0f, 427.0f, 454.0f, 350.0f, 883.0f, 355.0f, 1340.0f, 1584.0f}; |
| 1936 | const GLint location = shaderProgram.uniformLocation("m43"); |
| 1937 | m_glHelper.glUniformMatrix4x3fv(location, 1, values); |
| 1938 | |
| 1939 | // THEN |
| 1940 | GLfloat setValues[12] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }; |
| 1941 | m_func->glGetUniformfv(shaderProgram.programId(), location, setValues); |
| 1942 | for (int i = 0; i < 12; ++i) |
| 1943 | QCOMPARE(values[i], setValues[i]); |
| 1944 | |
| 1945 | // Restore |
| 1946 | m_func->glUseProgram(0); |
| 1947 | } |
| 1948 | |
| 1949 | void blitFramebuffer() |
| 1950 | { |
| 1951 | if (!m_initializationSuccessful) |
| 1952 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 1953 | |
| 1954 | GLint maxSamples; |
| 1955 | m_func->glGetIntegerv(GL_MAX_SAMPLES, &maxSamples); |
| 1956 | if (maxSamples < 1) |
| 1957 | QSKIP("This test requires an implementation that supports multisampled textures"); |
| 1958 | |
| 1959 | // GIVEN |
| 1960 | GLuint fbos[2]; |
| 1961 | GLuint fboTextures[2]; |
| 1962 | |
| 1963 | m_func->glGenFramebuffers(2, fbos); |
| 1964 | m_func->glGenTextures(2, fboTextures); |
| 1965 | |
| 1966 | m_func->glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, fboTextures[0]); |
| 1967 | m_func->glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, maxSamples, GL_RGBA8, 10, 10, true); |
| 1968 | m_func->glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); |
| 1969 | |
| 1970 | m_func->glBindTexture(GL_TEXTURE_2D, fboTextures[1]); |
| 1971 | m_func->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 10, 10, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr); |
| 1972 | m_func->glBindTexture(GL_TEXTURE_2D, 0); |
| 1973 | |
| 1974 | m_func->glBindFramebuffer(GL_FRAMEBUFFER, fbos[1]); |
| 1975 | m_func->glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, fboTextures[1], 0); |
| 1976 | |
| 1977 | GLenum status = m_func->glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 1978 | QVERIFY(status == GL_FRAMEBUFFER_COMPLETE); |
| 1979 | |
| 1980 | m_func->glBindFramebuffer(GL_FRAMEBUFFER, fbos[0]); |
| 1981 | m_func->glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, fboTextures[0], 0); |
| 1982 | |
| 1983 | status = m_func->glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 1984 | QVERIFY(status == GL_FRAMEBUFFER_COMPLETE); |
| 1985 | |
| 1986 | m_func->glEnable(GL_MULTISAMPLE); |
| 1987 | m_func->glClearColor(0.2f, 0.2f, 0.2f, 0.2f); |
| 1988 | m_func->glClear(GL_COLOR_BUFFER_BIT); |
| 1989 | m_func->glDisable(GL_MULTISAMPLE); |
| 1990 | |
| 1991 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]); |
| 1992 | |
| 1993 | // WHEN |
| 1994 | m_glHelper.blitFramebuffer(0,0,10,10,0,0,10,10, GL_COLOR_BUFFER_BIT, GL_NEAREST); |
| 1995 | |
| 1996 | m_func->glBindFramebuffer(GL_READ_FRAMEBUFFER, fbos[1]); |
| 1997 | |
| 1998 | GLuint result[10*10]; |
| 1999 | m_func->glReadPixels(0,0,10,10,GL_RGBA, GL_UNSIGNED_BYTE, result); |
| 2000 | |
| 2001 | // THEN |
| 2002 | GLuint v = (0.2f) * 255; |
| 2003 | v = v | (v<<8) | (v<<16) | (v<<24); |
| 2004 | for (GLuint value : result) { |
| 2005 | QCOMPARE(value, v); |
| 2006 | } |
| 2007 | m_func->glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); |
| 2008 | m_func->glBindFramebuffer(GL_READ_FRAMEBUFFER, 0); |
| 2009 | |
| 2010 | m_func->glDeleteFramebuffers(2, fbos); |
| 2011 | m_func->glDeleteTextures(2, fboTextures); |
| 2012 | } |
| 2013 | |
| 2014 | #define ADD_GL_TYPE_ENTRY(Type, Expected) \ |
| 2015 | QTest::newRow(#Type) << Type << Expected; |
| 2016 | |
| 2017 | void uniformTypeFromGLType_data() |
| 2018 | { |
| 2019 | QTest::addColumn<int>("glType"); |
| 2020 | QTest::addColumn<UniformType>("expected"); |
| 2021 | |
| 2022 | ADD_GL_TYPE_ENTRY(GL_FLOAT, UniformType::Float); |
| 2023 | ADD_GL_TYPE_ENTRY(GL_FLOAT_VEC2, UniformType::Vec2); |
| 2024 | ADD_GL_TYPE_ENTRY(GL_FLOAT_VEC3, UniformType::Vec3); |
| 2025 | ADD_GL_TYPE_ENTRY(GL_FLOAT_VEC3, UniformType::Vec3); |
| 2026 | ADD_GL_TYPE_ENTRY(GL_FLOAT_VEC2, UniformType::Vec2); |
| 2027 | ADD_GL_TYPE_ENTRY(GL_FLOAT_VEC3, UniformType::Vec3); |
| 2028 | ADD_GL_TYPE_ENTRY(GL_FLOAT_VEC3, UniformType::Vec3); |
| 2029 | ADD_GL_TYPE_ENTRY(GL_INT, UniformType::Int); |
| 2030 | ADD_GL_TYPE_ENTRY(GL_INT_VEC2, UniformType::IVec2); |
| 2031 | ADD_GL_TYPE_ENTRY(GL_INT_VEC3, UniformType::IVec3); |
| 2032 | ADD_GL_TYPE_ENTRY(GL_INT_VEC4, UniformType::IVec4); |
| 2033 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT, UniformType::UInt); |
| 2034 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_VEC2, UniformType::UIVec2); |
| 2035 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_VEC3, UniformType::UIVec3); |
| 2036 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_VEC4, UniformType::UIVec4); |
| 2037 | ADD_GL_TYPE_ENTRY(GL_BOOL, UniformType::Bool); |
| 2038 | ADD_GL_TYPE_ENTRY(GL_BOOL_VEC2, UniformType::BVec2); |
| 2039 | ADD_GL_TYPE_ENTRY(GL_BOOL_VEC3, UniformType::BVec3); |
| 2040 | ADD_GL_TYPE_ENTRY(GL_BOOL_VEC4, UniformType::BVec4); |
| 2041 | ADD_GL_TYPE_ENTRY(GL_FLOAT_MAT2, UniformType::Mat2); |
| 2042 | ADD_GL_TYPE_ENTRY(GL_FLOAT_MAT3, UniformType::Mat3); |
| 2043 | ADD_GL_TYPE_ENTRY(GL_FLOAT_MAT4, UniformType::Mat4); |
| 2044 | ADD_GL_TYPE_ENTRY(GL_FLOAT_MAT2x3, UniformType::Mat2x3); |
| 2045 | ADD_GL_TYPE_ENTRY(GL_FLOAT_MAT2x4, UniformType::Mat2x4); |
| 2046 | ADD_GL_TYPE_ENTRY(GL_FLOAT_MAT3x2, UniformType::Mat3x2); |
| 2047 | ADD_GL_TYPE_ENTRY(GL_FLOAT_MAT4x2, UniformType::Mat4x2); |
| 2048 | ADD_GL_TYPE_ENTRY(GL_FLOAT_MAT4x3, UniformType::Mat4x3); |
| 2049 | ADD_GL_TYPE_ENTRY(GL_FLOAT_MAT3x4, UniformType::Mat3x4); |
| 2050 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_1D, UniformType::Sampler); |
| 2051 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_1D_ARRAY, UniformType::Sampler); |
| 2052 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_1D_SHADOW, UniformType::Sampler); |
| 2053 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_2D, UniformType::Sampler); |
| 2054 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_2D_ARRAY, UniformType::Sampler); |
| 2055 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_2D_RECT, UniformType::Sampler); |
| 2056 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_2D_MULTISAMPLE, UniformType::Sampler); |
| 2057 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_2D_MULTISAMPLE_ARRAY, UniformType::Sampler); |
| 2058 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_2D_SHADOW, UniformType::Sampler); |
| 2059 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_2D_ARRAY_SHADOW, UniformType::Sampler); |
| 2060 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_3D, UniformType::Sampler); |
| 2061 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_CUBE, UniformType::Sampler); |
| 2062 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_CUBE_SHADOW, UniformType::Sampler); |
| 2063 | ADD_GL_TYPE_ENTRY(GL_SAMPLER_BUFFER, UniformType::Sampler); |
| 2064 | ADD_GL_TYPE_ENTRY(GL_INT_SAMPLER_1D, UniformType::Sampler); |
| 2065 | ADD_GL_TYPE_ENTRY(GL_INT_SAMPLER_2D, UniformType::Sampler); |
| 2066 | ADD_GL_TYPE_ENTRY(GL_INT_SAMPLER_3D, UniformType::Sampler); |
| 2067 | ADD_GL_TYPE_ENTRY(GL_INT_SAMPLER_BUFFER, UniformType::Sampler); |
| 2068 | ADD_GL_TYPE_ENTRY(GL_INT_SAMPLER_2D_ARRAY, UniformType::Sampler); |
| 2069 | ADD_GL_TYPE_ENTRY(GL_INT_SAMPLER_2D_MULTISAMPLE, UniformType::Sampler); |
| 2070 | ADD_GL_TYPE_ENTRY(GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY, UniformType::Sampler); |
| 2071 | ADD_GL_TYPE_ENTRY(GL_INT_SAMPLER_CUBE, UniformType::Sampler); |
| 2072 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_SAMPLER_1D, UniformType::Sampler); |
| 2073 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_SAMPLER_2D, UniformType::Sampler); |
| 2074 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_SAMPLER_3D, UniformType::Sampler); |
| 2075 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_SAMPLER_BUFFER, UniformType::Sampler); |
| 2076 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_SAMPLER_2D_ARRAY, UniformType::Sampler); |
| 2077 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE, UniformType::Sampler); |
| 2078 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY, UniformType::Sampler); |
| 2079 | ADD_GL_TYPE_ENTRY(GL_UNSIGNED_INT_SAMPLER_CUBE, UniformType::Sampler); |
| 2080 | } |
| 2081 | |
| 2082 | void uniformTypeFromGLType() |
| 2083 | { |
| 2084 | // GIVEN |
| 2085 | QFETCH(int, glType); |
| 2086 | QFETCH(UniformType, expected); |
| 2087 | |
| 2088 | // WHEN |
| 2089 | UniformType computed = m_glHelper.uniformTypeFromGLType(glType); |
| 2090 | |
| 2091 | // THEN |
| 2092 | QCOMPARE(computed, expected); |
| 2093 | } |
| 2094 | |
| 2095 | void drawBuffer() |
| 2096 | { |
| 2097 | if (!m_initializationSuccessful) |
| 2098 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 2099 | |
| 2100 | m_func->glGetError(); |
| 2101 | |
| 2102 | // WHEN |
| 2103 | m_glHelper.drawBuffer(GL_FRONT); |
| 2104 | const GLint error = m_func->glGetError(); |
| 2105 | QVERIFY(error == 0); |
| 2106 | |
| 2107 | // THEN |
| 2108 | GLint p; |
| 2109 | m_func->glGetIntegerv(GL_DRAW_BUFFER, &p); |
| 2110 | QCOMPARE(p, GL_FRONT); |
| 2111 | } |
| 2112 | |
| 2113 | void readBuffer() |
| 2114 | { |
| 2115 | if (!m_initializationSuccessful) |
| 2116 | QSKIP("Initialization failed, OpenGL 3.2 Core functions not supported"); |
| 2117 | |
| 2118 | m_func->glGetError(); |
| 2119 | |
| 2120 | // WHEN |
| 2121 | m_glHelper.readBuffer(GL_FRONT); |
| 2122 | |
| 2123 | // THEN |
| 2124 | const GLint error = m_func->glGetError(); |
| 2125 | QVERIFY(error == 0); |
| 2126 | GLint p; |
| 2127 | m_func->glGetIntegerv(GL_READ_BUFFER, &p); |
| 2128 | QCOMPARE(p, GL_FRONT); |
| 2129 | } |
| 2130 | |
| 2131 | void fenceSync() |
| 2132 | { |
| 2133 | if (!m_initializationSuccessful) |
| 2134 | QSKIP("Initialization failed, OpenGL 4.3 Core functions not supported"); |
| 2135 | |
| 2136 | m_func->glGetError(); |
| 2137 | |
| 2138 | // WHEN |
| 2139 | GLsync sync = reinterpret_cast<GLsync>(m_glHelper.fenceSync()); |
| 2140 | |
| 2141 | // THEN |
| 2142 | QVERIFY(sync != nullptr); |
| 2143 | QCOMPARE(m_func->glIsSync(sync), GL_TRUE); |
| 2144 | const GLint error = m_func->glGetError(); |
| 2145 | QVERIFY(error == 0); |
| 2146 | } |
| 2147 | |
| 2148 | void clientWaitSync() |
| 2149 | { |
| 2150 | if (!m_initializationSuccessful) |
| 2151 | QSKIP("Initialization failed, OpenGL 4.3 Core functions not supported"); |
| 2152 | |
| 2153 | m_func->glGetError(); |
| 2154 | |
| 2155 | // WHEN |
| 2156 | QElapsedTimer t; |
| 2157 | t.start(); |
| 2158 | |
| 2159 | GLsync sync = reinterpret_cast<GLsync>(m_glHelper.fenceSync()); |
| 2160 | |
| 2161 | m_glHelper.clientWaitSync(sync, 1000000); |
| 2162 | |
| 2163 | // THEN |
| 2164 | const GLint error = m_func->glGetError(); |
| 2165 | QVERIFY(error == 0); |
| 2166 | qDebug() << t.nsecsElapsed(); |
| 2167 | } |
| 2168 | |
| 2169 | void waitSync() |
| 2170 | { |
| 2171 | if (!m_initializationSuccessful) |
| 2172 | QSKIP("Initialization failed, OpenGL 4.3 Core functions not supported"); |
| 2173 | |
| 2174 | m_func->glGetError(); |
| 2175 | |
| 2176 | // WHEN |
| 2177 | GLsync sync = reinterpret_cast<GLsync>(m_glHelper.fenceSync()); |
| 2178 | m_func->glFlush(); |
| 2179 | m_glHelper.waitSync(sync); |
| 2180 | |
| 2181 | // THEN |
| 2182 | const GLint error = m_func->glGetError(); |
| 2183 | QVERIFY(error == 0); |
| 2184 | } |
| 2185 | |
| 2186 | void wasSyncSignaled() |
| 2187 | { |
| 2188 | if (!m_initializationSuccessful) |
| 2189 | QSKIP("Initialization failed, OpenGL 4.3 Core functions not supported"); |
| 2190 | |
| 2191 | m_func->glGetError(); |
| 2192 | |
| 2193 | // WHEN |
| 2194 | GLsync sync = reinterpret_cast<GLsync>(m_glHelper.fenceSync()); |
| 2195 | m_func->glFlush(); |
| 2196 | m_glHelper.waitSync(sync); |
| 2197 | |
| 2198 | // THEN |
| 2199 | const GLint error = m_func->glGetError(); |
| 2200 | QVERIFY(error == 0); |
| 2201 | |
| 2202 | // Shouldn't loop forever |
| 2203 | while (!m_glHelper.wasSyncSignaled(sync)) |
| 2204 | ; |
| 2205 | } |
| 2206 | |
| 2207 | void deleteSync() |
| 2208 | { |
| 2209 | if (!m_initializationSuccessful) |
| 2210 | QSKIP("Initialization failed, OpenGL 4.3 Core functions not supported"); |
| 2211 | |
| 2212 | m_func->glGetError(); |
| 2213 | |
| 2214 | // WHEN |
| 2215 | GLsync sync = reinterpret_cast<GLsync>(m_glHelper.fenceSync()); |
| 2216 | m_glHelper.clientWaitSync(sync, GLuint64(-1)); |
| 2217 | |
| 2218 | // THEN |
| 2219 | const GLint error = m_func->glGetError(); |
| 2220 | QVERIFY(error == 0); |
| 2221 | QVERIFY(m_glHelper.wasSyncSignaled(sync) == true); |
| 2222 | |
| 2223 | // WHEN |
| 2224 | m_glHelper.deleteSync(sync); |
| 2225 | |
| 2226 | // THEN |
| 2227 | QCOMPARE(m_func->glIsSync(sync), GL_FALSE); |
| 2228 | } |
| 2229 | |
| 2230 | void rasterMode() |
| 2231 | { |
| 2232 | if (!m_initializationSuccessful) |
| 2233 | QSKIP("Initialization failed, OpenGL 3.2 functions not supported"); |
| 2234 | |
| 2235 | m_func->glGetError(); |
| 2236 | |
| 2237 | m_glHelper.rasterMode(GL_FRONT_AND_BACK, GL_LINE); |
| 2238 | |
| 2239 | // THEN |
| 2240 | const GLint error = m_func->glGetError(); |
| 2241 | QVERIFY(error == 0); |
| 2242 | GLint p; |
| 2243 | m_func->glGetIntegerv(GL_POLYGON_MODE, &p); |
| 2244 | QCOMPARE(p, GL_LINE); |
| 2245 | } |
| 2246 | |
| 2247 | private: |
| 2248 | QScopedPointer<QWindow> m_window; |
| 2249 | QOpenGLContext m_glContext; |
| 2250 | GraphicsHelperGL3_2 m_glHelper; |
| 2251 | QOpenGLFunctions_3_2_Core *m_func = nullptr; |
| 2252 | bool m_initializationSuccessful = false; |
| 2253 | }; |
| 2254 | |
| 2255 | #endif |
| 2256 | |
| 2257 | int main(int argc, char *argv[]) |
| 2258 | { |
| 2259 | #ifdef TEST_SHOULD_BE_PERFORMED |
| 2260 | QGuiApplication app(argc, argv); |
| 2261 | app.setAttribute(Qt::AA_Use96Dpi, true); |
| 2262 | tst_GraphicsHelperGL3_2 tc; |
| 2263 | QTEST_SET_MAIN_SOURCE_PATH |
| 2264 | return QTest::qExec(&tc, argc, argv); |
| 2265 | #endif |
| 2266 | return 0; |
| 2267 | } |
| 2268 | |
| 2269 | #ifdef TEST_SHOULD_BE_PERFORMED |
| 2270 | #include "tst_graphicshelpergl3_2.moc" |
| 2271 | #endif |