1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program OpenGL ES 3.1 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 Shader built-in constant tests.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "es31fShaderBuiltinConstantTests.hpp"
25 #include "glsShaderExecUtil.hpp"
26 #include "deUniquePtr.hpp"
27 #include "deStringUtil.hpp"
28 #include "tcuTestLog.hpp"
29 #include "gluStrUtil.hpp"
30 #include "gluContextInfo.hpp"
31 #include "glwEnums.hpp"
32 #include "glwFunctions.hpp"
33 
34 using std::string;
35 using std::vector;
36 using tcu::TestLog;
37 
38 namespace deqp
39 {
40 namespace gles31
41 {
42 namespace Functional
43 {
44 namespace
45 {
46 
getInteger(const glw::Functions & gl,deUint32 pname)47 static int getInteger (const glw::Functions& gl, deUint32 pname)
48 {
49 	int value = -1;
50 	gl.getIntegerv(pname, &value);
51 	GLU_EXPECT_NO_ERROR(gl.getError(), ("glGetIntegerv(" + glu::getGettableStateStr((int)pname).toString() + ")").c_str());
52 	return value;
53 }
54 
55 template<deUint32 Pname>
getInteger(const glw::Functions & gl)56 static int getInteger (const glw::Functions& gl)
57 {
58 	return getInteger(gl, Pname);
59 }
60 
getVectorsFromComps(const glw::Functions & gl,deUint32 pname)61 static int getVectorsFromComps (const glw::Functions& gl, deUint32 pname)
62 {
63 	int value = -1;
64 	gl.getIntegerv(pname, &value);
65 	GLU_EXPECT_NO_ERROR(gl.getError(), ("glGetIntegerv(" + glu::getGettableStateStr((int)pname).toString() + ")").c_str());
66 	TCU_CHECK_MSG(value%4 == 0, ("Expected " + glu::getGettableStateStr((int)pname).toString() + " to be divisible by 4").c_str());
67 	return value/4;
68 }
69 
70 template<deUint32 Pname>
getVectorsFromComps(const glw::Functions & gl)71 static int getVectorsFromComps (const glw::Functions& gl)
72 {
73 	return getVectorsFromComps(gl, Pname);
74 }
75 
getIVec3(const glw::Functions & gl,deUint32 pname)76 static tcu::IVec3 getIVec3 (const glw::Functions& gl, deUint32 pname)
77 {
78 	tcu::IVec3 value(-1);
79 	for (int ndx = 0; ndx < 3; ndx++)
80 		gl.getIntegeri_v(pname, (glw::GLuint)ndx, &value[ndx]);
81 	GLU_EXPECT_NO_ERROR(gl.getError(), ("glGetIntegeri_v(" + glu::getGettableStateStr((int)pname).toString() + ")").c_str());
82 	return value;
83 }
84 
85 template<deUint32 Pname>
getIVec3(const glw::Functions & gl)86 static tcu::IVec3 getIVec3 (const glw::Functions& gl)
87 {
88 	return getIVec3(gl, Pname);
89 }
90 
makeCaseName(const std::string & varName)91 static std::string makeCaseName (const std::string& varName)
92 {
93 	DE_ASSERT(varName.length() > 3);
94 	DE_ASSERT(varName.substr(0,3) == "gl_");
95 
96 	std::ostringstream name;
97 	name << de::toLower(char(varName[3]));
98 
99 	for (size_t ndx = 4; ndx < varName.length(); ndx++)
100 	{
101 		const char c = char(varName[ndx]);
102 		if (de::isUpper(c))
103 			name << '_' << de::toLower(c);
104 		else
105 			name << c;
106 	}
107 
108 	return name.str();
109 }
110 
111 enum
112 {
113 	VS = (1<<glu::SHADERTYPE_VERTEX),
114 	TC = (1<<glu::SHADERTYPE_TESSELLATION_CONTROL),
115 	TE = (1<<glu::SHADERTYPE_TESSELLATION_EVALUATION),
116 	GS = (1<<glu::SHADERTYPE_GEOMETRY),
117 	FS = (1<<glu::SHADERTYPE_FRAGMENT),
118 	CS = (1<<glu::SHADERTYPE_COMPUTE),
119 
120 	SHADER_TYPES = VS|TC|TE|GS|FS|CS
121 };
122 
123 template<typename DataType>
124 class ShaderBuiltinConstantCase : public TestCase
125 {
126 public:
127 	typedef DataType (*GetConstantValueFunc) (const glw::Functions& gl);
128 
129 								ShaderBuiltinConstantCase	(Context& context, const char* varName, GetConstantValueFunc getValue, const char* requiredExt);
130 								~ShaderBuiltinConstantCase	(void);
131 
132 	void						init						(void);
133 	IterateResult				iterate						(void);
134 
135 private:
136 	bool						verifyInShaderType			(glu::ShaderType shaderType, DataType reference);
137 
138 	const std::string			m_varName;
139 	const GetConstantValueFunc	m_getValue;
140 	const std::string			m_requiredExt;
141 };
142 
143 template<typename DataType>
ShaderBuiltinConstantCase(Context & context,const char * varName,GetConstantValueFunc getValue,const char * requiredExt)144 ShaderBuiltinConstantCase<DataType>::ShaderBuiltinConstantCase (Context& context, const char* varName, GetConstantValueFunc getValue, const char* requiredExt)
145 	: TestCase		(context, makeCaseName(varName).c_str(), varName)
146 	, m_varName		(varName)
147 	, m_getValue	(getValue)
148 	, m_requiredExt	(requiredExt ? requiredExt : "")
149 {
150 	DE_ASSERT(!requiredExt == m_requiredExt.empty());
151 }
152 
153 template<typename DataType>
~ShaderBuiltinConstantCase(void)154 ShaderBuiltinConstantCase<DataType>::~ShaderBuiltinConstantCase (void)
155 {
156 }
157 
158 template<typename DataType>
init(void)159 void ShaderBuiltinConstantCase<DataType>::init (void)
160 {
161 	if (!m_requiredExt.empty() && !m_context.getContextInfo().isExtensionSupported(m_requiredExt.c_str()))
162 		throw tcu::NotSupportedError(m_requiredExt + " not supported");
163 }
164 
createGetConstantExecutor(const glu::RenderContext & renderCtx,glu::ShaderType shaderType,glu::DataType dataType,const std::string & varName,const std::string & extName)165 static gls::ShaderExecUtil::ShaderExecutor* createGetConstantExecutor (const glu::RenderContext&	renderCtx,
166 																	   glu::ShaderType				shaderType,
167 																	   glu::DataType				dataType,
168 																	   const std::string&			varName,
169 																	   const std::string&			extName)
170 {
171 	using namespace gls::ShaderExecUtil;
172 
173 	ShaderSpec	shaderSpec;
174 
175 	shaderSpec.version	= glu::GLSL_VERSION_310_ES;
176 	shaderSpec.source	= string("result = ") + varName + ";\n";
177 
178 	shaderSpec.outputs.push_back(Symbol("result", glu::VarType(dataType, glu::PRECISION_HIGHP)));
179 
180 	if (!extName.empty())
181 		shaderSpec.globalDeclarations = "#extension " + extName + " : require\n";
182 
183 	return createExecutor(renderCtx, shaderType, shaderSpec);
184 }
185 
186 template<typename DataType>
logVarValue(tcu::TestLog & log,const std::string & varName,DataType value)187 static void logVarValue (tcu::TestLog& log, const std::string& varName, DataType value)
188 {
189 	log << TestLog::Message << varName << " = " << value << TestLog::EndMessage;
190 }
191 
192 template<>
logVarValue(tcu::TestLog & log,const std::string & varName,int value)193 void logVarValue<int> (tcu::TestLog& log, const std::string& varName, int value)
194 {
195 	log << TestLog::Integer(varName, varName, "", QP_KEY_TAG_NONE, value);
196 }
197 
198 template<typename DataType>
verifyInShaderType(glu::ShaderType shaderType,DataType reference)199 bool ShaderBuiltinConstantCase<DataType>::verifyInShaderType (glu::ShaderType shaderType, DataType reference)
200 {
201 	using namespace gls::ShaderExecUtil;
202 
203 	const de::UniquePtr<ShaderExecutor>	shaderExecutor	(createGetConstantExecutor(m_context.getRenderContext(), shaderType, glu::dataTypeOf<DataType>(), m_varName, m_requiredExt));
204 	DataType							result			= DataType(-1);
205 	void* const							outputs			= &result;
206 
207 	if (!shaderExecutor->isOk())
208 	{
209 		shaderExecutor->log(m_testCtx.getLog());
210 		TCU_FAIL("Compile failed");
211 	}
212 
213 	shaderExecutor->useProgram();
214 	shaderExecutor->execute(1, DE_NULL, &outputs);
215 
216 	logVarValue(m_testCtx.getLog(), m_varName, result);
217 
218 	if (result != reference)
219 	{
220 		m_testCtx.getLog() << TestLog::Message << "ERROR: Expected " << m_varName << " = " << reference << TestLog::EndMessage
221 						   << TestLog::Message << "Test shader:" << TestLog::EndMessage;
222 		shaderExecutor->log(m_testCtx.getLog());
223 		m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Invalid builtin constant value");
224 		return false;
225 	}
226 	else
227 		return true;
228 }
229 
230 template<typename DataType>
iterate(void)231 TestCase::IterateResult ShaderBuiltinConstantCase<DataType>::iterate (void)
232 {
233 	const DataType	reference	= m_getValue(m_context.getRenderContext().getFunctions());
234 
235 	m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
236 
237 	for (int shaderType = 0; shaderType < glu::SHADERTYPE_LAST; shaderType++)
238 	{
239 		if ((SHADER_TYPES & (1<<shaderType)) != 0)
240 		{
241 			const char* const			shaderTypeName	= glu::getShaderTypeName(glu::ShaderType(shaderType));
242 			const tcu::ScopedLogSection	section			(m_testCtx.getLog(), shaderTypeName, shaderTypeName);
243 
244 			try
245 			{
246 				const bool isOk = verifyInShaderType(glu::ShaderType(shaderType), reference);
247 				DE_ASSERT(isOk || m_testCtx.getTestResult() != QP_TEST_RESULT_PASS);
248 				DE_UNREF(isOk);
249 			}
250 			catch (const tcu::NotSupportedError& e)
251 			{
252 				m_testCtx.getLog() << TestLog::Message << "Not checking " << shaderTypeName << ": " << e.what() << TestLog::EndMessage;
253 			}
254 			catch (const tcu::TestError& e)
255 			{
256 				m_testCtx.getLog() << TestLog::Message << e.what() << TestLog::EndMessage;
257 				m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, e.getMessage());
258 			}
259 		}
260 	}
261 
262 	return STOP;
263 }
264 
265 } // anonymous
266 
ShaderBuiltinConstantTests(Context & context)267 ShaderBuiltinConstantTests::ShaderBuiltinConstantTests (Context& context)
268 	: TestCaseGroup(context, "builtin_constants", "Built-in Constant Tests")
269 {
270 }
271 
~ShaderBuiltinConstantTests(void)272 ShaderBuiltinConstantTests::~ShaderBuiltinConstantTests (void)
273 {
274 }
275 
init(void)276 void ShaderBuiltinConstantTests::init (void)
277 {
278 	// Core builtin constants
279 	{
280 		static const struct
281 		{
282 			const char*												varName;
283 			ShaderBuiltinConstantCase<int>::GetConstantValueFunc	getValue;
284 		} intConstants[] =
285 		{
286 			{ "gl_MaxVertexAttribs",					getInteger<GL_MAX_VERTEX_ATTRIBS>						},
287 			{ "gl_MaxVertexUniformVectors",				getInteger<GL_MAX_VERTEX_UNIFORM_VECTORS>				},
288 			{ "gl_MaxVertexOutputVectors",				getVectorsFromComps<GL_MAX_VERTEX_OUTPUT_COMPONENTS>	},
289 			{ "gl_MaxFragmentInputVectors",				getVectorsFromComps<GL_MAX_FRAGMENT_INPUT_COMPONENTS>	},
290 			{ "gl_MaxFragmentUniformVectors",			getInteger<GL_MAX_FRAGMENT_UNIFORM_VECTORS>				},
291 			{ "gl_MaxDrawBuffers",						getInteger<GL_MAX_DRAW_BUFFERS>							},
292 
293 			{ "gl_MaxVertexTextureImageUnits",			getInteger<GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS>			},
294 			{ "gl_MaxCombinedTextureImageUnits",		getInteger<GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS>			},
295 			{ "gl_MaxTextureImageUnits",				getInteger<GL_MAX_TEXTURE_IMAGE_UNITS>					},
296 
297 			{ "gl_MinProgramTexelOffset",				getInteger<GL_MIN_PROGRAM_TEXEL_OFFSET>					},
298 			{ "gl_MaxProgramTexelOffset",				getInteger<GL_MAX_PROGRAM_TEXEL_OFFSET>					},
299 
300 			{ "gl_MaxImageUnits",						getInteger<GL_MAX_IMAGE_UNITS>							},
301 			{ "gl_MaxVertexImageUniforms",				getInteger<GL_MAX_VERTEX_IMAGE_UNIFORMS>				},
302 			{ "gl_MaxFragmentImageUniforms",			getInteger<GL_MAX_FRAGMENT_IMAGE_UNIFORMS>				},
303 			{ "gl_MaxComputeImageUniforms",				getInteger<GL_MAX_COMPUTE_IMAGE_UNIFORMS>				},
304 			{ "gl_MaxCombinedImageUniforms",			getInteger<GL_MAX_COMBINED_IMAGE_UNIFORMS>				},
305 
306 			{ "gl_MaxCombinedShaderOutputResources",	getInteger<GL_MAX_COMBINED_SHADER_OUTPUT_RESOURCES>		},
307 
308 			{ "gl_MaxComputeUniformComponents",			getInteger<GL_MAX_COMPUTE_UNIFORM_COMPONENTS>			},
309 			{ "gl_MaxComputeTextureImageUnits",			getInteger<GL_MAX_COMPUTE_TEXTURE_IMAGE_UNITS>			},
310 
311 			{ "gl_MaxComputeAtomicCounters",			getInteger<GL_MAX_COMPUTE_ATOMIC_COUNTERS>				},
312 			{ "gl_MaxComputeAtomicCounterBuffers",		getInteger<GL_MAX_COMPUTE_ATOMIC_COUNTER_BUFFERS>		},
313 
314 			{ "gl_MaxVertexAtomicCounters",				getInteger<GL_MAX_VERTEX_ATOMIC_COUNTERS>				},
315 			{ "gl_MaxFragmentAtomicCounters",			getInteger<GL_MAX_FRAGMENT_ATOMIC_COUNTERS>				},
316 			{ "gl_MaxCombinedAtomicCounters",			getInteger<GL_MAX_COMBINED_ATOMIC_COUNTERS>				},
317 			{ "gl_MaxAtomicCounterBindings",			getInteger<GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS>		},
318 
319 			{ "gl_MaxVertexAtomicCounterBuffers",		getInteger<GL_MAX_VERTEX_ATOMIC_COUNTER_BUFFERS>		},
320 			{ "gl_MaxFragmentAtomicCounterBuffers",		getInteger<GL_MAX_FRAGMENT_ATOMIC_COUNTER_BUFFERS>		},
321 			{ "gl_MaxCombinedAtomicCounterBuffers",		getInteger<GL_MAX_COMBINED_ATOMIC_COUNTER_BUFFERS>		},
322 			{ "gl_MaxAtomicCounterBufferSize",			getInteger<GL_MAX_ATOMIC_COUNTER_BUFFER_SIZE>			},
323 		};
324 
325 		static const struct
326 		{
327 			const char*													varName;
328 			ShaderBuiltinConstantCase<tcu::IVec3>::GetConstantValueFunc	getValue;
329 		} ivec3Constants[] =
330 		{
331 			{ "gl_MaxComputeWorkGroupCount",			getIVec3<GL_MAX_COMPUTE_WORK_GROUP_COUNT>				},
332 			{ "gl_MaxComputeWorkGroupSize",				getIVec3<GL_MAX_COMPUTE_WORK_GROUP_SIZE>				},
333 		};
334 
335 		tcu::TestCaseGroup* const coreGroup = new tcu::TestCaseGroup(m_testCtx, "core", "Core Specification");
336 		addChild(coreGroup);
337 
338 		for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(intConstants); ndx++)
339 			coreGroup->addChild(new ShaderBuiltinConstantCase<int>(m_context, intConstants[ndx].varName, intConstants[ndx].getValue, DE_NULL));
340 
341 		for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(ivec3Constants); ndx++)
342 			coreGroup->addChild(new ShaderBuiltinConstantCase<tcu::IVec3>(m_context, ivec3Constants[ndx].varName, ivec3Constants[ndx].getValue, DE_NULL));
343 	}
344 
345 	// OES_sample_variables
346 	{
347 		tcu::TestCaseGroup* const sampleVarGroup = new tcu::TestCaseGroup(m_testCtx, "sample_variables", "GL_OES_sample_variables");
348 		addChild(sampleVarGroup);
349 		sampleVarGroup->addChild(new ShaderBuiltinConstantCase<int>(m_context, "gl_MaxSamples", getInteger<GL_MAX_SAMPLES>, "GL_OES_sample_variables"));
350 	}
351 
352 	// EXT_geometry_shader
353 	{
354 		static const struct
355 		{
356 			const char*												varName;
357 			ShaderBuiltinConstantCase<int>::GetConstantValueFunc	getValue;
358 		} intConstants[] =
359 		{
360 			{ "gl_MaxGeometryInputComponents",			getInteger<GL_MAX_GEOMETRY_INPUT_COMPONENTS>			},
361 			{ "gl_MaxGeometryOutputComponents",			getInteger<GL_MAX_GEOMETRY_OUTPUT_COMPONENTS>			},
362 			{ "gl_MaxGeometryImageUniforms",			getInteger<GL_MAX_GEOMETRY_IMAGE_UNIFORMS>				},
363 			{ "gl_MaxGeometryTextureImageUnits",		getInteger<GL_MAX_GEOMETRY_TEXTURE_IMAGE_UNITS>			},
364 			{ "gl_MaxGeometryOutputVertices",			getInteger<GL_MAX_GEOMETRY_OUTPUT_VERTICES>				},
365 			{ "gl_MaxGeometryTotalOutputComponents",	getInteger<GL_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS>		},
366 			{ "gl_MaxGeometryUniformComponents",		getInteger<GL_MAX_GEOMETRY_UNIFORM_COMPONENTS>			},
367 			{ "gl_MaxGeometryAtomicCounters",			getInteger<GL_MAX_GEOMETRY_ATOMIC_COUNTERS>				},
368 			{ "gl_MaxGeometryAtomicCounterBuffers",		getInteger<GL_MAX_GEOMETRY_ATOMIC_COUNTER_BUFFERS>		},
369 		};
370 
371 		tcu::TestCaseGroup* const geomGroup = new tcu::TestCaseGroup(m_testCtx, "geometry_shader", "GL_EXT_geometry_shader");
372 		addChild(geomGroup);
373 
374 		for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(intConstants); ndx++)
375 			geomGroup->addChild(new ShaderBuiltinConstantCase<int>(m_context, intConstants[ndx].varName, intConstants[ndx].getValue, "GL_EXT_geometry_shader"));
376 	}
377 
378 	// EXT_tessellation_shader
379 	{
380 		static const struct
381 		{
382 			const char*												varName;
383 			ShaderBuiltinConstantCase<int>::GetConstantValueFunc	getValue;
384 		} intConstants[] =
385 		{
386 			{ "gl_MaxTessControlInputComponents",			getInteger<GL_MAX_TESS_CONTROL_INPUT_COMPONENTS>		},
387 			{ "gl_MaxTessControlOutputComponents",			getInteger<GL_MAX_TESS_CONTROL_OUTPUT_COMPONENTS>		},
388 			{ "gl_MaxTessControlTextureImageUnits",			getInteger<GL_MAX_TESS_CONTROL_TEXTURE_IMAGE_UNITS>		},
389 			{ "gl_MaxTessControlUniformComponents",			getInteger<GL_MAX_TESS_CONTROL_UNIFORM_COMPONENTS>		},
390 			{ "gl_MaxTessControlTotalOutputComponents",		getInteger<GL_MAX_TESS_CONTROL_TOTAL_OUTPUT_COMPONENTS>	},
391 
392 			{ "gl_MaxTessEvaluationInputComponents",		getInteger<GL_MAX_TESS_EVALUATION_INPUT_COMPONENTS>		},
393 			{ "gl_MaxTessEvaluationOutputComponents",		getInteger<GL_MAX_TESS_EVALUATION_OUTPUT_COMPONENTS>	},
394 			{ "gl_MaxTessEvaluationTextureImageUnits",		getInteger<GL_MAX_TESS_EVALUATION_TEXTURE_IMAGE_UNITS>	},
395 			{ "gl_MaxTessEvaluationUniformComponents",		getInteger<GL_MAX_TESS_EVALUATION_UNIFORM_COMPONENTS>	},
396 
397 			{ "gl_MaxTessPatchComponents",					getInteger<GL_MAX_TESS_PATCH_COMPONENTS>				},
398 
399 			{ "gl_MaxPatchVertices",						getInteger<GL_MAX_PATCH_VERTICES>						},
400 			{ "gl_MaxTessGenLevel",							getInteger<GL_MAX_TESS_GEN_LEVEL>						},
401 		};
402 
403 		tcu::TestCaseGroup* const tessGroup = new tcu::TestCaseGroup(m_testCtx, "tessellation_shader", "GL_EXT_tessellation_shader");
404 		addChild(tessGroup);
405 
406 		for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(intConstants); ndx++)
407 			tessGroup->addChild(new ShaderBuiltinConstantCase<int>(m_context, intConstants[ndx].varName, intConstants[ndx].getValue, "GL_EXT_tessellation_shader"));
408 	}
409 }
410 
411 } // Functional
412 } // gles31
413 } // deqp
414