1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/angle/src/compiler/MapLongVariableNames.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,132 @@ 1.4 +// 1.5 +// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. 1.6 +// Use of this source code is governed by a BSD-style license that can be 1.7 +// found in the LICENSE file. 1.8 +// 1.9 + 1.10 +#include "compiler/MapLongVariableNames.h" 1.11 + 1.12 +#include "third_party/murmurhash/MurmurHash3.h" 1.13 + 1.14 +namespace { 1.15 + 1.16 +TString mapLongName(size_t id, const TString& name, bool isGlobal) 1.17 +{ 1.18 + ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE); 1.19 + TStringStream stream; 1.20 + 1.21 + uint64_t hash[2] = {0, 0}; 1.22 + MurmurHash3_x64_128(name.data(), name.length(), 0, hash); 1.23 + 1.24 + // We want to avoid producing a string with a double underscore, 1.25 + // which would be an illegal GLSL identifier. We can assume that the 1.26 + // original identifier doesn't have a double underscore, otherwise 1.27 + // it's illegal anyway. 1.28 + stream << (name[0] == '_' ? "webgl" : "webgl_") 1.29 + << name.substr(0, 9) 1.30 + << (name[8] == '_' ? "" : "_") 1.31 + << std::hex 1.32 + << hash[0]; 1.33 + ASSERT(stream.str().length() <= MAX_SHORTENED_IDENTIFIER_SIZE); 1.34 + ASSERT(stream.str().length() >= MAX_SHORTENED_IDENTIFIER_SIZE - 2); 1.35 + return stream.str(); 1.36 +} 1.37 + 1.38 +LongNameMap* gLongNameMapInstance = NULL; 1.39 + 1.40 +} // anonymous namespace 1.41 + 1.42 +LongNameMap::LongNameMap() 1.43 + : refCount(0) 1.44 +{ 1.45 +} 1.46 + 1.47 +LongNameMap::~LongNameMap() 1.48 +{ 1.49 +} 1.50 + 1.51 +// static 1.52 +LongNameMap* LongNameMap::GetInstance() 1.53 +{ 1.54 + if (gLongNameMapInstance == NULL) 1.55 + gLongNameMapInstance = new LongNameMap; 1.56 + gLongNameMapInstance->refCount++; 1.57 + return gLongNameMapInstance; 1.58 +} 1.59 + 1.60 +void LongNameMap::Release() 1.61 +{ 1.62 + ASSERT(gLongNameMapInstance == this); 1.63 + ASSERT(refCount > 0); 1.64 + refCount--; 1.65 + if (refCount == 0) { 1.66 + delete gLongNameMapInstance; 1.67 + gLongNameMapInstance = NULL; 1.68 + } 1.69 +} 1.70 + 1.71 +const char* LongNameMap::Find(const char* originalName) const 1.72 +{ 1.73 + std::map<std::string, std::string>::const_iterator it = mLongNameMap.find( 1.74 + originalName); 1.75 + if (it != mLongNameMap.end()) 1.76 + return (*it).second.c_str(); 1.77 + return NULL; 1.78 +} 1.79 + 1.80 +void LongNameMap::Insert(const char* originalName, const char* mappedName) 1.81 +{ 1.82 + mLongNameMap.insert(std::map<std::string, std::string>::value_type( 1.83 + originalName, mappedName)); 1.84 +} 1.85 + 1.86 +size_t LongNameMap::Size() const 1.87 +{ 1.88 + return mLongNameMap.size(); 1.89 +} 1.90 + 1.91 +MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap) 1.92 +{ 1.93 + ASSERT(globalMap); 1.94 + mGlobalMap = globalMap; 1.95 +} 1.96 + 1.97 +void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol) 1.98 +{ 1.99 + ASSERT(symbol != NULL); 1.100 + if (symbol->getSymbol().size() > MAX_SHORTENED_IDENTIFIER_SIZE) { 1.101 + switch (symbol->getQualifier()) { 1.102 + case EvqVaryingIn: 1.103 + case EvqVaryingOut: 1.104 + case EvqInvariantVaryingIn: 1.105 + case EvqInvariantVaryingOut: 1.106 + case EvqUniform: 1.107 + symbol->setSymbol( 1.108 + mapGlobalLongName(symbol->getSymbol())); 1.109 + break; 1.110 + default: 1.111 + symbol->setSymbol( 1.112 + mapLongName(symbol->getId(), symbol->getSymbol(), false)); 1.113 + break; 1.114 + }; 1.115 + } 1.116 +} 1.117 + 1.118 +bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node) 1.119 +{ 1.120 + if (node->getInit()) 1.121 + node->getInit()->traverse(this); 1.122 + return true; 1.123 +} 1.124 + 1.125 +TString MapLongVariableNames::mapGlobalLongName(const TString& name) 1.126 +{ 1.127 + ASSERT(mGlobalMap); 1.128 + const char* mappedName = mGlobalMap->Find(name.c_str()); 1.129 + if (mappedName != NULL) 1.130 + return mappedName; 1.131 + size_t id = mGlobalMap->Size(); 1.132 + TString rt = mapLongName(id, name, true); 1.133 + mGlobalMap->Insert(name.c_str(), rt.c_str()); 1.134 + return rt; 1.135 +}