blob: 62dc2cf2919a332d7dfe4a759bfe7f764750223f [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)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000143 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000144 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400145 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400146 maxUniformVectors(0),
147 maxExpressionComplexity(0),
148 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200149 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000150 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000151 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200152 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700153 mSourcePath(NULL),
Martin Radev802abe02016-08-04 17:48:32 +0300154 mComputeShaderLocalSizeDeclared(false),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700155 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000156{
Martin Radev802abe02016-08-04 17:48:32 +0300157 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000158}
159
160TCompiler::~TCompiler()
161{
162}
163
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300164bool TCompiler::shouldRunLoopAndIndexingValidation(int compileOptions) const
165{
166 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
167 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
168 // of ESSL 1.00 as in Appendix A of the spec).
169 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
170 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
171}
172
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000173bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000174{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000175 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400176 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000177 resources.MaxVertexUniformVectors :
178 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400179 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200180 maxCallStackDepth = resources.MaxCallStackDepth;
181 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400182
183 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000184
alokp@chromium.org07620a52010-09-23 17:53:56 +0000185 // Generate built-in symbol table.
186 if (!InitBuiltInSymbolTable(resources))
187 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000188 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000189 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000190
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000191 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
192 clampingStrategy = resources.ArrayIndexClampingStrategy;
193
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000194 hashFunction = resources.HashFunction;
195
alokp@chromium.org07620a52010-09-23 17:53:56 +0000196 return true;
197}
198
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200199TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
200 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000201{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200202 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
203}
204
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300205TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
206 size_t numStrings,
207 const int compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200208{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000209 clearResults();
210
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200211 ASSERT(numStrings > 0);
212 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000213
David Yen0fbd1282015-02-02 14:46:09 -0800214 // Reset the extension behavior for each compilation unit.
215 ResetExtensionBehavior(extensionBehavior);
216
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000217 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000218 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000219 if (compileOptions & SH_SOURCE_PATH)
220 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200221 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000222 ++firstSource;
223 }
224
alokp@chromium.org07620a52010-09-23 17:53:56 +0000225 TIntermediate intermediate(infoSink);
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200226 TParseContext parseContext(symbolTable, extensionBehavior, intermediate, shaderType, shaderSpec,
227 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200228
Olli Etuahoa6996682015-10-12 14:32:30 +0300229 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400230 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000231
232 // We preserve symbols at the built-in level from compile-to-compile.
233 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400234 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000235
236 // Parse shader.
237 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400238 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
239 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000240
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000241 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700242 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
243 {
244 infoSink.info.prefix(EPrefixError);
245 infoSink.info << "unsupported shader version";
246 success = false;
247 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000248
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400249 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200250
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800251 if (success)
252 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700253 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700254 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700255
Martin Radev802abe02016-08-04 17:48:32 +0300256 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
257 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
258
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400259 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300260 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000261
Olli Etuahoa6996682015-10-12 14:32:30 +0300262 // Highp might have been auto-enabled based on shader version
263 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
264
Jamie Madill6654bc92014-03-26 14:01:57 -0400265 // Disallow expressions deemed too complex.
266 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
267 success = limitExpressionComplexity(root);
268
Corentin Wallez71d147f2015-02-11 11:15:24 -0800269 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000270 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800271 success = initCallDag(root);
272
273 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
274 success = checkCallDepth();
275
276 // Checks which functions are used and if "main" exists
277 if (success)
278 {
279 functionMetadata.clear();
280 functionMetadata.resize(mCallDag.size());
281 success = tagUsedFunctions();
282 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000283
Corentin Walleza094a8a2015-04-07 11:53:06 -0700284 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
285 success = pruneUnusedFunctions(root);
286
Olli Etuahoc6833112015-04-22 15:15:54 +0300287 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
288 if (success)
289 PruneEmptyDeclarations(root);
290
Jamie Madill183bde52014-07-02 15:31:19 -0400291 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400292 success = validateOutputs(root);
293
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300294 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000295 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000296
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000297 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000298 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000299
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000300 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
301 rewriteCSSShader(root);
302
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000303 // Unroll for-loop markup needs to happen after validateLimitations pass.
304 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800305 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200306 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
307 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800308 root->traverse(&marker);
309 }
310 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800311 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200312 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
313 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800314 root->traverse(&marker);
315 if (marker.samplerArrayIndexIsFloatLoopIndex())
316 {
317 infoSink.info.prefix(EPrefixError);
318 infoSink.info << "sampler array index is float loop index";
319 success = false;
320 }
321 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000322
zmo@google.com32e97312011-08-24 01:03:11 +0000323 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200324 if (success)
325 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400326 // TODO(jmadill): Remove global pool allocator.
327 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200328 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400329 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000330 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200331 }
zmo@google.com32e97312011-08-24 01:03:11 +0000332
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000333 // Clamping uniform array bounds needs to happen after validateLimitations pass.
334 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
335 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
336
Ian Ewell924b7de2016-01-21 13:54:28 -0500337 // gl_Position is always written in compatibility output mode
338 if (success && shaderType == GL_VERTEX_SHADER &&
339 ((compileOptions & SH_INIT_GL_POSITION) ||
340 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800341 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400342
Corentin Wallezd4b50542015-09-28 12:19:26 -0700343 // This pass might emit short circuits so keep it before the short circuit unfolding
344 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
345 RewriteDoWhile(root, getTemporaryIndex());
346
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800347 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
348 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700349 UnfoldShortCircuitAST unfoldShortCircuit;
350 root->traverse(&unfoldShortCircuit);
351 unfoldShortCircuit.updateTree();
352 }
353
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300354 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
355 {
356 RemovePow(root);
357 }
358
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300359 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800360 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400361 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800362 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
363 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000364 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800365 if (!success)
366 {
Jamie Madill075edd82013-07-08 13:30:19 -0400367 infoSink.info.prefix(EPrefixError);
368 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000369 }
370 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700371 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
372 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300373 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700374 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000375 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000376
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700377 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
378 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700379 ScalarizeVecAndMatConstructorArgs scalarizer(
380 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700381 root->traverse(&scalarizer);
382 }
383
Zhenyao Moe740add2014-07-18 17:01:01 -0700384 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
385 {
386 RegenerateStructNames gen(symbolTable, shaderVersion);
387 root->traverse(&gen);
388 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300389
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700390 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
391 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
392 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
393 {
394 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
395 }
396
Olli Etuaho3d932d82016-04-12 11:10:30 +0300397 if (success)
398 {
399 DeferGlobalInitializers(root);
400 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000401 }
402
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700403 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200404 if (success)
405 return root;
406
407 return NULL;
408}
409
410bool TCompiler::compile(const char* const shaderStrings[],
411 size_t numStrings, int compileOptions)
412{
413 if (numStrings == 0)
414 return true;
415
416 TScopedPoolAllocator scopedAlloc(&allocator);
417 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
418
419 if (root)
420 {
421 if (compileOptions & SH_INTERMEDIATE_TREE)
422 TIntermediate::outputTree(root, infoSink.info);
423
424 if (compileOptions & SH_OBJECT_CODE)
425 translate(root, compileOptions);
426
427 // The IntermNode tree doesn't need to be deleted here, since the
428 // memory will be freed in a big chunk by the PoolAllocator.
429 return true;
430 }
431 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000432}
433
Nicolas Capens49a88872013-06-20 09:54:03 -0400434bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000435{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000436 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400437 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000438
Nicolas Capens49a88872013-06-20 09:54:03 -0400439 assert(symbolTable.isEmpty());
440 symbolTable.push(); // COMMON_BUILTINS
441 symbolTable.push(); // ESSL1_BUILTINS
442 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300443 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000444
Nicolas Capens49a88872013-06-20 09:54:03 -0400445 TPublicType integer;
446 integer.type = EbtInt;
447 integer.primarySize = 1;
448 integer.secondarySize = 1;
449 integer.array = false;
450
451 TPublicType floatingPoint;
452 floatingPoint.type = EbtFloat;
453 floatingPoint.primarySize = 1;
454 floatingPoint.secondarySize = 1;
455 floatingPoint.array = false;
456
457 switch(shaderType)
458 {
Jamie Madill183bde52014-07-02 15:31:19 -0400459 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400460 symbolTable.setDefaultPrecision(integer, EbpMedium);
461 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400462 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400463 symbolTable.setDefaultPrecision(integer, EbpHigh);
464 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
465 break;
Martin Radev802abe02016-08-04 17:48:32 +0300466 case GL_COMPUTE_SHADER:
467 symbolTable.setDefaultPrecision(integer, EbpHigh);
468 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
469 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800470 default:
471 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400472 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200473 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400474 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200475 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
476 initSamplerDefaultPrecision(EbtSampler2D);
477 initSamplerDefaultPrecision(EbtSamplerCube);
478 // SamplerExternalOES is specified in the extension to have default precision.
479 initSamplerDefaultPrecision(EbtSamplerExternalOES);
480 // It isn't specified whether Sampler2DRect has default precision.
481 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400482
Jamie Madill1b452142013-07-12 14:51:11 -0400483 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400484
485 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
486
487 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000488}
489
Olli Etuaho183d7e22015-11-20 15:59:09 +0200490void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
491{
492 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
493 TPublicType sampler;
494 sampler.primarySize = 1;
495 sampler.secondarySize = 1;
496 sampler.array = false;
497 sampler.type = samplerType;
498 symbolTable.setDefaultPrecision(sampler, EbpLow);
499}
500
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400501void TCompiler::setResourceString()
502{
503 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400504
505 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400506 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
507 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
508 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
509 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
510 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
511 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
512 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
513 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
514 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
515 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400516 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
517 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400518 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
519 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
520 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
521 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
522 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200523 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300524 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400525 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
526 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100527 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
528 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
529 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400530 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
531 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
532 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300533 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300534 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200535 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
Martin Radeve93d24e2016-07-28 12:06:05 +0300536 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
537 << ":MaxImageUnits:" << compileResources.MaxImageUnits
538 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
539 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
540 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
541 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
542 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
543 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
544 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
545 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
546 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
547 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
548 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
549 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
550 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
551 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
552 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
553 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
554 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
555 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
556 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
557 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
558 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
559 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
560 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400561 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400562
563 builtInResourcesString = strstream.str();
564}
565
alokp@chromium.org07620a52010-09-23 17:53:56 +0000566void TCompiler::clearResults()
567{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000568 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000569 infoSink.info.erase();
570 infoSink.obj.erase();
571 infoSink.debug.erase();
572
Jamie Madilled27c722014-07-02 15:31:23 -0400573 attributes.clear();
574 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000575 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400576 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400577 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400578 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000579
580 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000581
582 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200583
584 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700585 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000586}
587
Corentin Wallez71d147f2015-02-11 11:15:24 -0800588bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000589{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800590 mCallDag.clear();
591
592 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800593 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800594 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800595 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800596 case CallDAG::INITDAG_RECURSION:
597 infoSink.info.prefix(EPrefixError);
598 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800599 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800600 case CallDAG::INITDAG_UNDEFINED:
601 infoSink.info.prefix(EPrefixError);
602 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800603 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800604 }
605
606 UNREACHABLE();
607 return true;
608}
609
610bool TCompiler::checkCallDepth()
611{
612 std::vector<int> depths(mCallDag.size());
613
614 for (size_t i = 0; i < mCallDag.size(); i++)
615 {
616 int depth = 0;
617 auto &record = mCallDag.getRecordFromIndex(i);
618
619 for (auto &calleeIndex : record.callees)
620 {
621 depth = std::max(depth, depths[calleeIndex] + 1);
622 }
623
624 depths[i] = depth;
625
626 if (depth >= maxCallStackDepth)
627 {
628 // Trace back the function chain to have a meaningful info log.
629 infoSink.info.prefix(EPrefixError);
630 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
631 << ") with the following call chain: " << record.name;
632
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700633 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800634 int currentDepth = depth;
635
636 while (currentFunction != -1)
637 {
638 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
639
640 int nextFunction = -1;
641 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
642 {
643 if (depths[calleeIndex] == currentDepth - 1)
644 {
645 currentDepth--;
646 nextFunction = calleeIndex;
647 }
648 }
649
650 currentFunction = nextFunction;
651 }
652
653 return false;
654 }
655 }
656
657 return true;
658}
659
660bool TCompiler::tagUsedFunctions()
661{
662 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700663 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800664 {
665 if (mCallDag.getRecordFromIndex(i).name == "main(")
666 {
667 internalTagUsedFunction(i);
668 return true;
669 }
670 }
671
672 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200673 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800674 return false;
675}
676
677void TCompiler::internalTagUsedFunction(size_t index)
678{
679 if (functionMetadata[index].used)
680 {
681 return;
682 }
683
684 functionMetadata[index].used = true;
685
686 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
687 {
688 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000689 }
690}
691
Corentin Walleza094a8a2015-04-07 11:53:06 -0700692// A predicate for the stl that returns if a top-level node is unused
693class TCompiler::UnusedPredicate
694{
695 public:
696 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
697 : mCallDag(callDag),
698 mMetadatas(metadatas)
699 {
700 }
701
702 bool operator ()(TIntermNode *node)
703 {
704 const TIntermAggregate *asAggregate = node->getAsAggregate();
705
706 if (asAggregate == nullptr)
707 {
708 return false;
709 }
710
711 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
712 {
713 return false;
714 }
715
716 size_t callDagIndex = mCallDag->findIndex(asAggregate);
717 if (callDagIndex == CallDAG::InvalidIndex)
718 {
719 // This happens only for unimplemented prototypes which are thus unused
720 ASSERT(asAggregate->getOp() == EOpPrototype);
721 return true;
722 }
723
724 ASSERT(callDagIndex < mMetadatas->size());
725 return !(*mMetadatas)[callDagIndex].used;
726 }
727
728 private:
729 const CallDAG *mCallDag;
730 const std::vector<FunctionMetadata> *mMetadatas;
731};
732
733bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
734{
735 TIntermAggregate *rootNode = root->getAsAggregate();
736 ASSERT(rootNode != nullptr);
737
738 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
739 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700740
741 if (!sequence->empty())
742 {
743 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
744 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700745
746 return true;
747}
748
Jamie Madill05a80ce2013-06-20 11:55:49 -0400749bool TCompiler::validateOutputs(TIntermNode* root)
750{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300751 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400752 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300753 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400754}
755
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000756void TCompiler::rewriteCSSShader(TIntermNode* root)
757{
758 RenameFunction renamer("main(", "css_main(");
759 root->traverse(&renamer);
760}
761
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800762bool TCompiler::validateLimitations(TIntermNode* root)
763{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200764 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000765 root->traverse(&validate);
766 return validate.numErrors() == 0;
767}
768
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000769bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000770{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800771 if (shaderSpec != SH_WEBGL_SPEC)
772 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000773 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
774 return false;
775 }
776
Jamie Madill183bde52014-07-02 15:31:19 -0400777 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800778 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000779 TDependencyGraph graph(root);
780
781 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000782 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500783
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000784 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800785 if (outputGraph)
786 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000787 TDependencyGraphOutput output(infoSink.info);
788 output.outputAllSpanningTrees(graph);
789 }
Jamie Madill5508f392014-02-20 13:31:36 -0500790
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000791 return success;
792 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800793 else
794 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000795 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000796 }
797}
798
Jamie Madilleb1a0102013-07-08 13:31:38 -0400799bool TCompiler::limitExpressionComplexity(TIntermNode* root)
800{
Jamie Madill6654bc92014-03-26 14:01:57 -0400801 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400802 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400803
804 if (traverser.getMaxDepth() > maxExpressionComplexity)
805 {
806 infoSink.info << "Expression too complex.";
807 return false;
808 }
809
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200810 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
811 {
812 infoSink.info << "Function has too many parameters.";
813 return false;
814 }
815
Jamie Madilleb1a0102013-07-08 13:31:38 -0400816 return true;
817}
818
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000819bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000820{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000821 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000822 restrictor.enforceRestrictions(graph);
823 return restrictor.numErrors() == 0;
824}
825
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000826bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000827{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000828 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000829 restrictor.enforceRestrictions(root);
830 return restrictor.numErrors() == 0;
831}
832
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400833void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000834{
Zhenyao Mof178d0b2016-07-23 06:59:00 -0700835 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
836 &interfaceBlocks, hashFunction, symbolTable, extensionBehavior);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000837 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400838
Zhenyao Mo409078f2014-10-28 13:23:18 -0700839 // This is for enforcePackingRestriction().
840 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000841}
zmo@google.comfd747b82011-04-23 01:30:07 +0000842
gman@chromium.org8d804792012-10-17 21:33:48 +0000843bool TCompiler::enforcePackingRestrictions()
844{
845 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400846 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000847}
848
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800849void TCompiler::initializeGLPosition(TIntermNode* root)
850{
Zhenyao Mo72111912016-07-20 17:45:56 -0700851 InitVariableList list;
852 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
853 var.name = "gl_Position";
854 list.push_back(var);
855 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800856}
857
Olli Etuaho27776e32016-07-22 14:00:56 +0300858void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800859{
Zhenyao Mo72111912016-07-20 17:45:56 -0700860 InitVariableList list;
861 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800862 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700863 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800864 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700865 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800866 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800867 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700868 else
869 {
870 ASSERT(shaderType == GL_FRAGMENT_SHADER);
871 for (auto var : outputVariables)
872 {
873 list.push_back(var);
874 }
875 }
876 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800877}
878
zmo@google.com5601ea02011-06-10 18:23:25 +0000879const TExtensionBehavior& TCompiler::getExtensionBehavior() const
880{
881 return extensionBehavior;
882}
zmo@google.com32e97312011-08-24 01:03:11 +0000883
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200884const char *TCompiler::getSourcePath() const
885{
886 return mSourcePath;
887}
888
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000889const ShBuiltInResources& TCompiler::getResources() const
890{
891 return compileResources;
892}
893
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000894const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
895{
896 return arrayBoundsClamper;
897}
898
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000899ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
900{
901 return clampingStrategy;
902}
903
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200904const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000905{
906 return builtInFunctionEmulator;
907}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700908
909void TCompiler::writePragma()
910{
911 TInfoSinkBase &sink = infoSink.obj;
912 if (mPragma.stdgl.invariantAll)
913 sink << "#pragma STDGL invariant(all)\n";
914}