Long name mapping needs to be consistent between vertex/fragment shaders.

For example: varying variables, uniforms.

This CL makes MapLongVariableNames a ref-counted singleton and therefore, the map is shared by all shaders.

Also, function/variable names changes from Varying to Global because uniforms also need to be consistent, not just varying variables.

ANGLEBUG=279
TEST=webgl conformance tests, especially invalid-passed-params.html and glsl-long-variable-names.html
Review URL: http://codereview.appspot.com/5539046

git-svn-id: https://angleproject.googlecode.com/svn/trunk@942 736b8ea6-26fd-11df-bfd4-992fa37f6226
diff --git a/src/compiler/Compiler.cpp b/src/compiler/Compiler.cpp
index 22764bd..3a50319 100644
--- a/src/compiler/Compiler.cpp
+++ b/src/compiler/Compiler.cpp
@@ -1,5 +1,5 @@
 //
-// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
+// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 //
@@ -89,12 +89,15 @@
 TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec)
     : shaderType(type),
       shaderSpec(spec),
-      builtInFunctionEmulator(type)
+      builtInFunctionEmulator(type),
+      longNameMapper(NULL)
 {
 }
 
 TCompiler::~TCompiler()
 {
+    if (longNameMapper)
+        longNameMapper->Release();
 }
 
 bool TCompiler::Init(const ShBuiltInResources& resources)
@@ -246,8 +249,9 @@
 
 void TCompiler::mapLongVariableNames(TIntermNode* root)
 {
-    MapLongVariableNames map(varyingLongNameMap);
-    root->traverse(&map);
+    if (longNameMapper == NULL)
+        longNameMapper = MapLongVariableNames::GetInstance();
+    root->traverse(longNameMapper);
 }
 
 int TCompiler::getMappedNameMaxLength() const
diff --git a/src/compiler/MapLongVariableNames.cpp b/src/compiler/MapLongVariableNames.cpp
index 3c5d356..678f674 100644
--- a/src/compiler/MapLongVariableNames.cpp
+++ b/src/compiler/MapLongVariableNames.cpp
@@ -8,26 +8,51 @@
 
 namespace {
 
-TString mapLongName(int id, const TString& name, bool isVarying)
+TString mapLongName(int id, const TString& name, bool global)
 {
     ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE);
     TStringStream stream;
     stream << "webgl_";
-    if (isVarying)
-        stream << "v";
+    if (global)
+        stream << "g";
     stream << id << "_";
     stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size());
     return stream.str();
 }
 
+MapLongVariableNames* gMapLongVariableNamesInstance = NULL;
+
 }  // anonymous namespace
 
-MapLongVariableNames::MapLongVariableNames(
-    std::map<std::string, std::string>& varyingLongNameMap)
-    : mVaryingLongNameMap(varyingLongNameMap)
+MapLongVariableNames::MapLongVariableNames()
+    : refCount(0)
 {
 }
 
+MapLongVariableNames::~MapLongVariableNames()
+{
+}
+
+// static
+MapLongVariableNames* MapLongVariableNames::GetInstance()
+{
+    if (gMapLongVariableNamesInstance == NULL)
+        gMapLongVariableNamesInstance = new MapLongVariableNames;
+    gMapLongVariableNamesInstance->refCount++;
+    return gMapLongVariableNamesInstance;
+}
+
+void MapLongVariableNames::Release()
+{
+    ASSERT(gMapLongVariableNamesInstance == this);
+    ASSERT(refCount > 0);
+    refCount--;
+    if (refCount == 0) {
+        delete gMapLongVariableNamesInstance;
+        gMapLongVariableNamesInstance = NULL;
+    }
+}
+
 void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
 {
     ASSERT(symbol != NULL);
@@ -39,7 +64,7 @@
           case EvqInvariantVaryingOut:
           case EvqUniform:
             symbol->setSymbol(
-                mapVaryingLongName(symbol->getSymbol()));
+                mapLongGlobalName(symbol->getSymbol()));
             break;
           default:
             symbol->setSymbol(
@@ -56,15 +81,15 @@
     return true;
 }
 
-TString MapLongVariableNames::mapVaryingLongName(const TString& name)
+TString MapLongVariableNames::mapLongGlobalName(const TString& name)
 {
-    std::map<std::string, std::string>::const_iterator it = mVaryingLongNameMap.find(name.c_str());
-    if (it != mVaryingLongNameMap.end())
+    std::map<std::string, std::string>::const_iterator it = longGlobalNameMap.find(name.c_str());
+    if (it != longGlobalNameMap.end())
         return (*it).second.c_str();
 
-    int id = mVaryingLongNameMap.size();
+    int id = longGlobalNameMap.size();
     TString mappedName = mapLongName(id, name, true);
-    mVaryingLongNameMap.insert(
+    longGlobalNameMap.insert(
         std::map<std::string, std::string>::value_type(name.c_str(), mappedName.c_str()));
     return mappedName;
 }
diff --git a/src/compiler/MapLongVariableNames.h b/src/compiler/MapLongVariableNames.h
index 086f2b9..0a5d609 100644
--- a/src/compiler/MapLongVariableNames.h
+++ b/src/compiler/MapLongVariableNames.h
@@ -1,5 +1,5 @@
 //
-// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
+// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 //
@@ -15,19 +15,30 @@
 // This size does not include '\0' in the end.
 #define MAX_SHORTENED_IDENTIFIER_SIZE 32
 
+// MapLongVariableNames is implemented as a ref-counted singleton.  The first
+// call of GetInstance() will create an instance and return it; latter calls
+// will return the same instance, with ref-count increased.  Release() will
+// reduce the ref-count, and when no more reference, release the instance.
+
 // Traverses intermediate tree to map attributes and uniforms names that are
 // longer than MAX_SHORTENED_IDENTIFIER_SIZE to MAX_SHORTENED_IDENTIFIER_SIZE.
 class MapLongVariableNames : public TIntermTraverser {
 public:
-    MapLongVariableNames(std::map<std::string, std::string>& varyingLongNameMap);
+    static MapLongVariableNames* GetInstance();
+    void Release();
 
     virtual void visitSymbol(TIntermSymbol*);
     virtual bool visitLoop(Visit, TIntermLoop*);
 
 private:
-    TString mapVaryingLongName(const TString& name);
+    MapLongVariableNames();
+    virtual ~MapLongVariableNames();
 
-    std::map<std::string, std::string>& mVaryingLongNameMap;
+    TString mapLongGlobalName(const TString& name);
+
+    // Pair of long global varibale name <originalName, mappedName>.
+    std::map<std::string, std::string> longGlobalNameMap;
+    size_t refCount;
 };
 
 #endif  // COMPILER_MAP_LONG_VARIABLE_NAMES_H_
diff --git a/src/compiler/ShHandle.h b/src/compiler/ShHandle.h
index 8ddf0f1..a775460 100644
--- a/src/compiler/ShHandle.h
+++ b/src/compiler/ShHandle.h
@@ -22,6 +22,7 @@
 #include "compiler/SymbolTable.h"
 #include "compiler/VariableInfo.h"
 
+class MapLongVariableNames;
 class TCompiler;
 
 //
@@ -100,8 +101,8 @@
     TVariableInfoList attribs;  // Active attributes in the compiled shader.
     TVariableInfoList uniforms;  // Active uniforms in the compiled shader.
 
-    // Pair of long varying varibale name <originalName, mappedName>.
-    std::map<std::string, std::string> varyingLongNameMap;
+    // Local instance of the ref-counted singleton.
+    MapLongVariableNames* longNameMapper;
 };
 
 //