blob: 93062ca6a733b24e99ee1e58fab0f2376704c48d [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
Jamie Madill88f6e942014-02-19 10:27:53 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org07620a52010-09-23 17:53:56 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Dmitry Skiba01971112015-07-10 14:54:00 -04007#include "compiler/translator/Cache.h"
Jamie Madilld4a3a312014-06-25 16:04:56 -04008#include "compiler/translator/Compiler.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -08009#include "compiler/translator/CallDAG.h"
Olli Etuaho3d932d82016-04-12 11:10:30 +030010#include "compiler/translator/DeferGlobalInitializers.h"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/ForLoopUnroll.h"
12#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040013#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080014#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040015#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030016#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070017#include "compiler/translator/RegenerateStructNames.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030018#include "compiler/translator/RemovePow.h"
Geoff Lang17732822013-08-29 13:46:49 -040019#include "compiler/translator/RenameFunction.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070020#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070021#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070022#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040023#include "compiler/translator/ValidateLimitations.h"
Olli Etuaho19d1dc92016-03-08 17:18:46 +020024#include "compiler/translator/ValidateMaxParameters.h"
Geoff Lang17732822013-08-29 13:46:49 -040025#include "compiler/translator/ValidateOutputs.h"
26#include "compiler/translator/VariablePacker.h"
27#include "compiler/translator/depgraph/DependencyGraph.h"
28#include "compiler/translator/depgraph/DependencyGraphOutput.h"
29#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
30#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000031#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040032#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040033#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000034
Jamie Madill5508f392014-02-20 13:31:36 -050035bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000036{
Zhenyao Modb9b40b2014-10-29 15:00:04 -070037 return (spec == SH_WEBGL_SPEC ||
38 spec == SH_CSS_SHADERS_SPEC ||
39 spec == SH_WEBGL2_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000040}
41
Qingqing Dengad0d0792015-04-08 14:25:06 -070042bool IsGLSL130OrNewer(ShShaderOutput output)
43{
44 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070045 output == SH_GLSL_140_OUTPUT ||
46 output == SH_GLSL_150_CORE_OUTPUT ||
47 output == SH_GLSL_330_CORE_OUTPUT ||
48 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070049 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070050 output == SH_GLSL_420_CORE_OUTPUT ||
51 output == SH_GLSL_430_CORE_OUTPUT ||
52 output == SH_GLSL_440_CORE_OUTPUT ||
53 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070054}
55
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070056size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050057{
Jamie Madill88f6e942014-02-19 10:27:53 -050058 // WebGL defines a max token legnth of 256, while ES2 leaves max token
59 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070060 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050061 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070062 case SH_WEBGL_SPEC:
63 case SH_CSS_SHADERS_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050064 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070065 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050066 return 1024;
67 }
68}
69
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000070namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070071
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080072class TScopedPoolAllocator
73{
74 public:
75 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
76 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040077 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000078 SetGlobalPoolAllocator(mAllocator);
79 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080080 ~TScopedPoolAllocator()
81 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000082 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040083 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000084 }
85
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080086 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000087 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040088};
89
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080090class TScopedSymbolTableLevel
91{
92 public:
93 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
94 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040095 ASSERT(mTable->atBuiltInLevel());
96 mTable->push();
97 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080098 ~TScopedSymbolTableLevel()
99 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400100 while (!mTable->atBuiltInLevel())
101 mTable->pop();
102 }
103
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800104 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400105 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000106};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700107
108int MapSpecToShaderVersion(ShShaderSpec spec)
109{
110 switch (spec)
111 {
112 case SH_GLES2_SPEC:
113 case SH_WEBGL_SPEC:
114 case SH_CSS_SHADERS_SPEC:
115 return 100;
116 case SH_GLES3_SPEC:
117 case SH_WEBGL2_SPEC:
118 return 300;
119 default:
120 UNREACHABLE();
121 return 0;
122 }
123}
124
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000125} // namespace
126
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800127TShHandleBase::TShHandleBase()
128{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000129 allocator.push();
130 SetGlobalPoolAllocator(&allocator);
131}
132
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800133TShHandleBase::~TShHandleBase()
134{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000135 SetGlobalPoolAllocator(NULL);
136 allocator.popAll();
137}
138
Jamie Madill183bde52014-07-02 15:31:19 -0400139TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000140 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000141 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400142 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400143 maxUniformVectors(0),
144 maxExpressionComplexity(0),
145 maxCallStackDepth(0),
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200146 maxFunctionParameters(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000147 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000148 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200149 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700150 mSourcePath(NULL),
151 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000152{
153}
154
155TCompiler::~TCompiler()
156{
157}
158
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300159bool TCompiler::shouldRunLoopAndIndexingValidation(int compileOptions) const
160{
161 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
162 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
163 // of ESSL 1.00 as in Appendix A of the spec).
164 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
165 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
166}
167
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000168bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000169{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000170 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400171 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000172 resources.MaxVertexUniformVectors :
173 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400174 maxExpressionComplexity = resources.MaxExpressionComplexity;
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200175 maxCallStackDepth = resources.MaxCallStackDepth;
176 maxFunctionParameters = resources.MaxFunctionParameters;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400177
178 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000179
alokp@chromium.org07620a52010-09-23 17:53:56 +0000180 // Generate built-in symbol table.
181 if (!InitBuiltInSymbolTable(resources))
182 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000183 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000184 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000185
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000186 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
187 clampingStrategy = resources.ArrayIndexClampingStrategy;
188
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000189 hashFunction = resources.HashFunction;
190
alokp@chromium.org07620a52010-09-23 17:53:56 +0000191 return true;
192}
193
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200194TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
195 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000196{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200197 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
198}
199
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300200TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
201 size_t numStrings,
202 const int compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200203{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000204 clearResults();
205
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200206 ASSERT(numStrings > 0);
207 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000208
David Yen0fbd1282015-02-02 14:46:09 -0800209 // Reset the extension behavior for each compilation unit.
210 ResetExtensionBehavior(extensionBehavior);
211
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000212 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000213 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000214 if (compileOptions & SH_SOURCE_PATH)
215 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200216 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000217 ++firstSource;
218 }
219
alokp@chromium.org07620a52010-09-23 17:53:56 +0000220 TIntermediate intermediate(infoSink);
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200221 TParseContext parseContext(symbolTable, extensionBehavior, intermediate, shaderType, shaderSpec,
222 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200223
Olli Etuahoa6996682015-10-12 14:32:30 +0300224 parseContext.setFragmentPrecisionHighOnESSL1(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400225 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000226
227 // We preserve symbols at the built-in level from compile-to-compile.
228 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400229 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000230
231 // Parse shader.
232 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400233 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
234 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000235
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000236 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700237 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
238 {
239 infoSink.info.prefix(EPrefixError);
240 infoSink.info << "unsupported shader version";
241 success = false;
242 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000243
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400244 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200245
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800246 if (success)
247 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700248 mPragma = parseContext.pragma();
249 if (mPragma.stdgl.invariantAll)
250 {
251 symbolTable.setGlobalInvariant();
252 }
253
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400254 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300255 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000256
Olli Etuahoa6996682015-10-12 14:32:30 +0300257 // Highp might have been auto-enabled based on shader version
258 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
259
Jamie Madill6654bc92014-03-26 14:01:57 -0400260 // Disallow expressions deemed too complex.
261 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
262 success = limitExpressionComplexity(root);
263
Corentin Wallez71d147f2015-02-11 11:15:24 -0800264 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000265 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800266 success = initCallDag(root);
267
268 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
269 success = checkCallDepth();
270
271 // Checks which functions are used and if "main" exists
272 if (success)
273 {
274 functionMetadata.clear();
275 functionMetadata.resize(mCallDag.size());
276 success = tagUsedFunctions();
277 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000278
Corentin Walleza094a8a2015-04-07 11:53:06 -0700279 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
280 success = pruneUnusedFunctions(root);
281
Olli Etuahoc6833112015-04-22 15:15:54 +0300282 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
283 if (success)
284 PruneEmptyDeclarations(root);
285
Jamie Madill183bde52014-07-02 15:31:19 -0400286 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400287 success = validateOutputs(root);
288
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300289 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000290 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000291
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000292 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000293 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000294
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000295 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
296 rewriteCSSShader(root);
297
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000298 // Unroll for-loop markup needs to happen after validateLimitations pass.
299 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800300 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200301 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
302 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800303 root->traverse(&marker);
304 }
305 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800306 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200307 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
308 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800309 root->traverse(&marker);
310 if (marker.samplerArrayIndexIsFloatLoopIndex())
311 {
312 infoSink.info.prefix(EPrefixError);
313 infoSink.info << "sampler array index is float loop index";
314 success = false;
315 }
316 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000317
zmo@google.com32e97312011-08-24 01:03:11 +0000318 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200319 if (success)
320 {
321 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
zmo@google.com32e97312011-08-24 01:03:11 +0000322 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200323 }
zmo@google.com32e97312011-08-24 01:03:11 +0000324
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000325 // Clamping uniform array bounds needs to happen after validateLimitations pass.
326 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
327 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
328
Ian Ewell924b7de2016-01-21 13:54:28 -0500329 // gl_Position is always written in compatibility output mode
330 if (success && shaderType == GL_VERTEX_SHADER &&
331 ((compileOptions & SH_INIT_GL_POSITION) ||
332 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800333 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400334
Corentin Wallezd4b50542015-09-28 12:19:26 -0700335 // This pass might emit short circuits so keep it before the short circuit unfolding
336 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
337 RewriteDoWhile(root, getTemporaryIndex());
338
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800339 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
340 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700341 UnfoldShortCircuitAST unfoldShortCircuit;
342 root->traverse(&unfoldShortCircuit);
343 unfoldShortCircuit.updateTree();
344 }
345
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300346 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
347 {
348 RemovePow(root);
349 }
350
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300351 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800352 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400353 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800354 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
355 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000356 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800357 if (!success)
358 {
Jamie Madill075edd82013-07-08 13:30:19 -0400359 infoSink.info.prefix(EPrefixError);
360 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000361 }
362 }
Jamie Madill183bde52014-07-02 15:31:19 -0400363 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800364 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
365 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000366 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000367
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700368 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
369 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700370 ScalarizeVecAndMatConstructorArgs scalarizer(
371 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700372 root->traverse(&scalarizer);
373 }
374
Zhenyao Moe740add2014-07-18 17:01:01 -0700375 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
376 {
377 RegenerateStructNames gen(symbolTable, shaderVersion);
378 root->traverse(&gen);
379 }
Olli Etuaho3d932d82016-04-12 11:10:30 +0300380
381 if (success)
382 {
383 DeferGlobalInitializers(root);
384 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000385 }
386
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700387 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200388 if (success)
389 return root;
390
391 return NULL;
392}
393
394bool TCompiler::compile(const char* const shaderStrings[],
395 size_t numStrings, int compileOptions)
396{
397 if (numStrings == 0)
398 return true;
399
400 TScopedPoolAllocator scopedAlloc(&allocator);
401 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
402
403 if (root)
404 {
405 if (compileOptions & SH_INTERMEDIATE_TREE)
406 TIntermediate::outputTree(root, infoSink.info);
407
408 if (compileOptions & SH_OBJECT_CODE)
409 translate(root, compileOptions);
410
411 // The IntermNode tree doesn't need to be deleted here, since the
412 // memory will be freed in a big chunk by the PoolAllocator.
413 return true;
414 }
415 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000416}
417
Nicolas Capens49a88872013-06-20 09:54:03 -0400418bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000419{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000420 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400421 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000422
Nicolas Capens49a88872013-06-20 09:54:03 -0400423 assert(symbolTable.isEmpty());
424 symbolTable.push(); // COMMON_BUILTINS
425 symbolTable.push(); // ESSL1_BUILTINS
426 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000427
Nicolas Capens49a88872013-06-20 09:54:03 -0400428 TPublicType integer;
429 integer.type = EbtInt;
430 integer.primarySize = 1;
431 integer.secondarySize = 1;
432 integer.array = false;
433
434 TPublicType floatingPoint;
435 floatingPoint.type = EbtFloat;
436 floatingPoint.primarySize = 1;
437 floatingPoint.secondarySize = 1;
438 floatingPoint.array = false;
439
440 switch(shaderType)
441 {
Jamie Madill183bde52014-07-02 15:31:19 -0400442 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400443 symbolTable.setDefaultPrecision(integer, EbpMedium);
444 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400445 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400446 symbolTable.setDefaultPrecision(integer, EbpHigh);
447 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
448 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800449 default:
450 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400451 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200452 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400453 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200454 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
455 initSamplerDefaultPrecision(EbtSampler2D);
456 initSamplerDefaultPrecision(EbtSamplerCube);
457 // SamplerExternalOES is specified in the extension to have default precision.
458 initSamplerDefaultPrecision(EbtSamplerExternalOES);
459 // It isn't specified whether Sampler2DRect has default precision.
460 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400461
Jamie Madill1b452142013-07-12 14:51:11 -0400462 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400463
464 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
465
466 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000467}
468
Olli Etuaho183d7e22015-11-20 15:59:09 +0200469void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
470{
471 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
472 TPublicType sampler;
473 sampler.primarySize = 1;
474 sampler.secondarySize = 1;
475 sampler.array = false;
476 sampler.type = samplerType;
477 symbolTable.setDefaultPrecision(sampler, EbpLow);
478}
479
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400480void TCompiler::setResourceString()
481{
482 std::ostringstream strstream;
Geoff Langb66a9092016-05-16 15:59:14 -0400483
484 // clang-format off
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400485 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
486 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
487 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
488 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
489 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
490 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
491 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
492 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
493 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
494 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
Geoff Langb66a9092016-05-16 15:59:14 -0400495 << ":OES_EGL_image_external_essl3:" << compileResources.OES_EGL_image_external_essl3
496 << ":NV_EGL_stream_consumer_external:" << compileResources.NV_EGL_stream_consumer_external
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400497 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
498 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
499 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
500 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
501 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200502 << ":MaxFunctionParameters:" << compileResources.MaxFunctionParameters
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300503 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400504 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
505 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100506 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
507 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
508 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400509 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
510 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
511 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300512 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300513 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200514 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
515 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Geoff Langb66a9092016-05-16 15:59:14 -0400516 // clang-format on
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400517
518 builtInResourcesString = strstream.str();
519}
520
alokp@chromium.org07620a52010-09-23 17:53:56 +0000521void TCompiler::clearResults()
522{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000523 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000524 infoSink.info.erase();
525 infoSink.obj.erase();
526 infoSink.debug.erase();
527
Jamie Madilled27c722014-07-02 15:31:23 -0400528 attributes.clear();
529 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000530 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400531 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400532 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400533 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000534
535 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000536
537 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200538
539 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700540 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000541}
542
Corentin Wallez71d147f2015-02-11 11:15:24 -0800543bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000544{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800545 mCallDag.clear();
546
547 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800548 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800549 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800550 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800551 case CallDAG::INITDAG_RECURSION:
552 infoSink.info.prefix(EPrefixError);
553 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800554 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800555 case CallDAG::INITDAG_UNDEFINED:
556 infoSink.info.prefix(EPrefixError);
557 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800558 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800559 }
560
561 UNREACHABLE();
562 return true;
563}
564
565bool TCompiler::checkCallDepth()
566{
567 std::vector<int> depths(mCallDag.size());
568
569 for (size_t i = 0; i < mCallDag.size(); i++)
570 {
571 int depth = 0;
572 auto &record = mCallDag.getRecordFromIndex(i);
573
574 for (auto &calleeIndex : record.callees)
575 {
576 depth = std::max(depth, depths[calleeIndex] + 1);
577 }
578
579 depths[i] = depth;
580
581 if (depth >= maxCallStackDepth)
582 {
583 // Trace back the function chain to have a meaningful info log.
584 infoSink.info.prefix(EPrefixError);
585 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
586 << ") with the following call chain: " << record.name;
587
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700588 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800589 int currentDepth = depth;
590
591 while (currentFunction != -1)
592 {
593 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
594
595 int nextFunction = -1;
596 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
597 {
598 if (depths[calleeIndex] == currentDepth - 1)
599 {
600 currentDepth--;
601 nextFunction = calleeIndex;
602 }
603 }
604
605 currentFunction = nextFunction;
606 }
607
608 return false;
609 }
610 }
611
612 return true;
613}
614
615bool TCompiler::tagUsedFunctions()
616{
617 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700618 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800619 {
620 if (mCallDag.getRecordFromIndex(i).name == "main(")
621 {
622 internalTagUsedFunction(i);
623 return true;
624 }
625 }
626
627 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200628 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800629 return false;
630}
631
632void TCompiler::internalTagUsedFunction(size_t index)
633{
634 if (functionMetadata[index].used)
635 {
636 return;
637 }
638
639 functionMetadata[index].used = true;
640
641 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
642 {
643 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000644 }
645}
646
Corentin Walleza094a8a2015-04-07 11:53:06 -0700647// A predicate for the stl that returns if a top-level node is unused
648class TCompiler::UnusedPredicate
649{
650 public:
651 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
652 : mCallDag(callDag),
653 mMetadatas(metadatas)
654 {
655 }
656
657 bool operator ()(TIntermNode *node)
658 {
659 const TIntermAggregate *asAggregate = node->getAsAggregate();
660
661 if (asAggregate == nullptr)
662 {
663 return false;
664 }
665
666 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
667 {
668 return false;
669 }
670
671 size_t callDagIndex = mCallDag->findIndex(asAggregate);
672 if (callDagIndex == CallDAG::InvalidIndex)
673 {
674 // This happens only for unimplemented prototypes which are thus unused
675 ASSERT(asAggregate->getOp() == EOpPrototype);
676 return true;
677 }
678
679 ASSERT(callDagIndex < mMetadatas->size());
680 return !(*mMetadatas)[callDagIndex].used;
681 }
682
683 private:
684 const CallDAG *mCallDag;
685 const std::vector<FunctionMetadata> *mMetadatas;
686};
687
688bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
689{
690 TIntermAggregate *rootNode = root->getAsAggregate();
691 ASSERT(rootNode != nullptr);
692
693 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
694 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700695
696 if (!sequence->empty())
697 {
698 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
699 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700700
701 return true;
702}
703
Jamie Madill05a80ce2013-06-20 11:55:49 -0400704bool TCompiler::validateOutputs(TIntermNode* root)
705{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300706 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400707 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300708 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400709}
710
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000711void TCompiler::rewriteCSSShader(TIntermNode* root)
712{
713 RenameFunction renamer("main(", "css_main(");
714 root->traverse(&renamer);
715}
716
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800717bool TCompiler::validateLimitations(TIntermNode* root)
718{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200719 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000720 root->traverse(&validate);
721 return validate.numErrors() == 0;
722}
723
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000724bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000725{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800726 if (shaderSpec != SH_WEBGL_SPEC)
727 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000728 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
729 return false;
730 }
731
Jamie Madill183bde52014-07-02 15:31:19 -0400732 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800733 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000734 TDependencyGraph graph(root);
735
736 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000737 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500738
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000739 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800740 if (outputGraph)
741 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000742 TDependencyGraphOutput output(infoSink.info);
743 output.outputAllSpanningTrees(graph);
744 }
Jamie Madill5508f392014-02-20 13:31:36 -0500745
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000746 return success;
747 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800748 else
749 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000750 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000751 }
752}
753
Jamie Madilleb1a0102013-07-08 13:31:38 -0400754bool TCompiler::limitExpressionComplexity(TIntermNode* root)
755{
Jamie Madill6654bc92014-03-26 14:01:57 -0400756 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400757 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400758
759 if (traverser.getMaxDepth() > maxExpressionComplexity)
760 {
761 infoSink.info << "Expression too complex.";
762 return false;
763 }
764
Olli Etuaho19d1dc92016-03-08 17:18:46 +0200765 if (!ValidateMaxParameters::validate(root, maxFunctionParameters))
766 {
767 infoSink.info << "Function has too many parameters.";
768 return false;
769 }
770
Jamie Madilleb1a0102013-07-08 13:31:38 -0400771 return true;
772}
773
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000774bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000775{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000776 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000777 restrictor.enforceRestrictions(graph);
778 return restrictor.numErrors() == 0;
779}
780
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000781bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000782{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000783 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000784 restrictor.enforceRestrictions(root);
785 return restrictor.numErrors() == 0;
786}
787
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400788void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000789{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400790 sh::CollectVariables collect(&attributes,
791 &outputVariables,
792 &uniforms,
793 &varyings,
794 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700795 hashFunction,
796 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000797 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400798
Zhenyao Mo409078f2014-10-28 13:23:18 -0700799 // This is for enforcePackingRestriction().
800 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000801}
zmo@google.comfd747b82011-04-23 01:30:07 +0000802
gman@chromium.org8d804792012-10-17 21:33:48 +0000803bool TCompiler::enforcePackingRestrictions()
804{
805 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400806 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000807}
808
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800809void TCompiler::initializeGLPosition(TIntermNode* root)
810{
811 InitializeVariables::InitVariableInfoList variables;
812 InitializeVariables::InitVariableInfo var(
813 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
814 variables.push_back(var);
815 InitializeVariables initializer(variables);
816 root->traverse(&initializer);
817}
818
819void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
820{
821 InitializeVariables::InitVariableInfoList variables;
822 for (size_t ii = 0; ii < varyings.size(); ++ii)
823 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400824 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800825 if (varying.staticUse)
826 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400827 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
828 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400829 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800830 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400831 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800832 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400833 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800834 name = name.substr(0, name.find_first_of('['));
835 }
836
837 InitializeVariables::InitVariableInfo var(name, type);
838 variables.push_back(var);
839 }
840 InitializeVariables initializer(variables);
841 root->traverse(&initializer);
842}
843
zmo@google.com5601ea02011-06-10 18:23:25 +0000844const TExtensionBehavior& TCompiler::getExtensionBehavior() const
845{
846 return extensionBehavior;
847}
zmo@google.com32e97312011-08-24 01:03:11 +0000848
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200849const char *TCompiler::getSourcePath() const
850{
851 return mSourcePath;
852}
853
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000854const ShBuiltInResources& TCompiler::getResources() const
855{
856 return compileResources;
857}
858
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000859const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
860{
861 return arrayBoundsClamper;
862}
863
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000864ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
865{
866 return clampingStrategy;
867}
868
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200869const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000870{
871 return builtInFunctionEmulator;
872}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700873
874void TCompiler::writePragma()
875{
876 TInfoSinkBase &sink = infoSink.obj;
877 if (mPragma.stdgl.invariantAll)
878 sink << "#pragma STDGL invariant(all)\n";
879}