blob: b15be1a92d31703f25e84132c24463dd3f075635 [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;
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400270 setResourceString();
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000271
Nicolas Capens49a88872013-06-20 09:54:03 -0400272 assert(symbolTable.isEmpty());
273 symbolTable.push(); // COMMON_BUILTINS
274 symbolTable.push(); // ESSL1_BUILTINS
275 symbolTable.push(); // ESSL3_BUILTINS
shannonwoods@chromium.org2ac0be92013-05-30 00:02:27 +0000276
Nicolas Capens49a88872013-06-20 09:54:03 -0400277 TPublicType integer;
278 integer.type = EbtInt;
279 integer.primarySize = 1;
280 integer.secondarySize = 1;
281 integer.array = false;
282
283 TPublicType floatingPoint;
284 floatingPoint.type = EbtFloat;
285 floatingPoint.primarySize = 1;
286 floatingPoint.secondarySize = 1;
287 floatingPoint.array = false;
288
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400289 TPublicType sampler;
290 sampler.primarySize = 1;
291 sampler.secondarySize = 1;
292 sampler.array = false;
293
Nicolas Capens49a88872013-06-20 09:54:03 -0400294 switch(shaderType)
295 {
296 case SH_FRAGMENT_SHADER:
297 symbolTable.setDefaultPrecision(integer, EbpMedium);
298 break;
299 case SH_VERTEX_SHADER:
300 symbolTable.setDefaultPrecision(integer, EbpHigh);
301 symbolTable.setDefaultPrecision(floatingPoint, EbpHigh);
302 break;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800303 default:
304 assert(false && "Language not supported");
Nicolas Capens49a88872013-06-20 09:54:03 -0400305 }
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400306 // We set defaults for all the sampler types, even those that are
307 // only available if an extension exists.
308 for (int samplerType = EbtGuardSamplerBegin + 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800309 samplerType < EbtGuardSamplerEnd; ++samplerType)
310 {
Zhenyao Moa5a1dfc2013-09-23 14:57:03 -0400311 sampler.type = static_cast<TBasicType>(samplerType);
312 symbolTable.setDefaultPrecision(sampler, EbpLow);
313 }
Nicolas Capens49a88872013-06-20 09:54:03 -0400314
Jamie Madill1b452142013-07-12 14:51:11 -0400315 InsertBuiltInFunctions(shaderType, shaderSpec, resources, symbolTable);
Nicolas Capens49a88872013-06-20 09:54:03 -0400316
317 IdentifyBuiltIns(shaderType, shaderSpec, resources, symbolTable);
318
319 return true;
alokp@chromium.org07620a52010-09-23 17:53:56 +0000320}
321
Shannon Woods2d76e5f2014-05-16 17:46:41 -0400322void TCompiler::setResourceString()
323{
324 std::ostringstream strstream;
325 strstream << ":MaxVertexAttribs:" << compileResources.MaxVertexAttribs
326 << ":MaxVertexUniformVectors:" << compileResources.MaxVertexUniformVectors
327 << ":MaxVaryingVectors:" << compileResources.MaxVaryingVectors
328 << ":MaxVertexTextureImageUnits:" << compileResources.MaxVertexTextureImageUnits
329 << ":MaxCombinedTextureImageUnits:" << compileResources.MaxCombinedTextureImageUnits
330 << ":MaxTextureImageUnits:" << compileResources.MaxTextureImageUnits
331 << ":MaxFragmentUniformVectors:" << compileResources.MaxFragmentUniformVectors
332 << ":MaxDrawBuffers:" << compileResources.MaxDrawBuffers
333 << ":OES_standard_derivatives:" << compileResources.OES_standard_derivatives
334 << ":OES_EGL_image_external:" << compileResources.OES_EGL_image_external
335 << ":ARB_texture_rectangle:" << compileResources.ARB_texture_rectangle
336 << ":EXT_draw_buffers:" << compileResources.EXT_draw_buffers
337 << ":FragmentPrecisionHigh:" << compileResources.FragmentPrecisionHigh
338 << ":MaxExpressionComplexity:" << compileResources.MaxExpressionComplexity
339 << ":MaxCallStackDepth:" << compileResources.MaxCallStackDepth
340 << ":EXT_frag_depth:" << compileResources.EXT_frag_depth
341 << ":EXT_shader_texture_lod:" << compileResources.EXT_shader_texture_lod
342 << ":MaxVertexOutputVectors:" << compileResources.MaxVertexOutputVectors
343 << ":MaxFragmentInputVectors:" << compileResources.MaxFragmentInputVectors
344 << ":MinProgramTexelOffset:" << compileResources.MinProgramTexelOffset
345 << ":MaxProgramTexelOffset:" << compileResources.MaxProgramTexelOffset;
346
347 builtInResourcesString = strstream.str();
348}
349
alokp@chromium.org07620a52010-09-23 17:53:56 +0000350void TCompiler::clearResults()
351{
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000352 arrayBoundsClamper.Cleanup();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000353 infoSink.info.erase();
354 infoSink.obj.erase();
355 infoSink.debug.erase();
356
357 attribs.clear();
358 uniforms.clear();
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400359 varyings.clear();
zmo@google.coma3b4ab42011-09-16 00:53:26 +0000360
361 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000362
363 nameMap.clear();
alokp@chromium.org07620a52010-09-23 17:53:56 +0000364}
365
Jamie Madilleb1a0102013-07-08 13:31:38 -0400366bool TCompiler::detectCallDepth(TIntermNode* root, TInfoSink& infoSink, bool limitCallStackDepth)
zmo@google.comb1762df2011-07-30 02:04:23 +0000367{
Jamie Madilleb1a0102013-07-08 13:31:38 -0400368 DetectCallDepth detect(infoSink, limitCallStackDepth, maxCallStackDepth);
zmo@google.comb1762df2011-07-30 02:04:23 +0000369 root->traverse(&detect);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800370 switch (detect.detectCallDepth())
371 {
372 case DetectCallDepth::kErrorNone:
373 return true;
374 case DetectCallDepth::kErrorMissingMain:
375 infoSink.info.prefix(EPrefixError);
376 infoSink.info << "Missing main()";
377 return false;
378 case DetectCallDepth::kErrorRecursion:
379 infoSink.info.prefix(EPrefixError);
380 infoSink.info << "Function recursion detected";
381 return false;
382 case DetectCallDepth::kErrorMaxDepthExceeded:
383 infoSink.info.prefix(EPrefixError);
384 infoSink.info << "Function call stack too deep";
385 return false;
386 default:
387 UNREACHABLE();
388 return false;
zmo@google.comb1762df2011-07-30 02:04:23 +0000389 }
390}
391
Jamie Madill05a80ce2013-06-20 11:55:49 -0400392bool TCompiler::validateOutputs(TIntermNode* root)
393{
394 ValidateOutputs validateOutputs(infoSink.info, compileResources.MaxDrawBuffers);
395 root->traverse(&validateOutputs);
396 return (validateOutputs.numErrors() == 0);
397}
398
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000399void TCompiler::rewriteCSSShader(TIntermNode* root)
400{
401 RenameFunction renamer("main(", "css_main(");
402 root->traverse(&renamer);
403}
404
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800405bool TCompiler::validateLimitations(TIntermNode* root)
406{
alokp@chromium.orgb59a7782010-11-24 18:38:33 +0000407 ValidateLimitations validate(shaderType, infoSink.info);
408 root->traverse(&validate);
409 return validate.numErrors() == 0;
410}
411
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000412bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000413{
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800414 if (shaderSpec != SH_WEBGL_SPEC)
415 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000416 infoSink.info << "Timing restrictions must be enforced under the WebGL spec.";
417 return false;
418 }
419
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800420 if (shaderType == SH_FRAGMENT_SHADER)
421 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000422 TDependencyGraph graph(root);
423
424 // Output any errors first.
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000425 bool success = enforceFragmentShaderTimingRestrictions(graph);
Jamie Madill5508f392014-02-20 13:31:36 -0500426
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000427 // Then, output the dependency graph.
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800428 if (outputGraph)
429 {
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000430 TDependencyGraphOutput output(infoSink.info);
431 output.outputAllSpanningTrees(graph);
432 }
Jamie Madill5508f392014-02-20 13:31:36 -0500433
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000434 return success;
435 }
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800436 else
437 {
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000438 return enforceVertexShaderTimingRestrictions(root);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000439 }
440}
441
Jamie Madilleb1a0102013-07-08 13:31:38 -0400442bool TCompiler::limitExpressionComplexity(TIntermNode* root)
443{
Jamie Madill6654bc92014-03-26 14:01:57 -0400444 TMaxDepthTraverser traverser(maxExpressionComplexity+1);
Jamie Madilleb1a0102013-07-08 13:31:38 -0400445 root->traverse(&traverser);
Jamie Madill6654bc92014-03-26 14:01:57 -0400446
447 if (traverser.getMaxDepth() > maxExpressionComplexity)
448 {
449 infoSink.info << "Expression too complex.";
450 return false;
451 }
452
Jamie Madilleb1a0102013-07-08 13:31:38 -0400453 TDependencyGraph graph(root);
454
455 for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls();
456 iter != graph.endUserDefinedFunctionCalls();
457 ++iter)
458 {
459 TGraphFunctionCall* samplerSymbol = *iter;
460 TDependencyGraphTraverser graphTraverser;
461 samplerSymbol->traverse(&graphTraverser);
462 }
463
Jamie Madilleb1a0102013-07-08 13:31:38 -0400464 return true;
465}
466
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000467bool TCompiler::enforceFragmentShaderTimingRestrictions(const TDependencyGraph& graph)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000468{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000469 RestrictFragmentShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000470 restrictor.enforceRestrictions(graph);
471 return restrictor.numErrors() == 0;
472}
473
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000474bool TCompiler::enforceVertexShaderTimingRestrictions(TIntermNode* root)
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000475{
maxvujovic@gmail.com77222c92012-06-04 21:06:05 +0000476 RestrictVertexShaderTiming restrictor(infoSink.info);
maxvujovic@gmail.com66ebd012012-05-30 22:18:11 +0000477 restrictor.enforceRestrictions(root);
478 return restrictor.numErrors() == 0;
479}
480
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400481void TCompiler::collectVariables(TIntermNode* root)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000482{
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400483 CollectVariables collect(attribs, uniforms, varyings, hashFunction);
alokp@chromium.org07620a52010-09-23 17:53:56 +0000484 root->traverse(&collect);
485}
zmo@google.comfd747b82011-04-23 01:30:07 +0000486
gman@chromium.org8d804792012-10-17 21:33:48 +0000487bool TCompiler::enforcePackingRestrictions()
488{
489 VariablePacker packer;
490 return packer.CheckVariablesWithinPackingLimits(maxUniformVectors, uniforms);
491}
492
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800493void TCompiler::initializeGLPosition(TIntermNode* root)
494{
495 InitializeVariables::InitVariableInfoList variables;
496 InitializeVariables::InitVariableInfo var(
497 "gl_Position", TType(EbtFloat, EbpUndefined, EvqPosition, 4));
498 variables.push_back(var);
499 InitializeVariables initializer(variables);
500 root->traverse(&initializer);
501}
502
503void TCompiler::initializeVaryingsWithoutStaticUse(TIntermNode* root)
504{
505 InitializeVariables::InitVariableInfoList variables;
506 for (size_t ii = 0; ii < varyings.size(); ++ii)
507 {
508 const TVariableInfo& varying = varyings[ii];
509 if (varying.staticUse)
510 continue;
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700511 unsigned char primarySize = 1, secondarySize = 1;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800512 switch (varying.type)
513 {
514 case SH_FLOAT:
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800515 break;
516 case SH_FLOAT_VEC2:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700517 primarySize = 2;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800518 break;
519 case SH_FLOAT_VEC3:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700520 primarySize = 3;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800521 break;
522 case SH_FLOAT_VEC4:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700523 primarySize = 4;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800524 break;
525 case SH_FLOAT_MAT2:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700526 primarySize = 2;
527 secondarySize = 2;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800528 break;
529 case SH_FLOAT_MAT3:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700530 primarySize = 3;
531 secondarySize = 3;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800532 break;
533 case SH_FLOAT_MAT4:
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700534 primarySize = 4;
535 secondarySize = 4;
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800536 break;
537 default:
538 ASSERT(false);
539 }
Zhenyao Mo66e5dda2014-04-28 11:13:25 -0700540 TType type(EbtFloat, EbpUndefined, EvqVaryingOut, primarySize, secondarySize, varying.isArray);
Zhenyao Mo4a667fe2014-02-11 12:35:01 -0800541 TString name = varying.name.c_str();
542 if (varying.isArray)
543 {
544 type.setArraySize(varying.size);
545 name = name.substr(0, name.find_first_of('['));
546 }
547
548 InitializeVariables::InitVariableInfo var(name, type);
549 variables.push_back(var);
550 }
551 InitializeVariables initializer(variables);
552 root->traverse(&initializer);
553}
554
zmo@google.com5601ea02011-06-10 18:23:25 +0000555const TExtensionBehavior& TCompiler::getExtensionBehavior() const
556{
557 return extensionBehavior;
558}
zmo@google.com32e97312011-08-24 01:03:11 +0000559
shannon.woods%transgaming.com@gtempaccount.com18b4c4b2013-04-13 03:31:40 +0000560const ShBuiltInResources& TCompiler::getResources() const
561{
562 return compileResources;
563}
564
daniel@transgaming.com4167cc92013-01-11 04:11:53 +0000565const ArrayBoundsClamper& TCompiler::getArrayBoundsClamper() const
566{
567 return arrayBoundsClamper;
568}
569
shannon.woods@transgaming.com1d432bb2013-01-25 21:57:28 +0000570ShArrayIndexClampingStrategy TCompiler::getArrayIndexClampingStrategy() const
571{
572 return clampingStrategy;
573}
574
575const BuiltInFunctionEmulator& TCompiler::getBuiltInFunctionEmulator() const
576{
577 return builtInFunctionEmulator;
578}