blob: 2f2be16218f8e9601277aaa2248672a06ab90ec3 [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 Etuaho5d91dda2015-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
alokp@chromium.org07620a52010-09-23 17:53:56 +0000216 TIntermediate intermediate(infoSink);
Olli Etuahoe1a94c62015-11-16 17:35:25 +0200217 TParseContext parseContext(symbolTable, extensionBehavior, intermediate, shaderType, shaderSpec,
218 compileOptions, true, infoSink, getResources());
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200219
Olli Etuahoa6996682015-10-12 14:32:30 +0300220 parseContext.setFragmentPrecisionHighOnESSL1(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
Olli Etuahoa6996682015-10-12 14:32:30 +0300253 // Highp might have been auto-enabled based on shader version
254 fragmentPrecisionHigh = parseContext.getFragmentPrecisionHigh();
255
Jamie Madill6654bc92014-03-26 14:01:57 -0400256 // Disallow expressions deemed too complex.
257 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
258 success = limitExpressionComplexity(root);
259
Corentin Wallez71d147f2015-02-11 11:15:24 -0800260 // Create the function DAG and check there is no recursion
zmo@google.comb1762df2011-07-30 02:04:23 +0000261 if (success)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800262 success = initCallDag(root);
263
264 if (success && (compileOptions & SH_LIMIT_CALL_STACK_DEPTH))
265 success = checkCallDepth();
266
267 // Checks which functions are used and if "main" exists
268 if (success)
269 {
270 functionMetadata.clear();
271 functionMetadata.resize(mCallDag.size());
272 success = tagUsedFunctions();
273 }
zmo@google.comb1762df2011-07-30 02:04:23 +0000274
Corentin Walleza094a8a2015-04-07 11:53:06 -0700275 if (success && !(compileOptions & SH_DONT_PRUNE_UNUSED_FUNCTIONS))
276 success = pruneUnusedFunctions(root);
277
Olli Etuahoc6833112015-04-22 15:15:54 +0300278 // Prune empty declarations to work around driver bugs and to keep declaration output simple.
279 if (success)
280 PruneEmptyDeclarations(root);
281
Jamie Madill183bde52014-07-02 15:31:19 -0400282 if (success && shaderVersion == 300 && shaderType == GL_FRAGMENT_SHADER)
Jamie Madill05a80ce2013-06-20 11:55:49 -0400283 success = validateOutputs(root);
284
Olli Etuaho5d91dda2015-06-18 15:47:46 +0300285 if (success && shouldRunLoopAndIndexingValidation(compileOptions))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000286 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000287
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000288 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000289 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000290
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000291 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
292 rewriteCSSShader(root);
293
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000294 // Unroll for-loop markup needs to happen after validateLimitations pass.
295 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800296 {
297 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800298 root->traverse(&marker);
299 }
300 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800301 {
302 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800303 root->traverse(&marker);
304 if (marker.samplerArrayIndexIsFloatLoopIndex())
305 {
306 infoSink.info.prefix(EPrefixError);
307 infoSink.info << "sampler array index is float loop index";
308 success = false;
309 }
310 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000311
zmo@google.com32e97312011-08-24 01:03:11 +0000312 // Built-in function emulation needs to happen after validateLimitations pass.
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200313 if (success)
314 {
315 initBuiltInFunctionEmulator(&builtInFunctionEmulator, compileOptions);
zmo@google.com32e97312011-08-24 01:03:11 +0000316 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200317 }
zmo@google.com32e97312011-08-24 01:03:11 +0000318
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000319 // Clamping uniform array bounds needs to happen after validateLimitations pass.
320 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
321 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
322
Jamie Madill183bde52014-07-02 15:31:19 -0400323 if (success && shaderType == GL_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800324 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400325
Corentin Wallezd4b50542015-09-28 12:19:26 -0700326 // This pass might emit short circuits so keep it before the short circuit unfolding
327 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
328 RewriteDoWhile(root, getTemporaryIndex());
329
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800330 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
331 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700332 UnfoldShortCircuitAST unfoldShortCircuit;
333 root->traverse(&unfoldShortCircuit);
334 unfoldShortCircuit.updateTree();
335 }
336
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300337 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
338 {
339 RemovePow(root);
340 }
341
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300342 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800343 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400344 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800345 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
346 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000347 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800348 if (!success)
349 {
Jamie Madill075edd82013-07-08 13:30:19 -0400350 infoSink.info.prefix(EPrefixError);
351 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000352 }
353 }
Jamie Madill183bde52014-07-02 15:31:19 -0400354 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800355 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
356 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000357 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000358
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700359 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
360 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700361 ScalarizeVecAndMatConstructorArgs scalarizer(
362 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700363 root->traverse(&scalarizer);
364 }
365
Zhenyao Moe740add2014-07-18 17:01:01 -0700366 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
367 {
368 RegenerateStructNames gen(symbolTable, shaderVersion);
369 root->traverse(&gen);
370 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000371 }
372
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700373 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200374 if (success)
375 return root;
376
377 return NULL;
378}
379
380bool TCompiler::compile(const char* const shaderStrings[],
381 size_t numStrings, int compileOptions)
382{
383 if (numStrings == 0)
384 return true;
385
386 TScopedPoolAllocator scopedAlloc(&allocator);
387 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
388
389 if (root)
390 {
391 if (compileOptions & SH_INTERMEDIATE_TREE)
392 TIntermediate::outputTree(root, infoSink.info);
393
394 if (compileOptions & SH_OBJECT_CODE)
395 translate(root, compileOptions);
396
397 // The IntermNode tree doesn't need to be deleted here, since the
398 // memory will be freed in a big chunk by the PoolAllocator.
399 return true;
400 }
401 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000402}
403
Nicolas Capens49a88872013-06-20 09:54:03 -0400404bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000405{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000406 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400407 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000408
Nicolas Capens49a88872013-06-20 09:54:03 -0400409 assert(symbolTable.isEmpty());
410 symbolTable.push(); // COMMON_BUILTINS
411 symbolTable.push(); // ESSL1_BUILTINS
412 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000413
Nicolas Capens49a88872013-06-20 09:54:03 -0400414 TPublicType integer;
415 integer.type = EbtInt;
416 integer.primarySize = 1;
417 integer.secondarySize = 1;
418 integer.array = false;
419
420 TPublicType floatingPoint;
421 floatingPoint.type = EbtFloat;
422 floatingPoint.primarySize = 1;
423 floatingPoint.secondarySize = 1;
424 floatingPoint.array = false;
425
426 switch(shaderType)
427 {
Jamie Madill183bde52014-07-02 15:31:19 -0400428 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400429 symbolTable.setDefaultPrecision(integer, EbpMedium);
430 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400431 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400432 symbolTable.setDefaultPrecision(integer, EbpHigh);
433 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
434 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800435 default:
436 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400437 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200438 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400439 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200440 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
441 initSamplerDefaultPrecision(EbtSampler2D);
442 initSamplerDefaultPrecision(EbtSamplerCube);
443 // SamplerExternalOES is specified in the extension to have default precision.
444 initSamplerDefaultPrecision(EbtSamplerExternalOES);
445 // It isn't specified whether Sampler2DRect has default precision.
446 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400447
Jamie Madill1b452142013-07-12 14:51:11 -0400448 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400449
450 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
451
452 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000453}
454
Olli Etuaho183d7e22015-11-20 15:59:09 +0200455void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
456{
457 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
458 TPublicType sampler;
459 sampler.primarySize = 1;
460 sampler.secondarySize = 1;
461 sampler.array = false;
462 sampler.type = samplerType;
463 symbolTable.setDefaultPrecision(sampler, EbpLow);
464}
465
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400466void TCompiler::setResourceString()
467{
468 std::ostringstream strstream;
469 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
470 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
471 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
472 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
473 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
474 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
475 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
476 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
477 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
478 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
479 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
480 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
481 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
482 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
483 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300484 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400485 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
486 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100487 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
488 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
489 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400490 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
491 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
492 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300493 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300494 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200495 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
496 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400497
498 builtInResourcesString = strstream.str();
499}
500
alokp@chromium.org07620a52010-09-23 17:53:56 +0000501void TCompiler::clearResults()
502{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000503 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000504 infoSink.info.erase();
505 infoSink.obj.erase();
506 infoSink.debug.erase();
507
Jamie Madilled27c722014-07-02 15:31:23 -0400508 attributes.clear();
509 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000510 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400511 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400512 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400513 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000514
515 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000516
517 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200518
519 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700520 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000521}
522
Corentin Wallez71d147f2015-02-11 11:15:24 -0800523bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000524{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800525 mCallDag.clear();
526
527 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800528 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800529 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800530 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800531 case CallDAG::INITDAG_RECURSION:
532 infoSink.info.prefix(EPrefixError);
533 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800534 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800535 case CallDAG::INITDAG_UNDEFINED:
536 infoSink.info.prefix(EPrefixError);
537 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800538 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800539 }
540
541 UNREACHABLE();
542 return true;
543}
544
545bool TCompiler::checkCallDepth()
546{
547 std::vector<int> depths(mCallDag.size());
548
549 for (size_t i = 0; i < mCallDag.size(); i++)
550 {
551 int depth = 0;
552 auto &record = mCallDag.getRecordFromIndex(i);
553
554 for (auto &calleeIndex : record.callees)
555 {
556 depth = std::max(depth, depths[calleeIndex] + 1);
557 }
558
559 depths[i] = depth;
560
561 if (depth >= maxCallStackDepth)
562 {
563 // Trace back the function chain to have a meaningful info log.
564 infoSink.info.prefix(EPrefixError);
565 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
566 << ") with the following call chain: " << record.name;
567
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700568 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800569 int currentDepth = depth;
570
571 while (currentFunction != -1)
572 {
573 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
574
575 int nextFunction = -1;
576 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
577 {
578 if (depths[calleeIndex] == currentDepth - 1)
579 {
580 currentDepth--;
581 nextFunction = calleeIndex;
582 }
583 }
584
585 currentFunction = nextFunction;
586 }
587
588 return false;
589 }
590 }
591
592 return true;
593}
594
595bool TCompiler::tagUsedFunctions()
596{
597 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700598 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800599 {
600 if (mCallDag.getRecordFromIndex(i).name == "main(")
601 {
602 internalTagUsedFunction(i);
603 return true;
604 }
605 }
606
607 infoSink.info.prefix(EPrefixError);
608 infoSink.info << "Missing main()";
609 return false;
610}
611
612void TCompiler::internalTagUsedFunction(size_t index)
613{
614 if (functionMetadata[index].used)
615 {
616 return;
617 }
618
619 functionMetadata[index].used = true;
620
621 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
622 {
623 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000624 }
625}
626
Corentin Walleza094a8a2015-04-07 11:53:06 -0700627// A predicate for the stl that returns if a top-level node is unused
628class TCompiler::UnusedPredicate
629{
630 public:
631 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
632 : mCallDag(callDag),
633 mMetadatas(metadatas)
634 {
635 }
636
637 bool operator ()(TIntermNode *node)
638 {
639 const TIntermAggregate *asAggregate = node->getAsAggregate();
640
641 if (asAggregate == nullptr)
642 {
643 return false;
644 }
645
646 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
647 {
648 return false;
649 }
650
651 size_t callDagIndex = mCallDag->findIndex(asAggregate);
652 if (callDagIndex == CallDAG::InvalidIndex)
653 {
654 // This happens only for unimplemented prototypes which are thus unused
655 ASSERT(asAggregate->getOp() == EOpPrototype);
656 return true;
657 }
658
659 ASSERT(callDagIndex < mMetadatas->size());
660 return !(*mMetadatas)[callDagIndex].used;
661 }
662
663 private:
664 const CallDAG *mCallDag;
665 const std::vector<FunctionMetadata> *mMetadatas;
666};
667
668bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
669{
670 TIntermAggregate *rootNode = root->getAsAggregate();
671 ASSERT(rootNode != nullptr);
672
673 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
674 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700675
676 if (!sequence->empty())
677 {
678 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
679 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700680
681 return true;
682}
683
Jamie Madill05a80ce2013-06-20 11:55:49 -0400684bool TCompiler::validateOutputs(TIntermNode* root)
685{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300686 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400687 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300688 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400689}
690
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000691void TCompiler::rewriteCSSShader(TIntermNode* root)
692{
693 RenameFunction renamer("main(", "css_main(");
694 root->traverse(&renamer);
695}
696
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800697bool TCompiler::validateLimitations(TIntermNode* root)
698{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000699 ValidateLimitations validate(shaderType, infoSink.info);
700 root->traverse(&validate);
701 return validate.numErrors() == 0;
702}
703
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000704bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000705{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800706 if (shaderSpec != SH_WEBGL_SPEC)
707 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000708 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
709 return false;
710 }
711
Jamie Madill183bde52014-07-02 15:31:19 -0400712 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800713 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000714 TDependencyGraph graph(root);
715
716 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000717 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500718
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000719 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800720 if (outputGraph)
721 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000722 TDependencyGraphOutput output(infoSink.info);
723 output.outputAllSpanningTrees(graph);
724 }
Jamie Madill5508f392014-02-20 13:31:36 -0500725
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000726 return success;
727 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800728 else
729 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000730 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000731 }
732}
733
Jamie Madilleb1a0102013-07-08 13:31:38 -0400734bool TCompiler::limitExpressionComplexity(TIntermNode* root)
735{
Jamie Madill6654bc92014-03-26 14:01:57 -0400736 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400737 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400738
739 if (traverser.getMaxDepth() > maxExpressionComplexity)
740 {
741 infoSink.info << "Expression too complex.";
742 return false;
743 }
744
Jamie Madilleb1a0102013-07-08 13:31:38 -0400745 TDependencyGraph graph(root);
746
747 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
748 iter != graph.endUserDefinedFunctionCalls();
749 ++iter)
750 {
751 TGraphFunctionCall* samplerSymbol = *iter;
752 TDependencyGraphTraverser graphTraverser;
753 samplerSymbol->traverse(&graphTraverser);
754 }
755
Jamie Madilleb1a0102013-07-08 13:31:38 -0400756 return true;
757}
758
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000759bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000760{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000761 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000762 restrictor.enforceRestrictions(graph);
763 return restrictor.numErrors() == 0;
764}
765
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000766bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000767{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000768 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000769 restrictor.enforceRestrictions(root);
770 return restrictor.numErrors() == 0;
771}
772
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400773void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000774{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400775 sh::CollectVariables collect(&attributes,
776 &outputVariables,
777 &uniforms,
778 &varyings,
779 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700780 hashFunction,
781 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000782 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400783
Zhenyao Mo409078f2014-10-28 13:23:18 -0700784 // This is for enforcePackingRestriction().
785 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000786}
zmo@google.comfd747b82011-04-23 01:30:07 +0000787
gman@chromium.org8d804792012-10-17 21:33:48 +0000788bool TCompiler::enforcePackingRestrictions()
789{
790 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400791 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000792}
793
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800794void TCompiler::initializeGLPosition(TIntermNode* root)
795{
796 InitializeVariables::InitVariableInfoList variables;
797 InitializeVariables::InitVariableInfo var(
798 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
799 variables.push_back(var);
800 InitializeVariables initializer(variables);
801 root->traverse(&initializer);
802}
803
804void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
805{
806 InitializeVariables::InitVariableInfoList variables;
807 for (size_t ii = 0; ii < varyings.size(); ++ii)
808 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400809 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800810 if (varying.staticUse)
811 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400812 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
813 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400814 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800815 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400816 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800817 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400818 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800819 name = name.substr(0, name.find_first_of('['));
820 }
821
822 InitializeVariables::InitVariableInfo var(name, type);
823 variables.push_back(var);
824 }
825 InitializeVariables initializer(variables);
826 root->traverse(&initializer);
827}
828
zmo@google.com5601ea02011-06-10 18:23:25 +0000829const TExtensionBehavior& TCompiler::getExtensionBehavior() const
830{
831 return extensionBehavior;
832}
zmo@google.com32e97312011-08-24 01:03:11 +0000833
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200834const char *TCompiler::getSourcePath() const
835{
836 return mSourcePath;
837}
838
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000839const ShBuiltInResources& TCompiler::getResources() const
840{
841 return compileResources;
842}
843
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000844const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
845{
846 return arrayBoundsClamper;
847}
848
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000849ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
850{
851 return clampingStrategy;
852}
853
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200854const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000855{
856 return builtInFunctionEmulator;
857}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700858
859void TCompiler::writePragma()
860{
861 TInfoSinkBase &sink = infoSink.obj;
862 if (mPragma.stdgl.invariantAll)
863 sink << "#pragma STDGL invariant(all)\n";
864}