blob: ed2bcff8effe1e9842873953d7abfe290a12bd08 [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 Etuaho5c407bb2015-06-01 12:20:39 +030032#include "compiler/translator/RemovePow.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070033#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070034#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030035#include "compiler/translator/SeparateDeclarations.h"
36#include "compiler/translator/SimplifyLoopConditions.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030037#include "compiler/translator/SplitSequenceOperator.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070038#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080039#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040040#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020041#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040042#include "compiler/translator/ValidateOutputs.h"
43#include "compiler/translator/VariablePacker.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000044#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070045
Jamie Madillacb4b812016-11-07 13:50:29 -050046namespace sh
47{
48
Corentin Wallez28b65282016-06-16 07:24:50 -070049namespace
50{
51
52#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
53void DumpFuzzerCase(char const *const *shaderStrings,
54 size_t numStrings,
55 uint32_t type,
56 uint32_t spec,
57 uint32_t output,
58 uint64_t options)
59{
60 static int fileIndex = 0;
61
62 std::ostringstream o;
63 o << "corpus/" << fileIndex++ << ".sample";
64 std::string s = o.str();
65
66 // Must match the input format of the fuzzer
67 FILE *f = fopen(s.c_str(), "w");
68 fwrite(&type, sizeof(type), 1, f);
69 fwrite(&spec, sizeof(spec), 1, f);
70 fwrite(&output, sizeof(output), 1, f);
71 fwrite(&options, sizeof(options), 1, f);
72
73 char zero[128 - 20] = {0};
74 fwrite(&zero, 128 - 20, 1, f);
75
76 for (size_t i = 0; i < numStrings; i++)
77 {
78 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
79 }
80 fwrite(&zero, 1, 1, f);
81
82 fclose(f);
83}
84#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
85} // anonymous namespace
86
Jamie Madill5508f392014-02-20 13:31:36 -050087bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000088{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080089 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000090}
91
Qingqing Dengad0d0792015-04-08 14:25:06 -070092bool IsGLSL130OrNewer(ShShaderOutput output)
93{
Jamie Madillacb4b812016-11-07 13:50:29 -050094 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
95 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
96 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
97 output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
98 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070099}
100
Qiankun Miao705a9192016-08-29 10:05:27 +0800101bool IsGLSL420OrNewer(ShShaderOutput output)
102{
Jamie Madillacb4b812016-11-07 13:50:29 -0500103 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
104 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800105}
106
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800107bool IsGLSL410OrOlder(ShShaderOutput output)
108{
109 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
110 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
111 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
112}
113
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000114bool RemoveInvariant(sh::GLenum shaderType,
115 int shaderVersion,
116 ShShaderOutput outputType,
117 ShCompileOptions compileOptions)
118{
119 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
120 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
121 return true;
122
123 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800124 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000125 return true;
126
127 return false;
128}
129
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700130size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500131{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800132 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500133 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700134 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500135 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500136 case SH_WEBGL_SPEC:
137 return 256;
138 default:
139 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500140 }
141}
142
Shao77891c02017-06-23 16:30:17 +0800143int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
144{
145 switch (shaderType)
146 {
147 case GL_VERTEX_SHADER:
148 return resources.MaxVertexUniformVectors;
149 case GL_FRAGMENT_SHADER:
150 return resources.MaxFragmentUniformVectors;
Shaob5cc1192017-07-06 10:47:20 +0800151
152 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
Shao77891c02017-06-23 16:30:17 +0800153 case GL_COMPUTE_SHADER:
Shao77891c02017-06-23 16:30:17 +0800154 return resources.MaxComputeUniformComponents / 4;
Shaob5cc1192017-07-06 10:47:20 +0800155 case GL_GEOMETRY_SHADER_OES:
156 return resources.MaxGeometryUniformComponents / 4;
Shao77891c02017-06-23 16:30:17 +0800157 default:
Shaob5cc1192017-07-06 10:47:20 +0800158 UNREACHABLE();
Shao77891c02017-06-23 16:30:17 +0800159 return -1;
160 }
161}
162
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500163namespace
164{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700165
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800166class TScopedPoolAllocator
167{
168 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500169 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800170 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400171 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000172 SetGlobalPoolAllocator(mAllocator);
173 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800174 ~TScopedPoolAllocator()
175 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800176 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400177 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000178 }
179
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800180 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500181 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400182};
183
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800184class TScopedSymbolTableLevel
185{
186 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500187 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800188 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400189 ASSERT(mTable->atBuiltInLevel());
190 mTable->push();
191 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800192 ~TScopedSymbolTableLevel()
193 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400194 while (!mTable->atBuiltInLevel())
195 mTable->pop();
196 }
197
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800198 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500199 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000200};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700201
202int MapSpecToShaderVersion(ShShaderSpec spec)
203{
204 switch (spec)
205 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500206 case SH_GLES2_SPEC:
207 case SH_WEBGL_SPEC:
208 return 100;
209 case SH_GLES3_SPEC:
210 case SH_WEBGL2_SPEC:
211 return 300;
212 case SH_GLES3_1_SPEC:
213 case SH_WEBGL3_SPEC:
214 return 310;
215 default:
216 UNREACHABLE();
217 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700218 }
219}
220
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000221} // namespace
222
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800223TShHandleBase::TShHandleBase()
224{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000225 allocator.push();
226 SetGlobalPoolAllocator(&allocator);
227}
228
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800229TShHandleBase::~TShHandleBase()
230{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800231 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000232 allocator.popAll();
233}
234
Jamie Madill183bde52014-07-02 15:31:19 -0400235TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700236 : variablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300237 mGLPositionInitialized(false),
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700238 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000239 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400240 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400241 maxUniformVectors(0),
242 maxExpressionComplexity(0),
243 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200244 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000245 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000246 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200247 builtInFunctionEmulator(),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000248 mDiagnostics(infoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800249 mSourcePath(nullptr),
Shaob5cc1192017-07-06 10:47:20 +0800250 mComputeShaderLocalSizeDeclared(false),
251 mGeometryShaderMaxVertices(-1),
252 mGeometryShaderInvocations(0),
253 mGeometryShaderInputPrimitiveType(EptUndefined),
254 mGeometryShaderOutputPrimitiveType(EptUndefined)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000255{
Martin Radev802abe02016-08-04 17:48:32 +0300256 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000257}
258
259TCompiler::~TCompiler()
260{
261}
262
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800263bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300264{
265 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
266 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
267 // of ESSL 1.00 as in Appendix A of the spec).
268 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
269 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
270}
271
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500272bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000273{
Shao77891c02017-06-23 16:30:17 +0800274 shaderVersion = 100;
275
276 maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources);
277
Jamie Madilleb1a0102013-07-08 13:31:38 -0400278 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200279 maxCallStackDepth = resources.MaxCallStackDepth;
280 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400281
282 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000283
alokp@chromium.org07620a52010-09-23 17:53:56 +0000284 // Generate built-in symbol table.
285 if (!InitBuiltInSymbolTable(resources))
286 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000287 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000288 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000289
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000290 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
291 clampingStrategy = resources.ArrayIndexClampingStrategy;
292
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000293 hashFunction = resources.HashFunction;
294
alokp@chromium.org07620a52010-09-23 17:53:56 +0000295 return true;
296}
297
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100298TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
299 size_t numStrings,
300 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000301{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200302 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
303}
304
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100305TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
306 size_t numStrings,
307 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200308{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000309 clearResults();
310
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200311 ASSERT(numStrings > 0);
312 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000313
David Yen0fbd1282015-02-02 14:46:09 -0800314 // Reset the extension behavior for each compilation unit.
315 ResetExtensionBehavior(extensionBehavior);
316
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000317 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000318 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000319 if (compileOptions & SH_SOURCE_PATH)
320 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200321 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000322 ++firstSource;
323 }
324
Olli Etuahof119a262016-08-19 15:54:22 +0300325 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000326 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200327
Olli Etuahoa6996682015-10-12 14:32:30 +0300328 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000329
330 // We preserve symbols at the built-in level from compile-to-compile.
331 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400332 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000333
334 // Parse shader.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500335 bool success = (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
336 &parseContext) == 0) &&
337 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000338
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000339 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700340 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
341 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000342 mDiagnostics.globalError("unsupported shader version");
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700343 success = false;
344 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000345
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100346 TIntermBlock *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200347
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800348 if (success)
349 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700350 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700351 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700352
Martin Radev802abe02016-08-04 17:48:32 +0300353 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
354 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
355
Olli Etuaho09b04a22016-12-15 13:30:26 +0000356 mNumViews = parseContext.getNumViews();
357
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400358 root = parseContext.getTreeRoot();
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000359
Olli Etuahoa6996682015-10-12 14:32:30 +0300360 // Highp might have been auto-enabled based on shader version
361 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
362
Shaob5cc1192017-07-06 10:47:20 +0800363 if (success && shaderType == GL_GEOMETRY_SHADER_OES)
364 {
365 mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType();
366 mGeometryShaderOutputPrimitiveType =
367 parseContext.getGeometryShaderOutputPrimitiveType();
368 mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices();
369 mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations();
370 }
371
Jamie Madill6654bc92014-03-26 14:01:57 -0400372 // Disallow expressions deemed too complex.
373 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
374 success = limitExpressionComplexity(root);
375
Corentin Wallez71d147f2015-02-11 11:15:24 -0800376 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000377 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800378 success = initCallDag(root);
379
380 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
381 success = checkCallDepth();
382
383 // Checks which functions are used and if "main" exists
384 if (success)
385 {
386 functionMetadata.clear();
387 functionMetadata.resize(mCallDag.size());
388 success = tagUsedFunctions();
389 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000390
Corentin Walleza094a8a2015-04-07 11:53:06 -0700391 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
392 success = pruneUnusedFunctions(root);
393
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500394 // Prune empty declarations to work around driver bugs and to keep declaration output
395 // simple.
Olli Etuahoc6833112015-04-22 15:15:54 +0300396 if (success)
397 PruneEmptyDeclarations(root);
398
Andrei Volykhin73fa4b82017-05-05 18:45:06 +0300399 if (success && shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER)
Olli Etuaho12b0b392017-05-30 13:22:31 +0300400 {
401 success = ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers,
402 &mDiagnostics);
403 }
Jamie Madill05a80ce2013-06-20 11:55:49 -0400404
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300405 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
Olli Etuaho9ec79392017-03-31 23:04:23 +0300406 success =
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300407 ValidateLimitations(root, shaderType, &symbolTable, shaderVersion, &mDiagnostics);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000408
Jamie Madilld5696192016-10-06 11:09:24 -0400409 // Fail compilation if precision emulation not supported.
410 if (success && getResources().WEBGL_debug_shader_precision &&
411 getPragma().debugShaderPrecision)
412 {
413 if (!EmulatePrecision::SupportedInLanguage(outputType))
414 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000415 mDiagnostics.globalError("Precision emulation not supported for this output type.");
Jamie Madilld5696192016-10-06 11:09:24 -0400416 success = false;
417 }
418 }
419
zmo@google.com32e97312011-08-24 01:03:11 +0000420 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200421 if (success)
422 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400423 // TODO(jmadill): Remove global pool allocator.
424 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200425 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400426 GetGlobalPoolAllocator()->unlock();
Olli Etuahodfa75e82017-01-23 09:43:06 -0800427 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200428 }
zmo@google.com32e97312011-08-24 01:03:11 +0000429
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000430 // Clamping uniform array bounds needs to happen after validateLimitations pass.
431 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
432 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
433
Martin Radev69056a12017-05-18 11:14:50 +0300434 if (success && (compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300435 parseContext.isExtensionEnabled(TExtension::OVR_multiview) &&
436 getShaderType() != GL_COMPUTE_SHADER)
Martin Radev69056a12017-05-18 11:14:50 +0300437 {
Martin Radevc39a19a2017-07-07 18:52:09 +0300438 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300439 outputType, &symbolTable);
Martin Radev69056a12017-05-18 11:14:50 +0300440 }
441
Corentin Wallezd4b50542015-09-28 12:19:26 -0700442 // This pass might emit short circuits so keep it before the short circuit unfolding
443 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300444 RewriteDoWhile(root, &symbolTable);
Corentin Wallezd4b50542015-09-28 12:19:26 -0700445
Qiankun Miao09cfac62016-09-06 17:25:16 +0800446 if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION))
447 sh::AddAndTrueToLoopCondition(root);
448
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800449 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
450 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700451 UnfoldShortCircuitAST unfoldShortCircuit;
452 root->traverse(&unfoldShortCircuit);
453 unfoldShortCircuit.updateTree();
454 }
455
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300456 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
457 {
458 RemovePow(root);
459 }
460
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300461 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800462 {
Olli Etuaho19515012017-06-26 18:00:17 +0300463 ASSERT(!variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800464 CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings,
Jiawei Shaod8105a02017-08-08 09:54:36 +0800465 &outputVaryings, &uniformBlocks, &shaderStorageBlocks, &inBlocks,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800466 hashFunction, &symbolTable, shaderVersion, shaderType,
467 extensionBehavior);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800468 collectInterfaceBlocks();
Olli Etuaho19515012017-06-26 18:00:17 +0300469 variablesCollected = true;
Qin Jiajia7835b522016-10-08 11:20:17 +0800470 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
471 {
472 useAllMembersInUnusedStandardAndSharedBlocks(root);
473 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800474 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
475 {
Olli Etuaho19515012017-06-26 18:00:17 +0300476 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
477 // Appendix A, section 7, the shader does not use too many uniforms.
Olli Etuahod7487b12017-08-09 15:45:13 +0300478 success = CheckVariablesInPackingLimits(maxUniformVectors, uniforms);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800479 if (!success)
480 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000481 mDiagnostics.globalError("too many uniforms");
gman@chromium.org8d804792012-10-17 21:33:48 +0000482 }
483 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700484 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
485 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300486 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700487 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000488 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000489
Olli Etuahob12040c2017-06-27 14:20:45 +0300490 // gl_Position is always written in compatibility output mode.
491 // It may have been already initialized among other output variables, in that case we don't
492 // need to initialize it twice.
493 if (success && shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
494 ((compileOptions & SH_INIT_GL_POSITION) ||
495 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
496 {
497 initializeGLPosition(root);
498 mGLPositionInitialized = true;
499 }
500
Yuly Novikov817232e2017-02-22 18:36:10 -0500501 // Removing invariant declarations must be done after collecting variables.
502 // Otherwise, built-in invariant declarations don't apply.
503 if (success && RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
504 sh::RemoveInvariantDeclaration(root);
505
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700506 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
507 {
Olli Etuahob990b552016-10-27 12:29:17 +0100508 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300509 &symbolTable);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700510 }
511
Zhenyao Moe740add2014-07-18 17:01:01 -0700512 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
513 {
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300514 RegenerateStructNames gen(&symbolTable, shaderVersion);
Zhenyao Moe740add2014-07-18 17:01:01 -0700515 root->traverse(&gen);
516 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300517
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700518 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
519 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300520 IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers))
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700521 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300522 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300523 &symbolTable, shaderVersion);
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700524 }
525
Olli Etuaho3d932d82016-04-12 11:10:30 +0300526 if (success)
527 {
Olli Etuahoa16a84f2017-09-12 13:49:18 +0300528 DeferGlobalInitializers(root, needToInitializeGlobalsInAST(), &symbolTable);
Olli Etuaho3d932d82016-04-12 11:10:30 +0300529 }
Olli Etuaho9733cee2017-05-11 19:14:35 +0300530
Olli Etuahobb5a7e22017-08-30 13:03:12 +0300531 // Split multi declarations and remove calls to array length().
532 if (success)
533 {
534 // Note that SimplifyLoopConditions needs to be run before any other AST transformations
535 // that may need to generate new statements from loop conditions or loop expressions.
536 SimplifyLoopConditions(root,
537 IntermNodePatternMatcher::kMultiDeclaration |
538 IntermNodePatternMatcher::kArrayLengthMethod,
539 &getSymbolTable(), getShaderVersion());
540
541 // Note that separate declarations need to be run before other AST transformations that
542 // generate new statements from expressions.
543 SeparateDeclarations(root);
544
545 SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod,
546 &getSymbolTable(), getShaderVersion());
547
548 RemoveArrayLengthMethod(root);
549 }
550
Olli Etuaho9733cee2017-05-11 19:14:35 +0300551 if (success && (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && getOutputType())
552 {
553 // Initialize uninitialized local variables.
554 // In some cases initializing can generate extra statements in the parent block, such as
555 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
Olli Etuahobb5a7e22017-08-30 13:03:12 +0300556 // we need to first simplify loop conditions. We've already separated declarations
557 // earlier, which is also required. If we don't follow the Appendix A limitations, loop
558 // init statements can declare arrays or nameless structs and have multiple
559 // declarations.
Olli Etuaho9733cee2017-05-11 19:14:35 +0300560
561 if (!shouldRunLoopAndIndexingValidation(compileOptions))
562 {
563 SimplifyLoopConditions(root,
Olli Etuaho9733cee2017-05-11 19:14:35 +0300564 IntermNodePatternMatcher::kArrayDeclaration |
565 IntermNodePatternMatcher::kNamelessStructDeclaration,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300566 &getSymbolTable(), getShaderVersion());
Olli Etuaho9733cee2017-05-11 19:14:35 +0300567 }
Olli Etuaho9733cee2017-05-11 19:14:35 +0300568 InitializeUninitializedLocals(root, getShaderVersion());
569 }
Olli Etuahoab918822017-07-14 17:03:42 +0300570
571 if (success && getShaderType() == GL_VERTEX_SHADER &&
572 (compileOptions & SH_CLAMP_POINT_SIZE))
573 {
574 ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable());
575 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000576 }
577
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200578 if (success)
579 return root;
580
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800581 return nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200582}
583
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800584bool TCompiler::compile(const char *const shaderStrings[],
585 size_t numStrings,
586 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200587{
Corentin Wallez28b65282016-06-16 07:24:50 -0700588#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
589 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
590#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
591
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200592 if (numStrings == 0)
593 return true;
594
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800595 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700596
597 // Apply key workarounds.
598 if (shouldFlattenPragmaStdglInvariantAll())
599 {
600 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
601 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
602 }
603
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200604 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100605 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200606
607 if (root)
608 {
609 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuahocccf2b02017-07-05 14:50:54 +0300610 OutputTree(root, infoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200611
612 if (compileOptions & SH_OBJECT_CODE)
613 translate(root, compileOptions);
614
615 // The IntermNode tree doesn't need to be deleted here, since the
616 // memory will be freed in a big chunk by the PoolAllocator.
617 return true;
618 }
619 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000620}
621
Nicolas Capens49a88872013-06-20 09:54:03 -0400622bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000623{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000624 if (resources.MaxDrawBuffers < 1)
625 {
626 return false;
627 }
628 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
629 {
630 return false;
631 }
632
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000633 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400634 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000635
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300636 ASSERT(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500637 symbolTable.push(); // COMMON_BUILTINS
638 symbolTable.push(); // ESSL1_BUILTINS
639 symbolTable.push(); // ESSL3_BUILTINS
640 symbolTable.push(); // ESSL3_1_BUILTINS
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300641 symbolTable.push(); // GLSL_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000642
Jamie Madillacb4b812016-11-07 13:50:29 -0500643 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400644 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500645 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300646 symbolTable.setDefaultPrecision(EbtInt, EbpMedium);
Jamie Madillacb4b812016-11-07 13:50:29 -0500647 break;
648 case GL_VERTEX_SHADER:
Jamie Madillacb4b812016-11-07 13:50:29 -0500649 case GL_COMPUTE_SHADER:
Shaob5cc1192017-07-06 10:47:20 +0800650 case GL_GEOMETRY_SHADER_OES:
Olli Etuahocce89652017-06-19 16:04:09 +0300651 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
652 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500653 break;
654 default:
Shaob5cc1192017-07-06 10:47:20 +0800655 UNREACHABLE();
Nicolas Capens49a88872013-06-20 09:54:03 -0400656 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200657 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400658 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200659 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
660 initSamplerDefaultPrecision(EbtSampler2D);
661 initSamplerDefaultPrecision(EbtSamplerCube);
662 // SamplerExternalOES is specified in the extension to have default precision.
663 initSamplerDefaultPrecision(EbtSamplerExternalOES);
Andrei Volykhina5527072017-03-22 16:46:30 +0300664 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
665 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200666 // It isn't specified whether Sampler2DRect has default precision.
667 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400668
Olli Etuahocce89652017-06-19 16:04:09 +0300669 symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh);
jchen104cdac9e2017-05-08 11:01:20 +0800670
Jamie Madill1b452142013-07-12 14:51:11 -0400671 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400672
673 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
674
675 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000676}
677
Olli Etuaho183d7e22015-11-20 15:59:09 +0200678void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
679{
680 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
Olli Etuahocce89652017-06-19 16:04:09 +0300681 symbolTable.setDefaultPrecision(samplerType, EbpLow);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200682}
683
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400684void TCompiler::setResourceString()
685{
686 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400687
688 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400689 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500690 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
691 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
692 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
693 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
694 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
695 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
696 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
697 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
698 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
699 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
700 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
701 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
702 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
703 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
704 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
705 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
706 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
707 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
708 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
709 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
710 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
711 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
712 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Martin Radev318f9aa2017-05-17 17:47:28 +0300713 << ":OVR_multiview:" << compileResources.OVR_multiview
Andrei Volykhina5527072017-03-22 16:46:30 +0300714 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Shaob5cc1192017-07-06 10:47:20 +0800715 << ":OES_geometry_shader:" << compileResources.OES_geometry_shader
Jamie Madillacb4b812016-11-07 13:50:29 -0500716 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
717 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
718 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
719 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
720 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Martin Radev318f9aa2017-05-17 17:47:28 +0300721 << ":MaxViewsOVR:" << compileResources.MaxViewsOVR
Jamie Madillacb4b812016-11-07 13:50:29 -0500722 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
723 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
724 << ":MaxImageUnits:" << compileResources.MaxImageUnits
725 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
726 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
727 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
728 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
729 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
730 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
731 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
732 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
733 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
734 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
735 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
736 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
737 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
738 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
739 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
740 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
741 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
742 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
743 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
744 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
745 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
746 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
Shaob5cc1192017-07-06 10:47:20 +0800747 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize
Shaob5cc1192017-07-06 10:47:20 +0800748 << ":MaxGeometryUniformComponents:" << compileResources.MaxGeometryUniformComponents
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800749 << ":MaxGeometryUniformBlocks:" << compileResources.MaxGeometryUniformBlocks
750 << ":MaxGeometryInputComponents:" << compileResources.MaxGeometryInputComponents
751 << ":MaxGeometryOutputComponents:" << compileResources.MaxGeometryOutputComponents
752 << ":MaxGeometryOutputVertices:" << compileResources.MaxGeometryOutputVertices
753 << ":MaxGeometryTotalOutputComponents:" << compileResources.MaxGeometryTotalOutputComponents
754 << ":MaxGeometryTextureImageUnits:" << compileResources.MaxGeometryTextureImageUnits
755 << ":MaxGeometryAtomicCounterBuffers:" << compileResources.MaxGeometryAtomicCounterBuffers
756 << ":MaxGeometryAtomicCounters:" << compileResources.MaxGeometryAtomicCounters
757 << ":MaxGeometryShaderStorageBlocks:" << compileResources.MaxGeometryShaderStorageBlocks
758 << ":MaxGeometryShaderInvocations:" << compileResources.MaxGeometryShaderInvocations
759 << ":MaxGeometryImageUniforms:" << compileResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -0400760 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400761
762 builtInResourcesString = strstream.str();
763}
764
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800765void TCompiler::collectInterfaceBlocks()
766{
767 ASSERT(interfaceBlocks.empty());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800768 interfaceBlocks.reserve(uniformBlocks.size() + shaderStorageBlocks.size() + inBlocks.size());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800769 interfaceBlocks.insert(interfaceBlocks.end(), uniformBlocks.begin(), uniformBlocks.end());
770 interfaceBlocks.insert(interfaceBlocks.end(), shaderStorageBlocks.begin(),
771 shaderStorageBlocks.end());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800772 interfaceBlocks.insert(interfaceBlocks.end(), inBlocks.begin(), inBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800773}
774
alokp@chromium.org07620a52010-09-23 17:53:56 +0000775void TCompiler::clearResults()
776{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000777 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000778 infoSink.info.erase();
779 infoSink.obj.erase();
780 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000781 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000782
Jamie Madilled27c722014-07-02 15:31:23 -0400783 attributes.clear();
784 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000785 uniforms.clear();
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800786 inputVaryings.clear();
787 outputVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400788 interfaceBlocks.clear();
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800789 uniformBlocks.clear();
790 shaderStorageBlocks.clear();
Jiawei Shaod8105a02017-08-08 09:54:36 +0800791 inBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700792 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300793 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000794
Olli Etuaho09b04a22016-12-15 13:30:26 +0000795 mNumViews = -1;
796
Shaob5cc1192017-07-06 10:47:20 +0800797 mGeometryShaderInputPrimitiveType = EptUndefined;
798 mGeometryShaderOutputPrimitiveType = EptUndefined;
799 mGeometryShaderInvocations = 0;
800 mGeometryShaderMaxVertices = -1;
801
Olli Etuahodfa75e82017-01-23 09:43:06 -0800802 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000803
804 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200805
Yunchao Hed7297bf2017-04-19 15:27:10 +0800806 mSourcePath = nullptr;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000807}
808
Corentin Wallez71d147f2015-02-11 11:15:24 -0800809bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000810{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800811 mCallDag.clear();
812
Olli Etuaho77ba4082016-12-16 12:01:18 +0000813 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800814 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500815 case CallDAG::INITDAG_SUCCESS:
816 return true;
817 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500818 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000819 // Error message has already been written out.
820 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500821 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800822 }
823
824 UNREACHABLE();
825 return true;
826}
827
828bool TCompiler::checkCallDepth()
829{
830 std::vector<int> depths(mCallDag.size());
831
832 for (size_t i = 0; i < mCallDag.size(); i++)
833 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500834 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800835 auto &record = mCallDag.getRecordFromIndex(i);
836
837 for (auto &calleeIndex : record.callees)
838 {
839 depth = std::max(depth, depths[calleeIndex] + 1);
840 }
841
842 depths[i] = depth;
843
844 if (depth >= maxCallStackDepth)
845 {
846 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000847 std::stringstream errorStream;
848 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
849 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800850
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700851 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500852 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800853
854 while (currentFunction != -1)
855 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000856 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800857
858 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500859 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800860 {
861 if (depths[calleeIndex] == currentDepth - 1)
862 {
863 currentDepth--;
864 nextFunction = calleeIndex;
865 }
866 }
867
868 currentFunction = nextFunction;
869 }
870
Olli Etuaho77ba4082016-12-16 12:01:18 +0000871 std::string errorStr = errorStream.str();
872 mDiagnostics.globalError(errorStr.c_str());
873
Corentin Wallez71d147f2015-02-11 11:15:24 -0800874 return false;
875 }
876 }
877
878 return true;
879}
880
881bool TCompiler::tagUsedFunctions()
882{
883 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700884 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800885 {
Olli Etuahoec9232b2017-03-27 17:01:37 +0300886 if (mCallDag.getRecordFromIndex(i).name == "main")
Corentin Wallez71d147f2015-02-11 11:15:24 -0800887 {
888 internalTagUsedFunction(i);
889 return true;
890 }
891 }
892
Olli Etuaho77ba4082016-12-16 12:01:18 +0000893 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800894 return false;
895}
896
897void TCompiler::internalTagUsedFunction(size_t index)
898{
899 if (functionMetadata[index].used)
900 {
901 return;
902 }
903
904 functionMetadata[index].used = true;
905
906 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
907 {
908 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000909 }
910}
911
Corentin Walleza094a8a2015-04-07 11:53:06 -0700912// A predicate for the stl that returns if a top-level node is unused
913class TCompiler::UnusedPredicate
914{
915 public:
916 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500917 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700918 {
919 }
920
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500921 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700922 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000923 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
924 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700925
Olli Etuaho336b1472016-10-05 16:37:55 +0100926 const TFunctionSymbolInfo *functionInfo = nullptr;
927
Olli Etuaho16c745a2017-01-16 17:02:27 +0000928 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100929 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000930 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100931 }
Olli Etuaho16c745a2017-01-16 17:02:27 +0000932 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +0100933 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000934 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100935 }
936 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700937 {
938 return false;
939 }
940
Olli Etuaho336b1472016-10-05 16:37:55 +0100941 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700942 if (callDagIndex == CallDAG::InvalidIndex)
943 {
944 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +0000945 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700946 return true;
947 }
948
949 ASSERT(callDagIndex < mMetadatas->size());
950 return !(*mMetadatas)[callDagIndex].used;
951 }
952
953 private:
954 const CallDAG *mCallDag;
955 const std::vector<FunctionMetadata> *mMetadatas;
956};
957
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100958bool TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700959{
Corentin Walleza094a8a2015-04-07 11:53:06 -0700960 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100961 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700962
963 if (!sequence->empty())
964 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500965 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
966 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -0700967 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700968
969 return true;
970}
971
Olli Etuahob4279202017-05-18 15:08:24 +0300972bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400973{
Olli Etuahocccf2b02017-07-05 14:50:54 +0300974 if (!IsASTDepthBelowLimit(root, maxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -0400975 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000976 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -0400977 return false;
978 }
979
Olli Etuahob4279202017-05-18 15:08:24 +0300980 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200981 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000982 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200983 return false;
984 }
985
Jamie Madilleb1a0102013-07-08 13:31:38 -0400986 return true;
987}
988
Corentin Wallez1df16022016-10-27 08:16:56 -0400989bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
990{
991 return (compileOptions & SH_VARIABLES) != 0;
992}
993
994bool TCompiler::wereVariablesCollected() const
995{
996 return variablesCollected;
997}
998
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300999void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001000{
Zhenyao Mo72111912016-07-20 17:45:56 -07001001 InitVariableList list;
1002 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
1003 var.name = "gl_Position";
1004 list.push_back(var);
Olli Etuahodaaff1c2017-07-05 18:03:26 +03001005 InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001006}
1007
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001008void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001009{
1010 sh::InterfaceBlockList list;
1011
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001012 for (auto block : uniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001013 {
1014 if (!block.staticUse &&
1015 (block.layout == sh::BLOCKLAYOUT_STANDARD || block.layout == sh::BLOCKLAYOUT_SHARED))
1016 {
1017 list.push_back(block);
1018 }
1019 }
1020
Zhenyao Mod7490962016-11-09 15:49:51 -08001021 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001022}
1023
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001024void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001025{
Zhenyao Mo72111912016-07-20 17:45:56 -07001026 InitVariableList list;
Jiawei Shaod27f5c82017-08-23 09:38:08 +08001027 if (shaderType == GL_VERTEX_SHADER || shaderType == GL_GEOMETRY_SHADER_OES)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001028 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001029 for (auto var : outputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001030 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001031 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001032 if (var.name == "gl_Position")
1033 {
1034 ASSERT(!mGLPositionInitialized);
1035 mGLPositionInitialized = true;
1036 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001037 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001038 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001039 else
1040 {
1041 ASSERT(shaderType == GL_FRAGMENT_SHADER);
1042 for (auto var : outputVariables)
1043 {
1044 list.push_back(var);
1045 }
1046 }
Olli Etuahodaaff1c2017-07-05 18:03:26 +03001047 InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001048}
1049
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001050const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001051{
1052 return extensionBehavior;
1053}
zmo@google.com32e97312011-08-24 01:03:11 +00001054
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001055const char *TCompiler::getSourcePath() const
1056{
1057 return mSourcePath;
1058}
1059
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001060const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001061{
1062 return compileResources;
1063}
1064
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001065const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001066{
1067 return arrayBoundsClamper;
1068}
1069
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001070ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1071{
1072 return clampingStrategy;
1073}
1074
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001075const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001076{
1077 return builtInFunctionEmulator;
1078}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001079
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001080void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001081{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001082 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1083 {
1084 TInfoSinkBase &sink = infoSink.obj;
1085 if (mPragma.stdgl.invariantAll)
1086 sink << "#pragma STDGL invariant(all)\n";
1087 }
1088}
1089
1090bool TCompiler::isVaryingDefined(const char *varyingName)
1091{
1092 ASSERT(variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001093 for (size_t ii = 0; ii < inputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001094 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001095 if (inputVaryings[ii].name == varyingName)
1096 {
1097 return true;
1098 }
1099 }
1100 for (size_t ii = 0; ii < outputVaryings.size(); ++ii)
1101 {
1102 if (outputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001103 {
1104 return true;
1105 }
1106 }
1107
1108 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001109}
Jamie Madillacb4b812016-11-07 13:50:29 -05001110
1111} // namespace sh