blob: 49b49fc8f24e7038a0837e11bcac7bbceb577cf2 [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
70 void insertUnmangledBuiltInName(const char *name);
71 bool hasUnmangledBuiltIn(const char *name) const;
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 Etuahodd21ecf2018-01-10 12:42:09 +020081
Olli Etuaho2d8e4322018-01-22 14:12:46 +020082 std::set<ImmutableString> mUnmangledBuiltInNames;
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
122void TSymbolTable::TSymbolTableBuiltInLevel::insertUnmangledBuiltInName(const char *name)
Olli Etuaho342b83d2018-01-10 13:24:01 +0200123{
Olli Etuaho2d8e4322018-01-22 14:12:46 +0200124 mUnmangledBuiltInNames.insert(ImmutableString(name));
Olli Etuaho342b83d2018-01-10 13:24:01 +0200125}
126
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200127bool TSymbolTable::TSymbolTableBuiltInLevel::hasUnmangledBuiltIn(const char *name) const
Olli Etuaho342b83d2018-01-10 13:24:01 +0200128{
Olli Etuaho2d8e4322018-01-22 14:12:46 +0200129 return mUnmangledBuiltInNames.count(ImmutableString(name)) > 0;
Olli Etuaho342b83d2018-01-10 13:24:01 +0200130}
131
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200132TSymbolTable::TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(-1)
133{
134}
135
136TSymbolTable::~TSymbolTable() = default;
137
138void TSymbolTable::pushBuiltInLevel()
139{
140 mBuiltInTable.push_back(
141 std::unique_ptr<TSymbolTableBuiltInLevel>(new TSymbolTableBuiltInLevel));
142}
143
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200144void TSymbolTable::push()
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000145{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200146 mTable.push_back(std::unique_ptr<TSymbolTableLevel>(new TSymbolTableLevel));
147 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200148}
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000149
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200150void TSymbolTable::pop()
151{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200152 mTable.pop_back();
153 mPrecisionStack.pop_back();
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200154}
155
156const TFunction *TSymbolTable::markUserDefinedFunctionHasPrototypeDeclaration(
Olli Etuahofbb1c792018-01-19 16:26:59 +0200157 const ImmutableString &mangledName,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200158 bool *hadPrototypeDeclarationOut)
159{
160 TFunction *function = findUserDefinedFunction(mangledName);
161 *hadPrototypeDeclarationOut = function->hasPrototypeDeclaration();
162 function->setHasPrototypeDeclaration();
163 return function;
164}
165
166const TFunction *TSymbolTable::setUserDefinedFunctionParameterNamesFromDefinition(
167 const TFunction *function,
168 bool *wasDefinedOut)
169{
170 TFunction *firstDeclaration = findUserDefinedFunction(function->getMangledName());
171 ASSERT(firstDeclaration);
172 // Note: 'firstDeclaration' could be 'function' if this is the first time we've seen function as
173 // it would have just been put in the symbol table. Otherwise, we're looking up an earlier
174 // occurance.
175 if (function != firstDeclaration)
176 {
177 // Swap the parameters of the previous declaration to the parameters of the function
178 // definition (parameter names may differ).
179 firstDeclaration->swapParameters(*function);
180 }
181
182 *wasDefinedOut = firstDeclaration->isDefined();
183 firstDeclaration->setDefined();
184 return firstDeclaration;
185}
186
Olli Etuahofbb1c792018-01-19 16:26:59 +0200187const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200188{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200189 int userDefinedLevel = static_cast<int>(mTable.size()) - 1;
190 while (userDefinedLevel >= 0)
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000191 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200192 const TSymbol *symbol = mTable[userDefinedLevel]->find(name);
193 if (symbol)
194 {
195 return symbol;
196 }
197 userDefinedLevel--;
198 }
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000199
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200200 return findBuiltIn(name, shaderVersion, false);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000201}
202
Olli Etuahofbb1c792018-01-19 16:26:59 +0200203TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200204{
205 // User-defined functions are always declared at the global level.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200206 ASSERT(!mTable.empty());
207 return static_cast<TFunction *>(mTable[0]->find(name));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200208}
209
Olli Etuahofbb1c792018-01-19 16:26:59 +0200210const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const
Zhenyao Mod7490962016-11-09 15:49:51 -0800211{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200212 ASSERT(!mTable.empty());
213 return mTable[0]->find(name);
Zhenyao Mod7490962016-11-09 15:49:51 -0800214}
215
Olli Etuahofbb1c792018-01-19 16:26:59 +0200216const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000217{
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300218 return findBuiltIn(name, shaderVersion, false);
219}
220
Olli Etuahofbb1c792018-01-19 16:26:59 +0200221const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200222 int shaderVersion,
223 bool includeGLSLBuiltins) const
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300224{
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000225 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
226 {
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300227 if (level == GLSL_BUILTINS && !includeGLSLBuiltins)
228 level--;
Martin Radeve93d24e2016-07-28 12:06:05 +0300229 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
230 level--;
231 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700232 level--;
233 if (level == ESSL1_BUILTINS && shaderVersion != 100)
234 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000235
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200236 const TSymbol *symbol = mBuiltInTable[level]->find(name);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000237
238 if (symbol)
239 return symbol;
240 }
241
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300242 return nullptr;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000243}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400244
Kai Ninomiya030017a2017-12-06 14:06:53 -0800245constexpr bool IsGenType(const TType *type)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500246{
247 if (type)
248 {
249 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500250 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType ||
251 basicType == EbtGenBType;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500252 }
253
254 return false;
255}
256
Kai Ninomiya030017a2017-12-06 14:06:53 -0800257constexpr bool IsVecType(const TType *type)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500258{
259 if (type)
260 {
261 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500262 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec ||
263 basicType == EbtBVec;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500264 }
265
266 return false;
267}
268
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800269constexpr const TType *SpecificType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500270{
271 ASSERT(size >= 1 && size <= 4);
272
273 if (!type)
274 {
275 return nullptr;
276 }
277
278 ASSERT(!IsVecType(type));
279
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500280 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500281 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500282 case EbtGenType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800283 return StaticType::GetForVec<EbtFloat>(type->getQualifier(),
284 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500285 case EbtGenIType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800286 return StaticType::GetForVec<EbtInt>(type->getQualifier(),
287 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500288 case EbtGenUType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800289 return StaticType::GetForVec<EbtUInt>(type->getQualifier(),
290 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500291 case EbtGenBType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800292 return StaticType::GetForVec<EbtBool>(type->getQualifier(),
293 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500294 default:
295 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500296 }
297}
298
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800299constexpr const TType *VectorType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500300{
301 ASSERT(size >= 2 && size <= 4);
302
303 if (!type)
304 {
305 return nullptr;
306 }
307
308 ASSERT(!IsGenType(type));
309
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500310 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500311 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500312 case EbtVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800313 return StaticType::GetForVecMat<EbtFloat>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500314 case EbtIVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800315 return StaticType::GetForVecMat<EbtInt>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500316 case EbtUVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800317 return StaticType::GetForVecMat<EbtUInt>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500318 case EbtBVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800319 return StaticType::GetForVecMat<EbtBool>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500320 default:
321 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500322 }
323}
324
Olli Etuaho195be942017-12-04 23:40:14 +0200325bool TSymbolTable::declareVariable(TVariable *variable)
Olli Etuaho0f684632017-07-13 12:42:15 +0300326{
Olli Etuaho195be942017-12-04 23:40:14 +0200327 ASSERT(variable->symbolType() == SymbolType::UserDefined);
328 return insertVariable(currentLevel(), variable);
Olli Etuaho0f684632017-07-13 12:42:15 +0300329}
330
Olli Etuaho035419f2017-11-28 14:27:15 +0200331bool TSymbolTable::declareStructType(TStructure *str)
Olli Etuaho0f684632017-07-13 12:42:15 +0300332{
333 return insertStructType(currentLevel(), str);
334}
335
Olli Etuaho378c3a52017-12-04 11:32:13 +0200336bool TSymbolTable::declareInterfaceBlock(TInterfaceBlock *interfaceBlock)
Olli Etuaho0f684632017-07-13 12:42:15 +0300337{
Olli Etuaho378c3a52017-12-04 11:32:13 +0200338 return insert(currentLevel(), interfaceBlock);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800339}
340
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200341void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
342{
343 ASSERT(currentLevel() >= GLOBAL_LEVEL);
344 if (insertUnmangledName)
345 {
346 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200347 mTable[0]->insertUnmangled(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200348 }
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200349 mTable[0]->insert(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200350}
351
Olli Etuahofbb1c792018-01-19 16:26:59 +0200352TVariable *TSymbolTable::insertVariable(ESymbolLevel level,
353 const ImmutableString &name,
354 const TType *type)
Olli Etuaho0f684632017-07-13 12:42:15 +0300355{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100356 ASSERT(level <= LAST_BUILTIN_LEVEL);
Olli Etuahob60d30f2018-01-16 12:31:06 +0200357 ASSERT(type->isRealized());
Olli Etuahofbb1c792018-01-19 16:26:59 +0200358 return insertVariable(level, name, type, SymbolType::BuiltIn);
Olli Etuaho0f684632017-07-13 12:42:15 +0300359}
360
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100361TVariable *TSymbolTable::insertVariable(ESymbolLevel level,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200362 const ImmutableString &name,
Olli Etuahob60d30f2018-01-16 12:31:06 +0200363 const TType *type,
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100364 SymbolType symbolType)
Olli Etuaho0f684632017-07-13 12:42:15 +0300365{
Olli Etuahob60d30f2018-01-16 12:31:06 +0200366 ASSERT(level > LAST_BUILTIN_LEVEL || type->isRealized());
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100367 TVariable *var = new TVariable(this, name, type, symbolType);
Olli Etuaho0f684632017-07-13 12:42:15 +0300368 if (insert(level, var))
369 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300370 return var;
371 }
372 return nullptr;
373}
374
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200375void TSymbolTable::insertVariableExt(ESymbolLevel level,
376 TExtension ext,
377 const ImmutableString &name,
378 const TType *type)
Olli Etuaho0f684632017-07-13 12:42:15 +0300379{
Olli Etuahob60d30f2018-01-16 12:31:06 +0200380 ASSERT(level <= LAST_BUILTIN_LEVEL);
381 ASSERT(type->isRealized());
Olli Etuahofbb1c792018-01-19 16:26:59 +0200382 TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn, ext);
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200383 bool inserted = insert(level, var);
384 UNUSED_VARIABLE(inserted);
385 ASSERT(inserted);
Olli Etuaho0f684632017-07-13 12:42:15 +0300386}
387
Olli Etuaho195be942017-12-04 23:40:14 +0200388bool TSymbolTable::insertVariable(ESymbolLevel level, TVariable *variable)
389{
390 ASSERT(variable);
Olli Etuahob60d30f2018-01-16 12:31:06 +0200391 ASSERT(level > LAST_BUILTIN_LEVEL || variable->getType().isRealized());
Olli Etuaho195be942017-12-04 23:40:14 +0200392 return insert(level, variable);
393}
394
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200395bool TSymbolTable::insert(ESymbolLevel level, TSymbol *symbol)
396{
397 ASSERT(level > LAST_BUILTIN_LEVEL || mUserDefinedUniqueIdsStart == -1);
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200398 if (level <= LAST_BUILTIN_LEVEL)
399 {
400 return mBuiltInTable[level]->insert(symbol);
401 }
402 else
403 {
404 return mTable[level - LAST_BUILTIN_LEVEL - 1]->insert(symbol);
405 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200406}
407
Olli Etuaho035419f2017-11-28 14:27:15 +0200408bool TSymbolTable::insertStructType(ESymbolLevel level, TStructure *str)
Olli Etuaho0f684632017-07-13 12:42:15 +0300409{
Olli Etuaho035419f2017-11-28 14:27:15 +0200410 ASSERT(str);
Olli Etuaho378c3a52017-12-04 11:32:13 +0200411 return insert(level, str);
412}
413
414bool TSymbolTable::insertInterfaceBlock(ESymbolLevel level, TInterfaceBlock *interfaceBlock)
415{
416 ASSERT(interfaceBlock);
417 return insert(level, interfaceBlock);
Olli Etuaho0f684632017-07-13 12:42:15 +0300418}
419
Olli Etuaho29bda812018-01-26 17:37:36 +0200420template <TPrecision precision>
421bool TSymbolTable::insertConstInt(ESymbolLevel level, const ImmutableString &name, int value)
422{
423 TVariable *constant = new TVariable(
424 this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn);
425 TConstantUnion *unionArray = new TConstantUnion[1];
426 unionArray[0].setIConst(value);
427 constant->shareConstPointer(unionArray);
428 return insert(level, constant);
429}
430
431template <TPrecision precision>
432bool TSymbolTable::insertConstIntExt(ESymbolLevel level,
433 TExtension ext,
434 const ImmutableString &name,
435 int value)
436{
437 TVariable *constant = new TVariable(
438 this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn, ext);
439 TConstantUnion *unionArray = new TConstantUnion[1];
440 unionArray[0].setIConst(value);
441 constant->shareConstPointer(unionArray);
442 return insert(level, constant);
443}
444
445template <TPrecision precision>
446bool TSymbolTable::insertConstIvec3(ESymbolLevel level,
447 const ImmutableString &name,
448 const std::array<int, 3> &values)
449{
450 TVariable *constantIvec3 = new TVariable(
451 this, name, StaticType::Get<EbtInt, precision, EvqConst, 3, 1>(), SymbolType::BuiltIn);
452
453 TConstantUnion *unionArray = new TConstantUnion[3];
454 for (size_t index = 0u; index < 3u; ++index)
455 {
456 unionArray[index].setIConst(values[index]);
457 }
458 constantIvec3->shareConstPointer(unionArray);
459
460 return insert(level, constantIvec3);
461}
462
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500463void TSymbolTable::insertBuiltIn(ESymbolLevel level,
464 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300465 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500466 const TType *rvalue,
467 const char *name,
468 const TType *ptype1,
469 const TType *ptype2,
470 const TType *ptype3,
471 const TType *ptype4,
472 const TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700473{
474 if (ptype1->getBasicType() == EbtGSampler2D)
475 {
Martin Radevda6254b2016-12-14 17:00:36 +0200476 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700477 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800478 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
479 StaticType::GetBasic<EbtSampler2D>(), ptype2, ptype3, ptype4, ptype5);
480 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
481 StaticType::GetBasic<EbtISampler2D>(), ptype2, ptype3, ptype4, ptype5);
482 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
483 StaticType::GetBasic<EbtUSampler2D>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700484 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500485 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700486 {
Martin Radevda6254b2016-12-14 17:00:36 +0200487 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700488 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800489 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
490 StaticType::GetBasic<EbtSampler3D>(), ptype2, ptype3, ptype4, ptype5);
491 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
492 StaticType::GetBasic<EbtISampler3D>(), ptype2, ptype3, ptype4, ptype5);
493 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
494 StaticType::GetBasic<EbtUSampler3D>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700495 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500496 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700497 {
Martin Radevda6254b2016-12-14 17:00:36 +0200498 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700499 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800500 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
501 StaticType::GetBasic<EbtSamplerCube>(), ptype2, ptype3, ptype4, ptype5);
502 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
503 StaticType::GetBasic<EbtISamplerCube>(), ptype2, ptype3, ptype4, ptype5);
504 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
505 StaticType::GetBasic<EbtUSamplerCube>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700506 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500507 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700508 {
Martin Radevda6254b2016-12-14 17:00:36 +0200509 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700510 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800511 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
512 StaticType::GetBasic<EbtSampler2DArray>(), ptype2, ptype3, ptype4,
513 ptype5);
514 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
515 StaticType::GetBasic<EbtISampler2DArray>(), ptype2, ptype3, ptype4,
516 ptype5);
517 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
518 StaticType::GetBasic<EbtUSampler2DArray>(), ptype2, ptype3, ptype4,
519 ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700520 }
JiangYizhou40219322016-12-09 09:50:51 +0800521 else if (ptype1->getBasicType() == EbtGSampler2DMS)
522 {
523 insertUnmangledBuiltInName(name, level);
524 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800525 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
526 StaticType::GetBasic<EbtSampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
527 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
528 StaticType::GetBasic<EbtISampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
529 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
530 StaticType::GetBasic<EbtUSampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
JiangYizhou40219322016-12-09 09:50:51 +0800531 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300532 else if (IsGImage(ptype1->getBasicType()))
533 {
Martin Radevda6254b2016-12-14 17:00:36 +0200534 insertUnmangledBuiltInName(name, level);
Martin Radev2cc85b32016-08-05 16:22:53 +0300535
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800536 const TType *floatType = StaticType::GetBasic<EbtFloat, 4>();
537 const TType *intType = StaticType::GetBasic<EbtInt, 4>();
538 const TType *unsignedType = StaticType::GetBasic<EbtUInt, 4>();
Martin Radev2cc85b32016-08-05 16:22:53 +0300539
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800540 const TType *floatImage = StaticType::GetForFloatImage(ptype1->getBasicType());
541 const TType *intImage = StaticType::GetForIntImage(ptype1->getBasicType());
542 const TType *unsignedImage = StaticType::GetForUintImage(ptype1->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300543
544 // GLSL ES 3.10, Revision 4, 8.12 Image Functions
545 if (rvalue->getBasicType() == EbtGVec4)
546 {
547 // imageLoad
548 insertBuiltIn(level, floatType, name, floatImage, ptype2, ptype3, ptype4, ptype5);
549 insertBuiltIn(level, intType, name, intImage, ptype2, ptype3, ptype4, ptype5);
550 insertBuiltIn(level, unsignedType, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
551 }
552 else if (rvalue->getBasicType() == EbtVoid)
553 {
554 // imageStore
555 insertBuiltIn(level, rvalue, name, floatImage, ptype2, floatType, ptype4, ptype5);
556 insertBuiltIn(level, rvalue, name, intImage, ptype2, intType, ptype4, ptype5);
557 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, unsignedType, ptype4, ptype5);
558 }
559 else
560 {
561 // imageSize
562 insertBuiltIn(level, rvalue, name, floatImage, ptype2, ptype3, ptype4, ptype5);
563 insertBuiltIn(level, rvalue, name, intImage, ptype2, ptype3, ptype4, ptype5);
564 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
565 }
566 }
Olli Etuaho9250cb22017-01-21 10:51:27 +0000567 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3) ||
568 IsGenType(ptype4))
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500569 {
Olli Etuaho9250cb22017-01-21 10:51:27 +0000570 ASSERT(!ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200571 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500572 insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000573 SpecificType(ptype2, 1), SpecificType(ptype3, 1), SpecificType(ptype4, 1));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500574 insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000575 SpecificType(ptype2, 2), SpecificType(ptype3, 2), SpecificType(ptype4, 2));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500576 insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000577 SpecificType(ptype2, 3), SpecificType(ptype3, 3), SpecificType(ptype4, 3));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500578 insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000579 SpecificType(ptype2, 4), SpecificType(ptype3, 4), SpecificType(ptype4, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500580 }
581 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
582 {
583 ASSERT(!ptype4 && !ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200584 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500585 insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2),
586 VectorType(ptype2, 2), VectorType(ptype3, 2));
587 insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3),
588 VectorType(ptype2, 3), VectorType(ptype3, 3));
589 insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4),
590 VectorType(ptype2, 4), VectorType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500591 }
592 else
593 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100594 TFunction *function =
Olli Etuahofbb1c792018-01-19 16:26:59 +0200595 new TFunction(this, ImmutableString(name), rvalue, SymbolType::BuiltIn, false, op, ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700596
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700597 function->addParameter(TConstParameter(ptype1));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700598
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500599 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700600 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700601 function->addParameter(TConstParameter(ptype2));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700602 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700603
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500604 if (ptype3)
605 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700606 function->addParameter(TConstParameter(ptype3));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500607 }
608
609 if (ptype4)
610 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700611 function->addParameter(TConstParameter(ptype4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500612 }
613
614 if (ptype5)
615 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700616 function->addParameter(TConstParameter(ptype5));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500617 }
618
Martin Radevda6254b2016-12-14 17:00:36 +0200619 ASSERT(hasUnmangledBuiltInAtLevel(name, level));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500620 insert(level, function);
621 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700622}
623
Olli Etuaho492cfab2017-01-20 21:18:29 +0000624void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
625 TOperator op,
626 const TType *rvalue,
627 const TType *ptype1,
628 const TType *ptype2,
629 const TType *ptype3,
630 const TType *ptype4,
631 const TType *ptype5)
632{
633 const char *name = GetOperatorString(op);
634 ASSERT(strlen(name) > 0);
635 insertUnmangledBuiltInName(name, level);
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300636 insertBuiltIn(level, op, TExtension::UNDEFINED, rvalue, name, ptype1, ptype2, ptype3, ptype4,
637 ptype5);
Olli Etuaho492cfab2017-01-20 21:18:29 +0000638}
639
640void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
641 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300642 TExtension ext,
Olli Etuaho492cfab2017-01-20 21:18:29 +0000643 const TType *rvalue,
644 const TType *ptype1,
645 const TType *ptype2,
646 const TType *ptype3,
647 const TType *ptype4,
648 const TType *ptype5)
649{
650 const char *name = GetOperatorString(op);
651 insertUnmangledBuiltInName(name, level);
652 insertBuiltIn(level, op, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
653}
654
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300655void TSymbolTable::insertBuiltInFunctionNoParameters(ESymbolLevel level,
656 TOperator op,
657 const TType *rvalue,
658 const char *name)
659{
660 insertUnmangledBuiltInName(name, level);
Olli Etuaho0c371002017-12-13 17:00:25 +0400661 insert(level,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200662 new TFunction(this, ImmutableString(name), rvalue, SymbolType::BuiltIn, false, op));
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300663}
664
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800665void TSymbolTable::insertBuiltInFunctionNoParametersExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300666 TExtension ext,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800667 TOperator op,
668 const TType *rvalue,
669 const char *name)
670{
671 insertUnmangledBuiltInName(name, level);
Olli Etuaho0c371002017-12-13 17:00:25 +0400672 insert(level,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200673 new TFunction(this, ImmutableString(name), rvalue, SymbolType::BuiltIn, false, op, ext));
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800674}
675
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200676void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec)
677{
678 int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1;
679 // Uses map operator [], overwrites the current value
680 (*mPrecisionStack[indexOfLastElement])[type] = prec;
681}
682
Zhenyao Moe740add2014-07-18 17:01:01 -0700683TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700684{
685 if (!SupportsPrecision(type))
686 return EbpUndefined;
687
688 // unsigned integers use the same precision as signed
689 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
690
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200691 int level = static_cast<int>(mPrecisionStack.size()) - 1;
692 ASSERT(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200693 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700694 TPrecision prec = EbpUndefined;
695 while (level >= 0)
696 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200697 PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType);
698 if (it != mPrecisionStack[level]->end())
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700699 {
700 prec = (*it).second;
701 break;
702 }
703 level--;
704 }
705 return prec;
706}
Jamie Madill45bcc782016-11-07 13:58:48 -0500707
Olli Etuahodefe3932018-02-13 11:56:09 +0200708void TSymbolTable::addInvariantVarying(const ImmutableString &originalName)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200709{
710 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200711 mTable.back()->addInvariantVarying(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200712}
713
Olli Etuahodefe3932018-02-13 11:56:09 +0200714bool TSymbolTable::isVaryingInvariant(const ImmutableString &originalName) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200715{
716 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200717 return mTable.back()->isVaryingInvariant(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200718}
719
720void TSymbolTable::setGlobalInvariant(bool invariant)
721{
722 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200723 mTable.back()->setGlobalInvariant(invariant);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200724}
725
Martin Radevda6254b2016-12-14 17:00:36 +0200726void TSymbolTable::insertUnmangledBuiltInName(const char *name, ESymbolLevel level)
727{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200728 ASSERT(level >= 0 && level <= LAST_BUILTIN_LEVEL);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200729 ASSERT(mUserDefinedUniqueIdsStart == -1);
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200730 mBuiltInTable[level]->insertUnmangledBuiltInName(name);
Martin Radevda6254b2016-12-14 17:00:36 +0200731}
732
733bool TSymbolTable::hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level)
734{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200735 ASSERT(level >= 0 && level <= LAST_BUILTIN_LEVEL);
736 return mBuiltInTable[level]->hasUnmangledBuiltIn(name);
Martin Radevda6254b2016-12-14 17:00:36 +0200737}
738
739bool TSymbolTable::hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion)
740{
Martin Radevda6254b2016-12-14 17:00:36 +0200741 for (int level = LAST_BUILTIN_LEVEL; level >= 0; --level)
742 {
743 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
744 {
745 --level;
746 }
747 if (level == ESSL3_BUILTINS && shaderVersion < 300)
748 {
749 --level;
750 }
751 if (level == ESSL1_BUILTINS && shaderVersion != 100)
752 {
753 --level;
754 }
755
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200756 if (mBuiltInTable[level]->hasUnmangledBuiltIn(name))
Martin Radevda6254b2016-12-14 17:00:36 +0200757 {
758 return true;
759 }
760 }
761 return false;
762}
763
Olli Etuaho5d69db12017-11-24 16:51:15 +0200764void TSymbolTable::markBuiltInInitializationFinished()
765{
766 mUserDefinedUniqueIdsStart = mUniqueIdCounter;
767}
768
769void TSymbolTable::clearCompilationResults()
770{
771 mUniqueIdCounter = mUserDefinedUniqueIdsStart;
772
773 // User-defined scopes should have already been cleared when the compilation finished.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200774 ASSERT(mTable.size() == 0u);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200775}
776
777int TSymbolTable::nextUniqueIdValue()
778{
779 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
780 return ++mUniqueIdCounter;
781}
782
Olli Etuaho29bda812018-01-26 17:37:36 +0200783void TSymbolTable::initializeBuiltIns(sh::GLenum type,
784 ShShaderSpec spec,
785 const ShBuiltInResources &resources)
786{
787 ASSERT(isEmpty());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200788 pushBuiltInLevel(); // COMMON_BUILTINS
789 pushBuiltInLevel(); // ESSL1_BUILTINS
790 pushBuiltInLevel(); // ESSL3_BUILTINS
791 pushBuiltInLevel(); // ESSL3_1_BUILTINS
792 pushBuiltInLevel(); // GLSL_BUILTINS
793
794 // We need just one precision stack level for predefined precisions.
795 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuaho29bda812018-01-26 17:37:36 +0200796
797 switch (type)
798 {
799 case GL_FRAGMENT_SHADER:
800 setDefaultPrecision(EbtInt, EbpMedium);
801 break;
802 case GL_VERTEX_SHADER:
803 case GL_COMPUTE_SHADER:
804 case GL_GEOMETRY_SHADER_EXT:
805 setDefaultPrecision(EbtInt, EbpHigh);
806 setDefaultPrecision(EbtFloat, EbpHigh);
807 break;
808 default:
809 UNREACHABLE();
810 }
811 // Set defaults for sampler types that have default precision, even those that are
812 // only available if an extension exists.
813 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
814 initSamplerDefaultPrecision(EbtSampler2D);
815 initSamplerDefaultPrecision(EbtSamplerCube);
816 // SamplerExternalOES is specified in the extension to have default precision.
817 initSamplerDefaultPrecision(EbtSamplerExternalOES);
818 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
819 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
820 // It isn't specified whether Sampler2DRect has default precision.
821 initSamplerDefaultPrecision(EbtSampler2DRect);
822
823 setDefaultPrecision(EbtAtomicCounter, EbpHigh);
824
825 initializeBuiltInFunctions(type, spec, resources);
826 initializeBuiltInVariables(type, spec, resources);
827 markBuiltInInitializationFinished();
828}
829
830void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType)
831{
832 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
833 setDefaultPrecision(samplerType, EbpLow);
834}
835
836void TSymbolTable::initializeBuiltInFunctions(sh::GLenum type,
837 ShShaderSpec spec,
838 const ShBuiltInResources &resources)
839{
840 const TType *voidType = StaticType::GetBasic<EbtVoid>();
841 const TType *float1 = StaticType::GetBasic<EbtFloat>();
842 const TType *float2 = StaticType::GetBasic<EbtFloat, 2>();
843 const TType *float3 = StaticType::GetBasic<EbtFloat, 3>();
844 const TType *float4 = StaticType::GetBasic<EbtFloat, 4>();
845 const TType *int1 = StaticType::GetBasic<EbtInt>();
846 const TType *int2 = StaticType::GetBasic<EbtInt, 2>();
847 const TType *int3 = StaticType::GetBasic<EbtInt, 3>();
848 const TType *uint1 = StaticType::GetBasic<EbtUInt>();
849 const TType *bool1 = StaticType::GetBasic<EbtBool>();
850 const TType *genType = StaticType::GetBasic<EbtGenType>();
851 const TType *genIType = StaticType::GetBasic<EbtGenIType>();
852 const TType *genUType = StaticType::GetBasic<EbtGenUType>();
853 const TType *genBType = StaticType::GetBasic<EbtGenBType>();
854
855 //
856 // Angle and Trigonometric Functions.
857 //
858 insertBuiltInOp(COMMON_BUILTINS, EOpRadians, genType, genType);
859 insertBuiltInOp(COMMON_BUILTINS, EOpDegrees, genType, genType);
860 insertBuiltInOp(COMMON_BUILTINS, EOpSin, genType, genType);
861 insertBuiltInOp(COMMON_BUILTINS, EOpCos, genType, genType);
862 insertBuiltInOp(COMMON_BUILTINS, EOpTan, genType, genType);
863 insertBuiltInOp(COMMON_BUILTINS, EOpAsin, genType, genType);
864 insertBuiltInOp(COMMON_BUILTINS, EOpAcos, genType, genType);
865 insertBuiltInOp(COMMON_BUILTINS, EOpAtan, genType, genType, genType);
866 insertBuiltInOp(COMMON_BUILTINS, EOpAtan, genType, genType);
867 insertBuiltInOp(ESSL3_BUILTINS, EOpSinh, genType, genType);
868 insertBuiltInOp(ESSL3_BUILTINS, EOpCosh, genType, genType);
869 insertBuiltInOp(ESSL3_BUILTINS, EOpTanh, genType, genType);
870 insertBuiltInOp(ESSL3_BUILTINS, EOpAsinh, genType, genType);
871 insertBuiltInOp(ESSL3_BUILTINS, EOpAcosh, genType, genType);
872 insertBuiltInOp(ESSL3_BUILTINS, EOpAtanh, genType, genType);
873
874 //
875 // Exponential Functions.
876 //
877 insertBuiltInOp(COMMON_BUILTINS, EOpPow, genType, genType, genType);
878 insertBuiltInOp(COMMON_BUILTINS, EOpExp, genType, genType);
879 insertBuiltInOp(COMMON_BUILTINS, EOpLog, genType, genType);
880 insertBuiltInOp(COMMON_BUILTINS, EOpExp2, genType, genType);
881 insertBuiltInOp(COMMON_BUILTINS, EOpLog2, genType, genType);
882 insertBuiltInOp(COMMON_BUILTINS, EOpSqrt, genType, genType);
883 insertBuiltInOp(COMMON_BUILTINS, EOpInverseSqrt, genType, genType);
884
885 //
886 // Common Functions.
887 //
888 insertBuiltInOp(COMMON_BUILTINS, EOpAbs, genType, genType);
889 insertBuiltInOp(ESSL3_BUILTINS, EOpAbs, genIType, genIType);
890 insertBuiltInOp(COMMON_BUILTINS, EOpSign, genType, genType);
891 insertBuiltInOp(ESSL3_BUILTINS, EOpSign, genIType, genIType);
892 insertBuiltInOp(COMMON_BUILTINS, EOpFloor, genType, genType);
893 insertBuiltInOp(ESSL3_BUILTINS, EOpTrunc, genType, genType);
894 insertBuiltInOp(ESSL3_BUILTINS, EOpRound, genType, genType);
895 insertBuiltInOp(ESSL3_BUILTINS, EOpRoundEven, genType, genType);
896 insertBuiltInOp(COMMON_BUILTINS, EOpCeil, genType, genType);
897 insertBuiltInOp(COMMON_BUILTINS, EOpFract, genType, genType);
898 insertBuiltInOp(COMMON_BUILTINS, EOpMod, genType, genType, float1);
899 insertBuiltInOp(COMMON_BUILTINS, EOpMod, genType, genType, genType);
900 insertBuiltInOp(COMMON_BUILTINS, EOpMin, genType, genType, float1);
901 insertBuiltInOp(COMMON_BUILTINS, EOpMin, genType, genType, genType);
902 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genIType, genIType, genIType);
903 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genIType, genIType, int1);
904 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genUType, genUType, genUType);
905 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genUType, genUType, uint1);
906 insertBuiltInOp(COMMON_BUILTINS, EOpMax, genType, genType, float1);
907 insertBuiltInOp(COMMON_BUILTINS, EOpMax, genType, genType, genType);
908 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genIType, genIType, genIType);
909 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genIType, genIType, int1);
910 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genUType, genUType, genUType);
911 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genUType, genUType, uint1);
912 insertBuiltInOp(COMMON_BUILTINS, EOpClamp, genType, genType, float1, float1);
913 insertBuiltInOp(COMMON_BUILTINS, EOpClamp, genType, genType, genType, genType);
914 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genIType, genIType, int1, int1);
915 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genIType, genIType, genIType, genIType);
916 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genUType, genUType, uint1, uint1);
917 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genUType, genUType, genUType, genUType);
918 insertBuiltInOp(COMMON_BUILTINS, EOpMix, genType, genType, genType, float1);
919 insertBuiltInOp(COMMON_BUILTINS, EOpMix, genType, genType, genType, genType);
920 insertBuiltInOp(ESSL3_BUILTINS, EOpMix, genType, genType, genType, genBType);
921 insertBuiltInOp(COMMON_BUILTINS, EOpStep, genType, genType, genType);
922 insertBuiltInOp(COMMON_BUILTINS, EOpStep, genType, float1, genType);
923 insertBuiltInOp(COMMON_BUILTINS, EOpSmoothStep, genType, genType, genType, genType);
924 insertBuiltInOp(COMMON_BUILTINS, EOpSmoothStep, genType, float1, float1, genType);
925
926 const TType *outGenType = StaticType::GetQualified<EbtGenType, EvqOut>();
927 const TType *outGenIType = StaticType::GetQualified<EbtGenIType, EvqOut>();
928
929 insertBuiltInOp(ESSL3_BUILTINS, EOpModf, genType, genType, outGenType);
930
931 insertBuiltInOp(ESSL3_BUILTINS, EOpIsNan, genBType, genType);
932 insertBuiltInOp(ESSL3_BUILTINS, EOpIsInf, genBType, genType);
933 insertBuiltInOp(ESSL3_BUILTINS, EOpFloatBitsToInt, genIType, genType);
934 insertBuiltInOp(ESSL3_BUILTINS, EOpFloatBitsToUint, genUType, genType);
935 insertBuiltInOp(ESSL3_BUILTINS, EOpIntBitsToFloat, genType, genIType);
936 insertBuiltInOp(ESSL3_BUILTINS, EOpUintBitsToFloat, genType, genUType);
937
938 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFrexp, genType, genType, outGenIType);
939 insertBuiltInOp(ESSL3_1_BUILTINS, EOpLdexp, genType, genType, genIType);
940
941 insertBuiltInOp(ESSL3_BUILTINS, EOpPackSnorm2x16, uint1, float2);
942 insertBuiltInOp(ESSL3_BUILTINS, EOpPackUnorm2x16, uint1, float2);
943 insertBuiltInOp(ESSL3_BUILTINS, EOpPackHalf2x16, uint1, float2);
944 insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackSnorm2x16, float2, uint1);
945 insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackUnorm2x16, float2, uint1);
946 insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackHalf2x16, float2, uint1);
947
948 insertBuiltInOp(ESSL3_1_BUILTINS, EOpPackUnorm4x8, uint1, float4);
949 insertBuiltInOp(ESSL3_1_BUILTINS, EOpPackSnorm4x8, uint1, float4);
950 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUnpackUnorm4x8, float4, uint1);
951 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUnpackSnorm4x8, float4, uint1);
952
953 //
954 // Geometric Functions.
955 //
956 insertBuiltInOp(COMMON_BUILTINS, EOpLength, float1, genType);
957 insertBuiltInOp(COMMON_BUILTINS, EOpDistance, float1, genType, genType);
958 insertBuiltInOp(COMMON_BUILTINS, EOpDot, float1, genType, genType);
959 insertBuiltInOp(COMMON_BUILTINS, EOpCross, float3, float3, float3);
960 insertBuiltInOp(COMMON_BUILTINS, EOpNormalize, genType, genType);
961 insertBuiltInOp(COMMON_BUILTINS, EOpFaceforward, genType, genType, genType, genType);
962 insertBuiltInOp(COMMON_BUILTINS, EOpReflect, genType, genType, genType);
963 insertBuiltInOp(COMMON_BUILTINS, EOpRefract, genType, genType, genType, float1);
964
965 const TType *mat2 = StaticType::GetBasic<EbtFloat, 2, 2>();
966 const TType *mat3 = StaticType::GetBasic<EbtFloat, 3, 3>();
967 const TType *mat4 = StaticType::GetBasic<EbtFloat, 4, 4>();
968 const TType *mat2x3 = StaticType::GetBasic<EbtFloat, 2, 3>();
969 const TType *mat3x2 = StaticType::GetBasic<EbtFloat, 3, 2>();
970 const TType *mat2x4 = StaticType::GetBasic<EbtFloat, 2, 4>();
971 const TType *mat4x2 = StaticType::GetBasic<EbtFloat, 4, 2>();
972 const TType *mat3x4 = StaticType::GetBasic<EbtFloat, 3, 4>();
973 const TType *mat4x3 = StaticType::GetBasic<EbtFloat, 4, 3>();
974
975 //
976 // Matrix Functions.
977 //
978 insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat2, mat2, mat2);
979 insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat3, mat3, mat3);
980 insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat4, mat4, mat4);
981 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat2x3, mat2x3, mat2x3);
982 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat3x2, mat3x2, mat3x2);
983 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat2x4, mat2x4, mat2x4);
984 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat4x2, mat4x2, mat4x2);
985 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat3x4, mat3x4, mat3x4);
986 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat4x3, mat4x3, mat4x3);
987
988 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2, float2, float2);
989 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3, float3, float3);
990 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4, float4, float4);
991 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2x3, float3, float2);
992 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3x2, float2, float3);
993 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2x4, float4, float2);
994 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4x2, float2, float4);
995 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3x4, float4, float3);
996 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4x3, float3, float4);
997
998 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2, mat2);
999 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3, mat3);
1000 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4, mat4);
1001 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2x3, mat3x2);
1002 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3x2, mat2x3);
1003 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2x4, mat4x2);
1004 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4x2, mat2x4);
1005 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3x4, mat4x3);
1006 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4x3, mat3x4);
1007
1008 insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat2);
1009 insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat3);
1010 insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat4);
1011
1012 insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat2, mat2);
1013 insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat3, mat3);
1014 insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat4, mat4);
1015
1016 const TType *vec = StaticType::GetBasic<EbtVec>();
1017 const TType *ivec = StaticType::GetBasic<EbtIVec>();
1018 const TType *uvec = StaticType::GetBasic<EbtUVec>();
1019 const TType *bvec = StaticType::GetBasic<EbtBVec>();
1020
1021 //
1022 // Vector relational functions.
1023 //
1024 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanComponentWise, bvec, vec, vec);
1025 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanComponentWise, bvec, ivec, ivec);
1026 insertBuiltInOp(ESSL3_BUILTINS, EOpLessThanComponentWise, bvec, uvec, uvec);
1027 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanEqualComponentWise, bvec, vec, vec);
1028 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanEqualComponentWise, bvec, ivec, ivec);
1029 insertBuiltInOp(ESSL3_BUILTINS, EOpLessThanEqualComponentWise, bvec, uvec, uvec);
1030 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanComponentWise, bvec, vec, vec);
1031 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanComponentWise, bvec, ivec, ivec);
1032 insertBuiltInOp(ESSL3_BUILTINS, EOpGreaterThanComponentWise, bvec, uvec, uvec);
1033 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, vec, vec);
1034 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, ivec, ivec);
1035 insertBuiltInOp(ESSL3_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, uvec, uvec);
1036 insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, vec, vec);
1037 insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, ivec, ivec);
1038 insertBuiltInOp(ESSL3_BUILTINS, EOpEqualComponentWise, bvec, uvec, uvec);
1039 insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, bvec, bvec);
1040 insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, vec, vec);
1041 insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, ivec, ivec);
1042 insertBuiltInOp(ESSL3_BUILTINS, EOpNotEqualComponentWise, bvec, uvec, uvec);
1043 insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, bvec, bvec);
1044 insertBuiltInOp(COMMON_BUILTINS, EOpAny, bool1, bvec);
1045 insertBuiltInOp(COMMON_BUILTINS, EOpAll, bool1, bvec);
1046 insertBuiltInOp(COMMON_BUILTINS, EOpLogicalNotComponentWise, bvec, bvec);
1047
1048 //
1049 // Integer functions
1050 //
1051 const TType *outGenUType = StaticType::GetQualified<EbtGenUType, EvqOut>();
1052
1053 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldExtract, genIType, genIType, int1, int1);
1054 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldExtract, genUType, genUType, int1, int1);
1055 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldInsert, genIType, genIType, genIType, int1, int1);
1056 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldInsert, genUType, genUType, genUType, int1, int1);
1057 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldReverse, genIType, genIType);
1058 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldReverse, genUType, genUType);
1059 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitCount, genIType, genIType);
1060 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitCount, genIType, genUType);
1061 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindLSB, genIType, genIType);
1062 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindLSB, genIType, genUType);
1063 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindMSB, genIType, genIType);
1064 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindMSB, genIType, genUType);
1065 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUaddCarry, genUType, genUType, genUType, outGenUType);
1066 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUsubBorrow, genUType, genUType, genUType, outGenUType);
1067 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUmulExtended, voidType, genUType, genUType, outGenUType,
1068 outGenUType);
1069 insertBuiltInOp(ESSL3_1_BUILTINS, EOpImulExtended, voidType, genIType, genIType, outGenIType,
1070 outGenIType);
1071
1072 const TType *sampler2D = StaticType::GetBasic<EbtSampler2D>();
1073 const TType *samplerCube = StaticType::GetBasic<EbtSamplerCube>();
1074
1075 //
1076 // Texture Functions for GLSL ES 1.0
1077 //
1078 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", sampler2D, float2);
1079 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float3);
1080 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float4);
1081 insertBuiltIn(ESSL1_BUILTINS, float4, "textureCube", samplerCube, float3);
1082
1083 if (resources.OES_EGL_image_external || resources.NV_EGL_stream_consumer_external)
1084 {
1085 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1086
1087 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", samplerExternalOES, float2);
1088 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", samplerExternalOES, float3);
1089 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", samplerExternalOES, float4);
1090 }
1091
1092 if (resources.ARB_texture_rectangle)
1093 {
1094 const TType *sampler2DRect = StaticType::GetBasic<EbtSampler2DRect>();
1095
1096 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DRect", sampler2DRect, float2);
1097 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DRectProj", sampler2DRect, float3);
1098 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DRectProj", sampler2DRect, float4);
1099 }
1100
1101 if (resources.EXT_shader_texture_lod)
1102 {
1103 /* The *Grad* variants are new to both vertex and fragment shaders; the fragment
1104 * shader specific pieces are added separately below.
1105 */
1106 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1107 "texture2DGradEXT", sampler2D, float2, float2, float2);
1108 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1109 "texture2DProjGradEXT", sampler2D, float3, float2, float2);
1110 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1111 "texture2DProjGradEXT", sampler2D, float4, float2, float2);
1112 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1113 "textureCubeGradEXT", samplerCube, float3, float3, float3);
1114 }
1115
1116 if (type == GL_FRAGMENT_SHADER)
1117 {
1118 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", sampler2D, float2, float1);
1119 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float3, float1);
1120 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float4, float1);
1121 insertBuiltIn(ESSL1_BUILTINS, float4, "textureCube", samplerCube, float3, float1);
1122
1123 if (resources.OES_standard_derivatives)
1124 {
1125 insertBuiltInOp(ESSL1_BUILTINS, EOpDFdx, TExtension::OES_standard_derivatives, genType,
1126 genType);
1127 insertBuiltInOp(ESSL1_BUILTINS, EOpDFdy, TExtension::OES_standard_derivatives, genType,
1128 genType);
1129 insertBuiltInOp(ESSL1_BUILTINS, EOpFwidth, TExtension::OES_standard_derivatives,
1130 genType, genType);
1131 }
1132
1133 if (resources.EXT_shader_texture_lod)
1134 {
1135 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1136 "texture2DLodEXT", sampler2D, float2, float1);
1137 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1138 "texture2DProjLodEXT", sampler2D, float3, float1);
1139 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1140 "texture2DProjLodEXT", sampler2D, float4, float1);
1141 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1142 "textureCubeLodEXT", samplerCube, float3, float1);
1143 }
1144 }
1145
1146 if (type == GL_VERTEX_SHADER)
1147 {
1148 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DLod", sampler2D, float2, float1);
1149 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProjLod", sampler2D, float3, float1);
1150 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProjLod", sampler2D, float4, float1);
1151 insertBuiltIn(ESSL1_BUILTINS, float4, "textureCubeLod", samplerCube, float3, float1);
1152 }
1153
1154 const TType *gvec4 = StaticType::GetBasic<EbtGVec4>();
1155
1156 const TType *gsampler2D = StaticType::GetBasic<EbtGSampler2D>();
1157 const TType *gsamplerCube = StaticType::GetBasic<EbtGSamplerCube>();
1158 const TType *gsampler3D = StaticType::GetBasic<EbtGSampler3D>();
1159 const TType *gsampler2DArray = StaticType::GetBasic<EbtGSampler2DArray>();
1160 const TType *gsampler2DMS = StaticType::GetBasic<EbtGSampler2DMS>();
1161
1162 //
1163 // Texture Functions for GLSL ES 3.0
1164 //
1165 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2D, float2);
1166 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler3D, float3);
1167 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsamplerCube, float3);
1168 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2DArray, float3);
1169 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float3);
1170 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float4);
1171 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler3D, float4);
1172 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler2D, float2, float1);
1173 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler3D, float3, float1);
1174 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsamplerCube, float3, float1);
1175 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler2DArray, float3, float1);
1176
1177 if (resources.OES_EGL_image_external_essl3)
1178 {
1179 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1180
1181 insertBuiltIn(ESSL3_BUILTINS, float4, "texture", samplerExternalOES, float2);
1182 insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES, float3);
1183 insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES, float4);
1184 }
1185
1186 if (resources.EXT_YUV_target)
1187 {
1188 const TType *samplerExternal2DY2YEXT = StaticType::GetBasic<EbtSamplerExternal2DY2YEXT>();
1189
1190 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texture",
1191 samplerExternal2DY2YEXT, float2);
1192 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1193 samplerExternal2DY2YEXT, float3);
1194 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1195 samplerExternal2DY2YEXT, float4);
1196
1197 const TType *yuvCscStandardEXT = StaticType::GetBasic<EbtYuvCscStandardEXT>();
1198
1199 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float3, "rgb_2_yuv", float3,
1200 yuvCscStandardEXT);
1201 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float3, "yuv_2_rgb", float3,
1202 yuvCscStandardEXT);
1203 }
1204
1205 if (type == GL_FRAGMENT_SHADER)
1206 {
1207 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2D, float2, float1);
1208 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler3D, float3, float1);
1209 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsamplerCube, float3, float1);
1210 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2DArray, float3, float1);
1211 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float3, float1);
1212 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float4, float1);
1213 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler3D, float4, float1);
1214
1215 if (resources.OES_EGL_image_external_essl3)
1216 {
1217 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1218
1219 insertBuiltIn(ESSL3_BUILTINS, float4, "texture", samplerExternalOES, float2, float1);
1220 insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES, float3,
1221 float1);
1222 insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES, float4,
1223 float1);
1224 }
1225
1226 if (resources.EXT_YUV_target)
1227 {
1228 const TType *samplerExternal2DY2YEXT =
1229 StaticType::GetBasic<EbtSamplerExternal2DY2YEXT>();
1230
1231 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texture",
1232 samplerExternal2DY2YEXT, float2, float1);
1233 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1234 samplerExternal2DY2YEXT, float3, float1);
1235 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1236 samplerExternal2DY2YEXT, float4, float1);
1237 }
1238 }
1239
1240 const TType *sampler2DShadow = StaticType::GetBasic<EbtSampler2DShadow>();
1241 const TType *samplerCubeShadow = StaticType::GetBasic<EbtSamplerCubeShadow>();
1242 const TType *sampler2DArrayShadow = StaticType::GetBasic<EbtSampler2DArrayShadow>();
1243
1244 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DShadow, float3);
1245 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", samplerCubeShadow, float4);
1246 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DArrayShadow, float4);
1247 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProj", sampler2DShadow, float4);
1248 insertBuiltIn(ESSL3_BUILTINS, float1, "textureLod", sampler2DShadow, float3, float1);
1249
1250 if (type == GL_FRAGMENT_SHADER)
1251 {
1252 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DShadow, float3, float1);
1253 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", samplerCubeShadow, float4, float1);
1254 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProj", sampler2DShadow, float4, float1);
1255 }
1256
1257 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsampler2D, int1);
1258 insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", gsampler3D, int1);
1259 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsamplerCube, int1);
1260 insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", gsampler2DArray, int1);
1261 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", sampler2DShadow, int1);
1262 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", samplerCubeShadow, int1);
1263 insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", sampler2DArrayShadow, int1);
1264 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsampler2DMS);
1265
1266 if (resources.OES_EGL_image_external_essl3)
1267 {
1268 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1269
1270 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", samplerExternalOES, int1);
1271 }
1272
1273 if (resources.EXT_YUV_target)
1274 {
1275 const TType *samplerExternal2DY2YEXT = StaticType::GetBasic<EbtSamplerExternal2DY2YEXT>();
1276
1277 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, int2, "textureSize",
1278 samplerExternal2DY2YEXT, int1);
1279 }
1280
1281 if (type == GL_FRAGMENT_SHADER)
1282 {
1283 insertBuiltInOp(ESSL3_BUILTINS, EOpDFdx, genType, genType);
1284 insertBuiltInOp(ESSL3_BUILTINS, EOpDFdy, genType, genType);
1285 insertBuiltInOp(ESSL3_BUILTINS, EOpFwidth, genType, genType);
1286 }
1287
1288 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2D, float2, int2);
1289 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler3D, float3, int3);
1290 insertBuiltIn(ESSL3_BUILTINS, float1, "textureOffset", sampler2DShadow, float3, int2);
1291 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2DArray, float3, int2);
1292
1293 if (type == GL_FRAGMENT_SHADER)
1294 {
1295 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2D, float2, int2, float1);
1296 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler3D, float3, int3, float1);
1297 insertBuiltIn(ESSL3_BUILTINS, float1, "textureOffset", sampler2DShadow, float3, int2,
1298 float1);
1299 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2DArray, float3, int2,
1300 float1);
1301 }
1302
1303 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float3, int2);
1304 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float4, int2);
1305 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler3D, float4, int3);
1306 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjOffset", sampler2DShadow, float4, int2);
1307
1308 if (type == GL_FRAGMENT_SHADER)
1309 {
1310 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float3, int2, float1);
1311 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float4, int2, float1);
1312 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler3D, float4, int3, float1);
1313 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjOffset", sampler2DShadow, float4, int2,
1314 float1);
1315 }
1316
1317 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler2D, float2, float1, int2);
1318 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler3D, float3, float1, int3);
1319 insertBuiltIn(ESSL3_BUILTINS, float1, "textureLodOffset", sampler2DShadow, float3, float1,
1320 int2);
1321 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler2DArray, float3, float1, int2);
1322
1323 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler2D, float3, float1);
1324 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler2D, float4, float1);
1325 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler3D, float4, float1);
1326 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjLod", sampler2DShadow, float4, float1);
1327
1328 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler2D, float3, float1, int2);
1329 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler2D, float4, float1, int2);
1330 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler3D, float4, float1, int3);
1331 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjLodOffset", sampler2DShadow, float4, float1,
1332 int2);
1333
1334 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler2D, int2, int1);
1335 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler3D, int3, int1);
1336 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler2DArray, int3, int1);
1337
1338 if (resources.OES_EGL_image_external_essl3)
1339 {
1340 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1341
1342 insertBuiltIn(ESSL3_BUILTINS, float4, "texelFetch", samplerExternalOES, int2, int1);
1343 }
1344
1345 if (resources.EXT_YUV_target)
1346 {
1347 const TType *samplerExternal2DY2YEXT = StaticType::GetBasic<EbtSamplerExternal2DY2YEXT>();
1348
1349 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texelFetch",
1350 samplerExternal2DY2YEXT, int2, int1);
1351 }
1352
1353 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler2D, int2, int1, int2);
1354 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler3D, int3, int1, int3);
1355 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler2DArray, int3, int1, int2);
1356
1357 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler2D, float2, float2, float2);
1358 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler3D, float3, float3, float3);
1359 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsamplerCube, float3, float3, float3);
1360 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", sampler2DShadow, float3, float2, float2);
1361 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", samplerCubeShadow, float4, float3, float3);
1362 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler2DArray, float3, float2, float2);
1363 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", sampler2DArrayShadow, float4, float2,
1364 float2);
1365
1366 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler2D, float2, float2, float2,
1367 int2);
1368 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler3D, float3, float3, float3,
1369 int3);
1370 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGradOffset", sampler2DShadow, float3, float2,
1371 float2, int2);
1372 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler2DArray, float3, float2,
1373 float2, int2);
1374 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGradOffset", sampler2DArrayShadow, float4, float2,
1375 float2, int2);
1376
1377 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler2D, float3, float2, float2);
1378 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler2D, float4, float2, float2);
1379 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler3D, float4, float3, float3);
1380 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjGrad", sampler2DShadow, float4, float2,
1381 float2);
1382
1383 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler2D, float3, float2,
1384 float2, int2);
1385 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler2D, float4, float2,
1386 float2, int2);
1387 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler3D, float4, float3,
1388 float3, int3);
1389 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjGradOffset", sampler2DShadow, float4, float2,
1390 float2, int2);
1391
1392 const TType *atomicCounter = StaticType::GetBasic<EbtAtomicCounter>();
1393 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounter", atomicCounter);
1394 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounterIncrement", atomicCounter);
1395 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounterDecrement", atomicCounter);
1396
1397 // Insert all atomic memory functions
1398 const TType *int1InOut = StaticType::GetQualified<EbtInt, EvqInOut>();
1399 const TType *uint1InOut = StaticType::GetQualified<EbtUInt, EvqInOut>();
1400 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicAdd", uint1InOut, uint1);
1401 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicAdd", int1InOut, int1);
1402 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicMin", uint1InOut, uint1);
1403 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicMin", int1InOut, int1);
1404 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicMax", uint1InOut, uint1);
1405 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicMax", int1InOut, int1);
1406 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicAnd", uint1InOut, uint1);
1407 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicAnd", int1InOut, int1);
1408 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicOr", uint1InOut, uint1);
1409 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicOr", int1InOut, int1);
1410 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicXor", uint1InOut, uint1);
1411 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicXor", int1InOut, int1);
1412 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicExchange", uint1InOut, uint1);
1413 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicExchange", int1InOut, int1);
1414 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCompSwap", uint1InOut, uint1, uint1);
1415 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicCompSwap", int1InOut, int1, int1);
1416
1417 const TType *gimage2D = StaticType::GetBasic<EbtGImage2D>();
1418 const TType *gimage3D = StaticType::GetBasic<EbtGImage3D>();
1419 const TType *gimage2DArray = StaticType::GetBasic<EbtGImage2DArray>();
1420 const TType *gimageCube = StaticType::GetBasic<EbtGImageCube>();
1421
1422 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage2D, int2, gvec4);
1423 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage3D, int3, gvec4);
1424 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage2DArray, int3, gvec4);
1425 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimageCube, int3, gvec4);
1426
1427 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage2D, int2);
1428 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage3D, int3);
1429 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage2DArray, int3);
1430 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimageCube, int3);
1431
1432 insertBuiltIn(ESSL3_1_BUILTINS, int2, "imageSize", gimage2D);
1433 insertBuiltIn(ESSL3_1_BUILTINS, int3, "imageSize", gimage3D);
1434 insertBuiltIn(ESSL3_1_BUILTINS, int3, "imageSize", gimage2DArray);
1435 insertBuiltIn(ESSL3_1_BUILTINS, int2, "imageSize", gimageCube);
1436
1437 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrier, voidType,
1438 "memoryBarrier");
1439 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierAtomicCounter, voidType,
1440 "memoryBarrierAtomicCounter");
1441 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierBuffer, voidType,
1442 "memoryBarrierBuffer");
1443 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierImage, voidType,
1444 "memoryBarrierImage");
1445
1446 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "texelFetch", gsampler2DMS, int2, int1);
1447
1448 // Insert all variations of textureGather.
1449 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2D, float2);
1450 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2D, float2, int1);
1451 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2DArray, float3);
1452 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2DArray, float3, int1);
1453 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsamplerCube, float3);
1454 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsamplerCube, float3, int1);
1455 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DShadow, float2);
1456 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DShadow, float2, float1);
1457 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DArrayShadow, float3);
1458 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DArrayShadow, float3, float1);
1459 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", samplerCubeShadow, float3);
1460 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", samplerCubeShadow, float3, float1);
1461
1462 // Insert all variations of textureGatherOffset.
1463 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2D, float2, int2);
1464 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2D, float2, int2, int1);
1465 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2DArray, float3, int2);
1466 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2DArray, float3, int2,
1467 int1);
1468 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGatherOffset", sampler2DShadow, float2, float1,
1469 int2);
1470 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGatherOffset", sampler2DArrayShadow, float3,
1471 float1, int2);
1472
1473 if (type == GL_COMPUTE_SHADER)
1474 {
1475 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpBarrier, voidType, "barrier");
1476 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierShared, voidType,
1477 "memoryBarrierShared");
1478 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpGroupMemoryBarrier, voidType,
1479 "groupMemoryBarrier");
1480 }
1481
1482 if (type == GL_GEOMETRY_SHADER_EXT)
1483 {
1484 TExtension extension = TExtension::EXT_geometry_shader;
1485 insertBuiltInFunctionNoParametersExt(ESSL3_1_BUILTINS, extension, EOpEmitVertex, voidType,
1486 "EmitVertex");
1487 insertBuiltInFunctionNoParametersExt(ESSL3_1_BUILTINS, extension, EOpEndPrimitive, voidType,
1488 "EndPrimitive");
1489 }
1490}
1491
1492void TSymbolTable::initializeBuiltInVariables(sh::GLenum type,
1493 ShShaderSpec spec,
1494 const ShBuiltInResources &resources)
1495{
1496 const TSourceLoc zeroSourceLoc = {0, 0, 0, 0};
1497
1498 //
1499 // Depth range in window coordinates
1500 //
1501 TFieldList *fields = new TFieldList();
1502 auto highpFloat1 = new TType(EbtFloat, EbpHigh, EvqGlobal, 1);
1503 TField *near = new TField(highpFloat1, ImmutableString("near"), zeroSourceLoc);
1504 TField *far = new TField(highpFloat1, ImmutableString("far"), zeroSourceLoc);
1505 TField *diff = new TField(highpFloat1, ImmutableString("diff"), zeroSourceLoc);
1506 fields->push_back(near);
1507 fields->push_back(far);
1508 fields->push_back(diff);
1509 TStructure *depthRangeStruct = new TStructure(this, ImmutableString("gl_DepthRangeParameters"),
1510 fields, SymbolType::BuiltIn);
1511 insertStructType(COMMON_BUILTINS, depthRangeStruct);
1512 TType *depthRangeType = new TType(depthRangeStruct);
1513 depthRangeType->setQualifier(EvqUniform);
1514 depthRangeType->realize();
1515 insertVariable(COMMON_BUILTINS, ImmutableString("gl_DepthRange"), depthRangeType);
1516
1517 //
1518 // Implementation dependent built-in constants.
1519 //
1520 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexAttribs"),
1521 resources.MaxVertexAttribs);
1522 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexUniformVectors"),
1523 resources.MaxVertexUniformVectors);
1524 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexTextureImageUnits"),
1525 resources.MaxVertexTextureImageUnits);
1526 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxCombinedTextureImageUnits"),
1527 resources.MaxCombinedTextureImageUnits);
1528 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxTextureImageUnits"),
1529 resources.MaxTextureImageUnits);
1530 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxFragmentUniformVectors"),
1531 resources.MaxFragmentUniformVectors);
1532
1533 insertConstInt<EbpMedium>(ESSL1_BUILTINS, ImmutableString("gl_MaxVaryingVectors"),
1534 resources.MaxVaryingVectors);
1535
1536 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxDrawBuffers"),
1537 resources.MaxDrawBuffers);
1538 if (resources.EXT_blend_func_extended)
1539 {
1540 insertConstIntExt<EbpMedium>(COMMON_BUILTINS, TExtension::EXT_blend_func_extended,
1541 ImmutableString("gl_MaxDualSourceDrawBuffersEXT"),
1542 resources.MaxDualSourceDrawBuffers);
1543 }
1544
1545 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxVertexOutputVectors"),
1546 resources.MaxVertexOutputVectors);
1547 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxFragmentInputVectors"),
1548 resources.MaxFragmentInputVectors);
1549 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MinProgramTexelOffset"),
1550 resources.MinProgramTexelOffset);
1551 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxProgramTexelOffset"),
1552 resources.MaxProgramTexelOffset);
1553
1554 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxImageUnits"),
1555 resources.MaxImageUnits);
1556 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexImageUniforms"),
1557 resources.MaxVertexImageUniforms);
1558 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxFragmentImageUniforms"),
1559 resources.MaxFragmentImageUniforms);
1560 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeImageUniforms"),
1561 resources.MaxComputeImageUniforms);
1562 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxCombinedImageUniforms"),
1563 resources.MaxCombinedImageUniforms);
1564
1565 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1566 ImmutableString("gl_MaxCombinedShaderOutputResources"),
1567 resources.MaxCombinedShaderOutputResources);
1568
1569 insertConstIvec3<EbpHigh>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeWorkGroupCount"),
1570 resources.MaxComputeWorkGroupCount);
1571 insertConstIvec3<EbpHigh>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeWorkGroupSize"),
1572 resources.MaxComputeWorkGroupSize);
1573 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeUniformComponents"),
1574 resources.MaxComputeUniformComponents);
1575 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeTextureImageUnits"),
1576 resources.MaxComputeTextureImageUnits);
1577
1578 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeAtomicCounters"),
1579 resources.MaxComputeAtomicCounters);
1580 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1581 ImmutableString("gl_MaxComputeAtomicCounterBuffers"),
1582 resources.MaxComputeAtomicCounterBuffers);
1583
1584 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexAtomicCounters"),
1585 resources.MaxVertexAtomicCounters);
1586 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxFragmentAtomicCounters"),
1587 resources.MaxFragmentAtomicCounters);
1588 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxCombinedAtomicCounters"),
1589 resources.MaxCombinedAtomicCounters);
1590 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxAtomicCounterBindings"),
1591 resources.MaxAtomicCounterBindings);
1592
1593 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexAtomicCounterBuffers"),
1594 resources.MaxVertexAtomicCounterBuffers);
1595 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1596 ImmutableString("gl_MaxFragmentAtomicCounterBuffers"),
1597 resources.MaxFragmentAtomicCounterBuffers);
1598 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1599 ImmutableString("gl_MaxCombinedAtomicCounterBuffers"),
1600 resources.MaxCombinedAtomicCounterBuffers);
1601 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxAtomicCounterBufferSize"),
1602 resources.MaxAtomicCounterBufferSize);
1603
1604 if (resources.EXT_geometry_shader)
1605 {
1606 TExtension ext = TExtension::EXT_geometry_shader;
1607 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1608 ImmutableString("gl_MaxGeometryInputComponents"),
1609 resources.MaxGeometryInputComponents);
1610 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1611 ImmutableString("gl_MaxGeometryOutputComponents"),
1612 resources.MaxGeometryOutputComponents);
1613 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1614 ImmutableString("gl_MaxGeometryImageUniforms"),
1615 resources.MaxGeometryImageUniforms);
1616 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1617 ImmutableString("gl_MaxGeometryTextureImageUnits"),
1618 resources.MaxGeometryTextureImageUnits);
1619 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1620 ImmutableString("gl_MaxGeometryOutputVertices"),
1621 resources.MaxGeometryOutputVertices);
1622 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1623 ImmutableString("gl_MaxGeometryTotalOutputComponents"),
1624 resources.MaxGeometryTotalOutputComponents);
1625 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1626 ImmutableString("gl_MaxGeometryUniformComponents"),
1627 resources.MaxGeometryUniformComponents);
1628 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1629 ImmutableString("gl_MaxGeometryAtomicCounters"),
1630 resources.MaxGeometryAtomicCounters);
1631 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1632 ImmutableString("gl_MaxGeometryAtomicCounterBuffers"),
1633 resources.MaxGeometryAtomicCounterBuffers);
1634 }
1635
1636 //
1637 // Insert some special built-in variables that are not in
1638 // the built-in header files.
1639 //
1640
1641 if (resources.OVR_multiview && type != GL_COMPUTE_SHADER)
1642 {
1643 const TType *viewIDType = StaticType::Get<EbtUInt, EbpHigh, EvqViewIDOVR, 1, 1>();
1644 insertVariableExt(ESSL3_BUILTINS, TExtension::OVR_multiview,
1645 ImmutableString("gl_ViewID_OVR"), viewIDType);
1646
1647 // ESSL 1.00 doesn't have unsigned integers, so gl_ViewID_OVR is a signed integer in ESSL
1648 // 1.00. This is specified in the WEBGL_multiview spec.
1649 const TType *viewIDIntType = StaticType::Get<EbtInt, EbpHigh, EvqViewIDOVR, 1, 1>();
1650 insertVariableExt(ESSL1_BUILTINS, TExtension::OVR_multiview,
1651 ImmutableString("gl_ViewID_OVR"), viewIDIntType);
1652 }
1653
1654 const TType *positionType = StaticType::Get<EbtFloat, EbpHigh, EvqPosition, 4, 1>();
1655 const TType *primitiveIDType = StaticType::Get<EbtInt, EbpHigh, EvqPrimitiveID, 1, 1>();
1656 const TType *layerType = StaticType::Get<EbtInt, EbpHigh, EvqLayer, 1, 1>();
1657
1658 switch (type)
1659 {
1660 case GL_FRAGMENT_SHADER:
1661 {
1662 const TType *fragCoordType = StaticType::Get<EbtFloat, EbpMedium, EvqFragCoord, 4, 1>();
1663 insertVariable(COMMON_BUILTINS, ImmutableString("gl_FragCoord"), fragCoordType);
1664 const TType *frontFacingType = StaticType::GetQualified<EbtBool, EvqFrontFacing>();
1665 insertVariable(COMMON_BUILTINS, ImmutableString("gl_FrontFacing"), frontFacingType);
1666 const TType *pointCoordType =
1667 StaticType::Get<EbtFloat, EbpMedium, EvqPointCoord, 2, 1>();
1668 insertVariable(COMMON_BUILTINS, ImmutableString("gl_PointCoord"), pointCoordType);
1669
1670 const TType *fragColorType = StaticType::Get<EbtFloat, EbpMedium, EvqFragColor, 4, 1>();
1671 insertVariable(ESSL1_BUILTINS, ImmutableString("gl_FragColor"), fragColorType);
1672
1673 TType *fragDataType = new TType(EbtFloat, EbpMedium, EvqFragData, 4);
1674 if (spec != SH_WEBGL2_SPEC && spec != SH_WEBGL3_SPEC)
1675 {
1676 fragDataType->makeArray(resources.MaxDrawBuffers);
1677 }
1678 else
1679 {
1680 fragDataType->makeArray(1u);
1681 }
1682 fragDataType->realize();
1683 insertVariable(ESSL1_BUILTINS, ImmutableString("gl_FragData"), fragDataType);
1684
1685 if (resources.EXT_blend_func_extended)
1686 {
1687 const TType *secondaryFragColorType =
1688 StaticType::Get<EbtFloat, EbpMedium, EvqSecondaryFragColorEXT, 4, 1>();
1689 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_blend_func_extended,
1690 ImmutableString("gl_SecondaryFragColorEXT"),
1691 secondaryFragColorType);
1692 TType *secondaryFragDataType =
1693 new TType(EbtFloat, EbpMedium, EvqSecondaryFragDataEXT, 4, 1);
1694 secondaryFragDataType->makeArray(resources.MaxDualSourceDrawBuffers);
1695 secondaryFragDataType->realize();
1696 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_blend_func_extended,
1697 ImmutableString("gl_SecondaryFragDataEXT"),
1698 secondaryFragDataType);
1699 }
1700
1701 if (resources.EXT_frag_depth)
1702 {
1703 TType *fragDepthEXTType =
1704 new TType(EbtFloat, resources.FragmentPrecisionHigh ? EbpHigh : EbpMedium,
1705 EvqFragDepthEXT, 1);
1706 fragDepthEXTType->realize();
1707 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_frag_depth,
1708 ImmutableString("gl_FragDepthEXT"), fragDepthEXTType);
1709 }
1710
1711 const TType *fragDepthType = StaticType::Get<EbtFloat, EbpHigh, EvqFragDepth, 1, 1>();
1712 insertVariable(ESSL3_BUILTINS, ImmutableString("gl_FragDepth"), fragDepthType);
1713
1714 const TType *lastFragColorType =
1715 StaticType::Get<EbtFloat, EbpMedium, EvqLastFragColor, 4, 1>();
1716
1717 if (resources.EXT_shader_framebuffer_fetch || resources.NV_shader_framebuffer_fetch)
1718 {
1719 TType *lastFragDataType = new TType(EbtFloat, EbpMedium, EvqLastFragData, 4, 1);
1720 lastFragDataType->makeArray(resources.MaxDrawBuffers);
1721 lastFragDataType->realize();
1722
1723 if (resources.EXT_shader_framebuffer_fetch)
1724 {
1725 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_shader_framebuffer_fetch,
1726 ImmutableString("gl_LastFragData"), lastFragDataType);
1727 }
1728 else if (resources.NV_shader_framebuffer_fetch)
1729 {
1730 insertVariableExt(ESSL1_BUILTINS, TExtension::NV_shader_framebuffer_fetch,
1731 ImmutableString("gl_LastFragColor"), lastFragColorType);
1732 insertVariableExt(ESSL1_BUILTINS, TExtension::NV_shader_framebuffer_fetch,
1733 ImmutableString("gl_LastFragData"), lastFragDataType);
1734 }
1735 }
1736 else if (resources.ARM_shader_framebuffer_fetch)
1737 {
1738 insertVariableExt(ESSL1_BUILTINS, TExtension::ARM_shader_framebuffer_fetch,
1739 ImmutableString("gl_LastFragColorARM"), lastFragColorType);
1740 }
1741
1742 if (resources.EXT_geometry_shader)
1743 {
1744 TExtension extension = TExtension::EXT_geometry_shader;
1745 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveID"),
1746 primitiveIDType);
1747 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Layer"),
1748 layerType);
1749 }
1750
1751 break;
1752 }
1753 case GL_VERTEX_SHADER:
1754 {
1755 insertVariable(COMMON_BUILTINS, ImmutableString("gl_Position"), positionType);
1756 const TType *pointSizeType = StaticType::Get<EbtFloat, EbpMedium, EvqPointSize, 1, 1>();
1757 insertVariable(COMMON_BUILTINS, ImmutableString("gl_PointSize"), pointSizeType);
1758 const TType *instanceIDType = StaticType::Get<EbtInt, EbpHigh, EvqInstanceID, 1, 1>();
1759 insertVariable(ESSL3_BUILTINS, ImmutableString("gl_InstanceID"), instanceIDType);
1760 const TType *vertexIDType = StaticType::Get<EbtInt, EbpHigh, EvqVertexID, 1, 1>();
1761 insertVariable(ESSL3_BUILTINS, ImmutableString("gl_VertexID"), vertexIDType);
1762
1763 // For internal use by ANGLE - not exposed to the parser.
1764 const TType *viewportIndexType =
1765 StaticType::Get<EbtInt, EbpHigh, EvqViewportIndex, 1, 1>();
1766 insertVariable(GLSL_BUILTINS, ImmutableString("gl_ViewportIndex"), viewportIndexType);
1767 // gl_Layer exists in other shader stages in ESSL, but not in vertex shader so far.
1768 insertVariable(GLSL_BUILTINS, ImmutableString("gl_Layer"), layerType);
1769 break;
1770 }
1771 case GL_COMPUTE_SHADER:
1772 {
1773 const TType *numWorkGroupsType =
1774 StaticType::Get<EbtUInt, EbpUndefined, EvqNumWorkGroups, 3, 1>();
1775 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_NumWorkGroups"),
1776 numWorkGroupsType);
1777 const TType *workGroupSizeType =
1778 StaticType::Get<EbtUInt, EbpUndefined, EvqWorkGroupSize, 3, 1>();
1779 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_WorkGroupSize"),
1780 workGroupSizeType);
1781 const TType *workGroupIDType =
1782 StaticType::Get<EbtUInt, EbpUndefined, EvqWorkGroupID, 3, 1>();
1783 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_WorkGroupID"), workGroupIDType);
1784 const TType *localInvocationIDType =
1785 StaticType::Get<EbtUInt, EbpUndefined, EvqLocalInvocationID, 3, 1>();
1786 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_LocalInvocationID"),
1787 localInvocationIDType);
1788 const TType *globalInvocationIDType =
1789 StaticType::Get<EbtUInt, EbpUndefined, EvqGlobalInvocationID, 3, 1>();
1790 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_GlobalInvocationID"),
1791 globalInvocationIDType);
1792 const TType *localInvocationIndexType =
1793 StaticType::Get<EbtUInt, EbpUndefined, EvqLocalInvocationIndex, 1, 1>();
1794 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_LocalInvocationIndex"),
1795 localInvocationIndexType);
1796 break;
1797 }
1798
1799 case GL_GEOMETRY_SHADER_EXT:
1800 {
1801 TExtension extension = TExtension::EXT_geometry_shader;
1802
1803 // Add built-in interface block gl_PerVertex and the built-in array gl_in.
1804 // TODO(jiawei.shao@intel.com): implement GL_EXT_geometry_point_size.
1805 TFieldList *glPerVertexFieldList = new TFieldList();
1806 TField *glPositionField =
1807 new TField(new TType(*positionType), ImmutableString("gl_Position"), zeroSourceLoc);
1808 glPerVertexFieldList->push_back(glPositionField);
1809
1810 const ImmutableString glPerVertexString("gl_PerVertex");
1811 TInterfaceBlock *glPerVertexInBlock =
1812 new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList,
1813 TLayoutQualifier::Create(), SymbolType::BuiltIn, extension);
1814 insertInterfaceBlock(ESSL3_1_BUILTINS, glPerVertexInBlock);
1815
1816 // The array size of gl_in is undefined until we get a valid input primitive
1817 // declaration.
1818 TType *glInType =
1819 new TType(glPerVertexInBlock, EvqPerVertexIn, TLayoutQualifier::Create());
1820 glInType->makeArray(0u);
1821 glInType->realize();
1822 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_in"), glInType);
1823
1824 TInterfaceBlock *glPerVertexOutBlock =
1825 new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList,
1826 TLayoutQualifier::Create(), SymbolType::BuiltIn);
1827 TType *glPositionInBlockType = new TType(EbtFloat, EbpHigh, EvqPosition, 4);
1828 glPositionInBlockType->setInterfaceBlock(glPerVertexOutBlock);
1829 glPositionInBlockType->realize();
1830 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Position"),
1831 glPositionInBlockType);
1832
1833 const TType *primitiveIDInType =
1834 StaticType::Get<EbtInt, EbpHigh, EvqPrimitiveIDIn, 1, 1>();
1835 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveIDIn"),
1836 primitiveIDInType);
1837 const TType *invocationIDType =
1838 StaticType::Get<EbtInt, EbpHigh, EvqInvocationID, 1, 1>();
1839 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_InvocationID"),
1840 invocationIDType);
1841 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveID"),
1842 primitiveIDType);
1843 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Layer"), layerType);
1844
1845 break;
1846 }
1847 default:
1848 UNREACHABLE();
1849 }
1850}
1851
Jamie Madill45bcc782016-11-07 13:58:48 -05001852} // namespace sh