blob: 547c09b1c461362fae426796fab3b7b91a3d030d [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
Jamie Madill88f6e942014-02-19 10:27:53 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org07620a52010-09-23 17:53:56 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Corentin Wallez8b28a8b2016-09-15 19:47:56 -04007#include "compiler/translator/Compiler.h"
8
9#include <sstream>
10
11#include "angle_gl.h"
12#include "common/utilities.h"
Qiankun Miao09cfac62016-09-06 17:25:16 +080013#include "compiler/translator/AddAndTrueToLoopCondition.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -080015#include "compiler/translator/CallDAG.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030016#include "compiler/translator/DeferGlobalInitializers.h"
Zhenyao Mo4e94fea2016-08-09 14:31:37 -070017#include "compiler/translator/EmulateGLFragColorBroadcast.h"
Jamie Madilld5696192016-10-06 11:09:24 -040018#include "compiler/translator/EmulatePrecision.h"
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/ForLoopUnroll.h"
20#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040021#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080022#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040023#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030024#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070025#include "compiler/translator/RegenerateStructNames.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030026#include "compiler/translator/RemovePow.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070027#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070028#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070029#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040030#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020031#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040032#include "compiler/translator/ValidateOutputs.h"
33#include "compiler/translator/VariablePacker.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000034#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070035
36namespace
37{
38
39#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
40void DumpFuzzerCase(char const *const *shaderStrings,
41 size_t numStrings,
42 uint32_t type,
43 uint32_t spec,
44 uint32_t output,
45 uint64_t options)
46{
47 static int fileIndex = 0;
48
49 std::ostringstream o;
50 o << "corpus/" << fileIndex++ << ".sample";
51 std::string s = o.str();
52
53 // Must match the input format of the fuzzer
54 FILE *f = fopen(s.c_str(), "w");
55 fwrite(&type, sizeof(type), 1, f);
56 fwrite(&spec, sizeof(spec), 1, f);
57 fwrite(&output, sizeof(output), 1, f);
58 fwrite(&options, sizeof(options), 1, f);
59
60 char zero[128 - 20] = {0};
61 fwrite(&zero, 128 - 20, 1, f);
62
63 for (size_t i = 0; i < numStrings; i++)
64 {
65 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
66 }
67 fwrite(&zero, 1, 1, f);
68
69 fclose(f);
70}
71#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
72} // anonymous namespace
73
Jamie Madill5508f392014-02-20 13:31:36 -050074bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000075{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080076 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000077}
78
Qingqing Dengad0d0792015-04-08 14:25:06 -070079bool IsGLSL130OrNewer(ShShaderOutput output)
80{
81 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070082 output == SH_GLSL_140_OUTPUT ||
83 output == SH_GLSL_150_CORE_OUTPUT ||
84 output == SH_GLSL_330_CORE_OUTPUT ||
85 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070086 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070087 output == SH_GLSL_420_CORE_OUTPUT ||
88 output == SH_GLSL_430_CORE_OUTPUT ||
89 output == SH_GLSL_440_CORE_OUTPUT ||
90 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070091}
92
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070093size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050094{
Jamie Madill88f6e942014-02-19 10:27:53 -050095 // WebGL defines a max token legnth of 256, while ES2 leaves max token
96 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070097 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050098 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070099 case SH_WEBGL_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -0500100 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700101 default:
Jamie Madill88f6e942014-02-19 10:27:53 -0500102 return 1024;
103 }
104}
105
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000106namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700107
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800108class TScopedPoolAllocator
109{
110 public:
111 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
112 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400113 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000114 SetGlobalPoolAllocator(mAllocator);
115 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800116 ~TScopedPoolAllocator()
117 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000118 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400119 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000120 }
121
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800122 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000123 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400124};
125
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800126class TScopedSymbolTableLevel
127{
128 public:
129 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
130 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400131 ASSERT(mTable->atBuiltInLevel());
132 mTable->push();
133 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800134 ~TScopedSymbolTableLevel()
135 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400136 while (!mTable->atBuiltInLevel())
137 mTable->pop();
138 }
139
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800140 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400141 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000142};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700143
144int MapSpecToShaderVersion(ShShaderSpec spec)
145{
146 switch (spec)
147 {
148 case SH_GLES2_SPEC:
149 case SH_WEBGL_SPEC:
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700150 return 100;
151 case SH_GLES3_SPEC:
152 case SH_WEBGL2_SPEC:
153 return 300;
Martin Radev1be913c2016-07-11 17:59:16 +0300154 case SH_GLES3_1_SPEC:
155 case SH_WEBGL3_SPEC:
156 return 310;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700157 default:
158 UNREACHABLE();
159 return 0;
160 }
161}
162
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000163} // namespace
164
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800165TShHandleBase::TShHandleBase()
166{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000167 allocator.push();
168 SetGlobalPoolAllocator(&allocator);
169}
170
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800171TShHandleBase::~TShHandleBase()
172{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000173 SetGlobalPoolAllocator(NULL);
174 allocator.popAll();
175}
176
Jamie Madill183bde52014-07-02 15:31:19 -0400177TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700178 : variablesCollected(false),
179 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000180 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400181 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400182 maxUniformVectors(0),
183 maxExpressionComplexity(0),
184 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200185 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000186 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000187 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200188 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700189 mSourcePath(NULL),
Martin Radev802abe02016-08-04 17:48:32 +0300190 mComputeShaderLocalSizeDeclared(false),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700191 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000192{
Martin Radev802abe02016-08-04 17:48:32 +0300193 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000194}
195
196TCompiler::~TCompiler()
197{
198}
199
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800200bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300201{
202 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
203 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
204 // of ESSL 1.00 as in Appendix A of the spec).
205 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
206 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
207}
208
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000209bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000210{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000211 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400212 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000213 resources.MaxVertexUniformVectors :
214 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400215 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200216 maxCallStackDepth = resources.MaxCallStackDepth;
217 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400218
219 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000220
alokp@chromium.org07620a52010-09-23 17:53:56 +0000221 // Generate built-in symbol table.
222 if (!InitBuiltInSymbolTable(resources))
223 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000224 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000225 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000226
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000227 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
228 clampingStrategy = resources.ArrayIndexClampingStrategy;
229
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000230 hashFunction = resources.HashFunction;
231
alokp@chromium.org07620a52010-09-23 17:53:56 +0000232 return true;
233}
234
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800235TIntermNode *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
236 size_t numStrings,
237 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000238{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200239 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
240}
241
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300242TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
243 size_t numStrings,
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800244 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200245{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000246 clearResults();
247
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200248 ASSERT(numStrings > 0);
249 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000250
David Yen0fbd1282015-02-02 14:46:09 -0800251 // Reset the extension behavior for each compilation unit.
252 ResetExtensionBehavior(extensionBehavior);
253
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000254 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000255 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000256 if (compileOptions & SH_SOURCE_PATH)
257 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200258 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000259 ++firstSource;
260 }
261
Olli Etuahof119a262016-08-19 15:54:22 +0300262 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200263 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200264
Olli Etuahoa6996682015-10-12 14:32:30 +0300265 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400266 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000267
268 // We preserve symbols at the built-in level from compile-to-compile.
269 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400270 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000271
272 // Parse shader.
273 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400274 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
275 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000276
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000277 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700278 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
279 {
280 infoSink.info.prefix(EPrefixError);
281 infoSink.info << "unsupported shader version";
282 success = false;
283 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000284
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400285 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200286
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800287 if (success)
288 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700289 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700290 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700291
Martin Radev802abe02016-08-04 17:48:32 +0300292 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
293 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
294
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400295 root = parseContext.getTreeRoot();
Olli Etuahof119a262016-08-19 15:54:22 +0300296 root = TIntermediate::PostProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000297
Olli Etuahoa6996682015-10-12 14:32:30 +0300298 // Highp might have been auto-enabled based on shader version
299 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
300
Jamie Madill6654bc92014-03-26 14:01:57 -0400301 // Disallow expressions deemed too complex.
302 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
303 success = limitExpressionComplexity(root);
304
Corentin Wallez71d147f2015-02-11 11:15:24 -0800305 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000306 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800307 success = initCallDag(root);
308
309 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
310 success = checkCallDepth();
311
312 // Checks which functions are used and if "main" exists
313 if (success)
314 {
315 functionMetadata.clear();
316 functionMetadata.resize(mCallDag.size());
317 success = tagUsedFunctions();
318 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000319
Corentin Walleza094a8a2015-04-07 11:53:06 -0700320 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
321 success = pruneUnusedFunctions(root);
322
Olli Etuahoc6833112015-04-22 15:15:54 +0300323 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
324 if (success)
325 PruneEmptyDeclarations(root);
326
Jamie Madill183bde52014-07-02 15:31:19 -0400327 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400328 success = validateOutputs(root);
329
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300330 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000331 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000332
Jamie Madilld5696192016-10-06 11:09:24 -0400333 // Fail compilation if precision emulation not supported.
334 if (success && getResources().WEBGL_debug_shader_precision &&
335 getPragma().debugShaderPrecision)
336 {
337 if (!EmulatePrecision::SupportedInLanguage(outputType))
338 {
339 infoSink.info.prefix(EPrefixError);
340 infoSink.info << "Precision emulation not supported for this output type.";
341 success = false;
342 }
343 }
344
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000345 // Unroll for-loop markup needs to happen after validateLimitations pass.
346 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800347 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200348 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
349 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800350 root->traverse(&marker);
351 }
352 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800353 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200354 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
355 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800356 root->traverse(&marker);
357 if (marker.samplerArrayIndexIsFloatLoopIndex())
358 {
359 infoSink.info.prefix(EPrefixError);
360 infoSink.info << "sampler array index is float loop index";
361 success = false;
362 }
363 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000364
zmo@google.com32e97312011-08-24 01:03:11 +0000365 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200366 if (success)
367 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400368 // TODO(jmadill): Remove global pool allocator.
369 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200370 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400371 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000372 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200373 }
zmo@google.com32e97312011-08-24 01:03:11 +0000374
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000375 // Clamping uniform array bounds needs to happen after validateLimitations pass.
376 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
377 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
378
Ian Ewell924b7de2016-01-21 13:54:28 -0500379 // gl_Position is always written in compatibility output mode
380 if (success && shaderType == GL_VERTEX_SHADER &&
381 ((compileOptions & SH_INIT_GL_POSITION) ||
382 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800383 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400384
Corentin Wallezd4b50542015-09-28 12:19:26 -0700385 // This pass might emit short circuits so keep it before the short circuit unfolding
386 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
387 RewriteDoWhile(root, getTemporaryIndex());
388
Qiankun Miao09cfac62016-09-06 17:25:16 +0800389 if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION))
390 sh::AddAndTrueToLoopCondition(root);
391
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800392 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
393 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700394 UnfoldShortCircuitAST unfoldShortCircuit;
395 root->traverse(&unfoldShortCircuit);
396 unfoldShortCircuit.updateTree();
397 }
398
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300399 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
400 {
401 RemovePow(root);
402 }
403
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300404 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800405 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400406 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800407 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
408 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000409 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800410 if (!success)
411 {
Jamie Madill075edd82013-07-08 13:30:19 -0400412 infoSink.info.prefix(EPrefixError);
413 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000414 }
415 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700416 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
417 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300418 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700419 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000420 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000421
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700422 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
423 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700424 ScalarizeVecAndMatConstructorArgs scalarizer(
425 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700426 root->traverse(&scalarizer);
427 }
428
Zhenyao Moe740add2014-07-18 17:01:01 -0700429 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
430 {
431 RegenerateStructNames gen(symbolTable, shaderVersion);
432 root->traverse(&gen);
433 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300434
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700435 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
436 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
437 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
438 {
439 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
440 }
441
Olli Etuaho3d932d82016-04-12 11:10:30 +0300442 if (success)
443 {
444 DeferGlobalInitializers(root);
445 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000446 }
447
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700448 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200449 if (success)
450 return root;
451
452 return NULL;
453}
454
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800455bool TCompiler::compile(const char *const shaderStrings[],
456 size_t numStrings,
457 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200458{
Corentin Wallez28b65282016-06-16 07:24:50 -0700459#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
460 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
461#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
462
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200463 if (numStrings == 0)
464 return true;
465
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800466 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700467
468 // Apply key workarounds.
469 if (shouldFlattenPragmaStdglInvariantAll())
470 {
471 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
472 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
473 }
474
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200475 TScopedPoolAllocator scopedAlloc(&allocator);
476 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
477
478 if (root)
479 {
480 if (compileOptions & SH_INTERMEDIATE_TREE)
481 TIntermediate::outputTree(root, infoSink.info);
482
483 if (compileOptions & SH_OBJECT_CODE)
484 translate(root, compileOptions);
485
486 // The IntermNode tree doesn't need to be deleted here, since the
487 // memory will be freed in a big chunk by the PoolAllocator.
488 return true;
489 }
490 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000491}
492
Nicolas Capens49a88872013-06-20 09:54:03 -0400493bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000494{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000495 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400496 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000497
Nicolas Capens49a88872013-06-20 09:54:03 -0400498 assert(symbolTable.isEmpty());
499 symbolTable.push(); // COMMON_BUILTINS
500 symbolTable.push(); // ESSL1_BUILTINS
501 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300502 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000503
Nicolas Capens49a88872013-06-20 09:54:03 -0400504 TPublicType integer;
Martin Radev4a9cd802016-09-01 16:51:51 +0300505 integer.setBasicType(EbtInt);
506 integer.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400507 integer.array = false;
508
509 TPublicType floatingPoint;
Martin Radev4a9cd802016-09-01 16:51:51 +0300510 floatingPoint.setBasicType(EbtFloat);
511 floatingPoint.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400512 floatingPoint.array = false;
513
514 switch(shaderType)
515 {
Jamie Madill183bde52014-07-02 15:31:19 -0400516 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400517 symbolTable.setDefaultPrecision(integer, EbpMedium);
518 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400519 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400520 symbolTable.setDefaultPrecision(integer, EbpHigh);
521 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
522 break;
Martin Radev802abe02016-08-04 17:48:32 +0300523 case GL_COMPUTE_SHADER:
524 symbolTable.setDefaultPrecision(integer, EbpHigh);
525 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
526 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800527 default:
528 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400529 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200530 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400531 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200532 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
533 initSamplerDefaultPrecision(EbtSampler2D);
534 initSamplerDefaultPrecision(EbtSamplerCube);
535 // SamplerExternalOES is specified in the extension to have default precision.
536 initSamplerDefaultPrecision(EbtSamplerExternalOES);
537 // It isn't specified whether Sampler2DRect has default precision.
538 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400539
Jamie Madill1b452142013-07-12 14:51:11 -0400540 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400541
542 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
543
544 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000545}
546
Olli Etuaho183d7e22015-11-20 15:59:09 +0200547void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
548{
549 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
550 TPublicType sampler;
Martin Radev4a9cd802016-09-01 16:51:51 +0300551 sampler.initializeSizeForScalarTypes();
552 sampler.setBasicType(samplerType);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200553 sampler.array = false;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200554 symbolTable.setDefaultPrecision(sampler, EbpLow);
555}
556
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400557void TCompiler::setResourceString()
558{
559 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400560
561 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400562 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
563 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
564 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
565 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
566 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
567 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
568 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
569 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
570 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
571 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400572 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
573 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400574 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
575 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
576 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
577 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
578 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200579 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300580 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400581 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
582 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100583 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
584 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
585 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400586 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
587 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
588 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300589 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300590 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200591 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
Martin Radeve93d24e2016-07-28 12:06:05 +0300592 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
593 << ":MaxImageUnits:" << compileResources.MaxImageUnits
594 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
595 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
596 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
597 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
598 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
599 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
600 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
601 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
602 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
603 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
604 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
605 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
606 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
607 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
608 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
609 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
610 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
611 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
612 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
613 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
614 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
615 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
616 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400617 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400618
619 builtInResourcesString = strstream.str();
620}
621
alokp@chromium.org07620a52010-09-23 17:53:56 +0000622void TCompiler::clearResults()
623{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000624 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000625 infoSink.info.erase();
626 infoSink.obj.erase();
627 infoSink.debug.erase();
628
Jamie Madilled27c722014-07-02 15:31:23 -0400629 attributes.clear();
630 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000631 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400632 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400633 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400634 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700635 variablesCollected = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000636
637 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000638
639 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200640
641 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700642 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000643}
644
Corentin Wallez71d147f2015-02-11 11:15:24 -0800645bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000646{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800647 mCallDag.clear();
648
649 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800650 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800651 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800652 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800653 case CallDAG::INITDAG_RECURSION:
654 infoSink.info.prefix(EPrefixError);
655 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800656 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800657 case CallDAG::INITDAG_UNDEFINED:
658 infoSink.info.prefix(EPrefixError);
659 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800660 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800661 }
662
663 UNREACHABLE();
664 return true;
665}
666
667bool TCompiler::checkCallDepth()
668{
669 std::vector<int> depths(mCallDag.size());
670
671 for (size_t i = 0; i < mCallDag.size(); i++)
672 {
673 int depth = 0;
674 auto &record = mCallDag.getRecordFromIndex(i);
675
676 for (auto &calleeIndex : record.callees)
677 {
678 depth = std::max(depth, depths[calleeIndex] + 1);
679 }
680
681 depths[i] = depth;
682
683 if (depth >= maxCallStackDepth)
684 {
685 // Trace back the function chain to have a meaningful info log.
686 infoSink.info.prefix(EPrefixError);
687 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
688 << ") with the following call chain: " << record.name;
689
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700690 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800691 int currentDepth = depth;
692
693 while (currentFunction != -1)
694 {
695 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
696
697 int nextFunction = -1;
698 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
699 {
700 if (depths[calleeIndex] == currentDepth - 1)
701 {
702 currentDepth--;
703 nextFunction = calleeIndex;
704 }
705 }
706
707 currentFunction = nextFunction;
708 }
709
710 return false;
711 }
712 }
713
714 return true;
715}
716
717bool TCompiler::tagUsedFunctions()
718{
719 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700720 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800721 {
722 if (mCallDag.getRecordFromIndex(i).name == "main(")
723 {
724 internalTagUsedFunction(i);
725 return true;
726 }
727 }
728
729 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200730 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800731 return false;
732}
733
734void TCompiler::internalTagUsedFunction(size_t index)
735{
736 if (functionMetadata[index].used)
737 {
738 return;
739 }
740
741 functionMetadata[index].used = true;
742
743 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
744 {
745 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000746 }
747}
748
Corentin Walleza094a8a2015-04-07 11:53:06 -0700749// A predicate for the stl that returns if a top-level node is unused
750class TCompiler::UnusedPredicate
751{
752 public:
753 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
754 : mCallDag(callDag),
755 mMetadatas(metadatas)
756 {
757 }
758
759 bool operator ()(TIntermNode *node)
760 {
761 const TIntermAggregate *asAggregate = node->getAsAggregate();
762
763 if (asAggregate == nullptr)
764 {
765 return false;
766 }
767
768 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
769 {
770 return false;
771 }
772
773 size_t callDagIndex = mCallDag->findIndex(asAggregate);
774 if (callDagIndex == CallDAG::InvalidIndex)
775 {
776 // This happens only for unimplemented prototypes which are thus unused
777 ASSERT(asAggregate->getOp() == EOpPrototype);
778 return true;
779 }
780
781 ASSERT(callDagIndex < mMetadatas->size());
782 return !(*mMetadatas)[callDagIndex].used;
783 }
784
785 private:
786 const CallDAG *mCallDag;
787 const std::vector<FunctionMetadata> *mMetadatas;
788};
789
790bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
791{
792 TIntermAggregate *rootNode = root->getAsAggregate();
793 ASSERT(rootNode != nullptr);
794
795 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
796 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700797
798 if (!sequence->empty())
799 {
800 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
801 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700802
803 return true;
804}
805
Jamie Madill05a80ce2013-06-20 11:55:49 -0400806bool TCompiler::validateOutputs(TIntermNode* root)
807{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300808 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400809 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300810 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400811}
812
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800813bool TCompiler::validateLimitations(TIntermNode* root)
814{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200815 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000816 root->traverse(&validate);
817 return validate.numErrors() == 0;
818}
819
Jamie Madilleb1a0102013-07-08 13:31:38 -0400820bool TCompiler::limitExpressionComplexity(TIntermNode* root)
821{
Jamie Madill6654bc92014-03-26 14:01:57 -0400822 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400823 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400824
825 if (traverser.getMaxDepth() > maxExpressionComplexity)
826 {
827 infoSink.info << "Expression too complex.";
828 return false;
829 }
830
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200831 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
832 {
833 infoSink.info << "Function has too many parameters.";
834 return false;
835 }
836
Jamie Madilleb1a0102013-07-08 13:31:38 -0400837 return true;
838}
839
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400840void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000841{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700842 if (!variablesCollected)
843 {
844 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
845 &interfaceBlocks, hashFunction, symbolTable, extensionBehavior);
846 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400847
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700848 // This is for enforcePackingRestriction().
849 sh::ExpandUniforms(uniforms, &expandedUniforms);
850 variablesCollected = true;
851 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000852}
zmo@google.comfd747b82011-04-23 01:30:07 +0000853
gman@chromium.org8d804792012-10-17 21:33:48 +0000854bool TCompiler::enforcePackingRestrictions()
855{
856 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400857 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000858}
859
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800860void TCompiler::initializeGLPosition(TIntermNode* root)
861{
Zhenyao Mo72111912016-07-20 17:45:56 -0700862 InitVariableList list;
863 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
864 var.name = "gl_Position";
865 list.push_back(var);
866 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800867}
868
Olli Etuaho27776e32016-07-22 14:00:56 +0300869void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800870{
Zhenyao Mo72111912016-07-20 17:45:56 -0700871 InitVariableList list;
872 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800873 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700874 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800875 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700876 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800877 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800878 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700879 else
880 {
881 ASSERT(shaderType == GL_FRAGMENT_SHADER);
882 for (auto var : outputVariables)
883 {
884 list.push_back(var);
885 }
886 }
887 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800888}
889
zmo@google.com5601ea02011-06-10 18:23:25 +0000890const TExtensionBehavior& TCompiler::getExtensionBehavior() const
891{
892 return extensionBehavior;
893}
zmo@google.com32e97312011-08-24 01:03:11 +0000894
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200895const char *TCompiler::getSourcePath() const
896{
897 return mSourcePath;
898}
899
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000900const ShBuiltInResources& TCompiler::getResources() const
901{
902 return compileResources;
903}
904
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000905const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
906{
907 return arrayBoundsClamper;
908}
909
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000910ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
911{
912 return clampingStrategy;
913}
914
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200915const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000916{
917 return builtInFunctionEmulator;
918}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700919
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800920void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700921{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700922 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
923 {
924 TInfoSinkBase &sink = infoSink.obj;
925 if (mPragma.stdgl.invariantAll)
926 sink << "#pragma STDGL invariant(all)\n";
927 }
928}
929
930bool TCompiler::isVaryingDefined(const char *varyingName)
931{
932 ASSERT(variablesCollected);
933 for (size_t ii = 0; ii < varyings.size(); ++ii)
934 {
935 if (varyings[ii].name == varyingName)
936 {
937 return true;
938 }
939 }
940
941 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700942}