blob: 0823ead033cbc9cf7b17ea3240b03f08a558e1b5 [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 Etuaho87c35882017-10-19 15:19:38 +030028#include "compiler/translator/PruneNoOps.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070029#include "compiler/translator/RegenerateStructNames.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030030#include "compiler/translator/RemoveArrayLengthMethod.h"
Olli Etuahoc1f14fb2017-10-28 19:17:23 +030031#include "compiler/translator/RemoveEmptySwitchStatements.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080032#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho923ecef2017-10-11 12:01:38 +030033#include "compiler/translator/RemoveNoOpCasesFromEndOfSwitchStatements.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030034#include "compiler/translator/RemovePow.h"
Olli Etuaho3d70ca92017-11-10 16:53:26 +020035#include "compiler/translator/RemoveUnreferencedVariables.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070036#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070037#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030038#include "compiler/translator/SeparateDeclarations.h"
39#include "compiler/translator/SimplifyLoopConditions.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030040#include "compiler/translator/SplitSequenceOperator.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070041#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080042#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040043#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020044#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040045#include "compiler/translator/ValidateOutputs.h"
Jiawei Shao4cc89e22017-08-31 14:25:54 +080046#include "compiler/translator/ValidateVaryingLocations.h"
Geoff Lang17732822013-08-29 13:46:49 -040047#include "compiler/translator/VariablePacker.h"
Olli Etuaho661fc482017-10-16 12:17:05 +030048#include "compiler/translator/VectorizeVectorScalarArithmetic.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;
Shaob5cc1192017-07-06 10:47:20 +0800161 case GL_GEOMETRY_SHADER_OES:
162 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 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400195 ASSERT(mTable->atBuiltInLevel());
196 mTable->push();
197 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800198 ~TScopedSymbolTableLevel()
199 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400200 while (!mTable->atBuiltInLevel())
201 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),
257 mGeometryShaderMaxVertices(-1),
258 mGeometryShaderInvocations(0),
259 mGeometryShaderInputPrimitiveType(EptUndefined),
260 mGeometryShaderOutputPrimitiveType(EptUndefined)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000261{
Martin Radev802abe02016-08-04 17:48:32 +0300262 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000263}
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.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400338 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000339
340 // Parse shader.
Olli Etuahod10cf692017-11-02 11:06:14 +0200341 if (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
342 &parseContext) != 0)
343 {
344 return nullptr;
345 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000346
Olli Etuahod10cf692017-11-02 11:06:14 +0200347 if (parseContext.getTreeRoot() == nullptr)
348 {
349 return nullptr;
350 }
351
352 setASTMetadata(parseContext);
353
354 if (MapSpecToShaderVersion(shaderSpec) < shaderVersion)
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700355 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000356 mDiagnostics.globalError("unsupported shader version");
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
Olli Etuahod10cf692017-11-02 11:06:14 +0200369void TCompiler::setASTMetadata(const TParseContext &parseContext)
370{
371 shaderVersion = parseContext.getShaderVersion();
Olli Etuaho09b04a22016-12-15 13:30:26 +0000372
Olli Etuahod10cf692017-11-02 11:06:14 +0200373 mPragma = parseContext.pragma();
374 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000375
Olli Etuahod10cf692017-11-02 11:06:14 +0200376 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
377 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
Olli Etuahoa6996682015-10-12 14:32:30 +0300378
Olli Etuahod10cf692017-11-02 11:06:14 +0200379 mNumViews = parseContext.getNumViews();
380
381 // Highp might have been auto-enabled based on shader version
382 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
383
384 if (shaderType == GL_GEOMETRY_SHADER_OES)
385 {
386 mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType();
387 mGeometryShaderOutputPrimitiveType = parseContext.getGeometryShaderOutputPrimitiveType();
388 mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices();
389 mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations();
390 }
391}
392
393bool TCompiler::checkAndSimplifyAST(TIntermBlock *root,
394 const TParseContext &parseContext,
395 ShCompileOptions compileOptions)
396{
397 // Disallow expressions deemed too complex.
398 if ((compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY) && !limitExpressionComplexity(root))
399 {
400 return false;
401 }
402
403 // We prune no-ops to work around driver bugs and to keep AST processing and output simple.
404 // The following kinds of no-ops are pruned:
405 // 1. Empty declarations "int;".
406 // 2. Literal statements: "1.0;". The ESSL output doesn't define a default precision
407 // for float, so float literal statements would end up with no precision which is
408 // invalid ESSL.
409 // After this empty declarations are not allowed in the AST.
410 PruneNoOps(root);
411
412 // In case the last case inside a switch statement is a certain type of no-op, GLSL
413 // compilers in drivers may not accept it. In this case we clean up the dead code from the
414 // end of switch statements. This is also required because PruneNoOps may have left switch
415 // statements that only contained an empty declaration inside the final case in an invalid
416 // state. Relies on that PruneNoOps has already been run.
417 RemoveNoOpCasesFromEndOfSwitchStatements(root, &symbolTable);
418
419 // Remove empty switch statements - this makes output simpler.
420 RemoveEmptySwitchStatements(root);
421
422 // Create the function DAG and check there is no recursion
423 if (!initCallDag(root))
424 {
425 return false;
426 }
427
428 if ((compileOptions & SH_LIMIT_CALL_STACK_DEPTH) && !checkCallDepth())
429 {
430 return false;
431 }
432
433 // Checks which functions are used and if "main" exists
434 functionMetadata.clear();
435 functionMetadata.resize(mCallDag.size());
436 if (!tagUsedFunctions())
437 {
438 return false;
439 }
440
441 if (!(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
442 {
443 pruneUnusedFunctions(root);
444 }
445
446 if (shaderVersion >= 310 && !ValidateVaryingLocations(root, &mDiagnostics, shaderType))
447 {
448 return false;
449 }
450
451 if (shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER &&
452 !ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers,
453 &mDiagnostics))
454 {
455 return false;
456 }
457
458 if (shouldRunLoopAndIndexingValidation(compileOptions) &&
459 !ValidateLimitations(root, shaderType, &symbolTable, shaderVersion, &mDiagnostics))
460 {
461 return false;
462 }
463
464 // Fail compilation if precision emulation not supported.
465 if (getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision &&
466 !EmulatePrecision::SupportedInLanguage(outputType))
467 {
468 mDiagnostics.globalError("Precision emulation not supported for this output type.");
469 return false;
470 }
471
Olli Etuahod10cf692017-11-02 11:06:14 +0200472 // Clamping uniform array bounds needs to happen after validateLimitations pass.
473 if (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)
474 {
475 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
476 }
477
478 if ((compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
479 parseContext.isExtensionEnabled(TExtension::OVR_multiview) &&
480 getShaderType() != GL_COMPUTE_SHADER)
481 {
482 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions,
483 outputType, &symbolTable);
484 }
485
486 // This pass might emit short circuits so keep it before the short circuit unfolding
487 if (compileOptions & SH_REWRITE_DO_WHILE_LOOPS)
488 RewriteDoWhile(root, &symbolTable);
489
490 if (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION)
491 sh::AddAndTrueToLoopCondition(root);
492
493 if (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)
494 {
495 UnfoldShortCircuitAST unfoldShortCircuit;
496 root->traverse(&unfoldShortCircuit);
497 unfoldShortCircuit.updateTree();
498 }
499
500 if (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT)
501 {
502 RemovePow(root);
503 }
504
Olli Etuahod10cf692017-11-02 11:06:14 +0200505 if (compileOptions & SH_REGENERATE_STRUCT_NAMES)
506 {
507 RegenerateStructNames gen(&symbolTable, shaderVersion);
508 root->traverse(&gen);
509 }
510
511 if (shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
512 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
513 IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers))
514 {
515 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables,
516 &symbolTable, shaderVersion);
517 }
518
Olli Etuahod10cf692017-11-02 11:06:14 +0200519 // Split multi declarations and remove calls to array length().
520 // Note that SimplifyLoopConditions needs to be run before any other AST transformations
521 // that may need to generate new statements from loop conditions or loop expressions.
522 SimplifyLoopConditions(
523 root,
524 IntermNodePatternMatcher::kMultiDeclaration | IntermNodePatternMatcher::kArrayLengthMethod,
525 &getSymbolTable(), getShaderVersion());
526
527 // Note that separate declarations need to be run before other AST transformations that
528 // generate new statements from expressions.
529 SeparateDeclarations(root);
530
531 SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod, &getSymbolTable(),
532 getShaderVersion());
533
534 RemoveArrayLengthMethod(root);
535
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200536 RemoveUnreferencedVariables(root, &symbolTable);
537
538 // Built-in function emulation needs to happen after validateLimitations pass.
539 // TODO(jmadill): Remove global pool allocator.
540 GetGlobalPoolAllocator()->lock();
541 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
542 GetGlobalPoolAllocator()->unlock();
543 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
544
545 if (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
546 {
547 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh, &symbolTable);
548 }
549
550 if (shouldCollectVariables(compileOptions))
551 {
552 ASSERT(!variablesCollected);
553 CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings,
554 &outputVaryings, &uniformBlocks, &shaderStorageBlocks, &inBlocks,
555 hashFunction, &symbolTable, shaderVersion, shaderType, extensionBehavior);
556 collectInterfaceBlocks();
557 variablesCollected = true;
558 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
559 {
560 useAllMembersInUnusedStandardAndSharedBlocks(root);
561 }
562 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
563 {
564 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
565 // Appendix A, section 7, the shader does not use too many uniforms.
566 if (!CheckVariablesInPackingLimits(maxUniformVectors, uniforms))
567 {
568 mDiagnostics.globalError("too many uniforms");
569 return false;
570 }
571 }
572 if (compileOptions & SH_INIT_OUTPUT_VARIABLES)
573 {
574 initializeOutputVariables(root);
575 }
576 }
577
578 // Removing invariant declarations must be done after collecting variables.
579 // Otherwise, built-in invariant declarations don't apply.
580 if (RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
581 {
582 RemoveInvariantDeclaration(root);
583 }
584
585 // gl_Position is always written in compatibility output mode.
586 // It may have been already initialized among other output variables, in that case we don't
587 // need to initialize it twice.
588 if (shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
589 ((compileOptions & SH_INIT_GL_POSITION) || (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
590 {
591 initializeGLPosition(root);
592 mGLPositionInitialized = true;
593 }
594
Olli Etuahob5601eb2017-11-15 18:08:04 +0200595 // DeferGlobalInitializers needs to be run before other AST transformations that generate new
596 // statements from expressions. But it's fine to run DeferGlobalInitializers after the above
597 // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST
598 // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00.
599 bool initializeLocalsAndGlobals =
600 (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType());
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300601 bool canUseLoopsToInitialize = !(compileOptions & SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES);
602 DeferGlobalInitializers(root, initializeLocalsAndGlobals, canUseLoopsToInitialize, &symbolTable);
603
Olli Etuahob5601eb2017-11-15 18:08:04 +0200604 if (initializeLocalsAndGlobals)
Olli Etuahod10cf692017-11-02 11:06:14 +0200605 {
606 // Initialize uninitialized local variables.
607 // In some cases initializing can generate extra statements in the parent block, such as
608 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
609 // we need to first simplify loop conditions. We've already separated declarations
610 // earlier, which is also required. If we don't follow the Appendix A limitations, loop
611 // init statements can declare arrays or nameless structs and have multiple
612 // declarations.
613
614 if (!shouldRunLoopAndIndexingValidation(compileOptions))
615 {
616 SimplifyLoopConditions(root,
617 IntermNodePatternMatcher::kArrayDeclaration |
618 IntermNodePatternMatcher::kNamelessStructDeclaration,
619 &getSymbolTable(), getShaderVersion());
620 }
Olli Etuaho661fc482017-10-16 12:17:05 +0300621
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300622 InitializeUninitializedLocals(root, getShaderVersion(), canUseLoopsToInitialize,
623 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200624 }
625
626 if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_CLAMP_POINT_SIZE))
627 {
628 ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable());
629 }
630
Olli Etuaho661fc482017-10-16 12:17:05 +0300631 if (compileOptions & SH_REWRITE_VECTOR_SCALAR_ARITHMETIC)
632 {
633 VectorizeVectorScalarArithmetic(root, &getSymbolTable());
634 }
635
Olli Etuahod10cf692017-11-02 11:06:14 +0200636 return true;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200637}
638
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800639bool TCompiler::compile(const char *const shaderStrings[],
640 size_t numStrings,
641 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200642{
Corentin Wallez28b65282016-06-16 07:24:50 -0700643#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
644 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
645#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
646
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200647 if (numStrings == 0)
648 return true;
649
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800650 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700651
652 // Apply key workarounds.
653 if (shouldFlattenPragmaStdglInvariantAll())
654 {
655 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
656 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
657 }
658
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200659 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100660 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200661
662 if (root)
663 {
664 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuahocccf2b02017-07-05 14:50:54 +0300665 OutputTree(root, infoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200666
667 if (compileOptions & SH_OBJECT_CODE)
Olli Etuaho89a69a02017-10-23 12:20:45 +0300668 {
669 PerformanceDiagnostics perfDiagnostics(&mDiagnostics);
670 translate(root, compileOptions, &perfDiagnostics);
671 }
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200672
673 // The IntermNode tree doesn't need to be deleted here, since the
674 // memory will be freed in a big chunk by the PoolAllocator.
675 return true;
676 }
677 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000678}
679
Nicolas Capens49a88872013-06-20 09:54:03 -0400680bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000681{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000682 if (resources.MaxDrawBuffers < 1)
683 {
684 return false;
685 }
686 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
687 {
688 return false;
689 }
690
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000691 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400692 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000693
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300694 ASSERT(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500695 symbolTable.push(); // COMMON_BUILTINS
696 symbolTable.push(); // ESSL1_BUILTINS
697 symbolTable.push(); // ESSL3_BUILTINS
698 symbolTable.push(); // ESSL3_1_BUILTINS
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300699 symbolTable.push(); // GLSL_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000700
Jamie Madillacb4b812016-11-07 13:50:29 -0500701 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400702 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500703 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300704 symbolTable.setDefaultPrecision(EbtInt, EbpMedium);
Jamie Madillacb4b812016-11-07 13:50:29 -0500705 break;
706 case GL_VERTEX_SHADER:
Jamie Madillacb4b812016-11-07 13:50:29 -0500707 case GL_COMPUTE_SHADER:
Shaob5cc1192017-07-06 10:47:20 +0800708 case GL_GEOMETRY_SHADER_OES:
Olli Etuahocce89652017-06-19 16:04:09 +0300709 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
710 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500711 break;
712 default:
Shaob5cc1192017-07-06 10:47:20 +0800713 UNREACHABLE();
Nicolas Capens49a88872013-06-20 09:54:03 -0400714 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200715 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400716 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200717 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
718 initSamplerDefaultPrecision(EbtSampler2D);
719 initSamplerDefaultPrecision(EbtSamplerCube);
720 // SamplerExternalOES is specified in the extension to have default precision.
721 initSamplerDefaultPrecision(EbtSamplerExternalOES);
Andrei Volykhina5527072017-03-22 16:46:30 +0300722 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
723 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200724 // It isn't specified whether Sampler2DRect has default precision.
725 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400726
Olli Etuahocce89652017-06-19 16:04:09 +0300727 symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh);
jchen104cdac9e2017-05-08 11:01:20 +0800728
Jamie Madill1b452142013-07-12 14:51:11 -0400729 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400730
731 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
732
733 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000734}
735
Olli Etuaho183d7e22015-11-20 15:59:09 +0200736void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
737{
738 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
Olli Etuahocce89652017-06-19 16:04:09 +0300739 symbolTable.setDefaultPrecision(samplerType, EbpLow);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200740}
741
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400742void TCompiler::setResourceString()
743{
744 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400745
746 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400747 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500748 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
749 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
750 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
751 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
752 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
753 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
754 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
755 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
756 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
757 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
758 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
759 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
760 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
761 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
762 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
763 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
764 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
765 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
766 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
767 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
768 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
769 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
770 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Martin Radev318f9aa2017-05-17 17:47:28 +0300771 << ":OVR_multiview:" << compileResources.OVR_multiview
Andrei Volykhina5527072017-03-22 16:46:30 +0300772 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Shaob5cc1192017-07-06 10:47:20 +0800773 << ":OES_geometry_shader:" << compileResources.OES_geometry_shader
Jamie Madillacb4b812016-11-07 13:50:29 -0500774 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
775 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
776 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
777 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
778 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Martin Radev318f9aa2017-05-17 17:47:28 +0300779 << ":MaxViewsOVR:" << compileResources.MaxViewsOVR
Jamie Madillacb4b812016-11-07 13:50:29 -0500780 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
781 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
Martin Radev84aa2dc2017-09-11 15:51:02 +0300782 << ":MinProgramTextureGatherOffset:" << compileResources.MinProgramTextureGatherOffset
783 << ":MaxProgramTextureGatherOffset:" << compileResources.MaxProgramTextureGatherOffset
Jamie Madillacb4b812016-11-07 13:50:29 -0500784 << ":MaxImageUnits:" << compileResources.MaxImageUnits
785 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
786 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
787 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
788 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
789 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
790 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
791 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
792 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
793 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
794 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
795 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
796 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
797 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
798 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
799 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
800 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
801 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
802 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
803 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
804 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
805 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
806 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
Shaob5cc1192017-07-06 10:47:20 +0800807 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize
Shaob5cc1192017-07-06 10:47:20 +0800808 << ":MaxGeometryUniformComponents:" << compileResources.MaxGeometryUniformComponents
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800809 << ":MaxGeometryUniformBlocks:" << compileResources.MaxGeometryUniformBlocks
810 << ":MaxGeometryInputComponents:" << compileResources.MaxGeometryInputComponents
811 << ":MaxGeometryOutputComponents:" << compileResources.MaxGeometryOutputComponents
812 << ":MaxGeometryOutputVertices:" << compileResources.MaxGeometryOutputVertices
813 << ":MaxGeometryTotalOutputComponents:" << compileResources.MaxGeometryTotalOutputComponents
814 << ":MaxGeometryTextureImageUnits:" << compileResources.MaxGeometryTextureImageUnits
815 << ":MaxGeometryAtomicCounterBuffers:" << compileResources.MaxGeometryAtomicCounterBuffers
816 << ":MaxGeometryAtomicCounters:" << compileResources.MaxGeometryAtomicCounters
817 << ":MaxGeometryShaderStorageBlocks:" << compileResources.MaxGeometryShaderStorageBlocks
818 << ":MaxGeometryShaderInvocations:" << compileResources.MaxGeometryShaderInvocations
819 << ":MaxGeometryImageUniforms:" << compileResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -0400820 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400821
822 builtInResourcesString = strstream.str();
823}
824
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800825void TCompiler::collectInterfaceBlocks()
826{
827 ASSERT(interfaceBlocks.empty());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800828 interfaceBlocks.reserve(uniformBlocks.size() + shaderStorageBlocks.size() + inBlocks.size());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800829 interfaceBlocks.insert(interfaceBlocks.end(), uniformBlocks.begin(), uniformBlocks.end());
830 interfaceBlocks.insert(interfaceBlocks.end(), shaderStorageBlocks.begin(),
831 shaderStorageBlocks.end());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800832 interfaceBlocks.insert(interfaceBlocks.end(), inBlocks.begin(), inBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800833}
834
alokp@chromium.org07620a52010-09-23 17:53:56 +0000835void TCompiler::clearResults()
836{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000837 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000838 infoSink.info.erase();
839 infoSink.obj.erase();
840 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000841 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000842
Jamie Madilled27c722014-07-02 15:31:23 -0400843 attributes.clear();
844 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000845 uniforms.clear();
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800846 inputVaryings.clear();
847 outputVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400848 interfaceBlocks.clear();
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800849 uniformBlocks.clear();
850 shaderStorageBlocks.clear();
Jiawei Shaod8105a02017-08-08 09:54:36 +0800851 inBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700852 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300853 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000854
Olli Etuaho09b04a22016-12-15 13:30:26 +0000855 mNumViews = -1;
856
Shaob5cc1192017-07-06 10:47:20 +0800857 mGeometryShaderInputPrimitiveType = EptUndefined;
858 mGeometryShaderOutputPrimitiveType = EptUndefined;
859 mGeometryShaderInvocations = 0;
860 mGeometryShaderMaxVertices = -1;
861
Olli Etuahodfa75e82017-01-23 09:43:06 -0800862 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000863
864 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200865
Yunchao Hed7297bf2017-04-19 15:27:10 +0800866 mSourcePath = nullptr;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000867}
868
Corentin Wallez71d147f2015-02-11 11:15:24 -0800869bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000870{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800871 mCallDag.clear();
872
Olli Etuaho77ba4082016-12-16 12:01:18 +0000873 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800874 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500875 case CallDAG::INITDAG_SUCCESS:
876 return true;
877 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500878 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000879 // Error message has already been written out.
880 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500881 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800882 }
883
884 UNREACHABLE();
885 return true;
886}
887
888bool TCompiler::checkCallDepth()
889{
890 std::vector<int> depths(mCallDag.size());
891
892 for (size_t i = 0; i < mCallDag.size(); i++)
893 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500894 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800895 auto &record = mCallDag.getRecordFromIndex(i);
896
897 for (auto &calleeIndex : record.callees)
898 {
899 depth = std::max(depth, depths[calleeIndex] + 1);
900 }
901
902 depths[i] = depth;
903
904 if (depth >= maxCallStackDepth)
905 {
906 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000907 std::stringstream errorStream;
908 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
909 << ") with the following call chain: " << record.name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800910
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700911 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500912 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800913
914 while (currentFunction != -1)
915 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000916 errorStream << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800917
918 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500919 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800920 {
921 if (depths[calleeIndex] == currentDepth - 1)
922 {
923 currentDepth--;
924 nextFunction = calleeIndex;
925 }
926 }
927
928 currentFunction = nextFunction;
929 }
930
Olli Etuaho77ba4082016-12-16 12:01:18 +0000931 std::string errorStr = errorStream.str();
932 mDiagnostics.globalError(errorStr.c_str());
933
Corentin Wallez71d147f2015-02-11 11:15:24 -0800934 return false;
935 }
936 }
937
938 return true;
939}
940
941bool TCompiler::tagUsedFunctions()
942{
943 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700944 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800945 {
Olli Etuahoec9232b2017-03-27 17:01:37 +0300946 if (mCallDag.getRecordFromIndex(i).name == "main")
Corentin Wallez71d147f2015-02-11 11:15:24 -0800947 {
948 internalTagUsedFunction(i);
949 return true;
950 }
951 }
952
Olli Etuaho77ba4082016-12-16 12:01:18 +0000953 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800954 return false;
955}
956
957void TCompiler::internalTagUsedFunction(size_t index)
958{
959 if (functionMetadata[index].used)
960 {
961 return;
962 }
963
964 functionMetadata[index].used = true;
965
966 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
967 {
968 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000969 }
970}
971
Corentin Walleza094a8a2015-04-07 11:53:06 -0700972// A predicate for the stl that returns if a top-level node is unused
973class TCompiler::UnusedPredicate
974{
975 public:
976 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500977 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700978 {
979 }
980
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500981 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700982 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000983 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
984 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700985
Olli Etuaho336b1472016-10-05 16:37:55 +0100986 const TFunctionSymbolInfo *functionInfo = nullptr;
987
Olli Etuaho16c745a2017-01-16 17:02:27 +0000988 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +0100989 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000990 functionInfo = asFunctionDefinition->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100991 }
Olli Etuaho16c745a2017-01-16 17:02:27 +0000992 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +0100993 {
Olli Etuaho16c745a2017-01-16 17:02:27 +0000994 functionInfo = asFunctionPrototype->getFunctionSymbolInfo();
Olli Etuaho336b1472016-10-05 16:37:55 +0100995 }
996 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700997 {
998 return false;
999 }
1000
Olli Etuaho336b1472016-10-05 16:37:55 +01001001 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001002 if (callDagIndex == CallDAG::InvalidIndex)
1003 {
1004 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +00001005 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001006 return true;
1007 }
1008
1009 ASSERT(callDagIndex < mMetadatas->size());
1010 return !(*mMetadatas)[callDagIndex].used;
1011 }
1012
1013 private:
1014 const CallDAG *mCallDag;
1015 const std::vector<FunctionMetadata> *mMetadatas;
1016};
1017
Olli Etuahod10cf692017-11-02 11:06:14 +02001018void TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001019{
Corentin Walleza094a8a2015-04-07 11:53:06 -07001020 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001021 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -07001022
1023 if (!sequence->empty())
1024 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001025 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
1026 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -07001027 }
Corentin Walleza094a8a2015-04-07 11:53:06 -07001028}
1029
Olli Etuahob4279202017-05-18 15:08:24 +03001030bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -04001031{
Olli Etuahocccf2b02017-07-05 14:50:54 +03001032 if (!IsASTDepthBelowLimit(root, maxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -04001033 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001034 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -04001035 return false;
1036 }
1037
Olli Etuahob4279202017-05-18 15:08:24 +03001038 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001039 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001040 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001041 return false;
1042 }
1043
Jamie Madilleb1a0102013-07-08 13:31:38 -04001044 return true;
1045}
1046
Corentin Wallez1df16022016-10-27 08:16:56 -04001047bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
1048{
1049 return (compileOptions & SH_VARIABLES) != 0;
1050}
1051
1052bool TCompiler::wereVariablesCollected() const
1053{
1054 return variablesCollected;
1055}
1056
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001057void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001058{
Zhenyao Mo72111912016-07-20 17:45:56 -07001059 InitVariableList list;
Olli Etuahoe7c28572017-10-23 16:29:33 +03001060 sh::ShaderVariable var(GL_FLOAT_VEC4);
Zhenyao Mo72111912016-07-20 17:45:56 -07001061 var.name = "gl_Position";
1062 list.push_back(var);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +03001063 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001064}
1065
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001066void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001067{
1068 sh::InterfaceBlockList list;
1069
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001070 for (auto block : uniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001071 {
1072 if (!block.staticUse &&
Qin Jiajiaca68d982017-09-18 16:41:56 +08001073 (block.layout == sh::BLOCKLAYOUT_STD140 || block.layout == sh::BLOCKLAYOUT_SHARED))
Qin Jiajia7835b522016-10-08 11:20:17 +08001074 {
1075 list.push_back(block);
1076 }
1077 }
1078
Zhenyao Mod7490962016-11-09 15:49:51 -08001079 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001080}
1081
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001082void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001083{
Zhenyao Mo72111912016-07-20 17:45:56 -07001084 InitVariableList list;
Jiawei Shaod27f5c82017-08-23 09:38:08 +08001085 if (shaderType == GL_VERTEX_SHADER || shaderType == GL_GEOMETRY_SHADER_OES)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001086 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001087 for (auto var : outputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001088 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001089 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001090 if (var.name == "gl_Position")
1091 {
1092 ASSERT(!mGLPositionInitialized);
1093 mGLPositionInitialized = true;
1094 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001095 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001096 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001097 else
1098 {
1099 ASSERT(shaderType == GL_FRAGMENT_SHADER);
1100 for (auto var : outputVariables)
1101 {
1102 list.push_back(var);
1103 }
1104 }
Olli Etuaho2c7f34c2017-10-09 17:18:02 +03001105 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001106}
1107
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001108const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001109{
1110 return extensionBehavior;
1111}
zmo@google.com32e97312011-08-24 01:03:11 +00001112
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001113const char *TCompiler::getSourcePath() const
1114{
1115 return mSourcePath;
1116}
1117
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001118const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001119{
1120 return compileResources;
1121}
1122
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001123const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001124{
1125 return arrayBoundsClamper;
1126}
1127
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001128ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1129{
1130 return clampingStrategy;
1131}
1132
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001133const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001134{
1135 return builtInFunctionEmulator;
1136}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001137
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001138void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001139{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001140 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1141 {
1142 TInfoSinkBase &sink = infoSink.obj;
1143 if (mPragma.stdgl.invariantAll)
1144 sink << "#pragma STDGL invariant(all)\n";
1145 }
1146}
1147
1148bool TCompiler::isVaryingDefined(const char *varyingName)
1149{
1150 ASSERT(variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001151 for (size_t ii = 0; ii < inputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001152 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001153 if (inputVaryings[ii].name == varyingName)
1154 {
1155 return true;
1156 }
1157 }
1158 for (size_t ii = 0; ii < outputVaryings.size(); ++ii)
1159 {
1160 if (outputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001161 {
1162 return true;
1163 }
1164 }
1165
1166 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001167}
Jamie Madillacb4b812016-11-07 13:50:29 -05001168
1169} // namespace sh