1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 2.0 Module
3 * -------------------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 *//*!
20 * \file
21 * \brief Read pixels tests
22 *//*--------------------------------------------------------------------*/
23
24 #include "es2fReadPixelsTests.hpp"
25
26 #include "tcuTexture.hpp"
27 #include "tcuTextureUtil.hpp"
28 #include "tcuImageCompare.hpp"
29 #include "tcuTestLog.hpp"
30 #include "tcuRenderTarget.hpp"
31
32 #include "deRandom.hpp"
33 #include "deMath.h"
34 #include "deString.h"
35 #include "deStringUtil.hpp"
36
37 #include "gluDefs.hpp"
38 #include "gluShaderProgram.hpp"
39 #include "gluStrUtil.hpp"
40 #include "gluTextureUtil.hpp"
41
42 #include "glw.h"
43
44 #include <cstring>
45
46 namespace deqp
47 {
48 namespace gles2
49 {
50 namespace Functional
51 {
52
53 class ReadPixelsTest : public TestCase
54 {
55 public:
56 ReadPixelsTest(Context &context, const char *name, const char *description, bool chooseFormat, int alignment);
57
58 IterateResult iterate(void);
59 void render(tcu::Texture2D &reference);
60
61 private:
62 bool m_chooseFormat;
63 int m_alignment;
64 int m_seed;
65
66 void getFormatInfo(tcu::TextureFormat &format, GLint &glFormat, GLint &glType, int &pixelSize);
67 };
68
ReadPixelsTest(Context & context,const char * name,const char * description,bool chooseFormat,int alignment)69 ReadPixelsTest::ReadPixelsTest(Context &context, const char *name, const char *description, bool chooseFormat,
70 int alignment)
71 : TestCase(context, name, description)
72 , m_chooseFormat(chooseFormat)
73 , m_alignment(alignment)
74 , m_seed(deStringHash(name))
75 {
76 }
77
render(tcu::Texture2D & reference)78 void ReadPixelsTest::render(tcu::Texture2D &reference)
79 {
80 // Create program
81 const char *vertexSource = "attribute mediump vec2 a_coord;\n"
82 "void main (void)\n"
83 "{\n"
84 "\tgl_Position = vec4(a_coord, 0.0, 1.0);\n"
85 "}\n";
86
87 const char *fragmentSource = "void main (void)\n"
88 "{\n"
89 "\tgl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
90 "}\n";
91
92 glu::ShaderProgram program(m_context.getRenderContext(), glu::makeVtxFragSources(vertexSource, fragmentSource));
93
94 m_testCtx.getLog() << program;
95 TCU_CHECK(program.isOk());
96 GLU_CHECK_CALL(glUseProgram(program.getProgram()));
97
98 // Render
99 {
100 const float coords[] = {-0.5f, -0.5f, 0.5f, -0.5f, 0.5f, 0.5f,
101
102 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f};
103 GLuint coordLoc;
104
105 coordLoc = glGetAttribLocation(program.getProgram(), "a_coord");
106 GLU_CHECK_MSG("glGetAttribLocation()");
107
108 GLU_CHECK_CALL(glEnableVertexAttribArray(coordLoc));
109
110 GLU_CHECK_CALL(glVertexAttribPointer(coordLoc, 2, GL_FLOAT, GL_FALSE, 0, coords));
111
112 GLU_CHECK_CALL(glDrawArrays(GL_TRIANGLES, 0, 6));
113 GLU_CHECK_CALL(glDisableVertexAttribArray(coordLoc));
114 }
115
116 // Render reference
117
118 const int coordX1 = (int)((-0.5f * (float)reference.getWidth() / 2.0f) + (float)reference.getWidth() / 2.0f);
119 const int coordY1 = (int)((-0.5f * (float)reference.getHeight() / 2.0f) + (float)reference.getHeight() / 2.0f);
120 const int coordX2 = (int)((0.5f * (float)reference.getWidth() / 2.0f) + (float)reference.getWidth() / 2.0f);
121 const int coordY2 = (int)((0.5f * (float)reference.getHeight() / 2.0f) + (float)reference.getHeight() / 2.0f);
122
123 for (int x = 0; x < reference.getWidth(); x++)
124 {
125 if (x < coordX1 || x > coordX2)
126 continue;
127
128 for (int y = 0; y < reference.getHeight(); y++)
129 {
130 if (y >= coordY1 && y <= coordY2)
131 reference.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), x, y);
132 }
133 }
134 }
135
getFormatInfo(tcu::TextureFormat & format,GLint & glFormat,GLint & glType,int & pixelSize)136 void ReadPixelsTest::getFormatInfo(tcu::TextureFormat &format, GLint &glFormat, GLint &glType, int &pixelSize)
137 {
138 if (m_chooseFormat)
139 {
140 GLU_CHECK_CALL(glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &glFormat));
141 GLU_CHECK_CALL(glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &glType));
142
143 if (glFormat != GL_RGBA && glFormat != GL_BGRA && glFormat != GL_RGB)
144 TCU_THROW(NotSupportedError, ("Unsupported IMPLEMENTATION_COLOR_READ_FORMAT: " +
145 de::toString(glu::getTextureFormatStr(glFormat)))
146 .c_str());
147 if (glu::getTypeName(glType) == DE_NULL)
148 TCU_THROW(NotSupportedError,
149 ("Unsupported GL_IMPLEMENTATION_COLOR_READ_TYPE: " + de::toString(tcu::Format::Hex<4>(glType)))
150 .c_str());
151
152 format = glu::mapGLTransferFormat(glFormat, glType);
153 pixelSize = format.getPixelSize();
154 }
155 else
156 {
157 format = tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8);
158 pixelSize = 1 * 4;
159 glFormat = GL_RGBA;
160 glType = GL_UNSIGNED_BYTE;
161 }
162 }
163
iterate(void)164 TestCase::IterateResult ReadPixelsTest::iterate(void)
165 {
166 // Create reference
167 const int width = 13;
168 const int height = 13;
169
170 de::Random rnd(m_seed);
171
172 tcu::TextureFormat format(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8);
173 int pixelSize;
174 GLint glFormat;
175 GLint glType;
176
177 getFormatInfo(format, glFormat, glType, pixelSize);
178 m_testCtx.getLog() << tcu::TestLog::Message << "Format: " << glu::getTextureFormatStr(glFormat)
179 << ", Type: " << glu::getTypeStr(glType) << tcu::TestLog::EndMessage;
180
181 tcu::Texture2D reference(format, width, height, glu::isES2Context(m_context.getRenderContext().getType()));
182 reference.allocLevel(0);
183
184 GLU_CHECK_CALL(glViewport(0, 0, width, height));
185
186 // Clear color
187 {
188 const float red = rnd.getFloat();
189 const float green = rnd.getFloat();
190 const float blue = rnd.getFloat();
191 const float alpha = 1.0f;
192
193 m_testCtx.getLog() << tcu::TestLog::Message << "Clear color: (" << red << ", " << green << ", " << blue << ", "
194 << alpha << ")" << tcu::TestLog::EndMessage;
195
196 // Clear target
197 GLU_CHECK_CALL(glClearColor(red, green, blue, alpha));
198 GLU_CHECK_CALL(glClear(GL_COLOR_BUFFER_BIT));
199
200 tcu::clear(reference.getLevel(0), tcu::Vec4(red, green, blue, alpha));
201 }
202
203 render(reference);
204
205 std::vector<uint8_t> pixelData;
206 const int rowPitch = m_alignment * deCeilFloatToInt32(float(pixelSize * width) / (float)m_alignment);
207
208 pixelData.resize(rowPitch * height, 0);
209
210 GLU_CHECK_CALL(glPixelStorei(GL_PACK_ALIGNMENT, m_alignment));
211 GLU_CHECK_CALL(glReadPixels(0, 0, width, height, glFormat, glType, &(pixelData[0])));
212
213 if (m_context.getRenderTarget().getNumSamples() > 1)
214 {
215 const tcu::IVec4 formatBitDepths = tcu::getTextureFormatBitDepth(format);
216 const uint8_t redThreshold = (uint8_t)deCeilFloatToInt32(
217 256.0f *
218 (2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().redBits, formatBitDepths.x()))));
219 const uint8_t greenThreshold = (uint8_t)deCeilFloatToInt32(
220 256.0f * (2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().greenBits,
221 formatBitDepths.y()))));
222 const uint8_t blueThreshold = (uint8_t)deCeilFloatToInt32(
223 256.0f *
224 (2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().blueBits, formatBitDepths.z()))));
225 const uint8_t alphaThreshold = (uint8_t)deCeilFloatToInt32(
226 256.0f * (2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().alphaBits,
227 formatBitDepths.w()))));
228
229 // bilinearCompare only accepts RGBA, UINT8
230 tcu::Texture2D referenceRGBA8(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8),
231 width, height, glu::isES2Context(m_context.getRenderContext().getType()));
232 tcu::Texture2D resultRGBA8(tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8), width,
233 height, glu::isES2Context(m_context.getRenderContext().getType()));
234
235 referenceRGBA8.allocLevel(0);
236 resultRGBA8.allocLevel(0);
237
238 tcu::copy(referenceRGBA8.getLevel(0), reference.getLevel(0));
239 tcu::copy(resultRGBA8.getLevel(0),
240 tcu::PixelBufferAccess(format, width, height, 1, rowPitch, 0, &(pixelData[0])));
241
242 if (tcu::bilinearCompare(
243 m_testCtx.getLog(), "Result", "Result", referenceRGBA8.getLevel(0), resultRGBA8.getLevel(0),
244 tcu::RGBA(redThreshold, greenThreshold, blueThreshold, alphaThreshold), tcu::COMPARE_LOG_RESULT))
245 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
246 else
247 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
248 }
249 else
250 {
251 const tcu::IVec4 formatBitDepths = tcu::getTextureFormatBitDepth(format);
252 const float redThreshold =
253 2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().redBits, formatBitDepths.x()));
254 const float greenThreshold =
255 2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().greenBits, formatBitDepths.y()));
256 const float blueThreshold =
257 2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().blueBits, formatBitDepths.z()));
258 const float alphaThreshold =
259 2.0f / (float)(1 << deMin32(m_context.getRenderTarget().getPixelFormat().alphaBits, formatBitDepths.w()));
260
261 // Compare
262 if (tcu::floatThresholdCompare(m_testCtx.getLog(), "Result", "Result", reference.getLevel(0),
263 tcu::PixelBufferAccess(format, width, height, 1, rowPitch, 0, &(pixelData[0])),
264 tcu::Vec4(redThreshold, greenThreshold, blueThreshold, alphaThreshold),
265 tcu::COMPARE_LOG_RESULT))
266 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
267 else
268 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
269 }
270
271 return STOP;
272 }
273
ReadPixelsTests(Context & context)274 ReadPixelsTests::ReadPixelsTests(Context &context) : TestCaseGroup(context, "read_pixels", "ReadPixel tests")
275 {
276 }
277
init(void)278 void ReadPixelsTests::init(void)
279 {
280 addChild(new ReadPixelsTest(m_context, "rgba_ubyte_align_1", "", false, 1));
281 addChild(new ReadPixelsTest(m_context, "rgba_ubyte_align_2", "", false, 2));
282 addChild(new ReadPixelsTest(m_context, "rgba_ubyte_align_4", "", false, 4));
283 addChild(new ReadPixelsTest(m_context, "rgba_ubyte_align_8", "", false, 8));
284
285 addChild(new ReadPixelsTest(m_context, "choose_align_1", "", true, 1));
286 addChild(new ReadPixelsTest(m_context, "choose_align_2", "", true, 2));
287 addChild(new ReadPixelsTest(m_context, "choose_align_4", "", true, 4));
288 addChild(new ReadPixelsTest(m_context, "choose_align_8", "", true, 8));
289 }
290
291 } // namespace Functional
292 } // namespace gles2
293 } // namespace deqp
294