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