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  esextcTextureBufferParamValueIntToFloatConversion.cpp
26 * \brief Texture Buffer - Integer To Float Conversion (Test 4)
27 */ /*-------------------------------------------------------------------*/
28
29#include "esextcTextureBufferParamValueIntToFloatConversion.hpp"
30#include "gluContextInfo.hpp"
31#include "gluDefs.hpp"
32#include "glwEnums.hpp"
33#include "glwFunctions.hpp"
34#include "tcuTestLog.hpp"
35#include <cmath>
36#include <vector>
37
38namespace glcts
39{
40
41const glw::GLfloat TextureBufferParamValueIntToFloatConversion::m_epsilon = 1.0f / 256.0f;
42glw::GLuint		   TextureBufferParamValueIntToFloatConversion::m_texture_buffer_size;
43glw::GLuint		   TextureBufferParamValueIntToFloatConversion::m_work_group_x_size;
44glw::GLuint		   TextureBufferParamValueIntToFloatConversion::m_work_group_y_size;
45
46/** Constructor
47 *
48 *  @param context     Test context
49 *  @param name        Test case's name
50 *  @param description Test case's description
51 **/
52TextureBufferParamValueIntToFloatConversion::TextureBufferParamValueIntToFloatConversion(Context&			  context,
53																						 const ExtParameters& extParams,
54																						 const char*		  name,
55																						 const char* description)
56	: TestCaseBase(context, extParams, name, description)
57	, m_cs_id(0)
58	, m_po_id(0)
59	, m_ssbo_id(0)
60	, m_tbo_id(0)
61	, m_tbo_tex_id(0)
62{
63	/* Nothing to be done here */
64}
65
66/** Initializes GLES objects used during the test.
67 *
68 */
69void TextureBufferParamValueIntToFloatConversion::initTest(void)
70{
71	/* Check if texture buffer extension is supported */
72	if (!m_is_texture_buffer_supported)
73	{
74		throw tcu::NotSupportedError(TEXTURE_BUFFER_EXTENSION_NOT_SUPPORTED, "", __FILE__, __LINE__);
75	}
76
77	/* Get GL entry points */
78	const glw::Functions& gl = m_context.getRenderContext().getFunctions();
79
80	glw::GLint max_workgroup_size;
81	gl.getIntegeri_v(GL_MAX_COMPUTE_WORK_GROUP_SIZE, 0, &max_workgroup_size);
82	GLU_EXPECT_NO_ERROR(gl.getError(), "Error getting GL_MAX_COMPUTE_WORK_GROUP_SIZE parameter value!");
83
84	m_work_group_x_size = m_work_group_y_size = de::min((glw::GLuint)floor(sqrt((float)max_workgroup_size)), 16u);
85	m_texture_buffer_size					  = m_work_group_x_size * m_work_group_y_size;
86
87	glw::GLubyte* dataBuffer = new glw::GLubyte[m_texture_buffer_size];
88
89	for (glw::GLuint i = 0; i < m_texture_buffer_size; ++i)
90	{
91		dataBuffer[i] = (glw::GLubyte)i;
92	}
93
94	/* Create texture buffer object*/
95	gl.genBuffers(1, &m_tbo_id);
96	GLU_EXPECT_NO_ERROR(gl.getError(), "Error generating buffer object!");
97	gl.bindBuffer(m_glExtTokens.TEXTURE_BUFFER, m_tbo_id);
98	GLU_EXPECT_NO_ERROR(gl.getError(), "Error binding buffer object!");
99	gl.bufferData(m_glExtTokens.TEXTURE_BUFFER, m_texture_buffer_size * sizeof(glw::GLubyte), &dataBuffer[0],
100				  GL_STATIC_READ);
101	GLU_EXPECT_NO_ERROR(gl.getError(), "Error allocating buffer object's data store!");
102
103	delete[] dataBuffer;
104
105	/* Initialize texture buffer */
106	gl.genTextures(1, &m_tbo_tex_id);
107	GLU_EXPECT_NO_ERROR(gl.getError(), "Error generating texture object!");
108	gl.activeTexture(GL_TEXTURE0);
109	GLU_EXPECT_NO_ERROR(gl.getError(), "Error setting active texture unit!");
110	gl.bindTexture(m_glExtTokens.TEXTURE_BUFFER, m_tbo_tex_id);
111	GLU_EXPECT_NO_ERROR(gl.getError(), "Error binding texture object to GL_TEXTURE_BUFFER_EXT target!");
112
113	gl.texBuffer(m_glExtTokens.TEXTURE_BUFFER, GL_R8, m_tbo_id);
114	GLU_EXPECT_NO_ERROR(gl.getError(), "Error setting buffer object as texture buffer's data store!");
115
116	/* Create program */
117	m_po_id = gl.createProgram();
118	GLU_EXPECT_NO_ERROR(gl.getError(), "Error creating program object!");
119
120	m_cs_id = gl.createShader(GL_COMPUTE_SHADER);
121	GLU_EXPECT_NO_ERROR(gl.getError(), "Error creating shader object!");
122
123	std::string csSource = getComputeShaderCode();
124	const char* csCode   = csSource.c_str();
125
126	if (!buildProgram(m_po_id, m_cs_id, 1, &csCode))
127	{
128		TCU_FAIL("Could not create a program object from valid compute shader object!");
129	}
130
131	/* Create Shader Storage Buffer Object */
132	gl.genBuffers(1, &m_ssbo_id);
133	GLU_EXPECT_NO_ERROR(gl.getError(), "Error generating buffer object!");
134	gl.bindBuffer(GL_ARRAY_BUFFER, m_ssbo_id);
135	GLU_EXPECT_NO_ERROR(gl.getError(), "Error binding  buffer object!");
136	gl.bufferData(GL_ARRAY_BUFFER, m_texture_buffer_size * sizeof(glw::GLfloat), 0, GL_DYNAMIC_COPY);
137	GLU_EXPECT_NO_ERROR(gl.getError(), "Error generating buffer object!");
138}
139
140/** Returns Compute shader Code
141 *
142 * @return pointer to literal with Compute Shader Code
143 */
144std::string TextureBufferParamValueIntToFloatConversion::getComputeShaderCode()
145{
146	std::stringstream result;
147
148	result << "${VERSION}\n"
149			  "\n"
150			  "${TEXTURE_BUFFER_REQUIRE}\n"
151			  "\n"
152			  "precision highp float;\n"
153			  "\n"
154			  "uniform highp samplerBuffer sampler_buffer;\n"
155			  "\n"
156			  "layout(std430) buffer ComputeSSBO\n"
157			  "{\n"
158			  "    float value[];\n"
159			  "} computeSSBO;\n"
160			  "\n"
161			  "layout (local_size_x = "
162		   << m_work_group_x_size << ", local_size_y = " << m_work_group_y_size
163		   << ") in;\n"
164			  "\n"
165			  "void main(void)\n"
166			  "{\n"
167			  "    int index = int(gl_LocalInvocationID.x * gl_WorkGroupSize.y + gl_LocalInvocationID.y);\n"
168			  "\n"
169			  "    computeSSBO.value[index] = texelFetch( sampler_buffer, index ).x;\n"
170			  "}\n";
171
172	return result.str();
173}
174
175/** Executes the test.
176 *
177 *  Sets the test result to QP_TEST_RESULT_FAIL if the test failed, QP_TEST_RESULT_PASS otherwise.
178 *
179 *  Note the function throws exception should an error occur!
180 *
181 *  @return STOP if the test has finished, CONTINUE to indicate iterate should be called once again.
182 **/
183tcu::TestNode::IterateResult TextureBufferParamValueIntToFloatConversion::iterate(void)
184{
185	initTest();
186
187	/* Get Gl entry points */
188	const glw::Functions& gl = m_context.getRenderContext().getFunctions();
189
190	bool testResult = true;
191
192	gl.useProgram(m_po_id);
193	GLU_EXPECT_NO_ERROR(gl.getError(), "Error using program!");
194
195	gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, m_ssbo_id);
196	GLU_EXPECT_NO_ERROR(gl.getError(), "Could not bind buffer object to shader storage binding point!");
197
198	gl.activeTexture(GL_TEXTURE0);
199	GLU_EXPECT_NO_ERROR(gl.getError(), "Error setting active texture unit!");
200
201	gl.bindTexture(m_glExtTokens.TEXTURE_BUFFER, m_tbo_tex_id);
202	GLU_EXPECT_NO_ERROR(gl.getError(), "Error binding texture object!");
203
204	glw::GLint location = gl.getUniformLocation(m_po_id, "sampler_buffer");
205	GLU_EXPECT_NO_ERROR(gl.getError(), "Error getting uniform location!");
206
207	gl.uniform1i(location, 0);
208	GLU_EXPECT_NO_ERROR(gl.getError(), "Error setting value for uniform location!");
209
210	gl.dispatchCompute(1, 1, 1);
211	GLU_EXPECT_NO_ERROR(gl.getError(), "Error running compute shader!");
212
213	gl.memoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT);
214	GLU_EXPECT_NO_ERROR(gl.getError(), "Error setting memory barrier!");
215
216	/* Get result data */
217	glw::GLfloat* result = (glw::GLfloat*)gl.mapBufferRange(
218		GL_SHADER_STORAGE_BUFFER, 0, m_texture_buffer_size * sizeof(glw::GLfloat), GL_MAP_READ_BIT);
219	GLU_EXPECT_NO_ERROR(gl.getError(), "Error mapping buffer object's data store to client address space!");
220
221	for (glw::GLuint i = 0; i < m_texture_buffer_size; ++i)
222	{
223		/* Log error if expected data and result data are not equal with epsilon tolerance */
224
225		/* Note: texture format is R8, so i value may wrap - hence the GLubyte cast */
226		if (de::abs(result[i] - (static_cast<glw::GLubyte>(i) / 255.0f)) > m_epsilon)
227		{
228			m_testCtx.getLog() << tcu::TestLog::Message << "Result is different than expected \n"
229							   << "Expected value: " << static_cast<glw::GLfloat>(i) / 255.0f << "\n"
230							   << "Result   value: " << result[i] << "\n"
231							   << tcu::TestLog::EndMessage;
232
233			testResult = false;
234			break;
235		}
236	}
237
238	gl.unmapBuffer(GL_SHADER_STORAGE_BUFFER);
239
240	if (testResult)
241	{
242		m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
243	}
244	else
245	{
246		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Fail");
247	}
248
249	return STOP;
250}
251
252/** Deinitializes GLES objects created during the test.
253 *
254 */
255void TextureBufferParamValueIntToFloatConversion::deinit(void)
256{
257	/* Get Gl entry points */
258	const glw::Functions& gl = m_context.getRenderContext().getFunctions();
259
260	/* Reset GLES state */
261	gl.useProgram(0);
262	gl.bindBuffer(GL_ARRAY_BUFFER, 0);
263	gl.bindBuffer(m_glExtTokens.TEXTURE_BUFFER, 0);
264	gl.bindTexture(m_glExtTokens.TEXTURE_BUFFER, 0);
265	gl.bindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, 0);
266
267	/* Delete GLES objects */
268	if (0 != m_cs_id)
269	{
270		gl.deleteShader(m_cs_id);
271		m_cs_id = 0;
272	}
273
274	if (0 != m_po_id)
275	{
276		gl.deleteProgram(m_po_id);
277		m_po_id = 0;
278	}
279
280	if (0 != m_ssbo_id)
281	{
282		gl.deleteBuffers(1, &m_ssbo_id);
283		m_ssbo_id = 0;
284	}
285
286	if (0 != m_tbo_id)
287	{
288		gl.deleteBuffers(1, &m_tbo_id);
289		m_tbo_id = 0;
290	}
291
292	if (0 != m_tbo_tex_id)
293	{
294		gl.deleteTextures(1, &m_tbo_tex_id);
295		m_tbo_tex_id = 0;
296	}
297
298	/* Deinitialize base class */
299	TestCaseBase::deinit();
300}
301
302} // namespace glcts
303