blob: 484b8995bf78a0dcda91fe1dd26e99d315e423fe [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"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000027#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040028#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040029#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000030
Jamie Madill5508f392014-02-20 13:31:36 -050031bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000032{
Qiankun Miaoc2c5fc42016-08-31 15:24:22 +080033 return (spec == SH_WEBGL_SPEC || spec == SH_WEBGL2_SPEC || spec == SH_WEBGL3_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000034}
35
Qingqing Dengad0d0792015-04-08 14:25:06 -070036bool IsGLSL130OrNewer(ShShaderOutput output)
37{
38 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070039 output == SH_GLSL_140_OUTPUT ||
40 output == SH_GLSL_150_CORE_OUTPUT ||
41 output == SH_GLSL_330_CORE_OUTPUT ||
42 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070043 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070044 output == SH_GLSL_420_CORE_OUTPUT ||
45 output == SH_GLSL_430_CORE_OUTPUT ||
46 output == SH_GLSL_440_CORE_OUTPUT ||
47 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070048}
49
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070050size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050051{
Jamie Madill88f6e942014-02-19 10:27:53 -050052 // WebGL defines a max token legnth of 256, while ES2 leaves max token
53 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070054 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050055 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070056 case SH_WEBGL_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050057 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070058 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050059 return 1024;
60 }
61}
62
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000063namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070064
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080065class TScopedPoolAllocator
66{
67 public:
68 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
69 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040070 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000071 SetGlobalPoolAllocator(mAllocator);
72 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080073 ~TScopedPoolAllocator()
74 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000075 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040076 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000077 }
78
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080079 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000080 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040081};
82
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080083class TScopedSymbolTableLevel
84{
85 public:
86 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
87 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040088 ASSERT(mTable->atBuiltInLevel());
89 mTable->push();
90 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080091 ~TScopedSymbolTableLevel()
92 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040093 while (!mTable->atBuiltInLevel())
94 mTable->pop();
95 }
96
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080097 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040098 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000099};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700100
101int MapSpecToShaderVersion(ShShaderSpec spec)
102{
103 switch (spec)
104 {
105 case SH_GLES2_SPEC:
106 case SH_WEBGL_SPEC:
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700107 return 100;
108 case SH_GLES3_SPEC:
109 case SH_WEBGL2_SPEC:
110 return 300;
Martin Radev1be913c2016-07-11 17:59:16 +0300111 case SH_GLES3_1_SPEC:
112 case SH_WEBGL3_SPEC:
113 return 310;
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700114 default:
115 UNREACHABLE();
116 return 0;
117 }
118}
119
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000120} // namespace
121
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800122TShHandleBase::TShHandleBase()
123{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000124 allocator.push();
125 SetGlobalPoolAllocator(&allocator);
126}
127
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800128TShHandleBase::~TShHandleBase()
129{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000130 SetGlobalPoolAllocator(NULL);
131 allocator.popAll();
132}
133
Jamie Madill183bde52014-07-02 15:31:19 -0400134TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700135 : variablesCollected(false),
136 shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000137 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400138 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400139 maxUniformVectors(0),
140 maxExpressionComplexity(0),
141 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200142 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000143 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000144 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200145 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700146 mSourcePath(NULL),
Martin Radev802abe02016-08-04 17:48:32 +0300147 mComputeShaderLocalSizeDeclared(false),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700148 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000149{
Martin Radev802abe02016-08-04 17:48:32 +0300150 mComputeShaderLocalSize.fill(1);
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000151}
152
153TCompiler::~TCompiler()
154{
155}
156
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300157bool TCompiler::shouldRunLoopAndIndexingValidation(int compileOptions) const
158{
159 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
160 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
161 // of ESSL 1.00 as in Appendix A of the spec).
162 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
163 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
164}
165
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000166bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000167{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000168 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400169 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000170 resources.MaxVertexUniformVectors :
171 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400172 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200173 maxCallStackDepth = resources.MaxCallStackDepth;
174 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400175
176 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000177
alokp@chromium.org07620a52010-09-23 17:53:56 +0000178 // Generate built-in symbol table.
179 if (!InitBuiltInSymbolTable(resources))
180 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000181 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000182 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000183
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000184 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
185 clampingStrategy = resources.ArrayIndexClampingStrategy;
186
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000187 hashFunction = resources.HashFunction;
188
alokp@chromium.org07620a52010-09-23 17:53:56 +0000189 return true;
190}
191
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200192TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
193 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000194{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200195 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
196}
197
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300198TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
199 size_t numStrings,
200 const int compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200201{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000202 clearResults();
203
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200204 ASSERT(numStrings > 0);
205 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000206
David Yen0fbd1282015-02-02 14:46:09 -0800207 // Reset the extension behavior for each compilation unit.
208 ResetExtensionBehavior(extensionBehavior);
209
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000210 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000211 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000212 if (compileOptions & SH_SOURCE_PATH)
213 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200214 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000215 ++firstSource;
216 }
217
Olli Etuahof119a262016-08-19 15:54:22 +0300218 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200219 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200220
Olli Etuahoa6996682015-10-12 14:32:30 +0300221 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400222 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000223
224 // We preserve symbols at the built-in level from compile-to-compile.
225 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400226 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000227
228 // Parse shader.
229 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400230 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
231 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000232
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000233 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700234 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
235 {
236 infoSink.info.prefix(EPrefixError);
237 infoSink.info << "unsupported shader version";
238 success = false;
239 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000240
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400241 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200242
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800243 if (success)
244 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700245 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700246 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700247
Martin Radev802abe02016-08-04 17:48:32 +0300248 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
249 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
250
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400251 root = parseContext.getTreeRoot();
Olli Etuahof119a262016-08-19 15:54:22 +0300252 root = TIntermediate::PostProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000253
Olli Etuahoa6996682015-10-12 14:32:30 +0300254 // Highp might have been auto-enabled based on shader version
255 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
256
Jamie Madill6654bc92014-03-26 14:01:57 -0400257 // Disallow expressions deemed too complex.
258 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
259 success = limitExpressionComplexity(root);
260
Corentin Wallez71d147f2015-02-11 11:15:24 -0800261 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000262 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800263 success = initCallDag(root);
264
265 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
266 success = checkCallDepth();
267
268 // Checks which functions are used and if "main" exists
269 if (success)
270 {
271 functionMetadata.clear();
272 functionMetadata.resize(mCallDag.size());
273 success = tagUsedFunctions();
274 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000275
Corentin Walleza094a8a2015-04-07 11:53:06 -0700276 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
277 success = pruneUnusedFunctions(root);
278
Olli Etuahoc6833112015-04-22 15:15:54 +0300279 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
280 if (success)
281 PruneEmptyDeclarations(root);
282
Jamie Madill183bde52014-07-02 15:31:19 -0400283 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400284 success = validateOutputs(root);
285
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300286 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000287 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000288
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000289 // Unroll for-loop markup needs to happen after validateLimitations pass.
290 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800291 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200292 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
293 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800294 root->traverse(&marker);
295 }
296 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800297 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200298 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
299 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800300 root->traverse(&marker);
301 if (marker.samplerArrayIndexIsFloatLoopIndex())
302 {
303 infoSink.info.prefix(EPrefixError);
304 infoSink.info << "sampler array index is float loop index";
305 success = false;
306 }
307 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000308
zmo@google.com32e97312011-08-24 01:03:11 +0000309 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200310 if (success)
311 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400312 // TODO(jmadill): Remove global pool allocator.
313 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200314 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400315 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000316 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200317 }
zmo@google.com32e97312011-08-24 01:03:11 +0000318
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000319 // Clamping uniform array bounds needs to happen after validateLimitations pass.
320 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
321 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
322
Ian Ewell924b7de2016-01-21 13:54:28 -0500323 // gl_Position is always written in compatibility output mode
324 if (success && shaderType == GL_VERTEX_SHADER &&
325 ((compileOptions & SH_INIT_GL_POSITION) ||
326 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800327 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400328
Corentin Wallezd4b50542015-09-28 12:19:26 -0700329 // This pass might emit short circuits so keep it before the short circuit unfolding
330 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
331 RewriteDoWhile(root, getTemporaryIndex());
332
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800333 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
334 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700335 UnfoldShortCircuitAST unfoldShortCircuit;
336 root->traverse(&unfoldShortCircuit);
337 unfoldShortCircuit.updateTree();
338 }
339
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300340 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
341 {
342 RemovePow(root);
343 }
344
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300345 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800346 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400347 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800348 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
349 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000350 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800351 if (!success)
352 {
Jamie Madill075edd82013-07-08 13:30:19 -0400353 infoSink.info.prefix(EPrefixError);
354 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000355 }
356 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700357 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
358 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300359 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700360 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000361 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000362
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700363 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
364 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700365 ScalarizeVecAndMatConstructorArgs scalarizer(
366 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700367 root->traverse(&scalarizer);
368 }
369
Zhenyao Moe740add2014-07-18 17:01:01 -0700370 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
371 {
372 RegenerateStructNames gen(symbolTable, shaderVersion);
373 root->traverse(&gen);
374 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300375
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700376 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
377 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
378 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
379 {
380 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
381 }
382
Olli Etuaho3d932d82016-04-12 11:10:30 +0300383 if (success)
384 {
385 DeferGlobalInitializers(root);
386 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000387 }
388
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700389 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200390 if (success)
391 return root;
392
393 return NULL;
394}
395
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700396bool TCompiler::compile(const char *const shaderStrings[], size_t numStrings, int compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200397{
398 if (numStrings == 0)
399 return true;
400
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700401 int compileOptions = compileOptionsIn;
402
403 // Apply key workarounds.
404 if (shouldFlattenPragmaStdglInvariantAll())
405 {
406 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
407 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
408 }
409
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200410 TScopedPoolAllocator scopedAlloc(&allocator);
411 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
412
413 if (root)
414 {
415 if (compileOptions & SH_INTERMEDIATE_TREE)
416 TIntermediate::outputTree(root, infoSink.info);
417
418 if (compileOptions & SH_OBJECT_CODE)
419 translate(root, compileOptions);
420
421 // The IntermNode tree doesn't need to be deleted here, since the
422 // memory will be freed in a big chunk by the PoolAllocator.
423 return true;
424 }
425 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000426}
427
Nicolas Capens49a88872013-06-20 09:54:03 -0400428bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000429{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000430 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400431 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000432
Nicolas Capens49a88872013-06-20 09:54:03 -0400433 assert(symbolTable.isEmpty());
434 symbolTable.push(); // COMMON_BUILTINS
435 symbolTable.push(); // ESSL1_BUILTINS
436 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300437 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000438
Nicolas Capens49a88872013-06-20 09:54:03 -0400439 TPublicType integer;
Martin Radev4a9cd802016-09-01 16:51:51 +0300440 integer.setBasicType(EbtInt);
441 integer.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400442 integer.array = false;
443
444 TPublicType floatingPoint;
Martin Radev4a9cd802016-09-01 16:51:51 +0300445 floatingPoint.setBasicType(EbtFloat);
446 floatingPoint.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400447 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;
Martin Radev4a9cd802016-09-01 16:51:51 +0300486 sampler.initializeSizeForScalarTypes();
487 sampler.setBasicType(samplerType);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200488 sampler.array = false;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200489 symbolTable.setDefaultPrecision(sampler, EbpLow);
490}
491
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400492void TCompiler::setResourceString()
493{
494 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400495
496 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400497 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
498 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
499 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
500 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
501 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
502 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
503 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
504 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
505 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
506 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400507 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
508 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400509 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
510 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
511 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
512 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
513 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200514 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300515 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400516 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
517 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100518 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
519 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
520 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400521 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
522 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
523 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300524 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300525 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200526 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
Martin Radeve93d24e2016-07-28 12:06:05 +0300527 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
528 << ":MaxImageUnits:" << compileResources.MaxImageUnits
529 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
530 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
531 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
532 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
533 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
534 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
535 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
536 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
537 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
538 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
539 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
540 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
541 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
542 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
543 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
544 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
545 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
546 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
547 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
548 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
549 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
550 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
551 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400552 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400553
554 builtInResourcesString = strstream.str();
555}
556
alokp@chromium.org07620a52010-09-23 17:53:56 +0000557void TCompiler::clearResults()
558{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000559 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000560 infoSink.info.erase();
561 infoSink.obj.erase();
562 infoSink.debug.erase();
563
Jamie Madilled27c722014-07-02 15:31:23 -0400564 attributes.clear();
565 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000566 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400567 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400568 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400569 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700570 variablesCollected = false;
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
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800748bool TCompiler::validateLimitations(TIntermNode* root)
749{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200750 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000751 root->traverse(&validate);
752 return validate.numErrors() == 0;
753}
754
Jamie Madilleb1a0102013-07-08 13:31:38 -0400755bool TCompiler::limitExpressionComplexity(TIntermNode* root)
756{
Jamie Madill6654bc92014-03-26 14:01:57 -0400757 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400758 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400759
760 if (traverser.getMaxDepth() > maxExpressionComplexity)
761 {
762 infoSink.info << "Expression too complex.";
763 return false;
764 }
765
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200766 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
767 {
768 infoSink.info << "Function has too many parameters.";
769 return false;
770 }
771
Jamie Madilleb1a0102013-07-08 13:31:38 -0400772 return true;
773}
774
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400775void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000776{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700777 if (!variablesCollected)
778 {
779 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
780 &interfaceBlocks, hashFunction, symbolTable, extensionBehavior);
781 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400782
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700783 // This is for enforcePackingRestriction().
784 sh::ExpandUniforms(uniforms, &expandedUniforms);
785 variablesCollected = true;
786 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000787}
zmo@google.comfd747b82011-04-23 01:30:07 +0000788
gman@chromium.org8d804792012-10-17 21:33:48 +0000789bool TCompiler::enforcePackingRestrictions()
790{
791 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400792 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000793}
794
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800795void TCompiler::initializeGLPosition(TIntermNode* root)
796{
Zhenyao Mo72111912016-07-20 17:45:56 -0700797 InitVariableList list;
798 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
799 var.name = "gl_Position";
800 list.push_back(var);
801 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800802}
803
Olli Etuaho27776e32016-07-22 14:00:56 +0300804void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800805{
Zhenyao Mo72111912016-07-20 17:45:56 -0700806 InitVariableList list;
807 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800808 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700809 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800810 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700811 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800812 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800813 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700814 else
815 {
816 ASSERT(shaderType == GL_FRAGMENT_SHADER);
817 for (auto var : outputVariables)
818 {
819 list.push_back(var);
820 }
821 }
822 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800823}
824
zmo@google.com5601ea02011-06-10 18:23:25 +0000825const TExtensionBehavior& TCompiler::getExtensionBehavior() const
826{
827 return extensionBehavior;
828}
zmo@google.com32e97312011-08-24 01:03:11 +0000829
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200830const char *TCompiler::getSourcePath() const
831{
832 return mSourcePath;
833}
834
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000835const ShBuiltInResources& TCompiler::getResources() const
836{
837 return compileResources;
838}
839
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000840const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
841{
842 return arrayBoundsClamper;
843}
844
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000845ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
846{
847 return clampingStrategy;
848}
849
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200850const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000851{
852 return builtInFunctionEmulator;
853}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700854
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700855void TCompiler::writePragma(int compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700856{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700857 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
858 {
859 TInfoSinkBase &sink = infoSink.obj;
860 if (mPragma.stdgl.invariantAll)
861 sink << "#pragma STDGL invariant(all)\n";
862 }
863}
864
865bool TCompiler::isVaryingDefined(const char *varyingName)
866{
867 ASSERT(variablesCollected);
868 for (size_t ii = 0; ii < varyings.size(); ++ii)
869 {
870 if (varyings[ii].name == varyingName)
871 {
872 return true;
873 }
874 }
875
876 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700877}