1/*-------------------------------------------------------------------------
2 * OpenGL Conformance Test Suite
3 * -----------------------------
4 *
5 * Copyright (c) 2015-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#include "gluDefs.hpp"
24#include "glwEnums.hpp"
25#include "glwFunctions.hpp"
26#include "tcuTestLog.hpp"
27#include <math.h>
28#include <string.h>
29
30#include "esextcGeometryShaderQualifiers.hpp"
31
32namespace glcts
33{
34/** Constructor
35 *
36 * @param context       Test context
37 * @param name          Test case's name
38 * @param description   Test case's desricption
39 **/
40GeometryShaderFlatInterpolationTest::GeometryShaderFlatInterpolationTest(Context&			  context,
41																		 const ExtParameters& extParams,
42																		 const char* name, const char* description)
43	: TestCaseBase(context, extParams, name, description)
44	, m_bo_id(0)
45	, m_fs_id(0)
46	, m_gs_id(0)
47	, m_po_id(0)
48	, m_vao_id(0)
49	, m_vs_id(0)
50{
51	/* Nothing to be done here */
52}
53
54/** Deinitializes GLES objects created during the test. */
55void GeometryShaderFlatInterpolationTest::deinit(void)
56{
57	const glw::Functions& gl = m_context.getRenderContext().getFunctions();
58
59	/* Deinitialize ES objects */
60	if (m_bo_id != 0)
61	{
62		gl.deleteBuffers(1, &m_bo_id);
63
64		m_bo_id = 0;
65	}
66
67	if (m_fs_id != 0)
68	{
69		gl.deleteShader(m_fs_id);
70
71		m_fs_id = 0;
72	}
73
74	if (m_gs_id != 0)
75	{
76		gl.deleteShader(m_gs_id);
77
78		m_gs_id = 0;
79	}
80
81	if (m_po_id != 0)
82	{
83		gl.deleteProgram(m_po_id);
84
85		m_po_id = 0;
86	}
87
88	if (m_vao_id != 0)
89	{
90		gl.deleteVertexArrays(1, &m_vao_id);
91
92		m_vao_id = 0;
93	}
94
95	if (m_vs_id != 0)
96	{
97		gl.deleteShader(m_vs_id);
98
99		m_vs_id = 0;
100	}
101
102	/* Deinitialize base implementation */
103	TestCaseBase::deinit();
104}
105
106/* Initializes a program object to be used for the conformance test. */
107void GeometryShaderFlatInterpolationTest::initProgram()
108{
109	const glw::Functions& gl = m_context.getRenderContext().getFunctions();
110
111	/* Create shader objects */
112	m_fs_id = gl.createShader(GL_FRAGMENT_SHADER);
113	m_gs_id = gl.createShader(GL_GEOMETRY_SHADER);
114	m_vs_id = gl.createShader(GL_VERTEX_SHADER);
115
116	GLU_EXPECT_NO_ERROR(gl.getError(), "glCreateShader() call(s) failed.");
117
118	/* Create the program object */
119	m_po_id = gl.createProgram();
120
121	GLU_EXPECT_NO_ERROR(gl.getError(), "glCreateProgram() call failed.");
122
123	/* Set the test program object up */
124	static const char* fs_code_raw = "${VERSION}\n"
125									 "\n"
126									 "precision highp float;\n"
127									 "\n"
128									 "out vec4 result;\n"
129									 "\n"
130									 "void main()\n"
131									 "{\n"
132									 "    result = vec4(1.0);\n"
133									 "}\n";
134
135	static const char* gs_code_raw = "${VERSION}\n"
136									 "${GEOMETRY_SHADER_REQUIRE}\n"
137									 "\n"
138									 "layout(triangles)                in;\n"
139									 "layout(points, max_vertices = 1) out;\n"
140									 "\n"
141									 "flat in int out_vertex[];\n"
142									 "\n"
143									 "void main()\n"
144									 "{\n"
145									 "    if (out_vertex[0] != 0 || out_vertex[1] != 1 || out_vertex[2] != 2)\n"
146									 "    {\n"
147									 "        gl_Position = vec4(0.0);\n"
148									 "    }\n"
149									 "    else\n"
150									 "    {\n"
151									 "        gl_Position = vec4(1.0);\n"
152									 "    }\n"
153									 "    EmitVertex();\n"
154									 "}\n";
155
156	static const char* po_varying  = "gl_Position";
157	static const char* vs_code_raw = "${VERSION}\n"
158									 "\n"
159									 "flat out int out_vertex;\n"
160									 "\n"
161									 "void main()\n"
162									 "{\n"
163									 "    gl_Position = vec4(0.0);\n"
164									 "    out_vertex  = gl_VertexID;\n"
165									 "}\n";
166
167	std::string fs_code_specialized = TestCaseBase::specializeShader(1, /* parts */
168																	 &fs_code_raw);
169	const char* fs_code_specialized_raw = fs_code_specialized.c_str();
170	std::string gs_code_specialized		= TestCaseBase::specializeShader(1, /* parts */
171																	 &gs_code_raw);
172	const char* gs_code_specialized_raw = gs_code_specialized.c_str();
173	std::string vs_code_specialized		= TestCaseBase::specializeShader(1, /* parts */
174																	 &vs_code_raw);
175	const char* vs_code_specialized_raw = vs_code_specialized.c_str();
176
177	gl.transformFeedbackVaryings(m_po_id, 1, /* count */
178								 &po_varying, GL_INTERLEAVED_ATTRIBS);
179	GLU_EXPECT_NO_ERROR(gl.getError(), "glTransformFeedbackVaryings() call failed.");
180
181	if (!TestCaseBase::buildProgram(m_po_id, m_gs_id, 1,				  /* n_sh1_body_parts */
182									&gs_code_specialized_raw, m_vs_id, 1, /* n_sh2_body_parts */
183									&vs_code_specialized_raw, m_fs_id, 1, /* n_sh3_body_parts */
184									&fs_code_specialized_raw))
185	{
186		TCU_FAIL("Could not build test program object");
187	}
188}
189
190/** Executes the test.
191 *  Sets the test result to QP_TEST_RESULT_FAIL if the test failed, QP_TEST_RESULT_PASS otherwise.
192 *  @return STOP if the test has finished, CONTINUE to indicate iterate should be called once again.
193 *  Note the function throws exception should an error occur!
194 **/
195tcu::TestNode::IterateResult GeometryShaderFlatInterpolationTest::iterate(void)
196{
197	const glw::Functions& gl	 = m_context.getRenderContext().getFunctions();
198	bool				  result = true;
199
200	/* This test should only run if EXT_geometry_shader is supported. */
201	if (!m_is_geometry_shader_extension_supported)
202	{
203		throw tcu::NotSupportedError(GEOMETRY_SHADER_EXTENSION_NOT_SUPPORTED, "", __FILE__, __LINE__);
204	}
205
206	/* Set up the buffer object */
207	gl.genBuffers(1, &m_bo_id);
208	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenBuffers() call failed.");
209
210	gl.bindBuffer(GL_ARRAY_BUFFER, m_bo_id);
211	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindBuffer() call failed.");
212
213	gl.bufferData(GL_ARRAY_BUFFER, sizeof(float) * 4, /* components */
214				  DE_NULL,							  /* data */
215				  GL_STATIC_DRAW);
216	GLU_EXPECT_NO_ERROR(gl.getError(), "glBufferData() call failed.");
217
218	gl.bindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, /* index */
219					  m_bo_id);
220	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindBufferBase() call failed.");
221
222	/* Set up the vertex array object */
223	gl.genVertexArrays(1, &m_vao_id);
224	GLU_EXPECT_NO_ERROR(gl.getError(), "glGenVertexArrays() call failed.");
225
226	gl.bindVertexArray(m_vao_id);
227	GLU_EXPECT_NO_ERROR(gl.getError(), "glBindVertexArray() call failed.");
228
229	/* Set up the test program */
230	initProgram();
231
232	gl.useProgram(m_po_id);
233	GLU_EXPECT_NO_ERROR(gl.getError(), "glUseProgram() call failed.");
234
235	gl.beginTransformFeedback(GL_POINTS);
236	GLU_EXPECT_NO_ERROR(gl.getError(), "glBeginTransformFeedback() call failed.");
237
238	/* Draw the input triangle */
239	gl.drawArrays(GL_TRIANGLES, 0, /* first */
240				  3);			   /* count */
241	GLU_EXPECT_NO_ERROR(gl.getError(), "glDrawArrays() call failed.");
242
243	gl.endTransformFeedback();
244	GLU_EXPECT_NO_ERROR(gl.getError(), "glEndTransformFeedback() call failed.");
245
246	/* Map the BO contents to the process space */
247	const float* result_bo_data = (const float*)gl.mapBufferRange(GL_ARRAY_BUFFER, 0, /* offset */
248																  sizeof(float) * 4, GL_MAP_READ_BIT);
249
250	GLU_EXPECT_NO_ERROR(gl.getError(), "glMapBufferRange() call failed.");
251
252	if (fabs(result_bo_data[0] - 1.0f) >= 1e-5f || fabs(result_bo_data[1] - 1.0f) >= 1e-5f ||
253		fabs(result_bo_data[2] - 1.0f) >= 1e-5f || fabs(result_bo_data[3] - 1.0f) >= 1e-5f)
254	{
255		m_testCtx.getLog() << tcu::TestLog::Message << "Invalid texel data was retrieved." << tcu::TestLog::EndMessage;
256
257		result = false;
258	}
259
260	gl.unmapBuffer(GL_ARRAY_BUFFER);
261	GLU_EXPECT_NO_ERROR(gl.getError(), "glUnmapBuffer() call failed.");
262
263	/* All done */
264	if (result)
265	{
266		m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
267	}
268	else
269	{
270		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
271	}
272
273	return STOP;
274}
275
276} // namespace glcts
277