blob: a48e0b48f7b221501b91fa45833e0a03b5baffba [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 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200297 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex,
298 shouldRunLoopAndIndexingValidation(compileOptions));
Zhenyao Mo550c6002014-02-26 15:40:48 -0800299 root->traverse(&marker);
300 }
301 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800302 {
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200303 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex,
304 shouldRunLoopAndIndexingValidation(compileOptions));
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
428 switch(shaderType)
429 {
Jamie Madill183bde52014-07-02 15:31:19 -0400430 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400431 symbolTable.setDefaultPrecision(integer, EbpMedium);
432 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400433 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400434 symbolTable.setDefaultPrecision(integer, EbpHigh);
435 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
436 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800437 default:
438 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400439 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200440 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400441 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200442 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
443 initSamplerDefaultPrecision(EbtSampler2D);
444 initSamplerDefaultPrecision(EbtSamplerCube);
445 // SamplerExternalOES is specified in the extension to have default precision.
446 initSamplerDefaultPrecision(EbtSamplerExternalOES);
447 // It isn't specified whether Sampler2DRect has default precision.
448 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400449
Jamie Madill1b452142013-07-12 14:51:11 -0400450 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400451
452 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
453
454 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000455}
456
Olli Etuaho183d7e22015-11-20 15:59:09 +0200457void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
458{
459 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
460 TPublicType sampler;
461 sampler.primarySize = 1;
462 sampler.secondarySize = 1;
463 sampler.array = false;
464 sampler.type = samplerType;
465 symbolTable.setDefaultPrecision(sampler, EbpLow);
466}
467
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400468void TCompiler::setResourceString()
469{
470 std::ostringstream strstream;
471 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
472 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
473 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
474 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
475 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
476 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
477 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
478 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
479 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
480 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
481 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
482 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
483 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
484 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
485 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300486 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400487 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
488 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100489 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
490 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
491 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400492 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
493 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
494 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300495 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300496 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200497 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
498 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400499
500 builtInResourcesString = strstream.str();
501}
502
alokp@chromium.org07620a52010-09-23 17:53:56 +0000503void TCompiler::clearResults()
504{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000505 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000506 infoSink.info.erase();
507 infoSink.obj.erase();
508 infoSink.debug.erase();
509
Jamie Madilled27c722014-07-02 15:31:23 -0400510 attributes.clear();
511 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000512 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400513 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400514 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400515 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000516
517 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000518
519 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200520
521 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700522 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000523}
524
Corentin Wallez71d147f2015-02-11 11:15:24 -0800525bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000526{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800527 mCallDag.clear();
528
529 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800530 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800531 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800532 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800533 case CallDAG::INITDAG_RECURSION:
534 infoSink.info.prefix(EPrefixError);
535 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800536 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800537 case CallDAG::INITDAG_UNDEFINED:
538 infoSink.info.prefix(EPrefixError);
539 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800540 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800541 }
542
543 UNREACHABLE();
544 return true;
545}
546
547bool TCompiler::checkCallDepth()
548{
549 std::vector<int> depths(mCallDag.size());
550
551 for (size_t i = 0; i < mCallDag.size(); i++)
552 {
553 int depth = 0;
554 auto &record = mCallDag.getRecordFromIndex(i);
555
556 for (auto &calleeIndex : record.callees)
557 {
558 depth = std::max(depth, depths[calleeIndex] + 1);
559 }
560
561 depths[i] = depth;
562
563 if (depth >= maxCallStackDepth)
564 {
565 // Trace back the function chain to have a meaningful info log.
566 infoSink.info.prefix(EPrefixError);
567 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
568 << ") with the following call chain: " << record.name;
569
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700570 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800571 int currentDepth = depth;
572
573 while (currentFunction != -1)
574 {
575 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
576
577 int nextFunction = -1;
578 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
579 {
580 if (depths[calleeIndex] == currentDepth - 1)
581 {
582 currentDepth--;
583 nextFunction = calleeIndex;
584 }
585 }
586
587 currentFunction = nextFunction;
588 }
589
590 return false;
591 }
592 }
593
594 return true;
595}
596
597bool TCompiler::tagUsedFunctions()
598{
599 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700600 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800601 {
602 if (mCallDag.getRecordFromIndex(i).name == "main(")
603 {
604 internalTagUsedFunction(i);
605 return true;
606 }
607 }
608
609 infoSink.info.prefix(EPrefixError);
610 infoSink.info << "Missing main()";
611 return false;
612}
613
614void TCompiler::internalTagUsedFunction(size_t index)
615{
616 if (functionMetadata[index].used)
617 {
618 return;
619 }
620
621 functionMetadata[index].used = true;
622
623 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
624 {
625 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000626 }
627}
628
Corentin Walleza094a8a2015-04-07 11:53:06 -0700629// A predicate for the stl that returns if a top-level node is unused
630class TCompiler::UnusedPredicate
631{
632 public:
633 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
634 : mCallDag(callDag),
635 mMetadatas(metadatas)
636 {
637 }
638
639 bool operator ()(TIntermNode *node)
640 {
641 const TIntermAggregate *asAggregate = node->getAsAggregate();
642
643 if (asAggregate == nullptr)
644 {
645 return false;
646 }
647
648 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
649 {
650 return false;
651 }
652
653 size_t callDagIndex = mCallDag->findIndex(asAggregate);
654 if (callDagIndex == CallDAG::InvalidIndex)
655 {
656 // This happens only for unimplemented prototypes which are thus unused
657 ASSERT(asAggregate->getOp() == EOpPrototype);
658 return true;
659 }
660
661 ASSERT(callDagIndex < mMetadatas->size());
662 return !(*mMetadatas)[callDagIndex].used;
663 }
664
665 private:
666 const CallDAG *mCallDag;
667 const std::vector<FunctionMetadata> *mMetadatas;
668};
669
670bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
671{
672 TIntermAggregate *rootNode = root->getAsAggregate();
673 ASSERT(rootNode != nullptr);
674
675 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
676 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700677
678 if (!sequence->empty())
679 {
680 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
681 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700682
683 return true;
684}
685
Jamie Madill05a80ce2013-06-20 11:55:49 -0400686bool TCompiler::validateOutputs(TIntermNode* root)
687{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300688 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400689 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300690 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400691}
692
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000693void TCompiler::rewriteCSSShader(TIntermNode* root)
694{
695 RenameFunction renamer("main(", "css_main(");
696 root->traverse(&renamer);
697}
698
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800699bool TCompiler::validateLimitations(TIntermNode* root)
700{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200701 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000702 root->traverse(&validate);
703 return validate.numErrors() == 0;
704}
705
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000706bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000707{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800708 if (shaderSpec != SH_WEBGL_SPEC)
709 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000710 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
711 return false;
712 }
713
Jamie Madill183bde52014-07-02 15:31:19 -0400714 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800715 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000716 TDependencyGraph graph(root);
717
718 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000719 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500720
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000721 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800722 if (outputGraph)
723 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000724 TDependencyGraphOutput output(infoSink.info);
725 output.outputAllSpanningTrees(graph);
726 }
Jamie Madill5508f392014-02-20 13:31:36 -0500727
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000728 return success;
729 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800730 else
731 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000732 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000733 }
734}
735
Jamie Madilleb1a0102013-07-08 13:31:38 -0400736bool TCompiler::limitExpressionComplexity(TIntermNode* root)
737{
Jamie Madill6654bc92014-03-26 14:01:57 -0400738 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400739 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400740
741 if (traverser.getMaxDepth() > maxExpressionComplexity)
742 {
743 infoSink.info << "Expression too complex.";
744 return false;
745 }
746
Jamie Madilleb1a0102013-07-08 13:31:38 -0400747 TDependencyGraph graph(root);
748
749 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
750 iter != graph.endUserDefinedFunctionCalls();
751 ++iter)
752 {
753 TGraphFunctionCall* samplerSymbol = *iter;
754 TDependencyGraphTraverser graphTraverser;
755 samplerSymbol->traverse(&graphTraverser);
756 }
757
Jamie Madilleb1a0102013-07-08 13:31:38 -0400758 return true;
759}
760
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000761bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000762{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000763 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000764 restrictor.enforceRestrictions(graph);
765 return restrictor.numErrors() == 0;
766}
767
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000768bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000769{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000770 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000771 restrictor.enforceRestrictions(root);
772 return restrictor.numErrors() == 0;
773}
774
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400775void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000776{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400777 sh::CollectVariables collect(&attributes,
778 &outputVariables,
779 &uniforms,
780 &varyings,
781 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700782 hashFunction,
783 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000784 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400785
Zhenyao Mo409078f2014-10-28 13:23:18 -0700786 // This is for enforcePackingRestriction().
787 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000788}
zmo@google.comfd747b82011-04-23 01:30:07 +0000789
gman@chromium.org8d804792012-10-17 21:33:48 +0000790bool TCompiler::enforcePackingRestrictions()
791{
792 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400793 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000794}
795
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800796void TCompiler::initializeGLPosition(TIntermNode* root)
797{
798 InitializeVariables::InitVariableInfoList variables;
799 InitializeVariables::InitVariableInfo var(
800 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
801 variables.push_back(var);
802 InitializeVariables initializer(variables);
803 root->traverse(&initializer);
804}
805
806void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
807{
808 InitializeVariables::InitVariableInfoList variables;
809 for (size_t ii = 0; ii < varyings.size(); ++ii)
810 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400811 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800812 if (varying.staticUse)
813 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400814 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
815 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400816 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800817 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400818 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800819 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400820 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800821 name = name.substr(0, name.find_first_of('['));
822 }
823
824 InitializeVariables::InitVariableInfo var(name, type);
825 variables.push_back(var);
826 }
827 InitializeVariables initializer(variables);
828 root->traverse(&initializer);
829}
830
zmo@google.com5601ea02011-06-10 18:23:25 +0000831const TExtensionBehavior& TCompiler::getExtensionBehavior() const
832{
833 return extensionBehavior;
834}
zmo@google.com32e97312011-08-24 01:03:11 +0000835
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200836const char *TCompiler::getSourcePath() const
837{
838 return mSourcePath;
839}
840
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000841const ShBuiltInResources& TCompiler::getResources() const
842{
843 return compileResources;
844}
845
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000846const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
847{
848 return arrayBoundsClamper;
849}
850
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000851ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
852{
853 return clampingStrategy;
854}
855
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200856const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000857{
858 return builtInFunctionEmulator;
859}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700860
861void TCompiler::writePragma()
862{
863 TInfoSinkBase &sink = infoSink.obj;
864 if (mPragma.stdgl.invariantAll)
865 sink << "#pragma STDGL invariant(all)\n";
866}