daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 6 | // Symbol table for parsing. The design principles and most of the functionality are documented in |
| 7 | // the header file. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 8 | // |
| 9 | |
apatrick@chromium.org | e057c5d | 2012-01-26 19:18:24 +0000 | [diff] [blame] | 10 | #if defined(_MSC_VER) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 11 | #pragma warning(disable : 4718) |
apatrick@chromium.org | e057c5d | 2012-01-26 19:18:24 +0000 | [diff] [blame] | 12 | #endif |
| 13 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 14 | #include "compiler/translator/SymbolTable.h" |
Olli Etuaho | 01d0ad0 | 2017-01-22 14:51:23 -0800 | [diff] [blame] | 15 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 16 | #include <algorithm> |
| 17 | #include <set> |
| 18 | |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 19 | #include "angle_gl.h" |
Olli Etuaho | 2d8e432 | 2018-01-22 14:12:46 +0200 | [diff] [blame] | 20 | #include "compiler/translator/ImmutableString.h" |
Olli Etuaho | 01d0ad0 | 2017-01-22 14:51:23 -0800 | [diff] [blame] | 21 | #include "compiler/translator/IntermNode.h" |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 22 | #include "compiler/translator/StaticType.h" |
kbr@chromium.org | 476541f | 2011-10-27 21:14:51 +0000 | [diff] [blame] | 23 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 24 | namespace sh |
| 25 | { |
| 26 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 27 | class TSymbolTable::TSymbolTableLevel |
| 28 | { |
| 29 | public: |
| 30 | TSymbolTableLevel() : mGlobalInvariant(false) {} |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 31 | |
| 32 | bool insert(TSymbol *symbol); |
| 33 | |
| 34 | // Insert a function using its unmangled name as the key. |
Olli Etuaho | b92f92a | 2018-02-15 19:14:59 +0200 | [diff] [blame] | 35 | void insertUnmangled(TFunction *function); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 36 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 37 | TSymbol *find(const ImmutableString &name) const; |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 38 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 39 | void addInvariantVarying(const ImmutableString &name) { mInvariantVaryings.insert(name); } |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 40 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 41 | bool isVaryingInvariant(const ImmutableString &name) |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 42 | { |
| 43 | return (mGlobalInvariant || mInvariantVaryings.count(name) > 0); |
| 44 | } |
| 45 | |
| 46 | void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; } |
| 47 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 48 | private: |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 49 | using tLevel = TUnorderedMap<ImmutableString, |
| 50 | TSymbol *, |
| 51 | ImmutableString::FowlerNollVoHash<sizeof(size_t)>>; |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 52 | using tLevelPair = const tLevel::value_type; |
| 53 | using tInsertResult = std::pair<tLevel::iterator, bool>; |
| 54 | |
| 55 | tLevel level; |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 56 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 57 | std::set<ImmutableString> mInvariantVaryings; |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 58 | bool mGlobalInvariant; |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | class TSymbolTable::TSymbolTableBuiltInLevel |
| 62 | { |
| 63 | public: |
| 64 | TSymbolTableBuiltInLevel() = default; |
| 65 | |
| 66 | bool insert(const TSymbol *symbol); |
| 67 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 68 | const TSymbol *find(const ImmutableString &name) const; |
| 69 | |
Olli Etuaho | 140152e | 2018-02-08 14:46:44 +0200 | [diff] [blame^] | 70 | void insertUnmangledBuiltIn(const ImmutableString &name, TExtension ext); |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 71 | const UnmangledBuiltIn *getUnmangledBuiltIn(const ImmutableString &name) const; |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | using tLevel = TUnorderedMap<ImmutableString, |
| 75 | const TSymbol *, |
| 76 | ImmutableString::FowlerNollVoHash<sizeof(size_t)>>; |
| 77 | using tLevelPair = const tLevel::value_type; |
| 78 | using tInsertResult = std::pair<tLevel::iterator, bool>; |
| 79 | |
| 80 | tLevel mLevel; |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 81 | |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 82 | std::map<ImmutableString, UnmangledBuiltIn> mUnmangledBuiltIns; |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 85 | bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol) |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 86 | { |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 87 | // returning true means symbol was added to the table |
Nicolas Capens | adfffe4 | 2014-06-17 02:13:36 -0400 | [diff] [blame] | 88 | tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol)); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 89 | return result.second; |
| 90 | } |
| 91 | |
Olli Etuaho | b92f92a | 2018-02-15 19:14:59 +0200 | [diff] [blame] | 92 | void TSymbolTable::TSymbolTableLevel::insertUnmangled(TFunction *function) |
Olli Etuaho | b2983c9 | 2015-03-18 14:02:46 +0200 | [diff] [blame] | 93 | { |
Olli Etuaho | b92f92a | 2018-02-15 19:14:59 +0200 | [diff] [blame] | 94 | level.insert(tLevelPair(function->name(), function)); |
Olli Etuaho | b2983c9 | 2015-03-18 14:02:46 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 97 | TSymbol *TSymbolTable::TSymbolTableLevel::find(const ImmutableString &name) const |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 98 | { |
| 99 | tLevel::const_iterator it = level.find(name); |
| 100 | if (it == level.end()) |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 101 | return nullptr; |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 102 | else |
| 103 | return (*it).second; |
| 104 | } |
| 105 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 106 | bool TSymbolTable::TSymbolTableBuiltInLevel::insert(const TSymbol *symbol) |
| 107 | { |
| 108 | // returning true means symbol was added to the table |
| 109 | tInsertResult result = mLevel.insert(tLevelPair(symbol->getMangledName(), symbol)); |
| 110 | return result.second; |
| 111 | } |
| 112 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 113 | const TSymbol *TSymbolTable::TSymbolTableBuiltInLevel::find(const ImmutableString &name) const |
| 114 | { |
| 115 | tLevel::const_iterator it = mLevel.find(name); |
| 116 | if (it == mLevel.end()) |
| 117 | return nullptr; |
| 118 | else |
| 119 | return (*it).second; |
| 120 | } |
| 121 | |
Olli Etuaho | 140152e | 2018-02-08 14:46:44 +0200 | [diff] [blame^] | 122 | void TSymbolTable::TSymbolTableBuiltInLevel::insertUnmangledBuiltIn(const ImmutableString &name, |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 123 | TExtension ext) |
Olli Etuaho | 342b83d | 2018-01-10 13:24:01 +0200 | [diff] [blame] | 124 | { |
Olli Etuaho | 140152e | 2018-02-08 14:46:44 +0200 | [diff] [blame^] | 125 | if (ext == TExtension::UNDEFINED || mUnmangledBuiltIns.find(name) == mUnmangledBuiltIns.end()) |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 126 | { |
Olli Etuaho | 140152e | 2018-02-08 14:46:44 +0200 | [diff] [blame^] | 127 | mUnmangledBuiltIns[name] = UnmangledBuiltIn(ext); |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 128 | } |
Olli Etuaho | 342b83d | 2018-01-10 13:24:01 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 131 | const UnmangledBuiltIn *TSymbolTable::TSymbolTableBuiltInLevel::getUnmangledBuiltIn( |
| 132 | const ImmutableString &name) const |
Olli Etuaho | 342b83d | 2018-01-10 13:24:01 +0200 | [diff] [blame] | 133 | { |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 134 | auto it = mUnmangledBuiltIns.find(ImmutableString(name)); |
| 135 | if (it == mUnmangledBuiltIns.end()) |
| 136 | { |
| 137 | return nullptr; |
| 138 | } |
| 139 | return &(it->second); |
Olli Etuaho | 342b83d | 2018-01-10 13:24:01 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 142 | TSymbolTable::TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(-1) |
| 143 | { |
| 144 | } |
| 145 | |
| 146 | TSymbolTable::~TSymbolTable() = default; |
| 147 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 148 | bool TSymbolTable::isEmpty() const |
| 149 | { |
| 150 | return mTable.empty(); |
| 151 | } |
| 152 | |
| 153 | bool TSymbolTable::atGlobalLevel() const |
| 154 | { |
| 155 | return mTable.size() == 1u; |
| 156 | } |
| 157 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 158 | void TSymbolTable::pushBuiltInLevel() |
| 159 | { |
| 160 | mBuiltInTable.push_back( |
| 161 | std::unique_ptr<TSymbolTableBuiltInLevel>(new TSymbolTableBuiltInLevel)); |
| 162 | } |
| 163 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 164 | void TSymbolTable::push() |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 165 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 166 | mTable.push_back(std::unique_ptr<TSymbolTableLevel>(new TSymbolTableLevel)); |
| 167 | mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel)); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 168 | } |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 169 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 170 | void TSymbolTable::pop() |
| 171 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 172 | mTable.pop_back(); |
| 173 | mPrecisionStack.pop_back(); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 176 | const TFunction *TSymbolTable::markFunctionHasPrototypeDeclaration( |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 177 | const ImmutableString &mangledName, |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 178 | bool *hadPrototypeDeclarationOut) |
| 179 | { |
| 180 | TFunction *function = findUserDefinedFunction(mangledName); |
| 181 | *hadPrototypeDeclarationOut = function->hasPrototypeDeclaration(); |
| 182 | function->setHasPrototypeDeclaration(); |
| 183 | return function; |
| 184 | } |
| 185 | |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 186 | const TFunction *TSymbolTable::setFunctionParameterNamesFromDefinition(const TFunction *function, |
| 187 | bool *wasDefinedOut) |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 188 | { |
| 189 | TFunction *firstDeclaration = findUserDefinedFunction(function->getMangledName()); |
| 190 | ASSERT(firstDeclaration); |
| 191 | // Note: 'firstDeclaration' could be 'function' if this is the first time we've seen function as |
| 192 | // it would have just been put in the symbol table. Otherwise, we're looking up an earlier |
| 193 | // occurance. |
| 194 | if (function != firstDeclaration) |
| 195 | { |
Olli Etuaho | 029e8ca | 2018-02-16 14:06:49 +0200 | [diff] [blame] | 196 | // The previous declaration should have the same parameters as the function definition |
| 197 | // (parameter names may differ). |
| 198 | firstDeclaration->shareParameters(*function); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | *wasDefinedOut = firstDeclaration->isDefined(); |
| 202 | firstDeclaration->setDefined(); |
| 203 | return firstDeclaration; |
| 204 | } |
| 205 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 206 | const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 207 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 208 | int userDefinedLevel = static_cast<int>(mTable.size()) - 1; |
| 209 | while (userDefinedLevel >= 0) |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 210 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 211 | const TSymbol *symbol = mTable[userDefinedLevel]->find(name); |
| 212 | if (symbol) |
| 213 | { |
| 214 | return symbol; |
| 215 | } |
| 216 | userDefinedLevel--; |
| 217 | } |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 218 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 219 | return findBuiltIn(name, shaderVersion, false); |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 222 | TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 223 | { |
| 224 | // User-defined functions are always declared at the global level. |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 225 | ASSERT(!mTable.empty()); |
| 226 | return static_cast<TFunction *>(mTable[0]->find(name)); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 229 | const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 230 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 231 | ASSERT(!mTable.empty()); |
| 232 | return mTable[0]->find(name); |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 235 | const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name, int shaderVersion) const |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 236 | { |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 237 | return findBuiltIn(name, shaderVersion, false); |
| 238 | } |
| 239 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 240 | const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name, |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 241 | int shaderVersion, |
| 242 | bool includeGLSLBuiltins) const |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 243 | { |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 244 | for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--) |
| 245 | { |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 246 | if (level == GLSL_BUILTINS && !includeGLSLBuiltins) |
| 247 | level--; |
Martin Radev | e93d24e | 2016-07-28 12:06:05 +0300 | [diff] [blame] | 248 | if (level == ESSL3_1_BUILTINS && shaderVersion != 310) |
| 249 | level--; |
| 250 | if (level == ESSL3_BUILTINS && shaderVersion < 300) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 251 | level--; |
| 252 | if (level == ESSL1_BUILTINS && shaderVersion != 100) |
| 253 | level--; |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 254 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 255 | const TSymbol *symbol = mBuiltInTable[level]->find(name); |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 256 | |
| 257 | if (symbol) |
| 258 | return symbol; |
| 259 | } |
| 260 | |
Olli Etuaho | 977ee7e | 2017-07-21 11:38:27 +0300 | [diff] [blame] | 261 | return nullptr; |
shannonwoods@chromium.org | 96e7ba1 | 2013-05-30 00:02:41 +0000 | [diff] [blame] | 262 | } |
Alok Priyadarshi | bc3f1ac | 2013-09-23 14:57:02 -0400 | [diff] [blame] | 263 | |
Nicolas Capens | f3cc4ae | 2015-02-23 13:51:25 -0500 | [diff] [blame] | 264 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 265 | bool TSymbolTable::declare(TSymbol *symbol) |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 266 | { |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 267 | ASSERT(!mTable.empty()); |
| 268 | ASSERT(symbol->symbolType() == SymbolType::UserDefined); |
| 269 | ASSERT(!symbol->isFunction()); |
| 270 | return mTable.back()->insert(symbol); |
Jiawei Shao | d8105a0 | 2017-08-08 09:54:36 +0800 | [diff] [blame] | 271 | } |
| 272 | |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 273 | void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName) |
| 274 | { |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 275 | ASSERT(!mTable.empty()); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 276 | if (insertUnmangledName) |
| 277 | { |
| 278 | // Insert the unmangled name to detect potential future redefinition as a variable. |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 279 | mTable[0]->insertUnmangled(function); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 280 | } |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 281 | mTable[0]->insert(function); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 284 | void TSymbolTable::insertVariable(ESymbolLevel level, |
| 285 | const ImmutableString &name, |
| 286 | const TType *type) |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 287 | { |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 288 | ASSERT(type->isRealized()); |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 289 | TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn); |
| 290 | insertBuiltIn(level, var); |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 291 | } |
| 292 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 293 | void TSymbolTable::insertVariableExt(ESymbolLevel level, |
| 294 | TExtension ext, |
| 295 | const ImmutableString &name, |
| 296 | const TType *type) |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 297 | { |
Olli Etuaho | b60d30f | 2018-01-16 12:31:06 +0200 | [diff] [blame] | 298 | ASSERT(type->isRealized()); |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 299 | TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn, ext); |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 300 | insertBuiltIn(level, var); |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 301 | } |
| 302 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 303 | void TSymbolTable::insertBuiltIn(ESymbolLevel level, const TSymbol *symbol) |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 304 | { |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 305 | ASSERT(symbol); |
| 306 | ASSERT(level <= LAST_BUILTIN_LEVEL); |
Olli Etuaho | 195be94 | 2017-12-04 23:40:14 +0200 | [diff] [blame] | 307 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 308 | mBuiltInTable[level]->insert(symbol); |
Olli Etuaho | 0f68463 | 2017-07-13 12:42:15 +0300 | [diff] [blame] | 309 | } |
| 310 | |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 311 | template <TPrecision precision> |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 312 | void TSymbolTable::insertConstInt(ESymbolLevel level, const ImmutableString &name, int value) |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 313 | { |
| 314 | TVariable *constant = new TVariable( |
| 315 | this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn); |
| 316 | TConstantUnion *unionArray = new TConstantUnion[1]; |
| 317 | unionArray[0].setIConst(value); |
| 318 | constant->shareConstPointer(unionArray); |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 319 | insertBuiltIn(level, constant); |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | template <TPrecision precision> |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 323 | void TSymbolTable::insertConstIntExt(ESymbolLevel level, |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 324 | TExtension ext, |
| 325 | const ImmutableString &name, |
| 326 | int value) |
| 327 | { |
| 328 | TVariable *constant = new TVariable( |
| 329 | this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn, ext); |
| 330 | TConstantUnion *unionArray = new TConstantUnion[1]; |
| 331 | unionArray[0].setIConst(value); |
| 332 | constant->shareConstPointer(unionArray); |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 333 | insertBuiltIn(level, constant); |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | template <TPrecision precision> |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 337 | void TSymbolTable::insertConstIvec3(ESymbolLevel level, |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 338 | const ImmutableString &name, |
| 339 | const std::array<int, 3> &values) |
| 340 | { |
| 341 | TVariable *constantIvec3 = new TVariable( |
| 342 | this, name, StaticType::Get<EbtInt, precision, EvqConst, 3, 1>(), SymbolType::BuiltIn); |
| 343 | |
| 344 | TConstantUnion *unionArray = new TConstantUnion[3]; |
| 345 | for (size_t index = 0u; index < 3u; ++index) |
| 346 | { |
| 347 | unionArray[index].setIConst(values[index]); |
| 348 | } |
| 349 | constantIvec3->shareConstPointer(unionArray); |
| 350 | |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 351 | insertBuiltIn(level, constantIvec3); |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 354 | void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec) |
| 355 | { |
| 356 | int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1; |
| 357 | // Uses map operator [], overwrites the current value |
| 358 | (*mPrecisionStack[indexOfLastElement])[type] = prec; |
| 359 | } |
| 360 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 361 | TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 362 | { |
| 363 | if (!SupportsPrecision(type)) |
| 364 | return EbpUndefined; |
| 365 | |
| 366 | // unsigned integers use the same precision as signed |
| 367 | TBasicType baseType = (type == EbtUInt) ? EbtInt : type; |
| 368 | |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 369 | int level = static_cast<int>(mPrecisionStack.size()) - 1; |
| 370 | ASSERT(level >= 0); // Just to be safe. Should not happen. |
Olli Etuaho | 183d7e2 | 2015-11-20 15:59:09 +0200 | [diff] [blame] | 371 | // If we dont find anything we return this. Some types don't have predefined default precision. |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 372 | TPrecision prec = EbpUndefined; |
| 373 | while (level >= 0) |
| 374 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 375 | PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType); |
| 376 | if (it != mPrecisionStack[level]->end()) |
Zhenyao Mo | 9eedea0 | 2014-05-12 16:02:35 -0700 | [diff] [blame] | 377 | { |
| 378 | prec = (*it).second; |
| 379 | break; |
| 380 | } |
| 381 | level--; |
| 382 | } |
| 383 | return prec; |
| 384 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 385 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 386 | void TSymbolTable::addInvariantVarying(const ImmutableString &originalName) |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 387 | { |
| 388 | ASSERT(atGlobalLevel()); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 389 | mTable.back()->addInvariantVarying(originalName); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Olli Etuaho | defe393 | 2018-02-13 11:56:09 +0200 | [diff] [blame] | 392 | bool TSymbolTable::isVaryingInvariant(const ImmutableString &originalName) const |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 393 | { |
| 394 | ASSERT(atGlobalLevel()); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 395 | return mTable.back()->isVaryingInvariant(originalName); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void TSymbolTable::setGlobalInvariant(bool invariant) |
| 399 | { |
| 400 | ASSERT(atGlobalLevel()); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 401 | mTable.back()->setGlobalInvariant(invariant); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Olli Etuaho | 140152e | 2018-02-08 14:46:44 +0200 | [diff] [blame^] | 404 | void TSymbolTable::insertUnmangledBuiltIn(const ImmutableString &name, |
| 405 | TExtension ext, |
| 406 | ESymbolLevel level) |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 407 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 408 | ASSERT(level >= 0 && level <= LAST_BUILTIN_LEVEL); |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 409 | ASSERT(mUserDefinedUniqueIdsStart == -1); |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 410 | mBuiltInTable[level]->insertUnmangledBuiltIn(name, ext); |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | bool TSymbolTable::hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level) |
| 414 | { |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 415 | ASSERT(level >= 0 && level <= LAST_BUILTIN_LEVEL); |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 416 | return mBuiltInTable[level]->getUnmangledBuiltIn(ImmutableString(name)) != nullptr; |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 419 | const UnmangledBuiltIn *TSymbolTable::getUnmangledBuiltInForShaderVersion( |
| 420 | const ImmutableString &name, |
| 421 | int shaderVersion) |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 422 | { |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 423 | for (int level = LAST_BUILTIN_LEVEL; level >= 0; --level) |
| 424 | { |
| 425 | if (level == ESSL3_1_BUILTINS && shaderVersion != 310) |
| 426 | { |
| 427 | --level; |
| 428 | } |
| 429 | if (level == ESSL3_BUILTINS && shaderVersion < 300) |
| 430 | { |
| 431 | --level; |
| 432 | } |
| 433 | if (level == ESSL1_BUILTINS && shaderVersion != 100) |
| 434 | { |
| 435 | --level; |
| 436 | } |
| 437 | |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 438 | const UnmangledBuiltIn *builtIn = mBuiltInTable[level]->getUnmangledBuiltIn(name); |
| 439 | if (builtIn != nullptr) |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 440 | { |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 441 | return builtIn; |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 442 | } |
| 443 | } |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 444 | return nullptr; |
Martin Radev | da6254b | 2016-12-14 17:00:36 +0200 | [diff] [blame] | 445 | } |
| 446 | |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 447 | void TSymbolTable::markBuiltInInitializationFinished() |
| 448 | { |
| 449 | mUserDefinedUniqueIdsStart = mUniqueIdCounter; |
| 450 | } |
| 451 | |
| 452 | void TSymbolTable::clearCompilationResults() |
| 453 | { |
| 454 | mUniqueIdCounter = mUserDefinedUniqueIdsStart; |
| 455 | |
| 456 | // User-defined scopes should have already been cleared when the compilation finished. |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 457 | ASSERT(mTable.size() == 0u); |
Olli Etuaho | 5d69db1 | 2017-11-24 16:51:15 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | int TSymbolTable::nextUniqueIdValue() |
| 461 | { |
| 462 | ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max()); |
| 463 | return ++mUniqueIdCounter; |
| 464 | } |
| 465 | |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 466 | void TSymbolTable::initializeBuiltIns(sh::GLenum type, |
| 467 | ShShaderSpec spec, |
| 468 | const ShBuiltInResources &resources) |
| 469 | { |
| 470 | ASSERT(isEmpty()); |
Olli Etuaho | e5fe7aa | 2018-01-29 12:06:11 +0200 | [diff] [blame] | 471 | pushBuiltInLevel(); // COMMON_BUILTINS |
| 472 | pushBuiltInLevel(); // ESSL1_BUILTINS |
| 473 | pushBuiltInLevel(); // ESSL3_BUILTINS |
| 474 | pushBuiltInLevel(); // ESSL3_1_BUILTINS |
| 475 | pushBuiltInLevel(); // GLSL_BUILTINS |
| 476 | |
| 477 | // We need just one precision stack level for predefined precisions. |
| 478 | mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel)); |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 479 | |
| 480 | switch (type) |
| 481 | { |
| 482 | case GL_FRAGMENT_SHADER: |
| 483 | setDefaultPrecision(EbtInt, EbpMedium); |
| 484 | break; |
| 485 | case GL_VERTEX_SHADER: |
| 486 | case GL_COMPUTE_SHADER: |
| 487 | case GL_GEOMETRY_SHADER_EXT: |
| 488 | setDefaultPrecision(EbtInt, EbpHigh); |
| 489 | setDefaultPrecision(EbtFloat, EbpHigh); |
| 490 | break; |
| 491 | default: |
| 492 | UNREACHABLE(); |
| 493 | } |
| 494 | // Set defaults for sampler types that have default precision, even those that are |
| 495 | // only available if an extension exists. |
| 496 | // New sampler types in ESSL3 don't have default precision. ESSL1 types do. |
| 497 | initSamplerDefaultPrecision(EbtSampler2D); |
| 498 | initSamplerDefaultPrecision(EbtSamplerCube); |
| 499 | // SamplerExternalOES is specified in the extension to have default precision. |
| 500 | initSamplerDefaultPrecision(EbtSamplerExternalOES); |
| 501 | // SamplerExternal2DY2YEXT is specified in the extension to have default precision. |
| 502 | initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT); |
| 503 | // It isn't specified whether Sampler2DRect has default precision. |
| 504 | initSamplerDefaultPrecision(EbtSampler2DRect); |
| 505 | |
| 506 | setDefaultPrecision(EbtAtomicCounter, EbpHigh); |
| 507 | |
Olli Etuaho | 140152e | 2018-02-08 14:46:44 +0200 | [diff] [blame^] | 508 | insertBuiltInFunctions(type); |
| 509 | insertBuiltInFunctionUnmangledNames(type); |
| 510 | mUniqueIdCounter = kLastStaticBuiltInId + 1; |
| 511 | |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 512 | initializeBuiltInVariables(type, spec, resources); |
| 513 | markBuiltInInitializationFinished(); |
| 514 | } |
| 515 | |
| 516 | void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType) |
| 517 | { |
| 518 | ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd); |
| 519 | setDefaultPrecision(samplerType, EbpLow); |
| 520 | } |
| 521 | |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 522 | |
| 523 | void TSymbolTable::initializeBuiltInVariables(sh::GLenum type, |
| 524 | ShShaderSpec spec, |
| 525 | const ShBuiltInResources &resources) |
| 526 | { |
| 527 | const TSourceLoc zeroSourceLoc = {0, 0, 0, 0}; |
| 528 | |
| 529 | // |
| 530 | // Depth range in window coordinates |
| 531 | // |
| 532 | TFieldList *fields = new TFieldList(); |
| 533 | auto highpFloat1 = new TType(EbtFloat, EbpHigh, EvqGlobal, 1); |
| 534 | TField *near = new TField(highpFloat1, ImmutableString("near"), zeroSourceLoc); |
| 535 | TField *far = new TField(highpFloat1, ImmutableString("far"), zeroSourceLoc); |
| 536 | TField *diff = new TField(highpFloat1, ImmutableString("diff"), zeroSourceLoc); |
| 537 | fields->push_back(near); |
| 538 | fields->push_back(far); |
| 539 | fields->push_back(diff); |
| 540 | TStructure *depthRangeStruct = new TStructure(this, ImmutableString("gl_DepthRangeParameters"), |
| 541 | fields, SymbolType::BuiltIn); |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 542 | insertBuiltIn(COMMON_BUILTINS, depthRangeStruct); |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 543 | TType *depthRangeType = new TType(depthRangeStruct); |
| 544 | depthRangeType->setQualifier(EvqUniform); |
| 545 | depthRangeType->realize(); |
| 546 | insertVariable(COMMON_BUILTINS, ImmutableString("gl_DepthRange"), depthRangeType); |
| 547 | |
| 548 | // |
| 549 | // Implementation dependent built-in constants. |
| 550 | // |
| 551 | insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexAttribs"), |
| 552 | resources.MaxVertexAttribs); |
| 553 | insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexUniformVectors"), |
| 554 | resources.MaxVertexUniformVectors); |
| 555 | insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexTextureImageUnits"), |
| 556 | resources.MaxVertexTextureImageUnits); |
| 557 | insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxCombinedTextureImageUnits"), |
| 558 | resources.MaxCombinedTextureImageUnits); |
| 559 | insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxTextureImageUnits"), |
| 560 | resources.MaxTextureImageUnits); |
| 561 | insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxFragmentUniformVectors"), |
| 562 | resources.MaxFragmentUniformVectors); |
| 563 | |
| 564 | insertConstInt<EbpMedium>(ESSL1_BUILTINS, ImmutableString("gl_MaxVaryingVectors"), |
| 565 | resources.MaxVaryingVectors); |
| 566 | |
| 567 | insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxDrawBuffers"), |
| 568 | resources.MaxDrawBuffers); |
Olli Etuaho | 7c8567a | 2018-02-20 15:44:07 +0200 | [diff] [blame] | 569 | insertConstIntExt<EbpMedium>(COMMON_BUILTINS, TExtension::EXT_blend_func_extended, |
| 570 | ImmutableString("gl_MaxDualSourceDrawBuffersEXT"), |
| 571 | resources.MaxDualSourceDrawBuffers); |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 572 | |
| 573 | insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxVertexOutputVectors"), |
| 574 | resources.MaxVertexOutputVectors); |
| 575 | insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxFragmentInputVectors"), |
| 576 | resources.MaxFragmentInputVectors); |
| 577 | insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MinProgramTexelOffset"), |
| 578 | resources.MinProgramTexelOffset); |
| 579 | insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxProgramTexelOffset"), |
| 580 | resources.MaxProgramTexelOffset); |
| 581 | |
| 582 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxImageUnits"), |
| 583 | resources.MaxImageUnits); |
| 584 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexImageUniforms"), |
| 585 | resources.MaxVertexImageUniforms); |
| 586 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxFragmentImageUniforms"), |
| 587 | resources.MaxFragmentImageUniforms); |
| 588 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeImageUniforms"), |
| 589 | resources.MaxComputeImageUniforms); |
| 590 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxCombinedImageUniforms"), |
| 591 | resources.MaxCombinedImageUniforms); |
| 592 | |
| 593 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, |
| 594 | ImmutableString("gl_MaxCombinedShaderOutputResources"), |
| 595 | resources.MaxCombinedShaderOutputResources); |
| 596 | |
| 597 | insertConstIvec3<EbpHigh>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeWorkGroupCount"), |
| 598 | resources.MaxComputeWorkGroupCount); |
| 599 | insertConstIvec3<EbpHigh>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeWorkGroupSize"), |
| 600 | resources.MaxComputeWorkGroupSize); |
| 601 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeUniformComponents"), |
| 602 | resources.MaxComputeUniformComponents); |
| 603 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeTextureImageUnits"), |
| 604 | resources.MaxComputeTextureImageUnits); |
| 605 | |
| 606 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeAtomicCounters"), |
| 607 | resources.MaxComputeAtomicCounters); |
| 608 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, |
| 609 | ImmutableString("gl_MaxComputeAtomicCounterBuffers"), |
| 610 | resources.MaxComputeAtomicCounterBuffers); |
| 611 | |
| 612 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexAtomicCounters"), |
| 613 | resources.MaxVertexAtomicCounters); |
| 614 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxFragmentAtomicCounters"), |
| 615 | resources.MaxFragmentAtomicCounters); |
| 616 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxCombinedAtomicCounters"), |
| 617 | resources.MaxCombinedAtomicCounters); |
| 618 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxAtomicCounterBindings"), |
| 619 | resources.MaxAtomicCounterBindings); |
| 620 | |
| 621 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexAtomicCounterBuffers"), |
| 622 | resources.MaxVertexAtomicCounterBuffers); |
| 623 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, |
| 624 | ImmutableString("gl_MaxFragmentAtomicCounterBuffers"), |
| 625 | resources.MaxFragmentAtomicCounterBuffers); |
| 626 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, |
| 627 | ImmutableString("gl_MaxCombinedAtomicCounterBuffers"), |
| 628 | resources.MaxCombinedAtomicCounterBuffers); |
| 629 | insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxAtomicCounterBufferSize"), |
| 630 | resources.MaxAtomicCounterBufferSize); |
| 631 | |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 632 | { |
| 633 | TExtension ext = TExtension::EXT_geometry_shader; |
| 634 | insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext, |
| 635 | ImmutableString("gl_MaxGeometryInputComponents"), |
| 636 | resources.MaxGeometryInputComponents); |
| 637 | insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext, |
| 638 | ImmutableString("gl_MaxGeometryOutputComponents"), |
| 639 | resources.MaxGeometryOutputComponents); |
| 640 | insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext, |
| 641 | ImmutableString("gl_MaxGeometryImageUniforms"), |
| 642 | resources.MaxGeometryImageUniforms); |
| 643 | insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext, |
| 644 | ImmutableString("gl_MaxGeometryTextureImageUnits"), |
| 645 | resources.MaxGeometryTextureImageUnits); |
| 646 | insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext, |
| 647 | ImmutableString("gl_MaxGeometryOutputVertices"), |
| 648 | resources.MaxGeometryOutputVertices); |
| 649 | insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext, |
| 650 | ImmutableString("gl_MaxGeometryTotalOutputComponents"), |
| 651 | resources.MaxGeometryTotalOutputComponents); |
| 652 | insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext, |
| 653 | ImmutableString("gl_MaxGeometryUniformComponents"), |
| 654 | resources.MaxGeometryUniformComponents); |
| 655 | insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext, |
| 656 | ImmutableString("gl_MaxGeometryAtomicCounters"), |
| 657 | resources.MaxGeometryAtomicCounters); |
| 658 | insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext, |
| 659 | ImmutableString("gl_MaxGeometryAtomicCounterBuffers"), |
| 660 | resources.MaxGeometryAtomicCounterBuffers); |
| 661 | } |
| 662 | |
| 663 | // |
| 664 | // Insert some special built-in variables that are not in |
| 665 | // the built-in header files. |
| 666 | // |
| 667 | |
| 668 | if (resources.OVR_multiview && type != GL_COMPUTE_SHADER) |
| 669 | { |
| 670 | const TType *viewIDType = StaticType::Get<EbtUInt, EbpHigh, EvqViewIDOVR, 1, 1>(); |
| 671 | insertVariableExt(ESSL3_BUILTINS, TExtension::OVR_multiview, |
| 672 | ImmutableString("gl_ViewID_OVR"), viewIDType); |
| 673 | |
| 674 | // ESSL 1.00 doesn't have unsigned integers, so gl_ViewID_OVR is a signed integer in ESSL |
| 675 | // 1.00. This is specified in the WEBGL_multiview spec. |
| 676 | const TType *viewIDIntType = StaticType::Get<EbtInt, EbpHigh, EvqViewIDOVR, 1, 1>(); |
| 677 | insertVariableExt(ESSL1_BUILTINS, TExtension::OVR_multiview, |
| 678 | ImmutableString("gl_ViewID_OVR"), viewIDIntType); |
| 679 | } |
| 680 | |
| 681 | const TType *positionType = StaticType::Get<EbtFloat, EbpHigh, EvqPosition, 4, 1>(); |
| 682 | const TType *primitiveIDType = StaticType::Get<EbtInt, EbpHigh, EvqPrimitiveID, 1, 1>(); |
| 683 | const TType *layerType = StaticType::Get<EbtInt, EbpHigh, EvqLayer, 1, 1>(); |
| 684 | |
| 685 | switch (type) |
| 686 | { |
| 687 | case GL_FRAGMENT_SHADER: |
| 688 | { |
| 689 | const TType *fragCoordType = StaticType::Get<EbtFloat, EbpMedium, EvqFragCoord, 4, 1>(); |
| 690 | insertVariable(COMMON_BUILTINS, ImmutableString("gl_FragCoord"), fragCoordType); |
| 691 | const TType *frontFacingType = StaticType::GetQualified<EbtBool, EvqFrontFacing>(); |
| 692 | insertVariable(COMMON_BUILTINS, ImmutableString("gl_FrontFacing"), frontFacingType); |
| 693 | const TType *pointCoordType = |
| 694 | StaticType::Get<EbtFloat, EbpMedium, EvqPointCoord, 2, 1>(); |
| 695 | insertVariable(COMMON_BUILTINS, ImmutableString("gl_PointCoord"), pointCoordType); |
| 696 | |
| 697 | const TType *fragColorType = StaticType::Get<EbtFloat, EbpMedium, EvqFragColor, 4, 1>(); |
| 698 | insertVariable(ESSL1_BUILTINS, ImmutableString("gl_FragColor"), fragColorType); |
| 699 | |
| 700 | TType *fragDataType = new TType(EbtFloat, EbpMedium, EvqFragData, 4); |
| 701 | if (spec != SH_WEBGL2_SPEC && spec != SH_WEBGL3_SPEC) |
| 702 | { |
| 703 | fragDataType->makeArray(resources.MaxDrawBuffers); |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | fragDataType->makeArray(1u); |
| 708 | } |
| 709 | fragDataType->realize(); |
| 710 | insertVariable(ESSL1_BUILTINS, ImmutableString("gl_FragData"), fragDataType); |
| 711 | |
| 712 | if (resources.EXT_blend_func_extended) |
| 713 | { |
| 714 | const TType *secondaryFragColorType = |
| 715 | StaticType::Get<EbtFloat, EbpMedium, EvqSecondaryFragColorEXT, 4, 1>(); |
| 716 | insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_blend_func_extended, |
| 717 | ImmutableString("gl_SecondaryFragColorEXT"), |
| 718 | secondaryFragColorType); |
| 719 | TType *secondaryFragDataType = |
| 720 | new TType(EbtFloat, EbpMedium, EvqSecondaryFragDataEXT, 4, 1); |
| 721 | secondaryFragDataType->makeArray(resources.MaxDualSourceDrawBuffers); |
| 722 | secondaryFragDataType->realize(); |
| 723 | insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_blend_func_extended, |
| 724 | ImmutableString("gl_SecondaryFragDataEXT"), |
| 725 | secondaryFragDataType); |
| 726 | } |
| 727 | |
| 728 | if (resources.EXT_frag_depth) |
| 729 | { |
| 730 | TType *fragDepthEXTType = |
| 731 | new TType(EbtFloat, resources.FragmentPrecisionHigh ? EbpHigh : EbpMedium, |
| 732 | EvqFragDepthEXT, 1); |
| 733 | fragDepthEXTType->realize(); |
| 734 | insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_frag_depth, |
| 735 | ImmutableString("gl_FragDepthEXT"), fragDepthEXTType); |
| 736 | } |
| 737 | |
| 738 | const TType *fragDepthType = StaticType::Get<EbtFloat, EbpHigh, EvqFragDepth, 1, 1>(); |
| 739 | insertVariable(ESSL3_BUILTINS, ImmutableString("gl_FragDepth"), fragDepthType); |
| 740 | |
| 741 | const TType *lastFragColorType = |
| 742 | StaticType::Get<EbtFloat, EbpMedium, EvqLastFragColor, 4, 1>(); |
| 743 | |
| 744 | if (resources.EXT_shader_framebuffer_fetch || resources.NV_shader_framebuffer_fetch) |
| 745 | { |
| 746 | TType *lastFragDataType = new TType(EbtFloat, EbpMedium, EvqLastFragData, 4, 1); |
| 747 | lastFragDataType->makeArray(resources.MaxDrawBuffers); |
| 748 | lastFragDataType->realize(); |
| 749 | |
| 750 | if (resources.EXT_shader_framebuffer_fetch) |
| 751 | { |
| 752 | insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_shader_framebuffer_fetch, |
| 753 | ImmutableString("gl_LastFragData"), lastFragDataType); |
| 754 | } |
| 755 | else if (resources.NV_shader_framebuffer_fetch) |
| 756 | { |
| 757 | insertVariableExt(ESSL1_BUILTINS, TExtension::NV_shader_framebuffer_fetch, |
| 758 | ImmutableString("gl_LastFragColor"), lastFragColorType); |
| 759 | insertVariableExt(ESSL1_BUILTINS, TExtension::NV_shader_framebuffer_fetch, |
| 760 | ImmutableString("gl_LastFragData"), lastFragDataType); |
| 761 | } |
| 762 | } |
| 763 | else if (resources.ARM_shader_framebuffer_fetch) |
| 764 | { |
| 765 | insertVariableExt(ESSL1_BUILTINS, TExtension::ARM_shader_framebuffer_fetch, |
| 766 | ImmutableString("gl_LastFragColorARM"), lastFragColorType); |
| 767 | } |
| 768 | |
| 769 | if (resources.EXT_geometry_shader) |
| 770 | { |
| 771 | TExtension extension = TExtension::EXT_geometry_shader; |
| 772 | insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveID"), |
| 773 | primitiveIDType); |
| 774 | insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Layer"), |
| 775 | layerType); |
| 776 | } |
| 777 | |
| 778 | break; |
| 779 | } |
| 780 | case GL_VERTEX_SHADER: |
| 781 | { |
| 782 | insertVariable(COMMON_BUILTINS, ImmutableString("gl_Position"), positionType); |
| 783 | const TType *pointSizeType = StaticType::Get<EbtFloat, EbpMedium, EvqPointSize, 1, 1>(); |
| 784 | insertVariable(COMMON_BUILTINS, ImmutableString("gl_PointSize"), pointSizeType); |
| 785 | const TType *instanceIDType = StaticType::Get<EbtInt, EbpHigh, EvqInstanceID, 1, 1>(); |
| 786 | insertVariable(ESSL3_BUILTINS, ImmutableString("gl_InstanceID"), instanceIDType); |
| 787 | const TType *vertexIDType = StaticType::Get<EbtInt, EbpHigh, EvqVertexID, 1, 1>(); |
| 788 | insertVariable(ESSL3_BUILTINS, ImmutableString("gl_VertexID"), vertexIDType); |
| 789 | |
| 790 | // For internal use by ANGLE - not exposed to the parser. |
| 791 | const TType *viewportIndexType = |
| 792 | StaticType::Get<EbtInt, EbpHigh, EvqViewportIndex, 1, 1>(); |
| 793 | insertVariable(GLSL_BUILTINS, ImmutableString("gl_ViewportIndex"), viewportIndexType); |
| 794 | // gl_Layer exists in other shader stages in ESSL, but not in vertex shader so far. |
| 795 | insertVariable(GLSL_BUILTINS, ImmutableString("gl_Layer"), layerType); |
| 796 | break; |
| 797 | } |
| 798 | case GL_COMPUTE_SHADER: |
| 799 | { |
| 800 | const TType *numWorkGroupsType = |
| 801 | StaticType::Get<EbtUInt, EbpUndefined, EvqNumWorkGroups, 3, 1>(); |
| 802 | insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_NumWorkGroups"), |
| 803 | numWorkGroupsType); |
| 804 | const TType *workGroupSizeType = |
| 805 | StaticType::Get<EbtUInt, EbpUndefined, EvqWorkGroupSize, 3, 1>(); |
| 806 | insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_WorkGroupSize"), |
| 807 | workGroupSizeType); |
| 808 | const TType *workGroupIDType = |
| 809 | StaticType::Get<EbtUInt, EbpUndefined, EvqWorkGroupID, 3, 1>(); |
| 810 | insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_WorkGroupID"), workGroupIDType); |
| 811 | const TType *localInvocationIDType = |
| 812 | StaticType::Get<EbtUInt, EbpUndefined, EvqLocalInvocationID, 3, 1>(); |
| 813 | insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_LocalInvocationID"), |
| 814 | localInvocationIDType); |
| 815 | const TType *globalInvocationIDType = |
| 816 | StaticType::Get<EbtUInt, EbpUndefined, EvqGlobalInvocationID, 3, 1>(); |
| 817 | insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_GlobalInvocationID"), |
| 818 | globalInvocationIDType); |
| 819 | const TType *localInvocationIndexType = |
| 820 | StaticType::Get<EbtUInt, EbpUndefined, EvqLocalInvocationIndex, 1, 1>(); |
| 821 | insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_LocalInvocationIndex"), |
| 822 | localInvocationIndexType); |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | case GL_GEOMETRY_SHADER_EXT: |
| 827 | { |
| 828 | TExtension extension = TExtension::EXT_geometry_shader; |
| 829 | |
| 830 | // Add built-in interface block gl_PerVertex and the built-in array gl_in. |
| 831 | // TODO(jiawei.shao@intel.com): implement GL_EXT_geometry_point_size. |
| 832 | TFieldList *glPerVertexFieldList = new TFieldList(); |
| 833 | TField *glPositionField = |
| 834 | new TField(new TType(*positionType), ImmutableString("gl_Position"), zeroSourceLoc); |
| 835 | glPerVertexFieldList->push_back(glPositionField); |
| 836 | |
| 837 | const ImmutableString glPerVertexString("gl_PerVertex"); |
| 838 | TInterfaceBlock *glPerVertexInBlock = |
| 839 | new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList, |
| 840 | TLayoutQualifier::Create(), SymbolType::BuiltIn, extension); |
Olli Etuaho | 437664b | 2018-02-28 15:38:14 +0200 | [diff] [blame] | 841 | insertBuiltIn(ESSL3_1_BUILTINS, glPerVertexInBlock); |
Olli Etuaho | 29bda81 | 2018-01-26 17:37:36 +0200 | [diff] [blame] | 842 | |
| 843 | // The array size of gl_in is undefined until we get a valid input primitive |
| 844 | // declaration. |
| 845 | TType *glInType = |
| 846 | new TType(glPerVertexInBlock, EvqPerVertexIn, TLayoutQualifier::Create()); |
| 847 | glInType->makeArray(0u); |
| 848 | glInType->realize(); |
| 849 | insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_in"), glInType); |
| 850 | |
| 851 | TInterfaceBlock *glPerVertexOutBlock = |
| 852 | new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList, |
| 853 | TLayoutQualifier::Create(), SymbolType::BuiltIn); |
| 854 | TType *glPositionInBlockType = new TType(EbtFloat, EbpHigh, EvqPosition, 4); |
| 855 | glPositionInBlockType->setInterfaceBlock(glPerVertexOutBlock); |
| 856 | glPositionInBlockType->realize(); |
| 857 | insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Position"), |
| 858 | glPositionInBlockType); |
| 859 | |
| 860 | const TType *primitiveIDInType = |
| 861 | StaticType::Get<EbtInt, EbpHigh, EvqPrimitiveIDIn, 1, 1>(); |
| 862 | insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveIDIn"), |
| 863 | primitiveIDInType); |
| 864 | const TType *invocationIDType = |
| 865 | StaticType::Get<EbtInt, EbpHigh, EvqInvocationID, 1, 1>(); |
| 866 | insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_InvocationID"), |
| 867 | invocationIDType); |
| 868 | insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveID"), |
| 869 | primitiveIDType); |
| 870 | insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Layer"), layerType); |
| 871 | |
| 872 | break; |
| 873 | } |
| 874 | default: |
| 875 | UNREACHABLE(); |
| 876 | } |
| 877 | } |
| 878 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 879 | } // namespace sh |