blob: 9707250da65c465bdff21009653f2365fe06607a [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
Jamie Madill88f6e942014-02-19 10:27:53 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org07620a52010-09-23 17:53:56 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Dmitry Skiba01971112015-07-10 14:54:00 -04007#include "compiler/translator/Cache.h"
Jamie Madilld4a3a312014-06-25 16:04:56 -04008#include "compiler/translator/Compiler.h"
Corentin Wallez71d147f2015-02-11 11:15:24 -08009#include "compiler/translator/CallDAG.h"
Geoff Lang17732822013-08-29 13:46:49 -040010#include "compiler/translator/ForLoopUnroll.h"
11#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040012#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080013#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040014#include "compiler/translator/ParseContext.h"
Olli Etuahoc6833112015-04-22 15:15:54 +030015#include "compiler/translator/PruneEmptyDeclarations.h"
Zhenyao Moe740add2014-07-18 17:01:01 -070016#include "compiler/translator/RegenerateStructNames.h"
Olli Etuaho5c407bb2015-06-01 12:20:39 +030017#include "compiler/translator/RemovePow.h"
Geoff Lang17732822013-08-29 13:46:49 -040018#include "compiler/translator/RenameFunction.h"
Corentin Wallezd4b50542015-09-28 12:19:26 -070019#include "compiler/translator/RewriteDoWhile.h"
Zhenyao Mocd68fe72014-07-11 10:45:44 -070020#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070021#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040022#include "compiler/translator/ValidateLimitations.h"
23#include "compiler/translator/ValidateOutputs.h"
24#include "compiler/translator/VariablePacker.h"
25#include "compiler/translator/depgraph/DependencyGraph.h"
26#include "compiler/translator/depgraph/DependencyGraphOutput.h"
27#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
28#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000029#include "third_party/compiler/ArrayBoundsClamper.h"
Jamie Madill183bde52014-07-02 15:31:19 -040030#include "angle_gl.h"
Jamie Madillaa72d782014-07-02 15:31:19 -040031#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000032
Jamie Madill5508f392014-02-20 13:31:36 -050033bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000034{
Zhenyao Modb9b40b2014-10-29 15:00:04 -070035 return (spec == SH_WEBGL_SPEC ||
36 spec == SH_CSS_SHADERS_SPEC ||
37 spec == SH_WEBGL2_SPEC);
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000038}
39
Qingqing Dengad0d0792015-04-08 14:25:06 -070040bool IsGLSL130OrNewer(ShShaderOutput output)
41{
42 return (output == SH_GLSL_130_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070043 output == SH_GLSL_140_OUTPUT ||
44 output == SH_GLSL_150_CORE_OUTPUT ||
45 output == SH_GLSL_330_CORE_OUTPUT ||
46 output == SH_GLSL_400_CORE_OUTPUT ||
Qingqing Dengad0d0792015-04-08 14:25:06 -070047 output == SH_GLSL_410_CORE_OUTPUT ||
Geoff Lang8273e002015-06-15 13:40:19 -070048 output == SH_GLSL_420_CORE_OUTPUT ||
49 output == SH_GLSL_430_CORE_OUTPUT ||
50 output == SH_GLSL_440_CORE_OUTPUT ||
51 output == SH_GLSL_450_CORE_OUTPUT);
Qingqing Dengad0d0792015-04-08 14:25:06 -070052}
53
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070054size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050055{
Jamie Madill88f6e942014-02-19 10:27:53 -050056 // WebGL defines a max token legnth of 256, while ES2 leaves max token
57 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Modb9b40b2014-10-29 15:00:04 -070058 switch (spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050059 {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070060 case SH_WEBGL_SPEC:
61 case SH_CSS_SHADERS_SPEC:
Jamie Madill88f6e942014-02-19 10:27:53 -050062 return 256;
Zhenyao Modb9b40b2014-10-29 15:00:04 -070063 default:
Jamie Madill88f6e942014-02-19 10:27:53 -050064 return 1024;
65 }
66}
67
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000068namespace {
Zhenyao Modb9b40b2014-10-29 15:00:04 -070069
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080070class TScopedPoolAllocator
71{
72 public:
73 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
74 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040075 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000076 SetGlobalPoolAllocator(mAllocator);
77 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080078 ~TScopedPoolAllocator()
79 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000080 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040081 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000082 }
83
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080084 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000085 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040086};
87
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080088class TScopedSymbolTableLevel
89{
90 public:
91 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
92 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040093 ASSERT(mTable->atBuiltInLevel());
94 mTable->push();
95 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080096 ~TScopedSymbolTableLevel()
97 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040098 while (!mTable->atBuiltInLevel())
99 mTable->pop();
100 }
101
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800102 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400103 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000104};
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700105
106int MapSpecToShaderVersion(ShShaderSpec spec)
107{
108 switch (spec)
109 {
110 case SH_GLES2_SPEC:
111 case SH_WEBGL_SPEC:
112 case SH_CSS_SHADERS_SPEC:
113 return 100;
114 case SH_GLES3_SPEC:
115 case SH_WEBGL2_SPEC:
116 return 300;
117 default:
118 UNREACHABLE();
119 return 0;
120 }
121}
122
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000123} // namespace
124
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800125TShHandleBase::TShHandleBase()
126{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000127 allocator.push();
128 SetGlobalPoolAllocator(&allocator);
129}
130
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800131TShHandleBase::~TShHandleBase()
132{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000133 SetGlobalPoolAllocator(NULL);
134 allocator.popAll();
135}
136
Jamie Madill183bde52014-07-02 15:31:19 -0400137TCompiler::TCompiler(sh::GLenum type, ShShaderSpec spec, ShShaderOutput output)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000138 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +0000139 shaderSpec(spec),
Jamie Madill68fe74a2014-05-27 12:56:01 -0400140 outputType(output),
Jamie Madilleb1a0102013-07-08 13:31:38 -0400141 maxUniformVectors(0),
142 maxExpressionComplexity(0),
143 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000144 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000145 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200146 builtInFunctionEmulator(),
Corentin Wallezd4b50542015-09-28 12:19:26 -0700147 mSourcePath(NULL),
148 mTemporaryIndex(0)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000149{
150}
151
152TCompiler::~TCompiler()
153{
154}
155
Olli Etuaho3766a402015-06-18 15:47:46 +0300156bool TCompiler::shouldRunLoopAndIndexingValidation(int compileOptions) const
157{
158 // If compiling an ESSL 1.00 shader for WebGL, or if its been requested through the API,
159 // validate loop and indexing as well (to verify that the shader only uses minimal functionality
160 // of ESSL 1.00 as in Appendix A of the spec).
161 return (IsWebGLBasedSpec(shaderSpec) && shaderVersion == 100) ||
162 (compileOptions & SH_VALIDATE_LOOP_INDEXING);
163}
164
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000165bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000166{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000167 shaderVersion = 100;
Jamie Madill183bde52014-07-02 15:31:19 -0400168 maxUniformVectors = (shaderType == GL_VERTEX_SHADER) ?
gman@chromium.org8d804792012-10-17 21:33:48 +0000169 resources.MaxVertexUniformVectors :
170 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400171 maxExpressionComplexity = resources.MaxExpressionComplexity;
172 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400173
174 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000175
alokp@chromium.org07620a52010-09-23 17:53:56 +0000176 // Generate built-in symbol table.
177 if (!InitBuiltInSymbolTable(resources))
178 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000179 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000180 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000181
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000182 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
183 clampingStrategy = resources.ArrayIndexClampingStrategy;
184
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000185 hashFunction = resources.HashFunction;
186
alokp@chromium.org07620a52010-09-23 17:53:56 +0000187 return true;
188}
189
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200190TIntermNode *TCompiler::compileTreeForTesting(const char* const shaderStrings[],
191 size_t numStrings, int compileOptions)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000192{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200193 return compileTreeImpl(shaderStrings, numStrings, compileOptions);
194}
195
Olli Etuahoa7b6db72015-08-19 14:26:30 +0300196TIntermNode *TCompiler::compileTreeImpl(const char *const shaderStrings[],
197 size_t numStrings,
198 const int compileOptions)
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200199{
alokp@chromium.org07620a52010-09-23 17:53:56 +0000200 clearResults();
201
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200202 ASSERT(numStrings > 0);
203 ASSERT(GetGlobalPoolAllocator());
alokp@chromium.org07620a52010-09-23 17:53:56 +0000204
David Yen0fbd1282015-02-02 14:46:09 -0800205 // Reset the extension behavior for each compilation unit.
206 ResetExtensionBehavior(extensionBehavior);
207
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000208 // First string is path of source file if flag is set. The actual source follows.
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000209 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000210 if (compileOptions & SH_SOURCE_PATH)
211 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200212 mSourcePath = shaderStrings[0];
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000213 ++firstSource;
214 }
215
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200216 bool debugShaderPrecision = getResources().WEBGL_debug_shader_precision == 1;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000217 TIntermediate intermediate(infoSink);
218 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000219 shaderType, shaderSpec, compileOptions, true,
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200220 infoSink, debugShaderPrecision);
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200221
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400222 parseContext.setFragmentPrecisionHigh(fragmentPrecisionHigh);
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400223 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000224
225 // We preserve symbols at the built-in level from compile-to-compile.
226 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400227 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000228
229 // Parse shader.
230 bool success =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400231 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], nullptr, &parseContext) == 0) &&
232 (parseContext.getTreeRoot() != nullptr);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000233
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000234 shaderVersion = parseContext.getShaderVersion();
Zhenyao Modb9b40b2014-10-29 15:00:04 -0700235 if (success && MapSpecToShaderVersion(shaderSpec) < shaderVersion)
236 {
237 infoSink.info.prefix(EPrefixError);
238 infoSink.info << "unsupported shader version";
239 success = false;
240 }
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000241
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400242 TIntermNode *root = nullptr;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200243
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800244 if (success)
245 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700246 mPragma = parseContext.pragma();
247 if (mPragma.stdgl.invariantAll)
248 {
249 symbolTable.setGlobalInvariant();
250 }
251
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400252 root = parseContext.getTreeRoot();
Olli Etuaho43613b02015-08-04 11:02:21 +0300253 root = intermediate.postProcess(root);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000254
Jamie Madill6654bc92014-03-26 14:01:57 -0400255 // Disallow expressions deemed too complex.
256 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
257 success = limitExpressionComplexity(root);
258
Corentin Wallez71d147f2015-02-11 11:15:24 -0800259 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000260 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800261 success = initCallDag(root);
262
263 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
264 success = checkCallDepth();
265
266 // Checks which functions are used and if "main" exists
267 if (success)
268 {
269 functionMetadata.clear();
270 functionMetadata.resize(mCallDag.size());
271 success = tagUsedFunctions();
272 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000273
Corentin Walleza094a8a2015-04-07 11:53:06 -0700274 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
275 success = pruneUnusedFunctions(root);
276
Olli Etuahoc6833112015-04-22 15:15:54 +0300277 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
278 if (success)
279 PruneEmptyDeclarations(root);
280
Jamie Madill183bde52014-07-02 15:31:19 -0400281 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400282 success = validateOutputs(root);
283
Olli Etuaho3766a402015-06-18 15:47:46 +0300284 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000285 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000286
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000287 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000288 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000289
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000290 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
291 rewriteCSSShader(root);
292
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000293 // Unroll for-loop markup needs to happen after validateLimitations pass.
294 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800295 {
296 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800297 root->traverse(&marker);
298 }
299 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800300 {
301 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800302 root->traverse(&marker);
303 if (marker.samplerArrayIndexIsFloatLoopIndex())
304 {
305 infoSink.info.prefix(EPrefixError);
306 infoSink.info << "sampler array index is float loop index";
307 success = false;
308 }
309 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000310
zmo@google.com32e97312011-08-24 01:03:11 +0000311 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200312 if (success)
313 {
314 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
zmo@google.com32e97312011-08-24 01:03:11 +0000315 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200316 }
zmo@google.com32e97312011-08-24 01:03:11 +0000317
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000318 // Clamping uniform array bounds needs to happen after validateLimitations pass.
319 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
320 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
321
Jamie Madill183bde52014-07-02 15:31:19 -0400322 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800323 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400324
Corentin Wallezd4b50542015-09-28 12:19:26 -0700325 // This pass might emit short circuits so keep it before the short circuit unfolding
326 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
327 RewriteDoWhile(root, getTemporaryIndex());
328
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800329 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
330 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700331 UnfoldShortCircuitAST unfoldShortCircuit;
332 root->traverse(&unfoldShortCircuit);
333 unfoldShortCircuit.updateTree();
334 }
335
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300336 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
337 {
338 RemovePow(root);
339 }
340
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300341 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800342 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400343 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800344 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
345 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000346 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800347 if (!success)
348 {
Jamie Madill075edd82013-07-08 13:30:19 -0400349 infoSink.info.prefix(EPrefixError);
350 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000351 }
352 }
Jamie Madill183bde52014-07-02 15:31:19 -0400353 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800354 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
355 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000356 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000357
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700358 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
359 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700360 ScalarizeVecAndMatConstructorArgs scalarizer(
361 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700362 root->traverse(&scalarizer);
363 }
364
Zhenyao Moe740add2014-07-18 17:01:01 -0700365 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
366 {
367 RegenerateStructNames gen(symbolTable, shaderVersion);
368 root->traverse(&gen);
369 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000370 }
371
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700372 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200373 if (success)
374 return root;
375
376 return NULL;
377}
378
379bool TCompiler::compile(const char* const shaderStrings[],
380 size_t numStrings, int compileOptions)
381{
382 if (numStrings == 0)
383 return true;
384
385 TScopedPoolAllocator scopedAlloc(&allocator);
386 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
387
388 if (root)
389 {
390 if (compileOptions & SH_INTERMEDIATE_TREE)
391 TIntermediate::outputTree(root, infoSink.info);
392
393 if (compileOptions & SH_OBJECT_CODE)
394 translate(root, compileOptions);
395
396 // The IntermNode tree doesn't need to be deleted here, since the
397 // memory will be freed in a big chunk by the PoolAllocator.
398 return true;
399 }
400 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000401}
402
Nicolas Capens49a88872013-06-20 09:54:03 -0400403bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000404{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000405 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400406 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000407
Nicolas Capens49a88872013-06-20 09:54:03 -0400408 assert(symbolTable.isEmpty());
409 symbolTable.push(); // COMMON_BUILTINS
410 symbolTable.push(); // ESSL1_BUILTINS
411 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000412
Nicolas Capens49a88872013-06-20 09:54:03 -0400413 TPublicType integer;
414 integer.type = EbtInt;
415 integer.primarySize = 1;
416 integer.secondarySize = 1;
417 integer.array = false;
418
419 TPublicType floatingPoint;
420 floatingPoint.type = EbtFloat;
421 floatingPoint.primarySize = 1;
422 floatingPoint.secondarySize = 1;
423 floatingPoint.array = false;
424
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400425 TPublicType sampler;
426 sampler.primarySize = 1;
427 sampler.secondarySize = 1;
428 sampler.array = false;
429
Nicolas Capens49a88872013-06-20 09:54:03 -0400430 switch(shaderType)
431 {
Jamie Madill183bde52014-07-02 15:31:19 -0400432 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400433 symbolTable.setDefaultPrecision(integer, EbpMedium);
434 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400435 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400436 symbolTable.setDefaultPrecision(integer, EbpHigh);
437 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
438 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800439 default:
440 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400441 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400442 // We set defaults for all the sampler types, even those that are
443 // only available if an extension exists.
444 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800445 samplerType < EbtGuardSamplerEnd; ++samplerType)
446 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400447 sampler.type = static_cast<TBasicType>(samplerType);
448 symbolTable.setDefaultPrecision(sampler, EbpLow);
449 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400450
Jamie Madill1b452142013-07-12 14:51:11 -0400451 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400452
453 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
454
455 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000456}
457
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400458void TCompiler::setResourceString()
459{
460 std::ostringstream strstream;
461 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
462 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
463 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
464 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
465 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
466 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
467 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
468 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
469 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
470 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
471 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
472 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
473 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
474 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
475 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300476 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400477 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
478 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100479 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
480 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
481 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400482 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
483 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
484 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300485 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300486 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200487 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
488 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400489
490 builtInResourcesString = strstream.str();
491}
492
alokp@chromium.org07620a52010-09-23 17:53:56 +0000493void TCompiler::clearResults()
494{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000495 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000496 infoSink.info.erase();
497 infoSink.obj.erase();
498 infoSink.debug.erase();
499
Jamie Madilled27c722014-07-02 15:31:23 -0400500 attributes.clear();
501 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000502 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400503 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400504 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400505 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000506
507 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000508
509 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200510
511 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700512 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000513}
514
Corentin Wallez71d147f2015-02-11 11:15:24 -0800515bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000516{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800517 mCallDag.clear();
518
519 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800520 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800521 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800522 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800523 case CallDAG::INITDAG_RECURSION:
524 infoSink.info.prefix(EPrefixError);
525 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800526 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800527 case CallDAG::INITDAG_UNDEFINED:
528 infoSink.info.prefix(EPrefixError);
529 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800530 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800531 }
532
533 UNREACHABLE();
534 return true;
535}
536
537bool TCompiler::checkCallDepth()
538{
539 std::vector<int> depths(mCallDag.size());
540
541 for (size_t i = 0; i < mCallDag.size(); i++)
542 {
543 int depth = 0;
544 auto &record = mCallDag.getRecordFromIndex(i);
545
546 for (auto &calleeIndex : record.callees)
547 {
548 depth = std::max(depth, depths[calleeIndex] + 1);
549 }
550
551 depths[i] = depth;
552
553 if (depth >= maxCallStackDepth)
554 {
555 // Trace back the function chain to have a meaningful info log.
556 infoSink.info.prefix(EPrefixError);
557 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
558 << ") with the following call chain: " << record.name;
559
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700560 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800561 int currentDepth = depth;
562
563 while (currentFunction != -1)
564 {
565 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
566
567 int nextFunction = -1;
568 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
569 {
570 if (depths[calleeIndex] == currentDepth - 1)
571 {
572 currentDepth--;
573 nextFunction = calleeIndex;
574 }
575 }
576
577 currentFunction = nextFunction;
578 }
579
580 return false;
581 }
582 }
583
584 return true;
585}
586
587bool TCompiler::tagUsedFunctions()
588{
589 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700590 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800591 {
592 if (mCallDag.getRecordFromIndex(i).name == "main(")
593 {
594 internalTagUsedFunction(i);
595 return true;
596 }
597 }
598
599 infoSink.info.prefix(EPrefixError);
600 infoSink.info << "Missing main()";
601 return false;
602}
603
604void TCompiler::internalTagUsedFunction(size_t index)
605{
606 if (functionMetadata[index].used)
607 {
608 return;
609 }
610
611 functionMetadata[index].used = true;
612
613 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
614 {
615 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000616 }
617}
618
Corentin Walleza094a8a2015-04-07 11:53:06 -0700619// A predicate for the stl that returns if a top-level node is unused
620class TCompiler::UnusedPredicate
621{
622 public:
623 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
624 : mCallDag(callDag),
625 mMetadatas(metadatas)
626 {
627 }
628
629 bool operator ()(TIntermNode *node)
630 {
631 const TIntermAggregate *asAggregate = node->getAsAggregate();
632
633 if (asAggregate == nullptr)
634 {
635 return false;
636 }
637
638 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
639 {
640 return false;
641 }
642
643 size_t callDagIndex = mCallDag->findIndex(asAggregate);
644 if (callDagIndex == CallDAG::InvalidIndex)
645 {
646 // This happens only for unimplemented prototypes which are thus unused
647 ASSERT(asAggregate->getOp() == EOpPrototype);
648 return true;
649 }
650
651 ASSERT(callDagIndex < mMetadatas->size());
652 return !(*mMetadatas)[callDagIndex].used;
653 }
654
655 private:
656 const CallDAG *mCallDag;
657 const std::vector<FunctionMetadata> *mMetadatas;
658};
659
660bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
661{
662 TIntermAggregate *rootNode = root->getAsAggregate();
663 ASSERT(rootNode != nullptr);
664
665 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
666 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700667
668 if (!sequence->empty())
669 {
670 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
671 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700672
673 return true;
674}
675
Jamie Madill05a80ce2013-06-20 11:55:49 -0400676bool TCompiler::validateOutputs(TIntermNode* root)
677{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300678 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400679 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300680 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400681}
682
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000683void TCompiler::rewriteCSSShader(TIntermNode* root)
684{
685 RenameFunction renamer("main(", "css_main(");
686 root->traverse(&renamer);
687}
688
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800689bool TCompiler::validateLimitations(TIntermNode* root)
690{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000691 ValidateLimitations validate(shaderType, infoSink.info);
692 root->traverse(&validate);
693 return validate.numErrors() == 0;
694}
695
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000696bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000697{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800698 if (shaderSpec != SH_WEBGL_SPEC)
699 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000700 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
701 return false;
702 }
703
Jamie Madill183bde52014-07-02 15:31:19 -0400704 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800705 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000706 TDependencyGraph graph(root);
707
708 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000709 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500710
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000711 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800712 if (outputGraph)
713 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000714 TDependencyGraphOutput output(infoSink.info);
715 output.outputAllSpanningTrees(graph);
716 }
Jamie Madill5508f392014-02-20 13:31:36 -0500717
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000718 return success;
719 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800720 else
721 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000722 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000723 }
724}
725
Jamie Madilleb1a0102013-07-08 13:31:38 -0400726bool TCompiler::limitExpressionComplexity(TIntermNode* root)
727{
Jamie Madill6654bc92014-03-26 14:01:57 -0400728 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400729 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400730
731 if (traverser.getMaxDepth() > maxExpressionComplexity)
732 {
733 infoSink.info << "Expression too complex.";
734 return false;
735 }
736
Jamie Madilleb1a0102013-07-08 13:31:38 -0400737 TDependencyGraph graph(root);
738
739 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
740 iter != graph.endUserDefinedFunctionCalls();
741 ++iter)
742 {
743 TGraphFunctionCall* samplerSymbol = *iter;
744 TDependencyGraphTraverser graphTraverser;
745 samplerSymbol->traverse(&graphTraverser);
746 }
747
Jamie Madilleb1a0102013-07-08 13:31:38 -0400748 return true;
749}
750
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000751bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000752{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000753 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000754 restrictor.enforceRestrictions(graph);
755 return restrictor.numErrors() == 0;
756}
757
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000758bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000759{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000760 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000761 restrictor.enforceRestrictions(root);
762 return restrictor.numErrors() == 0;
763}
764
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400765void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000766{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400767 sh::CollectVariables collect(&attributes,
768 &outputVariables,
769 &uniforms,
770 &varyings,
771 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700772 hashFunction,
773 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000774 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400775
Zhenyao Mo409078f2014-10-28 13:23:18 -0700776 // This is for enforcePackingRestriction().
777 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000778}
zmo@google.comfd747b82011-04-23 01:30:07 +0000779
gman@chromium.org8d804792012-10-17 21:33:48 +0000780bool TCompiler::enforcePackingRestrictions()
781{
782 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400783 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000784}
785
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800786void TCompiler::initializeGLPosition(TIntermNode* root)
787{
788 InitializeVariables::InitVariableInfoList variables;
789 InitializeVariables::InitVariableInfo var(
790 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
791 variables.push_back(var);
792 InitializeVariables initializer(variables);
793 root->traverse(&initializer);
794}
795
796void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
797{
798 InitializeVariables::InitVariableInfoList variables;
799 for (size_t ii = 0; ii < varyings.size(); ++ii)
800 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400801 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800802 if (varying.staticUse)
803 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400804 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
805 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400806 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800807 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400808 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800809 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400810 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800811 name = name.substr(0, name.find_first_of('['));
812 }
813
814 InitializeVariables::InitVariableInfo var(name, type);
815 variables.push_back(var);
816 }
817 InitializeVariables initializer(variables);
818 root->traverse(&initializer);
819}
820
zmo@google.com5601ea02011-06-10 18:23:25 +0000821const TExtensionBehavior& TCompiler::getExtensionBehavior() const
822{
823 return extensionBehavior;
824}
zmo@google.com32e97312011-08-24 01:03:11 +0000825
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200826const char *TCompiler::getSourcePath() const
827{
828 return mSourcePath;
829}
830
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000831const ShBuiltInResources& TCompiler::getResources() const
832{
833 return compileResources;
834}
835
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000836const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
837{
838 return arrayBoundsClamper;
839}
840
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000841ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
842{
843 return clampingStrategy;
844}
845
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200846const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000847{
848 return builtInFunctionEmulator;
849}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700850
851void TCompiler::writePragma()
852{
853 TInfoSinkBase &sink = infoSink.obj;
854 if (mPragma.stdgl.invariantAll)
855 sink << "#pragma STDGL invariant(all)\n";
856}