blob: 1864cd8b6b5d4e49dd78d3f5d34238d0c40df713 [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
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/BuiltInFunctionEmulator.h"
8#include "compiler/translator/DetectCallDepth.h"
9#include "compiler/translator/ForLoopUnroll.h"
10#include "compiler/translator/Initialize.h"
Geoff Lang17732822013-08-29 13:46:49 -040011#include "compiler/translator/InitializeParseContext.h"
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080012#include "compiler/translator/InitializeVariables.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040013#include "compiler/translator/ParseContext.h"
Geoff Lang17732822013-08-29 13:46:49 -040014#include "compiler/translator/RenameFunction.h"
15#include "compiler/translator/ShHandle.h"
Zhenyao Mo7cab38b2013-10-15 12:59:30 -070016#include "compiler/translator/UnfoldShortCircuitAST.h"
Geoff Lang17732822013-08-29 13:46:49 -040017#include "compiler/translator/ValidateLimitations.h"
18#include "compiler/translator/ValidateOutputs.h"
19#include "compiler/translator/VariablePacker.h"
20#include "compiler/translator/depgraph/DependencyGraph.h"
21#include "compiler/translator/depgraph/DependencyGraphOutput.h"
22#include "compiler/translator/timing/RestrictFragmentShaderTiming.h"
23#include "compiler/translator/timing/RestrictVertexShaderTiming.h"
shannon.woods@transgaming.comda1ed362013-01-25 21:54:57 +000024#include "third_party/compiler/ArrayBoundsClamper.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000025
Jamie Madill5508f392014-02-20 13:31:36 -050026bool IsWebGLBasedSpec(ShShaderSpec spec)
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +000027{
28 return spec == SH_WEBGL_SPEC || spec == SH_CSS_SHADERS_SPEC;
29}
30
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070031size_t GetGlobalMaxTokenSize(ShShaderSpec spec)
Jamie Madill88f6e942014-02-19 10:27:53 -050032{
Jamie Madill88f6e942014-02-19 10:27:53 -050033 // WebGL defines a max token legnth of 256, while ES2 leaves max token
34 // size undefined. ES3 defines a max size of 1024 characters.
Zhenyao Mo7faf1a12014-04-25 18:03:56 -070035 if (IsWebGLBasedSpec(spec))
Jamie Madill88f6e942014-02-19 10:27:53 -050036 {
37 return 256;
38 }
39 else
40 {
41 return 1024;
42 }
43}
44
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000045namespace {
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080046class TScopedPoolAllocator
47{
48 public:
49 TScopedPoolAllocator(TPoolAllocator* allocator) : mAllocator(allocator)
50 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040051 mAllocator->push();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000052 SetGlobalPoolAllocator(mAllocator);
53 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080054 ~TScopedPoolAllocator()
55 {
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000056 SetGlobalPoolAllocator(NULL);
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040057 mAllocator->pop();
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000058 }
59
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080060 private:
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000061 TPoolAllocator* mAllocator;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040062};
63
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080064class TScopedSymbolTableLevel
65{
66 public:
67 TScopedSymbolTableLevel(TSymbolTable* table) : mTable(table)
68 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040069 ASSERT(mTable->atBuiltInLevel());
70 mTable->push();
71 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080072 ~TScopedSymbolTableLevel()
73 {
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040074 while (!mTable->atBuiltInLevel())
75 mTable->pop();
76 }
77
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080078 private:
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -040079 TSymbolTable* mTable;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000080};
81} // namespace
82
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080083TShHandleBase::TShHandleBase()
84{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000085 allocator.push();
86 SetGlobalPoolAllocator(&allocator);
87}
88
Zhenyao Mo4a667fe2014-02-11 12:35:01 -080089TShHandleBase::~TShHandleBase()
90{
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +000091 SetGlobalPoolAllocator(NULL);
92 allocator.popAll();
93}
94
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000095TCompiler::TCompiler(ShShaderType type, ShShaderSpec spec)
96 : shaderType(type),
zmo@google.comf420c422011-09-12 18:27:59 +000097 shaderSpec(spec),
Jamie Madilleb1a0102013-07-08 13:31:38 -040098 maxUniformVectors(0),
99 maxExpressionComplexity(0),
100 maxCallStackDepth(0),
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000101 fragmentPrecisionHigh(false),
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000102 clampingStrategy(SH_CLAMP_WITH_CLAMP_INTRINSIC),
zmo@google.com9996b8e2012-01-19 01:43:55 +0000103 builtInFunctionEmulator(type)
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000104{
105}
106
107TCompiler::~TCompiler()
108{
109}
110
111bool TCompiler::Init(const ShBuiltInResources& resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000112{
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000113 shaderVersion = 100;
gman@chromium.org8d804792012-10-17 21:33:48 +0000114 maxUniformVectors = (shaderType == SH_VERTEX_SHADER) ?
115 resources.MaxVertexUniformVectors :
116 resources.MaxFragmentUniformVectors;
Jamie Madilleb1a0102013-07-08 13:31:38 -0400117 maxExpressionComplexity = resources.MaxExpressionComplexity;
118 maxCallStackDepth = resources.MaxCallStackDepth;
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400119
120 SetGlobalPoolAllocator(&allocator);
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000121
alokp@chromium.org07620a52010-09-23 17:53:56 +0000122 // Generate built-in symbol table.
123 if (!InitBuiltInSymbolTable(resources))
124 return false;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000125 InitExtensionBehavior(resources, extensionBehavior);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000126 fragmentPrecisionHigh = resources.FragmentPrecisionHigh == 1;
alokp@chromium.orgbafcbaa2010-11-23 19:07:43 +0000127
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000128 arrayBoundsClamper.SetClampingStrategy(resources.ArrayIndexClampingStrategy);
129 clampingStrategy = resources.ArrayIndexClampingStrategy;
130
daniel@transgaming.comc23f4612012-11-28 19:42:57 +0000131 hashFunction = resources.HashFunction;
132
alokp@chromium.org07620a52010-09-23 17:53:56 +0000133 return true;
134}
135
136bool TCompiler::compile(const char* const shaderStrings[],
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000137 size_t numStrings,
alokp@chromium.org07620a52010-09-23 17:53:56 +0000138 int compileOptions)
139{
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400140 TScopedPoolAllocator scopedAlloc(&allocator);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000141 clearResults();
142
143 if (numStrings == 0)
144 return true;
145
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000146 // If compiling for WebGL, validate loop and indexing as well.
Jamie Madill5508f392014-02-20 13:31:36 -0500147 if (IsWebGLBasedSpec(shaderSpec))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000148 compileOptions |= SH_VALIDATE_LOOP_INDEXING;
alokp@chromium.org1f299542010-11-12 15:50:23 +0000149
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000150 // First string is path of source file if flag is set. The actual source follows.
151 const char* sourcePath = NULL;
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000152 size_t firstSource = 0;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000153 if (compileOptions & SH_SOURCE_PATH)
154 {
155 sourcePath = shaderStrings[0];
156 ++firstSource;
157 }
158
alokp@chromium.org07620a52010-09-23 17:53:56 +0000159 TIntermediate intermediate(infoSink);
160 TParseContext parseContext(symbolTable, extensionBehavior, intermediate,
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000161 shaderType, shaderSpec, compileOptions, true,
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000162 sourcePath, infoSink);
shannon.woods%transgaming.com@gtempaccount.comcbb6b6a2013-04-13 03:27:47 +0000163 parseContext.fragmentPrecisionHigh = fragmentPrecisionHigh;
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -0400164 SetGlobalParseContext(&parseContext);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000165
166 // We preserve symbols at the built-in level from compile-to-compile.
167 // Start pushing the user-defined symbols at global level.
Alok Priyadarshibc3f1ac2013-09-23 14:57:02 -0400168 TScopedSymbolTableLevel scopedSymbolLevel(&symbolTable);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000169
170 // Parse shader.
171 bool success =
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +0000172 (PaParseStrings(numStrings - firstSource, &shaderStrings[firstSource], NULL, &parseContext) == 0) &&
alokp@chromium.org07620a52010-09-23 17:53:56 +0000173 (parseContext.treeRoot != NULL);
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000174
shannon.woods%transgaming.com@gtempaccount.com5524db02013-04-13 03:38:16 +0000175 shaderVersion = parseContext.getShaderVersion();
shannon.woods%transgaming.com@gtempaccount.com0bbed382013-04-13 03:38:07 +0000176
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800177 if (success)
178 {
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000179 TIntermNode* root = parseContext.treeRoot;
180 success = intermediate.postProcess(root);
181
Jamie Madill6654bc92014-03-26 14:01:57 -0400182 // Disallow expressions deemed too complex.
183 if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY))
184 success = limitExpressionComplexity(root);
185
zmo@google.comb1762df2011-07-30 02:04:23 +0000186 if (success)
Jamie Madilleb1a0102013-07-08 13:31:38 -0400187 success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0);
zmo@google.comb1762df2011-07-30 02:04:23 +0000188
Jamie Madill05a80ce2013-06-20 11:55:49 -0400189 if (success && shaderVersion == 300 && shaderType == SH_FRAGMENT_SHADER)
190 success = validateOutputs(root);
191
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000192 if (success && (compileOptions & SH_VALIDATE_LOOP_INDEXING))
193 success = validateLimitations(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000194
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000195 if (success && (compileOptions & SH_TIMING_RESTRICTIONS))
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000196 success = enforceTimingRestrictions(root, (compileOptions & SH_DEPENDENCY_GRAPH) != 0);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000197
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000198 if (success && shaderSpec == SH_CSS_SHADERS_SPEC)
199 rewriteCSSShader(root);
200
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000201 // Unroll for-loop markup needs to happen after validateLimitations pass.
202 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800203 {
204 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kIntegerIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800205 root->traverse(&marker);
206 }
207 if (success && (compileOptions & SH_UNROLL_FOR_LOOP_WITH_SAMPLER_ARRAY_INDEX))
Zhenyao Mo3cdfcce2014-03-07 13:00:08 -0800208 {
209 ForLoopUnrollMarker marker(ForLoopUnrollMarker::kSamplerArrayIndex);
Zhenyao Mo550c6002014-02-26 15:40:48 -0800210 root->traverse(&marker);
211 if (marker.samplerArrayIndexIsFloatLoopIndex())
212 {
213 infoSink.info.prefix(EPrefixError);
214 infoSink.info << "sampler array index is float loop index";
215 success = false;
216 }
217 }
zmo@google.com0c6bb7a2011-08-17 19:39:58 +0000218
zmo@google.com32e97312011-08-24 01:03:11 +0000219 // Built-in function emulation needs to happen after validateLimitations pass.
220 if (success && (compileOptions & SH_EMULATE_BUILT_IN_FUNCTIONS))
221 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(root);
222
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000223 // Clamping uniform array bounds needs to happen after validateLimitations pass.
224 if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS))
225 arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root);
226
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800227 if (success && shaderType == SH_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION))
228 initializeGLPosition(root);
Zhenyao Moac44cd22013-09-23 14:57:09 -0400229
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800230 if (success && (compileOptions & SH_UNFOLD_SHORT_CIRCUIT))
231 {
Zhenyao Mo7cab38b2013-10-15 12:59:30 -0700232 UnfoldShortCircuitAST unfoldShortCircuit;
233 root->traverse(&unfoldShortCircuit);
234 unfoldShortCircuit.updateTree();
235 }
236
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800237 if (success && (compileOptions & SH_VARIABLES))
238 {
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400239 collectVariables(root);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800240 if (compileOptions & SH_ENFORCE_PACKING_RESTRICTIONS)
241 {
gman@chromium.org8d804792012-10-17 21:33:48 +0000242 success = enforcePackingRestrictions();
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800243 if (!success)
244 {
Jamie Madill075edd82013-07-08 13:30:19 -0400245 infoSink.info.prefix(EPrefixError);
246 infoSink.info << "too many uniforms";
gman@chromium.org8d804792012-10-17 21:33:48 +0000247 }
248 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800249 if (success && shaderType == SH_VERTEX_SHADER &&
250 (compileOptions & SH_INIT_VARYINGS_WITHOUT_STATIC_USE))
251 initializeVaryingsWithoutStaticUse(root);
gman@chromium.org8d804792012-10-17 21:33:48 +0000252 }
zmo@google.comfd747b82011-04-23 01:30:07 +0000253
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000254 if (success && (compileOptions & SH_INTERMEDIATE_TREE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000255 intermediate.outputTree(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000256
alokp@chromium.org4888ceb2010-10-01 21:13:12 +0000257 if (success && (compileOptions & SH_OBJECT_CODE))
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000258 translate(root);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000259 }
260
261 // Cleanup memory.
262 intermediate.remove(parseContext.treeRoot);
Zhenyao Mo7faf1a12014-04-25 18:03:56 -0700263 SetGlobalParseContext(NULL);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000264 return success;
265}
266
Nicolas Capens49a88872013-06-20 09:54:03 -0400267bool TCompiler::InitBuiltInSymbolTable(const ShBuiltInResources &resources)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000268{
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000269 compileResources = resources;
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000270
Nicolas Capens49a88872013-06-20 09:54:03 -0400271 assert(symbolTable.isEmpty());
272 symbolTable.push(); // COMMON_BUILTINS
273 symbolTable.push(); // ESSL1_BUILTINS
274 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000275
Nicolas Capens49a88872013-06-20 09:54:03 -0400276 TPublicType integer;
277 integer.type = EbtInt;
278 integer.primarySize = 1;
279 integer.secondarySize = 1;
280 integer.array = false;
281
282 TPublicType floatingPoint;
283 floatingPoint.type = EbtFloat;
284 floatingPoint.primarySize = 1;
285 floatingPoint.secondarySize = 1;
286 floatingPoint.array = false;
287
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400288 TPublicType sampler;
289 sampler.primarySize = 1;
290 sampler.secondarySize = 1;
291 sampler.array = false;
292
Nicolas Capens49a88872013-06-20 09:54:03 -0400293 switch(shaderType)
294 {
295 case SH_FRAGMENT_SHADER:
296 symbolTable.setDefaultPrecision(integer, EbpMedium);
297 break;
298 case SH_VERTEX_SHADER:
299 symbolTable.setDefaultPrecision(integer, EbpHigh);
300 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
301 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800302 default:
303 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400304 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400305 // We set defaults for all the sampler types, even those that are
306 // only available if an extension exists.
307 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800308 samplerType < EbtGuardSamplerEnd; ++samplerType)
309 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400310 sampler.type = static_cast<TBasicType>(samplerType);
311 symbolTable.setDefaultPrecision(sampler, EbpLow);
312 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400313
Jamie Madill1b452142013-07-12 14:51:11 -0400314 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400315
316 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
317
318 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000319}
320
321void TCompiler::clearResults()
322{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000323 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000324 infoSink.info.erase();
325 infoSink.obj.erase();
326 infoSink.debug.erase();
327
328 attribs.clear();
329 uniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400330 varyings.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000331
332 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000333
334 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000335}
336
Jamie Madilleb1a0102013-07-08 13:31:38 -0400337bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000338{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400339 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000340 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800341 switch (detect.detectCallDepth())
342 {
343 case DetectCallDepth::kErrorNone:
344 return true;
345 case DetectCallDepth::kErrorMissingMain:
346 infoSink.info.prefix(EPrefixError);
347 infoSink.info << "Missing main()";
348 return false;
349 case DetectCallDepth::kErrorRecursion:
350 infoSink.info.prefix(EPrefixError);
351 infoSink.info << "Function recursion detected";
352 return false;
353 case DetectCallDepth::kErrorMaxDepthExceeded:
354 infoSink.info.prefix(EPrefixError);
355 infoSink.info << "Function call stack too deep";
356 return false;
357 default:
358 UNREACHABLE();
359 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000360 }
361}
362
Jamie Madill05a80ce2013-06-20 11:55:49 -0400363bool TCompiler::validateOutputs(TIntermNode* root)
364{
365 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
366 root->traverse(&validateOutputs);
367 return (validateOutputs.numErrors() == 0);
368}
369
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000370void TCompiler::rewriteCSSShader(TIntermNode* root)
371{
372 RenameFunction renamer("main(", "css_main(");
373 root->traverse(&renamer);
374}
375
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800376bool TCompiler::validateLimitations(TIntermNode* root)
377{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000378 ValidateLimitations validate(shaderType, infoSink.info);
379 root->traverse(&validate);
380 return validate.numErrors() == 0;
381}
382
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000383bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000384{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800385 if (shaderSpec != SH_WEBGL_SPEC)
386 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000387 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
388 return false;
389 }
390
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800391 if (shaderType == SH_FRAGMENT_SHADER)
392 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000393 TDependencyGraph graph(root);
394
395 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000396 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500397
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000398 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800399 if (outputGraph)
400 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000401 TDependencyGraphOutput output(infoSink.info);
402 output.outputAllSpanningTrees(graph);
403 }
Jamie Madill5508f392014-02-20 13:31:36 -0500404
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000405 return success;
406 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800407 else
408 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000409 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000410 }
411}
412
Jamie Madilleb1a0102013-07-08 13:31:38 -0400413bool TCompiler::limitExpressionComplexity(TIntermNode* root)
414{
Jamie Madill6654bc92014-03-26 14:01:57 -0400415 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400416 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400417
418 if (traverser.getMaxDepth() > maxExpressionComplexity)
419 {
420 infoSink.info << "Expression too complex.";
421 return false;
422 }
423
Jamie Madilleb1a0102013-07-08 13:31:38 -0400424 TDependencyGraph graph(root);
425
426 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
427 iter != graph.endUserDefinedFunctionCalls();
428 ++iter)
429 {
430 TGraphFunctionCall* samplerSymbol = *iter;
431 TDependencyGraphTraverser graphTraverser;
432 samplerSymbol->traverse(&graphTraverser);
433 }
434
Jamie Madilleb1a0102013-07-08 13:31:38 -0400435 return true;
436}
437
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000438bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000439{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000440 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000441 restrictor.enforceRestrictions(graph);
442 return restrictor.numErrors() == 0;
443}
444
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000445bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000446{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000447 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000448 restrictor.enforceRestrictions(root);
449 return restrictor.numErrors() == 0;
450}
451
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400452void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000453{
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400454 CollectVariables collect(attribs, uniforms, varyings, hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000455 root->traverse(&collect);
456}
zmo@google.comfd747b82011-04-23 01:30:07 +0000457
gman@chromium.org8d804792012-10-17 21:33:48 +0000458bool TCompiler::enforcePackingRestrictions()
459{
460 VariablePacker packer;
461 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms);
462}
463
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800464void TCompiler::initializeGLPosition(TIntermNode* root)
465{
466 InitializeVariables::InitVariableInfoList variables;
467 InitializeVariables::InitVariableInfo var(
468 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
469 variables.push_back(var);
470 InitializeVariables initializer(variables);
471 root->traverse(&initializer);
472}
473
474void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
475{
476 InitializeVariables::InitVariableInfoList variables;
477 for (size_t ii = 0; ii < varyings.size(); ++ii)
478 {
479 const TVariableInfo& varying = varyings[ii];
480 if (varying.staticUse)
481 continue;
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700482 unsigned char primarySize = 1, secondarySize = 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800483 switch (varying.type)
484 {
485 case SH_FLOAT:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800486 break;
487 case SH_FLOAT_VEC2:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700488 primarySize = 2;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800489 break;
490 case SH_FLOAT_VEC3:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700491 primarySize = 3;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800492 break;
493 case SH_FLOAT_VEC4:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700494 primarySize = 4;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800495 break;
496 case SH_FLOAT_MAT2:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700497 primarySize = 2;
498 secondarySize = 2;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800499 break;
500 case SH_FLOAT_MAT3:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700501 primarySize = 3;
502 secondarySize = 3;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800503 break;
504 case SH_FLOAT_MAT4:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700505 primarySize = 4;
506 secondarySize = 4;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800507 break;
508 default:
509 ASSERT(false);
510 }
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700511 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800512 TString name = varying.name.c_str();
513 if (varying.isArray)
514 {
515 type.setArraySize(varying.size);
516 name = name.substr(0, name.find_first_of('['));
517 }
518
519 InitializeVariables::InitVariableInfo var(name, type);
520 variables.push_back(var);
521 }
522 InitializeVariables initializer(variables);
523 root->traverse(&initializer);
524}
525
zmo@google.com5601ea02011-06-10 18:23:25 +0000526const TExtensionBehavior& TCompiler::getExtensionBehavior() const
527{
528 return extensionBehavior;
529}
zmo@google.com32e97312011-08-24 01:03:11 +0000530
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000531const ShBuiltInResources& TCompiler::getResources() const
532{
533 return compileResources;
534}
535
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000536const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
537{
538 return arrayBoundsClamper;
539}
540
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000541ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
542{
543 return clampingStrategy;
544}
545
546const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
547{
548 return builtInFunctionEmulator;
549}