blob: 18524ce569e15fecd8c4c9572a48c5718de15d3b [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
Ian Ewell924b7de2016-01-21 13:54:28 -0500325 // gl_Position is always written in compatibility output mode
326 if (success && shaderType == GL_VERTEX_SHADER &&
327 ((compileOptions & SH_INIT_GL_POSITION) ||
328 (outputType == SH_GLSL_COMPATIBILITY_OUTPUT)))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800329 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400330
Corentin Wallezd4b50542015-09-28 12:19:26 -0700331 // This pass might emit short circuits so keep it before the short circuit unfolding
332 if (success && (compileOptions & SH_REWRITE_DO_WHILE_LOOPS))
333 RewriteDoWhile(root, getTemporaryIndex());
334
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800335 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
336 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700337 UnfoldShortCircuitAST unfoldShortCircuit;
338 root->traverse(&unfoldShortCircuit);
339 unfoldShortCircuit.updateTree();
340 }
341
Olli Etuaho5c407bb2015-06-01 12:20:39 +0300342 if (success && (compileOptions & SH_REMOVE_POW_WITH_CONSTANT_EXPONENT))
343 {
344 RemovePow(root);
345 }
346
Olli Etuaho4dfe8092015-08-21 17:44:35 +0300347 if (success && shouldCollectVariables(compileOptions))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800348 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400349 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800350 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
351 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000352 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800353 if (!success)
354 {
Jamie Madill075edd82013-07-08 13:30:19 -0400355 infoSink.info.prefix(EPrefixError);
356 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000357 }
358 }
Jamie Madill183bde52014-07-02 15:31:19 -0400359 if (success && shaderType == GL_VERTEX_SHADER &&
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800360 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
361 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000362 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000363
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700364 if (success && (compileOptions & SH_SCALARIZE_VEC_AND_MAT_CONSTRUCTOR_ARGS))
365 {
Zhenyao Modaf56572014-08-06 16:18:30 -0700366 ScalarizeVecAndMatConstructorArgs scalarizer(
367 shaderType, fragmentPrecisionHigh);
Zhenyao Mocd68fe72014-07-11 10:45:44 -0700368 root->traverse(&scalarizer);
369 }
370
Zhenyao Moe740add2014-07-18 17:01:01 -0700371 if (success && (compileOptions & SH_REGENERATE_STRUCT_NAMES))
372 {
373 RegenerateStructNames gen(symbolTable, shaderVersion);
374 root->traverse(&gen);
375 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000376 }
377
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700378 SetGlobalParseContext(NULL);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200379 if (success)
380 return root;
381
382 return NULL;
383}
384
385bool TCompiler::compile(const char* const shaderStrings[],
386 size_t numStrings, int compileOptions)
387{
388 if (numStrings == 0)
389 return true;
390
391 TScopedPoolAllocator scopedAlloc(&allocator);
392 TIntermNode *root = compileTreeImpl(shaderStrings, numStrings, compileOptions);
393
394 if (root)
395 {
396 if (compileOptions & SH_INTERMEDIATE_TREE)
397 TIntermediate::outputTree(root, infoSink.info);
398
399 if (compileOptions & SH_OBJECT_CODE)
400 translate(root, compileOptions);
401
402 // The IntermNode tree doesn't need to be deleted here, since the
403 // memory will be freed in a big chunk by the PoolAllocator.
404 return true;
405 }
406 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000407}
408
Nicolas Capens49a88872013-06-20 09:54:03 -0400409bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000410{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000411 compileResources = resources;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400412 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000413
Nicolas Capens49a88872013-06-20 09:54:03 -0400414 assert(symbolTable.isEmpty());
415 symbolTable.push(); // COMMON_BUILTINS
416 symbolTable.push(); // ESSL1_BUILTINS
417 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000418
Nicolas Capens49a88872013-06-20 09:54:03 -0400419 TPublicType integer;
420 integer.type = EbtInt;
421 integer.primarySize = 1;
422 integer.secondarySize = 1;
423 integer.array = false;
424
425 TPublicType floatingPoint;
426 floatingPoint.type = EbtFloat;
427 floatingPoint.primarySize = 1;
428 floatingPoint.secondarySize = 1;
429 floatingPoint.array = false;
430
431 switch(shaderType)
432 {
Jamie Madill183bde52014-07-02 15:31:19 -0400433 case GL_FRAGMENT_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400434 symbolTable.setDefaultPrecision(integer, EbpMedium);
435 break;
Jamie Madill183bde52014-07-02 15:31:19 -0400436 case GL_VERTEX_SHADER:
Nicolas Capens49a88872013-06-20 09:54:03 -0400437 symbolTable.setDefaultPrecision(integer, EbpHigh);
438 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
439 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800440 default:
441 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400442 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200443 // Set defaults for sampler types that have default precision, even those that are
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400444 // only available if an extension exists.
Olli Etuaho183d7e22015-11-20 15:59:09 +0200445 // New sampler types in ESSL3 don't have default precision. ESSL1 types do.
446 initSamplerDefaultPrecision(EbtSampler2D);
447 initSamplerDefaultPrecision(EbtSamplerCube);
448 // SamplerExternalOES is specified in the extension to have default precision.
449 initSamplerDefaultPrecision(EbtSamplerExternalOES);
450 // It isn't specified whether Sampler2DRect has default precision.
451 initSamplerDefaultPrecision(EbtSampler2DRect);
Nicolas Capens49a88872013-06-20 09:54:03 -0400452
Jamie Madill1b452142013-07-12 14:51:11 -0400453 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400454
455 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
456
457 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000458}
459
Olli Etuaho183d7e22015-11-20 15:59:09 +0200460void TCompiler::initSamplerDefaultPrecision(TBasicType samplerType)
461{
462 ASSERT(samplerType > EbtGuardSamplerBegin && samplerType < EbtGuardSamplerEnd);
463 TPublicType sampler;
464 sampler.primarySize = 1;
465 sampler.secondarySize = 1;
466 sampler.array = false;
467 sampler.type = samplerType;
468 symbolTable.setDefaultPrecision(sampler, EbpLow);
469}
470
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400471void TCompiler::setResourceString()
472{
473 std::ostringstream strstream;
474 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
475 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
476 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
477 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
478 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
479 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
480 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
481 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
482 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
483 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
484 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
485 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
486 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
487 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
488 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300489 << ":EXT_blend_func_extended:" << compileResources.EXT_blend_func_extended
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400490 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
491 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
Erik Dahlströmea7a2122014-11-17 16:15:57 +0100492 << ":EXT_shader_framebuffer_fetch:" << compileResources.EXT_shader_framebuffer_fetch
493 << ":NV_shader_framebuffer_fetch:" << compileResources.NV_shader_framebuffer_fetch
494 << ":ARM_shader_framebuffer_fetch:" << compileResources.ARM_shader_framebuffer_fetch
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400495 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
496 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
497 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
Olli Etuahoe61209a2014-09-26 12:01:17 +0300498 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300499 << ":MaxDualSourceDrawBuffers:" << compileResources.MaxDualSourceDrawBuffers
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200500 << ":NV_draw_buffers:" << compileResources.NV_draw_buffers
501 << ":WEBGL_debug_shader_precision:" << compileResources.WEBGL_debug_shader_precision;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400502
503 builtInResourcesString = strstream.str();
504}
505
alokp@chromium.org07620a52010-09-23 17:53:56 +0000506void TCompiler::clearResults()
507{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000508 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000509 infoSink.info.erase();
510 infoSink.obj.erase();
511 infoSink.debug.erase();
512
Jamie Madilled27c722014-07-02 15:31:23 -0400513 attributes.clear();
514 outputVariables.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000515 uniforms.clear();
Jamie Madill23a8a432014-07-09 13:27:42 -0400516 expandedUniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400517 varyings.clear();
Jamie Madilled27c722014-07-02 15:31:23 -0400518 interfaceBlocks.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000519
520 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000521
522 nameMap.clear();
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200523
524 mSourcePath = NULL;
Corentin Wallezd4b50542015-09-28 12:19:26 -0700525 mTemporaryIndex = 0;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000526}
527
Corentin Wallez71d147f2015-02-11 11:15:24 -0800528bool TCompiler::initCallDag(TIntermNode *root)
zmo@google.comb1762df2011-07-30 02:04:23 +0000529{
Corentin Wallez71d147f2015-02-11 11:15:24 -0800530 mCallDag.clear();
531
532 switch (mCallDag.init(root, &infoSink.info))
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800533 {
Corentin Wallez71d147f2015-02-11 11:15:24 -0800534 case CallDAG::INITDAG_SUCCESS:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800535 return true;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800536 case CallDAG::INITDAG_RECURSION:
537 infoSink.info.prefix(EPrefixError);
538 infoSink.info << "Function recursion detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800539 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800540 case CallDAG::INITDAG_UNDEFINED:
541 infoSink.info.prefix(EPrefixError);
542 infoSink.info << "Unimplemented function detected";
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800543 return false;
Corentin Wallez71d147f2015-02-11 11:15:24 -0800544 }
545
546 UNREACHABLE();
547 return true;
548}
549
550bool TCompiler::checkCallDepth()
551{
552 std::vector<int> depths(mCallDag.size());
553
554 for (size_t i = 0; i < mCallDag.size(); i++)
555 {
556 int depth = 0;
557 auto &record = mCallDag.getRecordFromIndex(i);
558
559 for (auto &calleeIndex : record.callees)
560 {
561 depth = std::max(depth, depths[calleeIndex] + 1);
562 }
563
564 depths[i] = depth;
565
566 if (depth >= maxCallStackDepth)
567 {
568 // Trace back the function chain to have a meaningful info log.
569 infoSink.info.prefix(EPrefixError);
570 infoSink.info << "Call stack too deep (larger than " << maxCallStackDepth
571 << ") with the following call chain: " << record.name;
572
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700573 int currentFunction = static_cast<int>(i);
Corentin Wallez71d147f2015-02-11 11:15:24 -0800574 int currentDepth = depth;
575
576 while (currentFunction != -1)
577 {
578 infoSink.info << " -> " << mCallDag.getRecordFromIndex(currentFunction).name;
579
580 int nextFunction = -1;
581 for (auto& calleeIndex : mCallDag.getRecordFromIndex(currentFunction).callees)
582 {
583 if (depths[calleeIndex] == currentDepth - 1)
584 {
585 currentDepth--;
586 nextFunction = calleeIndex;
587 }
588 }
589
590 currentFunction = nextFunction;
591 }
592
593 return false;
594 }
595 }
596
597 return true;
598}
599
600bool TCompiler::tagUsedFunctions()
601{
602 // Search from main, starting from the end of the DAG as it usually is the root.
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700603 for (size_t i = mCallDag.size(); i-- > 0;)
Corentin Wallez71d147f2015-02-11 11:15:24 -0800604 {
605 if (mCallDag.getRecordFromIndex(i).name == "main(")
606 {
607 internalTagUsedFunction(i);
608 return true;
609 }
610 }
611
612 infoSink.info.prefix(EPrefixError);
Olli Etuaho792a41d2015-12-15 12:39:16 +0200613 infoSink.info << "Missing main()\n";
Corentin Wallez71d147f2015-02-11 11:15:24 -0800614 return false;
615}
616
617void TCompiler::internalTagUsedFunction(size_t index)
618{
619 if (functionMetadata[index].used)
620 {
621 return;
622 }
623
624 functionMetadata[index].used = true;
625
626 for (int calleeIndex : mCallDag.getRecordFromIndex(index).callees)
627 {
628 internalTagUsedFunction(calleeIndex);
zmo@google.comb1762df2011-07-30 02:04:23 +0000629 }
630}
631
Corentin Walleza094a8a2015-04-07 11:53:06 -0700632// A predicate for the stl that returns if a top-level node is unused
633class TCompiler::UnusedPredicate
634{
635 public:
636 UnusedPredicate(const CallDAG *callDag, const std::vector<FunctionMetadata> *metadatas)
637 : mCallDag(callDag),
638 mMetadatas(metadatas)
639 {
640 }
641
642 bool operator ()(TIntermNode *node)
643 {
644 const TIntermAggregate *asAggregate = node->getAsAggregate();
645
646 if (asAggregate == nullptr)
647 {
648 return false;
649 }
650
651 if (!(asAggregate->getOp() == EOpFunction || asAggregate->getOp() == EOpPrototype))
652 {
653 return false;
654 }
655
656 size_t callDagIndex = mCallDag->findIndex(asAggregate);
657 if (callDagIndex == CallDAG::InvalidIndex)
658 {
659 // This happens only for unimplemented prototypes which are thus unused
660 ASSERT(asAggregate->getOp() == EOpPrototype);
661 return true;
662 }
663
664 ASSERT(callDagIndex < mMetadatas->size());
665 return !(*mMetadatas)[callDagIndex].used;
666 }
667
668 private:
669 const CallDAG *mCallDag;
670 const std::vector<FunctionMetadata> *mMetadatas;
671};
672
673bool TCompiler::pruneUnusedFunctions(TIntermNode *root)
674{
675 TIntermAggregate *rootNode = root->getAsAggregate();
676 ASSERT(rootNode != nullptr);
677
678 UnusedPredicate isUnused(&mCallDag, &functionMetadata);
679 TIntermSequence *sequence = rootNode->getSequence();
Corentin Wallezb081e782015-07-20 05:40:04 -0700680
681 if (!sequence->empty())
682 {
683 sequence->erase(std::remove_if(sequence->begin(), sequence->end(), isUnused), sequence->end());
684 }
Corentin Walleza094a8a2015-04-07 11:53:06 -0700685
686 return true;
687}
688
Jamie Madill05a80ce2013-06-20 11:55:49 -0400689bool TCompiler::validateOutputs(TIntermNode* root)
690{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300691 ValidateOutputs validateOutputs(getExtensionBehavior(), compileResources.MaxDrawBuffers);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400692 root->traverse(&validateOutputs);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300693 return (validateOutputs.validateAndCountErrors(infoSink.info) == 0);
Jamie Madill05a80ce2013-06-20 11:55:49 -0400694}
695
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000696void TCompiler::rewriteCSSShader(TIntermNode* root)
697{
698 RenameFunction renamer("main(", "css_main(");
699 root->traverse(&renamer);
700}
701
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800702bool TCompiler::validateLimitations(TIntermNode* root)
703{
Olli Etuaho8a76dcc2015-12-10 20:25:12 +0200704 ValidateLimitations validate(shaderType, &infoSink.info);
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000705 root->traverse(&validate);
706 return validate.numErrors() == 0;
707}
708
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000709bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000710{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800711 if (shaderSpec != SH_WEBGL_SPEC)
712 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000713 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
714 return false;
715 }
716
Jamie Madill183bde52014-07-02 15:31:19 -0400717 if (shaderType == GL_FRAGMENT_SHADER)
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800718 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000719 TDependencyGraph graph(root);
720
721 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000722 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500723
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000724 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800725 if (outputGraph)
726 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000727 TDependencyGraphOutput output(infoSink.info);
728 output.outputAllSpanningTrees(graph);
729 }
Jamie Madill5508f392014-02-20 13:31:36 -0500730
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000731 return success;
732 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800733 else
734 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000735 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000736 }
737}
738
Jamie Madilleb1a0102013-07-08 13:31:38 -0400739bool TCompiler::limitExpressionComplexity(TIntermNode* root)
740{
Jamie Madill6654bc92014-03-26 14:01:57 -0400741 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400742 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400743
744 if (traverser.getMaxDepth() > maxExpressionComplexity)
745 {
746 infoSink.info << "Expression too complex.";
747 return false;
748 }
749
Jamie Madilleb1a0102013-07-08 13:31:38 -0400750 return true;
751}
752
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000753bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000754{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000755 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000756 restrictor.enforceRestrictions(graph);
757 return restrictor.numErrors() == 0;
758}
759
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000760bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000761{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000762 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000763 restrictor.enforceRestrictions(root);
764 return restrictor.numErrors() == 0;
765}
766
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400767void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000768{
Jamie Madilla2fbb842014-09-03 09:40:47 -0400769 sh::CollectVariables collect(&attributes,
770 &outputVariables,
771 &uniforms,
772 &varyings,
773 &interfaceBlocks,
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700774 hashFunction,
775 symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000776 root->traverse(&collect);
Jamie Madill23a8a432014-07-09 13:27:42 -0400777
Zhenyao Mo409078f2014-10-28 13:23:18 -0700778 // This is for enforcePackingRestriction().
779 sh::ExpandUniforms(uniforms, &expandedUniforms);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000780}
zmo@google.comfd747b82011-04-23 01:30:07 +0000781
gman@chromium.org8d804792012-10-17 21:33:48 +0000782bool TCompiler::enforcePackingRestrictions()
783{
784 VariablePacker packer;
Jamie Madill23a8a432014-07-09 13:27:42 -0400785 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, expandedUniforms);
gman@chromium.org8d804792012-10-17 21:33:48 +0000786}
787
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800788void TCompiler::initializeGLPosition(TIntermNode* root)
789{
790 InitializeVariables::InitVariableInfoList variables;
791 InitializeVariables::InitVariableInfo var(
792 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
793 variables.push_back(var);
794 InitializeVariables initializer(variables);
795 root->traverse(&initializer);
796}
797
798void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
799{
800 InitializeVariables::InitVariableInfoList variables;
801 for (size_t ii = 0; ii < varyings.size(); ++ii)
802 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400803 const sh::Varying& varying = varyings[ii];
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800804 if (varying.staticUse)
805 continue;
Jamie Madillaa72d782014-07-02 15:31:19 -0400806 unsigned char primarySize = static_cast<unsigned char>(gl::VariableColumnCount(varying.type));
807 unsigned char secondarySize = static_cast<unsigned char>(gl::VariableRowCount(varying.type));
Jamie Madilla718c1e2014-07-02 15:31:22 -0400808 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray());
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800809 TString name = varying.name.c_str();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400810 if (varying.isArray())
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800811 {
Jamie Madilla718c1e2014-07-02 15:31:22 -0400812 type.setArraySize(varying.arraySize);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800813 name = name.substr(0, name.find_first_of('['));
814 }
815
816 InitializeVariables::InitVariableInfo var(name, type);
817 variables.push_back(var);
818 }
819 InitializeVariables initializer(variables);
820 root->traverse(&initializer);
821}
822
zmo@google.com5601ea02011-06-10 18:23:25 +0000823const TExtensionBehavior& TCompiler::getExtensionBehavior() const
824{
825 return extensionBehavior;
826}
zmo@google.com32e97312011-08-24 01:03:11 +0000827
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200828const char *TCompiler::getSourcePath() const
829{
830 return mSourcePath;
831}
832
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000833const ShBuiltInResources& TCompiler::getResources() const
834{
835 return compileResources;
836}
837
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000838const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
839{
840 return arrayBoundsClamper;
841}
842
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000843ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
844{
845 return clampingStrategy;
846}
847
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200848const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000849{
850 return builtInFunctionEmulator;
851}
Zhenyao Mo94ac7b72014-10-15 18:22:08 -0700852
853void TCompiler::writePragma()
854{
855 TInfoSinkBase &sink = infoSink.obj;
856 if (mPragma.stdgl.invariantAll)
857 sink << "#pragma STDGL invariant(all)\n";
858}