• 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 		m_textures.push_back(brickTexture);
93 	}
94 }
95 
~ShaderDiscardCaseInstance(void)96 ShaderDiscardCaseInstance::~ShaderDiscardCaseInstance (void)
97 {
98 }
99 
100 class ShaderDiscardCase : public ShaderRenderCase
101 {
102 public:
103 							ShaderDiscardCase			(tcu::TestContext&		testCtx,
104 														 const char*			name,
105 														 const char*			description,
106 														 const char*			shaderSource,
107 														 const ShaderEvalFunc	evalFunc,
108 														 bool					usesTexture,
109 														 bool					fuzzyCompare,
110 														 bool					demote);
createInstance(Context & context) const111 	virtual TestInstance*	createInstance				(Context& context) const
112 							{
113 								DE_ASSERT(m_evaluator != DE_NULL);
114 								DE_ASSERT(m_uniformSetup != DE_NULL);
115 								return new ShaderDiscardCaseInstance(context, m_isVertexCase, *m_evaluator, *m_uniformSetup, m_usesTexture, m_fuzzyCompare);
116 							}
117 
118 	virtual void				checkSupport					(Context& context) const;
119 
120 private:
121 	const bool				m_usesTexture;
122 	const bool				m_fuzzyCompare;
123 	const bool				m_demote;
124 };
125 
ShaderDiscardCase(tcu::TestContext & testCtx,const char * name,const char * description,const char * shaderSource,const ShaderEvalFunc evalFunc,bool usesTexture,bool fuzzyCompare,bool demote)126 ShaderDiscardCase::ShaderDiscardCase (tcu::TestContext&		testCtx,
127 									  const char*			name,
128 									  const char*			description,
129 									  const char*			shaderSource,
130 									  const ShaderEvalFunc	evalFunc,
131 									  bool					usesTexture,
132 									  bool					fuzzyCompare,
133 									  bool					demote)
134 	: ShaderRenderCase	(testCtx, name, description, false, evalFunc, new SamplerUniformSetup(usesTexture), DE_NULL)
135 	, m_usesTexture		(usesTexture)
136 	, m_fuzzyCompare	(fuzzyCompare)
137 	, m_demote			(demote)
138 {
139 	m_fragShaderSource	= shaderSource;
140 	m_vertShaderSource	=
141 		"#version 310 es\n"
142 		"layout(location=0) in  highp   vec4 a_position;\n"
143 		"layout(location=1) in  highp   vec4 a_coords;\n"
144 		"layout(location=2) in  highp   vec4 a_one;\n"
145 		"layout(location=0) out mediump vec4 v_color;\n"
146 		"layout(location=1) out mediump vec4 v_coords;\n\n"
147 		"layout(location=2) out mediump vec4 v_one;\n"
148 		"void main (void)\n"
149 		"{\n"
150 		"    gl_Position = a_position;\n"
151 		"    v_color = vec4(a_coords.xyz, 1.0);\n"
152 		"    v_coords = a_coords;\n"
153 		"    v_one = a_one;\n"
154 		"}\n";
155 }
156 
checkSupport(Context & context) const157 void ShaderDiscardCase::checkSupport(Context& context) const
158 {
159 	if (m_demote && !context.getShaderDemoteToHelperInvocationFeaturesEXT().shaderDemoteToHelperInvocation)
160 		TCU_THROW(NotSupportedError, "VK_EXT_shader_demote_to_helper_invocation not supported");
161 }
162 
163 enum DiscardMode
164 {
165 	DISCARDMODE_ALWAYS = 0,
166 	DISCARDMODE_NEVER,
167 	DISCARDMODE_UNIFORM,
168 	DISCARDMODE_DYNAMIC,
169 	DISCARDMODE_TEXTURE,
170 	DISCARDMODE_DERIV,
171 
172 	DISCARDMODE_LAST
173 };
174 
175 enum DiscardTemplate
176 {
177 	DISCARDTEMPLATE_MAIN_BASIC = 0,
178 	DISCARDTEMPLATE_FUNCTION_BASIC,
179 	DISCARDTEMPLATE_MAIN_STATIC_LOOP,
180 	DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP,
181 	DISCARDTEMPLATE_FUNCTION_STATIC_LOOP,
182 
183 	DISCARDTEMPLATE_LAST
184 };
185 
186 // Evaluation functions
evalDiscardAlways(ShaderEvalContext & c)187 inline void evalDiscardAlways	(ShaderEvalContext& c) { c.discard(); }
evalDiscardNever(ShaderEvalContext & c)188 inline void evalDiscardNever	(ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2); }
evalDiscardDynamic(ShaderEvalContext & c)189 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(); }
190 
evalDiscardTexture(ShaderEvalContext & c)191 inline void evalDiscardTexture (ShaderEvalContext& c)
192 {
193 	c.color.xyz() = c.coords.swizzle(0,1,2);
194 	if (c.texture2D(0, c.coords.swizzle(0,1) * 0.25f + 0.5f).x() < 0.7f)
195 		c.discard();
196 }
197 
getEvalFunc(DiscardMode mode)198 static ShaderEvalFunc getEvalFunc (DiscardMode mode)
199 {
200 	switch (mode)
201 	{
202 		case DISCARDMODE_ALWAYS:	return evalDiscardAlways;
203 		case DISCARDMODE_NEVER:		return evalDiscardNever;
204 		case DISCARDMODE_UNIFORM:	return evalDiscardAlways;
205 		case DISCARDMODE_DYNAMIC:	return evalDiscardDynamic;
206 		case DISCARDMODE_TEXTURE:	return evalDiscardTexture;
207 		case DISCARDMODE_DERIV:		return evalDiscardAlways;
208 		default:
209 			DE_ASSERT(DE_FALSE);
210 			return evalDiscardAlways;
211 	}
212 }
213 
getTemplate(DiscardTemplate variant)214 static const char* getTemplate (DiscardTemplate variant)
215 {
216 	#define GLSL_SHADER_TEMPLATE_HEADER \
217 				"#version 310 es\n"	\
218 				"#extension GL_EXT_demote_to_helper_invocation : enable\n"	\
219 				"layout(location = 0) in mediump vec4 v_color;\n"	\
220 				"layout(location = 1) in mediump vec4 v_coords;\n"	\
221 				"layout(location = 2) in mediump vec4 a_one;\n"	\
222 				"layout(location = 0) out mediump vec4 o_color;\n"	\
223 				"layout(set = 0, binding = 2) uniform sampler2D    ut_brick;\n"	\
224 				"layout(set = 0, binding = 0) uniform block0 { mediump int  ui_one; };\n\n"
225 
226 	switch (variant)
227 	{
228 		case DISCARDTEMPLATE_MAIN_BASIC:
229 			return GLSL_SHADER_TEMPLATE_HEADER
230 				   "void main (void)\n"
231 				   "{\n"
232 				   "    o_color = v_color;\n"
233 				   "    ${DISCARD};\n"
234 				   "}\n";
235 
236 		case DISCARDTEMPLATE_FUNCTION_BASIC:
237 			return GLSL_SHADER_TEMPLATE_HEADER
238 				   "void myfunc (void)\n"
239 				   "{\n"
240 				   "    ${DISCARD};\n"
241 				   "}\n\n"
242 				   "void main (void)\n"
243 				   "{\n"
244 				   "    o_color = v_color;\n"
245 				   "    myfunc();\n"
246 				   "}\n";
247 
248 		case DISCARDTEMPLATE_MAIN_STATIC_LOOP:
249 			return GLSL_SHADER_TEMPLATE_HEADER
250 				   "void main (void)\n"
251 				   "{\n"
252 				   "    o_color = v_color;\n"
253 				   "    for (int i = 0; i < 2; i++)\n"
254 				   "    {\n"
255 				   "        if (i > 0) {\n"
256 				   "            ${DISCARD};\n"
257 				   "        }\n"
258 				   "    }\n"
259 				   "}\n";
260 
261 		case DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP:
262 			return GLSL_SHADER_TEMPLATE_HEADER
263 				   "layout(set = 0, binding = 1) uniform block1 { mediump int  ui_two; };\n\n"
264 				   "void main (void)\n"
265 				   "{\n"
266 				   "    o_color = v_color;\n"
267 				   "    for (int i = 0; i < ui_two; i++)\n"
268 				   "    {\n"
269 				   "        if (i > 0) {\n"
270 				   "            ${DISCARD};\n"
271 				   "        }\n"
272 				   "    }\n"
273 				   "}\n";
274 
275 		case DISCARDTEMPLATE_FUNCTION_STATIC_LOOP:
276 			return GLSL_SHADER_TEMPLATE_HEADER
277 				   "void myfunc (void)\n"
278 				   "{\n"
279 				   "    for (int i = 0; i < 2; i++)\n"
280 				   "    {\n"
281 				   "        if (i > 0) {\n"
282 				   "            ${DISCARD};\n"
283 				   "        }\n"
284 				   "    }\n"
285 				   "}\n\n"
286 				   "void main (void)\n"
287 				   "{\n"
288 				   "    o_color = v_color;\n"
289 				   "    myfunc();\n"
290 				   "}\n";
291 
292 		default:
293 			DE_ASSERT(DE_FALSE);
294 			return DE_NULL;
295 	}
296 
297 	#undef GLSL_SHADER_TEMPLATE_HEADER
298 }
299 
getTemplateName(DiscardTemplate variant)300 static const char* getTemplateName (DiscardTemplate variant)
301 {
302 	switch (variant)
303 	{
304 		case DISCARDTEMPLATE_MAIN_BASIC:			return "basic";
305 		case DISCARDTEMPLATE_FUNCTION_BASIC:		return "function";
306 		case DISCARDTEMPLATE_MAIN_STATIC_LOOP:		return "static_loop";
307 		case DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP:		return "dynamic_loop";
308 		case DISCARDTEMPLATE_FUNCTION_STATIC_LOOP:	return "function_static_loop";
309 		default:
310 			DE_ASSERT(DE_FALSE);
311 			return DE_NULL;
312 	}
313 }
314 
getModeName(DiscardMode mode)315 static const char* getModeName (DiscardMode mode)
316 {
317 	switch (mode)
318 	{
319 		case DISCARDMODE_ALWAYS:	return "always";
320 		case DISCARDMODE_NEVER:		return "never";
321 		case DISCARDMODE_UNIFORM:	return "uniform";
322 		case DISCARDMODE_DYNAMIC:	return "dynamic";
323 		case DISCARDMODE_TEXTURE:	return "texture";
324 		case DISCARDMODE_DERIV:		return "deriv";
325 		default:
326 			DE_ASSERT(DE_FALSE);
327 			return DE_NULL;
328 	}
329 }
330 
getTemplateDesc(DiscardTemplate variant)331 static const char* getTemplateDesc (DiscardTemplate variant)
332 {
333 	switch (variant)
334 	{
335 		case DISCARDTEMPLATE_MAIN_BASIC:			return "main";
336 		case DISCARDTEMPLATE_FUNCTION_BASIC:		return "function";
337 		case DISCARDTEMPLATE_MAIN_STATIC_LOOP:		return "static loop";
338 		case DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP:		return "dynamic loop";
339 		case DISCARDTEMPLATE_FUNCTION_STATIC_LOOP:	return "static loop in function";
340 		default:
341 			DE_ASSERT(DE_FALSE);
342 			return DE_NULL;
343 	}
344 }
345 
getModeDesc(DiscardMode mode)346 static const char* getModeDesc (DiscardMode mode)
347 {
348 	switch (mode)
349 	{
350 		case DISCARDMODE_ALWAYS:	return "Always discard";
351 		case DISCARDMODE_NEVER:		return "Never discard";
352 		case DISCARDMODE_UNIFORM:	return "Discard based on uniform value";
353 		case DISCARDMODE_DYNAMIC:	return "Discard based on varying values";
354 		case DISCARDMODE_TEXTURE:	return "Discard based on texture value";
355 		case DISCARDMODE_DERIV:		return "Discard based on derivatives after an earlier discard";
356 		default:
357 			DE_ASSERT(DE_FALSE);
358 			return DE_NULL;
359 	}
360 }
361 
makeDiscardCase(tcu::TestContext & testCtx,DiscardTemplate tmpl,DiscardMode mode,const std::string & discardStr)362 de::MovePtr<ShaderDiscardCase> makeDiscardCase (tcu::TestContext& testCtx, DiscardTemplate tmpl, DiscardMode mode, const std::string& discardStr)
363 {
364 	StringTemplate shaderTemplate(getTemplate(tmpl));
365 
366 	std::map<std::string, std::string> params;
367 
368 	switch (mode)
369 	{
370 		case DISCARDMODE_ALWAYS:	params["DISCARD"] = discardStr;																break;
371 		case DISCARDMODE_NEVER:		params["DISCARD"] = "if (false) " + discardStr;												break;
372 		case DISCARDMODE_UNIFORM:	params["DISCARD"] = "if (ui_one > 0) " + discardStr;										break;
373 		case DISCARDMODE_DYNAMIC:	params["DISCARD"] = "if (v_coords.x+v_coords.y > 0.0) " + discardStr;						break;
374 		case DISCARDMODE_TEXTURE:	params["DISCARD"] = "if (texture(ut_brick, v_coords.xy*0.25+0.5).x < 0.7) " + discardStr;	break;
375 		case DISCARDMODE_DERIV:		params["DISCARD"] =
376 										// First demote pixels where fragCoord.xy LSBs are not both zero, leaving only one
377 										// non-helper pixel per quad. Then compute derivatives of "one+fragCoord" and check they
378 										// are 0 or 1 as appropriate. Also check that helperInvocationEXT varies in the quad and
379 										// is false on non-helper pixels. Demote the pixel if it gets the right values, so the final
380 										// image should be entirely the clear color. If we don't get the right values, output red.
381 										// This test case would not work for discard, because derivatives become undefined.
382 										"  ivec2 f = ivec2(gl_FragCoord.xy);\n"
383 										"  int lsb = (f.x | f.y)&1;\n"
384 										"  if (lsb != 0) demote;\n"
385 										"  bool isHelper = helperInvocationEXT();\n"
386 										"  highp vec2 dx = dFdx(a_one.xy + gl_FragCoord.xy);\n"
387 										"  highp vec2 dy = dFdy(a_one.xy + gl_FragCoord.xy);\n"
388 										"  highp float dh = dFdx(float(isHelper));\n"
389 										"  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"
390 										"  if (valid) demote;\n"
391 										"  o_color = vec4(1,0,0,1);\n";
392 										break;
393 		default:
394 			DE_ASSERT(DE_FALSE);
395 			break;
396 	}
397 
398 	std::string name		= std::string(getTemplateName(tmpl)) + "_" + getModeName(mode);
399 	std::string description	= std::string(getModeDesc(mode)) + " in " + getTemplateDesc(tmpl);
400 
401 	return de::MovePtr<ShaderDiscardCase>(new ShaderDiscardCase(testCtx, name.c_str(),
402 																description.c_str(),
403 																shaderTemplate.specialize(params).c_str(),
404 																getEvalFunc(mode),
405 																mode == DISCARDMODE_TEXTURE,		// usesTexture
406 																mode != DISCARDMODE_DERIV,			// fuzzyCompare
407 																discardStr == "demote"));			// demote
408 }
409 
410 class ShaderDiscardTests : public tcu::TestCaseGroup
411 {
412 public:
413 							ShaderDiscardTests		(tcu::TestContext& textCtx, const char *groupName);
414 	virtual					~ShaderDiscardTests		(void);
415 
416 	virtual void			init					(void);
417 
418 private:
419 							ShaderDiscardTests		(const ShaderDiscardTests&);		// not allowed!
420 	ShaderDiscardTests&		operator=				(const ShaderDiscardTests&);		// not allowed!
421 	const std::string		m_groupName;
422 };
423 
ShaderDiscardTests(tcu::TestContext & testCtx,const char * groupName)424 ShaderDiscardTests::ShaderDiscardTests (tcu::TestContext& testCtx, const char *groupName)
425 	: TestCaseGroup(testCtx, groupName, "Discard statement tests")
426 	, m_groupName(groupName)
427 {
428 }
429 
~ShaderDiscardTests(void)430 ShaderDiscardTests::~ShaderDiscardTests (void)
431 {
432 }
433 
init(void)434 void ShaderDiscardTests::init (void)
435 {
436 	for (int tmpl = 0; tmpl < DISCARDTEMPLATE_LAST; tmpl++)
437 	{
438 		for (int mode = 0; mode < DISCARDMODE_LAST; mode++)
439 		{
440 			if (mode == DISCARDMODE_DERIV && m_groupName == "discard")
441 				continue;
442 			addChild(makeDiscardCase(m_testCtx, (DiscardTemplate)tmpl, (DiscardMode)mode, m_groupName).release());
443 		}
444 	}
445 }
446 
447 } // anonymous
448 
createDiscardTests(tcu::TestContext & testCtx)449 tcu::TestCaseGroup* createDiscardTests (tcu::TestContext& testCtx)
450 {
451 	return new ShaderDiscardTests(testCtx, "discard");
452 }
453 
createDemoteTests(tcu::TestContext & testCtx)454 tcu::TestCaseGroup* createDemoteTests (tcu::TestContext& testCtx)
455 {
456 	return new ShaderDiscardTests(testCtx, "demote");
457 }
458 
459 } // sr
460 } // vkt
461