blob: 77360d238607370842a7ff2023d73fc0dd0a38b6 [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"
Geoff Lang17732822013-08-29 13:46:49 -040010#include "compiler/translator/ForLoopUnroll.h"
11#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080013#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040014#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030015#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070016#include "compiler/translator/RegenerateStructNames.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030017#include "compiler/translator/RemovePow.h"
Geoff Lang17732822013-08-29 13:46:49 -040018#include "compiler/translator/RenameFunction.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070019#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070020#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070021#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040022#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020023#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040024#include "compiler/translator/ValidateOutputs.h"
25#include "compiler/translator/VariablePacker.h"
26#include "compiler/translator/depgraph/DependencyGraph.h"
27#include "compiler/translator/depgraph/DependencyGraphOutput.h"
28#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
29#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000030#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040031#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040032#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000033
Jamie Madill5508f392014-02-20 13:31:36 -050034bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000035{
Zhenyao Modb9b40b2014-10-29 15:00:04 -070036 return (spec == SH_WEBGL_SPEC ||
37 spec == SH_CSS_SHADERS_SPEC ||
38 spec == SH_WEBGL2_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;
118 default:
119 UNREACHABLE();
120 return 0;
121 }
122}
123
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000124} // namespace
125
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800126TShHandleBase::TShHandleBase()
127{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000128 allocator.push();
129 SetGlobalPoolAllocator(&allocator);
130}
131
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800132TShHandleBase::~TShHandleBase()
133{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000134 SetGlobalPoolAllocator(NULL);
135 allocator.popAll();
136}
137
Jamie Madill183bde52014-07-02 15:31:19 -0400138TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000139 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000140 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400141 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400142 maxUniformVectors(0),
143 maxExpressionComplexity(0),
144 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200145 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000146 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000147 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200148 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700149 mSourcePath(NULL),
150 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000151{
152}
153
154TCompiler::~TCompiler()
155{
156}
157
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300158bool TCompiler::shouldRunLoopAndIndexingValidation(int compileOptions) const
159{
160 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
161 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
162 // of ESSL 1.00 as in Appendix A of the spec).
163 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
164 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
165}
166
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000167bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000168{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000169 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400170 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000171 resources.MaxVertexUniformVectors :
172 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400173 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200174 maxCallStackDepth = resources.MaxCallStackDepth;
175 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400176
177 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000178
alokp@chromium.org07620a52010-09-23 17:53:56 +0000179 // Generate built-in symbol table.
180 if (!InitBuiltInSymbolTable(resources))
181 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000182 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000183 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000184
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000185 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
186 clampingStrategy = resources.ArrayIndexClampingStrategy;
187
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000188 hashFunction = resources.HashFunction;
189
alokp@chromium.org07620a52010-09-23 17:53:56 +0000190 return true;
191}
192
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200193TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
194 size_t numStrings, int 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,
201 const int 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
alokp@chromium.org07620a52010-09-23 17:53:56 +0000219 TIntermediate intermediate(infoSink);
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200220 TParseContext parseContext(symbolTable, extensionBehavior, intermediate, shaderType, shaderSpec,
221 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200222
Olli Etuahoa6996682015-10-12 14:32:30 +0300223 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400224 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000225
226 // We preserve symbols at the built-in level from compile-to-compile.
227 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400228 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000229
230 // Parse shader.
231 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400232 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
233 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000234
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000235 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700236 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
237 {
238 infoSink.info.prefix(EPrefixError);
239 infoSink.info << "unsupported shader version";
240 success = false;
241 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000242
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400243 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200244
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800245 if (success)
246 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700247 mPragma = parseContext.pragma();
248 if (mPragma.stdgl.invariantAll)
249 {
250 symbolTable.setGlobalInvariant();
251 }
252
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400253 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300254 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000255
Olli Etuahoa6996682015-10-12 14:32:30 +0300256 // Highp might have been auto-enabled based on shader version
257 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
258
Jamie Madill6654bc92014-03-26 14:01:57 -0400259 // Disallow expressions deemed too complex.
260 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
261 success = limitExpressionComplexity(root);
262
Corentin Wallez71d147f2015-02-11 11:15:24 -0800263 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000264 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800265 success = initCallDag(root);
266
267 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
268 success = checkCallDepth();
269
270 // Checks which functions are used and if "main" exists
271 if (success)
272 {
273 functionMetadata.clear();
274 functionMetadata.resize(mCallDag.size());
275 success = tagUsedFunctions();
276 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000277
Corentin Walleza094a8a2015-04-07 11:53:06 -0700278 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
279 success = pruneUnusedFunctions(root);
280
Olli Etuahoc6833112015-04-22 15:15:54 +0300281 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
282 if (success)
283 PruneEmptyDeclarations(root);
284
Jamie Madill183bde52014-07-02 15:31:19 -0400285 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400286 success = validateOutputs(root);
287
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300288 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000289 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000290
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000291 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000292 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000293
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000294 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
295 rewriteCSSShader(root);
296
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000297 // Unroll for-loop markup needs to happen after validateLimitations pass.
298 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800299 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200300 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
301 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800302 root->traverse(&marker);
303 }
304 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800305 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200306 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
307 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800308 root->traverse(&marker);
309 if (marker.samplerArrayIndexIsFloatLoopIndex())
310 {
311 infoSink.info.prefix(EPrefixError);
312 infoSink.info << "sampler array index is float loop index";
313 success = false;
314 }
315 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000316
zmo@google.com32e97312011-08-24 01:03:11 +0000317 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200318 if (success)
319 {
320 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
zmo@google.com32e97312011-08-24 01:03:11 +0000321 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200322 }
zmo@google.com32e97312011-08-24 01:03:11 +0000323
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000324 // Clamping uniform array bounds needs to happen after validateLimitations pass.
325 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
326 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
327
Ian Ewell924b7de2016-01-21 13:54:28 -0500328 // gl_Position is always written in compatibility output mode
329 if (success && shaderType == GL_VERTEX_SHADER &&
330 ((compileOptions & SH_INIT_GL_POSITION) ||
331 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800332 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400333
Corentin Wallezd4b50542015-09-28 12:19:26 -0700334 // This pass might emit short circuits so keep it before the short circuit unfolding
335 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
336 RewriteDoWhile(root, getTemporaryIndex());
337
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800338 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
339 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700340 UnfoldShortCircuitAST unfoldShortCircuit;
341 root->traverse(&unfoldShortCircuit);
342 unfoldShortCircuit.updateTree();
343 }
344
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300345 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
346 {
347 RemovePow(root);
348 }
349
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300350 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800351 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400352 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800353 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
354 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000355 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800356 if (!success)
357 {
Jamie Madill075edd82013-07-08 13:30:19 -0400358 infoSink.info.prefix(EPrefixError);
359 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000360 }
361 }
Jamie Madill183bde52014-07-02 15:31:19 -0400362 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800363 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
364 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000365 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000366
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700367 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
368 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700369 ScalarizeVecAndMatConstructorArgs scalarizer(
370 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700371 root->traverse(&scalarizer);
372 }
373
Zhenyao Moe740add2014-07-18 17:01:01 -0700374 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
375 {
376 RegenerateStructNames gen(symbolTable, shaderVersion);
377 root->traverse(&gen);
378 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000379 }
380
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700381 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200382 if (success)
383 return root;
384
385 return NULL;
386}
387
388bool TCompiler::compile(const char* const shaderStrings[],
389 size_t numStrings, int compileOptions)
390{
391 if (numStrings == 0)
392 return true;
393
394 TScopedPoolAllocator scopedAlloc(&allocator);
395 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
396
397 if (root)
398 {
399 if (compileOptions & SH_INTERMEDIATE_TREE)
400 TIntermediate::outputTree(root, infoSink.info);
401
402 if (compileOptions & SH_OBJECT_CODE)
403 translate(root, compileOptions);
404
405 // The IntermNode tree doesn't need to be deleted here, since the
406 // memory will be freed in a big chunk by the PoolAllocator.
407 return true;
408 }
409 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000410}
411
Nicolas Capens49a88872013-06-20 09:54:03 -0400412bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000413{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000414 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400415 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000416
Nicolas Capens49a88872013-06-20 09:54:03 -0400417 assert(symbolTable.isEmpty());
418 symbolTable.push(); // COMMON_BUILTINS
419 symbolTable.push(); // ESSL1_BUILTINS
420 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000421
Nicolas Capens49a88872013-06-20 09:54:03 -0400422 TPublicType integer;
423 integer.type = EbtInt;
424 integer.primarySize = 1;
425 integer.secondarySize = 1;
426 integer.array = false;
427
428 TPublicType floatingPoint;
429 floatingPoint.type = EbtFloat;
430 floatingPoint.primarySize = 1;
431 floatingPoint.secondarySize = 1;
432 floatingPoint.array = false;
433
434 switch(shaderType)
435 {
Jamie Madill183bde52014-07-02 15:31:19 -0400436 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400437 symbolTable.setDefaultPrecision(integer, EbpMedium);
438 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400439 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400440 symbolTable.setDefaultPrecision(integer, EbpHigh);
441 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
442 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800443 default:
444 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400445 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200446 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400447 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200448 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
449 initSamplerDefaultPrecision(EbtSampler2D);
450 initSamplerDefaultPrecision(EbtSamplerCube);
451 // SamplerExternalOES is specified in the extension to have default precision.
452 initSamplerDefaultPrecision(EbtSamplerExternalOES);
453 // It isn't specified whether Sampler2DRect has default precision.
454 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400455
Jamie Madill1b452142013-07-12 14:51:11 -0400456 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400457
458 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
459
460 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000461}
462
Olli Etuaho183d7e22015-11-20 15:59:09 +0200463void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
464{
465 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
466 TPublicType sampler;
467 sampler.primarySize = 1;
468 sampler.secondarySize = 1;
469 sampler.array = false;
470 sampler.type = samplerType;
471 symbolTable.setDefaultPrecision(sampler, EbpLow);
472}
473
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400474void TCompiler::setResourceString()
475{
476 std::ostringstream strstream;
477 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
478 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
479 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
480 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
481 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
482 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
483 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
484 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
485 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
486 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
487 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
488 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
489 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
490 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
491 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200492 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300493 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400494 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
495 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100496 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
497 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
498 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400499 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
500 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
501 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300502 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300503 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200504 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
505 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400506
507 builtInResourcesString = strstream.str();
508}
509
alokp@chromium.org07620a52010-09-23 17:53:56 +0000510void TCompiler::clearResults()
511{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000512 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000513 infoSink.info.erase();
514 infoSink.obj.erase();
515 infoSink.debug.erase();
516
Jamie Madilled27c722014-07-02 15:31:23 -0400517 attributes.clear();
518 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000519 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400520 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400521 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400522 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000523
524 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000525
526 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200527
528 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700529 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000530}
531
Corentin Wallez71d147f2015-02-11 11:15:24 -0800532bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000533{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800534 mCallDag.clear();
535
536 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800537 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800538 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800539 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800540 case CallDAG::INITDAG_RECURSION:
541 infoSink.info.prefix(EPrefixError);
542 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800543 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800544 case CallDAG::INITDAG_UNDEFINED:
545 infoSink.info.prefix(EPrefixError);
546 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800547 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800548 }
549
550 UNREACHABLE();
551 return true;
552}
553
554bool TCompiler::checkCallDepth()
555{
556 std::vector<int> depths(mCallDag.size());
557
558 for (size_t i = 0; i < mCallDag.size(); i++)
559 {
560 int depth = 0;
561 auto &record = mCallDag.getRecordFromIndex(i);
562
563 for (auto &calleeIndex : record.callees)
564 {
565 depth = std::max(depth, depths[calleeIndex] + 1);
566 }
567
568 depths[i] = depth;
569
570 if (depth >= maxCallStackDepth)
571 {
572 // Trace back the function chain to have a meaningful info log.
573 infoSink.info.prefix(EPrefixError);
574 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
575 << ") with the following call chain: " << record.name;
576
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700577 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800578 int currentDepth = depth;
579
580 while (currentFunction != -1)
581 {
582 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
583
584 int nextFunction = -1;
585 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
586 {
587 if (depths[calleeIndex] == currentDepth - 1)
588 {
589 currentDepth--;
590 nextFunction = calleeIndex;
591 }
592 }
593
594 currentFunction = nextFunction;
595 }
596
597 return false;
598 }
599 }
600
601 return true;
602}
603
604bool TCompiler::tagUsedFunctions()
605{
606 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700607 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800608 {
609 if (mCallDag.getRecordFromIndex(i).name == "main(")
610 {
611 internalTagUsedFunction(i);
612 return true;
613 }
614 }
615
616 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200617 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800618 return false;
619}
620
621void TCompiler::internalTagUsedFunction(size_t index)
622{
623 if (functionMetadata[index].used)
624 {
625 return;
626 }
627
628 functionMetadata[index].used = true;
629
630 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
631 {
632 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000633 }
634}
635
Corentin Walleza094a8a2015-04-07 11:53:06 -0700636// A predicate for the stl that returns if a top-level node is unused
637class TCompiler::UnusedPredicate
638{
639 public:
640 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
641 : mCallDag(callDag),
642 mMetadatas(metadatas)
643 {
644 }
645
646 bool operator ()(TIntermNode *node)
647 {
648 const TIntermAggregate *asAggregate = node->getAsAggregate();
649
650 if (asAggregate == nullptr)
651 {
652 return false;
653 }
654
655 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
656 {
657 return false;
658 }
659
660 size_t callDagIndex = mCallDag->findIndex(asAggregate);
661 if (callDagIndex == CallDAG::InvalidIndex)
662 {
663 // This happens only for unimplemented prototypes which are thus unused
664 ASSERT(asAggregate->getOp() == EOpPrototype);
665 return true;
666 }
667
668 ASSERT(callDagIndex < mMetadatas->size());
669 return !(*mMetadatas)[callDagIndex].used;
670 }
671
672 private:
673 const CallDAG *mCallDag;
674 const std::vector<FunctionMetadata> *mMetadatas;
675};
676
677bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
678{
679 TIntermAggregate *rootNode = root->getAsAggregate();
680 ASSERT(rootNode != nullptr);
681
682 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
683 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700684
685 if (!sequence->empty())
686 {
687 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
688 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700689
690 return true;
691}
692
Jamie Madill05a80ce2013-06-20 11:55:49 -0400693bool TCompiler::validateOutputs(TIntermNode* root)
694{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300695 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400696 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300697 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400698}
699
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000700void TCompiler::rewriteCSSShader(TIntermNode* root)
701{
702 RenameFunction renamer("main(", "css_main(");
703 root->traverse(&renamer);
704}
705
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800706bool TCompiler::validateLimitations(TIntermNode* root)
707{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200708 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000709 root->traverse(&validate);
710 return validate.numErrors() == 0;
711}
712
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000713bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000714{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800715 if (shaderSpec != SH_WEBGL_SPEC)
716 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000717 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
718 return false;
719 }
720
Jamie Madill183bde52014-07-02 15:31:19 -0400721 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800722 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000723 TDependencyGraph graph(root);
724
725 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000726 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500727
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000728 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800729 if (outputGraph)
730 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000731 TDependencyGraphOutput output(infoSink.info);
732 output.outputAllSpanningTrees(graph);
733 }
Jamie Madill5508f392014-02-20 13:31:36 -0500734
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000735 return success;
736 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800737 else
738 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000739 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000740 }
741}
742
Jamie Madilleb1a0102013-07-08 13:31:38 -0400743bool TCompiler::limitExpressionComplexity(TIntermNode* root)
744{
Jamie Madill6654bc92014-03-26 14:01:57 -0400745 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400746 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400747
748 if (traverser.getMaxDepth() > maxExpressionComplexity)
749 {
750 infoSink.info << "Expression too complex.";
751 return false;
752 }
753
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200754 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
755 {
756 infoSink.info << "Function has too many parameters.";
757 return false;
758 }
759
Jamie Madilleb1a0102013-07-08 13:31:38 -0400760 return true;
761}
762
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000763bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000764{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000765 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000766 restrictor.enforceRestrictions(graph);
767 return restrictor.numErrors() == 0;
768}
769
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000770bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000771{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000772 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000773 restrictor.enforceRestrictions(root);
774 return restrictor.numErrors() == 0;
775}
776
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400777void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000778{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400779 sh::CollectVariables collect(&attributes,
780 &outputVariables,
781 &uniforms,
782 &varyings,
783 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700784 hashFunction,
785 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000786 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400787
Zhenyao Mo409078f2014-10-28 13:23:18 -0700788 // This is for enforcePackingRestriction().
789 sh::ExpandUniforms(uniforms, &expandedUniforms);
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{
800 InitializeVariables::InitVariableInfoList variables;
801 InitializeVariables::InitVariableInfo var(
802 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
803 variables.push_back(var);
804 InitializeVariables initializer(variables);
805 root->traverse(&initializer);
806}
807
808void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
809{
810 InitializeVariables::InitVariableInfoList variables;
811 for (size_t ii = 0; ii < varyings.size(); ++ii)
812 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400813 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800814 if (varying.staticUse)
815 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400816 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
817 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400818 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800819 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400820 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800821 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400822 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800823 name = name.substr(0, name.find_first_of('['));
824 }
825
826 InitializeVariables::InitVariableInfo var(name, type);
827 variables.push_back(var);
828 }
829 InitializeVariables initializer(variables);
830 root->traverse(&initializer);
831}
832
zmo@google.com5601ea02011-06-10 18:23:25 +0000833const TExtensionBehavior& TCompiler::getExtensionBehavior() const
834{
835 return extensionBehavior;
836}
zmo@google.com32e97312011-08-24 01:03:11 +0000837
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200838const char *TCompiler::getSourcePath() const
839{
840 return mSourcePath;
841}
842
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000843const ShBuiltInResources& TCompiler::getResources() const
844{
845 return compileResources;
846}
847
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000848const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
849{
850 return arrayBoundsClamper;
851}
852
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000853ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
854{
855 return clampingStrategy;
856}
857
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200858const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000859{
860 return builtInFunctionEmulator;
861}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700862
863void TCompiler::writePragma()
864{
865 TInfoSinkBase &sink = infoSink.obj;
866 if (mPragma.stdgl.invariantAll)
867 sink << "#pragma STDGL invariant(all)\n";
868}