blob: 06dc6cd590a0042fcc3f6697b48e799324f278c4 [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();
Qiankun Miao583c4d22016-08-05 17:58:40 +0800211 symbolTable.clearInvariantVaryings();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000212
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200213 ASSERT(numStrings > 0);
214 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000215
David Yen0fbd1282015-02-02 14:46:09 -0800216 // Reset the extension behavior for each compilation unit.
217 ResetExtensionBehavior(extensionBehavior);
218
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000219 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000220 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000221 if (compileOptions & SH_SOURCE_PATH)
222 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200223 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000224 ++firstSource;
225 }
226
alokp@chromium.org07620a52010-09-23 17:53:56 +0000227 TIntermediate intermediate(infoSink);
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200228 TParseContext parseContext(symbolTable, extensionBehavior, intermediate, shaderType, shaderSpec,
229 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200230
Olli Etuahoa6996682015-10-12 14:32:30 +0300231 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400232 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000233
234 // We preserve symbols at the built-in level from compile-to-compile.
235 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400236 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000237
238 // Parse shader.
239 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400240 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
241 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000242
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000243 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700244 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
245 {
246 infoSink.info.prefix(EPrefixError);
247 infoSink.info << "unsupported shader version";
248 success = false;
249 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000250
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400251 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200252
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800253 if (success)
254 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700255 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700256 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700257
Martin Radev802abe02016-08-04 17:48:32 +0300258 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
259 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
260
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400261 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300262 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000263
Olli Etuahoa6996682015-10-12 14:32:30 +0300264 // Highp might have been auto-enabled based on shader version
265 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
266
Jamie Madill6654bc92014-03-26 14:01:57 -0400267 // Disallow expressions deemed too complex.
268 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
269 success = limitExpressionComplexity(root);
270
Corentin Wallez71d147f2015-02-11 11:15:24 -0800271 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000272 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800273 success = initCallDag(root);
274
275 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
276 success = checkCallDepth();
277
278 // Checks which functions are used and if "main" exists
279 if (success)
280 {
281 functionMetadata.clear();
282 functionMetadata.resize(mCallDag.size());
283 success = tagUsedFunctions();
284 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000285
Corentin Walleza094a8a2015-04-07 11:53:06 -0700286 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
287 success = pruneUnusedFunctions(root);
288
Olli Etuahoc6833112015-04-22 15:15:54 +0300289 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
290 if (success)
291 PruneEmptyDeclarations(root);
292
Jamie Madill183bde52014-07-02 15:31:19 -0400293 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400294 success = validateOutputs(root);
295
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300296 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000297 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000298
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000299 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000300 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000301
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000302 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
303 rewriteCSSShader(root);
304
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000305 // Unroll for-loop markup needs to happen after validateLimitations pass.
306 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800307 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200308 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
309 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800310 root->traverse(&marker);
311 }
312 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800313 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200314 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
315 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800316 root->traverse(&marker);
317 if (marker.samplerArrayIndexIsFloatLoopIndex())
318 {
319 infoSink.info.prefix(EPrefixError);
320 infoSink.info << "sampler array index is float loop index";
321 success = false;
322 }
323 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000324
zmo@google.com32e97312011-08-24 01:03:11 +0000325 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200326 if (success)
327 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400328 // TODO(jmadill): Remove global pool allocator.
329 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200330 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400331 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000332 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200333 }
zmo@google.com32e97312011-08-24 01:03:11 +0000334
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000335 // Clamping uniform array bounds needs to happen after validateLimitations pass.
336 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
337 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
338
Ian Ewell924b7de2016-01-21 13:54:28 -0500339 // gl_Position is always written in compatibility output mode
340 if (success && shaderType == GL_VERTEX_SHADER &&
341 ((compileOptions & SH_INIT_GL_POSITION) ||
342 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800343 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400344
Corentin Wallezd4b50542015-09-28 12:19:26 -0700345 // This pass might emit short circuits so keep it before the short circuit unfolding
346 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
347 RewriteDoWhile(root, getTemporaryIndex());
348
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800349 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
350 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700351 UnfoldShortCircuitAST unfoldShortCircuit;
352 root->traverse(&unfoldShortCircuit);
353 unfoldShortCircuit.updateTree();
354 }
355
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300356 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
357 {
358 RemovePow(root);
359 }
360
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300361 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800362 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400363 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800364 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
365 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000366 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800367 if (!success)
368 {
Jamie Madill075edd82013-07-08 13:30:19 -0400369 infoSink.info.prefix(EPrefixError);
370 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000371 }
372 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700373 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
374 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300375 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700376 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000377 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000378
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700379 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
380 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700381 ScalarizeVecAndMatConstructorArgs scalarizer(
382 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700383 root->traverse(&scalarizer);
384 }
385
Zhenyao Moe740add2014-07-18 17:01:01 -0700386 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
387 {
388 RegenerateStructNames gen(symbolTable, shaderVersion);
389 root->traverse(&gen);
390 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300391
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700392 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
393 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
394 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
395 {
396 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
397 }
398
Olli Etuaho3d932d82016-04-12 11:10:30 +0300399 if (success)
400 {
401 DeferGlobalInitializers(root);
402 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000403 }
404
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700405 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200406 if (success)
407 return root;
408
409 return NULL;
410}
411
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700412bool TCompiler::compile(const char *const shaderStrings[], size_t numStrings, int compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200413{
414 if (numStrings == 0)
415 return true;
416
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700417 int compileOptions = compileOptionsIn;
418
419 // Apply key workarounds.
420 if (shouldFlattenPragmaStdglInvariantAll())
421 {
422 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
423 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
424 }
425
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200426 TScopedPoolAllocator scopedAlloc(&allocator);
427 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
428
429 if (root)
430 {
431 if (compileOptions & SH_INTERMEDIATE_TREE)
432 TIntermediate::outputTree(root, infoSink.info);
433
434 if (compileOptions & SH_OBJECT_CODE)
435 translate(root, compileOptions);
436
437 // The IntermNode tree doesn't need to be deleted here, since the
438 // memory will be freed in a big chunk by the PoolAllocator.
439 return true;
440 }
441 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000442}
443
Nicolas Capens49a88872013-06-20 09:54:03 -0400444bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000445{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000446 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400447 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000448
Nicolas Capens49a88872013-06-20 09:54:03 -0400449 assert(symbolTable.isEmpty());
450 symbolTable.push(); // COMMON_BUILTINS
451 symbolTable.push(); // ESSL1_BUILTINS
452 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300453 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000454
Nicolas Capens49a88872013-06-20 09:54:03 -0400455 TPublicType integer;
456 integer.type = EbtInt;
457 integer.primarySize = 1;
458 integer.secondarySize = 1;
459 integer.array = false;
460
461 TPublicType floatingPoint;
462 floatingPoint.type = EbtFloat;
463 floatingPoint.primarySize = 1;
464 floatingPoint.secondarySize = 1;
465 floatingPoint.array = false;
466
467 switch(shaderType)
468 {
Jamie Madill183bde52014-07-02 15:31:19 -0400469 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400470 symbolTable.setDefaultPrecision(integer, EbpMedium);
471 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400472 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400473 symbolTable.setDefaultPrecision(integer, EbpHigh);
474 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
475 break;
Martin Radev802abe02016-08-04 17:48:32 +0300476 case GL_COMPUTE_SHADER:
477 symbolTable.setDefaultPrecision(integer, EbpHigh);
478 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
479 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800480 default:
481 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400482 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200483 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400484 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200485 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
486 initSamplerDefaultPrecision(EbtSampler2D);
487 initSamplerDefaultPrecision(EbtSamplerCube);
488 // SamplerExternalOES is specified in the extension to have default precision.
489 initSamplerDefaultPrecision(EbtSamplerExternalOES);
490 // It isn't specified whether Sampler2DRect has default precision.
491 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400492
Jamie Madill1b452142013-07-12 14:51:11 -0400493 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400494
495 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
496
497 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000498}
499
Olli Etuaho183d7e22015-11-20 15:59:09 +0200500void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
501{
502 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
503 TPublicType sampler;
504 sampler.primarySize = 1;
505 sampler.secondarySize = 1;
506 sampler.array = false;
507 sampler.type = samplerType;
508 symbolTable.setDefaultPrecision(sampler, EbpLow);
509}
510
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400511void TCompiler::setResourceString()
512{
513 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400514
515 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400516 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
517 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
518 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
519 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
520 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
521 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
522 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
523 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
524 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
525 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400526 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
527 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400528 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
529 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
530 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
531 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
532 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200533 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300534 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400535 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
536 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100537 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
538 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
539 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400540 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
541 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
542 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300543 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300544 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200545 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
Martin Radeve93d24e2016-07-28 12:06:05 +0300546 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
547 << ":MaxImageUnits:" << compileResources.MaxImageUnits
548 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
549 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
550 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
551 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
552 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
553 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
554 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
555 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
556 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
557 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
558 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
559 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
560 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
561 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
562 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
563 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
564 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
565 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
566 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
567 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
568 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
569 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
570 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400571 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400572
573 builtInResourcesString = strstream.str();
574}
575
alokp@chromium.org07620a52010-09-23 17:53:56 +0000576void TCompiler::clearResults()
577{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000578 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000579 infoSink.info.erase();
580 infoSink.obj.erase();
581 infoSink.debug.erase();
582
Jamie Madilled27c722014-07-02 15:31:23 -0400583 attributes.clear();
584 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000585 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400586 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400587 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400588 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700589 variablesCollected = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000590
591 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000592
593 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200594
595 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700596 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000597}
598
Corentin Wallez71d147f2015-02-11 11:15:24 -0800599bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000600{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800601 mCallDag.clear();
602
603 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800604 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800605 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800606 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800607 case CallDAG::INITDAG_RECURSION:
608 infoSink.info.prefix(EPrefixError);
609 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800610 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800611 case CallDAG::INITDAG_UNDEFINED:
612 infoSink.info.prefix(EPrefixError);
613 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800614 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800615 }
616
617 UNREACHABLE();
618 return true;
619}
620
621bool TCompiler::checkCallDepth()
622{
623 std::vector<int> depths(mCallDag.size());
624
625 for (size_t i = 0; i < mCallDag.size(); i++)
626 {
627 int depth = 0;
628 auto &record = mCallDag.getRecordFromIndex(i);
629
630 for (auto &calleeIndex : record.callees)
631 {
632 depth = std::max(depth, depths[calleeIndex] + 1);
633 }
634
635 depths[i] = depth;
636
637 if (depth >= maxCallStackDepth)
638 {
639 // Trace back the function chain to have a meaningful info log.
640 infoSink.info.prefix(EPrefixError);
641 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
642 << ") with the following call chain: " << record.name;
643
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700644 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800645 int currentDepth = depth;
646
647 while (currentFunction != -1)
648 {
649 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
650
651 int nextFunction = -1;
652 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
653 {
654 if (depths[calleeIndex] == currentDepth - 1)
655 {
656 currentDepth--;
657 nextFunction = calleeIndex;
658 }
659 }
660
661 currentFunction = nextFunction;
662 }
663
664 return false;
665 }
666 }
667
668 return true;
669}
670
671bool TCompiler::tagUsedFunctions()
672{
673 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700674 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800675 {
676 if (mCallDag.getRecordFromIndex(i).name == "main(")
677 {
678 internalTagUsedFunction(i);
679 return true;
680 }
681 }
682
683 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200684 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800685 return false;
686}
687
688void TCompiler::internalTagUsedFunction(size_t index)
689{
690 if (functionMetadata[index].used)
691 {
692 return;
693 }
694
695 functionMetadata[index].used = true;
696
697 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
698 {
699 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000700 }
701}
702
Corentin Walleza094a8a2015-04-07 11:53:06 -0700703// A predicate for the stl that returns if a top-level node is unused
704class TCompiler::UnusedPredicate
705{
706 public:
707 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
708 : mCallDag(callDag),
709 mMetadatas(metadatas)
710 {
711 }
712
713 bool operator ()(TIntermNode *node)
714 {
715 const TIntermAggregate *asAggregate = node->getAsAggregate();
716
717 if (asAggregate == nullptr)
718 {
719 return false;
720 }
721
722 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
723 {
724 return false;
725 }
726
727 size_t callDagIndex = mCallDag->findIndex(asAggregate);
728 if (callDagIndex == CallDAG::InvalidIndex)
729 {
730 // This happens only for unimplemented prototypes which are thus unused
731 ASSERT(asAggregate->getOp() == EOpPrototype);
732 return true;
733 }
734
735 ASSERT(callDagIndex < mMetadatas->size());
736 return !(*mMetadatas)[callDagIndex].used;
737 }
738
739 private:
740 const CallDAG *mCallDag;
741 const std::vector<FunctionMetadata> *mMetadatas;
742};
743
744bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
745{
746 TIntermAggregate *rootNode = root->getAsAggregate();
747 ASSERT(rootNode != nullptr);
748
749 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
750 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700751
752 if (!sequence->empty())
753 {
754 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
755 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700756
757 return true;
758}
759
Jamie Madill05a80ce2013-06-20 11:55:49 -0400760bool TCompiler::validateOutputs(TIntermNode* root)
761{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300762 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400763 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300764 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400765}
766
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000767void TCompiler::rewriteCSSShader(TIntermNode* root)
768{
769 RenameFunction renamer("main(", "css_main(");
770 root->traverse(&renamer);
771}
772
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800773bool TCompiler::validateLimitations(TIntermNode* root)
774{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200775 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000776 root->traverse(&validate);
777 return validate.numErrors() == 0;
778}
779
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000780bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000781{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800782 if (shaderSpec != SH_WEBGL_SPEC)
783 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000784 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
785 return false;
786 }
787
Jamie Madill183bde52014-07-02 15:31:19 -0400788 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800789 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000790 TDependencyGraph graph(root);
791
792 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000793 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500794
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000795 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800796 if (outputGraph)
797 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000798 TDependencyGraphOutput output(infoSink.info);
799 output.outputAllSpanningTrees(graph);
800 }
Jamie Madill5508f392014-02-20 13:31:36 -0500801
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000802 return success;
803 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800804 else
805 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000806 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000807 }
808}
809
Jamie Madilleb1a0102013-07-08 13:31:38 -0400810bool TCompiler::limitExpressionComplexity(TIntermNode* root)
811{
Jamie Madill6654bc92014-03-26 14:01:57 -0400812 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400813 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400814
815 if (traverser.getMaxDepth() > maxExpressionComplexity)
816 {
817 infoSink.info << "Expression too complex.";
818 return false;
819 }
820
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200821 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
822 {
823 infoSink.info << "Function has too many parameters.";
824 return false;
825 }
826
Jamie Madilleb1a0102013-07-08 13:31:38 -0400827 return true;
828}
829
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000830bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000831{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000832 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000833 restrictor.enforceRestrictions(graph);
834 return restrictor.numErrors() == 0;
835}
836
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000837bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000838{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000839 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000840 restrictor.enforceRestrictions(root);
841 return restrictor.numErrors() == 0;
842}
843
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400844void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000845{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700846 if (!variablesCollected)
847 {
848 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
849 &interfaceBlocks, hashFunction, symbolTable, extensionBehavior);
850 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400851
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700852 // This is for enforcePackingRestriction().
853 sh::ExpandUniforms(uniforms, &expandedUniforms);
854 variablesCollected = true;
855 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000856}
zmo@google.comfd747b82011-04-23 01:30:07 +0000857
gman@chromium.org8d804792012-10-17 21:33:48 +0000858bool TCompiler::enforcePackingRestrictions()
859{
860 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400861 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000862}
863
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800864void TCompiler::initializeGLPosition(TIntermNode* root)
865{
Zhenyao Mo72111912016-07-20 17:45:56 -0700866 InitVariableList list;
867 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
868 var.name = "gl_Position";
869 list.push_back(var);
870 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800871}
872
Olli Etuaho27776e32016-07-22 14:00:56 +0300873void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800874{
Zhenyao Mo72111912016-07-20 17:45:56 -0700875 InitVariableList list;
876 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800877 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700878 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800879 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700880 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800881 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800882 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700883 else
884 {
885 ASSERT(shaderType == GL_FRAGMENT_SHADER);
886 for (auto var : outputVariables)
887 {
888 list.push_back(var);
889 }
890 }
891 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800892}
893
zmo@google.com5601ea02011-06-10 18:23:25 +0000894const TExtensionBehavior& TCompiler::getExtensionBehavior() const
895{
896 return extensionBehavior;
897}
zmo@google.com32e97312011-08-24 01:03:11 +0000898
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200899const char *TCompiler::getSourcePath() const
900{
901 return mSourcePath;
902}
903
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000904const ShBuiltInResources& TCompiler::getResources() const
905{
906 return compileResources;
907}
908
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000909const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
910{
911 return arrayBoundsClamper;
912}
913
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000914ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
915{
916 return clampingStrategy;
917}
918
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200919const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000920{
921 return builtInFunctionEmulator;
922}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700923
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700924void TCompiler::writePragma(int compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700925{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700926 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
927 {
928 TInfoSinkBase &sink = infoSink.obj;
929 if (mPragma.stdgl.invariantAll)
930 sink << "#pragma STDGL invariant(all)\n";
931 }
932}
933
934bool TCompiler::isVaryingDefined(const char *varyingName)
935{
936 ASSERT(variablesCollected);
937 for (size_t ii = 0; ii < varyings.size(); ++ii)
938 {
939 if (varyings[ii].name == varyingName)
940 {
941 return true;
942 }
943 }
944
945 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700946}