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