blob: d89f3d1f749203e641fac26137635a8c6a72df4c [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
Jamie Madill88f6e942014-02-19 10:27:53 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org07620a52010-09-23 17:53:56 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Corentin Wallez8b28a8b2016-09-15 19:47:56 -04007#include "compiler/translator/Compiler.h"
8
9#include <sstream>
10
11#include "angle_gl.h"
12#include "common/utilities.h"
Qiankun Miao09cfac62016-09-06 17:25:16 +080013#include "compiler/translator/AddAndTrueToLoopCondition.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -080015#include "compiler/translator/CallDAG.h"
Olli Etuahoab918822017-07-14 17:03:42 +030016#include "compiler/translator/ClampPointSize.h"
Olli Etuaho78ed6cd2017-08-09 16:19:00 +030017#include "compiler/translator/CollectVariables.h"
Martin Radev69056a12017-05-18 11:14:50 +030018#include "compiler/translator/DeclareAndInitBuiltinsForInstancedMultiview.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030019#include "compiler/translator/DeferGlobalInitializers.h"
Zhenyao Mo4e94fea2016-08-09 14:31:37 -070020#include "compiler/translator/EmulateGLFragColorBroadcast.h"
Jamie Madilld5696192016-10-06 11:09:24 -040021#include "compiler/translator/EmulatePrecision.h"
Geoff Lang17732822013-08-29 13:46:49 -040022#include "compiler/translator/Initialize.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080023#include "compiler/translator/InitializeVariables.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030024#include "compiler/translator/IntermNodePatternMatcher.h"
Olli Etuahocccf2b02017-07-05 14:50:54 +030025#include "compiler/translator/IsASTDepthBelowLimit.h"
26#include "compiler/translator/OutputTree.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040027#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030028#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070029#include "compiler/translator/RegenerateStructNames.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030030#include "compiler/translator/RemoveArrayLengthMethod.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080031#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho923ecef2017-10-11 12:01:38 +030032#include "compiler/translator/RemoveNoOpCasesFromEndOfSwitchStatements.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030033#include "compiler/translator/RemovePow.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070034#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070035#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030036#include "compiler/translator/SeparateDeclarations.h"
37#include "compiler/translator/SimplifyLoopConditions.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030038#include "compiler/translator/SplitSequenceOperator.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070039#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080040#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040041#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020042#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040043#include "compiler/translator/ValidateOutputs.h"
Jiawei Shao4cc89e22017-08-31 14:25:54 +080044#include "compiler/translator/ValidateVaryingLocations.h"
Geoff Lang17732822013-08-29 13:46:49 -040045#include "compiler/translator/VariablePacker.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000046#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070047
Jamie Madillacb4b812016-11-07 13:50:29 -050048namespace sh
49{
50
Corentin Wallez28b65282016-06-16 07:24:50 -070051namespace
52{
53
54#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
55void DumpFuzzerCase(char const *const *shaderStrings,
56 size_t numStrings,
57 uint32_t type,
58 uint32_t spec,
59 uint32_t output,
60 uint64_t options)
61{
62 static int fileIndex = 0;
63
64 std::ostringstream o;
65 o << "corpus/" << fileIndex++ << ".sample";
66 std::string s = o.str();
67
68 // Must match the input format of the fuzzer
69 FILE *f = fopen(s.c_str(), "w");
70 fwrite(&type, sizeof(type), 1, f);
71 fwrite(&spec, sizeof(spec), 1, f);
72 fwrite(&output, sizeof(output), 1, f);
73 fwrite(&options, sizeof(options), 1, f);
74
75 char zero[128 - 20] = {0};
76 fwrite(&zero, 128 - 20, 1, f);
77
78 for (size_t i = 0; i < numStrings; i++)
79 {
80 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
81 }
82 fwrite(&zero, 1, 1, f);
83
84 fclose(f);
85}
86#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
87} // anonymous namespace
88
Jamie Madill5508f392014-02-20 13:31:36 -050089bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000090{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080091 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000092}
93
Qingqing Dengad0d0792015-04-08 14:25:06 -070094bool IsGLSL130OrNewer(ShShaderOutput output)
95{
Jamie Madillacb4b812016-11-07 13:50:29 -050096 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
97 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
98 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
99 output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
100 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -0700101}
102
Qiankun Miao705a9192016-08-29 10:05:27 +0800103bool IsGLSL420OrNewer(ShShaderOutput output)
104{
Jamie Madillacb4b812016-11-07 13:50:29 -0500105 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
106 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800107}
108
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800109bool IsGLSL410OrOlder(ShShaderOutput output)
110{
111 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
112 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
113 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
114}
115
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000116bool RemoveInvariant(sh::GLenum shaderType,
117 int shaderVersion,
118 ShShaderOutput outputType,
119 ShCompileOptions compileOptions)
120{
121 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
122 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
123 return true;
124
125 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800126 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000127 return true;
128
129 return false;
130}
131
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700132size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500133{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800134 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500135 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700136 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500137 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500138 case SH_WEBGL_SPEC:
139 return 256;
140 default:
141 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500142 }
143}
144
Shao77891c02017-06-23 16:30:17 +0800145int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
146{
147 switch (shaderType)
148 {
149 case GL_VERTEX_SHADER:
150 return resources.MaxVertexUniformVectors;
151 case GL_FRAGMENT_SHADER:
152 return resources.MaxFragmentUniformVectors;
Shaob5cc1192017-07-06 10:47:20 +0800153
154 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
Shao77891c02017-06-23 16:30:17 +0800155 case GL_COMPUTE_SHADER:
Shao77891c02017-06-23 16:30:17 +0800156 return resources.MaxComputeUniformComponents / 4;
Shaob5cc1192017-07-06 10:47:20 +0800157 case GL_GEOMETRY_SHADER_OES:
158 return resources.MaxGeometryUniformComponents / 4;
Shao77891c02017-06-23 16:30:17 +0800159 default:
Shaob5cc1192017-07-06 10:47:20 +0800160 UNREACHABLE();
Shao77891c02017-06-23 16:30:17 +0800161 return -1;
162 }
163}
164
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500165namespace
166{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700167
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800168class TScopedPoolAllocator
169{
170 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500171 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800172 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400173 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000174 SetGlobalPoolAllocator(mAllocator);
175 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800176 ~TScopedPoolAllocator()
177 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800178 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400179 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000180 }
181
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800182 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500183 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400184};
185
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800186class TScopedSymbolTableLevel
187{
188 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500189 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800190 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400191 ASSERT(mTable->atBuiltInLevel());
192 mTable->push();
193 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800194 ~TScopedSymbolTableLevel()
195 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400196 while (!mTable->atBuiltInLevel())
197 mTable->pop();
198 }
199
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800200 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500201 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000202};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700203
204int MapSpecToShaderVersion(ShShaderSpec spec)
205{
206 switch (spec)
207 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500208 case SH_GLES2_SPEC:
209 case SH_WEBGL_SPEC:
210 return 100;
211 case SH_GLES3_SPEC:
212 case SH_WEBGL2_SPEC:
213 return 300;
214 case SH_GLES3_1_SPEC:
215 case SH_WEBGL3_SPEC:
216 return 310;
217 default:
218 UNREACHABLE();
219 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700220 }
221}
222
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000223} // namespace
224
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800225TShHandleBase::TShHandleBase()
226{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000227 allocator.push();
228 SetGlobalPoolAllocator(&allocator);
229}
230
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800231TShHandleBase::~TShHandleBase()
232{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800233 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000234 allocator.popAll();
235}
236
Jamie Madill183bde52014-07-02 15:31:19 -0400237TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700238 : variablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300239 mGLPositionInitialized(false),
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700240 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000241 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400242 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400243 maxUniformVectors(0),
244 maxExpressionComplexity(0),
245 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200246 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000247 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000248 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200249 builtInFunctionEmulator(),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000250 mDiagnostics(infoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800251 mSourcePath(nullptr),
Shaob5cc1192017-07-06 10:47:20 +0800252 mComputeShaderLocalSizeDeclared(false),
253 mGeometryShaderMaxVertices(-1),
254 mGeometryShaderInvocations(0),
255 mGeometryShaderInputPrimitiveType(EptUndefined),
256 mGeometryShaderOutputPrimitiveType(EptUndefined)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000257{
Martin Radev802abe02016-08-04 17:48:32 +0300258 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000259}
260
261TCompiler::~TCompiler()
262{
263}
264
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800265bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300266{
267 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
268 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
269 // of ESSL 1.00 as in Appendix A of the spec).
270 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
271 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
272}
273
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500274bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000275{
Shao77891c02017-06-23 16:30:17 +0800276 shaderVersion = 100;
277
278 maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources);
279
Jamie Madilleb1a0102013-07-08 13:31:38 -0400280 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200281 maxCallStackDepth = resources.MaxCallStackDepth;
282 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400283
284 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000285
alokp@chromium.org07620a52010-09-23 17:53:56 +0000286 // Generate built-in symbol table.
287 if (!InitBuiltInSymbolTable(resources))
288 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000289 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000290 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000291
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000292 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
293 clampingStrategy = resources.ArrayIndexClampingStrategy;
294
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000295 hashFunction = resources.HashFunction;
296
alokp@chromium.org07620a52010-09-23 17:53:56 +0000297 return true;
298}
299
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100300TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
301 size_t numStrings,
302 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000303{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200304 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
305}
306
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100307TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
308 size_t numStrings,
309 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200310{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000311 clearResults();
312
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200313 ASSERT(numStrings > 0);
314 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000315
David Yen0fbd1282015-02-02 14:46:09 -0800316 // Reset the extension behavior for each compilation unit.
317 ResetExtensionBehavior(extensionBehavior);
318
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000319 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000320 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000321 if (compileOptions & SH_SOURCE_PATH)
322 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200323 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000324 ++firstSource;
325 }
326
Olli Etuahof119a262016-08-19 15:54:22 +0300327 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000328 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200329
Olli Etuahoa6996682015-10-12 14:32:30 +0300330 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000331
332 // We preserve symbols at the built-in level from compile-to-compile.
333 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400334 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000335
336 // Parse shader.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500337 bool success = (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
338 &parseContext) == 0) &&
339 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000340
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000341 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700342 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
343 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000344 mDiagnostics.globalError("unsupported shader version");
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700345 success = false;
346 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000347
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100348 TIntermBlock *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200349
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800350 if (success)
351 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700352 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700353 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700354
Martin Radev802abe02016-08-04 17:48:32 +0300355 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
356 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
357
Olli Etuaho09b04a22016-12-15 13:30:26 +0000358 mNumViews = parseContext.getNumViews();
359
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400360 root = parseContext.getTreeRoot();
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000361
Olli Etuahoa6996682015-10-12 14:32:30 +0300362 // Highp might have been auto-enabled based on shader version
363 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
364
Shaob5cc1192017-07-06 10:47:20 +0800365 if (success && shaderType == GL_GEOMETRY_SHADER_OES)
366 {
367 mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType();
368 mGeometryShaderOutputPrimitiveType =
369 parseContext.getGeometryShaderOutputPrimitiveType();
370 mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices();
371 mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations();
372 }
373
Jamie Madill6654bc92014-03-26 14:01:57 -0400374 // Disallow expressions deemed too complex.
375 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
376 success = limitExpressionComplexity(root);
377
Olli Etuaho923ecef2017-10-11 12:01:38 +0300378 // Prune empty declarations to work around driver bugs and to keep declaration output
379 // simple. We want to do this before most other operations since TIntermDeclaration nodes
380 // without any children are tricky to work with.
381 if (success)
382 PruneEmptyDeclarations(root);
383
384 // In case the last case inside a switch statement is a certain type of no-op, GLSL
385 // compilers in drivers may not accept it. In this case we clean up the dead code from the
386 // end of switch statements. This is also required because PruneEmptyDeclarations may have
387 // left switch statements that only contained an empty declaration inside the final case in
388 // an invalid state. Relies on that PruneEmptyDeclarations has already been run.
389 if (success)
390 RemoveNoOpCasesFromEndOfSwitchStatements(root, &symbolTable);
391
Corentin Wallez71d147f2015-02-11 11:15:24 -0800392 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000393 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800394 success = initCallDag(root);
395
396 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
397 success = checkCallDepth();
398
399 // Checks which functions are used and if "main" exists
400 if (success)
401 {
402 functionMetadata.clear();
403 functionMetadata.resize(mCallDag.size());
404 success = tagUsedFunctions();
405 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000406
Corentin Walleza094a8a2015-04-07 11:53:06 -0700407 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
408 success = pruneUnusedFunctions(root);
409
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800410 if (success && shaderVersion >= 310)
411 {
412 success = ValidateVaryingLocations(root, &mDiagnostics);
413 }
414
Andrei Volykhin73fa4b82017-05-05 18:45:06 +0300415 if (success && shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER)
Olli Etuaho12b0b392017-05-30 13:22:31 +0300416 {
417 success = ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers,
418 &mDiagnostics);
419 }
Jamie Madill05a80ce2013-06-20 11:55:49 -0400420
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300421 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
Olli Etuaho9ec79392017-03-31 23:04:23 +0300422 success =
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300423 ValidateLimitations(root, shaderType, &symbolTable, shaderVersion, &mDiagnostics);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000424
Jamie Madilld5696192016-10-06 11:09:24 -0400425 // Fail compilation if precision emulation not supported.
426 if (success && getResources().WEBGL_debug_shader_precision &&
427 getPragma().debugShaderPrecision)
428 {
429 if (!EmulatePrecision::SupportedInLanguage(outputType))
430 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000431 mDiagnostics.globalError("Precision emulation not supported for this output type.");
Jamie Madilld5696192016-10-06 11:09:24 -0400432 success = false;
433 }
434 }
435
zmo@google.com32e97312011-08-24 01:03:11 +0000436 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200437 if (success)
438 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400439 // TODO(jmadill): Remove global pool allocator.
440 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200441 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400442 GetGlobalPoolAllocator()->unlock();
Olli Etuahodfa75e82017-01-23 09:43:06 -0800443 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200444 }
zmo@google.com32e97312011-08-24 01:03:11 +0000445
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000446 // Clamping uniform array bounds needs to happen after validateLimitations pass.
447 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
448 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
449
Martin Radev69056a12017-05-18 11:14:50 +0300450 if (success && (compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300451 parseContext.isExtensionEnabled(TExtension::OVR_multiview) &&
452 getShaderType() != GL_COMPUTE_SHADER)
Martin Radev69056a12017-05-18 11:14:50 +0300453 {
Martin Radevc39a19a2017-07-07 18:52:09 +0300454 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300455 outputType, &symbolTable);
Martin Radev69056a12017-05-18 11:14:50 +0300456 }
457
Corentin Wallezd4b50542015-09-28 12:19:26 -0700458 // This pass might emit short circuits so keep it before the short circuit unfolding
459 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300460 RewriteDoWhile(root, &symbolTable);
Corentin Wallezd4b50542015-09-28 12:19:26 -0700461
Qiankun Miao09cfac62016-09-06 17:25:16 +0800462 if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION))
463 sh::AddAndTrueToLoopCondition(root);
464
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800465 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
466 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700467 UnfoldShortCircuitAST unfoldShortCircuit;
468 root->traverse(&unfoldShortCircuit);
469 unfoldShortCircuit.updateTree();
470 }
471
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300472 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
473 {
474 RemovePow(root);
475 }
476
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300477 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800478 {
Olli Etuaho19515012017-06-26 18:00:17 +0300479 ASSERT(!variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800480 CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings,
Jiawei Shaod8105a02017-08-08 09:54:36 +0800481 &outputVaryings, &uniformBlocks, &shaderStorageBlocks, &inBlocks,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800482 hashFunction, &symbolTable, shaderVersion, shaderType,
483 extensionBehavior);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800484 collectInterfaceBlocks();
Olli Etuaho19515012017-06-26 18:00:17 +0300485 variablesCollected = true;
Qin Jiajia7835b522016-10-08 11:20:17 +0800486 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
487 {
488 useAllMembersInUnusedStandardAndSharedBlocks(root);
489 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800490 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
491 {
Olli Etuaho19515012017-06-26 18:00:17 +0300492 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
493 // Appendix A, section 7, the shader does not use too many uniforms.
Olli Etuahod7487b12017-08-09 15:45:13 +0300494 success = CheckVariablesInPackingLimits(maxUniformVectors, uniforms);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800495 if (!success)
496 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000497 mDiagnostics.globalError("too many uniforms");
gman@chromium.org8d804792012-10-17 21:33:48 +0000498 }
499 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700500 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
501 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300502 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700503 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000504 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000505
Olli Etuahob12040c2017-06-27 14:20:45 +0300506 // gl_Position is always written in compatibility output mode.
507 // It may have been already initialized among other output variables, in that case we don't
508 // need to initialize it twice.
509 if (success && shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
510 ((compileOptions & SH_INIT_GL_POSITION) ||
511 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
512 {
513 initializeGLPosition(root);
514 mGLPositionInitialized = true;
515 }
516
Yuly Novikov817232e2017-02-22 18:36:10 -0500517 // Removing invariant declarations must be done after collecting variables.
518 // Otherwise, built-in invariant declarations don't apply.
519 if (success && RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
520 sh::RemoveInvariantDeclaration(root);
521
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700522 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
523 {
Olli Etuahob990b552016-10-27 12:29:17 +0100524 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300525 &symbolTable);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700526 }
527
Zhenyao Moe740add2014-07-18 17:01:01 -0700528 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
529 {
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300530 RegenerateStructNames gen(&symbolTable, shaderVersion);
Zhenyao Moe740add2014-07-18 17:01:01 -0700531 root->traverse(&gen);
532 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300533
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700534 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
535 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300536 IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers))
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700537 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300538 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300539 &symbolTable, shaderVersion);
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700540 }
541
Olli Etuaho3d932d82016-04-12 11:10:30 +0300542 if (success)
543 {
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300544 DeferGlobalInitializers(root, needToInitializeGlobalsInAST(), &symbolTable);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300545 }
Olli Etuaho9733cee2017-05-11 19:14:35 +0300546
Olli Etuahobb5a7e22017-08-30 13:03:12 +0300547 // Split multi declarations and remove calls to array length().
548 if (success)
549 {
550 // Note that SimplifyLoopConditions needs to be run before any other AST transformations
551 // that may need to generate new statements from loop conditions or loop expressions.
552 SimplifyLoopConditions(root,
553 IntermNodePatternMatcher::kMultiDeclaration |
554 IntermNodePatternMatcher::kArrayLengthMethod,
555 &getSymbolTable(), getShaderVersion());
556
557 // Note that separate declarations need to be run before other AST transformations that
558 // generate new statements from expressions.
559 SeparateDeclarations(root);
560
561 SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod,
562 &getSymbolTable(), getShaderVersion());
563
564 RemoveArrayLengthMethod(root);
565 }
566
Olli Etuaho9733cee2017-05-11 19:14:35 +0300567 if (success && (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && getOutputType())
568 {
569 // Initialize uninitialized local variables.
570 // In some cases initializing can generate extra statements in the parent block, such as
571 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
Olli Etuahobb5a7e22017-08-30 13:03:12 +0300572 // we need to first simplify loop conditions. We've already separated declarations
573 // earlier, which is also required. If we don't follow the Appendix A limitations, loop
574 // init statements can declare arrays or nameless structs and have multiple
575 // declarations.
Olli Etuaho9733cee2017-05-11 19:14:35 +0300576
577 if (!shouldRunLoopAndIndexingValidation(compileOptions))
578 {
579 SimplifyLoopConditions(root,
Olli Etuaho9733cee2017-05-11 19:14:35 +0300580 IntermNodePatternMatcher::kArrayDeclaration |
581 IntermNodePatternMatcher::kNamelessStructDeclaration,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300582 &getSymbolTable(), getShaderVersion());
Olli Etuaho9733cee2017-05-11 19:14:35 +0300583 }
Olli Etuaho9733cee2017-05-11 19:14:35 +0300584 InitializeUninitializedLocals(root, getShaderVersion());
585 }
Olli Etuahoab918822017-07-14 17:03:42 +0300586
587 if (success && getShaderType() == GL_VERTEX_SHADER &&
588 (compileOptions & SH_CLAMP_POINT_SIZE))
589 {
590 ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable());
591 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000592 }
593
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200594 if (success)
595 return root;
596
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800597 return nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200598}
599
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800600bool TCompiler::compile(const char *const shaderStrings[],
601 size_t numStrings,
602 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200603{
Corentin Wallez28b65282016-06-16 07:24:50 -0700604#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
605 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
606#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
607
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200608 if (numStrings == 0)
609 return true;
610
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800611 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700612
613 // Apply key workarounds.
614 if (shouldFlattenPragmaStdglInvariantAll())
615 {
616 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
617 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
618 }
619
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200620 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100621 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200622
623 if (root)
624 {
625 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuahocccf2b02017-07-05 14:50:54 +0300626 OutputTree(root, infoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200627
628 if (compileOptions & SH_OBJECT_CODE)
629 translate(root, compileOptions);
630
631 // The IntermNode tree doesn't need to be deleted here, since the
632 // memory will be freed in a big chunk by the PoolAllocator.
633 return true;
634 }
635 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000636}
637
Nicolas Capens49a88872013-06-20 09:54:03 -0400638bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000639{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000640 if (resources.MaxDrawBuffers < 1)
641 {
642 return false;
643 }
644 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
645 {
646 return false;
647 }
648
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000649 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400650 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000651
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300652 ASSERT(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500653 symbolTable.push(); // COMMON_BUILTINS
654 symbolTable.push(); // ESSL1_BUILTINS
655 symbolTable.push(); // ESSL3_BUILTINS
656 symbolTable.push(); // ESSL3_1_BUILTINS
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300657 symbolTable.push(); // GLSL_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000658
Jamie Madillacb4b812016-11-07 13:50:29 -0500659 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400660 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500661 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300662 symbolTable.setDefaultPrecision(EbtInt, EbpMedium);
Jamie Madillacb4b812016-11-07 13:50:29 -0500663 break;
664 case GL_VERTEX_SHADER:
Jamie Madillacb4b812016-11-07 13:50:29 -0500665 case GL_COMPUTE_SHADER:
Shaob5cc1192017-07-06 10:47:20 +0800666 case GL_GEOMETRY_SHADER_OES:
Olli Etuahocce89652017-06-19 16:04:09 +0300667 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
668 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500669 break;
670 default:
Shaob5cc1192017-07-06 10:47:20 +0800671 UNREACHABLE();
Nicolas Capens49a88872013-06-20 09:54:03 -0400672 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200673 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400674 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200675 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
676 initSamplerDefaultPrecision(EbtSampler2D);
677 initSamplerDefaultPrecision(EbtSamplerCube);
678 // SamplerExternalOES is specified in the extension to have default precision.
679 initSamplerDefaultPrecision(EbtSamplerExternalOES);
Andrei Volykhina5527072017-03-22 16:46:30 +0300680 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
681 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200682 // It isn't specified whether Sampler2DRect has default precision.
683 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400684
Olli Etuahocce89652017-06-19 16:04:09 +0300685 symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh);
jchen104cdac9e2017-05-08 11:01:20 +0800686
Jamie Madill1b452142013-07-12 14:51:11 -0400687 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400688
689 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
690
691 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000692}
693
Olli Etuaho183d7e22015-11-20 15:59:09 +0200694void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
695{
696 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
Olli Etuahocce89652017-06-19 16:04:09 +0300697 symbolTable.setDefaultPrecision(samplerType, EbpLow);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200698}
699
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400700void TCompiler::setResourceString()
701{
702 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400703
704 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400705 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500706 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
707 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
708 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
709 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
710 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
711 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
712 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
713 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
714 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
715 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
716 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
717 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
718 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
719 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
720 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
721 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
722 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
723 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
724 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
725 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
726 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
727 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
728 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Martin Radev318f9aa2017-05-17 17:47:28 +0300729 << ":OVR_multiview:" << compileResources.OVR_multiview
Andrei Volykhina5527072017-03-22 16:46:30 +0300730 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Shaob5cc1192017-07-06 10:47:20 +0800731 << ":OES_geometry_shader:" << compileResources.OES_geometry_shader
Jamie Madillacb4b812016-11-07 13:50:29 -0500732 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
733 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
734 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
735 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
736 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Martin Radev318f9aa2017-05-17 17:47:28 +0300737 << ":MaxViewsOVR:" << compileResources.MaxViewsOVR
Jamie Madillacb4b812016-11-07 13:50:29 -0500738 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
739 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
Martin Radev84aa2dc2017-09-11 15:51:02 +0300740 << ":MinProgramTextureGatherOffset:" << compileResources.MinProgramTextureGatherOffset
741 << ":MaxProgramTextureGatherOffset:" << compileResources.MaxProgramTextureGatherOffset
Jamie Madillacb4b812016-11-07 13:50:29 -0500742 << ":MaxImageUnits:" << compileResources.MaxImageUnits
743 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
744 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
745 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
746 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
747 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
748 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
749 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
750 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
751 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
752 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
753 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
754 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
755 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
756 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
757 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
758 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
759 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
760 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
761 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
762 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
763 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
764 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
Shaob5cc1192017-07-06 10:47:20 +0800765 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize
Shaob5cc1192017-07-06 10:47:20 +0800766 << ":MaxGeometryUniformComponents:" << compileResources.MaxGeometryUniformComponents
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800767 << ":MaxGeometryUniformBlocks:" << compileResources.MaxGeometryUniformBlocks
768 << ":MaxGeometryInputComponents:" << compileResources.MaxGeometryInputComponents
769 << ":MaxGeometryOutputComponents:" << compileResources.MaxGeometryOutputComponents
770 << ":MaxGeometryOutputVertices:" << compileResources.MaxGeometryOutputVertices
771 << ":MaxGeometryTotalOutputComponents:" << compileResources.MaxGeometryTotalOutputComponents
772 << ":MaxGeometryTextureImageUnits:" << compileResources.MaxGeometryTextureImageUnits
773 << ":MaxGeometryAtomicCounterBuffers:" << compileResources.MaxGeometryAtomicCounterBuffers
774 << ":MaxGeometryAtomicCounters:" << compileResources.MaxGeometryAtomicCounters
775 << ":MaxGeometryShaderStorageBlocks:" << compileResources.MaxGeometryShaderStorageBlocks
776 << ":MaxGeometryShaderInvocations:" << compileResources.MaxGeometryShaderInvocations
777 << ":MaxGeometryImageUniforms:" << compileResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -0400778 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400779
780 builtInResourcesString = strstream.str();
781}
782
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800783void TCompiler::collectInterfaceBlocks()
784{
785 ASSERT(interfaceBlocks.empty());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800786 interfaceBlocks.reserve(uniformBlocks.size() + shaderStorageBlocks.size() + inBlocks.size());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800787 interfaceBlocks.insert(interfaceBlocks.end(), uniformBlocks.begin(), uniformBlocks.end());
788 interfaceBlocks.insert(interfaceBlocks.end(), shaderStorageBlocks.begin(),
789 shaderStorageBlocks.end());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800790 interfaceBlocks.insert(interfaceBlocks.end(), inBlocks.begin(), inBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800791}
792
alokp@chromium.org07620a52010-09-23 17:53:56 +0000793void TCompiler::clearResults()
794{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000795 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000796 infoSink.info.erase();
797 infoSink.obj.erase();
798 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000799 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000800
Jamie Madilled27c722014-07-02 15:31:23 -0400801 attributes.clear();
802 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000803 uniforms.clear();
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800804 inputVaryings.clear();
805 outputVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400806 interfaceBlocks.clear();
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800807 uniformBlocks.clear();
808 shaderStorageBlocks.clear();
Jiawei Shaod8105a02017-08-08 09:54:36 +0800809 inBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700810 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300811 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000812
Olli Etuaho09b04a22016-12-15 13:30:26 +0000813 mNumViews = -1;
814
Shaob5cc1192017-07-06 10:47:20 +0800815 mGeometryShaderInputPrimitiveType = EptUndefined;
816 mGeometryShaderOutputPrimitiveType = EptUndefined;
817 mGeometryShaderInvocations = 0;
818 mGeometryShaderMaxVertices = -1;
819
Olli Etuahodfa75e82017-01-23 09:43:06 -0800820 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000821
822 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200823
Yunchao Hed7297bf2017-04-19 15:27:10 +0800824 mSourcePath = nullptr;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000825}
826
Corentin Wallez71d147f2015-02-11 11:15:24 -0800827bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000828{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800829 mCallDag.clear();
830
Olli Etuaho77ba4082016-12-16 12:01:18 +0000831 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800832 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500833 case CallDAG::INITDAG_SUCCESS:
834 return true;
835 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500836 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000837 // Error message has already been written out.
838 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500839 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800840 }
841
842 UNREACHABLE();
843 return true;
844}
845
846bool TCompiler::checkCallDepth()
847{
848 std::vector<int> depths(mCallDag.size());
849
850 for (size_t i = 0; i < mCallDag.size(); i++)
851 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500852 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800853 auto &record = mCallDag.getRecordFromIndex(i);
854
855 for (auto &calleeIndex : record.callees)
856 {
857 depth = std::max(depth, depths[calleeIndex] + 1);
858 }
859
860 depths[i] = depth;
861
862 if (depth >= maxCallStackDepth)
863 {
864 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000865 std::stringstream errorStream;
866 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
867 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800868
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700869 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500870 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800871
872 while (currentFunction != -1)
873 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000874 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800875
876 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500877 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800878 {
879 if (depths[calleeIndex] == currentDepth - 1)
880 {
881 currentDepth--;
882 nextFunction = calleeIndex;
883 }
884 }
885
886 currentFunction = nextFunction;
887 }
888
Olli Etuaho77ba4082016-12-16 12:01:18 +0000889 std::string errorStr = errorStream.str();
890 mDiagnostics.globalError(errorStr.c_str());
891
Corentin Wallez71d147f2015-02-11 11:15:24 -0800892 return false;
893 }
894 }
895
896 return true;
897}
898
899bool TCompiler::tagUsedFunctions()
900{
901 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700902 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800903 {
Olli Etuahoec9232b2017-03-27 17:01:37 +0300904 if (mCallDag.getRecordFromIndex(i).name == "main")
Corentin Wallez71d147f2015-02-11 11:15:24 -0800905 {
906 internalTagUsedFunction(i);
907 return true;
908 }
909 }
910
Olli Etuaho77ba4082016-12-16 12:01:18 +0000911 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800912 return false;
913}
914
915void TCompiler::internalTagUsedFunction(size_t index)
916{
917 if (functionMetadata[index].used)
918 {
919 return;
920 }
921
922 functionMetadata[index].used = true;
923
924 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
925 {
926 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000927 }
928}
929
Corentin Walleza094a8a2015-04-07 11:53:06 -0700930// A predicate for the stl that returns if a top-level node is unused
931class TCompiler::UnusedPredicate
932{
933 public:
934 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500935 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700936 {
937 }
938
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500939 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700940 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000941 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
942 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700943
Olli Etuaho336b1472016-10-05 16:37:55 +0100944 const TFunctionSymbolInfo *functionInfo = nullptr;
945
Olli Etuaho16c745a2017-01-16 17:02:27 +0000946 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100947 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000948 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100949 }
Olli Etuaho16c745a2017-01-16 17:02:27 +0000950 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +0100951 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000952 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100953 }
954 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700955 {
956 return false;
957 }
958
Olli Etuaho336b1472016-10-05 16:37:55 +0100959 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700960 if (callDagIndex == CallDAG::InvalidIndex)
961 {
962 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +0000963 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700964 return true;
965 }
966
967 ASSERT(callDagIndex < mMetadatas->size());
968 return !(*mMetadatas)[callDagIndex].used;
969 }
970
971 private:
972 const CallDAG *mCallDag;
973 const std::vector<FunctionMetadata> *mMetadatas;
974};
975
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100976bool TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700977{
Corentin Walleza094a8a2015-04-07 11:53:06 -0700978 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100979 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700980
981 if (!sequence->empty())
982 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500983 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
984 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -0700985 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700986
987 return true;
988}
989
Olli Etuahob4279202017-05-18 15:08:24 +0300990bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400991{
Olli Etuahocccf2b02017-07-05 14:50:54 +0300992 if (!IsASTDepthBelowLimit(root, maxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -0400993 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000994 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -0400995 return false;
996 }
997
Olli Etuahob4279202017-05-18 15:08:24 +0300998 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200999 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001000 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001001 return false;
1002 }
1003
Jamie Madilleb1a0102013-07-08 13:31:38 -04001004 return true;
1005}
1006
Corentin Wallez1df16022016-10-27 08:16:56 -04001007bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
1008{
1009 return (compileOptions & SH_VARIABLES) != 0;
1010}
1011
1012bool TCompiler::wereVariablesCollected() const
1013{
1014 return variablesCollected;
1015}
1016
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001017void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001018{
Zhenyao Mo72111912016-07-20 17:45:56 -07001019 InitVariableList list;
1020 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
1021 var.name = "gl_Position";
1022 list.push_back(var);
Olli Etuahodaaff1c2017-07-05 18:03:26 +03001023 InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001024}
1025
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001026void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001027{
1028 sh::InterfaceBlockList list;
1029
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001030 for (auto block : uniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001031 {
1032 if (!block.staticUse &&
1033 (block.layout == sh::BLOCKLAYOUT_STANDARD || block.layout == sh::BLOCKLAYOUT_SHARED))
1034 {
1035 list.push_back(block);
1036 }
1037 }
1038
Zhenyao Mod7490962016-11-09 15:49:51 -08001039 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001040}
1041
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001042void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001043{
Zhenyao Mo72111912016-07-20 17:45:56 -07001044 InitVariableList list;
Jiawei Shaod27f5c82017-08-23 09:38:08 +08001045 if (shaderType == GL_VERTEX_SHADER || shaderType == GL_GEOMETRY_SHADER_OES)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001046 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001047 for (auto var : outputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001048 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001049 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001050 if (var.name == "gl_Position")
1051 {
1052 ASSERT(!mGLPositionInitialized);
1053 mGLPositionInitialized = true;
1054 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001055 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001056 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001057 else
1058 {
1059 ASSERT(shaderType == GL_FRAGMENT_SHADER);
1060 for (auto var : outputVariables)
1061 {
1062 list.push_back(var);
1063 }
1064 }
Olli Etuahodaaff1c2017-07-05 18:03:26 +03001065 InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001066}
1067
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001068const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001069{
1070 return extensionBehavior;
1071}
zmo@google.com32e97312011-08-24 01:03:11 +00001072
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001073const char *TCompiler::getSourcePath() const
1074{
1075 return mSourcePath;
1076}
1077
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001078const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001079{
1080 return compileResources;
1081}
1082
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001083const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001084{
1085 return arrayBoundsClamper;
1086}
1087
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001088ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1089{
1090 return clampingStrategy;
1091}
1092
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001093const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001094{
1095 return builtInFunctionEmulator;
1096}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001097
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001098void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001099{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001100 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1101 {
1102 TInfoSinkBase &sink = infoSink.obj;
1103 if (mPragma.stdgl.invariantAll)
1104 sink << "#pragma STDGL invariant(all)\n";
1105 }
1106}
1107
1108bool TCompiler::isVaryingDefined(const char *varyingName)
1109{
1110 ASSERT(variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001111 for (size_t ii = 0; ii < inputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001112 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001113 if (inputVaryings[ii].name == varyingName)
1114 {
1115 return true;
1116 }
1117 }
1118 for (size_t ii = 0; ii < outputVaryings.size(); ++ii)
1119 {
1120 if (outputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001121 {
1122 return true;
1123 }
1124 }
1125
1126 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001127}
Jamie Madillacb4b812016-11-07 13:50:29 -05001128
1129} // namespace sh