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 Optimized vs unoptimized shader performance tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es2pShaderOptimizationTests.hpp"
25 #include "glsShaderPerformanceMeasurer.hpp"
26 #include "gluRenderContext.hpp"
27 #include "gluShaderProgram.hpp"
28 #include "tcuTestLog.hpp"
29 #include "tcuVector.hpp"
30 #include "tcuStringTemplate.hpp"
31 #include "deSharedPtr.hpp"
32 #include "deStringUtil.hpp"
35 #include "glwFunctions.hpp"
41 using glu::ShaderProgram;
53 using gls::ShaderPerformanceMeasurer;
60 static inline std::map<string, string> singleMap (const string& key, const string& value)
62 std::map<string, string> res;
67 static inline string repeat (const string& str, int numRepeats, const string& delim = "")
70 for (int i = 1; i < numRepeats; i++)
71 result += delim + str;
75 static inline string repeatIndexedTemplate (const string& strTempl, int numRepeats, const string& delim = "", int ndxStart = 0)
77 const tcu::StringTemplate templ(strTempl);
79 std::map<string, string> params;
81 for (int i = 0; i < numRepeats; i++)
83 params["PREV_NDX"] = toString(i + ndxStart - 1);
84 params["NDX"] = toString(i + ndxStart);
86 result += (i > 0 ? delim : "") + templ.specialize(params);
97 CASESHADERTYPE_VERTEX = 0,
98 CASESHADERTYPE_FRAGMENT,
103 static inline string getShaderPrecision (CaseShaderType shaderType)
107 case CASESHADERTYPE_VERTEX: return "highp";
108 case CASESHADERTYPE_FRAGMENT: return "mediump";
117 glu::ProgramSources sources;
118 vector<gls::AttribSpec> attributes; //!< \note Shouldn't contain a_position; that one is set by gls::ShaderPerformanceMeasurer.
120 ProgramData (void) {}
121 ProgramData (const glu::ProgramSources& sources_, const vector<gls::AttribSpec>& attributes_ = vector<gls::AttribSpec>()) : sources(sources_), attributes(attributes_) {}
122 ProgramData (const glu::ProgramSources& sources_, const gls::AttribSpec& attribute) : sources(sources_), attributes(1, attribute) {}
125 //! Shader boilerplate helper; most cases have similar basic shader structure.
126 static inline ProgramData defaultProgramData (CaseShaderType shaderType, const string& funcDefs, const string& mainStatements)
128 const bool isVertexCase = shaderType == CASESHADERTYPE_VERTEX;
129 const bool isFragmentCase = shaderType == CASESHADERTYPE_FRAGMENT;
130 const string vtxPrec = getShaderPrecision(CASESHADERTYPE_VERTEX);
131 const string fragPrec = getShaderPrecision(CASESHADERTYPE_FRAGMENT);
133 return ProgramData(glu::ProgramSources() << glu::VertexSource( "attribute " + vtxPrec + " vec4 a_position;\n"
134 "attribute " + vtxPrec + " vec4 a_value;\n"
135 "varying " + fragPrec + " vec4 v_value;\n"
136 + (isVertexCase ? funcDefs : "") +
139 " gl_Position = a_position;\n"
140 " " + vtxPrec + " vec4 value = a_value;\n"
141 + (isVertexCase ? mainStatements : "") +
142 " v_value = value;\n"
145 << glu::FragmentSource( "varying " + fragPrec + " vec4 v_value;\n"
146 + (isFragmentCase ? funcDefs : "") +
149 " " + fragPrec + " vec4 value = v_value;\n"
150 + (isFragmentCase ? mainStatements : "") +
151 " gl_FragColor = value;\n"
153 gls::AttribSpec("a_value",
154 Vec4(1.0f, 0.0f, 0.0f, 0.0f),
155 Vec4(0.0f, 1.0f, 0.0f, 0.0f),
156 Vec4(0.0f, 0.0f, 1.0f, 0.0f),
157 Vec4(0.0f, 0.0f, 0.0f, 1.0f)));
160 static inline ProgramData defaultProgramData (CaseShaderType shaderType, const string& mainStatements)
162 return defaultProgramData(shaderType, "", mainStatements);
165 class ShaderOptimizationCase : public TestCase
168 ShaderOptimizationCase (Context& context, const char* name, const char* description, CaseShaderType caseShaderType)
169 : TestCase (context, tcu::NODETYPE_PERFORMANCE, name, description)
170 , m_caseShaderType (caseShaderType)
171 , m_state (STATE_LAST)
172 , m_measurer (context.getRenderContext(), caseShaderType == CASESHADERTYPE_VERTEX ? gls::CASETYPE_VERTEX
173 : caseShaderType == CASESHADERTYPE_FRAGMENT ? gls::CASETYPE_FRAGMENT
174 : gls::CASETYPE_LAST)
175 , m_unoptimizedResult (-1.0f, -1.0f)
176 , m_optimizedResult (-1.0f, -1.0f)
180 virtual ~ShaderOptimizationCase (void) {}
183 IterateResult iterate (void);
186 virtual ProgramData generateProgramData (bool optimized) const = 0;
188 const CaseShaderType m_caseShaderType;
193 STATE_INIT_UNOPTIMIZED = 0,
194 STATE_MEASURE_UNOPTIMIZED,
195 STATE_INIT_OPTIMIZED,
196 STATE_MEASURE_OPTIMIZED,
202 ProgramData& programData (bool optimized) { return optimized ? m_optimizedData : m_unoptimizedData; }
203 SharedPtr<const ShaderProgram>& program (bool optimized) { return optimized ? m_optimizedProgram : m_unoptimizedProgram; }
204 ShaderPerformanceMeasurer::Result& result (bool optimized) { return optimized ? m_optimizedResult : m_unoptimizedResult; }
207 ShaderPerformanceMeasurer m_measurer;
209 ProgramData m_unoptimizedData;
210 ProgramData m_optimizedData;
211 SharedPtr<const ShaderProgram> m_unoptimizedProgram;
212 SharedPtr<const ShaderProgram> m_optimizedProgram;
213 ShaderPerformanceMeasurer::Result m_unoptimizedResult;
214 ShaderPerformanceMeasurer::Result m_optimizedResult;
217 void ShaderOptimizationCase::init (void)
219 const glu::RenderContext& renderCtx = m_context.getRenderContext();
220 TestLog& log = m_testCtx.getLog();
222 m_measurer.logParameters(log);
224 for (int ndx = 0; ndx < 2; ndx++)
226 const bool optimized = ndx == 1;
228 programData(optimized) = generateProgramData(optimized);
230 for (int i = 0; i < (int)programData(optimized).attributes.size(); i++)
231 DE_ASSERT(programData(optimized).attributes[i].name != "a_position"); // \note Position attribute is set by m_measurer.
233 program(optimized) = SharedPtr<const ShaderProgram>(new ShaderProgram(renderCtx, programData(optimized).sources));
236 const tcu::ScopedLogSection section(log, optimized ? "OptimizedProgram" : "UnoptimizedProgram",
237 optimized ? "Hand-optimized program" : "Unoptimized program");
238 log << *program(optimized);
241 if (!program(optimized)->isOk())
242 TCU_FAIL("Shader compilation failed");
245 m_state = STATE_INIT_UNOPTIMIZED;
248 ShaderOptimizationCase::IterateResult ShaderOptimizationCase::iterate (void)
250 TestLog& log = m_testCtx.getLog();
252 if (m_state == STATE_INIT_UNOPTIMIZED || m_state == STATE_INIT_OPTIMIZED)
254 const bool optimized = m_state == STATE_INIT_OPTIMIZED;
255 m_measurer.init(program(optimized)->getProgram(), programData(optimized).attributes, 1);
256 m_state = optimized ? STATE_MEASURE_OPTIMIZED : STATE_MEASURE_UNOPTIMIZED;
260 else if (m_state == STATE_MEASURE_UNOPTIMIZED || m_state == STATE_MEASURE_OPTIMIZED)
262 m_measurer.iterate();
264 if (m_measurer.isFinished())
266 const bool optimized = m_state == STATE_MEASURE_OPTIMIZED;
267 const tcu::ScopedLogSection section (log, optimized ? "OptimizedResult" : "UnoptimizedResult",
268 optimized ? "Measurement results for hand-optimized program" : "Measurement result for unoptimized program");
269 m_measurer.logMeasurementInfo(log);
270 result(optimized) = m_measurer.getResult();
272 m_state = optimized ? STATE_FINISHED : STATE_INIT_OPTIMIZED;
279 DE_ASSERT(m_state == STATE_FINISHED);
281 const float unoptimizedRelevantResult = m_caseShaderType == CASESHADERTYPE_VERTEX ? m_unoptimizedResult.megaVertPerSec : m_unoptimizedResult.megaFragPerSec;
282 const float optimizedRelevantResult = m_caseShaderType == CASESHADERTYPE_VERTEX ? m_optimizedResult.megaVertPerSec : m_optimizedResult.megaFragPerSec;
283 const char* const relevantResultName = m_caseShaderType == CASESHADERTYPE_VERTEX ? "vertex" : "fragment";
284 const float ratio = unoptimizedRelevantResult / optimizedRelevantResult;
285 const int handOptimizationGain = (int)deFloatRound(100.0f/ratio) - 100;
287 log << TestLog::Message << "Unoptimized / optimized " << relevantResultName << " performance ratio: " << ratio << TestLog::EndMessage;
289 if (handOptimizationGain >= 0)
290 log << TestLog::Message << "Note: " << handOptimizationGain << "% performance gain was achieved with hand-optimized version" << TestLog::EndMessage;
292 log << TestLog::Message << "Note: hand-optimization degraded performance by " << -handOptimizationGain << "%" << TestLog::EndMessage;
294 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, de::floatToString(ratio, 2).c_str());
300 class LoopUnrollCase : public ShaderOptimizationCase
305 CASETYPE_INDEPENDENT = 0,
311 LoopUnrollCase (Context& context, const char* name, const char* description, CaseShaderType caseShaderType, CaseType caseType, int numRepetitions)
312 : ShaderOptimizationCase (context, name, description, caseShaderType)
313 , m_numRepetitions (numRepetitions)
314 , m_caseType (caseType)
319 ProgramData generateProgramData (bool optimized) const
321 const string repetition = optimized ? repeatIndexedTemplate("\t" + expressionTemplate(m_caseType) + ";\n", m_numRepetitions)
322 : loop(m_numRepetitions, expressionTemplate(m_caseType));
324 return defaultProgramData(m_caseShaderType, "\t" + getShaderPrecision(m_caseShaderType) + " vec4 valueOrig = value;\n" + repetition);
328 const int m_numRepetitions;
329 const CaseType m_caseType;
331 static inline string expressionTemplate (CaseType caseType)
335 case CASETYPE_INDEPENDENT: return "value += sin(float(${NDX}+1)*valueOrig)";
336 case CASETYPE_DEPENDENT: return "value = sin(value)";
343 static inline string loop (int iterations, const string& innerExpr)
345 return "\tfor (int i = 0; i < " + toString(iterations) + "; i++)\n\t\t" + tcu::StringTemplate(innerExpr).specialize(singleMap("NDX", "i")) + ";\n";
349 class LoopInvariantCodeMotionCase : public ShaderOptimizationCase
352 LoopInvariantCodeMotionCase (Context& context, const char* name, const char* description, CaseShaderType caseShaderType, int numLoopIterations)
353 : ShaderOptimizationCase (context, name, description, caseShaderType)
354 , m_numLoopIterations (numLoopIterations)
359 ProgramData generateProgramData (bool optimized) const
362 for (int i = 0; i < m_numLoopIterations; i++)
363 scale += 3.2f*(float)i + 4.6f;
364 scale = 1.0f / scale;
366 const string precision = getShaderPrecision(m_caseShaderType);
367 const string statements = optimized ? " " + precision + " vec4 valueOrig = value;\n"
368 " " + precision + " vec4 y = sin(cos(sin(valueOrig)));\n"
369 " for (int i = 0; i < " + toString(m_numLoopIterations) + "; i++)\n"
371 " " + precision + " float x = 3.2*float(i) + 4.6;\n"
374 " value *= " + toString(scale) + ";\n"
376 : " " + precision + " vec4 valueOrig = value;\n"
377 " for (int i = 0; i < " + toString(m_numLoopIterations) + "; i++)\n"
379 " " + precision + " float x = 3.2*float(i) + 4.6;\n"
380 " " + precision + " vec4 y = sin(cos(sin(valueOrig)));\n"
383 " value *= " + toString(scale) + ";\n";
385 return defaultProgramData(m_caseShaderType, statements);
389 const int m_numLoopIterations;
392 class FunctionInliningCase : public ShaderOptimizationCase
395 FunctionInliningCase (Context& context, const char* name, const char* description, CaseShaderType caseShaderType, int callNestingDepth)
396 : ShaderOptimizationCase (context, name, description, caseShaderType)
397 , m_callNestingDepth (callNestingDepth)
402 ProgramData generateProgramData (bool optimized) const
404 const string precision = getShaderPrecision(m_caseShaderType);
405 const string expression = "value*vec4(0.8, 0.7, 0.6, 0.9)";
406 const string maybeFuncDefs = optimized ? "" : funcDefinitions(m_callNestingDepth, precision, expression);
407 const string mainValueStatement = (optimized ? "\tvalue = " + expression : "\tvalue = func" + toString(m_callNestingDepth-1) + "(value)") + ";\n";
409 return defaultProgramData(m_caseShaderType, maybeFuncDefs, mainValueStatement);
413 const int m_callNestingDepth;
415 static inline string funcDefinitions (int callNestingDepth, const string& precision, const string& expression)
417 string result = precision + " vec4 func0 (" + precision + " vec4 value) { return " + expression + "; }\n";
419 for (int i = 1; i < callNestingDepth; i++)
420 result += precision + " vec4 func" + toString(i) + " (" + precision + " vec4 v) { return func" + toString(i-1) + "(v); }\n";
426 class ConstantPropagationCase : public ShaderOptimizationCase
431 CASETYPE_BUILT_IN_FUNCTIONS = 0,
438 ConstantPropagationCase (Context& context, const char* name, const char* description, CaseShaderType caseShaderType, CaseType caseType, bool useConstantExpressionsOnly)
439 : ShaderOptimizationCase (context, name, description, caseShaderType)
440 , m_caseType (caseType)
441 , m_useConstantExpressionsOnly (useConstantExpressionsOnly)
443 DE_ASSERT(!(m_caseType == CASETYPE_ARRAY && m_useConstantExpressionsOnly)); // \note Would need array constructors, which GLSL ES 1 doesn't have.
447 ProgramData generateProgramData (bool optimized) const
449 const bool isVertexCase = m_caseShaderType == CASESHADERTYPE_VERTEX;
450 const string precision = getShaderPrecision(m_caseShaderType);
451 const string statements = m_caseType == CASETYPE_BUILT_IN_FUNCTIONS ? builtinFunctionsCaseStatements (optimized, m_useConstantExpressionsOnly, precision, isVertexCase)
452 : m_caseType == CASETYPE_ARRAY ? arrayCaseStatements (optimized, precision, isVertexCase)
453 : m_caseType == CASETYPE_STRUCT ? structCaseStatements (optimized, m_useConstantExpressionsOnly, precision, isVertexCase)
456 return defaultProgramData(m_caseShaderType, statements);
460 const CaseType m_caseType;
461 const bool m_useConstantExpressionsOnly;
463 static inline string builtinFunctionsCaseStatements (bool optimized, bool constantExpressionsOnly, const string& precision, bool useHeavierWorkload)
465 const string constMaybe = constantExpressionsOnly ? "const " : "";
466 const int numSinRows = useHeavierWorkload ? 12 : 1;
468 return optimized ? " value = vec4(0.4, 0.5, 0.6, 0.7) * value; // NOTE: factor doesn't necessarily match the one in unoptimized shader, but shouldn't make a difference performance-wise\n"
470 : " " + constMaybe + precision + " vec4 a = vec4(sin(0.7), cos(0.2), sin(0.9), abs(-0.5));\n"
471 " " + constMaybe + precision + " vec4 b = cos(a) + fract(3.0*a.xzzw);\n"
472 " " + constMaybe + "bvec4 c = bvec4(true, false, true, true);\n"
473 " " + constMaybe + precision + " vec4 d = exp(b + vec4(c));\n"
474 " " + constMaybe + precision + " vec4 e0 = inversesqrt(mix(d+a, d+b, a));\n"
475 + repeatIndexedTemplate(" " + constMaybe + precision + " vec4 e${NDX} = sin(sin(sin(sin(e${PREV_NDX}))));\n", numSinRows, "", 1) +
476 " " + constMaybe + precision + " vec4 f = abs(e" + toString(numSinRows) + ");\n" +
477 " value = f*value;\n";
480 static inline string arrayCaseStatements (bool optimized, const string& precision, bool useHeavierWorkload)
482 const int numSinRows = useHeavierWorkload ? 12 : 1;
484 return optimized ? " value = vec4(0.4, 0.5, 0.6, 0.7) * value; // NOTE: factor doesn't necessarily match the one in unoptimized shader, but shouldn't make a difference performance-wise\n"
486 : " const int arrLen = 4;\n"
487 " " + precision + " vec4 arr[arrLen];\n"
488 " arr[0] = vec4(0.1, 0.5, 0.9, 1.3);\n"
489 " arr[1] = vec4(0.2, 0.6, 1.0, 1.4);\n"
490 " arr[2] = vec4(0.3, 0.7, 1.1, 1.5);\n"
491 " arr[3] = vec4(0.4, 0.8, 1.2, 1.6);\n"
492 " " + precision + " vec4 a = (arr[0] + arr[1] + arr[2] + arr[3]) * 0.25;\n"
493 " " + precision + " vec4 b0 = cos(sin(a));\n"
494 + repeatIndexedTemplate(" " + precision + " vec4 b${NDX} = sin(sin(sin(sin(b${PREV_NDX}))));\n", numSinRows, "", 1) +
495 " " + precision + " vec4 c = abs(b" + toString(numSinRows) + ");\n" +
496 " value = c*value;\n";
499 static inline string structCaseStatements (bool optimized, bool constantExpressionsOnly, const string& precision, bool useHeavierWorkload)
501 const string constMaybe = constantExpressionsOnly ? "const " : "";
502 const int numSinRows = useHeavierWorkload ? 12 : 1;
504 return optimized ? " value = vec4(0.4, 0.5, 0.6, 0.7) * value; // NOTE: factor doesn't necessarily match the one in unoptimized shader, but shouldn't make a difference performance-wise\n"
508 " " + precision + " vec4 a;\n"
509 " " + precision + " vec4 b;\n"
510 " " + precision + " vec4 c;\n"
511 " " + precision + " vec4 d;\n"
514 " " + constMaybe + "S s =\n"
515 " S(vec4(0.1, 0.5, 0.9, 1.3),\n"
516 " vec4(0.2, 0.6, 1.0, 1.4),\n"
517 " vec4(0.3, 0.7, 1.1, 1.5),\n"
518 " vec4(0.4, 0.8, 1.2, 1.6));\n"
519 " " + constMaybe + precision + " vec4 a = (s.a + s.b + s.c + s.d) * 0.25;\n"
520 " " + constMaybe + precision + " vec4 b0 = cos(sin(a));\n"
521 + repeatIndexedTemplate(" " + constMaybe + precision + " vec4 b${NDX} = sin(sin(sin(sin(b${PREV_NDX}))));\n", numSinRows, "", 1) +
522 " " + constMaybe + precision + " vec4 c = abs(b" + toString(numSinRows) + ");\n" +
523 " value = c*value;\n";
527 class CommonSubexpressionCase : public ShaderOptimizationCase
532 CASETYPE_SINGLE_STATEMENT = 0,
533 CASETYPE_MULTIPLE_STATEMENTS,
534 CASETYPE_STATIC_BRANCH,
540 CommonSubexpressionCase (Context& context, const char* name, const char* description, CaseShaderType caseShaderType, CaseType caseType)
541 : ShaderOptimizationCase (context, name, description, caseShaderType)
542 , m_caseType (caseType)
547 ProgramData generateProgramData (bool optimized) const
549 const bool isVertexCase = m_caseShaderType == CASESHADERTYPE_VERTEX;
550 const string precision = getShaderPrecision(m_caseShaderType);
551 const string statements = m_caseType == CASETYPE_SINGLE_STATEMENT ? singleStatementCaseStatements (optimized, precision, isVertexCase)
552 : m_caseType == CASETYPE_MULTIPLE_STATEMENTS ? multipleStatementsCaseStatements (optimized, precision, isVertexCase)
553 : m_caseType == CASETYPE_STATIC_BRANCH ? staticBranchCaseStatements (optimized, precision, isVertexCase)
554 : m_caseType == CASETYPE_LOOP ? loopCaseStatements (optimized, precision, isVertexCase)
557 return defaultProgramData(m_caseShaderType, statements);
561 const CaseType m_caseType;
563 static inline string singleStatementCaseStatements (bool optimized, const string& precision, bool useHeavierWorkload)
565 const int numTopLevelRepeats = useHeavierWorkload ? 4 : 1;
567 return optimized ? " " + precision + " vec4 s = sin(value);\n"
568 " " + precision + " vec4 cs = cos(s);\n"
569 " " + precision + " vec4 d = fract(s + cs) + sqrt(s + exp(cs));\n"
570 " value = " + repeat("d", numTopLevelRepeats, "+") + ";\n"
572 : " value = " + repeat("fract(sin(value) + cos(sin(value))) + sqrt(sin(value) + exp(cos(sin(value))))", numTopLevelRepeats, "\n\t + ") + ";\n";
575 static inline string multipleStatementsCaseStatements (bool optimized, const string& precision, bool useHeavierWorkload)
577 const int numTopLevelRepeats = useHeavierWorkload ? 4 : 2;
578 DE_ASSERT(numTopLevelRepeats >= 2);
580 return optimized ? " " + precision + " vec4 a = sin(value) + cos(exp(value));\n"
581 " " + precision + " vec4 b = cos(cos(a));\n"
582 " a = fract(exp(sqrt(b)));\n"
584 + repeat("\tvalue += a*b;\n", numTopLevelRepeats)
586 : repeatIndexedTemplate( " " + precision + " vec4 a${NDX} = sin(value) + cos(exp(value));\n"
587 " " + precision + " vec4 b${NDX} = cos(cos(a${NDX}));\n"
588 " a${NDX} = fract(exp(sqrt(b${NDX})));\n"
590 numTopLevelRepeats) +
592 repeatIndexedTemplate( " value += a${NDX}*b${NDX};\n", numTopLevelRepeats);
595 static inline string staticBranchCaseStatements (bool optimized, const string& precision, bool useHeavierWorkload)
597 const int numTopLevelRepeats = useHeavierWorkload ? 4 : 2;
598 DE_ASSERT(numTopLevelRepeats >= 2);
602 return " " + precision + " vec4 a = sin(value) + cos(exp(value));\n"
603 " " + precision + " vec4 b = cos(a);\n"
605 " a = fract(exp(sqrt(b)));\n"
607 + repeat(" value += a*b;\n", numTopLevelRepeats);
613 for (int i = 0; i < numTopLevelRepeats; i++)
615 result += " " + precision + " vec4 a" + toString(i) + " = sin(value) + cos(exp(value));\n"
616 " " + precision + " vec4 b" + toString(i) + " = cos(a" + toString(i) + ");\n";
619 result += " if (1 < 2)\n"
620 " b" + toString(i) + " = cos(b" + toString(i) + ");\n";
622 result += " b" + toString(i) + " = cos(b" + toString(i) + ");\n";
624 result += " if (2 < 1);\n"
626 " b" + toString(i) + " = cos(b" + toString(i) + ");\n";
630 result += " a" + toString(i) + " = fract(exp(sqrt(b" + toString(i) + ")));\n\n";
633 result += repeatIndexedTemplate(" value += a${NDX}*b${NDX};\n", numTopLevelRepeats);
639 static inline string loopCaseStatements (bool optimized, const string& precision, bool useHeavierWorkload)
641 const int numLoopIterations = useHeavierWorkload ? 32 : 4;
643 return optimized ? " " + precision + " vec4 acc = value;\n"
644 " for (int i = 0; i < " + toString(numLoopIterations) + "; i++)\n"
650 : " " + precision + " vec4 acc0 = value;\n"
651 " for (int i = 0; i < " + toString(numLoopIterations) + "; i++)\n"
652 " acc0 = sin(acc0);\n"
654 " " + precision + " vec4 acc1 = value;\n"
655 " for (int i = 0; i < " + toString(numLoopIterations) + "; i++)\n"
656 " acc1 = sin(acc1);\n"
663 class DeadCodeEliminationCase : public ShaderOptimizationCase
668 CASETYPE_DEAD_BRANCH_SIMPLE = 0,
669 CASETYPE_DEAD_BRANCH_COMPLEX,
670 CASETYPE_DEAD_BRANCH_COMPLEX_NO_CONST,
671 CASETYPE_DEAD_BRANCH_FUNC_CALL,
672 CASETYPE_UNUSED_VALUE_BASIC,
673 CASETYPE_UNUSED_VALUE_LOOP,
674 CASETYPE_UNUSED_VALUE_DEAD_BRANCH,
675 CASETYPE_UNUSED_VALUE_AFTER_RETURN,
676 CASETYPE_UNUSED_VALUE_MUL_ZERO,
681 DeadCodeEliminationCase (Context& context, const char* name, const char* description, CaseShaderType caseShaderType, CaseType caseType)
682 : ShaderOptimizationCase (context, name, description, caseShaderType)
683 , m_caseType (caseType)
688 ProgramData generateProgramData (bool optimized) const
690 const bool isVertexCase = m_caseShaderType == CASESHADERTYPE_VERTEX;
691 const string precision = getShaderPrecision(m_caseShaderType);
692 const string funcDefs = m_caseType == CASETYPE_DEAD_BRANCH_FUNC_CALL ? deadBranchFuncCallCaseFuncDefs (optimized, precision)
693 : m_caseType == CASETYPE_UNUSED_VALUE_AFTER_RETURN ? unusedValueAfterReturnCaseFuncDefs (optimized, precision, isVertexCase)
696 const string statements = m_caseType == CASETYPE_DEAD_BRANCH_SIMPLE ? deadBranchSimpleCaseStatements (optimized, isVertexCase)
697 : m_caseType == CASETYPE_DEAD_BRANCH_COMPLEX ? deadBranchComplexCaseStatements (optimized, precision, true, isVertexCase)
698 : m_caseType == CASETYPE_DEAD_BRANCH_COMPLEX_NO_CONST ? deadBranchComplexCaseStatements (optimized, precision, false, isVertexCase)
699 : m_caseType == CASETYPE_DEAD_BRANCH_FUNC_CALL ? deadBranchFuncCallCaseStatements (optimized, isVertexCase)
700 : m_caseType == CASETYPE_UNUSED_VALUE_BASIC ? unusedValueBasicCaseStatements (optimized, precision, isVertexCase)
701 : m_caseType == CASETYPE_UNUSED_VALUE_LOOP ? unusedValueLoopCaseStatements (optimized, precision, isVertexCase)
702 : m_caseType == CASETYPE_UNUSED_VALUE_DEAD_BRANCH ? unusedValueDeadBranchCaseStatements (optimized, precision, isVertexCase)
703 : m_caseType == CASETYPE_UNUSED_VALUE_AFTER_RETURN ? unusedValueAfterReturnCaseStatements ()
704 : m_caseType == CASETYPE_UNUSED_VALUE_MUL_ZERO ? unusedValueMulZeroCaseStatements (optimized, precision, isVertexCase)
707 return defaultProgramData(m_caseShaderType, funcDefs, statements);
711 const CaseType m_caseType;
713 static inline string deadBranchSimpleCaseStatements (bool optimized, bool useHeavierWorkload)
715 const int numLoopIterations = useHeavierWorkload ? 16 : 4;
717 return optimized ? " value = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
719 : " value = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
722 " value = cos(exp(sin(value))*log(sqrt(value)));\n"
723 " for (int i = 0; i < " + toString(numLoopIterations) + "; i++)\n"
724 " value = sin(value);\n"
728 static inline string deadBranchComplexCaseStatements (bool optimized, const string& precision, bool useConst, bool useHeavierWorkload)
730 const string constMaybe = useConst ? "const " : "";
731 const int numLoopIterations = useHeavierWorkload ? 16 : 4;
733 return optimized ? " value = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
735 : " value = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
736 " " + constMaybe + precision + " vec4 a = vec4(sin(0.7), cos(0.2), sin(0.9), abs(-0.5));\n"
737 " " + constMaybe + precision + " vec4 b = cos(a) + fract(3.0*a.xzzw);\n"
738 " " + constMaybe + "bvec4 c = bvec4(true, false, true, true);\n"
739 " " + constMaybe + precision + " vec4 d = exp(b + vec4(c));\n"
740 " " + constMaybe + precision + " vec4 e = 1.8*abs(sin(sin(inversesqrt(mix(d+a, d+b, a)))));\n"
743 " value = cos(exp(sin(value))*log(sqrt(value)));\n"
744 " for (int i = 0; i < " + toString(numLoopIterations) + "; i++)\n"
745 " value = sin(value);\n"
749 static inline string deadBranchFuncCallCaseFuncDefs (bool optimized, const string& precision)
751 return optimized ? "" : precision + " float func (" + precision + " float x) { return 2.0*x; }\n";
754 static inline string deadBranchFuncCallCaseStatements (bool optimized, bool useHeavierWorkload)
756 const int numLoopIterations = useHeavierWorkload ? 16 : 4;
758 return optimized ? " value = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
760 : " value = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
761 " if (func(0.3) > 1.0)\n"
763 " value = cos(exp(sin(value))*log(sqrt(value)));\n"
764 " for (int i = 0; i < " + toString(numLoopIterations) + "; i++)\n"
765 " value = sin(value);\n"
769 static inline string unusedValueBasicCaseStatements (bool optimized, const string& precision, bool useHeavierWorkload)
771 const int numSinRows = useHeavierWorkload ? 12 : 1;
773 return optimized ? " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
776 : " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
777 " " + precision + " vec4 unused = cos(exp(sin(value))*log(sqrt(value))) + used;\n"
778 + repeat(" unused = sin(sin(sin(sin(unused))));\n", numSinRows) +
782 static inline string unusedValueLoopCaseStatements (bool optimized, const string& precision, bool useHeavierWorkload)
784 const int numLoopIterations = useHeavierWorkload ? 16 : 4;
786 return optimized ? " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
789 : " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
790 " " + precision + " vec4 unused = cos(exp(sin(value))*log(sqrt(value)));\n"
791 " for (int i = 0; i < " + toString(numLoopIterations) + "; i++)\n"
792 " unused = sin(unused + used);\n"
796 static inline string unusedValueAfterReturnCaseFuncDefs (bool optimized, const string& precision, bool useHeavierWorkload)
798 const int numSinRows = useHeavierWorkload ? 12 : 1;
800 return optimized ? precision + " vec4 func (" + precision + " vec4 v)\n"
802 " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * v;\n"
806 : precision + " vec4 func (" + precision + " vec4 v)\n"
808 " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * v;\n"
809 " " + precision + " vec4 unused = cos(exp(sin(v))*log(sqrt(v)));\n"
810 + repeat(" unused = sin(sin(sin(sin(unused))));\n", numSinRows) +
812 " used = used*unused;"
817 static inline string unusedValueAfterReturnCaseStatements (void)
819 return " value = func(value);\n";
822 static inline string unusedValueDeadBranchCaseStatements (bool optimized, const string& precision, bool useHeavierWorkload)
824 const int numSinRows = useHeavierWorkload ? 12 : 1;
826 return optimized ? " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
829 : " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
830 " " + precision + " vec4 unused = cos(exp(sin(value))*log(sqrt(value)));\n"
831 + repeat(" unused = sin(sin(sin(sin(unused))));\n", numSinRows) +
833 " used = used*unused;\n"
837 static inline string unusedValueMulZeroCaseStatements (bool optimized, const string& precision, bool useHeavierWorkload)
839 const int numSinRows = useHeavierWorkload ? 12 : 1;
841 return optimized ? " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
844 : " " + precision + " vec4 used = vec4(0.6, 0.7, 0.8, 0.9) * value;\n"
845 " " + precision + " vec4 unused = cos(exp(sin(value))*log(sqrt(value)));\n"
846 + repeat(" unused = sin(sin(sin(sin(unused))));\n", numSinRows) +
847 " value = used + unused*float(1-1);\n";
853 ShaderOptimizationTests::ShaderOptimizationTests (Context& context)
854 : TestCaseGroup(context, "optimization", "Shader Optimization Performance Tests")
858 ShaderOptimizationTests::~ShaderOptimizationTests (void)
862 void ShaderOptimizationTests::init (void)
864 TestCaseGroup* const unrollGroup = new TestCaseGroup(m_context, "loop_unrolling", "Loop Unrolling Cases");
865 TestCaseGroup* const loopInvariantCodeMotionGroup = new TestCaseGroup(m_context, "loop_invariant_code_motion", "Loop-Invariant Code Motion Cases");
866 TestCaseGroup* const inlineGroup = new TestCaseGroup(m_context, "function_inlining", "Function Inlining Cases");
867 TestCaseGroup* const constantPropagationGroup = new TestCaseGroup(m_context, "constant_propagation", "Constant Propagation Cases");
868 TestCaseGroup* const commonSubexpressionGroup = new TestCaseGroup(m_context, "common_subexpression_elimination", "Common Subexpression Elimination Cases");
869 TestCaseGroup* const deadCodeEliminationGroup = new TestCaseGroup(m_context, "dead_code_elimination", "Dead Code Elimination Cases");
870 addChild(unrollGroup);
871 addChild(loopInvariantCodeMotionGroup);
872 addChild(inlineGroup);
873 addChild(constantPropagationGroup);
874 addChild(commonSubexpressionGroup);
875 addChild(deadCodeEliminationGroup);
877 for (int caseShaderTypeI = 0; caseShaderTypeI < CASESHADERTYPE_LAST; caseShaderTypeI++)
879 const CaseShaderType caseShaderType = (CaseShaderType)caseShaderTypeI;
880 const char* const caseShaderTypeSuffix = caseShaderType == CASESHADERTYPE_VERTEX ? "_vertex"
881 : caseShaderType == CASESHADERTYPE_FRAGMENT ? "_fragment"
884 // Loop unrolling cases.
887 static const int loopIterationCounts[] = { 4, 8, 32 };
889 for (int caseTypeI = 0; caseTypeI < LoopUnrollCase::CASETYPE_LAST; caseTypeI++)
891 const LoopUnrollCase::CaseType caseType = (LoopUnrollCase::CaseType)caseTypeI;
892 const string caseTypeName = caseType == LoopUnrollCase::CASETYPE_INDEPENDENT ? "independent_iterations"
893 : caseType == LoopUnrollCase::CASETYPE_DEPENDENT ? "dependent_iterations"
895 const string caseTypeDesc = caseType == LoopUnrollCase::CASETYPE_INDEPENDENT ? "loop iterations don't depend on each other"
896 : caseType == LoopUnrollCase::CASETYPE_DEPENDENT ? "loop iterations depend on each other"
899 for (int loopIterNdx = 0; loopIterNdx < DE_LENGTH_OF_ARRAY(loopIterationCounts); loopIterNdx++)
901 const int loopIterations = loopIterationCounts[loopIterNdx];
902 const string name = caseTypeName + "_" + toString(loopIterations) + caseShaderTypeSuffix;
903 const string description = toString(loopIterations) + " iterations; " + caseTypeDesc;
905 unrollGroup->addChild(new LoopUnrollCase(m_context, name.c_str(), description.c_str(), caseShaderType, caseType, loopIterations));
910 // Loop-invariant code motion cases.
913 static const int loopIterationCounts[] = { 4, 8, 32 };
915 for (int loopIterNdx = 0; loopIterNdx < DE_LENGTH_OF_ARRAY(loopIterationCounts); loopIterNdx++)
917 const int loopIterations = loopIterationCounts[loopIterNdx];
918 const string name = toString(loopIterations) + "_iterations" + caseShaderTypeSuffix;
920 loopInvariantCodeMotionGroup->addChild(new LoopInvariantCodeMotionCase(m_context, name.c_str(), "", caseShaderType, loopIterations));
924 // Function inlining cases.
927 static const int callNestingDepths[] = { 4, 8, 32 };
929 for (int nestDepthNdx = 0; nestDepthNdx < DE_LENGTH_OF_ARRAY(callNestingDepths); nestDepthNdx++)
931 const int nestingDepth = callNestingDepths[nestDepthNdx];
932 const string name = toString(nestingDepth) + "_nested" + caseShaderTypeSuffix;
934 inlineGroup->addChild(new FunctionInliningCase(m_context, name.c_str(), "", caseShaderType, nestingDepth));
938 // Constant propagation cases.
940 for (int caseTypeI = 0; caseTypeI < ConstantPropagationCase::CASETYPE_LAST; caseTypeI++)
942 const ConstantPropagationCase::CaseType caseType = (ConstantPropagationCase::CaseType)caseTypeI;
943 const string caseTypeName = caseType == ConstantPropagationCase::CASETYPE_BUILT_IN_FUNCTIONS ? "built_in_functions"
944 : caseType == ConstantPropagationCase::CASETYPE_ARRAY ? "array"
945 : caseType == ConstantPropagationCase::CASETYPE_STRUCT ? "struct"
948 for (int constantExpressionsOnlyI = 0; constantExpressionsOnlyI <= 1; constantExpressionsOnlyI++)
950 const bool constantExpressionsOnly = constantExpressionsOnlyI != 0;
951 const string name = caseTypeName + (constantExpressionsOnly ? "" : "_no_const") + caseShaderTypeSuffix;
953 if (caseType == ConstantPropagationCase::CASETYPE_ARRAY && constantExpressionsOnly) // \note See ConstantPropagationCase's constructor for explanation.
956 constantPropagationGroup->addChild(new ConstantPropagationCase(m_context, name.c_str(), "", caseShaderType, caseType, constantExpressionsOnly));
960 // Common subexpression cases.
962 for (int caseTypeI = 0; caseTypeI < CommonSubexpressionCase::CASETYPE_LAST; caseTypeI++)
964 const CommonSubexpressionCase::CaseType caseType = (CommonSubexpressionCase::CaseType)caseTypeI;
966 const string caseTypeName = caseType == CommonSubexpressionCase::CASETYPE_SINGLE_STATEMENT ? "single_statement"
967 : caseType == CommonSubexpressionCase::CASETYPE_MULTIPLE_STATEMENTS ? "multiple_statements"
968 : caseType == CommonSubexpressionCase::CASETYPE_STATIC_BRANCH ? "static_branch"
969 : caseType == CommonSubexpressionCase::CASETYPE_LOOP ? "loop"
972 const string description = caseType == CommonSubexpressionCase::CASETYPE_SINGLE_STATEMENT ? "A single statement containing multiple uses of same subexpression"
973 : caseType == CommonSubexpressionCase::CASETYPE_MULTIPLE_STATEMENTS ? "Multiple statements performing same computations"
974 : caseType == CommonSubexpressionCase::CASETYPE_STATIC_BRANCH ? "Multiple statements including a static conditional"
975 : caseType == CommonSubexpressionCase::CASETYPE_LOOP ? "Multiple loops performing the same computations"
978 commonSubexpressionGroup->addChild(new CommonSubexpressionCase(m_context, (caseTypeName + caseShaderTypeSuffix).c_str(), description.c_str(), caseShaderType, caseType));
981 // Dead code elimination cases.
983 for (int caseTypeI = 0; caseTypeI < DeadCodeEliminationCase::CASETYPE_LAST; caseTypeI++)
985 const DeadCodeEliminationCase::CaseType caseType = (DeadCodeEliminationCase::CaseType)caseTypeI;
986 const char* const caseTypeName = caseType == DeadCodeEliminationCase::CASETYPE_DEAD_BRANCH_SIMPLE ? "dead_branch_simple"
987 : caseType == DeadCodeEliminationCase::CASETYPE_DEAD_BRANCH_COMPLEX ? "dead_branch_complex"
988 : caseType == DeadCodeEliminationCase::CASETYPE_DEAD_BRANCH_COMPLEX_NO_CONST ? "dead_branch_complex_no_const"
989 : caseType == DeadCodeEliminationCase::CASETYPE_DEAD_BRANCH_FUNC_CALL ? "dead_branch_func_call"
990 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_BASIC ? "unused_value_basic"
991 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_LOOP ? "unused_value_loop"
992 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_DEAD_BRANCH ? "unused_value_dead_branch"
993 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_AFTER_RETURN ? "unused_value_after_return"
994 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_MUL_ZERO ? "unused_value_mul_zero"
997 const char* const caseTypeDescription = caseType == DeadCodeEliminationCase::CASETYPE_DEAD_BRANCH_SIMPLE ? "Do computation inside a branch that is never taken (condition is simple false constant expression)"
998 : caseType == DeadCodeEliminationCase::CASETYPE_DEAD_BRANCH_COMPLEX ? "Do computation inside a branch that is never taken (condition is complex false constant expression)"
999 : caseType == DeadCodeEliminationCase::CASETYPE_DEAD_BRANCH_COMPLEX_NO_CONST ? "Do computation inside a branch that is never taken (condition is complex false expression, not constant expression but still compile-time computable)"
1000 : caseType == DeadCodeEliminationCase::CASETYPE_DEAD_BRANCH_FUNC_CALL ? "Do computation inside a branch that is never taken (condition is compile-time computable false expression containing function call to a simple inlineable function)"
1001 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_BASIC ? "Compute a value that is never used even statically"
1002 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_LOOP ? "Compute a value, using a loop, that is never used even statically"
1003 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_DEAD_BRANCH ? "Compute a value that is used only inside a statically dead branch"
1004 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_AFTER_RETURN ? "Compute a value that is used only after a return statement"
1005 : caseType == DeadCodeEliminationCase::CASETYPE_UNUSED_VALUE_MUL_ZERO ? "Compute a value that is used but multiplied by a zero constant expression"
1008 deadCodeEliminationGroup->addChild(new DeadCodeEliminationCase(m_context, (string() + caseTypeName + caseShaderTypeSuffix).c_str(), caseTypeDescription, caseShaderType, caseType));