Optimize uniform handling by storing both decorated and undecorated names. Use a consistent naming scheme to clarify decorated/undecorated name usage.

TRAC #16567
Bug=136
Signed-off-by: Daniel Koch
Author: Nicolas Capens

git-svn-id: https://angleproject.googlecode.com/svn/trunk@759 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/libGLESv2/Program.cpp b/src/libGLESv2/Program.cpp
index 98dc201..476999f 100644
--- a/src/libGLESv2/Program.cpp
+++ b/src/libGLESv2/Program.cpp
@@ -33,7 +33,8 @@
     return buffer;
 }
 
-Uniform::Uniform(GLenum type, const std::string &name, unsigned int arraySize) : type(type), name(name), arraySize(arraySize)
+Uniform::Uniform(GLenum type, const std::string &_name, unsigned int arraySize)
+    : type(type), _name(_name), name(Program::undecorate(_name)), arraySize(arraySize)
 {
     int bytes = UniformTypeSize(type) * arraySize;
     data = new unsigned char[bytes];
@@ -47,8 +48,13 @@
     delete[] data;
 }
 
-UniformLocation::UniformLocation(const std::string &name, unsigned int element, unsigned int index) 
-    : name(name), element(element), index(index)
+bool Uniform::isArray()
+{
+    return arraySize != 1;   // FIXME: Arrays can be of size 1
+}
+
+UniformLocation::UniformLocation(const std::string &_name, unsigned int element, unsigned int index) 
+    : name(Program::undecorate(_name)), element(element), index(index)
 {
 }
 
@@ -249,24 +255,23 @@
     return TEXTURE_2D;
 }
 
-GLint Program::getUniformLocation(const char *name, bool decorated)
+GLint Program::getUniformLocation(std::string name)
 {
-    std::string _name = decorated ? name : decorate(name);
     int subscript = 0;
 
     // Strip any trailing array operator and retrieve the subscript
-    size_t open = _name.find_last_of('[');
-    size_t close = _name.find_last_of(']');
-    if (open != std::string::npos && close == _name.length() - 1)
+    size_t open = name.find_last_of('[');
+    size_t close = name.find_last_of(']');
+    if (open != std::string::npos && close == name.length() - 1)
     {
-        subscript = atoi(_name.substr(open + 1).c_str());
-        _name.erase(open);
+        subscript = atoi(name.substr(open + 1).c_str());
+        name.erase(open);
     }
 
     unsigned int numUniforms = mUniformIndex.size();
     for (unsigned int location = 0; location < numUniforms; location++)
     {
-        if (mUniformIndex[location].name == _name &&
+        if (mUniformIndex[location].name == name &&
             mUniformIndex[location].element == subscript)
         {
             return location;
@@ -1588,12 +1593,12 @@
 
             // these uniforms are searched as already-decorated because gl_ and dx_
             // are reserved prefixes, and do not receive additional decoration
-            mDxDepthRangeLocation = getUniformLocation("dx_DepthRange", true);
-            mDxDepthLocation = getUniformLocation("dx_Depth", true);
-            mDxViewportLocation = getUniformLocation("dx_Viewport", true);
-            mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize", true);
-            mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW", true);
-            mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines", true);
+            mDxDepthRangeLocation = getUniformLocation("dx_DepthRange");
+            mDxDepthLocation = getUniformLocation("dx_Depth");
+            mDxViewportLocation = getUniformLocation("dx_Viewport");
+            mDxHalfPixelSizeLocation = getUniformLocation("dx_HalfPixelSize");
+            mDxFrontCCWLocation = getUniformLocation("dx_FrontCCW");
+            mDxPointsOrLinesLocation = getUniformLocation("dx_PointsOrLines");
 
             mLinked = true;   // Success
         }
@@ -1784,9 +1789,9 @@
     }
 }
 
-bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
+bool Program::defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &_name)
 {
-    Uniform *uniform = createUniform(constantDescription, name);
+    Uniform *uniform = createUniform(constantDescription, _name);
 
     if(!uniform)
     {
@@ -1794,7 +1799,7 @@
     }
 
     // Check if already defined
-    GLint location = getUniformLocation(name.c_str(), true);
+    GLint location = getUniformLocation(uniform->name);
     GLenum type = uniform->type;
 
     if (location >= 0)
@@ -1816,13 +1821,13 @@
 
     for (unsigned int i = 0; i < uniform->arraySize; ++i)
     {
-        mUniformIndex.push_back(UniformLocation(name, i, uniformIndex));
+        mUniformIndex.push_back(UniformLocation(_name, i, uniformIndex));
     }
 
     return true;
 }
 
-Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name)
+Uniform *Program::createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &_name)
 {
     if (constantDescription.Rows == 1)   // Vectors and scalars
     {
@@ -1831,44 +1836,44 @@
           case D3DXPT_SAMPLER2D:
             switch (constantDescription.Columns)
             {
-              case 1: return new Uniform(GL_SAMPLER_2D, name, constantDescription.Elements);
+              case 1: return new Uniform(GL_SAMPLER_2D, _name, constantDescription.Elements);
               default: UNREACHABLE();
             }
             break;
           case D3DXPT_SAMPLERCUBE:
             switch (constantDescription.Columns)
             {
-              case 1: return new Uniform(GL_SAMPLER_CUBE, name, constantDescription.Elements);
+              case 1: return new Uniform(GL_SAMPLER_CUBE, _name, constantDescription.Elements);
               default: UNREACHABLE();
             }
             break;
           case D3DXPT_BOOL:
             switch (constantDescription.Columns)
             {
-              case 1: return new Uniform(GL_BOOL, name, constantDescription.Elements);
-              case 2: return new Uniform(GL_BOOL_VEC2, name, constantDescription.Elements);
-              case 3: return new Uniform(GL_BOOL_VEC3, name, constantDescription.Elements);
-              case 4: return new Uniform(GL_BOOL_VEC4, name, constantDescription.Elements);
+              case 1: return new Uniform(GL_BOOL, _name, constantDescription.Elements);
+              case 2: return new Uniform(GL_BOOL_VEC2, _name, constantDescription.Elements);
+              case 3: return new Uniform(GL_BOOL_VEC3, _name, constantDescription.Elements);
+              case 4: return new Uniform(GL_BOOL_VEC4, _name, constantDescription.Elements);
               default: UNREACHABLE();
             }
             break;
           case D3DXPT_INT:
             switch (constantDescription.Columns)
             {
-              case 1: return new Uniform(GL_INT, name, constantDescription.Elements);
-              case 2: return new Uniform(GL_INT_VEC2, name, constantDescription.Elements);
-              case 3: return new Uniform(GL_INT_VEC3, name, constantDescription.Elements);
-              case 4: return new Uniform(GL_INT_VEC4, name, constantDescription.Elements);
+              case 1: return new Uniform(GL_INT, _name, constantDescription.Elements);
+              case 2: return new Uniform(GL_INT_VEC2, _name, constantDescription.Elements);
+              case 3: return new Uniform(GL_INT_VEC3, _name, constantDescription.Elements);
+              case 4: return new Uniform(GL_INT_VEC4, _name, constantDescription.Elements);
               default: UNREACHABLE();
             }
             break;
           case D3DXPT_FLOAT:
             switch (constantDescription.Columns)
             {
-              case 1: return new Uniform(GL_FLOAT, name, constantDescription.Elements);
-              case 2: return new Uniform(GL_FLOAT_VEC2, name, constantDescription.Elements);
-              case 3: return new Uniform(GL_FLOAT_VEC3, name, constantDescription.Elements);
-              case 4: return new Uniform(GL_FLOAT_VEC4, name, constantDescription.Elements);
+              case 1: return new Uniform(GL_FLOAT, _name, constantDescription.Elements);
+              case 2: return new Uniform(GL_FLOAT_VEC2, _name, constantDescription.Elements);
+              case 3: return new Uniform(GL_FLOAT_VEC3, _name, constantDescription.Elements);
+              case 4: return new Uniform(GL_FLOAT_VEC4, _name, constantDescription.Elements);
               default: UNREACHABLE();
             }
             break;
@@ -1883,9 +1888,9 @@
           case D3DXPT_FLOAT:
             switch (constantDescription.Rows)
             {
-              case 2: return new Uniform(GL_FLOAT_MAT2, name, constantDescription.Elements);
-              case 3: return new Uniform(GL_FLOAT_MAT3, name, constantDescription.Elements);
-              case 4: return new Uniform(GL_FLOAT_MAT4, name, constantDescription.Elements);
+              case 2: return new Uniform(GL_FLOAT_MAT2, _name, constantDescription.Elements);
+              case 3: return new Uniform(GL_FLOAT_MAT3, _name, constantDescription.Elements);
+              case 4: return new Uniform(GL_FLOAT_MAT4, _name, constantDescription.Elements);
               default: UNREACHABLE();
             }
             break;
@@ -1898,28 +1903,24 @@
 }
 
 // This method needs to match OutputHLSL::decorate
-std::string Program::decorate(const std::string &string)
+std::string Program::decorate(const std::string &name)
 {
-    if (string.substr(0, 3) != "gl_" && string.substr(0, 3) != "dx_")
+    if (name.substr(0, 3) != "gl_" && name.substr(0, 3) != "dx_")
     {
-        return "_" + string;
+        return "_" + name;
     }
-    else
-    {
-        return string;
-    }
+    
+    return name;
 }
 
-std::string Program::undecorate(const std::string &string)
+std::string Program::undecorate(const std::string &_name)
 {
-    if (string.substr(0, 1) == "_")
+    if (_name.substr(0, 1) == "_")
     {
-        return string.substr(1);
+        return _name.substr(1);
     }
-    else
-    {
-        return string;
-    }
+    
+    return _name;
 }
 
 bool Program::applyUniform1bv(GLint location, GLsizei count, const GLboolean *v)
@@ -2790,9 +2791,9 @@
 
     if (bufsize > 0)
     {
-        std::string string = undecorate(mUniforms[uniform]->name);
+        std::string string = mUniforms[uniform]->name;
 
-        if (mUniforms[uniform]->arraySize != 1)
+        if (mUniforms[uniform]->isArray())
         {
             string += "[0]";
         }
@@ -2836,8 +2837,8 @@
     {
         if (!mUniforms[uniformIndex]->name.empty() && mUniforms[uniformIndex]->name.substr(0, 3) != "dx_")
         {
-            int length = (int)(undecorate(mUniforms[uniformIndex]->name).length() + 1);
-            if (mUniforms[uniformIndex]->arraySize != 1)
+            int length = (int)(mUniforms[uniformIndex]->name.length() + 1);
+            if (mUniforms[uniformIndex]->isArray())
             {
                 length += 3;  // Counting in "[0]".
             }
@@ -2972,8 +2973,8 @@
 {
     if (!targetUniform->handlesSet)
     {
-        targetUniform->psHandle = mConstantTablePS->GetConstantByName(0, targetUniform->name.c_str());
-        targetUniform->vsHandle = mConstantTableVS->GetConstantByName(0, targetUniform->name.c_str());
+        targetUniform->psHandle = mConstantTablePS->GetConstantByName(0, targetUniform->_name.c_str());
+        targetUniform->vsHandle = mConstantTableVS->GetConstantByName(0, targetUniform->_name.c_str());
         targetUniform->handlesSet = true;
     }