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 virtual ~ShaderDiscardCaseInstance (void);
71 };
72
ShaderDiscardCaseInstance(Context & context,bool isVertexCase,const ShaderEvaluator & evaluator,const UniformSetup & uniformSetup,bool usesTexture)73 ShaderDiscardCaseInstance::ShaderDiscardCaseInstance (Context& context,
74 bool isVertexCase,
75 const ShaderEvaluator& evaluator,
76 const UniformSetup& uniformSetup,
77 bool usesTexture)
78 : ShaderRenderCaseInstance (context, isVertexCase, evaluator, uniformSetup, DE_NULL)
79 {
80 if (usesTexture)
81 {
82 de::SharedPtr<TextureBinding> brickTexture(new TextureBinding(m_context.getTestContext().getArchive(),
83 "vulkan/data/brick.png",
84 TextureBinding::TYPE_2D,
85 tcu::Sampler(tcu::Sampler::CLAMP_TO_EDGE,
86 tcu::Sampler::CLAMP_TO_EDGE,
87 tcu::Sampler::CLAMP_TO_EDGE,
88 tcu::Sampler::LINEAR,
89 tcu::Sampler::LINEAR)));
90 m_textures.push_back(brickTexture);
91 }
92 }
93
~ShaderDiscardCaseInstance(void)94 ShaderDiscardCaseInstance::~ShaderDiscardCaseInstance (void)
95 {
96 }
97
98 class ShaderDiscardCase : public ShaderRenderCase
99 {
100 public:
101 ShaderDiscardCase (tcu::TestContext& testCtx,
102 const char* name,
103 const char* description,
104 const char* shaderSource,
105 const ShaderEvalFunc evalFunc,
106 bool usesTexture);
createInstance(Context & context) const107 virtual TestInstance* createInstance (Context& context) const
108 {
109 DE_ASSERT(m_evaluator != DE_NULL);
110 DE_ASSERT(m_uniformSetup != DE_NULL);
111 return new ShaderDiscardCaseInstance(context, m_isVertexCase, *m_evaluator, *m_uniformSetup, m_usesTexture);
112 }
113
114 private:
115 const bool m_usesTexture;
116 };
117
ShaderDiscardCase(tcu::TestContext & testCtx,const char * name,const char * description,const char * shaderSource,const ShaderEvalFunc evalFunc,bool usesTexture)118 ShaderDiscardCase::ShaderDiscardCase (tcu::TestContext& testCtx,
119 const char* name,
120 const char* description,
121 const char* shaderSource,
122 const ShaderEvalFunc evalFunc,
123 bool usesTexture)
124 : ShaderRenderCase (testCtx, name, description, false, evalFunc, new SamplerUniformSetup(usesTexture), DE_NULL)
125 , m_usesTexture (usesTexture)
126 {
127 m_fragShaderSource = shaderSource;
128 m_vertShaderSource =
129 "#version 310 es\n"
130 "layout(location=0) in highp vec4 a_position;\n"
131 "layout(location=1) in highp vec4 a_coords;\n"
132 "layout(location=0) out mediump vec4 v_color;\n"
133 "layout(location=1) out mediump vec4 v_coords;\n\n"
134 "void main (void)\n"
135 "{\n"
136 " gl_Position = a_position;\n"
137 " v_color = vec4(a_coords.xyz, 1.0);\n"
138 " v_coords = a_coords;\n"
139 "}\n";
140 }
141
142
143 enum DiscardMode
144 {
145 DISCARDMODE_ALWAYS = 0,
146 DISCARDMODE_NEVER,
147 DISCARDMODE_UNIFORM,
148 DISCARDMODE_DYNAMIC,
149 DISCARDMODE_TEXTURE,
150
151 DISCARDMODE_LAST
152 };
153
154 enum DiscardTemplate
155 {
156 DISCARDTEMPLATE_MAIN_BASIC = 0,
157 DISCARDTEMPLATE_FUNCTION_BASIC,
158 DISCARDTEMPLATE_MAIN_STATIC_LOOP,
159 DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP,
160 DISCARDTEMPLATE_FUNCTION_STATIC_LOOP,
161
162 DISCARDTEMPLATE_LAST
163 };
164
165 // Evaluation functions
evalDiscardAlways(ShaderEvalContext & c)166 inline void evalDiscardAlways (ShaderEvalContext& c) { c.discard(); }
evalDiscardNever(ShaderEvalContext & c)167 inline void evalDiscardNever (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2); }
evalDiscardDynamic(ShaderEvalContext & c)168 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(); }
169
evalDiscardTexture(ShaderEvalContext & c)170 inline void evalDiscardTexture (ShaderEvalContext& c)
171 {
172 c.color.xyz() = c.coords.swizzle(0,1,2);
173 if (c.texture2D(0, c.coords.swizzle(0,1) * 0.25f + 0.5f).x() < 0.7f)
174 c.discard();
175 }
176
getEvalFunc(DiscardMode mode)177 static ShaderEvalFunc getEvalFunc (DiscardMode mode)
178 {
179 switch (mode)
180 {
181 case DISCARDMODE_ALWAYS: return evalDiscardAlways;
182 case DISCARDMODE_NEVER: return evalDiscardNever;
183 case DISCARDMODE_UNIFORM: return evalDiscardAlways;
184 case DISCARDMODE_DYNAMIC: return evalDiscardDynamic;
185 case DISCARDMODE_TEXTURE: return evalDiscardTexture;
186 default:
187 DE_ASSERT(DE_FALSE);
188 return evalDiscardAlways;
189 }
190 }
191
getTemplate(DiscardTemplate variant)192 static const char* getTemplate (DiscardTemplate variant)
193 {
194 #define GLSL_SHADER_TEMPLATE_HEADER \
195 "#version 310 es\n" \
196 "layout(location = 0) in mediump vec4 v_color;\n" \
197 "layout(location = 1) in mediump vec4 v_coords;\n" \
198 "layout(location = 0) out mediump vec4 o_color;\n" \
199 "layout(set = 0, binding = 2) uniform sampler2D ut_brick;\n" \
200 "layout(set = 0, binding = 0) uniform block0 { mediump int ui_one; };\n\n"
201
202 switch (variant)
203 {
204 case DISCARDTEMPLATE_MAIN_BASIC:
205 return GLSL_SHADER_TEMPLATE_HEADER
206 "void main (void)\n"
207 "{\n"
208 " o_color = v_color;\n"
209 " ${DISCARD};\n"
210 "}\n";
211
212 case DISCARDTEMPLATE_FUNCTION_BASIC:
213 return GLSL_SHADER_TEMPLATE_HEADER
214 "void myfunc (void)\n"
215 "{\n"
216 " ${DISCARD};\n"
217 "}\n\n"
218 "void main (void)\n"
219 "{\n"
220 " o_color = v_color;\n"
221 " myfunc();\n"
222 "}\n";
223
224 case DISCARDTEMPLATE_MAIN_STATIC_LOOP:
225 return GLSL_SHADER_TEMPLATE_HEADER
226 "void main (void)\n"
227 "{\n"
228 " o_color = v_color;\n"
229 " for (int i = 0; i < 2; i++)\n"
230 " {\n"
231 " if (i > 0)\n"
232 " ${DISCARD};\n"
233 " }\n"
234 "}\n";
235
236 case DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP:
237 return GLSL_SHADER_TEMPLATE_HEADER
238 "layout(set = 0, binding = 1) uniform block1 { mediump int ui_two; };\n\n"
239 "void main (void)\n"
240 "{\n"
241 " o_color = v_color;\n"
242 " for (int i = 0; i < ui_two; i++)\n"
243 " {\n"
244 " if (i > 0)\n"
245 " ${DISCARD};\n"
246 " }\n"
247 "}\n";
248
249 case DISCARDTEMPLATE_FUNCTION_STATIC_LOOP:
250 return GLSL_SHADER_TEMPLATE_HEADER
251 "void myfunc (void)\n"
252 "{\n"
253 " for (int i = 0; i < 2; i++)\n"
254 " {\n"
255 " if (i > 0)\n"
256 " ${DISCARD};\n"
257 " }\n"
258 "}\n\n"
259 "void main (void)\n"
260 "{\n"
261 " o_color = v_color;\n"
262 " myfunc();\n"
263 "}\n";
264
265 default:
266 DE_ASSERT(DE_FALSE);
267 return DE_NULL;
268 }
269
270 #undef GLSL_SHADER_TEMPLATE_HEADER
271 }
272
getTemplateName(DiscardTemplate variant)273 static const char* getTemplateName (DiscardTemplate variant)
274 {
275 switch (variant)
276 {
277 case DISCARDTEMPLATE_MAIN_BASIC: return "basic";
278 case DISCARDTEMPLATE_FUNCTION_BASIC: return "function";
279 case DISCARDTEMPLATE_MAIN_STATIC_LOOP: return "static_loop";
280 case DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP: return "dynamic_loop";
281 case DISCARDTEMPLATE_FUNCTION_STATIC_LOOP: return "function_static_loop";
282 default:
283 DE_ASSERT(DE_FALSE);
284 return DE_NULL;
285 }
286 }
287
getModeName(DiscardMode mode)288 static const char* getModeName (DiscardMode mode)
289 {
290 switch (mode)
291 {
292 case DISCARDMODE_ALWAYS: return "always";
293 case DISCARDMODE_NEVER: return "never";
294 case DISCARDMODE_UNIFORM: return "uniform";
295 case DISCARDMODE_DYNAMIC: return "dynamic";
296 case DISCARDMODE_TEXTURE: return "texture";
297 default:
298 DE_ASSERT(DE_FALSE);
299 return DE_NULL;
300 }
301 }
302
getTemplateDesc(DiscardTemplate variant)303 static const char* getTemplateDesc (DiscardTemplate variant)
304 {
305 switch (variant)
306 {
307 case DISCARDTEMPLATE_MAIN_BASIC: return "main";
308 case DISCARDTEMPLATE_FUNCTION_BASIC: return "function";
309 case DISCARDTEMPLATE_MAIN_STATIC_LOOP: return "static loop";
310 case DISCARDTEMPLATE_MAIN_DYNAMIC_LOOP: return "dynamic loop";
311 case DISCARDTEMPLATE_FUNCTION_STATIC_LOOP: return "static loop in function";
312 default:
313 DE_ASSERT(DE_FALSE);
314 return DE_NULL;
315 }
316 }
317
getModeDesc(DiscardMode mode)318 static const char* getModeDesc (DiscardMode mode)
319 {
320 switch (mode)
321 {
322 case DISCARDMODE_ALWAYS: return "Always discard";
323 case DISCARDMODE_NEVER: return "Never discard";
324 case DISCARDMODE_UNIFORM: return "Discard based on uniform value";
325 case DISCARDMODE_DYNAMIC: return "Discard based on varying values";
326 case DISCARDMODE_TEXTURE: return "Discard based on texture value";
327 default:
328 DE_ASSERT(DE_FALSE);
329 return DE_NULL;
330 }
331 }
332
makeDiscardCase(tcu::TestContext & testCtx,DiscardTemplate tmpl,DiscardMode mode)333 de::MovePtr<ShaderDiscardCase> makeDiscardCase (tcu::TestContext& testCtx, DiscardTemplate tmpl, DiscardMode mode)
334 {
335 StringTemplate shaderTemplate(getTemplate(tmpl));
336
337 std::map<std::string, std::string> params;
338
339 switch (mode)
340 {
341 case DISCARDMODE_ALWAYS: params["DISCARD"] = "discard"; break;
342 case DISCARDMODE_NEVER: params["DISCARD"] = "if (false) discard"; break;
343 case DISCARDMODE_UNIFORM: params["DISCARD"] = "if (ui_one > 0) discard"; break;
344 case DISCARDMODE_DYNAMIC: params["DISCARD"] = "if (v_coords.x+v_coords.y > 0.0) discard"; break;
345 case DISCARDMODE_TEXTURE: params["DISCARD"] = "if (texture(ut_brick, v_coords.xy*0.25+0.5).x < 0.7) discard"; break;
346 default:
347 DE_ASSERT(DE_FALSE);
348 break;
349 }
350
351 std::string name = std::string(getTemplateName(tmpl)) + "_" + getModeName(mode);
352 std::string description = std::string(getModeDesc(mode)) + " in " + getTemplateDesc(tmpl);
353
354 return de::MovePtr<ShaderDiscardCase>(new ShaderDiscardCase(testCtx, name.c_str(), description.c_str(), shaderTemplate.specialize(params).c_str(), getEvalFunc(mode), mode == DISCARDMODE_TEXTURE));
355 }
356
357 class ShaderDiscardTests : public tcu::TestCaseGroup
358 {
359 public:
360 ShaderDiscardTests (tcu::TestContext& textCtx);
361 virtual ~ShaderDiscardTests (void);
362
363 virtual void init (void);
364
365 private:
366 ShaderDiscardTests (const ShaderDiscardTests&); // not allowed!
367 ShaderDiscardTests& operator= (const ShaderDiscardTests&); // not allowed!
368 };
369
ShaderDiscardTests(tcu::TestContext & testCtx)370 ShaderDiscardTests::ShaderDiscardTests (tcu::TestContext& testCtx)
371 : TestCaseGroup(testCtx, "discard", "Discard statement tests")
372 {
373 }
374
~ShaderDiscardTests(void)375 ShaderDiscardTests::~ShaderDiscardTests (void)
376 {
377 }
378
init(void)379 void ShaderDiscardTests::init (void)
380 {
381 for (int tmpl = 0; tmpl < DISCARDTEMPLATE_LAST; tmpl++)
382 for (int mode = 0; mode < DISCARDMODE_LAST; mode++)
383 addChild(makeDiscardCase(m_testCtx, (DiscardTemplate)tmpl, (DiscardMode)mode).release());
384 }
385
386 } // anonymous
387
createDiscardTests(tcu::TestContext & testCtx)388 tcu::TestCaseGroup* createDiscardTests (tcu::TestContext& testCtx)
389 {
390 return new ShaderDiscardTests(testCtx);
391 }
392
393 } // sr
394 } // vkt
395