blob: 46dddc6bfe457718ae15142afcf75f38280f53d3 [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
Olli Etuahoa6996682015-10-12 14:32:30 +0300222 parseContext.setFragmentPrecisionHighOnESSL1(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
Olli Etuahoa6996682015-10-12 14:32:30 +0300255 // Highp might have been auto-enabled based on shader version
256 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
257
Jamie Madill6654bc92014-03-26 14:01:57 -0400258 // Disallow expressions deemed too complex.
259 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
260 success = limitExpressionComplexity(root);
261
Corentin Wallez71d147f2015-02-11 11:15:24 -0800262 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000263 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800264 success = initCallDag(root);
265
266 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
267 success = checkCallDepth();
268
269 // Checks which functions are used and if "main" exists
270 if (success)
271 {
272 functionMetadata.clear();
273 functionMetadata.resize(mCallDag.size());
274 success = tagUsedFunctions();
275 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000276
Corentin Walleza094a8a2015-04-07 11:53:06 -0700277 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
278 success = pruneUnusedFunctions(root);
279
Olli Etuahoc6833112015-04-22 15:15:54 +0300280 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
281 if (success)
282 PruneEmptyDeclarations(root);
283
Jamie Madill183bde52014-07-02 15:31:19 -0400284 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400285 success = validateOutputs(root);
286
Olli Etuaho3766a402015-06-18 15:47:46 +0300287 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000288 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000289
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000290 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000291 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000292
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000293 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
294 rewriteCSSShader(root);
295
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000296 // Unroll for-loop markup needs to happen after validateLimitations pass.
297 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800298 {
299 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800300 root->traverse(&marker);
301 }
302 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800303 {
304 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800305 root->traverse(&marker);
306 if (marker.samplerArrayIndexIsFloatLoopIndex())
307 {
308 infoSink.info.prefix(EPrefixError);
309 infoSink.info << "sampler array index is float loop index";
310 success = false;
311 }
312 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000313
zmo@google.com32e97312011-08-24 01:03:11 +0000314 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200315 if (success)
316 {
317 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
zmo@google.com32e97312011-08-24 01:03:11 +0000318 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200319 }
zmo@google.com32e97312011-08-24 01:03:11 +0000320
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000321 // Clamping uniform array bounds needs to happen after validateLimitations pass.
322 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
323 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
324
Jamie Madill183bde52014-07-02 15:31:19 -0400325 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800326 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400327
Corentin Wallezd4b50542015-09-28 12:19:26 -0700328 // This pass might emit short circuits so keep it before the short circuit unfolding
329 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
330 RewriteDoWhile(root, getTemporaryIndex());
331
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800332 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
333 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700334 UnfoldShortCircuitAST unfoldShortCircuit;
335 root->traverse(&unfoldShortCircuit);
336 unfoldShortCircuit.updateTree();
337 }
338
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300339 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
340 {
341 RemovePow(root);
342 }
343
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300344 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800345 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400346 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800347 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
348 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000349 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800350 if (!success)
351 {
Jamie Madill075edd82013-07-08 13:30:19 -0400352 infoSink.info.prefix(EPrefixError);
353 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000354 }
355 }
Jamie Madill183bde52014-07-02 15:31:19 -0400356 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800357 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
358 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000359 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000360
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700361 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
362 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700363 ScalarizeVecAndMatConstructorArgs scalarizer(
364 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700365 root->traverse(&scalarizer);
366 }
367
Zhenyao Moe740add2014-07-18 17:01:01 -0700368 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
369 {
370 RegenerateStructNames gen(symbolTable, shaderVersion);
371 root->traverse(&gen);
372 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000373 }
374
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700375 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200376 if (success)
377 return root;
378
379 return NULL;
380}
381
382bool TCompiler::compile(const char* const shaderStrings[],
383 size_t numStrings, int compileOptions)
384{
385 if (numStrings == 0)
386 return true;
387
388 TScopedPoolAllocator scopedAlloc(&allocator);
389 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
390
391 if (root)
392 {
393 if (compileOptions & SH_INTERMEDIATE_TREE)
394 TIntermediate::outputTree(root, infoSink.info);
395
396 if (compileOptions & SH_OBJECT_CODE)
397 translate(root, compileOptions);
398
399 // The IntermNode tree doesn't need to be deleted here, since the
400 // memory will be freed in a big chunk by the PoolAllocator.
401 return true;
402 }
403 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000404}
405
Nicolas Capens49a88872013-06-20 09:54:03 -0400406bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000407{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000408 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400409 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000410
Nicolas Capens49a88872013-06-20 09:54:03 -0400411 assert(symbolTable.isEmpty());
412 symbolTable.push(); // COMMON_BUILTINS
413 symbolTable.push(); // ESSL1_BUILTINS
414 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000415
Nicolas Capens49a88872013-06-20 09:54:03 -0400416 TPublicType integer;
417 integer.type = EbtInt;
418 integer.primarySize = 1;
419 integer.secondarySize = 1;
420 integer.array = false;
421
422 TPublicType floatingPoint;
423 floatingPoint.type = EbtFloat;
424 floatingPoint.primarySize = 1;
425 floatingPoint.secondarySize = 1;
426 floatingPoint.array = false;
427
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400428 TPublicType sampler;
429 sampler.primarySize = 1;
430 sampler.secondarySize = 1;
431 sampler.array = false;
432
Nicolas Capens49a88872013-06-20 09:54:03 -0400433 switch(shaderType)
434 {
Jamie Madill183bde52014-07-02 15:31:19 -0400435 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400436 symbolTable.setDefaultPrecision(integer, EbpMedium);
437 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400438 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400439 symbolTable.setDefaultPrecision(integer, EbpHigh);
440 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
441 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800442 default:
443 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400444 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400445 // We set defaults for all the sampler types, even those that are
446 // only available if an extension exists.
447 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800448 samplerType < EbtGuardSamplerEnd; ++samplerType)
449 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400450 sampler.type = static_cast<TBasicType>(samplerType);
451 symbolTable.setDefaultPrecision(sampler, EbpLow);
452 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400453
Jamie Madill1b452142013-07-12 14:51:11 -0400454 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400455
456 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
457
458 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000459}
460
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400461void TCompiler::setResourceString()
462{
463 std::ostringstream strstream;
464 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
465 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
466 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
467 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
468 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
469 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
470 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
471 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
472 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
473 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
474 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
475 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
476 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
477 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
478 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300479 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400480 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
481 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100482 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
483 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
484 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400485 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
486 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
487 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300488 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300489 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200490 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
491 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400492
493 builtInResourcesString = strstream.str();
494}
495
alokp@chromium.org07620a52010-09-23 17:53:56 +0000496void TCompiler::clearResults()
497{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000498 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000499 infoSink.info.erase();
500 infoSink.obj.erase();
501 infoSink.debug.erase();
502
Jamie Madilled27c722014-07-02 15:31:23 -0400503 attributes.clear();
504 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000505 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400506 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400507 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400508 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000509
510 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000511
512 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200513
514 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700515 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000516}
517
Corentin Wallez71d147f2015-02-11 11:15:24 -0800518bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000519{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800520 mCallDag.clear();
521
522 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800523 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800524 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800525 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800526 case CallDAG::INITDAG_RECURSION:
527 infoSink.info.prefix(EPrefixError);
528 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800529 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800530 case CallDAG::INITDAG_UNDEFINED:
531 infoSink.info.prefix(EPrefixError);
532 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800533 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800534 }
535
536 UNREACHABLE();
537 return true;
538}
539
540bool TCompiler::checkCallDepth()
541{
542 std::vector<int> depths(mCallDag.size());
543
544 for (size_t i = 0; i < mCallDag.size(); i++)
545 {
546 int depth = 0;
547 auto &record = mCallDag.getRecordFromIndex(i);
548
549 for (auto &calleeIndex : record.callees)
550 {
551 depth = std::max(depth, depths[calleeIndex] + 1);
552 }
553
554 depths[i] = depth;
555
556 if (depth >= maxCallStackDepth)
557 {
558 // Trace back the function chain to have a meaningful info log.
559 infoSink.info.prefix(EPrefixError);
560 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
561 << ") with the following call chain: " << record.name;
562
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700563 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800564 int currentDepth = depth;
565
566 while (currentFunction != -1)
567 {
568 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
569
570 int nextFunction = -1;
571 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
572 {
573 if (depths[calleeIndex] == currentDepth - 1)
574 {
575 currentDepth--;
576 nextFunction = calleeIndex;
577 }
578 }
579
580 currentFunction = nextFunction;
581 }
582
583 return false;
584 }
585 }
586
587 return true;
588}
589
590bool TCompiler::tagUsedFunctions()
591{
592 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700593 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800594 {
595 if (mCallDag.getRecordFromIndex(i).name == "main(")
596 {
597 internalTagUsedFunction(i);
598 return true;
599 }
600 }
601
602 infoSink.info.prefix(EPrefixError);
603 infoSink.info << "Missing main()";
604 return false;
605}
606
607void TCompiler::internalTagUsedFunction(size_t index)
608{
609 if (functionMetadata[index].used)
610 {
611 return;
612 }
613
614 functionMetadata[index].used = true;
615
616 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
617 {
618 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000619 }
620}
621
Corentin Walleza094a8a2015-04-07 11:53:06 -0700622// A predicate for the stl that returns if a top-level node is unused
623class TCompiler::UnusedPredicate
624{
625 public:
626 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
627 : mCallDag(callDag),
628 mMetadatas(metadatas)
629 {
630 }
631
632 bool operator ()(TIntermNode *node)
633 {
634 const TIntermAggregate *asAggregate = node->getAsAggregate();
635
636 if (asAggregate == nullptr)
637 {
638 return false;
639 }
640
641 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
642 {
643 return false;
644 }
645
646 size_t callDagIndex = mCallDag->findIndex(asAggregate);
647 if (callDagIndex == CallDAG::InvalidIndex)
648 {
649 // This happens only for unimplemented prototypes which are thus unused
650 ASSERT(asAggregate->getOp() == EOpPrototype);
651 return true;
652 }
653
654 ASSERT(callDagIndex < mMetadatas->size());
655 return !(*mMetadatas)[callDagIndex].used;
656 }
657
658 private:
659 const CallDAG *mCallDag;
660 const std::vector<FunctionMetadata> *mMetadatas;
661};
662
663bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
664{
665 TIntermAggregate *rootNode = root->getAsAggregate();
666 ASSERT(rootNode != nullptr);
667
668 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
669 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700670
671 if (!sequence->empty())
672 {
673 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
674 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700675
676 return true;
677}
678
Jamie Madill05a80ce2013-06-20 11:55:49 -0400679bool TCompiler::validateOutputs(TIntermNode* root)
680{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300681 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400682 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300683 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400684}
685
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000686void TCompiler::rewriteCSSShader(TIntermNode* root)
687{
688 RenameFunction renamer("main(", "css_main(");
689 root->traverse(&renamer);
690}
691
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800692bool TCompiler::validateLimitations(TIntermNode* root)
693{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000694 ValidateLimitations validate(shaderType, infoSink.info);
695 root->traverse(&validate);
696 return validate.numErrors() == 0;
697}
698
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000699bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000700{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800701 if (shaderSpec != SH_WEBGL_SPEC)
702 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000703 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
704 return false;
705 }
706
Jamie Madill183bde52014-07-02 15:31:19 -0400707 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800708 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000709 TDependencyGraph graph(root);
710
711 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000712 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500713
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000714 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800715 if (outputGraph)
716 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000717 TDependencyGraphOutput output(infoSink.info);
718 output.outputAllSpanningTrees(graph);
719 }
Jamie Madill5508f392014-02-20 13:31:36 -0500720
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000721 return success;
722 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800723 else
724 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000725 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000726 }
727}
728
Jamie Madilleb1a0102013-07-08 13:31:38 -0400729bool TCompiler::limitExpressionComplexity(TIntermNode* root)
730{
Jamie Madill6654bc92014-03-26 14:01:57 -0400731 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400732 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400733
734 if (traverser.getMaxDepth() > maxExpressionComplexity)
735 {
736 infoSink.info << "Expression too complex.";
737 return false;
738 }
739
Jamie Madilleb1a0102013-07-08 13:31:38 -0400740 TDependencyGraph graph(root);
741
742 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
743 iter != graph.endUserDefinedFunctionCalls();
744 ++iter)
745 {
746 TGraphFunctionCall* samplerSymbol = *iter;
747 TDependencyGraphTraverser graphTraverser;
748 samplerSymbol->traverse(&graphTraverser);
749 }
750
Jamie Madilleb1a0102013-07-08 13:31:38 -0400751 return true;
752}
753
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000754bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000755{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000756 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000757 restrictor.enforceRestrictions(graph);
758 return restrictor.numErrors() == 0;
759}
760
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000761bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000762{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000763 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000764 restrictor.enforceRestrictions(root);
765 return restrictor.numErrors() == 0;
766}
767
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400768void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000769{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400770 sh::CollectVariables collect(&attributes,
771 &outputVariables,
772 &uniforms,
773 &varyings,
774 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700775 hashFunction,
776 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000777 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400778
Zhenyao Mo409078f2014-10-28 13:23:18 -0700779 // This is for enforcePackingRestriction().
780 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000781}
zmo@google.comfd747b82011-04-23 01:30:07 +0000782
gman@chromium.org8d804792012-10-17 21:33:48 +0000783bool TCompiler::enforcePackingRestrictions()
784{
785 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400786 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000787}
788
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800789void TCompiler::initializeGLPosition(TIntermNode* root)
790{
791 InitializeVariables::InitVariableInfoList variables;
792 InitializeVariables::InitVariableInfo var(
793 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
794 variables.push_back(var);
795 InitializeVariables initializer(variables);
796 root->traverse(&initializer);
797}
798
799void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
800{
801 InitializeVariables::InitVariableInfoList variables;
802 for (size_t ii = 0; ii < varyings.size(); ++ii)
803 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400804 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800805 if (varying.staticUse)
806 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400807 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
808 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400809 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800810 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400811 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800812 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400813 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800814 name = name.substr(0, name.find_first_of('['));
815 }
816
817 InitializeVariables::InitVariableInfo var(name, type);
818 variables.push_back(var);
819 }
820 InitializeVariables initializer(variables);
821 root->traverse(&initializer);
822}
823
zmo@google.com5601ea02011-06-10 18:23:25 +0000824const TExtensionBehavior& TCompiler::getExtensionBehavior() const
825{
826 return extensionBehavior;
827}
zmo@google.com32e97312011-08-24 01:03:11 +0000828
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200829const char *TCompiler::getSourcePath() const
830{
831 return mSourcePath;
832}
833
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000834const ShBuiltInResources& TCompiler::getResources() const
835{
836 return compileResources;
837}
838
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000839const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
840{
841 return arrayBoundsClamper;
842}
843
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000844ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
845{
846 return clampingStrategy;
847}
848
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200849const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000850{
851 return builtInFunctionEmulator;
852}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700853
854void TCompiler::writePragma()
855{
856 TInfoSinkBase &sink = infoSink.obj;
857 if (mPragma.stdgl.invariantAll)
858 sink << "#pragma STDGL invariant(all)\n";
859}