blob: aecc95b6b981ccfeade983442f7fb8a52073dd06 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
Olli Etuaho0f684632017-07-13 12:42:15 +03006// Symbol table for parsing. The design principles and most of the functionality are documented in
7// the header file.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008//
9
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000010#if defined(_MSC_VER)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050011#pragma warning(disable : 4718)
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000012#endif
13
Geoff Lang17732822013-08-29 13:46:49 -040014#include "compiler/translator/SymbolTable.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080015
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020016#include <algorithm>
17#include <set>
18
Olli Etuaho29bda812018-01-26 17:37:36 +020019#include "angle_gl.h"
Olli Etuaho2d8e4322018-01-22 14:12:46 +020020#include "compiler/translator/ImmutableString.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080021#include "compiler/translator/IntermNode.h"
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020022#include "compiler/translator/StaticType.h"
kbr@chromium.org476541f2011-10-27 21:14:51 +000023
Jamie Madill45bcc782016-11-07 13:58:48 -050024namespace sh
25{
26
Olli Etuahodd21ecf2018-01-10 12:42:09 +020027class TSymbolTable::TSymbolTableLevel
28{
29 public:
30 TSymbolTableLevel() : mGlobalInvariant(false) {}
Olli Etuahodd21ecf2018-01-10 12:42:09 +020031
32 bool insert(TSymbol *symbol);
33
34 // Insert a function using its unmangled name as the key.
Olli Etuahob92f92a2018-02-15 19:14:59 +020035 void insertUnmangled(TFunction *function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +020036
Olli Etuahofbb1c792018-01-19 16:26:59 +020037 TSymbol *find(const ImmutableString &name) const;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020038
Olli Etuahodefe3932018-02-13 11:56:09 +020039 void addInvariantVarying(const ImmutableString &name) { mInvariantVaryings.insert(name); }
Olli Etuahodd21ecf2018-01-10 12:42:09 +020040
Olli Etuahodefe3932018-02-13 11:56:09 +020041 bool isVaryingInvariant(const ImmutableString &name)
Olli Etuahodd21ecf2018-01-10 12:42:09 +020042 {
43 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
44 }
45
46 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
47
Olli Etuahodd21ecf2018-01-10 12:42:09 +020048 private:
Olli Etuahofbb1c792018-01-19 16:26:59 +020049 using tLevel = TUnorderedMap<ImmutableString,
50 TSymbol *,
51 ImmutableString::FowlerNollVoHash<sizeof(size_t)>>;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020052 using tLevelPair = const tLevel::value_type;
53 using tInsertResult = std::pair<tLevel::iterator, bool>;
54
55 tLevel level;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020056
Olli Etuahodefe3932018-02-13 11:56:09 +020057 std::set<ImmutableString> mInvariantVaryings;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020058 bool mGlobalInvariant;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020059};
60
61class TSymbolTable::TSymbolTableBuiltInLevel
62{
63 public:
64 TSymbolTableBuiltInLevel() = default;
65
66 bool insert(const TSymbol *symbol);
67
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020068 const TSymbol *find(const ImmutableString &name) const;
69
Olli Etuaho140152e2018-02-08 14:46:44 +020070 void insertUnmangledBuiltIn(const ImmutableString &name, TExtension ext);
Olli Etuaho7c8567a2018-02-20 15:44:07 +020071 const UnmangledBuiltIn *getUnmangledBuiltIn(const ImmutableString &name) const;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020072
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 Etuahodd21ecf2018-01-10 12:42:09 +020081
Olli Etuaho7c8567a2018-02-20 15:44:07 +020082 std::map<ImmutableString, UnmangledBuiltIn> mUnmangledBuiltIns;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020083};
84
Olli Etuahodd21ecf2018-01-10 12:42:09 +020085bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040086{
Jamie Madillbfa91f42014-06-05 15:45:18 -040087 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040088 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040089 return result.second;
90}
91
Olli Etuahob92f92a2018-02-15 19:14:59 +020092void TSymbolTable::TSymbolTableLevel::insertUnmangled(TFunction *function)
Olli Etuahob2983c92015-03-18 14:02:46 +020093{
Olli Etuahob92f92a2018-02-15 19:14:59 +020094 level.insert(tLevelPair(function->name(), function));
Olli Etuahob2983c92015-03-18 14:02:46 +020095}
96
Olli Etuahofbb1c792018-01-19 16:26:59 +020097TSymbol *TSymbolTable::TSymbolTableLevel::find(const ImmutableString &name) const
Jamie Madillbfa91f42014-06-05 15:45:18 -040098{
99 tLevel::const_iterator it = level.find(name);
100 if (it == level.end())
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200101 return nullptr;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400102 else
103 return (*it).second;
104}
105
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200106bool 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 Etuahoe5fe7aa2018-01-29 12:06:11 +0200113const 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 Etuaho140152e2018-02-08 14:46:44 +0200122void TSymbolTable::TSymbolTableBuiltInLevel::insertUnmangledBuiltIn(const ImmutableString &name,
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200123 TExtension ext)
Olli Etuaho342b83d2018-01-10 13:24:01 +0200124{
Olli Etuaho140152e2018-02-08 14:46:44 +0200125 if (ext == TExtension::UNDEFINED || mUnmangledBuiltIns.find(name) == mUnmangledBuiltIns.end())
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200126 {
Olli Etuaho140152e2018-02-08 14:46:44 +0200127 mUnmangledBuiltIns[name] = UnmangledBuiltIn(ext);
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200128 }
Olli Etuaho342b83d2018-01-10 13:24:01 +0200129}
130
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200131const UnmangledBuiltIn *TSymbolTable::TSymbolTableBuiltInLevel::getUnmangledBuiltIn(
132 const ImmutableString &name) const
Olli Etuaho342b83d2018-01-10 13:24:01 +0200133{
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200134 auto it = mUnmangledBuiltIns.find(ImmutableString(name));
135 if (it == mUnmangledBuiltIns.end())
136 {
137 return nullptr;
138 }
139 return &(it->second);
Olli Etuaho342b83d2018-01-10 13:24:01 +0200140}
141
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200142TSymbolTable::TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(-1)
143{
144}
145
146TSymbolTable::~TSymbolTable() = default;
147
Olli Etuaho437664b2018-02-28 15:38:14 +0200148bool TSymbolTable::isEmpty() const
149{
150 return mTable.empty();
151}
152
153bool TSymbolTable::atGlobalLevel() const
154{
155 return mTable.size() == 1u;
156}
157
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200158void TSymbolTable::pushBuiltInLevel()
159{
160 mBuiltInTable.push_back(
161 std::unique_ptr<TSymbolTableBuiltInLevel>(new TSymbolTableBuiltInLevel));
162}
163
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200164void TSymbolTable::push()
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000165{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200166 mTable.push_back(std::unique_ptr<TSymbolTableLevel>(new TSymbolTableLevel));
167 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200168}
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000169
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200170void TSymbolTable::pop()
171{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200172 mTable.pop_back();
173 mPrecisionStack.pop_back();
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200174}
175
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200176const TFunction *TSymbolTable::markFunctionHasPrototypeDeclaration(
Olli Etuahofbb1c792018-01-19 16:26:59 +0200177 const ImmutableString &mangledName,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200178 bool *hadPrototypeDeclarationOut)
179{
180 TFunction *function = findUserDefinedFunction(mangledName);
181 *hadPrototypeDeclarationOut = function->hasPrototypeDeclaration();
182 function->setHasPrototypeDeclaration();
183 return function;
184}
185
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200186const TFunction *TSymbolTable::setFunctionParameterNamesFromDefinition(const TFunction *function,
187 bool *wasDefinedOut)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200188{
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 Etuaho029e8ca2018-02-16 14:06:49 +0200196 // The previous declaration should have the same parameters as the function definition
197 // (parameter names may differ).
198 firstDeclaration->shareParameters(*function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200199 }
200
201 *wasDefinedOut = firstDeclaration->isDefined();
202 firstDeclaration->setDefined();
203 return firstDeclaration;
204}
205
Olli Etuahofbb1c792018-01-19 16:26:59 +0200206const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200207{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200208 int userDefinedLevel = static_cast<int>(mTable.size()) - 1;
209 while (userDefinedLevel >= 0)
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000210 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200211 const TSymbol *symbol = mTable[userDefinedLevel]->find(name);
212 if (symbol)
213 {
214 return symbol;
215 }
216 userDefinedLevel--;
217 }
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000218
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200219 return findBuiltIn(name, shaderVersion, false);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000220}
221
Olli Etuahofbb1c792018-01-19 16:26:59 +0200222TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200223{
224 // User-defined functions are always declared at the global level.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200225 ASSERT(!mTable.empty());
226 return static_cast<TFunction *>(mTable[0]->find(name));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200227}
228
Olli Etuahofbb1c792018-01-19 16:26:59 +0200229const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const
Zhenyao Mod7490962016-11-09 15:49:51 -0800230{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200231 ASSERT(!mTable.empty());
232 return mTable[0]->find(name);
Zhenyao Mod7490962016-11-09 15:49:51 -0800233}
234
Olli Etuahofbb1c792018-01-19 16:26:59 +0200235const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000236{
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300237 return findBuiltIn(name, shaderVersion, false);
238}
239
Olli Etuahofbb1c792018-01-19 16:26:59 +0200240const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200241 int shaderVersion,
242 bool includeGLSLBuiltins) const
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300243{
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000244 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
245 {
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300246 if (level == GLSL_BUILTINS && !includeGLSLBuiltins)
247 level--;
Martin Radeve93d24e2016-07-28 12:06:05 +0300248 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
249 level--;
250 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700251 level--;
252 if (level == ESSL1_BUILTINS && shaderVersion != 100)
253 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000254
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200255 const TSymbol *symbol = mBuiltInTable[level]->find(name);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000256
257 if (symbol)
258 return symbol;
259 }
260
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300261 return nullptr;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000262}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400263
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500264
Olli Etuaho437664b2018-02-28 15:38:14 +0200265bool TSymbolTable::declare(TSymbol *symbol)
Olli Etuaho0f684632017-07-13 12:42:15 +0300266{
Olli Etuaho437664b2018-02-28 15:38:14 +0200267 ASSERT(!mTable.empty());
268 ASSERT(symbol->symbolType() == SymbolType::UserDefined);
269 ASSERT(!symbol->isFunction());
270 return mTable.back()->insert(symbol);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800271}
272
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200273void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
274{
Olli Etuaho437664b2018-02-28 15:38:14 +0200275 ASSERT(!mTable.empty());
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200276 if (insertUnmangledName)
277 {
278 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200279 mTable[0]->insertUnmangled(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200280 }
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200281 mTable[0]->insert(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200282}
283
Olli Etuaho437664b2018-02-28 15:38:14 +0200284void TSymbolTable::insertVariable(ESymbolLevel level,
285 const ImmutableString &name,
286 const TType *type)
Olli Etuaho0f684632017-07-13 12:42:15 +0300287{
Olli Etuahob60d30f2018-01-16 12:31:06 +0200288 ASSERT(type->isRealized());
Olli Etuaho437664b2018-02-28 15:38:14 +0200289 TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn);
290 insertBuiltIn(level, var);
Olli Etuaho0f684632017-07-13 12:42:15 +0300291}
292
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200293void TSymbolTable::insertVariableExt(ESymbolLevel level,
294 TExtension ext,
295 const ImmutableString &name,
296 const TType *type)
Olli Etuaho0f684632017-07-13 12:42:15 +0300297{
Olli Etuahob60d30f2018-01-16 12:31:06 +0200298 ASSERT(type->isRealized());
Olli Etuahofbb1c792018-01-19 16:26:59 +0200299 TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn, ext);
Olli Etuaho437664b2018-02-28 15:38:14 +0200300 insertBuiltIn(level, var);
Olli Etuaho0f684632017-07-13 12:42:15 +0300301}
302
Olli Etuaho437664b2018-02-28 15:38:14 +0200303void TSymbolTable::insertBuiltIn(ESymbolLevel level, const TSymbol *symbol)
Olli Etuaho195be942017-12-04 23:40:14 +0200304{
Olli Etuaho437664b2018-02-28 15:38:14 +0200305 ASSERT(symbol);
306 ASSERT(level <= LAST_BUILTIN_LEVEL);
Olli Etuaho195be942017-12-04 23:40:14 +0200307
Olli Etuaho437664b2018-02-28 15:38:14 +0200308 mBuiltInTable[level]->insert(symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +0300309}
310
Olli Etuaho29bda812018-01-26 17:37:36 +0200311template <TPrecision precision>
Olli Etuaho437664b2018-02-28 15:38:14 +0200312void TSymbolTable::insertConstInt(ESymbolLevel level, const ImmutableString &name, int value)
Olli Etuaho29bda812018-01-26 17:37:36 +0200313{
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 Etuaho437664b2018-02-28 15:38:14 +0200319 insertBuiltIn(level, constant);
Olli Etuaho29bda812018-01-26 17:37:36 +0200320}
321
322template <TPrecision precision>
Olli Etuaho437664b2018-02-28 15:38:14 +0200323void TSymbolTable::insertConstIntExt(ESymbolLevel level,
Olli Etuaho29bda812018-01-26 17:37:36 +0200324 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 Etuaho437664b2018-02-28 15:38:14 +0200333 insertBuiltIn(level, constant);
Olli Etuaho29bda812018-01-26 17:37:36 +0200334}
335
336template <TPrecision precision>
Olli Etuaho437664b2018-02-28 15:38:14 +0200337void TSymbolTable::insertConstIvec3(ESymbolLevel level,
Olli Etuaho29bda812018-01-26 17:37:36 +0200338 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 Etuaho437664b2018-02-28 15:38:14 +0200351 insertBuiltIn(level, constantIvec3);
Olli Etuaho29bda812018-01-26 17:37:36 +0200352}
353
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200354void 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 Moe740add2014-07-18 17:01:01 -0700361TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700362{
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 Etuahoe5fe7aa2018-01-29 12:06:11 +0200369 int level = static_cast<int>(mPrecisionStack.size()) - 1;
370 ASSERT(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200371 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700372 TPrecision prec = EbpUndefined;
373 while (level >= 0)
374 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200375 PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType);
376 if (it != mPrecisionStack[level]->end())
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700377 {
378 prec = (*it).second;
379 break;
380 }
381 level--;
382 }
383 return prec;
384}
Jamie Madill45bcc782016-11-07 13:58:48 -0500385
Olli Etuahodefe3932018-02-13 11:56:09 +0200386void TSymbolTable::addInvariantVarying(const ImmutableString &originalName)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200387{
388 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200389 mTable.back()->addInvariantVarying(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200390}
391
Olli Etuahodefe3932018-02-13 11:56:09 +0200392bool TSymbolTable::isVaryingInvariant(const ImmutableString &originalName) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200393{
394 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200395 return mTable.back()->isVaryingInvariant(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200396}
397
398void TSymbolTable::setGlobalInvariant(bool invariant)
399{
400 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200401 mTable.back()->setGlobalInvariant(invariant);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200402}
403
Olli Etuaho140152e2018-02-08 14:46:44 +0200404void TSymbolTable::insertUnmangledBuiltIn(const ImmutableString &name,
405 TExtension ext,
406 ESymbolLevel level)
Martin Radevda6254b2016-12-14 17:00:36 +0200407{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200408 ASSERT(level >= 0 && level <= LAST_BUILTIN_LEVEL);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200409 ASSERT(mUserDefinedUniqueIdsStart == -1);
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200410 mBuiltInTable[level]->insertUnmangledBuiltIn(name, ext);
Martin Radevda6254b2016-12-14 17:00:36 +0200411}
412
413bool TSymbolTable::hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level)
414{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200415 ASSERT(level >= 0 && level <= LAST_BUILTIN_LEVEL);
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200416 return mBuiltInTable[level]->getUnmangledBuiltIn(ImmutableString(name)) != nullptr;
Martin Radevda6254b2016-12-14 17:00:36 +0200417}
418
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200419const UnmangledBuiltIn *TSymbolTable::getUnmangledBuiltInForShaderVersion(
420 const ImmutableString &name,
421 int shaderVersion)
Martin Radevda6254b2016-12-14 17:00:36 +0200422{
Martin Radevda6254b2016-12-14 17:00:36 +0200423 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 Etuaho7c8567a2018-02-20 15:44:07 +0200438 const UnmangledBuiltIn *builtIn = mBuiltInTable[level]->getUnmangledBuiltIn(name);
439 if (builtIn != nullptr)
Martin Radevda6254b2016-12-14 17:00:36 +0200440 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200441 return builtIn;
Martin Radevda6254b2016-12-14 17:00:36 +0200442 }
443 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200444 return nullptr;
Martin Radevda6254b2016-12-14 17:00:36 +0200445}
446
Olli Etuaho5d69db12017-11-24 16:51:15 +0200447void TSymbolTable::markBuiltInInitializationFinished()
448{
449 mUserDefinedUniqueIdsStart = mUniqueIdCounter;
450}
451
452void TSymbolTable::clearCompilationResults()
453{
454 mUniqueIdCounter = mUserDefinedUniqueIdsStart;
455
456 // User-defined scopes should have already been cleared when the compilation finished.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200457 ASSERT(mTable.size() == 0u);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200458}
459
460int TSymbolTable::nextUniqueIdValue()
461{
462 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
463 return ++mUniqueIdCounter;
464}
465
Olli Etuaho29bda812018-01-26 17:37:36 +0200466void TSymbolTable::initializeBuiltIns(sh::GLenum type,
467 ShShaderSpec spec,
468 const ShBuiltInResources &resources)
469{
470 ASSERT(isEmpty());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200471 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 Etuaho29bda812018-01-26 17:37:36 +0200479
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 Etuaho140152e2018-02-08 14:46:44 +0200508 insertBuiltInFunctions(type);
509 insertBuiltInFunctionUnmangledNames(type);
510 mUniqueIdCounter = kLastStaticBuiltInId + 1;
511
Olli Etuaho29bda812018-01-26 17:37:36 +0200512 initializeBuiltInVariables(type, spec, resources);
513 markBuiltInInitializationFinished();
514}
515
516void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType)
517{
518 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
519 setDefaultPrecision(samplerType, EbpLow);
520}
521
Olli Etuaho29bda812018-01-26 17:37:36 +0200522
523void 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 Etuaho437664b2018-02-28 15:38:14 +0200542 insertBuiltIn(COMMON_BUILTINS, depthRangeStruct);
Olli Etuaho29bda812018-01-26 17:37:36 +0200543 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 Etuaho7c8567a2018-02-20 15:44:07 +0200569 insertConstIntExt<EbpMedium>(COMMON_BUILTINS, TExtension::EXT_blend_func_extended,
570 ImmutableString("gl_MaxDualSourceDrawBuffersEXT"),
571 resources.MaxDualSourceDrawBuffers);
Olli Etuaho29bda812018-01-26 17:37:36 +0200572
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 Etuaho29bda812018-01-26 17:37:36 +0200632 {
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 Etuaho437664b2018-02-28 15:38:14 +0200841 insertBuiltIn(ESSL3_1_BUILTINS, glPerVertexInBlock);
Olli Etuaho29bda812018-01-26 17:37:36 +0200842
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 Madill45bcc782016-11-07 13:58:48 -0500879} // namespace sh