blob: 6b188c1c7142394f1dd79af0d55a96e164f97d3d [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.
35 bool insertUnmangled(TFunction *function);
36
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
68 // Insert a function using its unmangled name as the key.
69 bool insertUnmangled(const TFunction *function);
70
71 const TSymbol *find(const ImmutableString &name) const;
72
73 void insertUnmangledBuiltInName(const char *name);
74 bool hasUnmangledBuiltIn(const char *name) const;
75
76 private:
77 using tLevel = TUnorderedMap<ImmutableString,
78 const TSymbol *,
79 ImmutableString::FowlerNollVoHash<sizeof(size_t)>>;
80 using tLevelPair = const tLevel::value_type;
81 using tInsertResult = std::pair<tLevel::iterator, bool>;
82
83 tLevel mLevel;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020084
Olli Etuaho2d8e4322018-01-22 14:12:46 +020085 std::set<ImmutableString> mUnmangledBuiltInNames;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020086};
87
Olli Etuahodd21ecf2018-01-10 12:42:09 +020088bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040089{
Jamie Madillbfa91f42014-06-05 15:45:18 -040090 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040091 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040092 return result.second;
93}
94
Olli Etuahodd21ecf2018-01-10 12:42:09 +020095bool TSymbolTable::TSymbolTableLevel::insertUnmangled(TFunction *function)
Olli Etuahob2983c92015-03-18 14:02:46 +020096{
Olli Etuahob2983c92015-03-18 14:02:46 +020097 // returning true means symbol was added to the table
Olli Etuahobed35d72017-12-20 16:36:26 +020098 tInsertResult result = level.insert(tLevelPair(function->name(), function));
Olli Etuahob2983c92015-03-18 14:02:46 +020099 return result.second;
100}
101
Olli Etuahofbb1c792018-01-19 16:26:59 +0200102TSymbol *TSymbolTable::TSymbolTableLevel::find(const ImmutableString &name) const
Jamie Madillbfa91f42014-06-05 15:45:18 -0400103{
104 tLevel::const_iterator it = level.find(name);
105 if (it == level.end())
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200106 return nullptr;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400107 else
108 return (*it).second;
109}
110
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200111bool TSymbolTable::TSymbolTableBuiltInLevel::insert(const TSymbol *symbol)
112{
113 // returning true means symbol was added to the table
114 tInsertResult result = mLevel.insert(tLevelPair(symbol->getMangledName(), symbol));
115 return result.second;
116}
117
118bool TSymbolTable::TSymbolTableBuiltInLevel::insertUnmangled(const TFunction *function)
119{
120 // returning true means symbol was added to the table
121 tInsertResult result = mLevel.insert(tLevelPair(function->name(), function));
122 return result.second;
123}
124
125const TSymbol *TSymbolTable::TSymbolTableBuiltInLevel::find(const ImmutableString &name) const
126{
127 tLevel::const_iterator it = mLevel.find(name);
128 if (it == mLevel.end())
129 return nullptr;
130 else
131 return (*it).second;
132}
133
134void TSymbolTable::TSymbolTableBuiltInLevel::insertUnmangledBuiltInName(const char *name)
Olli Etuaho342b83d2018-01-10 13:24:01 +0200135{
Olli Etuaho2d8e4322018-01-22 14:12:46 +0200136 mUnmangledBuiltInNames.insert(ImmutableString(name));
Olli Etuaho342b83d2018-01-10 13:24:01 +0200137}
138
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200139bool TSymbolTable::TSymbolTableBuiltInLevel::hasUnmangledBuiltIn(const char *name) const
Olli Etuaho342b83d2018-01-10 13:24:01 +0200140{
Olli Etuaho2d8e4322018-01-22 14:12:46 +0200141 return mUnmangledBuiltInNames.count(ImmutableString(name)) > 0;
Olli Etuaho342b83d2018-01-10 13:24:01 +0200142}
143
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200144TSymbolTable::TSymbolTable() : mUniqueIdCounter(0), mUserDefinedUniqueIdsStart(-1)
145{
146}
147
148TSymbolTable::~TSymbolTable() = default;
149
150void TSymbolTable::pushBuiltInLevel()
151{
152 mBuiltInTable.push_back(
153 std::unique_ptr<TSymbolTableBuiltInLevel>(new TSymbolTableBuiltInLevel));
154}
155
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200156void TSymbolTable::push()
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000157{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200158 mTable.push_back(std::unique_ptr<TSymbolTableLevel>(new TSymbolTableLevel));
159 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200160}
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000161
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200162void TSymbolTable::pop()
163{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200164 mTable.pop_back();
165 mPrecisionStack.pop_back();
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200166}
167
168const TFunction *TSymbolTable::markUserDefinedFunctionHasPrototypeDeclaration(
Olli Etuahofbb1c792018-01-19 16:26:59 +0200169 const ImmutableString &mangledName,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200170 bool *hadPrototypeDeclarationOut)
171{
172 TFunction *function = findUserDefinedFunction(mangledName);
173 *hadPrototypeDeclarationOut = function->hasPrototypeDeclaration();
174 function->setHasPrototypeDeclaration();
175 return function;
176}
177
178const TFunction *TSymbolTable::setUserDefinedFunctionParameterNamesFromDefinition(
179 const TFunction *function,
180 bool *wasDefinedOut)
181{
182 TFunction *firstDeclaration = findUserDefinedFunction(function->getMangledName());
183 ASSERT(firstDeclaration);
184 // Note: 'firstDeclaration' could be 'function' if this is the first time we've seen function as
185 // it would have just been put in the symbol table. Otherwise, we're looking up an earlier
186 // occurance.
187 if (function != firstDeclaration)
188 {
189 // Swap the parameters of the previous declaration to the parameters of the function
190 // definition (parameter names may differ).
191 firstDeclaration->swapParameters(*function);
192 }
193
194 *wasDefinedOut = firstDeclaration->isDefined();
195 firstDeclaration->setDefined();
196 return firstDeclaration;
197}
198
Olli Etuahofbb1c792018-01-19 16:26:59 +0200199const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200200{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200201 int userDefinedLevel = static_cast<int>(mTable.size()) - 1;
202 while (userDefinedLevel >= 0)
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000203 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200204 const TSymbol *symbol = mTable[userDefinedLevel]->find(name);
205 if (symbol)
206 {
207 return symbol;
208 }
209 userDefinedLevel--;
210 }
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000211
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200212 return findBuiltIn(name, shaderVersion, false);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000213}
214
Olli Etuahofbb1c792018-01-19 16:26:59 +0200215TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200216{
217 // User-defined functions are always declared at the global level.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200218 ASSERT(!mTable.empty());
219 return static_cast<TFunction *>(mTable[0]->find(name));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200220}
221
Olli Etuahofbb1c792018-01-19 16:26:59 +0200222const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const
Zhenyao Mod7490962016-11-09 15:49:51 -0800223{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200224 ASSERT(!mTable.empty());
225 return mTable[0]->find(name);
Zhenyao Mod7490962016-11-09 15:49:51 -0800226}
227
Olli Etuahofbb1c792018-01-19 16:26:59 +0200228const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000229{
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300230 return findBuiltIn(name, shaderVersion, false);
231}
232
Olli Etuahofbb1c792018-01-19 16:26:59 +0200233const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200234 int shaderVersion,
235 bool includeGLSLBuiltins) const
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300236{
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000237 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
238 {
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300239 if (level == GLSL_BUILTINS && !includeGLSLBuiltins)
240 level--;
Martin Radeve93d24e2016-07-28 12:06:05 +0300241 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
242 level--;
243 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700244 level--;
245 if (level == ESSL1_BUILTINS && shaderVersion != 100)
246 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000247
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200248 const TSymbol *symbol = mBuiltInTable[level]->find(name);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000249
250 if (symbol)
251 return symbol;
252 }
253
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300254 return nullptr;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000255}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400256
Kai Ninomiya030017a2017-12-06 14:06:53 -0800257constexpr bool IsGenType(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 == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType ||
263 basicType == EbtGenBType;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500264 }
265
266 return false;
267}
268
Kai Ninomiya030017a2017-12-06 14:06:53 -0800269constexpr bool IsVecType(const TType *type)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500270{
271 if (type)
272 {
273 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500274 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec ||
275 basicType == EbtBVec;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500276 }
277
278 return false;
279}
280
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800281constexpr const TType *SpecificType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500282{
283 ASSERT(size >= 1 && size <= 4);
284
285 if (!type)
286 {
287 return nullptr;
288 }
289
290 ASSERT(!IsVecType(type));
291
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500292 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500293 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500294 case EbtGenType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800295 return StaticType::GetForVec<EbtFloat>(type->getQualifier(),
296 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500297 case EbtGenIType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800298 return StaticType::GetForVec<EbtInt>(type->getQualifier(),
299 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500300 case EbtGenUType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800301 return StaticType::GetForVec<EbtUInt>(type->getQualifier(),
302 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500303 case EbtGenBType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800304 return StaticType::GetForVec<EbtBool>(type->getQualifier(),
305 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500306 default:
307 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500308 }
309}
310
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800311constexpr const TType *VectorType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500312{
313 ASSERT(size >= 2 && size <= 4);
314
315 if (!type)
316 {
317 return nullptr;
318 }
319
320 ASSERT(!IsGenType(type));
321
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500322 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500323 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324 case EbtVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800325 return StaticType::GetForVecMat<EbtFloat>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500326 case EbtIVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800327 return StaticType::GetForVecMat<EbtInt>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500328 case EbtUVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800329 return StaticType::GetForVecMat<EbtUInt>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500330 case EbtBVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800331 return StaticType::GetForVecMat<EbtBool>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500332 default:
333 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500334 }
335}
336
Olli Etuaho195be942017-12-04 23:40:14 +0200337bool TSymbolTable::declareVariable(TVariable *variable)
Olli Etuaho0f684632017-07-13 12:42:15 +0300338{
Olli Etuaho195be942017-12-04 23:40:14 +0200339 ASSERT(variable->symbolType() == SymbolType::UserDefined);
340 return insertVariable(currentLevel(), variable);
Olli Etuaho0f684632017-07-13 12:42:15 +0300341}
342
Olli Etuaho035419f2017-11-28 14:27:15 +0200343bool TSymbolTable::declareStructType(TStructure *str)
Olli Etuaho0f684632017-07-13 12:42:15 +0300344{
345 return insertStructType(currentLevel(), str);
346}
347
Olli Etuaho378c3a52017-12-04 11:32:13 +0200348bool TSymbolTable::declareInterfaceBlock(TInterfaceBlock *interfaceBlock)
Olli Etuaho0f684632017-07-13 12:42:15 +0300349{
Olli Etuaho378c3a52017-12-04 11:32:13 +0200350 return insert(currentLevel(), interfaceBlock);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800351}
352
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200353void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
354{
355 ASSERT(currentLevel() >= GLOBAL_LEVEL);
356 if (insertUnmangledName)
357 {
358 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200359 mTable[0]->insertUnmangled(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200360 }
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200361 mTable[0]->insert(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200362}
363
Olli Etuahofbb1c792018-01-19 16:26:59 +0200364TVariable *TSymbolTable::insertVariable(ESymbolLevel level,
365 const ImmutableString &name,
366 const TType *type)
Olli Etuaho0f684632017-07-13 12:42:15 +0300367{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100368 ASSERT(level <= LAST_BUILTIN_LEVEL);
Olli Etuahob60d30f2018-01-16 12:31:06 +0200369 ASSERT(type->isRealized());
Olli Etuahofbb1c792018-01-19 16:26:59 +0200370 return insertVariable(level, name, type, SymbolType::BuiltIn);
Olli Etuaho0f684632017-07-13 12:42:15 +0300371}
372
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100373TVariable *TSymbolTable::insertVariable(ESymbolLevel level,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200374 const ImmutableString &name,
Olli Etuahob60d30f2018-01-16 12:31:06 +0200375 const TType *type,
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100376 SymbolType symbolType)
Olli Etuaho0f684632017-07-13 12:42:15 +0300377{
Olli Etuahob60d30f2018-01-16 12:31:06 +0200378 ASSERT(level > LAST_BUILTIN_LEVEL || type->isRealized());
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100379 TVariable *var = new TVariable(this, name, type, symbolType);
Olli Etuaho0f684632017-07-13 12:42:15 +0300380 if (insert(level, var))
381 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300382 return var;
383 }
384 return nullptr;
385}
386
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200387void TSymbolTable::insertVariableExt(ESymbolLevel level,
388 TExtension ext,
389 const ImmutableString &name,
390 const TType *type)
Olli Etuaho0f684632017-07-13 12:42:15 +0300391{
Olli Etuahob60d30f2018-01-16 12:31:06 +0200392 ASSERT(level <= LAST_BUILTIN_LEVEL);
393 ASSERT(type->isRealized());
Olli Etuahofbb1c792018-01-19 16:26:59 +0200394 TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn, ext);
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200395 bool inserted = insert(level, var);
396 UNUSED_VARIABLE(inserted);
397 ASSERT(inserted);
Olli Etuaho0f684632017-07-13 12:42:15 +0300398}
399
Olli Etuaho195be942017-12-04 23:40:14 +0200400bool TSymbolTable::insertVariable(ESymbolLevel level, TVariable *variable)
401{
402 ASSERT(variable);
Olli Etuahob60d30f2018-01-16 12:31:06 +0200403 ASSERT(level > LAST_BUILTIN_LEVEL || variable->getType().isRealized());
Olli Etuaho195be942017-12-04 23:40:14 +0200404 return insert(level, variable);
405}
406
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200407bool TSymbolTable::insert(ESymbolLevel level, TSymbol *symbol)
408{
409 ASSERT(level > LAST_BUILTIN_LEVEL || mUserDefinedUniqueIdsStart == -1);
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200410 if (level <= LAST_BUILTIN_LEVEL)
411 {
412 return mBuiltInTable[level]->insert(symbol);
413 }
414 else
415 {
416 return mTable[level - LAST_BUILTIN_LEVEL - 1]->insert(symbol);
417 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200418}
419
Olli Etuaho035419f2017-11-28 14:27:15 +0200420bool TSymbolTable::insertStructType(ESymbolLevel level, TStructure *str)
Olli Etuaho0f684632017-07-13 12:42:15 +0300421{
Olli Etuaho035419f2017-11-28 14:27:15 +0200422 ASSERT(str);
Olli Etuaho378c3a52017-12-04 11:32:13 +0200423 return insert(level, str);
424}
425
426bool TSymbolTable::insertInterfaceBlock(ESymbolLevel level, TInterfaceBlock *interfaceBlock)
427{
428 ASSERT(interfaceBlock);
429 return insert(level, interfaceBlock);
Olli Etuaho0f684632017-07-13 12:42:15 +0300430}
431
Olli Etuaho29bda812018-01-26 17:37:36 +0200432template <TPrecision precision>
433bool TSymbolTable::insertConstInt(ESymbolLevel level, const ImmutableString &name, int value)
434{
435 TVariable *constant = new TVariable(
436 this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn);
437 TConstantUnion *unionArray = new TConstantUnion[1];
438 unionArray[0].setIConst(value);
439 constant->shareConstPointer(unionArray);
440 return insert(level, constant);
441}
442
443template <TPrecision precision>
444bool TSymbolTable::insertConstIntExt(ESymbolLevel level,
445 TExtension ext,
446 const ImmutableString &name,
447 int value)
448{
449 TVariable *constant = new TVariable(
450 this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn, ext);
451 TConstantUnion *unionArray = new TConstantUnion[1];
452 unionArray[0].setIConst(value);
453 constant->shareConstPointer(unionArray);
454 return insert(level, constant);
455}
456
457template <TPrecision precision>
458bool TSymbolTable::insertConstIvec3(ESymbolLevel level,
459 const ImmutableString &name,
460 const std::array<int, 3> &values)
461{
462 TVariable *constantIvec3 = new TVariable(
463 this, name, StaticType::Get<EbtInt, precision, EvqConst, 3, 1>(), SymbolType::BuiltIn);
464
465 TConstantUnion *unionArray = new TConstantUnion[3];
466 for (size_t index = 0u; index < 3u; ++index)
467 {
468 unionArray[index].setIConst(values[index]);
469 }
470 constantIvec3->shareConstPointer(unionArray);
471
472 return insert(level, constantIvec3);
473}
474
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500475void TSymbolTable::insertBuiltIn(ESymbolLevel level,
476 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300477 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500478 const TType *rvalue,
479 const char *name,
480 const TType *ptype1,
481 const TType *ptype2,
482 const TType *ptype3,
483 const TType *ptype4,
484 const TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700485{
486 if (ptype1->getBasicType() == EbtGSampler2D)
487 {
Martin Radevda6254b2016-12-14 17:00:36 +0200488 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700489 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800490 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
491 StaticType::GetBasic<EbtSampler2D>(), ptype2, ptype3, ptype4, ptype5);
492 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
493 StaticType::GetBasic<EbtISampler2D>(), ptype2, ptype3, ptype4, ptype5);
494 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
495 StaticType::GetBasic<EbtUSampler2D>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700496 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500497 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700498 {
Martin Radevda6254b2016-12-14 17:00:36 +0200499 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700500 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800501 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
502 StaticType::GetBasic<EbtSampler3D>(), ptype2, ptype3, ptype4, ptype5);
503 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
504 StaticType::GetBasic<EbtISampler3D>(), ptype2, ptype3, ptype4, ptype5);
505 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
506 StaticType::GetBasic<EbtUSampler3D>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700507 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500508 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700509 {
Martin Radevda6254b2016-12-14 17:00:36 +0200510 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700511 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800512 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
513 StaticType::GetBasic<EbtSamplerCube>(), ptype2, ptype3, ptype4, ptype5);
514 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
515 StaticType::GetBasic<EbtISamplerCube>(), ptype2, ptype3, ptype4, ptype5);
516 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
517 StaticType::GetBasic<EbtUSamplerCube>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700518 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500519 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700520 {
Martin Radevda6254b2016-12-14 17:00:36 +0200521 insertUnmangledBuiltInName(name, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700522 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800523 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
524 StaticType::GetBasic<EbtSampler2DArray>(), ptype2, ptype3, ptype4,
525 ptype5);
526 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
527 StaticType::GetBasic<EbtISampler2DArray>(), ptype2, ptype3, ptype4,
528 ptype5);
529 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
530 StaticType::GetBasic<EbtUSampler2DArray>(), ptype2, ptype3, ptype4,
531 ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700532 }
JiangYizhou40219322016-12-09 09:50:51 +0800533 else if (ptype1->getBasicType() == EbtGSampler2DMS)
534 {
535 insertUnmangledBuiltInName(name, level);
536 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800537 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
538 StaticType::GetBasic<EbtSampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
539 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
540 StaticType::GetBasic<EbtISampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
541 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
542 StaticType::GetBasic<EbtUSampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
JiangYizhou40219322016-12-09 09:50:51 +0800543 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300544 else if (IsGImage(ptype1->getBasicType()))
545 {
Martin Radevda6254b2016-12-14 17:00:36 +0200546 insertUnmangledBuiltInName(name, level);
Martin Radev2cc85b32016-08-05 16:22:53 +0300547
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800548 const TType *floatType = StaticType::GetBasic<EbtFloat, 4>();
549 const TType *intType = StaticType::GetBasic<EbtInt, 4>();
550 const TType *unsignedType = StaticType::GetBasic<EbtUInt, 4>();
Martin Radev2cc85b32016-08-05 16:22:53 +0300551
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800552 const TType *floatImage = StaticType::GetForFloatImage(ptype1->getBasicType());
553 const TType *intImage = StaticType::GetForIntImage(ptype1->getBasicType());
554 const TType *unsignedImage = StaticType::GetForUintImage(ptype1->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300555
556 // GLSL ES 3.10, Revision 4, 8.12 Image Functions
557 if (rvalue->getBasicType() == EbtGVec4)
558 {
559 // imageLoad
560 insertBuiltIn(level, floatType, name, floatImage, ptype2, ptype3, ptype4, ptype5);
561 insertBuiltIn(level, intType, name, intImage, ptype2, ptype3, ptype4, ptype5);
562 insertBuiltIn(level, unsignedType, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
563 }
564 else if (rvalue->getBasicType() == EbtVoid)
565 {
566 // imageStore
567 insertBuiltIn(level, rvalue, name, floatImage, ptype2, floatType, ptype4, ptype5);
568 insertBuiltIn(level, rvalue, name, intImage, ptype2, intType, ptype4, ptype5);
569 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, unsignedType, ptype4, ptype5);
570 }
571 else
572 {
573 // imageSize
574 insertBuiltIn(level, rvalue, name, floatImage, ptype2, ptype3, ptype4, ptype5);
575 insertBuiltIn(level, rvalue, name, intImage, ptype2, ptype3, ptype4, ptype5);
576 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
577 }
578 }
Olli Etuaho9250cb22017-01-21 10:51:27 +0000579 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3) ||
580 IsGenType(ptype4))
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500581 {
Olli Etuaho9250cb22017-01-21 10:51:27 +0000582 ASSERT(!ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200583 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500584 insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000585 SpecificType(ptype2, 1), SpecificType(ptype3, 1), SpecificType(ptype4, 1));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500586 insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000587 SpecificType(ptype2, 2), SpecificType(ptype3, 2), SpecificType(ptype4, 2));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500588 insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000589 SpecificType(ptype2, 3), SpecificType(ptype3, 3), SpecificType(ptype4, 3));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500590 insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000591 SpecificType(ptype2, 4), SpecificType(ptype3, 4), SpecificType(ptype4, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500592 }
593 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
594 {
595 ASSERT(!ptype4 && !ptype5);
Martin Radevda6254b2016-12-14 17:00:36 +0200596 insertUnmangledBuiltInName(name, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500597 insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2),
598 VectorType(ptype2, 2), VectorType(ptype3, 2));
599 insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3),
600 VectorType(ptype2, 3), VectorType(ptype3, 3));
601 insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4),
602 VectorType(ptype2, 4), VectorType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500603 }
604 else
605 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100606 TFunction *function =
Olli Etuahofbb1c792018-01-19 16:26:59 +0200607 new TFunction(this, ImmutableString(name), rvalue, SymbolType::BuiltIn, false, op, ext);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700608
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700609 function->addParameter(TConstParameter(ptype1));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700610
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500611 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700612 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700613 function->addParameter(TConstParameter(ptype2));
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700614 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700615
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500616 if (ptype3)
617 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700618 function->addParameter(TConstParameter(ptype3));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500619 }
620
621 if (ptype4)
622 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700623 function->addParameter(TConstParameter(ptype4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500624 }
625
626 if (ptype5)
627 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700628 function->addParameter(TConstParameter(ptype5));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500629 }
630
Martin Radevda6254b2016-12-14 17:00:36 +0200631 ASSERT(hasUnmangledBuiltInAtLevel(name, level));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500632 insert(level, function);
633 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700634}
635
Olli Etuaho492cfab2017-01-20 21:18:29 +0000636void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
637 TOperator op,
638 const TType *rvalue,
639 const TType *ptype1,
640 const TType *ptype2,
641 const TType *ptype3,
642 const TType *ptype4,
643 const TType *ptype5)
644{
645 const char *name = GetOperatorString(op);
646 ASSERT(strlen(name) > 0);
647 insertUnmangledBuiltInName(name, level);
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300648 insertBuiltIn(level, op, TExtension::UNDEFINED, rvalue, name, ptype1, ptype2, ptype3, ptype4,
649 ptype5);
Olli Etuaho492cfab2017-01-20 21:18:29 +0000650}
651
652void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
653 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300654 TExtension ext,
Olli Etuaho492cfab2017-01-20 21:18:29 +0000655 const TType *rvalue,
656 const TType *ptype1,
657 const TType *ptype2,
658 const TType *ptype3,
659 const TType *ptype4,
660 const TType *ptype5)
661{
662 const char *name = GetOperatorString(op);
663 insertUnmangledBuiltInName(name, level);
664 insertBuiltIn(level, op, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
665}
666
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300667void TSymbolTable::insertBuiltInFunctionNoParameters(ESymbolLevel level,
668 TOperator op,
669 const TType *rvalue,
670 const char *name)
671{
672 insertUnmangledBuiltInName(name, level);
Olli Etuaho0c371002017-12-13 17:00:25 +0400673 insert(level,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200674 new TFunction(this, ImmutableString(name), rvalue, SymbolType::BuiltIn, false, op));
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300675}
676
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800677void TSymbolTable::insertBuiltInFunctionNoParametersExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300678 TExtension ext,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800679 TOperator op,
680 const TType *rvalue,
681 const char *name)
682{
683 insertUnmangledBuiltInName(name, level);
Olli Etuaho0c371002017-12-13 17:00:25 +0400684 insert(level,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200685 new TFunction(this, ImmutableString(name), rvalue, SymbolType::BuiltIn, false, op, ext));
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800686}
687
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200688void TSymbolTable::setDefaultPrecision(TBasicType type, TPrecision prec)
689{
690 int indexOfLastElement = static_cast<int>(mPrecisionStack.size()) - 1;
691 // Uses map operator [], overwrites the current value
692 (*mPrecisionStack[indexOfLastElement])[type] = prec;
693}
694
Zhenyao Moe740add2014-07-18 17:01:01 -0700695TPrecision TSymbolTable::getDefaultPrecision(TBasicType type) const
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700696{
697 if (!SupportsPrecision(type))
698 return EbpUndefined;
699
700 // unsigned integers use the same precision as signed
701 TBasicType baseType = (type == EbtUInt) ? EbtInt : type;
702
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200703 int level = static_cast<int>(mPrecisionStack.size()) - 1;
704 ASSERT(level >= 0); // Just to be safe. Should not happen.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200705 // If we dont find anything we return this. Some types don't have predefined default precision.
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700706 TPrecision prec = EbpUndefined;
707 while (level >= 0)
708 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200709 PrecisionStackLevel::iterator it = mPrecisionStack[level]->find(baseType);
710 if (it != mPrecisionStack[level]->end())
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700711 {
712 prec = (*it).second;
713 break;
714 }
715 level--;
716 }
717 return prec;
718}
Jamie Madill45bcc782016-11-07 13:58:48 -0500719
Olli Etuahodefe3932018-02-13 11:56:09 +0200720void TSymbolTable::addInvariantVarying(const ImmutableString &originalName)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200721{
722 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200723 mTable.back()->addInvariantVarying(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200724}
725
Olli Etuahodefe3932018-02-13 11:56:09 +0200726bool TSymbolTable::isVaryingInvariant(const ImmutableString &originalName) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200727{
728 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200729 return mTable.back()->isVaryingInvariant(originalName);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200730}
731
732void TSymbolTable::setGlobalInvariant(bool invariant)
733{
734 ASSERT(atGlobalLevel());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200735 mTable.back()->setGlobalInvariant(invariant);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200736}
737
Martin Radevda6254b2016-12-14 17:00:36 +0200738void TSymbolTable::insertUnmangledBuiltInName(const char *name, ESymbolLevel level)
739{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200740 ASSERT(level >= 0 && level <= LAST_BUILTIN_LEVEL);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200741 ASSERT(mUserDefinedUniqueIdsStart == -1);
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200742 mBuiltInTable[level]->insertUnmangledBuiltInName(name);
Martin Radevda6254b2016-12-14 17:00:36 +0200743}
744
745bool TSymbolTable::hasUnmangledBuiltInAtLevel(const char *name, ESymbolLevel level)
746{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200747 ASSERT(level >= 0 && level <= LAST_BUILTIN_LEVEL);
748 return mBuiltInTable[level]->hasUnmangledBuiltIn(name);
Martin Radevda6254b2016-12-14 17:00:36 +0200749}
750
751bool TSymbolTable::hasUnmangledBuiltInForShaderVersion(const char *name, int shaderVersion)
752{
Martin Radevda6254b2016-12-14 17:00:36 +0200753 for (int level = LAST_BUILTIN_LEVEL; level >= 0; --level)
754 {
755 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
756 {
757 --level;
758 }
759 if (level == ESSL3_BUILTINS && shaderVersion < 300)
760 {
761 --level;
762 }
763 if (level == ESSL1_BUILTINS && shaderVersion != 100)
764 {
765 --level;
766 }
767
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200768 if (mBuiltInTable[level]->hasUnmangledBuiltIn(name))
Martin Radevda6254b2016-12-14 17:00:36 +0200769 {
770 return true;
771 }
772 }
773 return false;
774}
775
Olli Etuaho5d69db12017-11-24 16:51:15 +0200776void TSymbolTable::markBuiltInInitializationFinished()
777{
778 mUserDefinedUniqueIdsStart = mUniqueIdCounter;
779}
780
781void TSymbolTable::clearCompilationResults()
782{
783 mUniqueIdCounter = mUserDefinedUniqueIdsStart;
784
785 // User-defined scopes should have already been cleared when the compilation finished.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200786 ASSERT(mTable.size() == 0u);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200787}
788
789int TSymbolTable::nextUniqueIdValue()
790{
791 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
792 return ++mUniqueIdCounter;
793}
794
Olli Etuaho29bda812018-01-26 17:37:36 +0200795void TSymbolTable::initializeBuiltIns(sh::GLenum type,
796 ShShaderSpec spec,
797 const ShBuiltInResources &resources)
798{
799 ASSERT(isEmpty());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200800 pushBuiltInLevel(); // COMMON_BUILTINS
801 pushBuiltInLevel(); // ESSL1_BUILTINS
802 pushBuiltInLevel(); // ESSL3_BUILTINS
803 pushBuiltInLevel(); // ESSL3_1_BUILTINS
804 pushBuiltInLevel(); // GLSL_BUILTINS
805
806 // We need just one precision stack level for predefined precisions.
807 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuaho29bda812018-01-26 17:37:36 +0200808
809 switch (type)
810 {
811 case GL_FRAGMENT_SHADER:
812 setDefaultPrecision(EbtInt, EbpMedium);
813 break;
814 case GL_VERTEX_SHADER:
815 case GL_COMPUTE_SHADER:
816 case GL_GEOMETRY_SHADER_EXT:
817 setDefaultPrecision(EbtInt, EbpHigh);
818 setDefaultPrecision(EbtFloat, EbpHigh);
819 break;
820 default:
821 UNREACHABLE();
822 }
823 // Set defaults for sampler types that have default precision, even those that are
824 // only available if an extension exists.
825 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
826 initSamplerDefaultPrecision(EbtSampler2D);
827 initSamplerDefaultPrecision(EbtSamplerCube);
828 // SamplerExternalOES is specified in the extension to have default precision.
829 initSamplerDefaultPrecision(EbtSamplerExternalOES);
830 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
831 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
832 // It isn't specified whether Sampler2DRect has default precision.
833 initSamplerDefaultPrecision(EbtSampler2DRect);
834
835 setDefaultPrecision(EbtAtomicCounter, EbpHigh);
836
837 initializeBuiltInFunctions(type, spec, resources);
838 initializeBuiltInVariables(type, spec, resources);
839 markBuiltInInitializationFinished();
840}
841
842void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType)
843{
844 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
845 setDefaultPrecision(samplerType, EbpLow);
846}
847
848void TSymbolTable::initializeBuiltInFunctions(sh::GLenum type,
849 ShShaderSpec spec,
850 const ShBuiltInResources &resources)
851{
852 const TType *voidType = StaticType::GetBasic<EbtVoid>();
853 const TType *float1 = StaticType::GetBasic<EbtFloat>();
854 const TType *float2 = StaticType::GetBasic<EbtFloat, 2>();
855 const TType *float3 = StaticType::GetBasic<EbtFloat, 3>();
856 const TType *float4 = StaticType::GetBasic<EbtFloat, 4>();
857 const TType *int1 = StaticType::GetBasic<EbtInt>();
858 const TType *int2 = StaticType::GetBasic<EbtInt, 2>();
859 const TType *int3 = StaticType::GetBasic<EbtInt, 3>();
860 const TType *uint1 = StaticType::GetBasic<EbtUInt>();
861 const TType *bool1 = StaticType::GetBasic<EbtBool>();
862 const TType *genType = StaticType::GetBasic<EbtGenType>();
863 const TType *genIType = StaticType::GetBasic<EbtGenIType>();
864 const TType *genUType = StaticType::GetBasic<EbtGenUType>();
865 const TType *genBType = StaticType::GetBasic<EbtGenBType>();
866
867 //
868 // Angle and Trigonometric Functions.
869 //
870 insertBuiltInOp(COMMON_BUILTINS, EOpRadians, genType, genType);
871 insertBuiltInOp(COMMON_BUILTINS, EOpDegrees, genType, genType);
872 insertBuiltInOp(COMMON_BUILTINS, EOpSin, genType, genType);
873 insertBuiltInOp(COMMON_BUILTINS, EOpCos, genType, genType);
874 insertBuiltInOp(COMMON_BUILTINS, EOpTan, genType, genType);
875 insertBuiltInOp(COMMON_BUILTINS, EOpAsin, genType, genType);
876 insertBuiltInOp(COMMON_BUILTINS, EOpAcos, genType, genType);
877 insertBuiltInOp(COMMON_BUILTINS, EOpAtan, genType, genType, genType);
878 insertBuiltInOp(COMMON_BUILTINS, EOpAtan, genType, genType);
879 insertBuiltInOp(ESSL3_BUILTINS, EOpSinh, genType, genType);
880 insertBuiltInOp(ESSL3_BUILTINS, EOpCosh, genType, genType);
881 insertBuiltInOp(ESSL3_BUILTINS, EOpTanh, genType, genType);
882 insertBuiltInOp(ESSL3_BUILTINS, EOpAsinh, genType, genType);
883 insertBuiltInOp(ESSL3_BUILTINS, EOpAcosh, genType, genType);
884 insertBuiltInOp(ESSL3_BUILTINS, EOpAtanh, genType, genType);
885
886 //
887 // Exponential Functions.
888 //
889 insertBuiltInOp(COMMON_BUILTINS, EOpPow, genType, genType, genType);
890 insertBuiltInOp(COMMON_BUILTINS, EOpExp, genType, genType);
891 insertBuiltInOp(COMMON_BUILTINS, EOpLog, genType, genType);
892 insertBuiltInOp(COMMON_BUILTINS, EOpExp2, genType, genType);
893 insertBuiltInOp(COMMON_BUILTINS, EOpLog2, genType, genType);
894 insertBuiltInOp(COMMON_BUILTINS, EOpSqrt, genType, genType);
895 insertBuiltInOp(COMMON_BUILTINS, EOpInverseSqrt, genType, genType);
896
897 //
898 // Common Functions.
899 //
900 insertBuiltInOp(COMMON_BUILTINS, EOpAbs, genType, genType);
901 insertBuiltInOp(ESSL3_BUILTINS, EOpAbs, genIType, genIType);
902 insertBuiltInOp(COMMON_BUILTINS, EOpSign, genType, genType);
903 insertBuiltInOp(ESSL3_BUILTINS, EOpSign, genIType, genIType);
904 insertBuiltInOp(COMMON_BUILTINS, EOpFloor, genType, genType);
905 insertBuiltInOp(ESSL3_BUILTINS, EOpTrunc, genType, genType);
906 insertBuiltInOp(ESSL3_BUILTINS, EOpRound, genType, genType);
907 insertBuiltInOp(ESSL3_BUILTINS, EOpRoundEven, genType, genType);
908 insertBuiltInOp(COMMON_BUILTINS, EOpCeil, genType, genType);
909 insertBuiltInOp(COMMON_BUILTINS, EOpFract, genType, genType);
910 insertBuiltInOp(COMMON_BUILTINS, EOpMod, genType, genType, float1);
911 insertBuiltInOp(COMMON_BUILTINS, EOpMod, genType, genType, genType);
912 insertBuiltInOp(COMMON_BUILTINS, EOpMin, genType, genType, float1);
913 insertBuiltInOp(COMMON_BUILTINS, EOpMin, genType, genType, genType);
914 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genIType, genIType, genIType);
915 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genIType, genIType, int1);
916 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genUType, genUType, genUType);
917 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genUType, genUType, uint1);
918 insertBuiltInOp(COMMON_BUILTINS, EOpMax, genType, genType, float1);
919 insertBuiltInOp(COMMON_BUILTINS, EOpMax, genType, genType, genType);
920 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genIType, genIType, genIType);
921 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genIType, genIType, int1);
922 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genUType, genUType, genUType);
923 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genUType, genUType, uint1);
924 insertBuiltInOp(COMMON_BUILTINS, EOpClamp, genType, genType, float1, float1);
925 insertBuiltInOp(COMMON_BUILTINS, EOpClamp, genType, genType, genType, genType);
926 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genIType, genIType, int1, int1);
927 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genIType, genIType, genIType, genIType);
928 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genUType, genUType, uint1, uint1);
929 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genUType, genUType, genUType, genUType);
930 insertBuiltInOp(COMMON_BUILTINS, EOpMix, genType, genType, genType, float1);
931 insertBuiltInOp(COMMON_BUILTINS, EOpMix, genType, genType, genType, genType);
932 insertBuiltInOp(ESSL3_BUILTINS, EOpMix, genType, genType, genType, genBType);
933 insertBuiltInOp(COMMON_BUILTINS, EOpStep, genType, genType, genType);
934 insertBuiltInOp(COMMON_BUILTINS, EOpStep, genType, float1, genType);
935 insertBuiltInOp(COMMON_BUILTINS, EOpSmoothStep, genType, genType, genType, genType);
936 insertBuiltInOp(COMMON_BUILTINS, EOpSmoothStep, genType, float1, float1, genType);
937
938 const TType *outGenType = StaticType::GetQualified<EbtGenType, EvqOut>();
939 const TType *outGenIType = StaticType::GetQualified<EbtGenIType, EvqOut>();
940
941 insertBuiltInOp(ESSL3_BUILTINS, EOpModf, genType, genType, outGenType);
942
943 insertBuiltInOp(ESSL3_BUILTINS, EOpIsNan, genBType, genType);
944 insertBuiltInOp(ESSL3_BUILTINS, EOpIsInf, genBType, genType);
945 insertBuiltInOp(ESSL3_BUILTINS, EOpFloatBitsToInt, genIType, genType);
946 insertBuiltInOp(ESSL3_BUILTINS, EOpFloatBitsToUint, genUType, genType);
947 insertBuiltInOp(ESSL3_BUILTINS, EOpIntBitsToFloat, genType, genIType);
948 insertBuiltInOp(ESSL3_BUILTINS, EOpUintBitsToFloat, genType, genUType);
949
950 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFrexp, genType, genType, outGenIType);
951 insertBuiltInOp(ESSL3_1_BUILTINS, EOpLdexp, genType, genType, genIType);
952
953 insertBuiltInOp(ESSL3_BUILTINS, EOpPackSnorm2x16, uint1, float2);
954 insertBuiltInOp(ESSL3_BUILTINS, EOpPackUnorm2x16, uint1, float2);
955 insertBuiltInOp(ESSL3_BUILTINS, EOpPackHalf2x16, uint1, float2);
956 insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackSnorm2x16, float2, uint1);
957 insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackUnorm2x16, float2, uint1);
958 insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackHalf2x16, float2, uint1);
959
960 insertBuiltInOp(ESSL3_1_BUILTINS, EOpPackUnorm4x8, uint1, float4);
961 insertBuiltInOp(ESSL3_1_BUILTINS, EOpPackSnorm4x8, uint1, float4);
962 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUnpackUnorm4x8, float4, uint1);
963 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUnpackSnorm4x8, float4, uint1);
964
965 //
966 // Geometric Functions.
967 //
968 insertBuiltInOp(COMMON_BUILTINS, EOpLength, float1, genType);
969 insertBuiltInOp(COMMON_BUILTINS, EOpDistance, float1, genType, genType);
970 insertBuiltInOp(COMMON_BUILTINS, EOpDot, float1, genType, genType);
971 insertBuiltInOp(COMMON_BUILTINS, EOpCross, float3, float3, float3);
972 insertBuiltInOp(COMMON_BUILTINS, EOpNormalize, genType, genType);
973 insertBuiltInOp(COMMON_BUILTINS, EOpFaceforward, genType, genType, genType, genType);
974 insertBuiltInOp(COMMON_BUILTINS, EOpReflect, genType, genType, genType);
975 insertBuiltInOp(COMMON_BUILTINS, EOpRefract, genType, genType, genType, float1);
976
977 const TType *mat2 = StaticType::GetBasic<EbtFloat, 2, 2>();
978 const TType *mat3 = StaticType::GetBasic<EbtFloat, 3, 3>();
979 const TType *mat4 = StaticType::GetBasic<EbtFloat, 4, 4>();
980 const TType *mat2x3 = StaticType::GetBasic<EbtFloat, 2, 3>();
981 const TType *mat3x2 = StaticType::GetBasic<EbtFloat, 3, 2>();
982 const TType *mat2x4 = StaticType::GetBasic<EbtFloat, 2, 4>();
983 const TType *mat4x2 = StaticType::GetBasic<EbtFloat, 4, 2>();
984 const TType *mat3x4 = StaticType::GetBasic<EbtFloat, 3, 4>();
985 const TType *mat4x3 = StaticType::GetBasic<EbtFloat, 4, 3>();
986
987 //
988 // Matrix Functions.
989 //
990 insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat2, mat2, mat2);
991 insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat3, mat3, mat3);
992 insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat4, mat4, mat4);
993 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat2x3, mat2x3, mat2x3);
994 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat3x2, mat3x2, mat3x2);
995 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat2x4, mat2x4, mat2x4);
996 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat4x2, mat4x2, mat4x2);
997 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat3x4, mat3x4, mat3x4);
998 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat4x3, mat4x3, mat4x3);
999
1000 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2, float2, float2);
1001 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3, float3, float3);
1002 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4, float4, float4);
1003 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2x3, float3, float2);
1004 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3x2, float2, float3);
1005 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2x4, float4, float2);
1006 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4x2, float2, float4);
1007 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3x4, float4, float3);
1008 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4x3, float3, float4);
1009
1010 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2, mat2);
1011 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3, mat3);
1012 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4, mat4);
1013 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2x3, mat3x2);
1014 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3x2, mat2x3);
1015 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2x4, mat4x2);
1016 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4x2, mat2x4);
1017 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3x4, mat4x3);
1018 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4x3, mat3x4);
1019
1020 insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat2);
1021 insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat3);
1022 insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat4);
1023
1024 insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat2, mat2);
1025 insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat3, mat3);
1026 insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat4, mat4);
1027
1028 const TType *vec = StaticType::GetBasic<EbtVec>();
1029 const TType *ivec = StaticType::GetBasic<EbtIVec>();
1030 const TType *uvec = StaticType::GetBasic<EbtUVec>();
1031 const TType *bvec = StaticType::GetBasic<EbtBVec>();
1032
1033 //
1034 // Vector relational functions.
1035 //
1036 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanComponentWise, bvec, vec, vec);
1037 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanComponentWise, bvec, ivec, ivec);
1038 insertBuiltInOp(ESSL3_BUILTINS, EOpLessThanComponentWise, bvec, uvec, uvec);
1039 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanEqualComponentWise, bvec, vec, vec);
1040 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanEqualComponentWise, bvec, ivec, ivec);
1041 insertBuiltInOp(ESSL3_BUILTINS, EOpLessThanEqualComponentWise, bvec, uvec, uvec);
1042 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanComponentWise, bvec, vec, vec);
1043 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanComponentWise, bvec, ivec, ivec);
1044 insertBuiltInOp(ESSL3_BUILTINS, EOpGreaterThanComponentWise, bvec, uvec, uvec);
1045 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, vec, vec);
1046 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, ivec, ivec);
1047 insertBuiltInOp(ESSL3_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, uvec, uvec);
1048 insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, vec, vec);
1049 insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, ivec, ivec);
1050 insertBuiltInOp(ESSL3_BUILTINS, EOpEqualComponentWise, bvec, uvec, uvec);
1051 insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, bvec, bvec);
1052 insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, vec, vec);
1053 insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, ivec, ivec);
1054 insertBuiltInOp(ESSL3_BUILTINS, EOpNotEqualComponentWise, bvec, uvec, uvec);
1055 insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, bvec, bvec);
1056 insertBuiltInOp(COMMON_BUILTINS, EOpAny, bool1, bvec);
1057 insertBuiltInOp(COMMON_BUILTINS, EOpAll, bool1, bvec);
1058 insertBuiltInOp(COMMON_BUILTINS, EOpLogicalNotComponentWise, bvec, bvec);
1059
1060 //
1061 // Integer functions
1062 //
1063 const TType *outGenUType = StaticType::GetQualified<EbtGenUType, EvqOut>();
1064
1065 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldExtract, genIType, genIType, int1, int1);
1066 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldExtract, genUType, genUType, int1, int1);
1067 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldInsert, genIType, genIType, genIType, int1, int1);
1068 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldInsert, genUType, genUType, genUType, int1, int1);
1069 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldReverse, genIType, genIType);
1070 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldReverse, genUType, genUType);
1071 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitCount, genIType, genIType);
1072 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitCount, genIType, genUType);
1073 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindLSB, genIType, genIType);
1074 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindLSB, genIType, genUType);
1075 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindMSB, genIType, genIType);
1076 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindMSB, genIType, genUType);
1077 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUaddCarry, genUType, genUType, genUType, outGenUType);
1078 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUsubBorrow, genUType, genUType, genUType, outGenUType);
1079 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUmulExtended, voidType, genUType, genUType, outGenUType,
1080 outGenUType);
1081 insertBuiltInOp(ESSL3_1_BUILTINS, EOpImulExtended, voidType, genIType, genIType, outGenIType,
1082 outGenIType);
1083
1084 const TType *sampler2D = StaticType::GetBasic<EbtSampler2D>();
1085 const TType *samplerCube = StaticType::GetBasic<EbtSamplerCube>();
1086
1087 //
1088 // Texture Functions for GLSL ES 1.0
1089 //
1090 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", sampler2D, float2);
1091 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float3);
1092 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float4);
1093 insertBuiltIn(ESSL1_BUILTINS, float4, "textureCube", samplerCube, float3);
1094
1095 if (resources.OES_EGL_image_external || resources.NV_EGL_stream_consumer_external)
1096 {
1097 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1098
1099 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", samplerExternalOES, float2);
1100 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", samplerExternalOES, float3);
1101 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", samplerExternalOES, float4);
1102 }
1103
1104 if (resources.ARB_texture_rectangle)
1105 {
1106 const TType *sampler2DRect = StaticType::GetBasic<EbtSampler2DRect>();
1107
1108 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DRect", sampler2DRect, float2);
1109 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DRectProj", sampler2DRect, float3);
1110 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DRectProj", sampler2DRect, float4);
1111 }
1112
1113 if (resources.EXT_shader_texture_lod)
1114 {
1115 /* The *Grad* variants are new to both vertex and fragment shaders; the fragment
1116 * shader specific pieces are added separately below.
1117 */
1118 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1119 "texture2DGradEXT", sampler2D, float2, float2, float2);
1120 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1121 "texture2DProjGradEXT", sampler2D, float3, float2, float2);
1122 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1123 "texture2DProjGradEXT", sampler2D, float4, float2, float2);
1124 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1125 "textureCubeGradEXT", samplerCube, float3, float3, float3);
1126 }
1127
1128 if (type == GL_FRAGMENT_SHADER)
1129 {
1130 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", sampler2D, float2, float1);
1131 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float3, float1);
1132 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float4, float1);
1133 insertBuiltIn(ESSL1_BUILTINS, float4, "textureCube", samplerCube, float3, float1);
1134
1135 if (resources.OES_standard_derivatives)
1136 {
1137 insertBuiltInOp(ESSL1_BUILTINS, EOpDFdx, TExtension::OES_standard_derivatives, genType,
1138 genType);
1139 insertBuiltInOp(ESSL1_BUILTINS, EOpDFdy, TExtension::OES_standard_derivatives, genType,
1140 genType);
1141 insertBuiltInOp(ESSL1_BUILTINS, EOpFwidth, TExtension::OES_standard_derivatives,
1142 genType, genType);
1143 }
1144
1145 if (resources.EXT_shader_texture_lod)
1146 {
1147 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1148 "texture2DLodEXT", sampler2D, float2, float1);
1149 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1150 "texture2DProjLodEXT", sampler2D, float3, float1);
1151 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1152 "texture2DProjLodEXT", sampler2D, float4, float1);
1153 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1154 "textureCubeLodEXT", samplerCube, float3, float1);
1155 }
1156 }
1157
1158 if (type == GL_VERTEX_SHADER)
1159 {
1160 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DLod", sampler2D, float2, float1);
1161 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProjLod", sampler2D, float3, float1);
1162 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProjLod", sampler2D, float4, float1);
1163 insertBuiltIn(ESSL1_BUILTINS, float4, "textureCubeLod", samplerCube, float3, float1);
1164 }
1165
1166 const TType *gvec4 = StaticType::GetBasic<EbtGVec4>();
1167
1168 const TType *gsampler2D = StaticType::GetBasic<EbtGSampler2D>();
1169 const TType *gsamplerCube = StaticType::GetBasic<EbtGSamplerCube>();
1170 const TType *gsampler3D = StaticType::GetBasic<EbtGSampler3D>();
1171 const TType *gsampler2DArray = StaticType::GetBasic<EbtGSampler2DArray>();
1172 const TType *gsampler2DMS = StaticType::GetBasic<EbtGSampler2DMS>();
1173
1174 //
1175 // Texture Functions for GLSL ES 3.0
1176 //
1177 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2D, float2);
1178 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler3D, float3);
1179 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsamplerCube, float3);
1180 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2DArray, float3);
1181 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float3);
1182 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float4);
1183 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler3D, float4);
1184 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler2D, float2, float1);
1185 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler3D, float3, float1);
1186 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsamplerCube, float3, float1);
1187 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler2DArray, float3, float1);
1188
1189 if (resources.OES_EGL_image_external_essl3)
1190 {
1191 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1192
1193 insertBuiltIn(ESSL3_BUILTINS, float4, "texture", samplerExternalOES, float2);
1194 insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES, float3);
1195 insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES, float4);
1196 }
1197
1198 if (resources.EXT_YUV_target)
1199 {
1200 const TType *samplerExternal2DY2YEXT = StaticType::GetBasic<EbtSamplerExternal2DY2YEXT>();
1201
1202 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texture",
1203 samplerExternal2DY2YEXT, float2);
1204 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1205 samplerExternal2DY2YEXT, float3);
1206 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1207 samplerExternal2DY2YEXT, float4);
1208
1209 const TType *yuvCscStandardEXT = StaticType::GetBasic<EbtYuvCscStandardEXT>();
1210
1211 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float3, "rgb_2_yuv", float3,
1212 yuvCscStandardEXT);
1213 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float3, "yuv_2_rgb", float3,
1214 yuvCscStandardEXT);
1215 }
1216
1217 if (type == GL_FRAGMENT_SHADER)
1218 {
1219 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2D, float2, float1);
1220 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler3D, float3, float1);
1221 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsamplerCube, float3, float1);
1222 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2DArray, float3, float1);
1223 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float3, float1);
1224 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float4, float1);
1225 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler3D, float4, float1);
1226
1227 if (resources.OES_EGL_image_external_essl3)
1228 {
1229 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1230
1231 insertBuiltIn(ESSL3_BUILTINS, float4, "texture", samplerExternalOES, float2, float1);
1232 insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES, float3,
1233 float1);
1234 insertBuiltIn(ESSL3_BUILTINS, float4, "textureProj", samplerExternalOES, float4,
1235 float1);
1236 }
1237
1238 if (resources.EXT_YUV_target)
1239 {
1240 const TType *samplerExternal2DY2YEXT =
1241 StaticType::GetBasic<EbtSamplerExternal2DY2YEXT>();
1242
1243 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texture",
1244 samplerExternal2DY2YEXT, float2, float1);
1245 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1246 samplerExternal2DY2YEXT, float3, float1);
1247 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1248 samplerExternal2DY2YEXT, float4, float1);
1249 }
1250 }
1251
1252 const TType *sampler2DShadow = StaticType::GetBasic<EbtSampler2DShadow>();
1253 const TType *samplerCubeShadow = StaticType::GetBasic<EbtSamplerCubeShadow>();
1254 const TType *sampler2DArrayShadow = StaticType::GetBasic<EbtSampler2DArrayShadow>();
1255
1256 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DShadow, float3);
1257 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", samplerCubeShadow, float4);
1258 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DArrayShadow, float4);
1259 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProj", sampler2DShadow, float4);
1260 insertBuiltIn(ESSL3_BUILTINS, float1, "textureLod", sampler2DShadow, float3, float1);
1261
1262 if (type == GL_FRAGMENT_SHADER)
1263 {
1264 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DShadow, float3, float1);
1265 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", samplerCubeShadow, float4, float1);
1266 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProj", sampler2DShadow, float4, float1);
1267 }
1268
1269 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsampler2D, int1);
1270 insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", gsampler3D, int1);
1271 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsamplerCube, int1);
1272 insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", gsampler2DArray, int1);
1273 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", sampler2DShadow, int1);
1274 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", samplerCubeShadow, int1);
1275 insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", sampler2DArrayShadow, int1);
1276 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsampler2DMS);
1277
1278 if (resources.OES_EGL_image_external_essl3)
1279 {
1280 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1281
1282 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", samplerExternalOES, int1);
1283 }
1284
1285 if (resources.EXT_YUV_target)
1286 {
1287 const TType *samplerExternal2DY2YEXT = StaticType::GetBasic<EbtSamplerExternal2DY2YEXT>();
1288
1289 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, int2, "textureSize",
1290 samplerExternal2DY2YEXT, int1);
1291 }
1292
1293 if (type == GL_FRAGMENT_SHADER)
1294 {
1295 insertBuiltInOp(ESSL3_BUILTINS, EOpDFdx, genType, genType);
1296 insertBuiltInOp(ESSL3_BUILTINS, EOpDFdy, genType, genType);
1297 insertBuiltInOp(ESSL3_BUILTINS, EOpFwidth, genType, genType);
1298 }
1299
1300 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2D, float2, int2);
1301 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler3D, float3, int3);
1302 insertBuiltIn(ESSL3_BUILTINS, float1, "textureOffset", sampler2DShadow, float3, int2);
1303 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2DArray, float3, int2);
1304
1305 if (type == GL_FRAGMENT_SHADER)
1306 {
1307 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2D, float2, int2, float1);
1308 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler3D, float3, int3, float1);
1309 insertBuiltIn(ESSL3_BUILTINS, float1, "textureOffset", sampler2DShadow, float3, int2,
1310 float1);
1311 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2DArray, float3, int2,
1312 float1);
1313 }
1314
1315 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float3, int2);
1316 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float4, int2);
1317 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler3D, float4, int3);
1318 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjOffset", sampler2DShadow, float4, int2);
1319
1320 if (type == GL_FRAGMENT_SHADER)
1321 {
1322 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float3, int2, float1);
1323 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float4, int2, float1);
1324 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler3D, float4, int3, float1);
1325 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjOffset", sampler2DShadow, float4, int2,
1326 float1);
1327 }
1328
1329 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler2D, float2, float1, int2);
1330 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler3D, float3, float1, int3);
1331 insertBuiltIn(ESSL3_BUILTINS, float1, "textureLodOffset", sampler2DShadow, float3, float1,
1332 int2);
1333 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler2DArray, float3, float1, int2);
1334
1335 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler2D, float3, float1);
1336 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler2D, float4, float1);
1337 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler3D, float4, float1);
1338 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjLod", sampler2DShadow, float4, float1);
1339
1340 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler2D, float3, float1, int2);
1341 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler2D, float4, float1, int2);
1342 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler3D, float4, float1, int3);
1343 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjLodOffset", sampler2DShadow, float4, float1,
1344 int2);
1345
1346 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler2D, int2, int1);
1347 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler3D, int3, int1);
1348 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler2DArray, int3, int1);
1349
1350 if (resources.OES_EGL_image_external_essl3)
1351 {
1352 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
1353
1354 insertBuiltIn(ESSL3_BUILTINS, float4, "texelFetch", samplerExternalOES, int2, int1);
1355 }
1356
1357 if (resources.EXT_YUV_target)
1358 {
1359 const TType *samplerExternal2DY2YEXT = StaticType::GetBasic<EbtSamplerExternal2DY2YEXT>();
1360
1361 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texelFetch",
1362 samplerExternal2DY2YEXT, int2, int1);
1363 }
1364
1365 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler2D, int2, int1, int2);
1366 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler3D, int3, int1, int3);
1367 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler2DArray, int3, int1, int2);
1368
1369 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler2D, float2, float2, float2);
1370 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler3D, float3, float3, float3);
1371 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsamplerCube, float3, float3, float3);
1372 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", sampler2DShadow, float3, float2, float2);
1373 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", samplerCubeShadow, float4, float3, float3);
1374 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler2DArray, float3, float2, float2);
1375 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", sampler2DArrayShadow, float4, float2,
1376 float2);
1377
1378 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler2D, float2, float2, float2,
1379 int2);
1380 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler3D, float3, float3, float3,
1381 int3);
1382 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGradOffset", sampler2DShadow, float3, float2,
1383 float2, int2);
1384 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler2DArray, float3, float2,
1385 float2, int2);
1386 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGradOffset", sampler2DArrayShadow, float4, float2,
1387 float2, int2);
1388
1389 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler2D, float3, float2, float2);
1390 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler2D, float4, float2, float2);
1391 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler3D, float4, float3, float3);
1392 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjGrad", sampler2DShadow, float4, float2,
1393 float2);
1394
1395 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler2D, float3, float2,
1396 float2, int2);
1397 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler2D, float4, float2,
1398 float2, int2);
1399 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler3D, float4, float3,
1400 float3, int3);
1401 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjGradOffset", sampler2DShadow, float4, float2,
1402 float2, int2);
1403
1404 const TType *atomicCounter = StaticType::GetBasic<EbtAtomicCounter>();
1405 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounter", atomicCounter);
1406 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounterIncrement", atomicCounter);
1407 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounterDecrement", atomicCounter);
1408
1409 // Insert all atomic memory functions
1410 const TType *int1InOut = StaticType::GetQualified<EbtInt, EvqInOut>();
1411 const TType *uint1InOut = StaticType::GetQualified<EbtUInt, EvqInOut>();
1412 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicAdd", uint1InOut, uint1);
1413 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicAdd", int1InOut, int1);
1414 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicMin", uint1InOut, uint1);
1415 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicMin", int1InOut, int1);
1416 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicMax", uint1InOut, uint1);
1417 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicMax", int1InOut, int1);
1418 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicAnd", uint1InOut, uint1);
1419 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicAnd", int1InOut, int1);
1420 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicOr", uint1InOut, uint1);
1421 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicOr", int1InOut, int1);
1422 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicXor", uint1InOut, uint1);
1423 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicXor", int1InOut, int1);
1424 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicExchange", uint1InOut, uint1);
1425 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicExchange", int1InOut, int1);
1426 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCompSwap", uint1InOut, uint1, uint1);
1427 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicCompSwap", int1InOut, int1, int1);
1428
1429 const TType *gimage2D = StaticType::GetBasic<EbtGImage2D>();
1430 const TType *gimage3D = StaticType::GetBasic<EbtGImage3D>();
1431 const TType *gimage2DArray = StaticType::GetBasic<EbtGImage2DArray>();
1432 const TType *gimageCube = StaticType::GetBasic<EbtGImageCube>();
1433
1434 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage2D, int2, gvec4);
1435 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage3D, int3, gvec4);
1436 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage2DArray, int3, gvec4);
1437 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimageCube, int3, gvec4);
1438
1439 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage2D, int2);
1440 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage3D, int3);
1441 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage2DArray, int3);
1442 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimageCube, int3);
1443
1444 insertBuiltIn(ESSL3_1_BUILTINS, int2, "imageSize", gimage2D);
1445 insertBuiltIn(ESSL3_1_BUILTINS, int3, "imageSize", gimage3D);
1446 insertBuiltIn(ESSL3_1_BUILTINS, int3, "imageSize", gimage2DArray);
1447 insertBuiltIn(ESSL3_1_BUILTINS, int2, "imageSize", gimageCube);
1448
1449 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrier, voidType,
1450 "memoryBarrier");
1451 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierAtomicCounter, voidType,
1452 "memoryBarrierAtomicCounter");
1453 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierBuffer, voidType,
1454 "memoryBarrierBuffer");
1455 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierImage, voidType,
1456 "memoryBarrierImage");
1457
1458 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "texelFetch", gsampler2DMS, int2, int1);
1459
1460 // Insert all variations of textureGather.
1461 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2D, float2);
1462 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2D, float2, int1);
1463 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2DArray, float3);
1464 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2DArray, float3, int1);
1465 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsamplerCube, float3);
1466 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsamplerCube, float3, int1);
1467 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DShadow, float2);
1468 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DShadow, float2, float1);
1469 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DArrayShadow, float3);
1470 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DArrayShadow, float3, float1);
1471 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", samplerCubeShadow, float3);
1472 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", samplerCubeShadow, float3, float1);
1473
1474 // Insert all variations of textureGatherOffset.
1475 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2D, float2, int2);
1476 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2D, float2, int2, int1);
1477 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2DArray, float3, int2);
1478 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2DArray, float3, int2,
1479 int1);
1480 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGatherOffset", sampler2DShadow, float2, float1,
1481 int2);
1482 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGatherOffset", sampler2DArrayShadow, float3,
1483 float1, int2);
1484
1485 if (type == GL_COMPUTE_SHADER)
1486 {
1487 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpBarrier, voidType, "barrier");
1488 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierShared, voidType,
1489 "memoryBarrierShared");
1490 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpGroupMemoryBarrier, voidType,
1491 "groupMemoryBarrier");
1492 }
1493
1494 if (type == GL_GEOMETRY_SHADER_EXT)
1495 {
1496 TExtension extension = TExtension::EXT_geometry_shader;
1497 insertBuiltInFunctionNoParametersExt(ESSL3_1_BUILTINS, extension, EOpEmitVertex, voidType,
1498 "EmitVertex");
1499 insertBuiltInFunctionNoParametersExt(ESSL3_1_BUILTINS, extension, EOpEndPrimitive, voidType,
1500 "EndPrimitive");
1501 }
1502}
1503
1504void TSymbolTable::initializeBuiltInVariables(sh::GLenum type,
1505 ShShaderSpec spec,
1506 const ShBuiltInResources &resources)
1507{
1508 const TSourceLoc zeroSourceLoc = {0, 0, 0, 0};
1509
1510 //
1511 // Depth range in window coordinates
1512 //
1513 TFieldList *fields = new TFieldList();
1514 auto highpFloat1 = new TType(EbtFloat, EbpHigh, EvqGlobal, 1);
1515 TField *near = new TField(highpFloat1, ImmutableString("near"), zeroSourceLoc);
1516 TField *far = new TField(highpFloat1, ImmutableString("far"), zeroSourceLoc);
1517 TField *diff = new TField(highpFloat1, ImmutableString("diff"), zeroSourceLoc);
1518 fields->push_back(near);
1519 fields->push_back(far);
1520 fields->push_back(diff);
1521 TStructure *depthRangeStruct = new TStructure(this, ImmutableString("gl_DepthRangeParameters"),
1522 fields, SymbolType::BuiltIn);
1523 insertStructType(COMMON_BUILTINS, depthRangeStruct);
1524 TType *depthRangeType = new TType(depthRangeStruct);
1525 depthRangeType->setQualifier(EvqUniform);
1526 depthRangeType->realize();
1527 insertVariable(COMMON_BUILTINS, ImmutableString("gl_DepthRange"), depthRangeType);
1528
1529 //
1530 // Implementation dependent built-in constants.
1531 //
1532 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexAttribs"),
1533 resources.MaxVertexAttribs);
1534 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexUniformVectors"),
1535 resources.MaxVertexUniformVectors);
1536 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexTextureImageUnits"),
1537 resources.MaxVertexTextureImageUnits);
1538 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxCombinedTextureImageUnits"),
1539 resources.MaxCombinedTextureImageUnits);
1540 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxTextureImageUnits"),
1541 resources.MaxTextureImageUnits);
1542 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxFragmentUniformVectors"),
1543 resources.MaxFragmentUniformVectors);
1544
1545 insertConstInt<EbpMedium>(ESSL1_BUILTINS, ImmutableString("gl_MaxVaryingVectors"),
1546 resources.MaxVaryingVectors);
1547
1548 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxDrawBuffers"),
1549 resources.MaxDrawBuffers);
1550 if (resources.EXT_blend_func_extended)
1551 {
1552 insertConstIntExt<EbpMedium>(COMMON_BUILTINS, TExtension::EXT_blend_func_extended,
1553 ImmutableString("gl_MaxDualSourceDrawBuffersEXT"),
1554 resources.MaxDualSourceDrawBuffers);
1555 }
1556
1557 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxVertexOutputVectors"),
1558 resources.MaxVertexOutputVectors);
1559 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxFragmentInputVectors"),
1560 resources.MaxFragmentInputVectors);
1561 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MinProgramTexelOffset"),
1562 resources.MinProgramTexelOffset);
1563 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxProgramTexelOffset"),
1564 resources.MaxProgramTexelOffset);
1565
1566 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxImageUnits"),
1567 resources.MaxImageUnits);
1568 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexImageUniforms"),
1569 resources.MaxVertexImageUniforms);
1570 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxFragmentImageUniforms"),
1571 resources.MaxFragmentImageUniforms);
1572 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeImageUniforms"),
1573 resources.MaxComputeImageUniforms);
1574 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxCombinedImageUniforms"),
1575 resources.MaxCombinedImageUniforms);
1576
1577 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1578 ImmutableString("gl_MaxCombinedShaderOutputResources"),
1579 resources.MaxCombinedShaderOutputResources);
1580
1581 insertConstIvec3<EbpHigh>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeWorkGroupCount"),
1582 resources.MaxComputeWorkGroupCount);
1583 insertConstIvec3<EbpHigh>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeWorkGroupSize"),
1584 resources.MaxComputeWorkGroupSize);
1585 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeUniformComponents"),
1586 resources.MaxComputeUniformComponents);
1587 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeTextureImageUnits"),
1588 resources.MaxComputeTextureImageUnits);
1589
1590 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeAtomicCounters"),
1591 resources.MaxComputeAtomicCounters);
1592 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1593 ImmutableString("gl_MaxComputeAtomicCounterBuffers"),
1594 resources.MaxComputeAtomicCounterBuffers);
1595
1596 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexAtomicCounters"),
1597 resources.MaxVertexAtomicCounters);
1598 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxFragmentAtomicCounters"),
1599 resources.MaxFragmentAtomicCounters);
1600 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxCombinedAtomicCounters"),
1601 resources.MaxCombinedAtomicCounters);
1602 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxAtomicCounterBindings"),
1603 resources.MaxAtomicCounterBindings);
1604
1605 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexAtomicCounterBuffers"),
1606 resources.MaxVertexAtomicCounterBuffers);
1607 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1608 ImmutableString("gl_MaxFragmentAtomicCounterBuffers"),
1609 resources.MaxFragmentAtomicCounterBuffers);
1610 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1611 ImmutableString("gl_MaxCombinedAtomicCounterBuffers"),
1612 resources.MaxCombinedAtomicCounterBuffers);
1613 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxAtomicCounterBufferSize"),
1614 resources.MaxAtomicCounterBufferSize);
1615
1616 if (resources.EXT_geometry_shader)
1617 {
1618 TExtension ext = TExtension::EXT_geometry_shader;
1619 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1620 ImmutableString("gl_MaxGeometryInputComponents"),
1621 resources.MaxGeometryInputComponents);
1622 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1623 ImmutableString("gl_MaxGeometryOutputComponents"),
1624 resources.MaxGeometryOutputComponents);
1625 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1626 ImmutableString("gl_MaxGeometryImageUniforms"),
1627 resources.MaxGeometryImageUniforms);
1628 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1629 ImmutableString("gl_MaxGeometryTextureImageUnits"),
1630 resources.MaxGeometryTextureImageUnits);
1631 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1632 ImmutableString("gl_MaxGeometryOutputVertices"),
1633 resources.MaxGeometryOutputVertices);
1634 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1635 ImmutableString("gl_MaxGeometryTotalOutputComponents"),
1636 resources.MaxGeometryTotalOutputComponents);
1637 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1638 ImmutableString("gl_MaxGeometryUniformComponents"),
1639 resources.MaxGeometryUniformComponents);
1640 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1641 ImmutableString("gl_MaxGeometryAtomicCounters"),
1642 resources.MaxGeometryAtomicCounters);
1643 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1644 ImmutableString("gl_MaxGeometryAtomicCounterBuffers"),
1645 resources.MaxGeometryAtomicCounterBuffers);
1646 }
1647
1648 //
1649 // Insert some special built-in variables that are not in
1650 // the built-in header files.
1651 //
1652
1653 if (resources.OVR_multiview && type != GL_COMPUTE_SHADER)
1654 {
1655 const TType *viewIDType = StaticType::Get<EbtUInt, EbpHigh, EvqViewIDOVR, 1, 1>();
1656 insertVariableExt(ESSL3_BUILTINS, TExtension::OVR_multiview,
1657 ImmutableString("gl_ViewID_OVR"), viewIDType);
1658
1659 // ESSL 1.00 doesn't have unsigned integers, so gl_ViewID_OVR is a signed integer in ESSL
1660 // 1.00. This is specified in the WEBGL_multiview spec.
1661 const TType *viewIDIntType = StaticType::Get<EbtInt, EbpHigh, EvqViewIDOVR, 1, 1>();
1662 insertVariableExt(ESSL1_BUILTINS, TExtension::OVR_multiview,
1663 ImmutableString("gl_ViewID_OVR"), viewIDIntType);
1664 }
1665
1666 const TType *positionType = StaticType::Get<EbtFloat, EbpHigh, EvqPosition, 4, 1>();
1667 const TType *primitiveIDType = StaticType::Get<EbtInt, EbpHigh, EvqPrimitiveID, 1, 1>();
1668 const TType *layerType = StaticType::Get<EbtInt, EbpHigh, EvqLayer, 1, 1>();
1669
1670 switch (type)
1671 {
1672 case GL_FRAGMENT_SHADER:
1673 {
1674 const TType *fragCoordType = StaticType::Get<EbtFloat, EbpMedium, EvqFragCoord, 4, 1>();
1675 insertVariable(COMMON_BUILTINS, ImmutableString("gl_FragCoord"), fragCoordType);
1676 const TType *frontFacingType = StaticType::GetQualified<EbtBool, EvqFrontFacing>();
1677 insertVariable(COMMON_BUILTINS, ImmutableString("gl_FrontFacing"), frontFacingType);
1678 const TType *pointCoordType =
1679 StaticType::Get<EbtFloat, EbpMedium, EvqPointCoord, 2, 1>();
1680 insertVariable(COMMON_BUILTINS, ImmutableString("gl_PointCoord"), pointCoordType);
1681
1682 const TType *fragColorType = StaticType::Get<EbtFloat, EbpMedium, EvqFragColor, 4, 1>();
1683 insertVariable(ESSL1_BUILTINS, ImmutableString("gl_FragColor"), fragColorType);
1684
1685 TType *fragDataType = new TType(EbtFloat, EbpMedium, EvqFragData, 4);
1686 if (spec != SH_WEBGL2_SPEC && spec != SH_WEBGL3_SPEC)
1687 {
1688 fragDataType->makeArray(resources.MaxDrawBuffers);
1689 }
1690 else
1691 {
1692 fragDataType->makeArray(1u);
1693 }
1694 fragDataType->realize();
1695 insertVariable(ESSL1_BUILTINS, ImmutableString("gl_FragData"), fragDataType);
1696
1697 if (resources.EXT_blend_func_extended)
1698 {
1699 const TType *secondaryFragColorType =
1700 StaticType::Get<EbtFloat, EbpMedium, EvqSecondaryFragColorEXT, 4, 1>();
1701 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_blend_func_extended,
1702 ImmutableString("gl_SecondaryFragColorEXT"),
1703 secondaryFragColorType);
1704 TType *secondaryFragDataType =
1705 new TType(EbtFloat, EbpMedium, EvqSecondaryFragDataEXT, 4, 1);
1706 secondaryFragDataType->makeArray(resources.MaxDualSourceDrawBuffers);
1707 secondaryFragDataType->realize();
1708 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_blend_func_extended,
1709 ImmutableString("gl_SecondaryFragDataEXT"),
1710 secondaryFragDataType);
1711 }
1712
1713 if (resources.EXT_frag_depth)
1714 {
1715 TType *fragDepthEXTType =
1716 new TType(EbtFloat, resources.FragmentPrecisionHigh ? EbpHigh : EbpMedium,
1717 EvqFragDepthEXT, 1);
1718 fragDepthEXTType->realize();
1719 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_frag_depth,
1720 ImmutableString("gl_FragDepthEXT"), fragDepthEXTType);
1721 }
1722
1723 const TType *fragDepthType = StaticType::Get<EbtFloat, EbpHigh, EvqFragDepth, 1, 1>();
1724 insertVariable(ESSL3_BUILTINS, ImmutableString("gl_FragDepth"), fragDepthType);
1725
1726 const TType *lastFragColorType =
1727 StaticType::Get<EbtFloat, EbpMedium, EvqLastFragColor, 4, 1>();
1728
1729 if (resources.EXT_shader_framebuffer_fetch || resources.NV_shader_framebuffer_fetch)
1730 {
1731 TType *lastFragDataType = new TType(EbtFloat, EbpMedium, EvqLastFragData, 4, 1);
1732 lastFragDataType->makeArray(resources.MaxDrawBuffers);
1733 lastFragDataType->realize();
1734
1735 if (resources.EXT_shader_framebuffer_fetch)
1736 {
1737 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_shader_framebuffer_fetch,
1738 ImmutableString("gl_LastFragData"), lastFragDataType);
1739 }
1740 else if (resources.NV_shader_framebuffer_fetch)
1741 {
1742 insertVariableExt(ESSL1_BUILTINS, TExtension::NV_shader_framebuffer_fetch,
1743 ImmutableString("gl_LastFragColor"), lastFragColorType);
1744 insertVariableExt(ESSL1_BUILTINS, TExtension::NV_shader_framebuffer_fetch,
1745 ImmutableString("gl_LastFragData"), lastFragDataType);
1746 }
1747 }
1748 else if (resources.ARM_shader_framebuffer_fetch)
1749 {
1750 insertVariableExt(ESSL1_BUILTINS, TExtension::ARM_shader_framebuffer_fetch,
1751 ImmutableString("gl_LastFragColorARM"), lastFragColorType);
1752 }
1753
1754 if (resources.EXT_geometry_shader)
1755 {
1756 TExtension extension = TExtension::EXT_geometry_shader;
1757 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveID"),
1758 primitiveIDType);
1759 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Layer"),
1760 layerType);
1761 }
1762
1763 break;
1764 }
1765 case GL_VERTEX_SHADER:
1766 {
1767 insertVariable(COMMON_BUILTINS, ImmutableString("gl_Position"), positionType);
1768 const TType *pointSizeType = StaticType::Get<EbtFloat, EbpMedium, EvqPointSize, 1, 1>();
1769 insertVariable(COMMON_BUILTINS, ImmutableString("gl_PointSize"), pointSizeType);
1770 const TType *instanceIDType = StaticType::Get<EbtInt, EbpHigh, EvqInstanceID, 1, 1>();
1771 insertVariable(ESSL3_BUILTINS, ImmutableString("gl_InstanceID"), instanceIDType);
1772 const TType *vertexIDType = StaticType::Get<EbtInt, EbpHigh, EvqVertexID, 1, 1>();
1773 insertVariable(ESSL3_BUILTINS, ImmutableString("gl_VertexID"), vertexIDType);
1774
1775 // For internal use by ANGLE - not exposed to the parser.
1776 const TType *viewportIndexType =
1777 StaticType::Get<EbtInt, EbpHigh, EvqViewportIndex, 1, 1>();
1778 insertVariable(GLSL_BUILTINS, ImmutableString("gl_ViewportIndex"), viewportIndexType);
1779 // gl_Layer exists in other shader stages in ESSL, but not in vertex shader so far.
1780 insertVariable(GLSL_BUILTINS, ImmutableString("gl_Layer"), layerType);
1781 break;
1782 }
1783 case GL_COMPUTE_SHADER:
1784 {
1785 const TType *numWorkGroupsType =
1786 StaticType::Get<EbtUInt, EbpUndefined, EvqNumWorkGroups, 3, 1>();
1787 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_NumWorkGroups"),
1788 numWorkGroupsType);
1789 const TType *workGroupSizeType =
1790 StaticType::Get<EbtUInt, EbpUndefined, EvqWorkGroupSize, 3, 1>();
1791 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_WorkGroupSize"),
1792 workGroupSizeType);
1793 const TType *workGroupIDType =
1794 StaticType::Get<EbtUInt, EbpUndefined, EvqWorkGroupID, 3, 1>();
1795 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_WorkGroupID"), workGroupIDType);
1796 const TType *localInvocationIDType =
1797 StaticType::Get<EbtUInt, EbpUndefined, EvqLocalInvocationID, 3, 1>();
1798 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_LocalInvocationID"),
1799 localInvocationIDType);
1800 const TType *globalInvocationIDType =
1801 StaticType::Get<EbtUInt, EbpUndefined, EvqGlobalInvocationID, 3, 1>();
1802 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_GlobalInvocationID"),
1803 globalInvocationIDType);
1804 const TType *localInvocationIndexType =
1805 StaticType::Get<EbtUInt, EbpUndefined, EvqLocalInvocationIndex, 1, 1>();
1806 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_LocalInvocationIndex"),
1807 localInvocationIndexType);
1808 break;
1809 }
1810
1811 case GL_GEOMETRY_SHADER_EXT:
1812 {
1813 TExtension extension = TExtension::EXT_geometry_shader;
1814
1815 // Add built-in interface block gl_PerVertex and the built-in array gl_in.
1816 // TODO(jiawei.shao@intel.com): implement GL_EXT_geometry_point_size.
1817 TFieldList *glPerVertexFieldList = new TFieldList();
1818 TField *glPositionField =
1819 new TField(new TType(*positionType), ImmutableString("gl_Position"), zeroSourceLoc);
1820 glPerVertexFieldList->push_back(glPositionField);
1821
1822 const ImmutableString glPerVertexString("gl_PerVertex");
1823 TInterfaceBlock *glPerVertexInBlock =
1824 new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList,
1825 TLayoutQualifier::Create(), SymbolType::BuiltIn, extension);
1826 insertInterfaceBlock(ESSL3_1_BUILTINS, glPerVertexInBlock);
1827
1828 // The array size of gl_in is undefined until we get a valid input primitive
1829 // declaration.
1830 TType *glInType =
1831 new TType(glPerVertexInBlock, EvqPerVertexIn, TLayoutQualifier::Create());
1832 glInType->makeArray(0u);
1833 glInType->realize();
1834 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_in"), glInType);
1835
1836 TInterfaceBlock *glPerVertexOutBlock =
1837 new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList,
1838 TLayoutQualifier::Create(), SymbolType::BuiltIn);
1839 TType *glPositionInBlockType = new TType(EbtFloat, EbpHigh, EvqPosition, 4);
1840 glPositionInBlockType->setInterfaceBlock(glPerVertexOutBlock);
1841 glPositionInBlockType->realize();
1842 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Position"),
1843 glPositionInBlockType);
1844
1845 const TType *primitiveIDInType =
1846 StaticType::Get<EbtInt, EbpHigh, EvqPrimitiveIDIn, 1, 1>();
1847 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveIDIn"),
1848 primitiveIDInType);
1849 const TType *invocationIDType =
1850 StaticType::Get<EbtInt, EbpHigh, EvqInvocationID, 1, 1>();
1851 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_InvocationID"),
1852 invocationIDType);
1853 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveID"),
1854 primitiveIDType);
1855 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Layer"), layerType);
1856
1857 break;
1858 }
1859 default:
1860 UNREACHABLE();
1861 }
1862}
1863
Jamie Madill45bcc782016-11-07 13:58:48 -05001864} // namespace sh