blob: 1469a339623928ea13d0581dbe159eded01e00db [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"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/ForLoopUnroll.h"
12#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040013#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080014#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040015#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030016#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070017#include "compiler/translator/RegenerateStructNames.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030018#include "compiler/translator/RemovePow.h"
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/RenameFunction.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{
Martin Radev1be913c2016-07-11 17:59:16 +030037 return (spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC || spec == SH_WEBGL2_SPEC ||
38 spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000039}
40
Qingqing Dengad0d0792015-04-08 14:25:06 -070041bool IsGLSL130OrNewer(ShShaderOutput output)
42{
43 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070044 output == SH_GLSL_140_OUTPUT ||
45 output == SH_GLSL_150_CORE_OUTPUT ||
46 output == SH_GLSL_330_CORE_OUTPUT ||
47 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070048 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070049 output == SH_GLSL_420_CORE_OUTPUT ||
50 output == SH_GLSL_430_CORE_OUTPUT ||
51 output == SH_GLSL_440_CORE_OUTPUT ||
52 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070053}
54
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070055size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050056{
Jamie Madill88f6e942014-02-19 10:27:53 -050057 // WebGL defines a max token legnth of 256, while ES2 leaves max token
58 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070059 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050060 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070061 case SH_WEBGL_SPEC:
62 case SH_CSS_SHADERS_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050063 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070064 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050065 return 1024;
66 }
67}
68
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000069namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070070
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080071class TScopedPoolAllocator
72{
73 public:
74 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
75 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040076 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000077 SetGlobalPoolAllocator(mAllocator);
78 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080079 ~TScopedPoolAllocator()
80 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000081 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040082 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000083 }
84
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080085 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000086 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040087};
88
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080089class TScopedSymbolTableLevel
90{
91 public:
92 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
93 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040094 ASSERT(mTable->atBuiltInLevel());
95 mTable->push();
96 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080097 ~TScopedSymbolTableLevel()
98 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040099 while (!mTable->atBuiltInLevel())
100 mTable->pop();
101 }
102
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800103 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400104 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000105};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700106
107int MapSpecToShaderVersion(ShShaderSpec spec)
108{
109 switch (spec)
110 {
111 case SH_GLES2_SPEC:
112 case SH_WEBGL_SPEC:
113 case SH_CSS_SHADERS_SPEC:
114 return 100;
115 case SH_GLES3_SPEC:
116 case SH_WEBGL2_SPEC:
117 return 300;
Martin Radev1be913c2016-07-11 17:59:16 +0300118 case SH_GLES3_1_SPEC:
119 case SH_WEBGL3_SPEC:
120 return 310;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700121 default:
122 UNREACHABLE();
123 return 0;
124 }
125}
126
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000127} // namespace
128
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800129TShHandleBase::TShHandleBase()
130{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000131 allocator.push();
132 SetGlobalPoolAllocator(&allocator);
133}
134
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800135TShHandleBase::~TShHandleBase()
136{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000137 SetGlobalPoolAllocator(NULL);
138 allocator.popAll();
139}
140
Jamie Madill183bde52014-07-02 15:31:19 -0400141TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000142 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000143 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400144 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400145 maxUniformVectors(0),
146 maxExpressionComplexity(0),
147 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200148 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000149 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000150 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200151 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700152 mSourcePath(NULL),
Martin Radev802abe02016-08-04 17:48:32 +0300153 mComputeShaderLocalSizeDeclared(false),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700154 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000155{
Martin Radev802abe02016-08-04 17:48:32 +0300156 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000157}
158
159TCompiler::~TCompiler()
160{
161}
162
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300163bool TCompiler::shouldRunLoopAndIndexingValidation(int compileOptions) const
164{
165 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
166 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
167 // of ESSL 1.00 as in Appendix A of the spec).
168 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
169 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
170}
171
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000172bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000173{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000174 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400175 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000176 resources.MaxVertexUniformVectors :
177 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400178 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200179 maxCallStackDepth = resources.MaxCallStackDepth;
180 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400181
182 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000183
alokp@chromium.org07620a52010-09-23 17:53:56 +0000184 // Generate built-in symbol table.
185 if (!InitBuiltInSymbolTable(resources))
186 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000187 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000188 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000189
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000190 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
191 clampingStrategy = resources.ArrayIndexClampingStrategy;
192
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000193 hashFunction = resources.HashFunction;
194
alokp@chromium.org07620a52010-09-23 17:53:56 +0000195 return true;
196}
197
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200198TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
199 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000200{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200201 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
202}
203
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300204TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
205 size_t numStrings,
206 const int compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200207{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000208 clearResults();
209
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200210 ASSERT(numStrings > 0);
211 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000212
David Yen0fbd1282015-02-02 14:46:09 -0800213 // Reset the extension behavior for each compilation unit.
214 ResetExtensionBehavior(extensionBehavior);
215
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000216 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000217 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000218 if (compileOptions & SH_SOURCE_PATH)
219 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200220 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000221 ++firstSource;
222 }
223
alokp@chromium.org07620a52010-09-23 17:53:56 +0000224 TIntermediate intermediate(infoSink);
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200225 TParseContext parseContext(symbolTable, extensionBehavior, intermediate, shaderType, shaderSpec,
226 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200227
Olli Etuahoa6996682015-10-12 14:32:30 +0300228 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400229 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000230
231 // We preserve symbols at the built-in level from compile-to-compile.
232 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400233 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000234
235 // Parse shader.
236 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400237 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
238 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000239
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000240 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700241 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
242 {
243 infoSink.info.prefix(EPrefixError);
244 infoSink.info << "unsupported shader version";
245 success = false;
246 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000247
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400248 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200249
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800250 if (success)
251 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700252 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700253 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700254
Martin Radev802abe02016-08-04 17:48:32 +0300255 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
256 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
257
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400258 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300259 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000260
Olli Etuahoa6996682015-10-12 14:32:30 +0300261 // Highp might have been auto-enabled based on shader version
262 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
263
Jamie Madill6654bc92014-03-26 14:01:57 -0400264 // Disallow expressions deemed too complex.
265 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
266 success = limitExpressionComplexity(root);
267
Corentin Wallez71d147f2015-02-11 11:15:24 -0800268 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000269 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800270 success = initCallDag(root);
271
272 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
273 success = checkCallDepth();
274
275 // Checks which functions are used and if "main" exists
276 if (success)
277 {
278 functionMetadata.clear();
279 functionMetadata.resize(mCallDag.size());
280 success = tagUsedFunctions();
281 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000282
Corentin Walleza094a8a2015-04-07 11:53:06 -0700283 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
284 success = pruneUnusedFunctions(root);
285
Olli Etuahoc6833112015-04-22 15:15:54 +0300286 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
287 if (success)
288 PruneEmptyDeclarations(root);
289
Jamie Madill183bde52014-07-02 15:31:19 -0400290 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400291 success = validateOutputs(root);
292
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300293 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000294 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000295
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000296 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000297 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000298
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000299 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
300 rewriteCSSShader(root);
301
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000302 // Unroll for-loop markup needs to happen after validateLimitations pass.
303 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800304 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200305 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
306 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800307 root->traverse(&marker);
308 }
309 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800310 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200311 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
312 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800313 root->traverse(&marker);
314 if (marker.samplerArrayIndexIsFloatLoopIndex())
315 {
316 infoSink.info.prefix(EPrefixError);
317 infoSink.info << "sampler array index is float loop index";
318 success = false;
319 }
320 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000321
zmo@google.com32e97312011-08-24 01:03:11 +0000322 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200323 if (success)
324 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400325 // TODO(jmadill): Remove global pool allocator.
326 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200327 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400328 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000329 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200330 }
zmo@google.com32e97312011-08-24 01:03:11 +0000331
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000332 // Clamping uniform array bounds needs to happen after validateLimitations pass.
333 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
334 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
335
Ian Ewell924b7de2016-01-21 13:54:28 -0500336 // gl_Position is always written in compatibility output mode
337 if (success && shaderType == GL_VERTEX_SHADER &&
338 ((compileOptions & SH_INIT_GL_POSITION) ||
339 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800340 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400341
Corentin Wallezd4b50542015-09-28 12:19:26 -0700342 // This pass might emit short circuits so keep it before the short circuit unfolding
343 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
344 RewriteDoWhile(root, getTemporaryIndex());
345
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800346 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
347 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700348 UnfoldShortCircuitAST unfoldShortCircuit;
349 root->traverse(&unfoldShortCircuit);
350 unfoldShortCircuit.updateTree();
351 }
352
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300353 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
354 {
355 RemovePow(root);
356 }
357
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300358 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800359 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400360 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800361 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
362 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000363 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800364 if (!success)
365 {
Jamie Madill075edd82013-07-08 13:30:19 -0400366 infoSink.info.prefix(EPrefixError);
367 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000368 }
369 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700370 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
371 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300372 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700373 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000374 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000375
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700376 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
377 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700378 ScalarizeVecAndMatConstructorArgs scalarizer(
379 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700380 root->traverse(&scalarizer);
381 }
382
Zhenyao Moe740add2014-07-18 17:01:01 -0700383 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
384 {
385 RegenerateStructNames gen(symbolTable, shaderVersion);
386 root->traverse(&gen);
387 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300388
389 if (success)
390 {
391 DeferGlobalInitializers(root);
392 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000393 }
394
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700395 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200396 if (success)
397 return root;
398
399 return NULL;
400}
401
402bool TCompiler::compile(const char* const shaderStrings[],
403 size_t numStrings, int compileOptions)
404{
405 if (numStrings == 0)
406 return true;
407
408 TScopedPoolAllocator scopedAlloc(&allocator);
409 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
410
411 if (root)
412 {
413 if (compileOptions & SH_INTERMEDIATE_TREE)
414 TIntermediate::outputTree(root, infoSink.info);
415
416 if (compileOptions & SH_OBJECT_CODE)
417 translate(root, compileOptions);
418
419 // The IntermNode tree doesn't need to be deleted here, since the
420 // memory will be freed in a big chunk by the PoolAllocator.
421 return true;
422 }
423 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000424}
425
Nicolas Capens49a88872013-06-20 09:54:03 -0400426bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000427{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000428 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400429 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000430
Nicolas Capens49a88872013-06-20 09:54:03 -0400431 assert(symbolTable.isEmpty());
432 symbolTable.push(); // COMMON_BUILTINS
433 symbolTable.push(); // ESSL1_BUILTINS
434 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300435 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000436
Nicolas Capens49a88872013-06-20 09:54:03 -0400437 TPublicType integer;
438 integer.type = EbtInt;
439 integer.primarySize = 1;
440 integer.secondarySize = 1;
441 integer.array = false;
442
443 TPublicType floatingPoint;
444 floatingPoint.type = EbtFloat;
445 floatingPoint.primarySize = 1;
446 floatingPoint.secondarySize = 1;
447 floatingPoint.array = false;
448
449 switch(shaderType)
450 {
Jamie Madill183bde52014-07-02 15:31:19 -0400451 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400452 symbolTable.setDefaultPrecision(integer, EbpMedium);
453 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400454 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400455 symbolTable.setDefaultPrecision(integer, EbpHigh);
456 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
457 break;
Martin Radev802abe02016-08-04 17:48:32 +0300458 case GL_COMPUTE_SHADER:
459 symbolTable.setDefaultPrecision(integer, EbpHigh);
460 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
461 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800462 default:
463 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400464 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200465 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400466 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200467 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
468 initSamplerDefaultPrecision(EbtSampler2D);
469 initSamplerDefaultPrecision(EbtSamplerCube);
470 // SamplerExternalOES is specified in the extension to have default precision.
471 initSamplerDefaultPrecision(EbtSamplerExternalOES);
472 // It isn't specified whether Sampler2DRect has default precision.
473 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400474
Jamie Madill1b452142013-07-12 14:51:11 -0400475 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400476
477 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
478
479 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000480}
481
Olli Etuaho183d7e22015-11-20 15:59:09 +0200482void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
483{
484 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
485 TPublicType sampler;
486 sampler.primarySize = 1;
487 sampler.secondarySize = 1;
488 sampler.array = false;
489 sampler.type = samplerType;
490 symbolTable.setDefaultPrecision(sampler, EbpLow);
491}
492
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400493void TCompiler::setResourceString()
494{
495 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400496
497 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400498 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
499 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
500 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
501 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
502 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
503 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
504 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
505 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
506 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
507 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400508 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
509 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400510 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
511 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
512 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
513 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
514 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200515 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300516 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400517 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
518 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100519 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
520 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
521 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400522 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
523 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
524 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300525 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300526 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200527 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
Martin Radeve93d24e2016-07-28 12:06:05 +0300528 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
529 << ":MaxImageUnits:" << compileResources.MaxImageUnits
530 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
531 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
532 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
533 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
534 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
535 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
536 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
537 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
538 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
539 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
540 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
541 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
542 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
543 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
544 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
545 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
546 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
547 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
548 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
549 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
550 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
551 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
552 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400553 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400554
555 builtInResourcesString = strstream.str();
556}
557
alokp@chromium.org07620a52010-09-23 17:53:56 +0000558void TCompiler::clearResults()
559{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000560 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000561 infoSink.info.erase();
562 infoSink.obj.erase();
563 infoSink.debug.erase();
564
Jamie Madilled27c722014-07-02 15:31:23 -0400565 attributes.clear();
566 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000567 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400568 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400569 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400570 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000571
572 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000573
574 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200575
576 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700577 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000578}
579
Corentin Wallez71d147f2015-02-11 11:15:24 -0800580bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000581{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800582 mCallDag.clear();
583
584 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800585 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800586 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800587 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800588 case CallDAG::INITDAG_RECURSION:
589 infoSink.info.prefix(EPrefixError);
590 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800591 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800592 case CallDAG::INITDAG_UNDEFINED:
593 infoSink.info.prefix(EPrefixError);
594 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800595 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800596 }
597
598 UNREACHABLE();
599 return true;
600}
601
602bool TCompiler::checkCallDepth()
603{
604 std::vector<int> depths(mCallDag.size());
605
606 for (size_t i = 0; i < mCallDag.size(); i++)
607 {
608 int depth = 0;
609 auto &record = mCallDag.getRecordFromIndex(i);
610
611 for (auto &calleeIndex : record.callees)
612 {
613 depth = std::max(depth, depths[calleeIndex] + 1);
614 }
615
616 depths[i] = depth;
617
618 if (depth >= maxCallStackDepth)
619 {
620 // Trace back the function chain to have a meaningful info log.
621 infoSink.info.prefix(EPrefixError);
622 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
623 << ") with the following call chain: " << record.name;
624
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700625 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800626 int currentDepth = depth;
627
628 while (currentFunction != -1)
629 {
630 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
631
632 int nextFunction = -1;
633 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
634 {
635 if (depths[calleeIndex] == currentDepth - 1)
636 {
637 currentDepth--;
638 nextFunction = calleeIndex;
639 }
640 }
641
642 currentFunction = nextFunction;
643 }
644
645 return false;
646 }
647 }
648
649 return true;
650}
651
652bool TCompiler::tagUsedFunctions()
653{
654 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700655 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800656 {
657 if (mCallDag.getRecordFromIndex(i).name == "main(")
658 {
659 internalTagUsedFunction(i);
660 return true;
661 }
662 }
663
664 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200665 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800666 return false;
667}
668
669void TCompiler::internalTagUsedFunction(size_t index)
670{
671 if (functionMetadata[index].used)
672 {
673 return;
674 }
675
676 functionMetadata[index].used = true;
677
678 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
679 {
680 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000681 }
682}
683
Corentin Walleza094a8a2015-04-07 11:53:06 -0700684// A predicate for the stl that returns if a top-level node is unused
685class TCompiler::UnusedPredicate
686{
687 public:
688 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
689 : mCallDag(callDag),
690 mMetadatas(metadatas)
691 {
692 }
693
694 bool operator ()(TIntermNode *node)
695 {
696 const TIntermAggregate *asAggregate = node->getAsAggregate();
697
698 if (asAggregate == nullptr)
699 {
700 return false;
701 }
702
703 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
704 {
705 return false;
706 }
707
708 size_t callDagIndex = mCallDag->findIndex(asAggregate);
709 if (callDagIndex == CallDAG::InvalidIndex)
710 {
711 // This happens only for unimplemented prototypes which are thus unused
712 ASSERT(asAggregate->getOp() == EOpPrototype);
713 return true;
714 }
715
716 ASSERT(callDagIndex < mMetadatas->size());
717 return !(*mMetadatas)[callDagIndex].used;
718 }
719
720 private:
721 const CallDAG *mCallDag;
722 const std::vector<FunctionMetadata> *mMetadatas;
723};
724
725bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
726{
727 TIntermAggregate *rootNode = root->getAsAggregate();
728 ASSERT(rootNode != nullptr);
729
730 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
731 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700732
733 if (!sequence->empty())
734 {
735 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
736 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700737
738 return true;
739}
740
Jamie Madill05a80ce2013-06-20 11:55:49 -0400741bool TCompiler::validateOutputs(TIntermNode* root)
742{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300743 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400744 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300745 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400746}
747
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000748void TCompiler::rewriteCSSShader(TIntermNode* root)
749{
750 RenameFunction renamer("main(", "css_main(");
751 root->traverse(&renamer);
752}
753
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800754bool TCompiler::validateLimitations(TIntermNode* root)
755{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200756 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000757 root->traverse(&validate);
758 return validate.numErrors() == 0;
759}
760
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000761bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000762{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800763 if (shaderSpec != SH_WEBGL_SPEC)
764 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000765 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
766 return false;
767 }
768
Jamie Madill183bde52014-07-02 15:31:19 -0400769 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800770 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000771 TDependencyGraph graph(root);
772
773 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000774 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500775
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000776 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800777 if (outputGraph)
778 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000779 TDependencyGraphOutput output(infoSink.info);
780 output.outputAllSpanningTrees(graph);
781 }
Jamie Madill5508f392014-02-20 13:31:36 -0500782
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000783 return success;
784 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800785 else
786 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000787 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000788 }
789}
790
Jamie Madilleb1a0102013-07-08 13:31:38 -0400791bool TCompiler::limitExpressionComplexity(TIntermNode* root)
792{
Jamie Madill6654bc92014-03-26 14:01:57 -0400793 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400794 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400795
796 if (traverser.getMaxDepth() > maxExpressionComplexity)
797 {
798 infoSink.info << "Expression too complex.";
799 return false;
800 }
801
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200802 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
803 {
804 infoSink.info << "Function has too many parameters.";
805 return false;
806 }
807
Jamie Madilleb1a0102013-07-08 13:31:38 -0400808 return true;
809}
810
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000811bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000812{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000813 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000814 restrictor.enforceRestrictions(graph);
815 return restrictor.numErrors() == 0;
816}
817
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000818bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000819{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000820 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000821 restrictor.enforceRestrictions(root);
822 return restrictor.numErrors() == 0;
823}
824
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400825void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000826{
Zhenyao Mof178d0b2016-07-23 06:59:00 -0700827 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
828 &interfaceBlocks, hashFunction, symbolTable, extensionBehavior);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000829 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400830
Zhenyao Mo409078f2014-10-28 13:23:18 -0700831 // This is for enforcePackingRestriction().
832 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000833}
zmo@google.comfd747b82011-04-23 01:30:07 +0000834
gman@chromium.org8d804792012-10-17 21:33:48 +0000835bool TCompiler::enforcePackingRestrictions()
836{
837 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400838 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000839}
840
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800841void TCompiler::initializeGLPosition(TIntermNode* root)
842{
Zhenyao Mo72111912016-07-20 17:45:56 -0700843 InitVariableList list;
844 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
845 var.name = "gl_Position";
846 list.push_back(var);
847 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800848}
849
Olli Etuaho27776e32016-07-22 14:00:56 +0300850void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800851{
Zhenyao Mo72111912016-07-20 17:45:56 -0700852 InitVariableList list;
853 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800854 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700855 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800856 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700857 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800858 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800859 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700860 else
861 {
862 ASSERT(shaderType == GL_FRAGMENT_SHADER);
863 for (auto var : outputVariables)
864 {
865 list.push_back(var);
866 }
867 }
868 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800869}
870
zmo@google.com5601ea02011-06-10 18:23:25 +0000871const TExtensionBehavior& TCompiler::getExtensionBehavior() const
872{
873 return extensionBehavior;
874}
zmo@google.com32e97312011-08-24 01:03:11 +0000875
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200876const char *TCompiler::getSourcePath() const
877{
878 return mSourcePath;
879}
880
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000881const ShBuiltInResources& TCompiler::getResources() const
882{
883 return compileResources;
884}
885
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000886const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
887{
888 return arrayBoundsClamper;
889}
890
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000891ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
892{
893 return clampingStrategy;
894}
895
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200896const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000897{
898 return builtInFunctionEmulator;
899}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700900
901void TCompiler::writePragma()
902{
903 TInfoSinkBase &sink = infoSink.obj;
904 if (mPragma.stdgl.invariantAll)
905 sink << "#pragma STDGL invariant(all)\n";
906}