blob: 7b8f485f8c83768ddc133c4398541e61354dd6b9 [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/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040020#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080021#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040022#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030023#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070024#include "compiler/translator/RegenerateStructNames.h"
Qiankun Miao705a9192016-08-29 10:05:27 +080025#include "compiler/translator/RemoveInvariantDeclaration.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"
Qin Jiajia7835b522016-10-08 11:20:17 +080030#include "compiler/translator/UseInterfaceBlockFields.h"
Geoff Lang17732822013-08-29 13:46:49 -040031#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020032#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040033#include "compiler/translator/ValidateOutputs.h"
34#include "compiler/translator/VariablePacker.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000035#include "third_party/compiler/ArrayBoundsClamper.h"
Corentin Wallez28b65282016-06-16 07:24:50 -070036
Jamie Madillacb4b812016-11-07 13:50:29 -050037namespace sh
38{
39
Corentin Wallez28b65282016-06-16 07:24:50 -070040namespace
41{
42
43#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
44void DumpFuzzerCase(char const *const *shaderStrings,
45 size_t numStrings,
46 uint32_t type,
47 uint32_t spec,
48 uint32_t output,
49 uint64_t options)
50{
51 static int fileIndex = 0;
52
53 std::ostringstream o;
54 o << "corpus/" << fileIndex++ << ".sample";
55 std::string s = o.str();
56
57 // Must match the input format of the fuzzer
58 FILE *f = fopen(s.c_str(), "w");
59 fwrite(&type, sizeof(type), 1, f);
60 fwrite(&spec, sizeof(spec), 1, f);
61 fwrite(&output, sizeof(output), 1, f);
62 fwrite(&options, sizeof(options), 1, f);
63
64 char zero[128 - 20] = {0};
65 fwrite(&zero, 128 - 20, 1, f);
66
67 for (size_t i = 0; i < numStrings; i++)
68 {
69 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
70 }
71 fwrite(&zero, 1, 1, f);
72
73 fclose(f);
74}
75#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
76} // anonymous namespace
77
Jamie Madill5508f392014-02-20 13:31:36 -050078bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000079{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080080 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000081}
82
Qingqing Dengad0d0792015-04-08 14:25:06 -070083bool IsGLSL130OrNewer(ShShaderOutput output)
84{
Jamie Madillacb4b812016-11-07 13:50:29 -050085 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
86 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
87 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT ||
88 output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
89 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070090}
91
Qiankun Miao705a9192016-08-29 10:05:27 +080092bool IsGLSL420OrNewer(ShShaderOutput output)
93{
Jamie Madillacb4b812016-11-07 13:50:29 -050094 return (output == SH_GLSL_420_CORE_OUTPUT || output == SH_GLSL_430_CORE_OUTPUT ||
95 output == SH_GLSL_440_CORE_OUTPUT || output == SH_GLSL_450_CORE_OUTPUT);
Qiankun Miao705a9192016-08-29 10:05:27 +080096}
97
Zhenyao Mob7bf7422016-11-08 14:44:05 -080098bool IsGLSL410OrOlder(ShShaderOutput output)
99{
100 return (output == SH_GLSL_130_OUTPUT || output == SH_GLSL_140_OUTPUT ||
101 output == SH_GLSL_150_CORE_OUTPUT || output == SH_GLSL_330_CORE_OUTPUT ||
102 output == SH_GLSL_400_CORE_OUTPUT || output == SH_GLSL_410_CORE_OUTPUT);
103}
104
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000105bool RemoveInvariant(sh::GLenum shaderType,
106 int shaderVersion,
107 ShShaderOutput outputType,
108 ShCompileOptions compileOptions)
109{
110 if ((compileOptions & SH_DONT_REMOVE_INVARIANT_FOR_FRAGMENT_INPUT) == 0 &&
111 shaderType == GL_FRAGMENT_SHADER && IsGLSL420OrNewer(outputType))
112 return true;
113
114 if ((compileOptions & SH_REMOVE_INVARIANT_AND_CENTROID_FOR_ESSL3) != 0 &&
Qiankun Miao41f9f672016-11-16 17:04:36 +0800115 shaderVersion >= 300 && shaderType == GL_VERTEX_SHADER)
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000116 return true;
117
118 return false;
119}
120
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700121size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500122{
He Yunchao29ab9ff2015-08-06 16:58:30 +0800123 // WebGL defines a max token length of 256, while ES2 leaves max token
Jamie Madill88f6e942014-02-19 10:27:53 -0500124 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700125 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -0500126 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500127 case SH_WEBGL_SPEC:
128 return 256;
129 default:
130 return 1024;
Jamie Madill88f6e942014-02-19 10:27:53 -0500131 }
132}
133
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000134namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700135
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800136class TScopedPoolAllocator
137{
138 public:
139 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
140 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400141 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000142 SetGlobalPoolAllocator(mAllocator);
143 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800144 ~TScopedPoolAllocator()
145 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000146 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400147 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000148 }
149
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800150 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000151 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400152};
153
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800154class TScopedSymbolTableLevel
155{
156 public:
157 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
158 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400159 ASSERT(mTable->atBuiltInLevel());
160 mTable->push();
161 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800162 ~TScopedSymbolTableLevel()
163 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400164 while (!mTable->atBuiltInLevel())
165 mTable->pop();
166 }
167
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800168 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400169 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000170};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700171
172int MapSpecToShaderVersion(ShShaderSpec spec)
173{
174 switch (spec)
175 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500176 case SH_GLES2_SPEC:
177 case SH_WEBGL_SPEC:
178 return 100;
179 case SH_GLES3_SPEC:
180 case SH_WEBGL2_SPEC:
181 return 300;
182 case SH_GLES3_1_SPEC:
183 case SH_WEBGL3_SPEC:
184 return 310;
185 default:
186 UNREACHABLE();
187 return 0;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700188 }
189}
190
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000191} // namespace
192
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800193TShHandleBase::TShHandleBase()
194{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000195 allocator.push();
196 SetGlobalPoolAllocator(&allocator);
197}
198
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800199TShHandleBase::~TShHandleBase()
200{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000201 SetGlobalPoolAllocator(NULL);
202 allocator.popAll();
203}
204
Jamie Madill183bde52014-07-02 15:31:19 -0400205TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700206 : variablesCollected(false),
207 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000208 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400209 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400210 maxUniformVectors(0),
211 maxExpressionComplexity(0),
212 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200213 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000214 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000215 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200216 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700217 mSourcePath(NULL),
Martin Radev802abe02016-08-04 17:48:32 +0300218 mComputeShaderLocalSizeDeclared(false),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700219 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000220{
Martin Radev802abe02016-08-04 17:48:32 +0300221 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000222}
223
224TCompiler::~TCompiler()
225{
226}
227
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800228bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300229{
230 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
231 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
232 // of ESSL 1.00 as in Appendix A of the spec).
233 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
234 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
235}
236
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000237bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000238{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000239 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400240 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000241 resources.MaxVertexUniformVectors :
242 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400243 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200244 maxCallStackDepth = resources.MaxCallStackDepth;
245 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400246
247 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000248
alokp@chromium.org07620a52010-09-23 17:53:56 +0000249 // Generate built-in symbol table.
250 if (!InitBuiltInSymbolTable(resources))
251 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000252 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000253 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000254
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000255 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
256 clampingStrategy = resources.ArrayIndexClampingStrategy;
257
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000258 hashFunction = resources.HashFunction;
259
alokp@chromium.org07620a52010-09-23 17:53:56 +0000260 return true;
261}
262
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100263TIntermBlock *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
264 size_t numStrings,
265 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000266{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200267 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
268}
269
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100270TIntermBlock *TCompiler::compileTreeImpl(const char *const shaderStrings[],
271 size_t numStrings,
272 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200273{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000274 clearResults();
275
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200276 ASSERT(numStrings > 0);
277 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000278
David Yen0fbd1282015-02-02 14:46:09 -0800279 // Reset the extension behavior for each compilation unit.
280 ResetExtensionBehavior(extensionBehavior);
281
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000282 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000283 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000284 if (compileOptions & SH_SOURCE_PATH)
285 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200286 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000287 ++firstSource;
288 }
289
Olli Etuahof119a262016-08-19 15:54:22 +0300290 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200291 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200292
Olli Etuahoa6996682015-10-12 14:32:30 +0300293 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400294 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000295
296 // We preserve symbols at the built-in level from compile-to-compile.
297 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400298 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000299
300 // Parse shader.
301 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400302 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
303 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000304
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000305 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700306 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
307 {
308 infoSink.info.prefix(EPrefixError);
309 infoSink.info << "unsupported shader version";
310 success = false;
311 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000312
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100313 TIntermBlock *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200314
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800315 if (success)
316 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700317 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700318 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700319
Martin Radev802abe02016-08-04 17:48:32 +0300320 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
321 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
322
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400323 root = parseContext.getTreeRoot();
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000324
Olli Etuahoa6996682015-10-12 14:32:30 +0300325 // Highp might have been auto-enabled based on shader version
326 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
327
Jamie Madill6654bc92014-03-26 14:01:57 -0400328 // Disallow expressions deemed too complex.
329 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
330 success = limitExpressionComplexity(root);
331
Corentin Wallez71d147f2015-02-11 11:15:24 -0800332 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000333 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800334 success = initCallDag(root);
335
336 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
337 success = checkCallDepth();
338
339 // Checks which functions are used and if "main" exists
340 if (success)
341 {
342 functionMetadata.clear();
343 functionMetadata.resize(mCallDag.size());
344 success = tagUsedFunctions();
345 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000346
Corentin Walleza094a8a2015-04-07 11:53:06 -0700347 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
348 success = pruneUnusedFunctions(root);
349
Olli Etuahoc6833112015-04-22 15:15:54 +0300350 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
351 if (success)
352 PruneEmptyDeclarations(root);
353
Jamie Madill183bde52014-07-02 15:31:19 -0400354 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400355 success = validateOutputs(root);
356
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300357 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000358 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000359
Jamie Madilld5696192016-10-06 11:09:24 -0400360 // Fail compilation if precision emulation not supported.
361 if (success && getResources().WEBGL_debug_shader_precision &&
362 getPragma().debugShaderPrecision)
363 {
364 if (!EmulatePrecision::SupportedInLanguage(outputType))
365 {
366 infoSink.info.prefix(EPrefixError);
367 infoSink.info << "Precision emulation not supported for this output type.";
368 success = false;
369 }
370 }
371
zmo@google.com32e97312011-08-24 01:03:11 +0000372 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200373 if (success)
374 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400375 // TODO(jmadill): Remove global pool allocator.
376 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200377 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400378 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000379 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200380 }
zmo@google.com32e97312011-08-24 01:03:11 +0000381
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000382 // Clamping uniform array bounds needs to happen after validateLimitations pass.
383 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
384 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
385
Ian Ewell924b7de2016-01-21 13:54:28 -0500386 // gl_Position is always written in compatibility output mode
387 if (success && shaderType == GL_VERTEX_SHADER &&
388 ((compileOptions & SH_INIT_GL_POSITION) ||
389 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800390 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400391
Qiankun Miao89dd8f32016-11-09 12:59:30 +0000392 if (success && RemoveInvariant(shaderType, shaderVersion, outputType, compileOptions))
Qiankun Miao705a9192016-08-29 10:05:27 +0800393 sh::RemoveInvariantDeclaration(root);
394
Corentin Wallezd4b50542015-09-28 12:19:26 -0700395 // This pass might emit short circuits so keep it before the short circuit unfolding
396 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
397 RewriteDoWhile(root, getTemporaryIndex());
398
Qiankun Miao09cfac62016-09-06 17:25:16 +0800399 if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION))
400 sh::AddAndTrueToLoopCondition(root);
401
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800402 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
403 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700404 UnfoldShortCircuitAST unfoldShortCircuit;
405 root->traverse(&unfoldShortCircuit);
406 unfoldShortCircuit.updateTree();
407 }
408
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300409 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
410 {
411 RemovePow(root);
412 }
413
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300414 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800415 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400416 collectVariables(root);
Qin Jiajia7835b522016-10-08 11:20:17 +0800417 if (compileOptions & SH_USE_UNUSED_STANDARD_SHARED_BLOCKS)
418 {
419 useAllMembersInUnusedStandardAndSharedBlocks(root);
420 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800421 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
422 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000423 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800424 if (!success)
425 {
Jamie Madill075edd82013-07-08 13:30:19 -0400426 infoSink.info.prefix(EPrefixError);
427 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000428 }
429 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700430 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
431 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300432 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700433 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000434 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000435
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700436 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
437 {
Olli Etuahob990b552016-10-27 12:29:17 +0100438 ScalarizeVecAndMatConstructorArgs(root, shaderType, fragmentPrecisionHigh,
439 &mTemporaryIndex);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700440 }
441
Zhenyao Moe740add2014-07-18 17:01:01 -0700442 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
443 {
444 RegenerateStructNames gen(symbolTable, shaderVersion);
445 root->traverse(&gen);
446 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300447
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700448 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
449 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
450 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
451 {
452 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
453 }
454
Olli Etuaho3d932d82016-04-12 11:10:30 +0300455 if (success)
456 {
457 DeferGlobalInitializers(root);
458 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000459 }
460
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700461 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200462 if (success)
463 return root;
464
465 return NULL;
466}
467
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800468bool TCompiler::compile(const char *const shaderStrings[],
469 size_t numStrings,
470 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200471{
Corentin Wallez28b65282016-06-16 07:24:50 -0700472#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
473 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
474#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
475
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200476 if (numStrings == 0)
477 return true;
478
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800479 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700480
481 // Apply key workarounds.
482 if (shouldFlattenPragmaStdglInvariantAll())
483 {
484 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
485 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
486 }
487
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200488 TScopedPoolAllocator scopedAlloc(&allocator);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100489 TIntermBlock *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200490
491 if (root)
492 {
493 if (compileOptions & SH_INTERMEDIATE_TREE)
494 TIntermediate::outputTree(root, infoSink.info);
495
496 if (compileOptions & SH_OBJECT_CODE)
497 translate(root, compileOptions);
498
499 // The IntermNode tree doesn't need to be deleted here, since the
500 // memory will be freed in a big chunk by the PoolAllocator.
501 return true;
502 }
503 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000504}
505
Nicolas Capens49a88872013-06-20 09:54:03 -0400506bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000507{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000508 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400509 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000510
Nicolas Capens49a88872013-06-20 09:54:03 -0400511 assert(symbolTable.isEmpty());
512 symbolTable.push(); // COMMON_BUILTINS
513 symbolTable.push(); // ESSL1_BUILTINS
514 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300515 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000516
Nicolas Capens49a88872013-06-20 09:54:03 -0400517 TPublicType integer;
Martin Radev2cc85b32016-08-05 16:22:53 +0300518 integer.initializeBasicType(EbtInt);
Nicolas Capens49a88872013-06-20 09:54:03 -0400519
520 TPublicType floatingPoint;
Martin Radev2cc85b32016-08-05 16:22:53 +0300521 floatingPoint.initializeBasicType(EbtFloat);
Nicolas Capens49a88872013-06-20 09:54:03 -0400522
Jamie Madillacb4b812016-11-07 13:50:29 -0500523 switch (shaderType)
Nicolas Capens49a88872013-06-20 09:54:03 -0400524 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500525 case GL_FRAGMENT_SHADER:
526 symbolTable.setDefaultPrecision(integer, EbpMedium);
527 break;
528 case GL_VERTEX_SHADER:
529 symbolTable.setDefaultPrecision(integer, EbpHigh);
530 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
531 break;
532 case GL_COMPUTE_SHADER:
533 symbolTable.setDefaultPrecision(integer, EbpHigh);
534 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
535 break;
536 default:
537 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400538 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200539 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400540 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200541 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
542 initSamplerDefaultPrecision(EbtSampler2D);
543 initSamplerDefaultPrecision(EbtSamplerCube);
544 // SamplerExternalOES is specified in the extension to have default precision.
545 initSamplerDefaultPrecision(EbtSamplerExternalOES);
546 // It isn't specified whether Sampler2DRect has default precision.
547 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400548
Jamie Madill1b452142013-07-12 14:51:11 -0400549 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400550
551 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
552
553 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000554}
555
Olli Etuaho183d7e22015-11-20 15:59:09 +0200556void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
557{
558 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
559 TPublicType sampler;
Martin Radev2cc85b32016-08-05 16:22:53 +0300560 sampler.initializeBasicType(samplerType);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200561 symbolTable.setDefaultPrecision(sampler, EbpLow);
562}
563
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400564void TCompiler::setResourceString()
565{
566 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400567
568 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400569 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
Jamie Madillacb4b812016-11-07 13:50:29 -0500570 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
571 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
572 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
573 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
574 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
575 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
576 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
577 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
578 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
579 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
580 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
581 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
582 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
583 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
584 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
585 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
586 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
587 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
588 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
589 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
590 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
591 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
592 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
593 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
594 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
595 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
596 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
597 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
598 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
599 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
600 << ":MaxImageUnits:" << compileResources.MaxImageUnits
601 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
602 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
603 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
604 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
605 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
606 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
607 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
608 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
609 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
610 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
611 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
612 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
613 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
614 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
615 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
616 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
617 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
618 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
619 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
620 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
621 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
622 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
623 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400624 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400625
626 builtInResourcesString = strstream.str();
627}
628
alokp@chromium.org07620a52010-09-23 17:53:56 +0000629void TCompiler::clearResults()
630{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000631 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000632 infoSink.info.erase();
633 infoSink.obj.erase();
634 infoSink.debug.erase();
635
Jamie Madilled27c722014-07-02 15:31:23 -0400636 attributes.clear();
637 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000638 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400639 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400640 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400641 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700642 variablesCollected = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000643
644 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000645
646 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200647
648 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700649 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000650}
651
Corentin Wallez71d147f2015-02-11 11:15:24 -0800652bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000653{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800654 mCallDag.clear();
655
656 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800657 {
Jamie Madillacb4b812016-11-07 13:50:29 -0500658 case CallDAG::INITDAG_SUCCESS:
659 return true;
660 case CallDAG::INITDAG_RECURSION:
661 infoSink.info.prefix(EPrefixError);
662 infoSink.info << "Function recursion detected";
663 return false;
664 case CallDAG::INITDAG_UNDEFINED:
665 infoSink.info.prefix(EPrefixError);
666 infoSink.info << "Unimplemented function detected";
667 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800668 }
669
670 UNREACHABLE();
671 return true;
672}
673
674bool TCompiler::checkCallDepth()
675{
676 std::vector<int> depths(mCallDag.size());
677
678 for (size_t i = 0; i < mCallDag.size(); i++)
679 {
680 int depth = 0;
681 auto &record = mCallDag.getRecordFromIndex(i);
682
683 for (auto &calleeIndex : record.callees)
684 {
685 depth = std::max(depth, depths[calleeIndex] + 1);
686 }
687
688 depths[i] = depth;
689
690 if (depth >= maxCallStackDepth)
691 {
692 // Trace back the function chain to have a meaningful info log.
693 infoSink.info.prefix(EPrefixError);
694 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
695 << ") with the following call chain: " << record.name;
696
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700697 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800698 int currentDepth = depth;
699
700 while (currentFunction != -1)
701 {
702 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
703
704 int nextFunction = -1;
705 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
706 {
707 if (depths[calleeIndex] == currentDepth - 1)
708 {
709 currentDepth--;
710 nextFunction = calleeIndex;
711 }
712 }
713
714 currentFunction = nextFunction;
715 }
716
717 return false;
718 }
719 }
720
721 return true;
722}
723
724bool TCompiler::tagUsedFunctions()
725{
726 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700727 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800728 {
729 if (mCallDag.getRecordFromIndex(i).name == "main(")
730 {
731 internalTagUsedFunction(i);
732 return true;
733 }
734 }
735
736 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200737 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800738 return false;
739}
740
741void TCompiler::internalTagUsedFunction(size_t index)
742{
743 if (functionMetadata[index].used)
744 {
745 return;
746 }
747
748 functionMetadata[index].used = true;
749
750 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
751 {
752 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000753 }
754}
755
Corentin Walleza094a8a2015-04-07 11:53:06 -0700756// A predicate for the stl that returns if a top-level node is unused
757class TCompiler::UnusedPredicate
758{
759 public:
760 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
Jamie Madillacb4b812016-11-07 13:50:29 -0500761 : mCallDag(callDag), mMetadatas(metadatas)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700762 {
763 }
764
765 bool operator ()(TIntermNode *node)
766 {
767 const TIntermAggregate *asAggregate = node->getAsAggregate();
Olli Etuaho336b1472016-10-05 16:37:55 +0100768 const TIntermFunctionDefinition *asFunction = node->getAsFunctionDefinition();
Corentin Walleza094a8a2015-04-07 11:53:06 -0700769
Olli Etuaho336b1472016-10-05 16:37:55 +0100770 const TFunctionSymbolInfo *functionInfo = nullptr;
771
772 if (asFunction)
773 {
774 functionInfo = asFunction->getFunctionSymbolInfo();
775 }
776 else if (asAggregate)
777 {
778 if (asAggregate->getOp() == EOpPrototype)
779 {
780 functionInfo = asAggregate->getFunctionSymbolInfo();
781 }
782 }
783 if (functionInfo == nullptr)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700784 {
785 return false;
786 }
787
Olli Etuaho336b1472016-10-05 16:37:55 +0100788 size_t callDagIndex = mCallDag->findIndex(functionInfo);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700789 if (callDagIndex == CallDAG::InvalidIndex)
790 {
791 // This happens only for unimplemented prototypes which are thus unused
Olli Etuaho336b1472016-10-05 16:37:55 +0100792 ASSERT(asAggregate && asAggregate->getOp() == EOpPrototype);
Corentin Walleza094a8a2015-04-07 11:53:06 -0700793 return true;
794 }
795
796 ASSERT(callDagIndex < mMetadatas->size());
797 return !(*mMetadatas)[callDagIndex].used;
798 }
799
800 private:
801 const CallDAG *mCallDag;
802 const std::vector<FunctionMetadata> *mMetadatas;
803};
804
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100805bool TCompiler::pruneUnusedFunctions(TIntermBlock *root)
Corentin Walleza094a8a2015-04-07 11:53:06 -0700806{
Corentin Walleza094a8a2015-04-07 11:53:06 -0700807 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +0100808 TIntermSequence *sequence = root->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700809
810 if (!sequence->empty())
811 {
812 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
813 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700814
815 return true;
816}
817
Jamie Madill05a80ce2013-06-20 11:55:49 -0400818bool TCompiler::validateOutputs(TIntermNode* root)
819{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300820 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400821 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300822 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400823}
824
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800825bool TCompiler::validateLimitations(TIntermNode* root)
826{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200827 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000828 root->traverse(&validate);
829 return validate.numErrors() == 0;
830}
831
Jamie Madilleb1a0102013-07-08 13:31:38 -0400832bool TCompiler::limitExpressionComplexity(TIntermNode* root)
833{
Jamie Madillacb4b812016-11-07 13:50:29 -0500834 TMaxDepthTraverser traverser(maxExpressionComplexity + 1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400835 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400836
837 if (traverser.getMaxDepth() > maxExpressionComplexity)
838 {
839 infoSink.info << "Expression too complex.";
840 return false;
841 }
842
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200843 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
844 {
845 infoSink.info << "Function has too many parameters.";
846 return false;
847 }
848
Jamie Madilleb1a0102013-07-08 13:31:38 -0400849 return true;
850}
851
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400852void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000853{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700854 if (!variablesCollected)
855 {
856 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
Jamie Madillacb4b812016-11-07 13:50:29 -0500857 &interfaceBlocks, hashFunction, symbolTable,
858 extensionBehavior);
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700859 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400860
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700861 // This is for enforcePackingRestriction().
862 sh::ExpandUniforms(uniforms, &expandedUniforms);
863 variablesCollected = true;
864 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000865}
zmo@google.comfd747b82011-04-23 01:30:07 +0000866
Corentin Wallez1df16022016-10-27 08:16:56 -0400867bool TCompiler::shouldCollectVariables(ShCompileOptions compileOptions)
868{
869 return (compileOptions & SH_VARIABLES) != 0;
870}
871
872bool TCompiler::wereVariablesCollected() const
873{
874 return variablesCollected;
875}
876
gman@chromium.org8d804792012-10-17 21:33:48 +0000877bool TCompiler::enforcePackingRestrictions()
878{
879 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400880 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000881}
882
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800883void TCompiler::initializeGLPosition(TIntermNode* root)
884{
Zhenyao Mo72111912016-07-20 17:45:56 -0700885 InitVariableList list;
886 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
887 var.name = "gl_Position";
888 list.push_back(var);
Zhenyao Mod7490962016-11-09 15:49:51 -0800889 InitializeVariables(root, list, symbolTable);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800890}
891
Qin Jiajia7835b522016-10-08 11:20:17 +0800892void TCompiler::useAllMembersInUnusedStandardAndSharedBlocks(TIntermNode *root)
893{
894 sh::InterfaceBlockList list;
895
896 for (auto block : interfaceBlocks)
897 {
898 if (!block.staticUse &&
899 (block.layout == sh::BLOCKLAYOUT_STANDARD || block.layout == sh::BLOCKLAYOUT_SHARED))
900 {
901 list.push_back(block);
902 }
903 }
904
Zhenyao Mod7490962016-11-09 15:49:51 -0800905 sh::UseInterfaceBlockFields(root, list, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +0800906}
907
Olli Etuaho27776e32016-07-22 14:00:56 +0300908void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800909{
Zhenyao Mo72111912016-07-20 17:45:56 -0700910 InitVariableList list;
911 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800912 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700913 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800914 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700915 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800916 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800917 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700918 else
919 {
920 ASSERT(shaderType == GL_FRAGMENT_SHADER);
921 for (auto var : outputVariables)
922 {
923 list.push_back(var);
924 }
925 }
Zhenyao Mod7490962016-11-09 15:49:51 -0800926 InitializeVariables(root, list, symbolTable);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800927}
928
zmo@google.com5601ea02011-06-10 18:23:25 +0000929const TExtensionBehavior& TCompiler::getExtensionBehavior() const
930{
931 return extensionBehavior;
932}
zmo@google.com32e97312011-08-24 01:03:11 +0000933
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200934const char *TCompiler::getSourcePath() const
935{
936 return mSourcePath;
937}
938
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000939const ShBuiltInResources& TCompiler::getResources() const
940{
941 return compileResources;
942}
943
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000944const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
945{
946 return arrayBoundsClamper;
947}
948
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000949ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
950{
951 return clampingStrategy;
952}
953
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200954const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000955{
956 return builtInFunctionEmulator;
957}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700958
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800959void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700960{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700961 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
962 {
963 TInfoSinkBase &sink = infoSink.obj;
964 if (mPragma.stdgl.invariantAll)
965 sink << "#pragma STDGL invariant(all)\n";
966 }
967}
968
969bool TCompiler::isVaryingDefined(const char *varyingName)
970{
971 ASSERT(variablesCollected);
972 for (size_t ii = 0; ii < varyings.size(); ++ii)
973 {
974 if (varyings[ii].name == varyingName)
975 {
976 return true;
977 }
978 }
979
980 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700981}
Jamie Madillacb4b812016-11-07 13:50:29 -0500982
983} // namespace sh