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