Switch implementation
Implemented switch/case for glsl in OpenGL ES 3.0.
For simplicity, it is implemented as a loop without a condition,
so break statements work properly like so:
begin switch
if(...) // 1st case
...
else if(...) // other cases
...
else // default case
...
end switch // Anchor point for break statements
All related dEQP tests pass, except 7 tests where vertex shaders
contain a switch or a loop within another switch. These 7 failures
have only about 5% of bad pixel and seem to be related to an issue
with int(floor(...)), since the equivalent tests inside the fragment
shader pass.
KNOWN ISSUE: If a switch is within a loop and one of the cases
contains a "continue" statement, this will not be
handled correctly at the moment. There are no dEQP
tests for this at the moment, AFAIK.
Change-Id: I3ba34ab06a759d07e8520f6a87d75036a5cdaef5
Reviewed-on: https://swiftshader-review.googlesource.com/5272
Tested-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/src/OpenGL/compiler/OutputASM.cpp b/src/OpenGL/compiler/OutputASM.cpp
index 59949f1..a27b30e 100644
--- a/src/OpenGL/compiler/OutputASM.cpp
+++ b/src/OpenGL/compiler/OutputASM.cpp
@@ -1775,6 +1775,90 @@
return true;
}
+ bool OutputASM::visitSwitch(Visit visit, TIntermSwitch *node)
+ {
+ if(currentScope != emitScope)
+ {
+ return false;
+ }
+
+ TIntermTyped* switchValue = node->getInit();
+ TIntermAggregate* opList = node->getStatementList();
+
+ if(!switchValue || !opList)
+ {
+ return false;
+ }
+
+ switchValue->traverse(this);
+
+ emit(sw::Shader::OPCODE_SWITCH);
+
+ TIntermSequence& sequence = opList->getSequence();
+ TIntermSequence::iterator it = sequence.begin();
+ TIntermSequence::iterator defaultIt = sequence.end();
+ int nbCases = 0;
+ for(; it != sequence.end(); ++it)
+ {
+ TIntermCase* currentCase = (*it)->getAsCaseNode();
+ if(currentCase)
+ {
+ TIntermSequence::iterator caseIt = it;
+
+ TIntermTyped* condition = currentCase->getCondition();
+ if(condition) // non default case
+ {
+ if(nbCases != 0)
+ {
+ emit(sw::Shader::OPCODE_ELSE);
+ }
+
+ condition->traverse(this);
+ Temporary result(this);
+ emitBinary(sw::Shader::OPCODE_EQ, &result, switchValue, condition);
+ emit(sw::Shader::OPCODE_IF, 0, &result);
+ nbCases++;
+
+ for(++caseIt; caseIt != sequence.end(); ++caseIt)
+ {
+ (*caseIt)->traverse(this);
+ if((*caseIt)->getAsBranchNode()) // Kill, Break, Continue or Return
+ {
+ break;
+ }
+ }
+ }
+ else
+ {
+ defaultIt = it; // The default case might not be the last case, keep it for last
+ }
+ }
+ }
+
+ // If there's a default case, traverse it here
+ if(defaultIt != sequence.end())
+ {
+ emit(sw::Shader::OPCODE_ELSE);
+ for(++defaultIt; defaultIt != sequence.end(); ++defaultIt)
+ {
+ (*defaultIt)->traverse(this);
+ if((*defaultIt)->getAsBranchNode()) // Kill, Break, Continue or Return
+ {
+ break;
+ }
+ }
+ }
+
+ for(int i = 0; i < nbCases; ++i)
+ {
+ emit(sw::Shader::OPCODE_ENDIF);
+ }
+
+ emit(sw::Shader::OPCODE_ENDSWITCH);
+
+ return false;
+ }
+
Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, TIntermNode *src3, TIntermNode *src4)
{
return emit(op, dst, 0, src0, 0, src1, 0, src2, 0, src3, 0, src4, 0);