blob: 1f1f831065fe2b08cd7ff5af865f787b1deaef8c [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"
Corentin Wallezd4b50542015-09-28 12:19:26 -070020#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070021#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070022#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040023#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020024#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040025#include "compiler/translator/ValidateOutputs.h"
26#include "compiler/translator/VariablePacker.h"
27#include "compiler/translator/depgraph/DependencyGraph.h"
28#include "compiler/translator/depgraph/DependencyGraphOutput.h"
29#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
30#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000031#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040032#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040033#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000034
Jamie Madill5508f392014-02-20 13:31:36 -050035bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000036{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080037 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000038}
39
Qingqing Dengad0d0792015-04-08 14:25:06 -070040bool IsGLSL130OrNewer(ShShaderOutput output)
41{
42 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070043 output == SH_GLSL_140_OUTPUT ||
44 output == SH_GLSL_150_CORE_OUTPUT ||
45 output == SH_GLSL_330_CORE_OUTPUT ||
46 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070047 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070048 output == SH_GLSL_420_CORE_OUTPUT ||
49 output == SH_GLSL_430_CORE_OUTPUT ||
50 output == SH_GLSL_440_CORE_OUTPUT ||
51 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070052}
53
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070054size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050055{
Jamie Madill88f6e942014-02-19 10:27:53 -050056 // WebGL defines a max token legnth of 256, while ES2 leaves max token
57 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070058 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050059 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070060 case SH_WEBGL_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050061 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070062 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050063 return 1024;
64 }
65}
66
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000067namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070068
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080069class TScopedPoolAllocator
70{
71 public:
72 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
73 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040074 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000075 SetGlobalPoolAllocator(mAllocator);
76 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080077 ~TScopedPoolAllocator()
78 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000079 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040080 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000081 }
82
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080083 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000084 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040085};
86
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080087class TScopedSymbolTableLevel
88{
89 public:
90 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
91 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040092 ASSERT(mTable->atBuiltInLevel());
93 mTable->push();
94 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080095 ~TScopedSymbolTableLevel()
96 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040097 while (!mTable->atBuiltInLevel())
98 mTable->pop();
99 }
100
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800101 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400102 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000103};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700104
105int MapSpecToShaderVersion(ShShaderSpec spec)
106{
107 switch (spec)
108 {
109 case SH_GLES2_SPEC:
110 case SH_WEBGL_SPEC:
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700111 return 100;
112 case SH_GLES3_SPEC:
113 case SH_WEBGL2_SPEC:
114 return 300;
Martin Radev1be913c2016-07-11 17:59:16 +0300115 case SH_GLES3_1_SPEC:
116 case SH_WEBGL3_SPEC:
117 return 310;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700118 default:
119 UNREACHABLE();
120 return 0;
121 }
122}
123
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000124} // namespace
125
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800126TShHandleBase::TShHandleBase()
127{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000128 allocator.push();
129 SetGlobalPoolAllocator(&allocator);
130}
131
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800132TShHandleBase::~TShHandleBase()
133{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000134 SetGlobalPoolAllocator(NULL);
135 allocator.popAll();
136}
137
Jamie Madill183bde52014-07-02 15:31:19 -0400138TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700139 : variablesCollected(false),
140 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000141 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400142 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400143 maxUniformVectors(0),
144 maxExpressionComplexity(0),
145 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200146 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000147 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000148 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200149 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700150 mSourcePath(NULL),
Martin Radev802abe02016-08-04 17:48:32 +0300151 mComputeShaderLocalSizeDeclared(false),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700152 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000153{
Martin Radev802abe02016-08-04 17:48:32 +0300154 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000155}
156
157TCompiler::~TCompiler()
158{
159}
160
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300161bool TCompiler::shouldRunLoopAndIndexingValidation(int compileOptions) const
162{
163 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
164 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
165 // of ESSL 1.00 as in Appendix A of the spec).
166 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
167 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
168}
169
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000170bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000171{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000172 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400173 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000174 resources.MaxVertexUniformVectors :
175 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400176 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200177 maxCallStackDepth = resources.MaxCallStackDepth;
178 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400179
180 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000181
alokp@chromium.org07620a52010-09-23 17:53:56 +0000182 // Generate built-in symbol table.
183 if (!InitBuiltInSymbolTable(resources))
184 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000185 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000186 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000187
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000188 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
189 clampingStrategy = resources.ArrayIndexClampingStrategy;
190
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000191 hashFunction = resources.HashFunction;
192
alokp@chromium.org07620a52010-09-23 17:53:56 +0000193 return true;
194}
195
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200196TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
197 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000198{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200199 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
200}
201
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300202TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
203 size_t numStrings,
204 const int compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200205{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000206 clearResults();
207
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200208 ASSERT(numStrings > 0);
209 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000210
David Yen0fbd1282015-02-02 14:46:09 -0800211 // Reset the extension behavior for each compilation unit.
212 ResetExtensionBehavior(extensionBehavior);
213
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000214 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000215 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000216 if (compileOptions & SH_SOURCE_PATH)
217 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200218 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000219 ++firstSource;
220 }
221
Olli Etuahof119a262016-08-19 15:54:22 +0300222 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200223 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200224
Olli Etuahoa6996682015-10-12 14:32:30 +0300225 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400226 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000227
228 // We preserve symbols at the built-in level from compile-to-compile.
229 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400230 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000231
232 // Parse shader.
233 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400234 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
235 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000236
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000237 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700238 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
239 {
240 infoSink.info.prefix(EPrefixError);
241 infoSink.info << "unsupported shader version";
242 success = false;
243 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000244
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400245 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200246
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800247 if (success)
248 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700249 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700250 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700251
Martin Radev802abe02016-08-04 17:48:32 +0300252 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
253 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
254
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400255 root = parseContext.getTreeRoot();
Olli Etuahof119a262016-08-19 15:54:22 +0300256 root = TIntermediate::PostProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000257
Olli Etuahoa6996682015-10-12 14:32:30 +0300258 // Highp might have been auto-enabled based on shader version
259 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
260
Jamie Madill6654bc92014-03-26 14:01:57 -0400261 // Disallow expressions deemed too complex.
262 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
263 success = limitExpressionComplexity(root);
264
Corentin Wallez71d147f2015-02-11 11:15:24 -0800265 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000266 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800267 success = initCallDag(root);
268
269 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
270 success = checkCallDepth();
271
272 // Checks which functions are used and if "main" exists
273 if (success)
274 {
275 functionMetadata.clear();
276 functionMetadata.resize(mCallDag.size());
277 success = tagUsedFunctions();
278 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000279
Corentin Walleza094a8a2015-04-07 11:53:06 -0700280 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
281 success = pruneUnusedFunctions(root);
282
Olli Etuahoc6833112015-04-22 15:15:54 +0300283 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
284 if (success)
285 PruneEmptyDeclarations(root);
286
Jamie Madill183bde52014-07-02 15:31:19 -0400287 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400288 success = validateOutputs(root);
289
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300290 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000291 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000292
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000293 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000294 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000295
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000296 // Unroll for-loop markup needs to happen after validateLimitations pass.
297 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800298 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200299 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
300 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800301 root->traverse(&marker);
302 }
303 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800304 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200305 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
306 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800307 root->traverse(&marker);
308 if (marker.samplerArrayIndexIsFloatLoopIndex())
309 {
310 infoSink.info.prefix(EPrefixError);
311 infoSink.info << "sampler array index is float loop index";
312 success = false;
313 }
314 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000315
zmo@google.com32e97312011-08-24 01:03:11 +0000316 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200317 if (success)
318 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400319 // TODO(jmadill): Remove global pool allocator.
320 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200321 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400322 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000323 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200324 }
zmo@google.com32e97312011-08-24 01:03:11 +0000325
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000326 // Clamping uniform array bounds needs to happen after validateLimitations pass.
327 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
328 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
329
Ian Ewell924b7de2016-01-21 13:54:28 -0500330 // gl_Position is always written in compatibility output mode
331 if (success && shaderType == GL_VERTEX_SHADER &&
332 ((compileOptions & SH_INIT_GL_POSITION) ||
333 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800334 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400335
Corentin Wallezd4b50542015-09-28 12:19:26 -0700336 // This pass might emit short circuits so keep it before the short circuit unfolding
337 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
338 RewriteDoWhile(root, getTemporaryIndex());
339
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800340 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
341 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700342 UnfoldShortCircuitAST unfoldShortCircuit;
343 root->traverse(&unfoldShortCircuit);
344 unfoldShortCircuit.updateTree();
345 }
346
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300347 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
348 {
349 RemovePow(root);
350 }
351
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300352 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800353 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400354 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800355 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
356 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000357 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800358 if (!success)
359 {
Jamie Madill075edd82013-07-08 13:30:19 -0400360 infoSink.info.prefix(EPrefixError);
361 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000362 }
363 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700364 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
365 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300366 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700367 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000368 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000369
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700370 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
371 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700372 ScalarizeVecAndMatConstructorArgs scalarizer(
373 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700374 root->traverse(&scalarizer);
375 }
376
Zhenyao Moe740add2014-07-18 17:01:01 -0700377 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
378 {
379 RegenerateStructNames gen(symbolTable, shaderVersion);
380 root->traverse(&gen);
381 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300382
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700383 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
384 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
385 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
386 {
387 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
388 }
389
Olli Etuaho3d932d82016-04-12 11:10:30 +0300390 if (success)
391 {
392 DeferGlobalInitializers(root);
393 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000394 }
395
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700396 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200397 if (success)
398 return root;
399
400 return NULL;
401}
402
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700403bool TCompiler::compile(const char *const shaderStrings[], size_t numStrings, int compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200404{
405 if (numStrings == 0)
406 return true;
407
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700408 int compileOptions = compileOptionsIn;
409
410 // Apply key workarounds.
411 if (shouldFlattenPragmaStdglInvariantAll())
412 {
413 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
414 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
415 }
416
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200417 TScopedPoolAllocator scopedAlloc(&allocator);
418 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
419
420 if (root)
421 {
422 if (compileOptions & SH_INTERMEDIATE_TREE)
423 TIntermediate::outputTree(root, infoSink.info);
424
425 if (compileOptions & SH_OBJECT_CODE)
426 translate(root, compileOptions);
427
428 // The IntermNode tree doesn't need to be deleted here, since the
429 // memory will be freed in a big chunk by the PoolAllocator.
430 return true;
431 }
432 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000433}
434
Nicolas Capens49a88872013-06-20 09:54:03 -0400435bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000436{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000437 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400438 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000439
Nicolas Capens49a88872013-06-20 09:54:03 -0400440 assert(symbolTable.isEmpty());
441 symbolTable.push(); // COMMON_BUILTINS
442 symbolTable.push(); // ESSL1_BUILTINS
443 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300444 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000445
Nicolas Capens49a88872013-06-20 09:54:03 -0400446 TPublicType integer;
Martin Radev4a9cd802016-09-01 16:51:51 +0300447 integer.setBasicType(EbtInt);
448 integer.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400449 integer.array = false;
450
451 TPublicType floatingPoint;
Martin Radev4a9cd802016-09-01 16:51:51 +0300452 floatingPoint.setBasicType(EbtFloat);
453 floatingPoint.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400454 floatingPoint.array = false;
455
456 switch(shaderType)
457 {
Jamie Madill183bde52014-07-02 15:31:19 -0400458 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400459 symbolTable.setDefaultPrecision(integer, EbpMedium);
460 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400461 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400462 symbolTable.setDefaultPrecision(integer, EbpHigh);
463 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
464 break;
Martin Radev802abe02016-08-04 17:48:32 +0300465 case GL_COMPUTE_SHADER:
466 symbolTable.setDefaultPrecision(integer, EbpHigh);
467 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
468 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800469 default:
470 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400471 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200472 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400473 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200474 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
475 initSamplerDefaultPrecision(EbtSampler2D);
476 initSamplerDefaultPrecision(EbtSamplerCube);
477 // SamplerExternalOES is specified in the extension to have default precision.
478 initSamplerDefaultPrecision(EbtSamplerExternalOES);
479 // It isn't specified whether Sampler2DRect has default precision.
480 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400481
Jamie Madill1b452142013-07-12 14:51:11 -0400482 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400483
484 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
485
486 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000487}
488
Olli Etuaho183d7e22015-11-20 15:59:09 +0200489void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
490{
491 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
492 TPublicType sampler;
Martin Radev4a9cd802016-09-01 16:51:51 +0300493 sampler.initializeSizeForScalarTypes();
494 sampler.setBasicType(samplerType);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200495 sampler.array = false;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200496 symbolTable.setDefaultPrecision(sampler, EbpLow);
497}
498
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400499void TCompiler::setResourceString()
500{
501 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400502
503 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400504 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
505 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
506 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
507 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
508 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
509 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
510 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
511 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
512 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
513 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400514 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
515 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400516 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
517 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
518 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
519 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
520 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200521 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300522 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400523 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
524 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100525 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
526 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
527 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400528 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
529 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
530 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300531 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300532 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200533 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
Martin Radeve93d24e2016-07-28 12:06:05 +0300534 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
535 << ":MaxImageUnits:" << compileResources.MaxImageUnits
536 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
537 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
538 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
539 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
540 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
541 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
542 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
543 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
544 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
545 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
546 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
547 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
548 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
549 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
550 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
551 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
552 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
553 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
554 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
555 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
556 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
557 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
558 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400559 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400560
561 builtInResourcesString = strstream.str();
562}
563
alokp@chromium.org07620a52010-09-23 17:53:56 +0000564void TCompiler::clearResults()
565{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000566 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000567 infoSink.info.erase();
568 infoSink.obj.erase();
569 infoSink.debug.erase();
570
Jamie Madilled27c722014-07-02 15:31:23 -0400571 attributes.clear();
572 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000573 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400574 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400575 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400576 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700577 variablesCollected = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000578
579 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000580
581 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200582
583 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700584 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000585}
586
Corentin Wallez71d147f2015-02-11 11:15:24 -0800587bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000588{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800589 mCallDag.clear();
590
591 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800592 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800593 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800594 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800595 case CallDAG::INITDAG_RECURSION:
596 infoSink.info.prefix(EPrefixError);
597 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800598 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800599 case CallDAG::INITDAG_UNDEFINED:
600 infoSink.info.prefix(EPrefixError);
601 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800602 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800603 }
604
605 UNREACHABLE();
606 return true;
607}
608
609bool TCompiler::checkCallDepth()
610{
611 std::vector<int> depths(mCallDag.size());
612
613 for (size_t i = 0; i < mCallDag.size(); i++)
614 {
615 int depth = 0;
616 auto &record = mCallDag.getRecordFromIndex(i);
617
618 for (auto &calleeIndex : record.callees)
619 {
620 depth = std::max(depth, depths[calleeIndex] + 1);
621 }
622
623 depths[i] = depth;
624
625 if (depth >= maxCallStackDepth)
626 {
627 // Trace back the function chain to have a meaningful info log.
628 infoSink.info.prefix(EPrefixError);
629 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
630 << ") with the following call chain: " << record.name;
631
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700632 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800633 int currentDepth = depth;
634
635 while (currentFunction != -1)
636 {
637 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
638
639 int nextFunction = -1;
640 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
641 {
642 if (depths[calleeIndex] == currentDepth - 1)
643 {
644 currentDepth--;
645 nextFunction = calleeIndex;
646 }
647 }
648
649 currentFunction = nextFunction;
650 }
651
652 return false;
653 }
654 }
655
656 return true;
657}
658
659bool TCompiler::tagUsedFunctions()
660{
661 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700662 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800663 {
664 if (mCallDag.getRecordFromIndex(i).name == "main(")
665 {
666 internalTagUsedFunction(i);
667 return true;
668 }
669 }
670
671 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200672 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800673 return false;
674}
675
676void TCompiler::internalTagUsedFunction(size_t index)
677{
678 if (functionMetadata[index].used)
679 {
680 return;
681 }
682
683 functionMetadata[index].used = true;
684
685 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
686 {
687 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000688 }
689}
690
Corentin Walleza094a8a2015-04-07 11:53:06 -0700691// A predicate for the stl that returns if a top-level node is unused
692class TCompiler::UnusedPredicate
693{
694 public:
695 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
696 : mCallDag(callDag),
697 mMetadatas(metadatas)
698 {
699 }
700
701 bool operator ()(TIntermNode *node)
702 {
703 const TIntermAggregate *asAggregate = node->getAsAggregate();
704
705 if (asAggregate == nullptr)
706 {
707 return false;
708 }
709
710 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
711 {
712 return false;
713 }
714
715 size_t callDagIndex = mCallDag->findIndex(asAggregate);
716 if (callDagIndex == CallDAG::InvalidIndex)
717 {
718 // This happens only for unimplemented prototypes which are thus unused
719 ASSERT(asAggregate->getOp() == EOpPrototype);
720 return true;
721 }
722
723 ASSERT(callDagIndex < mMetadatas->size());
724 return !(*mMetadatas)[callDagIndex].used;
725 }
726
727 private:
728 const CallDAG *mCallDag;
729 const std::vector<FunctionMetadata> *mMetadatas;
730};
731
732bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
733{
734 TIntermAggregate *rootNode = root->getAsAggregate();
735 ASSERT(rootNode != nullptr);
736
737 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
738 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700739
740 if (!sequence->empty())
741 {
742 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
743 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700744
745 return true;
746}
747
Jamie Madill05a80ce2013-06-20 11:55:49 -0400748bool TCompiler::validateOutputs(TIntermNode* root)
749{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300750 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400751 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300752 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400753}
754
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800755bool TCompiler::validateLimitations(TIntermNode* root)
756{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200757 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000758 root->traverse(&validate);
759 return validate.numErrors() == 0;
760}
761
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000762bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000763{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800764 if (shaderSpec != SH_WEBGL_SPEC)
765 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000766 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
767 return false;
768 }
769
Jamie Madill183bde52014-07-02 15:31:19 -0400770 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800771 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000772 TDependencyGraph graph(root);
773
774 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000775 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500776
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000777 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800778 if (outputGraph)
779 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000780 TDependencyGraphOutput output(infoSink.info);
781 output.outputAllSpanningTrees(graph);
782 }
Jamie Madill5508f392014-02-20 13:31:36 -0500783
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000784 return success;
785 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800786 else
787 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000788 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000789 }
790}
791
Jamie Madilleb1a0102013-07-08 13:31:38 -0400792bool TCompiler::limitExpressionComplexity(TIntermNode* root)
793{
Jamie Madill6654bc92014-03-26 14:01:57 -0400794 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400795 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400796
797 if (traverser.getMaxDepth() > maxExpressionComplexity)
798 {
799 infoSink.info << "Expression too complex.";
800 return false;
801 }
802
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200803 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
804 {
805 infoSink.info << "Function has too many parameters.";
806 return false;
807 }
808
Jamie Madilleb1a0102013-07-08 13:31:38 -0400809 return true;
810}
811
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000812bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000813{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000814 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000815 restrictor.enforceRestrictions(graph);
816 return restrictor.numErrors() == 0;
817}
818
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000819bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000820{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000821 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000822 restrictor.enforceRestrictions(root);
823 return restrictor.numErrors() == 0;
824}
825
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400826void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000827{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700828 if (!variablesCollected)
829 {
830 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
831 &interfaceBlocks, hashFunction, symbolTable, extensionBehavior);
832 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400833
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700834 // This is for enforcePackingRestriction().
835 sh::ExpandUniforms(uniforms, &expandedUniforms);
836 variablesCollected = true;
837 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000838}
zmo@google.comfd747b82011-04-23 01:30:07 +0000839
gman@chromium.org8d804792012-10-17 21:33:48 +0000840bool TCompiler::enforcePackingRestrictions()
841{
842 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400843 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000844}
845
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800846void TCompiler::initializeGLPosition(TIntermNode* root)
847{
Zhenyao Mo72111912016-07-20 17:45:56 -0700848 InitVariableList list;
849 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
850 var.name = "gl_Position";
851 list.push_back(var);
852 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800853}
854
Olli Etuaho27776e32016-07-22 14:00:56 +0300855void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800856{
Zhenyao Mo72111912016-07-20 17:45:56 -0700857 InitVariableList list;
858 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800859 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700860 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800861 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700862 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800863 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800864 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700865 else
866 {
867 ASSERT(shaderType == GL_FRAGMENT_SHADER);
868 for (auto var : outputVariables)
869 {
870 list.push_back(var);
871 }
872 }
873 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800874}
875
zmo@google.com5601ea02011-06-10 18:23:25 +0000876const TExtensionBehavior& TCompiler::getExtensionBehavior() const
877{
878 return extensionBehavior;
879}
zmo@google.com32e97312011-08-24 01:03:11 +0000880
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200881const char *TCompiler::getSourcePath() const
882{
883 return mSourcePath;
884}
885
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000886const ShBuiltInResources& TCompiler::getResources() const
887{
888 return compileResources;
889}
890
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000891const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
892{
893 return arrayBoundsClamper;
894}
895
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000896ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
897{
898 return clampingStrategy;
899}
900
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200901const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000902{
903 return builtInFunctionEmulator;
904}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700905
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700906void TCompiler::writePragma(int compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700907{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700908 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
909 {
910 TInfoSinkBase &sink = infoSink.obj;
911 if (mPragma.stdgl.invariantAll)
912 sink << "#pragma STDGL invariant(all)\n";
913 }
914}
915
916bool TCompiler::isVaryingDefined(const char *varyingName)
917{
918 ASSERT(variablesCollected);
919 for (size_t ii = 0; ii < varyings.size(); ++ii)
920 {
921 if (varyings[ii].name == varyingName)
922 {
923 return true;
924 }
925 }
926
927 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700928}