|
1 // |
|
2 // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. |
|
3 // Use of this source code is governed by a BSD-style license that can be |
|
4 // found in the LICENSE file. |
|
5 // |
|
6 |
|
7 #include "compiler/MapLongVariableNames.h" |
|
8 |
|
9 #include "third_party/murmurhash/MurmurHash3.h" |
|
10 |
|
11 namespace { |
|
12 |
|
13 TString mapLongName(size_t id, const TString& name, bool isGlobal) |
|
14 { |
|
15 ASSERT(name.size() > MAX_SHORTENED_IDENTIFIER_SIZE); |
|
16 TStringStream stream; |
|
17 |
|
18 uint64_t hash[2] = {0, 0}; |
|
19 MurmurHash3_x64_128(name.data(), name.length(), 0, hash); |
|
20 |
|
21 // We want to avoid producing a string with a double underscore, |
|
22 // which would be an illegal GLSL identifier. We can assume that the |
|
23 // original identifier doesn't have a double underscore, otherwise |
|
24 // it's illegal anyway. |
|
25 stream << (name[0] == '_' ? "webgl" : "webgl_") |
|
26 << name.substr(0, 9) |
|
27 << (name[8] == '_' ? "" : "_") |
|
28 << std::hex |
|
29 << hash[0]; |
|
30 ASSERT(stream.str().length() <= MAX_SHORTENED_IDENTIFIER_SIZE); |
|
31 ASSERT(stream.str().length() >= MAX_SHORTENED_IDENTIFIER_SIZE - 2); |
|
32 return stream.str(); |
|
33 } |
|
34 |
|
35 LongNameMap* gLongNameMapInstance = NULL; |
|
36 |
|
37 } // anonymous namespace |
|
38 |
|
39 LongNameMap::LongNameMap() |
|
40 : refCount(0) |
|
41 { |
|
42 } |
|
43 |
|
44 LongNameMap::~LongNameMap() |
|
45 { |
|
46 } |
|
47 |
|
48 // static |
|
49 LongNameMap* LongNameMap::GetInstance() |
|
50 { |
|
51 if (gLongNameMapInstance == NULL) |
|
52 gLongNameMapInstance = new LongNameMap; |
|
53 gLongNameMapInstance->refCount++; |
|
54 return gLongNameMapInstance; |
|
55 } |
|
56 |
|
57 void LongNameMap::Release() |
|
58 { |
|
59 ASSERT(gLongNameMapInstance == this); |
|
60 ASSERT(refCount > 0); |
|
61 refCount--; |
|
62 if (refCount == 0) { |
|
63 delete gLongNameMapInstance; |
|
64 gLongNameMapInstance = NULL; |
|
65 } |
|
66 } |
|
67 |
|
68 const char* LongNameMap::Find(const char* originalName) const |
|
69 { |
|
70 std::map<std::string, std::string>::const_iterator it = mLongNameMap.find( |
|
71 originalName); |
|
72 if (it != mLongNameMap.end()) |
|
73 return (*it).second.c_str(); |
|
74 return NULL; |
|
75 } |
|
76 |
|
77 void LongNameMap::Insert(const char* originalName, const char* mappedName) |
|
78 { |
|
79 mLongNameMap.insert(std::map<std::string, std::string>::value_type( |
|
80 originalName, mappedName)); |
|
81 } |
|
82 |
|
83 size_t LongNameMap::Size() const |
|
84 { |
|
85 return mLongNameMap.size(); |
|
86 } |
|
87 |
|
88 MapLongVariableNames::MapLongVariableNames(LongNameMap* globalMap) |
|
89 { |
|
90 ASSERT(globalMap); |
|
91 mGlobalMap = globalMap; |
|
92 } |
|
93 |
|
94 void MapLongVariableNames::visitSymbol(TIntermSymbol* symbol) |
|
95 { |
|
96 ASSERT(symbol != NULL); |
|
97 if (symbol->getSymbol().size() > MAX_SHORTENED_IDENTIFIER_SIZE) { |
|
98 switch (symbol->getQualifier()) { |
|
99 case EvqVaryingIn: |
|
100 case EvqVaryingOut: |
|
101 case EvqInvariantVaryingIn: |
|
102 case EvqInvariantVaryingOut: |
|
103 case EvqUniform: |
|
104 symbol->setSymbol( |
|
105 mapGlobalLongName(symbol->getSymbol())); |
|
106 break; |
|
107 default: |
|
108 symbol->setSymbol( |
|
109 mapLongName(symbol->getId(), symbol->getSymbol(), false)); |
|
110 break; |
|
111 }; |
|
112 } |
|
113 } |
|
114 |
|
115 bool MapLongVariableNames::visitLoop(Visit, TIntermLoop* node) |
|
116 { |
|
117 if (node->getInit()) |
|
118 node->getInit()->traverse(this); |
|
119 return true; |
|
120 } |
|
121 |
|
122 TString MapLongVariableNames::mapGlobalLongName(const TString& name) |
|
123 { |
|
124 ASSERT(mGlobalMap); |
|
125 const char* mappedName = mGlobalMap->Find(name.c_str()); |
|
126 if (mappedName != NULL) |
|
127 return mappedName; |
|
128 size_t id = mGlobalMap->Size(); |
|
129 TString rt = mapLongName(id, name, true); |
|
130 mGlobalMap->Insert(name.c_str(), rt.c_str()); |
|
131 return rt; |
|
132 } |