blob: b61fb6d26ecf42b3e5dce26616f4d528f7d0a1f8 [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"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070019#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070020#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040021#include "compiler/translator/ValidateLimitations.h"
22#include "compiler/translator/ValidateOutputs.h"
23#include "compiler/translator/VariablePacker.h"
24#include "compiler/translator/depgraph/DependencyGraph.h"
25#include "compiler/translator/depgraph/DependencyGraphOutput.h"
26#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
27#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000028#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040029#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040030#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000031
Jamie Madill5508f392014-02-20 13:31:36 -050032bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000033{
Zhenyao Modb9b40b2014-10-29 15:00:04 -070034 return (spec == SH_WEBGL_SPEC ||
35 spec == SH_CSS_SHADERS_SPEC ||
36 spec == SH_WEBGL2_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000037}
38
Qingqing Dengad0d0792015-04-08 14:25:06 -070039bool IsGLSL130OrNewer(ShShaderOutput output)
40{
41 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070042 output == SH_GLSL_140_OUTPUT ||
43 output == SH_GLSL_150_CORE_OUTPUT ||
44 output == SH_GLSL_330_CORE_OUTPUT ||
45 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070046 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070047 output == SH_GLSL_420_CORE_OUTPUT ||
48 output == SH_GLSL_430_CORE_OUTPUT ||
49 output == SH_GLSL_440_CORE_OUTPUT ||
50 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070051}
52
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070053size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050054{
Jamie Madill88f6e942014-02-19 10:27:53 -050055 // WebGL defines a max token legnth of 256, while ES2 leaves max token
56 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070057 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050058 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070059 case SH_WEBGL_SPEC:
60 case SH_CSS_SHADERS_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050061 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070062 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050063 return 1024;
64 }
65}
66
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000067namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070068
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080069class TScopedPoolAllocator
70{
71 public:
72 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
73 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040074 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000075 SetGlobalPoolAllocator(mAllocator);
76 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080077 ~TScopedPoolAllocator()
78 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000079 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040080 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000081 }
82
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080083 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000084 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040085};
86
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080087class TScopedSymbolTableLevel
88{
89 public:
90 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
91 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040092 ASSERT(mTable->atBuiltInLevel());
93 mTable->push();
94 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080095 ~TScopedSymbolTableLevel()
96 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040097 while (!mTable->atBuiltInLevel())
98 mTable->pop();
99 }
100
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800101 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400102 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000103};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700104
105int MapSpecToShaderVersion(ShShaderSpec spec)
106{
107 switch (spec)
108 {
109 case SH_GLES2_SPEC:
110 case SH_WEBGL_SPEC:
111 case SH_CSS_SHADERS_SPEC:
112 return 100;
113 case SH_GLES3_SPEC:
114 case SH_WEBGL2_SPEC:
115 return 300;
116 default:
117 UNREACHABLE();
118 return 0;
119 }
120}
121
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000122} // namespace
123
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800124TShHandleBase::TShHandleBase()
125{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000126 allocator.push();
127 SetGlobalPoolAllocator(&allocator);
128}
129
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800130TShHandleBase::~TShHandleBase()
131{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000132 SetGlobalPoolAllocator(NULL);
133 allocator.popAll();
134}
135
Jamie Madill183bde52014-07-02 15:31:19 -0400136TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000137 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000138 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400139 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400140 maxUniformVectors(0),
141 maxExpressionComplexity(0),
142 maxCallStackDepth(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(),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200146 mSourcePath(NULL)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000147{
148}
149
150TCompiler::~TCompiler()
151{
152}
153
154bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000155{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000156 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400157 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000158 resources.MaxVertexUniformVectors :
159 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400160 maxExpressionComplexity = resources.MaxExpressionComplexity;
161 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400162
163 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000164
alokp@chromium.org07620a52010-09-23 17:53:56 +0000165 // Generate built-in symbol table.
166 if (!InitBuiltInSymbolTable(resources))
167 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000168 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000169 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000170
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000171 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
172 clampingStrategy = resources.ArrayIndexClampingStrategy;
173
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000174 hashFunction = resources.HashFunction;
175
alokp@chromium.org07620a52010-09-23 17:53:56 +0000176 return true;
177}
178
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200179TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
180 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000181{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200182 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
183}
184
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300185TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
186 size_t numStrings,
187 const int compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200188{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000189 clearResults();
190
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200191 ASSERT(numStrings > 0);
192 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000193
David Yen0fbd1282015-02-02 14:46:09 -0800194 // Reset the extension behavior for each compilation unit.
195 ResetExtensionBehavior(extensionBehavior);
196
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000197 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000198 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000199 if (compileOptions & SH_SOURCE_PATH)
200 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200201 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000202 ++firstSource;
203 }
204
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200205 bool debugShaderPrecision = getResources().WEBGL_debug_shader_precision == 1;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000206 TIntermediate intermediate(infoSink);
207 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000208 shaderType, shaderSpec, compileOptions, true,
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200209 infoSink, debugShaderPrecision);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200210
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400211 parseContext.setFragmentPrecisionHigh(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400212 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000213
214 // We preserve symbols at the built-in level from compile-to-compile.
215 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400216 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000217
218 // Parse shader.
219 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400220 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
221 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000222
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000223 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700224 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
225 {
226 infoSink.info.prefix(EPrefixError);
227 infoSink.info << "unsupported shader version";
228 success = false;
229 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000230
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300231 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
232 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
233 // of ESSL 1.00 as in Appendix A of the spec).
234 bool validateLoopAndIndexing = (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
235 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
236
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400237 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200238
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800239 if (success)
240 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700241 mPragma = parseContext.pragma();
242 if (mPragma.stdgl.invariantAll)
243 {
244 symbolTable.setGlobalInvariant();
245 }
246
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400247 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300248 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000249
Jamie Madill6654bc92014-03-26 14:01:57 -0400250 // Disallow expressions deemed too complex.
251 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
252 success = limitExpressionComplexity(root);
253
Corentin Wallez71d147f2015-02-11 11:15:24 -0800254 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000255 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800256 success = initCallDag(root);
257
258 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
259 success = checkCallDepth();
260
261 // Checks which functions are used and if "main" exists
262 if (success)
263 {
264 functionMetadata.clear();
265 functionMetadata.resize(mCallDag.size());
266 success = tagUsedFunctions();
267 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000268
Corentin Walleza094a8a2015-04-07 11:53:06 -0700269 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
270 success = pruneUnusedFunctions(root);
271
Olli Etuahoc6833112015-04-22 15:15:54 +0300272 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
273 if (success)
274 PruneEmptyDeclarations(root);
275
Jamie Madill183bde52014-07-02 15:31:19 -0400276 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400277 success = validateOutputs(root);
278
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300279 if (success && validateLoopAndIndexing)
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000280 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000281
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000282 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000283 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000284
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000285 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
286 rewriteCSSShader(root);
287
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000288 // Unroll for-loop markup needs to happen after validateLimitations pass.
289 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800290 {
291 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800292 root->traverse(&marker);
293 }
294 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800295 {
296 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800297 root->traverse(&marker);
298 if (marker.samplerArrayIndexIsFloatLoopIndex())
299 {
300 infoSink.info.prefix(EPrefixError);
301 infoSink.info << "sampler array index is float loop index";
302 success = false;
303 }
304 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000305
zmo@google.com32e97312011-08-24 01:03:11 +0000306 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200307 if (success)
308 {
309 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
zmo@google.com32e97312011-08-24 01:03:11 +0000310 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200311 }
zmo@google.com32e97312011-08-24 01:03:11 +0000312
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000313 // Clamping uniform array bounds needs to happen after validateLimitations pass.
314 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
315 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
316
Jamie Madill183bde52014-07-02 15:31:19 -0400317 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800318 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400319
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800320 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
321 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700322 UnfoldShortCircuitAST unfoldShortCircuit;
323 root->traverse(&unfoldShortCircuit);
324 unfoldShortCircuit.updateTree();
325 }
326
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300327 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
328 {
329 RemovePow(root);
330 }
331
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300332 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800333 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400334 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800335 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
336 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000337 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800338 if (!success)
339 {
Jamie Madill075edd82013-07-08 13:30:19 -0400340 infoSink.info.prefix(EPrefixError);
341 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000342 }
343 }
Jamie Madill183bde52014-07-02 15:31:19 -0400344 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800345 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
346 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000347 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000348
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700349 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
350 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700351 ScalarizeVecAndMatConstructorArgs scalarizer(
352 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700353 root->traverse(&scalarizer);
354 }
355
Zhenyao Moe740add2014-07-18 17:01:01 -0700356 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
357 {
358 RegenerateStructNames gen(symbolTable, shaderVersion);
359 root->traverse(&gen);
360 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000361 }
362
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700363 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200364 if (success)
365 return root;
366
367 return NULL;
368}
369
370bool TCompiler::compile(const char* const shaderStrings[],
371 size_t numStrings, int compileOptions)
372{
373 if (numStrings == 0)
374 return true;
375
376 TScopedPoolAllocator scopedAlloc(&allocator);
377 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
378
379 if (root)
380 {
381 if (compileOptions & SH_INTERMEDIATE_TREE)
382 TIntermediate::outputTree(root, infoSink.info);
383
384 if (compileOptions & SH_OBJECT_CODE)
385 translate(root, compileOptions);
386
387 // The IntermNode tree doesn't need to be deleted here, since the
388 // memory will be freed in a big chunk by the PoolAllocator.
389 return true;
390 }
391 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000392}
393
Nicolas Capens49a88872013-06-20 09:54:03 -0400394bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000395{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000396 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400397 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000398
Nicolas Capens49a88872013-06-20 09:54:03 -0400399 assert(symbolTable.isEmpty());
400 symbolTable.push(); // COMMON_BUILTINS
401 symbolTable.push(); // ESSL1_BUILTINS
402 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000403
Nicolas Capens49a88872013-06-20 09:54:03 -0400404 TPublicType integer;
405 integer.type = EbtInt;
406 integer.primarySize = 1;
407 integer.secondarySize = 1;
408 integer.array = false;
409
410 TPublicType floatingPoint;
411 floatingPoint.type = EbtFloat;
412 floatingPoint.primarySize = 1;
413 floatingPoint.secondarySize = 1;
414 floatingPoint.array = false;
415
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400416 TPublicType sampler;
417 sampler.primarySize = 1;
418 sampler.secondarySize = 1;
419 sampler.array = false;
420
Nicolas Capens49a88872013-06-20 09:54:03 -0400421 switch(shaderType)
422 {
Jamie Madill183bde52014-07-02 15:31:19 -0400423 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400424 symbolTable.setDefaultPrecision(integer, EbpMedium);
425 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400426 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400427 symbolTable.setDefaultPrecision(integer, EbpHigh);
428 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
429 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800430 default:
431 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400432 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400433 // We set defaults for all the sampler types, even those that are
434 // only available if an extension exists.
435 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800436 samplerType < EbtGuardSamplerEnd; ++samplerType)
437 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400438 sampler.type = static_cast<TBasicType>(samplerType);
439 symbolTable.setDefaultPrecision(sampler, EbpLow);
440 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400441
Jamie Madill1b452142013-07-12 14:51:11 -0400442 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400443
444 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
445
446 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000447}
448
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400449void TCompiler::setResourceString()
450{
451 std::ostringstream strstream;
452 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
453 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
454 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
455 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
456 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
457 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
458 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
459 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
460 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
461 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
462 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
463 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
464 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
465 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
466 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300467 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400468 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
469 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100470 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
471 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
472 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400473 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
474 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
475 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300476 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300477 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200478 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
479 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400480
481 builtInResourcesString = strstream.str();
482}
483
alokp@chromium.org07620a52010-09-23 17:53:56 +0000484void TCompiler::clearResults()
485{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000486 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000487 infoSink.info.erase();
488 infoSink.obj.erase();
489 infoSink.debug.erase();
490
Jamie Madilled27c722014-07-02 15:31:23 -0400491 attributes.clear();
492 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000493 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400494 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400495 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400496 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000497
498 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000499
500 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200501
502 mSourcePath = NULL;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000503}
504
Corentin Wallez71d147f2015-02-11 11:15:24 -0800505bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000506{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800507 mCallDag.clear();
508
509 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800510 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800511 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800512 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800513 case CallDAG::INITDAG_RECURSION:
514 infoSink.info.prefix(EPrefixError);
515 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800516 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800517 case CallDAG::INITDAG_UNDEFINED:
518 infoSink.info.prefix(EPrefixError);
519 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800520 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800521 }
522
523 UNREACHABLE();
524 return true;
525}
526
527bool TCompiler::checkCallDepth()
528{
529 std::vector<int> depths(mCallDag.size());
530
531 for (size_t i = 0; i < mCallDag.size(); i++)
532 {
533 int depth = 0;
534 auto &record = mCallDag.getRecordFromIndex(i);
535
536 for (auto &calleeIndex : record.callees)
537 {
538 depth = std::max(depth, depths[calleeIndex] + 1);
539 }
540
541 depths[i] = depth;
542
543 if (depth >= maxCallStackDepth)
544 {
545 // Trace back the function chain to have a meaningful info log.
546 infoSink.info.prefix(EPrefixError);
547 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
548 << ") with the following call chain: " << record.name;
549
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700550 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800551 int currentDepth = depth;
552
553 while (currentFunction != -1)
554 {
555 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
556
557 int nextFunction = -1;
558 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
559 {
560 if (depths[calleeIndex] == currentDepth - 1)
561 {
562 currentDepth--;
563 nextFunction = calleeIndex;
564 }
565 }
566
567 currentFunction = nextFunction;
568 }
569
570 return false;
571 }
572 }
573
574 return true;
575}
576
577bool TCompiler::tagUsedFunctions()
578{
579 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700580 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800581 {
582 if (mCallDag.getRecordFromIndex(i).name == "main(")
583 {
584 internalTagUsedFunction(i);
585 return true;
586 }
587 }
588
589 infoSink.info.prefix(EPrefixError);
590 infoSink.info << "Missing main()";
591 return false;
592}
593
594void TCompiler::internalTagUsedFunction(size_t index)
595{
596 if (functionMetadata[index].used)
597 {
598 return;
599 }
600
601 functionMetadata[index].used = true;
602
603 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
604 {
605 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000606 }
607}
608
Corentin Walleza094a8a2015-04-07 11:53:06 -0700609// A predicate for the stl that returns if a top-level node is unused
610class TCompiler::UnusedPredicate
611{
612 public:
613 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
614 : mCallDag(callDag),
615 mMetadatas(metadatas)
616 {
617 }
618
619 bool operator ()(TIntermNode *node)
620 {
621 const TIntermAggregate *asAggregate = node->getAsAggregate();
622
623 if (asAggregate == nullptr)
624 {
625 return false;
626 }
627
628 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
629 {
630 return false;
631 }
632
633 size_t callDagIndex = mCallDag->findIndex(asAggregate);
634 if (callDagIndex == CallDAG::InvalidIndex)
635 {
636 // This happens only for unimplemented prototypes which are thus unused
637 ASSERT(asAggregate->getOp() == EOpPrototype);
638 return true;
639 }
640
641 ASSERT(callDagIndex < mMetadatas->size());
642 return !(*mMetadatas)[callDagIndex].used;
643 }
644
645 private:
646 const CallDAG *mCallDag;
647 const std::vector<FunctionMetadata> *mMetadatas;
648};
649
650bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
651{
652 TIntermAggregate *rootNode = root->getAsAggregate();
653 ASSERT(rootNode != nullptr);
654
655 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
656 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700657
658 if (!sequence->empty())
659 {
660 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
661 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700662
663 return true;
664}
665
Jamie Madill05a80ce2013-06-20 11:55:49 -0400666bool TCompiler::validateOutputs(TIntermNode* root)
667{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300668 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400669 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300670 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400671}
672
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000673void TCompiler::rewriteCSSShader(TIntermNode* root)
674{
675 RenameFunction renamer("main(", "css_main(");
676 root->traverse(&renamer);
677}
678
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800679bool TCompiler::validateLimitations(TIntermNode* root)
680{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000681 ValidateLimitations validate(shaderType, infoSink.info);
682 root->traverse(&validate);
683 return validate.numErrors() == 0;
684}
685
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000686bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000687{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800688 if (shaderSpec != SH_WEBGL_SPEC)
689 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000690 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
691 return false;
692 }
693
Jamie Madill183bde52014-07-02 15:31:19 -0400694 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800695 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000696 TDependencyGraph graph(root);
697
698 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000699 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500700
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000701 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800702 if (outputGraph)
703 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000704 TDependencyGraphOutput output(infoSink.info);
705 output.outputAllSpanningTrees(graph);
706 }
Jamie Madill5508f392014-02-20 13:31:36 -0500707
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000708 return success;
709 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800710 else
711 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000712 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000713 }
714}
715
Jamie Madilleb1a0102013-07-08 13:31:38 -0400716bool TCompiler::limitExpressionComplexity(TIntermNode* root)
717{
Jamie Madill6654bc92014-03-26 14:01:57 -0400718 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400719 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400720
721 if (traverser.getMaxDepth() > maxExpressionComplexity)
722 {
723 infoSink.info << "Expression too complex.";
724 return false;
725 }
726
Jamie Madilleb1a0102013-07-08 13:31:38 -0400727 TDependencyGraph graph(root);
728
729 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
730 iter != graph.endUserDefinedFunctionCalls();
731 ++iter)
732 {
733 TGraphFunctionCall* samplerSymbol = *iter;
734 TDependencyGraphTraverser graphTraverser;
735 samplerSymbol->traverse(&graphTraverser);
736 }
737
Jamie Madilleb1a0102013-07-08 13:31:38 -0400738 return true;
739}
740
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000741bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000742{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000743 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000744 restrictor.enforceRestrictions(graph);
745 return restrictor.numErrors() == 0;
746}
747
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000748bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000749{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000750 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000751 restrictor.enforceRestrictions(root);
752 return restrictor.numErrors() == 0;
753}
754
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400755void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000756{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400757 sh::CollectVariables collect(&attributes,
758 &outputVariables,
759 &uniforms,
760 &varyings,
761 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700762 hashFunction,
763 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000764 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400765
Zhenyao Mo409078f2014-10-28 13:23:18 -0700766 // This is for enforcePackingRestriction().
767 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000768}
zmo@google.comfd747b82011-04-23 01:30:07 +0000769
gman@chromium.org8d804792012-10-17 21:33:48 +0000770bool TCompiler::enforcePackingRestrictions()
771{
772 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400773 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000774}
775
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800776void TCompiler::initializeGLPosition(TIntermNode* root)
777{
778 InitializeVariables::InitVariableInfoList variables;
779 InitializeVariables::InitVariableInfo var(
780 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
781 variables.push_back(var);
782 InitializeVariables initializer(variables);
783 root->traverse(&initializer);
784}
785
786void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
787{
788 InitializeVariables::InitVariableInfoList variables;
789 for (size_t ii = 0; ii < varyings.size(); ++ii)
790 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400791 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800792 if (varying.staticUse)
793 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400794 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
795 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400796 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800797 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400798 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800799 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400800 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800801 name = name.substr(0, name.find_first_of('['));
802 }
803
804 InitializeVariables::InitVariableInfo var(name, type);
805 variables.push_back(var);
806 }
807 InitializeVariables initializer(variables);
808 root->traverse(&initializer);
809}
810
zmo@google.com5601ea02011-06-10 18:23:25 +0000811const TExtensionBehavior& TCompiler::getExtensionBehavior() const
812{
813 return extensionBehavior;
814}
zmo@google.com32e97312011-08-24 01:03:11 +0000815
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200816const char *TCompiler::getSourcePath() const
817{
818 return mSourcePath;
819}
820
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000821const ShBuiltInResources& TCompiler::getResources() const
822{
823 return compileResources;
824}
825
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000826const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
827{
828 return arrayBoundsClamper;
829}
830
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000831ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
832{
833 return clampingStrategy;
834}
835
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200836const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000837{
838 return builtInFunctionEmulator;
839}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700840
841void TCompiler::writePragma()
842{
843 TInfoSinkBase &sink = infoSink.obj;
844 if (mPragma.stdgl.invariantAll)
845 sink << "#pragma STDGL invariant(all)\n";
846}