1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
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
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * \brief Shader switch statement tests.
26 * + Selection expression type: static, uniform, dynamic
27 * + Switch layout - fall-through or use of default label
28 * + Switch nested in loop/conditional statement
29 * + Loop/conditional statement nested in switch
30 *//*--------------------------------------------------------------------*/
32 #include "vktShaderRenderSwitchTests.hpp"
33 #include "vktShaderRender.hpp"
34 #include "tcuStringTemplate.hpp"
44 static void setUniforms(ShaderRenderCaseInstance& instance, const tcu::Vec4&)
46 instance.useUniform(0u, UI_TWO);
51 class ShaderSwitchCase : public ShaderRenderCase
54 ShaderSwitchCase (tcu::TestContext& testCtx,
56 const string& description,
58 const string& vtxSource,
59 const string& fragSource,
60 ShaderEvalFunc evalFunc,
61 UniformSetupFunc setupUniformsFunc);
62 virtual ~ShaderSwitchCase (void);
65 ShaderSwitchCase::ShaderSwitchCase (tcu::TestContext& testCtx,
67 const string& description,
69 const string& vtxSource,
70 const string& fragSource,
71 ShaderEvalFunc evalFunc,
72 UniformSetupFunc setupUniformsFunc)
73 : ShaderRenderCase (testCtx, name, description, isVertexCase, evalFunc, new UniformSetup(setupUniformsFunc), DE_NULL)
75 m_vertShaderSource = vtxSource;
76 m_fragShaderSource = fragSource;
79 ShaderSwitchCase::~ShaderSwitchCase (void)
85 SWITCHTYPE_STATIC = 0,
92 static void evalSwitchStatic (ShaderEvalContext& evalCtx) { evalCtx.color.xyz() = evalCtx.coords.swizzle(1,2,3); }
93 static void evalSwitchUniform (ShaderEvalContext& evalCtx) { evalCtx.color.xyz() = evalCtx.coords.swizzle(1,2,3); }
94 static void evalSwitchDynamic (ShaderEvalContext& evalCtx)
96 switch (int(deFloatFloor(evalCtx.coords.z()*1.5f + 2.0f)))
98 case 0: evalCtx.color.xyz() = evalCtx.coords.swizzle(0,1,2); break;
99 case 1: evalCtx.color.xyz() = evalCtx.coords.swizzle(3,2,1); break;
100 case 2: evalCtx.color.xyz() = evalCtx.coords.swizzle(1,2,3); break;
101 case 3: evalCtx.color.xyz() = evalCtx.coords.swizzle(2,1,0); break;
102 default: evalCtx.color.xyz() = evalCtx.coords.swizzle(0,0,0); break;
106 static de::MovePtr<ShaderSwitchCase> makeSwitchCase (tcu::TestContext& testCtx, const string& name, const string& desc, SwitchType type, bool isVertex, const LineStream& switchBody)
108 std::ostringstream vtx;
109 std::ostringstream frag;
110 std::ostringstream& op = isVertex ? vtx : frag;
112 vtx << "#version 310 es\n"
113 << "layout(location = 0) in highp vec4 a_position;\n"
114 << "layout(location = 1) in highp vec4 a_coords;\n\n";
115 frag << "#version 310 es\n"
116 << "layout(location = 0) out mediump vec4 o_color;\n";
120 vtx << "layout(location = 0) out mediump vec4 v_color;\n";
121 frag << "layout(location = 0) in mediump vec4 v_color;\n";
125 vtx << "layout(location = 0) out highp vec4 v_coords;\n";
126 frag << "layout(location = 0) in highp vec4 v_coords;\n";
129 if (type == SWITCHTYPE_UNIFORM)
130 op << "layout (std140, set=0, binding=0) uniform buffer0 { highp int ui_two; };\n";
133 << "void main (void)\n"
135 << " gl_Position = a_position;\n";
137 << "void main (void)\n"
141 op << " highp vec4 coords = " << (isVertex ? "a_coords" : "v_coords") << ";\n";
142 op << " mediump vec3 res = vec3(0.0);\n\n";
145 std::map<string, string> params;
146 params["CONDITION"] = type == SWITCHTYPE_STATIC ? "2" :
147 type == SWITCHTYPE_UNIFORM ? "ui_two" :
148 type == SWITCHTYPE_DYNAMIC ? "int(floor(coords.z*1.5 + 2.0))" : "???";
150 op << tcu::StringTemplate(switchBody.str()).specialize(params);
155 vtx << " v_color = vec4(res, 1.0);\n";
156 frag << " o_color = v_color;\n";
160 vtx << " v_coords = a_coords;\n";
161 frag << " o_color = vec4(res, 1.0);\n";
167 return de::MovePtr<ShaderSwitchCase>(new ShaderSwitchCase(testCtx, name, desc, isVertex, vtx.str(), frag.str(),
168 type == SWITCHTYPE_STATIC ? evalSwitchStatic :
169 type == SWITCHTYPE_UNIFORM ? evalSwitchUniform :
170 type == SWITCHTYPE_DYNAMIC ? evalSwitchDynamic : (ShaderEvalFunc)DE_NULL,
171 type == SWITCHTYPE_UNIFORM ? setUniforms : DE_NULL));
174 class ShaderSwitchTests : public tcu::TestCaseGroup
177 ShaderSwitchTests (tcu::TestContext& context);
178 virtual ~ShaderSwitchTests (void);
180 virtual void init (void);
183 ShaderSwitchTests (const ShaderSwitchTests&); // not allowed!
184 ShaderSwitchTests& operator= (const ShaderSwitchTests&); // not allowed!
186 void makeSwitchCases (const string& name, const string& desc, const LineStream& switchBody, const bool skipDynamicType = false);
189 ShaderSwitchTests::ShaderSwitchTests (tcu::TestContext& testCtx)
190 : tcu::TestCaseGroup (testCtx, "switch", "Switch statement tests")
194 ShaderSwitchTests::~ShaderSwitchTests (void)
198 void ShaderSwitchTests::makeSwitchCases (const string& name, const string& desc, const LineStream& switchBody, const bool skipDynamicType)
200 static const char* switchTypeNames[] = { "static", "uniform", "dynamic" };
201 DE_STATIC_ASSERT(DE_LENGTH_OF_ARRAY(switchTypeNames) == SWITCHTYPE_LAST);
203 for (int type = 0; type < SWITCHTYPE_LAST; type++)
205 if (skipDynamicType && (type == SWITCHTYPE_DYNAMIC))
208 addChild(makeSwitchCase(m_testCtx, (name + "_" + switchTypeNames[type] + "_vertex"), desc, (SwitchType)type, true, switchBody).release());
209 addChild(makeSwitchCase(m_testCtx, (name + "_" + switchTypeNames[type] + "_fragment"), desc, (SwitchType)type, false, switchBody).release());
213 void ShaderSwitchTests::init (void)
215 // Expected swizzles:
221 makeSwitchCases("basic", "Basic switch statement usage",
223 << "switch (${CONDITION})"
225 << " case 0: res = coords.xyz; break;"
226 << " case 1: res = coords.wzy; break;"
227 << " case 2: res = coords.yzw; break;"
228 << " case 3: res = coords.zyx; break;"
231 makeSwitchCases("const_expr_in_label", "Constant expression in label",
233 << "const int t = 2;"
234 << "switch (${CONDITION})"
236 << " case int(0.0): res = coords.xyz; break;"
237 << " case 2-1: res = coords.wzy; break;"
238 << " case 3&(1<<1): res = coords.yzw; break;"
239 << " case t+1: res = coords.zyx; break;"
242 makeSwitchCases("default_label", "Default label usage",
244 << "switch (${CONDITION})"
246 << " case 0: res = coords.xyz; break;"
247 << " case 1: res = coords.wzy; break;"
248 << " case 3: res = coords.zyx; break;"
249 << " default: res = coords.yzw;"
252 makeSwitchCases("default_not_last", "Default label usage",
254 << "switch (${CONDITION})"
256 << " case 0: res = coords.xyz; break;"
257 << " default: res = coords.yzw; break;"
258 << " case 1: res = coords.wzy; break;"
259 << " case 3: res = coords.zyx; break;"
262 makeSwitchCases("no_default_label", "No match in switch without default label",
264 << "res = coords.yzw;\n"
265 << "switch (${CONDITION})"
267 << " case 0: res = coords.xyz; break;"
268 << " case 1: res = coords.wzy; break;"
269 << " case 3: res = coords.zyx; break;"
272 makeSwitchCases("default_only", "Default case only",
274 << "switch (${CONDITION})"
277 << " res = coords.yzw;"
280 makeSwitchCases("empty_case_default", "Empty case and default",
282 << "switch (${CONDITION})"
286 << " res = coords.yzw;"
289 makeSwitchCases("fall_through", "Fall-through",
291 << "switch (${CONDITION})"
293 << " case 0: res = coords.xyz; break;"
294 << " case 1: res = coords.wzy; break;"
295 << " case 2: coords = coords.yzwx;"
296 << " case 4: res = vec3(coords); break;"
297 << " case 3: res = coords.zyx; break;"
300 makeSwitchCases("fall_through_default", "Fall-through",
302 << "switch (${CONDITION})"
304 << " case 0: res = coords.xyz; break;"
305 << " case 1: res = coords.wzy; break;"
306 << " case 3: res = coords.zyx; break;"
307 << " case 2: coords = coords.yzwx;"
308 << " default: res = vec3(coords);"
311 makeSwitchCases("conditional_fall_through", "Fall-through",
313 << "highp vec4 tmp = coords;"
314 << "switch (${CONDITION})"
316 << " case 0: res = coords.xyz; break;"
317 << " case 1: res = coords.wzy; break;"
319 << " tmp = coords.yzwx;"
321 << " res = vec3(tmp);"
322 << " if (${CONDITION} != 3)"
324 << " default: res = tmp.zyx; break;"
327 makeSwitchCases("conditional_fall_through_2", "Fall-through",
329 << "highp vec4 tmp = coords;"
330 << "mediump int c = ${CONDITION};"
333 << " case 0: res = coords.xyz; break;"
334 << " case 1: res = coords.wzy; break;"
336 << " c += ${CONDITION};"
337 << " tmp = coords.yzwx;"
339 << " res = vec3(tmp);"
342 << " default: res = tmp.zyx; break;"
345 makeSwitchCases("scope", "Basic switch statement usage",
347 << "switch (${CONDITION})"
349 << " case 0: res = coords.xyz; break;"
350 << " case 1: res = coords.wzy; break;"
353 << " mediump vec3 t = coords.yzw;"
357 << " case 3: res = coords.zyx; break;"
360 makeSwitchCases("switch_in_if", "Switch in for loop",
362 << "if (${CONDITION} >= 0)"
364 << " switch (${CONDITION})"
366 << " case 0: res = coords.xyz; break;"
367 << " case 1: res = coords.wzy; break;"
368 << " case 2: res = coords.yzw; break;"
369 << " case 3: res = coords.zyx; break;"
373 makeSwitchCases("switch_in_for_loop", "Switch in for loop",
375 << "for (int i = 0; i <= ${CONDITION}; i++)"
379 << " case 0: res = coords.xyz; break;"
380 << " case 1: res = coords.wzy; break;"
381 << " case 2: res = coords.yzw; break;"
382 << " case 3: res = coords.zyx; break;"
387 makeSwitchCases("switch_in_while_loop", "Switch in while loop",
390 << "while (i <= ${CONDITION})"
394 << " case 0: res = coords.xyz; break;"
395 << " case 1: res = coords.wzy; break;"
396 << " case 2: res = coords.yzw; break;"
397 << " case 3: res = coords.zyx; break;"
402 makeSwitchCases("switch_in_do_while_loop", "Switch in do-while loop",
409 << " case 0: res = coords.xyz; break;"
410 << " case 1: res = coords.wzy; break;"
411 << " case 2: res = coords.yzw; break;"
412 << " case 3: res = coords.zyx; break;"
415 << "} while (i <= ${CONDITION});");
417 makeSwitchCases("if_in_switch", "Basic switch statement usage",
419 << "switch (${CONDITION})"
421 << " case 0: res = coords.xyz; break;"
422 << " case 1: res = coords.wzy; break;"
424 << " if (${CONDITION} == 2)"
425 << " res = coords.yzw;"
427 << " res = coords.zyx;"
431 makeSwitchCases("for_loop_in_switch", "Basic switch statement usage",
433 << "switch (${CONDITION})"
435 << " case 0: res = coords.xyz; break;"
439 << " highp vec3 t = coords.yzw;"
440 << " for (int i = 0; i < ${CONDITION}; i++)"
445 << " default: res = coords.zyx; break;"
448 makeSwitchCases("while_loop_in_switch", "Basic switch statement usage",
450 << "switch (${CONDITION})"
452 << " case 0: res = coords.xyz; break;"
456 << " highp vec3 t = coords.yzw;"
458 << " while (i < ${CONDITION})"
466 << " default: res = coords.zyx; break;"
469 makeSwitchCases("do_while_loop_in_switch", "Basic switch statement usage",
471 << "switch (${CONDITION})"
473 << " case 0: res = coords.xyz; break;"
477 << " highp vec3 t = coords.yzw;"
483 << " } while (i < ${CONDITION});"
487 << " default: res = coords.zyx; break;"
490 makeSwitchCases("switch_in_switch", "Basic switch statement usage",
492 << "switch (${CONDITION})"
494 << " case 0: res = coords.xyz; break;"
497 << " switch (${CONDITION} - 1)"
499 << " case 0: res = coords.wzy; break;"
500 << " case 1: res = coords.yzw; break;"
503 << " default: res = coords.zyx; break;"
509 tcu::TestCaseGroup* createSwitchTests (tcu::TestContext& testCtx)
511 return new ShaderSwitchTests(testCtx);