blob: f6b170cc68bd6e71a0733940944387c6e53ca089 [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"
Corentin Wallez71d147f2015-02-11 11:15:24 -080014#include "compiler/translator/CallDAG.h"
Olli Etuaho0690e1a2017-12-21 20:51:38 +020015#include "compiler/translator/ClampFragDepth.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"
Olli Etuaho765924f2018-01-04 12:48:36 +020022#include "compiler/translator/FoldExpressions.h"
Geoff Lang17732822013-08-29 13:46:49 -040023#include "compiler/translator/Initialize.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080024#include "compiler/translator/InitializeVariables.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 Etuaho0b0dcbc2018-03-14 11:33:22 +020028#include "compiler/translator/PruneEmptyCases.h"
Olli Etuaho87c35882017-10-19 15:19:38 +030029#include "compiler/translator/PruneNoOps.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070030#include "compiler/translator/RegenerateStructNames.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030031#include "compiler/translator/RemoveArrayLengthMethod.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080032#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030033#include "compiler/translator/RemovePow.h"
Olli Etuaho3d70ca92017-11-10 16:53:26 +020034#include "compiler/translator/RemoveUnreferencedVariables.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070035#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070036#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030037#include "compiler/translator/SeparateDeclarations.h"
38#include "compiler/translator/SimplifyLoopConditions.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030039#include "compiler/translator/SplitSequenceOperator.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070040#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080041#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040042#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020043#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040044#include "compiler/translator/ValidateOutputs.h"
Jiawei Shao4cc89e22017-08-31 14:25:54 +080045#include "compiler/translator/ValidateVaryingLocations.h"
Geoff Lang17732822013-08-29 13:46:49 -040046#include "compiler/translator/VariablePacker.h"
Olli Etuaho661fc482017-10-16 12:17:05 +030047#include "compiler/translator/VectorizeVectorScalarArithmetic.h"
Olli Etuahoc26214d2018-03-16 10:43:11 +020048#include "compiler/translator/tree_util/IntermNodePatternMatcher.h"
Olli Etuahob5601eb2017-11-15 18:08:04 +020049#include "compiler/translator/util.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000050#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070051
Jamie Madillacb4b812016-11-07 13:50:29 -050052namespace sh
53{
54
Corentin Wallez28b65282016-06-16 07:24:50 -070055namespace
56{
57
58#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
59void DumpFuzzerCase(char const *const *shaderStrings,
60 size_t numStrings,
61 uint32_t type,
62 uint32_t spec,
63 uint32_t output,
64 uint64_t options)
65{
66 static int fileIndex = 0;
67
68 std::ostringstream o;
69 o << "corpus/" << fileIndex++ << ".sample";
70 std::string s = o.str();
71
72 // Must match the input format of the fuzzer
73 FILE *f = fopen(s.c_str(), "w");
74 fwrite(&type, sizeof(type), 1, f);
75 fwrite(&spec, sizeof(spec), 1, f);
76 fwrite(&output, sizeof(output), 1, f);
77 fwrite(&options, sizeof(options), 1, f);
78
79 char zero[128 - 20] = {0};
80 fwrite(&zero, 128 - 20, 1, f);
81
82 for (size_t i = 0; i < numStrings; i++)
83 {
84 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
85 }
86 fwrite(&zero, 1, 1, f);
87
88 fclose(f);
89}
90#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
91} // anonymous namespace
92
Jamie Madill5508f392014-02-20 13:31:36 -050093bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000094{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080095 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000096}
97
Qingqing Dengad0d0792015-04-08 14:25:06 -070098bool IsGLSL130OrNewer(ShShaderOutput output)
99{
Jamie Madillacb4b812016-11-07 13:50:29 -0500100 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
101 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
102 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
103 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);
Qingqing Dengad0d0792015-04-08 14:25:06 -0700105}
106
Qiankun Miao705a9192016-08-29 10:05:27 +0800107bool IsGLSL420OrNewer(ShShaderOutput output)
108{
Jamie Madillacb4b812016-11-07 13:50:29 -0500109 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
110 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800111}
112
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800113bool IsGLSL410OrOlder(ShShaderOutput output)
114{
115 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
116 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
117 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
118}
119
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000120bool RemoveInvariant(sh::GLenum shaderType,
121 int shaderVersion,
122 ShShaderOutput outputType,
123 ShCompileOptions compileOptions)
124{
125 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
126 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
127 return true;
128
129 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800130 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000131 return true;
132
133 return false;
134}
135
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700136size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500137{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800138 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500139 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700140 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500141 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500142 case SH_WEBGL_SPEC:
143 return 256;
144 default:
145 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500146 }
147}
148
Shao77891c02017-06-23 16:30:17 +0800149int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
150{
151 switch (shaderType)
152 {
153 case GL_VERTEX_SHADER:
154 return resources.MaxVertexUniformVectors;
155 case GL_FRAGMENT_SHADER:
156 return resources.MaxFragmentUniformVectors;
Shaob5cc1192017-07-06 10:47:20 +0800157
158 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
Shao77891c02017-06-23 16:30:17 +0800159 case GL_COMPUTE_SHADER:
Shao77891c02017-06-23 16:30:17 +0800160 return resources.MaxComputeUniformComponents / 4;
Jiawei Shaobd924af2017-11-16 15:28:04 +0800161 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +0800162 return resources.MaxGeometryUniformComponents / 4;
Shao77891c02017-06-23 16:30:17 +0800163 default:
Shaob5cc1192017-07-06 10:47:20 +0800164 UNREACHABLE();
Shao77891c02017-06-23 16:30:17 +0800165 return -1;
166 }
167}
168
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500169namespace
170{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700171
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800172class TScopedPoolAllocator
173{
174 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500175 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800176 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400177 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000178 SetGlobalPoolAllocator(mAllocator);
179 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800180 ~TScopedPoolAllocator()
181 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800182 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400183 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000184 }
185
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800186 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500187 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400188};
189
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800190class TScopedSymbolTableLevel
191{
192 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500193 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800194 {
Olli Etuaho437664b2018-02-28 15:38:14 +0200195 ASSERT(mTable->isEmpty());
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400196 mTable->push();
197 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800198 ~TScopedSymbolTableLevel()
199 {
Olli Etuaho437664b2018-02-28 15:38:14 +0200200 while (!mTable->isEmpty())
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400201 mTable->pop();
202 }
203
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800204 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500205 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000206};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700207
208int MapSpecToShaderVersion(ShShaderSpec spec)
209{
210 switch (spec)
211 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500212 case SH_GLES2_SPEC:
213 case SH_WEBGL_SPEC:
214 return 100;
215 case SH_GLES3_SPEC:
216 case SH_WEBGL2_SPEC:
217 return 300;
218 case SH_GLES3_1_SPEC:
219 case SH_WEBGL3_SPEC:
220 return 310;
221 default:
222 UNREACHABLE();
223 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700224 }
225}
226
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000227} // namespace
228
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800229TShHandleBase::TShHandleBase()
230{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000231 allocator.push();
232 SetGlobalPoolAllocator(&allocator);
233}
234
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800235TShHandleBase::~TShHandleBase()
236{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800237 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000238 allocator.popAll();
239}
240
Jamie Madill183bde52014-07-02 15:31:19 -0400241TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700242 : variablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300243 mGLPositionInitialized(false),
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700244 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000245 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400246 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400247 maxUniformVectors(0),
248 maxExpressionComplexity(0),
249 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200250 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000251 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000252 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200253 builtInFunctionEmulator(),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000254 mDiagnostics(infoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800255 mSourcePath(nullptr),
Shaob5cc1192017-07-06 10:47:20 +0800256 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500257 mComputeShaderLocalSize(1),
Shaob5cc1192017-07-06 10:47:20 +0800258 mGeometryShaderMaxVertices(-1),
259 mGeometryShaderInvocations(0),
260 mGeometryShaderInputPrimitiveType(EptUndefined),
261 mGeometryShaderOutputPrimitiveType(EptUndefined)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000262{
263}
264
265TCompiler::~TCompiler()
266{
267}
268
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800269bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300270{
271 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
272 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
273 // of ESSL 1.00 as in Appendix A of the spec).
274 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
275 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
276}
277
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500278bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000279{
Shao77891c02017-06-23 16:30:17 +0800280 shaderVersion = 100;
281
282 maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources);
283
Jamie Madilleb1a0102013-07-08 13:31:38 -0400284 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200285 maxCallStackDepth = resources.MaxCallStackDepth;
286 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400287
288 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000289
alokp@chromium.org07620a52010-09-23 17:53:56 +0000290 // Generate built-in symbol table.
291 if (!InitBuiltInSymbolTable(resources))
292 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000293 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000294 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000295
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000296 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
297 clampingStrategy = resources.ArrayIndexClampingStrategy;
298
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000299 hashFunction = resources.HashFunction;
300
alokp@chromium.org07620a52010-09-23 17:53:56 +0000301 return true;
302}
303
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100304TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
305 size_t numStrings,
306 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000307{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200308 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
309}
310
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100311TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
312 size_t numStrings,
313 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200314{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000315 clearResults();
316
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200317 ASSERT(numStrings > 0);
318 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000319
David Yen0fbd1282015-02-02 14:46:09 -0800320 // Reset the extension behavior for each compilation unit.
321 ResetExtensionBehavior(extensionBehavior);
322
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000323 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000324 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000325 if (compileOptions & SH_SOURCE_PATH)
326 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200327 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000328 ++firstSource;
329 }
330
Olli Etuahof119a262016-08-19 15:54:22 +0300331 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000332 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200333
Olli Etuahoa6996682015-10-12 14:32:30 +0300334 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000335
336 // We preserve symbols at the built-in level from compile-to-compile.
337 // Start pushing the user-defined symbols at global level.
Olli Etuahoe5fe7aa2018-01-29 12:06:11 +0200338 TScopedSymbolTableLevel globalLevel(&symbolTable);
339 ASSERT(symbolTable.atGlobalLevel());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000340
341 // Parse shader.
Olli Etuahod10cf692017-11-02 11:06:14 +0200342 if (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
343 &parseContext) != 0)
344 {
345 return nullptr;
346 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000347
Olli Etuahod10cf692017-11-02 11:06:14 +0200348 if (parseContext.getTreeRoot() == nullptr)
349 {
350 return nullptr;
351 }
352
353 setASTMetadata(parseContext);
354
Jiawei Shaoe41df652018-02-09 14:31:39 +0800355 if (!checkShaderVersion(&parseContext))
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700356 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200357 return nullptr;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700358 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000359
Olli Etuahod10cf692017-11-02 11:06:14 +0200360 TIntermBlock *root = parseContext.getTreeRoot();
361 if (!checkAndSimplifyAST(root, parseContext, compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800362 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200363 return nullptr;
364 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700365
Olli Etuahod10cf692017-11-02 11:06:14 +0200366 return root;
367}
Martin Radev802abe02016-08-04 17:48:32 +0300368
Jiawei Shaoe41df652018-02-09 14:31:39 +0800369bool TCompiler::checkShaderVersion(TParseContext *parseContext)
370{
371 if (MapSpecToShaderVersion(shaderSpec) < shaderVersion)
372 {
373 mDiagnostics.globalError("unsupported shader version");
374 return false;
375 }
376
377 ASSERT(parseContext);
378 switch (shaderType)
379 {
380 case GL_COMPUTE_SHADER:
381 if (shaderVersion < 310)
382 {
383 mDiagnostics.globalError("Compute shader is not supported in this shader version.");
384 return false;
385 }
386 break;
387
388 case GL_GEOMETRY_SHADER_EXT:
389 if (shaderVersion < 310)
390 {
391 mDiagnostics.globalError(
392 "Geometry shader is not supported in this shader version.");
393 return false;
394 }
395 else
396 {
397 ASSERT(shaderVersion == 310);
398 if (!parseContext->checkCanUseExtension(sh::TSourceLoc(),
399 TExtension::EXT_geometry_shader))
400 {
401 return false;
402 }
403 }
404 break;
405
406 default:
407 break;
408 }
409
410 return true;
411}
412
Olli Etuahod10cf692017-11-02 11:06:14 +0200413void TCompiler::setASTMetadata(const TParseContext &parseContext)
414{
415 shaderVersion = parseContext.getShaderVersion();
Olli Etuaho09b04a22016-12-15 13:30:26 +0000416
Olli Etuahod10cf692017-11-02 11:06:14 +0200417 mPragma = parseContext.pragma();
418 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000419
Olli Etuahod10cf692017-11-02 11:06:14 +0200420 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
421 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
Olli Etuahoa6996682015-10-12 14:32:30 +0300422
Olli Etuahod10cf692017-11-02 11:06:14 +0200423 mNumViews = parseContext.getNumViews();
424
425 // Highp might have been auto-enabled based on shader version
426 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
427
Jiawei Shaobd924af2017-11-16 15:28:04 +0800428 if (shaderType == GL_GEOMETRY_SHADER_EXT)
Olli Etuahod10cf692017-11-02 11:06:14 +0200429 {
430 mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType();
431 mGeometryShaderOutputPrimitiveType = parseContext.getGeometryShaderOutputPrimitiveType();
432 mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices();
433 mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations();
434 }
435}
436
437bool TCompiler::checkAndSimplifyAST(TIntermBlock *root,
438 const TParseContext &parseContext,
439 ShCompileOptions compileOptions)
440{
441 // Disallow expressions deemed too complex.
442 if ((compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY) && !limitExpressionComplexity(root))
443 {
444 return false;
445 }
446
Olli Etuaho765924f2018-01-04 12:48:36 +0200447 if (shouldRunLoopAndIndexingValidation(compileOptions) &&
Olli Etuaho68981eb2018-01-23 17:46:12 +0200448 !ValidateLimitations(root, shaderType, &symbolTable, &mDiagnostics))
Olli Etuaho765924f2018-01-04 12:48:36 +0200449 {
450 return false;
451 }
452
453 // Fold expressions that could not be folded before validation that was done as a part of
454 // parsing.
455 FoldExpressions(root, &mDiagnostics);
456 // Folding should only be able to generate warnings.
457 ASSERT(mDiagnostics.numErrors() == 0);
458
Olli Etuahod10cf692017-11-02 11:06:14 +0200459 // We prune no-ops to work around driver bugs and to keep AST processing and output simple.
460 // The following kinds of no-ops are pruned:
461 // 1. Empty declarations "int;".
462 // 2. Literal statements: "1.0;". The ESSL output doesn't define a default precision
463 // for float, so float literal statements would end up with no precision which is
464 // invalid ESSL.
465 // After this empty declarations are not allowed in the AST.
Olli Etuahob38dfde2018-01-04 15:49:03 +0200466 PruneNoOps(root, &symbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200467
Olli Etuahod10cf692017-11-02 11:06:14 +0200468 // Create the function DAG and check there is no recursion
469 if (!initCallDag(root))
470 {
471 return false;
472 }
473
474 if ((compileOptions & SH_LIMIT_CALL_STACK_DEPTH) && !checkCallDepth())
475 {
476 return false;
477 }
478
479 // Checks which functions are used and if "main" exists
480 functionMetadata.clear();
481 functionMetadata.resize(mCallDag.size());
482 if (!tagUsedFunctions())
483 {
484 return false;
485 }
486
487 if (!(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
488 {
489 pruneUnusedFunctions(root);
490 }
491
492 if (shaderVersion >= 310 && !ValidateVaryingLocations(root, &mDiagnostics, shaderType))
493 {
494 return false;
495 }
496
497 if (shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER &&
498 !ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers,
499 &mDiagnostics))
500 {
501 return false;
502 }
503
Olli Etuahod10cf692017-11-02 11:06:14 +0200504 // Fail compilation if precision emulation not supported.
505 if (getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision &&
506 !EmulatePrecision::SupportedInLanguage(outputType))
507 {
508 mDiagnostics.globalError("Precision emulation not supported for this output type.");
509 return false;
510 }
511
Olli Etuahod10cf692017-11-02 11:06:14 +0200512 // Clamping uniform array bounds needs to happen after validateLimitations pass.
513 if (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)
514 {
515 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
516 }
517
518 if ((compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
519 parseContext.isExtensionEnabled(TExtension::OVR_multiview) &&
520 getShaderType() != GL_COMPUTE_SHADER)
521 {
522 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions,
523 outputType, &symbolTable);
524 }
525
526 // This pass might emit short circuits so keep it before the short circuit unfolding
527 if (compileOptions & SH_REWRITE_DO_WHILE_LOOPS)
528 RewriteDoWhile(root, &symbolTable);
529
530 if (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION)
Olli Etuaho1776fd02018-01-31 11:46:52 +0200531 AddAndTrueToLoopCondition(root);
Olli Etuahod10cf692017-11-02 11:06:14 +0200532
533 if (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)
534 {
Olli Etuaho1776fd02018-01-31 11:46:52 +0200535 UnfoldShortCircuitAST(root);
Olli Etuahod10cf692017-11-02 11:06:14 +0200536 }
537
538 if (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT)
539 {
540 RemovePow(root);
541 }
542
Olli Etuahod10cf692017-11-02 11:06:14 +0200543 if (compileOptions & SH_REGENERATE_STRUCT_NAMES)
544 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100545 RegenerateStructNames gen(&symbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200546 root->traverse(&gen);
547 }
548
549 if (shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
550 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
551 IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers))
552 {
553 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables,
554 &symbolTable, shaderVersion);
555 }
556
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200557 int simplifyScalarized = (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
558 ? IntermNodePatternMatcher::kScalarizedVecOrMatConstructor
559 : 0;
560
Olli Etuahod10cf692017-11-02 11:06:14 +0200561 // Split multi declarations and remove calls to array length().
562 // Note that SimplifyLoopConditions needs to be run before any other AST transformations
563 // that may need to generate new statements from loop conditions or loop expressions.
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200564 SimplifyLoopConditions(root,
565 IntermNodePatternMatcher::kMultiDeclaration |
566 IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200567 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200568
569 // Note that separate declarations need to be run before other AST transformations that
570 // generate new statements from expressions.
571 SeparateDeclarations(root);
572
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200573 SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200574 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200575
576 RemoveArrayLengthMethod(root);
577
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200578 RemoveUnreferencedVariables(root, &symbolTable);
579
Olli Etuaho41217992018-03-15 11:15:33 +0200580 // In case the last case inside a switch statement is a certain type of no-op, GLSL compilers in
581 // drivers may not accept it. In this case we clean up the dead code from the end of switch
582 // statements. This is also required because PruneNoOps or RemoveUnreferencedVariables may have
583 // left switch statements that only contained an empty declaration inside the final case in an
584 // invalid state. Relies on that PruneNoOps and RemoveUnreferencedVariables have already been
585 // run.
Olli Etuaho0b0dcbc2018-03-14 11:33:22 +0200586 PruneEmptyCases(root);
587
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200588 // Built-in function emulation needs to happen after validateLimitations pass.
589 // TODO(jmadill): Remove global pool allocator.
590 GetGlobalPoolAllocator()->lock();
591 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
592 GetGlobalPoolAllocator()->unlock();
593 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
594
595 if (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
596 {
597 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh, &symbolTable);
598 }
599
600 if (shouldCollectVariables(compileOptions))
601 {
602 ASSERT(!variablesCollected);
603 CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings,
604 &outputVaryings, &uniformBlocks, &shaderStorageBlocks, &inBlocks,
605 hashFunction, &symbolTable, shaderVersion, shaderType, extensionBehavior);
606 collectInterfaceBlocks();
607 variablesCollected = true;
608 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
609 {
610 useAllMembersInUnusedStandardAndSharedBlocks(root);
611 }
612 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
613 {
614 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
615 // Appendix A, section 7, the shader does not use too many uniforms.
616 if (!CheckVariablesInPackingLimits(maxUniformVectors, uniforms))
617 {
618 mDiagnostics.globalError("too many uniforms");
619 return false;
620 }
621 }
622 if (compileOptions & SH_INIT_OUTPUT_VARIABLES)
623 {
624 initializeOutputVariables(root);
625 }
626 }
627
628 // Removing invariant declarations must be done after collecting variables.
629 // Otherwise, built-in invariant declarations don't apply.
630 if (RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
631 {
632 RemoveInvariantDeclaration(root);
633 }
634
635 // gl_Position is always written in compatibility output mode.
636 // It may have been already initialized among other output variables, in that case we don't
637 // need to initialize it twice.
638 if (shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
639 ((compileOptions & SH_INIT_GL_POSITION) || (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
640 {
641 initializeGLPosition(root);
642 mGLPositionInitialized = true;
643 }
644
Olli Etuahob5601eb2017-11-15 18:08:04 +0200645 // DeferGlobalInitializers needs to be run before other AST transformations that generate new
646 // statements from expressions. But it's fine to run DeferGlobalInitializers after the above
647 // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST
648 // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00.
649 bool initializeLocalsAndGlobals =
650 (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType());
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300651 bool canUseLoopsToInitialize = !(compileOptions & SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES);
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200652 bool highPrecisionSupported =
653 shaderType != GL_FRAGMENT_SHADER || compileResources.FragmentPrecisionHigh;
654 DeferGlobalInitializers(root, initializeLocalsAndGlobals, canUseLoopsToInitialize,
655 highPrecisionSupported, &symbolTable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300656
Olli Etuahob5601eb2017-11-15 18:08:04 +0200657 if (initializeLocalsAndGlobals)
Olli Etuahod10cf692017-11-02 11:06:14 +0200658 {
659 // Initialize uninitialized local variables.
660 // In some cases initializing can generate extra statements in the parent block, such as
661 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
662 // we need to first simplify loop conditions. We've already separated declarations
663 // earlier, which is also required. If we don't follow the Appendix A limitations, loop
664 // init statements can declare arrays or nameless structs and have multiple
665 // declarations.
666
667 if (!shouldRunLoopAndIndexingValidation(compileOptions))
668 {
669 SimplifyLoopConditions(root,
670 IntermNodePatternMatcher::kArrayDeclaration |
671 IntermNodePatternMatcher::kNamelessStructDeclaration,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200672 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200673 }
Olli Etuaho661fc482017-10-16 12:17:05 +0300674
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300675 InitializeUninitializedLocals(root, getShaderVersion(), canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200676 highPrecisionSupported, &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200677 }
678
679 if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_CLAMP_POINT_SIZE))
680 {
681 ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable());
682 }
683
Olli Etuaho0690e1a2017-12-21 20:51:38 +0200684 if (getShaderType() == GL_FRAGMENT_SHADER && (compileOptions & SH_CLAMP_FRAG_DEPTH))
685 {
686 ClampFragDepth(root, &getSymbolTable());
687 }
688
Olli Etuaho661fc482017-10-16 12:17:05 +0300689 if (compileOptions & SH_REWRITE_VECTOR_SCALAR_ARITHMETIC)
690 {
691 VectorizeVectorScalarArithmetic(root, &getSymbolTable());
692 }
693
Olli Etuahod10cf692017-11-02 11:06:14 +0200694 return true;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200695}
696
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800697bool TCompiler::compile(const char *const shaderStrings[],
698 size_t numStrings,
699 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200700{
Corentin Wallez28b65282016-06-16 07:24:50 -0700701#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
702 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
703#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
704
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200705 if (numStrings == 0)
706 return true;
707
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800708 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700709
710 // Apply key workarounds.
711 if (shouldFlattenPragmaStdglInvariantAll())
712 {
713 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
714 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
715 }
716
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200717 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100718 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200719
720 if (root)
721 {
722 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuahocccf2b02017-07-05 14:50:54 +0300723 OutputTree(root, infoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200724
725 if (compileOptions & SH_OBJECT_CODE)
Olli Etuaho89a69a02017-10-23 12:20:45 +0300726 {
727 PerformanceDiagnostics perfDiagnostics(&mDiagnostics);
728 translate(root, compileOptions, &perfDiagnostics);
729 }
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200730
731 // The IntermNode tree doesn't need to be deleted here, since the
732 // memory will be freed in a big chunk by the PoolAllocator.
733 return true;
734 }
735 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000736}
737
Nicolas Capens49a88872013-06-20 09:54:03 -0400738bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000739{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000740 if (resources.MaxDrawBuffers < 1)
741 {
742 return false;
743 }
744 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
745 {
746 return false;
747 }
748
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000749 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400750 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000751
Olli Etuaho29bda812018-01-26 17:37:36 +0200752 symbolTable.initializeBuiltIns(shaderType, shaderSpec, resources);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200753
Nicolas Capens49a88872013-06-20 09:54:03 -0400754 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000755}
756
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400757void TCompiler::setResourceString()
758{
759 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400760
761 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400762 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500763 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
764 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
765 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
766 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
767 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
768 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
769 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
770 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
771 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
772 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
773 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
774 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
775 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
776 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
777 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
778 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
779 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
780 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
781 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
782 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
783 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
784 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
785 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Martin Radev318f9aa2017-05-17 17:47:28 +0300786 << ":OVR_multiview:" << compileResources.OVR_multiview
Andrei Volykhina5527072017-03-22 16:46:30 +0300787 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Jiawei Shaobd924af2017-11-16 15:28:04 +0800788 << ":EXT_geometry_shader:" << compileResources.EXT_geometry_shader
Jamie Madillacb4b812016-11-07 13:50:29 -0500789 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
790 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
791 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
792 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
793 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Martin Radev318f9aa2017-05-17 17:47:28 +0300794 << ":MaxViewsOVR:" << compileResources.MaxViewsOVR
Jamie Madillacb4b812016-11-07 13:50:29 -0500795 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
796 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
Martin Radev84aa2dc2017-09-11 15:51:02 +0300797 << ":MinProgramTextureGatherOffset:" << compileResources.MinProgramTextureGatherOffset
798 << ":MaxProgramTextureGatherOffset:" << compileResources.MaxProgramTextureGatherOffset
Jamie Madillacb4b812016-11-07 13:50:29 -0500799 << ":MaxImageUnits:" << compileResources.MaxImageUnits
800 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
801 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
802 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
803 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
804 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
805 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
806 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
807 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
808 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
809 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
810 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
811 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
812 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
813 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
814 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
815 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
816 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
817 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
818 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
819 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
820 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
821 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
Shaob5cc1192017-07-06 10:47:20 +0800822 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize
Shaob5cc1192017-07-06 10:47:20 +0800823 << ":MaxGeometryUniformComponents:" << compileResources.MaxGeometryUniformComponents
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800824 << ":MaxGeometryUniformBlocks:" << compileResources.MaxGeometryUniformBlocks
825 << ":MaxGeometryInputComponents:" << compileResources.MaxGeometryInputComponents
826 << ":MaxGeometryOutputComponents:" << compileResources.MaxGeometryOutputComponents
827 << ":MaxGeometryOutputVertices:" << compileResources.MaxGeometryOutputVertices
828 << ":MaxGeometryTotalOutputComponents:" << compileResources.MaxGeometryTotalOutputComponents
829 << ":MaxGeometryTextureImageUnits:" << compileResources.MaxGeometryTextureImageUnits
830 << ":MaxGeometryAtomicCounterBuffers:" << compileResources.MaxGeometryAtomicCounterBuffers
831 << ":MaxGeometryAtomicCounters:" << compileResources.MaxGeometryAtomicCounters
832 << ":MaxGeometryShaderStorageBlocks:" << compileResources.MaxGeometryShaderStorageBlocks
833 << ":MaxGeometryShaderInvocations:" << compileResources.MaxGeometryShaderInvocations
834 << ":MaxGeometryImageUniforms:" << compileResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -0400835 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400836
837 builtInResourcesString = strstream.str();
838}
839
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800840void TCompiler::collectInterfaceBlocks()
841{
842 ASSERT(interfaceBlocks.empty());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800843 interfaceBlocks.reserve(uniformBlocks.size() + shaderStorageBlocks.size() + inBlocks.size());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800844 interfaceBlocks.insert(interfaceBlocks.end(), uniformBlocks.begin(), uniformBlocks.end());
845 interfaceBlocks.insert(interfaceBlocks.end(), shaderStorageBlocks.begin(),
846 shaderStorageBlocks.end());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800847 interfaceBlocks.insert(interfaceBlocks.end(), inBlocks.begin(), inBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800848}
849
alokp@chromium.org07620a52010-09-23 17:53:56 +0000850void TCompiler::clearResults()
851{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000852 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000853 infoSink.info.erase();
854 infoSink.obj.erase();
855 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000856 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000857
Jamie Madilled27c722014-07-02 15:31:23 -0400858 attributes.clear();
859 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000860 uniforms.clear();
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800861 inputVaryings.clear();
862 outputVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400863 interfaceBlocks.clear();
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800864 uniformBlocks.clear();
865 shaderStorageBlocks.clear();
Jiawei Shaod8105a02017-08-08 09:54:36 +0800866 inBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700867 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300868 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000869
Olli Etuaho09b04a22016-12-15 13:30:26 +0000870 mNumViews = -1;
871
Shaob5cc1192017-07-06 10:47:20 +0800872 mGeometryShaderInputPrimitiveType = EptUndefined;
873 mGeometryShaderOutputPrimitiveType = EptUndefined;
874 mGeometryShaderInvocations = 0;
875 mGeometryShaderMaxVertices = -1;
876
Olli Etuahodfa75e82017-01-23 09:43:06 -0800877 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000878
879 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200880
Yunchao Hed7297bf2017-04-19 15:27:10 +0800881 mSourcePath = nullptr;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200882
883 symbolTable.clearCompilationResults();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000884}
885
Corentin Wallez71d147f2015-02-11 11:15:24 -0800886bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000887{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800888 mCallDag.clear();
889
Olli Etuaho77ba4082016-12-16 12:01:18 +0000890 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800891 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500892 case CallDAG::INITDAG_SUCCESS:
893 return true;
894 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500895 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000896 // Error message has already been written out.
897 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500898 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800899 }
900
901 UNREACHABLE();
902 return true;
903}
904
905bool TCompiler::checkCallDepth()
906{
907 std::vector<int> depths(mCallDag.size());
908
909 for (size_t i = 0; i < mCallDag.size(); i++)
910 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500911 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800912 auto &record = mCallDag.getRecordFromIndex(i);
913
914 for (auto &calleeIndex : record.callees)
915 {
916 depth = std::max(depth, depths[calleeIndex] + 1);
917 }
918
919 depths[i] = depth;
920
921 if (depth >= maxCallStackDepth)
922 {
923 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000924 std::stringstream errorStream;
925 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
Olli Etuahoc33f1e82017-12-29 16:55:29 +0200926 << ") with the following call chain: "
927 << record.node->getFunction()->name();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800928
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700929 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500930 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800931
932 while (currentFunction != -1)
933 {
Olli Etuahoc33f1e82017-12-29 16:55:29 +0200934 errorStream
935 << " -> "
936 << mCallDag.getRecordFromIndex(currentFunction).node->getFunction()->name();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800937
938 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500939 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800940 {
941 if (depths[calleeIndex] == currentDepth - 1)
942 {
943 currentDepth--;
944 nextFunction = calleeIndex;
945 }
946 }
947
948 currentFunction = nextFunction;
949 }
950
Olli Etuaho77ba4082016-12-16 12:01:18 +0000951 std::string errorStr = errorStream.str();
952 mDiagnostics.globalError(errorStr.c_str());
953
Corentin Wallez71d147f2015-02-11 11:15:24 -0800954 return false;
955 }
956 }
957
958 return true;
959}
960
961bool TCompiler::tagUsedFunctions()
962{
963 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700964 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800965 {
Olli Etuahoc33f1e82017-12-29 16:55:29 +0200966 if (mCallDag.getRecordFromIndex(i).node->getFunction()->isMain())
Corentin Wallez71d147f2015-02-11 11:15:24 -0800967 {
968 internalTagUsedFunction(i);
969 return true;
970 }
971 }
972
Olli Etuaho77ba4082016-12-16 12:01:18 +0000973 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800974 return false;
975}
976
977void TCompiler::internalTagUsedFunction(size_t index)
978{
979 if (functionMetadata[index].used)
980 {
981 return;
982 }
983
984 functionMetadata[index].used = true;
985
986 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
987 {
988 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000989 }
990}
991
Corentin Walleza094a8a2015-04-07 11:53:06 -0700992// A predicate for the stl that returns if a top-level node is unused
993class TCompiler::UnusedPredicate
994{
995 public:
996 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500997 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700998 {
999 }
1000
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001001 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001002 {
Olli Etuaho16c745a2017-01-16 17:02:27 +00001003 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
1004 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -07001005
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001006 const TFunction *func = nullptr;
Olli Etuaho336b1472016-10-05 16:37:55 +01001007
Olli Etuaho16c745a2017-01-16 17:02:27 +00001008 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +01001009 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001010 func = asFunctionDefinition->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001011 }
Olli Etuaho16c745a2017-01-16 17:02:27 +00001012 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +01001013 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001014 func = asFunctionPrototype->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001015 }
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001016 if (func == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001017 {
1018 return false;
1019 }
1020
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001021 size_t callDagIndex = mCallDag->findIndex(func->uniqueId());
Corentin Walleza094a8a2015-04-07 11:53:06 -07001022 if (callDagIndex == CallDAG::InvalidIndex)
1023 {
1024 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +00001025 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001026 return true;
1027 }
1028
1029 ASSERT(callDagIndex < mMetadatas->size());
1030 return !(*mMetadatas)[callDagIndex].used;
1031 }
1032
1033 private:
1034 const CallDAG *mCallDag;
1035 const std::vector<FunctionMetadata> *mMetadatas;
1036};
1037
Olli Etuahod10cf692017-11-02 11:06:14 +02001038void TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001039{
Corentin Walleza094a8a2015-04-07 11:53:06 -07001040 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001041 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -07001042
1043 if (!sequence->empty())
1044 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001045 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
1046 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -07001047 }
Corentin Walleza094a8a2015-04-07 11:53:06 -07001048}
1049
Olli Etuahob4279202017-05-18 15:08:24 +03001050bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -04001051{
Olli Etuahocccf2b02017-07-05 14:50:54 +03001052 if (!IsASTDepthBelowLimit(root, maxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -04001053 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001054 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -04001055 return false;
1056 }
1057
Olli Etuahob4279202017-05-18 15:08:24 +03001058 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001059 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001060 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001061 return false;
1062 }
1063
Jamie Madilleb1a0102013-07-08 13:31:38 -04001064 return true;
1065}
1066
Corentin Wallez1df16022016-10-27 08:16:56 -04001067bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
1068{
1069 return (compileOptions & SH_VARIABLES) != 0;
1070}
1071
1072bool TCompiler::wereVariablesCollected() const
1073{
1074 return variablesCollected;
1075}
1076
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001077void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001078{
Zhenyao Mo72111912016-07-20 17:45:56 -07001079 InitVariableList list;
Olli Etuahoe7c28572017-10-23 16:29:33 +03001080 sh::ShaderVariable var(GL_FLOAT_VEC4);
Zhenyao Mo72111912016-07-20 17:45:56 -07001081 var.name = "gl_Position";
1082 list.push_back(var);
Olli Etuaho87cc90d2017-12-12 15:28:06 +02001083 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001084}
1085
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001086void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001087{
1088 sh::InterfaceBlockList list;
1089
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001090 for (auto block : uniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001091 {
1092 if (!block.staticUse &&
Qin Jiajiaca68d982017-09-18 16:41:56 +08001093 (block.layout == sh::BLOCKLAYOUT_STD140 || block.layout == sh::BLOCKLAYOUT_SHARED))
Qin Jiajia7835b522016-10-08 11:20:17 +08001094 {
1095 list.push_back(block);
1096 }
1097 }
1098
Zhenyao Mod7490962016-11-09 15:49:51 -08001099 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001100}
1101
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001102void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001103{
Zhenyao Mo72111912016-07-20 17:45:56 -07001104 InitVariableList list;
Jiawei Shaobd924af2017-11-16 15:28:04 +08001105 if (shaderType == GL_VERTEX_SHADER || shaderType == GL_GEOMETRY_SHADER_EXT)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001106 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001107 for (auto var : outputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001108 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001109 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001110 if (var.name == "gl_Position")
1111 {
1112 ASSERT(!mGLPositionInitialized);
1113 mGLPositionInitialized = true;
1114 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001115 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001116 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001117 else
1118 {
1119 ASSERT(shaderType == GL_FRAGMENT_SHADER);
1120 for (auto var : outputVariables)
1121 {
1122 list.push_back(var);
1123 }
1124 }
Olli Etuaho87cc90d2017-12-12 15:28:06 +02001125 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001126}
1127
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001128const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001129{
1130 return extensionBehavior;
1131}
zmo@google.com32e97312011-08-24 01:03:11 +00001132
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001133const char *TCompiler::getSourcePath() const
1134{
1135 return mSourcePath;
1136}
1137
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001138const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001139{
1140 return compileResources;
1141}
1142
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001143const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001144{
1145 return arrayBoundsClamper;
1146}
1147
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001148ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1149{
1150 return clampingStrategy;
1151}
1152
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001153const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001154{
1155 return builtInFunctionEmulator;
1156}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001157
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001158void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001159{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001160 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1161 {
1162 TInfoSinkBase &sink = infoSink.obj;
1163 if (mPragma.stdgl.invariantAll)
1164 sink << "#pragma STDGL invariant(all)\n";
1165 }
1166}
1167
1168bool TCompiler::isVaryingDefined(const char *varyingName)
1169{
1170 ASSERT(variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001171 for (size_t ii = 0; ii < inputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001172 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001173 if (inputVaryings[ii].name == varyingName)
1174 {
1175 return true;
1176 }
1177 }
1178 for (size_t ii = 0; ii < outputVaryings.size(); ++ii)
1179 {
1180 if (outputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001181 {
1182 return true;
1183 }
1184 }
1185
1186 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001187}
Jamie Madillacb4b812016-11-07 13:50:29 -05001188
1189} // namespace sh