• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
7  * Copyright (c) 2016 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Shader discard statement tests.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "vktShaderRenderDiscardTests.hpp"
27 #include "vktShaderRender.hpp"
28 #include "tcuStringTemplate.hpp"
29 #include "gluTexture.hpp"
30 
31 #include <string>
32 
33 using tcu::StringTemplate;
34 
35 namespace vkt
36 {
37 namespace sr
38 {
39 namespace
40 {
41 
42 class SamplerUniformSetup : public UniformSetup
43 {
44 public:
SamplerUniformSetup(bool useSampler)45 						SamplerUniformSetup			(bool useSampler)
46 							: m_useSampler(useSampler)
47 						{}
48 
setup(ShaderRenderCaseInstance & instance,const tcu::Vec4 &) const49 	virtual void		setup						 (ShaderRenderCaseInstance& instance, const tcu::Vec4&) const
50 						{
51 							instance.useUniform(0u, UI_ONE);
52 							instance.useUniform(1u, UI_TWO);
53 							if (m_useSampler)
54 								instance.useSampler(2u, 0u); // To the uniform binding location 2 bind the texture 0
55 						}
56 
57 private:
58 	const bool			m_useSampler;
59 };
60 
61 
62 class ShaderDiscardCaseInstance : public ShaderRenderCaseInstance
63 {
64 public:
65 						ShaderDiscardCaseInstance	(Context&				context,
66 													bool					isVertexCase,
67 													const ShaderEvaluator&	evaluator,
68 													const UniformSetup&		uniformSetup,
69 													bool					usesTexture,
70 													bool					fuzzyCompare);
71 	virtual				~ShaderDiscardCaseInstance	(void);
72 };
73 
ShaderDiscardCaseInstance(Context & context,bool isVertexCase,const ShaderEvaluator & evaluator,const UniformSetup & uniformSetup,bool usesTexture,bool fuzzyCompare)74 ShaderDiscardCaseInstance::ShaderDiscardCaseInstance (Context&					context,
75 													 bool						isVertexCase,
76 													 const ShaderEvaluator&		evaluator,
77 													 const UniformSetup&		uniformSetup,
78 													 bool						usesTexture,
79 													 bool						fuzzyCompare)
80 	: ShaderRenderCaseInstance	(context, isVertexCase, evaluator, uniformSetup, DE_NULL, IMAGE_BACKING_MODE_REGULAR, static_cast<deUint32>(GRID_SIZE_DEFAULTS), fuzzyCompare)
81 {
82 	if (usesTexture)
83 	{
84 		de::SharedPtr<TextureBinding> brickTexture(new TextureBinding(m_context.getTestContext().getArchive(),
85 																	  "vulkan/data/brick.png",
86 																	  TextureBinding::TYPE_2D,
87 																	  tcu::Sampler(tcu::Sampler::CLAMP_TO_EDGE,
88 																					tcu::Sampler::CLAMP_TO_EDGE,
89 																					tcu::Sampler::CLAMP_TO_EDGE,
90 																					tcu::Sampler::LINEAR,
91 																					tcu::Sampler::LINEAR,
92 																					0.0f,
93 																					true,
94 																					tcu::Sampler::COMPAREMODE_NONE,
95 																					0,
96 																					tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f),
97 																					true)));
98 		m_textures.push_back(brickTexture);
99 	}
100 }
101 
~ShaderDiscardCaseInstance(void)102 ShaderDiscardCaseInstance::~ShaderDiscardCaseInstance (void)
103 {
104 }
105 
106 class ShaderDiscardCase : public ShaderRenderCase
107 {
108 public:
109 							ShaderDiscardCase			(tcu::TestContext&		testCtx,
110 														 const char*			name,
111 														 const char*			shaderSource,
112 														 const ShaderEvalFunc	evalFunc,
113 														 bool					usesTexture,
114 														 bool					fuzzyCompare,
115 														 bool					demote);
createInstance(Context & context) const116 	virtual TestInstance*	createInstance				(Context& context) const
117 							{
118 								DE_ASSERT(m_evaluator != DE_NULL);
119 								DE_ASSERT(m_uniformSetup != DE_NULL);
120 								return new ShaderDiscardCaseInstance(context, m_isVertexCase, *m_evaluator, *m_uniformSetup, m_usesTexture, m_fuzzyCompare);
121 							}
122 
123 	virtual void				checkSupport					(Context& context) const;
124 
125 private:
126 	const bool				m_usesTexture;
127 	const bool				m_fuzzyCompare;
128 #ifndef CTS_USES_VULKANSC
129 	const bool				m_demote;
130 #endif // CTS_USES_VULKANSC
131 };
132 
ShaderDiscardCase(tcu::TestContext & testCtx,const char * name,const char * shaderSource,const ShaderEvalFunc evalFunc,bool usesTexture,bool fuzzyCompare,bool demote)133 ShaderDiscardCase::ShaderDiscardCase (tcu::TestContext&		testCtx,
134 									  const char*			name,
135 									  const char*			shaderSource,
136 									  const ShaderEvalFunc	evalFunc,
137 									  bool					usesTexture,
138 									  bool					fuzzyCompare,
139 									  bool					demote)
140 	: ShaderRenderCase	(testCtx, name, false, evalFunc, new SamplerUniformSetup(usesTexture), DE_NULL)
141 	, m_usesTexture		(usesTexture)
142 	, m_fuzzyCompare	(fuzzyCompare)
143 #ifndef CTS_USES_VULKANSC
144 	, m_demote(demote)
145 #endif // CTS_USES_VULKANSC
146 {
147 #ifdef CTS_USES_VULKANSC
148 	DE_UNREF(demote);
149 #endif // CTS_USES_VULKANSC
150 
151 	m_fragShaderSource	= shaderSource;
152 	m_vertShaderSource	=
153 		"#version 310 es\n"
154 		"layout(location=0) in  highp   vec4 a_position;\n"
155 		"layout(location=1) in  highp   vec4 a_coords;\n"
156 		"layout(location=2) in  highp   vec4 a_one;\n"
157 		"layout(location=0) out mediump vec4 v_color;\n"
158 		"layout(location=1) out mediump vec4 v_coords;\n\n"
159 		"layout(location=2) out mediump vec4 v_one;\n"
160 		"void main (void)\n"
161 		"{\n"
162 		"    gl_Position = a_position;\n"
163 		"    v_color = vec4(a_coords.xyz, 1.0);\n"
164 		"    v_coords = a_coords;\n"
165 		"    v_one = a_one;\n"
166 		"}\n";
167 }
168 
checkSupport(Context & context) const169 void ShaderDiscardCase::checkSupport(Context& context) const
170 {
171 #ifndef CTS_USES_VULKANSC
172 	if (m_demote && !context.getShaderDemoteToHelperInvocationFeatures().shaderDemoteToHelperInvocation)
173 		TCU_THROW(NotSupportedError, "VK_EXT_shader_demote_to_helper_invocation not supported");
174 #else
175 	DE_UNREF(context);
176 #endif // CTS_USES_VULKANSC
177 }
178 
179 enum DiscardMode
180 {
181 	DISCARDMODE_ALWAYS = 0,
182 	DISCARDMODE_NEVER,
183 	DISCARDMODE_UNIFORM,
184 	DISCARDMODE_DYNAMIC,
185 	DISCARDMODE_TEXTURE,
186 	DISCARDMODE_DERIV,
187 
188 	DISCARDMODE_LAST
189 };
190 
191 enum DiscardTemplate
192 {
193 	DISCARDTEMPLATE_MAIN_BASIC = 0,
194 	DISCARDTEMPLATE_FUNCTION_BASIC,
195 	DISCARDTEMPLATE_MAIN_STATIC_LOOP,
196 	DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP,
197 	DISCARDTEMPLATE_FUNCTION_STATIC_LOOP,
198 
199 	DISCARDTEMPLATE_LAST
200 };
201 
202 // Evaluation functions
evalDiscardAlways(ShaderEvalContext & c)203 inline void evalDiscardAlways	(ShaderEvalContext& c) { c.discard(); }
evalDiscardNever(ShaderEvalContext & c)204 inline void evalDiscardNever	(ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2); }
evalDiscardDynamic(ShaderEvalContext & c)205 inline void evalDiscardDynamic	(ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2); if (c.coords.x()+c.coords.y() > 0.0f) c.discard(); }
206 
evalDiscardTexture(ShaderEvalContext & c)207 inline void evalDiscardTexture (ShaderEvalContext& c)
208 {
209 	c.color.xyz() = c.coords.swizzle(0,1,2);
210 	if (c.texture2D(0, c.coords.swizzle(0,1) * 0.25f + 0.5f).x() < 0.7f)
211 		c.discard();
212 }
213 
getEvalFunc(DiscardMode mode)214 static ShaderEvalFunc getEvalFunc (DiscardMode mode)
215 {
216 	switch (mode)
217 	{
218 		case DISCARDMODE_ALWAYS:	return evalDiscardAlways;
219 		case DISCARDMODE_NEVER:		return evalDiscardNever;
220 		case DISCARDMODE_UNIFORM:	return evalDiscardAlways;
221 		case DISCARDMODE_DYNAMIC:	return evalDiscardDynamic;
222 		case DISCARDMODE_TEXTURE:	return evalDiscardTexture;
223 		case DISCARDMODE_DERIV:		return evalDiscardAlways;
224 		default:
225 			DE_ASSERT(DE_FALSE);
226 			return evalDiscardAlways;
227 	}
228 }
229 
getTemplate(DiscardTemplate variant)230 static const char* getTemplate (DiscardTemplate variant)
231 {
232 	#define GLSL_SHADER_TEMPLATE_HEADER \
233 				"#version 310 es\n"	\
234 				"#extension GL_EXT_demote_to_helper_invocation : enable\n"	\
235 				"layout(location = 0) in mediump vec4 v_color;\n"	\
236 				"layout(location = 1) in mediump vec4 v_coords;\n"	\
237 				"layout(location = 2) in mediump vec4 a_one;\n"	\
238 				"layout(location = 0) out mediump vec4 o_color;\n"	\
239 				"layout(set = 0, binding = 2) uniform sampler2D    ut_brick;\n"	\
240 				"layout(set = 0, binding = 0) uniform block0 { mediump int  ui_one; };\n\n"
241 
242 	switch (variant)
243 	{
244 		case DISCARDTEMPLATE_MAIN_BASIC:
245 			return GLSL_SHADER_TEMPLATE_HEADER
246 				   "void main (void)\n"
247 				   "{\n"
248 				   "    o_color = v_color;\n"
249 				   "    ${DISCARD};\n"
250 				   "}\n";
251 
252 		case DISCARDTEMPLATE_FUNCTION_BASIC:
253 			return GLSL_SHADER_TEMPLATE_HEADER
254 				   "void myfunc (void)\n"
255 				   "{\n"
256 				   "    ${DISCARD};\n"
257 				   "}\n\n"
258 				   "void main (void)\n"
259 				   "{\n"
260 				   "    o_color = v_color;\n"
261 				   "    myfunc();\n"
262 				   "}\n";
263 
264 		case DISCARDTEMPLATE_MAIN_STATIC_LOOP:
265 			return GLSL_SHADER_TEMPLATE_HEADER
266 				   "void main (void)\n"
267 				   "{\n"
268 				   "    o_color = v_color;\n"
269 				   "    for (int i = 0; i < 2; i++)\n"
270 				   "    {\n"
271 				   "        if (i > 0) {\n"
272 				   "            ${DISCARD};\n"
273 				   "        }\n"
274 				   "    }\n"
275 				   "}\n";
276 
277 		case DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP:
278 			return GLSL_SHADER_TEMPLATE_HEADER
279 				   "layout(set = 0, binding = 1) uniform block1 { mediump int  ui_two; };\n\n"
280 				   "void main (void)\n"
281 				   "{\n"
282 				   "    o_color = v_color;\n"
283 				   "    for (int i = 0; i < ui_two; i++)\n"
284 				   "    {\n"
285 				   "        if (i > 0) {\n"
286 				   "            ${DISCARD};\n"
287 				   "        }\n"
288 				   "    }\n"
289 				   "}\n";
290 
291 		case DISCARDTEMPLATE_FUNCTION_STATIC_LOOP:
292 			return GLSL_SHADER_TEMPLATE_HEADER
293 				   "void myfunc (void)\n"
294 				   "{\n"
295 				   "    for (int i = 0; i < 2; i++)\n"
296 				   "    {\n"
297 				   "        if (i > 0) {\n"
298 				   "            ${DISCARD};\n"
299 				   "        }\n"
300 				   "    }\n"
301 				   "}\n\n"
302 				   "void main (void)\n"
303 				   "{\n"
304 				   "    o_color = v_color;\n"
305 				   "    myfunc();\n"
306 				   "}\n";
307 
308 		default:
309 			DE_ASSERT(DE_FALSE);
310 			return DE_NULL;
311 	}
312 
313 	#undef GLSL_SHADER_TEMPLATE_HEADER
314 }
315 
getTemplateName(DiscardTemplate variant)316 static const char* getTemplateName (DiscardTemplate variant)
317 {
318 	switch (variant)
319 	{
320 		case DISCARDTEMPLATE_MAIN_BASIC:			return "basic";
321 		case DISCARDTEMPLATE_FUNCTION_BASIC:		return "function";
322 		case DISCARDTEMPLATE_MAIN_STATIC_LOOP:		return "static_loop";
323 		case DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP:		return "dynamic_loop";
324 		case DISCARDTEMPLATE_FUNCTION_STATIC_LOOP:	return "function_static_loop";
325 		default:
326 			DE_ASSERT(DE_FALSE);
327 			return DE_NULL;
328 	}
329 }
330 
getModeName(DiscardMode mode)331 static const char* getModeName (DiscardMode mode)
332 {
333 	switch (mode)
334 	{
335 		case DISCARDMODE_ALWAYS:	return "always";
336 		case DISCARDMODE_NEVER:		return "never";
337 		case DISCARDMODE_UNIFORM:	return "uniform";
338 		case DISCARDMODE_DYNAMIC:	return "dynamic";
339 		case DISCARDMODE_TEXTURE:	return "texture";
340 		case DISCARDMODE_DERIV:		return "deriv";
341 		default:
342 			DE_ASSERT(DE_FALSE);
343 			return DE_NULL;
344 	}
345 }
346 
makeDiscardCase(tcu::TestContext & testCtx,DiscardTemplate tmpl,DiscardMode mode,const std::string & discardStr)347 de::MovePtr<ShaderDiscardCase> makeDiscardCase (tcu::TestContext& testCtx, DiscardTemplate tmpl, DiscardMode mode, const std::string& discardStr)
348 {
349 	StringTemplate shaderTemplate(getTemplate(tmpl));
350 
351 	std::map<std::string, std::string> params;
352 
353 	switch (mode)
354 	{
355 		case DISCARDMODE_ALWAYS:	params["DISCARD"] = discardStr;																break;
356 		case DISCARDMODE_NEVER:		params["DISCARD"] = "if (false) " + discardStr;												break;
357 		case DISCARDMODE_UNIFORM:	params["DISCARD"] = "if (ui_one > 0) " + discardStr;										break;
358 		case DISCARDMODE_DYNAMIC:	params["DISCARD"] = "if (v_coords.x+v_coords.y > 0.0) " + discardStr;						break;
359 		case DISCARDMODE_TEXTURE:	params["DISCARD"] = "if (texture(ut_brick, v_coords.xy*0.25+0.5).x < 0.7) " + discardStr;	break;
360 		case DISCARDMODE_DERIV:		params["DISCARD"] =
361 										// First demote pixels where fragCoord.xy LSBs are not both zero, leaving only one
362 										// non-helper pixel per quad. Then compute derivatives of "one+fragCoord" and check they
363 										// are 0 or 1 as appropriate. Also check that helperInvocationEXT varies in the quad and
364 										// is false on non-helper pixels. Demote the pixel if it gets the right values, so the final
365 										// image should be entirely the clear color. If we don't get the right values, output red.
366 										// This test case would not work for discard, because derivatives become undefined.
367 										"  ivec2 f = ivec2(gl_FragCoord.xy);\n"
368 										"  int lsb = (f.x | f.y)&1;\n"
369 										"  if (lsb != 0) demote;\n"
370 										"  bool isHelper = helperInvocationEXT();\n"
371 										"  highp vec2 dx = dFdx(a_one.xy + gl_FragCoord.xy);\n"
372 										"  highp vec2 dy = dFdy(a_one.xy + gl_FragCoord.xy);\n"
373 										"  highp float dh = dFdx(float(isHelper));\n"
374 										"  bool valid = abs(dx.x-1.0) < 0.01 && dx.y == 0.0 && dy.x == 0.0 && abs(dy.y-1.0) < 0.01 && abs(dh-1.0) < 0.1 && !isHelper;\n"
375 										"  if (valid) demote;\n"
376 										"  o_color = vec4(1,0,0,1);\n";
377 										break;
378 		default:
379 			DE_ASSERT(DE_FALSE);
380 			break;
381 	}
382 
383 	std::string name		= std::string(getTemplateName(tmpl)) + "_" + getModeName(mode);
384 
385 	return de::MovePtr<ShaderDiscardCase>(new ShaderDiscardCase(testCtx, name.c_str(),
386 																shaderTemplate.specialize(params).c_str(),
387 																getEvalFunc(mode),
388 																mode == DISCARDMODE_TEXTURE,		// usesTexture
389 																mode != DISCARDMODE_DERIV,			// fuzzyCompare
390 																discardStr == "demote"));			// demote
391 }
392 
393 class ShaderDiscardTests : public tcu::TestCaseGroup
394 {
395 public:
396 							ShaderDiscardTests		(tcu::TestContext& textCtx, const char *groupName);
397 	virtual					~ShaderDiscardTests		(void);
398 
399 	virtual void			init					(void);
400 
401 private:
402 							ShaderDiscardTests		(const ShaderDiscardTests&);		// not allowed!
403 	ShaderDiscardTests&		operator=				(const ShaderDiscardTests&);		// not allowed!
404 	const std::string		m_groupName;
405 };
406 
ShaderDiscardTests(tcu::TestContext & testCtx,const char * groupName)407 ShaderDiscardTests::ShaderDiscardTests (tcu::TestContext& testCtx, const char *groupName)
408 	: TestCaseGroup(testCtx, groupName)
409 	, m_groupName(groupName)
410 {
411 }
412 
~ShaderDiscardTests(void)413 ShaderDiscardTests::~ShaderDiscardTests (void)
414 {
415 }
416 
init(void)417 void ShaderDiscardTests::init (void)
418 {
419 	for (int tmpl = 0; tmpl < DISCARDTEMPLATE_LAST; tmpl++)
420 	{
421 		for (int mode = 0; mode < DISCARDMODE_LAST; mode++)
422 		{
423 			if (mode == DISCARDMODE_DERIV && m_groupName == "discard")
424 				continue;
425 			addChild(makeDiscardCase(m_testCtx, (DiscardTemplate)tmpl, (DiscardMode)mode, m_groupName).release());
426 		}
427 	}
428 }
429 
430 } // anonymous
431 
createDiscardTests(tcu::TestContext & testCtx)432 tcu::TestCaseGroup* createDiscardTests (tcu::TestContext& testCtx)
433 {
434 	return new ShaderDiscardTests(testCtx, "discard");
435 }
436 
createDemoteTests(tcu::TestContext & testCtx)437 tcu::TestCaseGroup* createDemoteTests (tcu::TestContext& testCtx)
438 {
439 	return new ShaderDiscardTests(testCtx, "demote");
440 }
441 
442 } // sr
443 } // vkt
444