blob: a53e875445f922ffec4b6ff3e944ae23a4719513 [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 Etuaho9733cee2017-05-11 19:14:35 +030025#include "compiler/translator/IntermNodePatternMatcher.h"
Olli Etuahocccf2b02017-07-05 14:50:54 +030026#include "compiler/translator/IsASTDepthBelowLimit.h"
27#include "compiler/translator/OutputTree.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040028#include "compiler/translator/ParseContext.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"
Olli Etuahoc1f14fb2017-10-28 19:17:23 +030032#include "compiler/translator/RemoveEmptySwitchStatements.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080033#include "compiler/translator/RemoveInvariantDeclaration.h"
Olli Etuaho923ecef2017-10-11 12:01:38 +030034#include "compiler/translator/RemoveNoOpCasesFromEndOfSwitchStatements.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030035#include "compiler/translator/RemovePow.h"
Olli Etuaho3d70ca92017-11-10 16:53:26 +020036#include "compiler/translator/RemoveUnreferencedVariables.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070037#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070038#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Olli Etuaho9733cee2017-05-11 19:14:35 +030039#include "compiler/translator/SeparateDeclarations.h"
40#include "compiler/translator/SimplifyLoopConditions.h"
Olli Etuahobb5a7e22017-08-30 13:03:12 +030041#include "compiler/translator/SplitSequenceOperator.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070042#include "compiler/translator/UnfoldShortCircuitAST.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080043#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040044#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020045#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040046#include "compiler/translator/ValidateOutputs.h"
Jiawei Shao4cc89e22017-08-31 14:25:54 +080047#include "compiler/translator/ValidateVaryingLocations.h"
Geoff Lang17732822013-08-29 13:46:49 -040048#include "compiler/translator/VariablePacker.h"
Olli Etuaho661fc482017-10-16 12:17:05 +030049#include "compiler/translator/VectorizeVectorScalarArithmetic.h"
Olli Etuahob5601eb2017-11-15 18:08:04 +020050#include "compiler/translator/util.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000051#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070052
Jamie Madillacb4b812016-11-07 13:50:29 -050053namespace sh
54{
55
Corentin Wallez28b65282016-06-16 07:24:50 -070056namespace
57{
58
59#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
60void DumpFuzzerCase(char const *const *shaderStrings,
61 size_t numStrings,
62 uint32_t type,
63 uint32_t spec,
64 uint32_t output,
65 uint64_t options)
66{
67 static int fileIndex = 0;
68
69 std::ostringstream o;
70 o << "corpus/" << fileIndex++ << ".sample";
71 std::string s = o.str();
72
73 // Must match the input format of the fuzzer
74 FILE *f = fopen(s.c_str(), "w");
75 fwrite(&type, sizeof(type), 1, f);
76 fwrite(&spec, sizeof(spec), 1, f);
77 fwrite(&output, sizeof(output), 1, f);
78 fwrite(&options, sizeof(options), 1, f);
79
80 char zero[128 - 20] = {0};
81 fwrite(&zero, 128 - 20, 1, f);
82
83 for (size_t i = 0; i < numStrings; i++)
84 {
85 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
86 }
87 fwrite(&zero, 1, 1, f);
88
89 fclose(f);
90}
91#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
92} // anonymous namespace
93
Jamie Madill5508f392014-02-20 13:31:36 -050094bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000095{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080096 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000097}
98
Qingqing Dengad0d0792015-04-08 14:25:06 -070099bool IsGLSL130OrNewer(ShShaderOutput output)
100{
Jamie Madillacb4b812016-11-07 13:50:29 -0500101 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
102 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
103 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
104 output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
105 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -0700106}
107
Qiankun Miao705a9192016-08-29 10:05:27 +0800108bool IsGLSL420OrNewer(ShShaderOutput output)
109{
Jamie Madillacb4b812016-11-07 13:50:29 -0500110 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
111 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800112}
113
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800114bool IsGLSL410OrOlder(ShShaderOutput output)
115{
116 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
117 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
118 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
119}
120
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000121bool RemoveInvariant(sh::GLenum shaderType,
122 int shaderVersion,
123 ShShaderOutput outputType,
124 ShCompileOptions compileOptions)
125{
126 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
127 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
128 return true;
129
130 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800131 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000132 return true;
133
134 return false;
135}
136
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700137size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500138{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800139 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500140 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700141 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500142 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500143 case SH_WEBGL_SPEC:
144 return 256;
145 default:
146 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500147 }
148}
149
Shao77891c02017-06-23 16:30:17 +0800150int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
151{
152 switch (shaderType)
153 {
154 case GL_VERTEX_SHADER:
155 return resources.MaxVertexUniformVectors;
156 case GL_FRAGMENT_SHADER:
157 return resources.MaxFragmentUniformVectors;
Shaob5cc1192017-07-06 10:47:20 +0800158
159 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
Shao77891c02017-06-23 16:30:17 +0800160 case GL_COMPUTE_SHADER:
Shao77891c02017-06-23 16:30:17 +0800161 return resources.MaxComputeUniformComponents / 4;
Jiawei Shaobd924af2017-11-16 15:28:04 +0800162 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +0800163 return resources.MaxGeometryUniformComponents / 4;
Shao77891c02017-06-23 16:30:17 +0800164 default:
Shaob5cc1192017-07-06 10:47:20 +0800165 UNREACHABLE();
Shao77891c02017-06-23 16:30:17 +0800166 return -1;
167 }
168}
169
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500170namespace
171{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700172
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800173class TScopedPoolAllocator
174{
175 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500176 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800177 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400178 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000179 SetGlobalPoolAllocator(mAllocator);
180 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800181 ~TScopedPoolAllocator()
182 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800183 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400184 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000185 }
186
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800187 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500188 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400189};
190
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800191class TScopedSymbolTableLevel
192{
193 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500194 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800195 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400196 ASSERT(mTable->atBuiltInLevel());
197 mTable->push();
198 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800199 ~TScopedSymbolTableLevel()
200 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400201 while (!mTable->atBuiltInLevel())
202 mTable->pop();
203 }
204
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800205 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500206 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000207};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700208
209int MapSpecToShaderVersion(ShShaderSpec spec)
210{
211 switch (spec)
212 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500213 case SH_GLES2_SPEC:
214 case SH_WEBGL_SPEC:
215 return 100;
216 case SH_GLES3_SPEC:
217 case SH_WEBGL2_SPEC:
218 return 300;
219 case SH_GLES3_1_SPEC:
220 case SH_WEBGL3_SPEC:
221 return 310;
222 default:
223 UNREACHABLE();
224 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700225 }
226}
227
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000228} // namespace
229
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800230TShHandleBase::TShHandleBase()
231{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000232 allocator.push();
233 SetGlobalPoolAllocator(&allocator);
234}
235
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800236TShHandleBase::~TShHandleBase()
237{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800238 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000239 allocator.popAll();
240}
241
Jamie Madill183bde52014-07-02 15:31:19 -0400242TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700243 : variablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300244 mGLPositionInitialized(false),
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700245 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000246 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400247 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400248 maxUniformVectors(0),
249 maxExpressionComplexity(0),
250 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200251 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000252 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000253 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200254 builtInFunctionEmulator(),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000255 mDiagnostics(infoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800256 mSourcePath(nullptr),
Shaob5cc1192017-07-06 10:47:20 +0800257 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500258 mComputeShaderLocalSize(1),
Shaob5cc1192017-07-06 10:47:20 +0800259 mGeometryShaderMaxVertices(-1),
260 mGeometryShaderInvocations(0),
261 mGeometryShaderInputPrimitiveType(EptUndefined),
262 mGeometryShaderOutputPrimitiveType(EptUndefined)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000263{
264}
265
266TCompiler::~TCompiler()
267{
268}
269
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800270bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300271{
272 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
273 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
274 // of ESSL 1.00 as in Appendix A of the spec).
275 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
276 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
277}
278
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500279bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000280{
Shao77891c02017-06-23 16:30:17 +0800281 shaderVersion = 100;
282
283 maxUniformVectors = GetMaxUniformVectorsForShaderType(shaderType, resources);
284
Jamie Madilleb1a0102013-07-08 13:31:38 -0400285 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200286 maxCallStackDepth = resources.MaxCallStackDepth;
287 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400288
289 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000290
alokp@chromium.org07620a52010-09-23 17:53:56 +0000291 // Generate built-in symbol table.
292 if (!InitBuiltInSymbolTable(resources))
293 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000294 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000295 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000296
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000297 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
298 clampingStrategy = resources.ArrayIndexClampingStrategy;
299
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000300 hashFunction = resources.HashFunction;
301
alokp@chromium.org07620a52010-09-23 17:53:56 +0000302 return true;
303}
304
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100305TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
306 size_t numStrings,
307 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000308{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200309 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
310}
311
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100312TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
313 size_t numStrings,
314 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200315{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000316 clearResults();
317
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200318 ASSERT(numStrings > 0);
319 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000320
David Yen0fbd1282015-02-02 14:46:09 -0800321 // Reset the extension behavior for each compilation unit.
322 ResetExtensionBehavior(extensionBehavior);
323
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000324 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000325 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000326 if (compileOptions & SH_SOURCE_PATH)
327 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200328 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000329 ++firstSource;
330 }
331
Olli Etuahof119a262016-08-19 15:54:22 +0300332 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000333 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200334
Olli Etuahoa6996682015-10-12 14:32:30 +0300335 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000336
337 // We preserve symbols at the built-in level from compile-to-compile.
338 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400339 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
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
355 if (MapSpecToShaderVersion(shaderSpec) < shaderVersion)
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700356 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000357 mDiagnostics.globalError("unsupported shader version");
Olli Etuahod10cf692017-11-02 11:06:14 +0200358 return nullptr;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700359 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000360
Olli Etuahod10cf692017-11-02 11:06:14 +0200361 TIntermBlock *root = parseContext.getTreeRoot();
362 if (!checkAndSimplifyAST(root, parseContext, compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800363 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200364 return nullptr;
365 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700366
Olli Etuahod10cf692017-11-02 11:06:14 +0200367 return root;
368}
Martin Radev802abe02016-08-04 17:48:32 +0300369
Olli Etuahod10cf692017-11-02 11:06:14 +0200370void TCompiler::setASTMetadata(const TParseContext &parseContext)
371{
372 shaderVersion = parseContext.getShaderVersion();
Olli Etuaho09b04a22016-12-15 13:30:26 +0000373
Olli Etuahod10cf692017-11-02 11:06:14 +0200374 mPragma = parseContext.pragma();
375 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000376
Olli Etuahod10cf692017-11-02 11:06:14 +0200377 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
378 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
Olli Etuahoa6996682015-10-12 14:32:30 +0300379
Olli Etuahod10cf692017-11-02 11:06:14 +0200380 mNumViews = parseContext.getNumViews();
381
382 // Highp might have been auto-enabled based on shader version
383 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
384
Jiawei Shaobd924af2017-11-16 15:28:04 +0800385 if (shaderType == GL_GEOMETRY_SHADER_EXT)
Olli Etuahod10cf692017-11-02 11:06:14 +0200386 {
387 mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType();
388 mGeometryShaderOutputPrimitiveType = parseContext.getGeometryShaderOutputPrimitiveType();
389 mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices();
390 mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations();
391 }
392}
393
394bool TCompiler::checkAndSimplifyAST(TIntermBlock *root,
395 const TParseContext &parseContext,
396 ShCompileOptions compileOptions)
397{
398 // Disallow expressions deemed too complex.
399 if ((compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY) && !limitExpressionComplexity(root))
400 {
401 return false;
402 }
403
Olli Etuaho765924f2018-01-04 12:48:36 +0200404 if (shouldRunLoopAndIndexingValidation(compileOptions) &&
Olli Etuaho68981eb2018-01-23 17:46:12 +0200405 !ValidateLimitations(root, shaderType, &symbolTable, &mDiagnostics))
Olli Etuaho765924f2018-01-04 12:48:36 +0200406 {
407 return false;
408 }
409
410 // Fold expressions that could not be folded before validation that was done as a part of
411 // parsing.
412 FoldExpressions(root, &mDiagnostics);
413 // Folding should only be able to generate warnings.
414 ASSERT(mDiagnostics.numErrors() == 0);
415
Olli Etuahod10cf692017-11-02 11:06:14 +0200416 // We prune no-ops to work around driver bugs and to keep AST processing and output simple.
417 // The following kinds of no-ops are pruned:
418 // 1. Empty declarations "int;".
419 // 2. Literal statements: "1.0;". The ESSL output doesn't define a default precision
420 // for float, so float literal statements would end up with no precision which is
421 // invalid ESSL.
422 // After this empty declarations are not allowed in the AST.
Olli Etuahob38dfde2018-01-04 15:49:03 +0200423 PruneNoOps(root, &symbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200424
425 // In case the last case inside a switch statement is a certain type of no-op, GLSL
426 // compilers in drivers may not accept it. In this case we clean up the dead code from the
427 // end of switch statements. This is also required because PruneNoOps may have left switch
428 // statements that only contained an empty declaration inside the final case in an invalid
429 // state. Relies on that PruneNoOps has already been run.
430 RemoveNoOpCasesFromEndOfSwitchStatements(root, &symbolTable);
431
432 // Remove empty switch statements - this makes output simpler.
433 RemoveEmptySwitchStatements(root);
434
435 // Create the function DAG and check there is no recursion
436 if (!initCallDag(root))
437 {
438 return false;
439 }
440
441 if ((compileOptions & SH_LIMIT_CALL_STACK_DEPTH) && !checkCallDepth())
442 {
443 return false;
444 }
445
446 // Checks which functions are used and if "main" exists
447 functionMetadata.clear();
448 functionMetadata.resize(mCallDag.size());
449 if (!tagUsedFunctions())
450 {
451 return false;
452 }
453
454 if (!(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
455 {
456 pruneUnusedFunctions(root);
457 }
458
459 if (shaderVersion >= 310 && !ValidateVaryingLocations(root, &mDiagnostics, shaderType))
460 {
461 return false;
462 }
463
464 if (shaderVersion >= 300 && shaderType == GL_FRAGMENT_SHADER &&
465 !ValidateOutputs(root, getExtensionBehavior(), compileResources.MaxDrawBuffers,
466 &mDiagnostics))
467 {
468 return false;
469 }
470
Olli Etuahod10cf692017-11-02 11:06:14 +0200471 // Fail compilation if precision emulation not supported.
472 if (getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision &&
473 !EmulatePrecision::SupportedInLanguage(outputType))
474 {
475 mDiagnostics.globalError("Precision emulation not supported for this output type.");
476 return false;
477 }
478
Olli Etuahod10cf692017-11-02 11:06:14 +0200479 // Clamping uniform array bounds needs to happen after validateLimitations pass.
480 if (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)
481 {
482 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
483 }
484
485 if ((compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
486 parseContext.isExtensionEnabled(TExtension::OVR_multiview) &&
487 getShaderType() != GL_COMPUTE_SHADER)
488 {
489 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, shaderType, compileOptions,
490 outputType, &symbolTable);
491 }
492
493 // This pass might emit short circuits so keep it before the short circuit unfolding
494 if (compileOptions & SH_REWRITE_DO_WHILE_LOOPS)
495 RewriteDoWhile(root, &symbolTable);
496
497 if (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION)
Olli Etuaho1776fd02018-01-31 11:46:52 +0200498 AddAndTrueToLoopCondition(root);
Olli Etuahod10cf692017-11-02 11:06:14 +0200499
500 if (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)
501 {
Olli Etuaho1776fd02018-01-31 11:46:52 +0200502 UnfoldShortCircuitAST(root);
Olli Etuahod10cf692017-11-02 11:06:14 +0200503 }
504
505 if (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT)
506 {
507 RemovePow(root);
508 }
509
Olli Etuahod10cf692017-11-02 11:06:14 +0200510 if (compileOptions & SH_REGENERATE_STRUCT_NAMES)
511 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +0100512 RegenerateStructNames gen(&symbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200513 root->traverse(&gen);
514 }
515
516 if (shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
517 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
518 IsExtensionEnabled(extensionBehavior, TExtension::EXT_draw_buffers))
519 {
520 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables,
521 &symbolTable, shaderVersion);
522 }
523
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200524 int simplifyScalarized = (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
525 ? IntermNodePatternMatcher::kScalarizedVecOrMatConstructor
526 : 0;
527
Olli Etuahod10cf692017-11-02 11:06:14 +0200528 // Split multi declarations and remove calls to array length().
529 // Note that SimplifyLoopConditions needs to be run before any other AST transformations
530 // that may need to generate new statements from loop conditions or loop expressions.
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200531 SimplifyLoopConditions(root,
532 IntermNodePatternMatcher::kMultiDeclaration |
533 IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200534 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200535
536 // Note that separate declarations need to be run before other AST transformations that
537 // generate new statements from expressions.
538 SeparateDeclarations(root);
539
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200540 SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200541 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200542
543 RemoveArrayLengthMethod(root);
544
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200545 RemoveUnreferencedVariables(root, &symbolTable);
546
547 // Built-in function emulation needs to happen after validateLimitations pass.
548 // TODO(jmadill): Remove global pool allocator.
549 GetGlobalPoolAllocator()->lock();
550 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
551 GetGlobalPoolAllocator()->unlock();
552 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
553
554 if (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
555 {
556 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh, &symbolTable);
557 }
558
559 if (shouldCollectVariables(compileOptions))
560 {
561 ASSERT(!variablesCollected);
562 CollectVariables(root, &attributes, &outputVariables, &uniforms, &inputVaryings,
563 &outputVaryings, &uniformBlocks, &shaderStorageBlocks, &inBlocks,
564 hashFunction, &symbolTable, shaderVersion, shaderType, extensionBehavior);
565 collectInterfaceBlocks();
566 variablesCollected = true;
567 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
568 {
569 useAllMembersInUnusedStandardAndSharedBlocks(root);
570 }
571 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
572 {
573 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
574 // Appendix A, section 7, the shader does not use too many uniforms.
575 if (!CheckVariablesInPackingLimits(maxUniformVectors, uniforms))
576 {
577 mDiagnostics.globalError("too many uniforms");
578 return false;
579 }
580 }
581 if (compileOptions & SH_INIT_OUTPUT_VARIABLES)
582 {
583 initializeOutputVariables(root);
584 }
585 }
586
587 // Removing invariant declarations must be done after collecting variables.
588 // Otherwise, built-in invariant declarations don't apply.
589 if (RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
590 {
591 RemoveInvariantDeclaration(root);
592 }
593
594 // gl_Position is always written in compatibility output mode.
595 // It may have been already initialized among other output variables, in that case we don't
596 // need to initialize it twice.
597 if (shaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
598 ((compileOptions & SH_INIT_GL_POSITION) || (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
599 {
600 initializeGLPosition(root);
601 mGLPositionInitialized = true;
602 }
603
Olli Etuahob5601eb2017-11-15 18:08:04 +0200604 // DeferGlobalInitializers needs to be run before other AST transformations that generate new
605 // statements from expressions. But it's fine to run DeferGlobalInitializers after the above
606 // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST
607 // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00.
608 bool initializeLocalsAndGlobals =
609 (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType());
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300610 bool canUseLoopsToInitialize = !(compileOptions & SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES);
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200611 bool highPrecisionSupported =
612 shaderType != GL_FRAGMENT_SHADER || compileResources.FragmentPrecisionHigh;
613 DeferGlobalInitializers(root, initializeLocalsAndGlobals, canUseLoopsToInitialize,
614 highPrecisionSupported, &symbolTable);
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300615
Olli Etuahob5601eb2017-11-15 18:08:04 +0200616 if (initializeLocalsAndGlobals)
Olli Etuahod10cf692017-11-02 11:06:14 +0200617 {
618 // Initialize uninitialized local variables.
619 // In some cases initializing can generate extra statements in the parent block, such as
620 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
621 // we need to first simplify loop conditions. We've already separated declarations
622 // earlier, which is also required. If we don't follow the Appendix A limitations, loop
623 // init statements can declare arrays or nameless structs and have multiple
624 // declarations.
625
626 if (!shouldRunLoopAndIndexingValidation(compileOptions))
627 {
628 SimplifyLoopConditions(root,
629 IntermNodePatternMatcher::kArrayDeclaration |
630 IntermNodePatternMatcher::kNamelessStructDeclaration,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200631 &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200632 }
Olli Etuaho661fc482017-10-16 12:17:05 +0300633
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300634 InitializeUninitializedLocals(root, getShaderVersion(), canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200635 highPrecisionSupported, &getSymbolTable());
Olli Etuahod10cf692017-11-02 11:06:14 +0200636 }
637
638 if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_CLAMP_POINT_SIZE))
639 {
640 ClampPointSize(root, compileResources.MaxPointSize, &getSymbolTable());
641 }
642
Olli Etuaho0690e1a2017-12-21 20:51:38 +0200643 if (getShaderType() == GL_FRAGMENT_SHADER && (compileOptions & SH_CLAMP_FRAG_DEPTH))
644 {
645 ClampFragDepth(root, &getSymbolTable());
646 }
647
Olli Etuaho661fc482017-10-16 12:17:05 +0300648 if (compileOptions & SH_REWRITE_VECTOR_SCALAR_ARITHMETIC)
649 {
650 VectorizeVectorScalarArithmetic(root, &getSymbolTable());
651 }
652
Olli Etuahod10cf692017-11-02 11:06:14 +0200653 return true;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200654}
655
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800656bool TCompiler::compile(const char *const shaderStrings[],
657 size_t numStrings,
658 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200659{
Corentin Wallez28b65282016-06-16 07:24:50 -0700660#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
661 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
662#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
663
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200664 if (numStrings == 0)
665 return true;
666
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800667 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700668
669 // Apply key workarounds.
670 if (shouldFlattenPragmaStdglInvariantAll())
671 {
672 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
673 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
674 }
675
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200676 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100677 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200678
679 if (root)
680 {
681 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuahocccf2b02017-07-05 14:50:54 +0300682 OutputTree(root, infoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200683
684 if (compileOptions & SH_OBJECT_CODE)
Olli Etuaho89a69a02017-10-23 12:20:45 +0300685 {
686 PerformanceDiagnostics perfDiagnostics(&mDiagnostics);
687 translate(root, compileOptions, &perfDiagnostics);
688 }
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200689
690 // The IntermNode tree doesn't need to be deleted here, since the
691 // memory will be freed in a big chunk by the PoolAllocator.
692 return true;
693 }
694 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000695}
696
Nicolas Capens49a88872013-06-20 09:54:03 -0400697bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000698{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000699 if (resources.MaxDrawBuffers < 1)
700 {
701 return false;
702 }
703 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
704 {
705 return false;
706 }
707
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000708 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400709 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000710
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300711 ASSERT(symbolTable.isEmpty());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500712 symbolTable.push(); // COMMON_BUILTINS
713 symbolTable.push(); // ESSL1_BUILTINS
714 symbolTable.push(); // ESSL3_BUILTINS
715 symbolTable.push(); // ESSL3_1_BUILTINS
Olli Etuaho977ee7e2017-07-21 11:38:27 +0300716 symbolTable.push(); // GLSL_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000717
Jamie Madillacb4b812016-11-07 13:50:29 -0500718 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400719 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500720 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +0300721 symbolTable.setDefaultPrecision(EbtInt, EbpMedium);
Jamie Madillacb4b812016-11-07 13:50:29 -0500722 break;
723 case GL_VERTEX_SHADER:
Jamie Madillacb4b812016-11-07 13:50:29 -0500724 case GL_COMPUTE_SHADER:
Jiawei Shaobd924af2017-11-16 15:28:04 +0800725 case GL_GEOMETRY_SHADER_EXT:
Olli Etuahocce89652017-06-19 16:04:09 +0300726 symbolTable.setDefaultPrecision(EbtInt, EbpHigh);
727 symbolTable.setDefaultPrecision(EbtFloat, EbpHigh);
Jamie Madillacb4b812016-11-07 13:50:29 -0500728 break;
729 default:
Shaob5cc1192017-07-06 10:47:20 +0800730 UNREACHABLE();
Nicolas Capens49a88872013-06-20 09:54:03 -0400731 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200732 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400733 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200734 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
735 initSamplerDefaultPrecision(EbtSampler2D);
736 initSamplerDefaultPrecision(EbtSamplerCube);
737 // SamplerExternalOES is specified in the extension to have default precision.
738 initSamplerDefaultPrecision(EbtSamplerExternalOES);
Andrei Volykhina5527072017-03-22 16:46:30 +0300739 // SamplerExternal2DY2YEXT is specified in the extension to have default precision.
740 initSamplerDefaultPrecision(EbtSamplerExternal2DY2YEXT);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200741 // It isn't specified whether Sampler2DRect has default precision.
742 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400743
Olli Etuahocce89652017-06-19 16:04:09 +0300744 symbolTable.setDefaultPrecision(EbtAtomicCounter, EbpHigh);
jchen104cdac9e2017-05-08 11:01:20 +0800745
Jamie Madill1b452142013-07-12 14:51:11 -0400746 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400747
748 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
749
Olli Etuaho5d69db12017-11-24 16:51:15 +0200750 symbolTable.markBuiltInInitializationFinished();
751
Nicolas Capens49a88872013-06-20 09:54:03 -0400752 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000753}
754
Olli Etuaho183d7e22015-11-20 15:59:09 +0200755void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
756{
757 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
Olli Etuahocce89652017-06-19 16:04:09 +0300758 symbolTable.setDefaultPrecision(samplerType, EbpLow);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200759}
760
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400761void TCompiler::setResourceString()
762{
763 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400764
765 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400766 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500767 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
768 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
769 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
770 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
771 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
772 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
773 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
774 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
775 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
776 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
777 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
778 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
779 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
780 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
781 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
782 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
783 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
784 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
785 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
786 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
787 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
788 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
789 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Martin Radev318f9aa2017-05-17 17:47:28 +0300790 << ":OVR_multiview:" << compileResources.OVR_multiview
Andrei Volykhina5527072017-03-22 16:46:30 +0300791 << ":EXT_YUV_target:" << compileResources.EXT_YUV_target
Jiawei Shaobd924af2017-11-16 15:28:04 +0800792 << ":EXT_geometry_shader:" << compileResources.EXT_geometry_shader
Jamie Madillacb4b812016-11-07 13:50:29 -0500793 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
794 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
795 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
796 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
797 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Martin Radev318f9aa2017-05-17 17:47:28 +0300798 << ":MaxViewsOVR:" << compileResources.MaxViewsOVR
Jamie Madillacb4b812016-11-07 13:50:29 -0500799 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
800 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
Martin Radev84aa2dc2017-09-11 15:51:02 +0300801 << ":MinProgramTextureGatherOffset:" << compileResources.MinProgramTextureGatherOffset
802 << ":MaxProgramTextureGatherOffset:" << compileResources.MaxProgramTextureGatherOffset
Jamie Madillacb4b812016-11-07 13:50:29 -0500803 << ":MaxImageUnits:" << compileResources.MaxImageUnits
804 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
805 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
806 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
807 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
808 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
809 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
810 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
811 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
812 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
813 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
814 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
815 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
816 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
817 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
818 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
819 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
820 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
821 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
822 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
823 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
824 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
825 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
Shaob5cc1192017-07-06 10:47:20 +0800826 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize
Shaob5cc1192017-07-06 10:47:20 +0800827 << ":MaxGeometryUniformComponents:" << compileResources.MaxGeometryUniformComponents
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800828 << ":MaxGeometryUniformBlocks:" << compileResources.MaxGeometryUniformBlocks
829 << ":MaxGeometryInputComponents:" << compileResources.MaxGeometryInputComponents
830 << ":MaxGeometryOutputComponents:" << compileResources.MaxGeometryOutputComponents
831 << ":MaxGeometryOutputVertices:" << compileResources.MaxGeometryOutputVertices
832 << ":MaxGeometryTotalOutputComponents:" << compileResources.MaxGeometryTotalOutputComponents
833 << ":MaxGeometryTextureImageUnits:" << compileResources.MaxGeometryTextureImageUnits
834 << ":MaxGeometryAtomicCounterBuffers:" << compileResources.MaxGeometryAtomicCounterBuffers
835 << ":MaxGeometryAtomicCounters:" << compileResources.MaxGeometryAtomicCounters
836 << ":MaxGeometryShaderStorageBlocks:" << compileResources.MaxGeometryShaderStorageBlocks
837 << ":MaxGeometryShaderInvocations:" << compileResources.MaxGeometryShaderInvocations
838 << ":MaxGeometryImageUniforms:" << compileResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -0400839 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400840
841 builtInResourcesString = strstream.str();
842}
843
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800844void TCompiler::collectInterfaceBlocks()
845{
846 ASSERT(interfaceBlocks.empty());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800847 interfaceBlocks.reserve(uniformBlocks.size() + shaderStorageBlocks.size() + inBlocks.size());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800848 interfaceBlocks.insert(interfaceBlocks.end(), uniformBlocks.begin(), uniformBlocks.end());
849 interfaceBlocks.insert(interfaceBlocks.end(), shaderStorageBlocks.begin(),
850 shaderStorageBlocks.end());
Jiawei Shaod8105a02017-08-08 09:54:36 +0800851 interfaceBlocks.insert(interfaceBlocks.end(), inBlocks.begin(), inBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800852}
853
alokp@chromium.org07620a52010-09-23 17:53:56 +0000854void TCompiler::clearResults()
855{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000856 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000857 infoSink.info.erase();
858 infoSink.obj.erase();
859 infoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +0000860 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000861
Jamie Madilled27c722014-07-02 15:31:23 -0400862 attributes.clear();
863 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000864 uniforms.clear();
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +0800865 inputVaryings.clear();
866 outputVaryings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400867 interfaceBlocks.clear();
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800868 uniformBlocks.clear();
869 shaderStorageBlocks.clear();
Jiawei Shaod8105a02017-08-08 09:54:36 +0800870 inBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700871 variablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +0300872 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000873
Olli Etuaho09b04a22016-12-15 13:30:26 +0000874 mNumViews = -1;
875
Shaob5cc1192017-07-06 10:47:20 +0800876 mGeometryShaderInputPrimitiveType = EptUndefined;
877 mGeometryShaderOutputPrimitiveType = EptUndefined;
878 mGeometryShaderInvocations = 0;
879 mGeometryShaderMaxVertices = -1;
880
Olli Etuahodfa75e82017-01-23 09:43:06 -0800881 builtInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000882
883 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200884
Yunchao Hed7297bf2017-04-19 15:27:10 +0800885 mSourcePath = nullptr;
Olli Etuaho5d69db12017-11-24 16:51:15 +0200886
887 symbolTable.clearCompilationResults();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000888}
889
Corentin Wallez71d147f2015-02-11 11:15:24 -0800890bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000891{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800892 mCallDag.clear();
893
Olli Etuaho77ba4082016-12-16 12:01:18 +0000894 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800895 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500896 case CallDAG::INITDAG_SUCCESS:
897 return true;
898 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -0500899 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +0000900 // Error message has already been written out.
901 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -0500902 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800903 }
904
905 UNREACHABLE();
906 return true;
907}
908
909bool TCompiler::checkCallDepth()
910{
911 std::vector<int> depths(mCallDag.size());
912
913 for (size_t i = 0; i < mCallDag.size(); i++)
914 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500915 int depth = 0;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800916 auto &record = mCallDag.getRecordFromIndex(i);
917
918 for (auto &calleeIndex : record.callees)
919 {
920 depth = std::max(depth, depths[calleeIndex] + 1);
921 }
922
923 depths[i] = depth;
924
925 if (depth >= maxCallStackDepth)
926 {
927 // Trace back the function chain to have a meaningful info log.
Olli Etuaho77ba4082016-12-16 12:01:18 +0000928 std::stringstream errorStream;
929 errorStream << "Call stack too deep (larger than " << maxCallStackDepth
Olli Etuahoc33f1e82017-12-29 16:55:29 +0200930 << ") with the following call chain: "
931 << record.node->getFunction()->name();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800932
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700933 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500934 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800935
936 while (currentFunction != -1)
937 {
Olli Etuahoc33f1e82017-12-29 16:55:29 +0200938 errorStream
939 << " -> "
940 << mCallDag.getRecordFromIndex(currentFunction).node->getFunction()->name();
Corentin Wallez71d147f2015-02-11 11:15:24 -0800941
942 int nextFunction = -1;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500943 for (auto &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800944 {
945 if (depths[calleeIndex] == currentDepth - 1)
946 {
947 currentDepth--;
948 nextFunction = calleeIndex;
949 }
950 }
951
952 currentFunction = nextFunction;
953 }
954
Olli Etuaho77ba4082016-12-16 12:01:18 +0000955 std::string errorStr = errorStream.str();
956 mDiagnostics.globalError(errorStr.c_str());
957
Corentin Wallez71d147f2015-02-11 11:15:24 -0800958 return false;
959 }
960 }
961
962 return true;
963}
964
965bool TCompiler::tagUsedFunctions()
966{
967 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700968 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800969 {
Olli Etuahoc33f1e82017-12-29 16:55:29 +0200970 if (mCallDag.getRecordFromIndex(i).node->getFunction()->isMain())
Corentin Wallez71d147f2015-02-11 11:15:24 -0800971 {
972 internalTagUsedFunction(i);
973 return true;
974 }
975 }
976
Olli Etuaho77ba4082016-12-16 12:01:18 +0000977 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -0800978 return false;
979}
980
981void TCompiler::internalTagUsedFunction(size_t index)
982{
983 if (functionMetadata[index].used)
984 {
985 return;
986 }
987
988 functionMetadata[index].used = true;
989
990 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
991 {
992 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000993 }
994}
995
Corentin Walleza094a8a2015-04-07 11:53:06 -0700996// A predicate for the stl that returns if a top-level node is unused
997class TCompiler::UnusedPredicate
998{
999 public:
1000 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -05001001 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001002 {
1003 }
1004
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001005 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001006 {
Olli Etuaho16c745a2017-01-16 17:02:27 +00001007 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
1008 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -07001009
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001010 const TFunction *func = nullptr;
Olli Etuaho336b1472016-10-05 16:37:55 +01001011
Olli Etuaho16c745a2017-01-16 17:02:27 +00001012 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +01001013 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001014 func = asFunctionDefinition->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001015 }
Olli Etuaho16c745a2017-01-16 17:02:27 +00001016 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +01001017 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001018 func = asFunctionPrototype->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001019 }
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001020 if (func == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001021 {
1022 return false;
1023 }
1024
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001025 size_t callDagIndex = mCallDag->findIndex(func->uniqueId());
Corentin Walleza094a8a2015-04-07 11:53:06 -07001026 if (callDagIndex == CallDAG::InvalidIndex)
1027 {
1028 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +00001029 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001030 return true;
1031 }
1032
1033 ASSERT(callDagIndex < mMetadatas->size());
1034 return !(*mMetadatas)[callDagIndex].used;
1035 }
1036
1037 private:
1038 const CallDAG *mCallDag;
1039 const std::vector<FunctionMetadata> *mMetadatas;
1040};
1041
Olli Etuahod10cf692017-11-02 11:06:14 +02001042void TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001043{
Corentin Walleza094a8a2015-04-07 11:53:06 -07001044 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001045 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -07001046
1047 if (!sequence->empty())
1048 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001049 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
1050 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -07001051 }
Corentin Walleza094a8a2015-04-07 11:53:06 -07001052}
1053
Olli Etuahob4279202017-05-18 15:08:24 +03001054bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -04001055{
Olli Etuahocccf2b02017-07-05 14:50:54 +03001056 if (!IsASTDepthBelowLimit(root, maxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -04001057 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001058 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -04001059 return false;
1060 }
1061
Olli Etuahob4279202017-05-18 15:08:24 +03001062 if (!ValidateMaxParameters(root, maxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001063 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001064 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001065 return false;
1066 }
1067
Jamie Madilleb1a0102013-07-08 13:31:38 -04001068 return true;
1069}
1070
Corentin Wallez1df16022016-10-27 08:16:56 -04001071bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
1072{
1073 return (compileOptions & SH_VARIABLES) != 0;
1074}
1075
1076bool TCompiler::wereVariablesCollected() const
1077{
1078 return variablesCollected;
1079}
1080
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001081void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001082{
Zhenyao Mo72111912016-07-20 17:45:56 -07001083 InitVariableList list;
Olli Etuahoe7c28572017-10-23 16:29:33 +03001084 sh::ShaderVariable var(GL_FLOAT_VEC4);
Zhenyao Mo72111912016-07-20 17:45:56 -07001085 var.name = "gl_Position";
1086 list.push_back(var);
Olli Etuaho87cc90d2017-12-12 15:28:06 +02001087 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001088}
1089
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001090void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001091{
1092 sh::InterfaceBlockList list;
1093
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001094 for (auto block : uniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001095 {
1096 if (!block.staticUse &&
Qin Jiajiaca68d982017-09-18 16:41:56 +08001097 (block.layout == sh::BLOCKLAYOUT_STD140 || block.layout == sh::BLOCKLAYOUT_SHARED))
Qin Jiajia7835b522016-10-08 11:20:17 +08001098 {
1099 list.push_back(block);
1100 }
1101 }
1102
Zhenyao Mod7490962016-11-09 15:49:51 -08001103 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001104}
1105
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001106void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001107{
Zhenyao Mo72111912016-07-20 17:45:56 -07001108 InitVariableList list;
Jiawei Shaobd924af2017-11-16 15:28:04 +08001109 if (shaderType == GL_VERTEX_SHADER || shaderType == GL_GEOMETRY_SHADER_EXT)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001110 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001111 for (auto var : outputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001112 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001113 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001114 if (var.name == "gl_Position")
1115 {
1116 ASSERT(!mGLPositionInitialized);
1117 mGLPositionInitialized = true;
1118 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001119 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001120 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001121 else
1122 {
1123 ASSERT(shaderType == GL_FRAGMENT_SHADER);
1124 for (auto var : outputVariables)
1125 {
1126 list.push_back(var);
1127 }
1128 }
Olli Etuaho87cc90d2017-12-12 15:28:06 +02001129 InitializeVariables(root, list, &symbolTable, shaderVersion, extensionBehavior, false, false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001130}
1131
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001132const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001133{
1134 return extensionBehavior;
1135}
zmo@google.com32e97312011-08-24 01:03:11 +00001136
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001137const char *TCompiler::getSourcePath() const
1138{
1139 return mSourcePath;
1140}
1141
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001142const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001143{
1144 return compileResources;
1145}
1146
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001147const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001148{
1149 return arrayBoundsClamper;
1150}
1151
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001152ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1153{
1154 return clampingStrategy;
1155}
1156
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001157const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001158{
1159 return builtInFunctionEmulator;
1160}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001161
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001162void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001163{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001164 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1165 {
1166 TInfoSinkBase &sink = infoSink.obj;
1167 if (mPragma.stdgl.invariantAll)
1168 sink << "#pragma STDGL invariant(all)\n";
1169 }
1170}
1171
1172bool TCompiler::isVaryingDefined(const char *varyingName)
1173{
1174 ASSERT(variablesCollected);
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001175 for (size_t ii = 0; ii < inputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001176 {
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001177 if (inputVaryings[ii].name == varyingName)
1178 {
1179 return true;
1180 }
1181 }
1182 for (size_t ii = 0; ii < outputVaryings.size(); ++ii)
1183 {
1184 if (outputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001185 {
1186 return true;
1187 }
1188 }
1189
1190 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001191}
Jamie Madillacb4b812016-11-07 13:50:29 -05001192
1193} // namespace sh