blob: 0257dd3a7300db9e52e596e6ec8685814edf1307 [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
Dmitry Skiba01971112015-07-10 14:54:00 -04007#include "compiler/translator/Cache.h"
Jamie Madilld4a3a312014-06-25 16:04:56 -04008#include "compiler/translator/Compiler.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -08009#include "compiler/translator/CallDAG.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030010#include "compiler/translator/DeferGlobalInitializers.h"
Zhenyao Mo4e94fea2016-08-09 14:31:37 -070011#include "compiler/translator/EmulateGLFragColorBroadcast.h"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/ForLoopUnroll.h"
13#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040014#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080015#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040016#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030017#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070018#include "compiler/translator/RegenerateStructNames.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030019#include "compiler/translator/RemovePow.h"
Geoff Lang17732822013-08-29 13:46:49 -040020#include "compiler/translator/RenameFunction.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"
28#include "compiler/translator/depgraph/DependencyGraph.h"
29#include "compiler/translator/depgraph/DependencyGraphOutput.h"
30#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
31#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000032#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040033#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040034#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000035
Jamie Madill5508f392014-02-20 13:31:36 -050036bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000037{
Martin Radev1be913c2016-07-11 17:59:16 +030038 return (spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC || spec == SH_WEBGL2_SPEC ||
39 spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000040}
41
Qingqing Dengad0d0792015-04-08 14:25:06 -070042bool IsGLSL130OrNewer(ShShaderOutput output)
43{
44 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070045 output == SH_GLSL_140_OUTPUT ||
46 output == SH_GLSL_150_CORE_OUTPUT ||
47 output == SH_GLSL_330_CORE_OUTPUT ||
48 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070049 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070050 output == SH_GLSL_420_CORE_OUTPUT ||
51 output == SH_GLSL_430_CORE_OUTPUT ||
52 output == SH_GLSL_440_CORE_OUTPUT ||
53 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070054}
55
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070056size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050057{
Jamie Madill88f6e942014-02-19 10:27:53 -050058 // WebGL defines a max token legnth of 256, while ES2 leaves max token
59 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070060 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050061 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070062 case SH_WEBGL_SPEC:
63 case SH_CSS_SHADERS_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050064 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070065 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050066 return 1024;
67 }
68}
69
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000070namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070071
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080072class TScopedPoolAllocator
73{
74 public:
75 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
76 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040077 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000078 SetGlobalPoolAllocator(mAllocator);
79 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080080 ~TScopedPoolAllocator()
81 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000082 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040083 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000084 }
85
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080086 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000087 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040088};
89
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080090class TScopedSymbolTableLevel
91{
92 public:
93 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
94 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040095 ASSERT(mTable->atBuiltInLevel());
96 mTable->push();
97 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080098 ~TScopedSymbolTableLevel()
99 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400100 while (!mTable->atBuiltInLevel())
101 mTable->pop();
102 }
103
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800104 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400105 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000106};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700107
108int MapSpecToShaderVersion(ShShaderSpec spec)
109{
110 switch (spec)
111 {
112 case SH_GLES2_SPEC:
113 case SH_WEBGL_SPEC:
114 case SH_CSS_SHADERS_SPEC:
115 return 100;
116 case SH_GLES3_SPEC:
117 case SH_WEBGL2_SPEC:
118 return 300;
Martin Radev1be913c2016-07-11 17:59:16 +0300119 case SH_GLES3_1_SPEC:
120 case SH_WEBGL3_SPEC:
121 return 310;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700122 default:
123 UNREACHABLE();
124 return 0;
125 }
126}
127
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000128} // namespace
129
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800130TShHandleBase::TShHandleBase()
131{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000132 allocator.push();
133 SetGlobalPoolAllocator(&allocator);
134}
135
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800136TShHandleBase::~TShHandleBase()
137{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000138 SetGlobalPoolAllocator(NULL);
139 allocator.popAll();
140}
141
Jamie Madill183bde52014-07-02 15:31:19 -0400142TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700143 : variablesCollected(false),
144 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000145 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400146 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400147 maxUniformVectors(0),
148 maxExpressionComplexity(0),
149 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200150 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000151 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000152 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200153 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700154 mSourcePath(NULL),
Martin Radev802abe02016-08-04 17:48:32 +0300155 mComputeShaderLocalSizeDeclared(false),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700156 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000157{
Martin Radev802abe02016-08-04 17:48:32 +0300158 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000159}
160
161TCompiler::~TCompiler()
162{
163}
164
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300165bool TCompiler::shouldRunLoopAndIndexingValidation(int compileOptions) const
166{
167 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
168 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
169 // of ESSL 1.00 as in Appendix A of the spec).
170 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
171 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
172}
173
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000174bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000175{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000176 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400177 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000178 resources.MaxVertexUniformVectors :
179 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400180 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200181 maxCallStackDepth = resources.MaxCallStackDepth;
182 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400183
184 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000185
alokp@chromium.org07620a52010-09-23 17:53:56 +0000186 // Generate built-in symbol table.
187 if (!InitBuiltInSymbolTable(resources))
188 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000189 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000190 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000191
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000192 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
193 clampingStrategy = resources.ArrayIndexClampingStrategy;
194
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000195 hashFunction = resources.HashFunction;
196
alokp@chromium.org07620a52010-09-23 17:53:56 +0000197 return true;
198}
199
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200200TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
201 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000202{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200203 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
204}
205
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300206TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
207 size_t numStrings,
208 const int compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200209{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000210 clearResults();
211
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200212 ASSERT(numStrings > 0);
213 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000214
David Yen0fbd1282015-02-02 14:46:09 -0800215 // Reset the extension behavior for each compilation unit.
216 ResetExtensionBehavior(extensionBehavior);
217
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000218 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000219 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000220 if (compileOptions & SH_SOURCE_PATH)
221 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200222 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000223 ++firstSource;
224 }
225
alokp@chromium.org07620a52010-09-23 17:53:56 +0000226 TIntermediate intermediate(infoSink);
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200227 TParseContext parseContext(symbolTable, extensionBehavior, intermediate, shaderType, shaderSpec,
228 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200229
Olli Etuahoa6996682015-10-12 14:32:30 +0300230 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400231 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000232
233 // We preserve symbols at the built-in level from compile-to-compile.
234 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400235 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000236
237 // Parse shader.
238 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400239 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
240 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000241
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000242 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700243 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
244 {
245 infoSink.info.prefix(EPrefixError);
246 infoSink.info << "unsupported shader version";
247 success = false;
248 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000249
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400250 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200251
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800252 if (success)
253 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700254 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700255 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700256
Martin Radev802abe02016-08-04 17:48:32 +0300257 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
258 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
259
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400260 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300261 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000262
Olli Etuahoa6996682015-10-12 14:32:30 +0300263 // Highp might have been auto-enabled based on shader version
264 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
265
Jamie Madill6654bc92014-03-26 14:01:57 -0400266 // Disallow expressions deemed too complex.
267 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
268 success = limitExpressionComplexity(root);
269
Corentin Wallez71d147f2015-02-11 11:15:24 -0800270 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000271 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800272 success = initCallDag(root);
273
274 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
275 success = checkCallDepth();
276
277 // Checks which functions are used and if "main" exists
278 if (success)
279 {
280 functionMetadata.clear();
281 functionMetadata.resize(mCallDag.size());
282 success = tagUsedFunctions();
283 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000284
Corentin Walleza094a8a2015-04-07 11:53:06 -0700285 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
286 success = pruneUnusedFunctions(root);
287
Olli Etuahoc6833112015-04-22 15:15:54 +0300288 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
289 if (success)
290 PruneEmptyDeclarations(root);
291
Jamie Madill183bde52014-07-02 15:31:19 -0400292 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400293 success = validateOutputs(root);
294
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300295 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000296 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000297
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000298 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000299 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000300
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000301 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
302 rewriteCSSShader(root);
303
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000304 // Unroll for-loop markup needs to happen after validateLimitations pass.
305 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800306 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200307 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
308 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800309 root->traverse(&marker);
310 }
311 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800312 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200313 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
314 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800315 root->traverse(&marker);
316 if (marker.samplerArrayIndexIsFloatLoopIndex())
317 {
318 infoSink.info.prefix(EPrefixError);
319 infoSink.info << "sampler array index is float loop index";
320 success = false;
321 }
322 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000323
zmo@google.com32e97312011-08-24 01:03:11 +0000324 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200325 if (success)
326 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400327 // TODO(jmadill): Remove global pool allocator.
328 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200329 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400330 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000331 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200332 }
zmo@google.com32e97312011-08-24 01:03:11 +0000333
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000334 // Clamping uniform array bounds needs to happen after validateLimitations pass.
335 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
336 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
337
Ian Ewell924b7de2016-01-21 13:54:28 -0500338 // gl_Position is always written in compatibility output mode
339 if (success && shaderType == GL_VERTEX_SHADER &&
340 ((compileOptions & SH_INIT_GL_POSITION) ||
341 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800342 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400343
Corentin Wallezd4b50542015-09-28 12:19:26 -0700344 // This pass might emit short circuits so keep it before the short circuit unfolding
345 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
346 RewriteDoWhile(root, getTemporaryIndex());
347
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800348 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
349 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700350 UnfoldShortCircuitAST unfoldShortCircuit;
351 root->traverse(&unfoldShortCircuit);
352 unfoldShortCircuit.updateTree();
353 }
354
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300355 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
356 {
357 RemovePow(root);
358 }
359
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300360 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800361 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400362 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800363 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
364 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000365 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800366 if (!success)
367 {
Jamie Madill075edd82013-07-08 13:30:19 -0400368 infoSink.info.prefix(EPrefixError);
369 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000370 }
371 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700372 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
373 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300374 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700375 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000376 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000377
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700378 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
379 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700380 ScalarizeVecAndMatConstructorArgs scalarizer(
381 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700382 root->traverse(&scalarizer);
383 }
384
Zhenyao Moe740add2014-07-18 17:01:01 -0700385 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
386 {
387 RegenerateStructNames gen(symbolTable, shaderVersion);
388 root->traverse(&gen);
389 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300390
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700391 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
392 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
393 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
394 {
395 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
396 }
397
Olli Etuaho3d932d82016-04-12 11:10:30 +0300398 if (success)
399 {
400 DeferGlobalInitializers(root);
401 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000402 }
403
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700404 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200405 if (success)
406 return root;
407
408 return NULL;
409}
410
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700411bool TCompiler::compile(const char *const shaderStrings[], size_t numStrings, int compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200412{
413 if (numStrings == 0)
414 return true;
415
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700416 int compileOptions = compileOptionsIn;
417
418 // Apply key workarounds.
419 if (shouldFlattenPragmaStdglInvariantAll())
420 {
421 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
422 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
423 }
424
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200425 TScopedPoolAllocator scopedAlloc(&allocator);
426 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
427
428 if (root)
429 {
430 if (compileOptions & SH_INTERMEDIATE_TREE)
431 TIntermediate::outputTree(root, infoSink.info);
432
433 if (compileOptions & SH_OBJECT_CODE)
434 translate(root, compileOptions);
435
436 // The IntermNode tree doesn't need to be deleted here, since the
437 // memory will be freed in a big chunk by the PoolAllocator.
438 return true;
439 }
440 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000441}
442
Nicolas Capens49a88872013-06-20 09:54:03 -0400443bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000444{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000445 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400446 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000447
Nicolas Capens49a88872013-06-20 09:54:03 -0400448 assert(symbolTable.isEmpty());
449 symbolTable.push(); // COMMON_BUILTINS
450 symbolTable.push(); // ESSL1_BUILTINS
451 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300452 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000453
Nicolas Capens49a88872013-06-20 09:54:03 -0400454 TPublicType integer;
455 integer.type = EbtInt;
456 integer.primarySize = 1;
457 integer.secondarySize = 1;
458 integer.array = false;
459
460 TPublicType floatingPoint;
461 floatingPoint.type = EbtFloat;
462 floatingPoint.primarySize = 1;
463 floatingPoint.secondarySize = 1;
464 floatingPoint.array = false;
465
466 switch(shaderType)
467 {
Jamie Madill183bde52014-07-02 15:31:19 -0400468 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400469 symbolTable.setDefaultPrecision(integer, EbpMedium);
470 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400471 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400472 symbolTable.setDefaultPrecision(integer, EbpHigh);
473 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
474 break;
Martin Radev802abe02016-08-04 17:48:32 +0300475 case GL_COMPUTE_SHADER:
476 symbolTable.setDefaultPrecision(integer, EbpHigh);
477 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
478 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800479 default:
480 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400481 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200482 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400483 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200484 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
485 initSamplerDefaultPrecision(EbtSampler2D);
486 initSamplerDefaultPrecision(EbtSamplerCube);
487 // SamplerExternalOES is specified in the extension to have default precision.
488 initSamplerDefaultPrecision(EbtSamplerExternalOES);
489 // It isn't specified whether Sampler2DRect has default precision.
490 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400491
Jamie Madill1b452142013-07-12 14:51:11 -0400492 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400493
494 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
495
496 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000497}
498
Olli Etuaho183d7e22015-11-20 15:59:09 +0200499void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
500{
501 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
502 TPublicType sampler;
503 sampler.primarySize = 1;
504 sampler.secondarySize = 1;
505 sampler.array = false;
506 sampler.type = samplerType;
507 symbolTable.setDefaultPrecision(sampler, EbpLow);
508}
509
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400510void TCompiler::setResourceString()
511{
512 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400513
514 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400515 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
516 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
517 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
518 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
519 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
520 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
521 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
522 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
523 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
524 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400525 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
526 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400527 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
528 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
529 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
530 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
531 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200532 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300533 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400534 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
535 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100536 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
537 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
538 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400539 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
540 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
541 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300542 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300543 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200544 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
Martin Radeve93d24e2016-07-28 12:06:05 +0300545 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
546 << ":MaxImageUnits:" << compileResources.MaxImageUnits
547 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
548 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
549 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
550 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
551 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
552 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
553 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
554 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
555 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
556 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
557 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
558 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
559 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
560 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
561 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
562 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
563 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
564 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
565 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
566 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
567 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
568 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
569 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400570 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400571
572 builtInResourcesString = strstream.str();
573}
574
alokp@chromium.org07620a52010-09-23 17:53:56 +0000575void TCompiler::clearResults()
576{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000577 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000578 infoSink.info.erase();
579 infoSink.obj.erase();
580 infoSink.debug.erase();
581
Jamie Madilled27c722014-07-02 15:31:23 -0400582 attributes.clear();
583 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000584 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400585 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400586 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400587 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700588 variablesCollected = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000589
590 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000591
592 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200593
594 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700595 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000596}
597
Corentin Wallez71d147f2015-02-11 11:15:24 -0800598bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000599{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800600 mCallDag.clear();
601
602 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800603 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800604 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800605 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800606 case CallDAG::INITDAG_RECURSION:
607 infoSink.info.prefix(EPrefixError);
608 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800609 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800610 case CallDAG::INITDAG_UNDEFINED:
611 infoSink.info.prefix(EPrefixError);
612 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800613 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800614 }
615
616 UNREACHABLE();
617 return true;
618}
619
620bool TCompiler::checkCallDepth()
621{
622 std::vector<int> depths(mCallDag.size());
623
624 for (size_t i = 0; i < mCallDag.size(); i++)
625 {
626 int depth = 0;
627 auto &record = mCallDag.getRecordFromIndex(i);
628
629 for (auto &calleeIndex : record.callees)
630 {
631 depth = std::max(depth, depths[calleeIndex] + 1);
632 }
633
634 depths[i] = depth;
635
636 if (depth >= maxCallStackDepth)
637 {
638 // Trace back the function chain to have a meaningful info log.
639 infoSink.info.prefix(EPrefixError);
640 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
641 << ") with the following call chain: " << record.name;
642
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700643 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800644 int currentDepth = depth;
645
646 while (currentFunction != -1)
647 {
648 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
649
650 int nextFunction = -1;
651 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
652 {
653 if (depths[calleeIndex] == currentDepth - 1)
654 {
655 currentDepth--;
656 nextFunction = calleeIndex;
657 }
658 }
659
660 currentFunction = nextFunction;
661 }
662
663 return false;
664 }
665 }
666
667 return true;
668}
669
670bool TCompiler::tagUsedFunctions()
671{
672 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700673 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800674 {
675 if (mCallDag.getRecordFromIndex(i).name == "main(")
676 {
677 internalTagUsedFunction(i);
678 return true;
679 }
680 }
681
682 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200683 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800684 return false;
685}
686
687void TCompiler::internalTagUsedFunction(size_t index)
688{
689 if (functionMetadata[index].used)
690 {
691 return;
692 }
693
694 functionMetadata[index].used = true;
695
696 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
697 {
698 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000699 }
700}
701
Corentin Walleza094a8a2015-04-07 11:53:06 -0700702// A predicate for the stl that returns if a top-level node is unused
703class TCompiler::UnusedPredicate
704{
705 public:
706 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
707 : mCallDag(callDag),
708 mMetadatas(metadatas)
709 {
710 }
711
712 bool operator ()(TIntermNode *node)
713 {
714 const TIntermAggregate *asAggregate = node->getAsAggregate();
715
716 if (asAggregate == nullptr)
717 {
718 return false;
719 }
720
721 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
722 {
723 return false;
724 }
725
726 size_t callDagIndex = mCallDag->findIndex(asAggregate);
727 if (callDagIndex == CallDAG::InvalidIndex)
728 {
729 // This happens only for unimplemented prototypes which are thus unused
730 ASSERT(asAggregate->getOp() == EOpPrototype);
731 return true;
732 }
733
734 ASSERT(callDagIndex < mMetadatas->size());
735 return !(*mMetadatas)[callDagIndex].used;
736 }
737
738 private:
739 const CallDAG *mCallDag;
740 const std::vector<FunctionMetadata> *mMetadatas;
741};
742
743bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
744{
745 TIntermAggregate *rootNode = root->getAsAggregate();
746 ASSERT(rootNode != nullptr);
747
748 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
749 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700750
751 if (!sequence->empty())
752 {
753 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
754 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700755
756 return true;
757}
758
Jamie Madill05a80ce2013-06-20 11:55:49 -0400759bool TCompiler::validateOutputs(TIntermNode* root)
760{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300761 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400762 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300763 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400764}
765
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000766void TCompiler::rewriteCSSShader(TIntermNode* root)
767{
768 RenameFunction renamer("main(", "css_main(");
769 root->traverse(&renamer);
770}
771
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800772bool TCompiler::validateLimitations(TIntermNode* root)
773{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200774 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000775 root->traverse(&validate);
776 return validate.numErrors() == 0;
777}
778
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000779bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000780{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800781 if (shaderSpec != SH_WEBGL_SPEC)
782 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000783 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
784 return false;
785 }
786
Jamie Madill183bde52014-07-02 15:31:19 -0400787 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800788 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000789 TDependencyGraph graph(root);
790
791 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000792 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500793
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000794 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800795 if (outputGraph)
796 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000797 TDependencyGraphOutput output(infoSink.info);
798 output.outputAllSpanningTrees(graph);
799 }
Jamie Madill5508f392014-02-20 13:31:36 -0500800
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000801 return success;
802 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800803 else
804 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000805 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000806 }
807}
808
Jamie Madilleb1a0102013-07-08 13:31:38 -0400809bool TCompiler::limitExpressionComplexity(TIntermNode* root)
810{
Jamie Madill6654bc92014-03-26 14:01:57 -0400811 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400812 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400813
814 if (traverser.getMaxDepth() > maxExpressionComplexity)
815 {
816 infoSink.info << "Expression too complex.";
817 return false;
818 }
819
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200820 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
821 {
822 infoSink.info << "Function has too many parameters.";
823 return false;
824 }
825
Jamie Madilleb1a0102013-07-08 13:31:38 -0400826 return true;
827}
828
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000829bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000830{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000831 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000832 restrictor.enforceRestrictions(graph);
833 return restrictor.numErrors() == 0;
834}
835
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000836bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000837{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000838 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000839 restrictor.enforceRestrictions(root);
840 return restrictor.numErrors() == 0;
841}
842
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400843void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000844{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700845 if (!variablesCollected)
846 {
847 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
848 &interfaceBlocks, hashFunction, symbolTable, extensionBehavior);
849 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400850
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700851 // This is for enforcePackingRestriction().
852 sh::ExpandUniforms(uniforms, &expandedUniforms);
853 variablesCollected = true;
854 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000855}
zmo@google.comfd747b82011-04-23 01:30:07 +0000856
gman@chromium.org8d804792012-10-17 21:33:48 +0000857bool TCompiler::enforcePackingRestrictions()
858{
859 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400860 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000861}
862
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800863void TCompiler::initializeGLPosition(TIntermNode* root)
864{
Zhenyao Mo72111912016-07-20 17:45:56 -0700865 InitVariableList list;
866 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
867 var.name = "gl_Position";
868 list.push_back(var);
869 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800870}
871
Olli Etuaho27776e32016-07-22 14:00:56 +0300872void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800873{
Zhenyao Mo72111912016-07-20 17:45:56 -0700874 InitVariableList list;
875 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800876 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700877 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800878 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700879 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800880 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800881 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700882 else
883 {
884 ASSERT(shaderType == GL_FRAGMENT_SHADER);
885 for (auto var : outputVariables)
886 {
887 list.push_back(var);
888 }
889 }
890 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800891}
892
zmo@google.com5601ea02011-06-10 18:23:25 +0000893const TExtensionBehavior& TCompiler::getExtensionBehavior() const
894{
895 return extensionBehavior;
896}
zmo@google.com32e97312011-08-24 01:03:11 +0000897
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200898const char *TCompiler::getSourcePath() const
899{
900 return mSourcePath;
901}
902
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000903const ShBuiltInResources& TCompiler::getResources() const
904{
905 return compileResources;
906}
907
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000908const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
909{
910 return arrayBoundsClamper;
911}
912
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000913ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
914{
915 return clampingStrategy;
916}
917
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200918const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000919{
920 return builtInFunctionEmulator;
921}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700922
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700923void TCompiler::writePragma(int compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700924{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700925 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
926 {
927 TInfoSinkBase &sink = infoSink.obj;
928 if (mPragma.stdgl.invariantAll)
929 sink << "#pragma STDGL invariant(all)\n";
930 }
931}
932
933bool TCompiler::isVaryingDefined(const char *varyingName)
934{
935 ASSERT(variablesCollected);
936 for (size_t ii = 0; ii < varyings.size(); ++ii)
937 {
938 if (varyings[ii].name == varyingName)
939 {
940 return true;
941 }
942 }
943
944 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700945}