1/*-------------------------------------------------------------------------
2 * OpenGL Conformance Test Suite
3 * -----------------------------
4 *
5 * Copyright (c) 2014-2016 The Khronos Group Inc.
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
22 */ /*-------------------------------------------------------------------*/
23
24/*!
25 * \file esextcGPUShader5SamplerArrayIndexing.cpp
26 * \brief  gpu_shader5 extension - Sampler Array Indexing (Test 1)
27 */ /*-------------------------------------------------------------------*/
28
29#include "esextcGPUShader5SamplerArrayIndexing.hpp"
30
31#include "gluDefs.hpp"
32#include "glwEnums.hpp"
33#include "glwFunctions.hpp"
34#include "tcuTestLog.hpp"
35#include <cstring>
36
37namespace glcts
38{
39
40const int GPUShader5SamplerArrayIndexing::m_n_small_textures	 = 4;
41const int GPUShader5SamplerArrayIndexing::m_n_texture_components = 4;
42const int GPUShader5SamplerArrayIndexing::m_big_texture_height   = 3;
43const int GPUShader5SamplerArrayIndexing::m_big_texture_width	= 3;
44const int GPUShader5SamplerArrayIndexing::m_n_texture_levels	 = 1;
45const int GPUShader5SamplerArrayIndexing::m_small_texture_height = 1;
46const int GPUShader5SamplerArrayIndexing::m_small_texture_width  = 1;
47
48/** Constructor
49 *
50 * @param context       Test context
51 * @param name          Test case's name
52 * @param description   Test case's description
53 **/
54GPUShader5SamplerArrayIndexing::GPUShader5SamplerArrayIndexing(Context& context, const ExtParameters& extParams,
55															   const char* name, const char* description)
56	: TestCaseBase(context, extParams, name, description)
57	, m_big_to_id(0)
58	, m_fbo_id(0)
59	, m_fs_id(0)
60	, m_po_id(0)
61	, m_small_to_ids(DE_NULL)
62	, m_vao_id(0)
63	, m_vbo_id(0)
64	, m_vs_id(0)
65{
66}
67
68/** Deinitializes GLES objects created during the test.
69 *
70 */
71void GPUShader5SamplerArrayIndexing::deinit(void)
72{
73	const glw::Functions& gl = m_context.getRenderContext().getFunctions();
74
75	/* Reset OpenGL ES state */
76	gl.useProgram(0);
77	gl.bindBuffer(GL_ARRAY_BUFFER, 0);
78	gl.activeTexture(GL_TEXTURE0);
79	gl.bindTexture(GL_TEXTURE_2D, 0);
80	gl.bindFramebuffer(GL_FRAMEBUFFER, 0);
81	gl.bindVertexArray(0);
82
83	if (m_po_id != 0)
84	{
85		gl.deleteProgram(m_po_id);
86		m_po_id = 0;
87	}
88
89	if (m_fs_id != 0)
90	{
91		gl.deleteShader(m_fs_id);
92		m_fs_id = 0;
93	}
94
95	if (m_vs_id != 0)
96	{
97		gl.deleteShader(m_vs_id);
98		m_vs_id = 0;
99	}
100
101	if (m_vbo_id != 0)
102	{
103		gl.deleteBuffers(1, &m_vbo_id);
104		m_vbo_id = 0;
105	}
106
107	if (m_fbo_id != 0)
108	{
109		gl.deleteFramebuffers(1, &m_fbo_id);
110		m_fbo_id = 0;
111	}
112
113	if (m_vao_id != 0)
114	{
115		gl.deleteVertexArrays(1, &m_vao_id);
116		m_vao_id = 0;
117	}
118
119	if (m_big_to_id != 0)
120	{
121		gl.deleteTextures(1, &m_big_to_id);
122		m_big_to_id = 0;
123	}
124
125	if (m_small_to_ids != DE_NULL)
126	{
127		gl.deleteTextures(m_n_small_textures, m_small_to_ids);
128		delete[] m_small_to_ids;
129		m_small_to_ids = DE_NULL;
130	}
131
132	/* Release base class */
133	TestCaseBase::deinit();
134}
135
136/** Initializes GLES objects used during the test.
137 *
138 */
139void GPUShader5SamplerArrayIndexing::initTest(void)
140{
141	const glw::Functions& gl = m_context.getRenderContext().getFunctions();
142
143	/* Check if gpu_shader5 extension is supported */
144	if (!m_is_gpu_shader5_supported)
145	{
146		throw tcu::NotSupportedError(GPU_SHADER5_EXTENSION_NOT_SUPPORTED, "", __FILE__, __LINE__);
147	}
148
149	/* Create shader objects */
150	m_fs_id = gl.createShader(GL_FRAGMENT_SHADER);
151	m_vs_id = gl.createShader(GL_VERTEX_SHADER);
152
153	/* Create progream object */
154	m_po_id = gl.createProgram();
155
156	const char* fsCode = getFragmentShaderCode();
157	const char* vsCode = getVertexShaderCode();
158
159	if (!buildProgram(m_po_id, m_fs_id, 1 /* part */, &fsCode, m_vs_id, 1 /* part */, &vsCode))
160	{
161		TCU_FAIL("Could not create program object!");
162	}
163
164	/* Create and bind vertex array object */
165	gl.genVertexArrays(1, &m_vao_id);
166	gl.bindVertexArray(m_vao_id);
167
168	GLU_EXPECT_NO_ERROR(gl.getError(), "Error configuring vertex array object");
169
170	/* Configure vertex buffer */
171	const glw::GLfloat vertices[] = { -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 1.0f,
172									  -1.0f, 1.0f,  0.0f, 1.0f, 1.0f, 1.0f,  0.0f, 1.0f };
173
174	gl.genBuffers(1, &m_vbo_id);
175	gl.bindBuffer(GL_ARRAY_BUFFER, m_vbo_id);
176	gl.bufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
177
178	GLU_EXPECT_NO_ERROR(gl.getError(), "Error creating vertex buffer object!");
179
180	/* Create and configure texture object used as color attachment */
181	gl.genTextures(1, &m_big_to_id);
182	gl.bindTexture(GL_TEXTURE_2D, m_big_to_id);
183	gl.texStorage2D(GL_TEXTURE_2D, m_n_texture_levels, GL_RGBA8, m_big_texture_width, m_big_texture_height);
184	GLU_EXPECT_NO_ERROR(gl.getError(), "Error configuring texture object!");
185
186	/* Create and configure the framebuffer object */
187	gl.genFramebuffers(1, &m_fbo_id);
188	gl.bindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo_id);
189	gl.framebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_big_to_id, 0 /* level */);
190	GLU_EXPECT_NO_ERROR(gl.getError(), "Error configuring framebuffer object!");
191
192	/* Configure textures used in fragment shader */
193	const glw::GLfloat alpha[] = { 0.0f, 0.0f, 0.0f, 1.0f };
194	const glw::GLfloat blue[]  = { 0.0f, 0.0f, 1.0f, 0.0f };
195	const glw::GLfloat green[] = { 0.0f, 1.0f, 0.0f, 0.0f };
196	const glw::GLfloat red[]   = { 1.0f, 0.0f, 0.0f, 0.0f };
197
198	m_small_to_ids = new glw::GLuint[m_n_small_textures];
199	memset(m_small_to_ids, 0, m_n_small_textures * sizeof(glw::GLuint));
200
201	gl.genTextures(m_n_small_textures, m_small_to_ids);
202
203	gl.activeTexture(GL_TEXTURE0);
204	gl.bindTexture(GL_TEXTURE_2D, m_small_to_ids[0]);
205	gl.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_small_texture_width, m_small_texture_height, 0 /* border */, GL_RGBA,
206				  GL_FLOAT, red);
207	gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
208	gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
209	GLU_EXPECT_NO_ERROR(gl.getError(), "Error configuring texture object");
210
211	gl.activeTexture(GL_TEXTURE1);
212	gl.bindTexture(GL_TEXTURE_2D, m_small_to_ids[1]);
213	gl.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_small_texture_width, m_small_texture_height, 0 /* border */, GL_RGBA,
214				  GL_FLOAT, green);
215	gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
216	gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
217	GLU_EXPECT_NO_ERROR(gl.getError(), "Error configuring texture object");
218
219	gl.activeTexture(GL_TEXTURE2);
220	gl.bindTexture(GL_TEXTURE_2D, m_small_to_ids[2]);
221	gl.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_small_texture_width, m_small_texture_height, 0 /* border */, GL_RGBA,
222				  GL_FLOAT, blue);
223	gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
224	gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
225	GLU_EXPECT_NO_ERROR(gl.getError(), "Error configuring texture object");
226
227	gl.activeTexture(GL_TEXTURE3);
228	gl.bindTexture(GL_TEXTURE_2D, m_small_to_ids[3]);
229	gl.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_small_texture_width, m_small_texture_height, 0 /* border */, GL_RGBA,
230				  GL_FLOAT, alpha);
231	gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
232	gl.texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
233	GLU_EXPECT_NO_ERROR(gl.getError(), "Error configuring texture object");
234}
235
236/** Executes the test.
237 *  Sets the test result to QP_TEST_RESULT_FAIL if the test failed, QP_TEST_RESULT_PASS otherwise.
238 *
239 *  @return STOP if the test has finished, CONTINUE to indicate iterate should be called once again.
240 *
241 *  Note the function throws exception should an error occur!
242 **/
243tcu::TestNode::IterateResult GPUShader5SamplerArrayIndexing::iterate(void)
244{
245	initTest();
246
247	const glw::Functions& gl = m_context.getRenderContext().getFunctions();
248
249	gl.viewport(0 /* x */, 0 /* y */, m_big_texture_width, m_big_texture_height);
250	GLU_EXPECT_NO_ERROR(gl.getError(), "glViewport() call failed");
251
252	gl.useProgram(m_po_id);
253	GLU_EXPECT_NO_ERROR(gl.getError(), "glUseProgram() call failed");
254
255	/* Configure position vertex array */
256	gl.bindBuffer(GL_ARRAY_BUFFER, m_vbo_id);
257	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindBuffer() call failed");
258
259	glw::GLint position_attribute_location = gl.getAttribLocation(m_po_id, "position");
260
261	gl.vertexAttribPointer(position_attribute_location, 4 /* size */, GL_FLOAT, GL_FALSE, 0 /* stride */,
262						   DE_NULL /* pointer */);
263	gl.enableVertexAttribArray(position_attribute_location);
264	GLU_EXPECT_NO_ERROR(gl.getError(), "Error configuring position vertex attribute array!");
265
266	glw::GLint samplers_uniform_location = gl.getUniformLocation(m_po_id, "samplers");
267
268	gl.activeTexture(GL_TEXTURE0);
269	gl.bindTexture(GL_TEXTURE_2D, m_small_to_ids[0]);
270	gl.uniform1i(samplers_uniform_location + 0, 0);
271	GLU_EXPECT_NO_ERROR(gl.getError(), "Error assigning texture unit 0 to samplers[0] uniform location!");
272
273	gl.activeTexture(GL_TEXTURE1);
274	gl.bindTexture(GL_TEXTURE_2D, m_small_to_ids[1]);
275	gl.uniform1i(samplers_uniform_location + 1, 1);
276	GLU_EXPECT_NO_ERROR(gl.getError(), "Error assigning texture unit 1 to samplers[1] uniform location!");
277
278	gl.activeTexture(GL_TEXTURE2);
279	gl.bindTexture(GL_TEXTURE_2D, m_small_to_ids[2]);
280	gl.uniform1i(samplers_uniform_location + 2, 2);
281	GLU_EXPECT_NO_ERROR(gl.getError(), "Error assigning texture unit 2 to samplers[2] uniform location!");
282
283	gl.activeTexture(GL_TEXTURE3);
284	gl.bindTexture(GL_TEXTURE_2D, m_small_to_ids[3]);
285	gl.uniform1i(samplers_uniform_location + 3, 3);
286	GLU_EXPECT_NO_ERROR(gl.getError(), "Error assigning texture unit 3 to samplers[3] uniform location!");
287
288	/* Render */
289	gl.clearColor(0.0f, 0.0f, 0.0f, 0.0f);
290	gl.clear(GL_COLOR_BUFFER_BIT);
291
292	GLU_EXPECT_NO_ERROR(gl.getError(), "Error clearing color buffer!");
293
294	gl.drawArrays(GL_TRIANGLE_STRIP, 0 /* first */, 4 /* count */);
295	GLU_EXPECT_NO_ERROR(gl.getError(), "Rendering error");
296
297	/* Verify results */
298	const glw::GLubyte referenceColor[] = { 255, 255, 255, 255 };
299	glw::GLubyte	   buffer[m_n_texture_components];
300
301	memset(buffer, 0, m_n_texture_components * sizeof(glw::GLubyte));
302
303	/* Reading data */
304	gl.bindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo_id);
305	gl.readPixels(1, /* x */
306				  1, /* y */
307				  1, /* width */
308				  1, /* height */
309				  GL_RGBA, GL_UNSIGNED_BYTE, buffer);
310	GLU_EXPECT_NO_ERROR(gl.getError(), "Error reading pixel data!");
311
312	/* Fail if result color is different from reference color */
313	if (memcmp(referenceColor, buffer, sizeof(referenceColor)))
314	{
315		m_testCtx.getLog() << tcu::TestLog::Message << "Rendered color [" << (int)buffer[0] << ", " << (int)buffer[1]
316						   << ", " << (int)buffer[2] << ", " << (int)buffer[3]
317						   << "] is different from reference color [" << (int)referenceColor[0] << ", "
318						   << (int)referenceColor[1] << ", " << (int)referenceColor[2] << ", " << (int)referenceColor[3]
319						   << "] !" << tcu::TestLog::EndMessage;
320
321		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
322		return STOP;
323	}
324
325	m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
326	return STOP;
327}
328
329/** Returns code for Vertex Shader
330 *
331 * @return pointer to literal with Vertex Shader code
332 **/
333const char* GPUShader5SamplerArrayIndexing::getVertexShaderCode()
334{
335	static const char* result = "${VERSION}\n"
336								"\n"
337								"${GPU_SHADER5_REQUIRE}\n"
338								"\n"
339								"precision highp float;\n"
340								"\n"
341								"in vec4 position;"
342								"\n"
343								"void main()\n"
344								"{\n"
345								"   gl_Position = position;"
346								"}\n";
347
348	return result;
349}
350
351/** Returns code for Fragment Shader
352 *
353 * @return pointer to literal with Fragment Shader code
354 **/
355const char* GPUShader5SamplerArrayIndexing::getFragmentShaderCode()
356{
357	static const char* result = "${VERSION}\n"
358								"\n"
359								"${GPU_SHADER5_REQUIRE}\n"
360								"\n"
361								"precision highp float;\n"
362								"\n"
363								"uniform sampler2D samplers[4];\n"
364								"\n"
365								"layout(location = 0) out vec4 outColor;\n"
366								"\n"
367								"void main(void)\n"
368								"{\n"
369								"    outColor = vec4(0, 0, 0, 0);\n"
370								"\n"
371								"    for (int i = 0;i < 4; ++i)\n"
372								"    {\n"
373								"        outColor +=  texture(samplers[i],vec2(0,0));\n"
374								"    }\n"
375								"}\n";
376
377	return result;
378}
379
380} // namespace glcts
381