blob: 7f1273a624dcf45450c48b15789c4ff943392c9d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
Olli Etuaho0f684632017-07-13 12:42:15 +03006// Symbol table for parsing. The design principles and most of the functionality are documented in
7// the header file.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008//
9
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000010#if defined(_MSC_VER)
Jamie Madilld7b1ab52016-12-12 14:42:19 -050011#pragma warning(disable : 4718)
apatrick@chromium.orge057c5d2012-01-26 19:18:24 +000012#endif
13
Geoff Lang17732822013-08-29 13:46:49 -040014#include "compiler/translator/SymbolTable.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080015
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020016#include <algorithm>
17#include <set>
18
Olli Etuaho29bda812018-01-26 17:37:36 +020019#include "angle_gl.h"
Olli Etuaho2d8e4322018-01-22 14:12:46 +020020#include "compiler/translator/ImmutableString.h"
Olli Etuaho01d0ad02017-01-22 14:51:23 -080021#include "compiler/translator/IntermNode.h"
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020022#include "compiler/translator/StaticType.h"
kbr@chromium.org476541f2011-10-27 21:14:51 +000023
Jamie Madill45bcc782016-11-07 13:58:48 -050024namespace sh
25{
26
Olli Etuahodd21ecf2018-01-10 12:42:09 +020027class TSymbolTable::TSymbolTableLevel
28{
29 public:
30 TSymbolTableLevel() : mGlobalInvariant(false) {}
Olli Etuahodd21ecf2018-01-10 12:42:09 +020031
32 bool insert(TSymbol *symbol);
33
34 // Insert a function using its unmangled name as the key.
Olli Etuahob92f92a2018-02-15 19:14:59 +020035 void insertUnmangled(TFunction *function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +020036
Olli Etuahofbb1c792018-01-19 16:26:59 +020037 TSymbol *find(const ImmutableString &name) const;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020038
Olli Etuahodefe3932018-02-13 11:56:09 +020039 void addInvariantVarying(const ImmutableString &name) { mInvariantVaryings.insert(name); }
Olli Etuahodd21ecf2018-01-10 12:42:09 +020040
Olli Etuahodefe3932018-02-13 11:56:09 +020041 bool isVaryingInvariant(const ImmutableString &name)
Olli Etuahodd21ecf2018-01-10 12:42:09 +020042 {
43 return (mGlobalInvariant || mInvariantVaryings.count(name) > 0);
44 }
45
46 void setGlobalInvariant(bool invariant) { mGlobalInvariant = invariant; }
47
Olli Etuahodd21ecf2018-01-10 12:42:09 +020048 private:
Olli Etuahofbb1c792018-01-19 16:26:59 +020049 using tLevel = TUnorderedMap<ImmutableString,
50 TSymbol *,
51 ImmutableString::FowlerNollVoHash<sizeof(size_t)>>;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020052 using tLevelPair = const tLevel::value_type;
53 using tInsertResult = std::pair<tLevel::iterator, bool>;
54
55 tLevel level;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020056
Olli Etuahodefe3932018-02-13 11:56:09 +020057 std::set<ImmutableString> mInvariantVaryings;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020058 bool mGlobalInvariant;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020059};
60
61class TSymbolTable::TSymbolTableBuiltInLevel
62{
63 public:
64 TSymbolTableBuiltInLevel() = default;
65
66 bool insert(const TSymbol *symbol);
67
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020068 const TSymbol *find(const ImmutableString &name) const;
69
Olli Etuaho7c8567a2018-02-20 15:44:07 +020070 void insertUnmangledBuiltIn(const char *name, TExtension ext);
71 const UnmangledBuiltIn *getUnmangledBuiltIn(const ImmutableString &name) const;
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +020072
73 private:
74 using tLevel = TUnorderedMap<ImmutableString,
75 const TSymbol *,
76 ImmutableString::FowlerNollVoHash<sizeof(size_t)>>;
77 using tLevelPair = const tLevel::value_type;
78 using tInsertResult = std::pair<tLevel::iterator, bool>;
79
80 tLevel mLevel;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020081
Olli Etuaho7c8567a2018-02-20 15:44:07 +020082 std::map<ImmutableString, UnmangledBuiltIn> mUnmangledBuiltIns;
Olli Etuahodd21ecf2018-01-10 12:42:09 +020083};
84
Olli Etuahodd21ecf2018-01-10 12:42:09 +020085bool TSymbolTable::TSymbolTableLevel::insert(TSymbol *symbol)
Jamie Madillbfa91f42014-06-05 15:45:18 -040086{
Jamie Madillbfa91f42014-06-05 15:45:18 -040087 // returning true means symbol was added to the table
Nicolas Capensadfffe42014-06-17 02:13:36 -040088 tInsertResult result = level.insert(tLevelPair(symbol->getMangledName(), symbol));
Jamie Madillbfa91f42014-06-05 15:45:18 -040089 return result.second;
90}
91
Olli Etuahob92f92a2018-02-15 19:14:59 +020092void TSymbolTable::TSymbolTableLevel::insertUnmangled(TFunction *function)
Olli Etuahob2983c92015-03-18 14:02:46 +020093{
Olli Etuahob92f92a2018-02-15 19:14:59 +020094 level.insert(tLevelPair(function->name(), function));
Olli Etuahob2983c92015-03-18 14:02:46 +020095}
96
Olli Etuahofbb1c792018-01-19 16:26:59 +020097TSymbol *TSymbolTable::TSymbolTableLevel::find(const ImmutableString &name) const
Jamie Madillbfa91f42014-06-05 15:45:18 -040098{
99 tLevel::const_iterator it = level.find(name);
100 if (it == level.end())
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200101 return nullptr;
Jamie Madillbfa91f42014-06-05 15:45:18 -0400102 else
103 return (*it).second;
104}
105
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200106bool TSymbolTable::TSymbolTableBuiltInLevel::insert(const TSymbol *symbol)
107{
108 // returning true means symbol was added to the table
109 tInsertResult result = mLevel.insert(tLevelPair(symbol->getMangledName(), symbol));
110 return result.second;
111}
112
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200113const TSymbol *TSymbolTable::TSymbolTableBuiltInLevel::find(const ImmutableString &name) const
114{
115 tLevel::const_iterator it = mLevel.find(name);
116 if (it == mLevel.end())
117 return nullptr;
118 else
119 return (*it).second;
120}
121
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200122void TSymbolTable::TSymbolTableBuiltInLevel::insertUnmangledBuiltIn(const char *name,
123 TExtension ext)
Olli Etuaho342b83d2018-01-10 13:24:01 +0200124{
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200125 ImmutableString immName(name);
126 if (ext == TExtension::UNDEFINED ||
127 mUnmangledBuiltIns.find(immName) == mUnmangledBuiltIns.end())
128 {
129 mUnmangledBuiltIns[immName] = UnmangledBuiltIn(ext);
130 }
Olli Etuaho342b83d2018-01-10 13:24:01 +0200131}
132
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200133const UnmangledBuiltIn *TSymbolTable::TSymbolTableBuiltInLevel::getUnmangledBuiltIn(
134 const ImmutableString &name) const
Olli Etuaho342b83d2018-01-10 13:24:01 +0200135{
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200136 auto it = mUnmangledBuiltIns.find(ImmutableString(name));
137 if (it == mUnmangledBuiltIns.end())
138 {
139 return nullptr;
140 }
141 return &(it->second);
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
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200168const TFunction *TSymbolTable::markFunctionHasPrototypeDeclaration(
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
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200178const TFunction *TSymbolTable::setFunctionParameterNamesFromDefinition(const TFunction *function,
179 bool *wasDefinedOut)
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200180{
181 TFunction *firstDeclaration = findUserDefinedFunction(function->getMangledName());
182 ASSERT(firstDeclaration);
183 // Note: 'firstDeclaration' could be 'function' if this is the first time we've seen function as
184 // it would have just been put in the symbol table. Otherwise, we're looking up an earlier
185 // occurance.
186 if (function != firstDeclaration)
187 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200188 // The previous declaration should have the same parameters as the function definition
189 // (parameter names may differ).
190 firstDeclaration->shareParameters(*function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200191 }
192
193 *wasDefinedOut = firstDeclaration->isDefined();
194 firstDeclaration->setDefined();
195 return firstDeclaration;
196}
197
Olli Etuahofbb1c792018-01-19 16:26:59 +0200198const TSymbol *TSymbolTable::find(const ImmutableString &name, int shaderVersion) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200199{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200200 int userDefinedLevel = static_cast<int>(mTable.size()) - 1;
201 while (userDefinedLevel >= 0)
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000202 {
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200203 const TSymbol *symbol = mTable[userDefinedLevel]->find(name);
204 if (symbol)
205 {
206 return symbol;
207 }
208 userDefinedLevel--;
209 }
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000210
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200211 return findBuiltIn(name, shaderVersion, false);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000212}
213
Olli Etuahofbb1c792018-01-19 16:26:59 +0200214TFunction *TSymbolTable::findUserDefinedFunction(const ImmutableString &name) const
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200215{
216 // User-defined functions are always declared at the global level.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200217 ASSERT(!mTable.empty());
218 return static_cast<TFunction *>(mTable[0]->find(name));
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200219}
220
Olli Etuahofbb1c792018-01-19 16:26:59 +0200221const TSymbol *TSymbolTable::findGlobal(const ImmutableString &name) const
Zhenyao Mod7490962016-11-09 15:49:51 -0800222{
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200223 ASSERT(!mTable.empty());
224 return mTable[0]->find(name);
Zhenyao Mod7490962016-11-09 15:49:51 -0800225}
226
Olli Etuahofbb1c792018-01-19 16:26:59 +0200227const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name, int shaderVersion) const
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000228{
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300229 return findBuiltIn(name, shaderVersion, false);
230}
231
Olli Etuahofbb1c792018-01-19 16:26:59 +0200232const TSymbol *TSymbolTable::findBuiltIn(const ImmutableString &name,
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200233 int shaderVersion,
234 bool includeGLSLBuiltins) const
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300235{
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000236 for (int level = LAST_BUILTIN_LEVEL; level >= 0; level--)
237 {
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300238 if (level == GLSL_BUILTINS && !includeGLSLBuiltins)
239 level--;
Martin Radeve93d24e2016-07-28 12:06:05 +0300240 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
241 level--;
242 if (level == ESSL3_BUILTINS && shaderVersion < 300)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700243 level--;
244 if (level == ESSL1_BUILTINS && shaderVersion != 100)
245 level--;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000246
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200247 const TSymbol *symbol = mBuiltInTable[level]->find(name);
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000248
249 if (symbol)
250 return symbol;
251 }
252
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300253 return nullptr;
shannonwoods@chromium.org96e7ba12013-05-30 00:02:41 +0000254}
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400255
Kai Ninomiya030017a2017-12-06 14:06:53 -0800256constexpr bool IsGenType(const TType *type)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500257{
258 if (type)
259 {
260 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500261 return basicType == EbtGenType || basicType == EbtGenIType || basicType == EbtGenUType ||
262 basicType == EbtGenBType;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500263 }
264
265 return false;
266}
267
Kai Ninomiya030017a2017-12-06 14:06:53 -0800268constexpr bool IsVecType(const TType *type)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500269{
270 if (type)
271 {
272 TBasicType basicType = type->getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500273 return basicType == EbtVec || basicType == EbtIVec || basicType == EbtUVec ||
274 basicType == EbtBVec;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500275 }
276
277 return false;
278}
279
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800280constexpr const TType *SpecificType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500281{
282 ASSERT(size >= 1 && size <= 4);
283
284 if (!type)
285 {
286 return nullptr;
287 }
288
289 ASSERT(!IsVecType(type));
290
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500291 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500292 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500293 case EbtGenType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800294 return StaticType::GetForVec<EbtFloat>(type->getQualifier(),
295 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500296 case EbtGenIType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800297 return StaticType::GetForVec<EbtInt>(type->getQualifier(),
298 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500299 case EbtGenUType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800300 return StaticType::GetForVec<EbtUInt>(type->getQualifier(),
301 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500302 case EbtGenBType:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800303 return StaticType::GetForVec<EbtBool>(type->getQualifier(),
304 static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500305 default:
306 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500307 }
308}
309
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800310constexpr const TType *VectorType(const TType *type, int size)
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500311{
312 ASSERT(size >= 2 && size <= 4);
313
314 if (!type)
315 {
316 return nullptr;
317 }
318
319 ASSERT(!IsGenType(type));
320
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500321 switch (type->getBasicType())
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500322 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500323 case EbtVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800324 return StaticType::GetForVecMat<EbtFloat>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500325 case EbtIVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800326 return StaticType::GetForVecMat<EbtInt>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500327 case EbtUVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800328 return StaticType::GetForVecMat<EbtUInt>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500329 case EbtBVec:
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800330 return StaticType::GetForVecMat<EbtBool>(static_cast<unsigned char>(size));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500331 default:
332 return type;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500333 }
334}
335
Olli Etuaho195be942017-12-04 23:40:14 +0200336bool TSymbolTable::declareVariable(TVariable *variable)
Olli Etuaho0f684632017-07-13 12:42:15 +0300337{
Olli Etuaho195be942017-12-04 23:40:14 +0200338 ASSERT(variable->symbolType() == SymbolType::UserDefined);
339 return insertVariable(currentLevel(), variable);
Olli Etuaho0f684632017-07-13 12:42:15 +0300340}
341
Olli Etuaho035419f2017-11-28 14:27:15 +0200342bool TSymbolTable::declareStructType(TStructure *str)
Olli Etuaho0f684632017-07-13 12:42:15 +0300343{
344 return insertStructType(currentLevel(), str);
345}
346
Olli Etuaho378c3a52017-12-04 11:32:13 +0200347bool TSymbolTable::declareInterfaceBlock(TInterfaceBlock *interfaceBlock)
Olli Etuaho0f684632017-07-13 12:42:15 +0300348{
Olli Etuaho378c3a52017-12-04 11:32:13 +0200349 return insert(currentLevel(), interfaceBlock);
Jiawei Shaod8105a02017-08-08 09:54:36 +0800350}
351
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200352void TSymbolTable::declareUserDefinedFunction(TFunction *function, bool insertUnmangledName)
353{
354 ASSERT(currentLevel() >= GLOBAL_LEVEL);
355 if (insertUnmangledName)
356 {
357 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200358 mTable[0]->insertUnmangled(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200359 }
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200360 mTable[0]->insert(function);
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200361}
362
Olli Etuahofbb1c792018-01-19 16:26:59 +0200363TVariable *TSymbolTable::insertVariable(ESymbolLevel level,
364 const ImmutableString &name,
365 const TType *type)
Olli Etuaho0f684632017-07-13 12:42:15 +0300366{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100367 ASSERT(level <= LAST_BUILTIN_LEVEL);
Olli Etuahob60d30f2018-01-16 12:31:06 +0200368 ASSERT(type->isRealized());
Olli Etuahofbb1c792018-01-19 16:26:59 +0200369 return insertVariable(level, name, type, SymbolType::BuiltIn);
Olli Etuaho0f684632017-07-13 12:42:15 +0300370}
371
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100372TVariable *TSymbolTable::insertVariable(ESymbolLevel level,
Olli Etuahofbb1c792018-01-19 16:26:59 +0200373 const ImmutableString &name,
Olli Etuahob60d30f2018-01-16 12:31:06 +0200374 const TType *type,
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100375 SymbolType symbolType)
Olli Etuaho0f684632017-07-13 12:42:15 +0300376{
Olli Etuahob60d30f2018-01-16 12:31:06 +0200377 ASSERT(level > LAST_BUILTIN_LEVEL || type->isRealized());
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100378 TVariable *var = new TVariable(this, name, type, symbolType);
Olli Etuaho0f684632017-07-13 12:42:15 +0300379 if (insert(level, var))
380 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300381 return var;
382 }
383 return nullptr;
384}
385
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200386void TSymbolTable::insertVariableExt(ESymbolLevel level,
387 TExtension ext,
388 const ImmutableString &name,
389 const TType *type)
Olli Etuaho0f684632017-07-13 12:42:15 +0300390{
Olli Etuahob60d30f2018-01-16 12:31:06 +0200391 ASSERT(level <= LAST_BUILTIN_LEVEL);
392 ASSERT(type->isRealized());
Olli Etuahofbb1c792018-01-19 16:26:59 +0200393 TVariable *var = new TVariable(this, name, type, SymbolType::BuiltIn, ext);
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200394 bool inserted = insert(level, var);
395 UNUSED_VARIABLE(inserted);
396 ASSERT(inserted);
Olli Etuaho0f684632017-07-13 12:42:15 +0300397}
398
Olli Etuaho195be942017-12-04 23:40:14 +0200399bool TSymbolTable::insertVariable(ESymbolLevel level, TVariable *variable)
400{
401 ASSERT(variable);
Olli Etuahob60d30f2018-01-16 12:31:06 +0200402 ASSERT(level > LAST_BUILTIN_LEVEL || variable->getType().isRealized());
Olli Etuaho195be942017-12-04 23:40:14 +0200403 return insert(level, variable);
404}
405
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200406bool TSymbolTable::insert(ESymbolLevel level, TSymbol *symbol)
407{
408 ASSERT(level > LAST_BUILTIN_LEVEL || mUserDefinedUniqueIdsStart == -1);
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200409 if (level <= LAST_BUILTIN_LEVEL)
410 {
411 return mBuiltInTable[level]->insert(symbol);
412 }
413 else
414 {
415 return mTable[level - LAST_BUILTIN_LEVEL - 1]->insert(symbol);
416 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200417}
418
Olli Etuaho035419f2017-11-28 14:27:15 +0200419bool TSymbolTable::insertStructType(ESymbolLevel level, TStructure *str)
Olli Etuaho0f684632017-07-13 12:42:15 +0300420{
Olli Etuaho035419f2017-11-28 14:27:15 +0200421 ASSERT(str);
Olli Etuaho378c3a52017-12-04 11:32:13 +0200422 return insert(level, str);
423}
424
425bool TSymbolTable::insertInterfaceBlock(ESymbolLevel level, TInterfaceBlock *interfaceBlock)
426{
427 ASSERT(interfaceBlock);
428 return insert(level, interfaceBlock);
Olli Etuaho0f684632017-07-13 12:42:15 +0300429}
430
Olli Etuaho29bda812018-01-26 17:37:36 +0200431template <TPrecision precision>
432bool TSymbolTable::insertConstInt(ESymbolLevel level, const ImmutableString &name, int value)
433{
434 TVariable *constant = new TVariable(
435 this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn);
436 TConstantUnion *unionArray = new TConstantUnion[1];
437 unionArray[0].setIConst(value);
438 constant->shareConstPointer(unionArray);
439 return insert(level, constant);
440}
441
442template <TPrecision precision>
443bool TSymbolTable::insertConstIntExt(ESymbolLevel level,
444 TExtension ext,
445 const ImmutableString &name,
446 int value)
447{
448 TVariable *constant = new TVariable(
449 this, name, StaticType::Get<EbtInt, precision, EvqConst, 1, 1>(), SymbolType::BuiltIn, ext);
450 TConstantUnion *unionArray = new TConstantUnion[1];
451 unionArray[0].setIConst(value);
452 constant->shareConstPointer(unionArray);
453 return insert(level, constant);
454}
455
456template <TPrecision precision>
457bool TSymbolTable::insertConstIvec3(ESymbolLevel level,
458 const ImmutableString &name,
459 const std::array<int, 3> &values)
460{
461 TVariable *constantIvec3 = new TVariable(
462 this, name, StaticType::Get<EbtInt, precision, EvqConst, 3, 1>(), SymbolType::BuiltIn);
463
464 TConstantUnion *unionArray = new TConstantUnion[3];
465 for (size_t index = 0u; index < 3u; ++index)
466 {
467 unionArray[index].setIConst(values[index]);
468 }
469 constantIvec3->shareConstPointer(unionArray);
470
471 return insert(level, constantIvec3);
472}
473
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500474void TSymbolTable::insertBuiltIn(ESymbolLevel level,
475 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300476 TExtension ext,
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500477 const TType *rvalue,
478 const char *name,
479 const TType *ptype1,
480 const TType *ptype2,
481 const TType *ptype3,
482 const TType *ptype4,
483 const TType *ptype5)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700484{
485 if (ptype1->getBasicType() == EbtGSampler2D)
486 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200487 insertUnmangledBuiltIn(name, ext, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700488 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800489 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
490 StaticType::GetBasic<EbtSampler2D>(), ptype2, ptype3, ptype4, ptype5);
491 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
492 StaticType::GetBasic<EbtISampler2D>(), ptype2, ptype3, ptype4, ptype5);
493 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
494 StaticType::GetBasic<EbtUSampler2D>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700495 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500496 else if (ptype1->getBasicType() == EbtGSampler3D)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700497 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200498 insertUnmangledBuiltIn(name, ext, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700499 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800500 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
501 StaticType::GetBasic<EbtSampler3D>(), ptype2, ptype3, ptype4, ptype5);
502 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
503 StaticType::GetBasic<EbtISampler3D>(), ptype2, ptype3, ptype4, ptype5);
504 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
505 StaticType::GetBasic<EbtUSampler3D>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700506 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500507 else if (ptype1->getBasicType() == EbtGSamplerCube)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700508 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200509 insertUnmangledBuiltIn(name, ext, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700510 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800511 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
512 StaticType::GetBasic<EbtSamplerCube>(), ptype2, ptype3, ptype4, ptype5);
513 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
514 StaticType::GetBasic<EbtISamplerCube>(), ptype2, ptype3, ptype4, ptype5);
515 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
516 StaticType::GetBasic<EbtUSamplerCube>(), ptype2, ptype3, ptype4, ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700517 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500518 else if (ptype1->getBasicType() == EbtGSampler2DArray)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700519 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200520 insertUnmangledBuiltIn(name, ext, level);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700521 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800522 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
523 StaticType::GetBasic<EbtSampler2DArray>(), ptype2, ptype3, ptype4,
524 ptype5);
525 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
526 StaticType::GetBasic<EbtISampler2DArray>(), ptype2, ptype3, ptype4,
527 ptype5);
528 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
529 StaticType::GetBasic<EbtUSampler2DArray>(), ptype2, ptype3, ptype4,
530 ptype5);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700531 }
JiangYizhou40219322016-12-09 09:50:51 +0800532 else if (ptype1->getBasicType() == EbtGSampler2DMS)
533 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200534 insertUnmangledBuiltIn(name, ext, level);
JiangYizhou40219322016-12-09 09:50:51 +0800535 bool gvec4 = (rvalue->getBasicType() == EbtGVec4);
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800536 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtFloat, 4>() : rvalue, name,
537 StaticType::GetBasic<EbtSampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
538 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtInt, 4>() : rvalue, name,
539 StaticType::GetBasic<EbtISampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
540 insertBuiltIn(level, gvec4 ? StaticType::GetBasic<EbtUInt, 4>() : rvalue, name,
541 StaticType::GetBasic<EbtUSampler2DMS>(), ptype2, ptype3, ptype4, ptype5);
JiangYizhou40219322016-12-09 09:50:51 +0800542 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300543 else if (IsGImage(ptype1->getBasicType()))
544 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200545 insertUnmangledBuiltIn(name, ext, level);
Martin Radev2cc85b32016-08-05 16:22:53 +0300546
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800547 const TType *floatType = StaticType::GetBasic<EbtFloat, 4>();
548 const TType *intType = StaticType::GetBasic<EbtInt, 4>();
549 const TType *unsignedType = StaticType::GetBasic<EbtUInt, 4>();
Martin Radev2cc85b32016-08-05 16:22:53 +0300550
Kai Ninomiya614dd0f2017-11-22 14:04:48 -0800551 const TType *floatImage = StaticType::GetForFloatImage(ptype1->getBasicType());
552 const TType *intImage = StaticType::GetForIntImage(ptype1->getBasicType());
553 const TType *unsignedImage = StaticType::GetForUintImage(ptype1->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300554
555 // GLSL ES 3.10, Revision 4, 8.12 Image Functions
556 if (rvalue->getBasicType() == EbtGVec4)
557 {
558 // imageLoad
559 insertBuiltIn(level, floatType, name, floatImage, ptype2, ptype3, ptype4, ptype5);
560 insertBuiltIn(level, intType, name, intImage, ptype2, ptype3, ptype4, ptype5);
561 insertBuiltIn(level, unsignedType, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
562 }
563 else if (rvalue->getBasicType() == EbtVoid)
564 {
565 // imageStore
566 insertBuiltIn(level, rvalue, name, floatImage, ptype2, floatType, ptype4, ptype5);
567 insertBuiltIn(level, rvalue, name, intImage, ptype2, intType, ptype4, ptype5);
568 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, unsignedType, ptype4, ptype5);
569 }
570 else
571 {
572 // imageSize
573 insertBuiltIn(level, rvalue, name, floatImage, ptype2, ptype3, ptype4, ptype5);
574 insertBuiltIn(level, rvalue, name, intImage, ptype2, ptype3, ptype4, ptype5);
575 insertBuiltIn(level, rvalue, name, unsignedImage, ptype2, ptype3, ptype4, ptype5);
576 }
577 }
Olli Etuaho9250cb22017-01-21 10:51:27 +0000578 else if (IsGenType(rvalue) || IsGenType(ptype1) || IsGenType(ptype2) || IsGenType(ptype3) ||
579 IsGenType(ptype4))
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500580 {
Olli Etuaho9250cb22017-01-21 10:51:27 +0000581 ASSERT(!ptype5);
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200582 insertUnmangledBuiltIn(name, ext, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500583 insertBuiltIn(level, op, ext, SpecificType(rvalue, 1), name, SpecificType(ptype1, 1),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000584 SpecificType(ptype2, 1), SpecificType(ptype3, 1), SpecificType(ptype4, 1));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500585 insertBuiltIn(level, op, ext, SpecificType(rvalue, 2), name, SpecificType(ptype1, 2),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000586 SpecificType(ptype2, 2), SpecificType(ptype3, 2), SpecificType(ptype4, 2));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500587 insertBuiltIn(level, op, ext, SpecificType(rvalue, 3), name, SpecificType(ptype1, 3),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000588 SpecificType(ptype2, 3), SpecificType(ptype3, 3), SpecificType(ptype4, 3));
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500589 insertBuiltIn(level, op, ext, SpecificType(rvalue, 4), name, SpecificType(ptype1, 4),
Olli Etuaho9250cb22017-01-21 10:51:27 +0000590 SpecificType(ptype2, 4), SpecificType(ptype3, 4), SpecificType(ptype4, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500591 }
592 else if (IsVecType(rvalue) || IsVecType(ptype1) || IsVecType(ptype2) || IsVecType(ptype3))
593 {
594 ASSERT(!ptype4 && !ptype5);
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200595 insertUnmangledBuiltIn(name, ext, level);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500596 insertBuiltIn(level, op, ext, VectorType(rvalue, 2), name, VectorType(ptype1, 2),
597 VectorType(ptype2, 2), VectorType(ptype3, 2));
598 insertBuiltIn(level, op, ext, VectorType(rvalue, 3), name, VectorType(ptype1, 3),
599 VectorType(ptype2, 3), VectorType(ptype3, 3));
600 insertBuiltIn(level, op, ext, VectorType(rvalue, 4), name, VectorType(ptype1, 4),
601 VectorType(ptype2, 4), VectorType(ptype3, 4));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500602 }
603 else
604 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200605 size_t paramCount = 1;
606 TConstParameter *params = new TConstParameter[5];
607 new (params) TConstParameter(ptype1);
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700608
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500609 if (ptype2)
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700610 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200611 new (params + 1) TConstParameter(ptype2);
612 paramCount = 2;
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700613 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500614 if (ptype3)
615 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200616 new (params + 2) TConstParameter(ptype3);
617 paramCount = 3;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500618 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500619 if (ptype4)
620 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200621 new (params + 3) TConstParameter(ptype4);
622 paramCount = 4;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500623 }
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500624 if (ptype5)
625 {
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200626 new (params + 4) TConstParameter(ptype5);
627 paramCount = 5;
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500628 }
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200629 TFunction *function =
630 new TFunction(this, ImmutableString(name), ext, params, paramCount, rvalue, op, false);
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500631
Martin Radevda6254b2016-12-14 17:00:36 +0200632 ASSERT(hasUnmangledBuiltInAtLevel(name, level));
Nicolas Capensf3cc4ae2015-02-23 13:51:25 -0500633 insert(level, function);
634 }
Zhenyao Mo9eedea02014-05-12 16:02:35 -0700635}
636
Olli Etuaho492cfab2017-01-20 21:18:29 +0000637void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
638 TOperator op,
639 const TType *rvalue,
640 const TType *ptype1,
641 const TType *ptype2,
642 const TType *ptype3,
643 const TType *ptype4,
644 const TType *ptype5)
645{
646 const char *name = GetOperatorString(op);
647 ASSERT(strlen(name) > 0);
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200648 insertUnmangledBuiltIn(name, TExtension::UNDEFINED, level);
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300649 insertBuiltIn(level, op, TExtension::UNDEFINED, rvalue, name, ptype1, ptype2, ptype3, ptype4,
650 ptype5);
Olli Etuaho492cfab2017-01-20 21:18:29 +0000651}
652
653void TSymbolTable::insertBuiltInOp(ESymbolLevel level,
654 TOperator op,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300655 TExtension ext,
Olli Etuaho492cfab2017-01-20 21:18:29 +0000656 const TType *rvalue,
657 const TType *ptype1,
658 const TType *ptype2,
659 const TType *ptype3,
660 const TType *ptype4,
661 const TType *ptype5)
662{
663 const char *name = GetOperatorString(op);
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200664 insertUnmangledBuiltIn(name, ext, level);
Olli Etuaho492cfab2017-01-20 21:18:29 +0000665 insertBuiltIn(level, op, ext, rvalue, name, ptype1, ptype2, ptype3, ptype4, ptype5);
666}
667
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300668void TSymbolTable::insertBuiltInFunctionNoParameters(ESymbolLevel level,
669 TOperator op,
670 const TType *rvalue,
671 const char *name)
672{
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200673 insertUnmangledBuiltIn(name, TExtension::UNDEFINED, level);
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200674 insert(level, new TFunction(this, ImmutableString(name), TExtension::UNDEFINED, nullptr, 0,
675 rvalue, op, false));
Martin Radevd7c5b0a2016-07-27 14:04:43 +0300676}
677
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800678void TSymbolTable::insertBuiltInFunctionNoParametersExt(ESymbolLevel level,
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300679 TExtension ext,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800680 TOperator op,
681 const TType *rvalue,
682 const char *name)
683{
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200684 insertUnmangledBuiltIn(name, ext, level);
Olli Etuaho029e8ca2018-02-16 14:06:49 +0200685 insert(level, new TFunction(this, ImmutableString(name), ext, nullptr, 0, rvalue, op, false));
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
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200738void TSymbolTable::insertUnmangledBuiltIn(const char *name, TExtension ext, ESymbolLevel level)
Martin Radevda6254b2016-12-14 17:00:36 +0200739{
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 Etuaho7c8567a2018-02-20 15:44:07 +0200742 mBuiltInTable[level]->insertUnmangledBuiltIn(name, ext);
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);
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200748 return mBuiltInTable[level]->getUnmangledBuiltIn(ImmutableString(name)) != nullptr;
Martin Radevda6254b2016-12-14 17:00:36 +0200749}
750
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200751const UnmangledBuiltIn *TSymbolTable::getUnmangledBuiltInForShaderVersion(
752 const ImmutableString &name,
753 int shaderVersion)
Martin Radevda6254b2016-12-14 17:00:36 +0200754{
Martin Radevda6254b2016-12-14 17:00:36 +0200755 for (int level = LAST_BUILTIN_LEVEL; level >= 0; --level)
756 {
757 if (level == ESSL3_1_BUILTINS && shaderVersion != 310)
758 {
759 --level;
760 }
761 if (level == ESSL3_BUILTINS && shaderVersion < 300)
762 {
763 --level;
764 }
765 if (level == ESSL1_BUILTINS && shaderVersion != 100)
766 {
767 --level;
768 }
769
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200770 const UnmangledBuiltIn *builtIn = mBuiltInTable[level]->getUnmangledBuiltIn(name);
771 if (builtIn != nullptr)
Martin Radevda6254b2016-12-14 17:00:36 +0200772 {
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200773 return builtIn;
Martin Radevda6254b2016-12-14 17:00:36 +0200774 }
775 }
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200776 return nullptr;
Martin Radevda6254b2016-12-14 17:00:36 +0200777}
778
Olli Etuaho5d69db12017-11-24 16:51:15 +0200779void TSymbolTable::markBuiltInInitializationFinished()
780{
781 mUserDefinedUniqueIdsStart = mUniqueIdCounter;
782}
783
784void TSymbolTable::clearCompilationResults()
785{
786 mUniqueIdCounter = mUserDefinedUniqueIdsStart;
787
788 // User-defined scopes should have already been cleared when the compilation finished.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200789 ASSERT(mTable.size() == 0u);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200790}
791
792int TSymbolTable::nextUniqueIdValue()
793{
794 ASSERT(mUniqueIdCounter < std::numeric_limits<int>::max());
795 return ++mUniqueIdCounter;
796}
797
Olli Etuaho29bda812018-01-26 17:37:36 +0200798void TSymbolTable::initializeBuiltIns(sh::GLenum type,
799 ShShaderSpec spec,
800 const ShBuiltInResources &resources)
801{
802 ASSERT(isEmpty());
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200803 pushBuiltInLevel(); // COMMON_BUILTINS
804 pushBuiltInLevel(); // ESSL1_BUILTINS
805 pushBuiltInLevel(); // ESSL3_BUILTINS
806 pushBuiltInLevel(); // ESSL3_1_BUILTINS
807 pushBuiltInLevel(); // GLSL_BUILTINS
808
809 // We need just one precision stack level for predefined precisions.
810 mPrecisionStack.push_back(std::unique_ptr<PrecisionStackLevel>(new PrecisionStackLevel));
Olli Etuaho29bda812018-01-26 17:37:36 +0200811
812 switch (type)
813 {
814 case GL_FRAGMENT_SHADER:
815 setDefaultPrecision(EbtInt, EbpMedium);
816 break;
817 case GL_VERTEX_SHADER:
818 case GL_COMPUTE_SHADER:
819 case GL_GEOMETRY_SHADER_EXT:
820 setDefaultPrecision(EbtInt, EbpHigh);
821 setDefaultPrecision(EbtFloat, EbpHigh);
822 break;
823 default:
824 UNREACHABLE();
825 }
826 // Set defaults for sampler types that have default precision, even those that are
827 // only available if an extension exists.
828 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
829 initSamplerDefaultPrecision(EbtSampler2D);
830 initSamplerDefaultPrecision(EbtSamplerCube);
831 // SamplerExternalOES is specified in the extension to have default precision.
832 initSamplerDefaultPrecision(EbtSamplerExternalOES);
833 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
834 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
835 // It isn't specified whether Sampler2DRect has default precision.
836 initSamplerDefaultPrecision(EbtSampler2DRect);
837
838 setDefaultPrecision(EbtAtomicCounter, EbpHigh);
839
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200840 initializeBuiltInFunctions(type);
Olli Etuaho29bda812018-01-26 17:37:36 +0200841 initializeBuiltInVariables(type, spec, resources);
842 markBuiltInInitializationFinished();
843}
844
845void TSymbolTable::initSamplerDefaultPrecision(TBasicType samplerType)
846{
847 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
848 setDefaultPrecision(samplerType, EbpLow);
849}
850
Olli Etuaho7c8567a2018-02-20 15:44:07 +0200851void TSymbolTable::initializeBuiltInFunctions(sh::GLenum type)
Olli Etuaho29bda812018-01-26 17:37:36 +0200852{
853 const TType *voidType = StaticType::GetBasic<EbtVoid>();
854 const TType *float1 = StaticType::GetBasic<EbtFloat>();
855 const TType *float2 = StaticType::GetBasic<EbtFloat, 2>();
856 const TType *float3 = StaticType::GetBasic<EbtFloat, 3>();
857 const TType *float4 = StaticType::GetBasic<EbtFloat, 4>();
858 const TType *int1 = StaticType::GetBasic<EbtInt>();
859 const TType *int2 = StaticType::GetBasic<EbtInt, 2>();
860 const TType *int3 = StaticType::GetBasic<EbtInt, 3>();
861 const TType *uint1 = StaticType::GetBasic<EbtUInt>();
862 const TType *bool1 = StaticType::GetBasic<EbtBool>();
863 const TType *genType = StaticType::GetBasic<EbtGenType>();
864 const TType *genIType = StaticType::GetBasic<EbtGenIType>();
865 const TType *genUType = StaticType::GetBasic<EbtGenUType>();
866 const TType *genBType = StaticType::GetBasic<EbtGenBType>();
867
868 //
869 // Angle and Trigonometric Functions.
870 //
871 insertBuiltInOp(COMMON_BUILTINS, EOpRadians, genType, genType);
872 insertBuiltInOp(COMMON_BUILTINS, EOpDegrees, genType, genType);
873 insertBuiltInOp(COMMON_BUILTINS, EOpSin, genType, genType);
874 insertBuiltInOp(COMMON_BUILTINS, EOpCos, genType, genType);
875 insertBuiltInOp(COMMON_BUILTINS, EOpTan, genType, genType);
876 insertBuiltInOp(COMMON_BUILTINS, EOpAsin, genType, genType);
877 insertBuiltInOp(COMMON_BUILTINS, EOpAcos, genType, genType);
878 insertBuiltInOp(COMMON_BUILTINS, EOpAtan, genType, genType, genType);
879 insertBuiltInOp(COMMON_BUILTINS, EOpAtan, genType, genType);
880 insertBuiltInOp(ESSL3_BUILTINS, EOpSinh, genType, genType);
881 insertBuiltInOp(ESSL3_BUILTINS, EOpCosh, genType, genType);
882 insertBuiltInOp(ESSL3_BUILTINS, EOpTanh, genType, genType);
883 insertBuiltInOp(ESSL3_BUILTINS, EOpAsinh, genType, genType);
884 insertBuiltInOp(ESSL3_BUILTINS, EOpAcosh, genType, genType);
885 insertBuiltInOp(ESSL3_BUILTINS, EOpAtanh, genType, genType);
886
887 //
888 // Exponential Functions.
889 //
890 insertBuiltInOp(COMMON_BUILTINS, EOpPow, genType, genType, genType);
891 insertBuiltInOp(COMMON_BUILTINS, EOpExp, genType, genType);
892 insertBuiltInOp(COMMON_BUILTINS, EOpLog, genType, genType);
893 insertBuiltInOp(COMMON_BUILTINS, EOpExp2, genType, genType);
894 insertBuiltInOp(COMMON_BUILTINS, EOpLog2, genType, genType);
895 insertBuiltInOp(COMMON_BUILTINS, EOpSqrt, genType, genType);
896 insertBuiltInOp(COMMON_BUILTINS, EOpInverseSqrt, genType, genType);
897
898 //
899 // Common Functions.
900 //
901 insertBuiltInOp(COMMON_BUILTINS, EOpAbs, genType, genType);
902 insertBuiltInOp(ESSL3_BUILTINS, EOpAbs, genIType, genIType);
903 insertBuiltInOp(COMMON_BUILTINS, EOpSign, genType, genType);
904 insertBuiltInOp(ESSL3_BUILTINS, EOpSign, genIType, genIType);
905 insertBuiltInOp(COMMON_BUILTINS, EOpFloor, genType, genType);
906 insertBuiltInOp(ESSL3_BUILTINS, EOpTrunc, genType, genType);
907 insertBuiltInOp(ESSL3_BUILTINS, EOpRound, genType, genType);
908 insertBuiltInOp(ESSL3_BUILTINS, EOpRoundEven, genType, genType);
909 insertBuiltInOp(COMMON_BUILTINS, EOpCeil, genType, genType);
910 insertBuiltInOp(COMMON_BUILTINS, EOpFract, genType, genType);
911 insertBuiltInOp(COMMON_BUILTINS, EOpMod, genType, genType, float1);
912 insertBuiltInOp(COMMON_BUILTINS, EOpMod, genType, genType, genType);
913 insertBuiltInOp(COMMON_BUILTINS, EOpMin, genType, genType, float1);
914 insertBuiltInOp(COMMON_BUILTINS, EOpMin, genType, genType, genType);
915 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genIType, genIType, genIType);
916 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genIType, genIType, int1);
917 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genUType, genUType, genUType);
918 insertBuiltInOp(ESSL3_BUILTINS, EOpMin, genUType, genUType, uint1);
919 insertBuiltInOp(COMMON_BUILTINS, EOpMax, genType, genType, float1);
920 insertBuiltInOp(COMMON_BUILTINS, EOpMax, genType, genType, genType);
921 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genIType, genIType, genIType);
922 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genIType, genIType, int1);
923 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genUType, genUType, genUType);
924 insertBuiltInOp(ESSL3_BUILTINS, EOpMax, genUType, genUType, uint1);
925 insertBuiltInOp(COMMON_BUILTINS, EOpClamp, genType, genType, float1, float1);
926 insertBuiltInOp(COMMON_BUILTINS, EOpClamp, genType, genType, genType, genType);
927 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genIType, genIType, int1, int1);
928 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genIType, genIType, genIType, genIType);
929 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genUType, genUType, uint1, uint1);
930 insertBuiltInOp(ESSL3_BUILTINS, EOpClamp, genUType, genUType, genUType, genUType);
931 insertBuiltInOp(COMMON_BUILTINS, EOpMix, genType, genType, genType, float1);
932 insertBuiltInOp(COMMON_BUILTINS, EOpMix, genType, genType, genType, genType);
933 insertBuiltInOp(ESSL3_BUILTINS, EOpMix, genType, genType, genType, genBType);
934 insertBuiltInOp(COMMON_BUILTINS, EOpStep, genType, genType, genType);
935 insertBuiltInOp(COMMON_BUILTINS, EOpStep, genType, float1, genType);
936 insertBuiltInOp(COMMON_BUILTINS, EOpSmoothStep, genType, genType, genType, genType);
937 insertBuiltInOp(COMMON_BUILTINS, EOpSmoothStep, genType, float1, float1, genType);
938
939 const TType *outGenType = StaticType::GetQualified<EbtGenType, EvqOut>();
940 const TType *outGenIType = StaticType::GetQualified<EbtGenIType, EvqOut>();
941
942 insertBuiltInOp(ESSL3_BUILTINS, EOpModf, genType, genType, outGenType);
943
944 insertBuiltInOp(ESSL3_BUILTINS, EOpIsNan, genBType, genType);
945 insertBuiltInOp(ESSL3_BUILTINS, EOpIsInf, genBType, genType);
946 insertBuiltInOp(ESSL3_BUILTINS, EOpFloatBitsToInt, genIType, genType);
947 insertBuiltInOp(ESSL3_BUILTINS, EOpFloatBitsToUint, genUType, genType);
948 insertBuiltInOp(ESSL3_BUILTINS, EOpIntBitsToFloat, genType, genIType);
949 insertBuiltInOp(ESSL3_BUILTINS, EOpUintBitsToFloat, genType, genUType);
950
951 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFrexp, genType, genType, outGenIType);
952 insertBuiltInOp(ESSL3_1_BUILTINS, EOpLdexp, genType, genType, genIType);
953
954 insertBuiltInOp(ESSL3_BUILTINS, EOpPackSnorm2x16, uint1, float2);
955 insertBuiltInOp(ESSL3_BUILTINS, EOpPackUnorm2x16, uint1, float2);
956 insertBuiltInOp(ESSL3_BUILTINS, EOpPackHalf2x16, uint1, float2);
957 insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackSnorm2x16, float2, uint1);
958 insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackUnorm2x16, float2, uint1);
959 insertBuiltInOp(ESSL3_BUILTINS, EOpUnpackHalf2x16, float2, uint1);
960
961 insertBuiltInOp(ESSL3_1_BUILTINS, EOpPackUnorm4x8, uint1, float4);
962 insertBuiltInOp(ESSL3_1_BUILTINS, EOpPackSnorm4x8, uint1, float4);
963 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUnpackUnorm4x8, float4, uint1);
964 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUnpackSnorm4x8, float4, uint1);
965
966 //
967 // Geometric Functions.
968 //
969 insertBuiltInOp(COMMON_BUILTINS, EOpLength, float1, genType);
970 insertBuiltInOp(COMMON_BUILTINS, EOpDistance, float1, genType, genType);
971 insertBuiltInOp(COMMON_BUILTINS, EOpDot, float1, genType, genType);
972 insertBuiltInOp(COMMON_BUILTINS, EOpCross, float3, float3, float3);
973 insertBuiltInOp(COMMON_BUILTINS, EOpNormalize, genType, genType);
974 insertBuiltInOp(COMMON_BUILTINS, EOpFaceforward, genType, genType, genType, genType);
975 insertBuiltInOp(COMMON_BUILTINS, EOpReflect, genType, genType, genType);
976 insertBuiltInOp(COMMON_BUILTINS, EOpRefract, genType, genType, genType, float1);
977
978 const TType *mat2 = StaticType::GetBasic<EbtFloat, 2, 2>();
979 const TType *mat3 = StaticType::GetBasic<EbtFloat, 3, 3>();
980 const TType *mat4 = StaticType::GetBasic<EbtFloat, 4, 4>();
981 const TType *mat2x3 = StaticType::GetBasic<EbtFloat, 2, 3>();
982 const TType *mat3x2 = StaticType::GetBasic<EbtFloat, 3, 2>();
983 const TType *mat2x4 = StaticType::GetBasic<EbtFloat, 2, 4>();
984 const TType *mat4x2 = StaticType::GetBasic<EbtFloat, 4, 2>();
985 const TType *mat3x4 = StaticType::GetBasic<EbtFloat, 3, 4>();
986 const TType *mat4x3 = StaticType::GetBasic<EbtFloat, 4, 3>();
987
988 //
989 // Matrix Functions.
990 //
991 insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat2, mat2, mat2);
992 insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat3, mat3, mat3);
993 insertBuiltInOp(COMMON_BUILTINS, EOpMulMatrixComponentWise, mat4, mat4, mat4);
994 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat2x3, mat2x3, mat2x3);
995 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat3x2, mat3x2, mat3x2);
996 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat2x4, mat2x4, mat2x4);
997 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat4x2, mat4x2, mat4x2);
998 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat3x4, mat3x4, mat3x4);
999 insertBuiltInOp(ESSL3_BUILTINS, EOpMulMatrixComponentWise, mat4x3, mat4x3, mat4x3);
1000
1001 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2, float2, float2);
1002 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3, float3, float3);
1003 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4, float4, float4);
1004 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2x3, float3, float2);
1005 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3x2, float2, float3);
1006 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat2x4, float4, float2);
1007 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4x2, float2, float4);
1008 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat3x4, float4, float3);
1009 insertBuiltInOp(ESSL3_BUILTINS, EOpOuterProduct, mat4x3, float3, float4);
1010
1011 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2, mat2);
1012 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3, mat3);
1013 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4, mat4);
1014 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2x3, mat3x2);
1015 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3x2, mat2x3);
1016 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat2x4, mat4x2);
1017 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4x2, mat2x4);
1018 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat3x4, mat4x3);
1019 insertBuiltInOp(ESSL3_BUILTINS, EOpTranspose, mat4x3, mat3x4);
1020
1021 insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat2);
1022 insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat3);
1023 insertBuiltInOp(ESSL3_BUILTINS, EOpDeterminant, float1, mat4);
1024
1025 insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat2, mat2);
1026 insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat3, mat3);
1027 insertBuiltInOp(ESSL3_BUILTINS, EOpInverse, mat4, mat4);
1028
1029 const TType *vec = StaticType::GetBasic<EbtVec>();
1030 const TType *ivec = StaticType::GetBasic<EbtIVec>();
1031 const TType *uvec = StaticType::GetBasic<EbtUVec>();
1032 const TType *bvec = StaticType::GetBasic<EbtBVec>();
1033
1034 //
1035 // Vector relational functions.
1036 //
1037 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanComponentWise, bvec, vec, vec);
1038 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanComponentWise, bvec, ivec, ivec);
1039 insertBuiltInOp(ESSL3_BUILTINS, EOpLessThanComponentWise, bvec, uvec, uvec);
1040 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanEqualComponentWise, bvec, vec, vec);
1041 insertBuiltInOp(COMMON_BUILTINS, EOpLessThanEqualComponentWise, bvec, ivec, ivec);
1042 insertBuiltInOp(ESSL3_BUILTINS, EOpLessThanEqualComponentWise, bvec, uvec, uvec);
1043 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanComponentWise, bvec, vec, vec);
1044 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanComponentWise, bvec, ivec, ivec);
1045 insertBuiltInOp(ESSL3_BUILTINS, EOpGreaterThanComponentWise, bvec, uvec, uvec);
1046 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, vec, vec);
1047 insertBuiltInOp(COMMON_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, ivec, ivec);
1048 insertBuiltInOp(ESSL3_BUILTINS, EOpGreaterThanEqualComponentWise, bvec, uvec, uvec);
1049 insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, vec, vec);
1050 insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, ivec, ivec);
1051 insertBuiltInOp(ESSL3_BUILTINS, EOpEqualComponentWise, bvec, uvec, uvec);
1052 insertBuiltInOp(COMMON_BUILTINS, EOpEqualComponentWise, bvec, bvec, bvec);
1053 insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, vec, vec);
1054 insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, ivec, ivec);
1055 insertBuiltInOp(ESSL3_BUILTINS, EOpNotEqualComponentWise, bvec, uvec, uvec);
1056 insertBuiltInOp(COMMON_BUILTINS, EOpNotEqualComponentWise, bvec, bvec, bvec);
1057 insertBuiltInOp(COMMON_BUILTINS, EOpAny, bool1, bvec);
1058 insertBuiltInOp(COMMON_BUILTINS, EOpAll, bool1, bvec);
1059 insertBuiltInOp(COMMON_BUILTINS, EOpLogicalNotComponentWise, bvec, bvec);
1060
1061 //
1062 // Integer functions
1063 //
1064 const TType *outGenUType = StaticType::GetQualified<EbtGenUType, EvqOut>();
1065
1066 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldExtract, genIType, genIType, int1, int1);
1067 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldExtract, genUType, genUType, int1, int1);
1068 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldInsert, genIType, genIType, genIType, int1, int1);
1069 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldInsert, genUType, genUType, genUType, int1, int1);
1070 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldReverse, genIType, genIType);
1071 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitfieldReverse, genUType, genUType);
1072 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitCount, genIType, genIType);
1073 insertBuiltInOp(ESSL3_1_BUILTINS, EOpBitCount, genIType, genUType);
1074 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindLSB, genIType, genIType);
1075 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindLSB, genIType, genUType);
1076 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindMSB, genIType, genIType);
1077 insertBuiltInOp(ESSL3_1_BUILTINS, EOpFindMSB, genIType, genUType);
1078 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUaddCarry, genUType, genUType, genUType, outGenUType);
1079 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUsubBorrow, genUType, genUType, genUType, outGenUType);
1080 insertBuiltInOp(ESSL3_1_BUILTINS, EOpUmulExtended, voidType, genUType, genUType, outGenUType,
1081 outGenUType);
1082 insertBuiltInOp(ESSL3_1_BUILTINS, EOpImulExtended, voidType, genIType, genIType, outGenIType,
1083 outGenIType);
1084
1085 const TType *sampler2D = StaticType::GetBasic<EbtSampler2D>();
1086 const TType *samplerCube = StaticType::GetBasic<EbtSamplerCube>();
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001087 const TType *samplerExternalOES = StaticType::GetBasic<EbtSamplerExternalOES>();
Olli Etuaho29bda812018-01-26 17:37:36 +02001088
1089 //
1090 // Texture Functions for GLSL ES 1.0
1091 //
1092 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", sampler2D, float2);
1093 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float3);
1094 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float4);
1095 insertBuiltIn(ESSL1_BUILTINS, float4, "textureCube", samplerCube, float3);
1096
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001097 // These are extension functions from OES_EGL_image_external and
1098 // NV_EGL_stream_consumer_external. We don't have a way to mark a built-in with two alternative
1099 // extensions, so these are marked with none. This is fine, since these functions overload core
1100 // function names and the functions require a samplerExternalOES parameter, which can only be
1101 // created if one of the extensions is enabled.
1102 // TODO(oetuaho): Consider implementing a cleaner solution.
1103 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", samplerExternalOES, float2);
1104 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", samplerExternalOES, float3);
1105 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", samplerExternalOES, float4);
Olli Etuaho29bda812018-01-26 17:37:36 +02001106
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001107 // Note that ARB_texture_rectangle extension directive defaults to enabled in case the extension
1108 // is supported. The symbols are still conditional on the extension.
1109 const TType *sampler2DRect = StaticType::GetBasic<EbtSampler2DRect>();
1110 insertBuiltIn(ESSL1_BUILTINS, TExtension::ARB_texture_rectangle, float4, "texture2DRect",
1111 sampler2DRect, float2);
1112 insertBuiltIn(ESSL1_BUILTINS, TExtension::ARB_texture_rectangle, float4, "texture2DRectProj",
1113 sampler2DRect, float3);
1114 insertBuiltIn(ESSL1_BUILTINS, TExtension::ARB_texture_rectangle, float4, "texture2DRectProj",
1115 sampler2DRect, float4);
Olli Etuaho29bda812018-01-26 17:37:36 +02001116
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001117 /* The *Grad* variants are new to both vertex and fragment shaders; the fragment
1118 * shader specific pieces are added separately below.
1119 */
1120 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4, "texture2DGradEXT",
1121 sampler2D, float2, float2, float2);
1122 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1123 "texture2DProjGradEXT", sampler2D, float3, float2, float2);
1124 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1125 "texture2DProjGradEXT", sampler2D, float4, float2, float2);
1126 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4, "textureCubeGradEXT",
1127 samplerCube, float3, float3, float3);
Olli Etuaho29bda812018-01-26 17:37:36 +02001128
1129 if (type == GL_FRAGMENT_SHADER)
1130 {
1131 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2D", sampler2D, float2, float1);
1132 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float3, float1);
1133 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProj", sampler2D, float4, float1);
1134 insertBuiltIn(ESSL1_BUILTINS, float4, "textureCube", samplerCube, float3, float1);
1135
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001136 insertBuiltInOp(ESSL1_BUILTINS, EOpDFdx, TExtension::OES_standard_derivatives, genType,
1137 genType);
1138 insertBuiltInOp(ESSL1_BUILTINS, EOpDFdy, TExtension::OES_standard_derivatives, genType,
1139 genType);
1140 insertBuiltInOp(ESSL1_BUILTINS, EOpFwidth, TExtension::OES_standard_derivatives, genType,
1141 genType);
Olli Etuaho29bda812018-01-26 17:37:36 +02001142
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001143 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4, "texture2DLodEXT",
1144 sampler2D, float2, float1);
1145 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1146 "texture2DProjLodEXT", sampler2D, float3, float1);
1147 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1148 "texture2DProjLodEXT", sampler2D, float4, float1);
1149 insertBuiltIn(ESSL1_BUILTINS, TExtension::EXT_shader_texture_lod, float4,
1150 "textureCubeLodEXT", samplerCube, float3, float1);
Olli Etuaho29bda812018-01-26 17:37:36 +02001151 }
1152
1153 if (type == GL_VERTEX_SHADER)
1154 {
1155 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DLod", sampler2D, float2, float1);
1156 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProjLod", sampler2D, float3, float1);
1157 insertBuiltIn(ESSL1_BUILTINS, float4, "texture2DProjLod", sampler2D, float4, float1);
1158 insertBuiltIn(ESSL1_BUILTINS, float4, "textureCubeLod", samplerCube, float3, float1);
1159 }
1160
1161 const TType *gvec4 = StaticType::GetBasic<EbtGVec4>();
1162
1163 const TType *gsampler2D = StaticType::GetBasic<EbtGSampler2D>();
1164 const TType *gsamplerCube = StaticType::GetBasic<EbtGSamplerCube>();
1165 const TType *gsampler3D = StaticType::GetBasic<EbtGSampler3D>();
1166 const TType *gsampler2DArray = StaticType::GetBasic<EbtGSampler2DArray>();
1167 const TType *gsampler2DMS = StaticType::GetBasic<EbtGSampler2DMS>();
1168
1169 //
1170 // Texture Functions for GLSL ES 3.0
1171 //
1172 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2D, float2);
1173 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler3D, float3);
1174 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsamplerCube, float3);
1175 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2DArray, float3);
1176 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float3);
1177 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float4);
1178 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler3D, float4);
1179 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler2D, float2, float1);
1180 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler3D, float3, float1);
1181 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsamplerCube, float3, float1);
1182 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLod", gsampler2DArray, float3, float1);
1183
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001184 insertBuiltIn(ESSL3_BUILTINS, TExtension::OES_EGL_image_external_essl3, float4, "texture",
1185 samplerExternalOES, float2);
1186 insertBuiltIn(ESSL3_BUILTINS, TExtension::OES_EGL_image_external_essl3, float4, "textureProj",
1187 samplerExternalOES, float3);
1188 insertBuiltIn(ESSL3_BUILTINS, TExtension::OES_EGL_image_external_essl3, float4, "textureProj",
1189 samplerExternalOES, float4);
Olli Etuaho29bda812018-01-26 17:37:36 +02001190
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001191 const TType *samplerExternal2DY2YEXT = StaticType::GetBasic<EbtSamplerExternal2DY2YEXT>();
Olli Etuaho29bda812018-01-26 17:37:36 +02001192
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001193 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texture",
1194 samplerExternal2DY2YEXT, float2);
1195 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1196 samplerExternal2DY2YEXT, float3);
1197 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1198 samplerExternal2DY2YEXT, float4);
Olli Etuaho29bda812018-01-26 17:37:36 +02001199
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001200 const TType *yuvCscStandardEXT = StaticType::GetBasic<EbtYuvCscStandardEXT>();
Olli Etuaho29bda812018-01-26 17:37:36 +02001201
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001202 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float3, "rgb_2_yuv", float3,
1203 yuvCscStandardEXT);
1204 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float3, "yuv_2_rgb", float3,
1205 yuvCscStandardEXT);
Olli Etuaho29bda812018-01-26 17:37:36 +02001206
1207 if (type == GL_FRAGMENT_SHADER)
1208 {
1209 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2D, float2, float1);
1210 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler3D, float3, float1);
1211 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsamplerCube, float3, float1);
1212 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texture", gsampler2DArray, float3, float1);
1213 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float3, float1);
1214 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler2D, float4, float1);
1215 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProj", gsampler3D, float4, float1);
1216
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001217 insertBuiltIn(ESSL3_BUILTINS, TExtension::OES_EGL_image_external_essl3, float4, "texture",
1218 samplerExternalOES, float2, float1);
1219 insertBuiltIn(ESSL3_BUILTINS, TExtension::OES_EGL_image_external_essl3, float4,
1220 "textureProj", samplerExternalOES, float3, float1);
1221 insertBuiltIn(ESSL3_BUILTINS, TExtension::OES_EGL_image_external_essl3, float4,
1222 "textureProj", samplerExternalOES, float4, float1);
Olli Etuaho29bda812018-01-26 17:37:36 +02001223
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001224 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texture",
1225 samplerExternal2DY2YEXT, float2, float1);
1226 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1227 samplerExternal2DY2YEXT, float3, float1);
1228 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "textureProj",
1229 samplerExternal2DY2YEXT, float4, float1);
Olli Etuaho29bda812018-01-26 17:37:36 +02001230 }
1231
1232 const TType *sampler2DShadow = StaticType::GetBasic<EbtSampler2DShadow>();
1233 const TType *samplerCubeShadow = StaticType::GetBasic<EbtSamplerCubeShadow>();
1234 const TType *sampler2DArrayShadow = StaticType::GetBasic<EbtSampler2DArrayShadow>();
1235
1236 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DShadow, float3);
1237 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", samplerCubeShadow, float4);
1238 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DArrayShadow, float4);
1239 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProj", sampler2DShadow, float4);
1240 insertBuiltIn(ESSL3_BUILTINS, float1, "textureLod", sampler2DShadow, float3, float1);
1241
1242 if (type == GL_FRAGMENT_SHADER)
1243 {
1244 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", sampler2DShadow, float3, float1);
1245 insertBuiltIn(ESSL3_BUILTINS, float1, "texture", samplerCubeShadow, float4, float1);
1246 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProj", sampler2DShadow, float4, float1);
1247 }
1248
1249 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsampler2D, int1);
1250 insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", gsampler3D, int1);
1251 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsamplerCube, int1);
1252 insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", gsampler2DArray, int1);
1253 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", sampler2DShadow, int1);
1254 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", samplerCubeShadow, int1);
1255 insertBuiltIn(ESSL3_BUILTINS, int3, "textureSize", sampler2DArrayShadow, int1);
1256 insertBuiltIn(ESSL3_BUILTINS, int2, "textureSize", gsampler2DMS);
1257
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001258 insertBuiltIn(ESSL3_BUILTINS, TExtension::OES_EGL_image_external_essl3, int2, "textureSize",
1259 samplerExternalOES, int1);
Olli Etuaho29bda812018-01-26 17:37:36 +02001260
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001261 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, int2, "textureSize",
1262 samplerExternal2DY2YEXT, int1);
Olli Etuaho29bda812018-01-26 17:37:36 +02001263
1264 if (type == GL_FRAGMENT_SHADER)
1265 {
1266 insertBuiltInOp(ESSL3_BUILTINS, EOpDFdx, genType, genType);
1267 insertBuiltInOp(ESSL3_BUILTINS, EOpDFdy, genType, genType);
1268 insertBuiltInOp(ESSL3_BUILTINS, EOpFwidth, genType, genType);
1269 }
1270
1271 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2D, float2, int2);
1272 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler3D, float3, int3);
1273 insertBuiltIn(ESSL3_BUILTINS, float1, "textureOffset", sampler2DShadow, float3, int2);
1274 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2DArray, float3, int2);
1275
1276 if (type == GL_FRAGMENT_SHADER)
1277 {
1278 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2D, float2, int2, float1);
1279 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler3D, float3, int3, float1);
1280 insertBuiltIn(ESSL3_BUILTINS, float1, "textureOffset", sampler2DShadow, float3, int2,
1281 float1);
1282 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureOffset", gsampler2DArray, float3, int2,
1283 float1);
1284 }
1285
1286 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float3, int2);
1287 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float4, int2);
1288 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler3D, float4, int3);
1289 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjOffset", sampler2DShadow, float4, int2);
1290
1291 if (type == GL_FRAGMENT_SHADER)
1292 {
1293 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float3, int2, float1);
1294 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler2D, float4, int2, float1);
1295 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjOffset", gsampler3D, float4, int3, float1);
1296 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjOffset", sampler2DShadow, float4, int2,
1297 float1);
1298 }
1299
1300 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler2D, float2, float1, int2);
1301 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler3D, float3, float1, int3);
1302 insertBuiltIn(ESSL3_BUILTINS, float1, "textureLodOffset", sampler2DShadow, float3, float1,
1303 int2);
1304 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureLodOffset", gsampler2DArray, float3, float1, int2);
1305
1306 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler2D, float3, float1);
1307 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler2D, float4, float1);
1308 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLod", gsampler3D, float4, float1);
1309 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjLod", sampler2DShadow, float4, float1);
1310
1311 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler2D, float3, float1, int2);
1312 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler2D, float4, float1, int2);
1313 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjLodOffset", gsampler3D, float4, float1, int3);
1314 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjLodOffset", sampler2DShadow, float4, float1,
1315 int2);
1316
1317 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler2D, int2, int1);
1318 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler3D, int3, int1);
1319 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetch", gsampler2DArray, int3, int1);
1320
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001321 insertBuiltIn(ESSL3_BUILTINS, TExtension::OES_EGL_image_external_essl3, float4, "texelFetch",
1322 samplerExternalOES, int2, int1);
Olli Etuaho29bda812018-01-26 17:37:36 +02001323
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001324 insertBuiltIn(ESSL3_BUILTINS, TExtension::EXT_YUV_target, float4, "texelFetch",
1325 samplerExternal2DY2YEXT, int2, int1);
Olli Etuaho29bda812018-01-26 17:37:36 +02001326
1327 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler2D, int2, int1, int2);
1328 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler3D, int3, int1, int3);
1329 insertBuiltIn(ESSL3_BUILTINS, gvec4, "texelFetchOffset", gsampler2DArray, int3, int1, int2);
1330
1331 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler2D, float2, float2, float2);
1332 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler3D, float3, float3, float3);
1333 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsamplerCube, float3, float3, float3);
1334 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", sampler2DShadow, float3, float2, float2);
1335 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", samplerCubeShadow, float4, float3, float3);
1336 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGrad", gsampler2DArray, float3, float2, float2);
1337 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGrad", sampler2DArrayShadow, float4, float2,
1338 float2);
1339
1340 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler2D, float2, float2, float2,
1341 int2);
1342 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler3D, float3, float3, float3,
1343 int3);
1344 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGradOffset", sampler2DShadow, float3, float2,
1345 float2, int2);
1346 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureGradOffset", gsampler2DArray, float3, float2,
1347 float2, int2);
1348 insertBuiltIn(ESSL3_BUILTINS, float1, "textureGradOffset", sampler2DArrayShadow, float4, float2,
1349 float2, int2);
1350
1351 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler2D, float3, float2, float2);
1352 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler2D, float4, float2, float2);
1353 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGrad", gsampler3D, float4, float3, float3);
1354 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjGrad", sampler2DShadow, float4, float2,
1355 float2);
1356
1357 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler2D, float3, float2,
1358 float2, int2);
1359 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler2D, float4, float2,
1360 float2, int2);
1361 insertBuiltIn(ESSL3_BUILTINS, gvec4, "textureProjGradOffset", gsampler3D, float4, float3,
1362 float3, int3);
1363 insertBuiltIn(ESSL3_BUILTINS, float1, "textureProjGradOffset", sampler2DShadow, float4, float2,
1364 float2, int2);
1365
1366 const TType *atomicCounter = StaticType::GetBasic<EbtAtomicCounter>();
1367 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounter", atomicCounter);
1368 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounterIncrement", atomicCounter);
1369 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCounterDecrement", atomicCounter);
1370
1371 // Insert all atomic memory functions
1372 const TType *int1InOut = StaticType::GetQualified<EbtInt, EvqInOut>();
1373 const TType *uint1InOut = StaticType::GetQualified<EbtUInt, EvqInOut>();
1374 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicAdd", uint1InOut, uint1);
1375 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicAdd", int1InOut, int1);
1376 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicMin", uint1InOut, uint1);
1377 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicMin", int1InOut, int1);
1378 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicMax", uint1InOut, uint1);
1379 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicMax", int1InOut, int1);
1380 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicAnd", uint1InOut, uint1);
1381 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicAnd", int1InOut, int1);
1382 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicOr", uint1InOut, uint1);
1383 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicOr", int1InOut, int1);
1384 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicXor", uint1InOut, uint1);
1385 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicXor", int1InOut, int1);
1386 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicExchange", uint1InOut, uint1);
1387 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicExchange", int1InOut, int1);
1388 insertBuiltIn(ESSL3_1_BUILTINS, uint1, "atomicCompSwap", uint1InOut, uint1, uint1);
1389 insertBuiltIn(ESSL3_1_BUILTINS, int1, "atomicCompSwap", int1InOut, int1, int1);
1390
1391 const TType *gimage2D = StaticType::GetBasic<EbtGImage2D>();
1392 const TType *gimage3D = StaticType::GetBasic<EbtGImage3D>();
1393 const TType *gimage2DArray = StaticType::GetBasic<EbtGImage2DArray>();
1394 const TType *gimageCube = StaticType::GetBasic<EbtGImageCube>();
1395
1396 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage2D, int2, gvec4);
1397 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage3D, int3, gvec4);
1398 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimage2DArray, int3, gvec4);
1399 insertBuiltIn(ESSL3_1_BUILTINS, voidType, "imageStore", gimageCube, int3, gvec4);
1400
1401 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage2D, int2);
1402 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage3D, int3);
1403 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimage2DArray, int3);
1404 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "imageLoad", gimageCube, int3);
1405
1406 insertBuiltIn(ESSL3_1_BUILTINS, int2, "imageSize", gimage2D);
1407 insertBuiltIn(ESSL3_1_BUILTINS, int3, "imageSize", gimage3D);
1408 insertBuiltIn(ESSL3_1_BUILTINS, int3, "imageSize", gimage2DArray);
1409 insertBuiltIn(ESSL3_1_BUILTINS, int2, "imageSize", gimageCube);
1410
1411 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrier, voidType,
1412 "memoryBarrier");
1413 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierAtomicCounter, voidType,
1414 "memoryBarrierAtomicCounter");
1415 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierBuffer, voidType,
1416 "memoryBarrierBuffer");
1417 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierImage, voidType,
1418 "memoryBarrierImage");
1419
1420 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "texelFetch", gsampler2DMS, int2, int1);
1421
1422 // Insert all variations of textureGather.
1423 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2D, float2);
1424 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2D, float2, int1);
1425 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2DArray, float3);
1426 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsampler2DArray, float3, int1);
1427 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsamplerCube, float3);
1428 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGather", gsamplerCube, float3, int1);
1429 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DShadow, float2);
1430 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DShadow, float2, float1);
1431 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DArrayShadow, float3);
1432 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", sampler2DArrayShadow, float3, float1);
1433 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", samplerCubeShadow, float3);
1434 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGather", samplerCubeShadow, float3, float1);
1435
1436 // Insert all variations of textureGatherOffset.
1437 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2D, float2, int2);
1438 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2D, float2, int2, int1);
1439 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2DArray, float3, int2);
1440 insertBuiltIn(ESSL3_1_BUILTINS, gvec4, "textureGatherOffset", gsampler2DArray, float3, int2,
1441 int1);
1442 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGatherOffset", sampler2DShadow, float2, float1,
1443 int2);
1444 insertBuiltIn(ESSL3_1_BUILTINS, float4, "textureGatherOffset", sampler2DArrayShadow, float3,
1445 float1, int2);
1446
1447 if (type == GL_COMPUTE_SHADER)
1448 {
1449 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpBarrier, voidType, "barrier");
1450 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpMemoryBarrierShared, voidType,
1451 "memoryBarrierShared");
1452 insertBuiltInFunctionNoParameters(ESSL3_1_BUILTINS, EOpGroupMemoryBarrier, voidType,
1453 "groupMemoryBarrier");
1454 }
1455
1456 if (type == GL_GEOMETRY_SHADER_EXT)
1457 {
1458 TExtension extension = TExtension::EXT_geometry_shader;
1459 insertBuiltInFunctionNoParametersExt(ESSL3_1_BUILTINS, extension, EOpEmitVertex, voidType,
1460 "EmitVertex");
1461 insertBuiltInFunctionNoParametersExt(ESSL3_1_BUILTINS, extension, EOpEndPrimitive, voidType,
1462 "EndPrimitive");
1463 }
1464}
1465
1466void TSymbolTable::initializeBuiltInVariables(sh::GLenum type,
1467 ShShaderSpec spec,
1468 const ShBuiltInResources &resources)
1469{
1470 const TSourceLoc zeroSourceLoc = {0, 0, 0, 0};
1471
1472 //
1473 // Depth range in window coordinates
1474 //
1475 TFieldList *fields = new TFieldList();
1476 auto highpFloat1 = new TType(EbtFloat, EbpHigh, EvqGlobal, 1);
1477 TField *near = new TField(highpFloat1, ImmutableString("near"), zeroSourceLoc);
1478 TField *far = new TField(highpFloat1, ImmutableString("far"), zeroSourceLoc);
1479 TField *diff = new TField(highpFloat1, ImmutableString("diff"), zeroSourceLoc);
1480 fields->push_back(near);
1481 fields->push_back(far);
1482 fields->push_back(diff);
1483 TStructure *depthRangeStruct = new TStructure(this, ImmutableString("gl_DepthRangeParameters"),
1484 fields, SymbolType::BuiltIn);
1485 insertStructType(COMMON_BUILTINS, depthRangeStruct);
1486 TType *depthRangeType = new TType(depthRangeStruct);
1487 depthRangeType->setQualifier(EvqUniform);
1488 depthRangeType->realize();
1489 insertVariable(COMMON_BUILTINS, ImmutableString("gl_DepthRange"), depthRangeType);
1490
1491 //
1492 // Implementation dependent built-in constants.
1493 //
1494 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexAttribs"),
1495 resources.MaxVertexAttribs);
1496 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexUniformVectors"),
1497 resources.MaxVertexUniformVectors);
1498 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxVertexTextureImageUnits"),
1499 resources.MaxVertexTextureImageUnits);
1500 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxCombinedTextureImageUnits"),
1501 resources.MaxCombinedTextureImageUnits);
1502 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxTextureImageUnits"),
1503 resources.MaxTextureImageUnits);
1504 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxFragmentUniformVectors"),
1505 resources.MaxFragmentUniformVectors);
1506
1507 insertConstInt<EbpMedium>(ESSL1_BUILTINS, ImmutableString("gl_MaxVaryingVectors"),
1508 resources.MaxVaryingVectors);
1509
1510 insertConstInt<EbpMedium>(COMMON_BUILTINS, ImmutableString("gl_MaxDrawBuffers"),
1511 resources.MaxDrawBuffers);
Olli Etuaho7c8567a2018-02-20 15:44:07 +02001512 insertConstIntExt<EbpMedium>(COMMON_BUILTINS, TExtension::EXT_blend_func_extended,
1513 ImmutableString("gl_MaxDualSourceDrawBuffersEXT"),
1514 resources.MaxDualSourceDrawBuffers);
Olli Etuaho29bda812018-01-26 17:37:36 +02001515
1516 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxVertexOutputVectors"),
1517 resources.MaxVertexOutputVectors);
1518 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxFragmentInputVectors"),
1519 resources.MaxFragmentInputVectors);
1520 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MinProgramTexelOffset"),
1521 resources.MinProgramTexelOffset);
1522 insertConstInt<EbpMedium>(ESSL3_BUILTINS, ImmutableString("gl_MaxProgramTexelOffset"),
1523 resources.MaxProgramTexelOffset);
1524
1525 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxImageUnits"),
1526 resources.MaxImageUnits);
1527 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexImageUniforms"),
1528 resources.MaxVertexImageUniforms);
1529 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxFragmentImageUniforms"),
1530 resources.MaxFragmentImageUniforms);
1531 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeImageUniforms"),
1532 resources.MaxComputeImageUniforms);
1533 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxCombinedImageUniforms"),
1534 resources.MaxCombinedImageUniforms);
1535
1536 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1537 ImmutableString("gl_MaxCombinedShaderOutputResources"),
1538 resources.MaxCombinedShaderOutputResources);
1539
1540 insertConstIvec3<EbpHigh>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeWorkGroupCount"),
1541 resources.MaxComputeWorkGroupCount);
1542 insertConstIvec3<EbpHigh>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeWorkGroupSize"),
1543 resources.MaxComputeWorkGroupSize);
1544 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeUniformComponents"),
1545 resources.MaxComputeUniformComponents);
1546 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeTextureImageUnits"),
1547 resources.MaxComputeTextureImageUnits);
1548
1549 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxComputeAtomicCounters"),
1550 resources.MaxComputeAtomicCounters);
1551 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1552 ImmutableString("gl_MaxComputeAtomicCounterBuffers"),
1553 resources.MaxComputeAtomicCounterBuffers);
1554
1555 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexAtomicCounters"),
1556 resources.MaxVertexAtomicCounters);
1557 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxFragmentAtomicCounters"),
1558 resources.MaxFragmentAtomicCounters);
1559 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxCombinedAtomicCounters"),
1560 resources.MaxCombinedAtomicCounters);
1561 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxAtomicCounterBindings"),
1562 resources.MaxAtomicCounterBindings);
1563
1564 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxVertexAtomicCounterBuffers"),
1565 resources.MaxVertexAtomicCounterBuffers);
1566 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1567 ImmutableString("gl_MaxFragmentAtomicCounterBuffers"),
1568 resources.MaxFragmentAtomicCounterBuffers);
1569 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS,
1570 ImmutableString("gl_MaxCombinedAtomicCounterBuffers"),
1571 resources.MaxCombinedAtomicCounterBuffers);
1572 insertConstInt<EbpMedium>(ESSL3_1_BUILTINS, ImmutableString("gl_MaxAtomicCounterBufferSize"),
1573 resources.MaxAtomicCounterBufferSize);
1574
Olli Etuaho29bda812018-01-26 17:37:36 +02001575 {
1576 TExtension ext = TExtension::EXT_geometry_shader;
1577 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1578 ImmutableString("gl_MaxGeometryInputComponents"),
1579 resources.MaxGeometryInputComponents);
1580 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1581 ImmutableString("gl_MaxGeometryOutputComponents"),
1582 resources.MaxGeometryOutputComponents);
1583 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1584 ImmutableString("gl_MaxGeometryImageUniforms"),
1585 resources.MaxGeometryImageUniforms);
1586 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1587 ImmutableString("gl_MaxGeometryTextureImageUnits"),
1588 resources.MaxGeometryTextureImageUnits);
1589 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1590 ImmutableString("gl_MaxGeometryOutputVertices"),
1591 resources.MaxGeometryOutputVertices);
1592 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1593 ImmutableString("gl_MaxGeometryTotalOutputComponents"),
1594 resources.MaxGeometryTotalOutputComponents);
1595 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1596 ImmutableString("gl_MaxGeometryUniformComponents"),
1597 resources.MaxGeometryUniformComponents);
1598 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1599 ImmutableString("gl_MaxGeometryAtomicCounters"),
1600 resources.MaxGeometryAtomicCounters);
1601 insertConstIntExt<EbpMedium>(ESSL3_1_BUILTINS, ext,
1602 ImmutableString("gl_MaxGeometryAtomicCounterBuffers"),
1603 resources.MaxGeometryAtomicCounterBuffers);
1604 }
1605
1606 //
1607 // Insert some special built-in variables that are not in
1608 // the built-in header files.
1609 //
1610
1611 if (resources.OVR_multiview && type != GL_COMPUTE_SHADER)
1612 {
1613 const TType *viewIDType = StaticType::Get<EbtUInt, EbpHigh, EvqViewIDOVR, 1, 1>();
1614 insertVariableExt(ESSL3_BUILTINS, TExtension::OVR_multiview,
1615 ImmutableString("gl_ViewID_OVR"), viewIDType);
1616
1617 // ESSL 1.00 doesn't have unsigned integers, so gl_ViewID_OVR is a signed integer in ESSL
1618 // 1.00. This is specified in the WEBGL_multiview spec.
1619 const TType *viewIDIntType = StaticType::Get<EbtInt, EbpHigh, EvqViewIDOVR, 1, 1>();
1620 insertVariableExt(ESSL1_BUILTINS, TExtension::OVR_multiview,
1621 ImmutableString("gl_ViewID_OVR"), viewIDIntType);
1622 }
1623
1624 const TType *positionType = StaticType::Get<EbtFloat, EbpHigh, EvqPosition, 4, 1>();
1625 const TType *primitiveIDType = StaticType::Get<EbtInt, EbpHigh, EvqPrimitiveID, 1, 1>();
1626 const TType *layerType = StaticType::Get<EbtInt, EbpHigh, EvqLayer, 1, 1>();
1627
1628 switch (type)
1629 {
1630 case GL_FRAGMENT_SHADER:
1631 {
1632 const TType *fragCoordType = StaticType::Get<EbtFloat, EbpMedium, EvqFragCoord, 4, 1>();
1633 insertVariable(COMMON_BUILTINS, ImmutableString("gl_FragCoord"), fragCoordType);
1634 const TType *frontFacingType = StaticType::GetQualified<EbtBool, EvqFrontFacing>();
1635 insertVariable(COMMON_BUILTINS, ImmutableString("gl_FrontFacing"), frontFacingType);
1636 const TType *pointCoordType =
1637 StaticType::Get<EbtFloat, EbpMedium, EvqPointCoord, 2, 1>();
1638 insertVariable(COMMON_BUILTINS, ImmutableString("gl_PointCoord"), pointCoordType);
1639
1640 const TType *fragColorType = StaticType::Get<EbtFloat, EbpMedium, EvqFragColor, 4, 1>();
1641 insertVariable(ESSL1_BUILTINS, ImmutableString("gl_FragColor"), fragColorType);
1642
1643 TType *fragDataType = new TType(EbtFloat, EbpMedium, EvqFragData, 4);
1644 if (spec != SH_WEBGL2_SPEC && spec != SH_WEBGL3_SPEC)
1645 {
1646 fragDataType->makeArray(resources.MaxDrawBuffers);
1647 }
1648 else
1649 {
1650 fragDataType->makeArray(1u);
1651 }
1652 fragDataType->realize();
1653 insertVariable(ESSL1_BUILTINS, ImmutableString("gl_FragData"), fragDataType);
1654
1655 if (resources.EXT_blend_func_extended)
1656 {
1657 const TType *secondaryFragColorType =
1658 StaticType::Get<EbtFloat, EbpMedium, EvqSecondaryFragColorEXT, 4, 1>();
1659 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_blend_func_extended,
1660 ImmutableString("gl_SecondaryFragColorEXT"),
1661 secondaryFragColorType);
1662 TType *secondaryFragDataType =
1663 new TType(EbtFloat, EbpMedium, EvqSecondaryFragDataEXT, 4, 1);
1664 secondaryFragDataType->makeArray(resources.MaxDualSourceDrawBuffers);
1665 secondaryFragDataType->realize();
1666 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_blend_func_extended,
1667 ImmutableString("gl_SecondaryFragDataEXT"),
1668 secondaryFragDataType);
1669 }
1670
1671 if (resources.EXT_frag_depth)
1672 {
1673 TType *fragDepthEXTType =
1674 new TType(EbtFloat, resources.FragmentPrecisionHigh ? EbpHigh : EbpMedium,
1675 EvqFragDepthEXT, 1);
1676 fragDepthEXTType->realize();
1677 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_frag_depth,
1678 ImmutableString("gl_FragDepthEXT"), fragDepthEXTType);
1679 }
1680
1681 const TType *fragDepthType = StaticType::Get<EbtFloat, EbpHigh, EvqFragDepth, 1, 1>();
1682 insertVariable(ESSL3_BUILTINS, ImmutableString("gl_FragDepth"), fragDepthType);
1683
1684 const TType *lastFragColorType =
1685 StaticType::Get<EbtFloat, EbpMedium, EvqLastFragColor, 4, 1>();
1686
1687 if (resources.EXT_shader_framebuffer_fetch || resources.NV_shader_framebuffer_fetch)
1688 {
1689 TType *lastFragDataType = new TType(EbtFloat, EbpMedium, EvqLastFragData, 4, 1);
1690 lastFragDataType->makeArray(resources.MaxDrawBuffers);
1691 lastFragDataType->realize();
1692
1693 if (resources.EXT_shader_framebuffer_fetch)
1694 {
1695 insertVariableExt(ESSL1_BUILTINS, TExtension::EXT_shader_framebuffer_fetch,
1696 ImmutableString("gl_LastFragData"), lastFragDataType);
1697 }
1698 else if (resources.NV_shader_framebuffer_fetch)
1699 {
1700 insertVariableExt(ESSL1_BUILTINS, TExtension::NV_shader_framebuffer_fetch,
1701 ImmutableString("gl_LastFragColor"), lastFragColorType);
1702 insertVariableExt(ESSL1_BUILTINS, TExtension::NV_shader_framebuffer_fetch,
1703 ImmutableString("gl_LastFragData"), lastFragDataType);
1704 }
1705 }
1706 else if (resources.ARM_shader_framebuffer_fetch)
1707 {
1708 insertVariableExt(ESSL1_BUILTINS, TExtension::ARM_shader_framebuffer_fetch,
1709 ImmutableString("gl_LastFragColorARM"), lastFragColorType);
1710 }
1711
1712 if (resources.EXT_geometry_shader)
1713 {
1714 TExtension extension = TExtension::EXT_geometry_shader;
1715 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveID"),
1716 primitiveIDType);
1717 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Layer"),
1718 layerType);
1719 }
1720
1721 break;
1722 }
1723 case GL_VERTEX_SHADER:
1724 {
1725 insertVariable(COMMON_BUILTINS, ImmutableString("gl_Position"), positionType);
1726 const TType *pointSizeType = StaticType::Get<EbtFloat, EbpMedium, EvqPointSize, 1, 1>();
1727 insertVariable(COMMON_BUILTINS, ImmutableString("gl_PointSize"), pointSizeType);
1728 const TType *instanceIDType = StaticType::Get<EbtInt, EbpHigh, EvqInstanceID, 1, 1>();
1729 insertVariable(ESSL3_BUILTINS, ImmutableString("gl_InstanceID"), instanceIDType);
1730 const TType *vertexIDType = StaticType::Get<EbtInt, EbpHigh, EvqVertexID, 1, 1>();
1731 insertVariable(ESSL3_BUILTINS, ImmutableString("gl_VertexID"), vertexIDType);
1732
1733 // For internal use by ANGLE - not exposed to the parser.
1734 const TType *viewportIndexType =
1735 StaticType::Get<EbtInt, EbpHigh, EvqViewportIndex, 1, 1>();
1736 insertVariable(GLSL_BUILTINS, ImmutableString("gl_ViewportIndex"), viewportIndexType);
1737 // gl_Layer exists in other shader stages in ESSL, but not in vertex shader so far.
1738 insertVariable(GLSL_BUILTINS, ImmutableString("gl_Layer"), layerType);
1739 break;
1740 }
1741 case GL_COMPUTE_SHADER:
1742 {
1743 const TType *numWorkGroupsType =
1744 StaticType::Get<EbtUInt, EbpUndefined, EvqNumWorkGroups, 3, 1>();
1745 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_NumWorkGroups"),
1746 numWorkGroupsType);
1747 const TType *workGroupSizeType =
1748 StaticType::Get<EbtUInt, EbpUndefined, EvqWorkGroupSize, 3, 1>();
1749 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_WorkGroupSize"),
1750 workGroupSizeType);
1751 const TType *workGroupIDType =
1752 StaticType::Get<EbtUInt, EbpUndefined, EvqWorkGroupID, 3, 1>();
1753 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_WorkGroupID"), workGroupIDType);
1754 const TType *localInvocationIDType =
1755 StaticType::Get<EbtUInt, EbpUndefined, EvqLocalInvocationID, 3, 1>();
1756 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_LocalInvocationID"),
1757 localInvocationIDType);
1758 const TType *globalInvocationIDType =
1759 StaticType::Get<EbtUInt, EbpUndefined, EvqGlobalInvocationID, 3, 1>();
1760 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_GlobalInvocationID"),
1761 globalInvocationIDType);
1762 const TType *localInvocationIndexType =
1763 StaticType::Get<EbtUInt, EbpUndefined, EvqLocalInvocationIndex, 1, 1>();
1764 insertVariable(ESSL3_1_BUILTINS, ImmutableString("gl_LocalInvocationIndex"),
1765 localInvocationIndexType);
1766 break;
1767 }
1768
1769 case GL_GEOMETRY_SHADER_EXT:
1770 {
1771 TExtension extension = TExtension::EXT_geometry_shader;
1772
1773 // Add built-in interface block gl_PerVertex and the built-in array gl_in.
1774 // TODO(jiawei.shao@intel.com): implement GL_EXT_geometry_point_size.
1775 TFieldList *glPerVertexFieldList = new TFieldList();
1776 TField *glPositionField =
1777 new TField(new TType(*positionType), ImmutableString("gl_Position"), zeroSourceLoc);
1778 glPerVertexFieldList->push_back(glPositionField);
1779
1780 const ImmutableString glPerVertexString("gl_PerVertex");
1781 TInterfaceBlock *glPerVertexInBlock =
1782 new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList,
1783 TLayoutQualifier::Create(), SymbolType::BuiltIn, extension);
1784 insertInterfaceBlock(ESSL3_1_BUILTINS, glPerVertexInBlock);
1785
1786 // The array size of gl_in is undefined until we get a valid input primitive
1787 // declaration.
1788 TType *glInType =
1789 new TType(glPerVertexInBlock, EvqPerVertexIn, TLayoutQualifier::Create());
1790 glInType->makeArray(0u);
1791 glInType->realize();
1792 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_in"), glInType);
1793
1794 TInterfaceBlock *glPerVertexOutBlock =
1795 new TInterfaceBlock(this, glPerVertexString, glPerVertexFieldList,
1796 TLayoutQualifier::Create(), SymbolType::BuiltIn);
1797 TType *glPositionInBlockType = new TType(EbtFloat, EbpHigh, EvqPosition, 4);
1798 glPositionInBlockType->setInterfaceBlock(glPerVertexOutBlock);
1799 glPositionInBlockType->realize();
1800 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Position"),
1801 glPositionInBlockType);
1802
1803 const TType *primitiveIDInType =
1804 StaticType::Get<EbtInt, EbpHigh, EvqPrimitiveIDIn, 1, 1>();
1805 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveIDIn"),
1806 primitiveIDInType);
1807 const TType *invocationIDType =
1808 StaticType::Get<EbtInt, EbpHigh, EvqInvocationID, 1, 1>();
1809 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_InvocationID"),
1810 invocationIDType);
1811 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_PrimitiveID"),
1812 primitiveIDType);
1813 insertVariableExt(ESSL3_1_BUILTINS, extension, ImmutableString("gl_Layer"), layerType);
1814
1815 break;
1816 }
1817 default:
1818 UNREACHABLE();
1819 }
1820}
1821
Jamie Madill45bcc782016-11-07 13:58:48 -05001822} // namespace sh