Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2017 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/translator/HashNames.h" |
| 8 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 9 | #include "compiler/translator/IntermNode.h" |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 10 | #include "compiler/translator/Symbol.h" |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 11 | |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 12 | namespace sh |
| 13 | { |
| 14 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 15 | namespace |
| 16 | { |
| 17 | |
| 18 | // GLSL ES 3.00.6 section 3.9: the maximum length of an identifier is 1024 characters. |
| 19 | static const unsigned int kESSLMaxIdentifierLength = 1024u; |
| 20 | |
| 21 | static const char *kHashedNamePrefix = "webgl_"; |
| 22 | |
| 23 | // Can't prefix with just _ because then we might introduce a double underscore, which is not safe |
| 24 | // in GLSL (ESSL 3.00.6 section 3.8: All identifiers containing a double underscore are reserved for |
| 25 | // use by the underlying implementation). u is short for user-defined. |
| 26 | static const char *kUnhashedNamePrefix = "_u"; |
| 27 | static const unsigned int kUnhashedNamePrefixLength = 2u; |
| 28 | |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 29 | TString HashName(const TString &name, ShHashFunction64 hashFunction) |
| 30 | { |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 31 | ASSERT(!name.empty()); |
| 32 | ASSERT(hashFunction); |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 33 | khronos_uint64_t number = (*hashFunction)(name.c_str(), name.length()); |
| 34 | TStringStream stream; |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 35 | stream << kHashedNamePrefix << std::hex << number; |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 36 | TString hashedName = stream.str(); |
| 37 | return hashedName; |
| 38 | } |
| 39 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 40 | } // anonymous namespace |
| 41 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 42 | TString HashName(const TString &name, ShHashFunction64 hashFunction, NameMap *nameMap) |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 43 | { |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 44 | if (hashFunction == nullptr) |
| 45 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 46 | if (name.length() + kUnhashedNamePrefixLength > kESSLMaxIdentifierLength) |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 47 | { |
| 48 | // If the identifier length is already close to the limit, we can't prefix it. This is |
| 49 | // not a problem since there are no builtins or ANGLE's internal variables that would |
| 50 | // have as long names and could conflict. |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 51 | return name; |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 52 | } |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 53 | return kUnhashedNamePrefix + name; |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 54 | } |
| 55 | if (nameMap) |
| 56 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 57 | NameMap::const_iterator it = nameMap->find(name.c_str()); |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 58 | if (it != nameMap->end()) |
| 59 | return it->second.c_str(); |
| 60 | } |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 61 | TString hashedName = HashName(name, hashFunction); |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 62 | if (nameMap) |
| 63 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 64 | (*nameMap)[name.c_str()] = hashedName.c_str(); |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 65 | } |
| 66 | return hashedName; |
| 67 | } |
| 68 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 69 | TString HashName(const TSymbol *symbol, ShHashFunction64 hashFunction, NameMap *nameMap) |
| 70 | { |
| 71 | if (symbol->symbolType() == SymbolType::Empty) |
| 72 | { |
| 73 | return TString(); |
| 74 | } |
| 75 | if (symbol->symbolType() == SymbolType::AngleInternal || |
| 76 | symbol->symbolType() == SymbolType::BuiltIn) |
| 77 | { |
| 78 | return symbol->name(); |
| 79 | } |
| 80 | return HashName(symbol->name(), hashFunction, nameMap); |
| 81 | } |
| 82 | |
Olli Etuaho | cccf2b0 | 2017-07-05 14:50:54 +0300 | [diff] [blame] | 83 | } // namespace sh |