blob: ddeb05a9b16df289e729c68c40ceccc9cc1fcfde [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
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800157bool TCompiler::shouldRunLoopAndIndexingValidation(ShCompileOptions compileOptions) const
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300158{
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
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800192TIntermNode *TCompiler::compileTreeForTesting(const char *const shaderStrings[],
193 size_t numStrings,
194 ShCompileOptions compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000195{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200196 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
197}
198
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300199TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
200 size_t numStrings,
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800201 const ShCompileOptions compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200202{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000203 clearResults();
204
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200205 ASSERT(numStrings > 0);
206 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000207
David Yen0fbd1282015-02-02 14:46:09 -0800208 // Reset the extension behavior for each compilation unit.
209 ResetExtensionBehavior(extensionBehavior);
210
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000211 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000212 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000213 if (compileOptions & SH_SOURCE_PATH)
214 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200215 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000216 ++firstSource;
217 }
218
Olli Etuahof119a262016-08-19 15:54:22 +0300219 TParseContext parseContext(symbolTable, extensionBehavior, shaderType, shaderSpec,
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200220 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200221
Olli Etuahoa6996682015-10-12 14:32:30 +0300222 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400223 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000224
225 // We preserve symbols at the built-in level from compile-to-compile.
226 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400227 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000228
229 // Parse shader.
230 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400231 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
232 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000233
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000234 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700235 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
236 {
237 infoSink.info.prefix(EPrefixError);
238 infoSink.info << "unsupported shader version";
239 success = false;
240 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000241
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400242 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200243
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800244 if (success)
245 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700246 mPragma = parseContext.pragma();
Kenneth Russell8bad46d2016-07-01 19:52:52 -0700247 symbolTable.setGlobalInvariant(mPragma.stdgl.invariantAll);
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700248
Martin Radev802abe02016-08-04 17:48:32 +0300249 mComputeShaderLocalSizeDeclared = parseContext.isComputeShaderLocalSizeDeclared();
250 mComputeShaderLocalSize = parseContext.getComputeShaderLocalSize();
251
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400252 root = parseContext.getTreeRoot();
Olli Etuahof119a262016-08-19 15:54:22 +0300253 root = TIntermediate::PostProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000254
Olli Etuahoa6996682015-10-12 14:32:30 +0300255 // Highp might have been auto-enabled based on shader version
256 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
257
Jamie Madill6654bc92014-03-26 14:01:57 -0400258 // Disallow expressions deemed too complex.
259 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
260 success = limitExpressionComplexity(root);
261
Corentin Wallez71d147f2015-02-11 11:15:24 -0800262 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000263 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800264 success = initCallDag(root);
265
266 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
267 success = checkCallDepth();
268
269 // Checks which functions are used and if "main" exists
270 if (success)
271 {
272 functionMetadata.clear();
273 functionMetadata.resize(mCallDag.size());
274 success = tagUsedFunctions();
275 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000276
Corentin Walleza094a8a2015-04-07 11:53:06 -0700277 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
278 success = pruneUnusedFunctions(root);
279
Olli Etuahoc6833112015-04-22 15:15:54 +0300280 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
281 if (success)
282 PruneEmptyDeclarations(root);
283
Jamie Madill183bde52014-07-02 15:31:19 -0400284 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400285 success = validateOutputs(root);
286
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300287 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000288 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000289
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000290 // Unroll for-loop markup needs to happen after validateLimitations pass.
291 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800292 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200293 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
294 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800295 root->traverse(&marker);
296 }
297 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800298 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200299 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
300 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800301 root->traverse(&marker);
302 if (marker.samplerArrayIndexIsFloatLoopIndex())
303 {
304 infoSink.info.prefix(EPrefixError);
305 infoSink.info << "sampler array index is float loop index";
306 success = false;
307 }
308 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000309
zmo@google.com32e97312011-08-24 01:03:11 +0000310 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200311 if (success)
312 {
Jamie Madill438dbcf2016-06-17 14:20:05 -0400313 // TODO(jmadill): Remove global pool allocator.
314 GetGlobalPoolAllocator()->lock();
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200315 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
Jamie Madill438dbcf2016-06-17 14:20:05 -0400316 GetGlobalPoolAllocator()->unlock();
zmo@google.com32e97312011-08-24 01:03:11 +0000317 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200318 }
zmo@google.com32e97312011-08-24 01:03:11 +0000319
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000320 // Clamping uniform array bounds needs to happen after validateLimitations pass.
321 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
322 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
323
Ian Ewell924b7de2016-01-21 13:54:28 -0500324 // gl_Position is always written in compatibility output mode
325 if (success && shaderType == GL_VERTEX_SHADER &&
326 ((compileOptions & SH_INIT_GL_POSITION) ||
327 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800328 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400329
Corentin Wallezd4b50542015-09-28 12:19:26 -0700330 // This pass might emit short circuits so keep it before the short circuit unfolding
331 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
332 RewriteDoWhile(root, getTemporaryIndex());
333
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800334 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
335 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700336 UnfoldShortCircuitAST unfoldShortCircuit;
337 root->traverse(&unfoldShortCircuit);
338 unfoldShortCircuit.updateTree();
339 }
340
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300341 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
342 {
343 RemovePow(root);
344 }
345
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300346 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800347 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400348 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800349 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
350 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000351 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800352 if (!success)
353 {
Jamie Madill075edd82013-07-08 13:30:19 -0400354 infoSink.info.prefix(EPrefixError);
355 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000356 }
357 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700358 if (success && (compileOptions & SH_INIT_OUTPUT_VARIABLES))
359 {
Olli Etuaho27776e32016-07-22 14:00:56 +0300360 initializeOutputVariables(root);
Zhenyao Mo72111912016-07-20 17:45:56 -0700361 }
gman@chromium.org8d804792012-10-17 21:33:48 +0000362 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000363
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700364 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
365 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700366 ScalarizeVecAndMatConstructorArgs scalarizer(
367 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700368 root->traverse(&scalarizer);
369 }
370
Zhenyao Moe740add2014-07-18 17:01:01 -0700371 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
372 {
373 RegenerateStructNames gen(symbolTable, shaderVersion);
374 root->traverse(&gen);
375 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300376
Zhenyao Mo4e94fea2016-08-09 14:31:37 -0700377 if (success && shaderType == GL_FRAGMENT_SHADER && shaderVersion == 100 &&
378 compileResources.EXT_draw_buffers && compileResources.MaxDrawBuffers > 1 &&
379 IsExtensionEnabled(extensionBehavior, "GL_EXT_draw_buffers"))
380 {
381 EmulateGLFragColorBroadcast(root, compileResources.MaxDrawBuffers, &outputVariables);
382 }
383
Olli Etuaho3d932d82016-04-12 11:10:30 +0300384 if (success)
385 {
386 DeferGlobalInitializers(root);
387 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000388 }
389
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700390 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200391 if (success)
392 return root;
393
394 return NULL;
395}
396
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800397bool TCompiler::compile(const char *const shaderStrings[],
398 size_t numStrings,
399 ShCompileOptions compileOptionsIn)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200400{
401 if (numStrings == 0)
402 return true;
403
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800404 ShCompileOptions compileOptions = compileOptionsIn;
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700405
406 // Apply key workarounds.
407 if (shouldFlattenPragmaStdglInvariantAll())
408 {
409 // This should be harmless to do in all cases, but for the moment, do it only conditionally.
410 compileOptions |= SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL;
411 }
412
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200413 TScopedPoolAllocator scopedAlloc(&allocator);
414 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
415
416 if (root)
417 {
418 if (compileOptions & SH_INTERMEDIATE_TREE)
419 TIntermediate::outputTree(root, infoSink.info);
420
421 if (compileOptions & SH_OBJECT_CODE)
422 translate(root, compileOptions);
423
424 // The IntermNode tree doesn't need to be deleted here, since the
425 // memory will be freed in a big chunk by the PoolAllocator.
426 return true;
427 }
428 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000429}
430
Nicolas Capens49a88872013-06-20 09:54:03 -0400431bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000432{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000433 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400434 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000435
Nicolas Capens49a88872013-06-20 09:54:03 -0400436 assert(symbolTable.isEmpty());
437 symbolTable.push(); // COMMON_BUILTINS
438 symbolTable.push(); // ESSL1_BUILTINS
439 symbolTable.push(); // ESSL3_BUILTINS
Martin Radeve93d24e2016-07-28 12:06:05 +0300440 symbolTable.push(); // ESSL3_1_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000441
Nicolas Capens49a88872013-06-20 09:54:03 -0400442 TPublicType integer;
Martin Radev4a9cd802016-09-01 16:51:51 +0300443 integer.setBasicType(EbtInt);
444 integer.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400445 integer.array = false;
446
447 TPublicType floatingPoint;
Martin Radev4a9cd802016-09-01 16:51:51 +0300448 floatingPoint.setBasicType(EbtFloat);
449 floatingPoint.initializeSizeForScalarTypes();
Nicolas Capens49a88872013-06-20 09:54:03 -0400450 floatingPoint.array = false;
451
452 switch(shaderType)
453 {
Jamie Madill183bde52014-07-02 15:31:19 -0400454 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400455 symbolTable.setDefaultPrecision(integer, EbpMedium);
456 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400457 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400458 symbolTable.setDefaultPrecision(integer, EbpHigh);
459 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
460 break;
Martin Radev802abe02016-08-04 17:48:32 +0300461 case GL_COMPUTE_SHADER:
462 symbolTable.setDefaultPrecision(integer, EbpHigh);
463 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
464 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800465 default:
466 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400467 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200468 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400469 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200470 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
471 initSamplerDefaultPrecision(EbtSampler2D);
472 initSamplerDefaultPrecision(EbtSamplerCube);
473 // SamplerExternalOES is specified in the extension to have default precision.
474 initSamplerDefaultPrecision(EbtSamplerExternalOES);
475 // It isn't specified whether Sampler2DRect has default precision.
476 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400477
Jamie Madill1b452142013-07-12 14:51:11 -0400478 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400479
480 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
481
482 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000483}
484
Olli Etuaho183d7e22015-11-20 15:59:09 +0200485void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
486{
487 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
488 TPublicType sampler;
Martin Radev4a9cd802016-09-01 16:51:51 +0300489 sampler.initializeSizeForScalarTypes();
490 sampler.setBasicType(samplerType);
Olli Etuaho183d7e22015-11-20 15:59:09 +0200491 sampler.array = false;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200492 symbolTable.setDefaultPrecision(sampler, EbpLow);
493}
494
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400495void TCompiler::setResourceString()
496{
497 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400498
499 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400500 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
501 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
502 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
503 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
504 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
505 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
506 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
507 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
508 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
509 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400510 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
511 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400512 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
513 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
514 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
515 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
516 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200517 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300518 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400519 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
520 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100521 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
522 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
523 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400524 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
525 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
526 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300527 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300528 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200529 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
Martin Radeve93d24e2016-07-28 12:06:05 +0300530 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision
531 << ":MaxImageUnits:" << compileResources.MaxImageUnits
532 << ":MaxVertexImageUniforms:" << compileResources.MaxVertexImageUniforms
533 << ":MaxFragmentImageUniforms:" << compileResources.MaxFragmentImageUniforms
534 << ":MaxComputeImageUniforms:" << compileResources.MaxComputeImageUniforms
535 << ":MaxCombinedImageUniforms:" << compileResources.MaxCombinedImageUniforms
536 << ":MaxCombinedShaderOutputResources:" << compileResources.MaxCombinedShaderOutputResources
537 << ":MaxComputeWorkGroupCountX:" << compileResources.MaxComputeWorkGroupCount[0]
538 << ":MaxComputeWorkGroupCountY:" << compileResources.MaxComputeWorkGroupCount[1]
539 << ":MaxComputeWorkGroupCountZ:" << compileResources.MaxComputeWorkGroupCount[2]
540 << ":MaxComputeWorkGroupSizeX:" << compileResources.MaxComputeWorkGroupSize[0]
541 << ":MaxComputeWorkGroupSizeY:" << compileResources.MaxComputeWorkGroupSize[1]
542 << ":MaxComputeWorkGroupSizeZ:" << compileResources.MaxComputeWorkGroupSize[2]
543 << ":MaxComputeUniformComponents:" << compileResources.MaxComputeUniformComponents
544 << ":MaxComputeTextureImageUnits:" << compileResources.MaxComputeTextureImageUnits
545 << ":MaxComputeAtomicCounters:" << compileResources.MaxComputeAtomicCounters
546 << ":MaxComputeAtomicCounterBuffers:" << compileResources.MaxComputeAtomicCounterBuffers
547 << ":MaxVertexAtomicCounters:" << compileResources.MaxVertexAtomicCounters
548 << ":MaxFragmentAtomicCounters:" << compileResources.MaxFragmentAtomicCounters
549 << ":MaxCombinedAtomicCounters:" << compileResources.MaxCombinedAtomicCounters
550 << ":MaxAtomicCounterBindings:" << compileResources.MaxAtomicCounterBindings
551 << ":MaxVertexAtomicCounterBuffers:" << compileResources.MaxVertexAtomicCounterBuffers
552 << ":MaxFragmentAtomicCounterBuffers:" << compileResources.MaxFragmentAtomicCounterBuffers
553 << ":MaxCombinedAtomicCounterBuffers:" << compileResources.MaxCombinedAtomicCounterBuffers
554 << ":MaxAtomicCounterBufferSize:" << compileResources.MaxAtomicCounterBufferSize;
Geoff Langb66a9092016-05-16 15:59:14 -0400555 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400556
557 builtInResourcesString = strstream.str();
558}
559
alokp@chromium.org07620a52010-09-23 17:53:56 +0000560void TCompiler::clearResults()
561{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000562 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000563 infoSink.info.erase();
564 infoSink.obj.erase();
565 infoSink.debug.erase();
566
Jamie Madilled27c722014-07-02 15:31:23 -0400567 attributes.clear();
568 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000569 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400570 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400571 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400572 interfaceBlocks.clear();
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700573 variablesCollected = false;
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000574
575 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000576
577 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200578
579 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700580 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000581}
582
Corentin Wallez71d147f2015-02-11 11:15:24 -0800583bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000584{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800585 mCallDag.clear();
586
587 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800588 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800589 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800590 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800591 case CallDAG::INITDAG_RECURSION:
592 infoSink.info.prefix(EPrefixError);
593 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800594 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800595 case CallDAG::INITDAG_UNDEFINED:
596 infoSink.info.prefix(EPrefixError);
597 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800598 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800599 }
600
601 UNREACHABLE();
602 return true;
603}
604
605bool TCompiler::checkCallDepth()
606{
607 std::vector<int> depths(mCallDag.size());
608
609 for (size_t i = 0; i < mCallDag.size(); i++)
610 {
611 int depth = 0;
612 auto &record = mCallDag.getRecordFromIndex(i);
613
614 for (auto &calleeIndex : record.callees)
615 {
616 depth = std::max(depth, depths[calleeIndex] + 1);
617 }
618
619 depths[i] = depth;
620
621 if (depth >= maxCallStackDepth)
622 {
623 // Trace back the function chain to have a meaningful info log.
624 infoSink.info.prefix(EPrefixError);
625 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
626 << ") with the following call chain: " << record.name;
627
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700628 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800629 int currentDepth = depth;
630
631 while (currentFunction != -1)
632 {
633 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
634
635 int nextFunction = -1;
636 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
637 {
638 if (depths[calleeIndex] == currentDepth - 1)
639 {
640 currentDepth--;
641 nextFunction = calleeIndex;
642 }
643 }
644
645 currentFunction = nextFunction;
646 }
647
648 return false;
649 }
650 }
651
652 return true;
653}
654
655bool TCompiler::tagUsedFunctions()
656{
657 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700658 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800659 {
660 if (mCallDag.getRecordFromIndex(i).name == "main(")
661 {
662 internalTagUsedFunction(i);
663 return true;
664 }
665 }
666
667 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200668 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800669 return false;
670}
671
672void TCompiler::internalTagUsedFunction(size_t index)
673{
674 if (functionMetadata[index].used)
675 {
676 return;
677 }
678
679 functionMetadata[index].used = true;
680
681 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
682 {
683 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000684 }
685}
686
Corentin Walleza094a8a2015-04-07 11:53:06 -0700687// A predicate for the stl that returns if a top-level node is unused
688class TCompiler::UnusedPredicate
689{
690 public:
691 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
692 : mCallDag(callDag),
693 mMetadatas(metadatas)
694 {
695 }
696
697 bool operator ()(TIntermNode *node)
698 {
699 const TIntermAggregate *asAggregate = node->getAsAggregate();
700
701 if (asAggregate == nullptr)
702 {
703 return false;
704 }
705
706 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
707 {
708 return false;
709 }
710
711 size_t callDagIndex = mCallDag->findIndex(asAggregate);
712 if (callDagIndex == CallDAG::InvalidIndex)
713 {
714 // This happens only for unimplemented prototypes which are thus unused
715 ASSERT(asAggregate->getOp() == EOpPrototype);
716 return true;
717 }
718
719 ASSERT(callDagIndex < mMetadatas->size());
720 return !(*mMetadatas)[callDagIndex].used;
721 }
722
723 private:
724 const CallDAG *mCallDag;
725 const std::vector<FunctionMetadata> *mMetadatas;
726};
727
728bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
729{
730 TIntermAggregate *rootNode = root->getAsAggregate();
731 ASSERT(rootNode != nullptr);
732
733 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
734 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700735
736 if (!sequence->empty())
737 {
738 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
739 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700740
741 return true;
742}
743
Jamie Madill05a80ce2013-06-20 11:55:49 -0400744bool TCompiler::validateOutputs(TIntermNode* root)
745{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300746 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400747 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300748 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400749}
750
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800751bool TCompiler::validateLimitations(TIntermNode* root)
752{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200753 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000754 root->traverse(&validate);
755 return validate.numErrors() == 0;
756}
757
Jamie Madilleb1a0102013-07-08 13:31:38 -0400758bool TCompiler::limitExpressionComplexity(TIntermNode* root)
759{
Jamie Madill6654bc92014-03-26 14:01:57 -0400760 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400761 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400762
763 if (traverser.getMaxDepth() > maxExpressionComplexity)
764 {
765 infoSink.info << "Expression too complex.";
766 return false;
767 }
768
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200769 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
770 {
771 infoSink.info << "Function has too many parameters.";
772 return false;
773 }
774
Jamie Madilleb1a0102013-07-08 13:31:38 -0400775 return true;
776}
777
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400778void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000779{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700780 if (!variablesCollected)
781 {
782 sh::CollectVariables collect(&attributes, &outputVariables, &uniforms, &varyings,
783 &interfaceBlocks, hashFunction, symbolTable, extensionBehavior);
784 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400785
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700786 // This is for enforcePackingRestriction().
787 sh::ExpandUniforms(uniforms, &expandedUniforms);
788 variablesCollected = true;
789 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000790}
zmo@google.comfd747b82011-04-23 01:30:07 +0000791
gman@chromium.org8d804792012-10-17 21:33:48 +0000792bool TCompiler::enforcePackingRestrictions()
793{
794 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400795 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000796}
797
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800798void TCompiler::initializeGLPosition(TIntermNode* root)
799{
Zhenyao Mo72111912016-07-20 17:45:56 -0700800 InitVariableList list;
801 sh::ShaderVariable var(GL_FLOAT_VEC4, 0);
802 var.name = "gl_Position";
803 list.push_back(var);
804 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800805}
806
Olli Etuaho27776e32016-07-22 14:00:56 +0300807void TCompiler::initializeOutputVariables(TIntermNode *root)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800808{
Zhenyao Mo72111912016-07-20 17:45:56 -0700809 InitVariableList list;
810 if (shaderType == GL_VERTEX_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800811 {
Zhenyao Mo72111912016-07-20 17:45:56 -0700812 for (auto var : varyings)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800813 {
Zhenyao Mof9312682016-07-22 12:51:31 -0700814 list.push_back(var);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800815 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800816 }
Zhenyao Mo72111912016-07-20 17:45:56 -0700817 else
818 {
819 ASSERT(shaderType == GL_FRAGMENT_SHADER);
820 for (auto var : outputVariables)
821 {
822 list.push_back(var);
823 }
824 }
825 InitializeVariables(root, list);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800826}
827
zmo@google.com5601ea02011-06-10 18:23:25 +0000828const TExtensionBehavior& TCompiler::getExtensionBehavior() const
829{
830 return extensionBehavior;
831}
zmo@google.com32e97312011-08-24 01:03:11 +0000832
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200833const char *TCompiler::getSourcePath() const
834{
835 return mSourcePath;
836}
837
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000838const ShBuiltInResources& TCompiler::getResources() const
839{
840 return compileResources;
841}
842
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000843const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
844{
845 return arrayBoundsClamper;
846}
847
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000848ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
849{
850 return clampingStrategy;
851}
852
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200853const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000854{
855 return builtInFunctionEmulator;
856}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700857
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800858void TCompiler::writePragma(ShCompileOptions compileOptions)
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700859{
Kenneth Russellbccc65d2016-07-19 16:48:43 -0700860 if (!(compileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL))
861 {
862 TInfoSinkBase &sink = infoSink.obj;
863 if (mPragma.stdgl.invariantAll)
864 sink << "#pragma STDGL invariant(all)\n";
865 }
866}
867
868bool TCompiler::isVaryingDefined(const char *varyingName)
869{
870 ASSERT(variablesCollected);
871 for (size_t ii = 0; ii < varyings.size(); ++ii)
872 {
873 if (varyings[ii].name == varyingName)
874 {
875 return true;
876 }
877 }
878
879 return false;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700880}