1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.0 Module
3 * -------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Pixel Buffer Object tests
22 *//*--------------------------------------------------------------------*/
24 #include "es3fPixelBufferObjectTests.hpp"
26 #include "tcuTexture.hpp"
27 #include "tcuTextureUtil.hpp"
28 #include "tcuImageCompare.hpp"
29 #include "tcuTestLog.hpp"
30 #include "tcuRenderTarget.hpp"
32 #include "gluTextureUtil.hpp"
33 #include "gluPixelTransfer.hpp"
34 #include "gluShaderProgram.hpp"
36 #include "deRandom.hpp"
45 using std::stringstream;
57 class ReadPixelsTest : public TestCase
64 FRAMEBUFFERTYPE_NATIVE = 0,
65 FRAMEBUFFERTYPE_RENDERBUFFER
74 FramebufferType framebufferType;
75 GLenum renderbufferFormat;
78 ReadPixelsTest (Context& context, const TestSpec& spec);
79 ~ReadPixelsTest (void);
84 IterateResult iterate (void);
87 void renderTriangle (const tcu::Vec3& a, const tcu::Vec3& b, const tcu::Vec3& c);
88 void clearColor (float r, float g, float b, float a);
92 glu::ShaderProgram* m_program;
94 TestSpec::FramebufferType m_framebuffeType;
96 GLenum m_renderbufferFormat;
97 tcu::TextureChannelClass m_texChannelClass;
99 bool m_useColorClears;
100 bool m_renderTriangles;
102 GLfloat m_colorScale;
106 ReadPixelsTest::ReadPixelsTest (Context& context, const TestSpec& spec)
107 : TestCase (context, spec.name.c_str(), spec.description.c_str())
108 , m_random (deStringHash(spec.name.c_str()))
109 , m_log (m_testCtx.getLog())
111 , m_framebuffeType (spec.framebufferType)
112 , m_renderbufferFormat (spec.renderbufferFormat)
113 , m_texChannelClass (tcu::TEXTURECHANNELCLASS_LAST)
114 , m_useColorClears (spec.useColorClear)
115 , m_renderTriangles (spec.renderTriangles)
116 , m_colorScale (1.0f)
119 if (m_framebuffeType == TestSpec::FRAMEBUFFERTYPE_NATIVE)
123 else if (m_framebuffeType == TestSpec::FRAMEBUFFERTYPE_RENDERBUFFER)
125 m_texChannelClass = tcu::getTextureChannelClass(glu::mapGLInternalFormat(spec.renderbufferFormat).type);
126 switch (m_texChannelClass)
128 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
132 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
133 m_colorScale = 100.0f;
136 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
137 m_colorScale = 100.0f;
140 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
141 m_colorScale = 100.0f;
152 ReadPixelsTest::~ReadPixelsTest (void)
156 void ReadPixelsTest::init (void)
159 if (m_framebuffeType == TestSpec::FRAMEBUFFERTYPE_RENDERBUFFER)
161 bool supported = false;
163 if (m_renderbufferFormat == GL_RGBA16F
164 || m_renderbufferFormat == GL_RG16F)
166 std::istringstream extensions(std::string((const char*)glGetString(GL_EXTENSIONS)));
167 std::string extension;
169 while (std::getline(extensions, extension, ' '))
171 if (extension=="GL_EXT_color_buffer_half_float")
176 if (extension=="GL_EXT_color_buffer_float")
183 else if (m_renderbufferFormat == GL_RGBA32F
184 || m_renderbufferFormat == GL_RG32F
185 || m_renderbufferFormat == GL_R11F_G11F_B10F)
187 std::istringstream extensions(std::string((const char*)glGetString(GL_EXTENSIONS)));
188 std::string extension;
190 while (std::getline(extensions, extension, ' '))
192 if (extension=="GL_EXT_color_buffer_float")
203 throw tcu::NotSupportedError("Renderbuffer format not supported", "", __FILE__, __LINE__);
206 std::string outtype = "";
208 if (m_framebuffeType == TestSpec::FRAMEBUFFERTYPE_NATIVE)
210 else if (m_framebuffeType == TestSpec::FRAMEBUFFERTYPE_RENDERBUFFER)
212 switch (m_texChannelClass)
214 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
218 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
222 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
226 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
238 const char* vertexShaderSource =
240 "in mediump vec3 a_position;\n"
241 "in mediump vec4 a_color;\n"
242 "uniform mediump float u_colorScale;\n"
243 "out mediump vec4 v_color;\n"
246 "\tgl_Position = vec4(a_position, 1.0);\n"
247 "\tv_color = u_colorScale * a_color;\n"
250 stringstream fragmentShaderSource;
252 fragmentShaderSource <<
254 "in mediump vec4 v_color;\n";
257 fragmentShaderSource << "layout (location = 0) out mediump " << outtype << " o_color;\n"
260 "\to_color = " << outtype << "(v_color);\n"
263 m_program = new glu::ShaderProgram(m_context.getRenderContext(), glu::makeVtxFragSources(vertexShaderSource, fragmentShaderSource.str()));
265 if (!m_program->isOk())
268 TCU_FAIL("Failed to compile shader");
272 void ReadPixelsTest::deinit (void)
279 void ReadPixelsTest::renderTriangle (const tcu::Vec3& a, const tcu::Vec3& b, const tcu::Vec3& c)
281 float positions[3*3];
283 positions[0] = a.x();
284 positions[1] = a.y();
285 positions[2] = a.z();
287 positions[3] = b.x();
288 positions[4] = b.y();
289 positions[5] = b.z();
291 positions[6] = c.x();
292 positions[7] = c.y();
293 positions[8] = c.z();
296 1.0f, 0.0f, 0.0f, 1.0f,
297 0.0f, 1.0f, 0.0f, 1.0f,
298 0.0f, 0.0f, 1.0f, 1.0f
301 GLU_CHECK_CALL(glUseProgram(m_program->getProgram()));
303 GLuint coordLoc = (GLuint)-1;
304 GLuint colorLoc = (GLuint)-1;
305 GLuint colorScaleLoc = (GLuint)-1;
307 colorScaleLoc = glGetUniformLocation(m_program->getProgram(), "u_colorScale");
308 TCU_CHECK(colorScaleLoc != (GLuint)-1);
310 GLU_CHECK_CALL(glUniform1f(colorScaleLoc, m_colorScale));
312 coordLoc = glGetAttribLocation(m_program->getProgram(), "a_position");
313 TCU_CHECK(coordLoc != (GLuint)-1);
315 colorLoc = glGetAttribLocation(m_program->getProgram(), "a_color");
316 TCU_CHECK(colorLoc != (GLuint)-1);
318 GLU_CHECK_CALL(glEnableVertexAttribArray(colorLoc));
319 GLU_CHECK_CALL(glEnableVertexAttribArray(coordLoc));
321 GLU_CHECK_CALL(glVertexAttribPointer(coordLoc, 3, GL_FLOAT, GL_FALSE, 0, positions));
322 GLU_CHECK_CALL(glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 0, colors));
324 GLU_CHECK_CALL(glDrawArrays(GL_TRIANGLES, 0, 3));
326 GLU_CHECK_CALL(glDisableVertexAttribArray(colorLoc));
327 GLU_CHECK_CALL(glDisableVertexAttribArray(coordLoc));
332 void ReadPixelsTest::clearColor (float r, float g, float b, float a)
334 if (m_framebuffeType == TestSpec::FRAMEBUFFERTYPE_NATIVE)
336 GLU_CHECK_CALL(glClearColor(r, g, b, a));
337 GLU_CHECK_CALL(glClear(GL_COLOR_BUFFER_BIT));
339 else if (m_framebuffeType == TestSpec::FRAMEBUFFERTYPE_RENDERBUFFER)
341 switch (m_texChannelClass)
343 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
345 GLU_CHECK_CALL(glClearColor(r, g, b, a));
346 GLU_CHECK_CALL(glClear(GL_COLOR_BUFFER_BIT));
350 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
352 GLint color[4] = { (GLint)r, (GLint)g, (GLint)b, (GLint)a };
354 GLU_CHECK_CALL(glClearBufferiv(GL_COLOR, 0, color));
358 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
360 GLuint color[4] = { (GLuint)r, (GLuint)g, (GLuint)b, (GLuint)a };
362 GLU_CHECK_CALL(glClearBufferuiv(GL_COLOR, 0, color));
366 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
368 GLfloat color[4] = { (GLfloat)r, (GLfloat)g, (GLfloat)b, (GLfloat)a };
370 GLU_CHECK_CALL(glClearBufferfv(GL_COLOR, 0, color));
383 TestCase::IterateResult ReadPixelsTest::iterate(void)
385 int width = m_context.getRenderTarget().getWidth();
386 int height = m_context.getRenderTarget().getHeight();
388 GLuint framebuffer = 0;
389 GLuint renderbuffer = 0;
391 switch (m_framebuffeType)
393 case TestSpec::FRAMEBUFFERTYPE_NATIVE:
394 GLU_CHECK_CALL(glBindFramebuffer(GL_FRAMEBUFFER, framebuffer));
397 case TestSpec::FRAMEBUFFERTYPE_RENDERBUFFER:
399 GLU_CHECK_CALL(glGenFramebuffers(1, &framebuffer));
400 GLU_CHECK_CALL(glGenRenderbuffers(1, &renderbuffer));
402 GLU_CHECK_CALL(glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer));
403 GLU_CHECK_CALL(glRenderbufferStorage(GL_RENDERBUFFER, m_renderbufferFormat, width, height));
405 GLU_CHECK_CALL(glBindFramebuffer(GL_FRAMEBUFFER, framebuffer));
406 GLU_CHECK_CALL(glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer));
415 clearColor(m_colorScale * 0.4f, m_colorScale * 1.0f, m_colorScale * 0.5f, m_colorScale * 1.0f);
417 if (m_useColorClears)
419 const int maxClearCount = 10;
420 const int minClearCount = 6;
421 const int minClearSize = 15;
423 int clearCount = m_random.getInt(minClearCount, maxClearCount);
425 for (int clearNdx = 0; clearNdx < clearCount; clearNdx++)
427 int clearX = m_random.getInt(0, width - minClearSize);
428 int clearY = m_random.getInt(0, height - minClearSize);
430 int clearWidth = m_random.getInt(minClearSize, width - clearX);
431 int clearHeight = m_random.getInt(minClearSize, height - clearY);
433 float clearRed = m_colorScale * m_random.getFloat();
434 float clearGreen = m_colorScale * m_random.getFloat();
435 float clearBlue = m_colorScale * m_random.getFloat();
436 float clearAlpha = m_colorScale * (0.5f + 0.5f * m_random.getFloat());
438 GLU_CHECK_CALL(glEnable(GL_SCISSOR_TEST));
439 GLU_CHECK_CALL(glScissor(clearX, clearY, clearWidth, clearHeight));
441 clearColor(clearRed, clearGreen, clearBlue, clearAlpha);
444 GLU_CHECK_CALL(glDisable(GL_SCISSOR_TEST));
447 if (m_renderTriangles)
449 const int minTriangleCount = 4;
450 const int maxTriangleCount = 10;
452 int triangleCount = m_random.getInt(minTriangleCount, maxTriangleCount);
454 for (int triangleNdx = 0; triangleNdx < triangleCount; triangleNdx++)
456 float x1 = 2.0f * m_random.getFloat() - 1.0f;
457 float y1 = 2.0f * m_random.getFloat() - 1.0f;
458 float z1 = 2.0f * m_random.getFloat() - 1.0f;
460 float x2 = 2.0f * m_random.getFloat() - 1.0f;
461 float y2 = 2.0f * m_random.getFloat() - 1.0f;
462 float z2 = 2.0f * m_random.getFloat() - 1.0f;
464 float x3 = 2.0f * m_random.getFloat() - 1.0f;
465 float y3 = 2.0f * m_random.getFloat() - 1.0f;
466 float z3 = 2.0f * m_random.getFloat() - 1.0f;
468 renderTriangle(tcu::Vec3(x1, y1, z1), tcu::Vec3(x2, y2, z2), tcu::Vec3(x3, y3, z3));
472 tcu::TextureFormat readFormat;
473 GLenum readPixelsFormat;
474 GLenum readPixelsType;
478 if (m_framebuffeType == TestSpec::FRAMEBUFFERTYPE_NATIVE)
480 readFormat = glu::mapGLTransferFormat(GL_RGBA, GL_UNSIGNED_BYTE);
481 readPixelsFormat = GL_RGBA;
482 readPixelsType = GL_UNSIGNED_BYTE;
483 floatCompare = false;
485 else if (m_framebuffeType == TestSpec::FRAMEBUFFERTYPE_RENDERBUFFER)
487 switch (m_texChannelClass)
489 case tcu::TEXTURECHANNELCLASS_UNSIGNED_FIXED_POINT:
490 readFormat = glu::mapGLTransferFormat(GL_RGBA, GL_UNSIGNED_BYTE);
491 readPixelsFormat = GL_RGBA;
492 readPixelsType = GL_UNSIGNED_BYTE;
496 case tcu::TEXTURECHANNELCLASS_SIGNED_INTEGER:
497 readFormat = glu::mapGLTransferFormat(GL_RGBA_INTEGER, GL_INT);
498 readPixelsFormat = GL_RGBA_INTEGER;
499 readPixelsType = GL_INT;
500 floatCompare = false;
503 case tcu::TEXTURECHANNELCLASS_UNSIGNED_INTEGER:
504 readFormat = glu::mapGLTransferFormat(GL_RGBA_INTEGER, GL_UNSIGNED_INT);
505 readPixelsFormat = GL_RGBA_INTEGER;
506 readPixelsType = GL_UNSIGNED_INT;
507 floatCompare = false;
510 case tcu::TEXTURECHANNELCLASS_FLOATING_POINT:
511 readFormat = glu::mapGLTransferFormat(GL_RGBA, GL_FLOAT);
512 readPixelsFormat = GL_RGBA;
513 readPixelsType = GL_FLOAT;
520 readFormat = glu::mapGLTransferFormat(GL_RGBA, GL_FLOAT);
521 readPixelsFormat = GL_RGBA;
522 readPixelsType = GL_FLOAT;
529 readFormat = glu::mapGLTransferFormat(GL_RGBA, GL_FLOAT);
530 readPixelsFormat = GL_RGBA;
531 readPixelsType = GL_FLOAT;
536 tcu::Texture2D readRefrence (readFormat, width, height);
537 const int readDataSize = readRefrence.getWidth() * readRefrence.getHeight() * readFormat.getPixelSize();
539 readRefrence.allocLevel(0);
541 GLuint pixelBuffer = (GLuint)-1;
543 GLU_CHECK_CALL(glGenBuffers(1, &pixelBuffer));
544 GLU_CHECK_CALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, pixelBuffer));
545 GLU_CHECK_CALL(glBufferData(GL_PIXEL_PACK_BUFFER, readDataSize, NULL, GL_STREAM_READ));
547 GLU_CHECK_CALL(glReadPixels(0, 0, width, height, readPixelsFormat, readPixelsType, 0));
549 const deUint8* bufferData = (const deUint8*)glMapBufferRange(GL_PIXEL_PACK_BUFFER, 0, readDataSize, GL_MAP_READ_BIT);
550 GLU_CHECK_MSG("glMapBufferRange() failed");
552 tcu::ConstPixelBufferAccess readResult(readFormat, width, height, 1, bufferData);
554 GLU_CHECK_CALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
556 GLU_CHECK_CALL(glReadPixels(0, 0, width, height, readPixelsFormat, readPixelsType, readRefrence.getLevel(0).getDataPtr()));
559 GLU_CHECK_CALL(glDeleteFramebuffers(1, &framebuffer));
562 GLU_CHECK_CALL(glDeleteRenderbuffers(1, &renderbuffer));
568 isOk = tcu::floatThresholdCompare(m_log, "Result comparision", "Result of read pixels to memory compared with result of read pixels to buffer", readRefrence.getLevel(0), readResult, tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f), tcu::COMPARE_LOG_RESULT);
570 isOk = tcu::intThresholdCompare(m_log, "Result comparision", "Result of read pixels to memory compared with result of read pixels to buffer", readRefrence.getLevel(0), readResult, tcu::UVec4(0, 0, 0, 0), tcu::COMPARE_LOG_RESULT);
572 GLU_CHECK_CALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, pixelBuffer));
573 GLU_CHECK_CALL(glUnmapBuffer(GL_PIXEL_PACK_BUFFER));
574 GLU_CHECK_CALL(glDeleteBuffers(1, &pixelBuffer));
578 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
583 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
590 PixelBufferObjectTests::PixelBufferObjectTests (Context& context)
591 : TestCaseGroup (context, "pbo", "Pixel buffer objects tests")
595 PixelBufferObjectTests::~PixelBufferObjectTests (void)
599 void PixelBufferObjectTests::init (void)
601 TestCaseGroup* nativeFramebufferGroup = new TestCaseGroup(m_context, "native", "Tests with reading from native framebuffer");
603 ReadPixelsTest::TestSpec nativeFramebufferTests[] = {
606 "Simple read pixels test with color clears",
609 ReadPixelsTest::TestSpec::FRAMEBUFFERTYPE_NATIVE,
614 "Simple read pixels test rendering triangles",
617 ReadPixelsTest::TestSpec::FRAMEBUFFERTYPE_NATIVE,
622 for (int testNdx = 0; testNdx < DE_LENGTH_OF_ARRAY(nativeFramebufferTests); testNdx++)
624 nativeFramebufferGroup->addChild(new ReadPixelsTest(m_context, nativeFramebufferTests[testNdx]));
627 addChild(nativeFramebufferGroup);
629 TestCaseGroup* renderbufferGroup = new TestCaseGroup(m_context, "renderbuffer", "Tests with reading from renderbuffer");
631 GLenum renderbufferFormats[] = {
664 const char* renderbufferFormatsStr[] = {
697 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(renderbufferFormatsStr) == DE_LENGTH_OF_ARRAY(renderbufferFormats));
699 for (int formatNdx = 0; formatNdx < DE_LENGTH_OF_ARRAY(renderbufferFormats); formatNdx++)
701 for (int trianglesClears = 0; trianglesClears < 2; trianglesClears++)
703 ReadPixelsTest::TestSpec testSpec;
705 testSpec.name = string(renderbufferFormatsStr [formatNdx]) + "_" + (trianglesClears == 0 ? "triangles" : "clears"),
706 testSpec.description = testSpec.name;
707 testSpec.useColorClear = trianglesClears == 1,
708 testSpec.renderTriangles = trianglesClears == 0,
709 testSpec.framebufferType = ReadPixelsTest::TestSpec::FRAMEBUFFERTYPE_RENDERBUFFER,
710 testSpec.renderbufferFormat = renderbufferFormats[formatNdx];
712 renderbufferGroup->addChild(new ReadPixelsTest(m_context, testSpec));
716 addChild(renderbufferGroup);