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
46namespace deqp
47{
48namespace gles2
49{
50namespace Functional
51{
52
53class ReadPixelsTest : public TestCase
54{
55public:
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
61private:
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
69ReadPixelsTest::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))
74{
75}
76
77void ReadPixelsTest::render (tcu::Texture2D& reference)
78{
79	// Create program
80	const char* vertexSource =
81	"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 =
88	"void main (void)\n"
89	"{\n"
90	"\tgl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
91	"}\n";
92
93	glu::ShaderProgram program(m_context.getRenderContext(), glu::makeVtxFragSources(vertexSource, fragmentSource));
94
95	m_testCtx.getLog() << program;
96	TCU_CHECK(program.isOk());
97	GLU_CHECK_CALL(glUseProgram(program.getProgram()));
98
99	// Render
100	{
101		const float coords[] =
102		{
103			-0.5f, -0.5f,
104			 0.5f, -0.5f,
105			 0.5f,  0.5f,
106
107			 0.5f,  0.5f,
108			-0.5f,  0.5f,
109			-0.5f, -0.5f
110		};
111		GLuint coordLoc;
112
113		coordLoc = glGetAttribLocation(program.getProgram(), "a_coord");
114		GLU_CHECK_MSG("glGetAttribLocation()");
115
116		GLU_CHECK_CALL(glEnableVertexAttribArray(coordLoc));
117
118		GLU_CHECK_CALL(glVertexAttribPointer(coordLoc, 2, GL_FLOAT, GL_FALSE, 0, coords));
119
120		GLU_CHECK_CALL(glDrawArrays(GL_TRIANGLES, 0, 6));
121		GLU_CHECK_CALL(glDisableVertexAttribArray(coordLoc));
122	}
123
124	// Render reference
125
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);
130
131	for (int x = 0; x < reference.getWidth(); x++)
132	{
133		if (x < coordX1 || x > coordX2)
134			continue;
135
136		for (int y = 0; y < reference.getHeight(); y++)
137		{
138			if (y >= coordY1 && y <= coordY2)
139				reference.getLevel(0).setPixel(tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f), x, y);
140		}
141	}
142}
143
144void ReadPixelsTest::getFormatInfo (tcu::TextureFormat& format, GLint& glFormat, GLint& glType, int& pixelSize)
145{
146	if (m_chooseFormat)
147	{
148		GLU_CHECK_CALL(glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &glFormat));
149		GLU_CHECK_CALL(glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &glType));
150
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());
155
156		format = glu::mapGLTransferFormat(glFormat, glType);
157		pixelSize = format.getPixelSize();
158	}
159	else
160	{
161		format		= tcu::TextureFormat(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8);
162		pixelSize	= 1 * 4;
163		glFormat	= GL_RGBA;
164		glType		= GL_UNSIGNED_BYTE;
165	}
166}
167
168TestCase::IterateResult ReadPixelsTest::iterate (void)
169{
170	// Create reference
171	const int					width	= 13;
172	const int					height	= 13;
173
174	de::Random					rnd(m_seed);
175
176	tcu::TextureFormat			format(tcu::TextureFormat::RGBA, tcu::TextureFormat::UNORM_INT8);
177	int							pixelSize;
178	GLint						glFormat;
179	GLint						glType;
180
181	getFormatInfo(format, glFormat, glType, pixelSize);
182	m_testCtx.getLog() << tcu::TestLog::Message << "Format: " << glu::getTextureFormatStr(glFormat) << ", Type: " << glu::getTypeStr(glType) << tcu::TestLog::EndMessage;
183
184	tcu::Texture2D reference(format, width, height);
185	reference.allocLevel(0);
186
187	GLU_CHECK_CALL(glViewport(0, 0, width, height));
188
189	// Clear color
190	{
191		const float red		= rnd.getFloat();
192		const float green	= rnd.getFloat();
193		const float blue	= rnd.getFloat();
194		const float alpha	= 1.0f;
195
196		m_testCtx.getLog() << tcu::TestLog::Message << "Clear color: (" << red << ", " << green << ", " << blue << ", " << alpha << ")" << tcu::TestLog::EndMessage;
197
198		// Clear target
199		GLU_CHECK_CALL(glClearColor(red, green, blue, alpha));
200		GLU_CHECK_CALL(glClear(GL_COLOR_BUFFER_BIT));
201
202		tcu::clear(reference.getLevel(0), tcu::Vec4(red, green, blue, alpha));
203	}
204
205	render(reference);
206
207	std::vector<deUint8> pixelData;
208	const int rowPitch = m_alignment * deCeilFloatToInt32(float(pixelSize * width) / (float)m_alignment);
209
210	pixelData.resize(rowPitch * height, 0);
211
212	GLU_CHECK_CALL(glPixelStorei(GL_PACK_ALIGNMENT, m_alignment));
213	GLU_CHECK_CALL(glReadPixels(0, 0, width, height, glFormat, glType, &(pixelData[0])));
214
215	if (m_context.getRenderTarget().getNumSamples() > 1)
216	{
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()))));
222
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);
226
227		referenceRGBA8.allocLevel(0);
228		resultRGBA8.allocLevel(0);
229
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])));
232
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");
235		else
236			m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
237	}
238	else
239	{
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()));
245
246		// Compare
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");
249		else
250			m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
251	}
252
253	return STOP;
254}
255
256ReadPixelsTests::ReadPixelsTests (Context& context)
257	: TestCaseGroup(context, "read_pixels", "ReadPixel tests")
258{
259}
260
261void ReadPixelsTests::init (void)
262{
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));
267
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));
272}
273
274} // Functional
275} // gles2
276} // deqp
277