blob: e256f878341d5d4845ddd5a6c65432bb705e3056 [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
Qiankun Miao09cfac62016-09-06 17:25:16 +08007#include "compiler/translator/AddAndTrueToLoopCondition.h"
Dmitry Skiba01971112015-07-10 14:54:00 -04008#include "compiler/translator/Cache.h"
Jamie Madilld4a3a312014-06-25 16:04:56 -04009#include "compiler/translator/Compiler.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -080010#include "compiler/translator/CallDAG.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030011#include "compiler/translator/DeferGlobalInitializers.h"
Zhenyao Mo4e94fea2016-08-09 14:31:37 -070012#include "compiler/translator/EmulateGLFragColorBroadcast.h"
Geoff Lang17732822013-08-29 13:46:49 -040013#include "compiler/translator/ForLoopUnroll.h"
14#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040015#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080016#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040017#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030018#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070019#include "compiler/translator/RegenerateStructNames.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030020#include "compiler/translator/RemovePow.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070021#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070022#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070023#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040024#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020025#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040026#include "compiler/translator/ValidateOutputs.h"
27#include "compiler/translator/VariablePacker.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000028#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040029#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040030#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000031
Corentin Wallez28b65282016-06-16 07:24:50 -070032#include <iostream>
33
34namespace
35{
36
37#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
38void DumpFuzzerCase(char const *const *shaderStrings,
39 size_t numStrings,
40 uint32_t type,
41 uint32_t spec,
42 uint32_t output,
43 uint64_t options)
44{
45 static int fileIndex = 0;
46
47 std::ostringstream o;
48 o << "corpus/" << fileIndex++ << ".sample";
49 std::string s = o.str();
50
51 // Must match the input format of the fuzzer
52 FILE *f = fopen(s.c_str(), "w");
53 fwrite(&type, sizeof(type), 1, f);
54 fwrite(&spec, sizeof(spec), 1, f);
55 fwrite(&output, sizeof(output), 1, f);
56 fwrite(&options, sizeof(options), 1, f);
57
58 char zero[128 - 20] = {0};
59 fwrite(&zero, 128 - 20, 1, f);
60
61 for (size_t i = 0; i < numStrings; i++)
62 {
63 fwrite(shaderStrings[i], sizeof(char), strlen(shaderStrings[i]), f);
64 }
65 fwrite(&zero, 1, 1, f);
66
67 fclose(f);
68}
69#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
70} // anonymous namespace
71
Jamie Madill5508f392014-02-20 13:31:36 -050072bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000073{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080074 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000075}
76
Qingqing Dengad0d0792015-04-08 14:25:06 -070077bool IsGLSL130OrNewer(ShShaderOutput output)
78{
79 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070080 output == SH_GLSL_140_OUTPUT ||
81 output == SH_GLSL_150_CORE_OUTPUT ||
82 output == SH_GLSL_330_CORE_OUTPUT ||
83 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070084 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070085 output == SH_GLSL_420_CORE_OUTPUT ||
86 output == SH_GLSL_430_CORE_OUTPUT ||
87 output == SH_GLSL_440_CORE_OUTPUT ||
88 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070089}
90
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070091size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050092{
Jamie Madill88f6e942014-02-19 10:27:53 -050093 // WebGL defines a max token legnth of 256, while ES2 leaves max token
94 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070095 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050096 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070097 case SH_WEBGL_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050098 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070099 default:
Jamie Madill88f6e942014-02-19 10:27:53 -0500100 return 1024;
101 }
102}
103
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000104namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700105
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800106class TScopedPoolAllocator
107{
108 public:
109 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
110 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400111 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000112 SetGlobalPoolAllocator(mAllocator);
113 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800114 ~TScopedPoolAllocator()
115 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000116 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400117 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000118 }
119
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800120 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000121 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400122};
123
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800124class TScopedSymbolTableLevel
125{
126 public:
127 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
128 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400129 ASSERT(mTable->atBuiltInLevel());
130 mTable->push();
131 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800132 ~TScopedSymbolTableLevel()
133 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400134 while (!mTable->atBuiltInLevel())
135 mTable->pop();
136 }
137
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800138 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400139 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000140};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700141
142int MapSpecToShaderVersion(ShShaderSpec spec)
143{
144 switch (spec)
145 {
146 case SH_GLES2_SPEC:
147 case SH_WEBGL_SPEC:
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700148 return 100;
149 case SH_GLES3_SPEC:
150 case SH_WEBGL2_SPEC:
151 return 300;
Martin Radev1be913c2016-07-11 17:59:16 +0300152 case SH_GLES3_1_SPEC:
153 case SH_WEBGL3_SPEC:
154 return 310;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700155 default:
156 UNREACHABLE();
157 return 0;
158 }
159}
160
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000161} // namespace
162
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800163TShHandleBase::TShHandleBase()
164{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000165 allocator.push();
166 SetGlobalPoolAllocator(&allocator);
167}
168
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800169TShHandleBase::~TShHandleBase()
170{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000171 SetGlobalPoolAllocator(NULL);
172 allocator.popAll();
173}
174
Jamie Madill183bde52014-07-02 15:31:19 -0400175TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700176 : variablesCollected(false),
177 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000178 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400179 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400180 maxUniformVectors(0),
181 maxExpressionComplexity(0),
182 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200183 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000184 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000185 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200186 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700187 mSourcePath(NULL),
Martin Radev802abe02016-08-04 17:48:32 +0300188 mComputeShaderLocalSizeDeclared(false),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700189 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000190{
Martin Radev802abe02016-08-04 17:48:32 +0300191 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000192}
193
194TCompiler::~TCompiler()
195{
196}
197
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800198bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300199{
200 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
201 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
202 // of ESSL 1.00 as in Appendix A of the spec).
203 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
204 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
205}
206
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000207bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000208{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000209 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400210 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000211 resources.MaxVertexUniformVectors :
212 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400213 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200214 maxCallStackDepth = resources.MaxCallStackDepth;
215 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400216
217 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000218
alokp@chromium.org07620a52010-09-23 17:53:56 +0000219 // Generate built-in symbol table.
220 if (!InitBuiltInSymbolTable(resources))
221 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000222 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000223 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000224
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000225 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
226 clampingStrategy = resources.ArrayIndexClampingStrategy;
227
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000228 hashFunction = resources.HashFunction;
229
alokp@chromium.org07620a52010-09-23 17:53:56 +0000230 return true;
231}
232
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800233TIntermNode *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
234 size_t numStrings,
235 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000236{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200237 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
238}
239
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300240TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
241 size_t numStrings,
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800242 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200243{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000244 clearResults();
245
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200246 ASSERT(numStrings > 0);
247 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000248
David Yen0fbd1282015-02-02 14:46:09 -0800249 // Reset the extension behavior for each compilation unit.
250 ResetExtensionBehavior(extensionBehavior);
251
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000252 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000253 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000254 if (compileOptions & SH_SOURCE_PATH)
255 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200256 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000257 ++firstSource;
258 }
259
Olli Etuahof119a262016-08-19 15:54:22 +0300260 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200261 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200262
Olli Etuahoa6996682015-10-12 14:32:30 +0300263 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400264 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000265
266 // We preserve symbols at the built-in level from compile-to-compile.
267 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400268 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000269
270 // Parse shader.
271 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400272 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
273 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000274
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000275 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700276 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
277 {
278 infoSink.info.prefix(EPrefixError);
279 infoSink.info << "unsupported shader version";
280 success = false;
281 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000282
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400283 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200284
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800285 if (success)
286 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700287 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700288 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700289
Martin Radev802abe02016-08-04 17:48:32 +0300290 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
291 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
292
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400293 root = parseContext.getTreeRoot();
Olli Etuahof119a262016-08-19 15:54:22 +0300294 root = TIntermediate::PostProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000295
Olli Etuahoa6996682015-10-12 14:32:30 +0300296 // Highp might have been auto-enabled based on shader version
297 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
298
Jamie Madill6654bc92014-03-26 14:01:57 -0400299 // Disallow expressions deemed too complex.
300 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
301 success = limitExpressionComplexity(root);
302
Corentin Wallez71d147f2015-02-11 11:15:24 -0800303 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000304 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800305 success = initCallDag(root);
306
307 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
308 success = checkCallDepth();
309
310 // Checks which functions are used and if "main" exists
311 if (success)
312 {
313 functionMetadata.clear();
314 functionMetadata.resize(mCallDag.size());
315 success = tagUsedFunctions();
316 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000317
Corentin Walleza094a8a2015-04-07 11:53:06 -0700318 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
319 success = pruneUnusedFunctions(root);
320
Olli Etuahoc6833112015-04-22 15:15:54 +0300321 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
322 if (success)
323 PruneEmptyDeclarations(root);
324
Jamie Madill183bde52014-07-02 15:31:19 -0400325 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400326 success = validateOutputs(root);
327
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300328 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000329 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000330
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000331 // Unroll for-loop markup needs to happen after validateLimitations pass.
332 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800333 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200334 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
335 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800336 root->traverse(&marker);
337 }
338 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800339 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200340 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
341 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800342 root->traverse(&marker);
343 if (marker.samplerArrayIndexIsFloatLoopIndex())
344 {
345 infoSink.info.prefix(EPrefixError);
346 infoSink.info << "sampler array index is float loop index";
347 success = false;
348 }
349 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000350
zmo@google.com32e97312011-08-24 01:03:11 +0000351 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200352 if (success)
353 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400354 // TODO(jmadill): Remove global pool allocator.
355 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200356 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400357 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000358 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200359 }
zmo@google.com32e97312011-08-24 01:03:11 +0000360
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000361 // Clamping uniform array bounds needs to happen after validateLimitations pass.
362 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
363 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
364
Ian Ewell924b7de2016-01-21 13:54:28 -0500365 // gl_Position is always written in compatibility output mode
366 if (success && shaderType == GL_VERTEX_SHADER &&
367 ((compileOptions & SH_INIT_GL_POSITION) ||
368 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800369 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400370
Corentin Wallezd4b50542015-09-28 12:19:26 -0700371 // This pass might emit short circuits so keep it before the short circuit unfolding
372 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
373 RewriteDoWhile(root, getTemporaryIndex());
374
Qiankun Miao09cfac62016-09-06 17:25:16 +0800375 if (success && (compileOptions & SH_ADD_AND_TRUE_TO_LOOP_CONDITION))
376 sh::AddAndTrueToLoopCondition(root);
377
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800378 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
379 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700380 UnfoldShortCircuitAST unfoldShortCircuit;
381 root->traverse(&unfoldShortCircuit);
382 unfoldShortCircuit.updateTree();
383 }
384
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300385 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
386 {
387 RemovePow(root);
388 }
389
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300390 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800391 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400392 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800393 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
394 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000395 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800396 if (!success)
397 {
Jamie Madill075edd82013-07-08 13:30:19 -0400398 infoSink.info.prefix(EPrefixError);
399 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000400 }
401 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700402 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
403 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300404 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700405 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000406 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000407
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700408 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
409 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700410 ScalarizeVecAndMatConstructorArgs scalarizer(
411 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700412 root->traverse(&scalarizer);
413 }
414
Zhenyao Moe740add2014-07-18 17:01:01 -0700415 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
416 {
417 RegenerateStructNames gen(symbolTable, shaderVersion);
418 root->traverse(&gen);
419 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300420
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700421 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
422 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
423 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
424 {
425 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
426 }
427
Olli Etuaho3d932d82016-04-12 11:10:30 +0300428 if (success)
429 {
430 DeferGlobalInitializers(root);
431 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000432 }
433
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700434 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200435 if (success)
436 return root;
437
438 return NULL;
439}
440
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800441bool TCompiler::compile(const char *const shaderStrings[],
442 size_t numStrings,
443 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200444{
Corentin Wallez28b65282016-06-16 07:24:50 -0700445#if defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
446 DumpFuzzerCase(shaderStrings, numStrings, shaderType, shaderSpec, outputType, compileOptionsIn);
447#endif // defined(ANGLE_ENABLE_FUZZER_CORPUS_OUTPUT)
448
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200449 if (numStrings == 0)
450 return true;
451
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800452 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700453
454 // Apply key workarounds.
455 if (shouldFlattenPragmaStdglInvariantAll())
456 {
457 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
458 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
459 }
460
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200461 TScopedPoolAllocator scopedAlloc(&allocator);
462 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
463
464 if (root)
465 {
466 if (compileOptions & SH_INTERMEDIATE_TREE)
467 TIntermediate::outputTree(root, infoSink.info);
468
469 if (compileOptions & SH_OBJECT_CODE)
470 translate(root, compileOptions);
471
472 // The IntermNode tree doesn't need to be deleted here, since the
473 // memory will be freed in a big chunk by the PoolAllocator.
474 return true;
475 }
476 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000477}
478
Nicolas Capens49a88872013-06-20 09:54:03 -0400479bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000480{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000481 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400482 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000483
Nicolas Capens49a88872013-06-20 09:54:03 -0400484 assert(symbolTable.isEmpty());
485 symbolTable.push(); // COMMON_BUILTINS
486 symbolTable.push(); // ESSL1_BUILTINS
487 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300488 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000489
Nicolas Capens49a88872013-06-20 09:54:03 -0400490 TPublicType integer;
Martin Radev4a9cd802016-09-01 16:51:51 +0300491 integer.setBasicType(EbtInt);
492 integer.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400493 integer.array = false;
494
495 TPublicType floatingPoint;
Martin Radev4a9cd802016-09-01 16:51:51 +0300496 floatingPoint.setBasicType(EbtFloat);
497 floatingPoint.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400498 floatingPoint.array = false;
499
500 switch(shaderType)
501 {
Jamie Madill183bde52014-07-02 15:31:19 -0400502 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400503 symbolTable.setDefaultPrecision(integer, EbpMedium);
504 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400505 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400506 symbolTable.setDefaultPrecision(integer, EbpHigh);
507 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
508 break;
Martin Radev802abe02016-08-04 17:48:32 +0300509 case GL_COMPUTE_SHADER:
510 symbolTable.setDefaultPrecision(integer, EbpHigh);
511 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
512 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800513 default:
514 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400515 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200516 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400517 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200518 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
519 initSamplerDefaultPrecision(EbtSampler2D);
520 initSamplerDefaultPrecision(EbtSamplerCube);
521 // SamplerExternalOES is specified in the extension to have default precision.
522 initSamplerDefaultPrecision(EbtSamplerExternalOES);
523 // It isn't specified whether Sampler2DRect has default precision.
524 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400525
Jamie Madill1b452142013-07-12 14:51:11 -0400526 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400527
528 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
529
530 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000531}
532
Olli Etuaho183d7e22015-11-20 15:59:09 +0200533void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
534{
535 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
536 TPublicType sampler;
Martin Radev4a9cd802016-09-01 16:51:51 +0300537 sampler.initializeSizeForScalarTypes();
538 sampler.setBasicType(samplerType);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200539 sampler.array = false;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200540 symbolTable.setDefaultPrecision(sampler, EbpLow);
541}
542
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400543void TCompiler::setResourceString()
544{
545 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400546
547 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400548 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
549 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
550 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
551 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
552 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
553 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
554 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
555 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
556 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
557 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400558 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
559 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400560 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
561 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
562 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
563 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
564 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200565 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300566 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400567 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
568 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100569 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
570 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
571 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400572 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
573 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
574 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300575 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300576 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200577 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
Martin Radeve93d24e2016-07-28 12:06:05 +0300578 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
579 << ":MaxImageUnits:" << compileResources.MaxImageUnits
580 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
581 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
582 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
583 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
584 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
585 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
586 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
587 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
588 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
589 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
590 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
591 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
592 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
593 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
594 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
595 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
596 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
597 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
598 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
599 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
600 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
601 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
602 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400603 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400604
605 builtInResourcesString = strstream.str();
606}
607
alokp@chromium.org07620a52010-09-23 17:53:56 +0000608void TCompiler::clearResults()
609{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000610 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000611 infoSink.info.erase();
612 infoSink.obj.erase();
613 infoSink.debug.erase();
614
Jamie Madilled27c722014-07-02 15:31:23 -0400615 attributes.clear();
616 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000617 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400618 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400619 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400620 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700621 variablesCollected = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000622
623 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000624
625 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200626
627 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700628 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000629}
630
Corentin Wallez71d147f2015-02-11 11:15:24 -0800631bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000632{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800633 mCallDag.clear();
634
635 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800636 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800637 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800638 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800639 case CallDAG::INITDAG_RECURSION:
640 infoSink.info.prefix(EPrefixError);
641 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800642 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800643 case CallDAG::INITDAG_UNDEFINED:
644 infoSink.info.prefix(EPrefixError);
645 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800646 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800647 }
648
649 UNREACHABLE();
650 return true;
651}
652
653bool TCompiler::checkCallDepth()
654{
655 std::vector<int> depths(mCallDag.size());
656
657 for (size_t i = 0; i < mCallDag.size(); i++)
658 {
659 int depth = 0;
660 auto &record = mCallDag.getRecordFromIndex(i);
661
662 for (auto &calleeIndex : record.callees)
663 {
664 depth = std::max(depth, depths[calleeIndex] + 1);
665 }
666
667 depths[i] = depth;
668
669 if (depth >= maxCallStackDepth)
670 {
671 // Trace back the function chain to have a meaningful info log.
672 infoSink.info.prefix(EPrefixError);
673 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
674 << ") with the following call chain: " << record.name;
675
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700676 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800677 int currentDepth = depth;
678
679 while (currentFunction != -1)
680 {
681 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
682
683 int nextFunction = -1;
684 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
685 {
686 if (depths[calleeIndex] == currentDepth - 1)
687 {
688 currentDepth--;
689 nextFunction = calleeIndex;
690 }
691 }
692
693 currentFunction = nextFunction;
694 }
695
696 return false;
697 }
698 }
699
700 return true;
701}
702
703bool TCompiler::tagUsedFunctions()
704{
705 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700706 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800707 {
708 if (mCallDag.getRecordFromIndex(i).name == "main(")
709 {
710 internalTagUsedFunction(i);
711 return true;
712 }
713 }
714
715 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200716 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800717 return false;
718}
719
720void TCompiler::internalTagUsedFunction(size_t index)
721{
722 if (functionMetadata[index].used)
723 {
724 return;
725 }
726
727 functionMetadata[index].used = true;
728
729 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
730 {
731 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000732 }
733}
734
Corentin Walleza094a8a2015-04-07 11:53:06 -0700735// A predicate for the stl that returns if a top-level node is unused
736class TCompiler::UnusedPredicate
737{
738 public:
739 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
740 : mCallDag(callDag),
741 mMetadatas(metadatas)
742 {
743 }
744
745 bool operator ()(TIntermNode *node)
746 {
747 const TIntermAggregate *asAggregate = node->getAsAggregate();
748
749 if (asAggregate == nullptr)
750 {
751 return false;
752 }
753
754 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
755 {
756 return false;
757 }
758
759 size_t callDagIndex = mCallDag->findIndex(asAggregate);
760 if (callDagIndex == CallDAG::InvalidIndex)
761 {
762 // This happens only for unimplemented prototypes which are thus unused
763 ASSERT(asAggregate->getOp() == EOpPrototype);
764 return true;
765 }
766
767 ASSERT(callDagIndex < mMetadatas->size());
768 return !(*mMetadatas)[callDagIndex].used;
769 }
770
771 private:
772 const CallDAG *mCallDag;
773 const std::vector<FunctionMetadata> *mMetadatas;
774};
775
776bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
777{
778 TIntermAggregate *rootNode = root->getAsAggregate();
779 ASSERT(rootNode != nullptr);
780
781 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
782 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700783
784 if (!sequence->empty())
785 {
786 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
787 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700788
789 return true;
790}
791
Jamie Madill05a80ce2013-06-20 11:55:49 -0400792bool TCompiler::validateOutputs(TIntermNode* root)
793{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300794 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400795 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300796 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400797}
798
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800799bool TCompiler::validateLimitations(TIntermNode* root)
800{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200801 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000802 root->traverse(&validate);
803 return validate.numErrors() == 0;
804}
805
Jamie Madilleb1a0102013-07-08 13:31:38 -0400806bool TCompiler::limitExpressionComplexity(TIntermNode* root)
807{
Jamie Madill6654bc92014-03-26 14:01:57 -0400808 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400809 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400810
811 if (traverser.getMaxDepth() > maxExpressionComplexity)
812 {
813 infoSink.info << "Expression too complex.";
814 return false;
815 }
816
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200817 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
818 {
819 infoSink.info << "Function has too many parameters.";
820 return false;
821 }
822
Jamie Madilleb1a0102013-07-08 13:31:38 -0400823 return true;
824}
825
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400826void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000827{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700828 if (!variablesCollected)
829 {
830 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
831 &interfaceBlocks, hashFunction, symbolTable, extensionBehavior);
832 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400833
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700834 // This is for enforcePackingRestriction().
835 sh::ExpandUniforms(uniforms, &expandedUniforms);
836 variablesCollected = true;
837 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000838}
zmo@google.comfd747b82011-04-23 01:30:07 +0000839
gman@chromium.org8d804792012-10-17 21:33:48 +0000840bool TCompiler::enforcePackingRestrictions()
841{
842 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400843 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000844}
845
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800846void TCompiler::initializeGLPosition(TIntermNode* root)
847{
Zhenyao Mo72111912016-07-20 17:45:56 -0700848 InitVariableList list;
849 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
850 var.name = "gl_Position";
851 list.push_back(var);
852 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800853}
854
Olli Etuaho27776e32016-07-22 14:00:56 +0300855void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800856{
Zhenyao Mo72111912016-07-20 17:45:56 -0700857 InitVariableList list;
858 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800859 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700860 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800861 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700862 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800863 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800864 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700865 else
866 {
867 ASSERT(shaderType == GL_FRAGMENT_SHADER);
868 for (auto var : outputVariables)
869 {
870 list.push_back(var);
871 }
872 }
873 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800874}
875
zmo@google.com5601ea02011-06-10 18:23:25 +0000876const TExtensionBehavior& TCompiler::getExtensionBehavior() const
877{
878 return extensionBehavior;
879}
zmo@google.com32e97312011-08-24 01:03:11 +0000880
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200881const char *TCompiler::getSourcePath() const
882{
883 return mSourcePath;
884}
885
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000886const ShBuiltInResources& TCompiler::getResources() const
887{
888 return compileResources;
889}
890
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000891const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
892{
893 return arrayBoundsClamper;
894}
895
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000896ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
897{
898 return clampingStrategy;
899}
900
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200901const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000902{
903 return builtInFunctionEmulator;
904}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700905
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800906void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700907{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700908 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
909 {
910 TInfoSinkBase &sink = infoSink.obj;
911 if (mPragma.stdgl.invariantAll)
912 sink << "#pragma STDGL invariant(all)\n";
913 }
914}
915
916bool TCompiler::isVaryingDefined(const char *varyingName)
917{
918 ASSERT(variablesCollected);
919 for (size_t ii = 0; ii < varyings.size(); ++ii)
920 {
921 if (varyings[ii].name == varyingName)
922 {
923 return true;
924 }
925 }
926
927 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700928}