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 return statement tests.
24 *//*--------------------------------------------------------------------*/
26 #include "vktShaderRenderReturnTests.hpp"
27 #include "vktShaderRender.hpp"
28 #include "tcuStringTemplate.hpp"
42 RETURNMODE_ALWAYS = 0,
49 // Evaluation functions
50 inline void evalReturnAlways (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2); }
51 inline void evalReturnNever (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(3,2,1); }
52 inline void evalReturnDynamic (ShaderEvalContext& c) { c.color.xyz() = (c.coords.x()+c.coords.y() >= 0.0f) ? c.coords.swizzle(0,1,2) : c.coords.swizzle(3,2,1); }
54 static ShaderEvalFunc getEvalFunc (ReturnMode mode)
58 case RETURNMODE_ALWAYS: return evalReturnAlways;
59 case RETURNMODE_NEVER: return evalReturnNever;
60 case RETURNMODE_DYNAMIC: return evalReturnDynamic;
63 return (ShaderEvalFunc)DE_NULL;
67 class ShaderReturnCase : public ShaderRenderCase
70 ShaderReturnCase (tcu::TestContext& testCtx,
71 const std::string& name,
72 const std::string& description,
74 const std::string& shaderSource,
75 const ShaderEvalFunc evalFunc,
76 const UniformSetup* uniformFunc);
77 virtual ~ShaderReturnCase (void);
80 ShaderReturnCase::ShaderReturnCase (tcu::TestContext& testCtx,
81 const std::string& name,
82 const std::string& description,
84 const std::string& shaderSource,
85 const ShaderEvalFunc evalFunc,
86 const UniformSetup* uniformFunc)
87 : ShaderRenderCase(testCtx, name, description, isVertexCase, evalFunc, uniformFunc, DE_NULL)
91 m_vertShaderSource = shaderSource;
94 "layout(location = 0) in mediump vec4 v_color;\n"
95 "layout(location = 0) out mediump vec4 o_color;\n\n"
98 " o_color = v_color;\n"
103 m_fragShaderSource = shaderSource;
106 "layout(location = 0) in highp vec4 a_position;\n"
107 "layout(location = 1) in highp vec4 a_coords;\n"
108 "layout(location = 0) out mediump vec4 v_coords;\n\n"
111 " gl_Position = a_position;\n"
112 " v_coords = a_coords;\n"
117 ShaderReturnCase::~ShaderReturnCase (void)
121 class ReturnTestUniformSetup : public UniformSetup
124 ReturnTestUniformSetup (const BaseUniformType uniformType)
125 : m_uniformType(uniformType)
127 virtual void setup (ShaderRenderCaseInstance& instance, const tcu::Vec4&) const
129 instance.useUniform(0u, m_uniformType);
133 const BaseUniformType m_uniformType;
136 // Test case builders.
138 de::MovePtr<ShaderReturnCase> makeConditionalReturnInFuncCase (tcu::TestContext& context, const std::string& name, const std::string& description, ReturnMode returnMode, bool isVertex)
140 tcu::StringTemplate tmpl(
142 "layout(location = ${COORDLOC}) in ${COORDPREC} vec4 ${COORDS};\n"
144 "${COORDPREC} vec4 getColor (void)\n"
146 " if (${RETURNCOND})\n"
147 " return vec4(${COORDS}.xyz, 1.0);\n"
148 " return vec4(${COORDS}.wzy, 1.0);\n"
153 " ${OUTPUT} = getColor();\n"
156 const char* coords = isVertex ? "a_coords" : "v_coords";
158 std::map<std::string, std::string> params;
160 params["COORDLOC"] = isVertex ? "1" : "0";
161 params["COORDPREC"] = isVertex ? "highp" : "mediump";
162 params["OUTPUT"] = isVertex ? "v_color" : "o_color";
163 params["COORDS"] = coords;
164 params["EXTRADECL"] = isVertex ? "layout(location = 0) in highp vec4 a_position;\nlayout(location = 0) out mediump vec4 v_color;\n" : "layout(location = 0) out mediump vec4 o_color;\n";
165 params["POSITIONWRITE"] = isVertex ? " gl_Position = a_position;\n" : "";
169 case RETURNMODE_ALWAYS: params["RETURNCOND"] = "true"; break;
170 case RETURNMODE_NEVER: params["RETURNCOND"] = "false"; break;
171 case RETURNMODE_DYNAMIC: params["RETURNCOND"] = std::string(coords) + ".x+" + coords + ".y >= 0.0"; break;
172 default: DE_ASSERT(DE_FALSE);
175 return de::MovePtr<ShaderReturnCase>(new ShaderReturnCase(context, name, description, isVertex, tmpl.specialize(params), getEvalFunc(returnMode), DE_NULL));
178 de::MovePtr<ShaderReturnCase> makeOutputWriteReturnCase (tcu::TestContext& context, const std::string& name, const std::string& description, bool inFunction, ReturnMode returnMode, bool isVertex)
180 tcu::StringTemplate tmpl(
184 "layout(location = ${COORDLOC}) in ${COORDPREC} vec4 ${COORDS};\n"
186 "void myfunc (void)\n"
188 " ${OUTPUT} = vec4(${COORDS}.xyz, 1.0);\n"
189 " if (${RETURNCOND})\n"
191 " ${OUTPUT} = vec4(${COORDS}.wzy, 1.0);\n"
200 "layout(location = ${COORDLOC}) in ${COORDPREC} vec4 ${COORDS};\n"
205 " ${OUTPUT} = vec4(${COORDS}.xyz, 1.0);\n"
206 " if (${RETURNCOND})\n"
208 " ${OUTPUT} = vec4(${COORDS}.wzy, 1.0);\n"
211 const char* coords = isVertex ? "a_coords" : "v_coords";
213 std::map<std::string, std::string> params;
215 params["COORDLOC"] = isVertex ? "1" : "0";
216 params["COORDPREC"] = isVertex ? "highp" : "mediump";
217 params["COORDS"] = coords;
218 params["OUTPUT"] = isVertex ? "v_color" : "o_color";
219 params["EXTRADECL"] = isVertex ? "layout(location = 0) in highp vec4 a_position;\nlayout(location = 0) out mediump vec4 v_color;\n" : "layout(location = 0) out mediump vec4 o_color;\n";
220 params["POSITIONWRITE"] = isVertex ? " gl_Position = a_position;\n" : "";
224 case RETURNMODE_ALWAYS: params["RETURNCOND"] = "true"; break;
225 case RETURNMODE_NEVER: params["RETURNCOND"] = "false"; break;
226 case RETURNMODE_DYNAMIC: params["RETURNCOND"] = std::string(coords) + ".x+" + coords + ".y >= 0.0"; break;
227 default: DE_ASSERT(DE_FALSE);
230 return de::MovePtr<ShaderReturnCase>(new ShaderReturnCase(context, name, description, isVertex, tmpl.specialize(params), getEvalFunc(returnMode), DE_NULL));
233 de::MovePtr<ShaderReturnCase> makeReturnInLoopCase (tcu::TestContext& context, const std::string& name, const std::string& description, bool isDynamicLoop, ReturnMode returnMode, bool isVertex)
235 tcu::StringTemplate tmpl(
237 "layout(location = ${COORDLOC}) in ${COORDPREC} vec4 ${COORDS};\n"
238 "layout(binding = 0, std140) uniform something { mediump int ui_one; };\n"
240 "${COORDPREC} vec4 getCoords (void)\n"
242 " ${COORDPREC} vec4 coords = ${COORDS};\n"
243 " for (int i = 0; i < ${ITERLIMIT}; i++)\n"
245 " if (${RETURNCOND})\n"
247 " coords = coords.wzyx;\n"
254 " ${OUTPUT} = vec4(getCoords().xyz, 1.0);\n"
257 const char* coords = isVertex ? "a_coords" : "v_coords";
259 std::map<std::string, std::string> params;
261 params["COORDLOC"] = isVertex ? "1" : "0";
262 params["COORDPREC"] = isVertex ? "highp" : "mediump";
263 params["OUTPUT"] = isVertex ? "v_color" : "o_color";
264 params["COORDS"] = coords;
265 params["EXTRADECL"] = isVertex ? "layout(location = 0) in highp vec4 a_position;\nlayout(location = 0) out mediump vec4 v_color;\n" : "layout(location = 0) out mediump vec4 o_color;\n";
266 params["POSITIONWRITE"] = isVertex ? " gl_Position = a_position;\n" : "";
267 params["ITERLIMIT"] = isDynamicLoop ? "ui_one" : "1";
271 case RETURNMODE_ALWAYS: params["RETURNCOND"] = "true"; break;
272 case RETURNMODE_NEVER: params["RETURNCOND"] = "false"; break;
273 case RETURNMODE_DYNAMIC: params["RETURNCOND"] = std::string(coords) + ".x+" + coords + ".y >= 0.0"; break;
274 default: DE_ASSERT(DE_FALSE);
277 return de::MovePtr<ShaderReturnCase>(new ShaderReturnCase(context, name, description, isVertex, tmpl.specialize(params), getEvalFunc(returnMode), new ReturnTestUniformSetup(UI_ONE)));
280 static const char* getReturnModeName (ReturnMode mode)
284 case RETURNMODE_ALWAYS: return "always";
285 case RETURNMODE_NEVER: return "never";
286 case RETURNMODE_DYNAMIC: return "dynamic";
293 static const char* getReturnModeDesc (ReturnMode mode)
297 case RETURNMODE_ALWAYS: return "Always return";
298 case RETURNMODE_NEVER: return "Never return";
299 case RETURNMODE_DYNAMIC: return "Return based on coords";
306 class ShaderReturnTests : public tcu::TestCaseGroup
309 ShaderReturnTests (tcu::TestContext& context);
310 virtual ~ShaderReturnTests (void);
311 virtual void init (void);
314 ShaderReturnTests (const ShaderReturnTests&); // not allowed!
315 ShaderReturnTests& operator= (const ShaderReturnTests&); // not allowed!
318 ShaderReturnTests::ShaderReturnTests (tcu::TestContext& context)
319 : TestCaseGroup(context, "return", "Return Statement Tests")
323 ShaderReturnTests::~ShaderReturnTests (void)
327 void ShaderReturnTests::init (void)
329 addChild(new ShaderReturnCase(m_testCtx, "single_return_vertex", "Single return statement in function", true,
331 "layout(location = 0) in highp vec4 a_position;\n"
332 "layout(location = 1) in highp vec4 a_coords;\n"
333 "layout(location = 0) out mediump vec4 v_color;\n\n"
334 "vec4 getColor (void)\n"
336 " return vec4(a_coords.xyz, 1.0);\n"
340 " gl_Position = a_position;\n"
341 " v_color = getColor();\n"
342 "}\n", evalReturnAlways, DE_NULL));
343 addChild(new ShaderReturnCase(m_testCtx, "single_return_fragment", "Single return statement in function", false,
345 "layout(location = 0) in mediump vec4 v_coords;\n"
346 "layout(location = 0) out mediump vec4 o_color;\n"
347 "mediump vec4 getColor (void)\n"
349 " return vec4(v_coords.xyz, 1.0);\n"
353 " o_color = getColor();\n"
354 "}\n", evalReturnAlways, DE_NULL));
356 // Conditional return statement in function.
357 for (int returnMode = 0; returnMode < RETURNMODE_LAST; returnMode++)
359 for (int isFragment = 0; isFragment < 2; isFragment++)
361 std::string name = std::string("conditional_return_") + getReturnModeName((ReturnMode)returnMode) + (isFragment ? "_fragment" : "_vertex");
362 std::string description = std::string(getReturnModeDesc((ReturnMode)returnMode)) + " in function";
363 de::MovePtr<ShaderReturnCase> testCase (makeConditionalReturnInFuncCase(m_testCtx, name, description, (ReturnMode)returnMode, isFragment == 0));
364 addChild(testCase.release());
368 // Unconditional double return in function.
369 addChild(new ShaderReturnCase(m_testCtx, "double_return_vertex", "Unconditional double return in function", true,
371 "layout(location = 0) in highp vec4 a_position;\n"
372 "layout(location = 1) in highp vec4 a_coords;\n"
373 "layout(location = 0) out mediump vec4 v_color;\n\n"
374 "vec4 getColor (void)\n"
376 " return vec4(a_coords.xyz, 1.0);\n"
377 " return vec4(a_coords.wzy, 1.0);\n"
381 " gl_Position = a_position;\n"
382 " v_color = getColor();\n"
383 "}\n", evalReturnAlways, DE_NULL));
384 addChild(new ShaderReturnCase(m_testCtx, "double_return_fragment", "Unconditional double return in function", false,
386 "layout(location = 0) in mediump vec4 v_coords;\n"
387 "layout(location = 0) out mediump vec4 o_color;\n\n"
388 "mediump vec4 getColor (void)\n"
390 " return vec4(v_coords.xyz, 1.0);\n"
391 " return vec4(v_coords.wzy, 1.0);\n"
395 " o_color = getColor();\n"
396 "}\n", evalReturnAlways, DE_NULL));
398 // Last statement in main.
399 addChild(new ShaderReturnCase(m_testCtx, "last_statement_in_main_vertex", "Return as a final statement in main()", true,
401 "layout(location = 0) in highp vec4 a_position;\n"
402 "layout(location = 1) in highp vec4 a_coords;\n"
403 "layout(location = 0) out mediump vec4 v_color;\n\n"
406 " gl_Position = a_position;\n"
407 " v_color = vec4(a_coords.xyz, 1.0);\n"
409 "}\n", evalReturnAlways, DE_NULL));
410 addChild(new ShaderReturnCase(m_testCtx, "last_statement_in_main_fragment", "Return as a final statement in main()", false,
412 "layout(location = 0) in mediump vec4 v_coords;\n"
413 "layout(location = 0) out mediump vec4 o_color;\n\n"
416 " o_color = vec4(v_coords.xyz, 1.0);\n"
418 "}\n", evalReturnAlways, DE_NULL));
420 // Return between output variable writes.
421 for (int inFunc = 0; inFunc < 2; inFunc++)
423 for (int returnMode = 0; returnMode < RETURNMODE_LAST; returnMode++)
425 for (int isFragment = 0; isFragment < 2; isFragment++)
427 std::string name = std::string("output_write_") + (inFunc ? "in_func_" : "") + getReturnModeName((ReturnMode)returnMode) + (isFragment ? "_fragment" : "_vertex");
428 std::string desc = std::string(getReturnModeDesc((ReturnMode)returnMode)) + (inFunc ? " in user-defined function" : " in main()") + " between output writes";
429 de::MovePtr<ShaderReturnCase> testCase = (makeOutputWriteReturnCase(m_testCtx, name, desc, inFunc != 0, (ReturnMode)returnMode, isFragment == 0));
430 addChild(testCase.release());
435 // Conditional return statement in loop.
436 for (int isDynamicLoop = 0; isDynamicLoop < 2; isDynamicLoop++)
438 for (int returnMode = 0; returnMode < RETURNMODE_LAST; returnMode++)
440 for (int isFragment = 0; isFragment < 2; isFragment++)
442 std::string name = std::string("return_in_") + (isDynamicLoop ? "dynamic" : "static") + "_loop_" + getReturnModeName((ReturnMode)returnMode) + (isFragment ? "_fragment" : "_vertex");
443 std::string description = std::string(getReturnModeDesc((ReturnMode)returnMode)) + " in loop";
444 de::MovePtr<ShaderReturnCase> testCase (makeReturnInLoopCase(m_testCtx, name, description, isDynamicLoop != 0, (ReturnMode)returnMode, isFragment == 0));
445 addChild(testCase.release());
450 // Unconditional return in infinite loop.
451 addChild(new ShaderReturnCase(m_testCtx, "return_in_infinite_loop_vertex", "Return in infinite loop", true,
453 "layout(location = 0) in highp vec4 a_position;\n"
454 "layout(location = 1) in highp vec4 a_coords;\n"
455 "layout(location = 0) out mediump vec4 v_color;\n"
456 "layout(binding = 0, std140) uniform something { int ui_zero; };\n"
457 "highp vec4 getCoords (void)\n"
459 " for (int i = 1; i < 10; i += ui_zero)\n"
460 " return a_coords;\n"
461 " return a_coords.wzyx;\n"
465 " gl_Position = a_position;\n"
466 " v_color = vec4(getCoords().xyz, 1.0);\n"
468 "}\n", evalReturnAlways, new ReturnTestUniformSetup(UI_ZERO)));
469 addChild(new ShaderReturnCase(m_testCtx, "return_in_infinite_loop_fragment", "Return in infinite loop", false,
471 "layout(location = 0) in mediump vec4 v_coords;\n"
472 "layout(location = 0) out mediump vec4 o_color;\n"
473 "layout(binding = 0, std140) uniform something { int ui_zero; };\n\n"
474 "mediump vec4 getCoords (void)\n"
476 " for (int i = 1; i < 10; i += ui_zero)\n"
477 " return v_coords;\n"
478 " return v_coords.wzyx;\n"
482 " o_color = vec4(getCoords().xyz, 1.0);\n"
484 "}\n", evalReturnAlways, new ReturnTestUniformSetup(UI_ZERO)));
489 tcu::TestCaseGroup* createReturnTests (tcu::TestContext& testCtx)
491 return new ShaderReturnTests(testCtx);