blob: 771d050492559b59d206096ba839778f87511894 [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"
Corentin Wallez71d147f2015-02-11 11:15:24 -080013#include "compiler/translator/CallDAG.h"
Olli Etuaho78ed6cd2017-08-09 16:19:00 +030014#include "compiler/translator/CollectVariables.h"
Geoff Lang17732822013-08-29 13:46:49 -040015#include "compiler/translator/Initialize.h"
Olli Etuahocccf2b02017-07-05 14:50:54 +030016#include "compiler/translator/IsASTDepthBelowLimit.h"
17#include "compiler/translator/OutputTree.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040018#include "compiler/translator/ParseContext.h"
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020020#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040021#include "compiler/translator/ValidateOutputs.h"
Jiawei Shao4cc89e22017-08-31 14:25:54 +080022#include "compiler/translator/ValidateVaryingLocations.h"
Geoff Lang17732822013-08-29 13:46:49 -040023#include "compiler/translator/VariablePacker.h"
Olli Etuahoa07b4212018-03-22 16:13:13 +020024#include "compiler/translator/tree_ops/AddAndTrueToLoopCondition.h"
25#include "compiler/translator/tree_ops/ClampFragDepth.h"
26#include "compiler/translator/tree_ops/ClampPointSize.h"
27#include "compiler/translator/tree_ops/DeclareAndInitBuiltinsForInstancedMultiview.h"
28#include "compiler/translator/tree_ops/DeferGlobalInitializers.h"
Austin Eng7cf9cd22018-10-09 15:27:32 -070029#include "compiler/translator/tree_ops/EmulateGLDrawID.h"
Olli Etuahoa07b4212018-03-22 16:13:13 +020030#include "compiler/translator/tree_ops/EmulateGLFragColorBroadcast.h"
31#include "compiler/translator/tree_ops/EmulatePrecision.h"
32#include "compiler/translator/tree_ops/FoldExpressions.h"
33#include "compiler/translator/tree_ops/InitializeVariables.h"
34#include "compiler/translator/tree_ops/PruneEmptyCases.h"
35#include "compiler/translator/tree_ops/PruneNoOps.h"
36#include "compiler/translator/tree_ops/RegenerateStructNames.h"
37#include "compiler/translator/tree_ops/RemoveArrayLengthMethod.h"
38#include "compiler/translator/tree_ops/RemoveInvariantDeclaration.h"
39#include "compiler/translator/tree_ops/RemovePow.h"
40#include "compiler/translator/tree_ops/RemoveUnreferencedVariables.h"
41#include "compiler/translator/tree_ops/RewriteDoWhile.h"
Olli Etuaho983460e2018-05-02 17:57:39 +030042#include "compiler/translator/tree_ops/RewriteRepeatedAssignToSwizzled.h"
Olli Etuahoa07b4212018-03-22 16:13:13 +020043#include "compiler/translator/tree_ops/ScalarizeVecAndMatConstructorArgs.h"
44#include "compiler/translator/tree_ops/SeparateDeclarations.h"
45#include "compiler/translator/tree_ops/SimplifyLoopConditions.h"
46#include "compiler/translator/tree_ops/SplitSequenceOperator.h"
47#include "compiler/translator/tree_ops/UnfoldShortCircuitAST.h"
48#include "compiler/translator/tree_ops/UseInterfaceBlockFields.h"
49#include "compiler/translator/tree_ops/VectorizeVectorScalarArithmetic.h"
Olli Etuaho59c5b892018-04-03 11:44:50 +030050#include "compiler/translator/tree_util/BuiltIn_autogen.h"
Olli Etuahoc26214d2018-03-16 10:43:11 +020051#include "compiler/translator/tree_util/IntermNodePatternMatcher.h"
Olli Etuahob5601eb2017-11-15 18:08:04 +020052#include "compiler/translator/util.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000053#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070054
Jamie Madillacb4b812016-11-07 13:50:29 -050055namespace sh
56{
57
Corentin Wallez28b65282016-06-16 07:24:50 -070058namespace
59{
60
61#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
62void DumpFuzzerCase(char const *const *shaderStrings,
63 size_t numStrings,
64 uint32_t type,
65 uint32_t spec,
66 uint32_t output,
67 uint64_t options)
68{
69 static int fileIndex = 0;
70
Jonah Ryan-Davis1d35d512019-02-15 15:39:27 +000071 std::ostringstream o;
Corentin Wallez28b65282016-06-16 07:24:50 -070072 o << "corpus/" << fileIndex++ << ".sample";
73 std::string s = o.str();
74
75 // Must match the input format of the fuzzer
76 FILE *f = fopen(s.c_str(), "w");
77 fwrite(&type, sizeof(type), 1, f);
78 fwrite(&spec, sizeof(spec), 1, f);
79 fwrite(&output, sizeof(output), 1, f);
80 fwrite(&options, sizeof(options), 1, f);
81
82 char zero[128 - 20] = {0};
83 fwrite(&zero, 128 - 20, 1, f);
84
85 for (size_t i = 0; i < numStrings; i++)
86 {
87 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
88 }
89 fwrite(&zero, 1, 1, f);
90
91 fclose(f);
92}
93#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
94} // anonymous namespace
95
Qingqing Dengad0d0792015-04-08 14:25:06 -070096bool IsGLSL130OrNewer(ShShaderOutput output)
97{
Jamie Madillacb4b812016-11-07 13:50:29 -050098 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
99 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
100 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
101 output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
102 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -0700103}
104
Qiankun Miao705a9192016-08-29 10:05:27 +0800105bool IsGLSL420OrNewer(ShShaderOutput output)
106{
Jamie Madillacb4b812016-11-07 13:50:29 -0500107 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
108 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +0800109}
110
Zhenyao Mob7bf7422016-11-08 14:44:05 -0800111bool IsGLSL410OrOlder(ShShaderOutput output)
112{
113 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
114 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
115 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
116}
117
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000118bool RemoveInvariant(sh::GLenum shaderType,
119 int shaderVersion,
120 ShShaderOutput outputType,
121 ShCompileOptions compileOptions)
122{
123 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
124 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
125 return true;
126
127 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800128 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000129 return true;
130
131 return false;
132}
133
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700134size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500135{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800136 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500137 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700138 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500139 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500140 case SH_WEBGL_SPEC:
141 return 256;
142 default:
143 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500144 }
145}
146
Shao77891c02017-06-23 16:30:17 +0800147int GetMaxUniformVectorsForShaderType(GLenum shaderType, const ShBuiltInResources &resources)
148{
149 switch (shaderType)
150 {
151 case GL_VERTEX_SHADER:
152 return resources.MaxVertexUniformVectors;
153 case GL_FRAGMENT_SHADER:
154 return resources.MaxFragmentUniformVectors;
Shaob5cc1192017-07-06 10:47:20 +0800155
156 // TODO (jiawei.shao@intel.com): check if we need finer-grained component counting
Shao77891c02017-06-23 16:30:17 +0800157 case GL_COMPUTE_SHADER:
Shao77891c02017-06-23 16:30:17 +0800158 return resources.MaxComputeUniformComponents / 4;
Jiawei Shaobd924af2017-11-16 15:28:04 +0800159 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +0800160 return resources.MaxGeometryUniformComponents / 4;
Shao77891c02017-06-23 16:30:17 +0800161 default:
Shaob5cc1192017-07-06 10:47:20 +0800162 UNREACHABLE();
Shao77891c02017-06-23 16:30:17 +0800163 return -1;
164 }
165}
166
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500167namespace
168{
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700169
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800170class TScopedPoolAllocator
171{
172 public:
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000173 TScopedPoolAllocator(TPoolAllocator *allocator) : mAllocator(allocator)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800174 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400175 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000176 SetGlobalPoolAllocator(mAllocator);
177 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800178 ~TScopedPoolAllocator()
179 {
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800180 SetGlobalPoolAllocator(nullptr);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400181 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000182 }
183
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800184 private:
Tobin Ehlis47ca1b22019-01-23 16:11:41 +0000185 TPoolAllocator *mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400186};
187
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800188class TScopedSymbolTableLevel
189{
190 public:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500191 TScopedSymbolTableLevel(TSymbolTable *table) : mTable(table)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800192 {
Olli Etuaho437664b2018-02-28 15:38:14 +0200193 ASSERT(mTable->isEmpty());
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400194 mTable->push();
195 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800196 ~TScopedSymbolTableLevel()
197 {
Olli Etuaho437664b2018-02-28 15:38:14 +0200198 while (!mTable->isEmpty())
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400199 mTable->pop();
200 }
201
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800202 private:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500203 TSymbolTable *mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000204};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700205
206int MapSpecToShaderVersion(ShShaderSpec spec)
207{
208 switch (spec)
209 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500210 case SH_GLES2_SPEC:
211 case SH_WEBGL_SPEC:
212 return 100;
213 case SH_GLES3_SPEC:
214 case SH_WEBGL2_SPEC:
215 return 300;
216 case SH_GLES3_1_SPEC:
217 case SH_WEBGL3_SPEC:
218 return 310;
219 default:
220 UNREACHABLE();
221 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700222 }
223}
224
Olli Etuaho59c5b892018-04-03 11:44:50 +0300225bool ValidateFragColorAndFragData(GLenum shaderType,
226 int shaderVersion,
227 const TSymbolTable &symbolTable,
228 TDiagnostics *diagnostics)
229{
230 if (shaderVersion > 100 || shaderType != GL_FRAGMENT_SHADER)
231 {
232 return true;
233 }
234
235 bool usesFragColor = false;
236 bool usesFragData = false;
237 // This validation is a bit stricter than the spec - it's only an error to write to
238 // both FragData and FragColor. But because it's better not to have reads from undefined
239 // variables, we always return an error if they are both referenced, rather than only if they
240 // are written.
241 if (symbolTable.isStaticallyUsed(*BuiltInVariable::gl_FragColor()) ||
242 symbolTable.isStaticallyUsed(*BuiltInVariable::gl_SecondaryFragColorEXT()))
243 {
244 usesFragColor = true;
245 }
246 // Extension variables may not always be initialized (saves some time at symbol table init).
247 bool secondaryFragDataUsed =
248 symbolTable.gl_SecondaryFragDataEXT() != nullptr &&
249 symbolTable.isStaticallyUsed(*symbolTable.gl_SecondaryFragDataEXT());
250 if (symbolTable.isStaticallyUsed(*symbolTable.gl_FragData()) || secondaryFragDataUsed)
251 {
252 usesFragData = true;
253 }
254 if (usesFragColor && usesFragData)
255 {
256 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
257 if (symbolTable.isStaticallyUsed(*BuiltInVariable::gl_SecondaryFragColorEXT()) ||
258 secondaryFragDataUsed)
259 {
260 errorMessage =
261 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
262 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
263 }
264 diagnostics->globalError(errorMessage);
265 return false;
266 }
267 return true;
268}
269
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000270} // namespace
271
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800272TShHandleBase::TShHandleBase()
273{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000274 allocator.push();
275 SetGlobalPoolAllocator(&allocator);
276}
277
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800278TShHandleBase::~TShHandleBase()
279{
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800280 SetGlobalPoolAllocator(nullptr);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000281 allocator.popAll();
282}
283
Jamie Madill183bde52014-07-02 15:31:19 -0400284TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Olli Etuaho6f591c92018-09-21 11:20:47 +0300285 : mVariablesCollected(false),
Olli Etuahob12040c2017-06-27 14:20:45 +0300286 mGLPositionInitialized(false),
Olli Etuaho6f591c92018-09-21 11:20:47 +0300287 mShaderType(type),
288 mShaderSpec(spec),
289 mOutputType(output),
290 mBuiltInFunctionEmulator(),
291 mDiagnostics(mInfoSink.info),
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800292 mSourcePath(nullptr),
Shaob5cc1192017-07-06 10:47:20 +0800293 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500294 mComputeShaderLocalSize(1),
Shaob5cc1192017-07-06 10:47:20 +0800295 mGeometryShaderMaxVertices(-1),
296 mGeometryShaderInvocations(0),
297 mGeometryShaderInputPrimitiveType(EptUndefined),
298 mGeometryShaderOutputPrimitiveType(EptUndefined)
Jamie Madillb980c562018-11-27 11:34:27 -0500299{}
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000300
Jamie Madillb980c562018-11-27 11:34:27 -0500301TCompiler::~TCompiler() {}
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000302
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800303bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300304{
305 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
306 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
307 // of ESSL 1.00 as in Appendix A of the spec).
Olli Etuaho6f591c92018-09-21 11:20:47 +0300308 return (IsWebGLBasedSpec(mShaderSpec) && mShaderVersion == 100) ||
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300309 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
310}
311
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500312bool TCompiler::Init(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000313{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400314 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000315
alokp@chromium.org07620a52010-09-23 17:53:56 +0000316 // Generate built-in symbol table.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300317 if (!initBuiltInSymbolTable(resources))
alokp@chromium.org07620a52010-09-23 17:53:56 +0000318 return false;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000319
Olli Etuaho6f591c92018-09-21 11:20:47 +0300320 mResources = resources;
321 setResourceString();
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000322
Olli Etuaho6f591c92018-09-21 11:20:47 +0300323 InitExtensionBehavior(resources, mExtensionBehavior);
324 mArrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000325 return true;
326}
327
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100328TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
329 size_t numStrings,
330 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000331{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200332 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
333}
334
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100335TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
336 size_t numStrings,
337 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200338{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000339 clearResults();
340
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200341 ASSERT(numStrings > 0);
342 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000343
David Yen0fbd1282015-02-02 14:46:09 -0800344 // Reset the extension behavior for each compilation unit.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300345 ResetExtensionBehavior(mExtensionBehavior);
David Yen0fbd1282015-02-02 14:46:09 -0800346
Austin Eng7cf9cd22018-10-09 15:27:32 -0700347 // If gl_DrawID is not supported, remove it from the available extensions
348 // Currently we only allow emulation of gl_DrawID
349 const bool glDrawIDSupported = (compileOptions & SH_EMULATE_GL_DRAW_ID) != 0u;
350 if (!glDrawIDSupported)
351 {
352 auto it = mExtensionBehavior.find(TExtension::ANGLE_multi_draw);
353 if (it != mExtensionBehavior.end())
354 {
355 mExtensionBehavior.erase(it);
356 }
357 }
358
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000359 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000360 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000361 if (compileOptions & SH_SOURCE_PATH)
362 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200363 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000364 ++firstSource;
365 }
366
Olli Etuaho6f591c92018-09-21 11:20:47 +0300367 TParseContext parseContext(mSymbolTable, mExtensionBehavior, mShaderType, mShaderSpec,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000368 compileOptions, true, &mDiagnostics, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200369
Olli Etuaho6f591c92018-09-21 11:20:47 +0300370 parseContext.setFragmentPrecisionHighOnESSL1(mResources.FragmentPrecisionHigh == 1);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000371
372 // We preserve symbols at the built-in level from compile-to-compile.
373 // Start pushing the user-defined symbols at global level.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300374 TScopedSymbolTableLevel globalLevel(&mSymbolTable);
375 ASSERT(mSymbolTable.atGlobalLevel());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000376
377 // Parse shader.
Olli Etuahod10cf692017-11-02 11:06:14 +0200378 if (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr,
379 &parseContext) != 0)
380 {
381 return nullptr;
382 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000383
Olli Etuahod10cf692017-11-02 11:06:14 +0200384 if (parseContext.getTreeRoot() == nullptr)
385 {
386 return nullptr;
387 }
388
389 setASTMetadata(parseContext);
390
Jiawei Shaoe41df652018-02-09 14:31:39 +0800391 if (!checkShaderVersion(&parseContext))
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700392 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200393 return nullptr;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700394 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000395
Olli Etuahod10cf692017-11-02 11:06:14 +0200396 TIntermBlock *root = parseContext.getTreeRoot();
397 if (!checkAndSimplifyAST(root, parseContext, compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800398 {
Olli Etuahod10cf692017-11-02 11:06:14 +0200399 return nullptr;
400 }
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700401
Olli Etuahod10cf692017-11-02 11:06:14 +0200402 return root;
403}
Martin Radev802abe02016-08-04 17:48:32 +0300404
Jiawei Shaoe41df652018-02-09 14:31:39 +0800405bool TCompiler::checkShaderVersion(TParseContext *parseContext)
406{
Olli Etuaho6f591c92018-09-21 11:20:47 +0300407 if (MapSpecToShaderVersion(mShaderSpec) < mShaderVersion)
Jiawei Shaoe41df652018-02-09 14:31:39 +0800408 {
409 mDiagnostics.globalError("unsupported shader version");
410 return false;
411 }
412
413 ASSERT(parseContext);
Olli Etuaho6f591c92018-09-21 11:20:47 +0300414 switch (mShaderType)
Jiawei Shaoe41df652018-02-09 14:31:39 +0800415 {
416 case GL_COMPUTE_SHADER:
Olli Etuaho6f591c92018-09-21 11:20:47 +0300417 if (mShaderVersion < 310)
Jiawei Shaoe41df652018-02-09 14:31:39 +0800418 {
419 mDiagnostics.globalError("Compute shader is not supported in this shader version.");
420 return false;
421 }
422 break;
423
424 case GL_GEOMETRY_SHADER_EXT:
Olli Etuaho6f591c92018-09-21 11:20:47 +0300425 if (mShaderVersion < 310)
Jiawei Shaoe41df652018-02-09 14:31:39 +0800426 {
427 mDiagnostics.globalError(
428 "Geometry shader is not supported in this shader version.");
429 return false;
430 }
431 else
432 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300433 ASSERT(mShaderVersion == 310);
Jiawei Shaoe41df652018-02-09 14:31:39 +0800434 if (!parseContext->checkCanUseExtension(sh::TSourceLoc(),
435 TExtension::EXT_geometry_shader))
436 {
437 return false;
438 }
439 }
440 break;
441
442 default:
443 break;
444 }
445
446 return true;
447}
448
Olli Etuahod10cf692017-11-02 11:06:14 +0200449void TCompiler::setASTMetadata(const TParseContext &parseContext)
450{
Olli Etuaho6f591c92018-09-21 11:20:47 +0300451 mShaderVersion = parseContext.getShaderVersion();
Olli Etuaho09b04a22016-12-15 13:30:26 +0000452
Olli Etuahod10cf692017-11-02 11:06:14 +0200453 mPragma = parseContext.pragma();
Olli Etuaho6f591c92018-09-21 11:20:47 +0300454 mSymbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000455
Olli Etuahod10cf692017-11-02 11:06:14 +0200456 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
457 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
Olli Etuahoa6996682015-10-12 14:32:30 +0300458
Olli Etuahod10cf692017-11-02 11:06:14 +0200459 mNumViews = parseContext.getNumViews();
460
Olli Etuaho6f591c92018-09-21 11:20:47 +0300461 if (mShaderType == GL_GEOMETRY_SHADER_EXT)
Olli Etuahod10cf692017-11-02 11:06:14 +0200462 {
463 mGeometryShaderInputPrimitiveType = parseContext.getGeometryShaderInputPrimitiveType();
464 mGeometryShaderOutputPrimitiveType = parseContext.getGeometryShaderOutputPrimitiveType();
465 mGeometryShaderMaxVertices = parseContext.getGeometryShaderMaxVertices();
466 mGeometryShaderInvocations = parseContext.getGeometryShaderInvocations();
467 }
468}
469
470bool TCompiler::checkAndSimplifyAST(TIntermBlock *root,
471 const TParseContext &parseContext,
472 ShCompileOptions compileOptions)
473{
474 // Disallow expressions deemed too complex.
475 if ((compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY) && !limitExpressionComplexity(root))
476 {
477 return false;
478 }
479
Olli Etuaho765924f2018-01-04 12:48:36 +0200480 if (shouldRunLoopAndIndexingValidation(compileOptions) &&
Olli Etuaho6f591c92018-09-21 11:20:47 +0300481 !ValidateLimitations(root, mShaderType, &mSymbolTable, &mDiagnostics))
Olli Etuaho765924f2018-01-04 12:48:36 +0200482 {
483 return false;
484 }
485
Olli Etuaho6f591c92018-09-21 11:20:47 +0300486 if (!ValidateFragColorAndFragData(mShaderType, mShaderVersion, mSymbolTable, &mDiagnostics))
Olli Etuaho59c5b892018-04-03 11:44:50 +0300487 {
488 return false;
489 }
490
Olli Etuaho765924f2018-01-04 12:48:36 +0200491 // Fold expressions that could not be folded before validation that was done as a part of
492 // parsing.
493 FoldExpressions(root, &mDiagnostics);
494 // Folding should only be able to generate warnings.
495 ASSERT(mDiagnostics.numErrors() == 0);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400496 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
497 {
498 return false;
499 }
Olli Etuaho765924f2018-01-04 12:48:36 +0200500
Olli Etuahod10cf692017-11-02 11:06:14 +0200501 // We prune no-ops to work around driver bugs and to keep AST processing and output simple.
502 // The following kinds of no-ops are pruned:
503 // 1. Empty declarations "int;".
504 // 2. Literal statements: "1.0;". The ESSL output doesn't define a default precision
505 // for float, so float literal statements would end up with no precision which is
506 // invalid ESSL.
507 // After this empty declarations are not allowed in the AST.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300508 PruneNoOps(root, &mSymbolTable);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400509 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
510 {
511 return false;
512 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200513
Olli Etuahod10cf692017-11-02 11:06:14 +0200514 // Create the function DAG and check there is no recursion
515 if (!initCallDag(root))
516 {
517 return false;
518 }
519
520 if ((compileOptions & SH_LIMIT_CALL_STACK_DEPTH) && !checkCallDepth())
521 {
522 return false;
523 }
524
525 // Checks which functions are used and if "main" exists
Olli Etuaho6f591c92018-09-21 11:20:47 +0300526 mFunctionMetadata.clear();
527 mFunctionMetadata.resize(mCallDag.size());
Olli Etuahod10cf692017-11-02 11:06:14 +0200528 if (!tagUsedFunctions())
529 {
530 return false;
531 }
532
533 if (!(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
534 {
535 pruneUnusedFunctions(root);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400536 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
537 {
538 return false;
539 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200540 }
541
Olli Etuaho6f591c92018-09-21 11:20:47 +0300542 if (mShaderVersion >= 310 && !ValidateVaryingLocations(root, &mDiagnostics, mShaderType))
Olli Etuahod10cf692017-11-02 11:06:14 +0200543 {
544 return false;
545 }
546
Olli Etuaho6f591c92018-09-21 11:20:47 +0300547 if (mShaderVersion >= 300 && mShaderType == GL_FRAGMENT_SHADER &&
548 !ValidateOutputs(root, getExtensionBehavior(), mResources.MaxDrawBuffers, &mDiagnostics))
Olli Etuahod10cf692017-11-02 11:06:14 +0200549 {
550 return false;
551 }
552
Olli Etuahod10cf692017-11-02 11:06:14 +0200553 // Fail compilation if precision emulation not supported.
554 if (getResources().WEBGL_debug_shader_precision && getPragma().debugShaderPrecision &&
Olli Etuaho6f591c92018-09-21 11:20:47 +0300555 !EmulatePrecision::SupportedInLanguage(mOutputType))
Olli Etuahod10cf692017-11-02 11:06:14 +0200556 {
557 mDiagnostics.globalError("Precision emulation not supported for this output type.");
558 return false;
559 }
560
Olli Etuahod10cf692017-11-02 11:06:14 +0200561 // Clamping uniform array bounds needs to happen after validateLimitations pass.
562 if (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)
563 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300564 mArrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
Olli Etuahod10cf692017-11-02 11:06:14 +0200565 }
566
567 if ((compileOptions & SH_INITIALIZE_BUILTINS_FOR_INSTANCED_MULTIVIEW) &&
568 parseContext.isExtensionEnabled(TExtension::OVR_multiview) &&
569 getShaderType() != GL_COMPUTE_SHADER)
570 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300571 DeclareAndInitBuiltinsForInstancedMultiview(root, mNumViews, mShaderType, compileOptions,
572 mOutputType, &mSymbolTable);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400573 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
574 {
575 return false;
576 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200577 }
578
579 // This pass might emit short circuits so keep it before the short circuit unfolding
580 if (compileOptions & SH_REWRITE_DO_WHILE_LOOPS)
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400581 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300582 RewriteDoWhile(root, &mSymbolTable);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400583 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
584 {
585 return false;
586 }
587 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200588
589 if (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION)
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400590 {
Olli Etuaho1776fd02018-01-31 11:46:52 +0200591 AddAndTrueToLoopCondition(root);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400592 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
593 {
594 return false;
595 }
596 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200597
598 if (compileOptions & SH_UNFOLD_SHORT_CIRCUIT)
599 {
Olli Etuaho1776fd02018-01-31 11:46:52 +0200600 UnfoldShortCircuitAST(root);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400601 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
602 {
603 return false;
604 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200605 }
606
607 if (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT)
608 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300609 RemovePow(root, &mSymbolTable);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400610 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
611 {
612 return false;
613 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200614 }
615
Olli Etuahod10cf692017-11-02 11:06:14 +0200616 if (compileOptions & SH_REGENERATE_STRUCT_NAMES)
617 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300618 RegenerateStructNames gen(&mSymbolTable);
Olli Etuahod10cf692017-11-02 11:06:14 +0200619 root->traverse(&gen);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400620 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
621 {
622 return false;
623 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200624 }
625
Austin Eng7cf9cd22018-10-09 15:27:32 -0700626 if (mShaderType == GL_VERTEX_SHADER &&
627 IsExtensionEnabled(mExtensionBehavior, TExtension::ANGLE_multi_draw))
628 {
629 if ((compileOptions & SH_EMULATE_GL_DRAW_ID) != 0)
630 {
631 EmulateGLDrawID(root, &mSymbolTable, &mUniforms,
632 shouldCollectVariables(compileOptions));
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400633 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
634 {
635 return false;
636 }
Austin Eng7cf9cd22018-10-09 15:27:32 -0700637 }
638 }
639
Olli Etuaho6f591c92018-09-21 11:20:47 +0300640 if (mShaderType == GL_FRAGMENT_SHADER && mShaderVersion == 100 && mResources.EXT_draw_buffers &&
641 mResources.MaxDrawBuffers > 1 &&
642 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers))
Olli Etuahod10cf692017-11-02 11:06:14 +0200643 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300644 EmulateGLFragColorBroadcast(root, mResources.MaxDrawBuffers, &mOutputVariables,
645 &mSymbolTable, mShaderVersion);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400646 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
647 {
648 return false;
649 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200650 }
651
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200652 int simplifyScalarized = (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
653 ? IntermNodePatternMatcher::kScalarizedVecOrMatConstructor
654 : 0;
655
Olli Etuahod10cf692017-11-02 11:06:14 +0200656 // Split multi declarations and remove calls to array length().
657 // Note that SimplifyLoopConditions needs to be run before any other AST transformations
658 // that may need to generate new statements from loop conditions or loop expressions.
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200659 SimplifyLoopConditions(root,
660 IntermNodePatternMatcher::kMultiDeclaration |
661 IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200662 &getSymbolTable());
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400663 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
664 {
665 return false;
666 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200667
668 // Note that separate declarations need to be run before other AST transformations that
669 // generate new statements from expressions.
670 SeparateDeclarations(root);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400671 mValidateASTOptions.validateMultiDeclarations = true;
672 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
673 {
674 return false;
675 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200676
Olli Etuahoae04e1e2017-11-27 16:00:39 +0200677 SplitSequenceOperator(root, IntermNodePatternMatcher::kArrayLengthMethod | simplifyScalarized,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200678 &getSymbolTable());
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400679 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
680 {
681 return false;
682 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200683
684 RemoveArrayLengthMethod(root);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400685 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
686 {
687 return false;
688 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200689
Olli Etuaho6f591c92018-09-21 11:20:47 +0300690 RemoveUnreferencedVariables(root, &mSymbolTable);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400691 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
692 {
693 return false;
694 }
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200695
Olli Etuaho41217992018-03-15 11:15:33 +0200696 // In case the last case inside a switch statement is a certain type of no-op, GLSL compilers in
697 // drivers may not accept it. In this case we clean up the dead code from the end of switch
698 // statements. This is also required because PruneNoOps or RemoveUnreferencedVariables may have
699 // left switch statements that only contained an empty declaration inside the final case in an
700 // invalid state. Relies on that PruneNoOps and RemoveUnreferencedVariables have already been
701 // run.
Olli Etuaho0b0dcbc2018-03-14 11:33:22 +0200702 PruneEmptyCases(root);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400703 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
704 {
705 return false;
706 }
Olli Etuaho0b0dcbc2018-03-14 11:33:22 +0200707
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200708 // Built-in function emulation needs to happen after validateLimitations pass.
709 // TODO(jmadill): Remove global pool allocator.
710 GetGlobalPoolAllocator()->lock();
Olli Etuaho6f591c92018-09-21 11:20:47 +0300711 initBuiltInFunctionEmulator(&mBuiltInFunctionEmulator, compileOptions);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200712 GetGlobalPoolAllocator()->unlock();
Olli Etuaho6f591c92018-09-21 11:20:47 +0300713 mBuiltInFunctionEmulator.markBuiltInFunctionsForEmulation(root);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200714
Olli Etuaho6f591c92018-09-21 11:20:47 +0300715 bool highPrecisionSupported = mShaderVersion > 100 || mShaderType != GL_FRAGMENT_SHADER ||
716 mResources.FragmentPrecisionHigh == 1;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200717 if (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS)
718 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300719 ScalarizeVecAndMatConstructorArgs(root, mShaderType, highPrecisionSupported, &mSymbolTable);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400720 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
721 {
722 return false;
723 }
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200724 }
725
726 if (shouldCollectVariables(compileOptions))
727 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300728 ASSERT(!mVariablesCollected);
729 CollectVariables(root, &mAttributes, &mOutputVariables, &mUniforms, &mInputVaryings,
730 &mOutputVaryings, &mUniformBlocks, &mShaderStorageBlocks, &mInBlocks,
731 mResources.HashFunction, &mSymbolTable, mShaderType, mExtensionBehavior);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200732 collectInterfaceBlocks();
Olli Etuaho6f591c92018-09-21 11:20:47 +0300733 mVariablesCollected = true;
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200734 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
735 {
736 useAllMembersInUnusedStandardAndSharedBlocks(root);
737 }
738 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
739 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300740 int maxUniformVectors = GetMaxUniformVectorsForShaderType(mShaderType, mResources);
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200741 // Returns true if, after applying the packing rules in the GLSL ES 1.00.17 spec
742 // Appendix A, section 7, the shader does not use too many uniforms.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300743 if (!CheckVariablesInPackingLimits(maxUniformVectors, mUniforms))
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200744 {
745 mDiagnostics.globalError("too many uniforms");
746 return false;
747 }
748 }
Olli Etuaho6f591c92018-09-21 11:20:47 +0300749 if ((compileOptions & SH_INIT_OUTPUT_VARIABLES) && (mShaderType != GL_COMPUTE_SHADER))
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200750 {
751 initializeOutputVariables(root);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400752 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
753 {
754 return false;
755 }
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200756 }
757 }
758
759 // Removing invariant declarations must be done after collecting variables.
760 // Otherwise, built-in invariant declarations don't apply.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300761 if (RemoveInvariant(mShaderType, mShaderVersion, mOutputType, compileOptions))
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200762 {
763 RemoveInvariantDeclaration(root);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400764 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
765 {
766 return false;
767 }
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200768 }
769
770 // gl_Position is always written in compatibility output mode.
771 // It may have been already initialized among other output variables, in that case we don't
772 // need to initialize it twice.
Olli Etuaho6f591c92018-09-21 11:20:47 +0300773 if (mShaderType == GL_VERTEX_SHADER && !mGLPositionInitialized &&
774 ((compileOptions & SH_INIT_GL_POSITION) || (mOutputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200775 {
776 initializeGLPosition(root);
777 mGLPositionInitialized = true;
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400778 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
779 {
780 return false;
781 }
Olli Etuaho3d70ca92017-11-10 16:53:26 +0200782 }
783
Olli Etuahob5601eb2017-11-15 18:08:04 +0200784 // DeferGlobalInitializers needs to be run before other AST transformations that generate new
785 // statements from expressions. But it's fine to run DeferGlobalInitializers after the above
786 // SplitSequenceOperator and RemoveArrayLengthMethod since they only have an effect on the AST
787 // on ESSL >= 3.00, and the initializers that need to be deferred can only exist in ESSL < 3.00.
788 bool initializeLocalsAndGlobals =
789 (compileOptions & SH_INITIALIZE_UNINITIALIZED_LOCALS) && !IsOutputHLSL(getOutputType());
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300790 bool canUseLoopsToInitialize = !(compileOptions & SH_DONT_USE_LOOPS_TO_INITIALIZE_VARIABLES);
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200791 DeferGlobalInitializers(root, initializeLocalsAndGlobals, canUseLoopsToInitialize,
Olli Etuaho6f591c92018-09-21 11:20:47 +0300792 highPrecisionSupported, &mSymbolTable);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400793 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
794 {
795 return false;
796 }
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300797
Olli Etuahob5601eb2017-11-15 18:08:04 +0200798 if (initializeLocalsAndGlobals)
Olli Etuahod10cf692017-11-02 11:06:14 +0200799 {
800 // Initialize uninitialized local variables.
801 // In some cases initializing can generate extra statements in the parent block, such as
802 // when initializing nameless structs or initializing arrays in ESSL 1.00. In that case
803 // we need to first simplify loop conditions. We've already separated declarations
804 // earlier, which is also required. If we don't follow the Appendix A limitations, loop
805 // init statements can declare arrays or nameless structs and have multiple
806 // declarations.
807
808 if (!shouldRunLoopAndIndexingValidation(compileOptions))
809 {
810 SimplifyLoopConditions(root,
811 IntermNodePatternMatcher::kArrayDeclaration |
812 IntermNodePatternMatcher::kNamelessStructDeclaration,
Olli Etuaho68981eb2018-01-23 17:46:12 +0200813 &getSymbolTable());
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400814 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
815 {
816 return false;
817 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200818 }
Olli Etuaho661fc482017-10-16 12:17:05 +0300819
Olli Etuaho2c7f34c2017-10-09 17:18:02 +0300820 InitializeUninitializedLocals(root, getShaderVersion(), canUseLoopsToInitialize,
Olli Etuaho87cc90d2017-12-12 15:28:06 +0200821 highPrecisionSupported, &getSymbolTable());
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400822 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
823 {
824 return false;
825 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200826 }
827
828 if (getShaderType() == GL_VERTEX_SHADER && (compileOptions & SH_CLAMP_POINT_SIZE))
829 {
Olli Etuaho6f591c92018-09-21 11:20:47 +0300830 ClampPointSize(root, mResources.MaxPointSize, &getSymbolTable());
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400831 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
832 {
833 return false;
834 }
Olli Etuahod10cf692017-11-02 11:06:14 +0200835 }
836
Olli Etuaho0690e1a2017-12-21 20:51:38 +0200837 if (getShaderType() == GL_FRAGMENT_SHADER && (compileOptions & SH_CLAMP_FRAG_DEPTH))
838 {
839 ClampFragDepth(root, &getSymbolTable());
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400840 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
841 {
842 return false;
843 }
Olli Etuaho0690e1a2017-12-21 20:51:38 +0200844 }
845
Olli Etuaho983460e2018-05-02 17:57:39 +0300846 if (compileOptions & SH_REWRITE_REPEATED_ASSIGN_TO_SWIZZLED)
847 {
848 sh::RewriteRepeatedAssignToSwizzled(root);
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400849 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
850 {
851 return false;
852 }
Olli Etuaho983460e2018-05-02 17:57:39 +0300853 }
854
Olli Etuaho661fc482017-10-16 12:17:05 +0300855 if (compileOptions & SH_REWRITE_VECTOR_SCALAR_ARITHMETIC)
856 {
857 VectorizeVectorScalarArithmetic(root, &getSymbolTable());
Shahbaz Youssefi91d469a2018-08-27 10:47:25 -0400858 if (!ValidateAST(root, &mDiagnostics, mValidateASTOptions))
859 {
860 return false;
861 }
Olli Etuaho661fc482017-10-16 12:17:05 +0300862 }
863
Olli Etuahod10cf692017-11-02 11:06:14 +0200864 return true;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200865}
866
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800867bool TCompiler::compile(const char *const shaderStrings[],
868 size_t numStrings,
869 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200870{
Corentin Wallez28b65282016-06-16 07:24:50 -0700871#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
Olli Etuaho6f591c92018-09-21 11:20:47 +0300872 DumpFuzzerCase(shaderStrings, numStrings, mShaderType, mShaderSpec, mOutputType,
873 compileOptionsIn);
Corentin Wallez28b65282016-06-16 07:24:50 -0700874#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
875
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200876 if (numStrings == 0)
877 return true;
878
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800879 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700880
881 // Apply key workarounds.
882 if (shouldFlattenPragmaStdglInvariantAll())
883 {
884 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
885 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
886 }
887
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200888 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100889 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200890
891 if (root)
892 {
893 if (compileOptions & SH_INTERMEDIATE_TREE)
Olli Etuaho6f591c92018-09-21 11:20:47 +0300894 OutputTree(root, mInfoSink.info);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200895
896 if (compileOptions & SH_OBJECT_CODE)
Olli Etuaho89a69a02017-10-23 12:20:45 +0300897 {
898 PerformanceDiagnostics perfDiagnostics(&mDiagnostics);
899 translate(root, compileOptions, &perfDiagnostics);
900 }
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200901
Austin Eng7cf9cd22018-10-09 15:27:32 -0700902 if (mShaderType == GL_VERTEX_SHADER &&
903 IsExtensionEnabled(mExtensionBehavior, TExtension::ANGLE_multi_draw))
904 {
905 if ((compileOptions & SH_EMULATE_GL_DRAW_ID) != 0)
906 {
Jamie Madillb980c562018-11-27 11:34:27 -0500907 for (auto &uniform : mUniforms)
Austin Eng7cf9cd22018-10-09 15:27:32 -0700908 {
909 if (uniform.name == "angle_DrawID" && uniform.mappedName == "angle_DrawID")
910 {
911 uniform.name = "gl_DrawID";
912 break;
913 }
914 }
915 }
916 }
917
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200918 // The IntermNode tree doesn't need to be deleted here, since the
919 // memory will be freed in a big chunk by the PoolAllocator.
920 return true;
921 }
922 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000923}
924
Olli Etuaho6f591c92018-09-21 11:20:47 +0300925bool TCompiler::initBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000926{
Olli Etuaho28cb0362016-11-22 15:42:37 +0000927 if (resources.MaxDrawBuffers < 1)
928 {
929 return false;
930 }
931 if (resources.EXT_blend_func_extended && resources.MaxDualSourceDrawBuffers < 1)
932 {
933 return false;
934 }
935
Olli Etuaho6f591c92018-09-21 11:20:47 +0300936 mSymbolTable.initializeBuiltIns(mShaderType, mShaderSpec, resources);
Olli Etuaho5d69db12017-11-24 16:51:15 +0200937
Nicolas Capens49a88872013-06-20 09:54:03 -0400938 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000939}
940
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400941void TCompiler::setResourceString()
942{
Jonah Ryan-Davis1d35d512019-02-15 15:39:27 +0000943 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400944
945 // clang-format off
Olli Etuaho6f591c92018-09-21 11:20:47 +0300946 strstream << ":MaxVertexAttribs:" << mResources.MaxVertexAttribs
947 << ":MaxVertexUniformVectors:" << mResources.MaxVertexUniformVectors
948 << ":MaxVaryingVectors:" << mResources.MaxVaryingVectors
949 << ":MaxVertexTextureImageUnits:" << mResources.MaxVertexTextureImageUnits
950 << ":MaxCombinedTextureImageUnits:" << mResources.MaxCombinedTextureImageUnits
951 << ":MaxTextureImageUnits:" << mResources.MaxTextureImageUnits
952 << ":MaxFragmentUniformVectors:" << mResources.MaxFragmentUniformVectors
953 << ":MaxDrawBuffers:" << mResources.MaxDrawBuffers
954 << ":OES_standard_derivatives:" << mResources.OES_standard_derivatives
955 << ":OES_EGL_image_external:" << mResources.OES_EGL_image_external
956 << ":OES_EGL_image_external_essl3:" << mResources.OES_EGL_image_external_essl3
957 << ":NV_EGL_stream_consumer_external:" << mResources.NV_EGL_stream_consumer_external
958 << ":ARB_texture_rectangle:" << mResources.ARB_texture_rectangle
959 << ":EXT_draw_buffers:" << mResources.EXT_draw_buffers
960 << ":FragmentPrecisionHigh:" << mResources.FragmentPrecisionHigh
961 << ":MaxExpressionComplexity:" << mResources.MaxExpressionComplexity
962 << ":MaxCallStackDepth:" << mResources.MaxCallStackDepth
963 << ":MaxFunctionParameters:" << mResources.MaxFunctionParameters
964 << ":EXT_blend_func_extended:" << mResources.EXT_blend_func_extended
965 << ":EXT_frag_depth:" << mResources.EXT_frag_depth
966 << ":EXT_shader_texture_lod:" << mResources.EXT_shader_texture_lod
967 << ":EXT_shader_framebuffer_fetch:" << mResources.EXT_shader_framebuffer_fetch
968 << ":NV_shader_framebuffer_fetch:" << mResources.NV_shader_framebuffer_fetch
969 << ":ARM_shader_framebuffer_fetch:" << mResources.ARM_shader_framebuffer_fetch
970 << ":OVR_multiview:" << mResources.OVR_multiview
971 << ":EXT_YUV_target:" << mResources.EXT_YUV_target
972 << ":EXT_geometry_shader:" << mResources.EXT_geometry_shader
973 << ":MaxVertexOutputVectors:" << mResources.MaxVertexOutputVectors
974 << ":MaxFragmentInputVectors:" << mResources.MaxFragmentInputVectors
975 << ":MinProgramTexelOffset:" << mResources.MinProgramTexelOffset
976 << ":MaxProgramTexelOffset:" << mResources.MaxProgramTexelOffset
977 << ":MaxDualSourceDrawBuffers:" << mResources.MaxDualSourceDrawBuffers
978 << ":MaxViewsOVR:" << mResources.MaxViewsOVR
979 << ":NV_draw_buffers:" << mResources.NV_draw_buffers
980 << ":WEBGL_debug_shader_precision:" << mResources.WEBGL_debug_shader_precision
Austin Eng7cf9cd22018-10-09 15:27:32 -0700981 << ":ANGLE_multi_draw:" << mResources.ANGLE_multi_draw
Olli Etuaho6f591c92018-09-21 11:20:47 +0300982 << ":MinProgramTextureGatherOffset:" << mResources.MinProgramTextureGatherOffset
983 << ":MaxProgramTextureGatherOffset:" << mResources.MaxProgramTextureGatherOffset
984 << ":MaxImageUnits:" << mResources.MaxImageUnits
985 << ":MaxVertexImageUniforms:" << mResources.MaxVertexImageUniforms
986 << ":MaxFragmentImageUniforms:" << mResources.MaxFragmentImageUniforms
987 << ":MaxComputeImageUniforms:" << mResources.MaxComputeImageUniforms
988 << ":MaxCombinedImageUniforms:" << mResources.MaxCombinedImageUniforms
989 << ":MaxCombinedShaderOutputResources:" << mResources.MaxCombinedShaderOutputResources
990 << ":MaxComputeWorkGroupCountX:" << mResources.MaxComputeWorkGroupCount[0]
991 << ":MaxComputeWorkGroupCountY:" << mResources.MaxComputeWorkGroupCount[1]
992 << ":MaxComputeWorkGroupCountZ:" << mResources.MaxComputeWorkGroupCount[2]
993 << ":MaxComputeWorkGroupSizeX:" << mResources.MaxComputeWorkGroupSize[0]
994 << ":MaxComputeWorkGroupSizeY:" << mResources.MaxComputeWorkGroupSize[1]
995 << ":MaxComputeWorkGroupSizeZ:" << mResources.MaxComputeWorkGroupSize[2]
996 << ":MaxComputeUniformComponents:" << mResources.MaxComputeUniformComponents
997 << ":MaxComputeTextureImageUnits:" << mResources.MaxComputeTextureImageUnits
998 << ":MaxComputeAtomicCounters:" << mResources.MaxComputeAtomicCounters
999 << ":MaxComputeAtomicCounterBuffers:" << mResources.MaxComputeAtomicCounterBuffers
1000 << ":MaxVertexAtomicCounters:" << mResources.MaxVertexAtomicCounters
1001 << ":MaxFragmentAtomicCounters:" << mResources.MaxFragmentAtomicCounters
1002 << ":MaxCombinedAtomicCounters:" << mResources.MaxCombinedAtomicCounters
1003 << ":MaxAtomicCounterBindings:" << mResources.MaxAtomicCounterBindings
1004 << ":MaxVertexAtomicCounterBuffers:" << mResources.MaxVertexAtomicCounterBuffers
1005 << ":MaxFragmentAtomicCounterBuffers:" << mResources.MaxFragmentAtomicCounterBuffers
1006 << ":MaxCombinedAtomicCounterBuffers:" << mResources.MaxCombinedAtomicCounterBuffers
1007 << ":MaxAtomicCounterBufferSize:" << mResources.MaxAtomicCounterBufferSize
1008 << ":MaxGeometryUniformComponents:" << mResources.MaxGeometryUniformComponents
1009 << ":MaxGeometryUniformBlocks:" << mResources.MaxGeometryUniformBlocks
1010 << ":MaxGeometryInputComponents:" << mResources.MaxGeometryInputComponents
1011 << ":MaxGeometryOutputComponents:" << mResources.MaxGeometryOutputComponents
1012 << ":MaxGeometryOutputVertices:" << mResources.MaxGeometryOutputVertices
1013 << ":MaxGeometryTotalOutputComponents:" << mResources.MaxGeometryTotalOutputComponents
1014 << ":MaxGeometryTextureImageUnits:" << mResources.MaxGeometryTextureImageUnits
1015 << ":MaxGeometryAtomicCounterBuffers:" << mResources.MaxGeometryAtomicCounterBuffers
1016 << ":MaxGeometryAtomicCounters:" << mResources.MaxGeometryAtomicCounters
1017 << ":MaxGeometryShaderStorageBlocks:" << mResources.MaxGeometryShaderStorageBlocks
1018 << ":MaxGeometryShaderInvocations:" << mResources.MaxGeometryShaderInvocations
1019 << ":MaxGeometryImageUniforms:" << mResources.MaxGeometryImageUniforms;
Geoff Langb66a9092016-05-16 15:59:14 -04001020 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -04001021
Olli Etuaho6f591c92018-09-21 11:20:47 +03001022 mBuiltInResourcesString = strstream.str();
Shannon Woods2d76e5f2014-05-16 17:46:41 -04001023}
1024
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001025void TCompiler::collectInterfaceBlocks()
1026{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001027 ASSERT(mInterfaceBlocks.empty());
1028 mInterfaceBlocks.reserve(mUniformBlocks.size() + mShaderStorageBlocks.size() +
1029 mInBlocks.size());
1030 mInterfaceBlocks.insert(mInterfaceBlocks.end(), mUniformBlocks.begin(), mUniformBlocks.end());
1031 mInterfaceBlocks.insert(mInterfaceBlocks.end(), mShaderStorageBlocks.begin(),
1032 mShaderStorageBlocks.end());
1033 mInterfaceBlocks.insert(mInterfaceBlocks.end(), mInBlocks.begin(), mInBlocks.end());
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001034}
1035
alokp@chromium.org07620a52010-09-23 17:53:56 +00001036void TCompiler::clearResults()
1037{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001038 mArrayBoundsClamper.Cleanup();
1039 mInfoSink.info.erase();
1040 mInfoSink.obj.erase();
1041 mInfoSink.debug.erase();
Olli Etuaho77ba4082016-12-16 12:01:18 +00001042 mDiagnostics.resetErrorCount();
alokp@chromium.org07620a52010-09-23 17:53:56 +00001043
Olli Etuaho6f591c92018-09-21 11:20:47 +03001044 mAttributes.clear();
1045 mOutputVariables.clear();
1046 mUniforms.clear();
1047 mInputVaryings.clear();
1048 mOutputVaryings.clear();
1049 mInterfaceBlocks.clear();
1050 mUniformBlocks.clear();
1051 mShaderStorageBlocks.clear();
1052 mInBlocks.clear();
1053 mVariablesCollected = false;
Olli Etuahob12040c2017-06-27 14:20:45 +03001054 mGLPositionInitialized = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +00001055
Olli Etuaho09b04a22016-12-15 13:30:26 +00001056 mNumViews = -1;
1057
Shaob5cc1192017-07-06 10:47:20 +08001058 mGeometryShaderInputPrimitiveType = EptUndefined;
1059 mGeometryShaderOutputPrimitiveType = EptUndefined;
1060 mGeometryShaderInvocations = 0;
1061 mGeometryShaderMaxVertices = -1;
1062
Olli Etuaho6f591c92018-09-21 11:20:47 +03001063 mBuiltInFunctionEmulator.cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +00001064
Olli Etuaho6f591c92018-09-21 11:20:47 +03001065 mNameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001066
Jamie Madillb980c562018-11-27 11:34:27 -05001067 mSourcePath = nullptr;
Olli Etuaho5d69db12017-11-24 16:51:15 +02001068
Olli Etuaho6f591c92018-09-21 11:20:47 +03001069 mSymbolTable.clearCompilationResults();
alokp@chromium.org07620a52010-09-23 17:53:56 +00001070}
1071
Corentin Wallez71d147f2015-02-11 11:15:24 -08001072bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +00001073{
Corentin Wallez71d147f2015-02-11 11:15:24 -08001074 mCallDag.clear();
1075
Olli Etuaho77ba4082016-12-16 12:01:18 +00001076 switch (mCallDag.init(root, &mDiagnostics))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001077 {
Jamie Madillacb4b812016-11-07 13:50:29 -05001078 case CallDAG::INITDAG_SUCCESS:
1079 return true;
1080 case CallDAG::INITDAG_RECURSION:
Jamie Madillacb4b812016-11-07 13:50:29 -05001081 case CallDAG::INITDAG_UNDEFINED:
Olli Etuaho77ba4082016-12-16 12:01:18 +00001082 // Error message has already been written out.
1083 ASSERT(mDiagnostics.numErrors() > 0);
Jamie Madillacb4b812016-11-07 13:50:29 -05001084 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -08001085 }
1086
1087 UNREACHABLE();
1088 return true;
1089}
1090
1091bool TCompiler::checkCallDepth()
1092{
1093 std::vector<int> depths(mCallDag.size());
1094
1095 for (size_t i = 0; i < mCallDag.size(); i++)
1096 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001097 int depth = 0;
Jonah Ryan-Davisd4c09dd2019-01-29 11:22:54 -05001098 const CallDAG::Record &record = mCallDag.getRecordFromIndex(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -08001099
Jonah Ryan-Davisd4c09dd2019-01-29 11:22:54 -05001100 for (const int &calleeIndex : record.callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -08001101 {
1102 depth = std::max(depth, depths[calleeIndex] + 1);
1103 }
1104
1105 depths[i] = depth;
1106
Olli Etuaho6f591c92018-09-21 11:20:47 +03001107 if (depth >= mResources.MaxCallStackDepth)
Corentin Wallez71d147f2015-02-11 11:15:24 -08001108 {
1109 // Trace back the function chain to have a meaningful info log.
Jonah Ryan-Davis1d35d512019-02-15 15:39:27 +00001110 std::stringstream errorStream;
Olli Etuaho6f591c92018-09-21 11:20:47 +03001111 errorStream << "Call stack too deep (larger than " << mResources.MaxCallStackDepth
Olli Etuahoc33f1e82017-12-29 16:55:29 +02001112 << ") with the following call chain: "
1113 << record.node->getFunction()->name();
Corentin Wallez71d147f2015-02-11 11:15:24 -08001114
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001115 int currentFunction = static_cast<int>(i);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001116 int currentDepth = depth;
Corentin Wallez71d147f2015-02-11 11:15:24 -08001117
1118 while (currentFunction != -1)
1119 {
Olli Etuahoc33f1e82017-12-29 16:55:29 +02001120 errorStream
1121 << " -> "
1122 << mCallDag.getRecordFromIndex(currentFunction).node->getFunction()->name();
Corentin Wallez71d147f2015-02-11 11:15:24 -08001123
1124 int nextFunction = -1;
Jonah Ryan-Davisd4c09dd2019-01-29 11:22:54 -05001125 for (const int &calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
Corentin Wallez71d147f2015-02-11 11:15:24 -08001126 {
1127 if (depths[calleeIndex] == currentDepth - 1)
1128 {
1129 currentDepth--;
1130 nextFunction = calleeIndex;
1131 }
1132 }
1133
1134 currentFunction = nextFunction;
1135 }
1136
Olli Etuaho77ba4082016-12-16 12:01:18 +00001137 std::string errorStr = errorStream.str();
1138 mDiagnostics.globalError(errorStr.c_str());
1139
Corentin Wallez71d147f2015-02-11 11:15:24 -08001140 return false;
1141 }
1142 }
1143
1144 return true;
1145}
1146
1147bool TCompiler::tagUsedFunctions()
1148{
1149 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001150 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -08001151 {
Olli Etuahoc33f1e82017-12-29 16:55:29 +02001152 if (mCallDag.getRecordFromIndex(i).node->getFunction()->isMain())
Corentin Wallez71d147f2015-02-11 11:15:24 -08001153 {
1154 internalTagUsedFunction(i);
1155 return true;
1156 }
1157 }
1158
Olli Etuaho77ba4082016-12-16 12:01:18 +00001159 mDiagnostics.globalError("Missing main()");
Corentin Wallez71d147f2015-02-11 11:15:24 -08001160 return false;
1161}
1162
1163void TCompiler::internalTagUsedFunction(size_t index)
1164{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001165 if (mFunctionMetadata[index].used)
Corentin Wallez71d147f2015-02-11 11:15:24 -08001166 {
1167 return;
1168 }
1169
Olli Etuaho6f591c92018-09-21 11:20:47 +03001170 mFunctionMetadata[index].used = true;
Corentin Wallez71d147f2015-02-11 11:15:24 -08001171
1172 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
1173 {
1174 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +00001175 }
1176}
1177
Corentin Walleza094a8a2015-04-07 11:53:06 -07001178// A predicate for the stl that returns if a top-level node is unused
1179class TCompiler::UnusedPredicate
1180{
1181 public:
1182 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -05001183 : mCallDag(callDag), mMetadatas(metadatas)
Jamie Madillb980c562018-11-27 11:34:27 -05001184 {}
Corentin Walleza094a8a2015-04-07 11:53:06 -07001185
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001186 bool operator()(TIntermNode *node)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001187 {
Olli Etuaho16c745a2017-01-16 17:02:27 +00001188 const TIntermFunctionPrototype *asFunctionPrototype = node->getAsFunctionPrototypeNode();
1189 const TIntermFunctionDefinition *asFunctionDefinition = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -07001190
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001191 const TFunction *func = nullptr;
Olli Etuaho336b1472016-10-05 16:37:55 +01001192
Olli Etuaho16c745a2017-01-16 17:02:27 +00001193 if (asFunctionDefinition)
Olli Etuaho336b1472016-10-05 16:37:55 +01001194 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001195 func = asFunctionDefinition->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001196 }
Olli Etuaho16c745a2017-01-16 17:02:27 +00001197 else if (asFunctionPrototype)
Olli Etuaho336b1472016-10-05 16:37:55 +01001198 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001199 func = asFunctionPrototype->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001200 }
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001201 if (func == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001202 {
1203 return false;
1204 }
1205
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001206 size_t callDagIndex = mCallDag->findIndex(func->uniqueId());
Corentin Walleza094a8a2015-04-07 11:53:06 -07001207 if (callDagIndex == CallDAG::InvalidIndex)
1208 {
1209 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho16c745a2017-01-16 17:02:27 +00001210 ASSERT(asFunctionPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -07001211 return true;
1212 }
1213
1214 ASSERT(callDagIndex < mMetadatas->size());
1215 return !(*mMetadatas)[callDagIndex].used;
1216 }
1217
1218 private:
1219 const CallDAG *mCallDag;
1220 const std::vector<FunctionMetadata> *mMetadatas;
1221};
1222
Olli Etuahod10cf692017-11-02 11:06:14 +02001223void TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -07001224{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001225 UnusedPredicate isUnused(&mCallDag, &mFunctionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001226 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -07001227
1228 if (!sequence->empty())
1229 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001230 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused),
1231 sequence->end());
Corentin Wallezb081e782015-07-20 05:40:04 -07001232 }
Corentin Walleza094a8a2015-04-07 11:53:06 -07001233}
1234
Olli Etuahob4279202017-05-18 15:08:24 +03001235bool TCompiler::limitExpressionComplexity(TIntermBlock *root)
Jamie Madilleb1a0102013-07-08 13:31:38 -04001236{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001237 if (!IsASTDepthBelowLimit(root, mResources.MaxExpressionComplexity))
Jamie Madill6654bc92014-03-26 14:01:57 -04001238 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001239 mDiagnostics.globalError("Expression too complex.");
Jamie Madill6654bc92014-03-26 14:01:57 -04001240 return false;
1241 }
1242
Olli Etuaho6f591c92018-09-21 11:20:47 +03001243 if (!ValidateMaxParameters(root, mResources.MaxFunctionParameters))
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001244 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00001245 mDiagnostics.globalError("Function has too many parameters.");
Olli Etuaho19d1dc92016-03-08 17:18:46 +02001246 return false;
1247 }
1248
Jamie Madilleb1a0102013-07-08 13:31:38 -04001249 return true;
1250}
1251
Corentin Wallez1df16022016-10-27 08:16:56 -04001252bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
1253{
1254 return (compileOptions & SH_VARIABLES) != 0;
1255}
1256
1257bool TCompiler::wereVariablesCollected() const
1258{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001259 return mVariablesCollected;
Corentin Wallez1df16022016-10-27 08:16:56 -04001260}
1261
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001262void TCompiler::initializeGLPosition(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001263{
Zhenyao Mo72111912016-07-20 17:45:56 -07001264 InitVariableList list;
Olli Etuahoe7c28572017-10-23 16:29:33 +03001265 sh::ShaderVariable var(GL_FLOAT_VEC4);
Zhenyao Mo72111912016-07-20 17:45:56 -07001266 var.name = "gl_Position";
1267 list.push_back(var);
Olli Etuaho6f591c92018-09-21 11:20:47 +03001268 InitializeVariables(root, list, &mSymbolTable, mShaderVersion, mExtensionBehavior, false,
1269 false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001270}
1271
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001272void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermBlock *root)
Qin Jiajia7835b522016-10-08 11:20:17 +08001273{
1274 sh::InterfaceBlockList list;
1275
Jonah Ryan-Davisd4c09dd2019-01-29 11:22:54 -05001276 for (const sh::InterfaceBlock &block : mUniformBlocks)
Qin Jiajia7835b522016-10-08 11:20:17 +08001277 {
1278 if (!block.staticUse &&
Qin Jiajiaca68d982017-09-18 16:41:56 +08001279 (block.layout == sh::BLOCKLAYOUT_STD140 || block.layout == sh::BLOCKLAYOUT_SHARED))
Qin Jiajia7835b522016-10-08 11:20:17 +08001280 {
1281 list.push_back(block);
1282 }
1283 }
1284
Olli Etuaho6f591c92018-09-21 11:20:47 +03001285 sh::UseInterfaceBlockFields(root, list, mSymbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +08001286}
1287
Olli Etuaho9cbc07c2017-05-10 18:22:01 +03001288void TCompiler::initializeOutputVariables(TIntermBlock *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001289{
Zhenyao Mo72111912016-07-20 17:45:56 -07001290 InitVariableList list;
Olli Etuaho6f591c92018-09-21 11:20:47 +03001291 if (mShaderType == GL_VERTEX_SHADER || mShaderType == GL_GEOMETRY_SHADER_EXT)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001292 {
Jonah Ryan-Davisd4c09dd2019-01-29 11:22:54 -05001293 for (const sh::Varying &var : mOutputVaryings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001294 {
Zhenyao Mof9312682016-07-22 12:51:31 -07001295 list.push_back(var);
Olli Etuahob12040c2017-06-27 14:20:45 +03001296 if (var.name == "gl_Position")
1297 {
1298 ASSERT(!mGLPositionInitialized);
1299 mGLPositionInitialized = true;
1300 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001301 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001302 }
Zhenyao Mo72111912016-07-20 17:45:56 -07001303 else
1304 {
Olli Etuaho6f591c92018-09-21 11:20:47 +03001305 ASSERT(mShaderType == GL_FRAGMENT_SHADER);
Jonah Ryan-Davisd4c09dd2019-01-29 11:22:54 -05001306 for (const sh::OutputVariable &var : mOutputVariables)
Zhenyao Mo72111912016-07-20 17:45:56 -07001307 {
1308 list.push_back(var);
1309 }
1310 }
Olli Etuaho6f591c92018-09-21 11:20:47 +03001311 InitializeVariables(root, list, &mSymbolTable, mShaderVersion, mExtensionBehavior, false,
1312 false);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -08001313}
1314
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001315const TExtensionBehavior &TCompiler::getExtensionBehavior() const
zmo@google.com5601ea02011-06-10 18:23:25 +00001316{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001317 return mExtensionBehavior;
zmo@google.com5601ea02011-06-10 18:23:25 +00001318}
zmo@google.com32e97312011-08-24 01:03:11 +00001319
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02001320const char *TCompiler::getSourcePath() const
1321{
1322 return mSourcePath;
1323}
1324
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001325const ShBuiltInResources &TCompiler::getResources() const
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001326{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001327 return mResources;
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +00001328}
1329
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001330const ArrayBoundsClamper &TCompiler::getArrayBoundsClamper() const
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001331{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001332 return mArrayBoundsClamper;
daniel@transgaming.com4167cc92013-01-11 04:11:53 +00001333}
1334
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001335ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
1336{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001337 return mResources.ArrayIndexClampingStrategy;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001338}
1339
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001340const BuiltInFunctionEmulator &TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001341{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001342 return mBuiltInFunctionEmulator;
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +00001343}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001344
Qiankun Miao7ebb97f2016-09-08 18:01:50 +08001345void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001346{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001347 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
1348 {
Olli Etuaho6f591c92018-09-21 11:20:47 +03001349 TInfoSinkBase &sink = mInfoSink.obj;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001350 if (mPragma.stdgl.invariantAll)
1351 sink << "#pragma STDGL invariant(all)\n";
1352 }
1353}
1354
1355bool TCompiler::isVaryingDefined(const char *varyingName)
1356{
Olli Etuaho6f591c92018-09-21 11:20:47 +03001357 ASSERT(mVariablesCollected);
1358 for (size_t ii = 0; ii < mInputVaryings.size(); ++ii)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001359 {
Olli Etuaho6f591c92018-09-21 11:20:47 +03001360 if (mInputVaryings[ii].name == varyingName)
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001361 {
1362 return true;
1363 }
1364 }
Olli Etuaho6f591c92018-09-21 11:20:47 +03001365 for (size_t ii = 0; ii < mOutputVaryings.size(); ++ii)
Jiawei-Shaodf7d7c92017-07-31 09:34:04 +08001366 {
Olli Etuaho6f591c92018-09-21 11:20:47 +03001367 if (mOutputVaryings[ii].name == varyingName)
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001368 {
1369 return true;
1370 }
1371 }
1372
1373 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001374}
Jamie Madillacb4b812016-11-07 13:50:29 -05001375
1376} // namespace sh