Decorate all GLSL user-defined names with an underscore to avoid name clashes
TRAC #11314
Signed-off-by: Andrew Lewycky
Signed-off-by: Daniel Koch

Author:    Nicolas Capens

git-svn-id: https://angleproject.googlecode.com/svn/trunk@143 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/compiler/OutputHLSL.cpp b/src/compiler/OutputHLSL.cpp
index db64eac..bebbda8 100644
--- a/src/compiler/OutputHLSL.cpp
+++ b/src/compiler/OutputHLSL.cpp
@@ -65,7 +65,7 @@
 
                 if (qualifier == EvqUniform)
                 {
-                    uniforms += "uniform " + typeString(type) + " " + name + arrayString(type) + ";\n";
+                    uniforms += "uniform " + typeString(type) + " " + decorate(name) + arrayString(type) + ";\n";
                 }
                 else if (qualifier == EvqVaryingIn || qualifier == EvqInvariantVaryingIn)
                 {
@@ -74,8 +74,8 @@
                     semanticIndex += type.isArray() ? type.getArraySize() : 1;
 
                     // Program linking depends on this exact format
-                    varyingInput += "    " + typeString(type) + " " + name + arrayString(type) + semantic + ";\n";
-                    varyingGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
+                    varyingInput += "    " + typeString(type) + " " + decorate(name) + arrayString(type) + semantic + ";\n";
+                    varyingGlobals += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
                 }
                 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
                 {
@@ -91,7 +91,7 @@
 
         out << "uniform float4 gl_Window;\n"
                "uniform float2 gl_Depth;\n"
-               "uniform bool __frontCCW;\n"
+               "uniform bool gl_frontCCW;\n"
                "\n";
         out <<  uniforms;
         out << "\n"
@@ -99,7 +99,7 @@
                "{\n";
         out <<      varyingInput;
         out << "    float4 gl_FragCoord : TEXCOORD" << semanticIndex << ";\n";
-        out << "    float __vFace : VFACE;\n"
+        out << "    float vFace : VFACE;\n"
                "};\n"
                "\n";
         out <<    varyingGlobals;
@@ -158,7 +158,7 @@
 
                 if (qualifier == EvqUniform)
                 {
-                    uniforms += "uniform " + typeString(type) + " " + name + arrayString(type) + ";\n";
+                    uniforms += "uniform " + typeString(type) + " " + decorate(name) + arrayString(type) + ";\n";
                 }
                 else if (qualifier == EvqAttribute)
                 {
@@ -166,14 +166,14 @@
                     sprintf(semantic, " : TEXCOORD%d", semanticIndex);
                     semanticIndex += type.isArray() ? type.getArraySize() : 1;
 
-                    attributeInput += "    " + typeString(type) + " " + name + arrayString(type) + semantic + ";\n";
-                    attributeGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
+                    attributeInput += "    " + typeString(type) + " " + decorate(name) + arrayString(type) + semantic + ";\n";
+                    attributeGlobals += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
                 }
                 else if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
                 {
                     // Program linking depends on this exact format
-                    varyingOutput += "    " + typeString(type) + " " + name + arrayString(type) + " : TEXCOORD0;\n";   // Actual semantic index assigned during link
-                    varyingGlobals += "static " + typeString(type) + " " + name + arrayString(type) + " = " + initializer(type) + ";\n";
+                    varyingOutput += "    " + typeString(type) + " " + decorate(name) + arrayString(type) + " : TEXCOORD0;\n";   // Actual semantic index assigned during link
+                    varyingGlobals += "static " + typeString(type) + " " + decorate(name) + arrayString(type) + " = " + initializer(type) + ";\n";
                 }
                 else if (qualifier == EvqGlobal || qualifier == EvqTemporary)
                 {
@@ -417,7 +417,7 @@
 
     if (mUsesEqualMat2)
     {
-        out << "bool __equal(float2x2 m, float2x2 n)\n"
+        out << "bool equal(float2x2 m, float2x2 n)\n"
                "{\n"
                "    return m[0][0] == n[0][0] && m[0][1] == n[0][1] &&\n"
                "           m[1][0] == n[1][0] && m[1][1] == n[1][1];\n"
@@ -426,7 +426,7 @@
 
     if (mUsesEqualMat3)
     {
-        out << "bool __equal(float3x3 m, float3x3 n)\n"
+        out << "bool equal(float3x3 m, float3x3 n)\n"
                "{\n"
                "    return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] &&\n"
                "           m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] &&\n"
@@ -436,7 +436,7 @@
 
     if (mUsesEqualMat4)
     {
-        out << "bool __equal(float4x4 m, float4x4 n)\n"
+        out << "bool equal(float4x4 m, float4x4 n)\n"
                "{\n"
                "    return m[0][0] == n[0][0] && m[0][1] == n[0][1] && m[0][2] == n[0][2] && m[0][3] == n[0][3] &&\n"
                "           m[1][0] == n[1][0] && m[1][1] == n[1][1] && m[1][2] == n[1][2] && m[1][3] == n[1][3] &&\n"
@@ -447,7 +447,7 @@
 
     if (mUsesEqualVec2)
     {
-        out << "bool __equal(float2 v, float2 u)\n"
+        out << "bool equal(float2 v, float2 u)\n"
                "{\n"
                "    return v.x == u.x && v.y == u.y;\n"
                "}\n";
@@ -455,7 +455,7 @@
 
     if (mUsesEqualVec3)
     {
-        out << "bool __equal(float3 v, float3 u)\n"
+        out << "bool equal(float3 v, float3 u)\n"
                "{\n"
                "    return v.x == u.x && v.y == u.y && v.z == u.z;\n"
                "}\n";
@@ -463,7 +463,7 @@
 
     if (mUsesEqualVec4)
     {
-        out << "bool __equal(float4 v, float4 u)\n"
+        out << "bool equal(float4 v, float4 u)\n"
                "{\n"
                "    return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
                "}\n";
@@ -471,7 +471,7 @@
 
     if (mUsesEqualIVec2)
     {
-        out << "bool __equal(int2 v, int2 u)\n"
+        out << "bool equal(int2 v, int2 u)\n"
                "{\n"
                "    return v.x == u.x && v.y == u.y;\n"
                "}\n";
@@ -479,7 +479,7 @@
 
     if (mUsesEqualIVec3)
     {
-        out << "bool __equal(int3 v, int3 u)\n"
+        out << "bool equal(int3 v, int3 u)\n"
                "{\n"
                "    return v.x == u.x && v.y == u.y && v.z == u.z;\n"
                "}\n";
@@ -487,7 +487,7 @@
 
     if (mUsesEqualIVec4)
     {
-        out << "bool __equal(int4 v, int4 u)\n"
+        out << "bool equal(int4 v, int4 u)\n"
                "{\n"
                "    return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
                "}\n";
@@ -495,7 +495,7 @@
 
     if (mUsesEqualBVec2)
     {
-        out << "bool __equal(bool2 v, bool2 u)\n"
+        out << "bool equal(bool2 v, bool2 u)\n"
                "{\n"
                "    return v.x == u.x && v.y == u.y;\n"
                "}\n";
@@ -503,7 +503,7 @@
 
     if (mUsesEqualBVec3)
     {
-        out << "bool __equal(bool3 v, bool3 u)\n"
+        out << "bool equal(bool3 v, bool3 u)\n"
                "{\n"
                "    return v.x == u.x && v.y == u.y && v.z == u.z;\n"
                "}\n";
@@ -511,7 +511,7 @@
 
     if (mUsesEqualBVec4)
     {
-        out << "bool __equal(bool4 v, bool4 u)\n"
+        out << "bool equal(bool4 v, bool4 u)\n"
                "{\n"
                "    return v.x == u.x && v.y == u.y && v.z == u.z && v.w == u.w;\n"
                "}\n";
@@ -533,7 +533,7 @@
                "    gl_FragCoord.y = (input.gl_FragCoord.y * rhw) * gl_Window.y + gl_Window.w;\n"
                "    gl_FragCoord.z = (input.gl_FragCoord.z * rhw) * gl_Depth.x + gl_Depth.y;\n"
                "    gl_FragCoord.w = rhw;\n"
-               "    gl_FrontFacing = __frontCCW ? (input.__vFace >= 0.0) : (input.__vFace <= 0.0);\n";
+               "    gl_FrontFacing = gl_frontCCW ? (input.vFace >= 0.0) : (input.vFace <= 0.0);\n";
 
         for (TSymbolTableLevel::const_iterator namedSymbol = symbols->begin(); namedSymbol != symbols->end(); namedSymbol++)
         {
@@ -548,7 +548,7 @@
 
                 if (qualifier == EvqVaryingIn)
                 {
-                    out << "    " + name + " = input." + name + ";\n";   // FIXME: Prevent name clashes
+                    out << "    " + decorate(name) + " = input." + decorate(name) + ";\n";   // FIXME: Prevent name clashes
                 }
             }
         }
@@ -577,7 +577,7 @@
 
                 if (qualifier == EvqAttribute)
                 {
-                    out << "    " + name + " = input." + name + ";\n";   // FIXME: Prevent name clashes
+                    out << "    " + decorate(name) + " = input." + decorate(name) + ";\n";   // FIXME: Prevent name clashes
                 }
             }
         }
@@ -608,7 +608,7 @@
                 if (qualifier == EvqVaryingOut || qualifier == EvqInvariantVaryingOut)
                 {
                     // Program linking depends on this exact format
-                    out << "    output." + name + " = " + name + ";\n";   // FIXME: Prevent name clashes
+                    out << "    output." + decorate(name) + " = " + decorate(name) + ";\n";   // FIXME: Prevent name clashes
                 }
             }
         }
@@ -634,7 +634,7 @@
     }
     else
     {
-        out << name;
+        out << decorate(name);
     }
 }
 
@@ -822,11 +822,11 @@
 
             if (node->getOp() == EOpEqual)
             {
-                outputTriplet(visit, "__equal(", ", ", ")");
+                outputTriplet(visit, "equal(", ", ", ")");
             }
             else
             {
-                outputTriplet(visit, "!__equal(", ", ", ")");
+                outputTriplet(visit, "!equal(", ", ", ")");
             }
         }
         break;
@@ -986,7 +986,7 @@
                     const TType &type = variable->getType();
                     const TTypeList &fields = *type.getStruct();
 
-                    out << "struct " + type.getTypeName() + "\n"
+                    out << "struct " + decorate(type.getTypeName()) + "\n"
                            "{\n";
 
                     for (unsigned int i = 0; i < fields.size(); i++)
@@ -1011,7 +1011,7 @@
       case EOpPrototype:
         if (visit == PreVisit)
         {
-            out << typeString(node->getType()) << " " << node->getName() << "(";
+            out << typeString(node->getType()) << " " << decorate(node->getName()) << "(";
 
             TIntermSequence &arguments = node->getSequence();
 
@@ -1036,19 +1036,23 @@
             return false;
         }
         break;
-      case EOpComma:         UNIMPLEMENTED(); /* FIXME */ out << "Comma\n"; return true;
+      case EOpComma:            outputTriplet(visit, NULL, ", ", NULL);                break;
       case EOpFunction:
         {
             TString name = TFunction::unmangleName(node->getName());
 
             if (visit == PreVisit)
             {
+                out << typeString(node->getType()) << " ";
+
                 if (name == "main")
                 {
-                    name = "gl_main";
+                    out << "gl_main(";
                 }
-
-                out << typeString(node->getType()) << " " << name << "(";
+                else
+                {
+                    out << decorate(name) << "(";
+                }
 
                 TIntermSequence &sequence = node->getSequence();
                 TIntermSequence &arguments = sequence[0]->getAsAggregate()->getSequence();
@@ -1088,7 +1092,7 @@
 
                 if (node->isUserDefined())
                 {
-                    out << name << "(";
+                    out << decorate(name) << "(";
                 }
                 else
                 {
@@ -1640,7 +1644,7 @@
     const TType &type = symbol->getType();
     const TString &name = symbol->getSymbol();
 
-    return qualifierString(qualifier) + " " + typeString(type) + " " + name + arrayString(type);
+    return qualifierString(qualifier) + " " + typeString(type) + " " + decorate(name) + arrayString(type);
 }
 
 TString OutputHLSL::qualifierString(TQualifier qualifier)
@@ -1661,7 +1665,7 @@
 {
     if (type.getBasicType() == EbtStruct)
     {
-        return type.getTypeName();
+        return decorate(type.getTypeName());
     }
     else if (type.isMatrix())
     {
@@ -1766,4 +1770,16 @@
 
     return string;
 }
+
+TString OutputHLSL::decorate(const TString &string)
+{
+    if (string.substr(0, 3) != "gl_")
+    {
+        return "_" + string;
+    }
+    else
+    {
+        return string;
+    }
+}
 }
diff --git a/src/compiler/OutputHLSL.h b/src/compiler/OutputHLSL.h
index bb6203f..43ee78c 100644
--- a/src/compiler/OutputHLSL.h
+++ b/src/compiler/OutputHLSL.h
@@ -41,6 +41,7 @@
     static TString typeString(const TType &type);
     static TString arrayString(const TType &type);
     static TString initializer(const TType &type);
+    static TString decorate(const TString &string);   // Prepend an underscore to avoid naming clashes
 
     TParseContext &mContext;
 
diff --git a/src/libGLESv2/Context.cpp b/src/libGLESv2/Context.cpp
index 42cef63..cc941e8 100644
--- a/src/libGLESv2/Context.cpp
+++ b/src/libGLESv2/Context.cpp
@@ -1261,7 +1261,7 @@
     IDirect3DDevice9 *device = getDevice();
     Program *programObject = getCurrentProgram();
 
-    GLint frontCCW = programObject->getUniformLocation("__frontCCW");
+    GLint frontCCW = programObject->getUniformLocation("gl_frontCCW");
     GLint ccw = (frontFace == GL_CCW);
     programObject->setUniform1iv(frontCCW, 1, &ccw);
 
diff --git a/src/libGLESv2/Program.cpp b/src/libGLESv2/Program.cpp
index e886a86..9683956 100644
--- a/src/libGLESv2/Program.cpp
+++ b/src/libGLESv2/Program.cpp
@@ -195,7 +195,7 @@
 {
     for (unsigned int location = 0; location < mUniforms.size(); location++)
     {
-        if (mUniforms[location]->name == name)
+        if (mUniforms[location]->name == decorate(name))
         {
             return location;
         }
@@ -1250,6 +1250,19 @@
     return 0;
 }
 
+// This methods needs to match OutputHLSL::decorate
+std::string Program::decorate(const std::string &string)
+{
+    if (string.substr(0, 3) != "gl_")
+    {
+        return "_" + string;
+    }
+    else
+    {
+        return string;
+    }
+}
+
 bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v)
 {
     BOOL *vector = new BOOL[count];
diff --git a/src/libGLESv2/Program.h b/src/libGLESv2/Program.h
index 6ce38eb..d848af6 100644
--- a/src/libGLESv2/Program.h
+++ b/src/libGLESv2/Program.h
@@ -112,6 +112,7 @@
     bool defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name = "");
     bool defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name);
     Uniform *createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name);
+    static std::string decorate(const std::string &string);   // Prepend an underscore
     bool applyUniform1bv(GLint location, GLsizei count, const GLboolean *v);
     bool applyUniform2bv(GLint location, GLsizei count, const GLboolean *v);
     bool applyUniform3bv(GLint location, GLsizei count, const GLboolean *v);
diff --git a/src/libGLESv2/Shader.cpp b/src/libGLESv2/Shader.cpp
index 7144a8f..7deef0c 100644
--- a/src/libGLESv2/Shader.cpp
+++ b/src/libGLESv2/Shader.cpp
@@ -348,7 +348,7 @@
             char attributeName[100];
             int semanticIndex;
 
-            int matches = sscanf(input, "%s : TEXCOORD%d;", attributeName, &semanticIndex);
+            int matches = sscanf(input, "_%s : TEXCOORD%d;", attributeName, &semanticIndex);
 
             if (matches == 2)
             {