Define new variables after evaluating the initialization expression.
TRAC #13627
GLSL allows to write things like "float x = x;" where a new variable x is defined and the value of an existing variable x is assigned. HLSL uses C semantics (the new variable is created before the assignment is evaluated), so we need to convert this to "float t = x, x = t;".
Signed-off-by: Daniel Koch
Author: Nicolas Capens
git-svn-id: https://angleproject.googlecode.com/svn/trunk@478 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/compiler/OutputHLSL.cpp b/src/compiler/OutputHLSL.cpp
index 0e61d85..e306552 100644
--- a/src/compiler/OutputHLSL.cpp
+++ b/src/compiler/OutputHLSL.cpp
@@ -64,7 +64,7 @@
mScopeDepth = 0;
- mArgumentIndex = 0;
+ mUniqueIndex = 0;
}
OutputHLSL::~OutputHLSL()
@@ -653,7 +653,26 @@
switch (node->getOp())
{
case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break;
- case EOpInitialize: outputTriplet(visit, "", " = ", ""); break;
+ case EOpInitialize:
+ if (visit == PreVisit)
+ {
+ // GLSL allows to write things like "float x = x;" where a new variable x is defined
+ // and the value of an existing variable x is assigned. HLSL uses C semantics (the
+ // new variable is created before the assignment is evaluated), so we need to convert
+ // this to "float t = x, x = t;".
+
+ // Type already printed
+ out << "t" + str(mUniqueIndex) + " = ";
+ node->getRight()->traverse(this);
+ out << ", ";
+ node->getLeft()->traverse(this);
+ out << " = t" + str(mUniqueIndex);
+
+ mUniqueIndex++;
+
+ return false;
+ }
+ break;
case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break;
case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break;
case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break;
@@ -1711,7 +1730,7 @@
if (name.empty()) // HLSL demands named arguments, also for prototypes
{
- name = "x" + str(mArgumentIndex++);
+ name = "x" + str(mUniqueIndex++);
}
else
{
diff --git a/src/compiler/OutputHLSL.h b/src/compiler/OutputHLSL.h
index dc3c482..ddbd077 100644
--- a/src/compiler/OutputHLSL.h
+++ b/src/compiler/OutputHLSL.h
@@ -120,7 +120,7 @@
ScopeBracket mScopeBracket;
unsigned int mScopeDepth;
- int mArgumentIndex; // For creating unique argument names
+ int mUniqueIndex; // For creating unique names
};
}