Commit b9f64aae by zmo@google.com

Use a global ref-counted singleton for long name map.

This makes sure the same varying/uniform variables maps to the unique name in vertex/fragment shader. BUG= TEST=webgl conformance tests Review URL: https://codereview.appspot.com/5556065 git-svn-id: https://angleproject.googlecode.com/svn/trunk@950 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 9996b8e6
#define MAJOR_VERSION 1 #define MAJOR_VERSION 1
#define MINOR_VERSION 0 #define MINOR_VERSION 0
#define BUILD_VERSION 0 #define BUILD_VERSION 0
#define BUILD_REVISION 949 #define BUILD_REVISION 950
#define STRINGIFY(x) #x #define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x) #define MACRO_STRINGIFY(x) STRINGIFY(x)
......
// //
// 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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
...@@ -8,10 +8,10 @@ ...@@ -8,10 +8,10 @@
#include "compiler/DetectRecursion.h" #include "compiler/DetectRecursion.h"
#include "compiler/ForLoopUnroll.h" #include "compiler/ForLoopUnroll.h"
#include "compiler/Initialize.h" #include "compiler/Initialize.h"
#include "compiler/MapLongVariableNames.h"
#include "compiler/ParseHelper.h" #include "compiler/ParseHelper.h"
#include "compiler/ShHandle.h" #include "compiler/ShHandle.h"
#include "compiler/ValidateLimitations.h" #include "compiler/ValidateLimitations.h"
#include "compiler/MapLongVariableNames.h"
namespace { namespace {
bool InitializeSymbolTable( bool InitializeSymbolTable(
...@@ -91,10 +91,13 @@ TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec) ...@@ -91,10 +91,13 @@ TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec)
shaderSpec(spec), shaderSpec(spec),
builtInFunctionEmulator(type) builtInFunctionEmulator(type)
{ {
longNameMap = LongNameMap::GetInstance();
} }
TCompiler::~TCompiler() TCompiler::~TCompiler()
{ {
ASSERT(longNameMap);
longNameMap->Release();
} }
bool TCompiler::Init(const ShBuiltInResources& resources) bool TCompiler::Init(const ShBuiltInResources& resources)
...@@ -246,7 +249,8 @@ void TCompiler::collectAttribsUniforms(TIntermNode* root) ...@@ -246,7 +249,8 @@ void TCompiler::collectAttribsUniforms(TIntermNode* root)
void TCompiler::mapLongVariableNames(TIntermNode* root) void TCompiler::mapLongVariableNames(TIntermNode* root)
{ {
MapLongVariableNames map(varyingLongNameMap); ASSERT(longNameMap);
MapLongVariableNames map(longNameMap);
root->traverse(&map); root->traverse(&map);
} }
......
...@@ -8,24 +8,75 @@ ...@@ -8,24 +8,75 @@
namespace { namespace {
TString mapLongName(int id, const TString& name, bool isVarying) TString mapLongName(int id, const TString& name, bool isGlobal)
{ {
ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE); ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE);
TStringStream stream; TStringStream stream;
stream << "webgl_"; stream << "webgl_";
if (isVarying) if (isGlobal)
stream << "v"; stream << "g";
stream << id << "_"; stream << id << "_";
stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size()); stream << name.substr(0, MAX_SHORTENED_IDENTIFIER_SIZE - stream.str().size());
return stream.str(); return stream.str();
} }
LongNameMap* gLongNameMapInstance = NULL;
} // anonymous namespace } // anonymous namespace
MapLongVariableNames::MapLongVariableNames( LongNameMap::LongNameMap()
std::map<std::string, std::string>& varyingLongNameMap) : refCount(0)
: mVaryingLongNameMap(varyingLongNameMap) {
}
LongNameMap::~LongNameMap()
{
}
// static
LongNameMap* LongNameMap::GetInstance()
{
if (gLongNameMapInstance == NULL)
gLongNameMapInstance = new LongNameMap;
gLongNameMapInstance->refCount++;
return gLongNameMapInstance;
}
void LongNameMap::Release()
{
ASSERT(gLongNameMapInstance == this);
ASSERT(refCount > 0);
refCount--;
if (refCount == 0) {
delete gLongNameMapInstance;
gLongNameMapInstance = NULL;
}
}
const char* LongNameMap::Find(const char* originalName) const
{ {
std::map<std::string, std::string>::const_iterator it = mLongNameMap.find(
originalName);
if (it != mLongNameMap.end())
return (*it).second.c_str();
return NULL;
}
void LongNameMap::Insert(const char* originalName, const char* mappedName)
{
mLongNameMap.insert(std::map<std::string, std::string>::value_type(
originalName, mappedName));
}
int LongNameMap::Size() const
{
return mLongNameMap.size();
}
MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap)
{
ASSERT(globalMap);
mGlobalMap = globalMap;
} }
void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol) void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
...@@ -39,7 +90,7 @@ void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol) ...@@ -39,7 +90,7 @@ void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol)
case EvqInvariantVaryingOut: case EvqInvariantVaryingOut:
case EvqUniform: case EvqUniform:
symbol->setSymbol( symbol->setSymbol(
mapVaryingLongName(symbol->getSymbol())); mapGlobalLongName(symbol->getSymbol()));
break; break;
default: default:
symbol->setSymbol( symbol->setSymbol(
...@@ -56,15 +107,14 @@ bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node) ...@@ -56,15 +107,14 @@ bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node)
return true; return true;
} }
TString MapLongVariableNames::mapVaryingLongName(const TString& name) TString MapLongVariableNames::mapGlobalLongName(const TString& name)
{ {
std::map<std::string, std::string>::const_iterator it = mVaryingLongNameMap.find(name.c_str()); ASSERT(mGlobalMap);
if (it != mVaryingLongNameMap.end()) const char* mappedName = mGlobalMap->Find(name.c_str());
return (*it).second.c_str(); if (mappedName != NULL)
return mappedName;
int id = mVaryingLongNameMap.size(); int id = mGlobalMap->Size();
TString mappedName = mapLongName(id, name, true); TString rt = mapLongName(id, name, true);
mVaryingLongNameMap.insert( mGlobalMap->Insert(name.c_str(), rt.c_str());
std::map<std::string, std::string>::value_type(name.c_str(), mappedName.c_str())); return rt;
return mappedName;
} }
// //
// 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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
...@@ -15,19 +15,45 @@ ...@@ -15,19 +15,45 @@
// This size does not include '\0' in the end. // This size does not include '\0' in the end.
#define MAX_SHORTENED_IDENTIFIER_SIZE 32 #define MAX_SHORTENED_IDENTIFIER_SIZE 32
// This is a ref-counted singleton. GetInstance() returns a pointer to the
// singleton, and after use, call Release(). GetInstance() and Release() should
// be paired.
class LongNameMap {
public:
static LongNameMap* GetInstance();
void Release();
// Return the mapped name if <originalName, mappedName> is in the map;
// otherwise, return NULL.
const char* Find(const char* originalName) const;
// Insert a pair into the map.
void Insert(const char* originalName, const char* mappedName);
// Return the number of entries in the map.
int Size() const;
private:
LongNameMap();
~LongNameMap();
size_t refCount;
std::map<std::string, std::string> mLongNameMap;
};
// Traverses intermediate tree to map attributes and uniforms names that are // Traverses intermediate tree to map attributes and uniforms names that are
// longer than MAX_SHORTENED_IDENTIFIER_SIZE to MAX_SHORTENED_IDENTIFIER_SIZE. // longer than MAX_SHORTENED_IDENTIFIER_SIZE to MAX_SHORTENED_IDENTIFIER_SIZE.
class MapLongVariableNames : public TIntermTraverser { class MapLongVariableNames : public TIntermTraverser {
public: public:
MapLongVariableNames(std::map<std::string, std::string>& varyingLongNameMap); MapLongVariableNames(LongNameMap* globalMap);
virtual void visitSymbol(TIntermSymbol*); virtual void visitSymbol(TIntermSymbol*);
virtual bool visitLoop(Visit, TIntermLoop*); virtual bool visitLoop(Visit, TIntermLoop*);
private: private:
TString mapVaryingLongName(const TString& name); TString mapGlobalLongName(const TString& name);
std::map<std::string, std::string>& mVaryingLongNameMap; LongNameMap* mGlobalMap;
}; };
#endif // COMPILER_MAP_LONG_VARIABLE_NAMES_H_ #endif // COMPILER_MAP_LONG_VARIABLE_NAMES_H_
// //
// Copyright (c) 2002-2010 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 // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// //
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "compiler/SymbolTable.h" #include "compiler/SymbolTable.h"
#include "compiler/VariableInfo.h" #include "compiler/VariableInfo.h"
class LongNameMap;
class TCompiler; class TCompiler;
// //
...@@ -100,8 +101,8 @@ private: ...@@ -100,8 +101,8 @@ private:
TVariableInfoList attribs; // Active attributes in the compiled shader. TVariableInfoList attribs; // Active attributes in the compiled shader.
TVariableInfoList uniforms; // Active uniforms in the compiled shader. TVariableInfoList uniforms; // Active uniforms in the compiled shader.
// Pair of long varying varibale name <originalName, mappedName>. // Cached copy of the ref-counted singleton.
std::map<std::string, std::string> varyingLongNameMap; LongNameMap* longNameMap;
}; };
// //
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment