1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 2.0 Module
3 * -------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Shader return statement tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es2fShaderReturnTests.hpp"
25 #include "glsShaderRenderCase.hpp"
26 #include "tcuStringTemplate.hpp"
32 using tcu::StringTemplate;
36 using std::ostringstream;
39 using namespace deqp::gls;
50 RETURNMODE_ALWAYS = 0,
59 REQUIRE_DYNAMIC_LOOPS = (1<<0),
62 // Evaluation functions
63 inline void evalReturnAlways (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2); }
64 inline void evalReturnNever (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(3,2,1); }
65 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); }
67 static ShaderEvalFunc getEvalFunc (ReturnMode mode)
71 case RETURNMODE_ALWAYS: return evalReturnAlways;
72 case RETURNMODE_NEVER: return evalReturnNever;
73 case RETURNMODE_DYNAMIC: return evalReturnDynamic;
76 return (ShaderEvalFunc)DE_NULL;
80 class ShaderReturnCase : public ShaderRenderCase
83 ShaderReturnCase (Context& context, const char* name, const char* description, bool isVertexCase, const char* shaderSource, ShaderEvalFunc evalFunc, deUint32 requirements = 0);
84 virtual ~ShaderReturnCase (void);
89 const deUint32 m_requirements;
92 ShaderReturnCase::ShaderReturnCase (Context& context, const char* name, const char* description, bool isVertexCase, const char* shaderSource, ShaderEvalFunc evalFunc, deUint32 requirements)
93 : ShaderRenderCase (context.getTestContext(), context.getRenderContext(), context.getContextInfo(), name, description, isVertexCase, evalFunc)
94 , m_requirements (requirements)
98 m_vertShaderSource = shaderSource;
100 "varying mediump vec4 v_color;\n\n"
103 " gl_FragColor = v_color;\n"
108 m_fragShaderSource = shaderSource;
110 "attribute highp vec4 a_position;\n"
111 "attribute highp vec4 a_coords;\n"
112 "varying mediump vec4 v_coords;\n\n"
115 " gl_Position = a_position;\n"
116 " v_coords = a_coords;\n"
121 ShaderReturnCase::~ShaderReturnCase (void)
125 void ShaderReturnCase::init (void)
129 ShaderRenderCase::init();
131 catch (const CompileFailed&)
133 if (m_requirements & REQUIRE_DYNAMIC_LOOPS)
135 const bool isSupported = m_isVertexCase ? m_ctxInfo.isVertexDynamicLoopSupported() : m_ctxInfo.isFragmentDynamicLoopSupported();
137 throw tcu::NotSupportedError("Dynamic loops not supported");
144 ShaderReturnTests::ShaderReturnTests (Context& context)
145 : TestCaseGroup(context, "return", "Return Statement Tests")
149 ShaderReturnTests::~ShaderReturnTests (void)
153 ShaderReturnCase* makeConditionalReturnInFuncCase (Context& context, const char* name, const char* description, ReturnMode returnMode, bool isVertex)
157 "${COORDSTORAGE} ${COORDPREC} vec4 ${COORDS};\n"
159 "${COORDPREC} vec4 getColor (void)\n"
161 " if (${RETURNCOND})\n"
162 " return vec4(${COORDS}.xyz, 1.0);\n"
163 " return vec4(${COORDS}.wzy, 1.0);\n"
168 " ${OUTPUT} = getColor();\n"
171 const char* coords = isVertex ? "a_coords" : "v_coords";
173 map<string, string> params;
175 params["COORDSTORAGE"] = isVertex ? "attribute" : "varying";
176 params["COORDPREC"] = isVertex ? "highp" : "mediump";
177 params["OUTPUT"] = isVertex ? "v_color" : "gl_FragColor";
178 params["COORDS"] = coords;
179 params["EXTRADECL"] = isVertex ? "attribute highp vec4 a_position;\nvarying mediump vec4 v_color;\n" : "";
180 params["POSITIONWRITE"] = isVertex ? " gl_Position = a_position;\n" : "";
184 case RETURNMODE_ALWAYS: params["RETURNCOND"] = "true"; break;
185 case RETURNMODE_NEVER: params["RETURNCOND"] = "false"; break;
186 case RETURNMODE_DYNAMIC: params["RETURNCOND"] = string(coords) + ".x+" + coords + ".y >= 0.0"; break;
187 default: DE_ASSERT(DE_FALSE);
190 return new ShaderReturnCase(context, name, description, isVertex, tmpl.specialize(params).c_str(), getEvalFunc(returnMode));
193 ShaderReturnCase* makeOutputWriteReturnCase (Context& context, const char* name, const char* description, bool inFunction, ReturnMode returnMode, bool isVertex)
199 "${COORDATTRS} vec4 ${COORDS};\n"
201 "void myfunc (void)\n"
203 " ${OUTPUT} = vec4(${COORDS}.xyz, 1.0);\n"
204 " if (${RETURNCOND})\n"
206 " ${OUTPUT} = vec4(${COORDS}.wzy, 1.0);\n"
214 "${COORDATTRS} vec4 ${COORDS};\n"
215 "uniform mediump int ui_one;\n"
220 " ${OUTPUT} = vec4(${COORDS}.xyz, 1.0);\n"
221 " if (${RETURNCOND})\n"
223 " ${OUTPUT} = vec4(${COORDS}.wzy, 1.0);\n"
226 const char* coords = isVertex ? "a_coords" : "v_coords";
228 map<string, string> params;
230 params["COORDATTRS"] = isVertex ? "attribute highp" : "varying mediump";
231 params["COORDS"] = coords;
232 params["OUTPUT"] = isVertex ? "v_color" : "gl_FragColor";
233 params["EXTRADECL"] = isVertex ? "attribute highp vec4 a_position;\nvarying mediump vec4 v_color;\n" : "";
234 params["POSITIONWRITE"] = isVertex ? " gl_Position = a_position;\n" : "";
238 case RETURNMODE_ALWAYS: params["RETURNCOND"] = "true"; break;
239 case RETURNMODE_NEVER: params["RETURNCOND"] = "false"; break;
240 case RETURNMODE_DYNAMIC: params["RETURNCOND"] = string(coords) + ".x+" + coords + ".y >= 0.0"; break;
241 default: DE_ASSERT(DE_FALSE);
244 return new ShaderReturnCase(context, name, description, isVertex, tmpl.specialize(params).c_str(), getEvalFunc(returnMode));
247 ShaderReturnCase* makeReturnInLoopCase (Context& context, const char* name, const char* description, bool isDynamicLoop, ReturnMode returnMode, bool isVertex)
251 "${COORDSTORAGE} ${COORDPREC} vec4 ${COORDS};\n"
252 "uniform mediump int ui_one;\n"
254 "${COORDPREC} vec4 getCoords (void)\n"
256 " ${COORDPREC} vec4 coords = ${COORDS};\n"
257 " for (int i = 0; i < ${ITERLIMIT}; i++)\n"
259 " if (${RETURNCOND})\n"
261 " coords = coords.wzyx;\n"
268 " ${OUTPUT} = vec4(getCoords().xyz, 1.0);\n"
271 const char* coords = isVertex ? "a_coords" : "v_coords";
273 map<string, string> params;
275 params["COORDSTORAGE"] = isVertex ? "attribute" : "varying";
276 params["COORDPREC"] = isVertex ? "highp" : "mediump";
277 params["OUTPUT"] = isVertex ? "v_color" : "gl_FragColor";
278 params["COORDS"] = coords;
279 params["EXTRADECL"] = isVertex ? "attribute highp vec4 a_position;\nvarying mediump vec4 v_color;\n" : "";
280 params["POSITIONWRITE"] = isVertex ? " gl_Position = a_position;\n" : "";
281 params["ITERLIMIT"] = isDynamicLoop ? "ui_one" : "1";
285 case RETURNMODE_ALWAYS: params["RETURNCOND"] = "true"; break;
286 case RETURNMODE_NEVER: params["RETURNCOND"] = "false"; break;
287 case RETURNMODE_DYNAMIC: params["RETURNCOND"] = string(coords) + ".x+" + coords + ".y >= 0.0"; break;
288 default: DE_ASSERT(DE_FALSE);
291 return new ShaderReturnCase(context, name, description, isVertex, tmpl.specialize(params).c_str(), getEvalFunc(returnMode), isDynamicLoop ? REQUIRE_DYNAMIC_LOOPS : 0);
294 static const char* getReturnModeName (ReturnMode mode)
298 case RETURNMODE_ALWAYS: return "always";
299 case RETURNMODE_NEVER: return "never";
300 case RETURNMODE_DYNAMIC: return "dynamic";
307 static const char* getReturnModeDesc (ReturnMode mode)
311 case RETURNMODE_ALWAYS: return "Always return";
312 case RETURNMODE_NEVER: return "Never return";
313 case RETURNMODE_DYNAMIC: return "Return based on coords";
320 void ShaderReturnTests::init (void)
322 // Single return statement in function.
323 addChild(new ShaderReturnCase(m_context, "single_return_vertex", "Single return statement in function", true,
324 "attribute highp vec4 a_position;\n"
325 "attribute highp vec4 a_coords;\n"
326 "varying highp vec4 v_color;\n\n"
327 "vec4 getColor (void)\n"
329 " return vec4(a_coords.xyz, 1.0);\n"
333 " gl_Position = a_position;\n"
334 " v_color = getColor();\n"
335 "}\n", evalReturnAlways));
336 addChild(new ShaderReturnCase(m_context, "single_return_fragment", "Single return statement in function", false,
337 "varying mediump vec4 v_coords;\n"
338 "mediump vec4 getColor (void)\n"
340 " return vec4(v_coords.xyz, 1.0);\n"
344 " gl_FragColor = getColor();\n"
345 "}\n", evalReturnAlways));
347 // Conditional return statement in function.
348 for (int returnMode = 0; returnMode < RETURNMODE_LAST; returnMode++)
350 for (int isFragment = 0; isFragment < 2; isFragment++)
352 string name = string("conditional_return_") + getReturnModeName((ReturnMode)returnMode) + (isFragment ? "_fragment" : "_vertex");
353 string description = string(getReturnModeDesc((ReturnMode)returnMode)) + " in function";
354 addChild(makeConditionalReturnInFuncCase(m_context, name.c_str(), description.c_str(), (ReturnMode)returnMode, isFragment == 0));
358 // Unconditional double return in function.
359 addChild(new ShaderReturnCase(m_context, "double_return_vertex", "Unconditional double return in function", true,
360 "attribute highp vec4 a_position;\n"
361 "attribute highp vec4 a_coords;\n"
362 "varying highp vec4 v_color;\n\n"
363 "vec4 getColor (void)\n"
365 " return vec4(a_coords.xyz, 1.0);\n"
366 " return vec4(a_coords.wzy, 1.0);\n"
370 " gl_Position = a_position;\n"
371 " v_color = getColor();\n"
372 "}\n", evalReturnAlways));
373 addChild(new ShaderReturnCase(m_context, "double_return_fragment", "Unconditional double return in function", false,
374 "varying mediump vec4 v_coords;\n"
375 "mediump vec4 getColor (void)\n"
377 " return vec4(v_coords.xyz, 1.0);\n"
378 " return vec4(v_coords.wzy, 1.0);\n"
382 " gl_FragColor = getColor();\n"
383 "}\n", evalReturnAlways));
385 // Last statement in main.
386 addChild(new ShaderReturnCase(m_context, "last_statement_in_main_vertex", "Return as a final statement in main()", true,
387 "attribute highp vec4 a_position;\n"
388 "attribute highp vec4 a_coords;\n"
389 "varying highp vec4 v_color;\n\n"
392 " gl_Position = a_position;\n"
393 " v_color = vec4(a_coords.xyz, 1.0);\n"
395 "}\n", evalReturnAlways));
396 addChild(new ShaderReturnCase(m_context, "last_statement_in_main_fragment", "Return as a final statement in main()", false,
397 "varying mediump vec4 v_coords;\n\n"
400 " gl_FragColor = vec4(v_coords.xyz, 1.0);\n"
402 "}\n", evalReturnAlways));
404 // Return between output variable writes.
405 for (int inFunc = 0; inFunc < 2; inFunc++)
407 for (int returnMode = 0; returnMode < RETURNMODE_LAST; returnMode++)
409 for (int isFragment = 0; isFragment < 2; isFragment++)
411 string name = string("output_write_") + (inFunc ? "in_func_" : "") + getReturnModeName((ReturnMode)returnMode) + (isFragment ? "_fragment" : "_vertex");
412 string desc = string(getReturnModeDesc((ReturnMode)returnMode)) + (inFunc ? " in user-defined function" : " in main()") + " between output writes";
414 addChild(makeOutputWriteReturnCase(m_context, name.c_str(), desc.c_str(), inFunc != 0, (ReturnMode)returnMode, isFragment == 0));
419 // Conditional return statement in loop.
420 for (int isDynamicLoop = 0; isDynamicLoop < 2; isDynamicLoop++)
422 for (int returnMode = 0; returnMode < RETURNMODE_LAST; returnMode++)
424 for (int isFragment = 0; isFragment < 2; isFragment++)
426 string name = string("return_in_") + (isDynamicLoop ? "dynamic" : "static") + "_loop_" + getReturnModeName((ReturnMode)returnMode) + (isFragment ? "_fragment" : "_vertex");
427 string description = string(getReturnModeDesc((ReturnMode)returnMode)) + " in loop";
428 addChild(makeReturnInLoopCase(m_context, name.c_str(), description.c_str(), isDynamicLoop != 0, (ReturnMode)returnMode, isFragment == 0));
433 // Unconditional return in infinite loop.
434 addChild(new ShaderReturnCase(m_context, "return_in_infinite_loop_vertex", "Return in infinite loop", true,
435 "attribute highp vec4 a_position;\n"
436 "attribute highp vec4 a_coords;\n"
437 "varying highp vec4 v_color;\n"
438 "uniform int ui_zero;\n\n"
439 "highp vec4 getCoords (void)\n"
441 " for (int i = 1; i < 10; i += ui_zero)\n"
442 " return a_coords;\n"
443 " return a_coords.wzyx;\n"
447 " gl_Position = a_position;\n"
448 " v_color = vec4(getCoords().xyz, 1.0);\n"
450 "}\n", evalReturnAlways, REQUIRE_DYNAMIC_LOOPS));
451 addChild(new ShaderReturnCase(m_context, "return_in_infinite_loop_fragment", "Return in infinite loop", false,
452 "varying mediump vec4 v_coords;\n"
453 "uniform int ui_zero;\n\n"
454 "mediump vec4 getCoords (void)\n"
456 " for (int i = 1; i < 10; i += ui_zero)\n"
457 " return v_coords;\n"
458 " return v_coords.wzyx;\n"
462 " gl_FragColor = vec4(getCoords().xyz, 1.0);\n"
464 "}\n", evalReturnAlways, REQUIRE_DYNAMIC_LOOPS));