blob: 4b39fe1d7384b903355bfa5abc5498f5d858e4c6 [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"
Qiankun Miao705a9192016-08-29 10:05:27 +080030#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030031#include "compiler/translator/RemovePow.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070032#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070033#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030034#include "compiler/translator/SeparateDeclarations.h"
35#include "compiler/translator/SimplifyLoopConditions.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070036#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080037#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040038#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020039#include "compiler/translator/ValidateMaxParameters.h"
Olli Etuaho09b04a22016-12-15 13:30:26 +000040#include "compiler/translator/ValidateMultiviewWebGL.h"
Geoff Lang17732822013-08-29 13:46:49 -040041#include "compiler/translator/ValidateOutputs.h"
42#include "compiler/translator/VariablePacker.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000043#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070044
Jamie Madillacb4b812016-11-07 13:50:29 -050045namespace sh
46{
47
Corentin Wallez28b65282016-06-16 07:24:50 -070048namespace
49{
50
51#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
52void DumpFuzzerCase(char const *const *shaderStrings,
53 size_t numStrings,
54 uint32_t type,
55 uint32_t spec,
56 uint32_t output,
57 uint64_t options)
58{
59 static int fileIndex = 0;
60
61 std::ostringstream o;
62 o << "corpus/" << fileIndex++ << ".sample";
63 std::string s = o.str();
64
65 // Must match the input format of the fuzzer
66 FILE *f = fopen(s.c_str(), "w");
67 fwrite(&type, sizeof(type), 1, f);
68 fwrite(&spec, sizeof(spec), 1, f);
69 fwrite(&output, sizeof(output), 1, f);
70 fwrite(&options, sizeof(options), 1, f);
71
72 char zero[128 - 20] = {0};
73 fwrite(&zero, 128 - 20, 1, f);
74
75 for (size_t i = 0; i < numStrings; i++)
76 {
77 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
78 }
79 fwrite(&zero, 1, 1, f);
80
81 fclose(f);
82}
83#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
84} // anonymous namespace
85
Jamie Madill5508f392014-02-20 13:31:36 -050086bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000087{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080088 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000089}
90
Qingqing Dengad0d0792015-04-08 14:25:06 -070091bool IsGLSL130OrNewer(ShShaderOutput output)
92{
Jamie Madillacb4b812016-11-07 13:50:29 -050093 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
94 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
95 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
96 output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
97 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070098}
99
Qiankun Miao705a9192016-08-29 10:05:27 +0800100bool IsGLSL420OrNewer(ShShaderOutput output)
101{
Jamie Madillacb4b812016-11-07 13:50:29 -0500102 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
103 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800104}
105
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800106bool IsGLSL410OrOlder(ShShaderOutput output)
107{
108 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
109 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
110 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
111}
112
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000113bool RemoveInvariant(sh::GLenum shaderType,
114 int shaderVersion,
115 ShShaderOutput outputType,
116 ShCompileOptions compileOptions)
117{
118 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
119 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
120 return true;
121
122 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800123 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000124 return true;
125
126 return false;
127}
128
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700129size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500130{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800131 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500132 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700133 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500134 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500135 case SH_WEBGL_SPEC:
136 return 256;
137 default:
138 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500139 }
140}
141
Shao77891c02017-06-23 16:30:17 +0800142int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
143{
144 switch (shaderType)
145 {
146 case GL_VERTEX_SHADER:
147 return resources.MaxVertexUniformVectors;
148 case GL_FRAGMENT_SHADER:
149 return resources.MaxFragmentUniformVectors;
Shaob5cc1192017-07-06 10:47:20 +0800150
151 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
Shao77891c02017-06-23 16:30:17 +0800152 case GL_COMPUTE_SHADER:
Shao77891c02017-06-23 16:30:17 +0800153 return resources.MaxComputeUniformComponents / 4;
Shaob5cc1192017-07-06 10:47:20 +0800154 case GL_GEOMETRY_SHADER_OES:
155 return resources.MaxGeometryUniformComponents / 4;
Shao77891c02017-06-23 16:30:17 +0800156 default:
Shaob5cc1192017-07-06 10:47:20 +0800157 UNREACHABLE();
Shao77891c02017-06-23 16:30:17 +0800158 return -1;
159 }
160}
161
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500162namespace
163{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700164
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800165class TScopedPoolAllocator
166{
167 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500168 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800169 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400170 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000171 SetGlobalPoolAllocator(mAllocator);
172 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800173 ~TScopedPoolAllocator()
174 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800175 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400176 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000177 }
178
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800179 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500180 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400181};
182
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800183class TScopedSymbolTableLevel
184{
185 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500186 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800187 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400188 ASSERT(mTable->atBuiltInLevel());
189 mTable->push();
190 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800191 ~TScopedSymbolTableLevel()
192 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400193 while (!mTable->atBuiltInLevel())
194 mTable->pop();
195 }
196
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800197 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500198 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000199};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700200
201int MapSpecToShaderVersion(ShShaderSpec spec)
202{
203 switch (spec)
204 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500205 case SH_GLES2_SPEC:
206 case SH_WEBGL_SPEC:
207 return 100;
208 case SH_GLES3_SPEC:
209 case SH_WEBGL2_SPEC:
210 return 300;
211 case SH_GLES3_1_SPEC:
212 case SH_WEBGL3_SPEC:
213 return 310;
214 default:
215 UNREACHABLE();
216 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700217 }
218}
219
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000220} // namespace
221
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800222TShHandleBase::TShHandleBase()
223{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000224 allocator.push();
225 SetGlobalPoolAllocator(&allocator);
226}
227
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800228TShHandleBase::~TShHandleBase()
229{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800230 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000231 allocator.popAll();
232}
233
Jamie Madill183bde52014-07-02 15:31:19 -0400234TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700235 : variablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300236 mGLPositionInitialized(false),
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700237 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000238 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400239 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400240 maxUniformVectors(0),
241 maxExpressionComplexity(0),
242 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200243 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000244 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000245 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200246 builtInFunctionEmulator(),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000247 mDiagnostics(infoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800248 mSourcePath(nullptr),
Shaob5cc1192017-07-06 10:47:20 +0800249 mComputeShaderLocalSizeDeclared(false),
250 mGeometryShaderMaxVertices(-1),
251 mGeometryShaderInvocations(0),
252 mGeometryShaderInputPrimitiveType(EptUndefined),
253 mGeometryShaderOutputPrimitiveType(EptUndefined)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000254{
Martin Radev802abe02016-08-04 17:48:32 +0300255 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000256}
257
258TCompiler::~TCompiler()
259{
260}
261
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800262bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300263{
264 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
265 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
266 // of ESSL 1.00 as in Appendix A of the spec).
267 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
268 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
269}
270
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500271bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000272{
Shao77891c02017-06-23 16:30:17 +0800273 shaderVersion = 100;
274
275 maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources);
276
Jamie Madilleb1a0102013-07-08 13:31:38 -0400277 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200278 maxCallStackDepth = resources.MaxCallStackDepth;
279 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400280
281 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000282
alokp@chromium.org07620a52010-09-23 17:53:56 +0000283 // Generate built-in symbol table.
284 if (!InitBuiltInSymbolTable(resources))
285 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000286 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000287 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000288
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000289 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
290 clampingStrategy = resources.ArrayIndexClampingStrategy;
291
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000292 hashFunction = resources.HashFunction;
293
alokp@chromium.org07620a52010-09-23 17:53:56 +0000294 return true;
295}
296
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100297TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
298 size_t numStrings,
299 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000300{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200301 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
302}
303
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100304TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
305 size_t numStrings,
306 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200307{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000308 clearResults();
309
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200310 ASSERT(numStrings > 0);
311 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000312
David Yen0fbd1282015-02-02 14:46:09 -0800313 // Reset the extension behavior for each compilation unit.
314 ResetExtensionBehavior(extensionBehavior);
315
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000316 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000317 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000318 if (compileOptions & SH_SOURCE_PATH)
319 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200320 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000321 ++firstSource;
322 }
323
Olli Etuahof119a262016-08-19 15:54:22 +0300324 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000325 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200326
Olli Etuahoa6996682015-10-12 14:32:30 +0300327 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000328
329 // We preserve symbols at the built-in level from compile-to-compile.
330 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400331 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000332
333 // Parse shader.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500334 bool success = (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
335 &parseContext) == 0) &&
336 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000337
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000338 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700339 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
340 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000341 mDiagnostics.globalError("unsupported shader version");
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700342 success = false;
343 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000344
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100345 TIntermBlock *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200346
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800347 if (success)
348 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700349 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700350 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700351
Martin Radev802abe02016-08-04 17:48:32 +0300352 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
353 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
354
Olli Etuaho09b04a22016-12-15 13:30:26 +0000355 mNumViews = parseContext.getNumViews();
356
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400357 root = parseContext.getTreeRoot();
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000358
Olli Etuahoa6996682015-10-12 14:32:30 +0300359 // Highp might have been auto-enabled based on shader version
360 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
361
Olli Etuaho09b04a22016-12-15 13:30:26 +0000362 if (success && (IsWebGLBasedSpec(shaderSpec) &&
363 IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") &&
364 IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2")))
365 {
366 // Can't enable both extensions at the same time.
367 mDiagnostics.globalError("Can't enable both OVR_multiview and OVR_multiview2");
368 success = false;
369 }
370
Shaob5cc1192017-07-06 10:47:20 +0800371 if (success && shaderType == GL_GEOMETRY_SHADER_OES)
372 {
373 mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType();
374 mGeometryShaderOutputPrimitiveType =
375 parseContext.getGeometryShaderOutputPrimitiveType();
376 mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices();
377 mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations();
378 }
379
Jamie Madill6654bc92014-03-26 14:01:57 -0400380 // Disallow expressions deemed too complex.
381 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
382 success = limitExpressionComplexity(root);
383
Corentin Wallez71d147f2015-02-11 11:15:24 -0800384 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000385 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800386 success = initCallDag(root);
387
388 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
389 success = checkCallDepth();
390
391 // Checks which functions are used and if "main" exists
392 if (success)
393 {
394 functionMetadata.clear();
395 functionMetadata.resize(mCallDag.size());
396 success = tagUsedFunctions();
397 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000398
Corentin Walleza094a8a2015-04-07 11:53:06 -0700399 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
400 success = pruneUnusedFunctions(root);
401
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500402 // Prune empty declarations to work around driver bugs and to keep declaration output
403 // simple.
Olli Etuahoc6833112015-04-22 15:15:54 +0300404 if (success)
405 PruneEmptyDeclarations(root);
406
Andrei Volykhin73fa4b82017-05-05 18:45:06 +0300407 if (success && shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER)
Olli Etuaho12b0b392017-05-30 13:22:31 +0300408 {
409 success = ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers,
410 &mDiagnostics);
411 }
Jamie Madill05a80ce2013-06-20 11:55:49 -0400412
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300413 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
Olli Etuaho9ec79392017-03-31 23:04:23 +0300414 success =
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300415 ValidateLimitations(root, shaderType, &symbolTable, shaderVersion, &mDiagnostics);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000416
Olli Etuaho09b04a22016-12-15 13:30:26 +0000417 bool multiview2 = IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview2");
418 if (success && compileResources.OVR_multiview && IsWebGLBasedSpec(shaderSpec) &&
419 (IsExtensionEnabled(extensionBehavior, "GL_OVR_multiview") || multiview2))
Olli Etuaho01d0ad02017-01-22 14:51:23 -0800420 success = ValidateMultiviewWebGL(root, shaderType, symbolTable, shaderVersion,
421 multiview2, &mDiagnostics);
Olli Etuaho09b04a22016-12-15 13:30:26 +0000422
Jamie Madilld5696192016-10-06 11:09:24 -0400423 // Fail compilation if precision emulation not supported.
424 if (success && getResources().WEBGL_debug_shader_precision &&
425 getPragma().debugShaderPrecision)
426 {
427 if (!EmulatePrecision::SupportedInLanguage(outputType))
428 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000429 mDiagnostics.globalError("Precision emulation not supported for this output type.");
Jamie Madilld5696192016-10-06 11:09:24 -0400430 success = false;
431 }
432 }
433
zmo@google.com32e97312011-08-24 01:03:11 +0000434 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200435 if (success)
436 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400437 // TODO(jmadill): Remove global pool allocator.
438 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200439 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400440 GetGlobalPoolAllocator()->unlock();
Olli Etuahodfa75e82017-01-23 09:43:06 -0800441 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200442 }
zmo@google.com32e97312011-08-24 01:03:11 +0000443
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000444 // Clamping uniform array bounds needs to happen after validateLimitations pass.
445 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
446 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
447
Martin Radev69056a12017-05-18 11:14:50 +0300448 if (success && (compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
Martin Radev7ef89a42017-07-05 14:23:06 +0300449 parseContext.isMultiviewExtensionEnabled() && getShaderType() != GL_COMPUTE_SHADER)
Martin Radev69056a12017-05-18 11:14:50 +0300450 {
Martin Radevc39a19a2017-07-07 18:52:09 +0300451 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300452 outputType, &symbolTable);
Martin Radev69056a12017-05-18 11:14:50 +0300453 }
454
Corentin Wallezd4b50542015-09-28 12:19:26 -0700455 // This pass might emit short circuits so keep it before the short circuit unfolding
456 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300457 RewriteDoWhile(root, &symbolTable);
Corentin Wallezd4b50542015-09-28 12:19:26 -0700458
Qiankun Miao09cfac62016-09-06 17:25:16 +0800459 if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION))
460 sh::AddAndTrueToLoopCondition(root);
461
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800462 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
463 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700464 UnfoldShortCircuitAST unfoldShortCircuit;
465 root->traverse(&unfoldShortCircuit);
466 unfoldShortCircuit.updateTree();
467 }
468
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300469 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
470 {
471 RemovePow(root);
472 }
473
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300474 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800475 {
Olli Etuaho19515012017-06-26 18:00:17 +0300476 ASSERT(!variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800477 CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings,
Jiawei Shaod8105a02017-08-08 09:54:36 +0800478 &outputVaryings, &uniformBlocks, &shaderStorageBlocks, &inBlocks,
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800479 hashFunction, &symbolTable, shaderVersion, shaderType,
480 extensionBehavior);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800481 collectInterfaceBlocks();
Olli Etuaho19515012017-06-26 18:00:17 +0300482 variablesCollected = true;
Qin Jiajia7835b522016-10-08 11:20:17 +0800483 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
484 {
485 useAllMembersInUnusedStandardAndSharedBlocks(root);
486 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800487 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
488 {
Olli Etuaho19515012017-06-26 18:00:17 +0300489 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
490 // Appendix A, section 7, the shader does not use too many uniforms.
Olli Etuahod7487b12017-08-09 15:45:13 +0300491 success = CheckVariablesInPackingLimits(maxUniformVectors, uniforms);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800492 if (!success)
493 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000494 mDiagnostics.globalError("too many uniforms");
gman@chromium.org8d804792012-10-17 21:33:48 +0000495 }
496 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700497 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
498 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300499 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700500 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000501 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000502
Olli Etuahob12040c2017-06-27 14:20:45 +0300503 // gl_Position is always written in compatibility output mode.
504 // It may have been already initialized among other output variables, in that case we don't
505 // need to initialize it twice.
506 if (success && shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
507 ((compileOptions & SH_INIT_GL_POSITION) ||
508 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
509 {
510 initializeGLPosition(root);
511 mGLPositionInitialized = true;
512 }
513
Yuly Novikov817232e2017-02-22 18:36:10 -0500514 // Removing invariant declarations must be done after collecting variables.
515 // Otherwise, built-in invariant declarations don't apply.
516 if (success && RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
517 sh::RemoveInvariantDeclaration(root);
518
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700519 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
520 {
Olli Etuahob990b552016-10-27 12:29:17 +0100521 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300522 &symbolTable);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700523 }
524
Zhenyao Moe740add2014-07-18 17:01:01 -0700525 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
526 {
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300527 RegenerateStructNames gen(&symbolTable, shaderVersion);
Zhenyao Moe740add2014-07-18 17:01:01 -0700528 root->traverse(&gen);
529 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300530
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700531 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
532 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
533 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
534 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +0300535 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300536 &symbolTable, shaderVersion);
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700537 }
538
Olli Etuaho3d932d82016-04-12 11:10:30 +0300539 if (success)
540 {
Olli Etuaho0ffc4412017-05-19 14:18:55 +0300541 DeferGlobalInitializers(root, needToInitializeGlobalsInAST());
Olli Etuaho3d932d82016-04-12 11:10:30 +0300542 }
Olli Etuaho9733cee2017-05-11 19:14:35 +0300543
544 if (success && (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && getOutputType())
545 {
546 // Initialize uninitialized local variables.
547 // In some cases initializing can generate extra statements in the parent block, such as
548 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
549 // we need to first simplify loop conditions and separate declarations. If we don't
550 // follow the Appendix A limitations, loop init statements can declare arrays or
551 // nameless structs and have multiple declarations.
552
553 if (!shouldRunLoopAndIndexingValidation(compileOptions))
554 {
555 SimplifyLoopConditions(root,
556 IntermNodePatternMatcher::kMultiDeclaration |
557 IntermNodePatternMatcher::kArrayDeclaration |
558 IntermNodePatternMatcher::kNamelessStructDeclaration,
Olli Etuahoa5e693a2017-07-13 16:07:26 +0300559 &getSymbolTable(), getShaderVersion());
Olli Etuaho9733cee2017-05-11 19:14:35 +0300560 }
561 // We only really need to separate array declarations and nameless struct declarations,
562 // but it's simpler to just use the regular SeparateDeclarations.
563 SeparateDeclarations(root);
564 InitializeUninitializedLocals(root, getShaderVersion());
565 }
Olli Etuahoab918822017-07-14 17:03:42 +0300566
567 if (success && getShaderType() == GL_VERTEX_SHADER &&
568 (compileOptions & SH_CLAMP_POINT_SIZE))
569 {
570 ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable());
571 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000572 }
573
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200574 if (success)
575 return root;
576
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800577 return nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200578}
579
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800580bool TCompiler::compile(const char *const shaderStrings[],
581 size_t numStrings,
582 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200583{
Corentin Wallez28b65282016-06-16 07:24:50 -0700584#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
585 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
586#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
587
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200588 if (numStrings == 0)
589 return true;
590
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800591 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700592
593 // Apply key workarounds.
594 if (shouldFlattenPragmaStdglInvariantAll())
595 {
596 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
597 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
598 }
599
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200600 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100601 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200602
603 if (root)
604 {
605 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuahocccf2b02017-07-05 14:50:54 +0300606 OutputTree(root, infoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200607
608 if (compileOptions & SH_OBJECT_CODE)
609 translate(root, compileOptions);
610
611 // The IntermNode tree doesn't need to be deleted here, since the
612 // memory will be freed in a big chunk by the PoolAllocator.
613 return true;
614 }
615 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000616}
617
Nicolas Capens49a88872013-06-20 09:54:03 -0400618bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000619{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000620 if (resources.MaxDrawBuffers < 1)
621 {
622 return false;
623 }
624 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
625 {
626 return false;
627 }
628
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000629 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400630 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000631
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300632 ASSERT(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500633 symbolTable.push(); // COMMON_BUILTINS
634 symbolTable.push(); // ESSL1_BUILTINS
635 symbolTable.push(); // ESSL3_BUILTINS
636 symbolTable.push(); // ESSL3_1_BUILTINS
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300637 symbolTable.push(); // GLSL_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000638
Jamie Madillacb4b812016-11-07 13:50:29 -0500639 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400640 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500641 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300642 symbolTable.setDefaultPrecision(EbtInt, EbpMedium);
Jamie Madillacb4b812016-11-07 13:50:29 -0500643 break;
644 case GL_VERTEX_SHADER:
Jamie Madillacb4b812016-11-07 13:50:29 -0500645 case GL_COMPUTE_SHADER:
Shaob5cc1192017-07-06 10:47:20 +0800646 case GL_GEOMETRY_SHADER_OES:
Olli Etuahocce89652017-06-19 16:04:09 +0300647 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
648 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500649 break;
650 default:
Shaob5cc1192017-07-06 10:47:20 +0800651 UNREACHABLE();
Nicolas Capens49a88872013-06-20 09:54:03 -0400652 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200653 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400654 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200655 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
656 initSamplerDefaultPrecision(EbtSampler2D);
657 initSamplerDefaultPrecision(EbtSamplerCube);
658 // SamplerExternalOES is specified in the extension to have default precision.
659 initSamplerDefaultPrecision(EbtSamplerExternalOES);
Andrei Volykhina5527072017-03-22 16:46:30 +0300660 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
661 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200662 // It isn't specified whether Sampler2DRect has default precision.
663 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400664
Olli Etuahocce89652017-06-19 16:04:09 +0300665 symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh);
jchen104cdac9e2017-05-08 11:01:20 +0800666
Jamie Madill1b452142013-07-12 14:51:11 -0400667 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400668
669 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
670
671 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000672}
673
Olli Etuaho183d7e22015-11-20 15:59:09 +0200674void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
675{
676 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
Olli Etuahocce89652017-06-19 16:04:09 +0300677 symbolTable.setDefaultPrecision(samplerType, EbpLow);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200678}
679
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400680void TCompiler::setResourceString()
681{
682 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400683
684 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400685 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500686 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
687 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
688 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
689 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
690 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
691 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
692 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
693 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
694 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
695 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
696 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
697 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
698 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
699 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
700 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
701 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
702 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
703 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
704 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
705 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
706 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
707 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
708 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Martin Radev318f9aa2017-05-17 17:47:28 +0300709 << ":OVR_multiview:" << compileResources.OVR_multiview
Andrei Volykhina5527072017-03-22 16:46:30 +0300710 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Shaob5cc1192017-07-06 10:47:20 +0800711 << ":OES_geometry_shader:" << compileResources.OES_geometry_shader
Jamie Madillacb4b812016-11-07 13:50:29 -0500712 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
713 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
714 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
715 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
716 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Martin Radev318f9aa2017-05-17 17:47:28 +0300717 << ":MaxViewsOVR:" << compileResources.MaxViewsOVR
Jamie Madillacb4b812016-11-07 13:50:29 -0500718 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
719 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
720 << ":MaxImageUnits:" << compileResources.MaxImageUnits
721 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
722 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
723 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
724 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
725 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
726 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
727 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
728 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
729 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
730 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
731 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
732 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
733 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
734 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
735 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
736 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
737 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
738 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
739 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
740 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
741 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
742 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
Shaob5cc1192017-07-06 10:47:20 +0800743 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize
Shaob5cc1192017-07-06 10:47:20 +0800744 << ":MaxGeometryUniformComponents:" << compileResources.MaxGeometryUniformComponents
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800745 << ":MaxGeometryUniformBlocks:" << compileResources.MaxGeometryUniformBlocks
746 << ":MaxGeometryInputComponents:" << compileResources.MaxGeometryInputComponents
747 << ":MaxGeometryOutputComponents:" << compileResources.MaxGeometryOutputComponents
748 << ":MaxGeometryOutputVertices:" << compileResources.MaxGeometryOutputVertices
749 << ":MaxGeometryTotalOutputComponents:" << compileResources.MaxGeometryTotalOutputComponents
750 << ":MaxGeometryTextureImageUnits:" << compileResources.MaxGeometryTextureImageUnits
751 << ":MaxGeometryAtomicCounterBuffers:" << compileResources.MaxGeometryAtomicCounterBuffers
752 << ":MaxGeometryAtomicCounters:" << compileResources.MaxGeometryAtomicCounters
753 << ":MaxGeometryShaderStorageBlocks:" << compileResources.MaxGeometryShaderStorageBlocks
754 << ":MaxGeometryShaderInvocations:" << compileResources.MaxGeometryShaderInvocations
755 << ":MaxGeometryImageUniforms:" << compileResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -0400756 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400757
758 builtInResourcesString = strstream.str();
759}
760
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800761void TCompiler::collectInterfaceBlocks()
762{
763 ASSERT(interfaceBlocks.empty());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800764 interfaceBlocks.reserve(uniformBlocks.size() + shaderStorageBlocks.size() + inBlocks.size());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800765 interfaceBlocks.insert(interfaceBlocks.end(), uniformBlocks.begin(), uniformBlocks.end());
766 interfaceBlocks.insert(interfaceBlocks.end(), shaderStorageBlocks.begin(),
767 shaderStorageBlocks.end());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800768 interfaceBlocks.insert(interfaceBlocks.end(), inBlocks.begin(), inBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800769}
770
alokp@chromium.org07620a52010-09-23 17:53:56 +0000771void TCompiler::clearResults()
772{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000773 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000774 infoSink.info.erase();
775 infoSink.obj.erase();
776 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000777 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000778
Jamie Madilled27c722014-07-02 15:31:23 -0400779 attributes.clear();
780 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000781 uniforms.clear();
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800782 inputVaryings.clear();
783 outputVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400784 interfaceBlocks.clear();
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800785 uniformBlocks.clear();
786 shaderStorageBlocks.clear();
Jiawei Shaod8105a02017-08-08 09:54:36 +0800787 inBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700788 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300789 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000790
Olli Etuaho09b04a22016-12-15 13:30:26 +0000791 mNumViews = -1;
792
Shaob5cc1192017-07-06 10:47:20 +0800793 mGeometryShaderInputPrimitiveType = EptUndefined;
794 mGeometryShaderOutputPrimitiveType = EptUndefined;
795 mGeometryShaderInvocations = 0;
796 mGeometryShaderMaxVertices = -1;
797
Olli Etuahodfa75e82017-01-23 09:43:06 -0800798 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000799
800 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200801
Yunchao Hed7297bf2017-04-19 15:27:10 +0800802 mSourcePath = nullptr;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000803}
804
Corentin Wallez71d147f2015-02-11 11:15:24 -0800805bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000806{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800807 mCallDag.clear();
808
Olli Etuaho77ba4082016-12-16 12:01:18 +0000809 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800810 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500811 case CallDAG::INITDAG_SUCCESS:
812 return true;
813 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500814 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000815 // Error message has already been written out.
816 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500817 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800818 }
819
820 UNREACHABLE();
821 return true;
822}
823
824bool TCompiler::checkCallDepth()
825{
826 std::vector<int> depths(mCallDag.size());
827
828 for (size_t i = 0; i < mCallDag.size(); i++)
829 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500830 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800831 auto &record = mCallDag.getRecordFromIndex(i);
832
833 for (auto &calleeIndex : record.callees)
834 {
835 depth = std::max(depth, depths[calleeIndex] + 1);
836 }
837
838 depths[i] = depth;
839
840 if (depth >= maxCallStackDepth)
841 {
842 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000843 std::stringstream errorStream;
844 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
845 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800846
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700847 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500848 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800849
850 while (currentFunction != -1)
851 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000852 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800853
854 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500855 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800856 {
857 if (depths[calleeIndex] == currentDepth - 1)
858 {
859 currentDepth--;
860 nextFunction = calleeIndex;
861 }
862 }
863
864 currentFunction = nextFunction;
865 }
866
Olli Etuaho77ba4082016-12-16 12:01:18 +0000867 std::string errorStr = errorStream.str();
868 mDiagnostics.globalError(errorStr.c_str());
869
Corentin Wallez71d147f2015-02-11 11:15:24 -0800870 return false;
871 }
872 }
873
874 return true;
875}
876
877bool TCompiler::tagUsedFunctions()
878{
879 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700880 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800881 {
Olli Etuahoec9232b2017-03-27 17:01:37 +0300882 if (mCallDag.getRecordFromIndex(i).name == "main")
Corentin Wallez71d147f2015-02-11 11:15:24 -0800883 {
884 internalTagUsedFunction(i);
885 return true;
886 }
887 }
888
Olli Etuaho77ba4082016-12-16 12:01:18 +0000889 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800890 return false;
891}
892
893void TCompiler::internalTagUsedFunction(size_t index)
894{
895 if (functionMetadata[index].used)
896 {
897 return;
898 }
899
900 functionMetadata[index].used = true;
901
902 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
903 {
904 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000905 }
906}
907
Corentin Walleza094a8a2015-04-07 11:53:06 -0700908// A predicate for the stl that returns if a top-level node is unused
909class TCompiler::UnusedPredicate
910{
911 public:
912 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500913 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700914 {
915 }
916
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500917 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700918 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000919 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
920 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700921
Olli Etuaho336b1472016-10-05 16:37:55 +0100922 const TFunctionSymbolInfo *functionInfo = nullptr;
923
Olli Etuaho16c745a2017-01-16 17:02:27 +0000924 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100925 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000926 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100927 }
Olli Etuaho16c745a2017-01-16 17:02:27 +0000928 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +0100929 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000930 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100931 }
932 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700933 {
934 return false;
935 }
936
Olli Etuaho336b1472016-10-05 16:37:55 +0100937 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700938 if (callDagIndex == CallDAG::InvalidIndex)
939 {
940 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +0000941 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700942 return true;
943 }
944
945 ASSERT(callDagIndex < mMetadatas->size());
946 return !(*mMetadatas)[callDagIndex].used;
947 }
948
949 private:
950 const CallDAG *mCallDag;
951 const std::vector<FunctionMetadata> *mMetadatas;
952};
953
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100954bool TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700955{
Corentin Walleza094a8a2015-04-07 11:53:06 -0700956 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100957 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700958
959 if (!sequence->empty())
960 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500961 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
962 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -0700963 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700964
965 return true;
966}
967
Olli Etuahob4279202017-05-18 15:08:24 +0300968bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400969{
Olli Etuahocccf2b02017-07-05 14:50:54 +0300970 if (!IsASTDepthBelowLimit(root, maxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -0400971 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000972 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -0400973 return false;
974 }
975
Olli Etuahob4279202017-05-18 15:08:24 +0300976 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200977 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000978 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200979 return false;
980 }
981
Jamie Madilleb1a0102013-07-08 13:31:38 -0400982 return true;
983}
984
Corentin Wallez1df16022016-10-27 08:16:56 -0400985bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
986{
987 return (compileOptions & SH_VARIABLES) != 0;
988}
989
990bool TCompiler::wereVariablesCollected() const
991{
992 return variablesCollected;
993}
994
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300995void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800996{
Zhenyao Mo72111912016-07-20 17:45:56 -0700997 InitVariableList list;
998 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
999 var.name = "gl_Position";
1000 list.push_back(var);
Olli Etuahodaaff1c2017-07-05 18:03:26 +03001001 InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001002}
1003
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001004void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001005{
1006 sh::InterfaceBlockList list;
1007
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001008 for (auto block : uniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001009 {
1010 if (!block.staticUse &&
1011 (block.layout == sh::BLOCKLAYOUT_STANDARD || block.layout == sh::BLOCKLAYOUT_SHARED))
1012 {
1013 list.push_back(block);
1014 }
1015 }
1016
Zhenyao Mod7490962016-11-09 15:49:51 -08001017 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001018}
1019
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001020void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001021{
Zhenyao Mo72111912016-07-20 17:45:56 -07001022 InitVariableList list;
Jiawei Shaod27f5c82017-08-23 09:38:08 +08001023 if (shaderType == GL_VERTEX_SHADER || shaderType == GL_GEOMETRY_SHADER_OES)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001024 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001025 for (auto var : outputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001026 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001027 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001028 if (var.name == "gl_Position")
1029 {
1030 ASSERT(!mGLPositionInitialized);
1031 mGLPositionInitialized = true;
1032 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001033 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001034 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001035 else
1036 {
1037 ASSERT(shaderType == GL_FRAGMENT_SHADER);
1038 for (auto var : outputVariables)
1039 {
1040 list.push_back(var);
1041 }
1042 }
Olli Etuahodaaff1c2017-07-05 18:03:26 +03001043 InitializeVariables(root, list, symbolTable, shaderVersion, extensionBehavior);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001044}
1045
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001046const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001047{
1048 return extensionBehavior;
1049}
zmo@google.com32e97312011-08-24 01:03:11 +00001050
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001051const char *TCompiler::getSourcePath() const
1052{
1053 return mSourcePath;
1054}
1055
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001056const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001057{
1058 return compileResources;
1059}
1060
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001061const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001062{
1063 return arrayBoundsClamper;
1064}
1065
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001066ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1067{
1068 return clampingStrategy;
1069}
1070
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001071const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001072{
1073 return builtInFunctionEmulator;
1074}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001075
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001076void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001077{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001078 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1079 {
1080 TInfoSinkBase &sink = infoSink.obj;
1081 if (mPragma.stdgl.invariantAll)
1082 sink << "#pragma STDGL invariant(all)\n";
1083 }
1084}
1085
1086bool TCompiler::isVaryingDefined(const char *varyingName)
1087{
1088 ASSERT(variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001089 for (size_t ii = 0; ii < inputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001090 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001091 if (inputVaryings[ii].name == varyingName)
1092 {
1093 return true;
1094 }
1095 }
1096 for (size_t ii = 0; ii < outputVaryings.size(); ++ii)
1097 {
1098 if (outputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001099 {
1100 return true;
1101 }
1102 }
1103
1104 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001105}
Jamie Madillacb4b812016-11-07 13:50:29 -05001106
1107} // namespace sh