blob: 5994edefc08d56bbeadc9fb65a5c3ecdf6188228 [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
Olli Etuaho3766a402015-06-18 15:47:46 +0300154bool TCompiler::shouldRunLoopAndIndexingValidation(int compileOptions) const
155{
156 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
157 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
158 // of ESSL 1.00 as in Appendix A of the spec).
159 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
160 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
161}
162
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000163bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000164{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000165 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400166 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000167 resources.MaxVertexUniformVectors :
168 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400169 maxExpressionComplexity = resources.MaxExpressionComplexity;
170 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400171
172 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000173
alokp@chromium.org07620a52010-09-23 17:53:56 +0000174 // Generate built-in symbol table.
175 if (!InitBuiltInSymbolTable(resources))
176 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000177 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000178 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000179
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000180 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
181 clampingStrategy = resources.ArrayIndexClampingStrategy;
182
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000183 hashFunction = resources.HashFunction;
184
alokp@chromium.org07620a52010-09-23 17:53:56 +0000185 return true;
186}
187
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200188TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
189 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000190{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200191 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
192}
193
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300194TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
195 size_t numStrings,
196 const int compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200197{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000198 clearResults();
199
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200200 ASSERT(numStrings > 0);
201 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000202
David Yen0fbd1282015-02-02 14:46:09 -0800203 // Reset the extension behavior for each compilation unit.
204 ResetExtensionBehavior(extensionBehavior);
205
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000206 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000207 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000208 if (compileOptions & SH_SOURCE_PATH)
209 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200210 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000211 ++firstSource;
212 }
213
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200214 bool debugShaderPrecision = getResources().WEBGL_debug_shader_precision == 1;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000215 TIntermediate intermediate(infoSink);
216 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000217 shaderType, shaderSpec, compileOptions, true,
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200218 infoSink, debugShaderPrecision);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200219
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400220 parseContext.setFragmentPrecisionHigh(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400221 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000222
223 // We preserve symbols at the built-in level from compile-to-compile.
224 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400225 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000226
227 // Parse shader.
228 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400229 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
230 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000231
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000232 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700233 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
234 {
235 infoSink.info.prefix(EPrefixError);
236 infoSink.info << "unsupported shader version";
237 success = false;
238 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000239
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400240 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200241
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800242 if (success)
243 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700244 mPragma = parseContext.pragma();
245 if (mPragma.stdgl.invariantAll)
246 {
247 symbolTable.setGlobalInvariant();
248 }
249
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400250 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300251 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000252
Jamie Madill6654bc92014-03-26 14:01:57 -0400253 // Disallow expressions deemed too complex.
254 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
255 success = limitExpressionComplexity(root);
256
Corentin Wallez71d147f2015-02-11 11:15:24 -0800257 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000258 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800259 success = initCallDag(root);
260
261 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
262 success = checkCallDepth();
263
264 // Checks which functions are used and if "main" exists
265 if (success)
266 {
267 functionMetadata.clear();
268 functionMetadata.resize(mCallDag.size());
269 success = tagUsedFunctions();
270 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000271
Corentin Walleza094a8a2015-04-07 11:53:06 -0700272 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
273 success = pruneUnusedFunctions(root);
274
Olli Etuahoc6833112015-04-22 15:15:54 +0300275 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
276 if (success)
277 PruneEmptyDeclarations(root);
278
Jamie Madill183bde52014-07-02 15:31:19 -0400279 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400280 success = validateOutputs(root);
281
Olli Etuaho3766a402015-06-18 15:47:46 +0300282 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000283 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000284
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000285 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000286 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000287
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000288 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
289 rewriteCSSShader(root);
290
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000291 // Unroll for-loop markup needs to happen after validateLimitations pass.
292 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800293 {
294 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800295 root->traverse(&marker);
296 }
297 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800298 {
299 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800300 root->traverse(&marker);
301 if (marker.samplerArrayIndexIsFloatLoopIndex())
302 {
303 infoSink.info.prefix(EPrefixError);
304 infoSink.info << "sampler array index is float loop index";
305 success = false;
306 }
307 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000308
zmo@google.com32e97312011-08-24 01:03:11 +0000309 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200310 if (success)
311 {
312 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
zmo@google.com32e97312011-08-24 01:03:11 +0000313 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200314 }
zmo@google.com32e97312011-08-24 01:03:11 +0000315
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000316 // Clamping uniform array bounds needs to happen after validateLimitations pass.
317 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
318 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
319
Jamie Madill183bde52014-07-02 15:31:19 -0400320 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800321 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400322
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800323 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
324 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700325 UnfoldShortCircuitAST unfoldShortCircuit;
326 root->traverse(&unfoldShortCircuit);
327 unfoldShortCircuit.updateTree();
328 }
329
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300330 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
331 {
332 RemovePow(root);
333 }
334
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300335 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800336 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400337 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800338 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
339 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000340 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800341 if (!success)
342 {
Jamie Madill075edd82013-07-08 13:30:19 -0400343 infoSink.info.prefix(EPrefixError);
344 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000345 }
346 }
Jamie Madill183bde52014-07-02 15:31:19 -0400347 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800348 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
349 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000350 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000351
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700352 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
353 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700354 ScalarizeVecAndMatConstructorArgs scalarizer(
355 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700356 root->traverse(&scalarizer);
357 }
358
Zhenyao Moe740add2014-07-18 17:01:01 -0700359 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
360 {
361 RegenerateStructNames gen(symbolTable, shaderVersion);
362 root->traverse(&gen);
363 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000364 }
365
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700366 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200367 if (success)
368 return root;
369
370 return NULL;
371}
372
373bool TCompiler::compile(const char* const shaderStrings[],
374 size_t numStrings, int compileOptions)
375{
376 if (numStrings == 0)
377 return true;
378
379 TScopedPoolAllocator scopedAlloc(&allocator);
380 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
381
382 if (root)
383 {
384 if (compileOptions & SH_INTERMEDIATE_TREE)
385 TIntermediate::outputTree(root, infoSink.info);
386
387 if (compileOptions & SH_OBJECT_CODE)
388 translate(root, compileOptions);
389
390 // The IntermNode tree doesn't need to be deleted here, since the
391 // memory will be freed in a big chunk by the PoolAllocator.
392 return true;
393 }
394 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000395}
396
Nicolas Capens49a88872013-06-20 09:54:03 -0400397bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000398{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000399 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400400 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000401
Nicolas Capens49a88872013-06-20 09:54:03 -0400402 assert(symbolTable.isEmpty());
403 symbolTable.push(); // COMMON_BUILTINS
404 symbolTable.push(); // ESSL1_BUILTINS
405 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000406
Nicolas Capens49a88872013-06-20 09:54:03 -0400407 TPublicType integer;
408 integer.type = EbtInt;
409 integer.primarySize = 1;
410 integer.secondarySize = 1;
411 integer.array = false;
412
413 TPublicType floatingPoint;
414 floatingPoint.type = EbtFloat;
415 floatingPoint.primarySize = 1;
416 floatingPoint.secondarySize = 1;
417 floatingPoint.array = false;
418
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400419 TPublicType sampler;
420 sampler.primarySize = 1;
421 sampler.secondarySize = 1;
422 sampler.array = false;
423
Nicolas Capens49a88872013-06-20 09:54:03 -0400424 switch(shaderType)
425 {
Jamie Madill183bde52014-07-02 15:31:19 -0400426 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400427 symbolTable.setDefaultPrecision(integer, EbpMedium);
428 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400429 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400430 symbolTable.setDefaultPrecision(integer, EbpHigh);
431 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
432 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800433 default:
434 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400435 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400436 // We set defaults for all the sampler types, even those that are
437 // only available if an extension exists.
438 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800439 samplerType < EbtGuardSamplerEnd; ++samplerType)
440 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400441 sampler.type = static_cast<TBasicType>(samplerType);
442 symbolTable.setDefaultPrecision(sampler, EbpLow);
443 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400444
Jamie Madill1b452142013-07-12 14:51:11 -0400445 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400446
447 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
448
449 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000450}
451
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400452void TCompiler::setResourceString()
453{
454 std::ostringstream strstream;
455 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
456 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
457 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
458 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
459 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
460 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
461 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
462 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
463 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
464 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
465 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
466 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
467 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
468 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
469 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300470 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400471 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
472 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100473 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
474 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
475 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400476 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
477 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
478 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300479 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300480 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200481 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
482 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400483
484 builtInResourcesString = strstream.str();
485}
486
alokp@chromium.org07620a52010-09-23 17:53:56 +0000487void TCompiler::clearResults()
488{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000489 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000490 infoSink.info.erase();
491 infoSink.obj.erase();
492 infoSink.debug.erase();
493
Jamie Madilled27c722014-07-02 15:31:23 -0400494 attributes.clear();
495 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000496 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400497 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400498 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400499 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000500
501 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000502
503 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200504
505 mSourcePath = NULL;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000506}
507
Corentin Wallez71d147f2015-02-11 11:15:24 -0800508bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000509{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800510 mCallDag.clear();
511
512 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800513 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800514 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800515 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800516 case CallDAG::INITDAG_RECURSION:
517 infoSink.info.prefix(EPrefixError);
518 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800519 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800520 case CallDAG::INITDAG_UNDEFINED:
521 infoSink.info.prefix(EPrefixError);
522 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800523 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800524 }
525
526 UNREACHABLE();
527 return true;
528}
529
530bool TCompiler::checkCallDepth()
531{
532 std::vector<int> depths(mCallDag.size());
533
534 for (size_t i = 0; i < mCallDag.size(); i++)
535 {
536 int depth = 0;
537 auto &record = mCallDag.getRecordFromIndex(i);
538
539 for (auto &calleeIndex : record.callees)
540 {
541 depth = std::max(depth, depths[calleeIndex] + 1);
542 }
543
544 depths[i] = depth;
545
546 if (depth >= maxCallStackDepth)
547 {
548 // Trace back the function chain to have a meaningful info log.
549 infoSink.info.prefix(EPrefixError);
550 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
551 << ") with the following call chain: " << record.name;
552
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700553 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800554 int currentDepth = depth;
555
556 while (currentFunction != -1)
557 {
558 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
559
560 int nextFunction = -1;
561 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
562 {
563 if (depths[calleeIndex] == currentDepth - 1)
564 {
565 currentDepth--;
566 nextFunction = calleeIndex;
567 }
568 }
569
570 currentFunction = nextFunction;
571 }
572
573 return false;
574 }
575 }
576
577 return true;
578}
579
580bool TCompiler::tagUsedFunctions()
581{
582 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700583 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800584 {
585 if (mCallDag.getRecordFromIndex(i).name == "main(")
586 {
587 internalTagUsedFunction(i);
588 return true;
589 }
590 }
591
592 infoSink.info.prefix(EPrefixError);
593 infoSink.info << "Missing main()";
594 return false;
595}
596
597void TCompiler::internalTagUsedFunction(size_t index)
598{
599 if (functionMetadata[index].used)
600 {
601 return;
602 }
603
604 functionMetadata[index].used = true;
605
606 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
607 {
608 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000609 }
610}
611
Corentin Walleza094a8a2015-04-07 11:53:06 -0700612// A predicate for the stl that returns if a top-level node is unused
613class TCompiler::UnusedPredicate
614{
615 public:
616 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
617 : mCallDag(callDag),
618 mMetadatas(metadatas)
619 {
620 }
621
622 bool operator ()(TIntermNode *node)
623 {
624 const TIntermAggregate *asAggregate = node->getAsAggregate();
625
626 if (asAggregate == nullptr)
627 {
628 return false;
629 }
630
631 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
632 {
633 return false;
634 }
635
636 size_t callDagIndex = mCallDag->findIndex(asAggregate);
637 if (callDagIndex == CallDAG::InvalidIndex)
638 {
639 // This happens only for unimplemented prototypes which are thus unused
640 ASSERT(asAggregate->getOp() == EOpPrototype);
641 return true;
642 }
643
644 ASSERT(callDagIndex < mMetadatas->size());
645 return !(*mMetadatas)[callDagIndex].used;
646 }
647
648 private:
649 const CallDAG *mCallDag;
650 const std::vector<FunctionMetadata> *mMetadatas;
651};
652
653bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
654{
655 TIntermAggregate *rootNode = root->getAsAggregate();
656 ASSERT(rootNode != nullptr);
657
658 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
659 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700660
661 if (!sequence->empty())
662 {
663 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
664 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700665
666 return true;
667}
668
Jamie Madill05a80ce2013-06-20 11:55:49 -0400669bool TCompiler::validateOutputs(TIntermNode* root)
670{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300671 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400672 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300673 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400674}
675
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000676void TCompiler::rewriteCSSShader(TIntermNode* root)
677{
678 RenameFunction renamer("main(", "css_main(");
679 root->traverse(&renamer);
680}
681
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800682bool TCompiler::validateLimitations(TIntermNode* root)
683{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000684 ValidateLimitations validate(shaderType, infoSink.info);
685 root->traverse(&validate);
686 return validate.numErrors() == 0;
687}
688
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000689bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000690{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800691 if (shaderSpec != SH_WEBGL_SPEC)
692 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000693 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
694 return false;
695 }
696
Jamie Madill183bde52014-07-02 15:31:19 -0400697 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800698 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000699 TDependencyGraph graph(root);
700
701 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000702 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500703
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000704 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800705 if (outputGraph)
706 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000707 TDependencyGraphOutput output(infoSink.info);
708 output.outputAllSpanningTrees(graph);
709 }
Jamie Madill5508f392014-02-20 13:31:36 -0500710
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000711 return success;
712 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800713 else
714 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000715 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000716 }
717}
718
Jamie Madilleb1a0102013-07-08 13:31:38 -0400719bool TCompiler::limitExpressionComplexity(TIntermNode* root)
720{
Jamie Madill6654bc92014-03-26 14:01:57 -0400721 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400722 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400723
724 if (traverser.getMaxDepth() > maxExpressionComplexity)
725 {
726 infoSink.info << "Expression too complex.";
727 return false;
728 }
729
Jamie Madilleb1a0102013-07-08 13:31:38 -0400730 TDependencyGraph graph(root);
731
732 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
733 iter != graph.endUserDefinedFunctionCalls();
734 ++iter)
735 {
736 TGraphFunctionCall* samplerSymbol = *iter;
737 TDependencyGraphTraverser graphTraverser;
738 samplerSymbol->traverse(&graphTraverser);
739 }
740
Jamie Madilleb1a0102013-07-08 13:31:38 -0400741 return true;
742}
743
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000744bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000745{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000746 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000747 restrictor.enforceRestrictions(graph);
748 return restrictor.numErrors() == 0;
749}
750
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000751bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000752{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000753 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000754 restrictor.enforceRestrictions(root);
755 return restrictor.numErrors() == 0;
756}
757
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400758void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000759{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400760 sh::CollectVariables collect(&attributes,
761 &outputVariables,
762 &uniforms,
763 &varyings,
764 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700765 hashFunction,
766 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000767 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400768
Zhenyao Mo409078f2014-10-28 13:23:18 -0700769 // This is for enforcePackingRestriction().
770 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000771}
zmo@google.comfd747b82011-04-23 01:30:07 +0000772
gman@chromium.org8d804792012-10-17 21:33:48 +0000773bool TCompiler::enforcePackingRestrictions()
774{
775 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400776 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000777}
778
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800779void TCompiler::initializeGLPosition(TIntermNode* root)
780{
781 InitializeVariables::InitVariableInfoList variables;
782 InitializeVariables::InitVariableInfo var(
783 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
784 variables.push_back(var);
785 InitializeVariables initializer(variables);
786 root->traverse(&initializer);
787}
788
789void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
790{
791 InitializeVariables::InitVariableInfoList variables;
792 for (size_t ii = 0; ii < varyings.size(); ++ii)
793 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400794 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800795 if (varying.staticUse)
796 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400797 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
798 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400799 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800800 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400801 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800802 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400803 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800804 name = name.substr(0, name.find_first_of('['));
805 }
806
807 InitializeVariables::InitVariableInfo var(name, type);
808 variables.push_back(var);
809 }
810 InitializeVariables initializer(variables);
811 root->traverse(&initializer);
812}
813
zmo@google.com5601ea02011-06-10 18:23:25 +0000814const TExtensionBehavior& TCompiler::getExtensionBehavior() const
815{
816 return extensionBehavior;
817}
zmo@google.com32e97312011-08-24 01:03:11 +0000818
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200819const char *TCompiler::getSourcePath() const
820{
821 return mSourcePath;
822}
823
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000824const ShBuiltInResources& TCompiler::getResources() const
825{
826 return compileResources;
827}
828
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000829const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
830{
831 return arrayBoundsClamper;
832}
833
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000834ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
835{
836 return clampingStrategy;
837}
838
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200839const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000840{
841 return builtInFunctionEmulator;
842}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700843
844void TCompiler::writePragma()
845{
846 TInfoSinkBase &sink = infoSink.obj;
847 if (mPragma.stdgl.invariantAll)
848 sink << "#pragma STDGL invariant(all)\n";
849}