1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 2.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 Read pixels tests
22 *//*--------------------------------------------------------------------*/
24 #include "es2fReadPixelsTests.hpp"
26 #include "tcuTexture.hpp"
27 #include "tcuTextureUtil.hpp"
28 #include "tcuImageCompare.hpp"
29 #include "tcuTestLog.hpp"
30 #include "tcuRenderTarget.hpp"
32 #include "deRandom.hpp"
35 #include "deStringUtil.hpp"
37 #include "gluDefs.hpp"
38 #include "gluShaderProgram.hpp"
39 #include "gluStrUtil.hpp"
40 #include "gluTextureUtil.hpp"
53 class ReadPixelsTest : public TestCase
56 ReadPixelsTest (Context& context, const char* name, const char* description, bool chooseFormat, int alignment);
58 IterateResult iterate (void);
59 void render (tcu::Texture2D& reference);
66 void getFormatInfo (tcu::TextureFormat& format, GLint& glFormat, GLint& glType, int& pixelSize);
69 ReadPixelsTest::ReadPixelsTest (Context& context, const char* name, const char* description, bool chooseFormat, int alignment)
70 : TestCase (context, name, description)
71 , m_chooseFormat (chooseFormat)
72 , m_alignment (alignment)
73 , m_seed (deStringHash(name))
77 void ReadPixelsTest::render (tcu::Texture2D& reference)
80 const char* vertexSource =
81 "attribute mediump vec2 a_coord;\n"
84 "\tgl_Position = vec4(a_coord, 0.0, 1.0);\n"
87 const char* fragmentSource =
90 "\tgl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
93 glu::ShaderProgram program(m_context.getRenderContext(), glu::makeVtxFragSources(vertexSource, fragmentSource));
95 m_testCtx.getLog() << program;
96 TCU_CHECK(program.isOk());
97 GLU_CHECK_CALL(glUseProgram(program.getProgram()));
101 const float coords[] =
113 coordLoc = glGetAttribLocation(program.getProgram(), "a_coord");
114 GLU_CHECK_MSG("glGetAttribLocation()");
116 GLU_CHECK_CALL(glEnableVertexAttribArray(coordLoc));
118 GLU_CHECK_CALL(glVertexAttribPointer(coordLoc, 2, GL_FLOAT, GL_FALSE, 0, coords));
120 GLU_CHECK_CALL(glDrawArrays(GL_TRIANGLES, 0, 6));
121 GLU_CHECK_CALL(glDisableVertexAttribArray(coordLoc));
126 const int coordX1 = (int)((-0.5f * (float)reference.getWidth() / 2.0f) + (float)reference.getWidth() / 2.0f);
127 const int coordY1 = (int)((-0.5f * (float)reference.getHeight() / 2.0f) + (float)reference.getHeight() / 2.0f);
128 const int coordX2 = (int)(( 0.5f * (float)reference.getWidth() / 2.0f) + (float)reference.getWidth() / 2.0f);
129 const int coordY2 = (int)(( 0.5f * (float)reference.getHeight() / 2.0f) + (float)reference.getHeight() / 2.0f);
131 for (int x = 0; x < reference.getWidth(); x++)
133 if (x < coordX1 || x > coordX2)
136 for (int y = 0; y < reference.getHeight(); y++)
138 if (y >= coordY1 && y <= coordY2)
139 reference.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), x, y);
144 void ReadPixelsTest::getFormatInfo (tcu::TextureFormat& format, GLint& glFormat, GLint& glType, int& pixelSize)
148 GLU_CHECK_CALL(glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &glFormat));
149 GLU_CHECK_CALL(glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &glType));
151 if (glFormat != GL_RGBA && glFormat != GL_BGRA && glFormat != GL_RGB)
152 TCU_THROW(NotSupportedError, ("Unsupported IMPLEMENTATION_COLOR_READ_FORMAT: " + de::toString(glu::getTextureFormatStr(glFormat))).c_str());
153 if (glu::getTypeName(glType) == DE_NULL)
154 TCU_THROW(NotSupportedError, ("Unsupported GL_IMPLEMENTATION_COLOR_READ_TYPE: " + de::toString(tcu::Format::Hex<4>(glType))).c_str());
156 format = glu::mapGLTransferFormat(glFormat, glType);
157 pixelSize = format.getPixelSize();
161 format = tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8);
164 glType = GL_UNSIGNED_BYTE;
168 TestCase::IterateResult ReadPixelsTest::iterate (void)
171 const int width = 13;
172 const int height = 13;
174 de::Random rnd(m_seed);
176 tcu::TextureFormat format(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8);
181 getFormatInfo(format, glFormat, glType, pixelSize);
182 m_testCtx.getLog() << tcu::TestLog::Message << "Format: " << glu::getTextureFormatStr(glFormat) << ", Type: " << glu::getTypeStr(glType) << tcu::TestLog::EndMessage;
184 tcu::Texture2D reference(format, width, height);
185 reference.allocLevel(0);
187 GLU_CHECK_CALL(glViewport(0, 0, width, height));
191 const float red = rnd.getFloat();
192 const float green = rnd.getFloat();
193 const float blue = rnd.getFloat();
194 const float alpha = 1.0f;
196 m_testCtx.getLog() << tcu::TestLog::Message << "Clear color: (" << red << ", " << green << ", " << blue << ", " << alpha << ")" << tcu::TestLog::EndMessage;
199 GLU_CHECK_CALL(glClearColor(red, green, blue, alpha));
200 GLU_CHECK_CALL(glClear(GL_COLOR_BUFFER_BIT));
202 tcu::clear(reference.getLevel(0), tcu::Vec4(red, green, blue, alpha));
207 std::vector<deUint8> pixelData;
208 const int rowPitch = m_alignment * deCeilFloatToInt32(float(pixelSize * width) / (float)m_alignment);
210 pixelData.resize(rowPitch * height, 0);
212 GLU_CHECK_CALL(glPixelStorei(GL_PACK_ALIGNMENT, m_alignment));
213 GLU_CHECK_CALL(glReadPixels(0, 0, width, height, glFormat, glType, &(pixelData[0])));
215 if (m_context.getRenderTarget().getNumSamples() > 1)
217 const tcu::IVec4 formatBitDepths = tcu::getTextureFormatBitDepth(format);
218 const deUint8 redThreshold = (deUint8)deCeilFloatToInt32(256.0f * (2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().redBits, formatBitDepths.x()))));
219 const deUint8 greenThreshold = (deUint8)deCeilFloatToInt32(256.0f * (2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().greenBits, formatBitDepths.y()))));
220 const deUint8 blueThreshold = (deUint8)deCeilFloatToInt32(256.0f * (2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().blueBits, formatBitDepths.z()))));
221 const deUint8 alphaThreshold = (deUint8)deCeilFloatToInt32(256.0f * (2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().alphaBits, formatBitDepths.w()))));
223 // bilinearCompare only accepts RGBA, UINT8
224 tcu::Texture2D referenceRGBA8 (tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), width, height);
225 tcu::Texture2D resultRGBA8 (tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), width, height);
227 referenceRGBA8.allocLevel(0);
228 resultRGBA8.allocLevel(0);
230 tcu::copy(referenceRGBA8.getLevel(0), reference.getLevel(0));
231 tcu::copy(resultRGBA8.getLevel(0), tcu::PixelBufferAccess(format, width, height, 1, rowPitch, 0, &(pixelData[0])));
233 if (tcu::bilinearCompare(m_testCtx.getLog(), "Result", "Result", referenceRGBA8.getLevel(0), resultRGBA8.getLevel(0), tcu::RGBA(redThreshold, greenThreshold, blueThreshold, alphaThreshold), tcu::COMPARE_LOG_RESULT))
234 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
236 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
240 const tcu::IVec4 formatBitDepths = tcu::getTextureFormatBitDepth(format);
241 const float redThreshold = 2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().redBits, formatBitDepths.x()));
242 const float greenThreshold = 2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().greenBits, formatBitDepths.y()));
243 const float blueThreshold = 2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().blueBits, formatBitDepths.z()));
244 const float alphaThreshold = 2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().alphaBits, formatBitDepths.w()));
247 if (tcu::floatThresholdCompare(m_testCtx.getLog(), "Result", "Result", reference.getLevel(0), tcu::PixelBufferAccess(format, width, height, 1, rowPitch, 0, &(pixelData[0])), tcu::Vec4(redThreshold, greenThreshold, blueThreshold, alphaThreshold), tcu::COMPARE_LOG_RESULT))
248 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
250 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
256 ReadPixelsTests::ReadPixelsTests (Context& context)
257 : TestCaseGroup(context, "read_pixels", "ReadPixel tests")
261 void ReadPixelsTests::init (void)
263 addChild(new ReadPixelsTest(m_context, "rgba_ubyte_align_1", "", false, 1));
264 addChild(new ReadPixelsTest(m_context, "rgba_ubyte_align_2", "", false, 2));
265 addChild(new ReadPixelsTest(m_context, "rgba_ubyte_align_4", "", false, 4));
266 addChild(new ReadPixelsTest(m_context, "rgba_ubyte_align_8", "", false, 8));
268 addChild(new ReadPixelsTest(m_context, "choose_align_1", "", true, 1));
269 addChild(new ReadPixelsTest(m_context, "choose_align_2", "", true, 2));
270 addChild(new ReadPixelsTest(m_context, "choose_align_4", "", true, 4));
271 addChild(new ReadPixelsTest(m_context, "choose_align_8", "", true, 8));